scsi_error.c 52 KB

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