OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [scsi/] [scsi.c] - Diff between revs 1765 and 1782

Only display areas with differences | Details | Blame | View Log

Rev 1765 Rev 1782
/*
/*
 *  scsi.c Copyright (C) 1992 Drew Eckhardt
 *  scsi.c Copyright (C) 1992 Drew Eckhardt
 *         Copyright (C) 1993, 1994, 1995 Eric Youngdale
 *         Copyright (C) 1993, 1994, 1995 Eric Youngdale
 *
 *
 *  generic mid-level SCSI driver
 *  generic mid-level SCSI driver
 *      Initial versions: Drew Eckhardt
 *      Initial versions: Drew Eckhardt
 *      Subsequent revisions: Eric Youngdale
 *      Subsequent revisions: Eric Youngdale
 *
 *
 *  <drew@colorado.edu>
 *  <drew@colorado.edu>
 *
 *
 *  Bug correction thanks go to :
 *  Bug correction thanks go to :
 *      Rik Faith <faith@cs.unc.edu>
 *      Rik Faith <faith@cs.unc.edu>
 *      Tommy Thorn <tthorn>
 *      Tommy Thorn <tthorn>
 *      Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
 *      Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
 *
 *
 *  Modified by Eric Youngdale eric@aib.com to
 *  Modified by Eric Youngdale eric@aib.com to
 *  add scatter-gather, multiple outstanding request, and other
 *  add scatter-gather, multiple outstanding request, and other
 *  enhancements.
 *  enhancements.
 *
 *
 *  Native multichannel, wide scsi, /proc/scsi and hot plugging
 *  Native multichannel, wide scsi, /proc/scsi and hot plugging
 *  support added by Michael Neuffer <mike@i-connect.net>
 *  support added by Michael Neuffer <mike@i-connect.net>
 *
 *
 *  Added request_module("scsi_hostadapter") for kerneld:
 *  Added request_module("scsi_hostadapter") for kerneld:
 *  (Put an "alias scsi_hostadapter your_hostadapter" in /etc/conf.modules)
 *  (Put an "alias scsi_hostadapter your_hostadapter" in /etc/conf.modules)
 *  Bjorn Ekwall  <bj0rn@blox.se>
 *  Bjorn Ekwall  <bj0rn@blox.se>
 *
 *
 *  Major improvements to the timeout, abort, and reset processing,
 *  Major improvements to the timeout, abort, and reset processing,
 *  as well as performance modifications for large queue depths by
 *  as well as performance modifications for large queue depths by
 *  Leonard N. Zubkoff <lnz@dandelion.com>
 *  Leonard N. Zubkoff <lnz@dandelion.com>
 *
 *
 *  Corrected scsi_done when command fails - now returns command that failed
 *  Corrected scsi_done when command fails - now returns command that failed
 *  in SCpnt->cmnd (for sd.c).
 *  in SCpnt->cmnd (for sd.c).
 *  Russell King <rmk@ecs.soton.ac.uk>
 *  Russell King <rmk@ecs.soton.ac.uk>
 */
 */
 
 
/*
/*
 * Don't import our own symbols, as this would severely mess up our
 * Don't import our own symbols, as this would severely mess up our
 * symbol tables.
 * symbol tables.
 */
 */
#define _SCSI_SYMS_VER_
#define _SCSI_SYMS_VER_
 
 
#include <linux/config.h>
#include <linux/config.h>
#include <linux/module.h>
#include <linux/module.h>
 
 
#include <linux/sched.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/string.h>
#include <linux/malloc.h>
#include <linux/malloc.h>
#include <linux/ioport.h>
#include <linux/ioport.h>
#include <linux/kernel.h>
#include <linux/kernel.h>
#include <linux/stat.h>
#include <linux/stat.h>
#include <linux/blk.h>
#include <linux/blk.h>
#include <linux/interrupt.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/delay.h>
 
 
#include <asm/system.h>
#include <asm/system.h>
#include <asm/irq.h>
#include <asm/irq.h>
#include <asm/dma.h>
#include <asm/dma.h>
 
 
#include "scsi.h"
#include "scsi.h"
#include "hosts.h"
#include "hosts.h"
#include "constants.h"
#include "constants.h"
 
 
#ifdef CONFIG_KERNELD
#ifdef CONFIG_KERNELD
#include <linux/kerneld.h>
#include <linux/kerneld.h>
#endif
#endif
 
 
#undef USE_STATIC_SCSI_MEMORY
#undef USE_STATIC_SCSI_MEMORY
 
 
/*
/*
static const char RCSid[] = "$Header: /home/marcus/revision_ctrl_test/oc_cvs/cvs/or1k/rc203soc/sw/uClinux/arch/armnommu/drivers/scsi/scsi.c,v 1.1 2005-12-20 09:33:29 jcastillo Exp $";
static const char RCSid[] = "$Header: /home/marcus/revision_ctrl_test/oc_cvs/cvs/or1k/rc203soc/sw/uClinux/arch/armnommu/drivers/scsi/scsi.c,v 1.1 2005-12-20 09:33:29 jcastillo Exp $";
*/
*/
 
 
 
 
/* Command groups 3 and 4 are reserved and should never be used.  */
/* Command groups 3 and 4 are reserved and should never be used.  */
const unsigned char scsi_command_size[8] = { 6, 10, 10, 12, 12, 12, 10, 10 };
const unsigned char scsi_command_size[8] = { 6, 10, 10, 12, 12, 12, 10, 10 };
 
 
#define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))
#define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))
 
 
/*
/*
 * PAGE_SIZE must be a multiple of the sector size (512).  True
 * PAGE_SIZE must be a multiple of the sector size (512).  True
 * for all reasonably recent architectures (even the VAX...).
 * for all reasonably recent architectures (even the VAX...).
 */
 */
#define SECTOR_SIZE             512
#define SECTOR_SIZE             512
#define SECTORS_PER_PAGE        (PAGE_SIZE/SECTOR_SIZE)
#define SECTORS_PER_PAGE        (PAGE_SIZE/SECTOR_SIZE)
 
 
#if SECTORS_PER_PAGE <= 8
#if SECTORS_PER_PAGE <= 8
 typedef unsigned char  FreeSectorBitmap;
 typedef unsigned char  FreeSectorBitmap;
#elif SECTORS_PER_PAGE <= 32
#elif SECTORS_PER_PAGE <= 32
 typedef unsigned int   FreeSectorBitmap;
 typedef unsigned int   FreeSectorBitmap;
#elif SECTORS_PER_PAGE <= 64
#elif SECTORS_PER_PAGE <= 64
#if 0
#if 0
 typedef unsigned long long FreeSectorBitmap;
 typedef unsigned long long FreeSectorBitmap;
#else
#else
 typedef struct {
 typedef struct {
        unsigned long l,h;
        unsigned long l,h;
 } FreeSectorBitmap;
 } FreeSectorBitmap;
#define LARGE_MALLOC
#define LARGE_MALLOC
#endif
#endif
#else
#else
# error You lose.
# error You lose.
#endif
#endif
 
 
static void scsi_done (Scsi_Cmnd *SCpnt);
static void scsi_done (Scsi_Cmnd *SCpnt);
static int update_timeout (Scsi_Cmnd *, int);
static int update_timeout (Scsi_Cmnd *, int);
static void print_inquiry(unsigned char *data);
static void print_inquiry(unsigned char *data);
static void scsi_times_out (Scsi_Cmnd * SCpnt);
static void scsi_times_out (Scsi_Cmnd * SCpnt);
static int scan_scsis_single (int channel,int dev,int lun,int * max_scsi_dev ,
static int scan_scsis_single (int channel,int dev,int lun,int * max_scsi_dev ,
                 int * sparse_lun, Scsi_Device ** SDpnt, Scsi_Cmnd * SCpnt,
                 int * sparse_lun, Scsi_Device ** SDpnt, Scsi_Cmnd * SCpnt,
                 struct Scsi_Host *shpnt, char * scsi_result);
                 struct Scsi_Host *shpnt, char * scsi_result);
void scsi_build_commandblocks(Scsi_Device * SDpnt);
void scsi_build_commandblocks(Scsi_Device * SDpnt);
 
 
#ifdef CONFIG_MODULES
#ifdef CONFIG_MODULES
extern struct symbol_table scsi_symbol_table;
extern struct symbol_table scsi_symbol_table;
#endif
#endif
 
 
static FreeSectorBitmap * dma_malloc_freelist = NULL;
static FreeSectorBitmap * dma_malloc_freelist = NULL;
static int scsi_need_isa_bounce_buffers;
static int scsi_need_isa_bounce_buffers;
static unsigned int dma_sectors = 0;
static unsigned int dma_sectors = 0;
unsigned int dma_free_sectors = 0;
unsigned int dma_free_sectors = 0;
unsigned int need_isa_buffer = 0;
unsigned int need_isa_buffer = 0;
static unsigned char ** dma_malloc_pages = NULL;
static unsigned char ** dma_malloc_pages = NULL;
 
 
static int time_start;
static int time_start;
static int time_elapsed;
static int time_elapsed;
static volatile struct Scsi_Host * host_active = NULL;
static volatile struct Scsi_Host * host_active = NULL;
#define SCSI_BLOCK(HOST) ((HOST->block && host_active && HOST != host_active) \
#define SCSI_BLOCK(HOST) ((HOST->block && host_active && HOST != host_active) \
                          || (HOST->can_queue && HOST->host_busy >= HOST->can_queue))
                          || (HOST->can_queue && HOST->host_busy >= HOST->can_queue))
 
 
const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
{
{
    "Direct-Access    ",
    "Direct-Access    ",
    "Sequential-Access",
    "Sequential-Access",
    "Printer          ",
    "Printer          ",
    "Processor        ",
    "Processor        ",
    "WORM             ",
    "WORM             ",
    "CD-ROM           ",
    "CD-ROM           ",
    "Scanner          ",
    "Scanner          ",
    "Optical Device   ",
    "Optical Device   ",
    "Medium Changer   ",
    "Medium Changer   ",
    "Communications   "
    "Communications   "
};
};
 
 
 
 
/*
/*
 * global variables :
 * global variables :
 * scsi_devices an array of these specifying the address for each
 * scsi_devices an array of these specifying the address for each
 * (host, id, LUN)
 * (host, id, LUN)
 */
 */
 
 
Scsi_Device * scsi_devices = NULL;
Scsi_Device * scsi_devices = NULL;
 
 
/* Process ID of SCSI commands */
/* Process ID of SCSI commands */
unsigned long scsi_pid = 0;
unsigned long scsi_pid = 0;
 
 
static unsigned long serial_number = 0;
static unsigned long serial_number = 0;
 
 
static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};
static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};
static void resize_dma_pool(void);
static void resize_dma_pool(void);
 
 
/* This variable is merely a hook so that we can debug the kernel with gdb. */
/* This variable is merely a hook so that we can debug the kernel with gdb. */
Scsi_Cmnd * last_cmnd = NULL;
Scsi_Cmnd * last_cmnd = NULL;
 
 
/* This is the pointer to the /proc/scsi code.
/* This is the pointer to the /proc/scsi code.
 * It is only initialized to !=0 if the scsi code is present
 * It is only initialized to !=0 if the scsi code is present
 */
 */
#ifdef CONFIG_PROC_FS
#ifdef CONFIG_PROC_FS
extern int (* dispatch_scsi_info_ptr)(int ino, char *buffer, char **start,
extern int (* dispatch_scsi_info_ptr)(int ino, char *buffer, char **start,
                                      off_t offset, int length, int inout);
                                      off_t offset, int length, int inout);
extern int dispatch_scsi_info(int ino, char *buffer, char **start,
extern int dispatch_scsi_info(int ino, char *buffer, char **start,
                              off_t offset, int length, int inout);
                              off_t offset, int length, int inout);
 
 
struct proc_dir_entry proc_scsi_scsi = {
struct proc_dir_entry proc_scsi_scsi = {
    PROC_SCSI_SCSI, 4, "scsi",
    PROC_SCSI_SCSI, 4, "scsi",
    S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0, 0,
    S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0, 0,
    NULL,
    NULL,
    NULL, NULL,
    NULL, NULL,
    NULL, NULL, NULL
    NULL, NULL, NULL
};
};
#endif
#endif
 
 
/*
/*
 *  This is the number  of clock ticks we should wait before we time out
 *  This is the number  of clock ticks we should wait before we time out
 *  and abort the command.  This is for  where the scsi.c module generates
 *  and abort the command.  This is for  where the scsi.c module generates
 *  the command, not where it originates from a higher level, in which
 *  the command, not where it originates from a higher level, in which
 *  case the timeout is specified there.
 *  case the timeout is specified there.
 *
 *
 *  ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
 *  ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
 *  respectively.
 *  respectively.
 */
 */
 
 
#ifdef DEBUG_TIMEOUT
#ifdef DEBUG_TIMEOUT
static void scsi_dump_status(void);
static void scsi_dump_status(void);
#endif
#endif
 
 
 
 
#ifdef DEBUG
#ifdef DEBUG
    #define SCSI_TIMEOUT (5*HZ)
    #define SCSI_TIMEOUT (5*HZ)
#else
#else
    #define SCSI_TIMEOUT (2*HZ)
    #define SCSI_TIMEOUT (2*HZ)
#endif
#endif
 
 
#ifdef DEBUG
#ifdef DEBUG
    #define SENSE_TIMEOUT SCSI_TIMEOUT
    #define SENSE_TIMEOUT SCSI_TIMEOUT
    #define ABORT_TIMEOUT SCSI_TIMEOUT
    #define ABORT_TIMEOUT SCSI_TIMEOUT
    #define RESET_TIMEOUT SCSI_TIMEOUT
    #define RESET_TIMEOUT SCSI_TIMEOUT
#else
#else
    #define SENSE_TIMEOUT (5*HZ/10)
    #define SENSE_TIMEOUT (5*HZ/10)
    #define RESET_TIMEOUT (5*HZ/10)
    #define RESET_TIMEOUT (5*HZ/10)
    #define ABORT_TIMEOUT (5*HZ/10)
    #define ABORT_TIMEOUT (5*HZ/10)
#endif
#endif
 
 
#define MIN_RESET_DELAY (2*HZ)
#define MIN_RESET_DELAY (2*HZ)
 
 
/* Do not call reset on error if we just did a reset within 15 sec. */
/* Do not call reset on error if we just did a reset within 15 sec. */
#define MIN_RESET_PERIOD (15*HZ)
#define MIN_RESET_PERIOD (15*HZ)
 
 
/* The following devices are known not to tolerate a lun != 0 scan for
/* The following devices are known not to tolerate a lun != 0 scan for
 * one reason or another.  Some will respond to all luns, others will
 * one reason or another.  Some will respond to all luns, others will
 * lock up.
 * lock up.
 */
 */
 
 
#define BLIST_NOLUN     0x01
#define BLIST_NOLUN     0x01
#define BLIST_FORCELUN  0x02
#define BLIST_FORCELUN  0x02
#define BLIST_BORKEN    0x04
#define BLIST_BORKEN    0x04
#define BLIST_KEY       0x08
#define BLIST_KEY       0x08
#define BLIST_SINGLELUN 0x10
#define BLIST_SINGLELUN 0x10
#define BLIST_NOTQ      0x20
#define BLIST_NOTQ      0x20
#define BLIST_SPARSELUN 0x40
#define BLIST_SPARSELUN 0x40
#define BLIST_MAX5LUN   0x80
#define BLIST_MAX5LUN   0x80
 
 
struct dev_info{
struct dev_info{
    const char * vendor;
    const char * vendor;
    const char * model;
    const char * model;
    const char * revision; /* Latest revision known to be bad.  Not used yet */
    const char * revision; /* Latest revision known to be bad.  Not used yet */
    unsigned flags;
    unsigned flags;
};
};
 
 
/*
/*
 * This is what was previously known as the blacklist.  The concept
 * This is what was previously known as the blacklist.  The concept
 * has been expanded so that we can specify other types of things we
 * has been expanded so that we can specify other types of things we
 * need to be aware of.
 * need to be aware of.
 */
 */
