scsi_error.c 50 KB

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