scsi_error.c 50 KB

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