static struct dev_info device_list[] =
static struct dev_info device_list[] =
{
{
{"CHINON","CD-ROM CDS-431","H42", BLIST_NOLUN}, /* Locks up if polled for lun != 0 */
{"CHINON","CD-ROM CDS-431","H42", BLIST_NOLUN}, /* Locks up if polled for lun != 0 */
{"CHINON","CD-ROM CDS-535","Q14", BLIST_NOLUN}, /* Locks up if polled for lun != 0 */
{"CHINON","CD-ROM CDS-535","Q14", BLIST_NOLUN}, /* Locks up if polled for lun != 0 */
{"DENON","DRD-25X","V", BLIST_NOLUN},           /* Locks up if probed for lun != 0 */
{"DENON","DRD-25X","V", BLIST_NOLUN},           /* Locks up if probed for lun != 0 */
{"HITACHI","DK312C","CM81", BLIST_NOLUN},       /* Responds to all lun - dtg */
{"HITACHI","DK312C","CM81", BLIST_NOLUN},       /* Responds to all lun - dtg */
{"HITACHI","DK314C","CR21" , BLIST_NOLUN},      /* responds to all lun */
{"HITACHI","DK314C","CR21" , BLIST_NOLUN},      /* responds to all lun */
{"IMS", "CDD521/10","2.06", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
{"IMS", "CDD521/10","2.06", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
{"MAXTOR","XT-3280","PR02", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
{"MAXTOR","XT-3280","PR02", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
{"MAXTOR","XT-4380S","B3C", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
{"MAXTOR","XT-4380S","B3C", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
{"MAXTOR","MXT-1240S","I1.2", BLIST_NOLUN},     /* Locks up when LUN>0 polled */
{"MAXTOR","MXT-1240S","I1.2", BLIST_NOLUN},     /* Locks up when LUN>0 polled */
{"MAXTOR","XT-4170S","B5A", BLIST_NOLUN},       /* Locks-up sometimes when LUN>0 polled. */
{"MAXTOR","XT-4170S","B5A", BLIST_NOLUN},       /* Locks-up sometimes when LUN>0 polled. */
{"MAXTOR","XT-8760S","B7B", BLIST_NOLUN},       /* guess what? */
{"MAXTOR","XT-8760S","B7B", BLIST_NOLUN},       /* guess what? */
{"MEDIAVIS","RENO CD-ROMX2A","2.03",BLIST_NOLUN},/*Responds to all lun */
{"MEDIAVIS","RENO CD-ROMX2A","2.03",BLIST_NOLUN},/*Responds to all lun */
{"MICROP", "4110", "*", BLIST_NOTQ},            /* Buggy Tagged Queuing */
{"MICROP", "4110", "*", BLIST_NOTQ},            /* Buggy Tagged Queuing */
{"NEC","CD-ROM DRIVE:841","1.0", BLIST_NOLUN},  /* Locks-up when LUN>0 polled. */
{"NEC","CD-ROM DRIVE:841","1.0", BLIST_NOLUN},  /* Locks-up when LUN>0 polled. */
{"RODIME","RO3000S","2.33", BLIST_NOLUN},       /* Locks up if polled for lun != 0 */
{"RODIME","RO3000S","2.33", BLIST_NOLUN},       /* Locks up if polled for lun != 0 */
{"SANYO", "CRD-250S", "1.20", BLIST_NOLUN},     /* causes failed REQUEST SENSE on lun 1
{"SANYO", "CRD-250S", "1.20", BLIST_NOLUN},     /* causes failed REQUEST SENSE on lun 1
                                                 * for aha152x controller, which causes
                                                 * for aha152x controller, which causes
                                                 * SCSI code to reset bus.*/
                                                 * SCSI code to reset bus.*/
{"SEAGATE", "ST157N", "\004|j", BLIST_NOLUN},   /* causes failed REQUEST SENSE on lun 1
{"SEAGATE", "ST157N", "\004|j", BLIST_NOLUN},   /* causes failed REQUEST SENSE on lun 1
                                                 * for aha152x controller, which causes
                                                 * for aha152x controller, which causes
                                                 * SCSI code to reset bus.*/
                                                 * SCSI code to reset bus.*/
{"SEAGATE", "ST296","921", BLIST_NOLUN},        /* Responds to all lun */
{"SEAGATE", "ST296","921", BLIST_NOLUN},        /* Responds to all lun */
{"SEAGATE","ST1581","6538",BLIST_NOLUN},        /* Responds to all lun */
{"SEAGATE","ST1581","6538",BLIST_NOLUN},        /* Responds to all lun */
{"SONY","CD-ROM CDU-541","4.3d", BLIST_NOLUN},
{"SONY","CD-ROM CDU-541","4.3d", BLIST_NOLUN},
{"SONY","CD-ROM CDU-55S","1.0i", BLIST_NOLUN},
{"SONY","CD-ROM CDU-55S","1.0i", BLIST_NOLUN},
{"SONY","CD-ROM CDU-561","1.7x", BLIST_NOLUN},
{"SONY","CD-ROM CDU-561","1.7x", BLIST_NOLUN},
{"TANDBERG","TDC 3600","U07", BLIST_NOLUN},     /* Locks up if polled for lun != 0 */
{"TANDBERG","TDC 3600","U07", BLIST_NOLUN},     /* Locks up if polled for lun != 0 */
{"TEAC","CD-ROM","1.06", BLIST_NOLUN},          /* causes failed REQUEST SENSE on lun 1
{"TEAC","CD-ROM","1.06", BLIST_NOLUN},          /* causes failed REQUEST SENSE on lun 1
                                                 * for seagate controller, which causes
                                                 * for seagate controller, which causes
                                                 * SCSI code to reset bus.*/
                                                 * SCSI code to reset bus.*/
{"TEXEL","CD-ROM","1.06", BLIST_NOLUN},         /* causes failed REQUEST SENSE on lun 1
{"TEXEL","CD-ROM","1.06", BLIST_NOLUN},         /* causes failed REQUEST SENSE on lun 1
                                                 * for seagate controller, which causes
                                                 * for seagate controller, which causes
                                                 * SCSI code to reset bus.*/
                                                 * SCSI code to reset bus.*/
{"QUANTUM","LPS525S","3110", BLIST_NOLUN},      /* Locks sometimes if polled for lun != 0 */
{"QUANTUM","LPS525S","3110", BLIST_NOLUN},      /* Locks sometimes if polled for lun != 0 */
{"QUANTUM","PD1225S","3110", BLIST_NOLUN},      /* Locks sometimes if polled for lun != 0 */
{"QUANTUM","PD1225S","3110", BLIST_NOLUN},      /* Locks sometimes if polled for lun != 0 */
{"MEDIAVIS","CDR-H93MV","1.31", BLIST_NOLUN},   /* Locks up if polled for lun != 0 */
{"MEDIAVIS","CDR-H93MV","1.31", BLIST_NOLUN},   /* Locks up if polled for lun != 0 */
{"SANKYO", "CP525","6.64", BLIST_NOLUN},        /* causes failed REQ SENSE, extra reset */
{"SANKYO", "CP525","6.64", BLIST_NOLUN},        /* causes failed REQ SENSE, extra reset */
{"HP", "C1750A", "3226", BLIST_NOLUN},          /* scanjet iic */
{"HP", "C1750A", "3226", BLIST_NOLUN},          /* scanjet iic */
{"HP", "C1790A", "", BLIST_NOLUN},              /* scanjet iip */
{"HP", "C1790A", "", BLIST_NOLUN},              /* scanjet iip */
{"HP", "C2500A", "", BLIST_NOLUN},              /* scanjet iicx */
{"HP", "C2500A", "", BLIST_NOLUN},              /* scanjet iicx */
 
 
/*
/*
 * Other types of devices that have special flags.
 * Other types of devices that have special flags.
 */
 */
{"SONY","CD-ROM CDU-8001","*", BLIST_BORKEN},
{"SONY","CD-ROM CDU-8001","*", BLIST_BORKEN},
{"TEXEL","CD-ROM","1.06", BLIST_BORKEN},
{"TEXEL","CD-ROM","1.06", BLIST_BORKEN},
{"IOMEGA","Io20S         *F","*", BLIST_KEY},
{"IOMEGA","Io20S         *F","*", BLIST_KEY},
{"INSITE","Floptical   F*8I","*", BLIST_KEY},
{"INSITE","Floptical   F*8I","*", BLIST_KEY},
{"INSITE","I325VM","*", BLIST_KEY},
{"INSITE","I325VM","*", BLIST_KEY},
{"NRC","MBR-7","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"NRC","MBR-7","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"NRC","MBR-7.4","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"NRC","MBR-7.4","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"REGAL","CDC-4X","*", BLIST_MAX5LUN | BLIST_SINGLELUN},
{"REGAL","CDC-4X","*", BLIST_MAX5LUN | BLIST_SINGLELUN},
{"NAKAMICH","MJ-4.8S","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"NAKAMICH","MJ-4.8S","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"PIONEER","CD-ROM DRM-600","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"PIONEER","CD-ROM DRM-600","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"PIONEER","CD-ROM DRM-602X","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"PIONEER","CD-ROM DRM-602X","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"PIONEER","CD-ROM DRM-604X","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"PIONEER","CD-ROM DRM-604X","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"EMULEX","MD21/S2     ESDI","*", BLIST_SINGLELUN},
{"EMULEX","MD21/S2     ESDI","*", BLIST_SINGLELUN},
{"CANON","IPUBJD","*", BLIST_SPARSELUN},
{"CANON","IPUBJD","*", BLIST_SPARSELUN},
{"MATSHITA","PD","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"MATSHITA","PD","*", BLIST_FORCELUN | BLIST_SINGLELUN},
{"YAMAHA","CDR100","1.00", BLIST_NOLUN},        /* Locks up if polled for lun != 0 */
{"YAMAHA","CDR100","1.00", BLIST_NOLUN},        /* Locks up if polled for lun != 0 */
{"YAMAHA","CDR102","1.00", BLIST_NOLUN},        /* Locks up if polled for lun != 0 */
{"YAMAHA","CDR102","1.00", BLIST_NOLUN},        /* Locks up if polled for lun != 0 */
{"nCipher","Fastness Crypto","*", BLIST_FORCELUN},
{"nCipher","Fastness Crypto","*", BLIST_FORCELUN},
/*
/*
 * Must be at end of list...
 * Must be at end of list...
 */
 */
{NULL, NULL, NULL}
{NULL, NULL, NULL}
};
};
 
 
static int get_device_flags(unsigned char * response_data){
static int get_device_flags(unsigned char * response_data){
    int i = 0;
    int i = 0;
    unsigned char * pnt;
    unsigned char * pnt;
    for(i=0; 1; i++){
    for(i=0; 1; i++){
        if(device_list[i].vendor == NULL) return 0;
        if(device_list[i].vendor == NULL) return 0;
        pnt = &response_data[8];
        pnt = &response_data[8];
        while(*pnt && *pnt == ' ') pnt++;
        while(*pnt && *pnt == ' ') pnt++;
        if(memcmp(device_list[i].vendor, pnt,
        if(memcmp(device_list[i].vendor, pnt,
                  strlen(device_list[i].vendor))) continue;
                  strlen(device_list[i].vendor))) continue;
        pnt = &response_data[16];
        pnt = &response_data[16];
        while(*pnt && *pnt == ' ') pnt++;
        while(*pnt && *pnt == ' ') pnt++;
        if(memcmp(device_list[i].model, pnt,
        if(memcmp(device_list[i].model, pnt,
                  strlen(device_list[i].model))) continue;
                  strlen(device_list[i].model))) continue;
        return device_list[i].flags;
        return device_list[i].flags;
    }
    }
    return 0;
    return 0;
}
}
 
 
void scsi_make_blocked_list(void)  {
void scsi_make_blocked_list(void)  {
    int block_count = 0, index;
    int block_count = 0, index;
    unsigned long flags;
    unsigned long flags;
    struct Scsi_Host * sh[128], * shpnt;
    struct Scsi_Host * sh[128], * shpnt;
 
 
    /*
    /*
     * Create a circular linked list from the scsi hosts which have
     * Create a circular linked list from the scsi hosts which have
     * the "wish_block" field in the Scsi_Host structure set.
     * the "wish_block" field in the Scsi_Host structure set.
     * The blocked list should include all the scsi hosts using ISA DMA.
     * The blocked list should include all the scsi hosts using ISA DMA.
     * In some systems, using two dma channels simultaneously causes
     * In some systems, using two dma channels simultaneously causes
     * unpredictable results.
     * unpredictable results.
     * Among the scsi hosts in the blocked list, only one host at a time
     * Among the scsi hosts in the blocked list, only one host at a time
     * is allowed to have active commands queued. The transition from
     * is allowed to have active commands queued. The transition from
     * one active host to the next one is allowed only when host_busy == 0
     * one active host to the next one is allowed only when host_busy == 0
     * for the active host (which implies host_busy == 0 for all the hosts
     * for the active host (which implies host_busy == 0 for all the hosts
     * in the list). Moreover for block devices the transition to a new
     * in the list). Moreover for block devices the transition to a new
     * active host is allowed only when a request is completed, since a
     * active host is allowed only when a request is completed, since a
     * block device request can be divided into multiple scsi commands
     * block device request can be divided into multiple scsi commands
     * (when there are few sg lists or clustering is disabled).
     * (when there are few sg lists or clustering is disabled).
     *
     *
     * (DB, 4 Feb 1995)
     * (DB, 4 Feb 1995)
     */
     */
 
 
    save_flags_cli(flags);
    save_flags_cli(flags);
    host_active = NULL;
    host_active = NULL;
 
 
    for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next) {
    for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next) {
 
 
#if 0
#if 0
        /*
        /*
         * Is this is a candidate for the blocked list?
         * Is this is a candidate for the blocked list?
         * Useful to put into the blocked list all the hosts whose driver
         * Useful to put into the blocked list all the hosts whose driver
         * does not know about the host->block feature.
         * does not know about the host->block feature.
         */
         */
        if (shpnt->unchecked_isa_dma) shpnt->wish_block = 1;
        if (shpnt->unchecked_isa_dma) shpnt->wish_block = 1;
#endif
#endif
 
 
        if (shpnt->wish_block) sh[block_count++] = shpnt;
        if (shpnt->wish_block) sh[block_count++] = shpnt;
    }
    }
 
 
    if (block_count == 1) sh[0]->block = NULL;
    if (block_count == 1) sh[0]->block = NULL;
 
 
    else if (block_count > 1) {
    else if (block_count > 1) {
 
 
        for(index = 0; index < block_count - 1; index++) {
        for(index = 0; index < block_count - 1; index++) {
            sh[index]->block = sh[index + 1];
            sh[index]->block = sh[index + 1];
            printk("scsi%d : added to blocked host list.\n",
            printk("scsi%d : added to blocked host list.\n",
                   sh[index]->host_no);
                   sh[index]->host_no);
        }
        }
 
 
        sh[block_count - 1]->block = sh[0];
        sh[block_count - 1]->block = sh[0];
        printk("scsi%d : added to blocked host list.\n",
        printk("scsi%d : added to blocked host list.\n",
               sh[index]->host_no);
               sh[index]->host_no);
    }
    }
 
 
    restore_flags(flags);
    restore_flags(flags);
}
}
 
 
static void scan_scsis_done (Scsi_Cmnd * SCpnt)
static void scan_scsis_done (Scsi_Cmnd * SCpnt)
{
{
 
 
#ifdef DEBUG
#ifdef DEBUG
    printk ("scan_scsis_done(%p, %06x)\n", SCpnt->host, SCpnt->result);
    printk ("scan_scsis_done(%p, %06x)\n", SCpnt->host, SCpnt->result);
#endif
#endif
    SCpnt->request.rq_status = RQ_SCSI_DONE;
    SCpnt->request.rq_status = RQ_SCSI_DONE;
 
 
    if (SCpnt->request.sem != NULL)
    if (SCpnt->request.sem != NULL)
        up(SCpnt->request.sem);
        up(SCpnt->request.sem);
}
}
 
 
#ifdef CONFIG_SCSI_MULTI_LUN
#ifdef CONFIG_SCSI_MULTI_LUN
static int max_scsi_luns = 8;
static int max_scsi_luns = 8;
#else
#else
static int max_scsi_luns = 1;
static int max_scsi_luns = 1;
#endif
#endif
 
 
void scsi_luns_setup(char *str, int *ints) {
void scsi_luns_setup(char *str, int *ints) {
    if (ints[0] != 1)
    if (ints[0] != 1)
        printk("scsi_luns_setup : usage max_scsi_luns=n (n should be between 1 and 8)\n");
        printk("scsi_luns_setup : usage max_scsi_luns=n (n should be between 1 and 8)\n");
    else
    else
        max_scsi_luns = ints[1];
        max_scsi_luns = ints[1];
}
}
 
 
/*
/*
 *  Detecting SCSI devices :
 *  Detecting SCSI devices :
 *  We scan all present host adapter's busses,  from ID 0 to ID (max_id).
 *  We scan all present host adapter's busses,  from ID 0 to ID (max_id).
 *  We use the INQUIRY command, determine device type, and pass the ID /
 *  We use the INQUIRY command, determine device type, and pass the ID /
 *  lun address of all sequential devices to the tape driver, all random
 *  lun address of all sequential devices to the tape driver, all random
 *  devices to the disk driver.
 *  devices to the disk driver.
 */
 */
static void scan_scsis (struct Scsi_Host *shpnt, unchar hardcoded,
static void scan_scsis (struct Scsi_Host *shpnt, unchar hardcoded,
                 unchar hchannel, unchar hid, unchar hlun)
                 unchar hchannel, unchar hid, unchar hlun)
{
{
  int dev, lun, channel;
  int dev, lun, channel;
  unsigned char scsi_result0[256];
  unsigned char scsi_result0[256];
  unsigned char *scsi_result;
  unsigned char *scsi_result;
  Scsi_Device *SDpnt;
  Scsi_Device *SDpnt;
  int max_dev_lun, sparse_lun;
  int max_dev_lun, sparse_lun;
  Scsi_Cmnd *SCpnt;
  Scsi_Cmnd *SCpnt;
 
 
  SCpnt = (Scsi_Cmnd *) scsi_init_malloc (sizeof (Scsi_Cmnd), GFP_ATOMIC | GFP_DMA);
  SCpnt = (Scsi_Cmnd *) scsi_init_malloc (sizeof (Scsi_Cmnd), GFP_ATOMIC | GFP_DMA);
  SDpnt = (Scsi_Device *) scsi_init_malloc (sizeof (Scsi_Device), GFP_ATOMIC);
  SDpnt = (Scsi_Device *) scsi_init_malloc (sizeof (Scsi_Device), GFP_ATOMIC);
  memset (SCpnt, 0, sizeof (Scsi_Cmnd));
  memset (SCpnt, 0, sizeof (Scsi_Cmnd));
 
 
 
 
  /* Make sure we have something that is valid for DMA purposes */
  /* Make sure we have something that is valid for DMA purposes */
  scsi_result = ( ( !shpnt->unchecked_isa_dma )
  scsi_result = ( ( !shpnt->unchecked_isa_dma )
                 ? &scsi_result0[0] : scsi_init_malloc (512, GFP_DMA));
                 ? &scsi_result0[0] : scsi_init_malloc (512, GFP_DMA));
 
 
  if (scsi_result == NULL) {
  if (scsi_result == NULL) {
    printk ("Unable to obtain scsi_result buffer\n");
    printk ("Unable to obtain scsi_result buffer\n");
    goto leave;
    goto leave;
  }
  }
 
 
  /* We must chain ourself in the host_queue, so commands can time out */
  /* We must chain ourself in the host_queue, so commands can time out */
  if(shpnt->host_queue)
  if(shpnt->host_queue)
      shpnt->host_queue->prev = SCpnt;
      shpnt->host_queue->prev = SCpnt;
  SCpnt->next = shpnt->host_queue;
  SCpnt->next = shpnt->host_queue;
  SCpnt->prev = NULL;
  SCpnt->prev = NULL;
  shpnt->host_queue = SCpnt;
  shpnt->host_queue = SCpnt;
 
 
 
 
  if (hardcoded == 1) {
  if (hardcoded == 1) {
    Scsi_Device *oldSDpnt=SDpnt;
    Scsi_Device *oldSDpnt=SDpnt;
    struct Scsi_Device_Template * sdtpnt;
    struct Scsi_Device_Template * sdtpnt;
    channel = hchannel;
    channel = hchannel;
    if(channel > shpnt->max_channel) goto leave;
    if(channel > shpnt->max_channel) goto leave;
    dev = hid;
    dev = hid;
    if(dev >= shpnt->max_id) goto leave;
    if(dev >= shpnt->max_id) goto leave;
    lun = hlun;
    lun = hlun;
    if(lun >= shpnt->max_lun) goto leave;
    if(lun >= shpnt->max_lun) goto leave;
    scan_scsis_single (channel, dev, lun, &max_dev_lun, &sparse_lun,
    scan_scsis_single (channel, dev, lun, &max_dev_lun, &sparse_lun,
                       &SDpnt, SCpnt, shpnt, scsi_result);
                       &SDpnt, SCpnt, shpnt, scsi_result);
    if(SDpnt!=oldSDpnt) {
    if(SDpnt!=oldSDpnt) {
 
 
        /* it could happen the blockdevice hasn't yet been inited */
        /* it could happen the blockdevice hasn't yet been inited */
    for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
    for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
        if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
        if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
 
 
            oldSDpnt->scsi_request_fn = NULL;
            oldSDpnt->scsi_request_fn = NULL;
            for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
            for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                if(sdtpnt->attach) {
                if(sdtpnt->attach) {
                  (*sdtpnt->attach)(oldSDpnt);
                  (*sdtpnt->attach)(oldSDpnt);
                  if(oldSDpnt->attached) scsi_build_commandblocks(oldSDpnt);}
                  if(oldSDpnt->attached) scsi_build_commandblocks(oldSDpnt);}
            resize_dma_pool();
            resize_dma_pool();
 
 
        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next) {
        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next) {
            if(sdtpnt->finish && sdtpnt->nr_dev)
            if(sdtpnt->finish && sdtpnt->nr_dev)
                {(*sdtpnt->finish)();}
                {(*sdtpnt->finish)();}
        }
        }
    }
    }
 
 
  }
  }
  else {
  else {
    for (channel = 0; channel <= shpnt->max_channel; channel++) {
    for (channel = 0; channel <= shpnt->max_channel; channel++) {
      for (dev = 0; dev < shpnt->max_id; ++dev) {
      for (dev = 0; dev < shpnt->max_id; ++dev) {
        if (shpnt->this_id != dev) {
        if (shpnt->this_id != dev) {
 
 
          /*
          /*
           * We need the for so our continue, etc. work fine. We put this in
           * We need the for so our continue, etc. work fine. We put this in
           * a variable so that we can override it during the scan if we
           * a variable so that we can override it during the scan if we
           * detect a device *KNOWN* to have multiple logical units.
           * detect a device *KNOWN* to have multiple logical units.
           */
           */
          max_dev_lun = (max_scsi_luns < shpnt->max_lun ?
          max_dev_lun = (max_scsi_luns < shpnt->max_lun ?
                         max_scsi_luns : shpnt->max_lun);
                         max_scsi_luns : shpnt->max_lun);
          sparse_lun = 0;
          sparse_lun = 0;
          for (lun = 0; lun < max_dev_lun; ++lun) {
          for (lun = 0; lun < max_dev_lun; ++lun) {
            if (!scan_scsis_single (channel, dev, lun, &max_dev_lun,
            if (!scan_scsis_single (channel, dev, lun, &max_dev_lun,
                                    &sparse_lun, &SDpnt, SCpnt, shpnt,
                                    &sparse_lun, &SDpnt, SCpnt, shpnt,
                                    scsi_result)
                                    scsi_result)
                && !sparse_lun)
                && !sparse_lun)
              break; /* break means don't probe further for luns!=0 */
              break; /* break means don't probe further for luns!=0 */
          }                     /* for lun ends */
          }                     /* for lun ends */
        }                       /* if this_id != id ends */
        }                       /* if this_id != id ends */
      }                         /* for dev ends */
      }                         /* for dev ends */
    }                           /* for channel ends */
    }                           /* for channel ends */
  }                             /* if/else hardcoded */
  }                             /* if/else hardcoded */
 
 
  leave:
  leave:
 
 
  {/* Unchain SCpnt from host_queue */
  {/* Unchain SCpnt from host_queue */
    Scsi_Cmnd *prev, *next, *hqptr;
    Scsi_Cmnd *prev, *next, *hqptr;
    for(hqptr = shpnt->host_queue; hqptr != SCpnt; hqptr = hqptr->next) ;
    for(hqptr = shpnt->host_queue; hqptr != SCpnt; hqptr = hqptr->next) ;
    if(hqptr) {
    if(hqptr) {
      prev = hqptr->prev;
      prev = hqptr->prev;
      next = hqptr->next;
      next = hqptr->next;
      if(prev)
      if(prev)
        prev->next = next;
        prev->next = next;
      else
      else
        shpnt->host_queue = next;
        shpnt->host_queue = next;
      if(next) next->prev = prev;
      if(next) next->prev = prev;
    }
    }
  }
  }
 
 
     /* Last device block does not exist.  Free memory. */
     /* Last device block does not exist.  Free memory. */
    if (SDpnt != NULL)
    if (SDpnt != NULL)
      scsi_init_free ((char *) SDpnt, sizeof (Scsi_Device));
      scsi_init_free ((char *) SDpnt, sizeof (Scsi_Device));
 
 
    if (SCpnt != NULL)
    if (SCpnt != NULL)
      scsi_init_free ((char *) SCpnt, sizeof (Scsi_Cmnd));
      scsi_init_free ((char *) SCpnt, sizeof (Scsi_Cmnd));
 
 
    /* If we allocated a buffer so we could do DMA, free it now */
    /* If we allocated a buffer so we could do DMA, free it now */
    if (scsi_result != &scsi_result0[0] && scsi_result != NULL)
    if (scsi_result != &scsi_result0[0] && scsi_result != NULL)
      scsi_init_free (scsi_result, 512);
      scsi_init_free (scsi_result, 512);
 
 
}
}
 
 
/*
/*
 * The worker for scan_scsis.
 * The worker for scan_scsis.
 * Returning 0 means Please don't ask further for lun!=0, 1 means OK go on.
 * Returning 0 means Please don't ask further for lun!=0, 1 means OK go on.
 * Global variables used : scsi_devices(linked list)
 * Global variables used : scsi_devices(linked list)
 */
 */
int scan_scsis_single (int channel, int dev, int lun, int *max_dev_lun,
int scan_scsis_single (int channel, int dev, int lun, int *max_dev_lun,
    int *sparse_lun, Scsi_Device **SDpnt2, Scsi_Cmnd * SCpnt,
    int *sparse_lun, Scsi_Device **SDpnt2, Scsi_Cmnd * SCpnt,
    struct Scsi_Host * shpnt, char *scsi_result)
    struct Scsi_Host * shpnt, char *scsi_result)
{
{
  unsigned char scsi_cmd[12];
  unsigned char scsi_cmd[12];
  struct Scsi_Device_Template *sdtpnt;
  struct Scsi_Device_Template *sdtpnt;
  Scsi_Device * SDtail, *SDpnt=*SDpnt2;
  Scsi_Device * SDtail, *SDpnt=*SDpnt2;
  int bflags, type=-1;
  int bflags, type=-1;
 
 
  SDtail = scsi_devices;
  SDtail = scsi_devices;
  if (scsi_devices)
  if (scsi_devices)
    while (SDtail->next)
    while (SDtail->next)
      SDtail = SDtail->next;
      SDtail = SDtail->next;
 
 
  memset (SDpnt, 0, sizeof (Scsi_Device));
  memset (SDpnt, 0, sizeof (Scsi_Device));
  SDpnt->host = shpnt;
  SDpnt->host = shpnt;
  SDpnt->id = dev;
  SDpnt->id = dev;
  SDpnt->lun = lun;
  SDpnt->lun = lun;
  SDpnt->channel = channel;
  SDpnt->channel = channel;
 
 
  /* Some low level driver could use device->type (DB) */
  /* Some low level driver could use device->type (DB) */
  SDpnt->type = -1;
  SDpnt->type = -1;
 
 
  /*
  /*
   * Assume that the device will have handshaking problems, and then fix this
   * Assume that the device will have handshaking problems, and then fix this
   * field later if it turns out it doesn't
   * field later if it turns out it doesn't
   */
   */
  SDpnt->borken = 1;
  SDpnt->borken = 1;
  SDpnt->was_reset = 0;
  SDpnt->was_reset = 0;
  SDpnt->expecting_cc_ua = 0;
  SDpnt->expecting_cc_ua = 0;
 
 
  scsi_cmd[0] = TEST_UNIT_READY;
  scsi_cmd[0] = TEST_UNIT_READY;
  scsi_cmd[1] = lun << 5;
  scsi_cmd[1] = lun << 5;
  scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[4] = scsi_cmd[5] = 0;
  scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[4] = scsi_cmd[5] = 0;
 
 
  SCpnt->host = SDpnt->host;
  SCpnt->host = SDpnt->host;
  SCpnt->device = SDpnt;
  SCpnt->device = SDpnt;
  SCpnt->target = SDpnt->id;
  SCpnt->target = SDpnt->id;
  SCpnt->lun = SDpnt->lun;
  SCpnt->lun = SDpnt->lun;
  SCpnt->channel = SDpnt->channel;
  SCpnt->channel = SDpnt->channel;
  {
  {
    struct semaphore sem = MUTEX_LOCKED;
    struct semaphore sem = MUTEX_LOCKED;
    SCpnt->request.sem = &sem;
    SCpnt->request.sem = &sem;
    SCpnt->request.rq_status = RQ_SCSI_BUSY;
    SCpnt->request.rq_status = RQ_SCSI_BUSY;
    scsi_do_cmd (SCpnt, (void *) scsi_cmd,
    scsi_do_cmd (SCpnt, (void *) scsi_cmd,
                 (void *) scsi_result,
                 (void *) scsi_result,
                 256, scan_scsis_done, SCSI_TIMEOUT + 4 * HZ, 5);
                 256, scan_scsis_done, SCSI_TIMEOUT + 4 * HZ, 5);
    down (&sem);
    down (&sem);
  }
  }
 
 
#if defined(DEBUG) || defined(DEBUG_INIT)
#if defined(DEBUG) || defined(DEBUG_INIT)
  printk ("scsi: scan_scsis_single id %d lun %d. Return code 0x%08x\n",
  printk ("scsi: scan_scsis_single id %d lun %d. Return code 0x%08x\n",
          dev, lun, SCpnt->result);
          dev, lun, SCpnt->result);
  print_driverbyte(SCpnt->result); print_hostbyte(SCpnt->result);
  print_driverbyte(SCpnt->result); print_hostbyte(SCpnt->result);
  printk("\n");
  printk("\n");
#endif
#endif
 
 
  if (SCpnt->result) {
  if (SCpnt->result) {
    if (((driver_byte (SCpnt->result) & DRIVER_SENSE) ||
    if (((driver_byte (SCpnt->result) & DRIVER_SENSE) ||
         (status_byte (SCpnt->result) & CHECK_CONDITION)) &&
         (status_byte (SCpnt->result) & CHECK_CONDITION)) &&
        ((SCpnt->sense_buffer[0] & 0x70) >> 4) == 7) {
        ((SCpnt->sense_buffer[0] & 0x70) >> 4) == 7) {
      if (((SCpnt->sense_buffer[2] & 0xf) != NOT_READY) &&
      if (((SCpnt->sense_buffer[2] & 0xf) != NOT_READY) &&
          ((SCpnt->sense_buffer[2] & 0xf) != UNIT_ATTENTION) &&
          ((SCpnt->sense_buffer[2] & 0xf) != UNIT_ATTENTION) &&
          ((SCpnt->sense_buffer[2] & 0xf) != ILLEGAL_REQUEST || lun > 0))
          ((SCpnt->sense_buffer[2] & 0xf) != ILLEGAL_REQUEST || lun > 0))
        return 1;
        return 1;
    }
    }
    else
    else
      return 0;
      return 0;
  }
  }
 
 
#if defined (DEBUG) || defined(DEBUG_INIT)
#if defined (DEBUG) || defined(DEBUG_INIT)
  printk ("scsi: performing INQUIRY\n");
  printk ("scsi: performing INQUIRY\n");
#endif
#endif
  /*
  /*
   * Build an INQUIRY command block.
   * Build an INQUIRY command block.
   */
   */
  scsi_cmd[0] = INQUIRY;
  scsi_cmd[0] = INQUIRY;
  scsi_cmd[1] = (lun << 5) & 0xe0;
  scsi_cmd[1] = (lun << 5) & 0xe0;
  scsi_cmd[2] = 0;
  scsi_cmd[2] = 0;
  scsi_cmd[3] = 0;
  scsi_cmd[3] = 0;
  scsi_cmd[4] = 255;
  scsi_cmd[4] = 255;
  scsi_cmd[5] = 0;
  scsi_cmd[5] = 0;
  SCpnt->cmd_len = 0;
  SCpnt->cmd_len = 0;
  {
  {
    struct semaphore sem = MUTEX_LOCKED;
    struct semaphore sem = MUTEX_LOCKED;
    SCpnt->request.sem = &sem;
    SCpnt->request.sem = &sem;
    SCpnt->request.rq_status = RQ_SCSI_BUSY;
    SCpnt->request.rq_status = RQ_SCSI_BUSY;
    scsi_do_cmd (SCpnt, (void *) scsi_cmd,
    scsi_do_cmd (SCpnt, (void *) scsi_cmd,
                 (void *) scsi_result,
                 (void *) scsi_result,
                 256, scan_scsis_done, SCSI_TIMEOUT, 3);
                 256, scan_scsis_done, SCSI_TIMEOUT, 3);
    down (&sem);
    down (&sem);
  }
  }
 
 
#if defined(DEBUG) || defined(DEBUG_INIT)
#if defined(DEBUG) || defined(DEBUG_INIT)
  printk ("scsi: INQUIRY %s with code 0x%x\n",
  printk ("scsi: INQUIRY %s with code 0x%x\n",
          SCpnt->result ? "failed" : "successful", SCpnt->result);
          SCpnt->result ? "failed" : "successful", SCpnt->result);
#endif
#endif
 
 
  if (SCpnt->result)
  if (SCpnt->result)
    return 0;     /* assume no peripheral if any sort of error */
    return 0;     /* assume no peripheral if any sort of error */
 
 
  /*
  /*
   * Check the peripheral qualifier field - this tells us whether LUNS
   * Check the peripheral qualifier field - this tells us whether LUNS
   * are supported here or not.
   * are supported here or not.
   */
   */
  if( (scsi_result[0] >> 5) == 3 )
  if( (scsi_result[0] >> 5) == 3 )
    {
    {
      return 0;     /* assume no peripheral if any sort of error */
      return 0;     /* assume no peripheral if any sort of error */
    }
    }
 
 
  /*
  /*
   * It would seem some TOSHIBA CDROM gets things wrong
   * It would seem some TOSHIBA CDROM gets things wrong
   */
   */
  if (!strncmp (scsi_result + 8, "TOSHIBA", 7) &&
  if (!strncmp (scsi_result + 8, "TOSHIBA", 7) &&
      !strncmp (scsi_result + 16, "CD-ROM", 6) &&
      !strncmp (scsi_result + 16, "CD-ROM", 6) &&
      scsi_result[0] == TYPE_DISK) {
      scsi_result[0] == TYPE_DISK) {
    scsi_result[0] = TYPE_ROM;
    scsi_result[0] = TYPE_ROM;
    scsi_result[1] |= 0x80;     /* removable */
    scsi_result[1] |= 0x80;     /* removable */
  }
  }
 
 
  if (!strncmp (scsi_result + 8, "NEC", 3)) {
  if (!strncmp (scsi_result + 8, "NEC", 3)) {
    if (!strncmp (scsi_result + 16, "CD-ROM DRIVE:84 ", 16) ||
    if (!strncmp (scsi_result + 16, "CD-ROM DRIVE:84 ", 16) ||
        !strncmp (scsi_result + 16, "CD-ROM DRIVE:25", 15))
        !strncmp (scsi_result + 16, "CD-ROM DRIVE:25", 15))
      SDpnt->manufacturer = SCSI_MAN_NEC_OLDCDR;
      SDpnt->manufacturer = SCSI_MAN_NEC_OLDCDR;
    else
    else
      SDpnt->manufacturer = SCSI_MAN_NEC;
      SDpnt->manufacturer = SCSI_MAN_NEC;
  }
  }
  else if (!strncmp (scsi_result + 8, "TOSHIBA", 7))
  else if (!strncmp (scsi_result + 8, "TOSHIBA", 7))
    SDpnt->manufacturer = SCSI_MAN_TOSHIBA;
    SDpnt->manufacturer = SCSI_MAN_TOSHIBA;
  else if (!strncmp (scsi_result + 8, "SONY", 4))
  else if (!strncmp (scsi_result + 8, "SONY", 4))
    SDpnt->manufacturer = SCSI_MAN_SONY;
    SDpnt->manufacturer = SCSI_MAN_SONY;
  else if (!strncmp (scsi_result + 8, "PIONEER", 7))
  else if (!strncmp (scsi_result + 8, "PIONEER", 7))
    SDpnt->manufacturer = SCSI_MAN_PIONEER;
    SDpnt->manufacturer = SCSI_MAN_PIONEER;
  else
  else
    SDpnt->manufacturer = SCSI_MAN_UNKNOWN;
    SDpnt->manufacturer = SCSI_MAN_UNKNOWN;
 
 
  memcpy (SDpnt->vendor, scsi_result + 8, 8);
  memcpy (SDpnt->vendor, scsi_result + 8, 8);
  memcpy (SDpnt->model, scsi_result + 16, 16);
  memcpy (SDpnt->model, scsi_result + 16, 16);
  memcpy (SDpnt->rev, scsi_result + 32, 4);
  memcpy (SDpnt->rev, scsi_result + 32, 4);
 
 
  SDpnt->removable = (0x80 & scsi_result[1]) >> 7;
  SDpnt->removable = (0x80 & scsi_result[1]) >> 7;
  SDpnt->lockable = SDpnt->removable;
  SDpnt->lockable = SDpnt->removable;
  SDpnt->changed = 0;
  SDpnt->changed = 0;
  SDpnt->access_count = 0;
  SDpnt->access_count = 0;
  SDpnt->busy = 0;
  SDpnt->busy = 0;
  SDpnt->has_cmdblocks = 0;
  SDpnt->has_cmdblocks = 0;
  /*
  /*
   * Currently, all sequential devices are assumed to be tapes, all random
   * Currently, all sequential devices are assumed to be tapes, all random
   * devices disk, with the appropriate read only flags set for ROM / WORM
   * devices disk, with the appropriate read only flags set for ROM / WORM
   * treated as RO.
   * treated as RO.
   */
   */
  switch (type = (scsi_result[0] & 0x1f)) {
  switch (type = (scsi_result[0] & 0x1f)) {
  case TYPE_TAPE:
  case TYPE_TAPE:
  case TYPE_DISK:
  case TYPE_DISK:
  case TYPE_MOD:
  case TYPE_MOD:
  case TYPE_PROCESSOR:
  case TYPE_PROCESSOR:
  case TYPE_SCANNER:
  case TYPE_SCANNER:
    SDpnt->writeable = 1;
    SDpnt->writeable = 1;
    break;
    break;
  case TYPE_WORM:
  case TYPE_WORM:
  case TYPE_ROM:
  case TYPE_ROM:
    SDpnt->writeable = 0;
    SDpnt->writeable = 0;
    break;
    break;
  default:
  default:
    printk ("scsi: unknown type %d\n", type);
    printk ("scsi: unknown type %d\n", type);
  }
  }
 
 
  SDpnt->single_lun = 0;
  SDpnt->single_lun = 0;
  SDpnt->soft_reset =
  SDpnt->soft_reset =
    (scsi_result[7] & 1) && ((scsi_result[3] & 7) == 2);
    (scsi_result[7] & 1) && ((scsi_result[3] & 7) == 2);
  SDpnt->random = (type == TYPE_TAPE) ? 0 : 1;
  SDpnt->random = (type == TYPE_TAPE) ? 0 : 1;
  SDpnt->type = (type & 0x1f);
  SDpnt->type = (type & 0x1f);
 
 
  print_inquiry (scsi_result);
  print_inquiry (scsi_result);
 
 
  for (sdtpnt = scsi_devicelist; sdtpnt;
  for (sdtpnt = scsi_devicelist; sdtpnt;
       sdtpnt = sdtpnt->next)
       sdtpnt = sdtpnt->next)
    if (sdtpnt->detect)
    if (sdtpnt->detect)
      SDpnt->attached +=
      SDpnt->attached +=
        (*sdtpnt->detect) (SDpnt);
        (*sdtpnt->detect) (SDpnt);
 
 
  SDpnt->scsi_level = scsi_result[2] & 0x07;
  SDpnt->scsi_level = scsi_result[2] & 0x07;
  if (SDpnt->scsi_level >= 2 ||
  if (SDpnt->scsi_level >= 2 ||
      (SDpnt->scsi_level == 1 &&
      (SDpnt->scsi_level == 1 &&
       (scsi_result[3] & 0x0f) == 1))
       (scsi_result[3] & 0x0f) == 1))
    SDpnt->scsi_level++;
    SDpnt->scsi_level++;
 
 
  /*
  /*
   * Accommodate drivers that want to sleep when they should be in a polling
   * Accommodate drivers that want to sleep when they should be in a polling
   * loop.
   * loop.
   */
   */
  SDpnt->disconnect = 0;
  SDpnt->disconnect = 0;
 
 
  /*
  /*
   * Get any flags for this device.
   * Get any flags for this device.
   */
   */
  bflags = get_device_flags (scsi_result);
  bflags = get_device_flags (scsi_result);
 
 
  /*
  /*
   * Set the tagged_queue flag for SCSI-II devices that purport to support
   * Set the tagged_queue flag for SCSI-II devices that purport to support
   * tagged queuing in the INQUIRY data.
   * tagged queuing in the INQUIRY data.
   */
   */
  SDpnt->tagged_queue = 0;
  SDpnt->tagged_queue = 0;
  if ((SDpnt->scsi_level >= SCSI_2) &&
  if ((SDpnt->scsi_level >= SCSI_2) &&
      (scsi_result[7] & 2) &&
      (scsi_result[7] & 2) &&
      !(bflags & BLIST_NOTQ)) {
      !(bflags & BLIST_NOTQ)) {
    SDpnt->tagged_supported = 1;
    SDpnt->tagged_supported = 1;
    SDpnt->current_tag = 0;
    SDpnt->current_tag = 0;
  }
  }
 
 
  /*
  /*
   * Some revisions of the Texel CD ROM drives have handshaking problems when
   * Some revisions of the Texel CD ROM drives have handshaking problems when
   * used with the Seagate controllers.  Before we know what type of device
   * used with the Seagate controllers.  Before we know what type of device
   * we're talking to, we assume it's borken and then change it here if it
   * we're talking to, we assume it's borken and then change it here if it
   * turns out that it isn't a TEXEL drive.
   * turns out that it isn't a TEXEL drive.
   */
   */
  if ((bflags & BLIST_BORKEN) == 0)
  if ((bflags & BLIST_BORKEN) == 0)
    SDpnt->borken = 0;
    SDpnt->borken = 0;
 
 
  /*
  /*
   * These devices need this "key" to unlock the devices so we can use it
   * These devices need this "key" to unlock the devices so we can use it
   */
   */
  if ((bflags & BLIST_KEY) != 0) {
  if ((bflags & BLIST_KEY) != 0) {
    printk ("Unlocked floptical drive.\n");
    printk ("Unlocked floptical drive.\n");
    SDpnt->lockable = 0;
    SDpnt->lockable = 0;
    scsi_cmd[0] = MODE_SENSE;
    scsi_cmd[0] = MODE_SENSE;
    scsi_cmd[1] = (lun << 5) & 0xe0;
    scsi_cmd[1] = (lun << 5) & 0xe0;
    scsi_cmd[2] = 0x2e;
    scsi_cmd[2] = 0x2e;
    scsi_cmd[3] = 0;
    scsi_cmd[3] = 0;
    scsi_cmd[4] = 0x2a;
    scsi_cmd[4] = 0x2a;
    scsi_cmd[5] = 0;
    scsi_cmd[5] = 0;
    SCpnt->cmd_len = 0;
    SCpnt->cmd_len = 0;
    {
    {
      struct semaphore sem = MUTEX_LOCKED;
      struct semaphore sem = MUTEX_LOCKED;
      SCpnt->request.rq_status = RQ_SCSI_BUSY;
      SCpnt->request.rq_status = RQ_SCSI_BUSY;
      SCpnt->request.sem = &sem;
      SCpnt->request.sem = &sem;
      scsi_do_cmd (SCpnt, (void *) scsi_cmd,
      scsi_do_cmd (SCpnt, (void *) scsi_cmd,
                   (void *) scsi_result, 0x2a,
                   (void *) scsi_result, 0x2a,
                   scan_scsis_done, SCSI_TIMEOUT, 3);
                   scan_scsis_done, SCSI_TIMEOUT, 3);
      down (&sem);
      down (&sem);
    }
    }
  }
  }
  /* Add this device to the linked list at the end */
  /* Add this device to the linked list at the end */
  if (SDtail)
  if (SDtail)
    SDtail->next = SDpnt;
    SDtail->next = SDpnt;
  else
  else
    scsi_devices = SDpnt;
    scsi_devices = SDpnt;
  SDtail = SDpnt;
  SDtail = SDpnt;
 
 
  SDpnt = (Scsi_Device *) scsi_init_malloc (sizeof (Scsi_Device), GFP_ATOMIC);
  SDpnt = (Scsi_Device *) scsi_init_malloc (sizeof (Scsi_Device), GFP_ATOMIC);
  *SDpnt2=SDpnt;
  *SDpnt2=SDpnt;
  if (!SDpnt)
  if (!SDpnt)
    printk ("scsi: scan_scsis_single: Cannot malloc\n");
    printk ("scsi: scan_scsis_single: Cannot malloc\n");
 
 
 
 
  /*
  /*
   * Some scsi devices cannot be polled for lun != 0 due to firmware bugs
   * Some scsi devices cannot be polled for lun != 0 due to firmware bugs
   */
   */
  if (bflags & BLIST_NOLUN)
  if (bflags & BLIST_NOLUN)
    return 0;                   /* break; */
    return 0;                   /* break; */
 
 
  /*
  /*
   * If we want to only allow I/O to one of the luns attached to this device
   * If we want to only allow I/O to one of the luns attached to this device
   * at a time, then we set this flag.
   * at a time, then we set this flag.
   */
   */
  if (bflags & BLIST_SINGLELUN)
  if (bflags & BLIST_SINGLELUN)
    SDpnt->single_lun = 1;
    SDpnt->single_lun = 1;
 
 
  /*
  /*
   * If this device is known to support sparse multiple units, override the
   * If this device is known to support sparse multiple units, override the
   * other settings, and scan all of them.
   * other settings, and scan all of them.
   */
   */
  if (bflags & BLIST_SPARSELUN) {
  if (bflags & BLIST_SPARSELUN) {
    *max_dev_lun = 8;
    *max_dev_lun = 8;
    *sparse_lun = 1;
    *sparse_lun = 1;
    return 1;
    return 1;
  }
  }
 
 
  /*
  /*
   * If this device is known to support multiple units, override the other
   * If this device is known to support multiple units, override the other
   * settings, and scan all of them.
   * settings, and scan all of them.
   */
   */
  if (bflags & BLIST_FORCELUN) {
  if (bflags & BLIST_FORCELUN) {
    *max_dev_lun = 8;
    *max_dev_lun = 8;
    return 1;
    return 1;
  }
  }
 
 
  /*
  /*
   * REGAL CDC-4X: avoid hang after LUN 4
   * REGAL CDC-4X: avoid hang after LUN 4
   */
   */
  if (bflags & BLIST_MAX5LUN) {
  if (bflags & BLIST_MAX5LUN) {
    *max_dev_lun = 5;
    *max_dev_lun = 5;
    return 1;
    return 1;
  }
  }
 
 
  /*
  /*
   * We assume the device can't handle lun!=0 if: - it reports scsi-0 (ANSI
   * We assume the device can't handle lun!=0 if: - it reports scsi-0 (ANSI
   * SCSI Revision 0) (old drives like MAXTOR XT-3280) or - it reports scsi-1
   * SCSI Revision 0) (old drives like MAXTOR XT-3280) or - it reports scsi-1
   * (ANSI SCSI Revision 1) and Response Data Format 0
   * (ANSI SCSI Revision 1) and Response Data Format 0
   */
   */
  if (((scsi_result[2] & 0x07) == 0)
  if (((scsi_result[2] & 0x07) == 0)
      ||
      ||
      ((scsi_result[2] & 0x07) == 1 &&
      ((scsi_result[2] & 0x07) == 1 &&
       (scsi_result[3] & 0x0f) == 0))
       (scsi_result[3] & 0x0f) == 0))
    return 0;
    return 0;
  return 1;
  return 1;
}
}
 
 
/*
/*
 *  Flag bits for the internal_timeout array
 *  Flag bits for the internal_timeout array
 */
 */
#define NORMAL_TIMEOUT 0
#define NORMAL_TIMEOUT 0
#define IN_ABORT  1
#define IN_ABORT  1
#define IN_RESET  2
#define IN_RESET  2
#define IN_RESET2 4
#define IN_RESET2 4
#define IN_RESET3 8
#define IN_RESET3 8
 
 
/*
/*
 * This is our time out function, called when the timer expires for a
 * This is our time out function, called when the timer expires for a
 * given host adapter.  It will attempt to abort the currently executing
 * given host adapter.  It will attempt to abort the currently executing
 * command, that failing perform a kernel panic.
 * command, that failing perform a kernel panic.
 */
 */
 
 
static void scsi_times_out (Scsi_Cmnd * SCpnt)
static void scsi_times_out (Scsi_Cmnd * SCpnt)
{
{
 
 
    switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET | IN_RESET2 | IN_RESET3))
    switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET | IN_RESET2 | IN_RESET3))
    {
    {
    case NORMAL_TIMEOUT:
    case NORMAL_TIMEOUT:
        {
        {
#ifdef DEBUG_TIMEOUT
#ifdef DEBUG_TIMEOUT
            scsi_dump_status();
            scsi_dump_status();
#endif
#endif
        }
        }
 
 
        if (!scsi_abort (SCpnt, DID_TIME_OUT))
        if (!scsi_abort (SCpnt, DID_TIME_OUT))
            return;
            return;
    case IN_ABORT:
    case IN_ABORT:
        printk("SCSI host %d abort (pid %ld) timed out - resetting\n",
        printk("SCSI host %d abort (pid %ld) timed out - resetting\n",
               SCpnt->host->host_no, SCpnt->pid);
               SCpnt->host->host_no, SCpnt->pid);
        if (!scsi_reset (SCpnt, SCSI_RESET_ASYNCHRONOUS))
        if (!scsi_reset (SCpnt, SCSI_RESET_ASYNCHRONOUS))
            return;
            return;
    case IN_RESET:
    case IN_RESET:
    case (IN_ABORT | IN_RESET):
    case (IN_ABORT | IN_RESET):
        /* This might be controversial, but if there is a bus hang,
        /* This might be controversial, but if there is a bus hang,
         * you might conceivably want the machine up and running
         * you might conceivably want the machine up and running
         * esp if you have an ide disk.
         * esp if you have an ide disk.
         */
         */
        printk("SCSI host %d channel %d reset (pid %ld) timed out - "
        printk("SCSI host %d channel %d reset (pid %ld) timed out - "
               "trying harder\n",
               "trying harder\n",
               SCpnt->host->host_no, SCpnt->channel, SCpnt->pid);
               SCpnt->host->host_no, SCpnt->channel, SCpnt->pid);
        SCpnt->internal_timeout &= ~IN_RESET;
        SCpnt->internal_timeout &= ~IN_RESET;
        SCpnt->internal_timeout |= IN_RESET2;
        SCpnt->internal_timeout |= IN_RESET2;
        scsi_reset (SCpnt,
        scsi_reset (SCpnt,
                    SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_BUS_RESET);
                    SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_BUS_RESET);
        return;
        return;
    case IN_RESET2:
    case IN_RESET2:
    case (IN_ABORT | IN_RESET2):
    case (IN_ABORT | IN_RESET2):
        /* Obviously the bus reset didn't work.
        /* Obviously the bus reset didn't work.
         * Let's try even harder and call for an HBA reset.
         * Let's try even harder and call for an HBA reset.
         * Maybe the HBA itself crashed and this will shake it loose.
         * Maybe the HBA itself crashed and this will shake it loose.
         */
         */
        printk("SCSI host %d reset (pid %ld) timed out - trying to shake it loose\n",
        printk("SCSI host %d reset (pid %ld) timed out - trying to shake it loose\n",
               SCpnt->host->host_no, SCpnt->pid);
               SCpnt->host->host_no, SCpnt->pid);
        SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2);
        SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2);
        SCpnt->internal_timeout |= IN_RESET3;
        SCpnt->internal_timeout |= IN_RESET3;
        scsi_reset (SCpnt,
        scsi_reset (SCpnt,
                    SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_HOST_RESET);
                    SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_HOST_RESET);
        return;
        return;
 
 
    default:
    default:
        printk("SCSI host %d reset (pid %ld) timed out again -\n",
        printk("SCSI host %d reset (pid %ld) timed out again -\n",
               SCpnt->host->host_no, SCpnt->pid);
               SCpnt->host->host_no, SCpnt->pid);
        printk("probably an unrecoverable SCSI bus or device hang.\n");
        printk("probably an unrecoverable SCSI bus or device hang.\n");
        return;
        return;
 
 
    }
    }
 
 
}
}
 
 
 
 
/* This function takes a quick look at a request, and decides if it
/* This function takes a quick look at a request, and decides if it
 * can be queued now, or if there would be a stall while waiting for
 * can be queued now, or if there would be a stall while waiting for
 * something else to finish.  This routine assumes that interrupts are
 * something else to finish.  This routine assumes that interrupts are
 * turned off when entering the routine.  It is the responsibility
 * turned off when entering the routine.  It is the responsibility
 * of the calling code to ensure that this is the case.
 * of the calling code to ensure that this is the case.
 */
 */
 
 
