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

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