scsi_error.c 54 KB

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