Scsi_Cmnd * request_queueable (struct request * req, Scsi_Device * device)
Scsi_Cmnd * request_queueable (struct request * req, Scsi_Device * device)
{
{
    Scsi_Cmnd * SCpnt = NULL;
    Scsi_Cmnd * SCpnt = NULL;
    int tablesize;
    int tablesize;
    Scsi_Cmnd * found = NULL;
    Scsi_Cmnd * found = NULL;
    struct buffer_head * bh, *bhp;
    struct buffer_head * bh, *bhp;
 
 
    if (!device)
    if (!device)
        panic ("No device passed to request_queueable().\n");
        panic ("No device passed to request_queueable().\n");
 
 
    if (req && req->rq_status == RQ_INACTIVE)
    if (req && req->rq_status == RQ_INACTIVE)
        panic("Inactive in request_queueable");
        panic("Inactive in request_queueable");
 
 
    /*
    /*
     * Look for a free command block.  If we have been instructed not to queue
     * Look for a free command block.  If we have been instructed not to queue
     * multiple commands to multi-lun devices, then check to see what else is
     * multiple commands to multi-lun devices, then check to see what else is
     * going for this device first.
     * going for this device first.
     */
     */
 
 
    if (!device->single_lun) {
    if (!device->single_lun) {
        SCpnt = device->device_queue;
        SCpnt = device->device_queue;
        while(SCpnt){
        while(SCpnt){
            if(SCpnt->request.rq_status == RQ_INACTIVE) break;
            if(SCpnt->request.rq_status == RQ_INACTIVE) break;
            SCpnt = SCpnt->device_next;
            SCpnt = SCpnt->device_next;
        }
        }
    } else {
    } else {
        SCpnt = device->host->host_queue;
        SCpnt = device->host->host_queue;
        while(SCpnt){
        while(SCpnt){
            if(SCpnt->channel == device->channel
            if(SCpnt->channel == device->channel
                && SCpnt->target == device->id) {
                && SCpnt->target == device->id) {
                if (SCpnt->lun == device->lun) {
                if (SCpnt->lun == device->lun) {
                    if(found == NULL
                    if(found == NULL
                       && SCpnt->request.rq_status == RQ_INACTIVE)
                       && SCpnt->request.rq_status == RQ_INACTIVE)
                    {
                    {
                        found=SCpnt;
                        found=SCpnt;
                    }
                    }
                }
                }
                if(SCpnt->request.rq_status != RQ_INACTIVE) {
                if(SCpnt->request.rq_status != RQ_INACTIVE) {
                    /*
                    /*
                     * I think that we should really limit things to one
                     * I think that we should really limit things to one
                     * outstanding command per device - this is what tends
                     * outstanding command per device - this is what tends
                     * to trip up buggy firmware.
                     * to trip up buggy firmware.
                     */
                     */
                    return NULL;
                    return NULL;
                }
                }
            }
            }
            SCpnt = SCpnt->next;
            SCpnt = SCpnt->next;
        }
        }
        SCpnt = found;
        SCpnt = found;
    }
    }
 
 
    if (!SCpnt) return NULL;
    if (!SCpnt) return NULL;
 
 
    if (SCSI_BLOCK(device->host)) return NULL;
    if (SCSI_BLOCK(device->host)) return NULL;
 
 
    if (req) {
    if (req) {
        memcpy(&SCpnt->request, req, sizeof(struct request));
        memcpy(&SCpnt->request, req, sizeof(struct request));
        tablesize = device->host->sg_tablesize;
        tablesize = device->host->sg_tablesize;
        bhp = bh = req->bh;
        bhp = bh = req->bh;
        if(!tablesize) bh = NULL;
        if(!tablesize) bh = NULL;
        /* Take a quick look through the table to see how big it is.
        /* Take a quick look through the table to see how big it is.
         * We already have our copy of req, so we can mess with that
         * We already have our copy of req, so we can mess with that
         * if we want to.
         * if we want to.
         */
         */
        while(req->nr_sectors && bh){
        while(req->nr_sectors && bh){
            bhp = bhp->b_reqnext;
            bhp = bhp->b_reqnext;
            if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
            if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
            req->nr_sectors -= bh->b_size >> 9;
            req->nr_sectors -= bh->b_size >> 9;
            req->sector += bh->b_size >> 9;
            req->sector += bh->b_size >> 9;
            if(!tablesize) break;
            if(!tablesize) break;
            bh = bhp;
            bh = bhp;
        }
        }
        if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
        if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
            SCpnt->request.bhtail = bh;
            SCpnt->request.bhtail = bh;
            req->bh = bh->b_reqnext; /* Divide request */
            req->bh = bh->b_reqnext; /* Divide request */
            bh->b_reqnext = NULL;
            bh->b_reqnext = NULL;
            bh = req->bh;
            bh = req->bh;
 
 
            /* Now reset things so that req looks OK */
            /* Now reset things so that req looks OK */
            SCpnt->request.nr_sectors -= req->nr_sectors;
            SCpnt->request.nr_sectors -= req->nr_sectors;
            req->current_nr_sectors = bh->b_size >> 9;
            req->current_nr_sectors = bh->b_size >> 9;
            req->buffer = bh->b_data;
            req->buffer = bh->b_data;
            SCpnt->request.sem = NULL; /* Wait until whole thing done */
            SCpnt->request.sem = NULL; /* Wait until whole thing done */
        } else {
        } else {
            req->rq_status = RQ_INACTIVE;
            req->rq_status = RQ_INACTIVE;
            wake_up(&wait_for_request);
            wake_up(&wait_for_request);
        }
        }
    } else {
    } else {
        SCpnt->request.rq_status = RQ_SCSI_BUSY;  /* Busy, but no request */
        SCpnt->request.rq_status = RQ_SCSI_BUSY;  /* Busy, but no request */
        SCpnt->request.sem = NULL;   /* And no one is waiting for the device
        SCpnt->request.sem = NULL;   /* And no one is waiting for the device
                                      * either */
                                      * either */
    }
    }
 
 
    SCpnt->use_sg = 0;               /* Reset the scatter-gather flag */
    SCpnt->use_sg = 0;               /* Reset the scatter-gather flag */
    SCpnt->old_use_sg  = 0;
    SCpnt->old_use_sg  = 0;
    SCpnt->transfersize = 0;
    SCpnt->transfersize = 0;
    SCpnt->underflow = 0;
    SCpnt->underflow = 0;
    SCpnt->cmd_len = 0;
    SCpnt->cmd_len = 0;
 
 
/* Since not everyone seems to set the device info correctly
/* Since not everyone seems to set the device info correctly
 * before Scsi_Cmnd gets send out to scsi_do_command, we do it here.
 * before Scsi_Cmnd gets send out to scsi_do_command, we do it here.
 */
 */
    SCpnt->channel = device->channel;
    SCpnt->channel = device->channel;
    SCpnt->lun = device->lun;
    SCpnt->lun = device->lun;
    SCpnt->target = device->id;
    SCpnt->target = device->id;
 
 
    return SCpnt;
    return SCpnt;
}
}
 
 
/* This function returns a structure pointer that will be valid for
/* This function returns a structure pointer that will be valid for
 * the device.  The wait parameter tells us whether we should wait for
 * the device.  The wait parameter tells us whether we should wait for
 * the unit to become free or not.  We are also able to tell this routine
 * the unit to become free or not.  We are also able to tell this routine
 * not to return a descriptor if the host is unable to accept any more
 * not to return a descriptor if the host is unable to accept any more
 * commands for the time being.  We need to keep in mind that there is no
 * commands for the time being.  We need to keep in mind that there is no
 * guarantee that the host remain not busy.  Keep in mind the
 * guarantee that the host remain not busy.  Keep in mind the
 * request_queueable function also knows the internal allocation scheme
 * request_queueable function also knows the internal allocation scheme
 * of the packets for each device
 * of the packets for each device
 */
 */
 
 
Scsi_Cmnd * allocate_device (struct request ** reqp, Scsi_Device * device,
Scsi_Cmnd * allocate_device (struct request ** reqp, Scsi_Device * device,
                             int wait)
                             int wait)
{
{
    kdev_t dev;
    kdev_t dev;
    struct request * req = NULL;
    struct request * req = NULL;
    int tablesize;
    int tablesize;
    unsigned long flags;
    unsigned long flags;
    struct buffer_head * bh, *bhp;
    struct buffer_head * bh, *bhp;
    struct Scsi_Host * host;
    struct Scsi_Host * host;
    Scsi_Cmnd * SCpnt = NULL;
    Scsi_Cmnd * SCpnt = NULL;
    Scsi_Cmnd * SCwait = NULL;
    Scsi_Cmnd * SCwait = NULL;
    Scsi_Cmnd * found = NULL;
    Scsi_Cmnd * found = NULL;
 
 
    if (!device)
    if (!device)
        panic ("No device passed to allocate_device().\n");
        panic ("No device passed to allocate_device().\n");
 
 
    if (reqp) req = *reqp;
    if (reqp) req = *reqp;
 
 
    /* See if this request has already been queued by an interrupt routine */
    /* See if this request has already been queued by an interrupt routine */
    if (req) {
    if (req) {
        if(req->rq_status == RQ_INACTIVE) return NULL;
        if(req->rq_status == RQ_INACTIVE) return NULL;
        dev = req->rq_dev;
        dev = req->rq_dev;
    } else
    } else
        dev = 0;         /* unused */
        dev = 0;         /* unused */
 
 
    host = device->host;
    host = device->host;
 
 
    if (intr_count && SCSI_BLOCK(host)) return NULL;
    if (intr_count && SCSI_BLOCK(host)) return NULL;
 
 
    while (1==1){
    while (1==1){
        if (!device->single_lun) {
        if (!device->single_lun) {
            SCpnt = device->device_queue;
            SCpnt = device->device_queue;
            while(SCpnt){
            while(SCpnt){
                SCwait = SCpnt;
                SCwait = SCpnt;
                if(SCpnt->request.rq_status == RQ_INACTIVE) break;
                if(SCpnt->request.rq_status == RQ_INACTIVE) break;
                SCpnt = SCpnt->device_next;
                SCpnt = SCpnt->device_next;
            }
            }
        } else {
        } else {
            SCpnt = device->host->host_queue;
            SCpnt = device->host->host_queue;
            while(SCpnt){
            while(SCpnt){
                if(SCpnt->channel == device->channel
                if(SCpnt->channel == device->channel
                   && SCpnt->target == device->id) {
                   && SCpnt->target == device->id) {
                    if (SCpnt->lun == device->lun) {
                    if (SCpnt->lun == device->lun) {
                        SCwait = SCpnt;
                        SCwait = SCpnt;
                        if(found == NULL
                        if(found == NULL
                           && SCpnt->request.rq_status == RQ_INACTIVE)
                           && SCpnt->request.rq_status == RQ_INACTIVE)
                        {
                        {
                            found=SCpnt;
                            found=SCpnt;
                        }
                        }
                    }
                    }
                    if(SCpnt->request.rq_status != RQ_INACTIVE) {
                    if(SCpnt->request.rq_status != RQ_INACTIVE) {
                        /*
                        /*
                         * I think that we should really limit things to one
                         * I think that we should really limit things to one
                         * outstanding command per device - this is what tends
                         * outstanding command per device - this is what tends
                         * to trip up buggy firmware.
                         * to trip up buggy firmware.
                         */
                         */
                        found = NULL;
                        found = NULL;
                        break;
                        break;
                    }
                    }
                }
                }
                SCpnt = SCpnt->next;
                SCpnt = SCpnt->next;
            }
            }
            SCpnt = found;
            SCpnt = found;
        }
        }
 
 
        save_flags_cli(flags);
        save_flags_cli(flags);
        /* See if this request has already been queued by an interrupt routine
        /* See if this request has already been queued by an interrupt routine
         */
         */
        if (req && (req->rq_status == RQ_INACTIVE || req->rq_dev != dev)) {
        if (req && (req->rq_status == RQ_INACTIVE || req->rq_dev != dev)) {
            restore_flags(flags);
            restore_flags(flags);
            return NULL;
            return NULL;
        }
        }
        if (!SCpnt || SCpnt->request.rq_status != RQ_INACTIVE)  /* Might have changed */
        if (!SCpnt || SCpnt->request.rq_status != RQ_INACTIVE)  /* Might have changed */
        {
        {
#if 1   /* NEW CODE */
#if 1   /* NEW CODE */
                if (wait && SCwait && SCwait->request.rq_status != RQ_INACTIVE){
                if (wait && SCwait && SCwait->request.rq_status != RQ_INACTIVE){
                        sleep_on(&device->device_wait);
                        sleep_on(&device->device_wait);
                        restore_flags(flags);
                        restore_flags(flags);
                } else {
                } else {
                        restore_flags(flags);
                        restore_flags(flags);
                        if (!wait) return NULL;
                        if (!wait) return NULL;
                        if (!SCwait) {
                        if (!SCwait) {
                                printk("Attempt to allocate device channel %d,"
                                printk("Attempt to allocate device channel %d,"
                                       " target %d, lun %d\n", device->channel,
                                       " target %d, lun %d\n", device->channel,
                                       device->id, device->lun);
                                       device->id, device->lun);
                                panic("No device found in allocate_device\n");
                                panic("No device found in allocate_device\n");
                        }
                        }
                }
                }
#else   /* ORIGINAL CODE */
#else   /* ORIGINAL CODE */
                    restore_flags(flags);
                    restore_flags(flags);
                    if(!wait) return NULL;
                    if(!wait) return NULL;
                    if (!SCwait) {
                    if (!SCwait) {
                        printk("Attempt to allocate device channel %d, target"
                        printk("Attempt to allocate device channel %d, target"
                               " %d, lun %d\n", device->channel, device->id,
                               " %d, lun %d\n", device->channel, device->id,
                               device->lun);
                               device->lun);
                        panic("No device found in allocate_device\n");
                        panic("No device found in allocate_device\n");
                    }
                    }
                    SCSI_SLEEP(&device->device_wait,
                    SCSI_SLEEP(&device->device_wait,
                               (SCwait->request.rq_status != RQ_INACTIVE));
                               (SCwait->request.rq_status != RQ_INACTIVE));
#endif
#endif
        } else {
        } else {
            if (req) {
            if (req) {
                memcpy(&SCpnt->request, req, sizeof(struct request));
                memcpy(&SCpnt->request, req, sizeof(struct request));
                tablesize = device->host->sg_tablesize;
                tablesize = device->host->sg_tablesize;
                bhp = bh = req->bh;
                bhp = bh = req->bh;
                if(!tablesize) bh = NULL;
                if(!tablesize) bh = NULL;
                /* Take a quick look through the table to see how big it is.
                /* Take a quick look through the table to see how big it is.
                 * We already have our copy of req, so we can mess with that
                 * We already have our copy of req, so we can mess with that
                 * if we want to.
                 * if we want to.
                 */
                 */
                while(req->nr_sectors && bh){
                while(req->nr_sectors && bh){
                    bhp = bhp->b_reqnext;
                    bhp = bhp->b_reqnext;
                    if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
                    if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
                    req->nr_sectors -= bh->b_size >> 9;
                    req->nr_sectors -= bh->b_size >> 9;
                    req->sector += bh->b_size >> 9;
                    req->sector += bh->b_size >> 9;
                    if(!tablesize) break;
                    if(!tablesize) break;
                    bh = bhp;
                    bh = bhp;
                }
                }
                if(req->nr_sectors && bh && bh->b_reqnext){/* Any leftovers? */
                if(req->nr_sectors && bh && bh->b_reqnext){/* Any leftovers? */
                    SCpnt->request.bhtail = bh;
                    SCpnt->request.bhtail = bh;
                    req->bh = bh->b_reqnext; /* Divide request */
                    req->bh = bh->b_reqnext; /* Divide request */
                    bh->b_reqnext = NULL;
                    bh->b_reqnext = NULL;
                    bh = req->bh;
                    bh = req->bh;
                    /* Now reset things so that req looks OK */
                    /* Now reset things so that req looks OK */
                    SCpnt->request.nr_sectors -= req->nr_sectors;
                    SCpnt->request.nr_sectors -= req->nr_sectors;
                    req->current_nr_sectors = bh->b_size >> 9;
                    req->current_nr_sectors = bh->b_size >> 9;
                    req->buffer = bh->b_data;
                    req->buffer = bh->b_data;
                    SCpnt->request.sem = NULL; /* Wait until whole thing done*/
                    SCpnt->request.sem = NULL; /* Wait until whole thing done*/
                }
                }
                else
                else
                {
                {
                    req->rq_status = RQ_INACTIVE;
                    req->rq_status = RQ_INACTIVE;
                    *reqp = req->next;
                    *reqp = req->next;
                    wake_up(&wait_for_request);
                    wake_up(&wait_for_request);
                }
                }
            } else {
            } else {
                SCpnt->request.rq_status = RQ_SCSI_BUSY;
                SCpnt->request.rq_status = RQ_SCSI_BUSY;
                SCpnt->request.sem = NULL;   /* And no one is waiting for this
                SCpnt->request.sem = NULL;   /* And no one is waiting for this
                                              * to complete */
                                              * to complete */
            }
            }
            restore_flags(flags);
            restore_flags(flags);
            break;
            break;
        }
        }
    }
    }
 
 
    SCpnt->use_sg = 0;            /* Reset the scatter-gather flag */
    SCpnt->use_sg = 0;            /* Reset the scatter-gather flag */
    SCpnt->old_use_sg  = 0;
    SCpnt->old_use_sg  = 0;
    SCpnt->transfersize = 0;      /* No default transfer size */
    SCpnt->transfersize = 0;      /* No default transfer size */
    SCpnt->cmd_len = 0;
    SCpnt->cmd_len = 0;
 
 
    SCpnt->underflow = 0;         /* Do not flag underflow conditions */
    SCpnt->underflow = 0;         /* Do not flag underflow conditions */
 
 
    /* Since not everyone seems to set the device info correctly
    /* Since not everyone seems to set the device info correctly
     * before Scsi_Cmnd gets send out to scsi_do_command, we do it here.
     * before Scsi_Cmnd gets send out to scsi_do_command, we do it here.
     */
     */
    SCpnt->channel = device->channel;
    SCpnt->channel = device->channel;
    SCpnt->lun = device->lun;
    SCpnt->lun = device->lun;
    SCpnt->target = device->id;
    SCpnt->target = device->id;
 
 
    return SCpnt;
    return SCpnt;
}
}
 
 
/*
/*
 * This is inline because we have stack problemes if we recurse to deeply.
 * This is inline because we have stack problemes if we recurse to deeply.
 */
 */
 
 
