scsi_error.c 61 KB

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