scsi_error.c 61 KB

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