inline void internal_cmnd (Scsi_Cmnd * SCpnt)
inline void internal_cmnd (Scsi_Cmnd * SCpnt)
{
{
    unsigned long flags, timeout;
    unsigned long flags, timeout;
    struct Scsi_Host * host;
    struct Scsi_Host * host;
#ifdef DEBUG_DELAY
#ifdef DEBUG_DELAY
    unsigned long clock;
    unsigned long clock;
#endif
#endif
 
 
#if DEBUG
#if DEBUG
    unsigned long *ret = 0;
    unsigned long *ret = 0;
#ifdef __mips__
#ifdef __mips__
    __asm__ __volatile__ ("move\t%0,$31":"=r"(ret));
    __asm__ __volatile__ ("move\t%0,$31":"=r"(ret));
#else
#else
   ret =  __builtin_return_address(0);
   ret =  __builtin_return_address(0);
#endif
#endif
#endif
#endif
 
 
    host = SCpnt->host;
    host = SCpnt->host;
 
 
    save_flags_cli(flags);
    save_flags_cli(flags);
    /* Assign a unique nonzero serial_number. */
    /* Assign a unique nonzero serial_number. */
    if (++serial_number == 0) serial_number = 1;
    if (++serial_number == 0) serial_number = 1;
    SCpnt->serial_number = serial_number;
    SCpnt->serial_number = serial_number;
 
 
    /*
    /*
     * We will wait MIN_RESET_DELAY clock ticks after the last reset so
     * We will wait MIN_RESET_DELAY clock ticks after the last reset so
     * we can avoid the drive not being ready.
     * we can avoid the drive not being ready.
     */
     */
    timeout = host->last_reset + MIN_RESET_DELAY;
    timeout = host->last_reset + MIN_RESET_DELAY;
    if (jiffies < timeout) {
    if (jiffies < timeout) {
        int ticks_remaining = timeout - jiffies;
        int ticks_remaining = timeout - jiffies;
        /*
        /*
         * NOTE: This may be executed from within an interrupt
         * NOTE: This may be executed from within an interrupt
         * handler!  This is bad, but for now, it'll do.  The irq
         * handler!  This is bad, but for now, it'll do.  The irq
         * level of the interrupt handler has been masked out by the
         * level of the interrupt handler has been masked out by the
         * platform dependent interrupt handling code already, so the
         * platform dependent interrupt handling code already, so the
         * sti() here will not cause another call to the SCSI host's
         * sti() here will not cause another call to the SCSI host's
         * interrupt handler (assuming there is one irq-level per
         * interrupt handler (assuming there is one irq-level per
         * host).
         * host).
         */
         */
        sti();
        sti();
        while (--ticks_remaining >= 0) udelay(1000000/HZ);
        while (--ticks_remaining >= 0) udelay(1000000/HZ);
        host->last_reset = jiffies - MIN_RESET_DELAY;
        host->last_reset = jiffies - MIN_RESET_DELAY;
    }
    }
    restore_flags(flags);
    restore_flags(flags);
 
 
    update_timeout(SCpnt, SCpnt->timeout_per_command);
    update_timeout(SCpnt, SCpnt->timeout_per_command);
 
 
    /*
    /*
     * We will use a queued command if possible, otherwise we will emulate the
     * We will use a queued command if possible, otherwise we will emulate the
     * queuing and calling of completion function ourselves.
     * queuing and calling of completion function ourselves.
     */
     */
#ifdef DEBUG
#ifdef DEBUG
    printk("internal_cmnd (host = %d, channel = %d, target = %d, "
    printk("internal_cmnd (host = %d, channel = %d, target = %d, "
           "command = %p, buffer = %p, \nbufflen = %d, done = %p)\n",
           "command = %p, buffer = %p, \nbufflen = %d, done = %p)\n",
           SCpnt->host->host_no, SCpnt->channel, SCpnt->target, SCpnt->cmnd,
           SCpnt->host->host_no, SCpnt->channel, SCpnt->target, SCpnt->cmnd,
           SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
           SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
#endif
#endif
 
 
    if (host->can_queue)
    if (host->can_queue)
    {
    {
#ifdef DEBUG
#ifdef DEBUG
        printk("queuecommand : routine at %p\n",
        printk("queuecommand : routine at %p\n",
               host->hostt->queuecommand);
               host->hostt->queuecommand);
#endif
#endif
        /* This locking tries to prevent all sorts of races between
        /* This locking tries to prevent all sorts of races between
         * queuecommand and the interrupt code.  In effect,
         * queuecommand and the interrupt code.  In effect,
         * we are only allowed to be in queuecommand once at
         * we are only allowed to be in queuecommand once at
         * any given time, and we can only be in the interrupt
         * any given time, and we can only be in the interrupt
         * handler and the queuecommand function at the same time
         * handler and the queuecommand function at the same time
         * when queuecommand is called while servicing the
         * when queuecommand is called while servicing the
         * interrupt.
         * interrupt.
         */
         */
 
 
        if(!intr_count && SCpnt->host->irq)
        if(!intr_count && SCpnt->host->irq)
            disable_irq(SCpnt->host->irq);
            disable_irq(SCpnt->host->irq);
 
 
        host->hostt->queuecommand (SCpnt, scsi_done);
        host->hostt->queuecommand (SCpnt, scsi_done);
 
 
        if(!intr_count && SCpnt->host->irq)
        if(!intr_count && SCpnt->host->irq)
            enable_irq(SCpnt->host->irq);
            enable_irq(SCpnt->host->irq);
    }
    }
    else
    else
    {
    {
        int temp;
        int temp;
 
 
#ifdef DEBUG
#ifdef DEBUG
        printk("command() :  routine at %p\n", host->hostt->command);
        printk("command() :  routine at %p\n", host->hostt->command);
#endif
#endif
        temp = host->hostt->command (SCpnt);
        temp = host->hostt->command (SCpnt);
        SCpnt->result = temp;
        SCpnt->result = temp;
#ifdef DEBUG_DELAY
#ifdef DEBUG_DELAY
        clock = jiffies + 4 * HZ;
        clock = jiffies + 4 * HZ;
        while (jiffies < clock) barrier();
        while (jiffies < clock) barrier();
        printk("done(host = %d, result = %04x) : routine at %p\n",
        printk("done(host = %d, result = %04x) : routine at %p\n",
               host->host_no, temp, host->hostt->command);
               host->host_no, temp, host->hostt->command);
#endif
#endif
        scsi_done(SCpnt);
        scsi_done(SCpnt);
    }
    }
#ifdef DEBUG
#ifdef DEBUG
    printk("leaving internal_cmnd()\n");
    printk("leaving internal_cmnd()\n");
#endif
#endif
}
}
 
 
static void scsi_request_sense (Scsi_Cmnd * SCpnt)
static void scsi_request_sense (Scsi_Cmnd * SCpnt)
{
{
    unsigned long flags;
    unsigned long flags;
 
 
    save_flags_cli(flags);
    save_flags_cli(flags);
    SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
    SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
    update_timeout(SCpnt, SENSE_TIMEOUT);
    update_timeout(SCpnt, SENSE_TIMEOUT);
    restore_flags(flags);
    restore_flags(flags);
 
 
 
 
    memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
    memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
            sizeof(generic_sense));
            sizeof(generic_sense));
 
 
    SCpnt->cmnd[1] = SCpnt->lun << 5;
    SCpnt->cmnd[1] = SCpnt->lun << 5;
    SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
    SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
 
 
    SCpnt->request_buffer = &SCpnt->sense_buffer;
    SCpnt->request_buffer = &SCpnt->sense_buffer;
    SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
    SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
    SCpnt->use_sg = 0;
    SCpnt->use_sg = 0;
    SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
    SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
    internal_cmnd (SCpnt);
    internal_cmnd (SCpnt);
}
}
 
 
 
 
 
 
/*
/*
 * scsi_do_cmd sends all the commands out to the low-level driver.  It
 * scsi_do_cmd sends all the commands out to the low-level driver.  It
 * handles the specifics required for each low level driver - ie queued
 * handles the specifics required for each low level driver - ie queued
 * or non queued.  It also prevents conflicts when different high level
 * or non queued.  It also prevents conflicts when different high level
 * drivers go for the same host at the same time.
 * drivers go for the same host at the same time.
 */
 */
 
 
void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd ,
void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd ,
                  void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
                  void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
                  int timeout, int retries)
                  int timeout, int retries)
{
{
    unsigned long flags;
    unsigned long flags;
    struct Scsi_Host * host = SCpnt->host;
    struct Scsi_Host * host = SCpnt->host;
 
 
#ifdef DEBUG
#ifdef DEBUG
    {
    {
        int i;
        int i;
        int target = SCpnt->target;
        int target = SCpnt->target;
        printk ("scsi_do_cmd (host = %d, channel = %d target = %d, "
        printk ("scsi_do_cmd (host = %d, channel = %d target = %d, "
                "buffer =%p, bufflen = %d, done = %p, timeout = %d, "
                "buffer =%p, bufflen = %d, done = %p, timeout = %d, "
                "retries = %d)\n"
                "retries = %d)\n"
                "command : " , host->host_no, SCpnt->channel, target, buffer,
                "command : " , host->host_no, SCpnt->channel, target, buffer,
                bufflen, done, timeout, retries);
                bufflen, done, timeout, retries);
        for (i = 0; i < 10; ++i)
        for (i = 0; i < 10; ++i)
            printk ("%02x  ", ((unsigned char *) cmnd)[i]);
            printk ("%02x  ", ((unsigned char *) cmnd)[i]);
        printk("\n");
        printk("\n");
    }
    }
#endif
#endif
 
 
    if (!host)
    if (!host)
    {
    {
        panic ("Invalid or not present host.\n");
        panic ("Invalid or not present host.\n");
    }
    }
 
 
 
 
    /*
    /*
     * We must prevent reentrancy to the lowlevel host driver.  This prevents
     * We must prevent reentrancy to the lowlevel host driver.  This prevents
     * it - we enter a loop until the host we want to talk to is not busy.
     * it - we enter a loop until the host we want to talk to is not busy.
     * Race conditions are prevented, as interrupts are disabled in between the
     * Race conditions are prevented, as interrupts are disabled in between the
     * time we check for the host being not busy, and the time we mark it busy
     * time we check for the host being not busy, and the time we mark it busy
     * ourselves.
     * ourselves.
     */
     */
 
 
    save_flags_cli(flags);
    save_flags_cli(flags);
    SCpnt->pid = scsi_pid++;
    SCpnt->pid = scsi_pid++;
 
 
    while (SCSI_BLOCK(host)) {
    while (SCSI_BLOCK(host)) {
        restore_flags(flags);
        restore_flags(flags);
        SCSI_SLEEP(&host->host_wait, SCSI_BLOCK(host));
        SCSI_SLEEP(&host->host_wait, SCSI_BLOCK(host));
        cli();
        cli();
    }
    }
 
 
    if (host->block) host_active = host;
    if (host->block) host_active = host;
 
 
    host->host_busy++;
    host->host_busy++;
    restore_flags(flags);
    restore_flags(flags);
 
 
    /*
    /*
     * Our own function scsi_done (which marks the host as not busy, disables
     * Our own function scsi_done (which marks the host as not busy, disables
     * the timeout counter, etc) will be called by us or by the
     * the timeout counter, etc) will be called by us or by the
     * scsi_hosts[host].queuecommand() function needs to also call
     * scsi_hosts[host].queuecommand() function needs to also call
     * the completion function for the high level driver.
     * the completion function for the high level driver.
     */
     */
 
 
    memcpy ((void *) SCpnt->data_cmnd , (const void *) cmnd, 12);
    memcpy ((void *) SCpnt->data_cmnd , (const void *) cmnd, 12);
#if 0
#if 0
    SCpnt->host = host;
    SCpnt->host = host;
    SCpnt->channel = channel;
    SCpnt->channel = channel;
    SCpnt->target = target;
    SCpnt->target = target;
    SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
    SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
#endif
#endif
    SCpnt->reset_chain = NULL;
    SCpnt->reset_chain = NULL;
    SCpnt->serial_number = 0;
    SCpnt->serial_number = 0;
    SCpnt->bufflen = bufflen;
    SCpnt->bufflen = bufflen;
    SCpnt->buffer = buffer;
    SCpnt->buffer = buffer;
    SCpnt->flags = 0;
    SCpnt->flags = 0;
    SCpnt->retries = 0;
    SCpnt->retries = 0;
    SCpnt->allowed = retries;
    SCpnt->allowed = retries;
    SCpnt->done = done;
    SCpnt->done = done;
    SCpnt->timeout_per_command = timeout;
    SCpnt->timeout_per_command = timeout;
 
 
    memcpy ((void *) SCpnt->cmnd , (const void *) cmnd, 12);
    memcpy ((void *) SCpnt->cmnd , (const void *) cmnd, 12);
    /* Zero the sense buffer.  Some host adapters automatically request
    /* Zero the sense buffer.  Some host adapters automatically request
     * sense on error.  0 is not a valid sense code.
     * sense on error.  0 is not a valid sense code.
     */
     */
    memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
    memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
    SCpnt->request_buffer = buffer;
    SCpnt->request_buffer = buffer;
    SCpnt->request_bufflen = bufflen;
    SCpnt->request_bufflen = bufflen;
    SCpnt->old_use_sg = SCpnt->use_sg;
    SCpnt->old_use_sg = SCpnt->use_sg;
    if (SCpnt->cmd_len == 0)
    if (SCpnt->cmd_len == 0)
        SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
        SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
    SCpnt->old_cmd_len = SCpnt->cmd_len;
    SCpnt->old_cmd_len = SCpnt->cmd_len;
 
 
    /* Start the timer ticking.  */
    /* Start the timer ticking.  */
 
 
    SCpnt->internal_timeout = NORMAL_TIMEOUT;
    SCpnt->internal_timeout = NORMAL_TIMEOUT;
    SCpnt->abort_reason = 0;
    SCpnt->abort_reason = 0;
    internal_cmnd (SCpnt);
    internal_cmnd (SCpnt);
 
 
#ifdef DEBUG
#ifdef DEBUG
    printk ("Leaving scsi_do_cmd()\n");
    printk ("Leaving scsi_do_cmd()\n");
#endif
#endif
}
}
 
 
static int check_sense (Scsi_Cmnd * SCpnt)
static int check_sense (Scsi_Cmnd * SCpnt)
{
{
    /* If there is no sense information, request it.  If we have already
    /* If there is no sense information, request it.  If we have already
     * requested it, there is no point in asking again - the firmware must
     * requested it, there is no point in asking again - the firmware must
     * be confused.
     * be confused.
     */
     */
    if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
    if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
        if(!(SCpnt->flags & ASKED_FOR_SENSE))
        if(!(SCpnt->flags & ASKED_FOR_SENSE))
            return SUGGEST_SENSE;
            return SUGGEST_SENSE;
        else
        else
            return SUGGEST_RETRY;
            return SUGGEST_RETRY;
    }
    }
 
 
    SCpnt->flags &= ~ASKED_FOR_SENSE;
    SCpnt->flags &= ~ASKED_FOR_SENSE;
 
 
#ifdef DEBUG_INIT
#ifdef DEBUG_INIT
    printk("scsi%d, channel%d : ", SCpnt->host->host_no, SCpnt->channel);
    printk("scsi%d, channel%d : ", SCpnt->host->host_no, SCpnt->channel);
    print_sense("", SCpnt);
    print_sense("", SCpnt);
    printk("\n");
    printk("\n");
#endif
#endif
    if (SCpnt->sense_buffer[2] & 0xe0)
    if (SCpnt->sense_buffer[2] & 0xe0)
        return SUGGEST_ABORT;
        return SUGGEST_ABORT;
 
 
    switch (SCpnt->sense_buffer[2] & 0xf)
    switch (SCpnt->sense_buffer[2] & 0xf)
    {
    {
    case NO_SENSE:
    case NO_SENSE:
        return 0;
        return 0;
    case RECOVERED_ERROR:
    case RECOVERED_ERROR:
        return SUGGEST_IS_OK;
        return SUGGEST_IS_OK;
 
 
    case ABORTED_COMMAND:
    case ABORTED_COMMAND:
        return SUGGEST_RETRY;
        return SUGGEST_RETRY;
    case NOT_READY:
    case NOT_READY:
    case UNIT_ATTENTION:
    case UNIT_ATTENTION:
        /*
        /*
         * If we are expecting a CC/UA because of a bus reset that we
         * If we are expecting a CC/UA because of a bus reset that we
         * performed, treat this just as a retry.  Otherwise this is
         * performed, treat this just as a retry.  Otherwise this is
         * information that we should pass up to the upper-level driver
         * information that we should pass up to the upper-level driver
         * so that we can deal with it there.
         * so that we can deal with it there.
         */
         */
        if( SCpnt->device->expecting_cc_ua )
        if( SCpnt->device->expecting_cc_ua )
        {
        {
            SCpnt->device->expecting_cc_ua = 0;
            SCpnt->device->expecting_cc_ua = 0;
            return SUGGEST_RETRY;
            return SUGGEST_RETRY;
        }
        }
        return SUGGEST_ABORT;
        return SUGGEST_ABORT;
 
 
    /* these three are not supported */
    /* these three are not supported */
    case COPY_ABORTED:
    case COPY_ABORTED:
    case VOLUME_OVERFLOW:
    case VOLUME_OVERFLOW:
    case MISCOMPARE:
    case MISCOMPARE:
 
 
    case MEDIUM_ERROR:
    case MEDIUM_ERROR:
        return SUGGEST_REMAP;
        return SUGGEST_REMAP;
    case BLANK_CHECK:
    case BLANK_CHECK:
    case DATA_PROTECT:
    case DATA_PROTECT:
    case HARDWARE_ERROR:
    case HARDWARE_ERROR:
    case ILLEGAL_REQUEST:
    case ILLEGAL_REQUEST:
    default:
    default:
        return SUGGEST_ABORT;
        return SUGGEST_ABORT;
    }
    }
}
}
 
 
/* This function is the mid-level interrupt routine, which decides how
/* This function is the mid-level interrupt routine, which decides how
 *  to handle error conditions.  Each invocation of this function must
 *  to handle error conditions.  Each invocation of this function must
 *  do one and *only* one of the following:
 *  do one and *only* one of the following:
 *
 *
 *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
 *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
 *      normal completion, and indicates that the handling for this
 *      normal completion, and indicates that the handling for this
 *      request is complete.
 *      request is complete.
 *  (2) Call internal_cmnd to requeue the command.  This will result in
 *  (2) Call internal_cmnd to requeue the command.  This will result in
 *      scsi_done being called again when the retry is complete.
 *      scsi_done being called again when the retry is complete.
 *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
 *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
 *      more information about the error condition.  When the information
 *      more information about the error condition.  When the information
 *      is available, scsi_done will be called again.
 *      is available, scsi_done will be called again.
 *  (4) Call reset().  This is sort of a last resort, and the idea is that
 *  (4) Call reset().  This is sort of a last resort, and the idea is that
 *      this may kick things loose and get the drive working again.  reset()
 *      this may kick things loose and get the drive working again.  reset()
 *      automatically calls scsi_request_sense, and thus scsi_done will be
 *      automatically calls scsi_request_sense, and thus scsi_done will be
 *      called again once the reset is complete.
 *      called again once the reset is complete.
 *
 *
 *      If none of the above actions are taken, the drive in question
 *      If none of the above actions are taken, the drive in question
 *      will hang. If more than one of the above actions are taken by
 *      will hang. If more than one of the above actions are taken by
 *      scsi_done, then unpredictable behavior will result.
 *      scsi_done, then unpredictable behavior will result.
 */
 */
static void scsi_done (Scsi_Cmnd * SCpnt)
static void scsi_done (Scsi_Cmnd * SCpnt)
{
{
    int status=0;
    int status=0;
    int exit=0;
    int exit=0;
    int checked;
    int checked;
    int oldto;
    int oldto;
    struct Scsi_Host * host = SCpnt->host;
    struct Scsi_Host * host = SCpnt->host;
    int result = SCpnt->result;
    int result = SCpnt->result;
    SCpnt->serial_number = 0;
    SCpnt->serial_number = 0;
    oldto = update_timeout(SCpnt, 0);
    oldto = update_timeout(SCpnt, 0);
 
 
#ifdef DEBUG_TIMEOUT
#ifdef DEBUG_TIMEOUT
    if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
    if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
                      result, SCpnt->target, SCpnt->lun);
                      result, SCpnt->target, SCpnt->lun);
#endif
#endif
 
 
    /* If we requested an abort, (and we got it) then fix up the return
    /* If we requested an abort, (and we got it) then fix up the return
     *  status to say why
     *  status to say why
     */
     */
    if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
    if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
        SCpnt->result = result = (result & 0xff00ffff) |
        SCpnt->result = result = (result & 0xff00ffff) |
            (SCpnt->abort_reason << 16);
            (SCpnt->abort_reason << 16);
 
 
 
 
#define FINISHED 0
#define FINISHED 0
#define MAYREDO  1
#define MAYREDO  1
#define REDO     3
#define REDO     3
#define PENDING  4
#define PENDING  4
 
 
#ifdef DEBUG
#ifdef DEBUG
    printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
    printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
#endif
#endif
 
 
    if(SCpnt->flags & WAS_SENSE)
    if(SCpnt->flags & WAS_SENSE)
    {
    {
        SCpnt->use_sg = SCpnt->old_use_sg;
        SCpnt->use_sg = SCpnt->old_use_sg;
        SCpnt->cmd_len = SCpnt->old_cmd_len;
        SCpnt->cmd_len = SCpnt->old_cmd_len;
    }
    }
 
 
    switch (host_byte(result))
    switch (host_byte(result))
    {
    {
    case DID_OK:
    case DID_OK:
        if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
        if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
            /* Failed to obtain sense information */
            /* Failed to obtain sense information */
        {
        {
            SCpnt->flags &= ~WAS_SENSE;
            SCpnt->flags &= ~WAS_SENSE;
#if 0   /* This cannot possibly be correct. */
#if 0   /* This cannot possibly be correct. */
            SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
            SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
#endif
#endif
 
 
            if (!(SCpnt->flags & WAS_RESET))
            if (!(SCpnt->flags & WAS_RESET))
            {
            {
                printk("scsi%d : channel %d target %d lun %d request sense"
                printk("scsi%d : channel %d target %d lun %d request sense"
                       " failed, performing reset.\n",
                       " failed, performing reset.\n",
                       SCpnt->host->host_no, SCpnt->channel, SCpnt->target,
                       SCpnt->host->host_no, SCpnt->channel, SCpnt->target,
                       SCpnt->lun);
                       SCpnt->lun);
                scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
                scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
                return;
                return;
            }
            }
            else
            else
            {
            {
                exit = (DRIVER_HARD | SUGGEST_ABORT);
                exit = (DRIVER_HARD | SUGGEST_ABORT);
                status = FINISHED;
                status = FINISHED;
            }
            }
        }
        }
        else switch(msg_byte(result))
        else switch(msg_byte(result))
        {
        {
        case COMMAND_COMPLETE:
        case COMMAND_COMPLETE:
            switch (status_byte(result))
            switch (status_byte(result))
            {
            {
            case GOOD:
            case GOOD:
                if (SCpnt->flags & WAS_SENSE)
                if (SCpnt->flags & WAS_SENSE)
                {
                {
#ifdef DEBUG
#ifdef DEBUG
                    printk ("In scsi_done, GOOD status, COMMAND COMPLETE, "
                    printk ("In scsi_done, GOOD status, COMMAND COMPLETE, "
                            "parsing sense information.\n");
                            "parsing sense information.\n");
#endif
#endif
                    SCpnt->flags &= ~WAS_SENSE;
                    SCpnt->flags &= ~WAS_SENSE;
#if 0   /* This cannot possibly be correct. */
#if 0   /* This cannot possibly be correct. */
                    SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
                    SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
#endif
#endif
 
 
                    switch (checked = check_sense(SCpnt))
                    switch (checked = check_sense(SCpnt))
                    {
                    {
                    case SUGGEST_SENSE:
                    case SUGGEST_SENSE:
                    case 0:
                    case 0:
#ifdef DEBUG
#ifdef DEBUG
                        printk("NO SENSE.  status = REDO\n");
                        printk("NO SENSE.  status = REDO\n");
#endif
#endif
                        update_timeout(SCpnt, oldto);
                        update_timeout(SCpnt, oldto);
                        status = REDO;
                        status = REDO;
                        break;
                        break;
                    case SUGGEST_IS_OK:
                    case SUGGEST_IS_OK:
                        break;
                        break;
                    case SUGGEST_REMAP:
                    case SUGGEST_REMAP:
#ifdef DEBUG
#ifdef DEBUG
                        printk("SENSE SUGGEST REMAP - status = FINISHED\n");
                        printk("SENSE SUGGEST REMAP - status = FINISHED\n");
#endif
#endif
                        status = FINISHED;
                        status = FINISHED;
                        exit = DRIVER_SENSE | SUGGEST_ABORT;
                        exit = DRIVER_SENSE | SUGGEST_ABORT;
                        break;
                        break;
                    case SUGGEST_RETRY:
                    case SUGGEST_RETRY:
#ifdef DEBUG
#ifdef DEBUG
                        printk("SENSE SUGGEST RETRY - status = MAYREDO\n");
                        printk("SENSE SUGGEST RETRY - status = MAYREDO\n");
#endif
#endif
                        status = MAYREDO;
                        status = MAYREDO;
                        exit = DRIVER_SENSE | SUGGEST_RETRY;
                        exit = DRIVER_SENSE | SUGGEST_RETRY;
                        break;
                        break;
                    case SUGGEST_ABORT:
                    case SUGGEST_ABORT:
#ifdef DEBUG
#ifdef DEBUG
                        printk("SENSE SUGGEST ABORT - status = FINISHED");
                        printk("SENSE SUGGEST ABORT - status = FINISHED");
#endif
#endif
                        status = FINISHED;
                        status = FINISHED;
                        exit =  DRIVER_SENSE | SUGGEST_ABORT;
                        exit =  DRIVER_SENSE | SUGGEST_ABORT;
                        break;
                        break;
                    default:
                    default:
                        printk ("Internal error %s %d \n", __FILE__,
                        printk ("Internal error %s %d \n", __FILE__,
                                __LINE__);
                                __LINE__);
                    }
                    }
                } /* end WAS_SENSE */
                } /* end WAS_SENSE */
                else
                else
                {
                {
#ifdef DEBUG
#ifdef DEBUG
                    printk("COMMAND COMPLETE message returned, "
                    printk("COMMAND COMPLETE message returned, "
                           "status = FINISHED. \n");
                           "status = FINISHED. \n");
#endif
#endif
                    exit =  DRIVER_OK;
                    exit =  DRIVER_OK;
                    status = FINISHED;
                    status = FINISHED;
                }
                }
                break;
                break;
 
 
            case CHECK_CONDITION:
            case CHECK_CONDITION:
            case COMMAND_TERMINATED:
            case COMMAND_TERMINATED:
                switch (check_sense(SCpnt))
                switch (check_sense(SCpnt))
                {
                {
                case 0:
                case 0:
                    update_timeout(SCpnt, oldto);
                    update_timeout(SCpnt, oldto);
                    status = REDO;
                    status = REDO;
                    break;
                    break;
                case SUGGEST_REMAP:
                case SUGGEST_REMAP:
                    status = FINISHED;
                    status = FINISHED;
                    exit =  DRIVER_SENSE | SUGGEST_ABORT;
                    exit =  DRIVER_SENSE | SUGGEST_ABORT;
                    break;
                    break;
                case SUGGEST_RETRY:
                case SUGGEST_RETRY:
                    status = MAYREDO;
                    status = MAYREDO;
                    exit = DRIVER_SENSE | SUGGEST_RETRY;
                    exit = DRIVER_SENSE | SUGGEST_RETRY;
                    break;
                    break;
                case SUGGEST_ABORT:
                case SUGGEST_ABORT:
                    status = FINISHED;
                    status = FINISHED;
                    exit =  DRIVER_SENSE | SUGGEST_ABORT;
                    exit =  DRIVER_SENSE | SUGGEST_ABORT;
                    break;
                    break;
                case SUGGEST_SENSE:
                case SUGGEST_SENSE:
                    scsi_request_sense (SCpnt);
                    scsi_request_sense (SCpnt);
                    status = PENDING;
                    status = PENDING;
                    break;
                    break;
                }
                }
                break;
                break;
 
 
            case CONDITION_GOOD:
            case CONDITION_GOOD:
            case INTERMEDIATE_GOOD:
            case INTERMEDIATE_GOOD:
            case INTERMEDIATE_C_GOOD:
            case INTERMEDIATE_C_GOOD:
                break;
                break;
 
 
            case BUSY:
            case BUSY:
            case QUEUE_FULL:
            case QUEUE_FULL:
                update_timeout(SCpnt, oldto);
                update_timeout(SCpnt, oldto);
                status = REDO;
                status = REDO;
                break;
                break;
 
 
            case RESERVATION_CONFLICT:
            case RESERVATION_CONFLICT:
                printk("scsi%d, channel %d : RESERVATION CONFLICT performing"
                printk("scsi%d, channel %d : RESERVATION CONFLICT performing"
                       " reset.\n", SCpnt->host->host_no, SCpnt->channel);
                       " reset.\n", SCpnt->host->host_no, SCpnt->channel);
                scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
                scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
                return;
                return;
#if 0
#if 0
                exit = DRIVER_SOFT | SUGGEST_ABORT;
                exit = DRIVER_SOFT | SUGGEST_ABORT;
                status = MAYREDO;
                status = MAYREDO;
                break;
                break;
#endif
#endif
            default:
            default:
                printk ("Internal error %s %d \n"
                printk ("Internal error %s %d \n"
                        "status byte = %d \n", __FILE__,
                        "status byte = %d \n", __FILE__,
                        __LINE__, status_byte(result));
                        __LINE__, status_byte(result));
 
 
            }
            }
            break;
            break;
        default:
        default:
            panic("scsi: unsupported message byte %d received\n",
            panic("scsi: unsupported message byte %d received\n",
                  msg_byte(result));
                  msg_byte(result));
        }
        }
        break;
        break;
    case DID_TIME_OUT:
    case DID_TIME_OUT:
#ifdef DEBUG
#ifdef DEBUG
        printk("Host returned DID_TIME_OUT - ");
        printk("Host returned DID_TIME_OUT - ");
#endif
#endif
 
 
        if (SCpnt->flags & WAS_TIMEDOUT)
        if (SCpnt->flags & WAS_TIMEDOUT)
        {
        {
#ifdef DEBUG
#ifdef DEBUG
            printk("Aborting\n");
            printk("Aborting\n");
#endif
#endif
            /*
            /*
              Allow TEST_UNIT_READY and INQUIRY commands to timeout early
              Allow TEST_UNIT_READY and INQUIRY commands to timeout early
              without causing resets.  All other commands should be retried.
              without causing resets.  All other commands should be retried.
            */
            */
            if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
            if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
                SCpnt->cmnd[0] != INQUIRY)
                SCpnt->cmnd[0] != INQUIRY)
                    status = MAYREDO;
                    status = MAYREDO;
            exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
            exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
        }
        }
        else
        else
        {
        {
#ifdef DEBUG
#ifdef DEBUG
            printk ("Retrying.\n");
            printk ("Retrying.\n");
#endif
#endif
            SCpnt->flags  |= WAS_TIMEDOUT;
            SCpnt->flags  |= WAS_TIMEDOUT;
            SCpnt->internal_timeout &= ~IN_ABORT;
            SCpnt->internal_timeout &= ~IN_ABORT;
            status = REDO;
            status = REDO;
        }
        }
        break;
        break;
    case DID_BUS_BUSY:
    case DID_BUS_BUSY:
    case DID_PARITY:
    case DID_PARITY:
        status = REDO;
        status = REDO;
        break;
        break;
    case DID_NO_CONNECT:
    case DID_NO_CONNECT:
