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;
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                 if (SCpnt != SCset)
2165                   SCpnt->timeout -= used;
2166                 if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
2167                 if(SCpnt->timeout > 0 && SCpnt->timeout < least)
2168                     least = SCpnt->timeout;
2169             }
2170     
2171     /*
2172      * If something is due to timeout again, then we will set the next timeout
2173      * interrupt to occur.  Otherwise, timeouts are disabled.
2174      */
2175     
2176     if (least != 0xffffffff)
2177     {
2178         time_start = jiffies;
2179         timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
2180         timer_active |= 1 << SCSI_TIMER;
2181     }
2182     else
2183     {
2184         timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
2185         timer_active &= ~(1 << SCSI_TIMER);
2186     }
2187     restore_flags(flags);
2188     return oldto;
2189 }
2190 
2191 #ifdef CONFIG_MODULES
2192 static int scsi_register_host(Scsi_Host_Template *);
2193 static void scsi_unregister_host(Scsi_Host_Template *);
2194 #endif
2195 
2196 void *scsi_malloc(unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
2197 {
2198     unsigned int nbits, mask;
2199     unsigned long flags;
2200     int i, j;
2201     if(len % SECTOR_SIZE != 0 || len > PAGE_SIZE)
2202         return NULL;
2203     
2204     save_flags(flags);
2205     cli();
2206     nbits = len >> 9;
2207     mask = (1 << nbits) - 1;
2208     
2209     for(i=0;i < dma_sectors / SECTORS_PER_PAGE; i++)
2210         for(j=0; j<=SECTORS_PER_PAGE - nbits; j++){
2211             if ((dma_malloc_freelist[i] & (mask << j)) == 0){
2212                 dma_malloc_freelist[i] |= (mask << j);
2213                 restore_flags(flags);
2214                 dma_free_sectors -= nbits;
2215 #ifdef DEBUG
2216                 printk("SMalloc: %d %p\n",len, dma_malloc_pages[i] + (j << 9));
2217 #endif
2218                 return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
2219             }
2220         }
2221     restore_flags(flags);
2222     return NULL;  /* Nope.  No more */
2223 }
2224 
2225 int scsi_free(void *obj, unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
2226 {
2227     unsigned int page, sector, nbits, mask;
2228     unsigned long flags;
2229     
2230 #ifdef DEBUG
2231     printk("scsi_free %p %d\n",obj, len);
2232 #endif
2233     
2234     for (page = 0; page < dma_sectors / SECTORS_PER_PAGE; page++) {
2235         unsigned long page_addr = (unsigned long) dma_malloc_pages[page];
2236         if ((unsigned long) obj >= page_addr &&
2237             (unsigned long) obj <  page_addr + PAGE_SIZE)
2238         {
2239             sector = (((unsigned long) obj) - page_addr) >> 9;
2240 
2241             nbits = len >> 9;
2242             mask = (1 << nbits) - 1;
2243 
2244             if ((mask << sector) >= (1 << SECTORS_PER_PAGE))
2245                 panic ("scsi_free:Bad memory alignment");
2246 
2247             save_flags(flags);
2248             cli();
2249             if((dma_malloc_freelist[page] & (mask << sector)) != (mask<<sector))
2250                 panic("scsi_free:Trying to free unused memory");
2251 
2252             dma_free_sectors += nbits;
2253             dma_malloc_freelist[page] &= ~(mask << sector);
2254             restore_flags(flags);
2255             return 0;
2256         }
2257     }
2258     panic("scsi_free:Bad offset");
2259 }
2260 
2261 
2262 int scsi_loadable_module_flag; /* Set after we scan builtin drivers */
2263 
2264 void * scsi_init_malloc(unsigned int size, int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
2265 {
2266     void * retval;
2267     
2268     /*
2269      * For buffers used by the DMA pool, we assume page aligned 
2270      * structures.
2271      */
2272     if ((size % PAGE_SIZE) == 0) {
2273         int order, a_size;
2274         for (order = 0, a_size = PAGE_SIZE;
2275              a_size < size; order++, a_size <<= 1)
2276             ;
2277         retval = (void *) __get_dma_pages(priority & GFP_LEVEL_MASK,
2278                                                     order);
2279     } else
2280         retval = kmalloc(size, priority);
2281 
2282     if (retval)
2283         memset(retval, 0, size);
2284     return retval;
2285 }
2286 
2287 
2288 void scsi_init_free(char * ptr, unsigned int size)
     /* [previous][next][first][last][top][bottom][index][help] */
2289 { 
2290     /*
2291      * We need this special code here because the DMA pool assumes
2292      * page aligned data.  Besides, it is wasteful to allocate
2293      * page sized chunks with kmalloc.
2294      */
2295     if ((size % PAGE_SIZE) == 0) {
2296         int order, a_size;
2297 
2298         for (order = 0, a_size = PAGE_SIZE;
2299              a_size < size; order++, a_size <<= 1)
2300             ;
2301         free_pages((unsigned long)ptr, order);
2302     } else
2303         kfree(ptr);
2304 }
2305 
2306 void scsi_build_commandblocks(Scsi_Device * SDpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2307 {
2308     int j;
2309     Scsi_Cmnd * SCpnt;
2310     struct Scsi_Host * host = NULL;
2311     
2312     for(j=0;j<SDpnt->host->cmd_per_lun;j++){
2313         host = SDpnt->host;
2314         SCpnt = (Scsi_Cmnd *)
2315                 scsi_init_malloc(sizeof(Scsi_Cmnd),
2316                                  GFP_ATOMIC |
2317                                  (host->unchecked_isa_dma ? GFP_DMA : 0));
2318         SCpnt->host = host;
2319         SCpnt->device = SDpnt;
2320         SCpnt->target = SDpnt->id;
2321         SCpnt->lun = SDpnt->lun;
2322         SCpnt->channel = SDpnt->channel;
2323         SCpnt->request.rq_status = RQ_INACTIVE;
2324         SCpnt->use_sg = 0;
2325         SCpnt->old_use_sg = 0;
2326         SCpnt->old_cmd_len = 0;
2327         SCpnt->timeout = 0;
2328         SCpnt->underflow = 0;
2329         SCpnt->transfersize = 0;
2330         SCpnt->host_scribble = NULL;
2331         if(host->host_queue)
2332             host->host_queue->prev = SCpnt;
2333         SCpnt->next = host->host_queue;
2334         SCpnt->prev = NULL;
2335         host->host_queue = SCpnt;
2336     }
2337     SDpnt->has_cmdblocks = 1;
2338 }
2339 
2340 /*
2341  * scsi_dev_init() is our initialization routine, which in turn calls host
2342  * initialization, bus scanning, and sd/st initialization routines. 
2343  */
2344 
2345 int scsi_dev_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2346 {
2347     Scsi_Device * SDpnt;
2348     struct Scsi_Host * shpnt;
2349     struct Scsi_Device_Template * sdtpnt;
2350 #ifdef FOO_ON_YOU
2351     return;
2352 #endif
2353 
2354     /* Yes we're here... */
2355     dispatch_scsi_info_ptr = dispatch_scsi_info;
2356 
2357     /* Init a few things so we can "malloc" memory. */
2358     scsi_loadable_module_flag = 0;
2359     
2360     timer_table[SCSI_TIMER].fn = scsi_main_timeout;
2361     timer_table[SCSI_TIMER].expires = 0;
2362 
2363 #ifdef CONFIG_MODULES
2364     register_symtab(&scsi_symbol_table);
2365 #endif    
2366 
2367     /* Register the /proc/scsi/scsi entry */
2368 #if CONFIG_PROC_FS 
2369     proc_scsi_register(0, &proc_scsi_scsi);    
2370 #endif
2371 
2372     /* initialize all hosts */
2373     scsi_init();
2374 
2375     scsi_devices = (Scsi_Device *) NULL;
2376 
2377     for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2378         scan_scsis(shpnt,0,0,0,0);           /* scan for scsi devices */
2379 
2380     printk("scsi : detected ");
2381     for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2382         if (sdtpnt->dev_noticed && sdtpnt->name)
2383             printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
2384                    (sdtpnt->dev_noticed != 1) ? "s" : "");
2385     printk("total.\n");
2386     
2387     for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2388         if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2389 
2390     for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2391         SDpnt->scsi_request_fn = NULL;
2392         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2393             if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2394         if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
2395     }
2396     
2397 
2398     /*
2399      * This should build the DMA pool.
2400      */
2401     resize_dma_pool();
2402 
2403     /*
2404      * OK, now we finish the initialization by doing spin-up, read
2405      * capacity, etc, etc 
2406      */
2407     for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2408         if(sdtpnt->finish && sdtpnt->nr_dev)
2409             (*sdtpnt->finish)();
2410 
2411     scsi_loadable_module_flag = 1;
2412 
2413     return 0;
2414 }
2415 
2416 static void print_inquiry(unsigned char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
2417 {
2418     int i;
2419     
2420     printk("  Vendor: ");
2421     for (i = 8; i < 16; i++)
2422     {
2423         if (data[i] >= 0x20 && i < data[4] + 5)
2424             printk("%c", data[i]);
2425         else
2426             printk(" ");
2427     }
2428     
2429     printk("  Model: ");
2430     for (i = 16; i < 32; i++)
2431     {
2432         if (data[i] >= 0x20 && i < data[4] + 5)
2433             printk("%c", data[i]);
2434         else
2435             printk(" ");
2436     }
2437     
2438     printk("  Rev: ");
2439     for (i = 32; i < 36; i++)
2440     {
2441         if (data[i] >= 0x20 && i < data[4] + 5)
2442             printk("%c", data[i]);
2443         else
2444             printk(" ");
2445     }
2446     
2447     printk("\n");
2448     
2449     i = data[0] & 0x1f;
2450     
2451     printk("  Type:   %s ",
2452            i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
2453     printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
2454     if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
2455         printk(" CCS\n");
2456     else
2457         printk("\n");
2458 }
2459 
2460 
2461 #ifdef CONFIG_PROC_FS
2462 int scsi_proc_info(char *buffer, char **start, off_t offset, int length, 
     /* [previous][next][first][last][top][bottom][index][help] */
2463                     int hostno, int inout)
2464 {
2465     Scsi_Device *scd;
2466     struct Scsi_Host *HBA_ptr;
2467     int  parameter[4];
2468     char *p;
2469     int   i,size, len = 0;
2470     off_t begin = 0;
2471     off_t pos = 0;
2472 
2473     scd = scsi_devices;
2474     HBA_ptr = scsi_hostlist;
2475 
2476     if(inout == 0) { 
2477         size = sprintf(buffer+len,"Attached devices: %s\n", (scd)?"":"none");
2478         len += size; 
2479         pos = begin + len;
2480         while (HBA_ptr) {
2481 #if 0
2482             size += sprintf(buffer+len,"scsi%2d: %s\n", (int) HBA_ptr->host_no, 
2483                             HBA_ptr->hostt->procname);
2484             len += size; 
2485             pos = begin + len;
2486 #endif
2487             scd = scsi_devices;
2488             while (scd) {
2489                 if (scd->host == HBA_ptr) {
2490                     proc_print_scsidevice(scd, buffer, &size, len);
2491                     len += size; 
2492                     pos = begin + len;
2493                     
2494                     if (pos < offset) {
2495                         len = 0;
2496                         begin = pos;
2497                     }
2498                     if (pos > offset + length)
2499                         goto stop_output;
2500                 }
2501                 scd = scd->next;
2502             }
2503             HBA_ptr = HBA_ptr->next;
2504         }
2505         
2506     stop_output:
2507         *start=buffer+(offset-begin);   /* Start of wanted data */
2508         len-=(offset-begin);            /* Start slop */
2509         if(len>length)
2510             len = length;               /* Ending slop */
2511         return (len);     
2512     }
2513 
2514     /*
2515      * Usage: echo "scsi singledevice 0 1 2 3" >/proc/scsi/scsi
2516      * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
2517      * Consider this feature BETA.
2518      *     CAUTION: This is not for hotplugging your peripherals. As
2519      *     SCSI was not designed for this you could damage your
2520      *     hardware !  
2521      * However perhaps it is legal to switch on an
2522      * already connected device. It is perhaps not 
2523      * guaranteed this device doesn't corrupt an ongoing data transfer.
2524      */
2525     if(!buffer || length < 25 || strncmp("scsi", buffer, 4))
2526         return(-EINVAL);
2527 
2528     if(!strncmp("singledevice", buffer + 5, 12)) {
2529         p = buffer + 17;
2530 
2531         for(i=0; i<4; i++)      {
2532             p++;
2533             parameter[i] = simple_strtoul(p, &p, 0);
2534         }
2535         printk("scsi singledevice %d %d %d %d\n", parameter[0], parameter[1],
2536                         parameter[2], parameter[3]);
2537 
2538         while(scd && (scd->host->host_no != parameter[0] 
2539               || scd->channel != parameter[1] 
2540               || scd->id != parameter[2] 
2541               || scd->lun != parameter[3])) {
2542             scd = scd->next;
2543         }
2544         if(scd)
2545             return(-ENOSYS);  /* We do not yet support unplugging */
2546         while(HBA_ptr && HBA_ptr->host_no != parameter[0])
2547             HBA_ptr = HBA_ptr->next;
2548 
2549         if(!HBA_ptr)
2550             return(-ENXIO);
2551 
2552         scan_scsis (HBA_ptr, 1, parameter[1], parameter[2], parameter[3]);
2553         return(length);
2554     }
2555     return(-EINVAL);
2556 }
2557 #endif
2558 
2559 /*
2560  * Go through the device list and recompute the most appropriate size
2561  * for the dma pool.  Then grab more memory (as required).
2562  */
2563 static void resize_dma_pool(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2564 {
2565     int i;
2566     unsigned long size;
2567     struct Scsi_Host * shpnt;
2568     struct Scsi_Host * host = NULL;
2569     Scsi_Device * SDpnt;
2570     unsigned long flags;
2571     FreeSectorBitmap * new_dma_malloc_freelist = NULL;
2572     unsigned int new_dma_sectors = 0;
2573     unsigned int new_need_isa_buffer = 0;
2574     unsigned char ** new_dma_malloc_pages = NULL;
2575 
2576     if( !scsi_devices )
2577     {
2578         /*
2579          * Free up the DMA pool.
2580          */
2581         if( dma_free_sectors != dma_sectors )
2582             panic("SCSI DMA pool memory leak %d %d\n",dma_free_sectors,dma_sectors);
2583 
2584         for(i=0; i < dma_sectors / SECTORS_PER_PAGE; i++)
2585             scsi_init_free(dma_malloc_pages[i], PAGE_SIZE);
2586         if (dma_malloc_pages)
2587             scsi_init_free((char *) dma_malloc_pages,
2588                            (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages));
2589         dma_malloc_pages = NULL;
2590         if (dma_malloc_freelist)
2591             scsi_init_free((char *) dma_malloc_freelist,
2592                            (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_freelist));
2593         dma_malloc_freelist = NULL;
2594         dma_sectors = 0;
2595         dma_free_sectors = 0;
2596         return;
2597     }
2598     /* Next, check to see if we need to extend the DMA buffer pool */
2599         
2600     new_dma_sectors = 2*SECTORS_PER_PAGE;               /* Base value we use */
2601 
2602     if (high_memory-1 > ISA_DMA_THRESHOLD)
2603         scsi_need_isa_bounce_buffers = 1;
2604     else
2605         scsi_need_isa_bounce_buffers = 0;
2606     
2607     if (scsi_devicelist)
2608         for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2609             new_dma_sectors += SECTORS_PER_PAGE;        /* Increment for each host */
2610     
2611     for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2612         host = SDpnt->host;
2613         
2614         if(SDpnt->type != TYPE_TAPE)
2615             new_dma_sectors += ((host->sg_tablesize *
2616                                  sizeof(struct scatterlist) + 511) >> 9) *
2617                                      host->cmd_per_lun;
2618         
2619         if(host->unchecked_isa_dma &&
2620            scsi_need_isa_bounce_buffers &&
2621            SDpnt->type != TYPE_TAPE) {
2622             new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
2623                 host->cmd_per_lun;
2624             new_need_isa_buffer++;
2625         }
2626     }
2627     
2628     /* limit DMA memory to 32MB: */
2629     new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
2630     
2631     /*
2632      * We never shrink the buffers - this leads to
2633      * race conditions that I would rather not even think
2634      * about right now.
2635      */
2636     if( new_dma_sectors < dma_sectors )
2637         new_dma_sectors = dma_sectors;
2638     
2639     if (new_dma_sectors)
2640     {
2641         size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
2642         new_dma_malloc_freelist = (FreeSectorBitmap *) scsi_init_malloc(size, GFP_ATOMIC);
2643         memset(new_dma_malloc_freelist, 0, size);
2644 
2645         size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(*new_dma_malloc_pages);
2646         new_dma_malloc_pages = (unsigned char **) scsi_init_malloc(size, GFP_ATOMIC);
2647         memset(new_dma_malloc_pages, 0, size);
2648     }
2649     
2650     /*
2651      * If we need more buffers, expand the list.
2652      */
2653     if( new_dma_sectors > dma_sectors ) { 
2654         for(i=dma_sectors / SECTORS_PER_PAGE; i< new_dma_sectors / SECTORS_PER_PAGE; i++)
2655             new_dma_malloc_pages[i] = (unsigned char *)
2656                 scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
2657     }
2658     
2659     /* When we dick with the actual DMA list, we need to 
2660      * protect things 
2661      */
2662     save_flags(flags);
2663     cli();
2664     if (dma_malloc_freelist)
2665     {
2666         size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
2667         memcpy(new_dma_malloc_freelist, dma_malloc_freelist, size);
2668         scsi_init_free((char *) dma_malloc_freelist, size);
2669     }
2670     dma_malloc_freelist = new_dma_malloc_freelist;
2671     
2672     if (dma_malloc_pages)
2673     {
2674         size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages);
2675         memcpy(new_dma_malloc_pages, dma_malloc_pages, size);
2676         scsi_init_free((char *) dma_malloc_pages, size);
2677     }
2678     
2679     dma_free_sectors += new_dma_sectors - dma_sectors;
2680     dma_malloc_pages = new_dma_malloc_pages;
2681     dma_sectors = new_dma_sectors;
2682     need_isa_buffer = new_need_isa_buffer;
2683     restore_flags(flags);
2684 }
2685 
2686 #ifdef CONFIG_MODULES           /* a big #ifdef block... */
2687 
2688 /*
2689  * This entry point should be called by a loadable module if it is trying
2690  * add a low level scsi driver to the system.
2691  */
2692 static int scsi_register_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2693 {
2694     int pcount;
2695     struct Scsi_Host * shpnt;
2696     Scsi_Device * SDpnt;
2697     struct Scsi_Device_Template * sdtpnt;
2698     const char * name;
2699     
2700     if (tpnt->next || !tpnt->detect) return 1;/* Must be already loaded, or
2701                                                * no detect routine available 
2702                                                */
2703     pcount = next_scsi_host;
2704     if ((tpnt->present = tpnt->detect(tpnt)))
2705     {
2706         if(pcount == next_scsi_host) {
2707             if(tpnt->present > 1) {
2708                 printk("Failure to register low-level scsi driver");
2709                 scsi_unregister_host(tpnt);
2710                 return 1;
2711             }
2712             /* The low-level driver failed to register a driver.  We
2713              *  can do this now. 
2714              */
2715             scsi_register(tpnt,0);
2716         }
2717         tpnt->next = scsi_hosts; /* Add to the linked list */
2718         scsi_hosts = tpnt;
2719         
2720         /* Add the new driver to /proc/scsi */
2721 #if CONFIG_PROC_FS 
2722         build_proc_dir_entries(tpnt);
2723 #endif
2724         
2725         for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2726             if(shpnt->hostt == tpnt)
2727             {
2728                 if(tpnt->info)
2729                     name = tpnt->info(shpnt);
2730                 else
2731                     name = tpnt->name;
2732                 printk ("scsi%d : %s\n", /* And print a little message */
2733                         shpnt->host_no, name);
2734             }
2735         
2736         printk ("scsi : %d host%s.\n", next_scsi_host,
2737                 (next_scsi_host == 1) ? "" : "s");
2738         
2739         scsi_make_blocked_list();
2740         
2741         /* The next step is to call scan_scsis here.  This generates the
2742          * Scsi_Devices entries 
2743          */
2744         
2745         for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2746             if(shpnt->hostt == tpnt) scan_scsis(shpnt,0,0,0,0);
2747         
2748         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2749             if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2750         
2751         /* Next we create the Scsi_Cmnd structures for this host */
2752         
2753         for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2754             if(SDpnt->host->hostt == tpnt)
2755             {
2756                 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2757                     if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2758                 if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
2759             }
2760         
2761         /*
2762          * Now that we have all of the devices, resize the DMA pool,
2763          * as required.  */
2764         resize_dma_pool();
2765 
2766 
2767         /* This does any final handling that is required. */
2768         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2769             if(sdtpnt->finish && sdtpnt->nr_dev)
2770                 (*sdtpnt->finish)();
2771     }
2772     
2773 #if defined(USE_STATIC_SCSI_MEMORY)
2774     printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
2775             (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
2776             (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
2777             (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
2778 #endif
2779         
2780     MOD_INC_USE_COUNT;
2781     return 0;
2782 }
2783 
2784 /*
2785  * Similarly, this entry point should be called by a loadable module if it
2786  * is trying to remove a low level scsi driver from the system.
2787  */
2788 static void scsi_unregister_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2789 {
2790     Scsi_Host_Template * SHT, *SHTp;
2791     Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
2792     Scsi_Cmnd * SCpnt;
2793     unsigned long flags;
2794     struct Scsi_Device_Template * sdtpnt;
2795     struct Scsi_Host * shpnt, *sh1;
2796     int pcount;
2797     
2798     /* First verify that this host adapter is completely free with no pending
2799      * commands */
2800     
2801     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2802         if(sdpnt->host->hostt == tpnt && sdpnt->host->hostt->usage_count
2803            && *sdpnt->host->hostt->usage_count) return;
2804     
2805     for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2806     {
2807         if (shpnt->hostt != tpnt) continue;
2808         for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2809         {
2810             save_flags(flags);
2811             cli();
2812             if(SCpnt->request.rq_status != RQ_INACTIVE) {
2813                 restore_flags(flags);
2814                 for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2815                     if(SCpnt->request.rq_status == RQ_SCSI_DISCONNECTING)
2816                         SCpnt->request.rq_status = RQ_INACTIVE;
2817                 printk("Device busy???\n");
2818                 return;
2819             }
2820             SCpnt->request.rq_status = RQ_SCSI_DISCONNECTING;  /* Mark as busy */
2821             restore_flags(flags);
2822         }
2823     }
2824     /* Next we detach the high level drivers from the Scsi_Device structures */
2825     
2826     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2827         if(sdpnt->host->hostt == tpnt)
2828         {
2829             for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2830                 if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
2831             /* If something still attached, punt */
2832             if (sdpnt->attached) {
2833                 printk("Attached usage count = %d\n", sdpnt->attached);
2834                 return;
2835             }
2836         }
2837     
2838     /* Next we free up the Scsi_Cmnd structures for this host */
2839     
2840     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2841         if(sdpnt->host->hostt == tpnt)
2842             while (sdpnt->host->host_queue) {
2843                 SCpnt = sdpnt->host->host_queue->next;
2844                 scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
2845                 sdpnt->host->host_queue = SCpnt;
2846                 if (SCpnt) SCpnt->prev = NULL;
2847                 sdpnt->has_cmdblocks = 0;
2848             }
2849     
2850     /* Next free up the Scsi_Device structures for this host */
2851     
2852     sdppnt = NULL;
2853     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
2854     {
2855         sdpnt1 = sdpnt->next;
2856         if (sdpnt->host->hostt == tpnt) {
2857             if (sdppnt)
2858                 sdppnt->next = sdpnt->next;
2859             else
2860                 scsi_devices = sdpnt->next;
2861             scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
2862         } else
2863             sdppnt = sdpnt;
2864     }
2865     
2866     /* Next we go through and remove the instances of the individual hosts
2867      * that were detected */
2868     
2869     shpnt = scsi_hostlist;
2870     while(shpnt) {
2871         sh1 = shpnt->next;
2872         if(shpnt->hostt == tpnt) {
2873             if(shpnt->loaded_as_module) {
2874                 pcount = next_scsi_host;
2875                 /* Remove the /proc/scsi directory entry */
2876 #if CONFIG_PROC_FS 
2877                 proc_scsi_unregister(tpnt->proc_dir, 
2878                                      shpnt->host_no + PROC_SCSI_FILE);
2879 #endif   
2880                 if(tpnt->release)
2881                     (*tpnt->release)(shpnt);
2882                 else {
2883                     /* This is the default case for the release function.  
2884                      * It should do the right thing for most correctly 
2885                      * written host adapters. 
2886                      */
2887                     if (shpnt->irq) free_irq(shpnt->irq, NULL);
2888                     if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
2889                     if (shpnt->io_port && shpnt->n_io_port)
2890                         release_region(shpnt->io_port, shpnt->n_io_port);
2891                 }
2892                 if(pcount == next_scsi_host) scsi_unregister(shpnt);
2893                 tpnt->present--;
2894             }
2895         }
2896         shpnt = sh1;
2897     }
2898     
2899     /*
2900      * If there are absolutely no more hosts left, it is safe
2901      * to completely nuke the DMA pool.  The resize operation will
2902      * do the right thing and free everything.
2903      */
2904     if( !scsi_devices )
2905         resize_dma_pool();
2906 
2907     printk ("scsi : %d host%s.\n", next_scsi_host,
2908             (next_scsi_host == 1) ? "" : "s");
2909     
2910 #if defined(USE_STATIC_SCSI_MEMORY)
2911     printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
2912             (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
2913             (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
2914             (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
2915 #endif
2916     
2917     scsi_make_blocked_list();
2918     
2919     /* There were some hosts that were loaded at boot time, so we cannot
2920        do any more than this */
2921     if (tpnt->present) return;
2922     
2923     /* OK, this is the very last step.  Remove this host adapter from the
2924        linked list. */
2925     for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
2926         if(SHT == tpnt) {
2927             if(SHTp)
2928                 SHTp->next = SHT->next;
2929             else
2930                 scsi_hosts = SHT->next;
2931             SHT->next = NULL;
2932             break;
2933         }
2934     
2935     /* Rebuild the /proc/scsi directory entries */
2936 #if CONFIG_PROC_FS 
2937     proc_scsi_unregister(tpnt->proc_dir, tpnt->proc_dir->low_ino);
2938 #endif
2939     MOD_DEC_USE_COUNT;
2940 }
2941 
2942 /*
2943  * This entry point should be called by a loadable module if it is trying
2944  * add a high level scsi driver to the system.
2945  */
2946 static int scsi_register_device_module(struct Scsi_Device_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2947 {
2948     Scsi_Device * SDpnt;
2949     
2950     if (tpnt->next) return 1;
2951     
2952     scsi_register_device(tpnt);
2953     /*
2954      * First scan the devices that we know about, and see if we notice them.
2955      */
2956     
2957     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2958         if(tpnt->detect) SDpnt->attached += (*tpnt->detect)(SDpnt);
2959     
2960     /*
2961      * If any of the devices would match this driver, then perform the
2962      * init function.
2963      */
2964     if(tpnt->init && tpnt->dev_noticed)
2965         if ((*tpnt->init)()) return 1;
2966     
2967     /*
2968      * Now actually connect the devices to the new driver.
2969      */
2970     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2971     {
2972         if(tpnt->attach)  (*tpnt->attach)(SDpnt);
2973         /*
2974          * If this driver attached to the device, and we no longer
2975          * have anything attached, release the scso command blocks.
2976          */
2977         if(SDpnt->attached && SDpnt->has_cmdblocks == 0)
2978             scsi_build_commandblocks(SDpnt);
2979     }
2980     
2981     /*
2982      * This does any final handling that is required. 
2983      */
2984     if(tpnt->finish && tpnt->nr_dev)  (*tpnt->finish)();
2985     MOD_INC_USE_COUNT;
2986     return 0;
2987 }
2988 
2989 static int scsi_unregister_device(struct Scsi_Device_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2990 {
2991     Scsi_Device * SDpnt;
2992     Scsi_Cmnd * SCpnt;
2993     struct Scsi_Device_Template * spnt;
2994     struct Scsi_Device_Template * prev_spnt;
2995     
2996     /*
2997      * If we are busy, this is not going to fly.
2998      */
2999     if( *tpnt->usage_count != 0) return 0;
3000     /*
3001      * Next, detach the devices from the driver.
3002      */
3003     
3004     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
3005     {
3006         if(tpnt->detach) (*tpnt->detach)(SDpnt);
3007         if(SDpnt->attached == 0)
3008         {
3009             /*
3010              * Nobody is using this device any more.  Free all of the
3011              * command structures.
3012              */
3013             for(SCpnt = SDpnt->host->host_queue; SCpnt; SCpnt = SCpnt->next)
3014             {
3015                 if(SCpnt->device == SDpnt)
3016                 {
3017                     if(SCpnt->prev != NULL)
3018                         SCpnt->prev->next = SCpnt->next;
3019                     if(SCpnt->next != NULL)
3020                         SCpnt->next->prev = SCpnt->prev;
3021                     if(SCpnt == SDpnt->host->host_queue)
3022                         SDpnt->host->host_queue = SCpnt->next;
3023                     scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
3024                 }
3025             }
3026             SDpnt->has_cmdblocks = 0;
3027         }
3028     }
3029     /*
3030      * Extract the template from the linked list.
3031      */
3032     spnt = scsi_devicelist;
3033     prev_spnt = NULL;
3034     while(spnt != tpnt)
3035     {
3036         prev_spnt = spnt;
3037         spnt = spnt->next;
3038     }
3039     if(prev_spnt == NULL)
3040         scsi_devicelist = tpnt->next;
3041     else
3042         prev_spnt->next = spnt->next;
3043     
3044     MOD_DEC_USE_COUNT;
3045     /*
3046      * Final cleanup for the driver is done in the driver sources in the 
3047      * cleanup function.
3048      */
3049     return 0;
3050 }
3051 
3052 
3053 int scsi_register_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
3054 {
3055     switch(module_type){
3056     case MODULE_SCSI_HA:
3057         return scsi_register_host((Scsi_Host_Template *) ptr);
3058         
3059         /* Load upper level device handler of some kind */
3060     case MODULE_SCSI_DEV:
3061         return scsi_register_device_module((struct Scsi_Device_Template *) ptr);
3062         /* The rest of these are not yet implemented */
3063         
3064         /* Load constants.o */
3065     case MODULE_SCSI_CONST:
3066         
3067         /* Load specialized ioctl handler for some device.  Intended for 
3068          * cdroms that have non-SCSI2 audio command sets. */
3069     case MODULE_SCSI_IOCTL:
3070         
3071     default:
3072         return 1;
3073     }
3074 }
3075 
3076 void scsi_unregister_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
3077 {
3078     switch(module_type) {
3079     case MODULE_SCSI_HA:
3080         scsi_unregister_host((Scsi_Host_Template *) ptr);
3081         break;
3082     case MODULE_SCSI_DEV:
3083         scsi_unregister_device((struct Scsi_Device_Template *) ptr);
3084         break;
3085         /* The rest of these are not yet implemented. */
3086     case MODULE_SCSI_CONST:
3087     case MODULE_SCSI_IOCTL:
3088         break;
3089     default:
3090     }
3091     return;
3092 }
3093 
3094 #endif          /* CONFIG_MODULES */
3095 
3096 #ifdef DEBUG_TIMEOUT
3097 static void
3098 scsi_dump_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3099 {
3100     int i;
3101     struct Scsi_Host * shpnt;
3102     Scsi_Cmnd * SCpnt;
3103     printk("Dump of scsi parameters:\n");
3104     i = 0;
3105     for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
3106         for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
3107         {
3108             /*  (0) 0:0:0:0 (802 123434 8 8 0) (3 3 2) (%d %d %d) %d %x      */
3109             printk("(%d) %d:%d:%d:%d (%s %ld %ld %ld %d) (%d %d %x) (%d %d %d) %x %x %x\n",
3110                    i++, SCpnt->host->host_no,
3111                    SCpnt->channel,
3112                    SCpnt->target,
3113                    SCpnt->lun,
3114                    kdevname(SCpnt->request.rq_dev),
3115                    SCpnt->request.sector,
3116                    SCpnt->request.nr_sectors,
3117                    SCpnt->request.current_nr_sectors,
3118                    SCpnt->use_sg,
3119                    SCpnt->retries,
3120                    SCpnt->allowed,
3121                    SCpnt->flags,
3122                    SCpnt->timeout_per_command,
3123                    SCpnt->timeout,
3124                    SCpnt->internal_timeout,
3125                    SCpnt->cmnd[0],
3126                    SCpnt->sense_buffer[2],
3127                    SCpnt->result);
3128         }
3129     printk("wait_for_request = %p\n", wait_for_request);
3130     /* Now dump the request lists for each block device */
3131     printk("Dump of pending block device requests\n");
3132     for(i=0; i<MAX_BLKDEV; i++)
3133         if(blk_dev[i].current_request)
3134         {
3135             struct request * req;
3136             printk("%d: ", i);
3137             req = blk_dev[i].current_request;
3138             while(req) {
3139                 printk("(%s %d %ld %ld %ld) ",
3140                        kdevname(req->rq_dev),
3141                        req->cmd,
3142                        req->sector,
3143                        req->nr_sectors,
3144                        req->current_nr_sectors);
3145                 req = req->next;
3146             }
3147             printk("\n");
3148         }
3149 }
3150 #endif
3151 
3152 #ifdef MODULE
3153 
3154 int init_module(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
3155     unsigned long size;
3156 
3157     /*
3158      * This makes /proc/scsi visible.
3159      */
3160     dispatch_scsi_info_ptr = dispatch_scsi_info;
3161 
3162     timer_table[SCSI_TIMER].fn = scsi_main_timeout;
3163     timer_table[SCSI_TIMER].expires = 0;
3164     register_symtab(&scsi_symbol_table);
3165     scsi_loadable_module_flag = 1;
3166 
3167     /* Register the /proc/scsi/scsi entry */
3168 #if CONFIG_PROC_FS
3169     proc_scsi_register(0, &proc_scsi_scsi);
3170 #endif
3171 
3172     
3173     dma_sectors = PAGE_SIZE / SECTOR_SIZE;
3174     dma_free_sectors= dma_sectors;
3175     /*
3176      * Set up a minimal DMA buffer list - this will be used during scan_scsis
3177      * in some cases.
3178      */
3179     
3180     /* One bit per sector to indicate free/busy */
3181     size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
3182     dma_malloc_freelist = (unsigned char *) scsi_init_malloc(size, GFP_ATOMIC);
3183     memset(dma_malloc_freelist, 0, size);
3184 
3185     /* One pointer per page for the page list */
3186     dma_malloc_pages = (unsigned char **)
3187         scsi_init_malloc((dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages), GFP_ATOMIC);
3188     dma_malloc_pages[0] = (unsigned char *)
3189         scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
3190     return 0;
3191 }
3192 
3193 void cleanup_module( void) 
     /* [previous][next][first][last][top][bottom][index][help] */
3194 {
3195 #if CONFIG_PROC_FS
3196     proc_scsi_unregister(0, PROC_SCSI_SCSI);
3197 #endif
3198 
3199     /* No, we're not here anymore. Don't show the /proc/scsi files. */
3200     dispatch_scsi_info_ptr = 0L;
3201 
3202     /*
3203      * Free up the DMA pool.
3204      */
3205     resize_dma_pool();
3206 
3207     timer_table[SCSI_TIMER].fn = NULL;
3208     timer_table[SCSI_TIMER].expires = 0;
3209 }
3210 #endif /* MODULE */
3211 
3212 /*
3213  * Overrides for Emacs so that we follow Linus's tabbing style.
3214  * Emacs will notice this stuff at the end of the file and automatically
3215  * adjust the settings for this buffer only.  This must remain at the end
3216  * of the file.
3217  * ---------------------------------------------------------------------------
3218  * Local variables:
3219  * c-indent-level: 4
3220  * c-brace-imaginary-offset: 0
3221  * c-brace-offset: -4
3222  * c-argdecl-indent: 4
3223  * c-label-offset: -4
3224  * c-continued-statement-offset: 4
3225  * c-continued-brace-offset: 0
3226  * indent-tabs-mode: nil
3227  * tab-width: 8
3228  * End:
3229  */

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