scsi_error.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. /*
  2. * scsi_error.c Copyright (C) 1997 Eric Youngdale
  3. *
  4. * SCSI error/timeout handling
  5. * Initial versions: Eric Youngdale. Based upon conversations with
  6. * Leonard Zubkoff and David Miller at Linux Expo,
  7. * ideas originating from all over the place.
  8. *
  9. * Restructured scsi_unjam_host and associated functions.
  10. * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
  11. *
  12. * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
  13. * minor cleanups.
  14. * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
  15. */
  16. #include <linux/module.h>
  17. #include <linux/sched.h>
  18. #include <linux/timer.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/kernel.h>
  22. #include <linux/kthread.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/delay.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_dbg.h>
  29. #include <scsi/scsi_device.h>
  30. #include <scsi/scsi_eh.h>
  31. #include <scsi/scsi_transport.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_ioctl.h>
  34. #include "scsi_priv.h"
  35. #include "scsi_logging.h"
  36. #define SENSE_TIMEOUT (10*HZ)
  37. #define START_UNIT_TIMEOUT (30*HZ)
  38. /*
  39. * These should *probably* be handled by the host itself.
  40. * Since it is allowed to sleep, it probably should.
  41. */
  42. #define BUS_RESET_SETTLE_TIME (10)
  43. #define HOST_RESET_SETTLE_TIME (10)
  44. /* called with shost->host_lock held */
  45. void scsi_eh_wakeup(struct Scsi_Host *shost)
  46. {
  47. if (shost->host_busy == shost->host_failed) {
  48. wake_up_process(shost->ehandler);
  49. SCSI_LOG_ERROR_RECOVERY(5,
  50. printk("Waking error handler thread\n"));
  51. }
  52. }
  53. /**
  54. * scsi_schedule_eh - schedule EH for SCSI host
  55. * @shost: SCSI host to invoke error handling on.
  56. *
  57. * Schedule SCSI EH without scmd.
  58. **/
  59. void scsi_schedule_eh(struct Scsi_Host *shost)
  60. {
  61. unsigned long flags;
  62. spin_lock_irqsave(shost->host_lock, flags);
  63. if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
  64. scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
  65. shost->host_eh_scheduled++;
  66. scsi_eh_wakeup(shost);
  67. }
  68. spin_unlock_irqrestore(shost->host_lock, flags);
  69. }
  70. EXPORT_SYMBOL_GPL(scsi_schedule_eh);
  71. /**
  72. * scsi_eh_scmd_add - add scsi cmd to error handling.
  73. * @scmd: scmd to run eh on.
  74. * @eh_flag: optional SCSI_EH flag.
  75. *
  76. * Return value:
  77. * 0 on failure.
  78. **/
  79. int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
  80. {
  81. struct Scsi_Host *shost = scmd->device->host;
  82. unsigned long flags;
  83. int ret = 0;
  84. if (!shost->ehandler)
  85. return 0;
  86. spin_lock_irqsave(shost->host_lock, flags);
  87. if (scsi_host_set_state(shost, SHOST_RECOVERY))
  88. if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY))
  89. goto out_unlock;
  90. ret = 1;
  91. scmd->eh_eflags |= eh_flag;
  92. list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
  93. shost->host_failed++;
  94. scsi_eh_wakeup(shost);
  95. out_unlock:
  96. spin_unlock_irqrestore(shost->host_lock, flags);
  97. return ret;
  98. }
  99. /**
  100. * scsi_add_timer - Start timeout timer for a single scsi command.
  101. * @scmd: scsi command that is about to start running.
  102. * @timeout: amount of time to allow this command to run.
  103. * @complete: timeout function to call if timer isn't canceled.
  104. *
  105. * Notes:
  106. * This should be turned into an inline function. Each scsi command
  107. * has its own timer, and as it is added to the queue, we set up the
  108. * timer. When the command completes, we cancel the timer.
  109. **/
  110. void scsi_add_timer(struct scsi_cmnd *scmd, int timeout,
  111. void (*complete)(struct scsi_cmnd *))
  112. {
  113. /*
  114. * If the clock was already running for this command, then
  115. * first delete the timer. The timer handling code gets rather
  116. * confused if we don't do this.
  117. */
  118. if (scmd->eh_timeout.function)
  119. del_timer(&scmd->eh_timeout);
  120. scmd->eh_timeout.data = (unsigned long)scmd;
  121. scmd->eh_timeout.expires = jiffies + timeout;
  122. scmd->eh_timeout.function = (void (*)(unsigned long)) complete;
  123. SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p, time:"
  124. " %d, (%p)\n", __FUNCTION__,
  125. scmd, timeout, complete));
  126. add_timer(&scmd->eh_timeout);
  127. }
  128. /**
  129. * scsi_delete_timer - Delete/cancel timer for a given function.
  130. * @scmd: Cmd that we are canceling timer for
  131. *
  132. * Notes:
  133. * This should be turned into an inline function.
  134. *
  135. * Return value:
  136. * 1 if we were able to detach the timer. 0 if we blew it, and the
  137. * timer function has already started to run.
  138. **/
  139. int scsi_delete_timer(struct scsi_cmnd *scmd)
  140. {
  141. int rtn;
  142. rtn = del_timer(&scmd->eh_timeout);
  143. SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p,"
  144. " rtn: %d\n", __FUNCTION__,
  145. scmd, rtn));
  146. scmd->eh_timeout.data = (unsigned long)NULL;
  147. scmd->eh_timeout.function = NULL;
  148. return rtn;
  149. }
  150. /**
  151. * scsi_times_out - Timeout function for normal scsi commands.
  152. * @scmd: Cmd that is timing out.
  153. *
  154. * Notes:
  155. * We do not need to lock this. There is the potential for a race
  156. * only in that the normal completion handling might run, but if the
  157. * normal completion function determines that the timer has already
  158. * fired, then it mustn't do anything.
  159. **/
  160. void scsi_times_out(struct scsi_cmnd *scmd)
  161. {
  162. scsi_log_completion(scmd, TIMEOUT_ERROR);
  163. if (scmd->device->host->transportt->eh_timed_out)
  164. switch (scmd->device->host->transportt->eh_timed_out(scmd)) {
  165. case EH_HANDLED:
  166. __scsi_done(scmd);
  167. return;
  168. case EH_RESET_TIMER:
  169. scsi_add_timer(scmd, scmd->timeout_per_command,
  170. scsi_times_out);
  171. return;
  172. case EH_NOT_HANDLED:
  173. break;
  174. }
  175. if (unlikely(!scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))) {
  176. scmd->result |= DID_TIME_OUT << 16;
  177. __scsi_done(scmd);
  178. }
  179. }
  180. /**
  181. * scsi_block_when_processing_errors - Prevent cmds from being queued.
  182. * @sdev: Device on which we are performing recovery.
  183. *
  184. * Description:
  185. * We block until the host is out of error recovery, and then check to
  186. * see whether the host or the device is offline.
  187. *
  188. * Return value:
  189. * 0 when dev was taken offline by error recovery. 1 OK to proceed.
  190. **/
  191. int scsi_block_when_processing_errors(struct scsi_device *sdev)
  192. {
  193. int online;
  194. wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
  195. online = scsi_device_online(sdev);
  196. SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __FUNCTION__,
  197. online));
  198. return online;
  199. }
  200. EXPORT_SYMBOL(scsi_block_when_processing_errors);
  201. #ifdef CONFIG_SCSI_LOGGING
  202. /**
  203. * scsi_eh_prt_fail_stats - Log info on failures.
  204. * @shost: scsi host being recovered.
  205. * @work_q: Queue of scsi cmds to process.
  206. **/
  207. static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
  208. struct list_head *work_q)
  209. {
  210. struct scsi_cmnd *scmd;
  211. struct scsi_device *sdev;
  212. int total_failures = 0;
  213. int cmd_failed = 0;
  214. int cmd_cancel = 0;
  215. int devices_failed = 0;
  216. shost_for_each_device(sdev, shost) {
  217. list_for_each_entry(scmd, work_q, eh_entry) {
  218. if (scmd->device == sdev) {
  219. ++total_failures;
  220. if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
  221. ++cmd_cancel;
  222. else
  223. ++cmd_failed;
  224. }
  225. }
  226. if (cmd_cancel || cmd_failed) {
  227. SCSI_LOG_ERROR_RECOVERY(3,
  228. sdev_printk(KERN_INFO, sdev,
  229. "%s: cmds failed: %d, cancel: %d\n",
  230. __FUNCTION__, cmd_failed,
  231. cmd_cancel));
  232. cmd_cancel = 0;
  233. cmd_failed = 0;
  234. ++devices_failed;
  235. }
  236. }
  237. SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
  238. " devices require eh work\n",
  239. total_failures, devices_failed));
  240. }
  241. #endif
  242. /**
  243. * scsi_check_sense - Examine scsi cmd sense
  244. * @scmd: Cmd to have sense checked.
  245. *
  246. * Return value:
  247. * SUCCESS or FAILED or NEEDS_RETRY
  248. *
  249. * Notes:
  250. * When a deferred error is detected the current command has
  251. * not been executed and needs retrying.
  252. **/
  253. static int scsi_check_sense(struct scsi_cmnd *scmd)
  254. {
  255. struct scsi_sense_hdr sshdr;
  256. if (! scsi_command_normalize_sense(scmd, &sshdr))
  257. return FAILED; /* no valid sense data */
  258. if (scsi_sense_is_deferred(&sshdr))
  259. return NEEDS_RETRY;
  260. /*
  261. * Previous logic looked for FILEMARK, EOM or ILI which are
  262. * mainly associated with tapes and returned SUCCESS.
  263. */
  264. if (sshdr.response_code == 0x70) {
  265. /* fixed format */
  266. if (scmd->sense_buffer[2] & 0xe0)
  267. return SUCCESS;
  268. } else {
  269. /*
  270. * descriptor format: look for "stream commands sense data
  271. * descriptor" (see SSC-3). Assume single sense data
  272. * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
  273. */
  274. if ((sshdr.additional_length > 3) &&
  275. (scmd->sense_buffer[8] == 0x4) &&
  276. (scmd->sense_buffer[11] & 0xe0))
  277. return SUCCESS;
  278. }
  279. switch (sshdr.sense_key) {
  280. case NO_SENSE:
  281. return SUCCESS;
  282. case RECOVERED_ERROR:
  283. return /* soft_error */ SUCCESS;
  284. case ABORTED_COMMAND:
  285. return NEEDS_RETRY;
  286. case NOT_READY:
  287. case UNIT_ATTENTION:
  288. /*
  289. * if we are expecting a cc/ua because of a bus reset that we
  290. * performed, treat this just as a retry. otherwise this is
  291. * information that we should pass up to the upper-level driver
  292. * so that we can deal with it there.
  293. */
  294. if (scmd->device->expecting_cc_ua) {
  295. scmd->device->expecting_cc_ua = 0;
  296. return NEEDS_RETRY;
  297. }
  298. /*
  299. * if the device is in the process of becoming ready, we
  300. * should retry.
  301. */
  302. if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
  303. return NEEDS_RETRY;
  304. /*
  305. * if the device is not started, we need to wake
  306. * the error handler to start the motor
  307. */
  308. if (scmd->device->allow_restart &&
  309. (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
  310. return FAILED;
  311. return SUCCESS;
  312. /* these three are not supported */
  313. case COPY_ABORTED:
  314. case VOLUME_OVERFLOW:
  315. case MISCOMPARE:
  316. return SUCCESS;
  317. case MEDIUM_ERROR:
  318. return NEEDS_RETRY;
  319. case HARDWARE_ERROR:
  320. if (scmd->device->retry_hwerror)
  321. return NEEDS_RETRY;
  322. else
  323. return SUCCESS;
  324. case ILLEGAL_REQUEST:
  325. case BLANK_CHECK:
  326. case DATA_PROTECT:
  327. default:
  328. return SUCCESS;
  329. }
  330. }
  331. /**
  332. * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
  333. * @scmd: SCSI cmd to examine.
  334. *
  335. * Notes:
  336. * This is *only* called when we are examining the status of commands
  337. * queued during error recovery. the main difference here is that we
  338. * don't allow for the possibility of retries here, and we are a lot
  339. * more restrictive about what we consider acceptable.
  340. **/
  341. static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
  342. {
  343. /*
  344. * first check the host byte, to see if there is anything in there
  345. * that would indicate what we need to do.
  346. */
  347. if (host_byte(scmd->result) == DID_RESET) {
  348. /*
  349. * rats. we are already in the error handler, so we now
  350. * get to try and figure out what to do next. if the sense
  351. * is valid, we have a pretty good idea of what to do.
  352. * if not, we mark it as FAILED.
  353. */
  354. return scsi_check_sense(scmd);
  355. }
  356. if (host_byte(scmd->result) != DID_OK)
  357. return FAILED;
  358. /*
  359. * next, check the message byte.
  360. */
  361. if (msg_byte(scmd->result) != COMMAND_COMPLETE)
  362. return FAILED;
  363. /*
  364. * now, check the status byte to see if this indicates
  365. * anything special.
  366. */
  367. switch (status_byte(scmd->result)) {
  368. case GOOD:
  369. case COMMAND_TERMINATED:
  370. return SUCCESS;
  371. case CHECK_CONDITION:
  372. return scsi_check_sense(scmd);
  373. case CONDITION_GOOD:
  374. case INTERMEDIATE_GOOD:
  375. case INTERMEDIATE_C_GOOD:
  376. /*
  377. * who knows? FIXME(eric)
  378. */
  379. return SUCCESS;
  380. case BUSY:
  381. case QUEUE_FULL:
  382. case RESERVATION_CONFLICT:
  383. default:
  384. return FAILED;
  385. }
  386. return FAILED;
  387. }
  388. /**
  389. * scsi_eh_done - Completion function for error handling.
  390. * @scmd: Cmd that is done.
  391. **/
  392. static void scsi_eh_done(struct scsi_cmnd *scmd)
  393. {
  394. struct completion *eh_action;
  395. SCSI_LOG_ERROR_RECOVERY(3,
  396. printk("%s scmd: %p result: %x\n",
  397. __FUNCTION__, scmd, scmd->result));
  398. eh_action = scmd->device->host->eh_action;
  399. if (eh_action)
  400. complete(eh_action);
  401. }
  402. /**
  403. * scsi_send_eh_cmnd - send a cmd to a device as part of error recovery.
  404. * @scmd: SCSI Cmd to send.
  405. * @timeout: Timeout for cmd.
  406. *
  407. * Return value:
  408. * SUCCESS or FAILED or NEEDS_RETRY
  409. **/
  410. static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
  411. int cmnd_size, int timeout, int copy_sense)
  412. {
  413. struct scsi_device *sdev = scmd->device;
  414. struct Scsi_Host *shost = sdev->host;
  415. int old_result = scmd->result;
  416. DECLARE_COMPLETION_ONSTACK(done);
  417. unsigned long timeleft;
  418. unsigned long flags;
  419. unsigned char old_cmnd[MAX_COMMAND_SIZE];
  420. enum dma_data_direction old_data_direction;
  421. unsigned short old_use_sg;
  422. unsigned char old_cmd_len;
  423. unsigned old_bufflen;
  424. void *old_buffer;
  425. int rtn;
  426. /*
  427. * We need saved copies of a number of fields - this is because
  428. * error handling may need to overwrite these with different values
  429. * to run different commands, and once error handling is complete,
  430. * we will need to restore these values prior to running the actual
  431. * command.
  432. */
  433. old_buffer = scmd->request_buffer;
  434. old_bufflen = scmd->request_bufflen;
  435. memcpy(old_cmnd, scmd->cmnd, sizeof(scmd->cmnd));
  436. old_data_direction = scmd->sc_data_direction;
  437. old_cmd_len = scmd->cmd_len;
  438. old_use_sg = scmd->use_sg;
  439. memset(scmd->cmnd, 0, sizeof(scmd->cmnd));
  440. memcpy(scmd->cmnd, cmnd, cmnd_size);
  441. if (copy_sense) {
  442. gfp_t gfp_mask = GFP_ATOMIC;
  443. if (shost->hostt->unchecked_isa_dma)
  444. gfp_mask |= __GFP_DMA;
  445. scmd->sc_data_direction = DMA_FROM_DEVICE;
  446. scmd->request_bufflen = 252;
  447. scmd->request_buffer = kzalloc(scmd->request_bufflen, gfp_mask);
  448. if (!scmd->request_buffer)
  449. return FAILED;
  450. } else {
  451. scmd->request_buffer = NULL;
  452. scmd->request_bufflen = 0;
  453. scmd->sc_data_direction = DMA_NONE;
  454. }
  455. scmd->underflow = 0;
  456. scmd->use_sg = 0;
  457. scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
  458. if (sdev->scsi_level <= SCSI_2)
  459. scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
  460. (sdev->lun << 5 & 0xe0);
  461. /*
  462. * Zero the sense buffer. The scsi spec mandates that any
  463. * untransferred sense data should be interpreted as being zero.
  464. */
  465. memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
  466. shost->eh_action = &done;
  467. spin_lock_irqsave(shost->host_lock, flags);
  468. scsi_log_send(scmd);
  469. shost->hostt->queuecommand(scmd, scsi_eh_done);
  470. spin_unlock_irqrestore(shost->host_lock, flags);
  471. timeleft = wait_for_completion_timeout(&done, timeout);
  472. shost->eh_action = NULL;
  473. scsi_log_completion(scmd, SUCCESS);
  474. SCSI_LOG_ERROR_RECOVERY(3,
  475. printk("%s: scmd: %p, timeleft: %ld\n",
  476. __FUNCTION__, scmd, timeleft));
  477. /*
  478. * If there is time left scsi_eh_done got called, and we will
  479. * examine the actual status codes to see whether the command
  480. * actually did complete normally, else tell the host to forget
  481. * about this command.
  482. */
  483. if (timeleft) {
  484. rtn = scsi_eh_completed_normally(scmd);
  485. SCSI_LOG_ERROR_RECOVERY(3,
  486. printk("%s: scsi_eh_completed_normally %x\n",
  487. __FUNCTION__, rtn));
  488. switch (rtn) {
  489. case SUCCESS:
  490. case NEEDS_RETRY:
  491. case FAILED:
  492. break;
  493. default:
  494. rtn = FAILED;
  495. break;
  496. }
  497. } else {
  498. /*
  499. * FIXME(eric) - we are not tracking whether we could
  500. * abort a timed out command or not. not sure how
  501. * we should treat them differently anyways.
  502. */
  503. if (shost->hostt->eh_abort_handler)
  504. shost->hostt->eh_abort_handler(scmd);
  505. rtn = FAILED;
  506. }
  507. /*
  508. * Last chance to have valid sense data.
  509. */
  510. if (copy_sense) {
  511. if (!SCSI_SENSE_VALID(scmd)) {
  512. memcpy(scmd->sense_buffer, scmd->request_buffer,
  513. sizeof(scmd->sense_buffer));
  514. }
  515. kfree(scmd->request_buffer);
  516. }
  517. /*
  518. * Restore original data
  519. */
  520. scmd->request_buffer = old_buffer;
  521. scmd->request_bufflen = old_bufflen;
  522. memcpy(scmd->cmnd, old_cmnd, sizeof(scmd->cmnd));
  523. scmd->sc_data_direction = old_data_direction;
  524. scmd->cmd_len = old_cmd_len;
  525. scmd->use_sg = old_use_sg;
  526. scmd->result = old_result;
  527. return rtn;
  528. }
  529. /**
  530. * scsi_request_sense - Request sense data from a particular target.
  531. * @scmd: SCSI cmd for request sense.
  532. *
  533. * Notes:
  534. * Some hosts automatically obtain this information, others require
  535. * that we obtain it on our own. This function will *not* return until
  536. * the command either times out, or it completes.
  537. **/
  538. static int scsi_request_sense(struct scsi_cmnd *scmd)
  539. {
  540. static unsigned char generic_sense[6] =
  541. {REQUEST_SENSE, 0, 0, 0, 252, 0};
  542. return scsi_send_eh_cmnd(scmd, generic_sense, 6, SENSE_TIMEOUT, 1);
  543. }
  544. /**
  545. * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
  546. * @scmd: Original SCSI cmd that eh has finished.
  547. * @done_q: Queue for processed commands.
  548. *
  549. * Notes:
  550. * We don't want to use the normal command completion while we are are
  551. * still handling errors - it may cause other commands to be queued,
  552. * and that would disturb what we are doing. thus we really want to
  553. * keep a list of pending commands for final completion, and once we
  554. * are ready to leave error handling we handle completion for real.
  555. **/
  556. void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
  557. {
  558. scmd->device->host->host_failed--;
  559. scmd->eh_eflags = 0;
  560. list_move_tail(&scmd->eh_entry, done_q);
  561. }
  562. EXPORT_SYMBOL(scsi_eh_finish_cmd);
  563. /**
  564. * scsi_eh_get_sense - Get device sense data.
  565. * @work_q: Queue of commands to process.
  566. * @done_q: Queue of proccessed commands..
  567. *
  568. * Description:
  569. * See if we need to request sense information. if so, then get it
  570. * now, so we have a better idea of what to do.
  571. *
  572. * Notes:
  573. * This has the unfortunate side effect that if a shost adapter does
  574. * not automatically request sense information, that we end up shutting
  575. * it down before we request it.
  576. *
  577. * All drivers should request sense information internally these days,
  578. * so for now all I have to say is tough noogies if you end up in here.
  579. *
  580. * XXX: Long term this code should go away, but that needs an audit of
  581. * all LLDDs first.
  582. **/
  583. static int scsi_eh_get_sense(struct list_head *work_q,
  584. struct list_head *done_q)
  585. {
  586. struct scsi_cmnd *scmd, *next;
  587. int rtn;
  588. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  589. if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
  590. SCSI_SENSE_VALID(scmd))
  591. continue;
  592. SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
  593. "%s: requesting sense\n",
  594. current->comm));
  595. rtn = scsi_request_sense(scmd);
  596. if (rtn != SUCCESS)
  597. continue;
  598. SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
  599. " result %x\n", scmd,
  600. scmd->result));
  601. SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
  602. rtn = scsi_decide_disposition(scmd);
  603. /*
  604. * if the result was normal, then just pass it along to the
  605. * upper level.
  606. */
  607. if (rtn == SUCCESS)
  608. /* we don't want this command reissued, just
  609. * finished with the sense data, so set
  610. * retries to the max allowed to ensure it
  611. * won't get reissued */
  612. scmd->retries = scmd->allowed;
  613. else if (rtn != NEEDS_RETRY)
  614. continue;
  615. scsi_eh_finish_cmd(scmd, done_q);
  616. }
  617. return list_empty(work_q);
  618. }
  619. /**
  620. * scsi_try_to_abort_cmd - Ask host to abort a running command.
  621. * @scmd: SCSI cmd to abort from Lower Level.
  622. *
  623. * Notes:
  624. * This function will not return until the user's completion function
  625. * has been called. there is no timeout on this operation. if the
  626. * author of the low-level driver wishes this operation to be timed,
  627. * they can provide this facility themselves. helper functions in
  628. * scsi_error.c can be supplied to make this easier to do.
  629. **/
  630. static int scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
  631. {
  632. if (!scmd->device->host->hostt->eh_abort_handler)
  633. return FAILED;
  634. /*
  635. * scsi_done was called just after the command timed out and before
  636. * we had a chance to process it. (db)
  637. */
  638. if (scmd->serial_number == 0)
  639. return SUCCESS;
  640. return scmd->device->host->hostt->eh_abort_handler(scmd);
  641. }
  642. /**
  643. * scsi_eh_tur - Send TUR to device.
  644. * @scmd: Scsi cmd to send TUR
  645. *
  646. * Return value:
  647. * 0 - Device is ready. 1 - Device NOT ready.
  648. **/
  649. static int scsi_eh_tur(struct scsi_cmnd *scmd)
  650. {
  651. static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
  652. int retry_cnt = 1, rtn;
  653. retry_tur:
  654. rtn = scsi_send_eh_cmnd(scmd, tur_command, 6, SENSE_TIMEOUT, 0);
  655. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
  656. __FUNCTION__, scmd, rtn));
  657. switch (rtn) {
  658. case NEEDS_RETRY:
  659. if (retry_cnt--)
  660. goto retry_tur;
  661. /*FALLTHRU*/
  662. case SUCCESS:
  663. return 0;
  664. default:
  665. return 1;
  666. }
  667. }
  668. /**
  669. * scsi_eh_abort_cmds - abort canceled commands.
  670. * @shost: scsi host being recovered.
  671. * @eh_done_q: list_head for processed commands.
  672. *
  673. * Decription:
  674. * Try and see whether or not it makes sense to try and abort the
  675. * running command. this only works out to be the case if we have one
  676. * command that has timed out. if the command simply failed, it makes
  677. * no sense to try and abort the command, since as far as the shost
  678. * adapter is concerned, it isn't running.
  679. **/
  680. static int scsi_eh_abort_cmds(struct list_head *work_q,
  681. struct list_head *done_q)
  682. {
  683. struct scsi_cmnd *scmd, *next;
  684. int rtn;
  685. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  686. if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
  687. continue;
  688. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
  689. "0x%p\n", current->comm,
  690. scmd));
  691. rtn = scsi_try_to_abort_cmd(scmd);
  692. if (rtn == SUCCESS) {
  693. scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
  694. if (!scsi_device_online(scmd->device) ||
  695. !scsi_eh_tur(scmd)) {
  696. scsi_eh_finish_cmd(scmd, done_q);
  697. }
  698. } else
  699. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
  700. " cmd failed:"
  701. "0x%p\n",
  702. current->comm,
  703. scmd));
  704. }
  705. return list_empty(work_q);
  706. }
  707. /**
  708. * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
  709. * @scmd: SCSI cmd used to send BDR
  710. *
  711. * Notes:
  712. * There is no timeout for this operation. if this operation is
  713. * unreliable for a given host, then the host itself needs to put a
  714. * timer on it, and set the host back to a consistent state prior to
  715. * returning.
  716. **/
  717. static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
  718. {
  719. int rtn;
  720. if (!scmd->device->host->hostt->eh_device_reset_handler)
  721. return FAILED;
  722. rtn = scmd->device->host->hostt->eh_device_reset_handler(scmd);
  723. if (rtn == SUCCESS) {
  724. scmd->device->was_reset = 1;
  725. scmd->device->expecting_cc_ua = 1;
  726. }
  727. return rtn;
  728. }
  729. /**
  730. * scsi_eh_try_stu - Send START_UNIT to device.
  731. * @scmd: Scsi cmd to send START_UNIT
  732. *
  733. * Return value:
  734. * 0 - Device is ready. 1 - Device NOT ready.
  735. **/
  736. static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
  737. {
  738. static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
  739. if (scmd->device->allow_restart) {
  740. int rtn;
  741. rtn = scsi_send_eh_cmnd(scmd, stu_command, 6,
  742. START_UNIT_TIMEOUT, 0);
  743. if (rtn == SUCCESS)
  744. return 0;
  745. }
  746. return 1;
  747. }
  748. /**
  749. * scsi_eh_stu - send START_UNIT if needed
  750. * @shost: scsi host being recovered.
  751. * @eh_done_q: list_head for processed commands.
  752. *
  753. * Notes:
  754. * If commands are failing due to not ready, initializing command required,
  755. * try revalidating the device, which will end up sending a start unit.
  756. **/
  757. static int scsi_eh_stu(struct Scsi_Host *shost,
  758. struct list_head *work_q,
  759. struct list_head *done_q)
  760. {
  761. struct scsi_cmnd *scmd, *stu_scmd, *next;
  762. struct scsi_device *sdev;
  763. shost_for_each_device(sdev, shost) {
  764. stu_scmd = NULL;
  765. list_for_each_entry(scmd, work_q, eh_entry)
  766. if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
  767. scsi_check_sense(scmd) == FAILED ) {
  768. stu_scmd = scmd;
  769. break;
  770. }
  771. if (!stu_scmd)
  772. continue;
  773. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
  774. " 0x%p\n", current->comm, sdev));
  775. if (!scsi_eh_try_stu(stu_scmd)) {
  776. if (!scsi_device_online(sdev) ||
  777. !scsi_eh_tur(stu_scmd)) {
  778. list_for_each_entry_safe(scmd, next,
  779. work_q, eh_entry) {
  780. if (scmd->device == sdev)
  781. scsi_eh_finish_cmd(scmd, done_q);
  782. }
  783. }
  784. } else {
  785. SCSI_LOG_ERROR_RECOVERY(3,
  786. printk("%s: START_UNIT failed to sdev:"
  787. " 0x%p\n", current->comm, sdev));
  788. }
  789. }
  790. return list_empty(work_q);
  791. }
  792. /**
  793. * scsi_eh_bus_device_reset - send bdr if needed
  794. * @shost: scsi host being recovered.
  795. * @eh_done_q: list_head for processed commands.
  796. *
  797. * Notes:
  798. * Try a bus device reset. still, look to see whether we have multiple
  799. * devices that are jammed or not - if we have multiple devices, it
  800. * makes no sense to try bus_device_reset - we really would need to try
  801. * a bus_reset instead.
  802. **/
  803. static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
  804. struct list_head *work_q,
  805. struct list_head *done_q)
  806. {
  807. struct scsi_cmnd *scmd, *bdr_scmd, *next;
  808. struct scsi_device *sdev;
  809. int rtn;
  810. shost_for_each_device(sdev, shost) {
  811. bdr_scmd = NULL;
  812. list_for_each_entry(scmd, work_q, eh_entry)
  813. if (scmd->device == sdev) {
  814. bdr_scmd = scmd;
  815. break;
  816. }
  817. if (!bdr_scmd)
  818. continue;
  819. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
  820. " 0x%p\n", current->comm,
  821. sdev));
  822. rtn = scsi_try_bus_device_reset(bdr_scmd);
  823. if (rtn == SUCCESS) {
  824. if (!scsi_device_online(sdev) ||
  825. !scsi_eh_tur(bdr_scmd)) {
  826. list_for_each_entry_safe(scmd, next,
  827. work_q, eh_entry) {
  828. if (scmd->device == sdev)
  829. scsi_eh_finish_cmd(scmd,
  830. done_q);
  831. }
  832. }
  833. } else {
  834. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
  835. " failed sdev:"
  836. "0x%p\n",
  837. current->comm,
  838. sdev));
  839. }
  840. }
  841. return list_empty(work_q);
  842. }
  843. /**
  844. * scsi_try_bus_reset - ask host to perform a bus reset
  845. * @scmd: SCSI cmd to send bus reset.
  846. **/
  847. static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
  848. {
  849. unsigned long flags;
  850. int rtn;
  851. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
  852. __FUNCTION__));
  853. if (!scmd->device->host->hostt->eh_bus_reset_handler)
  854. return FAILED;
  855. rtn = scmd->device->host->hostt->eh_bus_reset_handler(scmd);
  856. if (rtn == SUCCESS) {
  857. if (!scmd->device->host->hostt->skip_settle_delay)
  858. ssleep(BUS_RESET_SETTLE_TIME);
  859. spin_lock_irqsave(scmd->device->host->host_lock, flags);
  860. scsi_report_bus_reset(scmd->device->host,
  861. scmd_channel(scmd));
  862. spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
  863. }
  864. return rtn;
  865. }
  866. /**
  867. * scsi_try_host_reset - ask host adapter to reset itself
  868. * @scmd: SCSI cmd to send hsot reset.
  869. **/
  870. static int scsi_try_host_reset(struct scsi_cmnd *scmd)
  871. {
  872. unsigned long flags;
  873. int rtn;
  874. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
  875. __FUNCTION__));
  876. if (!scmd->device->host->hostt->eh_host_reset_handler)
  877. return FAILED;
  878. rtn = scmd->device->host->hostt->eh_host_reset_handler(scmd);
  879. if (rtn == SUCCESS) {
  880. if (!scmd->device->host->hostt->skip_settle_delay)
  881. ssleep(HOST_RESET_SETTLE_TIME);
  882. spin_lock_irqsave(scmd->device->host->host_lock, flags);
  883. scsi_report_bus_reset(scmd->device->host,
  884. scmd_channel(scmd));
  885. spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
  886. }
  887. return rtn;
  888. }
  889. /**
  890. * scsi_eh_bus_reset - send a bus reset
  891. * @shost: scsi host being recovered.
  892. * @eh_done_q: list_head for processed commands.
  893. **/
  894. static int scsi_eh_bus_reset(struct Scsi_Host *shost,
  895. struct list_head *work_q,
  896. struct list_head *done_q)
  897. {
  898. struct scsi_cmnd *scmd, *chan_scmd, *next;
  899. unsigned int channel;
  900. int rtn;
  901. /*
  902. * we really want to loop over the various channels, and do this on
  903. * a channel by channel basis. we should also check to see if any
  904. * of the failed commands are on soft_reset devices, and if so, skip
  905. * the reset.
  906. */
  907. for (channel = 0; channel <= shost->max_channel; channel++) {
  908. chan_scmd = NULL;
  909. list_for_each_entry(scmd, work_q, eh_entry) {
  910. if (channel == scmd_channel(scmd)) {
  911. chan_scmd = scmd;
  912. break;
  913. /*
  914. * FIXME add back in some support for
  915. * soft_reset devices.
  916. */
  917. }
  918. }
  919. if (!chan_scmd)
  920. continue;
  921. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
  922. " %d\n", current->comm,
  923. channel));
  924. rtn = scsi_try_bus_reset(chan_scmd);
  925. if (rtn == SUCCESS) {
  926. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  927. if (channel == scmd_channel(scmd))
  928. if (!scsi_device_online(scmd->device) ||
  929. !scsi_eh_tur(scmd))
  930. scsi_eh_finish_cmd(scmd,
  931. done_q);
  932. }
  933. } else {
  934. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
  935. " failed chan: %d\n",
  936. current->comm,
  937. channel));
  938. }
  939. }
  940. return list_empty(work_q);
  941. }
  942. /**
  943. * scsi_eh_host_reset - send a host reset
  944. * @work_q: list_head for processed commands.
  945. * @done_q: list_head for processed commands.
  946. **/
  947. static int scsi_eh_host_reset(struct list_head *work_q,
  948. struct list_head *done_q)
  949. {
  950. struct scsi_cmnd *scmd, *next;
  951. int rtn;
  952. if (!list_empty(work_q)) {
  953. scmd = list_entry(work_q->next,
  954. struct scsi_cmnd, eh_entry);
  955. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
  956. , current->comm));
  957. rtn = scsi_try_host_reset(scmd);
  958. if (rtn == SUCCESS) {
  959. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  960. if (!scsi_device_online(scmd->device) ||
  961. (!scsi_eh_try_stu(scmd) && !scsi_eh_tur(scmd)) ||
  962. !scsi_eh_tur(scmd))
  963. scsi_eh_finish_cmd(scmd, done_q);
  964. }
  965. } else {
  966. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
  967. " failed\n",
  968. current->comm));
  969. }
  970. }
  971. return list_empty(work_q);
  972. }
  973. /**
  974. * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
  975. * @work_q: list_head for processed commands.
  976. * @done_q: list_head for processed commands.
  977. *
  978. **/
  979. static void scsi_eh_offline_sdevs(struct list_head *work_q,
  980. struct list_head *done_q)
  981. {
  982. struct scsi_cmnd *scmd, *next;
  983. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  984. sdev_printk(KERN_INFO, scmd->device,
  985. "scsi: Device offlined - not"
  986. " ready after error recovery\n");
  987. scsi_device_set_state(scmd->device, SDEV_OFFLINE);
  988. if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
  989. /*
  990. * FIXME: Handle lost cmds.
  991. */
  992. }
  993. scsi_eh_finish_cmd(scmd, done_q);
  994. }
  995. return;
  996. }
  997. /**
  998. * scsi_decide_disposition - Disposition a cmd on return from LLD.
  999. * @scmd: SCSI cmd to examine.
  1000. *
  1001. * Notes:
  1002. * This is *only* called when we are examining the status after sending
  1003. * out the actual data command. any commands that are queued for error
  1004. * recovery (e.g. test_unit_ready) do *not* come through here.
  1005. *
  1006. * When this routine returns failed, it means the error handler thread
  1007. * is woken. In cases where the error code indicates an error that
  1008. * doesn't require the error handler read (i.e. we don't need to
  1009. * abort/reset), this function should return SUCCESS.
  1010. **/
  1011. int scsi_decide_disposition(struct scsi_cmnd *scmd)
  1012. {
  1013. int rtn;
  1014. /*
  1015. * if the device is offline, then we clearly just pass the result back
  1016. * up to the top level.
  1017. */
  1018. if (!scsi_device_online(scmd->device)) {
  1019. SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
  1020. " as SUCCESS\n",
  1021. __FUNCTION__));
  1022. return SUCCESS;
  1023. }
  1024. /*
  1025. * first check the host byte, to see if there is anything in there
  1026. * that would indicate what we need to do.
  1027. */
  1028. switch (host_byte(scmd->result)) {
  1029. case DID_PASSTHROUGH:
  1030. /*
  1031. * no matter what, pass this through to the upper layer.
  1032. * nuke this special code so that it looks like we are saying
  1033. * did_ok.
  1034. */
  1035. scmd->result &= 0xff00ffff;
  1036. return SUCCESS;
  1037. case DID_OK:
  1038. /*
  1039. * looks good. drop through, and check the next byte.
  1040. */
  1041. break;
  1042. case DID_NO_CONNECT:
  1043. case DID_BAD_TARGET:
  1044. case DID_ABORT:
  1045. /*
  1046. * note - this means that we just report the status back
  1047. * to the top level driver, not that we actually think
  1048. * that it indicates SUCCESS.
  1049. */
  1050. return SUCCESS;
  1051. /*
  1052. * when the low level driver returns did_soft_error,
  1053. * it is responsible for keeping an internal retry counter
  1054. * in order to avoid endless loops (db)
  1055. *
  1056. * actually this is a bug in this function here. we should
  1057. * be mindful of the maximum number of retries specified
  1058. * and not get stuck in a loop.
  1059. */
  1060. case DID_SOFT_ERROR:
  1061. goto maybe_retry;
  1062. case DID_IMM_RETRY:
  1063. return NEEDS_RETRY;
  1064. case DID_REQUEUE:
  1065. return ADD_TO_MLQUEUE;
  1066. case DID_ERROR:
  1067. if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
  1068. status_byte(scmd->result) == RESERVATION_CONFLICT)
  1069. /*
  1070. * execute reservation conflict processing code
  1071. * lower down
  1072. */
  1073. break;
  1074. /* fallthrough */
  1075. case DID_BUS_BUSY:
  1076. case DID_PARITY:
  1077. goto maybe_retry;
  1078. case DID_TIME_OUT:
  1079. /*
  1080. * when we scan the bus, we get timeout messages for
  1081. * these commands if there is no device available.
  1082. * other hosts report did_no_connect for the same thing.
  1083. */
  1084. if ((scmd->cmnd[0] == TEST_UNIT_READY ||
  1085. scmd->cmnd[0] == INQUIRY)) {
  1086. return SUCCESS;
  1087. } else {
  1088. return FAILED;
  1089. }
  1090. case DID_RESET:
  1091. return SUCCESS;
  1092. default:
  1093. return FAILED;
  1094. }
  1095. /*
  1096. * next, check the message byte.
  1097. */
  1098. if (msg_byte(scmd->result) != COMMAND_COMPLETE)
  1099. return FAILED;
  1100. /*
  1101. * check the status byte to see if this indicates anything special.
  1102. */
  1103. switch (status_byte(scmd->result)) {
  1104. case QUEUE_FULL:
  1105. /*
  1106. * the case of trying to send too many commands to a
  1107. * tagged queueing device.
  1108. */
  1109. case BUSY:
  1110. /*
  1111. * device can't talk to us at the moment. Should only
  1112. * occur (SAM-3) when the task queue is empty, so will cause
  1113. * the empty queue handling to trigger a stall in the
  1114. * device.
  1115. */
  1116. return ADD_TO_MLQUEUE;
  1117. case GOOD:
  1118. case COMMAND_TERMINATED:
  1119. case TASK_ABORTED:
  1120. return SUCCESS;
  1121. case CHECK_CONDITION:
  1122. rtn = scsi_check_sense(scmd);
  1123. if (rtn == NEEDS_RETRY)
  1124. goto maybe_retry;
  1125. /* if rtn == FAILED, we have no sense information;
  1126. * returning FAILED will wake the error handler thread
  1127. * to collect the sense and redo the decide
  1128. * disposition */
  1129. return rtn;
  1130. case CONDITION_GOOD:
  1131. case INTERMEDIATE_GOOD:
  1132. case INTERMEDIATE_C_GOOD:
  1133. case ACA_ACTIVE:
  1134. /*
  1135. * who knows? FIXME(eric)
  1136. */
  1137. return SUCCESS;
  1138. case RESERVATION_CONFLICT:
  1139. sdev_printk(KERN_INFO, scmd->device,
  1140. "reservation conflict\n");
  1141. return SUCCESS; /* causes immediate i/o error */
  1142. default:
  1143. return FAILED;
  1144. }
  1145. return FAILED;
  1146. maybe_retry:
  1147. /* we requeue for retry because the error was retryable, and
  1148. * the request was not marked fast fail. Note that above,
  1149. * even if the request is marked fast fail, we still requeue
  1150. * for queue congestion conditions (QUEUE_FULL or BUSY) */
  1151. if ((++scmd->retries) <= scmd->allowed
  1152. && !blk_noretry_request(scmd->request)) {
  1153. return NEEDS_RETRY;
  1154. } else {
  1155. /*
  1156. * no more retries - report this one back to upper level.
  1157. */
  1158. return SUCCESS;
  1159. }
  1160. }
  1161. /**
  1162. * scsi_eh_lock_door - Prevent medium removal for the specified device
  1163. * @sdev: SCSI device to prevent medium removal
  1164. *
  1165. * Locking:
  1166. * We must be called from process context; scsi_allocate_request()
  1167. * may sleep.
  1168. *
  1169. * Notes:
  1170. * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
  1171. * head of the devices request queue, and continue.
  1172. *
  1173. * Bugs:
  1174. * scsi_allocate_request() may sleep waiting for existing requests to
  1175. * be processed. However, since we haven't kicked off any request
  1176. * processing for this host, this may deadlock.
  1177. *
  1178. * If scsi_allocate_request() fails for what ever reason, we
  1179. * completely forget to lock the door.
  1180. **/
  1181. static void scsi_eh_lock_door(struct scsi_device *sdev)
  1182. {
  1183. unsigned char cmnd[MAX_COMMAND_SIZE];
  1184. cmnd[0] = ALLOW_MEDIUM_REMOVAL;
  1185. cmnd[1] = 0;
  1186. cmnd[2] = 0;
  1187. cmnd[3] = 0;
  1188. cmnd[4] = SCSI_REMOVAL_PREVENT;
  1189. cmnd[5] = 0;
  1190. scsi_execute_async(sdev, cmnd, 6, DMA_NONE, NULL, 0, 0, 10 * HZ,
  1191. 5, NULL, NULL, GFP_KERNEL);
  1192. }
  1193. /**
  1194. * scsi_restart_operations - restart io operations to the specified host.
  1195. * @shost: Host we are restarting.
  1196. *
  1197. * Notes:
  1198. * When we entered the error handler, we blocked all further i/o to
  1199. * this device. we need to 'reverse' this process.
  1200. **/
  1201. static void scsi_restart_operations(struct Scsi_Host *shost)
  1202. {
  1203. struct scsi_device *sdev;
  1204. unsigned long flags;
  1205. /*
  1206. * If the door was locked, we need to insert a door lock request
  1207. * onto the head of the SCSI request queue for the device. There
  1208. * is no point trying to lock the door of an off-line device.
  1209. */
  1210. shost_for_each_device(sdev, shost) {
  1211. if (scsi_device_online(sdev) && sdev->locked)
  1212. scsi_eh_lock_door(sdev);
  1213. }
  1214. /*
  1215. * next free up anything directly waiting upon the host. this
  1216. * will be requests for character device operations, and also for
  1217. * ioctls to queued block devices.
  1218. */
  1219. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: waking up host to restart\n",
  1220. __FUNCTION__));
  1221. spin_lock_irqsave(shost->host_lock, flags);
  1222. if (scsi_host_set_state(shost, SHOST_RUNNING))
  1223. if (scsi_host_set_state(shost, SHOST_CANCEL))
  1224. BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
  1225. spin_unlock_irqrestore(shost->host_lock, flags);
  1226. wake_up(&shost->host_wait);
  1227. /*
  1228. * finally we need to re-initiate requests that may be pending. we will
  1229. * have had everything blocked while error handling is taking place, and
  1230. * now that error recovery is done, we will need to ensure that these
  1231. * requests are started.
  1232. */
  1233. scsi_run_host_queues(shost);
  1234. }
  1235. /**
  1236. * scsi_eh_ready_devs - check device ready state and recover if not.
  1237. * @shost: host to be recovered.
  1238. * @eh_done_q: list_head for processed commands.
  1239. *
  1240. **/
  1241. static void scsi_eh_ready_devs(struct Scsi_Host *shost,
  1242. struct list_head *work_q,
  1243. struct list_head *done_q)
  1244. {
  1245. if (!scsi_eh_stu(shost, work_q, done_q))
  1246. if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
  1247. if (!scsi_eh_bus_reset(shost, work_q, done_q))
  1248. if (!scsi_eh_host_reset(work_q, done_q))
  1249. scsi_eh_offline_sdevs(work_q, done_q);
  1250. }
  1251. /**
  1252. * scsi_eh_flush_done_q - finish processed commands or retry them.
  1253. * @done_q: list_head of processed commands.
  1254. *
  1255. **/
  1256. void scsi_eh_flush_done_q(struct list_head *done_q)
  1257. {
  1258. struct scsi_cmnd *scmd, *next;
  1259. list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
  1260. list_del_init(&scmd->eh_entry);
  1261. if (scsi_device_online(scmd->device) &&
  1262. !blk_noretry_request(scmd->request) &&
  1263. (++scmd->retries <= scmd->allowed)) {
  1264. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
  1265. " retry cmd: %p\n",
  1266. current->comm,
  1267. scmd));
  1268. scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
  1269. } else {
  1270. /*
  1271. * If just we got sense for the device (called
  1272. * scsi_eh_get_sense), scmd->result is already
  1273. * set, do not set DRIVER_TIMEOUT.
  1274. */
  1275. if (!scmd->result)
  1276. scmd->result |= (DRIVER_TIMEOUT << 24);
  1277. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
  1278. " cmd: %p\n",
  1279. current->comm, scmd));
  1280. scsi_finish_command(scmd);
  1281. }
  1282. }
  1283. }
  1284. EXPORT_SYMBOL(scsi_eh_flush_done_q);
  1285. /**
  1286. * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
  1287. * @shost: Host to unjam.
  1288. *
  1289. * Notes:
  1290. * When we come in here, we *know* that all commands on the bus have
  1291. * either completed, failed or timed out. we also know that no further
  1292. * commands are being sent to the host, so things are relatively quiet
  1293. * and we have freedom to fiddle with things as we wish.
  1294. *
  1295. * This is only the *default* implementation. it is possible for
  1296. * individual drivers to supply their own version of this function, and
  1297. * if the maintainer wishes to do this, it is strongly suggested that
  1298. * this function be taken as a template and modified. this function
  1299. * was designed to correctly handle problems for about 95% of the
  1300. * different cases out there, and it should always provide at least a
  1301. * reasonable amount of error recovery.
  1302. *
  1303. * Any command marked 'failed' or 'timeout' must eventually have
  1304. * scsi_finish_cmd() called for it. we do all of the retry stuff
  1305. * here, so when we restart the host after we return it should have an
  1306. * empty queue.
  1307. **/
  1308. static void scsi_unjam_host(struct Scsi_Host *shost)
  1309. {
  1310. unsigned long flags;
  1311. LIST_HEAD(eh_work_q);
  1312. LIST_HEAD(eh_done_q);
  1313. spin_lock_irqsave(shost->host_lock, flags);
  1314. list_splice_init(&shost->eh_cmd_q, &eh_work_q);
  1315. spin_unlock_irqrestore(shost->host_lock, flags);
  1316. SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
  1317. if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
  1318. if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
  1319. scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
  1320. scsi_eh_flush_done_q(&eh_done_q);
  1321. }
  1322. /**
  1323. * scsi_error_handler - SCSI error handler thread
  1324. * @data: Host for which we are running.
  1325. *
  1326. * Notes:
  1327. * This is the main error handling loop. This is run as a kernel thread
  1328. * for every SCSI host and handles all error handling activity.
  1329. **/
  1330. int scsi_error_handler(void *data)
  1331. {
  1332. struct Scsi_Host *shost = data;
  1333. current->flags |= PF_NOFREEZE;
  1334. /*
  1335. * We use TASK_INTERRUPTIBLE so that the thread is not
  1336. * counted against the load average as a running process.
  1337. * We never actually get interrupted because kthread_run
  1338. * disables singal delivery for the created thread.
  1339. */
  1340. set_current_state(TASK_INTERRUPTIBLE);
  1341. while (!kthread_should_stop()) {
  1342. if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
  1343. shost->host_failed != shost->host_busy) {
  1344. SCSI_LOG_ERROR_RECOVERY(1,
  1345. printk("Error handler scsi_eh_%d sleeping\n",
  1346. shost->host_no));
  1347. schedule();
  1348. set_current_state(TASK_INTERRUPTIBLE);
  1349. continue;
  1350. }
  1351. __set_current_state(TASK_RUNNING);
  1352. SCSI_LOG_ERROR_RECOVERY(1,
  1353. printk("Error handler scsi_eh_%d waking up\n",
  1354. shost->host_no));
  1355. /*
  1356. * We have a host that is failing for some reason. Figure out
  1357. * what we need to do to get it up and online again (if we can).
  1358. * If we fail, we end up taking the thing offline.
  1359. */
  1360. if (shost->transportt->eh_strategy_handler)
  1361. shost->transportt->eh_strategy_handler(shost);
  1362. else
  1363. scsi_unjam_host(shost);
  1364. /*
  1365. * Note - if the above fails completely, the action is to take
  1366. * individual devices offline and flush the queue of any
  1367. * outstanding requests that may have been pending. When we
  1368. * restart, we restart any I/O to any other devices on the bus
  1369. * which are still online.
  1370. */
  1371. scsi_restart_operations(shost);
  1372. set_current_state(TASK_INTERRUPTIBLE);
  1373. }
  1374. __set_current_state(TASK_RUNNING);
  1375. SCSI_LOG_ERROR_RECOVERY(1,
  1376. printk("Error handler scsi_eh_%d exiting\n", shost->host_no));
  1377. shost->ehandler = NULL;
  1378. return 0;
  1379. }
  1380. /*
  1381. * Function: scsi_report_bus_reset()
  1382. *
  1383. * Purpose: Utility function used by low-level drivers to report that
  1384. * they have observed a bus reset on the bus being handled.
  1385. *
  1386. * Arguments: shost - Host in question
  1387. * channel - channel on which reset was observed.
  1388. *
  1389. * Returns: Nothing
  1390. *
  1391. * Lock status: Host lock must be held.
  1392. *
  1393. * Notes: This only needs to be called if the reset is one which
  1394. * originates from an unknown location. Resets originated
  1395. * by the mid-level itself don't need to call this, but there
  1396. * should be no harm.
  1397. *
  1398. * The main purpose of this is to make sure that a CHECK_CONDITION
  1399. * is properly treated.
  1400. */
  1401. void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
  1402. {
  1403. struct scsi_device *sdev;
  1404. __shost_for_each_device(sdev, shost) {
  1405. if (channel == sdev_channel(sdev)) {
  1406. sdev->was_reset = 1;
  1407. sdev->expecting_cc_ua = 1;
  1408. }
  1409. }
  1410. }
  1411. EXPORT_SYMBOL(scsi_report_bus_reset);
  1412. /*
  1413. * Function: scsi_report_device_reset()
  1414. *
  1415. * Purpose: Utility function used by low-level drivers to report that
  1416. * they have observed a device reset on the device being handled.
  1417. *
  1418. * Arguments: shost - Host in question
  1419. * channel - channel on which reset was observed
  1420. * target - target on which reset was observed
  1421. *
  1422. * Returns: Nothing
  1423. *
  1424. * Lock status: Host lock must be held
  1425. *
  1426. * Notes: This only needs to be called if the reset is one which
  1427. * originates from an unknown location. Resets originated
  1428. * by the mid-level itself don't need to call this, but there
  1429. * should be no harm.
  1430. *
  1431. * The main purpose of this is to make sure that a CHECK_CONDITION
  1432. * is properly treated.
  1433. */
  1434. void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
  1435. {
  1436. struct scsi_device *sdev;
  1437. __shost_for_each_device(sdev, shost) {
  1438. if (channel == sdev_channel(sdev) &&
  1439. target == sdev_id(sdev)) {
  1440. sdev->was_reset = 1;
  1441. sdev->expecting_cc_ua = 1;
  1442. }
  1443. }
  1444. }
  1445. EXPORT_SYMBOL(scsi_report_device_reset);
  1446. static void
  1447. scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
  1448. {
  1449. }
  1450. /*
  1451. * Function: scsi_reset_provider
  1452. *
  1453. * Purpose: Send requested reset to a bus or device at any phase.
  1454. *
  1455. * Arguments: device - device to send reset to
  1456. * flag - reset type (see scsi.h)
  1457. *
  1458. * Returns: SUCCESS/FAILURE.
  1459. *
  1460. * Notes: This is used by the SCSI Generic driver to provide
  1461. * Bus/Device reset capability.
  1462. */
  1463. int
  1464. scsi_reset_provider(struct scsi_device *dev, int flag)
  1465. {
  1466. struct scsi_cmnd *scmd = scsi_get_command(dev, GFP_KERNEL);
  1467. struct Scsi_Host *shost = dev->host;
  1468. struct request req;
  1469. unsigned long flags;
  1470. int rtn;
  1471. scmd->request = &req;
  1472. memset(&scmd->eh_timeout, 0, sizeof(scmd->eh_timeout));
  1473. memset(&scmd->cmnd, '\0', sizeof(scmd->cmnd));
  1474. scmd->scsi_done = scsi_reset_provider_done_command;
  1475. scmd->done = NULL;
  1476. scmd->request_buffer = NULL;
  1477. scmd->request_bufflen = 0;
  1478. scmd->cmd_len = 0;
  1479. scmd->sc_data_direction = DMA_BIDIRECTIONAL;
  1480. init_timer(&scmd->eh_timeout);
  1481. /*
  1482. * Sometimes the command can get back into the timer chain,
  1483. * so use the pid as an identifier.
  1484. */
  1485. scmd->pid = 0;
  1486. spin_lock_irqsave(shost->host_lock, flags);
  1487. shost->tmf_in_progress = 1;
  1488. spin_unlock_irqrestore(shost->host_lock, flags);
  1489. switch (flag) {
  1490. case SCSI_TRY_RESET_DEVICE:
  1491. rtn = scsi_try_bus_device_reset(scmd);
  1492. if (rtn == SUCCESS)
  1493. break;
  1494. /* FALLTHROUGH */
  1495. case SCSI_TRY_RESET_BUS:
  1496. rtn = scsi_try_bus_reset(scmd);
  1497. if (rtn == SUCCESS)
  1498. break;
  1499. /* FALLTHROUGH */
  1500. case SCSI_TRY_RESET_HOST:
  1501. rtn = scsi_try_host_reset(scmd);
  1502. break;
  1503. default:
  1504. rtn = FAILED;
  1505. }
  1506. spin_lock_irqsave(shost->host_lock, flags);
  1507. shost->tmf_in_progress = 0;
  1508. spin_unlock_irqrestore(shost->host_lock, flags);
  1509. /*
  1510. * be sure to wake up anyone who was sleeping or had their queue
  1511. * suspended while we performed the TMF.
  1512. */
  1513. SCSI_LOG_ERROR_RECOVERY(3,
  1514. printk("%s: waking up host to restart after TMF\n",
  1515. __FUNCTION__));
  1516. wake_up(&shost->host_wait);
  1517. scsi_run_host_queues(shost);
  1518. scsi_next_command(scmd);
  1519. return rtn;
  1520. }
  1521. EXPORT_SYMBOL(scsi_reset_provider);
  1522. /**
  1523. * scsi_normalize_sense - normalize main elements from either fixed or
  1524. * descriptor sense data format into a common format.
  1525. *
  1526. * @sense_buffer: byte array containing sense data returned by device
  1527. * @sb_len: number of valid bytes in sense_buffer
  1528. * @sshdr: pointer to instance of structure that common
  1529. * elements are written to.
  1530. *
  1531. * Notes:
  1532. * The "main elements" from sense data are: response_code, sense_key,
  1533. * asc, ascq and additional_length (only for descriptor format).
  1534. *
  1535. * Typically this function can be called after a device has
  1536. * responded to a SCSI command with the CHECK_CONDITION status.
  1537. *
  1538. * Return value:
  1539. * 1 if valid sense data information found, else 0;
  1540. **/
  1541. int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
  1542. struct scsi_sense_hdr *sshdr)
  1543. {
  1544. if (!sense_buffer || !sb_len)
  1545. return 0;
  1546. memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
  1547. sshdr->response_code = (sense_buffer[0] & 0x7f);
  1548. if (!scsi_sense_valid(sshdr))
  1549. return 0;
  1550. if (sshdr->response_code >= 0x72) {
  1551. /*
  1552. * descriptor format
  1553. */
  1554. if (sb_len > 1)
  1555. sshdr->sense_key = (sense_buffer[1] & 0xf);
  1556. if (sb_len > 2)
  1557. sshdr->asc = sense_buffer[2];
  1558. if (sb_len > 3)
  1559. sshdr->ascq = sense_buffer[3];
  1560. if (sb_len > 7)
  1561. sshdr->additional_length = sense_buffer[7];
  1562. } else {
  1563. /*
  1564. * fixed format
  1565. */
  1566. if (sb_len > 2)
  1567. sshdr->sense_key = (sense_buffer[2] & 0xf);
  1568. if (sb_len > 7) {
  1569. sb_len = (sb_len < (sense_buffer[7] + 8)) ?
  1570. sb_len : (sense_buffer[7] + 8);
  1571. if (sb_len > 12)
  1572. sshdr->asc = sense_buffer[12];
  1573. if (sb_len > 13)
  1574. sshdr->ascq = sense_buffer[13];
  1575. }
  1576. }
  1577. return 1;
  1578. }
  1579. EXPORT_SYMBOL(scsi_normalize_sense);
  1580. int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
  1581. struct scsi_sense_hdr *sshdr)
  1582. {
  1583. return scsi_normalize_sense(cmd->sense_buffer,
  1584. sizeof(cmd->sense_buffer), sshdr);
  1585. }
  1586. EXPORT_SYMBOL(scsi_command_normalize_sense);
  1587. /**
  1588. * scsi_sense_desc_find - search for a given descriptor type in
  1589. * descriptor sense data format.
  1590. *
  1591. * @sense_buffer: byte array of descriptor format sense data
  1592. * @sb_len: number of valid bytes in sense_buffer
  1593. * @desc_type: value of descriptor type to find
  1594. * (e.g. 0 -> information)
  1595. *
  1596. * Notes:
  1597. * only valid when sense data is in descriptor format
  1598. *
  1599. * Return value:
  1600. * pointer to start of (first) descriptor if found else NULL
  1601. **/
  1602. const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
  1603. int desc_type)
  1604. {
  1605. int add_sen_len, add_len, desc_len, k;
  1606. const u8 * descp;
  1607. if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
  1608. return NULL;
  1609. if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
  1610. return NULL;
  1611. add_sen_len = (add_sen_len < (sb_len - 8)) ?
  1612. add_sen_len : (sb_len - 8);
  1613. descp = &sense_buffer[8];
  1614. for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
  1615. descp += desc_len;
  1616. add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
  1617. desc_len = add_len + 2;
  1618. if (descp[0] == desc_type)
  1619. return descp;
  1620. if (add_len < 0) // short descriptor ??
  1621. break;
  1622. }
  1623. return NULL;
  1624. }
  1625. EXPORT_SYMBOL(scsi_sense_desc_find);
  1626. /**
  1627. * scsi_get_sense_info_fld - attempts to get information field from
  1628. * sense data (either fixed or descriptor format)
  1629. *
  1630. * @sense_buffer: byte array of sense data
  1631. * @sb_len: number of valid bytes in sense_buffer
  1632. * @info_out: pointer to 64 integer where 8 or 4 byte information
  1633. * field will be placed if found.
  1634. *
  1635. * Return value:
  1636. * 1 if information field found, 0 if not found.
  1637. **/
  1638. int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
  1639. u64 * info_out)
  1640. {
  1641. int j;
  1642. const u8 * ucp;
  1643. u64 ull;
  1644. if (sb_len < 7)
  1645. return 0;
  1646. switch (sense_buffer[0] & 0x7f) {
  1647. case 0x70:
  1648. case 0x71:
  1649. if (sense_buffer[0] & 0x80) {
  1650. *info_out = (sense_buffer[3] << 24) +
  1651. (sense_buffer[4] << 16) +
  1652. (sense_buffer[5] << 8) + sense_buffer[6];
  1653. return 1;
  1654. } else
  1655. return 0;
  1656. case 0x72:
  1657. case 0x73:
  1658. ucp = scsi_sense_desc_find(sense_buffer, sb_len,
  1659. 0 /* info desc */);
  1660. if (ucp && (0xa == ucp[1])) {
  1661. ull = 0;
  1662. for (j = 0; j < 8; ++j) {
  1663. if (j > 0)
  1664. ull <<= 8;
  1665. ull |= ucp[4 + j];
  1666. }
  1667. *info_out = ull;
  1668. return 1;
  1669. } else
  1670. return 0;
  1671. default:
  1672. return 0;
  1673. }
  1674. }
  1675. EXPORT_SYMBOL(scsi_get_sense_info_fld);