scsi_error.c 54 KB

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