Annotation of Gnu-Mach/i386/i386at/gpl/linux/scsi/scsi.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  *  scsi.c Copyright (C) 1992 Drew Eckhardt
                      3:  *         Copyright (C) 1993, 1994, 1995 Eric Youngdale
                      4:  *
                      5:  *  generic mid-level SCSI driver
                      6:  *      Initial versions: Drew Eckhardt
                      7:  *      Subsequent revisions: Eric Youngdale
                      8:  *
                      9:  *  <[email protected]>
                     10:  *
                     11:  *  Bug correction thanks go to :
                     12:  *      Rik Faith <[email protected]>
                     13:  *      Tommy Thorn <tthorn>
                     14:  *      Thomas Wuensche <[email protected]>
                     15:  *
                     16:  *  Modified by Eric Youngdale [email protected] to
                     17:  *  add scatter-gather, multiple outstanding request, and other
                     18:  *  enhancements.
                     19:  *
                     20:  *  Native multichannel and wide scsi support added 
                     21:  *  by Michael Neuffer [email protected]
                     22:  */
                     23: 
                     24: /*
                     25:  * Don't import our own symbols, as this would severely mess up our
                     26:  * symbol tables.
                     27:  */
                     28: #define _SCSI_SYMS_VER_
                     29: #include <linux/module.h>
                     30: 
                     31: #include <asm/system.h>
                     32: #include <linux/sched.h>
                     33: #include <linux/timer.h>
                     34: #include <linux/string.h>
                     35: #include <linux/malloc.h>
                     36: #include <asm/irq.h>
                     37: #include <asm/dma.h>
                     38: #include <linux/ioport.h>
                     39: #include <linux/kernel.h>
                     40: #include<linux/stat.h>
                     41: 
                     42: #include <linux/blk.h>
                     43: #include "scsi.h"
                     44: #include "hosts.h"
                     45: #include "constants.h"
                     46: 
                     47: #include <linux/config.h>
                     48: 
                     49: #undef USE_STATIC_SCSI_MEMORY
                     50: 
                     51: /*
                     52: static const char RCSid[] = "$Header: /gd4/gnu/cvsroot/gnumach/i386/i386at/gpl/linux/scsi/scsi.c,v 1.2 1997/03/24 21:51:33 thomas Exp $";
                     53: */
                     54: 
                     55: 
                     56: /* Command groups 3 and 4 are reserved and should never be used.  */
                     57: const unsigned char scsi_command_size[8] = { 6, 10, 10, 12, 12, 12, 10, 10 };
                     58: 
                     59: #define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))
                     60: 
                     61: /*
                     62:  * PAGE_SIZE must be a multiple of the sector size (512).  True
                     63:  * for all reasonably recent architectures (even the VAX...).
                     64:  */
                     65: #define SECTOR_SIZE            512
                     66: #define SECTORS_PER_PAGE       (PAGE_SIZE/SECTOR_SIZE)
                     67: 
                     68: #if SECTORS_PER_PAGE <= 8
                     69:  typedef unsigned char FreeSectorBitmap;
                     70: #elif SECTORS_PER_PAGE <= 32
                     71:  typedef unsigned int  FreeSectorBitmap;
                     72: #else
                     73: # error You lose.
                     74: #endif
                     75: 
                     76: static void scsi_done (Scsi_Cmnd *SCpnt);
                     77: static int update_timeout (Scsi_Cmnd *, int);
                     78: static void print_inquiry(unsigned char *data);
                     79: static void scsi_times_out (Scsi_Cmnd * SCpnt, int pid);
                     80: static int scan_scsis_single (int channel,int dev,int lun,int * max_scsi_dev ,
                     81:                  Scsi_Device ** SDpnt, Scsi_Cmnd * SCpnt,
                     82:                  struct Scsi_Host *shpnt, char * scsi_result);
                     83: void scsi_build_commandblocks(Scsi_Device * SDpnt);
                     84: 
                     85: #ifdef CONFIG_MODULES
                     86: extern struct symbol_table scsi_symbol_table;
                     87: #endif
                     88: 
                     89: static FreeSectorBitmap * dma_malloc_freelist = NULL;
                     90: static int scsi_need_isa_bounce_buffers;
                     91: static unsigned int dma_sectors = 0;
                     92: unsigned int dma_free_sectors = 0;
                     93: unsigned int need_isa_buffer = 0;
                     94: static unsigned char ** dma_malloc_pages = NULL;
                     95: 
                     96: static int time_start;
                     97: static int time_elapsed;
                     98: static volatile struct Scsi_Host * host_active = NULL;
                     99: #define SCSI_BLOCK(HOST) ((HOST->block && host_active && HOST != host_active) \
                    100:                          || (HOST->can_queue && HOST->host_busy >= HOST->can_queue))
                    101: 
                    102: #define MAX_SCSI_DEVICE_CODE 10
                    103: const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
                    104: {
                    105:     "Direct-Access    ",
                    106:     "Sequential-Access",
                    107:     "Printer          ",
                    108:     "Processor        ",
                    109:     "WORM             ",
                    110:     "CD-ROM           ",
                    111:     "Scanner          ",
                    112:     "Optical Device   ",
                    113:     "Medium Changer   ",
                    114:     "Communications   "
                    115: };
                    116: 
                    117: 
                    118: /*
                    119:  * global variables :
                    120:  * scsi_devices an array of these specifying the address for each
                    121:  * (host, id, LUN)
                    122:  */
                    123: 
                    124: Scsi_Device * scsi_devices = NULL;
                    125: 
                    126: /* Process ID of SCSI commands */
                    127: unsigned long scsi_pid = 0;
                    128: 
                    129: static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};
                    130: static void resize_dma_pool(void);
                    131: 
                    132: /* This variable is merely a hook so that we can debug the kernel with gdb. */
                    133: Scsi_Cmnd * last_cmnd = NULL;
                    134: 
                    135: /* This is the pointer to the /proc/scsi code. 
                    136:  * It is only initialized to !=0 if the scsi code is present 
                    137:  */ 
                    138: extern int (* dispatch_scsi_info_ptr)(int ino, char *buffer, char **start, 
                    139:                                      off_t offset, int length, int inout); 
                    140: extern int dispatch_scsi_info(int ino, char *buffer, char **start, 
                    141:                              off_t offset, int length, int inout); 
                    142: 
                    143: struct proc_dir_entry proc_scsi_scsi = {
                    144:     PROC_SCSI_SCSI, 4, "scsi",
                    145:     S_IFREG | S_IRUGO | S_IWUSR, 2, 0, 0, 0, 
                    146:     NULL,
                    147:     NULL, NULL,
                    148:     NULL, NULL, NULL
                    149: };
                    150: 
                    151: 
                    152: /*
                    153:  *  As the scsi do command functions are intelligent, and may need to
                    154:  *  redo a command, we need to keep track of the last command
                    155:  *  executed on each one.
                    156:  */
                    157: 
                    158: #define WAS_RESET       0x01
                    159: #define WAS_TIMEDOUT    0x02
                    160: #define WAS_SENSE       0x04
                    161: #define IS_RESETTING    0x08
                    162: #define IS_ABORTING     0x10
                    163: #define ASKED_FOR_SENSE 0x20
                    164: 
                    165: /*
                    166:  *  This is the number  of clock ticks we should wait before we time out
                    167:  *  and abort the command.  This is for  where the scsi.c module generates
                    168:  *  the command, not where it originates from a higher level, in which
                    169:  *  case the timeout is specified there.
                    170:  *
                    171:  *  ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
                    172:  *  respectively.
                    173:  */
                    174: 
                    175: #ifdef DEBUG_TIMEOUT
                    176: static void scsi_dump_status(void);
                    177: #endif
                    178: 
                    179: 
                    180: #ifdef DEBUG
                    181:     #define SCSI_TIMEOUT (5*HZ)
                    182: #else
                    183:     #define SCSI_TIMEOUT (1*HZ)
                    184: #endif
                    185: 
                    186: #ifdef DEBUG
                    187:     #define SENSE_TIMEOUT SCSI_TIMEOUT
                    188:     #define ABORT_TIMEOUT SCSI_TIMEOUT
                    189:     #define RESET_TIMEOUT SCSI_TIMEOUT
                    190: #else
                    191:     #define SENSE_TIMEOUT (5*HZ/10)
                    192:     #define RESET_TIMEOUT (5*HZ/10)
                    193:     #define ABORT_TIMEOUT (5*HZ/10)
                    194: #endif
                    195: 
                    196: #define MIN_RESET_DELAY (1*HZ)
                    197: 
                    198: /* Do not call reset on error if we just did a reset within 10 sec. */
                    199: #define MIN_RESET_PERIOD (10*HZ)
                    200: 
                    201: /* The following devices are known not to tolerate a lun != 0 scan for
                    202:  * one reason or another.  Some will respond to all luns, others will
                    203:  * lock up. 
                    204:  */
                    205: 
                    206: #define BLIST_NOLUN     0x01
                    207: #define BLIST_FORCELUN  0x02
                    208: #define BLIST_BORKEN    0x04
                    209: #define BLIST_KEY       0x08
                    210: #define BLIST_SINGLELUN 0x10
                    211: 
                    212: struct dev_info{
                    213:     const char * vendor;
                    214:     const char * model;
                    215:     const char * revision; /* Latest revision known to be bad.  Not used yet */
                    216:     unsigned flags;
                    217: };
                    218: 
                    219: /*
                    220:  * This is what was previously known as the blacklist.  The concept
                    221:  * has been expanded so that we can specify other types of things we
                    222:  * need to be aware of.
                    223:  */
                    224: static struct dev_info device_list[] =
                    225: {
                    226: {"CHINON","CD-ROM CDS-431","H42", BLIST_NOLUN}, /* Locks up if polled for lun != 0 */
                    227: {"CHINON","CD-ROM CDS-535","Q14", BLIST_NOLUN}, /* Locks up if polled for lun != 0 */
                    228: {"DENON","DRD-25X","V", BLIST_NOLUN},           /* Locks up if probed for lun != 0 */
                    229: {"HITACHI","DK312C","CM81", BLIST_NOLUN},       /* Responds to all lun - dtg */
                    230: {"HITACHI","DK314C","CR21" , BLIST_NOLUN},      /* responds to all lun */
                    231: {"IMS", "CDD521/10","2.06", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
                    232: {"MAXTOR","XT-3280","PR02", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
                    233: {"MAXTOR","XT-4380S","B3C", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
                    234: {"MAXTOR","MXT-1240S","I1.2", BLIST_NOLUN},     /* Locks up when LUN>0 polled */
                    235: {"MAXTOR","XT-4170S","B5A", BLIST_NOLUN},       /* Locks-up sometimes when LUN>0 polled. */
                    236: {"MAXTOR","XT-8760S","B7B", BLIST_NOLUN},       /* guess what? */
                    237: {"MEDIAVIS","RENO CD-ROMX2A","2.03",BLIST_NOLUN},/*Responds to all lun */
                    238: {"NEC","CD-ROM DRIVE:841","1.0", BLIST_NOLUN},  /* Locks-up when LUN>0 polled. */
                    239: {"RODIME","RO3000S","2.33", BLIST_NOLUN},       /* Locks up if polled for lun != 0 */
                    240: {"SEAGATE", "ST157N", "\004|j", BLIST_NOLUN},   /* causes failed REQUEST SENSE on lun 1 
                    241:                                                 * for aha152x controller, which causes 
                    242:                                                 * SCSI code to reset bus.*/
                    243: {"SEAGATE", "ST296","921", BLIST_NOLUN},        /* Responds to all lun */
                    244: {"SEAGATE","ST1581","6538",BLIST_NOLUN},       /* Responds to all lun */
                    245: {"SONY","CD-ROM CDU-541","4.3d", BLIST_NOLUN},
                    246: {"SONY","CD-ROM CDU-55S","1.0i", BLIST_NOLUN},
                    247: {"SONY","CD-ROM CDU-561","1.7x", BLIST_NOLUN},
                    248: {"TANDBERG","TDC 3600","U07", BLIST_NOLUN},     /* Locks up if polled for lun != 0 */
                    249: {"TEAC","CD-ROM","1.06", BLIST_NOLUN},          /* causes failed REQUEST SENSE on lun 1 
                    250:                                                 * for seagate controller, which causes 
                    251:                                                 * SCSI code to reset bus.*/
                    252: {"TEXEL","CD-ROM","1.06", BLIST_NOLUN},         /* causes failed REQUEST SENSE on lun 1 
                    253:                                                 * for seagate controller, which causes 
                    254:                                                 * SCSI code to reset bus.*/
                    255: {"QUANTUM","LPS525S","3110", BLIST_NOLUN},      /* Locks sometimes if polled for lun != 0 */
                    256: {"QUANTUM","PD1225S","3110", BLIST_NOLUN},      /* Locks sometimes if polled for lun != 0 */
                    257: {"MEDIAVIS","CDR-H93MV","1.31", BLIST_NOLUN},   /* Locks up if polled for lun != 0 */
                    258: {"SANKYO", "CP525","6.64", BLIST_NOLUN},        /* causes failed REQ SENSE, extra reset */
                    259: {"HP", "C1750A", "3226", BLIST_NOLUN},          /* scanjet iic */
                    260: {"HP", "C1790A", "", BLIST_NOLUN},              /* scanjet iip */
                    261: {"HP", "C2500A", "", BLIST_NOLUN},              /* scanjet iicx */
                    262: 
                    263: /*
                    264:  * Other types of devices that have special flags.
                    265:  */
                    266: {"SONY","CD-ROM CDU-8001","*", BLIST_BORKEN},
                    267: {"TEXEL","CD-ROM","1.06", BLIST_BORKEN},
                    268: {"INSITE","Floptical   F*8I","*", BLIST_KEY},
                    269: {"INSITE","I325VM","*", BLIST_KEY},
                    270: {"PIONEER","CD-ROMDRM-602X","*", BLIST_FORCELUN | BLIST_SINGLELUN},
                    271: {"PIONEER","CD-ROMDRM-604X","*", BLIST_FORCELUN | BLIST_SINGLELUN},
                    272: /*
                    273:  * Must be at end of list...
                    274:  */
                    275: {NULL, NULL, NULL}
                    276: };
                    277: 
                    278: static int get_device_flags(unsigned char * response_data){
                    279:     int i = 0;
                    280:     unsigned char * pnt;
                    281:     for(i=0; 1; i++){
                    282:        if(device_list[i].vendor == NULL) return 0;
                    283:        pnt = &response_data[8];
                    284:        while(*pnt && *pnt == ' ') pnt++;
                    285:        if(memcmp(device_list[i].vendor, pnt,
                    286:                  strlen(device_list[i].vendor))) continue;
                    287:        pnt = &response_data[16];
                    288:        while(*pnt && *pnt == ' ') pnt++;
                    289:        if(memcmp(device_list[i].model, pnt,
                    290:                  strlen(device_list[i].model))) continue;
                    291:        return device_list[i].flags;
                    292:     }
                    293:     return 0;
                    294: }
                    295: 
                    296: void scsi_make_blocked_list(void)  {
                    297:     int block_count = 0, index;
                    298:     unsigned int flags;
                    299:     struct Scsi_Host * sh[128], * shpnt;
                    300:     
                    301:     /*
                    302:      * Create a circular linked list from the scsi hosts which have
                    303:      * the "wish_block" field in the Scsi_Host structure set.
                    304:      * The blocked list should include all the scsi hosts using ISA DMA.
                    305:      * In some systems, using two dma channels simultaneously causes
                    306:      * unpredictable results.
                    307:      * Among the scsi hosts in the blocked list, only one host at a time
                    308:      * is allowed to have active commands queued. The transition from
                    309:      * one active host to the next one is allowed only when host_busy == 0
                    310:      * for the active host (which implies host_busy == 0 for all the hosts
                    311:      * in the list). Moreover for block devices the transition to a new
                    312:      * active host is allowed only when a request is completed, since a
                    313:      * block device request can be divided into multiple scsi commands
                    314:      * (when there are few sg lists or clustering is disabled).
                    315:      *
                    316:      * (DB, 4 Feb 1995)
                    317:      */
                    318:     
                    319:     save_flags(flags);
                    320:     cli();
                    321:     host_active = NULL;
                    322:     
                    323:     for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next) {
                    324:        
                    325: #if 0
                    326:        /*
                    327:         * Is this is a candidate for the blocked list?
                    328:         * Useful to put into the blocked list all the hosts whose driver
                    329:         * does not know about the host->block feature.
                    330:         */
                    331:        if (shpnt->unchecked_isa_dma) shpnt->wish_block = 1;
                    332: #endif
                    333:        
                    334:        if (shpnt->wish_block) sh[block_count++] = shpnt;
                    335:     }
                    336:     
                    337:     if (block_count == 1) sh[0]->block = NULL;
                    338:     
                    339:     else if (block_count > 1) {
                    340:        
                    341:        for(index = 0; index < block_count - 1; index++) {
                    342:            sh[index]->block = sh[index + 1];
                    343:            printk("scsi%d : added to blocked host list.\n",
                    344:                   sh[index]->host_no);
                    345:        }
                    346:        
                    347:        sh[block_count - 1]->block = sh[0];
                    348:        printk("scsi%d : added to blocked host list.\n",
                    349:               sh[index]->host_no);
                    350:     }
                    351:     
                    352:     restore_flags(flags);
                    353: }
                    354: 
                    355: static void scan_scsis_done (Scsi_Cmnd * SCpnt)
                    356: {
                    357:     
                    358: #ifdef DEBUG
                    359:     printk ("scan_scsis_done(%p, %06x)\n", SCpnt->host, SCpnt->result);
                    360: #endif
                    361:     SCpnt->request.rq_status = RQ_SCSI_DONE;
                    362:     
                    363:     if (SCpnt->request.sem != NULL)
                    364:        up(SCpnt->request.sem);
                    365: }
                    366: 
                    367: #ifdef CONFIG_SCSI_MULTI_LUN
                    368: static int max_scsi_luns = 8;
                    369: #else
                    370: static int max_scsi_luns = 1;
                    371: #endif
                    372: 
                    373: void scsi_luns_setup(char *str, int *ints) {
                    374:     if (ints[0] != 1)
                    375:        printk("scsi_luns_setup : usage max_scsi_luns=n (n should be between 1 and 8)\n");
                    376:     else
                    377:        max_scsi_luns = ints[1];
                    378: }
                    379: 
                    380: /*
                    381:  *  Detecting SCSI devices :
                    382:  *  We scan all present host adapter's busses,  from ID 0 to ID (max_id).
                    383:  *  We use the INQUIRY command, determine device type, and pass the ID /
                    384:  *  lun address of all sequential devices to the tape driver, all random
                    385:  *  devices to the disk driver.
                    386:  */
                    387: static void scan_scsis (struct Scsi_Host *shpnt, unchar hardcoded,
                    388:                  unchar hchannel, unchar hid, unchar hlun)
                    389: {
                    390:   int dev, lun, channel;
                    391:   unsigned char scsi_result0[256];
                    392:   unsigned char *scsi_result;
                    393:   Scsi_Device *SDpnt;
                    394:   int max_dev_lun;
                    395:   Scsi_Cmnd *SCpnt;
                    396: 
                    397:   SCpnt = (Scsi_Cmnd *) scsi_init_malloc (sizeof (Scsi_Cmnd), GFP_ATOMIC | GFP_DMA);
                    398:   SDpnt = (Scsi_Device *) scsi_init_malloc (sizeof (Scsi_Device), GFP_ATOMIC);
                    399:   memset (SCpnt, 0, sizeof (Scsi_Cmnd));
                    400: 
                    401: 
                    402:   /* Make sure we have something that is valid for DMA purposes */
                    403:   scsi_result = ( ( !shpnt->unchecked_isa_dma )
                    404:                  ? &scsi_result0[0] : scsi_init_malloc (512, GFP_DMA));
                    405: 
                    406:   if (scsi_result == NULL) {
                    407:     printk ("Unable to obtain scsi_result buffer\n");
                    408:     goto leave;
                    409:   }
                    410: 
                    411:   /* We must chain ourself in the host_queue, so commands can time out */
                    412:   if(shpnt->host_queue)
                    413:       shpnt->host_queue->prev = SCpnt;
                    414:   SCpnt->next = shpnt->host_queue;
                    415:   SCpnt->prev = NULL;
                    416:   shpnt->host_queue = SCpnt;
                    417: 
                    418: 
                    419:   if (hardcoded == 1) {
                    420:     Scsi_Device *oldSDpnt=SDpnt;
                    421:     struct Scsi_Device_Template * sdtpnt;
                    422:     channel = hchannel;
                    423:     if(channel > shpnt->max_channel) goto leave;
                    424:     dev = hid;
                    425:     if(dev >= shpnt->max_id) goto leave;
                    426:     lun = hlun;
                    427:     if(lun >= shpnt->max_lun) goto leave;
                    428:     scan_scsis_single (channel, dev, lun, &max_dev_lun,
                    429:                    &SDpnt, SCpnt, shpnt, scsi_result);
                    430:     if(SDpnt!=oldSDpnt) {
                    431: 
                    432:        /* it could happen the blockdevice hasn't yet been inited */
                    433:     for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                    434:         if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
                    435: 
                    436:             oldSDpnt->scsi_request_fn = NULL;
                    437:             for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                    438:                 if(sdtpnt->attach) {
                    439:                  (*sdtpnt->attach)(oldSDpnt);
                    440:                   if(oldSDpnt->attached) scsi_build_commandblocks(oldSDpnt);}
                    441:            resize_dma_pool();
                    442:   
                    443:         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next) {
                    444:             if(sdtpnt->finish && sdtpnt->nr_dev)
                    445:                 {(*sdtpnt->finish)();}
                    446:        }
                    447:     }
                    448: 
                    449:   }
                    450:   else {
                    451:     for (channel = 0; channel <= shpnt->max_channel; channel++) {
                    452:       for (dev = 0; dev < shpnt->max_id; ++dev) {
                    453:         if (shpnt->this_id != dev) {
                    454: 
                    455:           /*
                    456:            * We need the for so our continue, etc. work fine. We put this in
                    457:            * a variable so that we can override it during the scan if we
                    458:            * detect a device *KNOWN* to have multiple logical units.
                    459:            */
                    460:           max_dev_lun = (max_scsi_luns < shpnt->max_lun ?
                    461:                          max_scsi_luns : shpnt->max_lun);
                    462:           for (lun = 0; lun < max_dev_lun; ++lun) {
                    463:             if (!scan_scsis_single (channel, dev, lun, &max_dev_lun,
                    464:                                     &SDpnt, SCpnt, shpnt, scsi_result))
                    465:               break; /* break means don't probe further for luns!=0 */
                    466:           }                     /* for lun ends */
                    467:         }                       /* if this_id != id ends */
                    468:       }                         /* for dev ends */
                    469:     }                           /* for channel ends */
                    470:   }                            /* if/else hardcoded */
                    471: 
                    472:   leave:
                    473: 
                    474:   {/* Unchain SCpnt from host_queue */
                    475:     Scsi_Cmnd *prev,*next,*hqptr;
                    476:     for(hqptr=shpnt->host_queue; hqptr!=SCpnt; hqptr=hqptr->next) ;
                    477:     if(hqptr) {
                    478:       prev=hqptr->prev;
                    479:       next=hqptr->next;
                    480:       if(prev) 
                    481:        prev->next=next;
                    482:       else 
                    483:        shpnt->host_queue=next;
                    484:       if(next) next->prev=prev;
                    485:     }
                    486:   }
                    487:  
                    488:      /* Last device block does not exist.  Free memory. */
                    489:     if (SDpnt != NULL)
                    490:       scsi_init_free ((char *) SDpnt, sizeof (Scsi_Device));
                    491: 
                    492:     if (SCpnt != NULL)
                    493:       scsi_init_free ((char *) SCpnt, sizeof (Scsi_Cmnd));
                    494: 
                    495:     /* If we allocated a buffer so we could do DMA, free it now */
                    496:     if (scsi_result != &scsi_result0[0] && scsi_result != NULL)
                    497:       scsi_init_free (scsi_result, 512);
                    498: 
                    499: }
                    500: 
                    501: /*
                    502:  * The worker for scan_scsis.
                    503:  * Returning 0 means Please don't ask further for lun!=0, 1 means OK go on.
                    504:  * Global variables used : scsi_devices(linked list)
                    505:  */
                    506: int scan_scsis_single (int channel, int dev, int lun, int *max_dev_lun,
                    507:     Scsi_Device **SDpnt2, Scsi_Cmnd * SCpnt, struct Scsi_Host * shpnt, 
                    508:     char *scsi_result)
                    509: {
                    510:   unsigned char scsi_cmd[12];
                    511:   struct Scsi_Device_Template *sdtpnt;
                    512:   Scsi_Device * SDtail, *SDpnt=*SDpnt2;
                    513:   int bflags, type=-1;
                    514: 
                    515:   SDtail = scsi_devices;
                    516:   if (scsi_devices)
                    517:     while (SDtail->next)
                    518:       SDtail = SDtail->next;
                    519: 
                    520:   memset (SDpnt, 0, sizeof (Scsi_Device));
                    521:   SDpnt->host = shpnt;
                    522:   SDpnt->id = dev;
                    523:   SDpnt->lun = lun;
                    524:   SDpnt->channel = channel;
                    525: 
                    526:   /* Some low level driver could use device->type (DB) */
                    527:   SDpnt->type = -1;
                    528: 
                    529:   /*
                    530:    * Assume that the device will have handshaking problems, and then fix this
                    531:    * field later if it turns out it doesn't
                    532:    */
                    533:   SDpnt->borken = 1;
                    534:   SDpnt->was_reset = 0;
                    535:   SDpnt->expecting_cc_ua = 0;
                    536: 
                    537:   scsi_cmd[0] = TEST_UNIT_READY;
                    538:   scsi_cmd[1] = lun << 5;
                    539:   scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[4] = scsi_cmd[5] = 0;
                    540: 
                    541:   SCpnt->host = SDpnt->host;
                    542:   SCpnt->device = SDpnt;
                    543:   SCpnt->target = SDpnt->id;
                    544:   SCpnt->lun = SDpnt->lun;
                    545:   SCpnt->channel = SDpnt->channel;
                    546:   {
                    547:     struct semaphore sem = MUTEX_LOCKED;
                    548:     SCpnt->request.sem = &sem;
                    549:     SCpnt->request.rq_status = RQ_SCSI_BUSY;
                    550:     scsi_do_cmd (SCpnt, (void *) scsi_cmd,
                    551:                  (void *) scsi_result,
                    552:                  256, scan_scsis_done, SCSI_TIMEOUT + 4 * HZ, 5);
                    553:     down (&sem);
                    554:   }
                    555: 
                    556: #if defined(DEBUG) || defined(DEBUG_INIT)
                    557:   printk ("scsi: scan_scsis_single id %d lun %d. Return code 0x%08x\n",
                    558:           dev, lun, SCpnt->result);
                    559:   print_driverbyte(SCpnt->result); print_hostbyte(SCpnt->result);
                    560:   printk("\n");
                    561: #endif
                    562: 
                    563:   if (SCpnt->result) {
                    564:     if (((driver_byte (SCpnt->result) & DRIVER_SENSE) ||
                    565:          (status_byte (SCpnt->result) & CHECK_CONDITION)) &&
                    566:         ((SCpnt->sense_buffer[0] & 0x70) >> 4) == 7) {
                    567:       if (((SCpnt->sense_buffer[2] & 0xf) != NOT_READY) &&
                    568:           ((SCpnt->sense_buffer[2] & 0xf) != UNIT_ATTENTION) &&
                    569:           ((SCpnt->sense_buffer[2] & 0xf) != ILLEGAL_REQUEST || lun > 0))
                    570:         return 1;
                    571:     }
                    572:     else
                    573:       return 0;
                    574:   }
                    575: 
                    576: #if defined (DEBUG) || defined(DEBUG_INIT)
                    577:   printk ("scsi: performing INQUIRY\n");
                    578: #endif
                    579:   /*
                    580:    * Build an INQUIRY command block.
                    581:    */
                    582:   scsi_cmd[0] = INQUIRY;
                    583:   scsi_cmd[1] = (lun << 5) & 0xe0;
                    584:   scsi_cmd[2] = 0;
                    585:   scsi_cmd[3] = 0;
                    586:   scsi_cmd[4] = 255;
                    587:   scsi_cmd[5] = 0;
                    588:   SCpnt->cmd_len = 0;
                    589:   {
                    590:     struct semaphore sem = MUTEX_LOCKED;
                    591:     SCpnt->request.sem = &sem;
                    592:     SCpnt->request.rq_status = RQ_SCSI_BUSY;
                    593:     scsi_do_cmd (SCpnt, (void *) scsi_cmd,
                    594:                  (void *) scsi_result,
                    595:                  256, scan_scsis_done, SCSI_TIMEOUT, 3);
                    596:     down (&sem);
                    597:   }
                    598: 
                    599: #if defined(DEBUG) || defined(DEBUG_INIT)
                    600:   printk ("scsi: INQUIRY %s with code 0x%x\n",
                    601:           SCpnt->result ? "failed" : "successful", SCpnt->result);
                    602: #endif
                    603: 
                    604:   if (SCpnt->result)
                    605:     return 0;     /* assume no peripheral if any sort of error */
                    606: 
                    607:   /*
                    608:    * It would seem some TOSHIBA CDROM gets things wrong
                    609:    */
                    610:   if (!strncmp (scsi_result + 8, "TOSHIBA", 7) &&
                    611:       !strncmp (scsi_result + 16, "CD-ROM", 6) &&
                    612:       scsi_result[0] == TYPE_DISK) {
                    613:     scsi_result[0] = TYPE_ROM;
                    614:     scsi_result[1] |= 0x80;     /* removable */
                    615:   }
                    616: 
                    617:   if (!strncmp (scsi_result + 8, "NEC", 3)) {
                    618:     if (!strncmp (scsi_result + 16, "CD-ROM DRIVE:84 ", 16) ||
                    619:         !strncmp (scsi_result + 16, "CD-ROM DRIVE:25", 15))
                    620:       SDpnt->manufacturer = SCSI_MAN_NEC_OLDCDR;
                    621:     else
                    622:       SDpnt->manufacturer = SCSI_MAN_NEC;
                    623:   }
                    624:   else if (!strncmp (scsi_result + 8, "TOSHIBA", 7))
                    625:     SDpnt->manufacturer = SCSI_MAN_TOSHIBA;
                    626:   else if (!strncmp (scsi_result + 8, "SONY", 4))
                    627:     SDpnt->manufacturer = SCSI_MAN_SONY;
                    628:   else if (!strncmp (scsi_result + 8, "PIONEER", 7))
                    629:     SDpnt->manufacturer = SCSI_MAN_PIONEER;
                    630:   else
                    631:     SDpnt->manufacturer = SCSI_MAN_UNKNOWN;
                    632: 
                    633:   memcpy (SDpnt->vendor, scsi_result + 8, 8);
                    634:   memcpy (SDpnt->model, scsi_result + 16, 16);
                    635:   memcpy (SDpnt->rev, scsi_result + 32, 4);
                    636: 
                    637:   SDpnt->removable = (0x80 & scsi_result[1]) >> 7;
                    638:   SDpnt->lockable = SDpnt->removable;
                    639:   SDpnt->changed = 0;
                    640:   SDpnt->access_count = 0;
                    641:   SDpnt->busy = 0;
                    642:   SDpnt->has_cmdblocks = 0;
                    643:   /*
                    644:    * Currently, all sequential devices are assumed to be tapes, all random
                    645:    * devices disk, with the appropriate read only flags set for ROM / WORM
                    646:    * treated as RO.
                    647:    */
                    648:   switch (type = (scsi_result[0] & 0x1f)) {
                    649:   case TYPE_TAPE:
                    650:   case TYPE_DISK:
                    651:   case TYPE_MOD:
                    652:   case TYPE_PROCESSOR:
                    653:   case TYPE_SCANNER:
                    654:     SDpnt->writeable = 1;
                    655:     break;
                    656:   case TYPE_WORM:
                    657:   case TYPE_ROM:
                    658:     SDpnt->writeable = 0;
                    659:     break;
                    660:   default:
                    661:     printk ("scsi: unknown type %d\n", type);
                    662:   }
                    663: 
                    664:   SDpnt->single_lun = 0;
                    665:   SDpnt->soft_reset =
                    666:     (scsi_result[7] & 1) && ((scsi_result[3] & 7) == 2);
                    667:   SDpnt->random = (type == TYPE_TAPE) ? 0 : 1;
                    668:   SDpnt->type = (type & 0x1f);
                    669: 
                    670:   print_inquiry (scsi_result);
                    671: 
                    672:   for (sdtpnt = scsi_devicelist; sdtpnt;
                    673:        sdtpnt = sdtpnt->next)
                    674:     if (sdtpnt->detect)
                    675:       SDpnt->attached +=
                    676:         (*sdtpnt->detect) (SDpnt);
                    677: 
                    678:   SDpnt->scsi_level = scsi_result[2] & 0x07;
                    679:   if (SDpnt->scsi_level >= 2 ||
                    680:       (SDpnt->scsi_level == 1 &&
                    681:        (scsi_result[3] & 0x0f) == 1))
                    682:     SDpnt->scsi_level++;
                    683: 
                    684:   /*
                    685:    * Set the tagged_queue flag for SCSI-II devices that purport to support
                    686:    * tagged queuing in the INQUIRY data.
                    687:    */
                    688:   SDpnt->tagged_queue = 0;
                    689:   if ((SDpnt->scsi_level >= SCSI_2) &&
                    690:       (scsi_result[7] & 2)) {
                    691:     SDpnt->tagged_supported = 1;
                    692:     SDpnt->current_tag = 0;
                    693:   }
                    694: 
                    695:   /*
                    696:    * Accommodate drivers that want to sleep when they should be in a polling
                    697:    * loop.
                    698:    */
                    699:   SDpnt->disconnect = 0;
                    700: 
                    701:   /*
                    702:    * Get any flags for this device.
                    703:    */
                    704:   bflags = get_device_flags (scsi_result);
                    705: 
                    706:   /*
                    707:    * Some revisions of the Texel CD ROM drives have handshaking problems when
                    708:    * used with the Seagate controllers.  Before we know what type of device
                    709:    * we're talking to, we assume it's borken and then change it here if it
                    710:    * turns out that it isn't a TEXEL drive.
                    711:    */
                    712:   if ((bflags & BLIST_BORKEN) == 0)
                    713:     SDpnt->borken = 0;
                    714: 
                    715:   /*
                    716:    * These devices need this "key" to unlock the devices so we can use it
                    717:    */
                    718:   if ((bflags & BLIST_KEY) != 0) {
                    719:     printk ("Unlocked floptical drive.\n");
                    720:     SDpnt->lockable = 0;
                    721:     scsi_cmd[0] = MODE_SENSE;
                    722:     scsi_cmd[1] = (lun << 5) & 0xe0;
                    723:     scsi_cmd[2] = 0x2e;
                    724:     scsi_cmd[3] = 0;
                    725:     scsi_cmd[4] = 0x2a;
                    726:     scsi_cmd[5] = 0;
                    727:     SCpnt->cmd_len = 0;
                    728:     {
                    729:       struct semaphore sem = MUTEX_LOCKED;
                    730:       SCpnt->request.rq_status = RQ_SCSI_BUSY;
                    731:       SCpnt->request.sem = &sem;
                    732:       scsi_do_cmd (SCpnt, (void *) scsi_cmd,
                    733:                    (void *) scsi_result, 0x2a,
                    734:                    scan_scsis_done, SCSI_TIMEOUT, 3);
                    735:       down (&sem);
                    736:     }
                    737:   }
                    738:   /* Add this device to the linked list at the end */
                    739:   if (SDtail)
                    740:     SDtail->next = SDpnt;
                    741:   else
                    742:     scsi_devices = SDpnt;
                    743:   SDtail = SDpnt;
                    744: 
                    745:   SDpnt = (Scsi_Device *) scsi_init_malloc (sizeof (Scsi_Device), GFP_ATOMIC);
                    746:   *SDpnt2=SDpnt;
                    747:   if (!SDpnt)
                    748:     printk ("scsi: scan_scsis_single: Cannot malloc\n");
                    749: 
                    750: 
                    751:   /*
                    752:    * Some scsi devices cannot be polled for lun != 0 due to firmware bugs
                    753:    */
                    754:   if (bflags & BLIST_NOLUN)
                    755:     return 0;                   /* break; */
                    756: 
                    757:   /*
                    758:    * If we want to only allow I/O to one of the luns attached to this device
                    759:    * at a time, then we set this flag.
                    760:    */
                    761:   if (bflags & BLIST_SINGLELUN)
                    762:     SDpnt->single_lun = 1;
                    763: 
                    764:   /*
                    765:    * If this device is known to support multiple units, override the other
                    766:    * settings, and scan all of them.
                    767:    */
                    768:   if (bflags & BLIST_FORCELUN)
                    769:     *max_dev_lun = 8;
                    770:   /*
                    771:    * We assume the device can't handle lun!=0 if: - it reports scsi-0 (ANSI
                    772:    * SCSI Revision 0) (old drives like MAXTOR XT-3280) or - it reports scsi-1
                    773:    * (ANSI SCSI Revision 1) and Response Data Format 0
                    774:    */
                    775:   if (((scsi_result[2] & 0x07) == 0)
                    776:       ||
                    777:       ((scsi_result[2] & 0x07) == 1 &&
                    778:        (scsi_result[3] & 0x0f) == 0))
                    779:     return 0;
                    780:   return 1;
                    781: }
                    782: 
                    783: /*
                    784:  *  Flag bits for the internal_timeout array
                    785:  */
                    786: #define NORMAL_TIMEOUT 0
                    787: #define IN_ABORT 1
                    788: #define IN_RESET 2
                    789: 
                    790: /*
                    791:  * This is our time out function, called when the timer expires for a
                    792:  * given host adapter.  It will attempt to abort the currently executing
                    793:  * command, that failing perform a kernel panic.
                    794:  */
                    795: 
                    796: static void scsi_times_out (Scsi_Cmnd * SCpnt, int pid)
                    797: {
                    798:     
                    799:     switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET))
                    800:     {
                    801:     case NORMAL_TIMEOUT:
                    802:        {
                    803: #ifdef DEBUG_TIMEOUT
                    804:            scsi_dump_status();
                    805: #endif
                    806:        }
                    807:        
                    808:        if (!scsi_abort (SCpnt, DID_TIME_OUT, pid))
                    809:            return;
                    810:     case IN_ABORT:
                    811:        printk("SCSI host %d abort (pid %ld) timed out - resetting\n",
                    812:               SCpnt->host->host_no, SCpnt->pid);
                    813:        if (!scsi_reset (SCpnt, FALSE))
                    814:            return;
                    815:     case IN_RESET:
                    816:     case (IN_ABORT | IN_RESET):
                    817:        /* This might be controversial, but if there is a bus hang,
                    818:         * you might conceivably want the machine up and running
                    819:         * esp if you have an ide disk. 
                    820:         */
                    821:        printk("Unable to reset scsi host %d - ", SCpnt->host->host_no);
                    822:        printk("probably a SCSI bus hang.\n");
                    823:        SCpnt->internal_timeout &= ~IN_RESET;
                    824:         scsi_reset (SCpnt, TRUE);
                    825:        return;
                    826:        
                    827:     default:
                    828:        INTERNAL_ERROR;
                    829:     }
                    830:     
                    831: }
                    832: 
                    833: 
                    834: /* This function takes a quick look at a request, and decides if it
                    835:  * can be queued now, or if there would be a stall while waiting for
                    836:  * something else to finish.  This routine assumes that interrupts are
                    837:  * turned off when entering the routine.  It is the responsibility
                    838:  * of the calling code to ensure that this is the case. 
                    839:  */
                    840: 
                    841: Scsi_Cmnd * request_queueable (struct request * req, Scsi_Device * device)
                    842: {
                    843:     Scsi_Cmnd * SCpnt = NULL;
                    844:     int tablesize;
                    845:     Scsi_Cmnd * found = NULL;
                    846:     struct buffer_head * bh, *bhp;
                    847:     
                    848:     if (!device)
                    849:        panic ("No device passed to request_queueable().\n");
                    850:     
                    851:     if (req && req->rq_status == RQ_INACTIVE)
                    852:        panic("Inactive in request_queueable");
                    853:     
                    854:     SCpnt =  device->host->host_queue;
                    855: 
                    856:     /*
                    857:      * Look for a free command block.  If we have been instructed not to queue
                    858:      * multiple commands to multi-lun devices, then check to see what else is 
                    859:      * going for this device first.
                    860:      */
                    861:       
                    862:     SCpnt = device->host->host_queue;
                    863:     if (!device->single_lun) {
                    864:        while(SCpnt){
                    865:            if(SCpnt->target == device->id &&
                    866:               SCpnt->lun == device->lun) {
                    867:                if(SCpnt->request.rq_status == RQ_INACTIVE) break;
                    868:            }
                    869:            SCpnt = SCpnt->next;
                    870:        }
                    871:     } else {
                    872:        while(SCpnt){
                    873:            if(SCpnt->target == device->id) {
                    874:                if (SCpnt->lun == device->lun) {
                    875:                    if(found == NULL 
                    876:                       && SCpnt->request.rq_status == RQ_INACTIVE) 
                    877:                    {
                    878:                        found=SCpnt;
                    879:                    }
                    880:                } 
                    881:                if(SCpnt->request.rq_status != RQ_INACTIVE) {
                    882:                    /*
                    883:                     * I think that we should really limit things to one
                    884:                     * outstanding command per device - this is what tends 
                    885:                      * to trip up buggy firmware.
                    886:                     */
                    887:                    return NULL;
                    888:                }
                    889:            }
                    890:            SCpnt = SCpnt->next;
                    891:        }
                    892:        SCpnt = found;
                    893:     }
                    894:     
                    895:     if (!SCpnt) return NULL;
                    896:     
                    897:     if (SCSI_BLOCK(device->host)) return NULL;
                    898:     
                    899:     if (req) {
                    900:        memcpy(&SCpnt->request, req, sizeof(struct request));
                    901:        tablesize = device->host->sg_tablesize;
                    902:        bhp = bh = req->bh;
                    903:        if(!tablesize) bh = NULL;
                    904:        /* Take a quick look through the table to see how big it is.  
                    905:         * We already have our copy of req, so we can mess with that 
                    906:         * if we want to. 
                    907:         */
                    908:        while(req->nr_sectors && bh){
                    909:            bhp = bhp->b_reqnext;
                    910:            if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
                    911:            req->nr_sectors -= bh->b_size >> 9;
                    912:            req->sector += bh->b_size >> 9;
                    913:            if(!tablesize) break;
                    914:            bh = bhp;
                    915:        }
                    916:        if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
                    917:            SCpnt->request.bhtail = bh;
                    918:            req->bh = bh->b_reqnext; /* Divide request */
                    919:            bh->b_reqnext = NULL;
                    920:            bh = req->bh;
                    921:            
                    922:            /* Now reset things so that req looks OK */
                    923:            SCpnt->request.nr_sectors -= req->nr_sectors;
                    924:            req->current_nr_sectors = bh->b_size >> 9;
                    925:            req->buffer = bh->b_data;
                    926:            SCpnt->request.sem = NULL; /* Wait until whole thing done */
                    927:        } else {
                    928:            req->rq_status = RQ_INACTIVE;
                    929:            wake_up(&wait_for_request);
                    930:        }
                    931:     } else {
                    932:        SCpnt->request.rq_status = RQ_SCSI_BUSY;  /* Busy, but no request */
                    933:        SCpnt->request.sem = NULL;   /* And no one is waiting for the device 
                    934:                                      * either */
                    935:     }
                    936:     
                    937:     SCpnt->use_sg = 0;               /* Reset the scatter-gather flag */
                    938:     SCpnt->old_use_sg  = 0;
                    939:     SCpnt->transfersize = 0;
                    940:     SCpnt->underflow = 0;
                    941:     SCpnt->cmd_len = 0;
                    942: 
                    943: /* Since not everyone seems to set the device info correctly
                    944:  * before Scsi_Cmnd gets send out to scsi_do_command, we do it here.
                    945:  */ 
                    946:     SCpnt->channel = device->channel;
                    947:     SCpnt->lun = device->lun;
                    948:     SCpnt->target = device->id;
                    949: 
                    950:     return SCpnt;
                    951: }
                    952: 
                    953: /* This function returns a structure pointer that will be valid for
                    954:  * the device.  The wait parameter tells us whether we should wait for
                    955:  * the unit to become free or not.  We are also able to tell this routine
                    956:  * not to return a descriptor if the host is unable to accept any more
                    957:  * commands for the time being.  We need to keep in mind that there is no
                    958:  * guarantee that the host remain not busy.  Keep in mind the
                    959:  * request_queueable function also knows the internal allocation scheme
                    960:  * of the packets for each device 
                    961:  */
                    962: 
                    963: Scsi_Cmnd * allocate_device (struct request ** reqp, Scsi_Device * device,
                    964:                             int wait)
                    965: {
                    966:     kdev_t dev;
                    967:     struct request * req = NULL;
                    968:     int tablesize;
                    969:     unsigned int flags;
                    970:     struct buffer_head * bh, *bhp;
                    971:     struct Scsi_Host * host;
                    972:     Scsi_Cmnd * SCpnt = NULL;
                    973:     Scsi_Cmnd * SCwait = NULL;
                    974:     Scsi_Cmnd * found = NULL;
                    975:     
                    976:     if (!device)
                    977:        panic ("No device passed to allocate_device().\n");
                    978:     
                    979:     if (reqp) req = *reqp;
                    980:     
                    981:     /* See if this request has already been queued by an interrupt routine */
                    982:     if (req) {
                    983:        if(req->rq_status == RQ_INACTIVE) return NULL;
                    984:        dev = req->rq_dev;
                    985:     } else
                    986:         dev = 0;               /* unused */
                    987:     
                    988:     host = device->host;
                    989:     
                    990:     if (intr_count && SCSI_BLOCK(host)) return NULL;
                    991:     
                    992:     while (1==1){
                    993:        SCpnt = device->host->host_queue;
                    994:        if (!device->single_lun) {
                    995:            while(SCpnt){
                    996:                if(SCpnt->target == device->id &&
                    997:                   SCpnt->lun == device->lun) {
                    998:                   SCwait = SCpnt;
                    999:                    if(SCpnt->request.rq_status == RQ_INACTIVE) break;
                   1000:                }
                   1001:                SCpnt = SCpnt->next;
                   1002:            }
                   1003:        } else {
                   1004:            while(SCpnt){
                   1005:                if(SCpnt->target == device->id) {
                   1006:                    if (SCpnt->lun == device->lun) {
                   1007:                        SCwait = SCpnt;
                   1008:                        if(found == NULL 
                   1009:                           && SCpnt->request.rq_status == RQ_INACTIVE) 
                   1010:                        {
                   1011:                            found=SCpnt;
                   1012:                        }
                   1013:                    } 
                   1014:                    if(SCpnt->request.rq_status != RQ_INACTIVE) {
                   1015:                        /*
                   1016:                         * I think that we should really limit things to one
                   1017:                         * outstanding command per device - this is what tends
                   1018:                          * to trip up buggy firmware.
                   1019:                         */
                   1020:                        found = NULL;
                   1021:                        break;
                   1022:                    }
                   1023:                }
                   1024:                SCpnt = SCpnt->next;
                   1025:            }
                   1026:            SCpnt = found;
                   1027:        }
                   1028: 
                   1029:        save_flags(flags);
                   1030:        cli();
                   1031:        /* See if this request has already been queued by an interrupt routine
                   1032:         */
                   1033:        if (req && (req->rq_status == RQ_INACTIVE || req->rq_dev != dev)) {
                   1034:            restore_flags(flags);
                   1035:            return NULL;
                   1036:        }
                   1037:        if (!SCpnt || SCpnt->request.rq_status != RQ_INACTIVE)  /* Might have changed */
                   1038:        {
                   1039:            restore_flags(flags);
                   1040:            if(!wait) return NULL;
                   1041:            if (!SCwait) {
                   1042:                printk("Attempt to allocate device channel %d, target %d, "
                   1043:                       "lun %d\n", device->channel, device->id, device->lun);
                   1044:                panic("No device found in allocate_device\n");
                   1045:            }
                   1046:            SCSI_SLEEP(&device->device_wait,
                   1047:                       (SCwait->request.rq_status != RQ_INACTIVE));
                   1048:        } else {
                   1049:            if (req) {
                   1050:                memcpy(&SCpnt->request, req, sizeof(struct request));
                   1051:                tablesize = device->host->sg_tablesize;
                   1052:                bhp = bh = req->bh;
                   1053:                if(!tablesize) bh = NULL;
                   1054:                /* Take a quick look through the table to see how big it is.  
                   1055:                 * We already have our copy of req, so we can mess with that 
                   1056:                 * if we want to.  
                   1057:                 */
                   1058:                while(req->nr_sectors && bh){
                   1059:                    bhp = bhp->b_reqnext;
                   1060:                    if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
                   1061:                    req->nr_sectors -= bh->b_size >> 9;
                   1062:                    req->sector += bh->b_size >> 9;
                   1063:                    if(!tablesize) break;
                   1064:                    bh = bhp;
                   1065:                }
                   1066:                if(req->nr_sectors && bh && bh->b_reqnext){/* Any leftovers? */
                   1067:                    SCpnt->request.bhtail = bh;
                   1068:                    req->bh = bh->b_reqnext; /* Divide request */
                   1069:                    bh->b_reqnext = NULL;
                   1070:                    bh = req->bh;
                   1071:                    /* Now reset things so that req looks OK */
                   1072:                    SCpnt->request.nr_sectors -= req->nr_sectors;
                   1073:                    req->current_nr_sectors = bh->b_size >> 9;
                   1074:                    req->buffer = bh->b_data;
                   1075:                    SCpnt->request.sem = NULL; /* Wait until whole thing done*/
                   1076:                }
                   1077:                else
                   1078:                {
                   1079:                    req->rq_status = RQ_INACTIVE;
                   1080:                    *reqp = req->next;
                   1081:                    wake_up(&wait_for_request);
                   1082:                }
                   1083:            } else {
                   1084:                SCpnt->request.rq_status = RQ_SCSI_BUSY;
                   1085:                SCpnt->request.sem = NULL;   /* And no one is waiting for this 
                   1086:                                              * to complete */
                   1087:            }
                   1088:            restore_flags(flags);
                   1089:            break;
                   1090:        }
                   1091:     }
                   1092:     
                   1093:     SCpnt->use_sg = 0;            /* Reset the scatter-gather flag */
                   1094:     SCpnt->old_use_sg  = 0;
                   1095:     SCpnt->transfersize = 0;      /* No default transfer size */
                   1096:     SCpnt->cmd_len = 0;
                   1097: 
                   1098:     SCpnt->underflow = 0;         /* Do not flag underflow conditions */
                   1099: 
                   1100:     /* Since not everyone seems to set the device info correctly
                   1101:      * before Scsi_Cmnd gets send out to scsi_do_command, we do it here.
                   1102:      */ 
                   1103:     SCpnt->channel = device->channel;
                   1104:     SCpnt->lun = device->lun;
                   1105:     SCpnt->target = device->id;
                   1106: 
                   1107:     return SCpnt;
                   1108: }
                   1109: 
                   1110: /*
                   1111:  * This is inline because we have stack problemes if we recurse to deeply.
                   1112:  */
                   1113: 
                   1114: inline void internal_cmnd (Scsi_Cmnd * SCpnt)
                   1115: {
                   1116:     int temp;
                   1117:     struct Scsi_Host * host;
                   1118:     unsigned int flags;
                   1119: #ifdef DEBUG_DELAY
                   1120:     int clock;
                   1121: #endif
                   1122:     
                   1123:     host = SCpnt->host;
                   1124:     
                   1125:     /*
                   1126:      * We will wait MIN_RESET_DELAY clock ticks after the last reset so
                   1127:      * we can avoid the drive not being ready.
                   1128:      */
                   1129:     save_flags(flags);
                   1130:     sti();
                   1131:     temp = host->last_reset + MIN_RESET_DELAY;
                   1132:     while (jiffies < temp);
                   1133:     restore_flags(flags);
                   1134:     
                   1135:     update_timeout(SCpnt, SCpnt->timeout_per_command);
                   1136:     
                   1137:     /*
                   1138:      * We will use a queued command if possible, otherwise we will emulate the
                   1139:      * queuing and calling of completion function ourselves.
                   1140:      */
                   1141: #ifdef DEBUG
                   1142:     printk("internal_cmnd (host = %d, channel = %d, target = %d, "
                   1143:           "command = %p, buffer = %p, \nbufflen = %d, done = %p)\n", 
                   1144:           SCpnt->host->host_no, SCpnt->channel, SCpnt->target, SCpnt->cmnd, 
                   1145:           SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
                   1146: #endif
                   1147:     
                   1148:     if (host->can_queue)
                   1149:     {
                   1150: #ifdef DEBUG
                   1151:        printk("queuecommand : routine at %p\n",
                   1152:               host->hostt->queuecommand);
                   1153: #endif
                   1154:        /* This locking tries to prevent all sorts of races between
                   1155:         * queuecommand and the interrupt code.  In effect,
                   1156:         * we are only allowed to be in queuecommand once at
                   1157:         * any given time, and we can only be in the interrupt
                   1158:         * handler and the queuecommand function at the same time
                   1159:         * when queuecommand is called while servicing the
                   1160:         * interrupt. 
                   1161:         */
                   1162:        
                   1163:        if(!intr_count && SCpnt->host->irq)
                   1164:            disable_irq(SCpnt->host->irq);
                   1165:        
                   1166:        host->hostt->queuecommand (SCpnt, scsi_done);
                   1167:        
                   1168:        if(!intr_count && SCpnt->host->irq)
                   1169:            enable_irq(SCpnt->host->irq);
                   1170:     }
                   1171:     else
                   1172:     {
                   1173:        
                   1174: #ifdef DEBUG
                   1175:        printk("command() :  routine at %p\n", host->hostt->command);
                   1176: #endif
                   1177:        temp=host->hostt->command (SCpnt);
                   1178:        SCpnt->result = temp;
                   1179: #ifdef DEBUG_DELAY
                   1180:        clock = jiffies + 4 * HZ;
                   1181:        while (jiffies < clock);
                   1182:        printk("done(host = %d, result = %04x) : routine at %p\n", 
                   1183:               host->host_no, temp, host->hostt->command);
                   1184: #endif
                   1185:        scsi_done(SCpnt);
                   1186:     }
                   1187: #ifdef DEBUG
                   1188:     printk("leaving internal_cmnd()\n");
                   1189: #endif
                   1190: }
                   1191: 
                   1192: static void scsi_request_sense (Scsi_Cmnd * SCpnt)
                   1193: {
                   1194:     unsigned int flags;
                   1195:     
                   1196:     save_flags(flags);
                   1197:     cli();
                   1198:     SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
                   1199:     update_timeout(SCpnt, SENSE_TIMEOUT);
                   1200:     restore_flags(flags);
                   1201:     
                   1202:     
                   1203:     memcpy ((void *) SCpnt->cmnd , (void *) generic_sense, 
                   1204:            sizeof(generic_sense));
                   1205:     
                   1206:     SCpnt->cmnd[1] = SCpnt->lun << 5;
                   1207:     SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
                   1208:     
                   1209:     SCpnt->request_buffer = &SCpnt->sense_buffer;
                   1210:     SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
                   1211:     SCpnt->use_sg = 0;
                   1212:     SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
                   1213:     internal_cmnd (SCpnt);
                   1214: }
                   1215: 
                   1216: 
                   1217: 
                   1218: /*
                   1219:  * scsi_do_cmd sends all the commands out to the low-level driver.  It
                   1220:  * handles the specifics required for each low level driver - ie queued
                   1221:  * or non queued.  It also prevents conflicts when different high level
                   1222:  * drivers go for the same host at the same time.
                   1223:  */
                   1224: 
                   1225: void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd ,
                   1226:                  void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
                   1227:                  int timeout, int retries)
                   1228: {
                   1229:     unsigned long flags;
                   1230:     struct Scsi_Host * host = SCpnt->host;
                   1231:     
                   1232: #ifdef DEBUG
                   1233:     {
                   1234:        int i;
                   1235:        int target = SCpnt->target;
                   1236:        printk ("scsi_do_cmd (host = %d, channel = %d target = %d, "
                   1237:                "buffer =%p, bufflen = %d, done = %p, timeout = %d, "
                   1238:                "retries = %d)\n"
                   1239:                "command : " , host->host_no, SCpnt->channel, target, buffer, 
                   1240:                bufflen, done, timeout, retries);
                   1241:        for (i = 0; i < 10; ++i)
                   1242:            printk ("%02x  ", ((unsigned char *) cmnd)[i]);
                   1243:        printk("\n");
                   1244:     }
                   1245: #endif
                   1246:     
                   1247:     if (!host)
                   1248:     {
                   1249:        panic ("Invalid or not present host.\n");
                   1250:     }
                   1251:     
                   1252:     
                   1253:     /*
                   1254:      * We must prevent reentrancy to the lowlevel host driver.  This prevents
                   1255:      * it - we enter a loop until the host we want to talk to is not busy.
                   1256:      * Race conditions are prevented, as interrupts are disabled in between the
                   1257:      * time we check for the host being not busy, and the time we mark it busy
                   1258:      * ourselves.
                   1259:      */
                   1260: 
                   1261:     save_flags(flags);
                   1262:     cli();
                   1263:     SCpnt->pid = scsi_pid++;
                   1264:     
                   1265:     while (SCSI_BLOCK(host)) {
                   1266:        restore_flags(flags);
                   1267:        SCSI_SLEEP(&host->host_wait, SCSI_BLOCK(host));
                   1268:        cli();
                   1269:     }
                   1270:     
                   1271:     if (host->block) host_active = host;
                   1272:     
                   1273:     host->host_busy++;
                   1274:     restore_flags(flags);
                   1275:     
                   1276:     /*
                   1277:      * Our own function scsi_done (which marks the host as not busy, disables
                   1278:      * the timeout counter, etc) will be called by us or by the
                   1279:      * scsi_hosts[host].queuecommand() function needs to also call
                   1280:      * the completion function for the high level driver.
                   1281:      */
                   1282:     
                   1283:     memcpy ((void *) SCpnt->data_cmnd , (const void *) cmnd, 12);
                   1284: #if 0
                   1285:     SCpnt->host = host;
                   1286:     SCpnt->channel = channel;
                   1287:     SCpnt->target = target;
                   1288:     SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
                   1289: #endif
                   1290:     SCpnt->bufflen = bufflen;
                   1291:     SCpnt->buffer = buffer;
                   1292:     SCpnt->flags=0;
                   1293:     SCpnt->retries=0;
                   1294:     SCpnt->allowed=retries;
                   1295:     SCpnt->done = done;
                   1296:     SCpnt->timeout_per_command = timeout;
                   1297: 
                   1298:     memcpy ((void *) SCpnt->cmnd , (const void *) cmnd, 12);
                   1299:     /* Zero the sense buffer.  Some host adapters automatically request
                   1300:      * sense on error.  0 is not a valid sense code.  
                   1301:      */
                   1302:     memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
                   1303:     SCpnt->request_buffer = buffer;
                   1304:     SCpnt->request_bufflen = bufflen;
                   1305:     SCpnt->old_use_sg = SCpnt->use_sg;
                   1306:     if (SCpnt->cmd_len == 0)
                   1307:        SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
                   1308:     SCpnt->old_cmd_len = SCpnt->cmd_len;
                   1309: 
                   1310:     /* Start the timer ticking.  */
                   1311: 
                   1312:     SCpnt->internal_timeout = 0;
                   1313:     SCpnt->abort_reason = 0;
                   1314:     internal_cmnd (SCpnt);
                   1315: 
                   1316: #ifdef DEBUG
                   1317:     printk ("Leaving scsi_do_cmd()\n");
                   1318: #endif
                   1319: }
                   1320: 
                   1321: static int check_sense (Scsi_Cmnd * SCpnt)
                   1322: {
                   1323:     /* If there is no sense information, request it.  If we have already
                   1324:      * requested it, there is no point in asking again - the firmware must
                   1325:      * be confused. 
                   1326:      */
                   1327:     if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
                   1328:        if(!(SCpnt->flags & ASKED_FOR_SENSE))
                   1329:            return SUGGEST_SENSE;
                   1330:        else
                   1331:            return SUGGEST_RETRY;
                   1332:     }
                   1333:     
                   1334:     SCpnt->flags &= ~ASKED_FOR_SENSE;
                   1335:     
                   1336: #ifdef DEBUG_INIT
                   1337:     printk("scsi%d, channel%d : ", SCpnt->host->host_no, SCpnt->channel);
                   1338:     print_sense("", SCpnt);
                   1339:     printk("\n");
                   1340: #endif
                   1341:     if (SCpnt->sense_buffer[2] & 0xe0)
                   1342:        return SUGGEST_ABORT;
                   1343:     
                   1344:     switch (SCpnt->sense_buffer[2] & 0xf)
                   1345:     {
                   1346:     case NO_SENSE:
                   1347:        return 0;
                   1348:     case RECOVERED_ERROR:
                   1349:        return SUGGEST_IS_OK;
                   1350:        
                   1351:     case ABORTED_COMMAND:
                   1352:        return SUGGEST_RETRY;
                   1353:     case NOT_READY:
                   1354:     case UNIT_ATTENTION:
                   1355:         /*
                   1356:          * If we are expecting a CC/UA because of a bus reset that we
                   1357:          * performed, treat this just as a retry.  Otherwise this is
                   1358:          * information that we should pass up to the upper-level driver
                   1359:          * so that we can deal with it there.
                   1360:          */
                   1361:         if( SCpnt->device->expecting_cc_ua )
                   1362:         {
                   1363:             SCpnt->device->expecting_cc_ua = 0;
                   1364:             return SUGGEST_RETRY;
                   1365:         }
                   1366:        return SUGGEST_ABORT;
                   1367:        
                   1368:     /* these three are not supported */
                   1369:     case COPY_ABORTED:
                   1370:     case VOLUME_OVERFLOW:
                   1371:     case MISCOMPARE:
                   1372:        
                   1373:     case MEDIUM_ERROR:
                   1374:        return SUGGEST_REMAP;
                   1375:     case BLANK_CHECK:
                   1376:     case DATA_PROTECT:
                   1377:     case HARDWARE_ERROR:
                   1378:     case ILLEGAL_REQUEST:
                   1379:     default:
                   1380:        return SUGGEST_ABORT;
                   1381:     }
                   1382: }
                   1383: 
                   1384: /* This function is the mid-level interrupt routine, which decides how
                   1385:  *  to handle error conditions.  Each invocation of this function must
                   1386:  *  do one and *only* one of the following:
                   1387:  *
                   1388:  *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
                   1389:  *      normal completion, and indicates that the handling for this
                   1390:  *      request is complete.
                   1391:  *  (2) Call internal_cmnd to requeue the command.  This will result in
                   1392:  *      scsi_done being called again when the retry is complete.
                   1393:  *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
                   1394:  *      more information about the error condition.  When the information
                   1395:  *      is available, scsi_done will be called again.
                   1396:  *  (4) Call reset().  This is sort of a last resort, and the idea is that
                   1397:  *      this may kick things loose and get the drive working again.  reset()
                   1398:  *      automatically calls scsi_request_sense, and thus scsi_done will be
                   1399:  *      called again once the reset is complete.
                   1400:  *
                   1401:  *      If none of the above actions are taken, the drive in question
                   1402:  *      will hang. If more than one of the above actions are taken by
                   1403:  *      scsi_done, then unpredictable behavior will result.
                   1404:  */
                   1405: static void scsi_done (Scsi_Cmnd * SCpnt)
                   1406: {
                   1407:     int status=0;
                   1408:     int exit=0;
                   1409:     int checked;
                   1410:     int oldto;
                   1411:     struct Scsi_Host * host = SCpnt->host;
                   1412:     int result = SCpnt->result;
                   1413:     oldto = update_timeout(SCpnt, 0);
                   1414:     
                   1415: #ifdef DEBUG_TIMEOUT
                   1416:     if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
                   1417:                      result, SCpnt->target, SCpnt->lun);
                   1418: #endif
                   1419:     
                   1420:     /* If we requested an abort, (and we got it) then fix up the return
                   1421:      *  status to say why 
                   1422:      */
                   1423:     if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
                   1424:        SCpnt->result = result = (result & 0xff00ffff) |
                   1425:            (SCpnt->abort_reason << 16);
                   1426: 
                   1427: 
                   1428: #define FINISHED 0
                   1429: #define MAYREDO  1
                   1430: #define REDO     3
                   1431: #define PENDING  4
                   1432: 
                   1433: #ifdef DEBUG
                   1434:     printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
                   1435: #endif
                   1436: 
                   1437:     if(SCpnt->flags & WAS_SENSE)
                   1438:     {
                   1439:        SCpnt->use_sg = SCpnt->old_use_sg;
                   1440:        SCpnt->cmd_len = SCpnt->old_cmd_len;
                   1441:     }
                   1442: 
                   1443:     switch (host_byte(result))
                   1444:     {
                   1445:     case DID_OK:
                   1446:        if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
                   1447:            /* Failed to obtain sense information */
                   1448:        {
                   1449:            SCpnt->flags &= ~WAS_SENSE;
                   1450:            SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
                   1451:            
                   1452:            if (!(SCpnt->flags & WAS_RESET))
                   1453:            {
                   1454:                printk("scsi%d : channel %d target %d lun %d request sense"
                   1455:                       " failed, performing reset.\n",
                   1456:                       SCpnt->host->host_no, SCpnt->channel, SCpnt->target, 
                   1457:                       SCpnt->lun);
                   1458:                scsi_reset(SCpnt, FALSE);
                   1459:                return;
                   1460:            }
                   1461:            else
                   1462:            {
                   1463:                exit = (DRIVER_HARD | SUGGEST_ABORT);
                   1464:                status = FINISHED;
                   1465:            }
                   1466:        }
                   1467:        else switch(msg_byte(result))
                   1468:        {
                   1469:        case COMMAND_COMPLETE:
                   1470:            switch (status_byte(result))
                   1471:            {
                   1472:            case GOOD:
                   1473:                if (SCpnt->flags & WAS_SENSE)
                   1474:                {
                   1475: #ifdef DEBUG
                   1476:                    printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
                   1477: #endif
                   1478:                    SCpnt->flags &= ~WAS_SENSE;
                   1479:                    SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
                   1480:                    
                   1481:                    switch (checked = check_sense(SCpnt))
                   1482:                    {
                   1483:                    case SUGGEST_SENSE:
                   1484:                    case 0:
                   1485: #ifdef DEBUG
                   1486:                        printk("NO SENSE.  status = REDO\n");
                   1487: #endif
                   1488:                        update_timeout(SCpnt, oldto);
                   1489:                        status = REDO;
                   1490:                        break;
                   1491:                    case SUGGEST_IS_OK:
                   1492:                        break;
                   1493:                    case SUGGEST_REMAP:
                   1494:                    case SUGGEST_RETRY:
                   1495: #ifdef DEBUG
                   1496:                        printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
                   1497: #endif
                   1498:                        status = MAYREDO;
                   1499:                        exit = DRIVER_SENSE | SUGGEST_RETRY;
                   1500:                        break;
                   1501:                    case SUGGEST_ABORT:
                   1502: #ifdef DEBUG
                   1503:                        printk("SENSE SUGGEST ABORT - status = FINISHED");
                   1504: #endif
                   1505:                        status = FINISHED;
                   1506:                        exit =  DRIVER_SENSE | SUGGEST_ABORT;
                   1507:                        break;
                   1508:                    default:
                   1509:                        printk ("Internal error %s %d \n", __FILE__,
                   1510:                                __LINE__);
                   1511:                    }
                   1512:                } /* end WAS_SENSE */
                   1513:                else
                   1514:                {
                   1515: #ifdef DEBUG
                   1516:                    printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
                   1517: #endif
                   1518:                    exit =  DRIVER_OK;
                   1519:                    status = FINISHED;
                   1520:                }
                   1521:                break;
                   1522:                
                   1523:            case CHECK_CONDITION:
                   1524:                switch (check_sense(SCpnt))
                   1525:                {
                   1526:                case 0:
                   1527:                    update_timeout(SCpnt, oldto);
                   1528:                    status = REDO;
                   1529:                    break;
                   1530:                case SUGGEST_REMAP:
                   1531:                case SUGGEST_RETRY:
                   1532:                    status = MAYREDO;
                   1533:                    exit = DRIVER_SENSE | SUGGEST_RETRY;
                   1534:                    break;
                   1535:                case SUGGEST_ABORT:
                   1536:                    status = FINISHED;
                   1537:                    exit =  DRIVER_SENSE | SUGGEST_ABORT;
                   1538:                    break;
                   1539:                case SUGGEST_SENSE:
                   1540:                    scsi_request_sense (SCpnt);
                   1541:                    status = PENDING;
                   1542:                    break;
                   1543:                }
                   1544:                break;
                   1545:                
                   1546:            case CONDITION_GOOD:
                   1547:            case INTERMEDIATE_GOOD:
                   1548:            case INTERMEDIATE_C_GOOD:
                   1549:                break;
                   1550:                
                   1551:            case BUSY:
                   1552:                update_timeout(SCpnt, oldto);
                   1553:                status = REDO;
                   1554:                break;
                   1555:                
                   1556:            case RESERVATION_CONFLICT:
                   1557:                printk("scsi%d, channel %d : RESERVATION CONFLICT performing"
                   1558:                       " reset.\n", SCpnt->host->host_no, SCpnt->channel);
                   1559:                scsi_reset(SCpnt, FALSE);
                   1560:                return;
                   1561: #if 0
                   1562:                exit = DRIVER_SOFT | SUGGEST_ABORT;
                   1563:                status = MAYREDO;
                   1564:                break;
                   1565: #endif
                   1566:            default:
                   1567:                printk ("Internal error %s %d \n"
                   1568:                        "status byte = %d \n", __FILE__,
                   1569:                        __LINE__, status_byte(result));
                   1570:                
                   1571:            }
                   1572:            break;
                   1573:        default:
                   1574:            panic("scsi: unsupported message byte %d received\n", 
                   1575:                  msg_byte(result));
                   1576:        }
                   1577:        break;
                   1578:     case DID_TIME_OUT:
                   1579: #ifdef DEBUG
                   1580:        printk("Host returned DID_TIME_OUT - ");
                   1581: #endif
                   1582:        
                   1583:        if (SCpnt->flags & WAS_TIMEDOUT)
                   1584:        {
                   1585: #ifdef DEBUG
                   1586:            printk("Aborting\n");
                   1587: #endif
                   1588:            /*
                   1589:              Allow TEST_UNIT_READY and INQUIRY commands to timeout early
                   1590:              without causing resets.  All other commands should be retried.
                   1591:            */
                   1592:            if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
                   1593:                SCpnt->cmnd[0] != INQUIRY)
                   1594:                    status = MAYREDO;
                   1595:            exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
                   1596:        }
                   1597:        else
                   1598:        {
                   1599: #ifdef DEBUG
                   1600:            printk ("Retrying.\n");
                   1601: #endif
                   1602:            SCpnt->flags  |= WAS_TIMEDOUT;
                   1603:            SCpnt->internal_timeout &= ~IN_ABORT;
                   1604:            status = REDO;
                   1605:        }
                   1606:        break;
                   1607:     case DID_BUS_BUSY:
                   1608:     case DID_PARITY:
                   1609:        status = REDO;
                   1610:        break;
                   1611:     case DID_NO_CONNECT:
                   1612: #ifdef DEBUG
                   1613:        printk("Couldn't connect.\n");
                   1614: #endif
                   1615:        exit  = (DRIVER_HARD | SUGGEST_ABORT);
                   1616:        break;
                   1617:     case DID_ERROR:
                   1618:        status = MAYREDO;
                   1619:        exit = (DRIVER_HARD | SUGGEST_ABORT);
                   1620:        break;
                   1621:     case DID_BAD_TARGET:
                   1622:     case DID_ABORT:
                   1623:        exit = (DRIVER_INVALID | SUGGEST_ABORT);
                   1624:        break;
                   1625:     case DID_RESET:
                   1626:        if (SCpnt->flags & IS_RESETTING)
                   1627:        {
                   1628:            SCpnt->flags &= ~IS_RESETTING;
                   1629:            status = REDO;
                   1630:            break;
                   1631:        }
                   1632:        
                   1633:        if(msg_byte(result) == GOOD &&
                   1634:           status_byte(result) == CHECK_CONDITION) {
                   1635:            switch (check_sense(SCpnt)) {
                   1636:            case 0:
                   1637:                update_timeout(SCpnt, oldto);
                   1638:                status = REDO;
                   1639:                break;
                   1640:            case SUGGEST_REMAP:
                   1641:            case SUGGEST_RETRY:
                   1642:                status = MAYREDO;
                   1643:                exit = DRIVER_SENSE | SUGGEST_RETRY;
                   1644:                break;
                   1645:            case SUGGEST_ABORT:
                   1646:                status = FINISHED;
                   1647:                exit =  DRIVER_SENSE | SUGGEST_ABORT;
                   1648:                break;
                   1649:            case SUGGEST_SENSE:
                   1650:                scsi_request_sense (SCpnt);
                   1651:                status = PENDING;
                   1652:                break;
                   1653:            }
                   1654:        } else {
                   1655:            status=REDO;
                   1656:            exit = SUGGEST_RETRY;
                   1657:        }
                   1658:        break;
                   1659:     default :
                   1660:        exit = (DRIVER_ERROR | SUGGEST_DIE);
                   1661:     }
                   1662:     
                   1663:     switch (status)
                   1664:     {
                   1665:     case FINISHED:
                   1666:     case PENDING:
                   1667:        break;
                   1668:     case MAYREDO:
                   1669: #ifdef DEBUG
                   1670:        printk("In MAYREDO, allowing %d retries, have %d\n",
                   1671:               SCpnt->allowed, SCpnt->retries);
                   1672: #endif
                   1673:        if ((++SCpnt->retries) < SCpnt->allowed)
                   1674:        {
                   1675:            if ((SCpnt->retries >= (SCpnt->allowed >> 1))
                   1676:                && !(jiffies < SCpnt->host->last_reset + MIN_RESET_PERIOD)
                   1677:                && !(SCpnt->flags & WAS_RESET))
                   1678:            {
                   1679:                printk("scsi%d channel %d : resetting for second half of retries.\n",
                   1680:                       SCpnt->host->host_no, SCpnt->channel);
                   1681:                scsi_reset(SCpnt, FALSE);
                   1682:                break;
                   1683:            }
                   1684:            
                   1685:        }
                   1686:        else
                   1687:        {
                   1688:            status = FINISHED;
                   1689:            break;
                   1690:        }
                   1691:        /* fall through to REDO */
                   1692:        
                   1693:     case REDO:
                   1694:        
                   1695:        if (SCpnt->flags & WAS_SENSE)
                   1696:            scsi_request_sense(SCpnt);
                   1697:        else
                   1698:        {
                   1699:            memcpy ((void *) SCpnt->cmnd,
                   1700:                    (void*) SCpnt->data_cmnd,
                   1701:                    sizeof(SCpnt->data_cmnd));
                   1702:            SCpnt->request_buffer = SCpnt->buffer;
                   1703:            SCpnt->request_bufflen = SCpnt->bufflen;
                   1704:            SCpnt->use_sg = SCpnt->old_use_sg;
                   1705:            SCpnt->cmd_len = SCpnt->old_cmd_len;
                   1706:            internal_cmnd (SCpnt);
                   1707:        }
                   1708:        break;
                   1709:     default:
                   1710:        INTERNAL_ERROR;
                   1711:     }
                   1712:     
                   1713:     if (status == FINISHED) {
                   1714: #ifdef DEBUG
                   1715:        printk("Calling done function - at address %p\n", SCpnt->done);
                   1716: #endif
                   1717:        host->host_busy--; /* Indicate that we are free */
                   1718:        
                   1719:        if (host->block && host->host_busy == 0) {
                   1720:            host_active = NULL;
                   1721:            
                   1722:            /* For block devices "wake_up" is done in end_scsi_request */
                   1723:            if (MAJOR(SCpnt->request.rq_dev) != SCSI_DISK_MAJOR &&
                   1724:                MAJOR(SCpnt->request.rq_dev) != SCSI_CDROM_MAJOR) {
                   1725:                struct Scsi_Host * next;
                   1726:                
                   1727:                for (next = host->block; next != host; next = next->block)
                   1728:                    wake_up(&next->host_wait);
                   1729:            }
                   1730:            
                   1731:        }
                   1732:        
                   1733:        wake_up(&host->host_wait);
                   1734:        SCpnt->result = result | ((exit & 0xff) << 24);
                   1735:        SCpnt->use_sg = SCpnt->old_use_sg;
                   1736:        SCpnt->cmd_len = SCpnt->old_cmd_len;
                   1737:        SCpnt->done (SCpnt);
                   1738:     }
                   1739:     
                   1740: #undef FINISHED
                   1741: #undef REDO
                   1742: #undef MAYREDO
                   1743: #undef PENDING
                   1744: }
                   1745: 
                   1746: /*
                   1747:  * The scsi_abort function interfaces with the abort() function of the host
                   1748:  * we are aborting, and causes the current command to not complete.  The
                   1749:  * caller should deal with any error messages or status returned on the
                   1750:  * next call.
                   1751:  * 
                   1752:  * This will not be called reentrantly for a given host.
                   1753:  */
                   1754: 
                   1755: /*
                   1756:  * Since we're nice guys and specified that abort() and reset()
                   1757:  * can be non-reentrant.  The internal_timeout flags are used for
                   1758:  * this.
                   1759:  */
                   1760: 
                   1761: 
                   1762: int scsi_abort (Scsi_Cmnd * SCpnt, int why, int pid)
                   1763: {
                   1764:     int oldto;
                   1765:     unsigned long flags;
                   1766:     struct Scsi_Host * host = SCpnt->host;
                   1767:     
                   1768:     while(1)
                   1769:     {
                   1770:        save_flags(flags);
                   1771:        cli();
                   1772:        
                   1773:        /*
                   1774:         * Protect against races here.  If the command is done, or we are
                   1775:         * on a different command forget it.
                   1776:         */
                   1777:        if (SCpnt->request.rq_status == RQ_INACTIVE || pid != SCpnt->pid) {
                   1778:            restore_flags(flags);
                   1779:            return 0;
                   1780:        }
                   1781: 
                   1782:        if (SCpnt->internal_timeout & IN_ABORT)
                   1783:        {
                   1784:            restore_flags(flags);
                   1785:            while (SCpnt->internal_timeout & IN_ABORT)
                   1786:                barrier();
                   1787:        }
                   1788:        else
                   1789:        {
                   1790:            SCpnt->internal_timeout |= IN_ABORT;
                   1791:            oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
                   1792:            
                   1793:            if ((SCpnt->flags & IS_RESETTING) && SCpnt->device->soft_reset) {
                   1794:                /* OK, this command must have died when we did the
                   1795:                 *  reset.  The device itself must have lied. 
                   1796:                 */
                   1797:                printk("Stale command on %d %d:%d appears to have died when"
                   1798:                       " the bus was reset\n", 
                   1799:                       SCpnt->channel, SCpnt->target, SCpnt->lun);
                   1800:            }
                   1801:            
                   1802:            restore_flags(flags);
                   1803:            if (!host->host_busy) {
                   1804:                SCpnt->internal_timeout &= ~IN_ABORT;
                   1805:                update_timeout(SCpnt, oldto);
                   1806:                return 0;
                   1807:            }
                   1808:            printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
                   1809:                   " channel %d, id %d, lun %d ",
                   1810:                   SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel, 
                   1811:                   (int) SCpnt->target, (int) SCpnt->lun);
                   1812:            print_command (SCpnt->cmnd);
                   1813:            if (SCpnt->request.rq_status == RQ_INACTIVE || pid != SCpnt->pid)
                   1814:                return 0;
                   1815:            SCpnt->abort_reason = why;
                   1816:            switch(host->hostt->abort(SCpnt)) {
                   1817:                /* We do not know how to abort.  Try waiting another
                   1818:                 * time increment and see if this helps. Set the
                   1819:                 * WAS_TIMEDOUT flag set so we do not try this twice
                   1820:                 */
                   1821:            case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
                   1822:                                   * this is too severe 
                   1823:                                   */
                   1824:            case SCSI_ABORT_SNOOZE:
                   1825:                if(why == DID_TIME_OUT) {
                   1826:                    save_flags(flags);
                   1827:                    cli();
                   1828:                    SCpnt->internal_timeout &= ~IN_ABORT;
                   1829:                    if(SCpnt->flags & WAS_TIMEDOUT) {
                   1830:                        restore_flags(flags);
                   1831:                        return 1; /* Indicate we cannot handle this.
                   1832:                                   * We drop down into the reset handler
                   1833:                                   * and try again 
                   1834:                                   */
                   1835:                    } else {
                   1836:                        SCpnt->flags |= WAS_TIMEDOUT;
                   1837:                        oldto = SCpnt->timeout_per_command;
                   1838:                        update_timeout(SCpnt, oldto);
                   1839:                    }
                   1840:                    restore_flags(flags);
                   1841:                }
                   1842:                return 0;
                   1843:            case SCSI_ABORT_PENDING:
                   1844:                if(why != DID_TIME_OUT) {
                   1845:                    save_flags(flags);
                   1846:                    cli();
                   1847:                    update_timeout(SCpnt, oldto);
                   1848:                    restore_flags(flags);
                   1849:                }
                   1850:                return 0;
                   1851:            case SCSI_ABORT_SUCCESS:
                   1852:                /* We should have already aborted this one.  No
                   1853:                 * need to adjust timeout 
                   1854:                 */
                   1855:                  SCpnt->internal_timeout &= ~IN_ABORT;
                   1856:                  return 0;
                   1857:            case SCSI_ABORT_NOT_RUNNING:
                   1858:                SCpnt->internal_timeout &= ~IN_ABORT;
                   1859:                update_timeout(SCpnt, 0);
                   1860:                return 0;
                   1861:            case SCSI_ABORT_ERROR:
                   1862:            default:
                   1863:                SCpnt->internal_timeout &= ~IN_ABORT;
                   1864:                return 1;
                   1865:            }
                   1866:        }
                   1867:     }
                   1868: }
                   1869: 
                   1870: 
                   1871: /* Mark a single SCSI Device as having been reset. */
                   1872: 
                   1873: static inline void scsi_mark_device_reset(Scsi_Device *Device)
                   1874: {
                   1875:   Device->was_reset = 1;
                   1876:   Device->expecting_cc_ua = 1;
                   1877: }
                   1878: 
                   1879: 
                   1880: /* Mark all SCSI Devices on a specific Host as having been reset. */
                   1881: 
                   1882: void scsi_mark_host_bus_reset(struct Scsi_Host *Host)
                   1883: {
                   1884:   Scsi_Cmnd *SCpnt;
                   1885:   for(SCpnt = Host->host_queue; SCpnt; SCpnt = SCpnt->next)
                   1886:     scsi_mark_device_reset(SCpnt->device);
                   1887: }
                   1888: 
                   1889: 
                   1890: int scsi_reset (Scsi_Cmnd * SCpnt, int bus_reset_flag)
                   1891: {
                   1892:     int temp, oldto;
                   1893:     unsigned long flags;
                   1894:     Scsi_Cmnd * SCpnt1;
                   1895:     struct Scsi_Host * host = SCpnt->host;
                   1896: 
                   1897:     printk("SCSI bus is being reset for host %d.\n",
                   1898:           host->host_no);
                   1899:  
                   1900:     /*
                   1901:      * First of all, we need to make a recommendation to the low-level
                   1902:      * driver as to whether a BUS_DEVICE_RESET should be performed,
                   1903:      * or whether we should do a full BUS_RESET.  There is no simple
                   1904:      * algorithm here - we basically use a series of heuristics
                   1905:      * to determine what we should do.
                   1906:      */
                   1907:     SCpnt->host->suggest_bus_reset = FALSE;
                   1908:     
                   1909:     /*
                   1910:      * First see if all of the active devices on the bus have
                   1911:      * been jammed up so that we are attempting resets.  If so,
                   1912:      * then suggest a bus reset.  Forcing a bus reset could
                   1913:      * result in some race conditions, but no more than
                   1914:      * you would usually get with timeouts.  We will cross
                   1915:      * that bridge when we come to it.
                   1916:      */
                   1917:     SCpnt1 = host->host_queue;
                   1918:     while(SCpnt1) {
                   1919:        if( SCpnt1->request.rq_status != RQ_INACTIVE
                   1920:            && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0 )
                   1921:                break;
                   1922:         SCpnt1 = SCpnt1->next;
                   1923:        }
                   1924:     if( SCpnt1 == NULL ) {
                   1925:         SCpnt->host->suggest_bus_reset = TRUE;
                   1926:     }
                   1927:     
                   1928:     
                   1929:     /*
                   1930:      * If the code that called us is suggesting a hard reset, then
                   1931:      * definitely request it.  This usually occurs because a
                   1932:      * BUS_DEVICE_RESET times out.
                   1933:      */
                   1934:     if( bus_reset_flag ) {
                   1935:         SCpnt->host->suggest_bus_reset = TRUE;
                   1936:     }
                   1937:     
                   1938:     while (1) {
                   1939:        save_flags(flags);
                   1940:        cli();
                   1941:        if (SCpnt->internal_timeout & IN_RESET)
                   1942:        {
                   1943:            restore_flags(flags);
                   1944:            while (SCpnt->internal_timeout & IN_RESET)
                   1945:                barrier();
                   1946:        }
                   1947:        else
                   1948:        {
                   1949:            SCpnt->internal_timeout |= IN_RESET;
                   1950:            oldto = update_timeout(SCpnt, RESET_TIMEOUT);
                   1951:            
                   1952:            if (host->host_busy)
                   1953:            {
                   1954:                restore_flags(flags);
                   1955:                SCpnt1 = host->host_queue;
                   1956:                while(SCpnt1) {
                   1957:                    if (SCpnt1->request.rq_status != RQ_INACTIVE) {
                   1958: #if 0
                   1959:                        if (!(SCpnt1->flags & IS_RESETTING) &&
                   1960:                            !(SCpnt1->internal_timeout & IN_ABORT))
                   1961:                            scsi_abort(SCpnt1, DID_RESET, SCpnt->pid);
                   1962: #endif
                   1963:                        SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
                   1964:                    }
                   1965:                    SCpnt1 = SCpnt1->next;
                   1966:                }
                   1967:                
                   1968:                host->last_reset = jiffies;
                   1969:                temp = host->hostt->reset(SCpnt);
                   1970:                host->last_reset = jiffies;
                   1971:            }
                   1972:            else
                   1973:            {
                   1974:                if (!host->block) host->host_busy++;
                   1975:                restore_flags(flags);
                   1976:                host->last_reset = jiffies;
                   1977:                SCpnt->flags |= (WAS_RESET | IS_RESETTING);
                   1978:                temp = host->hostt->reset(SCpnt);
                   1979:                host->last_reset = jiffies;
                   1980:                if (!host->block) host->host_busy--;
                   1981:            }
                   1982:            
                   1983: #ifdef DEBUG
                   1984:            printk("scsi reset function returned %d\n", temp);
                   1985: #endif
                   1986:             
                   1987:             /*
                   1988:              * Now figure out what we need to do, based upon
                   1989:              * what the low level driver said that it did.
                   1990:             * If the result is SCSI_RESET_SUCCESS, SCSI_RESET_PENDING,
                   1991:             * or SCSI_RESET_WAKEUP, then the low level driver did a
                   1992:             * bus device reset or bus reset, so we should go through
                   1993:             * and mark one or all of the devices on that bus
                   1994:             * as having been reset.
                   1995:              */
                   1996:             switch(temp & SCSI_RESET_ACTION) {
                   1997:            case SCSI_RESET_SUCCESS:
                   1998:                if (temp & SCSI_RESET_BUS_RESET)
                   1999:                  scsi_mark_host_bus_reset(host);
                   2000:                else scsi_mark_device_reset(SCpnt->device);
                   2001:                save_flags(flags);
                   2002:                cli();
                   2003:                SCpnt->internal_timeout &= ~IN_RESET;
                   2004:                update_timeout(SCpnt, oldto);
                   2005:                restore_flags(flags);
                   2006:                return 0;
                   2007:            case SCSI_RESET_PENDING:
                   2008:                if (temp & SCSI_RESET_BUS_RESET)
                   2009:                  scsi_mark_host_bus_reset(host);
                   2010:                else scsi_mark_device_reset(SCpnt->device);
                   2011:                return 0;
                   2012:            case SCSI_RESET_PUNT:
                   2013:                 SCpnt->internal_timeout &= ~IN_RESET;
                   2014:                 scsi_request_sense (SCpnt);
                   2015:                 return 0;
                   2016:            case SCSI_RESET_WAKEUP:
                   2017:                if (temp & SCSI_RESET_BUS_RESET)
                   2018:                  scsi_mark_host_bus_reset(host);
                   2019:                else scsi_mark_device_reset(SCpnt->device);
                   2020:                SCpnt->internal_timeout &= ~IN_RESET;
                   2021:                scsi_request_sense (SCpnt);
                   2022:                 /*
                   2023:                  * Since a bus reset was performed, we
                   2024:                  * need to wake up each and every command
                   2025:                  * that was active on the bus.
                   2026:                  */
                   2027:                 if( temp & SCSI_RESET_BUS_RESET )
                   2028:                 {
                   2029:                     SCpnt1 = host->host_queue;
                   2030:                     while(SCpnt1) {
                   2031:                         if( SCpnt->request.rq_status != RQ_INACTIVE
                   2032:                            && SCpnt1 != SCpnt)
                   2033:                             scsi_request_sense (SCpnt);
                   2034:                         SCpnt1 = SCpnt1->next;
                   2035:                     }
                   2036:                 }
                   2037:                return 0;
                   2038:            case SCSI_RESET_SNOOZE:
                   2039:                /* In this case, we set the timeout field to 0
                   2040:                 * so that this command does not time out any more,
                   2041:                 * and we return 1 so that we get a message on the
                   2042:                 * screen. 
                   2043:                 */
                   2044:                save_flags(flags);
                   2045:                cli();
                   2046:                SCpnt->internal_timeout &= ~IN_RESET;
                   2047:                update_timeout(SCpnt, 0);
                   2048:                restore_flags(flags);
                   2049:                /* If you snooze, you lose... */
                   2050:            case SCSI_RESET_ERROR:
                   2051:            default:
                   2052:                return 1;
                   2053:            }
                   2054:            
                   2055:            return temp;
                   2056:        }
                   2057:     }
                   2058: }
                   2059: 
                   2060: 
                   2061: static void scsi_main_timeout(void)
                   2062: {
                   2063:     /*
                   2064:      * We must not enter update_timeout with a timeout condition still pending.
                   2065:      */
                   2066:     
                   2067:     int timed_out, pid;
                   2068:     unsigned long flags;
                   2069:     struct Scsi_Host * host;
                   2070:     Scsi_Cmnd * SCpnt = NULL;
                   2071:     
                   2072:     do {
                   2073:        save_flags(flags);
                   2074:        cli();
                   2075:        
                   2076:        update_timeout(NULL, 0);
                   2077:        /*
                   2078:         * Find all timers such that they have 0 or negative (shouldn't happen)
                   2079:         * time remaining on them.
                   2080:         */
                   2081:        
                   2082:        timed_out = 0;
                   2083:        for(host = scsi_hostlist; host; host = host->next) {
                   2084:            for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
                   2085:                if (SCpnt->timeout == -1)
                   2086:                {
                   2087:                    SCpnt->timeout = 0;
                   2088:                    pid = SCpnt->pid;
                   2089:                    restore_flags(flags);
                   2090:                    scsi_times_out(SCpnt, pid);
                   2091:                    ++timed_out;
                   2092:                    save_flags(flags);
                   2093:                    cli();
                   2094:                }
                   2095:        }
                   2096:     } while (timed_out);
                   2097:     restore_flags(flags);
                   2098: }
                   2099: 
                   2100: /*
                   2101:  * The strategy is to cause the timer code to call scsi_times_out()
                   2102:  * when the soonest timeout is pending.
                   2103:  * The arguments are used when we are queueing a new command, because
                   2104:  * we do not want to subtract the time used from this time, but when we
                   2105:  * set the timer, we want to take this value into account.
                   2106:  */
                   2107: 
                   2108: static int update_timeout(Scsi_Cmnd * SCset, int timeout)
                   2109: {
                   2110:     unsigned int least, used;
                   2111:     unsigned int oldto;
                   2112:     unsigned long flags;
                   2113:     struct Scsi_Host * host;
                   2114:     Scsi_Cmnd * SCpnt = NULL;
                   2115: 
                   2116:     save_flags(flags);
                   2117:     cli();
                   2118: 
                   2119:     /*
                   2120:      * Figure out how much time has passed since the last time the timeouts
                   2121:      * were updated
                   2122:      */
                   2123:     used = (time_start) ? (jiffies - time_start) : 0;
                   2124: 
                   2125:     /*
                   2126:      * Find out what is due to timeout soonest, and adjust all timeouts for
                   2127:      * the amount of time that has passed since the last time we called
                   2128:      * update_timeout.
                   2129:      */
                   2130: 
                   2131:     oldto = 0;
                   2132:     
                   2133:     if(SCset){
                   2134:        oldto = SCset->timeout - used;
                   2135:        SCset->timeout = timeout + used;
                   2136:     }
                   2137: 
                   2138:     least = 0xffffffff;
                   2139:     
                   2140:     for(host = scsi_hostlist; host; host = host->next)
                   2141:        for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
                   2142:            if (SCpnt->timeout > 0) {
                   2143:                SCpnt->timeout -= used;
                   2144:                if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
                   2145:                if(SCpnt->timeout > 0 && SCpnt->timeout < least)
                   2146:                    least = SCpnt->timeout;
                   2147:            }
                   2148:     
                   2149:     /*
                   2150:      * If something is due to timeout again, then we will set the next timeout
                   2151:      * interrupt to occur.  Otherwise, timeouts are disabled.
                   2152:      */
                   2153:     
                   2154:     if (least != 0xffffffff)
                   2155:     {
                   2156:        time_start = jiffies;
                   2157:        timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
                   2158:        timer_active |= 1 << SCSI_TIMER;
                   2159:     }
                   2160:     else
                   2161:     {
                   2162:        timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
                   2163:        timer_active &= ~(1 << SCSI_TIMER);
                   2164:     }
                   2165:     restore_flags(flags);
                   2166:     return oldto;
                   2167: }
                   2168: 
                   2169: #ifdef CONFIG_MODULES
                   2170: static int scsi_register_host(Scsi_Host_Template *);
                   2171: static void scsi_unregister_host(Scsi_Host_Template *);
                   2172: #endif
                   2173: 
                   2174: void *scsi_malloc(unsigned int len)
                   2175: {
                   2176:     unsigned int nbits, mask;
                   2177:     unsigned long flags;
                   2178:     int i, j;
                   2179:     if(len % SECTOR_SIZE != 0 || len > PAGE_SIZE)
                   2180:        return NULL;
                   2181:     
                   2182:     save_flags(flags);
                   2183:     cli();
                   2184:     nbits = len >> 9;
                   2185:     mask = (1 << nbits) - 1;
                   2186:     
                   2187:     for(i=0;i < dma_sectors / SECTORS_PER_PAGE; i++)
                   2188:        for(j=0; j<=SECTORS_PER_PAGE - nbits; j++){
                   2189:            if ((dma_malloc_freelist[i] & (mask << j)) == 0){
                   2190:                dma_malloc_freelist[i] |= (mask << j);
                   2191:                restore_flags(flags);
                   2192:                dma_free_sectors -= nbits;
                   2193: #ifdef DEBUG
                   2194:                printk("SMalloc: %d %p\n",len, dma_malloc_pages[i] + (j << 9));
                   2195: #endif
                   2196:                return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
                   2197:            }
                   2198:        }
                   2199:     restore_flags(flags);
                   2200:     return NULL;  /* Nope.  No more */
                   2201: }
                   2202: 
                   2203: int scsi_free(void *obj, unsigned int len)
                   2204: {
                   2205:     unsigned int page, sector, nbits, mask;
                   2206:     unsigned long flags;
                   2207:     
                   2208: #ifdef DEBUG
                   2209:     printk("scsi_free %p %d\n",obj, len);
                   2210: #endif
                   2211:     
                   2212:     for (page = 0; page < dma_sectors / SECTORS_PER_PAGE; page++) {
                   2213:         unsigned long page_addr = (unsigned long) dma_malloc_pages[page];
                   2214:         if ((unsigned long) obj >= page_addr &&
                   2215:            (unsigned long) obj <  page_addr + PAGE_SIZE)
                   2216:        {
                   2217:            sector = (((unsigned long) obj) - page_addr) >> 9;
                   2218: 
                   2219:             nbits = len >> 9;
                   2220:             mask = (1 << nbits) - 1;
                   2221: 
                   2222:             if ((mask << sector) >= (1 << SECTORS_PER_PAGE))
                   2223:                 panic ("scsi_free:Bad memory alignment");
                   2224: 
                   2225:             save_flags(flags);
                   2226:             cli();
                   2227:             if((dma_malloc_freelist[page] & (mask << sector)) != (mask<<sector))
                   2228:                 panic("scsi_free:Trying to free unused memory");
                   2229: 
                   2230:             dma_free_sectors += nbits;
                   2231:             dma_malloc_freelist[page] &= ~(mask << sector);
                   2232:             restore_flags(flags);
                   2233:             return 0;
                   2234:        }
                   2235:     }
                   2236:     panic("scsi_free:Bad offset");
                   2237: }
                   2238: 
                   2239: 
                   2240: int scsi_loadable_module_flag; /* Set after we scan builtin drivers */
                   2241: 
                   2242: void * scsi_init_malloc(unsigned int size, int priority)
                   2243: {
                   2244:     void * retval;
                   2245:     
                   2246:     /*
                   2247:      * For buffers used by the DMA pool, we assume page aligned 
                   2248:      * structures.
                   2249:      */
                   2250:     if ((size % PAGE_SIZE) == 0) {
                   2251:        int order, a_size;
                   2252:        for (order = 0, a_size = PAGE_SIZE;
                   2253:              a_size < size; order++, a_size <<= 1)
                   2254:             ;
                   2255:         retval = (void *) __get_dma_pages(priority & GFP_LEVEL_MASK,
                   2256:                                                    order);
                   2257:     } else
                   2258:         retval = kmalloc(size, priority);
                   2259: 
                   2260:     if (retval)
                   2261:        memset(retval, 0, size);
                   2262:     return retval;
                   2263: }
                   2264: 
                   2265: 
                   2266: void scsi_init_free(char * ptr, unsigned int size)
                   2267: { 
                   2268:     /*
                   2269:      * We need this special code here because the DMA pool assumes
                   2270:      * page aligned data.  Besides, it is wasteful to allocate
                   2271:      * page sized chunks with kmalloc.
                   2272:      */
                   2273:     if ((size % PAGE_SIZE) == 0) {
                   2274:        int order, a_size;
                   2275: 
                   2276:        for (order = 0, a_size = PAGE_SIZE;
                   2277:             a_size < size; order++, a_size <<= 1)
                   2278:            ;
                   2279:        free_pages((unsigned long)ptr, order);
                   2280:     } else
                   2281:        kfree(ptr);
                   2282: }
                   2283: 
                   2284: void scsi_build_commandblocks(Scsi_Device * SDpnt)
                   2285: {
                   2286:     int j;
                   2287:     Scsi_Cmnd * SCpnt;
                   2288:     struct Scsi_Host * host = NULL;
                   2289:     
                   2290:     for(j=0;j<SDpnt->host->cmd_per_lun;j++){
                   2291:        SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
                   2292:        SCpnt->host = SDpnt->host;
                   2293:        SCpnt->device = SDpnt;
                   2294:        SCpnt->target = SDpnt->id;
                   2295:        SCpnt->lun = SDpnt->lun;
                   2296:        SCpnt->channel = SDpnt->channel;
                   2297:        SCpnt->request.rq_status = RQ_INACTIVE;
                   2298:        SCpnt->use_sg = 0;
                   2299:        SCpnt->old_use_sg = 0;
                   2300:        SCpnt->old_cmd_len = 0;
                   2301:        SCpnt->timeout = 0;
                   2302:        SCpnt->underflow = 0;
                   2303:        SCpnt->transfersize = 0;
                   2304:        SCpnt->host_scribble = NULL;
                   2305:        host = SDpnt->host;
                   2306:        if(host->host_queue)
                   2307:            host->host_queue->prev = SCpnt;
                   2308:        SCpnt->next = host->host_queue;
                   2309:        SCpnt->prev = NULL;
                   2310:        host->host_queue = SCpnt;
                   2311:     }
                   2312:     SDpnt->has_cmdblocks = 1;
                   2313: }
                   2314: 
                   2315: /*
                   2316:  * scsi_dev_init() is our initialization routine, which in turn calls host
                   2317:  * initialization, bus scanning, and sd/st initialization routines. 
                   2318:  */
                   2319: 
                   2320: int scsi_dev_init(void)
                   2321: {
                   2322:     Scsi_Device * SDpnt;
                   2323:     struct Scsi_Host * shpnt;
                   2324:     struct Scsi_Device_Template * sdtpnt;
                   2325: #ifdef FOO_ON_YOU
                   2326:     return;
                   2327: #endif
                   2328: 
                   2329:     /* Yes we're here... */
                   2330:     dispatch_scsi_info_ptr = dispatch_scsi_info;
                   2331: 
                   2332:     /* Init a few things so we can "malloc" memory. */
                   2333:     scsi_loadable_module_flag = 0;
                   2334:     
                   2335:     timer_table[SCSI_TIMER].fn = scsi_main_timeout;
                   2336:     timer_table[SCSI_TIMER].expires = 0;
                   2337: 
                   2338: #ifdef CONFIG_MODULES
                   2339:     register_symtab(&scsi_symbol_table);
                   2340: #endif    
                   2341: 
                   2342:     /* Register the /proc/scsi/scsi entry */
                   2343: #if CONFIG_PROC_FS 
                   2344:     proc_scsi_register(0, &proc_scsi_scsi);    
                   2345: #endif
                   2346: 
                   2347:     /* initialize all hosts */
                   2348:     scsi_init();
                   2349: 
                   2350:     scsi_devices = (Scsi_Device *) NULL;
                   2351: 
                   2352:     for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
                   2353:        scan_scsis(shpnt,0,0,0,0);           /* scan for scsi devices */
                   2354: 
                   2355: #if 0
                   2356:     printk("scsi : detected ");
                   2357:     for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                   2358:        if (sdtpnt->dev_noticed && sdtpnt->name)
                   2359:            printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
                   2360:                   (sdtpnt->dev_noticed != 1) ? "s" : "");
                   2361:     printk("total.\n");
                   2362: #endif
                   2363:     
                   2364:     for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                   2365:        if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
                   2366: 
                   2367:     for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
                   2368:        SDpnt->scsi_request_fn = NULL;
                   2369:        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                   2370:            if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
                   2371:        if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
                   2372:     }
                   2373:     
                   2374: 
                   2375:     /*
                   2376:      * This should build the DMA pool.
                   2377:      */
                   2378:     resize_dma_pool();
                   2379: 
                   2380:     /*
                   2381:      * OK, now we finish the initialization by doing spin-up, read
                   2382:      * capacity, etc, etc 
                   2383:      */
                   2384:     for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                   2385:        if(sdtpnt->finish && sdtpnt->nr_dev)
                   2386:            (*sdtpnt->finish)();
                   2387: 
                   2388:     scsi_loadable_module_flag = 1;
                   2389: 
                   2390:     return 0;
                   2391: }
                   2392: 
                   2393: static void print_inquiry(unsigned char *data)
                   2394: {
                   2395:     int i;
                   2396:     
                   2397:     printk("  Vendor: ");
                   2398:     for (i = 8; i < 16; i++)
                   2399:     {
                   2400:        if (data[i] >= 0x20 && i < data[4] + 5)
                   2401:            printk("%c", data[i]);
                   2402:        else
                   2403:            printk(" ");
                   2404:     }
                   2405:     
                   2406:     printk("  Model: ");
                   2407:     for (i = 16; i < 32; i++)
                   2408:     {
                   2409:        if (data[i] >= 0x20 && i < data[4] + 5)
                   2410:            printk("%c", data[i]);
                   2411:        else
                   2412:            printk(" ");
                   2413:     }
                   2414:     
                   2415:     printk("  Rev: ");
                   2416:     for (i = 32; i < 36; i++)
                   2417:     {
                   2418:        if (data[i] >= 0x20 && i < data[4] + 5)
                   2419:            printk("%c", data[i]);
                   2420:        else
                   2421:            printk(" ");
                   2422:     }
                   2423:     
                   2424:     printk("\n");
                   2425:     
                   2426:     i = data[0] & 0x1f;
                   2427:     
                   2428:     printk("  Type:   %s ",
                   2429:           i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
                   2430:     printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
                   2431:     if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
                   2432:        printk(" CCS\n");
                   2433:     else
                   2434:        printk("\n");
                   2435: }
                   2436: 
                   2437: 
                   2438: #ifdef CONFIG_PROC_FS
                   2439: int scsi_proc_info(char *buffer, char **start, off_t offset, int length, 
                   2440:                    int hostno, int inout)
                   2441: {
                   2442:     Scsi_Device *scd;
                   2443:     struct Scsi_Host *HBA_ptr;
                   2444:     int  parameter[4];
                   2445:     char *p;
                   2446:     int          i,size, len = 0;
                   2447:     off_t begin = 0;
                   2448:     off_t pos = 0;
                   2449: 
                   2450:     scd = scsi_devices;
                   2451:     HBA_ptr = scsi_hostlist;
                   2452: 
                   2453:     if(inout == 0) { 
                   2454:        size = sprintf(buffer+len,"Attached devices: %s\n", (scd)?"":"none");
                   2455:        len += size; 
                   2456:        pos = begin + len;
                   2457:        while (HBA_ptr) {
                   2458: #if 0
                   2459:            size += sprintf(buffer+len,"scsi%2d: %s\n", (int) HBA_ptr->host_no, 
                   2460:                            HBA_ptr->hostt->procname);
                   2461:            len += size; 
                   2462:            pos = begin + len;
                   2463: #endif
                   2464:            scd = scsi_devices;
                   2465:            while (scd) {
                   2466:                if (scd->host == HBA_ptr) {
                   2467:                    proc_print_scsidevice(scd, buffer, &size, len);
                   2468:                    len += size; 
                   2469:                    pos = begin + len;
                   2470:                    
                   2471:                    if (pos < offset) {
                   2472:                        len = 0;
                   2473:                        begin = pos;
                   2474:                    }
                   2475:                    if (pos > offset + length)
                   2476:                        goto stop_output;
                   2477:                }
                   2478:                scd = scd->next;
                   2479:            }
                   2480:            HBA_ptr = HBA_ptr->next;
                   2481:        }
                   2482:        
                   2483:     stop_output:
                   2484:        *start=buffer+(offset-begin);   /* Start of wanted data */
                   2485:        len-=(offset-begin);            /* Start slop */
                   2486:        if(len>length)
                   2487:            len = length;               /* Ending slop */
                   2488:        return (len);     
                   2489:     }
                   2490: 
                   2491:     /*
                   2492:      * Usage: echo "scsi singledevice 0 1 2 3" >/proc/scsi/scsi
                   2493:      * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
                   2494:      * Consider this feature BETA.
                   2495:      *     CAUTION: This is not for hotplugging your peripherals. As
                   2496:      *     SCSI was not designed for this you could damage your
                   2497:      *     hardware !  
                   2498:      * However perhaps it is legal to switch on an
                   2499:      * already connected device. It is perhaps not 
                   2500:      * guaranteed this device doesn't corrupt an ongoing data transfer.
                   2501:      */
                   2502:     if(!buffer || length < 25 || strncmp("scsi", buffer, 4))
                   2503:        return(-EINVAL);
                   2504: 
                   2505:     if(!strncmp("singledevice", buffer + 5, 12)) {
                   2506:        p = buffer + 17;
                   2507: 
                   2508:        for(i=0; i<4; i++)      {
                   2509:            p++;
                   2510:            parameter[i] = simple_strtoul(p, &p, 0);
                   2511:        }
                   2512:        printk("scsi singledevice %d %d %d %d\n", parameter[0], parameter[1],
                   2513:                        parameter[2], parameter[3]);
                   2514: 
                   2515:        while(scd && (scd->host->host_no != parameter[0] 
                   2516:              || scd->channel != parameter[1] 
                   2517:              || scd->id != parameter[2] 
                   2518:              || scd->lun != parameter[3])) {
                   2519:            scd = scd->next;
                   2520:        }
                   2521:        if(scd)
                   2522:            return(-ENOSYS);  /* We do not yet support unplugging */
                   2523:        while(HBA_ptr && HBA_ptr->host_no != parameter[0])
                   2524:            HBA_ptr = HBA_ptr->next;
                   2525: 
                   2526:        if(!HBA_ptr)
                   2527:            return(-ENXIO);
                   2528: 
                   2529:        scan_scsis (HBA_ptr, 1, parameter[1], parameter[2], parameter[3]);
                   2530:        return(length);
                   2531:     }
                   2532:     return(-EINVAL);
                   2533: }
                   2534: #endif
                   2535: 
                   2536: /*
                   2537:  * Go through the device list and recompute the most appropriate size
                   2538:  * for the dma pool.  Then grab more memory (as required).
                   2539:  */
                   2540: static void resize_dma_pool(void)
                   2541: {
                   2542:     int i;
                   2543:     unsigned long size;
                   2544:     struct Scsi_Host * shpnt;
                   2545:     struct Scsi_Host * host = NULL;
                   2546:     Scsi_Device * SDpnt;
                   2547:     unsigned long flags;
                   2548:     FreeSectorBitmap * new_dma_malloc_freelist = NULL;
                   2549:     unsigned int new_dma_sectors = 0;
                   2550:     unsigned int new_need_isa_buffer = 0;
                   2551:     unsigned char ** new_dma_malloc_pages = NULL;
                   2552: 
                   2553:     if( !scsi_devices )
                   2554:     {
                   2555:        /*
                   2556:         * Free up the DMA pool.
                   2557:         */
                   2558:        if( dma_free_sectors != dma_sectors )
                   2559:            panic("SCSI DMA pool memory leak %d %d\n",dma_free_sectors,dma_sectors);
                   2560: 
                   2561:        for(i=0; i < dma_sectors / SECTORS_PER_PAGE; i++)
                   2562:            scsi_init_free(dma_malloc_pages[i], PAGE_SIZE);
                   2563:        if (dma_malloc_pages)
                   2564:            scsi_init_free((char *) dma_malloc_pages,
                   2565:                            (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages));
                   2566:        dma_malloc_pages = NULL;
                   2567:        if (dma_malloc_freelist)
                   2568:            scsi_init_free((char *) dma_malloc_freelist,
                   2569:                            (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_freelist));
                   2570:        dma_malloc_freelist = NULL;
                   2571:        dma_sectors = 0;
                   2572:        dma_free_sectors = 0;
                   2573:        return;
                   2574:     }
                   2575:     /* Next, check to see if we need to extend the DMA buffer pool */
                   2576:        
                   2577:     new_dma_sectors = 2*SECTORS_PER_PAGE;              /* Base value we use */
                   2578: 
                   2579:     if (high_memory-1 > ISA_DMA_THRESHOLD)
                   2580:        scsi_need_isa_bounce_buffers = 1;
                   2581:     else
                   2582:        scsi_need_isa_bounce_buffers = 0;
                   2583:     
                   2584:     if (scsi_devicelist)
                   2585:        for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
                   2586:            new_dma_sectors += SECTORS_PER_PAGE;        /* Increment for each host */
                   2587:     
                   2588:     for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
                   2589:        host = SDpnt->host;
                   2590:        
                   2591:        if(SDpnt->type != TYPE_TAPE)
                   2592:            new_dma_sectors += ((host->sg_tablesize *
                   2593:                                 sizeof(struct scatterlist) + 511) >> 9) *
                   2594:                                     host->cmd_per_lun;
                   2595:        
                   2596:        if(host->unchecked_isa_dma &&
                   2597:           scsi_need_isa_bounce_buffers &&
                   2598:           SDpnt->type != TYPE_TAPE) {
                   2599:            new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
                   2600:                host->cmd_per_lun;
                   2601:            new_need_isa_buffer++;
                   2602:        }
                   2603:     }
                   2604:     
                   2605:     /* limit DMA memory to 32MB: */
                   2606:     new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
                   2607:     
                   2608:     /*
                   2609:      * We never shrink the buffers - this leads to
                   2610:      * race conditions that I would rather not even think
                   2611:      * about right now.
                   2612:      */
                   2613:     if( new_dma_sectors < dma_sectors )
                   2614:        new_dma_sectors = dma_sectors;
                   2615:     
                   2616:     if (new_dma_sectors)
                   2617:     {
                   2618:         size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
                   2619:        new_dma_malloc_freelist = (FreeSectorBitmap *) scsi_init_malloc(size, GFP_ATOMIC);
                   2620:        memset(new_dma_malloc_freelist, 0, size);
                   2621: 
                   2622:         size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(*new_dma_malloc_pages);
                   2623:        new_dma_malloc_pages = (unsigned char **) scsi_init_malloc(size, GFP_ATOMIC);
                   2624:        memset(new_dma_malloc_pages, 0, size);
                   2625:     }
                   2626:     
                   2627:     /*
                   2628:      * If we need more buffers, expand the list.
                   2629:      */
                   2630:     if( new_dma_sectors > dma_sectors ) { 
                   2631:        for(i=dma_sectors / SECTORS_PER_PAGE; i< new_dma_sectors / SECTORS_PER_PAGE; i++)
                   2632:            new_dma_malloc_pages[i] = (unsigned char *)
                   2633:                scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
                   2634:     }
                   2635:     
                   2636:     /* When we dick with the actual DMA list, we need to 
                   2637:      * protect things 
                   2638:      */
                   2639:     save_flags(flags);
                   2640:     cli();
                   2641:     if (dma_malloc_freelist)
                   2642:     {
                   2643:         size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
                   2644:        memcpy(new_dma_malloc_freelist, dma_malloc_freelist, size);
                   2645:        scsi_init_free((char *) dma_malloc_freelist, size);
                   2646:     }
                   2647:     dma_malloc_freelist = new_dma_malloc_freelist;
                   2648:     
                   2649:     if (dma_malloc_pages)
                   2650:     {
                   2651:         size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages);
                   2652:        memcpy(new_dma_malloc_pages, dma_malloc_pages, size);
                   2653:        scsi_init_free((char *) dma_malloc_pages, size);
                   2654:     }
                   2655:     
                   2656:     dma_free_sectors += new_dma_sectors - dma_sectors;
                   2657:     dma_malloc_pages = new_dma_malloc_pages;
                   2658:     dma_sectors = new_dma_sectors;
                   2659:     need_isa_buffer = new_need_isa_buffer;
                   2660:     restore_flags(flags);
                   2661: }
                   2662: 
                   2663: #ifdef CONFIG_MODULES          /* a big #ifdef block... */
                   2664: 
                   2665: /*
                   2666:  * This entry point should be called by a loadable module if it is trying
                   2667:  * add a low level scsi driver to the system.
                   2668:  */
                   2669: static int scsi_register_host(Scsi_Host_Template * tpnt)
                   2670: {
                   2671:     int pcount;
                   2672:     struct Scsi_Host * shpnt;
                   2673:     Scsi_Device * SDpnt;
                   2674:     struct Scsi_Device_Template * sdtpnt;
                   2675:     const char * name;
                   2676:     
                   2677:     if (tpnt->next || !tpnt->detect) return 1;/* Must be already loaded, or
                   2678:                                               * no detect routine available 
                   2679:                                               */
                   2680:     pcount = next_scsi_host;
                   2681:     if ((tpnt->present = tpnt->detect(tpnt)))
                   2682:     {
                   2683:        if(pcount == next_scsi_host) {
                   2684:            if(tpnt->present > 1) {
                   2685:                printk("Failure to register low-level scsi driver");
                   2686:                scsi_unregister_host(tpnt);
                   2687:                return 1;
                   2688:            }
                   2689:            /* The low-level driver failed to register a driver.  We
                   2690:             *  can do this now. 
                   2691:             */
                   2692:            scsi_register(tpnt,0);
                   2693:        }
                   2694:        tpnt->next = scsi_hosts; /* Add to the linked list */
                   2695:        scsi_hosts = tpnt;
                   2696:        
                   2697:        /* Add the new driver to /proc/scsi */
                   2698: #if CONFIG_PROC_FS 
                   2699:        build_proc_dir_entries(tpnt);
                   2700: #endif
                   2701:        
                   2702:        for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
                   2703:            if(shpnt->hostt == tpnt)
                   2704:            {
                   2705:                if(tpnt->info)
                   2706:                    name = tpnt->info(shpnt);
                   2707:                else
                   2708:                    name = tpnt->name;
                   2709:                printk ("scsi%d : %s\n", /* And print a little message */
                   2710:                        shpnt->host_no, name);
                   2711:            }
                   2712:        
                   2713:        printk ("scsi : %d host%s.\n", next_scsi_host,
                   2714:                (next_scsi_host == 1) ? "" : "s");
                   2715:        
                   2716:        scsi_make_blocked_list();
                   2717:        
                   2718:        /* The next step is to call scan_scsis here.  This generates the
                   2719:         * Scsi_Devices entries 
                   2720:         */
                   2721:        
                   2722:        for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
                   2723:            if(shpnt->hostt == tpnt) scan_scsis(shpnt,0,0,0,0);
                   2724:        
                   2725:        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                   2726:            if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
                   2727:        
                   2728:        /* Next we create the Scsi_Cmnd structures for this host */
                   2729:        
                   2730:        for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
                   2731:            if(SDpnt->host->hostt == tpnt)
                   2732:            {
                   2733:                for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                   2734:                    if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
                   2735:                if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
                   2736:            }
                   2737:        
                   2738:        /*
                   2739:         * Now that we have all of the devices, resize the DMA pool,
                   2740:         * as required.  */
                   2741:        resize_dma_pool();
                   2742: 
                   2743: 
                   2744:        /* This does any final handling that is required. */
                   2745:        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                   2746:            if(sdtpnt->finish && sdtpnt->nr_dev)
                   2747:                (*sdtpnt->finish)();
                   2748:     }
                   2749:     
                   2750: #if defined(USE_STATIC_SCSI_MEMORY)
                   2751:     printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
                   2752:            (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
                   2753:            (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
                   2754:            (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
                   2755: #endif
                   2756:        
                   2757:     MOD_INC_USE_COUNT;
                   2758:     return 0;
                   2759: }
                   2760: 
                   2761: /*
                   2762:  * Similarly, this entry point should be called by a loadable module if it
                   2763:  * is trying to remove a low level scsi driver from the system.
                   2764:  */
                   2765: static void scsi_unregister_host(Scsi_Host_Template * tpnt)
                   2766: {
                   2767:     Scsi_Host_Template * SHT, *SHTp;
                   2768:     Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
                   2769:     Scsi_Cmnd * SCpnt;
                   2770:     unsigned long flags;
                   2771:     struct Scsi_Device_Template * sdtpnt;
                   2772:     struct Scsi_Host * shpnt, *sh1;
                   2773:     int pcount;
                   2774:     
                   2775:     /* First verify that this host adapter is completely free with no pending
                   2776:      * commands */
                   2777:     
                   2778:     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
                   2779:        if(sdpnt->host->hostt == tpnt && sdpnt->host->hostt->usage_count
                   2780:           && *sdpnt->host->hostt->usage_count) return;
                   2781:     
                   2782:     for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
                   2783:     {
                   2784:        if (shpnt->hostt != tpnt) continue;
                   2785:        for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
                   2786:        {
                   2787:            save_flags(flags);
                   2788:            cli();
                   2789:            if(SCpnt->request.rq_status != RQ_INACTIVE) {
                   2790:                restore_flags(flags);
                   2791:                for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
                   2792:                    if(SCpnt->request.rq_status == RQ_SCSI_DISCONNECTING)
                   2793:                        SCpnt->request.rq_status = RQ_INACTIVE;
                   2794:                printk("Device busy???\n");
                   2795:                return;
                   2796:            }
                   2797:            SCpnt->request.rq_status = RQ_SCSI_DISCONNECTING;  /* Mark as busy */
                   2798:            restore_flags(flags);
                   2799:        }
                   2800:     }
                   2801:     /* Next we detach the high level drivers from the Scsi_Device structures */
                   2802:     
                   2803:     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
                   2804:        if(sdpnt->host->hostt == tpnt)
                   2805:        {
                   2806:            for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
                   2807:                if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
                   2808:            /* If something still attached, punt */
                   2809:            if (sdpnt->attached) {
                   2810:                printk("Attached usage count = %d\n", sdpnt->attached);
                   2811:                return;
                   2812:            }
                   2813:        }
                   2814:     
                   2815:     /* Next we free up the Scsi_Cmnd structures for this host */
                   2816:     
                   2817:     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
                   2818:        if(sdpnt->host->hostt == tpnt)
                   2819:            while (sdpnt->host->host_queue) {
                   2820:                SCpnt = sdpnt->host->host_queue->next;
                   2821:                scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
                   2822:                sdpnt->host->host_queue = SCpnt;
                   2823:                if (SCpnt) SCpnt->prev = NULL;
                   2824:                sdpnt->has_cmdblocks = 0;
                   2825:            }
                   2826:     
                   2827:     /* Next free up the Scsi_Device structures for this host */
                   2828:     
                   2829:     sdppnt = NULL;
                   2830:     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
                   2831:     {
                   2832:        sdpnt1 = sdpnt->next;
                   2833:        if (sdpnt->host->hostt == tpnt) {
                   2834:            if (sdppnt)
                   2835:                sdppnt->next = sdpnt->next;
                   2836:            else
                   2837:                scsi_devices = sdpnt->next;
                   2838:            scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
                   2839:        } else
                   2840:            sdppnt = sdpnt;
                   2841:     }
                   2842:     
                   2843:     /* Next we go through and remove the instances of the individual hosts
                   2844:      * that were detected */
                   2845:     
                   2846:     shpnt = scsi_hostlist;
                   2847:     while(shpnt) {
                   2848:        sh1 = shpnt->next;
                   2849:        if(shpnt->hostt == tpnt) {
                   2850:            if(shpnt->loaded_as_module) {
                   2851:                pcount = next_scsi_host;
                   2852:                /* Remove the /proc/scsi directory entry */
                   2853: #if CONFIG_PROC_FS 
                   2854:                proc_scsi_unregister(tpnt->proc_dir, 
                   2855:                                     shpnt->host_no + PROC_SCSI_FILE);
                   2856: #endif   
                   2857:                if(tpnt->release)
                   2858:                    (*tpnt->release)(shpnt);
                   2859:                else {
                   2860:                    /* This is the default case for the release function.  
                   2861:                     * It should do the right thing for most correctly 
                   2862:                     * written host adapters. 
                   2863:                     */
                   2864:                    if (shpnt->irq) free_irq(shpnt->irq);
                   2865:                    if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
                   2866:                    if (shpnt->io_port && shpnt->n_io_port)
                   2867:                        release_region(shpnt->io_port, shpnt->n_io_port);
                   2868:                }
                   2869:                if(pcount == next_scsi_host) scsi_unregister(shpnt);
                   2870:                tpnt->present--;
                   2871:            }
                   2872:        }
                   2873:        shpnt = sh1;
                   2874:     }
                   2875:     
                   2876:     /*
                   2877:      * If there are absolutely no more hosts left, it is safe
                   2878:      * to completely nuke the DMA pool.  The resize operation will
                   2879:      * do the right thing and free everything.
                   2880:      */
                   2881:     if( !scsi_devices )
                   2882:        resize_dma_pool();
                   2883: 
                   2884:     printk ("scsi : %d host%s.\n", next_scsi_host,
                   2885:            (next_scsi_host == 1) ? "" : "s");
                   2886:     
                   2887: #if defined(USE_STATIC_SCSI_MEMORY)
                   2888:     printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
                   2889:            (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
                   2890:            (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
                   2891:            (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
                   2892: #endif
                   2893:     
                   2894:     scsi_make_blocked_list();
                   2895:     
                   2896:     /* There were some hosts that were loaded at boot time, so we cannot
                   2897:        do any more than this */
                   2898:     if (tpnt->present) return;
                   2899:     
                   2900:     /* OK, this is the very last step.  Remove this host adapter from the
                   2901:        linked list. */
                   2902:     for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
                   2903:        if(SHT == tpnt) {
                   2904:            if(SHTp)
                   2905:                SHTp->next = SHT->next;
                   2906:            else
                   2907:                scsi_hosts = SHT->next;
                   2908:            SHT->next = NULL;
                   2909:            break;
                   2910:        }
                   2911:     
                   2912:     /* Rebuild the /proc/scsi directory entries */
                   2913: #if CONFIG_PROC_FS 
                   2914:     proc_scsi_unregister(tpnt->proc_dir, tpnt->proc_dir->low_ino);
                   2915: #endif
                   2916:     MOD_DEC_USE_COUNT;
                   2917: }
                   2918: 
                   2919: /*
                   2920:  * This entry point should be called by a loadable module if it is trying
                   2921:  * add a high level scsi driver to the system.
                   2922:  */
                   2923: static int scsi_register_device_module(struct Scsi_Device_Template * tpnt)
                   2924: {
                   2925:     Scsi_Device * SDpnt;
                   2926:     
                   2927:     if (tpnt->next) return 1;
                   2928:     
                   2929:     scsi_register_device(tpnt);
                   2930:     /*
                   2931:      * First scan the devices that we know about, and see if we notice them.
                   2932:      */
                   2933:     
                   2934:     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
                   2935:        if(tpnt->detect) SDpnt->attached += (*tpnt->detect)(SDpnt);
                   2936:     
                   2937:     /*
                   2938:      * If any of the devices would match this driver, then perform the
                   2939:      * init function.
                   2940:      */
                   2941:     if(tpnt->init && tpnt->dev_noticed)
                   2942:        if ((*tpnt->init)()) return 1;
                   2943:     
                   2944:     /*
                   2945:      * Now actually connect the devices to the new driver.
                   2946:      */
                   2947:     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
                   2948:     {
                   2949:        if(tpnt->attach)  (*tpnt->attach)(SDpnt);
                   2950:        /*
                   2951:         * If this driver attached to the device, and we no longer
                   2952:         * have anything attached, release the scso command blocks.
                   2953:         */
                   2954:        if(SDpnt->attached && SDpnt->has_cmdblocks == 0)
                   2955:            scsi_build_commandblocks(SDpnt);
                   2956:     }
                   2957:     
                   2958:     /*
                   2959:      * This does any final handling that is required. 
                   2960:      */
                   2961:     if(tpnt->finish && tpnt->nr_dev)  (*tpnt->finish)();
                   2962:     MOD_INC_USE_COUNT;
                   2963:     return 0;
                   2964: }
                   2965: 
                   2966: static int scsi_unregister_device(struct Scsi_Device_Template * tpnt)
                   2967: {
                   2968:     Scsi_Device * SDpnt;
                   2969:     Scsi_Cmnd * SCpnt;
                   2970:     struct Scsi_Device_Template * spnt;
                   2971:     struct Scsi_Device_Template * prev_spnt;
                   2972:     
                   2973:     /*
                   2974:      * If we are busy, this is not going to fly.
                   2975:      */
                   2976:     if( *tpnt->usage_count != 0) return 0;
                   2977:     /*
                   2978:      * Next, detach the devices from the driver.
                   2979:      */
                   2980:     
                   2981:     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
                   2982:     {
                   2983:        if(tpnt->detach) (*tpnt->detach)(SDpnt);
                   2984:        if(SDpnt->attached == 0)
                   2985:        {
                   2986:            /*
                   2987:             * Nobody is using this device any more.  Free all of the
                   2988:             * command structures.
                   2989:             */
                   2990:            for(SCpnt = SDpnt->host->host_queue; SCpnt; SCpnt = SCpnt->next)
                   2991:            {
                   2992:                if(SCpnt->device == SDpnt)
                   2993:                {
                   2994:                    if(SCpnt->prev != NULL)
                   2995:                        SCpnt->prev->next = SCpnt->next;
                   2996:                    if(SCpnt->next != NULL)
                   2997:                        SCpnt->next->prev = SCpnt->prev;
                   2998:                    if(SCpnt == SDpnt->host->host_queue)
                   2999:                        SDpnt->host->host_queue = SCpnt->next;
                   3000:                    scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
                   3001:                }
                   3002:            }
                   3003:            SDpnt->has_cmdblocks = 0;
                   3004:        }
                   3005:     }
                   3006:     /*
                   3007:      * Extract the template from the linked list.
                   3008:      */
                   3009:     spnt = scsi_devicelist;
                   3010:     prev_spnt = NULL;
                   3011:     while(spnt != tpnt)
                   3012:     {
                   3013:        prev_spnt = spnt;
                   3014:        spnt = spnt->next;
                   3015:     }
                   3016:     if(prev_spnt == NULL)
                   3017:        scsi_devicelist = tpnt->next;
                   3018:     else
                   3019:        prev_spnt->next = spnt->next;
                   3020:     
                   3021:     MOD_DEC_USE_COUNT;
                   3022:     /*
                   3023:      * Final cleanup for the driver is done in the driver sources in the 
                   3024:      * cleanup function.
                   3025:      */
                   3026:     return 0;
                   3027: }
                   3028: 
                   3029: 
                   3030: int scsi_register_module(int module_type, void * ptr)
                   3031: {
                   3032:     switch(module_type){
                   3033:     case MODULE_SCSI_HA:
                   3034:        return scsi_register_host((Scsi_Host_Template *) ptr);
                   3035:        
                   3036:        /* Load upper level device handler of some kind */
                   3037:     case MODULE_SCSI_DEV:
                   3038:        return scsi_register_device_module((struct Scsi_Device_Template *) ptr);
                   3039:        /* The rest of these are not yet implemented */
                   3040:        
                   3041:        /* Load constants.o */
                   3042:     case MODULE_SCSI_CONST:
                   3043:        
                   3044:        /* Load specialized ioctl handler for some device.  Intended for 
                   3045:         * cdroms that have non-SCSI2 audio command sets. */
                   3046:     case MODULE_SCSI_IOCTL:
                   3047:        
                   3048:     default:
                   3049:        return 1;
                   3050:     }
                   3051: }
                   3052: 
                   3053: void scsi_unregister_module(int module_type, void * ptr)
                   3054: {
                   3055:     switch(module_type) {
                   3056:     case MODULE_SCSI_HA:
                   3057:        scsi_unregister_host((Scsi_Host_Template *) ptr);
                   3058:        break;
                   3059:     case MODULE_SCSI_DEV:
                   3060:        scsi_unregister_device((struct Scsi_Device_Template *) ptr);
                   3061:        break;
                   3062:        /* The rest of these are not yet implemented. */
                   3063:     case MODULE_SCSI_CONST:
                   3064:     case MODULE_SCSI_IOCTL:
                   3065:        break;
                   3066:     default:
                   3067:     }
                   3068:     return;
                   3069: }
                   3070: 
                   3071: #endif         /* CONFIG_MODULES */
                   3072: 
                   3073: #ifdef DEBUG_TIMEOUT
                   3074: static void
                   3075: scsi_dump_status(void)
                   3076: {
                   3077:     int i;
                   3078:     struct Scsi_Host * shpnt;
                   3079:     Scsi_Cmnd * SCpnt;
                   3080:     printk("Dump of scsi parameters:\n");
                   3081:     i = 0;
                   3082:     for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
                   3083:        for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
                   3084:        {
                   3085:            /*  (0) 0:0:0:0 (802 123434 8 8 0) (3 3 2) (%d %d %d) %d %x      */
                   3086:            printk("(%d) %d:%d:%d:%d (%s %ld %ld %ld %d) (%d %d %x) (%d %d %d) %x %x %x\n",
                   3087:                   i++, SCpnt->host->host_no,
                   3088:                   SCpnt->channel,
                   3089:                   SCpnt->target,
                   3090:                   SCpnt->lun,
                   3091:                   kdevname(SCpnt->request.rq_dev),
                   3092:                   SCpnt->request.sector,
                   3093:                   SCpnt->request.nr_sectors,
                   3094:                   SCpnt->request.current_nr_sectors,
                   3095:                   SCpnt->use_sg,
                   3096:                   SCpnt->retries,
                   3097:                   SCpnt->allowed,
                   3098:                   SCpnt->flags,
                   3099:                   SCpnt->timeout_per_command,
                   3100:                   SCpnt->timeout,
                   3101:                   SCpnt->internal_timeout,
                   3102:                   SCpnt->cmnd[0],
                   3103:                   SCpnt->sense_buffer[2],
                   3104:                   SCpnt->result);
                   3105:        }
                   3106:     printk("wait_for_request = %p\n", wait_for_request);
                   3107:     /* Now dump the request lists for each block device */
                   3108:     printk("Dump of pending block device requests\n");
                   3109:     for(i=0; i<MAX_BLKDEV; i++)
                   3110:        if(blk_dev[i].current_request)
                   3111:        {
                   3112:            struct request * req;
                   3113:            printk("%d: ", i);
                   3114:            req = blk_dev[i].current_request;
                   3115:            while(req) {
                   3116:                printk("(%s %d %ld %ld %ld) ",
                   3117:                       kdevname(req->rq_dev),
                   3118:                       req->cmd,
                   3119:                       req->sector,
                   3120:                       req->nr_sectors,
                   3121:                       req->current_nr_sectors);
                   3122:                req = req->next;
                   3123:            }
                   3124:            printk("\n");
                   3125:        }
                   3126: }
                   3127: #endif
                   3128: 
                   3129: #ifdef MODULE
                   3130: 
                   3131: int init_module(void) {
                   3132:     unsigned long size;
                   3133: 
                   3134:     /*
                   3135:      * This makes /proc/scsi visible.
                   3136:      */
                   3137:     dispatch_scsi_info_ptr = dispatch_scsi_info;
                   3138: 
                   3139:     timer_table[SCSI_TIMER].fn = scsi_main_timeout;
                   3140:     timer_table[SCSI_TIMER].expires = 0;
                   3141:     register_symtab(&scsi_symbol_table);
                   3142:     scsi_loadable_module_flag = 1;
                   3143: 
                   3144:     /* Register the /proc/scsi/scsi entry */
                   3145: #if CONFIG_PROC_FS
                   3146:     proc_scsi_register(0, &proc_scsi_scsi);
                   3147: #endif
                   3148: 
                   3149:     
                   3150:     dma_sectors = PAGE_SIZE / SECTOR_SIZE;
                   3151:     dma_free_sectors= dma_sectors;
                   3152:     /*
                   3153:      * Set up a minimal DMA buffer list - this will be used during scan_scsis
                   3154:      * in some cases.
                   3155:      */
                   3156:     
                   3157:     /* One bit per sector to indicate free/busy */
                   3158:     size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
                   3159:     dma_malloc_freelist = (unsigned char *) scsi_init_malloc(size, GFP_ATOMIC);
                   3160:     memset(dma_malloc_freelist, 0, size);
                   3161: 
                   3162:     /* One pointer per page for the page list */
                   3163:     dma_malloc_pages = (unsigned char **)
                   3164:        scsi_init_malloc((dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages), GFP_ATOMIC);
                   3165:     dma_malloc_pages[0] = (unsigned char *)
                   3166:        scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
                   3167:     return 0;
                   3168: }
                   3169: 
                   3170: void cleanup_module( void) 
                   3171: {
                   3172: #if CONFIG_PROC_FS
                   3173:     proc_scsi_unregister(0, PROC_SCSI_SCSI);
                   3174: #endif
                   3175: 
                   3176:     /* No, we're not here anymore. Don't show the /proc/scsi files. */
                   3177:     dispatch_scsi_info_ptr = 0L;
                   3178: 
                   3179:     /*
                   3180:      * Free up the DMA pool.
                   3181:      */
                   3182:     resize_dma_pool();
                   3183: 
                   3184:     timer_table[SCSI_TIMER].fn = NULL;
                   3185:     timer_table[SCSI_TIMER].expires = 0;
                   3186: }
                   3187: #endif /* MODULE */
                   3188: 
                   3189: /*
                   3190:  * Overrides for Emacs so that we follow Linus's tabbing style.
                   3191:  * Emacs will notice this stuff at the end of the file and automatically
                   3192:  * adjust the settings for this buffer only.  This must remain at the end
                   3193:  * of the file.
                   3194:  * ---------------------------------------------------------------------------
                   3195:  * Local variables:
                   3196:  * c-indent-level: 4
                   3197:  * c-brace-imaginary-offset: 0
                   3198:  * c-brace-offset: -4
                   3199:  * c-argdecl-indent: 4
                   3200:  * c-label-offset: -4
                   3201:  * c-continued-statement-offset: 4
                   3202:  * c-continued-brace-offset: 0
                   3203:  * indent-tabs-mode: nil
                   3204:  * tab-width: 8
                   3205:  * End:
                   3206:  */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.