scsi_obsolete.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:33k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  scsi_obsolete.c Copyright (C) 1992 Drew Eckhardt
  3.  *         Copyright (C) 1993, 1994, 1995 Eric Youngdale
  4.  *
  5.  *  generic mid-level SCSI driver
  6.  *      Initial versions: Drew Eckhardt
  7.  *      Subsequent revisions: Eric Youngdale
  8.  *
  9.  *  <drew@colorado.edu>
  10.  *
  11.  *  Bug correction thanks go to :
  12.  *      Rik Faith <faith@cs.unc.edu>
  13.  *      Tommy Thorn <tthorn>
  14.  *      Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
  15.  *
  16.  *  Modified by Eric Youngdale eric@andante.org to
  17.  *  add scatter-gather, multiple outstanding request, and other
  18.  *  enhancements.
  19.  *
  20.  *  Native multichannel, wide scsi, /proc/scsi and hot plugging
  21.  *  support added by Michael Neuffer <mike@i-connect.net>
  22.  *
  23.  *  Major improvements to the timeout, abort, and reset processing,
  24.  *  as well as performance modifications for large queue depths by
  25.  *  Leonard N. Zubkoff <lnz@dandelion.com>
  26.  *
  27.  *  Improved compatibility with 2.0 behaviour by Manfred Spraul
  28.  *  <masp0008@stud.uni-sb.de>
  29.  */
  30. /*
  31.  *#########################################################################
  32.  *#########################################################################
  33.  *#########################################################################
  34.  *#########################################################################
  35.  *              NOTE - NOTE - NOTE - NOTE - NOTE - NOTE - NOTE
  36.  *
  37.  *#########################################################################
  38.  *#########################################################################
  39.  *#########################################################################
  40.  *#########################################################################
  41.  *
  42.  * This file contains the 'old' scsi error handling.  It is only present
  43.  * while the new error handling code is being debugged, and while the low
  44.  * level drivers are being converted to use the new code.  Once the last
  45.  * driver uses the new code this *ENTIRE* file will be nuked.
  46.  */
  47. #define __NO_VERSION__
  48. #include <linux/module.h>
  49. #include <linux/sched.h>
  50. #include <linux/timer.h>
  51. #include <linux/string.h>
  52. #include <linux/slab.h>
  53. #include <linux/ioport.h>
  54. #include <linux/kernel.h>
  55. #include <linux/stat.h>
  56. #include <linux/blk.h>
  57. #include <linux/interrupt.h>
  58. #include <linux/delay.h>
  59. #include <asm/system.h>
  60. #include <asm/irq.h>
  61. #include <asm/dma.h>
  62. #include "scsi.h"
  63. #include "hosts.h"
  64. #include "constants.h"
  65. #undef USE_STATIC_SCSI_MEMORY
  66. /*
  67.    static const char RCSid[] = "$Header: /mnt/ide/home/eric/CVSROOT/linux/drivers/scsi/scsi_obsolete.c,v 1.1 1997/05/18 23:27:21 eric Exp $";
  68.  */
  69. #define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.n", __FILE__, __LINE__))
  70. static int scsi_abort(Scsi_Cmnd *, int code);
  71. static int scsi_reset(Scsi_Cmnd *, unsigned int);
  72. extern void scsi_old_done(Scsi_Cmnd * SCpnt);
  73. int update_timeout(Scsi_Cmnd *, int);
  74. extern void scsi_old_times_out(Scsi_Cmnd * SCpnt);
  75. extern int scsi_dispatch_cmd(Scsi_Cmnd * SCpnt);
  76. #define SCSI_BLOCK(HOST) (HOST->can_queue && HOST->host_busy >= HOST->can_queue)
  77. static unsigned char generic_sense[6] =
  78. {REQUEST_SENSE, 0, 0, 0, 255, 0};
  79. /*
  80.  *  This is the number  of clock ticks we should wait before we time out
  81.  *  and abort the command.  This is for  where the scsi.c module generates
  82.  *  the command, not where it originates from a higher level, in which
  83.  *  case the timeout is specified there.
  84.  *
  85.  *  ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
  86.  *  respectively.
  87.  */
  88. #ifdef DEBUG_TIMEOUT
  89. static void scsi_dump_status(void);
  90. #endif
  91. #ifdef DEBUG
  92. #define SCSI_TIMEOUT (5*HZ)
  93. #else
  94. #define SCSI_TIMEOUT (2*HZ)
  95. #endif
  96. #ifdef DEBUG
  97. #define SENSE_TIMEOUT SCSI_TIMEOUT
  98. #define ABORT_TIMEOUT SCSI_TIMEOUT
  99. #define RESET_TIMEOUT SCSI_TIMEOUT
  100. #else
  101. #define SENSE_TIMEOUT (5*HZ/10)
  102. #define RESET_TIMEOUT (5*HZ/10)
  103. #define ABORT_TIMEOUT (5*HZ/10)
  104. #endif
  105. /* Do not call reset on error if we just did a reset within 15 sec. */
  106. #define MIN_RESET_PERIOD (15*HZ)
  107. /*
  108.  *  Flag bits for the internal_timeout array
  109.  */
  110. #define IN_ABORT  1
  111. #define IN_RESET  2
  112. #define IN_RESET2 4
  113. #define IN_RESET3 8
  114. /*
  115.  * This is our time out function, called when the timer expires for a
  116.  * given host adapter.  It will attempt to abort the currently executing
  117.  * command, that failing perform a kernel panic.
  118.  */
  119. void scsi_old_times_out(Scsi_Cmnd * SCpnt)
  120. {
  121. unsigned long flags;
  122. spin_lock_irqsave(&io_request_lock, flags);
  123. /* Set the serial_number_at_timeout to the current serial_number */
  124. SCpnt->serial_number_at_timeout = SCpnt->serial_number;
  125. switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET | IN_RESET2 | IN_RESET3)) {
  126. case NORMAL_TIMEOUT:
  127. {
  128. #ifdef DEBUG_TIMEOUT
  129. scsi_dump_status();
  130. #endif
  131. }
  132. if (!scsi_abort(SCpnt, DID_TIME_OUT))
  133. break;
  134. case IN_ABORT:
  135. printk("SCSI host %d abort (pid %ld) timed out - resettingn",
  136.        SCpnt->host->host_no, SCpnt->pid);
  137. if (!scsi_reset(SCpnt, SCSI_RESET_ASYNCHRONOUS))
  138. break;
  139. case IN_RESET:
  140. case (IN_ABORT | IN_RESET):
  141. /* This might be controversial, but if there is a bus hang,
  142.  * you might conceivably want the machine up and running
  143.  * esp if you have an ide disk.
  144.  */
  145. printk("SCSI host %d channel %d reset (pid %ld) timed out - "
  146.        "trying hardern",
  147.        SCpnt->host->host_no, SCpnt->channel, SCpnt->pid);
  148. SCpnt->internal_timeout &= ~IN_RESET;
  149. SCpnt->internal_timeout |= IN_RESET2;
  150. scsi_reset(SCpnt,
  151.  SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_BUS_RESET);
  152. break;
  153. case IN_RESET2:
  154. case (IN_ABORT | IN_RESET2):
  155. /* Obviously the bus reset didn't work.
  156.  * Let's try even harder and call for an HBA reset.
  157.  * Maybe the HBA itself crashed and this will shake it loose.
  158.  */
  159. printk("SCSI host %d reset (pid %ld) timed out - trying to shake it loosen",
  160.        SCpnt->host->host_no, SCpnt->pid);
  161. SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2);
  162. SCpnt->internal_timeout |= IN_RESET3;
  163. scsi_reset(SCpnt,
  164. SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_HOST_RESET);
  165. break;
  166. default:
  167. printk("SCSI host %d reset (pid %ld) timed out again -n",
  168.        SCpnt->host->host_no, SCpnt->pid);
  169. printk("probably an unrecoverable SCSI bus or device hang.n");
  170. break;
  171. }
  172. spin_unlock_irqrestore(&io_request_lock, flags);
  173. }
  174. /*
  175.  *  From what I can find in scsi_obsolete.c, this function is only called
  176.  *  by scsi_old_done and scsi_reset.  Both of these functions run with the
  177.  *  io_request_lock already held, so we need do nothing here about grabbing
  178.  *  any locks.
  179.  */
  180. static void scsi_request_sense(Scsi_Cmnd * SCpnt)
  181. {
  182. SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
  183. update_timeout(SCpnt, SENSE_TIMEOUT);
  184. memcpy((void *) SCpnt->cmnd, (void *) generic_sense,
  185.        sizeof(generic_sense));
  186. memset((void *) SCpnt->sense_buffer, 0,
  187.        sizeof(SCpnt->sense_buffer));
  188. if (SCpnt->device->scsi_level <= SCSI_2)
  189. SCpnt->cmnd[1] = SCpnt->lun << 5;
  190. SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
  191. SCpnt->request_buffer = &SCpnt->sense_buffer;
  192. SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
  193. SCpnt->use_sg = 0;
  194. SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
  195. SCpnt->result = 0;
  196. SCpnt->sc_data_direction = SCSI_DATA_READ;
  197.         /*
  198.          * Ugly, ugly.  The newer interfaces all assume that the lock
  199.          * isn't held.  Mustn't disappoint, or we deadlock the system.
  200.          */
  201.         spin_unlock_irq(&io_request_lock);
  202. scsi_dispatch_cmd(SCpnt);
  203.         spin_lock_irq(&io_request_lock);
  204. }
  205. static int check_sense(Scsi_Cmnd * SCpnt)
  206. {
  207. /* If there is no sense information, request it.  If we have already
  208.  * requested it, there is no point in asking again - the firmware must
  209.  * be confused.
  210.  */
  211. if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
  212. if (!(SCpnt->flags & ASKED_FOR_SENSE))
  213. return SUGGEST_SENSE;
  214. else
  215. return SUGGEST_RETRY;
  216. }
  217. SCpnt->flags &= ~ASKED_FOR_SENSE;
  218. #ifdef DEBUG_INIT
  219. printk("scsi%d, channel%d : ", SCpnt->host->host_no, SCpnt->channel);
  220. print_sense("", SCpnt);
  221. printk("n");
  222. #endif
  223. if (SCpnt->sense_buffer[2] & 0xe0)
  224. return SUGGEST_ABORT;
  225. switch (SCpnt->sense_buffer[2] & 0xf) {
  226. case NO_SENSE:
  227. return 0;
  228. case RECOVERED_ERROR:
  229. return SUGGEST_IS_OK;
  230. case ABORTED_COMMAND:
  231. return SUGGEST_RETRY;
  232. case NOT_READY:
  233. case UNIT_ATTENTION:
  234. /*
  235.  * If we are expecting a CC/UA because of a bus reset that we
  236.  * performed, treat this just as a retry.  Otherwise this is
  237.  * information that we should pass up to the upper-level driver
  238.  * so that we can deal with it there.
  239.  */
  240. if (SCpnt->device->expecting_cc_ua) {
  241. SCpnt->device->expecting_cc_ua = 0;
  242. return SUGGEST_RETRY;
  243. }
  244. return SUGGEST_ABORT;
  245. /* these three are not supported */
  246. case COPY_ABORTED:
  247. case VOLUME_OVERFLOW:
  248. case MISCOMPARE:
  249. case MEDIUM_ERROR:
  250. return SUGGEST_REMAP;
  251. case BLANK_CHECK:
  252. case DATA_PROTECT:
  253. case HARDWARE_ERROR:
  254. case ILLEGAL_REQUEST:
  255. default:
  256. return SUGGEST_ABORT;
  257. }
  258. }
  259. /* This function is the mid-level interrupt routine, which decides how
  260.  *  to handle error conditions.  Each invocation of this function must
  261.  *  do one and *only* one of the following:
  262.  *
  263.  *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
  264.  *      normal completion, and indicates that the handling for this
  265.  *      request is complete.
  266.  *  (2) Call internal_cmnd to requeue the command.  This will result in
  267.  *      scsi_done being called again when the retry is complete.
  268.  *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
  269.  *      more information about the error condition.  When the information
  270.  *      is available, scsi_done will be called again.
  271.  *  (4) Call reset().  This is sort of a last resort, and the idea is that
  272.  *      this may kick things loose and get the drive working again.  reset()
  273.  *      automatically calls scsi_request_sense, and thus scsi_done will be
  274.  *      called again once the reset is complete.
  275.  *
  276.  *      If none of the above actions are taken, the drive in question
  277.  *      will hang. If more than one of the above actions are taken by
  278.  *      scsi_done, then unpredictable behavior will result.
  279.  */
  280. void scsi_old_done(Scsi_Cmnd * SCpnt)
  281. {
  282. int status = 0;
  283. int exit = 0;
  284. int checked;
  285. int oldto;
  286. struct Scsi_Host *host = SCpnt->host;
  287.         Scsi_Device * device = SCpnt->device;
  288. int result = SCpnt->result;
  289. SCpnt->serial_number = 0;
  290. SCpnt->serial_number_at_timeout = 0;
  291. oldto = update_timeout(SCpnt, 0);
  292. #ifdef DEBUG_TIMEOUT
  293. if (result)
  294. printk("Non-zero result in scsi_done %x %d:%dn",
  295.        result, SCpnt->target, SCpnt->lun);
  296. #endif
  297. /* If we requested an abort, (and we got it) then fix up the return
  298.  *  status to say why
  299.  */
  300. if (host_byte(result) == DID_ABORT && SCpnt->abort_reason)
  301. SCpnt->result = result = (result & 0xff00ffff) |
  302.     (SCpnt->abort_reason << 16);
  303. #define CMD_FINISHED 0
  304. #define MAYREDO  1
  305. #define REDO     3
  306. #define PENDING  4
  307. #ifdef DEBUG
  308. printk("In scsi_done(host = %d, result = %06x)n", host->host_no, result);
  309. #endif
  310. if (SCpnt->flags & SYNC_RESET) {
  311. /*
  312.    * The behaviou of scsi_reset(SYNC) was changed in 2.1.? .
  313.    * The scsi mid-layer does a REDO after every sync reset, the driver
  314.    * must not do that any more. In order to prevent old drivers from
  315.    * crashing, all scsi_done() calls during sync resets are ignored.
  316.  */
  317. printk("scsi%d: device driver called scsi_done() "
  318.        "for a synchronous reset.n", SCpnt->host->host_no);
  319. return;
  320. }
  321. if (SCpnt->flags & WAS_SENSE) {
  322. SCpnt->use_sg = SCpnt->old_use_sg;
  323. SCpnt->cmd_len = SCpnt->old_cmd_len;
  324. SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
  325. SCpnt->underflow = SCpnt->old_underflow;
  326. }
  327. switch (host_byte(result)) {
  328. case DID_OK:
  329. if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
  330. /* Failed to obtain sense information */
  331. {
  332. SCpnt->flags &= ~WAS_SENSE;
  333. #if 0 /* This cannot possibly be correct. */
  334. SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
  335. #endif
  336. if (!(SCpnt->flags & WAS_RESET)) {
  337. printk("scsi%d : channel %d target %d lun %d request sense"
  338.        " failed, performing reset.n",
  339.        SCpnt->host->host_no, SCpnt->channel, SCpnt->target,
  340.        SCpnt->lun);
  341. scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
  342. status = REDO;
  343. break;
  344. } else {
  345. exit = (DRIVER_HARD | SUGGEST_ABORT);
  346. status = CMD_FINISHED;
  347. }
  348. } else
  349. switch (msg_byte(result)) {
  350. case COMMAND_COMPLETE:
  351. switch (status_byte(result)) {
  352. case GOOD:
  353. if (SCpnt->flags & WAS_SENSE) {
  354. #ifdef DEBUG
  355. printk("In scsi_done, GOOD status, COMMAND COMPLETE, "
  356.        "parsing sense information.n");
  357. #endif
  358. SCpnt->flags &= ~WAS_SENSE;
  359. #if 0 /* This cannot possibly be correct. */
  360. SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
  361. #endif
  362. switch (checked = check_sense(SCpnt)) {
  363. case SUGGEST_SENSE:
  364. case 0:
  365. #ifdef DEBUG
  366. printk("NO SENSE.  status = REDOn");
  367. #endif
  368. update_timeout(SCpnt, oldto);
  369. status = REDO;
  370. break;
  371. case SUGGEST_IS_OK:
  372. break;
  373. case SUGGEST_REMAP:
  374. #ifdef DEBUG
  375. printk("SENSE SUGGEST REMAP - status = CMD_FINISHEDn");
  376. #endif
  377. status = CMD_FINISHED;
  378. exit = DRIVER_SENSE | SUGGEST_ABORT;
  379. break;
  380. case SUGGEST_RETRY:
  381. #ifdef DEBUG
  382. printk("SENSE SUGGEST RETRY - status = MAYREDOn");
  383. #endif
  384. status = MAYREDO;
  385. exit = DRIVER_SENSE | SUGGEST_RETRY;
  386. break;
  387. case SUGGEST_ABORT:
  388. #ifdef DEBUG
  389. printk("SENSE SUGGEST ABORT - status = CMD_FINISHED");
  390. #endif
  391. status = CMD_FINISHED;
  392. exit = DRIVER_SENSE | SUGGEST_ABORT;
  393. break;
  394. default:
  395. printk("Internal error %s %d n", __FILE__,
  396.        __LINE__);
  397. }
  398. }
  399. /* end WAS_SENSE */
  400. else {
  401. #ifdef DEBUG
  402. printk("COMMAND COMPLETE message returned, "
  403.        "status = CMD_FINISHED. n");
  404. #endif
  405. exit = DRIVER_OK;
  406. status = CMD_FINISHED;
  407. }
  408. break;
  409. case CHECK_CONDITION:
  410. case COMMAND_TERMINATED:
  411. switch (check_sense(SCpnt)) {
  412. case 0:
  413. update_timeout(SCpnt, oldto);
  414. status = REDO;
  415. break;
  416. case SUGGEST_REMAP:
  417. status = CMD_FINISHED;
  418. exit = DRIVER_SENSE | SUGGEST_ABORT;
  419. break;
  420. case SUGGEST_RETRY:
  421. status = MAYREDO;
  422. exit = DRIVER_SENSE | SUGGEST_RETRY;
  423. break;
  424. case SUGGEST_ABORT:
  425. status = CMD_FINISHED;
  426. exit = DRIVER_SENSE | SUGGEST_ABORT;
  427. break;
  428. case SUGGEST_SENSE:
  429. scsi_request_sense(SCpnt);
  430. status = PENDING;
  431. break;
  432. }
  433. break;
  434. case CONDITION_GOOD:
  435. case INTERMEDIATE_GOOD:
  436. case INTERMEDIATE_C_GOOD:
  437. break;
  438. case BUSY:
  439. case QUEUE_FULL:
  440. update_timeout(SCpnt, oldto);
  441. status = REDO;
  442. break;
  443. case RESERVATION_CONFLICT:
  444. /*
  445.  * Most HAs will return an error for
  446.  * this, so usually reservation
  447.  * conflicts will  be processed under
  448.  * DID_ERROR code
  449.  */
  450. printk("scsi%d (%d,%d,%d) : RESERVATION CONFLICTn", 
  451.        SCpnt->host->host_no, SCpnt->channel,
  452.        SCpnt->device->id, SCpnt->device->lun);
  453. status = CMD_FINISHED; /* returns I/O error */
  454. break;
  455.                                         
  456. default:
  457. printk("Internal error %s %d n"
  458.  "status byte = %d n", __FILE__,
  459.   __LINE__, status_byte(result));
  460. }
  461. break;
  462. default:
  463. panic("scsi: unsupported message byte %d receivedn",
  464.       msg_byte(result));
  465. }
  466. break;
  467. case DID_TIME_OUT:
  468. #ifdef DEBUG
  469. printk("Host returned DID_TIME_OUT - ");
  470. #endif
  471. if (SCpnt->flags & WAS_TIMEDOUT) {
  472. #ifdef DEBUG
  473. printk("Abortingn");
  474. #endif
  475. /*
  476.    Allow TEST_UNIT_READY and INQUIRY commands to timeout early
  477.    without causing resets.  All other commands should be retried.
  478.  */
  479. if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
  480.     SCpnt->cmnd[0] != INQUIRY)
  481. status = MAYREDO;
  482. exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
  483. } else {
  484. #ifdef DEBUG
  485. printk("Retrying.n");
  486. #endif
  487. SCpnt->flags |= WAS_TIMEDOUT;
  488. SCpnt->internal_timeout &= ~IN_ABORT;
  489. status = REDO;
  490. }
  491. break;
  492. case DID_BUS_BUSY:
  493. case DID_PARITY:
  494. status = REDO;
  495. break;
  496. case DID_NO_CONNECT:
  497. #ifdef DEBUG
  498. printk("Couldn't connect.n");
  499. #endif
  500. exit = (DRIVER_HARD | SUGGEST_ABORT);
  501. break;
  502. case DID_ERROR:
  503. if (msg_byte(result) == COMMAND_COMPLETE &&
  504.     status_byte(result) == RESERVATION_CONFLICT) {
  505. printk("scsi%d (%d,%d,%d) : RESERVATION CONFLICTn", 
  506.        SCpnt->host->host_no, SCpnt->channel,
  507.        SCpnt->device->id, SCpnt->device->lun);
  508. status = CMD_FINISHED; /* returns I/O error */
  509. break;
  510. }
  511. status = MAYREDO;
  512. exit = (DRIVER_HARD | SUGGEST_ABORT);
  513. break;
  514. case DID_BAD_TARGET:
  515. case DID_ABORT:
  516. exit = (DRIVER_INVALID | SUGGEST_ABORT);
  517. break;
  518. case DID_RESET:
  519. if (SCpnt->flags & IS_RESETTING) {
  520. SCpnt->flags &= ~IS_RESETTING;
  521. status = REDO;
  522. break;
  523. }
  524. if (msg_byte(result) == GOOD &&
  525.     status_byte(result) == CHECK_CONDITION) {
  526. switch (check_sense(SCpnt)) {
  527. case 0:
  528. update_timeout(SCpnt, oldto);
  529. status = REDO;
  530. break;
  531. case SUGGEST_REMAP:
  532. case SUGGEST_RETRY:
  533. status = MAYREDO;
  534. exit = DRIVER_SENSE | SUGGEST_RETRY;
  535. break;
  536. case SUGGEST_ABORT:
  537. status = CMD_FINISHED;
  538. exit = DRIVER_SENSE | SUGGEST_ABORT;
  539. break;
  540. case SUGGEST_SENSE:
  541. scsi_request_sense(SCpnt);
  542. status = PENDING;
  543. break;
  544. }
  545. } else {
  546. status = REDO;
  547. exit = SUGGEST_RETRY;
  548. }
  549. break;
  550. default:
  551. exit = (DRIVER_ERROR | SUGGEST_DIE);
  552. }
  553. switch (status) {
  554. case CMD_FINISHED:
  555. case PENDING:
  556. break;
  557. case MAYREDO:
  558. #ifdef DEBUG
  559. printk("In MAYREDO, allowing %d retries, have %dn",
  560.        SCpnt->allowed, SCpnt->retries);
  561. #endif
  562. if ((++SCpnt->retries) < SCpnt->allowed) {
  563. if ((SCpnt->retries >= (SCpnt->allowed >> 1))
  564.     && !(SCpnt->host->resetting && time_before(jiffies, SCpnt->host->last_reset + MIN_RESET_PERIOD))
  565.     && !(SCpnt->flags & WAS_RESET)) {
  566. printk("scsi%d channel %d : resetting for second half of retries.n",
  567.    SCpnt->host->host_no, SCpnt->channel);
  568. scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
  569. /* fall through to REDO */
  570. }
  571. } else {
  572. status = CMD_FINISHED;
  573. break;
  574. }
  575. /* fall through to REDO */
  576. case REDO:
  577. if (SCpnt->flags & WAS_SENSE)
  578. scsi_request_sense(SCpnt);
  579. else {
  580. memcpy((void *) SCpnt->cmnd,
  581.        (void *) SCpnt->data_cmnd,
  582.        sizeof(SCpnt->data_cmnd));
  583. memset((void *) SCpnt->sense_buffer, 0,
  584.        sizeof(SCpnt->sense_buffer));
  585. SCpnt->request_buffer = SCpnt->buffer;
  586. SCpnt->request_bufflen = SCpnt->bufflen;
  587. SCpnt->use_sg = SCpnt->old_use_sg;
  588. SCpnt->cmd_len = SCpnt->old_cmd_len;
  589. SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
  590. SCpnt->underflow = SCpnt->old_underflow;
  591. SCpnt->result = 0;
  592.                         /*
  593.                          * Ugly, ugly.  The newer interfaces all
  594.                          * assume that the lock isn't held.  Mustn't
  595.                          * disappoint, or we deadlock the system.  
  596.                          */
  597.                         spin_unlock_irq(&io_request_lock);
  598. scsi_dispatch_cmd(SCpnt);
  599.                         spin_lock_irq(&io_request_lock);
  600. }
  601. break;
  602. default:
  603. INTERNAL_ERROR;
  604. }
  605. if (status == CMD_FINISHED) {
  606. Scsi_Request *SRpnt;
  607. #ifdef DEBUG
  608. printk("Calling done function - at address %pn", SCpnt->done);
  609. #endif
  610. host->host_busy--; /* Indicate that we are free */
  611.                 device->device_busy--; /* Decrement device usage counter. */
  612. SCpnt->result = result | ((exit & 0xff) << 24);
  613. SCpnt->use_sg = SCpnt->old_use_sg;
  614. SCpnt->cmd_len = SCpnt->old_cmd_len;
  615. SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
  616. SCpnt->underflow = SCpnt->old_underflow;
  617.                 /*
  618.                  * The upper layers assume the lock isn't held.  We mustn't
  619.                  * disappoint them.  When the new error handling code is in
  620.                  * use, the upper code is run from a bottom half handler, so
  621.                  * it isn't an issue.
  622.                  */
  623.                 spin_unlock_irq(&io_request_lock);
  624. SRpnt = SCpnt->sc_request;
  625. if( SRpnt != NULL ) {
  626. SRpnt->sr_result = SRpnt->sr_command->result;
  627. if( SRpnt->sr_result != 0 ) {
  628. memcpy(SRpnt->sr_sense_buffer,
  629.        SRpnt->sr_command->sense_buffer,
  630.        sizeof(SRpnt->sr_sense_buffer));
  631. }
  632. }
  633. SCpnt->done(SCpnt);
  634.                 spin_lock_irq(&io_request_lock);
  635. }
  636. #undef CMD_FINISHED
  637. #undef REDO
  638. #undef MAYREDO
  639. #undef PENDING
  640. }
  641. /*
  642.  * The scsi_abort function interfaces with the abort() function of the host
  643.  * we are aborting, and causes the current command to not complete.  The
  644.  * caller should deal with any error messages or status returned on the
  645.  * next call.
  646.  *
  647.  * This will not be called reentrantly for a given host.
  648.  */
  649. /*
  650.  * Since we're nice guys and specified that abort() and reset()
  651.  * can be non-reentrant.  The internal_timeout flags are used for
  652.  * this.
  653.  */
  654. static int scsi_abort(Scsi_Cmnd * SCpnt, int why)
  655. {
  656. int oldto;
  657. struct Scsi_Host *host = SCpnt->host;
  658. while (1) {
  659. /*
  660.  * Protect against races here.  If the command is done, or we are
  661.  * on a different command forget it.
  662.  */
  663. if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
  664. return 0;
  665. }
  666. if (SCpnt->internal_timeout & IN_ABORT) {
  667. spin_unlock_irq(&io_request_lock);
  668. while (SCpnt->internal_timeout & IN_ABORT)
  669. barrier();
  670. spin_lock_irq(&io_request_lock);
  671. } else {
  672. SCpnt->internal_timeout |= IN_ABORT;
  673. oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
  674. if ((SCpnt->flags & IS_RESETTING) && SCpnt->device->soft_reset) {
  675. /* OK, this command must have died when we did the
  676.  *  reset.  The device itself must have lied.
  677.  */
  678. printk("Stale command on %d %d:%d appears to have died when"
  679.        " the bus was resetn",
  680.        SCpnt->channel, SCpnt->target, SCpnt->lun);
  681. }
  682. if (!host->host_busy) {
  683. SCpnt->internal_timeout &= ~IN_ABORT;
  684. update_timeout(SCpnt, oldto);
  685. return 0;
  686. }
  687. printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
  688.        " channel %d, id %d, lun %d ",
  689.        SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel,
  690.        (int) SCpnt->target, (int) SCpnt->lun);
  691. print_command(SCpnt->cmnd);
  692. if (SCpnt->serial_number != SCpnt->serial_number_at_timeout)
  693. return 0;
  694. SCpnt->abort_reason = why;
  695. switch (host->hostt->abort(SCpnt)) {
  696. /* We do not know how to abort.  Try waiting another
  697.  * time increment and see if this helps. Set the
  698.  * WAS_TIMEDOUT flag set so we do not try this twice
  699.  */
  700. case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
  701.  * this is too severe
  702.  */
  703. case SCSI_ABORT_SNOOZE:
  704. if (why == DID_TIME_OUT) {
  705. SCpnt->internal_timeout &= ~IN_ABORT;
  706. if (SCpnt->flags & WAS_TIMEDOUT) {
  707. return 1; /* Indicate we cannot handle this.
  708.  * We drop down into the reset handler
  709.  * and try again
  710.  */
  711. } else {
  712. SCpnt->flags |= WAS_TIMEDOUT;
  713. oldto = SCpnt->timeout_per_command;
  714. update_timeout(SCpnt, oldto);
  715. }
  716. }
  717. return 0;
  718. case SCSI_ABORT_PENDING:
  719. if (why != DID_TIME_OUT) {
  720. update_timeout(SCpnt, oldto);
  721. }
  722. return 0;
  723. case SCSI_ABORT_SUCCESS:
  724. /* We should have already aborted this one.  No
  725.  * need to adjust timeout
  726.  */
  727. SCpnt->internal_timeout &= ~IN_ABORT;
  728. return 0;
  729. case SCSI_ABORT_NOT_RUNNING:
  730. SCpnt->internal_timeout &= ~IN_ABORT;
  731. update_timeout(SCpnt, 0);
  732. return 0;
  733. case SCSI_ABORT_ERROR:
  734. default:
  735. SCpnt->internal_timeout &= ~IN_ABORT;
  736. return 1;
  737. }
  738. }
  739. }
  740. }
  741. /* Mark a single SCSI Device as having been reset. */
  742. static inline void scsi_mark_device_reset(Scsi_Device * Device)
  743. {
  744. Device->was_reset = 1;
  745. Device->expecting_cc_ua = 1;
  746. }
  747. /* Mark all SCSI Devices on a specific Host as having been reset. */
  748. void scsi_mark_host_reset(struct Scsi_Host *Host)
  749. {
  750. Scsi_Cmnd *SCpnt;
  751. Scsi_Device *SDpnt;
  752. for (SDpnt = Host->host_queue; SDpnt; SDpnt = SDpnt->next) {
  753. for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next)
  754. scsi_mark_device_reset(SCpnt->device);
  755. }
  756. }
  757. /* Mark all SCSI Devices on a specific Host Bus as having been reset. */
  758. static void scsi_mark_bus_reset(struct Scsi_Host *Host, int channel)
  759. {
  760. Scsi_Cmnd *SCpnt;
  761. Scsi_Device *SDpnt;
  762. for (SDpnt = Host->host_queue; SDpnt; SDpnt = SDpnt->next) {
  763. for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next)
  764. if (SCpnt->channel == channel)
  765. scsi_mark_device_reset(SCpnt->device);
  766. }
  767. }
  768. static int scsi_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
  769. {
  770. int temp;
  771. Scsi_Cmnd *SCpnt1;
  772. Scsi_Device *SDpnt;
  773. struct Scsi_Host *host = SCpnt->host;
  774. printk("SCSI bus is being reset for host %d channel %d.n",
  775.        host->host_no, SCpnt->channel);
  776. #if 0
  777. /*
  778.  * First of all, we need to make a recommendation to the low-level
  779.  * driver as to whether a BUS_DEVICE_RESET should be performed,
  780.  * or whether we should do a full BUS_RESET.  There is no simple
  781.  * algorithm here - we basically use a series of heuristics
  782.  * to determine what we should do.
  783.  */
  784. SCpnt->host->suggest_bus_reset = FALSE;
  785. /*
  786.  * First see if all of the active devices on the bus have
  787.  * been jammed up so that we are attempting resets.  If so,
  788.  * then suggest a bus reset.  Forcing a bus reset could
  789.  * result in some race conditions, but no more than
  790.  * you would usually get with timeouts.  We will cross
  791.  * that bridge when we come to it.
  792.  *
  793.  * This is actually a pretty bad idea, since a sequence of
  794.  * commands will often timeout together and this will cause a
  795.  * Bus Device Reset followed immediately by a SCSI Bus Reset.
  796.  * If all of the active devices really are jammed up, the
  797.  * Bus Device Reset will quickly timeout and scsi_times_out
  798.  * will follow up with a SCSI Bus Reset anyway.
  799.  */
  800. SCpnt1 = host->host_queue;
  801. while (SCpnt1) {
  802. if (SCpnt1->request.rq_status != RQ_INACTIVE
  803.     && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0)
  804. break;
  805. SCpnt1 = SCpnt1->next;
  806. }
  807. if (SCpnt1 == NULL) {
  808. reset_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
  809. }
  810. /*
  811.  * If the code that called us is suggesting a hard reset, then
  812.  * definitely request it.  This usually occurs because a
  813.  * BUS_DEVICE_RESET times out.
  814.  *
  815.  * Passing reset_flags along takes care of this automatically.
  816.  */
  817. if (reset_flags & SCSI_RESET_SUGGEST_BUS_RESET) {
  818. SCpnt->host->suggest_bus_reset = TRUE;
  819. }
  820. #endif
  821. while (1) {
  822. /*
  823.  * Protect against races here.  If the command is done, or we are
  824.  * on a different command forget it.
  825.  */
  826. if (reset_flags & SCSI_RESET_ASYNCHRONOUS)
  827. if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
  828. return 0;
  829. }
  830. if (SCpnt->internal_timeout & IN_RESET) {
  831. spin_unlock_irq(&io_request_lock);
  832. while (SCpnt->internal_timeout & IN_RESET)
  833. barrier();
  834. spin_lock_irq(&io_request_lock);
  835. } else {
  836. SCpnt->internal_timeout |= IN_RESET;
  837. update_timeout(SCpnt, RESET_TIMEOUT);
  838. if (reset_flags & SCSI_RESET_SYNCHRONOUS)
  839. SCpnt->flags |= SYNC_RESET;
  840. if (host->host_busy) {
  841. for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
  842. SCpnt1 = SDpnt->device_queue;
  843. while (SCpnt1) {
  844. if (SCpnt1->request.rq_status != RQ_INACTIVE) {
  845. #if 0
  846. if (!(SCpnt1->flags & IS_RESETTING) &&
  847.     !(SCpnt1->internal_timeout & IN_ABORT))
  848. scsi_abort(SCpnt1, DID_RESET);
  849. #endif
  850. SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
  851. }
  852. SCpnt1 = SCpnt1->next;
  853. }
  854. }
  855. host->last_reset = jiffies;
  856. host->resetting = 1;
  857. /*
  858.  * I suppose that the host reset callback will not play
  859.  * with the resetting field. We have just set the resetting
  860.  * flag here. -arca
  861.  */
  862. temp = host->hostt->reset(SCpnt, reset_flags);
  863. /*
  864.    This test allows the driver to introduce an additional bus
  865.    settle time delay by setting last_reset up to 20 seconds in
  866.    the future.  In the normal case where the driver does not
  867.    modify last_reset, it must be assumed that the actual bus
  868.    reset occurred immediately prior to the return to this code,
  869.    and so last_reset must be updated to the current time, so
  870.    that the delay in internal_cmnd will guarantee at least a
  871.    MIN_RESET_DELAY bus settle time.
  872.  */
  873. if (host->last_reset - jiffies > 20UL * HZ)
  874. host->last_reset = jiffies;
  875. } else {
  876. host->host_busy++;
  877. host->last_reset = jiffies;
  878. host->resetting = 1;
  879. SCpnt->flags |= (WAS_RESET | IS_RESETTING);
  880. /*
  881.  * I suppose that the host reset callback will not play
  882.  * with the resetting field. We have just set the resetting
  883.  * flag here. -arca
  884.  */
  885. temp = host->hostt->reset(SCpnt, reset_flags);
  886. if (time_before(host->last_reset, jiffies) ||
  887.     (time_after(host->last_reset, jiffies + 20 * HZ)))
  888. host->last_reset = jiffies;
  889. host->host_busy--;
  890. }
  891. if (reset_flags & SCSI_RESET_SYNCHRONOUS)
  892. SCpnt->flags &= ~SYNC_RESET;
  893. #ifdef DEBUG
  894. printk("scsi reset function returned %dn", temp);
  895. #endif
  896. /*
  897.  * Now figure out what we need to do, based upon
  898.  * what the low level driver said that it did.
  899.  * If the result is SCSI_RESET_SUCCESS, SCSI_RESET_PENDING,
  900.  * or SCSI_RESET_WAKEUP, then the low level driver did a
  901.  * bus device reset or bus reset, so we should go through
  902.  * and mark one or all of the devices on that bus
  903.  * as having been reset.
  904.  */
  905. switch (temp & SCSI_RESET_ACTION) {
  906. case SCSI_RESET_SUCCESS:
  907. if (temp & SCSI_RESET_HOST_RESET)
  908. scsi_mark_host_reset(host);
  909. else if (temp & SCSI_RESET_BUS_RESET)
  910. scsi_mark_bus_reset(host, SCpnt->channel);
  911. else
  912. scsi_mark_device_reset(SCpnt->device);
  913. SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
  914. return 0;
  915. case SCSI_RESET_PENDING:
  916. if (temp & SCSI_RESET_HOST_RESET)
  917. scsi_mark_host_reset(host);
  918. else if (temp & SCSI_RESET_BUS_RESET)
  919. scsi_mark_bus_reset(host, SCpnt->channel);
  920. else
  921. scsi_mark_device_reset(SCpnt->device);
  922. case SCSI_RESET_NOT_RUNNING:
  923. return 0;
  924. case SCSI_RESET_PUNT:
  925. SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
  926. scsi_request_sense(SCpnt);
  927. return 0;
  928. case SCSI_RESET_WAKEUP:
  929. if (temp & SCSI_RESET_HOST_RESET)
  930. scsi_mark_host_reset(host);
  931. else if (temp & SCSI_RESET_BUS_RESET)
  932. scsi_mark_bus_reset(host, SCpnt->channel);
  933. else
  934. scsi_mark_device_reset(SCpnt->device);
  935. SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
  936. scsi_request_sense(SCpnt);
  937. /*
  938.  * If a bus reset was performed, we
  939.  * need to wake up each and every command
  940.  * that was active on the bus or if it was a HBA
  941.  * reset all active commands on all channels
  942.  */
  943. if (temp & SCSI_RESET_HOST_RESET) {
  944. for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
  945. SCpnt1 = SDpnt->device_queue;
  946. while (SCpnt1) {
  947. if (SCpnt1->request.rq_status != RQ_INACTIVE
  948.     && SCpnt1 != SCpnt)
  949. scsi_request_sense(SCpnt1);
  950. SCpnt1 = SCpnt1->next;
  951. }
  952. }
  953. } else if (temp & SCSI_RESET_BUS_RESET) {
  954. for (SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next) {
  955. SCpnt1 = SDpnt->device_queue;
  956. while (SCpnt1) {
  957. if (SCpnt1->request.rq_status != RQ_INACTIVE
  958. && SCpnt1 != SCpnt
  959.     && SCpnt1->channel == SCpnt->channel)
  960. scsi_request_sense(SCpnt);
  961. SCpnt1 = SCpnt1->next;
  962. }
  963. }
  964. }
  965. return 0;
  966. case SCSI_RESET_SNOOZE:
  967. /* In this case, we set the timeout field to 0
  968.  * so that this command does not time out any more,
  969.  * and we return 1 so that we get a message on the
  970.  * screen.
  971.  */
  972. SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2 | IN_RESET3);
  973. update_timeout(SCpnt, 0);
  974. /* If you snooze, you lose... */
  975. case SCSI_RESET_ERROR:
  976. default:
  977. return 1;
  978. }
  979. return temp;
  980. }
  981. }
  982. }
  983. /*
  984.  * The strategy is to cause the timer code to call scsi_times_out()
  985.  * when the soonest timeout is pending.
  986.  * The arguments are used when we are queueing a new command, because
  987.  * we do not want to subtract the time used from this time, but when we
  988.  * set the timer, we want to take this value into account.
  989.  */
  990. int update_timeout(Scsi_Cmnd * SCset, int timeout)
  991. {
  992. int rtn;
  993. /*
  994.  * We are using the new error handling code to actually register/deregister
  995.  * timers for timeout.
  996.  */
  997. if (!timer_pending(&SCset->eh_timeout)) {
  998. rtn = 0;
  999. } else {
  1000. rtn = SCset->eh_timeout.expires - jiffies;
  1001. }
  1002. if (timeout == 0) {
  1003. scsi_delete_timer(SCset);
  1004. } else {
  1005. scsi_add_timer(SCset, timeout, scsi_old_times_out);
  1006. }
  1007. return rtn;
  1008. }
  1009. /*
  1010.  * This function exports SCSI Bus, Device or Host reset capability
  1011.  * and is for use with the SCSI generic driver.
  1012.  */
  1013. int
  1014. scsi_old_reset(Scsi_Cmnd *SCpnt, unsigned int flag)
  1015. {
  1016. unsigned int old_flags = SCSI_RESET_SYNCHRONOUS;
  1017. switch(flag) {
  1018. case SCSI_TRY_RESET_DEVICE:
  1019. /* no suggestion flags to add, device reset is default */
  1020. break;
  1021. case SCSI_TRY_RESET_BUS:
  1022. old_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
  1023. break;
  1024. case SCSI_TRY_RESET_HOST:
  1025. old_flags |= SCSI_RESET_SUGGEST_HOST_RESET;
  1026. break;
  1027. default:
  1028. return FAILED;
  1029. }
  1030. if (scsi_reset(SCpnt, old_flags))
  1031. return FAILED;
  1032. return SUCCESS;
  1033. }
  1034. /*
  1035.  * Overrides for Emacs so that we follow Linus's tabbing style.
  1036.  * Emacs will notice this stuff at the end of the file and automatically
  1037.  * adjust the settings for this buffer only.  This must remain at the end
  1038.  * of the file.
  1039.  * ---------------------------------------------------------------------------
  1040.  * Local variables:
  1041.  * c-indent-level: 4
  1042.  * c-brace-imaginary-offset: 0
  1043.  * c-brace-offset: -4
  1044.  * c-argdecl-indent: 4
  1045.  * c-label-offset: -4
  1046.  * c-continued-statement-offset: 4
  1047.  * c-continued-brace-offset: 0
  1048.  * indent-tabs-mode: nil
  1049.  * tab-width: 8
  1050.  * End:
  1051.  */