root/kernel/blk_drv/scsi/scsi.c

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

DEFINITIONS

This source file includes following definitions.
  1. scan_scsis_done
  2. scan_scsis
  3. scsi_times_out
  4. internal_cmnd
  5. scsi_request_sense
  6. scsi_do_cmd
  7. reset
  8. check_sense
  9. scsi_done
  10. scsi_abort
  11. scsi_reset
  12. scsi_main_timeout
  13. update_timeout
  14. scsi_dev_init
  15. print_inquiry

   1 /*
   2  *      scsi.c Copyright (C) 1992 Drew Eckhardt 
   3  *      generic mid-level SCSI driver by
   4  *              Drew Eckhardt 
   5  *
   6  *      <drew@colorado.edu>
   7  *
   8  *      Bug correction thanks go to : 
   9  *              Rik Faith <faith@cs.unc.edu>
  10  *              Tommy Thorn <tthorn>
  11  *              Thomas Wuensche <tw@fgb1.fgb.mw.tu-meunchen.de>
  12  */
  13 
  14 #include <linux/config.h>
  15 
  16 #ifdef CONFIG_SCSI
  17 #include <asm/system.h>
  18 #include <linux/sched.h>
  19 #include <linux/timer.h>
  20 #include <linux/string.h>
  21 
  22 #include "scsi.h"
  23 #include "hosts.h"
  24 
  25 #ifdef CONFIG_BLK_DEV_SD
  26 #include "sd.h"
  27 #endif
  28 
  29 #ifdef CONFIG_BLK_DEV_ST
  30 #include "st.h"
  31 #endif
  32 
  33 #ifdef CONFIG_BLK_DEV_SR
  34 #include "sr.h"
  35 #endif
  36 
  37 /*
  38 static const char RCSid[] = "$Header: /usr/src/linux/kernel/blk_drv/scsi/RCS/scsi.c,v 1.1 1992/07/24 06:27:38 root Exp root $";
  39 */
  40 
  41 #define INTERNAL_ERROR (printk ("Internal error in file %s, line %d.\n", __FILE__, __LINE__), panic(""))
  42 
  43 static void scsi_done (int host, int result);
  44 static void update_timeout (void);
  45 static void print_inquiry(unsigned char *data);
  46 
  47 static int time_start;
  48 static int time_elapsed;
  49 
  50 /*
  51         global variables : 
  52         NR_SCSI_DEVICES is the number of SCSI devices we have detected, 
  53         scsi_devices an array of these specifing the address for each 
  54         (host, id, LUN)
  55 */
  56         
  57 int NR_SCSI_DEVICES=0;
  58 Scsi_Device scsi_devices[MAX_SCSI_DEVICE];
  59 
  60 #define SENSE_LENGTH 255
  61 /*
  62  *      As the scsi do command functions are inteligent, and may need to 
  63  *      redo a command, we need to keep track of the last command 
  64  *      executed on each one.
  65  */
  66 
  67 #define WAS_RESET       0x01
  68 #define WAS_TIMEDOUT    0x02
  69 #define WAS_SENSE       0x04
  70 #define IS_RESETTING    0x08
  71 
  72 static Scsi_Cmnd last_cmnd[MAX_SCSI_HOSTS];
  73 static int last_reset[MAX_SCSI_HOSTS];
  74 
  75 /*
  76  *      This is the number  of clock ticks we should wait before we time out 
  77  *      and abort the command.  This is for  where the scsi.c module generates 
  78  *      the command, not where it originates from a higher level, in which
  79  *      case the timeout is specified there.
  80  *
  81  *      ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
  82  *      respectively.
  83  */
  84 
  85 #ifdef DEBUG
  86         #define SCSI_TIMEOUT 500
  87 #else
  88         #define SCSI_TIMEOUT 100
  89 #endif
  90 
  91 #ifdef DEBUG
  92         #define SENSE_TIMEOUT SCSI_TIMEOUT
  93         #define ABORT_TIMEOUT SCSI_TIMEOUT
  94         #define RESET_TIMEOUT SCSI_TIMEOUT
  95 #else
  96         #define SENSE_TIMEOUT 50
  97         #define RESET_TIMEOUT 50
  98         #define ABORT_TIMEOUT 50
  99         #define MIN_RESET_DELAY 25
 100 #endif
 101 
 102 /*
 103  *      As the actual SCSI command runs in the background, we must set up a 
 104  *      flag that tells scan_scsis() when the result it has is valid.  
 105  *      scan_scsis can set the_result to -1, and watch for it to become the 
 106  *      actual return code for that call.  the scan_scsis_done function() is 
 107  *      our user specified completion function that is passed on to the  
 108  *      scsi_do_cmd() function.
 109  */
 110 
 111 volatile static int in_scan = 0;
 112 static int the_result;
 113 static unsigned char sense_buffer[SENSE_LENGTH];
 114 static void scan_scsis_done (int host, int result)
     /* [previous][next][first][last][top][bottom][index][help] */
 115         {
 116         
 117 #ifdef DEBUG
 118         printk ("scan_scsis_done(%d, %06x)\n\r", host, result);
 119 #endif  
 120         the_result = result;
 121         }
 122 /*
 123  *      Detecting SCSI devices :        
 124  *      We scan all present host adapter's busses,  from ID 0 to ID 6.  
 125  *      We use the INQUIRY command, determine device type, and pass the ID / 
 126  *      lun address of all sequential devices to the tape driver, all random 
 127  *      devices to the disk driver.
 128  */
 129 
 130 static void scan_scsis (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 131         {
 132         int host_nr , dev, lun, type, maxed, slave;
 133         static unsigned char scsi_cmd [12];
 134         static unsigned char scsi_result [256];
 135 
 136         ++in_scan;
 137 
 138         for (slave = host_nr = 0; host_nr < MAX_SCSI_HOSTS; ++host_nr, 
 139              slave = 0)
 140                 if (scsi_hosts[host_nr].present)
 141                         {
 142                         for (dev = 0; dev < 7; ++dev)
 143                                 if (scsi_hosts[host_nr].this_id != dev)
 144                                 #ifdef MULTI_LUN
 145                                 for (lun = 0; lun < 8; ++lun)
 146                                         {
 147                                 #else
 148                                         {
 149                                         lun = 0;
 150                                 #endif
 151 /*
 152  * Build an INQUIRY command block.  
 153  */
 154 
 155                                         scsi_cmd[0] = INQUIRY;
 156                                         scsi_cmd[1] = (lun << 5) & 0xe0;
 157                                         scsi_cmd[2] = 0;
 158                                         scsi_cmd[3] = 0;
 159                                         scsi_cmd[4] = 255;
 160                                         scsi_cmd[5] = 0;
 161                                         the_result = -1;        
 162 #ifdef DEBUG
 163                                         memset ((void *) scsi_result , 0, 255);
 164 #endif 
 165                                         scsi_do_cmd (host_nr, dev, (void *)  scsi_cmd, (void *)                                                             
 166                                                  scsi_result, 256,  scan_scsis_done, 
 167                                                  SCSI_TIMEOUT, sense_buffer, 3);
 168                                         
 169 /*
 170  *      Wait for valid result 
 171  */
 172 
 173                                         while (the_result < 0);
 174 
 175                                         if (!the_result)
 176                                                 {
 177                                                 scsi_devices[NR_SCSI_DEVICES].
 178                                                         host_no = host_nr;
 179                                                 scsi_devices[NR_SCSI_DEVICES].
 180                                                         id = dev;
 181                                                 scsi_devices[NR_SCSI_DEVICES].
 182                                                         lun = lun;
 183                                                 scsi_devices[NR_SCSI_DEVICES].
 184                                                         removable = (0x80 & 
 185                                                         scsi_result[1]) >> 7;
 186                                                 scsi_devices[NR_SCSI_DEVICES].
 187                                                         changed = 0;
 188                                                 scsi_devices[NR_SCSI_DEVICES].
 189                                                         access_count = 0;
 190                                                 scsi_devices[NR_SCSI_DEVICES].
 191                                                         busy = 0;
 192 /* 
 193  *      Currently, all sequential devices are assumed to be tapes,
 194  *      all random devices disk, with the appropriate read only 
 195  *      flags set for ROM / WORM treated as RO.
 196  */ 
 197 
 198                                                 switch (type = scsi_result[0])
 199                                                 {
 200                                                 case TYPE_TAPE :
 201                                                 case TYPE_DISK :
 202                                                         scsi_devices[NR_SCSI_DEVICES].writeable = 1;
 203                                                         break;
 204                                                 case TYPE_WORM :
 205                                                 case TYPE_ROM :
 206                                                         scsi_devices[NR_SCSI_DEVICES].writeable = 0;
 207                                                         break;
 208                                                 default :
 209                                                         type = -1;
 210                                                 }
 211 
 212                                                 scsi_devices[NR_SCSI_DEVICES].random = (type == TYPE_TAPE) ? 0 : 1;
 213 
 214                                                 maxed = 0;
 215                                                 switch (type)
 216                                                 {
 217                                                 case -1 :
 218                                                         break;
 219                                                 case TYPE_TAPE :
 220 #ifdef DEBUG
 221                                                         printk("Detected scsi tape at host %d, ID  %d, lun %d \n", host_nr, dev, lun);
 222 #endif
 223 #ifdef CONFIG_BLK_DEV_ST
 224                                                         if (!(maxed = (NR_ST == MAX_ST)))
 225                                                                 scsi_tapes[NR_ST].device = &scsi_devices[NR_SCSI_DEVICES];
 226 #endif
 227                                                         break;
 228                                                         case TYPE_ROM:
 229                                                                 printk("Detected scsi CD-ROM at host %d, ID  %d, lun %d \n", host_nr, dev, lun);
 230 #ifdef CONFIG_BLK_DEV_SR
 231                                                                 if (!(maxed = (NR_SR >= MAX_SR)))
 232                                                                                 scsi_CDs[NR_SR].device = &scsi_devices[NR_SCSI_DEVICES];
 233 #endif
 234                                                                 break;
 235                                                 default :
 236 #ifdef DEBUG
 237                                                         printk("Detected scsi disk at host %d, ID  %d, lun %d \n", host_nr, dev, lun);
 238 #endif
 239 #ifdef CONFIG_BLK_DEV_SD
 240                                                         if (!(maxed = (NR_SD >= MAX_SD)))
 241                                                                 rscsi_disks[NR_SD].device = &scsi_devices[NR_SCSI_DEVICES];
 242 #endif
 243                                                 }
 244 
 245                                                 print_inquiry(scsi_result);
 246 
 247                                                 if (maxed)
 248                                                         {
 249                                                                 printk ("Already have detected "
 250                                                                         "maximum number of SCSI "
 251                                                                         "%ss Unable to \n"
 252                                                                         "add drive at SCSI host "
 253                                                                         "%s, ID %d, LUN %d\n\r", 
 254                                                                         (type == TYPE_TAPE) ?
 255                                                                              "tape" : 
 256                                                                         (type == TYPE_DISK) ?
 257                                                                              "disk" : "CD-ROM", 
 258                                                                         scsi_hosts[host_nr].name,
 259                                                                 dev, lun);
 260                                                         type = -1;
 261                                                         break;
 262                                                         }
 263 
 264                                                  else if (type != -1)
 265                                                         {
 266                                                         char *p;
 267                                                         char str[25]; 
 268 memcpy((void *) str, (void *) &scsi_result[8], 8);
 269 for (p = str; (p < (str  + 8)) && (*p != ' '); ++p);
 270 *p++ = ' ';
 271 memcpy((void *) p, (void *) &scsi_result[16], 16);
 272 for (; *p != ' '; ++p);
 273 *p = 0;
 274 
 275 printk("s%c%d at scsi%d, id %d, lun %d : %s\n",
 276         (type == TYPE_TAPE) ? 't' : ((type == TYPE_ROM) ? 'r' : 'd'),
 277         (type == TYPE_TAPE) ? 
 278 #ifdef CONFIG_BLK_DEV_ST
 279         NR_ST  
 280 #else 
 281         -1
 282 #endif
 283         : 
 284        (type == TYPE_ROM ? 
 285 #ifdef CONFIG_BLK_DEV_SR
 286         NR_SR
 287 #else
 288         -1      
 289 #endif
 290         :
 291 #ifdef CONFIG_BLK_DEV_SD
 292         NR_SD
 293 #else
 294         -1      
 295 #endif
 296         )
 297 
 298         ,host_nr , dev, lun, p); 
 299                                                         if (type == TYPE_TAPE)
 300 #ifdef CONFIG_BLK_DEV_ST
 301                                                                 ++NR_ST;
 302 #else
 303 ;
 304 #endif
 305 
 306                                                   else if (type == TYPE_DISK)
 307 #ifdef CONFIG_BLK_DEV_SD
 308                                                                 ++NR_SD;
 309 #else
 310 ;
 311 #endif
 312                                                                 else
 313 #ifdef CONFIG_BLK_DEV_SR
 314                                                                         ++NR_SR;
 315 #else
 316 ;
 317 #endif
 318                                                                 }
 319                                                         ++slave;
 320                                                         ++NR_SCSI_DEVICES;
 321                                                         }       /* if result == DID_OK ends */
 322                                         }       /* for lun ends */
 323                         }       /* if present */  
 324 
 325         printk("scsi : detected "
 326 #ifdef CONFIG_BLK_DEV_SD
 327         "%d SCSI disk%s "
 328 #endif
 329 
 330 #ifdef CONFIG_BLK_DEV_ST
 331         "%d tape%s "
 332 #endif
 333 
 334 #ifdef CONFIG_BLK_DEV_SR
 335 "%d CD-ROM drive%s "
 336 #endif
 337 
 338         "total.\n"  
 339 
 340 #ifdef CONFIG_BLK_DEV_SD
 341         , NR_SD, (NR_SD != 1) ? "s" : ""
 342 #endif
 343 
 344 #ifdef CONFIG_BLK_DEV_ST
 345         , NR_ST, (NR_ST != 1) ? "s" : ""
 346 #endif
 347 
 348 #ifdef CONFIG_BLK_DEV_SR
 349         , NR_SR, (NR_SR != 1) ? "s" : ""
 350 #endif
 351         );
 352         in_scan = 0;
 353         }       /* scan_scsis  ends */
 354 
 355 /*
 356  *      We handle the timeout differently if it happens when a reset, 
 357  *      abort, etc are in process. 
 358  */
 359 
 360 static unsigned char internal_timeout[MAX_SCSI_HOSTS];
 361 
 362 /*
 363  *      Flag bits for the internal_timeout array 
 364  */
 365 
 366 #define NORMAL_TIMEOUT 0
 367 #define IN_ABORT 1
 368 #define IN_RESET 2
 369 /*
 370         This is our time out function, called when the timer expires for a 
 371         given host adapter.  It will attempt to abort the currently executing 
 372         command, that failing perform a kernel panic.
 373 */ 
 374 
 375 static void scsi_times_out (int host)
     /* [previous][next][first][last][top][bottom][index][help] */
 376         {
 377         
 378         switch (internal_timeout[host] & (IN_ABORT | IN_RESET))
 379                 {
 380                 case NORMAL_TIMEOUT:
 381                         if (!in_scan)
 382                               printk("SCSI host %d timed out - aborting command \r\n",
 383                                 host);
 384                         
 385                         if (!scsi_abort (host, DID_TIME_OUT))
 386                                 return;                         
 387                 case IN_ABORT:
 388                         printk("SCSI host %d abort() timed out - reseting \r\n",
 389                                 host);
 390                         if (!scsi_reset (host)) 
 391                                 return;
 392                 case IN_RESET:
 393                 case (IN_ABORT | IN_RESET):
 394                         printk("Unable to reset scsi host %d\r\n",host);
 395                         panic("");
 396                 default:
 397                         INTERNAL_ERROR;
 398                 }
 399                                         
 400         }
 401 
 402 /*
 403         This is inline because we have stack problemes if we recurse to deeply.
 404 */
 405                          
 406 static void internal_cmnd (int host,  unsigned char target, const void *cmnd , 
     /* [previous][next][first][last][top][bottom][index][help] */
 407                   void *buffer, unsigned bufflen, void (*done)(int,int))
 408         {
 409         int temp;
 410 
 411 #ifdef DEBUG_DELAY      
 412         int clock;
 413 #endif
 414 
 415         if ((host < 0) ||  (host > MAX_SCSI_HOSTS))
 416                 panic ("Host number in internal_cmnd() is out of range.\n");
 417 
 418 
 419 /*
 420         We will wait MIN_RESET_DELAY clock ticks after the last reset so 
 421         we can avoid the drive not being ready.
 422 */ 
 423 temp = last_reset[host];
 424 while (jiffies < temp);
 425 
 426 host_timeout[host] = last_cmnd[host].timeout_per_command;
 427 update_timeout();
 428 
 429 /*
 430         We will use a queued command if possible, otherwise we will emulate the
 431         queing and calling of completion function ourselves. 
 432 */
 433 #ifdef DEBUG
 434         printk("internal_cmnd (host = %d, target = %d, command = %08x, buffer =  %08x, \n"
 435                 "bufflen = %d, done = %08x)\n", host, target, cmnd, buffer, bufflen, done);
 436 #endif
 437 
 438         if (scsi_hosts[host].can_queue)
 439                 {
 440 #ifdef DEBUG
 441         printk("queuecommand : routine at %08x\n", 
 442                 scsi_hosts[host].queuecommand);
 443 #endif
 444                 scsi_hosts[host].queuecommand (target, cmnd, buffer, bufflen, 
 445                                                done);
 446                 }
 447         else
 448                 {
 449 
 450 #ifdef DEBUG
 451         printk("command() :  routine at %08x\n", scsi_hosts[host].command);
 452 #endif
 453                 temp=scsi_hosts[host].command (target, cmnd, buffer, bufflen);
 454 
 455 #ifdef DEBUG_DELAY
 456         clock = jiffies + 400;
 457         while (jiffies < clock);
 458         printk("done(host = %d, result = %04x) : routine at %08x\n", host, temp, done);
 459 #endif
 460                 done(host, temp);
 461                 }       
 462 #ifdef DEBUG
 463         printk("leaving internal_cmnd()\n");
 464 #endif
 465         }       
 466 
 467 static void scsi_request_sense (int host, unsigned char target, 
     /* [previous][next][first][last][top][bottom][index][help] */
 468                                         unsigned char lun)
 469         {
 470         cli();
 471         host_timeout[host] = SENSE_TIMEOUT;
 472         update_timeout();
 473         last_cmnd[host].flags |= WAS_SENSE;
 474         sti();
 475         
 476         last_cmnd[host].sense_cmnd[1] = lun << 5;       
 477 
 478         internal_cmnd (host, target, (void *) last_cmnd[host].sense_cmnd, 
 479                        (void *) last_cmnd[host].sense_buffer, SENSE_LENGTH,
 480                        scsi_done);
 481         }
 482 
 483 
 484 
 485 
 486 
 487 /*
 488         scsi_do_cmd sends all the commands out to the low-level driver.  It 
 489         handles the specifics required for each low level driver - ie queued 
 490         or non queud.  It also prevents conflicts when different high level 
 491         drivers go for the same host at the same time.
 492 */
 493 
 494 void scsi_do_cmd (int host,  unsigned char target, const void *cmnd , 
     /* [previous][next][first][last][top][bottom][index][help] */
 495                   void *buffer, unsigned bufflen, void (*done)(int,int),
 496                   int timeout, unsigned  char *sense_buffer, int retries 
 497                    )
 498         {
 499         int ok = 0;
 500 
 501 #ifdef DEBUG
 502         int i;  
 503         printk ("scsi_do_cmd (host = %d, target = %d, buffer =%08x, "
 504                 "bufflen = %d, done = %08x, timeout = %d, retries = %d)\n"
 505                 "command : " , host, target, buffer, bufflen, done, timeout, retries);
 506         for (i = 0; i < 10; ++i)
 507                 printk ("%02x  ", ((unsigned char *) cmnd)[i]); 
 508         printk("\n");
 509 #endif
 510         
 511         if ((host  >= MAX_SCSI_HOSTS) || !scsi_hosts[host].present)
 512                 {
 513                 printk ("Invalid or not present host number. %d\n", host);
 514                 panic("");
 515                 }
 516 
 517         
 518 /*
 519         We must prevent reentrancy to the lowlevel host driver.  This prevents 
 520         it - we enter a loop until the host we want to talk to is not busy.   
 521         Race conditions are prevented, as interrupts are disabled inbetween the
 522         time we check for the host being not busy, and the time we mark it busy
 523         ourselves.
 524 */
 525 
 526         do      {
 527                 cli();
 528                 if (host_busy[host])
 529                         {
 530                         sti();
 531 #ifdef DEBUG
 532                         printk("Host %d is busy.\n", host);
 533 #endif
 534                         while (host_busy[host]);
 535 #ifdef DEBUG
 536                         printk("Host %d is no longer busy.\n", host);
 537 #endif
 538                         }
 539                 else
 540                         {
 541                         host_busy[host] = 1;
 542                         ok = 1;
 543                         sti();
 544                         }
 545                 } while (!ok);
 546                 
 547 
 548 /*
 549         Our own function scsi_done (which marks the host as not busy, disables 
 550         the timeout counter, etc) will be called by us or by the 
 551         scsi_hosts[host].queuecommand() function needs to also call
 552         the completion function for the high level driver.
 553 
 554 */
 555 
 556         memcpy ((void *) last_cmnd[host].cmnd , (void *) cmnd, 10);
 557         last_cmnd[host].host = host;
 558         last_cmnd[host].target = target;
 559         last_cmnd[host].lun = (last_cmnd[host].cmnd[1] >> 5);
 560         last_cmnd[host].bufflen = bufflen;
 561         last_cmnd[host].buffer = buffer;
 562         last_cmnd[host].sense_buffer = sense_buffer;
 563         last_cmnd[host].flags=0;
 564         last_cmnd[host].retries=0;
 565         last_cmnd[host].allowed=retries;
 566         last_cmnd[host].done = done;
 567         last_cmnd[host].timeout_per_command = timeout;
 568                                 
 569         /* Start the timer ticking.  */
 570 
 571         internal_timeout[host] = 0;
 572         internal_cmnd (host,  target, cmnd , buffer, bufflen, scsi_done);
 573 
 574 #ifdef DEBUG
 575         printk ("Leaving scsi_do_cmd()\n");
 576 #endif
 577         }
 578 
 579 
 580 /*
 581         The scsi_done() function disables the timeout timer for the scsi host, 
 582         marks the host as not busy, and calls the user specified completion 
 583         function for that host's current command.
 584 */
 585 
 586 static void reset (int host)
     /* [previous][next][first][last][top][bottom][index][help] */
 587         {
 588         #ifdef DEBUG
 589                 printk("reset(%d)\n", host);
 590         #endif
 591 
 592         last_cmnd[host].flags |= (WAS_RESET | IS_RESETTING);
 593         scsi_reset(host);
 594 
 595         #ifdef DEBUG
 596                 printk("performing request sense\n");
 597         #endif
 598 
 599         scsi_request_sense (host, last_cmnd[host].target, last_cmnd[host].lun);
 600         }
 601         
 602         
 603 
 604 static int check_sense (int host)
     /* [previous][next][first][last][top][bottom][index][help] */
 605         {
 606         if (((last_cmnd[host].sense_buffer[0] & 0x70) >> 4) == 7)
 607                 switch (last_cmnd[host].sense_buffer[2] & 0xf)
 608                 {
 609                 case NO_SENSE:
 610                 case RECOVERED_ERROR:
 611                         return 0;
 612 
 613                 case ABORTED_COMMAND:
 614                 case NOT_READY:
 615                         return SUGGEST_RETRY;   
 616                 case UNIT_ATTENTION:
 617                         return SUGGEST_ABORT;
 618 
 619                 /* these three are not supported */     
 620                 case COPY_ABORTED:
 621                 case VOLUME_OVERFLOW:
 622                 case MISCOMPARE:
 623         
 624                 case MEDIUM_ERROR:
 625                         return SUGGEST_REMAP;
 626                 case BLANK_CHECK:
 627                 case DATA_PROTECT:
 628                 case HARDWARE_ERROR:
 629                 case ILLEGAL_REQUEST:
 630                 default:
 631                         return SUGGEST_ABORT;
 632                 }
 633         else
 634                 return SUGGEST_RETRY;   
 635         }       
 636 
 637 /* This function is the mid-level interrupt routine, which decides how
 638  *  to handle error conditions.  Each invocation of this function must
 639  *  do one and *only* one of the following:
 640  *
 641  *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
 642  *      normal completion, and indicates that the handling for this
 643  *      request is complete.
 644  *  (2) Call internal_cmnd to requeue the command.  This will result in
 645  *      scsi_done being called again when the retry is complete.
 646  *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
 647  *      more information about the error condition.  When the information
 648  *      is available, scsi_done will be called again.
 649  *  (4) Call reset().  This is sort of a last resort, and the idea is that
 650  *      this may kick things loose and get the drive working again.  reset()
 651  *      automatically calls scsi_request_sense, and thus scsi_done will be
 652  *      called again once the reset is complete.
 653  *
 654  *      If none of the above actions are taken, the drive in question
 655  * will hang. If more than one of the above actions are taken by
 656  * scsi_done, then unpredictable behavior will result.
 657  */
 658 static void scsi_done (int host, int result)
     /* [previous][next][first][last][top][bottom][index][help] */
 659         {
 660         int status=0;
 661         int exit=0;
 662         int checked;
 663         int oldto;
 664         oldto = host_timeout[host];
 665         host_timeout[host] = 0;
 666         update_timeout();
 667 
 668 #define FINISHED 0
 669 #define MAYREDO  1
 670 #define REDO     3
 671 #define PENDING  4
 672 
 673 #ifdef DEBUG
 674         printk("In scsi_done(host = %d, result = %06x)\n", host, result);
 675 #endif
 676         if (host > MAX_SCSI_HOSTS || host  < 0) 
 677                 {
 678                 host_timeout[host] = 0;
 679                 update_timeout();
 680                 panic("scsi_done() called with invalid host number.\n");
 681                 }
 682 
 683         switch (host_byte(result))      
 684         {
 685         case DID_OK:
 686                 if (last_cmnd[host].flags & IS_RESETTING)
 687                         {
 688                         last_cmnd[host].flags &= ~IS_RESETTING;
 689                         status = REDO;
 690                         break;
 691                         }
 692 
 693                 if (status_byte(result) && (last_cmnd[host].flags & 
 694                     WAS_SENSE)) 
 695                         {
 696                         last_cmnd[host].flags &= ~WAS_SENSE;
 697                         cli();
 698                         internal_timeout[host] &= ~SENSE_TIMEOUT;
 699                         sti();
 700 
 701                         if (!(last_cmnd[host].flags & WAS_RESET))
 702                                 {
 703                                 reset(host);
 704                                 return;
 705                                 }
 706                         else
 707                                 {
 708                                 exit = (DRIVER_HARD | SUGGEST_ABORT);
 709                                 status = FINISHED;
 710                                 }
 711                         }
 712                 else switch(msg_byte(result))
 713                         {
 714                         case COMMAND_COMPLETE:
 715                         switch (status_byte(result))
 716                         {
 717                         case GOOD:
 718                                 if (last_cmnd[host].flags & WAS_SENSE)
 719                                         {
 720 #ifdef DEBUG
 721         printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
 722 #endif
 723 
 724                                         last_cmnd[host].flags &= ~WAS_SENSE;
 725                                         cli();
 726                                         internal_timeout[host] &= ~SENSE_TIMEOUT;
 727                                         sti();
 728         
 729                                         switch (checked = check_sense(host))
 730                                         {
 731                                         case 0: 
 732 #ifdef DEBUG
 733         printk("NO SENSE.  status = REDO\n");
 734 #endif
 735 
 736                                                 host_timeout[host] = oldto;
 737                                                 update_timeout();
 738                                                 status = REDO;
 739                                                 break;
 740                                         case SUGGEST_REMAP:                     
 741                                         case SUGGEST_RETRY: 
 742 #ifdef DEBUG
 743         printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
 744 #endif
 745 
 746                                                 status = MAYREDO;
 747                                                 exit = SUGGEST_RETRY;
 748                                                 break;
 749                                         case SUGGEST_ABORT:
 750 #ifdef DEBUG
 751         printk("SENSE SUGGEST ABORT - status = FINISHED");
 752 #endif
 753 
 754                                                 status = FINISHED;
 755                                                 exit =  DRIVER_SENSE;
 756                                                 break;
 757                                         default:
 758                                                 printk ("Internal error %s %s \n", __FILE__, 
 759                                                         __LINE__);
 760                                         }                          
 761                                         }       
 762                                 else
 763                                         {
 764 #ifdef DEBUG
 765         printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
 766 #endif
 767 
 768                                         exit =  DRIVER_OK;
 769                                         status = FINISHED;
 770                                         }
 771                                 break;  
 772 
 773                         case CHECK_CONDITION:
 774 
 775 #ifdef DEBUG
 776         printk("CHECK CONDITION message returned, performing request sense.\n");
 777 #endif
 778 
 779                                 scsi_request_sense (host, last_cmnd[host].target, last_cmnd[host].lun);
 780                                 status = PENDING;
 781                                 break;          
 782                         
 783                         case CONDITION_GOOD:
 784                         case INTERMEDIATE_GOOD:
 785                         case INTERMEDIATE_C_GOOD:
 786 #ifdef DEBUG
 787         printk("CONDITION GOOD, INTERMEDIATE GOOD, or INTERMEDIATE CONDITION GOOD recieved and ignored. \n");
 788 #endif
 789                                 break;
 790                                 
 791                         case BUSY:
 792 #ifdef DEBUG
 793         printk("BUSY message returned, performing REDO");
 794 #endif
 795                                 host_timeout[host] = oldto;
 796                                 update_timeout();
 797                                 status = REDO;
 798                                 break;
 799 
 800                         case RESERVATION_CONFLICT:
 801                                 reset(host);
 802                                 return;
 803 #if 0
 804                                 exit = DRIVER_SOFT | SUGGEST_ABORT;
 805                                 status = MAYREDO;
 806                                 break;
 807 #endif
 808                         default:
 809                                 printk ("Internal error %s %s \n"
 810                                         "status byte = %d \n", __FILE__, 
 811                                         __LINE__, status_byte(result));
 812                                 
 813                         }
 814                         break;
 815                         default:
 816                                 panic ("unsupported message byte recieved.");
 817                         }
 818                         break;
 819         case DID_TIME_OUT:      
 820 #ifdef DEBUG
 821         printk("Host returned DID_TIME_OUT - ");
 822 #endif
 823 
 824                 if (last_cmnd[host].flags & WAS_TIMEDOUT)       
 825                         {
 826 #ifdef DEBUG
 827         printk("Aborting\n");
 828 #endif  
 829                         exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
 830                         }               
 831                 else 
 832                         {
 833 #ifdef DEBUG
 834                         printk ("Retrying.\n");
 835 #endif
 836                         last_cmnd[host].flags  |= WAS_TIMEDOUT;
 837                         status = REDO;
 838                         }
 839                 break;
 840         case DID_BUS_BUSY:
 841         case DID_PARITY:
 842                 status = REDO;
 843                 break;
 844         case DID_NO_CONNECT:
 845 #ifdef DEBUG
 846                 printk("Couldn't connect.\n");
 847 #endif
 848                 exit  = (DRIVER_HARD | SUGGEST_ABORT);
 849                 break;
 850         case DID_ERROR: 
 851                 status = MAYREDO;
 852                 exit = (DRIVER_HARD | SUGGEST_ABORT);
 853                 break;
 854         case DID_BAD_TARGET:
 855         case DID_ABORT:
 856                 exit = (DRIVER_INVALID | SUGGEST_ABORT);
 857                 break;  
 858         default :               
 859                 exit = (DRIVER_ERROR | SUGGEST_DIE);
 860         }
 861 
 862         switch (status) 
 863                 {
 864                 case FINISHED:
 865                 case PENDING:
 866                         break;
 867                 case MAYREDO:
 868 
 869 #ifdef DEBUG
 870         printk("In MAYREDO, allowing %d retries, have %d\n\r",
 871                last_cmnd[host].allowed, last_cmnd[host].retries);
 872 #endif
 873 
 874                         if ((++last_cmnd[host].retries) < last_cmnd[host].allowed)
 875                         {
 876                         if ((last_cmnd[host].retries >= (last_cmnd[host].allowed >> 1))
 877                             && !(last_cmnd[host].flags & WAS_RESET))
 878                                 {
 879                                         reset(host);
 880                                         break;
 881                                 }
 882 
 883                         }
 884                         else
 885                                 {
 886                                 status = FINISHED;
 887                                 break;
 888                                 }
 889                         /* fall through to REDO */
 890 
 891                 case REDO:
 892                         if (last_cmnd[host].flags & WAS_SENSE)                  
 893                                 scsi_request_sense (host, last_cmnd[host].target,       
 894                                last_cmnd[host].lun);    
 895                         else    
 896                                 internal_cmnd (host, last_cmnd[host].target,    
 897                                 last_cmnd[host].cmnd,  
 898                                 last_cmnd[host].buffer,   
 899                                 last_cmnd[host].bufflen, scsi_done);                    
 900                         break;  
 901                 default: 
 902                         INTERNAL_ERROR;
 903                 }
 904 
 905         if (status == FINISHED) 
 906                 {
 907                 #ifdef DEBUG
 908                         printk("Calling done function - at address %08x\n", last_cmnd[host].done);
 909                 #endif
 910                 host_busy[host] = 0;
 911                 last_cmnd[host].done (host, (result | ((exit & 0xff) << 24)));
 912                 }
 913 
 914 
 915 #undef FINISHED
 916 #undef REDO
 917 #undef MAYREDO
 918 #undef PENDING
 919         }
 920 
 921 /*
 922         The scsi_abort function interfaces with the abort() function of the host
 923         we are aborting, and causes the current command to not complete.  The 
 924         caller should deal with any error messages or status returned on the 
 925         next call.
 926         
 927         This will not be called rentrantly for a given host.
 928 */
 929         
 930 /*
 931         Since we're nice guys and specified that abort() and reset()
 932         can be non-reentrant.  The internal_timeout flags are used for
 933         this.
 934 */
 935 
 936 
 937 int scsi_abort (int host, int why)
     /* [previous][next][first][last][top][bottom][index][help] */
 938         {
 939         int temp, oldto;
 940         
 941         while(1)        
 942                 {
 943                 cli();
 944                 if (internal_timeout[host] & IN_ABORT) 
 945                         {
 946                         sti();
 947                         while (internal_timeout[host] & IN_ABORT);
 948                         }
 949                 else
 950                         {       
 951                         oldto = host_timeout[host];
 952                         internal_timeout[host] |= IN_ABORT;
 953                         host_timeout[host] = ABORT_TIMEOUT;     
 954                         update_timeout();
 955 
 956                         
 957                         sti();
 958                         if (!host_busy[host] || !scsi_hosts[host].abort(why))
 959                                 temp =  0;
 960                         else
 961                                 temp = 1;
 962                         
 963                         cli();
 964                         internal_timeout[host] &= ~IN_ABORT;
 965                         host_timeout[host]=oldto;
 966                         update_timeout();
 967                         sti();
 968                         return temp;
 969                         }
 970                 }       
 971         }
 972 
 973 int scsi_reset (int host)
     /* [previous][next][first][last][top][bottom][index][help] */
 974         {
 975         int temp, oldto;
 976         
 977         while (1) {
 978                 cli();  
 979                 if (internal_timeout[host] & IN_RESET)
 980                         {
 981                         sti();
 982                         while (internal_timeout[host] & IN_RESET);
 983                         }
 984                 else
 985                         {
 986                         oldto = host_timeout[host];     
 987                         host_timeout[host] = RESET_TIMEOUT;     
 988                         update_timeout();       
 989                         internal_timeout[host] |= IN_RESET;
 990                                         
 991                         if (host_busy[host])
 992                                 {       
 993                                 sti();
 994                                 if (!(last_cmnd[host].flags & IS_RESETTING) && !(internal_timeout[host] & IN_ABORT))
 995                                         scsi_abort(host, DID_RESET);
 996 
 997                                 temp = scsi_hosts[host].reset();                        
 998                                 }                               
 999                         else
1000                                 {
1001                                 host_busy[host]=1;
1002         
1003                                 sti();
1004                                 temp = scsi_hosts[host].reset();
1005                                 last_reset[host] = jiffies;
1006                                 host_busy[host]=0;
1007                                 }
1008         
1009                         cli();
1010                         host_timeout[host] = oldto;             
1011                         update_timeout();
1012                         internal_timeout[host] &= ~IN_RESET;
1013                         sti();
1014                         return temp;    
1015                         }
1016                 }
1017         }
1018                          
1019 
1020 static void scsi_main_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1021         {
1022         /*
1023                 We must not enter update_timeout with a timeout condition still pending.
1024         */
1025 
1026         int i, timed_out;
1027 
1028         do      {       
1029                 cli();
1030 
1031         /*
1032                 Find all timers such that they have 0 or negative (shouldn't happen)
1033                 time remaining on them.
1034         */
1035                         
1036                 for (i = timed_out = 0; i < MAX_SCSI_HOSTS; ++i)
1037                         if (host_timeout[i] != 0 && host_timeout[i] <= time_elapsed)
1038                                 {
1039                                 sti();
1040                                 host_timeout[i] = 0;
1041                                 scsi_times_out(i);
1042                                 ++timed_out; 
1043                                 }
1044 
1045                 update_timeout();                               
1046                 } while (timed_out);    
1047         sti();
1048         }
1049 
1050 /*
1051         These are used to keep track of things. 
1052 */
1053 
1054 static int time_start, time_elapsed;
1055 
1056 /*
1057         The strategy is to cause the timer code to call scsi_times_out()
1058         when the soonest timeout is pending.  
1059 */
1060         
1061 static void update_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1062         {
1063         unsigned int i, least, used;
1064 
1065         cli();
1066 
1067 /* 
1068         Figure out how much time has passed since the last time the timeouts 
1069         were updated 
1070 */
1071         used = (time_start) ? (jiffies - time_start) : 0;
1072 
1073 /*
1074         Find out what is due to timeout soonest, and adjust all timeouts for
1075         the amount of time that has passed since the last time we called 
1076         update_timeout. 
1077 */
1078         
1079         for (i = 0, least = 0xffffffff; i < MAX_SCSI_HOSTS; ++i)        
1080                 if (host_timeout[i] > 0 && (host_timeout[i] -= used) < least)
1081                         least = host_timeout[i]; 
1082 
1083 /*
1084         If something is due to timeout again, then we will set the next timeout 
1085         interrupt to occur.  Otherwise, timeouts are disabled.
1086 */
1087         
1088         if (least != 0xffffffff)
1089                 {
1090                 time_start = jiffies;   
1091                 timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;     
1092                 timer_active |= 1 << SCSI_TIMER;
1093                 }
1094         else
1095                 {
1096                 timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
1097                 timer_active &= ~(1 << SCSI_TIMER);
1098                 }       
1099         sti();
1100         }               
1101 /*
1102         scsi_dev_init() is our initialization routine, which inturn calls host 
1103         initialization, bus scanning, and sd/st initialization routines.  It 
1104         should be called from main().
1105 */
1106 
1107 static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};         
1108 unsigned long scsi_dev_init (unsigned long memory_start,unsigned long memory_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1109         {
1110         int i;
1111 #ifdef FOO_ON_YOU
1112         return;
1113 #endif  
1114         timer_table[SCSI_TIMER].fn = scsi_main_timeout;
1115         timer_table[SCSI_TIMER].expires = 0;
1116 
1117         scsi_init();            /* initialize all hosts */
1118 /*
1119  *      Set up sense command in each host structure.
1120  */
1121 
1122         for (i = 0; i < MAX_SCSI_HOSTS; ++i)
1123                 {
1124                 memcpy ((void *) last_cmnd[i].sense_cmnd, (void *) generic_sense,
1125                         6);
1126                 last_reset[i] = 0;
1127                 }
1128                                 
1129         scan_scsis();           /* scan for scsi devices */
1130 
1131 #ifdef CONFIG_BLK_DEV_SD
1132         memory_start = sd_init(memory_start, memory_end);              /* init scsi disks */
1133 #endif
1134 
1135 #ifdef CONFIG_BLK_DEV_ST
1136         memory_start = st_init(memory_start, memory_end);              /* init scsi tapes */
1137 #endif
1138 
1139 #ifdef CONFIG_BLK_DEV_SR
1140         memory_start = sr_init(memory_start, memory_end);
1141 #endif
1142         return memory_start;
1143         }
1144 #endif
1145 
1146 static void print_inquiry(unsigned char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
1147 {
1148         int i;
1149 
1150         printk("  Vendor:");
1151         for (i = 8; i < 15; i++)
1152                 {
1153                 if (data[i] >= 20)
1154                         printk("%c", data[i]);
1155                 else
1156                         printk(" ");
1157                 }
1158 
1159         printk("  Model:");
1160         for (i = 16; i < 31; i++)
1161                 {
1162                 if (data[i] >= 20)
1163                         printk("%c", data[i]);
1164                 else
1165                         printk(" ");
1166                 }
1167 
1168         printk("  Rev:");
1169         for (i = 32; i < 35; i++)
1170                 {
1171                 if (data[i] >= 20)
1172                         printk("%c", data[i]);
1173                 else
1174                         printk(" ");
1175                 }
1176 
1177         printk("\n");
1178 
1179         i = data[0] & 0x1f;
1180 
1181         printk("  Type: %s ",   i == 0x00 ? "Direct-Access    " :
1182                                 i == 0x01 ? "Sequential-Access" :
1183                                 i == 0x02 ? "Printer          " :
1184                                 i == 0x03 ? "Processor        " :
1185                                 i == 0x04 ? "WORM             " :
1186                                 i == 0x05 ? "CD-ROM           " :
1187                                 i == 0x06 ? "Scanner          " :
1188                                 i == 0x07 ? "Optical Device   " :
1189                                 i == 0x08 ? "Medium Changer   " :
1190                                 i == 0x09 ? "Communications   " :
1191                                             "Unknown          " );
1192         printk("ANSI SCSI revision: %02x\n", data[2] & 0x07);
1193 }
1194 

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