root/drivers/scsi/scsi.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get_device_flags
  2. scsi_make_blocked_list
  3. scan_scsis_done
  4. scsi_luns_setup
  5. scan_scsis
  6. scan_scsis_single
  7. scsi_times_out
  8. request_queueable
  9. allocate_device
  10. internal_cmnd
  11. scsi_request_sense
  12. scsi_do_cmd
  13. check_sense
  14. scsi_done
  15. scsi_abort
  16. scsi_mark_device_reset
  17. scsi_mark_host_bus_reset
  18. scsi_reset
  19. scsi_main_timeout
  20. update_timeout
  21. scsi_malloc
  22. scsi_free
  23. scsi_init_malloc
  24. scsi_init_free
  25. scsi_build_commandblocks
  26. scsi_dev_init
  27. print_inquiry
  28. scsi_proc_info
  29. resize_dma_pool
  30. scsi_register_host
  31. scsi_unregister_host
  32. scsi_register_device_module
  33. scsi_unregister_device
  34. scsi_register_module
  35. scsi_unregister_module
  36. scsi_dump_status
  37. init_module
  38. cleanup_module

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

/* [previous][next][first][last][top][bottom][index][help] */