#ifdef DEBUG
#ifdef DEBUG
        printk("Couldn't connect.\n");
        printk("Couldn't connect.\n");
#endif
#endif
        exit  = (DRIVER_HARD | SUGGEST_ABORT);
        exit  = (DRIVER_HARD | SUGGEST_ABORT);
        break;
        break;
    case DID_ERROR:
    case DID_ERROR:
        status = MAYREDO;
        status = MAYREDO;
        exit = (DRIVER_HARD | SUGGEST_ABORT);
        exit = (DRIVER_HARD | SUGGEST_ABORT);
        break;
        break;
    case DID_BAD_TARGET:
    case DID_BAD_TARGET:
    case DID_ABORT:
    case DID_ABORT:
        exit = (DRIVER_INVALID | SUGGEST_ABORT);
        exit = (DRIVER_INVALID | SUGGEST_ABORT);
        break;
        break;
    case DID_RESET:
    case DID_RESET:
        if (SCpnt->flags & IS_RESETTING)
        if (SCpnt->flags & IS_RESETTING)
        {
        {
            SCpnt->flags &= ~IS_RESETTING;
            SCpnt->flags &= ~IS_RESETTING;
            status = REDO;
            status = REDO;
            break;
            break;
        }
        }
 
 
        if(msg_byte(result) == GOOD &&
        if(msg_byte(result) == GOOD &&
           status_byte(result) == CHECK_CONDITION) {
           status_byte(result) == CHECK_CONDITION) {
            switch (check_sense(SCpnt)) {
            switch (check_sense(SCpnt)) {
            case 0:
            case 0:
                update_timeout(SCpnt, oldto);
                update_timeout(SCpnt, oldto);
                status = REDO;
                status = REDO;
                break;
                break;
            case SUGGEST_REMAP:
            case SUGGEST_REMAP:
            case SUGGEST_RETRY:
            case SUGGEST_RETRY:
                status = MAYREDO;
                status = MAYREDO;
                exit = DRIVER_SENSE | SUGGEST_RETRY;
                exit = DRIVER_SENSE | SUGGEST_RETRY;
                break;
                break;
            case SUGGEST_ABORT:
            case SUGGEST_ABORT:
                status = FINISHED;
                status = FINISHED;
                exit =  DRIVER_SENSE | SUGGEST_ABORT;
                exit =  DRIVER_SENSE | SUGGEST_ABORT;
                break;
                break;
            case SUGGEST_SENSE:
            case SUGGEST_SENSE:
                scsi_request_sense (SCpnt);
                scsi_request_sense (SCpnt);
                status = PENDING;
                status = PENDING;
                break;
                break;
            }
            }
        } else {
        } else {
            status=REDO;
            status=REDO;
            exit = SUGGEST_RETRY;
            exit = SUGGEST_RETRY;
        }
        }
        break;
        break;
    default :
    default :
        exit = (DRIVER_ERROR | SUGGEST_DIE);
        exit = (DRIVER_ERROR | SUGGEST_DIE);
    }
    }
 
 
    switch (status)
    switch (status)
    {
    {
    case FINISHED:
    case FINISHED:
    case PENDING:
    case PENDING:
        break;
        break;
    case MAYREDO:
    case MAYREDO:
#ifdef DEBUG
#ifdef DEBUG
        printk("In MAYREDO, allowing %d retries, have %d\n",
        printk("In MAYREDO, allowing %d retries, have %d\n",
               SCpnt->allowed, SCpnt->retries);
               SCpnt->allowed, SCpnt->retries);
#endif
#endif
        if ((++SCpnt->retries) < SCpnt->allowed)
        if ((++SCpnt->retries) < SCpnt->allowed)
        {
        {
            if ((SCpnt->retries >= (SCpnt->allowed >> 1))
            if ((SCpnt->retries >= (SCpnt->allowed >> 1))
                && !(SCpnt->host->last_reset > 0 &&
                && !(SCpnt->host->last_reset > 0 &&
                     jiffies < SCpnt->host->last_reset + MIN_RESET_PERIOD)
                     jiffies < SCpnt->host->last_reset + MIN_RESET_PERIOD)
                && !(SCpnt->flags & WAS_RESET))
                && !(SCpnt->flags & WAS_RESET))
            {
            {
                printk("scsi%d channel %d : resetting for second half of retries.\n",
                printk("scsi%d channel %d : resetting for second half of retries.\n",
                       SCpnt->host->host_no, SCpnt->channel);
                       SCpnt->host->host_no, SCpnt->channel);
                scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
                scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
                break;
                break;
            }
            }
 
 
        }
        }
        else
        else
        {
        {
            status = FINISHED;
            status = FINISHED;
            break;
            break;
        }
        }
        /* fall through to REDO */
        /* fall through to REDO */
 
 
    case REDO:
    case REDO:
 
 
        if (SCpnt->flags & WAS_SENSE)
        if (SCpnt->flags & WAS_SENSE)
            scsi_request_sense(SCpnt);
            scsi_request_sense(SCpnt);
        else
        else
        {
        {
            memcpy ((void *) SCpnt->cmnd,
            memcpy ((void *) SCpnt->cmnd,
                    (void*) SCpnt->data_cmnd,
                    (void*) SCpnt->data_cmnd,
                    sizeof(SCpnt->data_cmnd));
                    sizeof(SCpnt->data_cmnd));
            SCpnt->request_buffer = SCpnt->buffer;
            SCpnt->request_buffer = SCpnt->buffer;
            SCpnt->request_bufflen = SCpnt->bufflen;
            SCpnt->request_bufflen = SCpnt->bufflen;
            SCpnt->use_sg = SCpnt->old_use_sg;
            SCpnt->use_sg = SCpnt->old_use_sg;
            SCpnt->cmd_len = SCpnt->old_cmd_len;
            SCpnt->cmd_len = SCpnt->old_cmd_len;
            internal_cmnd (SCpnt);
            internal_cmnd (SCpnt);
        }
        }
        break;
        break;
    default:
    default:
        INTERNAL_ERROR;
        INTERNAL_ERROR;
    }
    }
 
 
    if (status == FINISHED) {
    if (status == FINISHED) {
#ifdef DEBUG
#ifdef DEBUG
        printk("Calling done function - at address %p\n", SCpnt->done);
        printk("Calling done function - at address %p\n", SCpnt->done);
#endif
#endif
        host->host_busy--; /* Indicate that we are free */
        host->host_busy--; /* Indicate that we are free */
 
 
        if (host->block && host->host_busy == 0) {
        if (host->block && host->host_busy == 0) {
            host_active = NULL;
            host_active = NULL;
 
 
            /* For block devices "wake_up" is done in end_scsi_request */
            /* For block devices "wake_up" is done in end_scsi_request */
            if (MAJOR(SCpnt->request.rq_dev) != SCSI_DISK_MAJOR &&
            if (MAJOR(SCpnt->request.rq_dev) != SCSI_DISK_MAJOR &&
                MAJOR(SCpnt->request.rq_dev) != SCSI_CDROM_MAJOR) {
                MAJOR(SCpnt->request.rq_dev) != SCSI_CDROM_MAJOR) {
                struct Scsi_Host * next;
                struct Scsi_Host * next;
 
 
                for (next = host->block; next != host; next = next->block)
                for (next = host->block; next != host; next = next->block)
                    wake_up(&next->host_wait);
                    wake_up(&next->host_wait);
            }
            }
 
 
        }
        }
 
 
        wake_up(&host->host_wait);
        wake_up(&host->host_wait);
        SCpnt->result = result | ((exit & 0xff) << 24);
        SCpnt->result = result | ((exit & 0xff) << 24);
        SCpnt->use_sg = SCpnt->old_use_sg;
        SCpnt->use_sg = SCpnt->old_use_sg;
        SCpnt->cmd_len = SCpnt->old_cmd_len;
        SCpnt->cmd_len = SCpnt->old_cmd_len;
        memcpy ((void *) SCpnt->cmnd,
        memcpy ((void *) SCpnt->cmnd,
                (void*) SCpnt->data_cmnd,
                (void*) SCpnt->data_cmnd,
                sizeof(SCpnt->data_cmnd));
                sizeof(SCpnt->data_cmnd));
        SCpnt->done (SCpnt);
        SCpnt->done (SCpnt);
    }
    }
 
 
#undef FINISHED
#undef FINISHED
#undef REDO
#undef REDO
#undef MAYREDO
#undef MAYREDO
#undef PENDING
#undef PENDING
}
}
 
 
/*
/*
 * The scsi_abort function interfaces with the abort() function of the host
 * The scsi_abort function interfaces with the abort() function of the host
 * we are aborting, and causes the current command to not complete.  The
 * we are aborting, and causes the current command to not complete.  The
 * caller should deal with any error messages or status returned on the
 * caller should deal with any error messages or status returned on the
 * next call.
 * next call.
 *
 *
 * This will not be called reentrantly for a given host.
 * This will not be called reentrantly for a given host.
 */
 */
 
 
/*
/*
 * Since we're nice guys and specified that abort() and reset()
 * Since we're nice guys and specified that abort() and reset()
 * can be non-reentrant.  The internal_timeout flags are used for
 * can be non-reentrant.  The internal_timeout flags are used for
 * this.
 * this.
 */
 */
 
 
 
 
int scsi_abort (Scsi_Cmnd * SCpnt, int why)
int scsi_abort (Scsi_Cmnd * SCpnt, int why)
{
{
    int oldto;
    int oldto;
    unsigned long flags;
    unsigned long flags;
    struct Scsi_Host * host = SCpnt->host;
    struct Scsi_Host * host = SCpnt->host;
 
 
    while(1)
    while(1)
    {
    {
        save_flags_cli(flags);
        save_flags_cli(flags);
 
 
        /*
        /*
         * Protect against races here.  If the command is done, or we are
         * Protect against races here.  If the command is done, or we are
         * on a different command forget it.
         * on a different command forget it.
         */
         */
        if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
        if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
            restore_flags(flags);
            restore_flags(flags);
            return 0;
            return 0;
        }
        }
 
 
        if (SCpnt->internal_timeout & IN_ABORT)
        if (SCpnt->internal_timeout & IN_ABORT)
        {
        {
            restore_flags(flags);
            restore_flags(flags);
            while (SCpnt->internal_timeout & IN_ABORT)
            while (SCpnt->internal_timeout & IN_ABORT)
                barrier();
                barrier();
        }
        }
        else
        else
        {
        {
            SCpnt->internal_timeout |= IN_ABORT;
            SCpnt->internal_timeout |= IN_ABORT;
            oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
            oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
 
 
            if ((SCpnt->flags & IS_RESETTING) && SCpnt->device->soft_reset) {
            if ((SCpnt->flags & IS_RESETTING) && SCpnt->device->soft_reset) {
                /* OK, this command must have died when we did the
                /* OK, this command must have died when we did the
                 *  reset.  The device itself must have lied.
                 *  reset.  The device itself must have lied.
                 */
                 */
                printk("Stale command on %d %d:%d appears to have died when"
                printk("Stale command on %d %d:%d appears to have died when"
                       " the bus was reset\n",
                       " the bus was reset\n",
                       SCpnt->channel, SCpnt->target, SCpnt->lun);
                       SCpnt->channel, SCpnt->target, SCpnt->lun);
            }
            }
 
 
            restore_flags(flags);
            restore_flags(flags);
            if (!host->host_busy) {
            if (!host->host_busy) {
                SCpnt->internal_timeout &= ~IN_ABORT;
                SCpnt->internal_timeout &= ~IN_ABORT;
                update_timeout(SCpnt, oldto);
                update_timeout(SCpnt, oldto);
                return 0;
                return 0;
            }
            }
            printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
            printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
                   " channel %d, id %d, lun %d ",
                   " channel %d, id %d, lun %d ",
                   SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel,
                   SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel,
                   (int) SCpnt->target, (int) SCpnt->lun);
                   (int) SCpnt->target, (int) SCpnt->lun);
            print_command (SCpnt->cmnd);
            print_command (SCpnt->cmnd);
            if (SCpnt->serial_number != SCpnt->serial_number_at_timeout)
            if (SCpnt->serial_number != SCpnt->serial_number_at_timeout)
                return 0;
                return 0;
            SCpnt->abort_reason = why;
            SCpnt->abort_reason = why;
            switch(host->hostt->abort(SCpnt)) {
            switch(host->hostt->abort(SCpnt)) {
                /* We do not know how to abort.  Try waiting another
                /* We do not know how to abort.  Try waiting another
                 * time increment and see if this helps. Set the
                 * time increment and see if this helps. Set the
                 * WAS_TIMEDOUT flag set so we do not try this twice
                 * WAS_TIMEDOUT flag set so we do not try this twice
                 */
                 */
            case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
            case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
                                   * this is too severe
                                   * this is too severe
                                   */
                                   */
            case SCSI_ABORT_SNOOZE:
            case SCSI_ABORT_SNOOZE:
                if(why == DID_TIME_OUT) {
                if(why == DID_TIME_OUT) {
                    save_flags_cli(flags);
                    save_flags_cli(flags);
                    SCpnt->internal_timeout &= ~IN_ABORT;
                    SCpnt->internal_timeout &= ~IN_ABORT;
                    if(SCpnt->flags & WAS_TIMEDOUT) {
                    if(SCpnt->flags & WAS_TIMEDOUT) {
                        restore_flags(flags);
                        restore_flags(flags);
                        return 1; /* Indicate we cannot handle this.
                        return 1; /* Indicate we cannot handle this.
                                   * We drop down into the reset handler
                                   * We drop down into the reset handler
                                   * and try again
                                   * and try again
                                   */
                                   */
                    } else {
                    } else {
                        SCpnt->flags |= WAS_TIMEDOUT;
                        SCpnt->flags |= WAS_TIMEDOUT;
                        oldto = SCpnt->timeout_per_command;
                        oldto = SCpnt->timeout_per_command;
                        update_timeout(SCpnt, oldto);
                        update_timeout(SCpnt, oldto);
                    }
                    }
                    restore_flags(flags);
                    restore_flags(flags);
                }
                }
                return 0;
                return 0;
            case SCSI_ABORT_PENDING:
            case SCSI_ABORT_PENDING:
                if(why != DID_TIME_OUT) {
                if(why != DID_TIME_OUT) {
                    save_flags_cli(flags);
                    save_flags_cli(flags);
                    update_timeout(SCpnt, oldto);
                    update_timeout(SCpnt, oldto);
                    restore_flags(flags);
                    restore_flags(flags);
                }
                }
                return 0;
                return 0;
            case SCSI_ABORT_SUCCESS:
            case SCSI_ABORT_SUCCESS:
                /* We should have already aborted this one.  No
                /* We should have already aborted this one.  No
                 * need to adjust timeout
                 * need to adjust timeout
                 */
                 */
                 SCpnt->internal_timeout &= ~IN_ABORT;
                 SCpnt->internal_timeout &= ~IN_ABORT;
                 return 0;
                 return 0;
            case SCSI_ABORT_NOT_RUNNING:
            case SCSI_ABORT_NOT_RUNNING:
                SCpnt->internal_timeout &= ~IN_ABORT;
                SCpnt->internal_timeout &= ~IN_ABORT;
                update_timeout(SCpnt, 0);
                update_timeout(SCpnt, 0);
                return 0;
                return 0;
            case SCSI_ABORT_ERROR:
            case SCSI_ABORT_ERROR:
            default:
            default:
                SCpnt->internal_timeout &= ~IN_ABORT;
                SCpnt->internal_timeout &= ~IN_ABORT;
                return 1;
                return 1;
            }
            }
        }
        }
    }
    }
}
}
 
 
 
 
/* Mark a single SCSI Device as having been reset. */
/* Mark a single SCSI Device as having been reset. */
 
 
static inline void scsi_mark_device_reset(Scsi_Device *Device)
static inline void scsi_mark_device_reset(Scsi_Device *Device)
{
{
  Device->was_reset = 1;
  Device->was_reset = 1;
  Device->expecting_cc_ua = 1;
  Device->expecting_cc_ua = 1;
}
}
 
 
 
 
/* Mark all SCSI Devices on a specific Host as having been reset. */
/* Mark all SCSI Devices on a specific Host as having been reset. */
 
 
void scsi_mark_host_reset(struct Scsi_Host *Host)
void scsi_mark_host_reset(struct Scsi_Host *Host)
{
{
  Scsi_Cmnd *SCpnt;
  Scsi_Cmnd *SCpnt;
  for (SCpnt = Host->host_queue; SCpnt; SCpnt = SCpnt->next)
  for (SCpnt = Host->host_queue; SCpnt; SCpnt = SCpnt->next)
      scsi_mark_device_reset(SCpnt->device);
      scsi_mark_device_reset(SCpnt->device);
}
}
 
 
 
 
/* Mark all SCSI Devices on a specific Host Bus as having been reset. */
/* Mark all SCSI Devices on a specific Host Bus as having been reset. */
 
 
void scsi_mark_bus_reset(struct Scsi_Host *Host, int channel)
void scsi_mark_bus_reset(struct Scsi_Host *Host, int channel)
{
{
  Scsi_Cmnd *SCpnt;
  Scsi_Cmnd *SCpnt;
  for (SCpnt = Host->host_queue; SCpnt; SCpnt = SCpnt->next)
  for (SCpnt = Host->host_queue; SCpnt; SCpnt = SCpnt->next)
      if (SCpnt->channel == channel)
      if (SCpnt->channel == channel)
          scsi_mark_device_reset(SCpnt->device);
          scsi_mark_device_reset(SCpnt->device);
}
}
 
 
 
 
int scsi_reset (Scsi_Cmnd * SCpnt, unsigned int reset_flags)
int scsi_reset (Scsi_Cmnd * SCpnt, unsigned int reset_flags)
{
{
    int temp;
    int temp;
    unsigned long flags;
    unsigned long flags;
    Scsi_Cmnd * SCpnt1;
    Scsi_Cmnd * SCpnt1;
    struct Scsi_Host * host = SCpnt->host;
    struct Scsi_Host * host = SCpnt->host;
 
 
    printk("SCSI bus is being reset for host %d channel %d.\n",
    printk("SCSI bus is being reset for host %d channel %d.\n",
           host->host_no, SCpnt->channel);
           host->host_no, SCpnt->channel);
 
 
#if 0
#if 0
    /*
    /*
     * First of all, we need to make a recommendation to the low-level
     * First of all, we need to make a recommendation to the low-level
     * driver as to whether a BUS_DEVICE_RESET should be performed,
     * driver as to whether a BUS_DEVICE_RESET should be performed,
     * or whether we should do a full BUS_RESET.  There is no simple
     * or whether we should do a full BUS_RESET.  There is no simple
     * algorithm here - we basically use a series of heuristics
     * algorithm here - we basically use a series of heuristics
     * to determine what we should do.
     * to determine what we should do.
     */
     */
    SCpnt->host->suggest_bus_reset = FALSE;
    SCpnt->host->suggest_bus_reset = FALSE;
 
 
    /*
    /*
     * First see if all of the active devices on the bus have
     * First see if all of the active devices on the bus have
     * been jammed up so that we are attempting resets.  If so,
     * been jammed up so that we are attempting resets.  If so,
     * then suggest a bus reset.  Forcing a bus reset could
     * then suggest a bus reset.  Forcing a bus reset could
     * result in some race conditions, but no more than
     * result in some race conditions, but no more than
     * you would usually get with timeouts.  We will cross
     * you would usually get with timeouts.  We will cross
     * that bridge when we come to it.
     * that bridge when we come to it.
     *
     *
     * This is actually a pretty bad idea, since a sequence of
     * This is actually a pretty bad idea, since a sequence of
     * commands will often timeout together and this will cause a
     * commands will often timeout together and this will cause a
     * Bus Device Reset followed immediately by a SCSI Bus Reset.
     * Bus Device Reset followed immediately by a SCSI Bus Reset.
     * If all of the active devices really are jammed up, the
     * If all of the active devices really are jammed up, the
     * Bus Device Reset will quickly timeout and scsi_times_out
     * Bus Device Reset will quickly timeout and scsi_times_out
     * will follow up with a SCSI Bus Reset anyway.
     * will follow up with a SCSI Bus Reset anyway.
     */
     */
    SCpnt1 = host->host_queue;
    SCpnt1 = host->host_queue;
    while(SCpnt1) {
    while(SCpnt1) {
        if( SCpnt1->request.rq_status != RQ_INACTIVE
        if( SCpnt1->request.rq_status != RQ_INACTIVE
            && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0 )
            && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0 )
                break;
                break;
        SCpnt1 = SCpnt1->next;
        SCpnt1 = SCpnt1->next;
        }
        }
    if( SCpnt1 == NULL ) {
    if( SCpnt1 == NULL ) {
        reset_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
        reset_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
    }
    }
 
 
    /*
    /*
     * If the code that called us is suggesting a hard reset, then
     * If the code that called us is suggesting a hard reset, then
     * definitely request it.  This usually occurs because a
     * definitely request it.  This usually occurs because a
     * BUS_DEVICE_RESET times out.
     * BUS_DEVICE_RESET times out.
     *
     *
     * Passing reset_flags along takes care of this automatically.
     * Passing reset_flags along takes care of this automatically.
     */
     */
    if( reset_flags & SCSI_RESET_SUGGEST_BUS_RESET ) {
    if( reset_flags & SCSI_RESET_SUGGEST_BUS_RESET ) {
        SCpnt->host->suggest_bus_reset = TRUE;
        SCpnt->host->suggest_bus_reset = TRUE;
    }
    }
#endif
#endif
 
 
    while (1) {
    while (1) {
        save_flags_cli(flags);
        save_flags_cli(flags);
 
 
        /*
        /*
         * Protect against races here.  If the command is done, or we are
         * Protect against races here.  If the command is done, or we are
         * on a different command forget it.
         * on a different command forget it.
         */
         */
        if (reset_flags & SCSI_RESET_ASYNCHRONOUS)
        if (reset_flags & SCSI_RESET_ASYNCHRONOUS)
          if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
          if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
            restore_flags(flags);
            restore_flags(flags);
            return 0;
            return 0;
          }
          }
 
 
        if (SCpnt->internal_timeout & IN_RESET)
        if (SCpnt->internal_timeout & IN_RESET)
        {
        {
            restore_flags(flags);
            restore_flags(flags);
            while (SCpnt->internal_timeout & IN_RESET)
            while (SCpnt->internal_timeout & IN_RESET)
                barrier();
                barrier();
        }
        }
        else
        else
        {
        {
            SCpnt->internal_timeout |= IN_RESET;
            SCpnt->internal_timeout |= IN_RESET;
            update_timeout(SCpnt, RESET_TIMEOUT);
            update_timeout(SCpnt, RESET_TIMEOUT);
 
 
            if (host->host_busy)
            if (host->host_busy)
            {
            {
                restore_flags(flags);
                restore_flags(flags);
                SCpnt1 = host->host_queue;
                SCpnt1 = host->host_queue;
                while(SCpnt1) {
                while(SCpnt1) {
                    if (SCpnt1->request.rq_status != RQ_INACTIVE) {
                    if (SCpnt1->request.rq_status != RQ_INACTIVE) {
#if 0
#if 0
                        if (!(SCpnt1->flags & IS_RESETTING) &&
                        if (!(SCpnt1->flags & IS_RESETTING) &&
                            !(SCpnt1->internal_timeout & IN_ABORT))
                            !(SCpnt1->internal_timeout & IN_ABORT))
                            scsi_abort(SCpnt1, DID_RESET);
                            scsi_abort(SCpnt1, DID_RESET);
#endif
#endif
                        SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
                        SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
                    }
                    }
                    SCpnt1 = SCpnt1->next;
                    SCpnt1 = SCpnt1->next;
                }
                }
 
 
                host->last_reset = jiffies;
                host->last_reset = jiffies;
                temp = host->hostt->reset(SCpnt, reset_flags);
                temp = host->hostt->reset(SCpnt, reset_flags);
                /*
                /*
                  This test allows the driver to introduce an additional bus
                  This test allows the driver to introduce an additional bus
                  settle time delay by setting last_reset up to 20 seconds in
                  settle time delay by setting last_reset up to 20 seconds in
                  the future.  In the normal case where the driver does not
                  the future.  In the normal case where the driver does not
                  modify last_reset, it must be assumed that the actual bus
                  modify last_reset, it must be assumed that the actual bus
                  reset occurred immediately prior to the return to this code,
                  reset occurred immediately prior to the return to this code,
                  and so last_reset must be updated to the current time, so
                  and so last_reset must be updated to the current time, so
                  that the delay in internal_cmnd will guarantee at least a
                  that the delay in internal_cmnd will guarantee at least a
                  MIN_RESET_DELAY bus settle time.
                  MIN_RESET_DELAY bus settle time.
                */
                */
                if ((host->last_reset < jiffies) ||
                if ((host->last_reset < jiffies) ||
                    (host->last_reset > (jiffies + 20 * HZ)))
                    (host->last_reset > (jiffies + 20 * HZ)))
                  host->last_reset = jiffies;
                  host->last_reset = jiffies;
            }
            }
            else
            else
            {
            {
                if (!host->block) host->host_busy++;
                if (!host->block) host->host_busy++;
                restore_flags(flags);
                restore_flags(flags);
                host->last_reset = jiffies;
                host->last_reset = jiffies;
                SCpnt->flags |= (WAS_RESET | IS_RESETTING);
                SCpnt->flags |= (WAS_RESET | IS_RESETTING);
                temp = host->hostt->reset(SCpnt, reset_flags);
                temp = host->hostt->reset(SCpnt, reset_flags);
                if ((host->last_reset < jiffies) ||
                if ((host->last_reset < jiffies) ||
                    (host->last_reset > (jiffies + 20 * HZ)))
                    (host->last_reset > (jiffies + 20 * HZ)))
                  host->last_reset = jiffies;
                  host->last_reset = jiffies;
                if (!host->block) host->host_busy--;
                if (!host->block) host->host_busy--;
            }
            }
 
 
#ifdef DEBUG
#ifdef DEBUG
            printk("scsi reset function returned %d\n", temp);
            printk("scsi reset function returned %d\n", temp);
#endif
#endif
 
 
            /*
            /*
             * Now figure out what we need to do, based upon
             * Now figure out what we need to do, based upon
             * what the low level driver said that it did.
             * what the low level driver said that it did.
             * If the result is SCSI_RESET_SUCCESS, SCSI_RESET_PENDING,
             * If the result is SCSI_RESET_SUCCESS, SCSI_RESET_PENDING,
             * or SCSI_RESET_WAKEUP, then the low level driver did a
             * or SCSI_RESET_WAKEUP, then the low level driver did a
             * bus device reset or bus reset, so we should go through
             * bus device reset or bus reset, so we should go through
             * and mark one or all of the devices on that bus
             * and mark one or all of the devices on that bus
             * as having been reset.
             * as having been reset.
             */
             */
            switch(temp & SCSI_RESET_ACTION) {
            switch(temp & SCSI_RESET_ACTION) {
            case SCSI_RESET_SUCCESS:
            case SCSI_RESET_SUCCESS:
                if (temp & SCSI_RESET_HOST_RESET)
                if (temp & SCSI_RESET_HOST_RESET)
                  scsi_mark_host_reset(host);
                  scsi_mark_host_reset(host);
                else if (temp & SCSI_RESET_BUS_RESET)
                else if (temp & SCSI_RESET_BUS_RESET)
                  scsi_mark_bus_reset(host, SCpnt->channel);
                  scsi_mark_bus_reset(host, SCpnt->channel);
                else scsi_mark_device_reset(SCpnt->device);
                else scsi_mark_device_reset(SCpnt->device);
                save_flags_cli(flags);
                save_flags_cli(flags);
                SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
                SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
                restore_flags(flags);
                restore_flags(flags);
                return 0;
                return 0;
            case SCSI_RESET_PENDING:
            case SCSI_RESET_PENDING:
                if (temp & SCSI_RESET_HOST_RESET)
                if (temp & SCSI_RESET_HOST_RESET)
                  scsi_mark_host_reset(host);
                  scsi_mark_host_reset(host);
                else if (temp & SCSI_RESET_BUS_RESET)
                else if (temp & SCSI_RESET_BUS_RESET)
                  scsi_mark_bus_reset(host, SCpnt->channel);
                  scsi_mark_bus_reset(host, SCpnt->channel);
                else scsi_mark_device_reset(SCpnt->device);
                else scsi_mark_device_reset(SCpnt->device);
            case SCSI_RESET_NOT_RUNNING:
            case SCSI_RESET_NOT_RUNNING:
                return 0;
                return 0;
            case SCSI_RESET_PUNT:
            case SCSI_RESET_PUNT:
                SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
                SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
                scsi_request_sense (SCpnt);
                scsi_request_sense (SCpnt);
                return 0;
                return 0;
            case SCSI_RESET_WAKEUP:
            case SCSI_RESET_WAKEUP:
                if (temp & SCSI_RESET_HOST_RESET)
                if (temp & SCSI_RESET_HOST_RESET)
                  scsi_mark_host_reset(host);
                  scsi_mark_host_reset(host);
                else if (temp & SCSI_RESET_BUS_RESET)
                else if (temp & SCSI_RESET_BUS_RESET)
                  scsi_mark_bus_reset(host, SCpnt->channel);
                  scsi_mark_bus_reset(host, SCpnt->channel);
                else scsi_mark_device_reset(SCpnt->device);
                else scsi_mark_device_reset(SCpnt->device);
                SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
                SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
                scsi_request_sense (SCpnt);
                scsi_request_sense (SCpnt);
                /*
                /*
                 * If a bus reset was performed, we
                 * If a bus reset was performed, we
                 * need to wake up each and every command
                 * need to wake up each and every command
                 * that was active on the bus or if it was a HBA
                 * that was active on the bus or if it was a HBA
                 * reset all active commands on all channels
                 * reset all active commands on all channels
                 */
                 */
                if( temp & SCSI_RESET_HOST_RESET )
                if( temp & SCSI_RESET_HOST_RESET )
                {
                {
                    SCpnt1 = host->host_queue;
                    SCpnt1 = host->host_queue;
                    while(SCpnt1) {
                    while(SCpnt1) {
                        if (SCpnt1->request.rq_status != RQ_INACTIVE
                        if (SCpnt1->request.rq_status != RQ_INACTIVE
                            && SCpnt1 != SCpnt)
                            && SCpnt1 != SCpnt)
                            scsi_request_sense (SCpnt1);
                            scsi_request_sense (SCpnt1);
                        SCpnt1 = SCpnt1->next;
                        SCpnt1 = SCpnt1->next;
                    }
                    }
                } else if( temp & SCSI_RESET_BUS_RESET ) {
                } else if( temp & SCSI_RESET_BUS_RESET ) {
                    SCpnt1 = host->host_queue;
                    SCpnt1 = host->host_queue;
                    while(SCpnt1) {
                    while(SCpnt1) {
                        if(SCpnt1->request.rq_status != RQ_INACTIVE
                        if(SCpnt1->request.rq_status != RQ_INACTIVE
                           && SCpnt1 != SCpnt
                           && SCpnt1 != SCpnt
                           && SCpnt1->channel == SCpnt->channel)
                           && SCpnt1->channel == SCpnt->channel)
                            scsi_request_sense (SCpnt);
                            scsi_request_sense (SCpnt);
                        SCpnt1 = SCpnt1->next;
                        SCpnt1 = SCpnt1->next;
                    }
                    }
                }
                }
                return 0;
                return 0;
            case SCSI_RESET_SNOOZE:
            case SCSI_RESET_SNOOZE:
                /* In this case, we set the timeout field to 0
                /* In this case, we set the timeout field to 0
                 * so that this command does not time out any more,
                 * so that this command does not time out any more,
                 * and we return 1 so that we get a message on the
                 * and we return 1 so that we get a message on the
                 * screen.
                 * screen.
                 */
                 */
                save_flags_cli(flags);
                save_flags_cli(flags);
                SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
                SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
                update_timeout(SCpnt, 0);
                update_timeout(SCpnt, 0);
                restore_flags(flags);
                restore_flags(flags);
                /* If you snooze, you lose... */
                /* If you snooze, you lose... */
            case SCSI_RESET_ERROR:
            case SCSI_RESET_ERROR:
            default:
            default:
                return 1;
                return 1;
            }
            }
 
 
            return temp;
            return temp;
        }
        }
    }
    }
}
}
 
 
 
 
static void scsi_main_timeout(void)
static void scsi_main_timeout(void)
{
{
    /*
    /*
     * We must not enter update_timeout with a timeout condition still pending.
     * We must not enter update_timeout with a timeout condition still pending.
     */
     */
 
 
    int timed_out;
    int timed_out;
    unsigned long flags;
    unsigned long flags;
    struct Scsi_Host * host;
    struct Scsi_Host * host;
    Scsi_Cmnd * SCpnt = NULL;
    Scsi_Cmnd * SCpnt = NULL;
 
 
    save_flags_cli(flags);
    save_flags_cli(flags);
 
 
    update_timeout(NULL, 0);
    update_timeout(NULL, 0);
 
 
    /*
    /*
     * Find all timers such that they have 0 or negative (shouldn't happen)
     * Find all timers such that they have 0 or negative (shouldn't happen)
     * time remaining on them.
     * time remaining on them.
     */
     */
    timed_out = 0;
    timed_out = 0;
    for (host = scsi_hostlist; host; host = host->next) {
    for (host = scsi_hostlist; host; host = host->next) {
        for (SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
        for (SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
            if (SCpnt->timeout == -1)
            if (SCpnt->timeout == -1)
              {
              {
                SCpnt->timeout = 0;
                SCpnt->timeout = 0;
                SCpnt->serial_number_at_timeout = SCpnt->serial_number;
                SCpnt->serial_number_at_timeout = SCpnt->serial_number;
                ++timed_out;
                ++timed_out;
              }
              }
    }
    }
    if (timed_out > 0) {
    if (timed_out > 0) {
        for (host = scsi_hostlist; host; host = host->next) {
        for (host = scsi_hostlist; host; host = host->next) {
            for (SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
            for (SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
                if (SCpnt->serial_number_at_timeout > 0 &&
                if (SCpnt->serial_number_at_timeout > 0 &&
                    SCpnt->serial_number_at_timeout == SCpnt->serial_number)
                    SCpnt->serial_number_at_timeout == SCpnt->serial_number)
                  {
                  {
                    restore_flags(flags);
                    restore_flags(flags);
                    scsi_times_out(SCpnt);
                    scsi_times_out(SCpnt);
                    SCpnt->serial_number_at_timeout = 0;
                    SCpnt->serial_number_at_timeout = 0;
                    cli();
                    cli();
                  }
                  }
          }
          }
    }
    }
    restore_flags(flags);
    restore_flags(flags);
}
}
 
 
/*
/*
 * The strategy is to cause the timer code to call scsi_times_out()
 * The strategy is to cause the timer code to call scsi_times_out()
 * when the soonest timeout is pending.
 * when the soonest timeout is pending.
 * The arguments are used when we are queueing a new command, because
 * The arguments are used when we are queueing a new command, because
 * we do not want to subtract the time used from this time, but when we
 * we do not want to subtract the time used from this time, but when we
 * set the timer, we want to take this value into account.
 * set the timer, we want to take this value into account.
 */
 */
 
 
static int update_timeout(Scsi_Cmnd * SCset, int timeout)
static int update_timeout(Scsi_Cmnd * SCset, int timeout)
{
{
    unsigned int least, used;
    unsigned int least, used;
    unsigned int oldto;
    unsigned int oldto;
    unsigned long flags;
    unsigned long flags;
    struct Scsi_Host * host;
    struct Scsi_Host * host;
    Scsi_Cmnd * SCpnt = NULL;
    Scsi_Cmnd * SCpnt = NULL;
 
 
    save_flags_cli(flags);
    save_flags_cli(flags);
 
 
    oldto = 0;
    oldto = 0;
 
 
    /*
    /*
     * This routine can be a performance bottleneck under high loads, since
     * This routine can be a performance bottleneck under high loads, since
     * it is called twice per SCSI operation: once when internal_cmnd is
     * it is called twice per SCSI operation: once when internal_cmnd is
     * called, and again when scsi_done completes the command.  To limit
     * called, and again when scsi_done completes the command.  To limit
     * the load this routine can cause, we shortcut processing if no clock
     * the load this routine can cause, we shortcut processing if no clock
     * ticks have occurred since the last time it was called.
     * ticks have occurred since the last time it was called.
     */
     */
 
 
    if (jiffies == time_start && timer_table[SCSI_TIMER].expires > 0) {
    if (jiffies == time_start && timer_table[SCSI_TIMER].expires > 0) {
        if(SCset){
        if(SCset){
            oldto = SCset->timeout;
            oldto = SCset->timeout;
            SCset->timeout = timeout;
            SCset->timeout = timeout;
            if (timeout > 0 &&
            if (timeout > 0 &&
                jiffies + timeout < timer_table[SCSI_TIMER].expires)
                jiffies + timeout < timer_table[SCSI_TIMER].expires)
                    timer_table[SCSI_TIMER].expires = jiffies + timeout;
                    timer_table[SCSI_TIMER].expires = jiffies + timeout;
        }
        }
        restore_flags(flags);
        restore_flags(flags);
        return oldto;
        return oldto;
    }
    }
 
 
    /*
    /*
     * Figure out how much time has passed since the last time the timeouts
     * Figure out how much time has passed since the last time the timeouts
     * were updated
     * were updated
     */
     */
    used = (time_start) ? (jiffies - time_start) : 0;
    used = (time_start) ? (jiffies - time_start) : 0;
 
 
    /*
    /*
     * Find out what is due to timeout soonest, and adjust all timeouts for
     * Find out what is due to timeout soonest, and adjust all timeouts for
     * the amount of time that has passed since the last time we called
     * the amount of time that has passed since the last time we called
     * update_timeout.
     * update_timeout.
     */
     */
 
 
    oldto = 0;
    oldto = 0;
 
 
    if(SCset){
    if(SCset){
        oldto = SCset->timeout - used;
        oldto = SCset->timeout - used;
        SCset->timeout = timeout;
        SCset->timeout = timeout;
    }
    }
 
 
    least = 0xffffffff;
    least = 0xffffffff;
 
 
    for(host = scsi_hostlist; host; host = host->next)
    for(host = scsi_hostlist; host; host = host->next)
        for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
        for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
            if (SCpnt->timeout > 0) {
            if (SCpnt->timeout > 0) {
                if (SCpnt != SCset)
                if (SCpnt != SCset)
                    SCpnt->timeout -= used;
                    SCpnt->timeout -= used;
                if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
                if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
                if(SCpnt->timeout > 0 && SCpnt->timeout < least)
                if(SCpnt->timeout > 0 && SCpnt->timeout < least)
                    least = SCpnt->timeout;
                    least = SCpnt->timeout;
            }
            }
 
 
    /*
    /*
     * If something is due to timeout again, then we will set the next timeout
     * If something is due to timeout again, then we will set the next timeout
     * interrupt to occur.  Otherwise, timeouts are disabled.
     * interrupt to occur.  Otherwise, timeouts are disabled.
     */
     */
 
 
    if (least != 0xffffffff)
    if (least != 0xffffffff)
    {
    {
        time_start = jiffies;
        time_start = jiffies;
        timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
        timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
        timer_active |= 1 << SCSI_TIMER;
        timer_active |= 1 << SCSI_TIMER;
    }
    }
    else
    else
    {
    {
        timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
        timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
        timer_active &= ~(1 << SCSI_TIMER);
        timer_active &= ~(1 << SCSI_TIMER);
    }
    }
    restore_flags(flags);
    restore_flags(flags);
    return oldto;
    return oldto;
}
}
 
 
#ifdef CONFIG_MODULES
#ifdef CONFIG_MODULES
static int scsi_register_host(Scsi_Host_Template *);
static int scsi_register_host(Scsi_Host_Template *);
static void scsi_unregister_host(Scsi_Host_Template *);
static void scsi_unregister_host(Scsi_Host_Template *);
#endif
#endif
 
 
void *scsi_malloc(unsigned int len)
void *scsi_malloc(unsigned int len)
{
{
#ifndef LARGE_MALLOC
#ifndef LARGE_MALLOC
    unsigned int nbits, mask;
    unsigned int nbits, mask;
    unsigned long flags;
    unsigned long flags;
    int i, j;
    int i, j;
    if(len % SECTOR_SIZE != 0 || len > PAGE_SIZE)
    if(len % SECTOR_SIZE != 0 || len > PAGE_SIZE)
        return NULL;
        return NULL;
 
 
    save_flags_cli(flags);
    save_flags_cli(flags);
    nbits = len >> 9;
    nbits = len >> 9;
    mask = (1 << nbits) - 1;
    mask = (1 << nbits) - 1;
 
 
    for(i=0;i < dma_sectors / SECTORS_PER_PAGE; i++)
    for(i=0;i < dma_sectors / SECTORS_PER_PAGE; i++)
        for(j=0; j<=SECTORS_PER_PAGE - nbits; j++){
        for(j=0; j<=SECTORS_PER_PAGE - nbits; j++){
            if ((dma_malloc_freelist[i] & (mask << j)) == 0){
            if ((dma_malloc_freelist[i] & (mask << j)) == 0){
                dma_malloc_freelist[i] |= (mask << j);
                dma_malloc_freelist[i] |= (mask << j);
                restore_flags(flags);
                restore_flags(flags);
                dma_free_sectors -= nbits;
                dma_free_sectors -= nbits;
#ifdef DEBUG
#ifdef DEBUG
                printk("SMalloc: %d %p\n",len, dma_malloc_pages[i] + (j << 9));
                printk("SMalloc: %d %p\n",len, dma_malloc_pages[i] + (j << 9));
#endif
#endif
                return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
                return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
            }
            }
        }
        }
    restore_flags(flags);
    restore_flags(flags);
    return NULL;  /* Nope.  No more */
    return NULL;  /* Nope.  No more */
#else
#else
    unsigned int nbits;
    unsigned int nbits;
    unsigned long maskl, maskh, flags;
    unsigned long maskl, maskh, flags;
    FreeSectorBitmap *fsb;
    FreeSectorBitmap *fsb;
    int i;
    int i;
 
 
    if (len % SECTOR_SIZE != 0 || len > PAGE_SIZE)
    if (len % SECTOR_SIZE != 0 || len > PAGE_SIZE)
        return NULL;
        return NULL;
 
 
    save_flags_cli (flags);
    save_flags_cli (flags);
    nbits = len >> 9;
    nbits = len >> 9;
    if (nbits < 32) {
    if (nbits < 32) {
        maskl = (1 << nbits) - 1;
        maskl = (1 << nbits) - 1;
        maskh = 0;
        maskh = 0;
    } else {
    } else {
        maskl = (unsigned long)-1;
        maskl = (unsigned long)-1;
        maskh = (1 << (nbits - 32)) - 1;
        maskh = (1 << (nbits - 32)) - 1;
    }
    }
 
 
    fsb = dma_malloc_freelist;
    fsb = dma_malloc_freelist;
 
 
    for (i = 0; i < dma_sectors / SECTORS_PER_PAGE; i++) {
    for (i = 0; i < dma_sectors / SECTORS_PER_PAGE; i++) {
        unsigned long mml, mmh;
        unsigned long mml, mmh;
        int j;
        int j;
        mml = maskl;
        mml = maskl;
        mmh = maskh;
        mmh = maskh;
        j = 0;
        j = 0;
        do {
        do {
            if ((fsb->l & mml) == 0 && (fsb->h & mmh) == 0) {
            if ((fsb->l & mml) == 0 && (fsb->h & mmh) == 0) {
                fsb->h |= mmh;
                fsb->h |= mmh;
                fsb->l |= mml;
                fsb->l |= mml;
                restore_flags (flags);
                restore_flags (flags);
                dma_free_sectors -= nbits;
                dma_free_sectors -= nbits;
#ifdef DEBUG
#ifdef DEBUG
                printk("SMalloc: %d %p\n",len, dma_malloc_pages[i] + (j << 9));
                printk("SMalloc: %d %p\n",len, dma_malloc_pages[i] + (j << 9));
#endif
#endif
                return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
                return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
            }
            }
            mmh = (mmh << 1) | (mml >> 31);
            mmh = (mmh << 1) | (mml >> 31);
            mml <<= 1;
            mml <<= 1;
            j++;
            j++;
        } while (!(mmh & (1 << 31)));
        } while (!(mmh & (1 << 31)));
        fsb ++;
        fsb ++;
    }
    }
    restore_flags(flags);
    restore_flags(flags);
    return NULL;  /* Nope.  No more */
    return NULL;  /* Nope.  No more */
#endif
#endif
}
}
 
 
int scsi_free(void *obj, unsigned int len)
int scsi_free(void *obj, unsigned int len)
{
{
#ifndef LARGE_MALLOC
#ifndef LARGE_MALLOC
    unsigned int page, sector, nbits, mask;
    unsigned int page, sector, nbits, mask;
    unsigned long flags;
    unsigned long flags;
 
 
#ifdef DEBUG
#ifdef DEBUG
    unsigned long ret = 0;
    unsigned long ret = 0;
 
 
#ifdef __mips__
#ifdef __mips__
    __asm__ __volatile__ ("move\t%0,$31":"=r"(ret));
    __asm__ __volatile__ ("move\t%0,$31":"=r"(ret));
#else
#else
   ret = __builtin_return_address(0);
   ret = __builtin_return_address(0);
#endif
#endif
    printk("scsi_free %p %d\n",obj, len);
    printk("scsi_free %p %d\n",obj, len);
#endif
#endif
 
 
    for (page = 0; page < dma_sectors / SECTORS_PER_PAGE; page++) {
    for (page = 0; page < dma_sectors / SECTORS_PER_PAGE; page++) {
        unsigned long page_addr = (unsigned long) dma_malloc_pages[page];
        unsigned long page_addr = (unsigned long) dma_malloc_pages[page];
        if ((unsigned long) obj >= page_addr &&
        if ((unsigned long) obj >= page_addr &&
            (unsigned long) obj <  page_addr + PAGE_SIZE)
            (unsigned long) obj <  page_addr + PAGE_SIZE)
        {
        {
            sector = (((unsigned long) obj) - page_addr) >> 9;
            sector = (((unsigned long) obj) - page_addr) >> 9;
 
 
            nbits = len >> 9;
            nbits = len >> 9;
            mask = (1 << nbits) - 1;
            mask = (1 << nbits) - 1;
 
 
            if ((mask << sector) >= (1 << SECTORS_PER_PAGE))
            if ((mask << sector) >= (1 << SECTORS_PER_PAGE))
                panic ("scsi_free:Bad memory alignment");
                panic ("scsi_free:Bad memory alignment");
 
 
            save_flags_cli(flags);
            save_flags_cli(flags);
            if((dma_malloc_freelist[page] &
            if((dma_malloc_freelist[page] &
                (mask << sector)) != (mask<<sector)){
                (mask << sector)) != (mask<<sector)){
#ifdef DEBUG
#ifdef DEBUG
                printk("scsi_free(obj=%p, len=%d) called from %08lx\n",
                printk("scsi_free(obj=%p, len=%d) called from %08lx\n",
                       obj, len, ret);
                       obj, len, ret);
#endif
#endif
                panic("scsi_free:Trying to free unused memory");
                panic("scsi_free:Trying to free unused memory");
            }
            }
            dma_free_sectors += nbits;
            dma_free_sectors += nbits;
            dma_malloc_freelist[page] &= ~(mask << sector);
            dma_malloc_freelist[page] &= ~(mask << sector);
            restore_flags(flags);
            restore_flags(flags);
            return 0;
            return 0;
        }
        }
    }
    }
#else
#else
    unsigned int page, sector, nbits;
    unsigned int page, sector, nbits;
    unsigned long maskl, maskh, flags;
    unsigned long maskl, maskh, flags;
 
 
#ifdef DEBUG
#ifdef DEBUG
    printk("scsi_free %p %d\n",obj, len);
    printk("scsi_free %p %d\n",obj, len);
#endif
#endif
 
 
    for (page = 0; page < dma_sectors / SECTORS_PER_PAGE; page++) {
    for (page = 0; page < dma_sectors / SECTORS_PER_PAGE; page++) {
        unsigned long page_addr = (unsigned long) dma_malloc_pages[page];
        unsigned long page_addr = (unsigned long) dma_malloc_pages[page];
        if ((unsigned long) obj >= page_addr &&
        if ((unsigned long) obj >= page_addr &&
            (unsigned long) obj < page_addr + PAGE_SIZE) {
            (unsigned long) obj < page_addr + PAGE_SIZE) {
            sector = (((unsigned long) obj) - page_addr) >> 9;
            sector = (((unsigned long) obj) - page_addr) >> 9;
            nbits = len >> 9;
            nbits = len >> 9;
            if (nbits < 32) {
            if (nbits < 32) {
                maskl = (1 << nbits) - 1;
                maskl = (1 << nbits) - 1;
                maskh = 0;
                maskh = 0;
            } else {
            } else {
                maskl = (unsigned long)-1;
                maskl = (unsigned long)-1;
                maskh = (1 << (nbits - 32)) - 1;
                maskh = (1 << (nbits - 32)) - 1;
            }
            }
 
 
            if ((sector + nbits) > SECTORS_PER_PAGE)
            if ((sector + nbits) > SECTORS_PER_PAGE)
                panic ("scsi_free:Bad memory alignment");
                panic ("scsi_free:Bad memory alignment");
 
 
            maskh = (maskh << sector) | (maskl >> (32 - sector));
            maskh = (maskh << sector) | (maskl >> (32 - sector));
            maskl = maskl << sector;
            maskl = maskl << sector;
 
 
            save_flags_cli(flags);
            save_flags_cli(flags);
            if (((dma_malloc_freelist[page].l & maskl) != maskl) ||
            if (((dma_malloc_freelist[page].l & maskl) != maskl) ||
                ((dma_malloc_freelist[page].h & maskh) != maskh))
                ((dma_malloc_freelist[page].h & maskh) != maskh))
                panic("scsi_free:Trying to free unused memory");
                panic("scsi_free:Trying to free unused memory");
 
 
            dma_free_sectors += nbits;
            dma_free_sectors += nbits;
            dma_malloc_freelist[page].l &= ~maskl;
            dma_malloc_freelist[page].l &= ~maskl;
            dma_malloc_freelist[page].h &= ~maskh;
            dma_malloc_freelist[page].h &= ~maskh;
            restore_flags(flags);
            restore_flags(flags);
            return 0;
            return 0;
        }
        }
    }
    }
#endif
#endif
    panic("scsi_free:Bad offset");
    panic("scsi_free:Bad offset");
}
}
 
 
 
 
int scsi_loadable_module_flag; /* Set after we scan builtin drivers */
int scsi_loadable_module_flag; /* Set after we scan builtin drivers */
 
 
void * scsi_init_malloc(unsigned int size, int priority)
void * scsi_init_malloc(unsigned int size, int priority)
{
{
    void * retval;
    void * retval;
 
 
    /*
    /*
     * For buffers used by the DMA pool, we assume page aligned
     * For buffers used by the DMA pool, we assume page aligned
     * structures.
     * structures.
     */
     */
    if ((size % PAGE_SIZE) == 0) {
    if ((size % PAGE_SIZE) == 0) {
        int order, a_size;
        int order, a_size;
        for (order = 0, a_size = PAGE_SIZE;
        for (order = 0, a_size = PAGE_SIZE;
             a_size < size; order++, a_size <<= 1)
             a_size < size; order++, a_size <<= 1)
            ;
            ;
        retval = (void *) __get_dma_pages(priority & GFP_LEVEL_MASK,
        retval = (void *) __get_dma_pages(priority & GFP_LEVEL_MASK,
                                                    order);
                                                    order);
    } else
    } else
        retval = kmalloc(size, priority);
        retval = kmalloc(size, priority);
 
 
    if (retval)
    if (retval)
        memset(retval, 0, size);
        memset(retval, 0, size);
    return retval;
    return retval;
}
}
 
 
 
 
void scsi_init_free(char * ptr, unsigned int size)
void scsi_init_free(char * ptr, unsigned int size)
{
{
    /*
    /*
     * We need this special code here because the DMA pool assumes
     * We need this special code here because the DMA pool assumes
     * page aligned data.  Besides, it is wasteful to allocate
     * page aligned data.  Besides, it is wasteful to allocate
     * page sized chunks with kmalloc.
     * page sized chunks with kmalloc.
     */
     */
    if ((size % PAGE_SIZE) == 0) {
    if ((size % PAGE_SIZE) == 0) {
        int order, a_size;
        int order, a_size;
 
 
        for (order = 0, a_size = PAGE_SIZE;
        for (order = 0, a_size = PAGE_SIZE;
             a_size < size; order++, a_size <<= 1)
             a_size < size; order++, a_size <<= 1)
            ;
            ;
        free_pages((unsigned long)ptr, order);
        free_pages((unsigned long)ptr, order);
    } else
    } else
        kfree(ptr);
        kfree(ptr);
}
}
 
 
void scsi_build_commandblocks(Scsi_Device * SDpnt)
void scsi_build_commandblocks(Scsi_Device * SDpnt)
{
{
    struct Scsi_Host *host = SDpnt->host;
    struct Scsi_Host *host = SDpnt->host;
    int j;
    int j;
    Scsi_Cmnd * SCpnt;
    Scsi_Cmnd * SCpnt;
 
 
    if (SDpnt->queue_depth == 0)
    if (SDpnt->queue_depth == 0)
        SDpnt->queue_depth = host->cmd_per_lun;
        SDpnt->queue_depth = host->cmd_per_lun;
    SDpnt->device_queue = NULL;
    SDpnt->device_queue = NULL;
 
 
    for(j=0;j<SDpnt->queue_depth;j++){
    for(j=0;j<SDpnt->queue_depth;j++){
      SCpnt = (Scsi_Cmnd *)
      SCpnt = (Scsi_Cmnd *)
              scsi_init_malloc(sizeof(Scsi_Cmnd),
              scsi_init_malloc(sizeof(Scsi_Cmnd),
                               GFP_ATOMIC |
                               GFP_ATOMIC |
                               (host->unchecked_isa_dma ? GFP_DMA : 0));
                               (host->unchecked_isa_dma ? GFP_DMA : 0));
        SCpnt->host = host;
        SCpnt->host = host;
        SCpnt->device = SDpnt;
        SCpnt->device = SDpnt;
        SCpnt->target = SDpnt->id;
        SCpnt->target = SDpnt->id;
        SCpnt->lun = SDpnt->lun;
        SCpnt->lun = SDpnt->lun;
        SCpnt->channel = SDpnt->channel;
        SCpnt->channel = SDpnt->channel;
        SCpnt->request.rq_status = RQ_INACTIVE;
        SCpnt->request.rq_status = RQ_INACTIVE;
        SCpnt->use_sg = 0;
        SCpnt->use_sg = 0;
        SCpnt->old_use_sg = 0;
        SCpnt->old_use_sg = 0;
        SCpnt->old_cmd_len = 0;
        SCpnt->old_cmd_len = 0;
        SCpnt->timeout = 0;
        SCpnt->timeout = 0;
        SCpnt->underflow = 0;
        SCpnt->underflow = 0;
        SCpnt->transfersize = 0;
        SCpnt->transfersize = 0;
        SCpnt->serial_number = 0;
        SCpnt->serial_number = 0;
        SCpnt->serial_number_at_timeout = 0;
        SCpnt->serial_number_at_timeout = 0;
        SCpnt->host_scribble = NULL;
        SCpnt->host_scribble = NULL;
        if(host->host_queue)
        if(host->host_queue)
            host->host_queue->prev = SCpnt;
            host->host_queue->prev = SCpnt;
        SCpnt->next = host->host_queue;
        SCpnt->next = host->host_queue;
        SCpnt->prev = NULL;
        SCpnt->prev = NULL;
        host->host_queue = SCpnt;
        host->host_queue = SCpnt;
        SCpnt->device_next = SDpnt->device_queue;
        SCpnt->device_next = SDpnt->device_queue;
        SDpnt->device_queue = SCpnt;
        SDpnt->device_queue = SCpnt;
    }
    }
    SDpnt->has_cmdblocks = 1;
    SDpnt->has_cmdblocks = 1;
}
}
 
 
/*
/*
 * scsi_dev_init() is our initialization routine, which in turn calls host
 * scsi_dev_init() is our initialization routine, which in turn calls host
 * initialization, bus scanning, and sd/st initialization routines.
 * initialization, bus scanning, and sd/st initialization routines.
 */
 */
 
 
int scsi_dev_init(void)
int scsi_dev_init(void)
{
{
    Scsi_Device * SDpnt;
    Scsi_Device * SDpnt;
    struct Scsi_Host * shpnt;
    struct Scsi_Host * shpnt;
    struct Scsi_Device_Template * sdtpnt;
    struct Scsi_Device_Template * sdtpnt;
#ifdef FOO_ON_YOU
#ifdef FOO_ON_YOU
    return;
    return;
#endif
#endif
 
 
    /* Yes we're here... */
    /* Yes we're here... */
#ifdef CONFIG_PROC_FS
#ifdef CONFIG_PROC_FS
    dispatch_scsi_info_ptr = dispatch_scsi_info;
    dispatch_scsi_info_ptr = dispatch_scsi_info;
#endif
#endif
 
 
    /* Init a few things so we can "malloc" memory. */
    /* Init a few things so we can "malloc" memory. */
    scsi_loadable_module_flag = 0;
    scsi_loadable_module_flag = 0;
 
 
    timer_table[SCSI_TIMER].fn = scsi_main_timeout;
    timer_table[SCSI_TIMER].fn = scsi_main_timeout;
    timer_table[SCSI_TIMER].expires = 0;
    timer_table[SCSI_TIMER].expires = 0;
 
 
#ifdef CONFIG_MODULES
#ifdef CONFIG_MODULES
    register_symtab(&scsi_symbol_table);
    register_symtab(&scsi_symbol_table);
#endif    
#endif    
 
 
    /* Register the /proc/scsi/scsi entry */
    /* Register the /proc/scsi/scsi entry */
#ifdef CONFIG_PROC_FS 
#ifdef CONFIG_PROC_FS 
    proc_scsi_register(0, &proc_scsi_scsi);
    proc_scsi_register(0, &proc_scsi_scsi);
#endif
#endif
 
 
    /* initialize all hosts */
    /* initialize all hosts */
    scsi_init();
    scsi_init();
 
 
    scsi_devices = (Scsi_Device *) NULL;
    scsi_devices = (Scsi_Device *) NULL;
 
 
    for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
    for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
        scan_scsis(shpnt,0,0,0,0);           /* scan for scsi devices */
        scan_scsis(shpnt,0,0,0,0);           /* scan for scsi devices */
        if (shpnt->select_queue_depths != NULL)
        if (shpnt->select_queue_depths != NULL)
            (shpnt->select_queue_depths)(shpnt, scsi_devices);
            (shpnt->select_queue_depths)(shpnt, scsi_devices);
    }
    }
 
 
    printk("scsi : detected ");
    printk("scsi : detected ");
    for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
    for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
        if (sdtpnt->dev_noticed && sdtpnt->name)
        if (sdtpnt->dev_noticed && sdtpnt->name)
            printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
            printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
                   (sdtpnt->dev_noticed != 1) ? "s" : "");
                   (sdtpnt->dev_noticed != 1) ? "s" : "");
    printk("total.\n");
    printk("total.\n");
 
 
    for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
    for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
        if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
        if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
 
 
    for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
    for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
        SDpnt->scsi_request_fn = NULL;
        SDpnt->scsi_request_fn = NULL;
        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
            if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
            if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
        if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
        if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
    }
    }
 
 
 
 
    /*
    /*
     * This should build the DMA pool.
     * This should build the DMA pool.
     */
     */
    resize_dma_pool();
    resize_dma_pool();
 
 
    /*
    /*
     * OK, now we finish the initialization by doing spin-up, read
     * OK, now we finish the initialization by doing spin-up, read
     * capacity, etc, etc
     * capacity, etc, etc
     */
     */
    for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
    for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
        if(sdtpnt->finish && sdtpnt->nr_dev)
        if(sdtpnt->finish && sdtpnt->nr_dev)
            (*sdtpnt->finish)();
            (*sdtpnt->finish)();
 
 
    scsi_loadable_module_flag = 1;
    scsi_loadable_module_flag = 1;
 
 
    return 0;
    return 0;
}
}
 
 
static void print_inquiry(unsigned char *data)
static void print_inquiry(unsigned char *data)
{
{
    int i;
    int i;
 
 
    printk("  Vendor: ");
    printk("  Vendor: ");
    for (i = 8; i < 16; i++)
    for (i = 8; i < 16; i++)
    {
    {
        if (data[i] >= 0x20 && i < data[4] + 5)
        if (data[i] >= 0x20 && i < data[4] + 5)
            printk("%c", data[i]);
            printk("%c", data[i]);
        else
        else
            printk(" ");
            printk(" ");
    }
    }
 
 
    printk("  Model: ");
    printk("  Model: ");
    for (i = 16; i < 32; i++)
    for (i = 16; i < 32; i++)
    {
    {
        if (data[i] >= 0x20 && i < data[4] + 5)
        if (data[i] >= 0x20 && i < data[4] + 5)
            printk("%c", data[i]);
            printk("%c", data[i]);
        else
        else
            printk(" ");
            printk(" ");
    }
    }
 
 
    printk("  Rev: ");
    printk("  Rev: ");
    for (i = 32; i < 36; i++)
    for (i = 32; i < 36; i++)
    {
    {
        if (data[i] >= 0x20 && i < data[4] + 5)
        if (data[i] >= 0x20 && i < data[4] + 5)
            printk("%c", data[i]);
            printk("%c", data[i]);
        else
        else
            printk(" ");
            printk(" ");
    }
    }
 
 
    printk("\n");
    printk("\n");
 
 
    i = data[0] & 0x1f;
    i = data[0] & 0x1f;
 
 
    printk("  Type:   %s ",
    printk("  Type:   %s ",
           i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
           i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
    printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
    printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
    if ((data[2] & 7) >= 2) {
    if ((data[2] & 7) >= 2) {
        if (data[7] & 2)
        if (data[7] & 2)
            printk(" TAG");
            printk(" TAG");
        if (data[7] & 0x10)
        if (data[7] & 0x10)
            printk(" SYNC");
            printk(" SYNC");
    }
    }
    if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
    if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
        printk(" CCS\n");
        printk(" CCS\n");
    else
    else
        printk("\n");
        printk("\n");
}
}
 
 
 
 
#ifdef CONFIG_PROC_FS
#ifdef CONFIG_PROC_FS
int scsi_proc_info(char *buffer, char **start, off_t offset, int length,
int scsi_proc_info(char *buffer, char **start, off_t offset, int length,
                    int hostno, int inout)
                    int hostno, int inout)
{
{
    Scsi_Cmnd *SCpnt;
    Scsi_Cmnd *SCpnt;
    struct Scsi_Device_Template *SDTpnt;
    struct Scsi_Device_Template *SDTpnt;
    Scsi_Device *scd, *scd_h = NULL;
    Scsi_Device *scd, *scd_h = NULL;
    struct Scsi_Host *HBA_ptr;
    struct Scsi_Host *HBA_ptr;
    char *p;
    char *p;
    int   host, channel, id, lun;
    int   host, channel, id, lun;
    int   size, len = 0;
    int   size, len = 0;
    off_t begin = 0;
    off_t begin = 0;
    off_t pos = 0;
    off_t pos = 0;
 
 
    scd = scsi_devices;
    scd = scsi_devices;
    HBA_ptr = scsi_hostlist;
    HBA_ptr = scsi_hostlist;
 
 
    if(inout == 0) {
    if(inout == 0) {
        size = sprintf(buffer+len,"Attached devices: %s\n", (scd)?"":"none");
        size = sprintf(buffer+len,"Attached devices: %s\n", (scd)?"":"none");
        len += size;
        len += size;
        pos = begin + len;
        pos = begin + len;
        while (HBA_ptr) {
        while (HBA_ptr) {
#if 0
#if 0
            size += sprintf(buffer+len,"scsi%2d: %s\n", (int) HBA_ptr->host_no,
            size += sprintf(buffer+len,"scsi%2d: %s\n", (int) HBA_ptr->host_no,
                            HBA_ptr->hostt->procname);
                            HBA_ptr->hostt->procname);
            len += size;
            len += size;
            pos = begin + len;
            pos = begin + len;
#endif
#endif
            scd = scsi_devices;
            scd = scsi_devices;
            while (scd) {
            while (scd) {
                if (scd->host == HBA_ptr) {
                if (scd->host == HBA_ptr) {
                    proc_print_scsidevice(scd, buffer, &size, len);
                    proc_print_scsidevice(scd, buffer, &size, len);
                    len += size;
                    len += size;
                    pos = begin + len;
                    pos = begin + len;
 
 
                    if (pos < offset) {
                    if (pos < offset) {
                        len = 0;
                        len = 0;
                        begin = pos;
                        begin = pos;
                    }
                    }
                    if (pos > offset + length)
                    if (pos > offset + length)
                        goto stop_output;
                        goto stop_output;
                }
                }
                scd = scd->next;
                scd = scd->next;
            }
            }
            HBA_ptr = HBA_ptr->next;
            HBA_ptr = HBA_ptr->next;
        }
        }
 
 
    stop_output:
    stop_output:
        *start=buffer+(offset-begin);   /* Start of wanted data */
        *start=buffer+(offset-begin);   /* Start of wanted data */
        len-=(offset-begin);            /* Start slop */
        len-=(offset-begin);            /* Start slop */
        if(len>length)
        if(len>length)
            len = length;               /* Ending slop */
            len = length;               /* Ending slop */
        return (len);
        return (len);
    }
    }
 
 
    if(!buffer || length < 25 || strncmp("scsi", buffer, 4))
    if(!buffer || length < 25 || strncmp("scsi", buffer, 4))
        return(-EINVAL);
        return(-EINVAL);
 
 
    /*
    /*
     * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
     * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
     * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
     * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
     * Consider this feature BETA.
     * Consider this feature BETA.
     *     CAUTION: This is not for hotplugging your peripherals. As
     *     CAUTION: This is not for hotplugging your peripherals. As
     *     SCSI was not designed for this you could damage your
     *     SCSI was not designed for this you could damage your
     *     hardware !
     *     hardware !
     * However perhaps it is legal to switch on an
     * However perhaps it is legal to switch on an
     * already connected device. It is perhaps not
     * already connected device. It is perhaps not
     * guaranteed this device doesn't corrupt an ongoing data transfer.
     * guaranteed this device doesn't corrupt an ongoing data transfer.
     */
     */
    if(!strncmp("add-single-device", buffer + 5, 17)) {
    if(!strncmp("add-single-device", buffer + 5, 17)) {
        p = buffer + 23;
        p = buffer + 23;
 
 
        host    = simple_strtoul(p, &p, 0);
        host    = simple_strtoul(p, &p, 0);
        channel = simple_strtoul(p+1, &p, 0);
        channel = simple_strtoul(p+1, &p, 0);
        id      = simple_strtoul(p+1, &p, 0);
        id      = simple_strtoul(p+1, &p, 0);
        lun     = simple_strtoul(p+1, &p, 0);
        lun     = simple_strtoul(p+1, &p, 0);
 
 
        printk("scsi singledevice %d %d %d %d\n", host, channel,
        printk("scsi singledevice %d %d %d %d\n", host, channel,
                        id, lun);
                        id, lun);
 
 
        while(scd && (scd->host->host_no != host
        while(scd && (scd->host->host_no != host
              || scd->channel != channel
              || scd->channel != channel
              || scd->id != id
              || scd->id != id
              || scd->lun != lun)) {
              || scd->lun != lun)) {
            scd = scd->next;
            scd = scd->next;
        }
        }
        if(scd)
        if(scd)
            return(-ENOSYS);  /* We do not yet support unplugging */
            return(-ENOSYS);  /* We do not yet support unplugging */
        while(HBA_ptr && HBA_ptr->host_no != host)
        while(HBA_ptr && HBA_ptr->host_no != host)
            HBA_ptr = HBA_ptr->next;
            HBA_ptr = HBA_ptr->next;
 
 
        if(!HBA_ptr)
        if(!HBA_ptr)
            return(-ENXIO);
            return(-ENXIO);
 
 
        scan_scsis (HBA_ptr, 1, channel, id, lun);
        scan_scsis (HBA_ptr, 1, channel, id, lun);
        return(length);
        return(length);
 
 
    }
    }
 
 
    /*
    /*
     * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
     * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
     * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
     * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
     *
     *
     * Consider this feature pre-BETA.
     * Consider this feature pre-BETA.
     *
     *
     *     CAUTION: This is not for hotplugging your peripherals. As
     *     CAUTION: This is not for hotplugging your peripherals. As
     *     SCSI was not designed for this you could damage your
     *     SCSI was not designed for this you could damage your
     *     hardware and thoroughly confuse the SCSI subsystem.
     *     hardware and thoroughly confuse the SCSI subsystem.
     *
     *
     */
     */
    else if(!strncmp("remove-single-device", buffer + 5, 20)) {
    else if(!strncmp("remove-single-device", buffer + 5, 20)) {
        p = buffer + 26;
        p = buffer + 26;
 
 
        host    = simple_strtoul(p, &p, 0);
        host    = simple_strtoul(p, &p, 0);
        channel = simple_strtoul(p+1, &p, 0);
        channel = simple_strtoul(p+1, &p, 0);
        id      = simple_strtoul(p+1, &p, 0);
        id      = simple_strtoul(p+1, &p, 0);
        lun     = simple_strtoul(p+1, &p, 0);
        lun     = simple_strtoul(p+1, &p, 0);
 
 
        while(scd != NULL) {
        while(scd != NULL) {
            if(scd->host->host_no == host
            if(scd->host->host_no == host
               && scd->channel == channel
               && scd->channel == channel
               && scd->id == id
               && scd->id == id
               && scd->lun == lun){
               && scd->lun == lun){
                break;
                break;
            }
            }
            scd_h = scd;
            scd_h = scd;
            scd = scd->next;
            scd = scd->next;
        }
        }
 
 
        if(scd == NULL)
        if(scd == NULL)
            return(-ENODEV);  /* there is no such device attached */
            return(-ENODEV);  /* there is no such device attached */
 
 
        if(scd->access_count)
        if(scd->access_count)
            return(-EBUSY);
            return(-EBUSY);
 
 
        SDTpnt = scsi_devicelist;
        SDTpnt = scsi_devicelist;
        while(SDTpnt != NULL) {
        while(SDTpnt != NULL) {
            if(SDTpnt->detach) (*SDTpnt->detach)(scd);
            if(SDTpnt->detach) (*SDTpnt->detach)(scd);
            SDTpnt = SDTpnt->next;
            SDTpnt = SDTpnt->next;
        }
        }
 
 
        if(scd->attached == 0) {
        if(scd->attached == 0) {
            /*
            /*
             * Nobody is using this device any more.
             * Nobody is using this device any more.
             * Free all of the command structures.
             * Free all of the command structures.
             */
             */
            for(SCpnt=scd->host->host_queue; SCpnt; SCpnt = SCpnt->next){
            for(SCpnt=scd->host->host_queue; SCpnt; SCpnt = SCpnt->next){
                if(SCpnt->device == scd) {
                if(SCpnt->device == scd) {
                    if(SCpnt->prev != NULL)
                    if(SCpnt->prev != NULL)
                        SCpnt->prev->next = SCpnt->next;
                        SCpnt->prev->next = SCpnt->next;
                    if(SCpnt->next != NULL)
                    if(SCpnt->next != NULL)
                        SCpnt->next->prev = SCpnt->prev;
                        SCpnt->next->prev = SCpnt->prev;
                    if(SCpnt == scd->host->host_queue)
                    if(SCpnt == scd->host->host_queue)
                        scd->host->host_queue = SCpnt->next;
                        scd->host->host_queue = SCpnt->next;
                    scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
                    scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
                }
                }
            }
            }
            /* Now we can remove the device structure */
            /* Now we can remove the device structure */
            if(scd_h != NULL) {
            if(scd_h != NULL) {
                scd_h->next = scd->next;
                scd_h->next = scd->next;
            } else if (scsi_devices == scd) {
            } else if (scsi_devices == scd) {
                /* We had a hit on the first entry of the device list */
                /* We had a hit on the first entry of the device list */
                scsi_devices = scd->next;
                scsi_devices = scd->next;
            }
            }
            scsi_init_free((char *) scd, sizeof(Scsi_Device));
            scsi_init_free((char *) scd, sizeof(Scsi_Device));
        } else {
        } else {
            return(-EBUSY);
            return(-EBUSY);
        }
        }
        return(0);
        return(0);
    }
    }
    return(-EINVAL);
    return(-EINVAL);
}
}
#endif
#endif
 
 
/*
/*
 * Go through the device list and recompute the most appropriate size
 * Go through the device list and recompute the most appropriate size
 * for the dma pool.  Then grab more memory (as required).
 * for the dma pool.  Then grab more memory (as required).
 */
 */
static void resize_dma_pool(void)
static void resize_dma_pool(void)
{
{
    int i;
    int i;
    unsigned long size;
    unsigned long size;
    struct Scsi_Host * shpnt;
    struct Scsi_Host * shpnt;
    struct Scsi_Host * host = NULL;
    struct Scsi_Host * host = NULL;
    Scsi_Device * SDpnt;
    Scsi_Device * SDpnt;
    unsigned long flags;
    unsigned long flags;
    FreeSectorBitmap * new_dma_malloc_freelist = NULL;
    FreeSectorBitmap * new_dma_malloc_freelist = NULL;
    unsigned int new_dma_sectors = 0;
    unsigned int new_dma_sectors = 0;
    unsigned int new_need_isa_buffer = 0;
    unsigned int new_need_isa_buffer = 0;
    unsigned char ** new_dma_malloc_pages = NULL;
    unsigned char ** new_dma_malloc_pages = NULL;
 
 
    if( !scsi_devices )
    if( !scsi_devices )
    {
    {
        /*
        /*
         * Free up the DMA pool.
         * Free up the DMA pool.
         */
         */
        if( dma_free_sectors != dma_sectors )
        if( dma_free_sectors != dma_sectors )
            panic("SCSI DMA pool memory leak %d %d\n",dma_free_sectors,dma_sectors);
            panic("SCSI DMA pool memory leak %d %d\n",dma_free_sectors,dma_sectors);
 
 
        for(i=0; i < dma_sectors / SECTORS_PER_PAGE; i++)
        for(i=0; i < dma_sectors / SECTORS_PER_PAGE; i++)
            scsi_init_free(dma_malloc_pages[i], PAGE_SIZE);
            scsi_init_free(dma_malloc_pages[i], PAGE_SIZE);
        if (dma_malloc_pages)
        if (dma_malloc_pages)
            scsi_init_free((char *) dma_malloc_pages,
            scsi_init_free((char *) dma_malloc_pages,
                           (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages));
                           (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages));
        dma_malloc_pages = NULL;
        dma_malloc_pages = NULL;
        if (dma_malloc_freelist)
        if (dma_malloc_freelist)
            scsi_init_free((char *) dma_malloc_freelist,
            scsi_init_free((char *) dma_malloc_freelist,
                           (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_freelist));
                           (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_freelist));
        dma_malloc_freelist = NULL;
        dma_malloc_freelist = NULL;
        dma_sectors = 0;
        dma_sectors = 0;
        dma_free_sectors = 0;
        dma_free_sectors = 0;
        return;
        return;
    }
    }
    /* Next, check to see if we need to extend the DMA buffer pool */
    /* Next, check to see if we need to extend the DMA buffer pool */
 
 
    new_dma_sectors = 2*SECTORS_PER_PAGE;               /* Base value we use */
    new_dma_sectors = 2*SECTORS_PER_PAGE;               /* Base value we use */
 
 
    if (high_memory-1 > ISA_DMA_THRESHOLD)
    if (high_memory-1 > ISA_DMA_THRESHOLD)
        scsi_need_isa_bounce_buffers = 1;
        scsi_need_isa_bounce_buffers = 1;
    else
    else
        scsi_need_isa_bounce_buffers = 0;
        scsi_need_isa_bounce_buffers = 0;
 
 
    if (scsi_devicelist)
    if (scsi_devicelist)
        for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
        for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
            new_dma_sectors += SECTORS_PER_PAGE;        /* Increment for each host */
            new_dma_sectors += SECTORS_PER_PAGE;        /* Increment for each host */
 
 
    for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
    for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
        host = SDpnt->host;
        host = SDpnt->host;
 
 
        /*
        /*
         * sd and sr drivers allocate scatterlists.
         * sd and sr drivers allocate scatterlists.
         * sr drivers may allocate for each command 1x2048 or 2x1024 extra
         * sr drivers may allocate for each command 1x2048 or 2x1024 extra
         * buffers for 2k sector size and 1k fs.
         * buffers for 2k sector size and 1k fs.
         * sg driver allocates buffers < 4k.
         * sg driver allocates buffers < 4k.
         * st driver does not need buffers from the dma pool.
         * st driver does not need buffers from the dma pool.
         * estimate 4k buffer/command for devices of unknown type (should panic).
         * estimate 4k buffer/command for devices of unknown type (should panic).
         */
         */
        if (SDpnt->type == TYPE_WORM || SDpnt->type == TYPE_ROM ||
        if (SDpnt->type == TYPE_WORM || SDpnt->type == TYPE_ROM ||
            SDpnt->type == TYPE_DISK || SDpnt->type == TYPE_MOD) {
            SDpnt->type == TYPE_DISK || SDpnt->type == TYPE_MOD) {
            new_dma_sectors += ((host->sg_tablesize *
            new_dma_sectors += ((host->sg_tablesize *
                                 sizeof(struct scatterlist) + 511) >> 9) *
                                 sizeof(struct scatterlist) + 511) >> 9) *
                               SDpnt->queue_depth;
                               SDpnt->queue_depth;
            if (SDpnt->type == TYPE_WORM || SDpnt->type == TYPE_ROM)
            if (SDpnt->type == TYPE_WORM || SDpnt->type == TYPE_ROM)
                new_dma_sectors += (2048 >> 9) * SDpnt->queue_depth;
                new_dma_sectors += (2048 >> 9) * SDpnt->queue_depth;
        }
        }
        else if (SDpnt->type == TYPE_SCANNER ||
        else if (SDpnt->type == TYPE_SCANNER ||
                 SDpnt->type == TYPE_PROCESSOR ||
                 SDpnt->type == TYPE_PROCESSOR ||
                 SDpnt->type == TYPE_MEDIUM_CHANGER) {
                 SDpnt->type == TYPE_MEDIUM_CHANGER) {
            new_dma_sectors += (4096 >> 9) * SDpnt->queue_depth;
            new_dma_sectors += (4096 >> 9) * SDpnt->queue_depth;
        }
        }
        else {
        else {
            if (SDpnt->type != TYPE_TAPE) {
            if (SDpnt->type != TYPE_TAPE) {
                printk("resize_dma_pool: unknown device type %d\n", SDpnt->type);
                printk("resize_dma_pool: unknown device type %d\n", SDpnt->type);
                new_dma_sectors += (4096 >> 9) * SDpnt->queue_depth;
                new_dma_sectors += (4096 >> 9) * SDpnt->queue_depth;
            }
            }
        }
        }
 
 
        if(host->unchecked_isa_dma &&
        if(host->unchecked_isa_dma &&
           scsi_need_isa_bounce_buffers &&
           scsi_need_isa_bounce_buffers &&
           SDpnt->type != TYPE_TAPE) {
           SDpnt->type != TYPE_TAPE) {
            new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
            new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
                                                  SDpnt->queue_depth;
                                                  SDpnt->queue_depth;
            new_need_isa_buffer++;
            new_need_isa_buffer++;
        }
        }
    }
    }
 
 
#ifdef DEBUG_INIT
#ifdef DEBUG_INIT
    printk("resize_dma_pool: needed dma sectors = %d\n", new_dma_sectors);
    printk("resize_dma_pool: needed dma sectors = %d\n", new_dma_sectors);
#endif
#endif
 
 
    /* limit DMA memory to 32MB: */
    /* limit DMA memory to 32MB: */
    new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
    new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
 
 
    /*
    /*
     * We never shrink the buffers - this leads to
     * We never shrink the buffers - this leads to
     * race conditions that I would rather not even think
     * race conditions that I would rather not even think
     * about right now.
     * about right now.
     */
     */
    if( new_dma_sectors < dma_sectors )
    if( new_dma_sectors < dma_sectors )
        new_dma_sectors = dma_sectors;
        new_dma_sectors = dma_sectors;
 
 
    if (new_dma_sectors)
    if (new_dma_sectors)
    {
    {
        size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
        size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
        new_dma_malloc_freelist = (FreeSectorBitmap *) scsi_init_malloc(size, GFP_ATOMIC);
        new_dma_malloc_freelist = (FreeSectorBitmap *) scsi_init_malloc(size, GFP_ATOMIC);
        memset(new_dma_malloc_freelist, 0, size);
        memset(new_dma_malloc_freelist, 0, size);
 
 
        size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(*new_dma_malloc_pages);
        size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(*new_dma_malloc_pages);
        new_dma_malloc_pages = (unsigned char **) scsi_init_malloc(size, GFP_ATOMIC);
        new_dma_malloc_pages = (unsigned char **) scsi_init_malloc(size, GFP_ATOMIC);
        memset(new_dma_malloc_pages, 0, size);
        memset(new_dma_malloc_pages, 0, size);
    }
    }
 
 
    /*
    /*
     * If we need more buffers, expand the list.
     * If we need more buffers, expand the list.
     */
     */
    if( new_dma_sectors > dma_sectors ) {
    if( new_dma_sectors > dma_sectors ) {
        for(i=dma_sectors / SECTORS_PER_PAGE; i< new_dma_sectors / SECTORS_PER_PAGE; i++)
        for(i=dma_sectors / SECTORS_PER_PAGE; i< new_dma_sectors / SECTORS_PER_PAGE; i++)
            new_dma_malloc_pages[i] = (unsigned char *)
            new_dma_malloc_pages[i] = (unsigned char *)
                scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
                scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
    }
    }
 
 
    /* When we dick with the actual DMA list, we need to
    /* When we dick with the actual DMA list, we need to
     * protect things
     * protect things
     */
     */
    save_flags_cli(flags);
    save_flags_cli(flags);
    if (dma_malloc_freelist)
    if (dma_malloc_freelist)
    {
    {
        size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
        size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
        memcpy(new_dma_malloc_freelist, dma_malloc_freelist, size);
        memcpy(new_dma_malloc_freelist, dma_malloc_freelist, size);
        scsi_init_free((char *) dma_malloc_freelist, size);
        scsi_init_free((char *) dma_malloc_freelist, size);
    }
    }
    dma_malloc_freelist = new_dma_malloc_freelist;
    dma_malloc_freelist = new_dma_malloc_freelist;
 
 
    if (dma_malloc_pages)
    if (dma_malloc_pages)
    {
    {
        size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages);
        size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages);
        memcpy(new_dma_malloc_pages, dma_malloc_pages, size);
        memcpy(new_dma_malloc_pages, dma_malloc_pages, size);
        scsi_init_free((char *) dma_malloc_pages, size);
        scsi_init_free((char *) dma_malloc_pages, size);
    }
    }
 
 
    dma_free_sectors += new_dma_sectors - dma_sectors;
    dma_free_sectors += new_dma_sectors - dma_sectors;
    dma_malloc_pages = new_dma_malloc_pages;
    dma_malloc_pages = new_dma_malloc_pages;
    dma_sectors = new_dma_sectors;
    dma_sectors = new_dma_sectors;
    need_isa_buffer = new_need_isa_buffer;
    need_isa_buffer = new_need_isa_buffer;
    restore_flags(flags);
    restore_flags(flags);
 
 
#ifdef DEBUG_INIT
#ifdef DEBUG_INIT
    printk("resize_dma_pool: dma free sectors   = %d\n", dma_free_sectors);
    printk("resize_dma_pool: dma free sectors   = %d\n", dma_free_sectors);
    printk("resize_dma_pool: dma sectors        = %d\n", dma_sectors);
    printk("resize_dma_pool: dma sectors        = %d\n", dma_sectors);
    printk("resize_dma_pool: need isa buffers   = %d\n", need_isa_buffer);
    printk("resize_dma_pool: need isa buffers   = %d\n", need_isa_buffer);
#endif
#endif
}
}
 
 
#ifdef CONFIG_MODULES           /* a big #ifdef block... */
#ifdef CONFIG_MODULES           /* a big #ifdef block... */
 
 
/*
/*
 * This entry point should be called by a loadable module if it is trying
 * This entry point should be called by a loadable module if it is trying
 * add a low level scsi driver to the system.
 * add a low level scsi driver to the system.
 */
 */
static int scsi_register_host(Scsi_Host_Template * tpnt)
static int scsi_register_host(Scsi_Host_Template * tpnt)
{
{
    int pcount;
    int pcount;
    struct Scsi_Host * shpnt;
    struct Scsi_Host * shpnt;
    Scsi_Device * SDpnt;
    Scsi_Device * SDpnt;
    struct Scsi_Device_Template * sdtpnt;
    struct Scsi_Device_Template * sdtpnt;
    const char * name;
    const char * name;
 
 
    if (tpnt->next || !tpnt->detect) return 1;/* Must be already loaded, or
    if (tpnt->next || !tpnt->detect) return 1;/* Must be already loaded, or
                                               * no detect routine available
                                               * no detect routine available
                                               */
                                               */
    pcount = next_scsi_host;
    pcount = next_scsi_host;
    if ((tpnt->present = tpnt->detect(tpnt)))
    if ((tpnt->present = tpnt->detect(tpnt)))
    {
    {
        if(pcount == next_scsi_host) {
        if(pcount == next_scsi_host) {
            if(tpnt->present > 1) {
            if(tpnt->present > 1) {
                printk("Failure to register low-level scsi driver");
                printk("Failure to register low-level scsi driver");
                scsi_unregister_host(tpnt);
                scsi_unregister_host(tpnt);
                return 1;
                return 1;
            }
            }
            /* The low-level driver failed to register a driver.  We
            /* The low-level driver failed to register a driver.  We
             *  can do this now.
             *  can do this now.
             */
             */
            scsi_register(tpnt,0);
            scsi_register(tpnt,0);
        }
        }
        tpnt->next = scsi_hosts; /* Add to the linked list */
        tpnt->next = scsi_hosts; /* Add to the linked list */
        scsi_hosts = tpnt;
        scsi_hosts = tpnt;
 
 
        /* Add the new driver to /proc/scsi */
        /* Add the new driver to /proc/scsi */
#if CONFIG_PROC_FS 
#if CONFIG_PROC_FS 
        build_proc_dir_entries(tpnt);
        build_proc_dir_entries(tpnt);
#endif
#endif
 
 
        for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
        for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
            if(shpnt->hostt == tpnt)
            if(shpnt->hostt == tpnt)
            {
            {
                if(tpnt->info)
                if(tpnt->info)
                    name = tpnt->info(shpnt);
                    name = tpnt->info(shpnt);
                else
                else
                    name = tpnt->name;
                    name = tpnt->name;
                printk ("scsi%d : %s\n", /* And print a little message */
                printk ("scsi%d : %s\n", /* And print a little message */
                        shpnt->host_no, name);
                        shpnt->host_no, name);
            }
            }
 
 
        printk ("scsi : %d host%s.\n", next_scsi_host,
        printk ("scsi : %d host%s.\n", next_scsi_host,
                (next_scsi_host == 1) ? "" : "s");
                (next_scsi_host == 1) ? "" : "s");
 
 
        scsi_make_blocked_list();
        scsi_make_blocked_list();
 
 
        /* The next step is to call scan_scsis here.  This generates the
        /* The next step is to call scan_scsis here.  This generates the
         * Scsi_Devices entries
         * Scsi_Devices entries
         */
         */
 
 
        for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
        for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
            if(shpnt->hostt == tpnt) {
            if(shpnt->hostt == tpnt) {
              scan_scsis(shpnt,0,0,0,0);
              scan_scsis(shpnt,0,0,0,0);
              if (shpnt->select_queue_depths != NULL)
              if (shpnt->select_queue_depths != NULL)
                  (shpnt->select_queue_depths)(shpnt, scsi_devices);
                  (shpnt->select_queue_depths)(shpnt, scsi_devices);
            }
            }
 
 
        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
            if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
            if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
 
 
        /* Next we create the Scsi_Cmnd structures for this host */
        /* Next we create the Scsi_Cmnd structures for this host */
 
 
        for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
        for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
            if(SDpnt->host->hostt == tpnt)
            if(SDpnt->host->hostt == tpnt)
            {
            {
                for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                    if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
                    if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
                if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
                if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
            }
            }
 
 
        /*
        /*
         * Now that we have all of the devices, resize the DMA pool,
         * Now that we have all of the devices, resize the DMA pool,
         * as required.  */
         * as required.  */
        resize_dma_pool();
        resize_dma_pool();
 
 
 
 
        /* This does any final handling that is required. */
        /* This does any final handling that is required. */
        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
            if(sdtpnt->finish && sdtpnt->nr_dev)
            if(sdtpnt->finish && sdtpnt->nr_dev)
                (*sdtpnt->finish)();
                (*sdtpnt->finish)();
    }
    }
 
 
#if defined(USE_STATIC_SCSI_MEMORY)
#if defined(USE_STATIC_SCSI_MEMORY)
    printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
    printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
            (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
            (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
            (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
            (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
            (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
            (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
#endif
#endif
 
 
    MOD_INC_USE_COUNT;
    MOD_INC_USE_COUNT;
    return 0;
    return 0;
}
}
 
 
/*
/*
 * Similarly, this entry point should be called by a loadable module if it
 * Similarly, this entry point should be called by a loadable module if it
 * is trying to remove a low level scsi driver from the system.
 * is trying to remove a low level scsi driver from the system.
 */
 */
static void scsi_unregister_host(Scsi_Host_Template * tpnt)
static void scsi_unregister_host(Scsi_Host_Template * tpnt)
{
{
    Scsi_Host_Template * SHT, *SHTp;
    Scsi_Host_Template * SHT, *SHTp;
    Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
    Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
    Scsi_Cmnd * SCpnt;
    Scsi_Cmnd * SCpnt;
    unsigned long flags;
    unsigned long flags;
    struct Scsi_Device_Template * sdtpnt;
    struct Scsi_Device_Template * sdtpnt;
    struct Scsi_Host * shpnt, *sh1;
    struct Scsi_Host * shpnt, *sh1;
    int pcount;
    int pcount;
 
 
    /* First verify that this host adapter is completely free with no pending
    /* First verify that this host adapter is completely free with no pending
     * commands */
     * commands */
 
 
    for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
    for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
        if(sdpnt->host->hostt == tpnt && sdpnt->host->hostt->usage_count
        if(sdpnt->host->hostt == tpnt && sdpnt->host->hostt->usage_count
           && *sdpnt->host->hostt->usage_count) return;
           && *sdpnt->host->hostt->usage_count) return;
 
 
    for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
    for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
    {
    {
        if (shpnt->hostt != tpnt) continue;
        if (shpnt->hostt != tpnt) continue;
        for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
        for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
        {
        {
            save_flags_cli(flags);
            save_flags_cli(flags);
            if(SCpnt->request.rq_status != RQ_INACTIVE) {
            if(SCpnt->request.rq_status != RQ_INACTIVE) {
                restore_flags(flags);
                restore_flags(flags);
                for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
                for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
                    if(SCpnt->request.rq_status == RQ_SCSI_DISCONNECTING)
                    if(SCpnt->request.rq_status == RQ_SCSI_DISCONNECTING)
                        SCpnt->request.rq_status = RQ_INACTIVE;
                        SCpnt->request.rq_status = RQ_INACTIVE;
                printk("Device busy???\n");
                printk("Device busy???\n");
                return;
                return;
            }
            }
            SCpnt->request.rq_status = RQ_SCSI_DISCONNECTING;  /* Mark as busy */
            SCpnt->request.rq_status = RQ_SCSI_DISCONNECTING;  /* Mark as busy */
            restore_flags(flags);
            restore_flags(flags);
        }
        }
    }
    }
    /* Next we detach the high level drivers from the Scsi_Device structures */
    /* Next we detach the high level drivers from the Scsi_Device structures */
 
 
    for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
    for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
        if(sdpnt->host->hostt == tpnt)
        if(sdpnt->host->hostt == tpnt)
        {
        {
            for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
            for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
                if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
            /* If something still attached, punt */
            /* If something still attached, punt */
            if (sdpnt->attached) {
            if (sdpnt->attached) {
                printk("Attached usage count = %d\n", sdpnt->attached);
                printk("Attached usage count = %d\n", sdpnt->attached);
                return;
                return;
            }
            }
        }
        }
 
 
    /* Next we free up the Scsi_Cmnd structures for this host */
    /* Next we free up the Scsi_Cmnd structures for this host */
 
 
    for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
    for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
        if(sdpnt->host->hostt == tpnt)
        if(sdpnt->host->hostt == tpnt)
            while (sdpnt->host->host_queue) {
            while (sdpnt->host->host_queue) {
                SCpnt = sdpnt->host->host_queue->next;
                SCpnt = sdpnt->host->host_queue->next;
                scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
                scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
                sdpnt->host->host_queue = SCpnt;
                sdpnt->host->host_queue = SCpnt;
                if (SCpnt) SCpnt->prev = NULL;
                if (SCpnt) SCpnt->prev = NULL;
                sdpnt->has_cmdblocks = 0;
                sdpnt->has_cmdblocks = 0;
            }
            }
 
 
    /* Next free up the Scsi_Device structures for this host */
    /* Next free up the Scsi_Device structures for this host */
 
 
    sdppnt = NULL;
    sdppnt = NULL;
    for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
    for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
    {
    {
        sdpnt1 = sdpnt->next;
        sdpnt1 = sdpnt->next;
        if (sdpnt->host->hostt == tpnt) {
        if (sdpnt->host->hostt == tpnt) {
            if (sdppnt)
            if (sdppnt)
                sdppnt->next = sdpnt->next;
                sdppnt->next = sdpnt->next;
            else
            else
                scsi_devices = sdpnt->next;
                scsi_devices = sdpnt->next;
            scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
            scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
        } else
        } else
            sdppnt = sdpnt;
            sdppnt = sdpnt;
    }
    }
 
 
    /* Next we go through and remove the instances of the individual hosts
    /* Next we go through and remove the instances of the individual hosts
     * that were detected */
     * that were detected */
 
 
    shpnt = scsi_hostlist;
    shpnt = scsi_hostlist;
    while(shpnt) {
    while(shpnt) {
        sh1 = shpnt->next;
        sh1 = shpnt->next;
        if(shpnt->hostt == tpnt) {
        if(shpnt->hostt == tpnt) {
            if(shpnt->loaded_as_module) {
            if(shpnt->loaded_as_module) {
                pcount = next_scsi_host;
                pcount = next_scsi_host;
                /* Remove the /proc/scsi directory entry */
                /* Remove the /proc/scsi directory entry */
#if CONFIG_PROC_FS 
#if CONFIG_PROC_FS 
                proc_scsi_unregister(tpnt->proc_dir,
                proc_scsi_unregister(tpnt->proc_dir,
                                     shpnt->host_no + PROC_SCSI_FILE);
                                     shpnt->host_no + PROC_SCSI_FILE);
#endif   
#endif   
                if(tpnt->release)
                if(tpnt->release)
                    (*tpnt->release)(shpnt);
                    (*tpnt->release)(shpnt);
                else {
                else {
                    /* This is the default case for the release function.
                    /* This is the default case for the release function.
                     * It should do the right thing for most correctly
                     * It should do the right thing for most correctly
                     * written host adapters.
                     * written host adapters.
                     */
                     */
                    if (shpnt->irq) free_irq(shpnt->irq, NULL);
                    if (shpnt->irq) free_irq(shpnt->irq, NULL);
                    if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
                    if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
                    if (shpnt->io_port && shpnt->n_io_port)
                    if (shpnt->io_port && shpnt->n_io_port)
                        release_region(shpnt->io_port, shpnt->n_io_port);
                        release_region(shpnt->io_port, shpnt->n_io_port);
                }
                }
                if(pcount == next_scsi_host) scsi_unregister(shpnt);
                if(pcount == next_scsi_host) scsi_unregister(shpnt);
                tpnt->present--;
                tpnt->present--;
            }
            }
        }
        }
        shpnt = sh1;
        shpnt = sh1;
    }
    }
 
 
    /*
    /*
     * If there are absolutely no more hosts left, it is safe
     * If there are absolutely no more hosts left, it is safe
     * to completely nuke the DMA pool.  The resize operation will
     * to completely nuke the DMA pool.  The resize operation will
     * do the right thing and free everything.
     * do the right thing and free everything.
     */
     */
    if( !scsi_devices )
    if( !scsi_devices )
        resize_dma_pool();
        resize_dma_pool();
 
 
    printk ("scsi : %d host%s.\n", next_scsi_host,
    printk ("scsi : %d host%s.\n", next_scsi_host,
            (next_scsi_host == 1) ? "" : "s");
            (next_scsi_host == 1) ? "" : "s");
 
 
#if defined(USE_STATIC_SCSI_MEMORY)
#if defined(USE_STATIC_SCSI_MEMORY)
    printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
    printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
            (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
            (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
            (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
            (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
            (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
            (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
#endif
#endif
 
 
    scsi_make_blocked_list();
    scsi_make_blocked_list();
 
 
    /* There were some hosts that were loaded at boot time, so we cannot
    /* There were some hosts that were loaded at boot time, so we cannot
       do any more than this */
       do any more than this */
    if (tpnt->present) return;
    if (tpnt->present) return;
 
 
    /* OK, this is the very last step.  Remove this host adapter from the
    /* OK, this is the very last step.  Remove this host adapter from the
       linked list. */
       linked list. */
    for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
    for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
        if(SHT == tpnt) {
        if(SHT == tpnt) {
            if(SHTp)
            if(SHTp)
                SHTp->next = SHT->next;
                SHTp->next = SHT->next;
            else
            else
                scsi_hosts = SHT->next;
                scsi_hosts = SHT->next;
            SHT->next = NULL;
            SHT->next = NULL;
            break;
            break;
        }
        }
 
 
    /* Rebuild the /proc/scsi directory entries */
    /* Rebuild the /proc/scsi directory entries */
#if CONFIG_PROC_FS 
#if CONFIG_PROC_FS 
    proc_scsi_unregister(tpnt->proc_dir, tpnt->proc_dir->low_ino);
    proc_scsi_unregister(tpnt->proc_dir, tpnt->proc_dir->low_ino);
#endif
#endif
    MOD_DEC_USE_COUNT;
    MOD_DEC_USE_COUNT;
}
}
 
 
/*
/*
 * This entry point should be called by a loadable module if it is trying
 * This entry point should be called by a loadable module if it is trying
 * add a high level scsi driver to the system.
 * add a high level scsi driver to the system.
 */
 */
static int scsi_register_device_module(struct Scsi_Device_Template * tpnt)
static int scsi_register_device_module(struct Scsi_Device_Template * tpnt)
{
{
    Scsi_Device * SDpnt;
    Scsi_Device * SDpnt;
 
 
    if (tpnt->next) return 1;
    if (tpnt->next) return 1;
 
 
    scsi_register_device(tpnt);
    scsi_register_device(tpnt);
    /*
    /*
     * First scan the devices that we know about, and see if we notice them.
     * First scan the devices that we know about, and see if we notice them.
     */
     */
 
 
    for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
    for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
        if(tpnt->detect) SDpnt->attached += (*tpnt->detect)(SDpnt);
        if(tpnt->detect) SDpnt->attached += (*tpnt->detect)(SDpnt);
 
 
    /*
    /*
     * If any of the devices would match this driver, then perform the
     * If any of the devices would match this driver, then perform the
     * init function.
     * init function.
     */
     */
    if(tpnt->init && tpnt->dev_noticed)
    if(tpnt->init && tpnt->dev_noticed)
        if ((*tpnt->init)()) return 1;
        if ((*tpnt->init)()) return 1;
 
 
    /*
    /*
     * Now actually connect the devices to the new driver.
     * Now actually connect the devices to the new driver.
     */
     */
    for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
    for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
    {
    {
        if(tpnt->attach)  (*tpnt->attach)(SDpnt);
        if(tpnt->attach)  (*tpnt->attach)(SDpnt);
        /*
        /*
         * If this driver attached to the device, and we no longer
         * If this driver attached to the device, and we no longer
         * have anything attached, release the scsi command blocks.
         * have anything attached, release the scsi command blocks.
         */
         */
        if(SDpnt->attached && SDpnt->has_cmdblocks == 0)
        if(SDpnt->attached && SDpnt->has_cmdblocks == 0)
            scsi_build_commandblocks(SDpnt);
            scsi_build_commandblocks(SDpnt);
    }
    }
 
 
    /*
    /*
     * This does any final handling that is required.
     * This does any final handling that is required.
     */
     */
    if(tpnt->finish && tpnt->nr_dev)  (*tpnt->finish)();
    if(tpnt->finish && tpnt->nr_dev)  (*tpnt->finish)();
    MOD_INC_USE_COUNT;
    MOD_INC_USE_COUNT;
    return 0;
    return 0;
}
}
 
 
static int scsi_unregister_device(struct Scsi_Device_Template * tpnt)
static int scsi_unregister_device(struct Scsi_Device_Template * tpnt)
{
{
    Scsi_Device * SDpnt;
    Scsi_Device * SDpnt;
    Scsi_Cmnd * SCpnt;
    Scsi_Cmnd * SCpnt;
    struct Scsi_Device_Template * spnt;
    struct Scsi_Device_Template * spnt;
    struct Scsi_Device_Template * prev_spnt;
    struct Scsi_Device_Template * prev_spnt;
 
 
    /*
    /*
     * If we are busy, this is not going to fly.
     * If we are busy, this is not going to fly.
     */
     */
    if( *tpnt->usage_count != 0) return 0;
    if( *tpnt->usage_count != 0) return 0;
    /*
    /*
     * Next, detach the devices from the driver.
     * Next, detach the devices from the driver.
     */
     */
 
 
    for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
    for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
    {
    {
        if(tpnt->detach) (*tpnt->detach)(SDpnt);
        if(tpnt->detach) (*tpnt->detach)(SDpnt);
        if(SDpnt->attached == 0)
        if(SDpnt->attached == 0)
        {
        {
            /*
            /*
             * Nobody is using this device any more.  Free all of the
             * Nobody is using this device any more.  Free all of the
             * command structures.
             * command structures.
             */
             */
            for(SCpnt = SDpnt->host->host_queue; SCpnt; SCpnt = SCpnt->next)
            for(SCpnt = SDpnt->host->host_queue; SCpnt; SCpnt = SCpnt->next)
            {
            {
                if(SCpnt->device == SDpnt)
                if(SCpnt->device == SDpnt)
                {
                {
                    if(SCpnt->prev != NULL)
                    if(SCpnt->prev != NULL)
                        SCpnt->prev->next = SCpnt->next;
                        SCpnt->prev->next = SCpnt->next;
                    if(SCpnt->next != NULL)
                    if(SCpnt->next != NULL)
                        SCpnt->next->prev = SCpnt->prev;
                        SCpnt->next->prev = SCpnt->prev;
                    if(SCpnt == SDpnt->host->host_queue)
                    if(SCpnt == SDpnt->host->host_queue)
                        SDpnt->host->host_queue = SCpnt->next;
                        SDpnt->host->host_queue = SCpnt->next;
                    scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
                    scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
                }
                }
            }
            }
            SDpnt->has_cmdblocks = 0;
            SDpnt->has_cmdblocks = 0;
        }
        }
    }
    }
    /*
    /*
     * Extract the template from the linked list.
     * Extract the template from the linked list.
     */
     */
    spnt = scsi_devicelist;
    spnt = scsi_devicelist;
    prev_spnt = NULL;
    prev_spnt = NULL;
    while(spnt != tpnt)
    while(spnt != tpnt)
    {
    {
        prev_spnt = spnt;
        prev_spnt = spnt;
        spnt = spnt->next;
        spnt = spnt->next;
    }
    }
    if(prev_spnt == NULL)
    if(prev_spnt == NULL)
        scsi_devicelist = tpnt->next;
        scsi_devicelist = tpnt->next;
    else
    else
        prev_spnt->next = spnt->next;
        prev_spnt->next = spnt->next;
 
 
    MOD_DEC_USE_COUNT;
    MOD_DEC_USE_COUNT;
    /*
    /*
     * Final cleanup for the driver is done in the driver sources in the
     * Final cleanup for the driver is done in the driver sources in the
     * cleanup function.
     * cleanup function.
     */
     */
    return 0;
    return 0;
}
}
 
 
 
 
int scsi_register_module(int module_type, void * ptr)
int scsi_register_module(int module_type, void * ptr)
{
{
    switch(module_type){
    switch(module_type){
    case MODULE_SCSI_HA:
    case MODULE_SCSI_HA:
        return scsi_register_host((Scsi_Host_Template *) ptr);
        return scsi_register_host((Scsi_Host_Template *) ptr);
 
 
        /* Load upper level device handler of some kind */
        /* Load upper level device handler of some kind */
    case MODULE_SCSI_DEV:
    case MODULE_SCSI_DEV:
#ifdef CONFIG_KERNELD
#ifdef CONFIG_KERNELD
        if (scsi_hosts == NULL)
        if (scsi_hosts == NULL)
                request_module("scsi_hostadapter");
                request_module("scsi_hostadapter");
#endif
#endif
        return scsi_register_device_module((struct Scsi_Device_Template *) ptr);
        return scsi_register_device_module((struct Scsi_Device_Template *) ptr);
        /* The rest of these are not yet implemented */
        /* The rest of these are not yet implemented */
 
 
        /* Load constants.o */
        /* Load constants.o */
    case MODULE_SCSI_CONST:
    case MODULE_SCSI_CONST:
 
 
        /* Load specialized ioctl handler for some device.  Intended for
        /* Load specialized ioctl handler for some device.  Intended for
         * cdroms that have non-SCSI2 audio command sets. */
         * cdroms that have non-SCSI2 audio command sets. */
    case MODULE_SCSI_IOCTL:
    case MODULE_SCSI_IOCTL:
 
 
    default:
    default:
        return 1;
        return 1;
    }
    }
}
}
 
 
void scsi_unregister_module(int module_type, void * ptr)
void scsi_unregister_module(int module_type, void * ptr)
{
{
    switch(module_type) {
    switch(module_type) {
    case MODULE_SCSI_HA:
    case MODULE_SCSI_HA:
        scsi_unregister_host((Scsi_Host_Template *) ptr);
        scsi_unregister_host((Scsi_Host_Template *) ptr);
        break;
        break;
    case MODULE_SCSI_DEV:
    case MODULE_SCSI_DEV:
        scsi_unregister_device((struct Scsi_Device_Template *) ptr);
        scsi_unregister_device((struct Scsi_Device_Template *) ptr);
        break;
        break;
        /* The rest of these are not yet implemented. */
        /* The rest of these are not yet implemented. */
    case MODULE_SCSI_CONST:
    case MODULE_SCSI_CONST:
    case MODULE_SCSI_IOCTL:
    case MODULE_SCSI_IOCTL:
        break;
        break;
    default:
    default:
    }
    }
    return;
    return;
}
}
 
 
#endif          /* CONFIG_MODULES */
#endif          /* CONFIG_MODULES */
 
 
#ifdef DEBUG_TIMEOUT
#ifdef DEBUG_TIMEOUT
static void
static void
scsi_dump_status(void)
scsi_dump_status(void)
{
{
    int i;
    int i;
    struct Scsi_Host * shpnt;
    struct Scsi_Host * shpnt;
    Scsi_Cmnd * SCpnt;
    Scsi_Cmnd * SCpnt;
    printk("Dump of scsi parameters:\n");
    printk("Dump of scsi parameters:\n");
    i = 0;
    i = 0;
    for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
    for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
        for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
        for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
        {
        {
            /*  (0) 0:0:0:0 (802 123434 8 8 0) (3 3 2) (%d %d %d) %d %x      */
            /*  (0) 0:0:0:0 (802 123434 8 8 0) (3 3 2) (%d %d %d) %d %x      */
            printk("(%d) %d:%d:%d:%d (%s %ld %ld %ld %d) (%d %d %x) (%d %d %d) %x %x %x\n",
            printk("(%d) %d:%d:%d:%d (%s %ld %ld %ld %d) (%d %d %x) (%d %d %d) %x %x %x\n",
                   i++, SCpnt->host->host_no,
                   i++, SCpnt->host->host_no,
                   SCpnt->channel,
                   SCpnt->channel,
                   SCpnt->target,
                   SCpnt->target,
                   SCpnt->lun,
                   SCpnt->lun,
                   kdevname(SCpnt->request.rq_dev),
                   kdevname(SCpnt->request.rq_dev),
                   SCpnt->request.sector,
                   SCpnt->request.sector,
                   SCpnt->request.nr_sectors,
                   SCpnt->request.nr_sectors,
                   SCpnt->request.current_nr_sectors,
                   SCpnt->request.current_nr_sectors,
                   SCpnt->use_sg,
                   SCpnt->use_sg,
                   SCpnt->retries,
                   SCpnt->retries,
                   SCpnt->allowed,
                   SCpnt->allowed,
                   SCpnt->flags,
                   SCpnt->flags,
                   SCpnt->timeout_per_command,
                   SCpnt->timeout_per_command,
                   SCpnt->timeout,
                   SCpnt->timeout,
                   SCpnt->internal_timeout,
                   SCpnt->internal_timeout,
                   SCpnt->cmnd[0],
                   SCpnt->cmnd[0],
                   SCpnt->sense_buffer[2],
                   SCpnt->sense_buffer[2],
                   SCpnt->result);
                   SCpnt->result);
        }
        }
    printk("wait_for_request = %p\n", wait_for_request);
    printk("wait_for_request = %p\n", wait_for_request);
    /* Now dump the request lists for each block device */
    /* Now dump the request lists for each block device */
    printk("Dump of pending block device requests\n");
    printk("Dump of pending block device requests\n");
    for(i=0; i<MAX_BLKDEV; i++)
    for(i=0; i<MAX_BLKDEV; i++)
        if(blk_dev[i].current_request)
        if(blk_dev[i].current_request)
        {
        {
            struct request * req;
            struct request * req;
            printk("%d: ", i);
            printk("%d: ", i);
            req = blk_dev[i].current_request;
            req = blk_dev[i].current_request;
            while(req) {
            while(req) {
                printk("(%s %d %ld %ld %ld) ",
                printk("(%s %d %ld %ld %ld) ",
                       kdevname(req->rq_dev),
                       kdevname(req->rq_dev),
                       req->cmd,
                       req->cmd,
                       req->sector,
                       req->sector,
                       req->nr_sectors,
                       req->nr_sectors,
                       req->current_nr_sectors);
                       req->current_nr_sectors);
                req = req->next;
                req = req->next;
            }
            }
            printk("\n");
            printk("\n");
        }
        }
}
}
#endif
#endif
 
 
#ifdef MODULE
#ifdef MODULE
 
 
int init_module(void) {
int init_module(void) {
    unsigned long size;
    unsigned long size;
 
 
    /*
    /*
     * This makes /proc/scsi visible.
     * This makes /proc/scsi visible.
     */
     */
#ifdef CONFIG_PROC_FS
#ifdef CONFIG_PROC_FS
    dispatch_scsi_info_ptr = dispatch_scsi_info;
    dispatch_scsi_info_ptr = dispatch_scsi_info;
#endif
#endif
 
 
    timer_table[SCSI_TIMER].fn = scsi_main_timeout;
    timer_table[SCSI_TIMER].fn = scsi_main_timeout;
    timer_table[SCSI_TIMER].expires = 0;
    timer_table[SCSI_TIMER].expires = 0;
    register_symtab(&scsi_symbol_table);
    register_symtab(&scsi_symbol_table);
    scsi_loadable_module_flag = 1;
    scsi_loadable_module_flag = 1;
 
 
    /* Register the /proc/scsi/scsi entry */
    /* Register the /proc/scsi/scsi entry */
#ifdef CONFIG_PROC_FS
#ifdef CONFIG_PROC_FS
    proc_scsi_register(0, &proc_scsi_scsi);
    proc_scsi_register(0, &proc_scsi_scsi);
#endif
#endif
 
 
 
 
    dma_sectors = PAGE_SIZE / SECTOR_SIZE;
    dma_sectors = PAGE_SIZE / SECTOR_SIZE;
    dma_free_sectors= dma_sectors;
    dma_free_sectors= dma_sectors;
    /*
    /*
     * Set up a minimal DMA buffer list - this will be used during scan_scsis
     * Set up a minimal DMA buffer list - this will be used during scan_scsis
     * in some cases.
     * in some cases.
     */
     */
 
 
    /* One bit per sector to indicate free/busy */
    /* One bit per sector to indicate free/busy */
    size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
    size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
    dma_malloc_freelist = (unsigned char *) scsi_init_malloc(size, GFP_ATOMIC);
    dma_malloc_freelist = (unsigned char *) scsi_init_malloc(size, GFP_ATOMIC);
    memset(dma_malloc_freelist, 0, size);
    memset(dma_malloc_freelist, 0, size);
 
 
    /* One pointer per page for the page list */
    /* One pointer per page for the page list */
    dma_malloc_pages = (unsigned char **)
    dma_malloc_pages = (unsigned char **)
        scsi_init_malloc((dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages), GFP_ATOMIC);
        scsi_init_malloc((dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages), GFP_ATOMIC);
    dma_malloc_pages[0] = (unsigned char *)
    dma_malloc_pages[0] = (unsigned char *)
        scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
        scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
    return 0;
    return 0;
}
}
 
 
void cleanup_module( void)
void cleanup_module( void)
{
{
#ifdef CONFIG_PROC_FS
#ifdef CONFIG_PROC_FS
    proc_scsi_unregister(0, PROC_SCSI_SCSI);
    proc_scsi_unregister(0, PROC_SCSI_SCSI);
 
 
    /* No, we're not here anymore. Don't show the /proc/scsi files. */
    /* No, we're not here anymore. Don't show the /proc/scsi files. */
    dispatch_scsi_info_ptr = 0L;
    dispatch_scsi_info_ptr = 0L;
#endif
#endif
 
 
    /*
    /*
     * Free up the DMA pool.
     * Free up the DMA pool.
     */
     */
    resize_dma_pool();
    resize_dma_pool();
 
 
    timer_table[SCSI_TIMER].fn = NULL;
    timer_table[SCSI_TIMER].fn = NULL;
    timer_table[SCSI_TIMER].expires = 0;
    timer_table[SCSI_TIMER].expires = 0;
}
}
#endif /* MODULE */
#endif /* MODULE */
 
 
/*
/*
 * Overrides for Emacs so that we follow Linus's tabbing style.
 * Overrides for Emacs so that we follow Linus's tabbing style.
 * Emacs will notice this stuff at the end of the file and automatically
 * Emacs will notice this stuff at the end of the file and automatically
 * adjust the settings for this buffer only.  This must remain at the end
 * adjust the settings for this buffer only.  This must remain at the end
 * of the file.
 * of the file.
 * ---------------------------------------------------------------------------
 * ---------------------------------------------------------------------------
 * Local variables:
 * Local variables:
 * c-indent-level: 4
 * c-indent-level: 4
 * c-brace-imaginary-offset: 0
 * c-brace-imaginary-offset: 0
 * c-brace-offset: -4
 * c-brace-offset: -4
 * c-argdecl-indent: 4
 * c-argdecl-indent: 4
 * c-label-offset: -4
 * c-label-offset: -4
 * c-continued-statement-offset: 4
 * c-continued-statement-offset: 4
 * c-continued-brace-offset: 0
 * c-continued-brace-offset: 0
 * indent-tabs-mode: nil
 * indent-tabs-mode: nil
 * tab-width: 8
 * tab-width: 8
 * End:
 * End:
 */
 */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.