scsi_error.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * scsi_error.c Copyright (C) 1997 Eric Youngdale
  4. *
  5. * SCSI error/timeout handling
  6. * Initial versions: Eric Youngdale. Based upon conversations with
  7. * Leonard Zubkoff and David Miller at Linux Expo,
  8. * ideas originating from all over the place.
  9. *
  10. * Restructured scsi_unjam_host and associated functions.
  11. * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
  12. *
  13. * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
  14. * minor cleanups.
  15. * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
  16. */
  17. #include <linux/module.h>
  18. #include <linux/sched.h>
  19. #include <linux/gfp.h>
  20. #include <linux/timer.h>
  21. #include <linux/string.h>
  22. #include <linux/kernel.h>
  23. #include <linux/freezer.h>
  24. #include <linux/kthread.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/delay.h>
  28. #include <linux/jiffies.h>
  29. #include <scsi/scsi.h>
  30. #include <scsi/scsi_cmnd.h>
  31. #include <scsi/scsi_dbg.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_driver.h>
  34. #include <scsi/scsi_eh.h>
  35. #include <scsi/scsi_common.h>
  36. #include <scsi/scsi_transport.h>
  37. #include <scsi/scsi_host.h>
  38. #include <scsi/scsi_ioctl.h>
  39. #include <scsi/scsi_dh.h>
  40. #include <scsi/scsi_devinfo.h>
  41. #include <scsi/sg.h>
  42. #include "scsi_priv.h"
  43. #include "scsi_logging.h"
  44. #include "scsi_transport_api.h"
  45. #include <trace/events/scsi.h>
  46. #include <linux/unaligned.h>
  47. /*
  48. * These should *probably* be handled by the host itself.
  49. * Since it is allowed to sleep, it probably should.
  50. */
  51. #define BUS_RESET_SETTLE_TIME (10)
  52. #define HOST_RESET_SETTLE_TIME (10)
  53. static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
  54. static enum scsi_disposition scsi_try_to_abort_cmd(const struct scsi_host_template *,
  55. struct scsi_cmnd *);
  56. void scsi_eh_wakeup(struct Scsi_Host *shost, unsigned int busy)
  57. {
  58. lockdep_assert_held(shost->host_lock);
  59. if (busy == shost->host_failed) {
  60. trace_scsi_eh_wakeup(shost);
  61. wake_up_process(shost->ehandler);
  62. SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
  63. "Waking error handler thread\n"));
  64. }
  65. }
  66. /**
  67. * scsi_schedule_eh - schedule EH for SCSI host
  68. * @shost: SCSI host to invoke error handling on.
  69. *
  70. * Schedule SCSI EH without scmd.
  71. */
  72. void scsi_schedule_eh(struct Scsi_Host *shost)
  73. {
  74. unsigned long flags;
  75. spin_lock_irqsave(shost->host_lock, flags);
  76. if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
  77. scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
  78. shost->host_eh_scheduled++;
  79. scsi_eh_wakeup(shost, scsi_host_busy(shost));
  80. }
  81. spin_unlock_irqrestore(shost->host_lock, flags);
  82. }
  83. EXPORT_SYMBOL_GPL(scsi_schedule_eh);
  84. static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
  85. {
  86. if (!shost->last_reset || shost->eh_deadline == -1)
  87. return 0;
  88. /*
  89. * 32bit accesses are guaranteed to be atomic
  90. * (on all supported architectures), so instead
  91. * of using a spinlock we can as well double check
  92. * if eh_deadline has been set to 'off' during the
  93. * time_before call.
  94. */
  95. if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
  96. shost->eh_deadline > -1)
  97. return 0;
  98. return 1;
  99. }
  100. static bool scsi_cmd_retry_allowed(struct scsi_cmnd *cmd)
  101. {
  102. if (cmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
  103. return true;
  104. return ++cmd->retries <= cmd->allowed;
  105. }
  106. static bool scsi_eh_should_retry_cmd(struct scsi_cmnd *cmd)
  107. {
  108. struct scsi_device *sdev = cmd->device;
  109. struct Scsi_Host *host = sdev->host;
  110. if (host->hostt->eh_should_retry_cmd)
  111. return host->hostt->eh_should_retry_cmd(cmd);
  112. return true;
  113. }
  114. /**
  115. * scmd_eh_abort_handler - Handle command aborts
  116. * @work: command to be aborted.
  117. *
  118. * Note: this function must be called only for a command that has timed out.
  119. * Because the block layer marks a request as complete before it calls
  120. * scsi_timeout(), a .scsi_done() call from the LLD for a command that has
  121. * timed out do not have any effect. Hence it is safe to call
  122. * scsi_finish_command() from this function.
  123. */
  124. void
  125. scmd_eh_abort_handler(struct work_struct *work)
  126. {
  127. struct scsi_cmnd *scmd =
  128. container_of(work, struct scsi_cmnd, abort_work.work);
  129. struct scsi_device *sdev = scmd->device;
  130. struct Scsi_Host *shost = sdev->host;
  131. enum scsi_disposition rtn;
  132. unsigned long flags;
  133. if (scsi_host_eh_past_deadline(shost)) {
  134. SCSI_LOG_ERROR_RECOVERY(3,
  135. scmd_printk(KERN_INFO, scmd,
  136. "eh timeout, not aborting\n"));
  137. goto out;
  138. }
  139. SCSI_LOG_ERROR_RECOVERY(3,
  140. scmd_printk(KERN_INFO, scmd,
  141. "aborting command\n"));
  142. rtn = scsi_try_to_abort_cmd(shost->hostt, scmd);
  143. if (rtn != SUCCESS) {
  144. SCSI_LOG_ERROR_RECOVERY(3,
  145. scmd_printk(KERN_INFO, scmd,
  146. "cmd abort %s\n",
  147. (rtn == FAST_IO_FAIL) ?
  148. "not send" : "failed"));
  149. goto out;
  150. }
  151. set_host_byte(scmd, DID_TIME_OUT);
  152. if (scsi_host_eh_past_deadline(shost)) {
  153. SCSI_LOG_ERROR_RECOVERY(3,
  154. scmd_printk(KERN_INFO, scmd,
  155. "eh timeout, not retrying "
  156. "aborted command\n"));
  157. goto out;
  158. }
  159. spin_lock_irqsave(shost->host_lock, flags);
  160. list_del_init(&scmd->eh_entry);
  161. /*
  162. * If the abort succeeds, and there is no further
  163. * EH action, clear the ->last_reset time.
  164. */
  165. if (list_empty(&shost->eh_abort_list) &&
  166. list_empty(&shost->eh_cmd_q))
  167. if (shost->eh_deadline != -1)
  168. shost->last_reset = 0;
  169. spin_unlock_irqrestore(shost->host_lock, flags);
  170. if (!scsi_noretry_cmd(scmd) &&
  171. scsi_cmd_retry_allowed(scmd) &&
  172. scsi_eh_should_retry_cmd(scmd)) {
  173. SCSI_LOG_ERROR_RECOVERY(3,
  174. scmd_printk(KERN_WARNING, scmd,
  175. "retry aborted command\n"));
  176. scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
  177. } else {
  178. SCSI_LOG_ERROR_RECOVERY(3,
  179. scmd_printk(KERN_WARNING, scmd,
  180. "finish aborted command\n"));
  181. scsi_finish_command(scmd);
  182. }
  183. return;
  184. out:
  185. spin_lock_irqsave(shost->host_lock, flags);
  186. list_del_init(&scmd->eh_entry);
  187. spin_unlock_irqrestore(shost->host_lock, flags);
  188. scsi_eh_scmd_add(scmd);
  189. }
  190. /**
  191. * scsi_abort_command - schedule a command abort
  192. * @scmd: scmd to abort.
  193. *
  194. * We only need to abort commands after a command timeout
  195. */
  196. static int
  197. scsi_abort_command(struct scsi_cmnd *scmd)
  198. {
  199. struct scsi_device *sdev = scmd->device;
  200. struct Scsi_Host *shost = sdev->host;
  201. unsigned long flags;
  202. if (!shost->hostt->eh_abort_handler) {
  203. /* No abort handler, fail command directly */
  204. return FAILED;
  205. }
  206. if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
  207. /*
  208. * Retry after abort failed, escalate to next level.
  209. */
  210. SCSI_LOG_ERROR_RECOVERY(3,
  211. scmd_printk(KERN_INFO, scmd,
  212. "previous abort failed\n"));
  213. BUG_ON(delayed_work_pending(&scmd->abort_work));
  214. return FAILED;
  215. }
  216. spin_lock_irqsave(shost->host_lock, flags);
  217. if (shost->eh_deadline != -1 && !shost->last_reset)
  218. shost->last_reset = jiffies;
  219. BUG_ON(!list_empty(&scmd->eh_entry));
  220. list_add_tail(&scmd->eh_entry, &shost->eh_abort_list);
  221. spin_unlock_irqrestore(shost->host_lock, flags);
  222. scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
  223. SCSI_LOG_ERROR_RECOVERY(3,
  224. scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
  225. queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
  226. return SUCCESS;
  227. }
  228. /**
  229. * scsi_eh_reset - call into ->eh_action to reset internal counters
  230. * @scmd: scmd to run eh on.
  231. *
  232. * The scsi driver might be carrying internal state about the
  233. * devices, so we need to call into the driver to reset the
  234. * internal state once the error handler is started.
  235. */
  236. static void scsi_eh_reset(struct scsi_cmnd *scmd)
  237. {
  238. if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) {
  239. struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
  240. if (sdrv->eh_reset)
  241. sdrv->eh_reset(scmd);
  242. }
  243. }
  244. static void scsi_eh_inc_host_failed(struct rcu_head *head)
  245. {
  246. struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu);
  247. struct Scsi_Host *shost = scmd->device->host;
  248. unsigned int busy;
  249. unsigned long flags;
  250. spin_lock_irqsave(shost->host_lock, flags);
  251. shost->host_failed++;
  252. spin_unlock_irqrestore(shost->host_lock, flags);
  253. /*
  254. * The counting of busy requests needs to occur after adding to
  255. * host_failed or after the lock acquire for adding to host_failed
  256. * to prevent a race with host unbusy and missing an eh wakeup.
  257. */
  258. busy = scsi_host_busy(shost);
  259. spin_lock_irqsave(shost->host_lock, flags);
  260. scsi_eh_wakeup(shost, busy);
  261. spin_unlock_irqrestore(shost->host_lock, flags);
  262. }
  263. /**
  264. * scsi_eh_scmd_add - add scsi cmd to error handling.
  265. * @scmd: scmd to run eh on.
  266. */
  267. void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
  268. {
  269. struct Scsi_Host *shost = scmd->device->host;
  270. unsigned long flags;
  271. int ret;
  272. WARN_ON_ONCE(!shost->ehandler);
  273. WARN_ON_ONCE(!test_bit(SCMD_STATE_INFLIGHT, &scmd->state));
  274. spin_lock_irqsave(shost->host_lock, flags);
  275. if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
  276. ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
  277. WARN_ON_ONCE(ret);
  278. }
  279. if (shost->eh_deadline != -1 && !shost->last_reset)
  280. shost->last_reset = jiffies;
  281. scsi_eh_reset(scmd);
  282. list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
  283. spin_unlock_irqrestore(shost->host_lock, flags);
  284. /*
  285. * Ensure that all tasks observe the host state change before the
  286. * host_failed change.
  287. */
  288. call_rcu_hurry(&scmd->rcu, scsi_eh_inc_host_failed);
  289. }
  290. /**
  291. * scsi_timeout - Timeout function for normal scsi commands.
  292. * @req: request that is timing out.
  293. *
  294. * Notes:
  295. * We do not need to lock this. There is the potential for a race
  296. * only in that the normal completion handling might run, but if the
  297. * normal completion function determines that the timer has already
  298. * fired, then it mustn't do anything.
  299. */
  300. enum blk_eh_timer_return scsi_timeout(struct request *req)
  301. {
  302. struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
  303. struct Scsi_Host *host = scmd->device->host;
  304. trace_scsi_dispatch_cmd_timeout(scmd);
  305. scsi_log_completion(scmd, TIMEOUT_ERROR);
  306. atomic_inc(&scmd->device->iotmo_cnt);
  307. if (host->eh_deadline != -1 && !host->last_reset)
  308. host->last_reset = jiffies;
  309. if (host->hostt->eh_timed_out) {
  310. switch (host->hostt->eh_timed_out(scmd)) {
  311. case SCSI_EH_DONE:
  312. return BLK_EH_DONE;
  313. case SCSI_EH_RESET_TIMER:
  314. return BLK_EH_RESET_TIMER;
  315. case SCSI_EH_NOT_HANDLED:
  316. break;
  317. }
  318. }
  319. /*
  320. * If scsi_done() has already set SCMD_STATE_COMPLETE, do not modify
  321. * *scmd.
  322. */
  323. if (test_and_set_bit(SCMD_STATE_COMPLETE, &scmd->state))
  324. return BLK_EH_DONE;
  325. atomic_inc(&scmd->device->iodone_cnt);
  326. if (scsi_abort_command(scmd) != SUCCESS) {
  327. set_host_byte(scmd, DID_TIME_OUT);
  328. scsi_eh_scmd_add(scmd);
  329. }
  330. return BLK_EH_DONE;
  331. }
  332. /**
  333. * scsi_block_when_processing_errors - Prevent cmds from being queued.
  334. * @sdev: Device on which we are performing recovery.
  335. *
  336. * Description:
  337. * We block until the host is out of error recovery, and then check to
  338. * see whether the host or the device is offline.
  339. *
  340. * Return value:
  341. * 0 when dev was taken offline by error recovery. 1 OK to proceed.
  342. */
  343. int scsi_block_when_processing_errors(struct scsi_device *sdev)
  344. {
  345. int online;
  346. wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
  347. online = scsi_device_online(sdev);
  348. return online;
  349. }
  350. EXPORT_SYMBOL(scsi_block_when_processing_errors);
  351. #ifdef CONFIG_SCSI_LOGGING
  352. /**
  353. * scsi_eh_prt_fail_stats - Log info on failures.
  354. * @shost: scsi host being recovered.
  355. * @work_q: Queue of scsi cmds to process.
  356. */
  357. static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
  358. struct list_head *work_q)
  359. {
  360. struct scsi_cmnd *scmd;
  361. struct scsi_device *sdev;
  362. int total_failures = 0;
  363. int cmd_failed = 0;
  364. int cmd_cancel = 0;
  365. int devices_failed = 0;
  366. shost_for_each_device(sdev, shost) {
  367. list_for_each_entry(scmd, work_q, eh_entry) {
  368. if (scmd->device == sdev) {
  369. ++total_failures;
  370. if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
  371. ++cmd_cancel;
  372. else
  373. ++cmd_failed;
  374. }
  375. }
  376. if (cmd_cancel || cmd_failed) {
  377. SCSI_LOG_ERROR_RECOVERY(3,
  378. shost_printk(KERN_INFO, shost,
  379. "%s: cmds failed: %d, cancel: %d\n",
  380. __func__, cmd_failed,
  381. cmd_cancel));
  382. cmd_cancel = 0;
  383. cmd_failed = 0;
  384. ++devices_failed;
  385. }
  386. }
  387. SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
  388. "Total of %d commands on %d"
  389. " devices require eh work\n",
  390. total_failures, devices_failed));
  391. }
  392. #endif
  393. /**
  394. * scsi_report_lun_change - Set flag on all *other* devices on the same target
  395. * to indicate that a UNIT ATTENTION is expected.
  396. * @sdev: Device reporting the UNIT ATTENTION
  397. */
  398. static void scsi_report_lun_change(struct scsi_device *sdev)
  399. {
  400. sdev->sdev_target->expecting_lun_change = 1;
  401. }
  402. /**
  403. * scsi_report_sense - Examine scsi sense information and log messages for
  404. * certain conditions, also issue uevents for some of them.
  405. * @sdev: Device reporting the sense code
  406. * @sshdr: sshdr to be examined
  407. */
  408. static void scsi_report_sense(struct scsi_device *sdev,
  409. struct scsi_sense_hdr *sshdr)
  410. {
  411. enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
  412. if (sshdr->sense_key == UNIT_ATTENTION) {
  413. if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
  414. evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
  415. sdev_printk(KERN_WARNING, sdev,
  416. "Inquiry data has changed");
  417. } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
  418. evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
  419. scsi_report_lun_change(sdev);
  420. sdev_printk(KERN_WARNING, sdev,
  421. "LUN assignments on this target have "
  422. "changed. The Linux SCSI layer does not "
  423. "automatically remap LUN assignments.\n");
  424. } else if (sshdr->asc == 0x3f)
  425. sdev_printk(KERN_WARNING, sdev,
  426. "Operating parameters on this target have "
  427. "changed. The Linux SCSI layer does not "
  428. "automatically adjust these parameters.\n");
  429. if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
  430. evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
  431. sdev_printk(KERN_WARNING, sdev,
  432. "Warning! Received an indication that the "
  433. "LUN reached a thin provisioning soft "
  434. "threshold.\n");
  435. }
  436. if (sshdr->asc == 0x29) {
  437. evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
  438. /*
  439. * Do not print message if it is an expected side-effect
  440. * of runtime PM.
  441. */
  442. if (!sdev->silence_suspend)
  443. sdev_printk(KERN_WARNING, sdev,
  444. "Power-on or device reset occurred\n");
  445. }
  446. if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
  447. evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
  448. sdev_printk(KERN_WARNING, sdev,
  449. "Mode parameters changed");
  450. } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
  451. evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
  452. sdev_printk(KERN_WARNING, sdev,
  453. "Asymmetric access state changed");
  454. } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
  455. evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
  456. sdev_printk(KERN_WARNING, sdev,
  457. "Capacity data has changed");
  458. } else if (sshdr->asc == 0x2a)
  459. sdev_printk(KERN_WARNING, sdev,
  460. "Parameters changed");
  461. }
  462. if (evt_type != SDEV_EVT_MAXBITS) {
  463. set_bit(evt_type, sdev->pending_events);
  464. schedule_work(&sdev->event_work);
  465. }
  466. }
  467. static inline void set_scsi_ml_byte(struct scsi_cmnd *cmd, u8 status)
  468. {
  469. cmd->result = (cmd->result & 0xffff00ff) | (status << 8);
  470. }
  471. /**
  472. * scsi_check_sense - Examine scsi cmd sense
  473. * @scmd: Cmd to have sense checked.
  474. *
  475. * Return value:
  476. * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
  477. *
  478. * Notes:
  479. * When a deferred error is detected the current command has
  480. * not been executed and needs retrying.
  481. */
  482. enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd)
  483. {
  484. struct request *req = scsi_cmd_to_rq(scmd);
  485. struct scsi_device *sdev = scmd->device;
  486. struct scsi_sense_hdr sshdr;
  487. if (! scsi_command_normalize_sense(scmd, &sshdr))
  488. return FAILED; /* no valid sense data */
  489. scsi_report_sense(sdev, &sshdr);
  490. if (sshdr.sense_key == UNIT_ATTENTION) {
  491. /*
  492. * Increment the counters for Power on/Reset or New Media so
  493. * that all ULDs interested in these can see that those have
  494. * happened, even if someone else gets the sense data.
  495. */
  496. if (sshdr.asc == 0x28)
  497. atomic_inc(&sdev->ua_new_media_ctr);
  498. else if (sshdr.asc == 0x29)
  499. atomic_inc(&sdev->ua_por_ctr);
  500. }
  501. if (scsi_sense_is_deferred(&sshdr))
  502. return NEEDS_RETRY;
  503. if (sdev->handler && sdev->handler->check_sense) {
  504. enum scsi_disposition rc;
  505. rc = sdev->handler->check_sense(sdev, &sshdr);
  506. if (rc != SCSI_RETURN_NOT_HANDLED)
  507. return rc;
  508. /* handler does not care. Drop down to default handling */
  509. }
  510. if (scmd->cmnd[0] == TEST_UNIT_READY &&
  511. scmd->submitter != SUBMITTED_BY_SCSI_ERROR_HANDLER)
  512. /*
  513. * nasty: for mid-layer issued TURs, we need to return the
  514. * actual sense data without any recovery attempt. For eh
  515. * issued ones, we need to try to recover and interpret
  516. */
  517. return SUCCESS;
  518. /*
  519. * Previous logic looked for FILEMARK, EOM or ILI which are
  520. * mainly associated with tapes and returned SUCCESS.
  521. */
  522. if (sshdr.response_code == 0x70) {
  523. /* fixed format */
  524. if (scmd->sense_buffer[2] & 0xe0)
  525. return SUCCESS;
  526. } else {
  527. /*
  528. * descriptor format: look for "stream commands sense data
  529. * descriptor" (see SSC-3). Assume single sense data
  530. * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
  531. */
  532. if ((sshdr.additional_length > 3) &&
  533. (scmd->sense_buffer[8] == 0x4) &&
  534. (scmd->sense_buffer[11] & 0xe0))
  535. return SUCCESS;
  536. }
  537. switch (sshdr.sense_key) {
  538. case NO_SENSE:
  539. return SUCCESS;
  540. case RECOVERED_ERROR:
  541. return /* soft_error */ SUCCESS;
  542. case ABORTED_COMMAND:
  543. if (sshdr.asc == 0x10) /* DIF */
  544. return SUCCESS;
  545. /*
  546. * Check aborts due to command duration limit policy:
  547. * ABORTED COMMAND additional sense code with the
  548. * COMMAND TIMEOUT BEFORE PROCESSING or
  549. * COMMAND TIMEOUT DURING PROCESSING or
  550. * COMMAND TIMEOUT DURING PROCESSING DUE TO ERROR RECOVERY
  551. * additional sense code qualifiers.
  552. */
  553. if (sshdr.asc == 0x2e &&
  554. sshdr.ascq >= 0x01 && sshdr.ascq <= 0x03) {
  555. set_scsi_ml_byte(scmd, SCSIML_STAT_DL_TIMEOUT);
  556. req->cmd_flags |= REQ_FAILFAST_DEV;
  557. req->rq_flags |= RQF_QUIET;
  558. return SUCCESS;
  559. }
  560. if (sshdr.asc == 0x44 && sdev->sdev_bflags & BLIST_RETRY_ITF)
  561. return ADD_TO_MLQUEUE;
  562. if (sshdr.asc == 0xc1 && sshdr.ascq == 0x01 &&
  563. sdev->sdev_bflags & BLIST_RETRY_ASC_C1)
  564. return ADD_TO_MLQUEUE;
  565. return NEEDS_RETRY;
  566. case NOT_READY:
  567. case UNIT_ATTENTION:
  568. /*
  569. * if we are expecting a cc/ua because of a bus reset that we
  570. * performed, treat this just as a retry. otherwise this is
  571. * information that we should pass up to the upper-level driver
  572. * so that we can deal with it there.
  573. */
  574. if (scmd->device->expecting_cc_ua) {
  575. /*
  576. * Because some device does not queue unit
  577. * attentions correctly, we carefully check
  578. * additional sense code and qualifier so as
  579. * not to squash media change unit attention.
  580. */
  581. if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
  582. scmd->device->expecting_cc_ua = 0;
  583. return NEEDS_RETRY;
  584. }
  585. }
  586. /*
  587. * we might also expect a cc/ua if another LUN on the target
  588. * reported a UA with an ASC/ASCQ of 3F 0E -
  589. * REPORTED LUNS DATA HAS CHANGED.
  590. */
  591. if (scmd->device->sdev_target->expecting_lun_change &&
  592. sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
  593. return NEEDS_RETRY;
  594. /*
  595. * if the device is in the process of becoming ready, we
  596. * should retry.
  597. */
  598. if ((sshdr.asc == 0x04) &&
  599. (sshdr.ascq == 0x01 || sshdr.ascq == 0x0a))
  600. return NEEDS_RETRY;
  601. /*
  602. * if the device is not started, we need to wake
  603. * the error handler to start the motor
  604. */
  605. if (scmd->device->allow_restart &&
  606. (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
  607. return FAILED;
  608. /*
  609. * Pass the UA upwards for a determination in the completion
  610. * functions.
  611. */
  612. return SUCCESS;
  613. /* these are not supported */
  614. case DATA_PROTECT:
  615. if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
  616. /* Thin provisioning hard threshold reached */
  617. set_scsi_ml_byte(scmd, SCSIML_STAT_NOSPC);
  618. return SUCCESS;
  619. }
  620. fallthrough;
  621. case COPY_ABORTED:
  622. case VOLUME_OVERFLOW:
  623. case MISCOMPARE:
  624. case BLANK_CHECK:
  625. set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE);
  626. return SUCCESS;
  627. case MEDIUM_ERROR:
  628. if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
  629. sshdr.asc == 0x13 || /* AMNF DATA FIELD */
  630. sshdr.asc == 0x14) { /* RECORD NOT FOUND */
  631. set_scsi_ml_byte(scmd, SCSIML_STAT_MED_ERROR);
  632. return SUCCESS;
  633. }
  634. return NEEDS_RETRY;
  635. case HARDWARE_ERROR:
  636. if (scmd->device->retry_hwerror)
  637. return ADD_TO_MLQUEUE;
  638. else
  639. set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE);
  640. fallthrough;
  641. case ILLEGAL_REQUEST:
  642. if (sshdr.asc == 0x20 || /* Invalid command operation code */
  643. sshdr.asc == 0x21 || /* Logical block address out of range */
  644. sshdr.asc == 0x22 || /* Invalid function */
  645. sshdr.asc == 0x24 || /* Invalid field in cdb */
  646. sshdr.asc == 0x26 || /* Parameter value invalid */
  647. sshdr.asc == 0x27) { /* Write protected */
  648. set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE);
  649. }
  650. return SUCCESS;
  651. case COMPLETED:
  652. /*
  653. * A command using command duration limits (CDL) with a
  654. * descriptor set with policy 0xD may be completed with success
  655. * and the sense data DATA CURRENTLY UNAVAILABLE, indicating
  656. * that the command was in fact aborted because it exceeded its
  657. * duration limit. Never retry these commands.
  658. */
  659. if (sshdr.asc == 0x55 && sshdr.ascq == 0x0a) {
  660. set_scsi_ml_byte(scmd, SCSIML_STAT_DL_TIMEOUT);
  661. req->cmd_flags |= REQ_FAILFAST_DEV;
  662. req->rq_flags |= RQF_QUIET;
  663. }
  664. return SUCCESS;
  665. default:
  666. return SUCCESS;
  667. }
  668. }
  669. EXPORT_SYMBOL_GPL(scsi_check_sense);
  670. static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
  671. {
  672. const struct scsi_host_template *sht = sdev->host->hostt;
  673. struct scsi_device *tmp_sdev;
  674. if (!sdev->budget_map.map)
  675. return;
  676. if (!sht->track_queue_depth ||
  677. sdev->queue_depth >= sdev->max_queue_depth)
  678. return;
  679. if (time_before(jiffies,
  680. sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
  681. return;
  682. if (time_before(jiffies,
  683. sdev->last_queue_full_time + sdev->queue_ramp_up_period))
  684. return;
  685. /*
  686. * Walk all devices of a target and do
  687. * ramp up on them.
  688. */
  689. shost_for_each_device(tmp_sdev, sdev->host) {
  690. if (tmp_sdev->channel != sdev->channel ||
  691. tmp_sdev->id != sdev->id ||
  692. tmp_sdev->queue_depth == sdev->max_queue_depth)
  693. continue;
  694. scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
  695. sdev->last_queue_ramp_up = jiffies;
  696. }
  697. }
  698. static void scsi_handle_queue_full(struct scsi_device *sdev)
  699. {
  700. const struct scsi_host_template *sht = sdev->host->hostt;
  701. struct scsi_device *tmp_sdev;
  702. if (!sht->track_queue_depth)
  703. return;
  704. shost_for_each_device(tmp_sdev, sdev->host) {
  705. if (tmp_sdev->channel != sdev->channel ||
  706. tmp_sdev->id != sdev->id)
  707. continue;
  708. /*
  709. * We do not know the number of commands that were at
  710. * the device when we got the queue full so we start
  711. * from the highest possible value and work our way down.
  712. */
  713. scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
  714. }
  715. }
  716. /**
  717. * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
  718. * @scmd: SCSI cmd to examine.
  719. *
  720. * Notes:
  721. * This is *only* called when we are examining the status of commands
  722. * queued during error recovery. the main difference here is that we
  723. * don't allow for the possibility of retries here, and we are a lot
  724. * more restrictive about what we consider acceptable.
  725. */
  726. static enum scsi_disposition scsi_eh_completed_normally(struct scsi_cmnd *scmd)
  727. {
  728. /*
  729. * first check the host byte, to see if there is anything in there
  730. * that would indicate what we need to do.
  731. */
  732. if (host_byte(scmd->result) == DID_RESET) {
  733. /*
  734. * rats. we are already in the error handler, so we now
  735. * get to try and figure out what to do next. if the sense
  736. * is valid, we have a pretty good idea of what to do.
  737. * if not, we mark it as FAILED.
  738. */
  739. return scsi_check_sense(scmd);
  740. }
  741. if (host_byte(scmd->result) != DID_OK)
  742. return FAILED;
  743. /*
  744. * now, check the status byte to see if this indicates
  745. * anything special.
  746. */
  747. switch (get_status_byte(scmd)) {
  748. case SAM_STAT_GOOD:
  749. scsi_handle_queue_ramp_up(scmd->device);
  750. if (scmd->sense_buffer && SCSI_SENSE_VALID(scmd))
  751. /*
  752. * If we have sense data, call scsi_check_sense() in
  753. * order to set the correct SCSI ML byte (if any).
  754. * No point in checking the return value, since the
  755. * command has already completed successfully.
  756. */
  757. scsi_check_sense(scmd);
  758. fallthrough;
  759. case SAM_STAT_COMMAND_TERMINATED:
  760. return SUCCESS;
  761. case SAM_STAT_CHECK_CONDITION:
  762. return scsi_check_sense(scmd);
  763. case SAM_STAT_CONDITION_MET:
  764. case SAM_STAT_INTERMEDIATE:
  765. case SAM_STAT_INTERMEDIATE_CONDITION_MET:
  766. /*
  767. * who knows? FIXME(eric)
  768. */
  769. return SUCCESS;
  770. case SAM_STAT_RESERVATION_CONFLICT:
  771. if (scmd->cmnd[0] == TEST_UNIT_READY)
  772. /* it is a success, we probed the device and
  773. * found it */
  774. return SUCCESS;
  775. /* otherwise, we failed to send the command */
  776. return FAILED;
  777. case SAM_STAT_TASK_SET_FULL:
  778. scsi_handle_queue_full(scmd->device);
  779. fallthrough;
  780. case SAM_STAT_BUSY:
  781. return NEEDS_RETRY;
  782. default:
  783. return FAILED;
  784. }
  785. return FAILED;
  786. }
  787. /**
  788. * scsi_eh_done - Completion function for error handling.
  789. * @scmd: Cmd that is done.
  790. */
  791. void scsi_eh_done(struct scsi_cmnd *scmd)
  792. {
  793. struct completion *eh_action;
  794. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  795. "%s result: %x\n", __func__, scmd->result));
  796. eh_action = scmd->device->host->eh_action;
  797. if (eh_action)
  798. complete(eh_action);
  799. }
  800. /**
  801. * scsi_try_host_reset - ask host adapter to reset itself
  802. * @scmd: SCSI cmd to send host reset.
  803. */
  804. static enum scsi_disposition scsi_try_host_reset(struct scsi_cmnd *scmd)
  805. {
  806. unsigned long flags;
  807. enum scsi_disposition rtn;
  808. struct Scsi_Host *host = scmd->device->host;
  809. const struct scsi_host_template *hostt = host->hostt;
  810. SCSI_LOG_ERROR_RECOVERY(3,
  811. shost_printk(KERN_INFO, host, "Snd Host RST\n"));
  812. if (!hostt->eh_host_reset_handler)
  813. return FAILED;
  814. rtn = hostt->eh_host_reset_handler(scmd);
  815. if (rtn == SUCCESS) {
  816. if (!hostt->skip_settle_delay)
  817. ssleep(HOST_RESET_SETTLE_TIME);
  818. spin_lock_irqsave(host->host_lock, flags);
  819. scsi_report_bus_reset(host, scmd_channel(scmd));
  820. spin_unlock_irqrestore(host->host_lock, flags);
  821. }
  822. return rtn;
  823. }
  824. /**
  825. * scsi_try_bus_reset - ask host to perform a bus reset
  826. * @scmd: SCSI cmd to send bus reset.
  827. */
  828. static enum scsi_disposition scsi_try_bus_reset(struct scsi_cmnd *scmd)
  829. {
  830. unsigned long flags;
  831. enum scsi_disposition rtn;
  832. struct Scsi_Host *host = scmd->device->host;
  833. const struct scsi_host_template *hostt = host->hostt;
  834. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  835. "%s: Snd Bus RST\n", __func__));
  836. if (!hostt->eh_bus_reset_handler)
  837. return FAILED;
  838. rtn = hostt->eh_bus_reset_handler(scmd);
  839. if (rtn == SUCCESS) {
  840. if (!hostt->skip_settle_delay)
  841. ssleep(BUS_RESET_SETTLE_TIME);
  842. spin_lock_irqsave(host->host_lock, flags);
  843. scsi_report_bus_reset(host, scmd_channel(scmd));
  844. spin_unlock_irqrestore(host->host_lock, flags);
  845. }
  846. return rtn;
  847. }
  848. static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
  849. {
  850. sdev->was_reset = 1;
  851. sdev->expecting_cc_ua = 1;
  852. }
  853. /**
  854. * scsi_try_target_reset - Ask host to perform a target reset
  855. * @scmd: SCSI cmd used to send a target reset
  856. *
  857. * Notes:
  858. * There is no timeout for this operation. if this operation is
  859. * unreliable for a given host, then the host itself needs to put a
  860. * timer on it, and set the host back to a consistent state prior to
  861. * returning.
  862. */
  863. static enum scsi_disposition scsi_try_target_reset(struct scsi_cmnd *scmd)
  864. {
  865. unsigned long flags;
  866. enum scsi_disposition rtn;
  867. struct Scsi_Host *host = scmd->device->host;
  868. const struct scsi_host_template *hostt = host->hostt;
  869. if (!hostt->eh_target_reset_handler)
  870. return FAILED;
  871. rtn = hostt->eh_target_reset_handler(scmd);
  872. if (rtn == SUCCESS) {
  873. spin_lock_irqsave(host->host_lock, flags);
  874. __starget_for_each_device(scsi_target(scmd->device), NULL,
  875. __scsi_report_device_reset);
  876. spin_unlock_irqrestore(host->host_lock, flags);
  877. }
  878. return rtn;
  879. }
  880. /**
  881. * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
  882. * @scmd: SCSI cmd used to send BDR
  883. *
  884. * Notes:
  885. * There is no timeout for this operation. if this operation is
  886. * unreliable for a given host, then the host itself needs to put a
  887. * timer on it, and set the host back to a consistent state prior to
  888. * returning.
  889. */
  890. static enum scsi_disposition scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
  891. {
  892. enum scsi_disposition rtn;
  893. const struct scsi_host_template *hostt = scmd->device->host->hostt;
  894. if (!hostt->eh_device_reset_handler)
  895. return FAILED;
  896. rtn = hostt->eh_device_reset_handler(scmd);
  897. if (rtn == SUCCESS)
  898. __scsi_report_device_reset(scmd->device, NULL);
  899. return rtn;
  900. }
  901. /**
  902. * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
  903. * @hostt: SCSI driver host template
  904. * @scmd: SCSI cmd used to send a target reset
  905. *
  906. * Return value:
  907. * SUCCESS, FAILED, or FAST_IO_FAIL
  908. *
  909. * Notes:
  910. * SUCCESS does not necessarily indicate that the command
  911. * has been aborted; it only indicates that the LLDDs
  912. * has cleared all references to that command.
  913. * LLDDs should return FAILED only if an abort was required
  914. * but could not be executed. LLDDs should return FAST_IO_FAIL
  915. * if the device is temporarily unavailable (eg due to a
  916. * link down on FibreChannel)
  917. */
  918. static enum scsi_disposition
  919. scsi_try_to_abort_cmd(const struct scsi_host_template *hostt, struct scsi_cmnd *scmd)
  920. {
  921. if (!hostt->eh_abort_handler)
  922. return FAILED;
  923. return hostt->eh_abort_handler(scmd);
  924. }
  925. static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
  926. {
  927. if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
  928. if (scsi_try_bus_device_reset(scmd) != SUCCESS)
  929. if (scsi_try_target_reset(scmd) != SUCCESS)
  930. if (scsi_try_bus_reset(scmd) != SUCCESS)
  931. scsi_try_host_reset(scmd);
  932. }
  933. /**
  934. * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
  935. * @scmd: SCSI command structure to hijack
  936. * @ses: structure to save restore information
  937. * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
  938. * @cmnd_size: size in bytes of @cmnd (must be <= MAX_COMMAND_SIZE)
  939. * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
  940. *
  941. * This function is used to save a scsi command information before re-execution
  942. * as part of the error recovery process. If @sense_bytes is 0 the command
  943. * sent must be one that does not transfer any data. If @sense_bytes != 0
  944. * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
  945. * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
  946. */
  947. void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
  948. unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
  949. {
  950. struct scsi_device *sdev = scmd->device;
  951. #ifdef CONFIG_BLK_INLINE_ENCRYPTION
  952. struct request *rq = scsi_cmd_to_rq(scmd);
  953. #endif
  954. /*
  955. * We need saved copies of a number of fields - this is because
  956. * error handling may need to overwrite these with different values
  957. * to run different commands, and once error handling is complete,
  958. * we will need to restore these values prior to running the actual
  959. * command.
  960. */
  961. ses->cmd_len = scmd->cmd_len;
  962. ses->data_direction = scmd->sc_data_direction;
  963. ses->sdb = scmd->sdb;
  964. ses->result = scmd->result;
  965. ses->resid_len = scmd->resid_len;
  966. ses->underflow = scmd->underflow;
  967. ses->prot_op = scmd->prot_op;
  968. ses->eh_eflags = scmd->eh_eflags;
  969. scmd->prot_op = SCSI_PROT_NORMAL;
  970. scmd->eh_eflags = 0;
  971. memcpy(ses->cmnd, scmd->cmnd, sizeof(ses->cmnd));
  972. memset(scmd->cmnd, 0, sizeof(scmd->cmnd));
  973. memset(&scmd->sdb, 0, sizeof(scmd->sdb));
  974. scmd->result = 0;
  975. scmd->resid_len = 0;
  976. if (sense_bytes) {
  977. scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
  978. sense_bytes);
  979. sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
  980. scmd->sdb.length);
  981. scmd->sdb.table.sgl = &ses->sense_sgl;
  982. scmd->sc_data_direction = DMA_FROM_DEVICE;
  983. scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
  984. scmd->cmnd[0] = REQUEST_SENSE;
  985. scmd->cmnd[4] = scmd->sdb.length;
  986. scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
  987. } else {
  988. scmd->sc_data_direction = DMA_NONE;
  989. if (cmnd) {
  990. BUG_ON(cmnd_size > sizeof(scmd->cmnd));
  991. memcpy(scmd->cmnd, cmnd, cmnd_size);
  992. scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
  993. }
  994. }
  995. scmd->underflow = 0;
  996. if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
  997. scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
  998. (sdev->lun << 5 & 0xe0);
  999. /*
  1000. * Encryption must be disabled for the commands submitted by the error handler.
  1001. * Hence, clear the encryption context information.
  1002. */
  1003. #ifdef CONFIG_BLK_INLINE_ENCRYPTION
  1004. ses->rq_crypt_keyslot = rq->crypt_keyslot;
  1005. ses->rq_crypt_ctx = rq->crypt_ctx;
  1006. rq->crypt_keyslot = NULL;
  1007. rq->crypt_ctx = NULL;
  1008. #endif
  1009. /*
  1010. * Zero the sense buffer. The scsi spec mandates that any
  1011. * untransferred sense data should be interpreted as being zero.
  1012. */
  1013. memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  1014. }
  1015. EXPORT_SYMBOL(scsi_eh_prep_cmnd);
  1016. /**
  1017. * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
  1018. * @scmd: SCSI command structure to restore
  1019. * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
  1020. *
  1021. * Undo any damage done by above scsi_eh_prep_cmnd().
  1022. */
  1023. void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
  1024. {
  1025. #ifdef CONFIG_BLK_INLINE_ENCRYPTION
  1026. struct request *rq = scsi_cmd_to_rq(scmd);
  1027. #endif
  1028. /*
  1029. * Restore original data
  1030. */
  1031. scmd->cmd_len = ses->cmd_len;
  1032. memcpy(scmd->cmnd, ses->cmnd, sizeof(ses->cmnd));
  1033. scmd->sc_data_direction = ses->data_direction;
  1034. scmd->sdb = ses->sdb;
  1035. scmd->result = ses->result;
  1036. scmd->resid_len = ses->resid_len;
  1037. scmd->underflow = ses->underflow;
  1038. scmd->prot_op = ses->prot_op;
  1039. scmd->eh_eflags = ses->eh_eflags;
  1040. #ifdef CONFIG_BLK_INLINE_ENCRYPTION
  1041. rq->crypt_keyslot = ses->rq_crypt_keyslot;
  1042. rq->crypt_ctx = ses->rq_crypt_ctx;
  1043. #endif
  1044. }
  1045. EXPORT_SYMBOL(scsi_eh_restore_cmnd);
  1046. /**
  1047. * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
  1048. * @scmd: SCSI command structure to hijack
  1049. * @cmnd: CDB to send
  1050. * @cmnd_size: size in bytes of @cmnd
  1051. * @timeout: timeout for this request
  1052. * @sense_bytes: size of sense data to copy or 0
  1053. *
  1054. * This function is used to send a scsi command down to a target device
  1055. * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
  1056. *
  1057. * Return value:
  1058. * SUCCESS or FAILED or NEEDS_RETRY
  1059. */
  1060. static enum scsi_disposition scsi_send_eh_cmnd(struct scsi_cmnd *scmd,
  1061. unsigned char *cmnd, int cmnd_size, int timeout, unsigned sense_bytes)
  1062. {
  1063. struct scsi_device *sdev = scmd->device;
  1064. struct Scsi_Host *shost = sdev->host;
  1065. DECLARE_COMPLETION_ONSTACK(done);
  1066. unsigned long timeleft = timeout, delay;
  1067. struct scsi_eh_save ses;
  1068. const unsigned long stall_for = msecs_to_jiffies(100);
  1069. int rtn;
  1070. retry:
  1071. scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
  1072. shost->eh_action = &done;
  1073. scsi_log_send(scmd);
  1074. scmd->submitter = SUBMITTED_BY_SCSI_ERROR_HANDLER;
  1075. scmd->flags |= SCMD_LAST;
  1076. /*
  1077. * Lock sdev->state_mutex to avoid that scsi_device_quiesce() can
  1078. * change the SCSI device state after we have examined it and before
  1079. * .queuecommand() is called.
  1080. */
  1081. mutex_lock(&sdev->state_mutex);
  1082. while (sdev->sdev_state == SDEV_BLOCK && timeleft > 0) {
  1083. mutex_unlock(&sdev->state_mutex);
  1084. SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_DEBUG, sdev,
  1085. "%s: state %d <> %d\n", __func__, sdev->sdev_state,
  1086. SDEV_BLOCK));
  1087. delay = min(timeleft, stall_for);
  1088. timeleft -= delay;
  1089. msleep(jiffies_to_msecs(delay));
  1090. mutex_lock(&sdev->state_mutex);
  1091. }
  1092. if (sdev->sdev_state != SDEV_BLOCK)
  1093. rtn = shost->hostt->queuecommand(shost, scmd);
  1094. else
  1095. rtn = FAILED;
  1096. mutex_unlock(&sdev->state_mutex);
  1097. if (rtn) {
  1098. if (timeleft > stall_for) {
  1099. scsi_eh_restore_cmnd(scmd, &ses);
  1100. timeleft -= stall_for;
  1101. msleep(jiffies_to_msecs(stall_for));
  1102. goto retry;
  1103. }
  1104. /* signal not to enter either branch of the if () below */
  1105. timeleft = 0;
  1106. rtn = FAILED;
  1107. } else {
  1108. timeleft = wait_for_completion_timeout(&done, timeout);
  1109. rtn = SUCCESS;
  1110. }
  1111. shost->eh_action = NULL;
  1112. scsi_log_completion(scmd, rtn);
  1113. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  1114. "%s timeleft: %ld\n",
  1115. __func__, timeleft));
  1116. /*
  1117. * If there is time left scsi_eh_done got called, and we will examine
  1118. * the actual status codes to see whether the command actually did
  1119. * complete normally, else if we have a zero return and no time left,
  1120. * the command must still be pending, so abort it and return FAILED.
  1121. * If we never actually managed to issue the command, because
  1122. * ->queuecommand() kept returning non zero, use the rtn = FAILED
  1123. * value above (so don't execute either branch of the if)
  1124. */
  1125. if (timeleft) {
  1126. rtn = scsi_eh_completed_normally(scmd);
  1127. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  1128. "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
  1129. switch (rtn) {
  1130. case SUCCESS:
  1131. case NEEDS_RETRY:
  1132. case FAILED:
  1133. break;
  1134. case ADD_TO_MLQUEUE:
  1135. rtn = NEEDS_RETRY;
  1136. break;
  1137. default:
  1138. rtn = FAILED;
  1139. break;
  1140. }
  1141. } else if (rtn != FAILED) {
  1142. scsi_abort_eh_cmnd(scmd);
  1143. rtn = FAILED;
  1144. }
  1145. scsi_eh_restore_cmnd(scmd, &ses);
  1146. return rtn;
  1147. }
  1148. /**
  1149. * scsi_request_sense - Request sense data from a particular target.
  1150. * @scmd: SCSI cmd for request sense.
  1151. *
  1152. * Notes:
  1153. * Some hosts automatically obtain this information, others require
  1154. * that we obtain it on our own. This function will *not* return until
  1155. * the command either times out, or it completes.
  1156. */
  1157. static enum scsi_disposition scsi_request_sense(struct scsi_cmnd *scmd)
  1158. {
  1159. return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
  1160. }
  1161. static enum scsi_disposition
  1162. scsi_eh_action(struct scsi_cmnd *scmd, enum scsi_disposition rtn)
  1163. {
  1164. if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) {
  1165. struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
  1166. if (sdrv->eh_action)
  1167. rtn = sdrv->eh_action(scmd, rtn);
  1168. }
  1169. return rtn;
  1170. }
  1171. /**
  1172. * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
  1173. * @scmd: Original SCSI cmd that eh has finished.
  1174. * @done_q: Queue for processed commands.
  1175. *
  1176. * Notes:
  1177. * We don't want to use the normal command completion while we are are
  1178. * still handling errors - it may cause other commands to be queued,
  1179. * and that would disturb what we are doing. Thus we really want to
  1180. * keep a list of pending commands for final completion, and once we
  1181. * are ready to leave error handling we handle completion for real.
  1182. */
  1183. void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
  1184. {
  1185. list_move_tail(&scmd->eh_entry, done_q);
  1186. }
  1187. EXPORT_SYMBOL(scsi_eh_finish_cmd);
  1188. /**
  1189. * scsi_eh_get_sense - Get device sense data.
  1190. * @work_q: Queue of commands to process.
  1191. * @done_q: Queue of processed commands.
  1192. *
  1193. * Description:
  1194. * See if we need to request sense information. if so, then get it
  1195. * now, so we have a better idea of what to do.
  1196. *
  1197. * Notes:
  1198. * This has the unfortunate side effect that if a shost adapter does
  1199. * not automatically request sense information, we end up shutting
  1200. * it down before we request it.
  1201. *
  1202. * All drivers should request sense information internally these days,
  1203. * so for now all I have to say is tough noogies if you end up in here.
  1204. *
  1205. * XXX: Long term this code should go away, but that needs an audit of
  1206. * all LLDDs first.
  1207. */
  1208. int scsi_eh_get_sense(struct list_head *work_q,
  1209. struct list_head *done_q)
  1210. {
  1211. struct scsi_cmnd *scmd, *next;
  1212. struct Scsi_Host *shost;
  1213. enum scsi_disposition rtn;
  1214. /*
  1215. * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
  1216. * should not get sense.
  1217. */
  1218. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1219. if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
  1220. SCSI_SENSE_VALID(scmd))
  1221. continue;
  1222. shost = scmd->device->host;
  1223. if (scsi_host_eh_past_deadline(shost)) {
  1224. SCSI_LOG_ERROR_RECOVERY(3,
  1225. scmd_printk(KERN_INFO, scmd,
  1226. "%s: skip request sense, past eh deadline\n",
  1227. current->comm));
  1228. break;
  1229. }
  1230. if (!scsi_status_is_check_condition(scmd->result))
  1231. /*
  1232. * don't request sense if there's no check condition
  1233. * status because the error we're processing isn't one
  1234. * that has a sense code (and some devices get
  1235. * confused by sense requests out of the blue)
  1236. */
  1237. continue;
  1238. SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
  1239. "%s: requesting sense\n",
  1240. current->comm));
  1241. rtn = scsi_request_sense(scmd);
  1242. if (rtn != SUCCESS)
  1243. continue;
  1244. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  1245. "sense requested, result %x\n", scmd->result));
  1246. SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
  1247. rtn = scsi_decide_disposition(scmd);
  1248. /*
  1249. * if the result was normal, then just pass it along to the
  1250. * upper level.
  1251. */
  1252. if (rtn == SUCCESS)
  1253. /*
  1254. * We don't want this command reissued, just finished
  1255. * with the sense data, so set retries to the max
  1256. * allowed to ensure it won't get reissued. If the user
  1257. * has requested infinite retries, we also want to
  1258. * finish this command, so force completion by setting
  1259. * retries and allowed to the same value.
  1260. */
  1261. if (scmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
  1262. scmd->retries = scmd->allowed = 1;
  1263. else
  1264. scmd->retries = scmd->allowed;
  1265. else if (rtn != NEEDS_RETRY)
  1266. continue;
  1267. scsi_eh_finish_cmd(scmd, done_q);
  1268. }
  1269. return list_empty(work_q);
  1270. }
  1271. EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
  1272. /**
  1273. * scsi_eh_tur - Send TUR to device.
  1274. * @scmd: &scsi_cmnd to send TUR
  1275. *
  1276. * Return value:
  1277. * 0 - Device is ready. 1 - Device NOT ready.
  1278. */
  1279. static int scsi_eh_tur(struct scsi_cmnd *scmd)
  1280. {
  1281. static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
  1282. int retry_cnt = 1;
  1283. enum scsi_disposition rtn;
  1284. retry_tur:
  1285. rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
  1286. scmd->device->eh_timeout, 0);
  1287. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  1288. "%s return: %x\n", __func__, rtn));
  1289. switch (rtn) {
  1290. case NEEDS_RETRY:
  1291. if (retry_cnt--)
  1292. goto retry_tur;
  1293. fallthrough;
  1294. case SUCCESS:
  1295. return 0;
  1296. default:
  1297. return 1;
  1298. }
  1299. }
  1300. /**
  1301. * scsi_eh_test_devices - check if devices are responding from error recovery.
  1302. * @cmd_list: scsi commands in error recovery.
  1303. * @work_q: queue for commands which still need more error recovery
  1304. * @done_q: queue for commands which are finished
  1305. * @try_stu: boolean on if a STU command should be tried in addition to TUR.
  1306. *
  1307. * Decription:
  1308. * Tests if devices are in a working state. Commands to devices now in
  1309. * a working state are sent to the done_q while commands to devices which
  1310. * are still failing to respond are returned to the work_q for more
  1311. * processing.
  1312. **/
  1313. static int scsi_eh_test_devices(struct list_head *cmd_list,
  1314. struct list_head *work_q,
  1315. struct list_head *done_q, int try_stu)
  1316. {
  1317. struct scsi_cmnd *scmd, *next;
  1318. struct scsi_device *sdev;
  1319. int finish_cmds;
  1320. while (!list_empty(cmd_list)) {
  1321. scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
  1322. sdev = scmd->device;
  1323. if (!try_stu) {
  1324. if (scsi_host_eh_past_deadline(sdev->host)) {
  1325. /* Push items back onto work_q */
  1326. list_splice_init(cmd_list, work_q);
  1327. SCSI_LOG_ERROR_RECOVERY(3,
  1328. sdev_printk(KERN_INFO, sdev,
  1329. "%s: skip test device, past eh deadline",
  1330. current->comm));
  1331. break;
  1332. }
  1333. }
  1334. finish_cmds = !scsi_device_online(scmd->device) ||
  1335. (try_stu && !scsi_eh_try_stu(scmd) &&
  1336. !scsi_eh_tur(scmd)) ||
  1337. !scsi_eh_tur(scmd);
  1338. list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
  1339. if (scmd->device == sdev) {
  1340. if (finish_cmds &&
  1341. (try_stu ||
  1342. scsi_eh_action(scmd, SUCCESS) == SUCCESS))
  1343. scsi_eh_finish_cmd(scmd, done_q);
  1344. else
  1345. list_move_tail(&scmd->eh_entry, work_q);
  1346. }
  1347. }
  1348. return list_empty(work_q);
  1349. }
  1350. /**
  1351. * scsi_eh_try_stu - Send START_UNIT to device.
  1352. * @scmd: &scsi_cmnd to send START_UNIT
  1353. *
  1354. * Return value:
  1355. * 0 - Device is ready. 1 - Device NOT ready.
  1356. */
  1357. static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
  1358. {
  1359. static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
  1360. if (scmd->device->allow_restart) {
  1361. int i;
  1362. enum scsi_disposition rtn = NEEDS_RETRY;
  1363. for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
  1364. rtn = scsi_send_eh_cmnd(scmd, stu_command, 6,
  1365. scmd->device->eh_timeout, 0);
  1366. if (rtn == SUCCESS)
  1367. return 0;
  1368. }
  1369. return 1;
  1370. }
  1371. /**
  1372. * scsi_eh_stu - send START_UNIT if needed
  1373. * @shost: &scsi host being recovered.
  1374. * @work_q: &list_head for pending commands.
  1375. * @done_q: &list_head for processed commands.
  1376. *
  1377. * Notes:
  1378. * If commands are failing due to not ready, initializing command required,
  1379. * try revalidating the device, which will end up sending a start unit.
  1380. */
  1381. static int scsi_eh_stu(struct Scsi_Host *shost,
  1382. struct list_head *work_q,
  1383. struct list_head *done_q)
  1384. {
  1385. struct scsi_cmnd *scmd, *stu_scmd, *next;
  1386. struct scsi_device *sdev;
  1387. shost_for_each_device(sdev, shost) {
  1388. if (scsi_host_eh_past_deadline(shost)) {
  1389. SCSI_LOG_ERROR_RECOVERY(3,
  1390. sdev_printk(KERN_INFO, sdev,
  1391. "%s: skip START_UNIT, past eh deadline\n",
  1392. current->comm));
  1393. scsi_device_put(sdev);
  1394. break;
  1395. }
  1396. stu_scmd = NULL;
  1397. list_for_each_entry(scmd, work_q, eh_entry)
  1398. if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
  1399. scsi_check_sense(scmd) == FAILED ) {
  1400. stu_scmd = scmd;
  1401. break;
  1402. }
  1403. if (!stu_scmd)
  1404. continue;
  1405. SCSI_LOG_ERROR_RECOVERY(3,
  1406. sdev_printk(KERN_INFO, sdev,
  1407. "%s: Sending START_UNIT\n",
  1408. current->comm));
  1409. if (!scsi_eh_try_stu(stu_scmd)) {
  1410. if (!scsi_device_online(sdev) ||
  1411. !scsi_eh_tur(stu_scmd)) {
  1412. list_for_each_entry_safe(scmd, next,
  1413. work_q, eh_entry) {
  1414. if (scmd->device == sdev &&
  1415. scsi_eh_action(scmd, SUCCESS) == SUCCESS)
  1416. scsi_eh_finish_cmd(scmd, done_q);
  1417. }
  1418. }
  1419. } else {
  1420. SCSI_LOG_ERROR_RECOVERY(3,
  1421. sdev_printk(KERN_INFO, sdev,
  1422. "%s: START_UNIT failed\n",
  1423. current->comm));
  1424. }
  1425. }
  1426. return list_empty(work_q);
  1427. }
  1428. /**
  1429. * scsi_eh_bus_device_reset - send bdr if needed
  1430. * @shost: scsi host being recovered.
  1431. * @work_q: &list_head for pending commands.
  1432. * @done_q: &list_head for processed commands.
  1433. *
  1434. * Notes:
  1435. * Try a bus device reset. Still, look to see whether we have multiple
  1436. * devices that are jammed or not - if we have multiple devices, it
  1437. * makes no sense to try bus_device_reset - we really would need to try
  1438. * a bus_reset instead.
  1439. */
  1440. static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
  1441. struct list_head *work_q,
  1442. struct list_head *done_q)
  1443. {
  1444. struct scsi_cmnd *scmd, *bdr_scmd, *next;
  1445. struct scsi_device *sdev;
  1446. enum scsi_disposition rtn;
  1447. shost_for_each_device(sdev, shost) {
  1448. if (scsi_host_eh_past_deadline(shost)) {
  1449. SCSI_LOG_ERROR_RECOVERY(3,
  1450. sdev_printk(KERN_INFO, sdev,
  1451. "%s: skip BDR, past eh deadline\n",
  1452. current->comm));
  1453. scsi_device_put(sdev);
  1454. break;
  1455. }
  1456. bdr_scmd = NULL;
  1457. list_for_each_entry(scmd, work_q, eh_entry)
  1458. if (scmd->device == sdev) {
  1459. bdr_scmd = scmd;
  1460. break;
  1461. }
  1462. if (!bdr_scmd)
  1463. continue;
  1464. SCSI_LOG_ERROR_RECOVERY(3,
  1465. sdev_printk(KERN_INFO, sdev,
  1466. "%s: Sending BDR\n", current->comm));
  1467. rtn = scsi_try_bus_device_reset(bdr_scmd);
  1468. if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
  1469. if (!scsi_device_online(sdev) ||
  1470. rtn == FAST_IO_FAIL ||
  1471. !scsi_eh_tur(bdr_scmd)) {
  1472. list_for_each_entry_safe(scmd, next,
  1473. work_q, eh_entry) {
  1474. if (scmd->device == sdev &&
  1475. scsi_eh_action(scmd, rtn) != FAILED)
  1476. scsi_eh_finish_cmd(scmd,
  1477. done_q);
  1478. }
  1479. }
  1480. } else {
  1481. SCSI_LOG_ERROR_RECOVERY(3,
  1482. sdev_printk(KERN_INFO, sdev,
  1483. "%s: BDR failed\n", current->comm));
  1484. }
  1485. }
  1486. return list_empty(work_q);
  1487. }
  1488. /**
  1489. * scsi_eh_target_reset - send target reset if needed
  1490. * @shost: scsi host being recovered.
  1491. * @work_q: &list_head for pending commands.
  1492. * @done_q: &list_head for processed commands.
  1493. *
  1494. * Notes:
  1495. * Try a target reset.
  1496. */
  1497. static int scsi_eh_target_reset(struct Scsi_Host *shost,
  1498. struct list_head *work_q,
  1499. struct list_head *done_q)
  1500. {
  1501. LIST_HEAD(tmp_list);
  1502. LIST_HEAD(check_list);
  1503. list_splice_init(work_q, &tmp_list);
  1504. while (!list_empty(&tmp_list)) {
  1505. struct scsi_cmnd *next, *scmd;
  1506. enum scsi_disposition rtn;
  1507. unsigned int id;
  1508. if (scsi_host_eh_past_deadline(shost)) {
  1509. /* push back on work queue for further processing */
  1510. list_splice_init(&check_list, work_q);
  1511. list_splice_init(&tmp_list, work_q);
  1512. SCSI_LOG_ERROR_RECOVERY(3,
  1513. shost_printk(KERN_INFO, shost,
  1514. "%s: Skip target reset, past eh deadline\n",
  1515. current->comm));
  1516. return list_empty(work_q);
  1517. }
  1518. scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
  1519. id = scmd_id(scmd);
  1520. SCSI_LOG_ERROR_RECOVERY(3,
  1521. shost_printk(KERN_INFO, shost,
  1522. "%s: Sending target reset to target %d\n",
  1523. current->comm, id));
  1524. rtn = scsi_try_target_reset(scmd);
  1525. if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
  1526. SCSI_LOG_ERROR_RECOVERY(3,
  1527. shost_printk(KERN_INFO, shost,
  1528. "%s: Target reset failed"
  1529. " target: %d\n",
  1530. current->comm, id));
  1531. list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
  1532. if (scmd_id(scmd) != id)
  1533. continue;
  1534. if (rtn == SUCCESS)
  1535. list_move_tail(&scmd->eh_entry, &check_list);
  1536. else if (rtn == FAST_IO_FAIL)
  1537. scsi_eh_finish_cmd(scmd, done_q);
  1538. else
  1539. /* push back on work queue for further processing */
  1540. list_move(&scmd->eh_entry, work_q);
  1541. }
  1542. }
  1543. return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
  1544. }
  1545. /**
  1546. * scsi_eh_bus_reset - send a bus reset
  1547. * @shost: &scsi host being recovered.
  1548. * @work_q: &list_head for pending commands.
  1549. * @done_q: &list_head for processed commands.
  1550. */
  1551. static int scsi_eh_bus_reset(struct Scsi_Host *shost,
  1552. struct list_head *work_q,
  1553. struct list_head *done_q)
  1554. {
  1555. struct scsi_cmnd *scmd, *chan_scmd, *next;
  1556. LIST_HEAD(check_list);
  1557. unsigned int channel;
  1558. enum scsi_disposition rtn;
  1559. /*
  1560. * we really want to loop over the various channels, and do this on
  1561. * a channel by channel basis. we should also check to see if any
  1562. * of the failed commands are on soft_reset devices, and if so, skip
  1563. * the reset.
  1564. */
  1565. for (channel = 0; channel <= shost->max_channel; channel++) {
  1566. if (scsi_host_eh_past_deadline(shost)) {
  1567. list_splice_init(&check_list, work_q);
  1568. SCSI_LOG_ERROR_RECOVERY(3,
  1569. shost_printk(KERN_INFO, shost,
  1570. "%s: skip BRST, past eh deadline\n",
  1571. current->comm));
  1572. return list_empty(work_q);
  1573. }
  1574. chan_scmd = NULL;
  1575. list_for_each_entry(scmd, work_q, eh_entry) {
  1576. if (channel == scmd_channel(scmd)) {
  1577. chan_scmd = scmd;
  1578. break;
  1579. /*
  1580. * FIXME add back in some support for
  1581. * soft_reset devices.
  1582. */
  1583. }
  1584. }
  1585. if (!chan_scmd)
  1586. continue;
  1587. SCSI_LOG_ERROR_RECOVERY(3,
  1588. shost_printk(KERN_INFO, shost,
  1589. "%s: Sending BRST chan: %d\n",
  1590. current->comm, channel));
  1591. rtn = scsi_try_bus_reset(chan_scmd);
  1592. if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
  1593. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1594. if (channel == scmd_channel(scmd)) {
  1595. if (rtn == FAST_IO_FAIL)
  1596. scsi_eh_finish_cmd(scmd,
  1597. done_q);
  1598. else
  1599. list_move_tail(&scmd->eh_entry,
  1600. &check_list);
  1601. }
  1602. }
  1603. } else {
  1604. SCSI_LOG_ERROR_RECOVERY(3,
  1605. shost_printk(KERN_INFO, shost,
  1606. "%s: BRST failed chan: %d\n",
  1607. current->comm, channel));
  1608. }
  1609. }
  1610. return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
  1611. }
  1612. /**
  1613. * scsi_eh_host_reset - send a host reset
  1614. * @shost: host to be reset.
  1615. * @work_q: &list_head for pending commands.
  1616. * @done_q: &list_head for processed commands.
  1617. */
  1618. static int scsi_eh_host_reset(struct Scsi_Host *shost,
  1619. struct list_head *work_q,
  1620. struct list_head *done_q)
  1621. {
  1622. struct scsi_cmnd *scmd, *next;
  1623. LIST_HEAD(check_list);
  1624. enum scsi_disposition rtn;
  1625. if (!list_empty(work_q)) {
  1626. scmd = list_entry(work_q->next,
  1627. struct scsi_cmnd, eh_entry);
  1628. SCSI_LOG_ERROR_RECOVERY(3,
  1629. shost_printk(KERN_INFO, shost,
  1630. "%s: Sending HRST\n",
  1631. current->comm));
  1632. rtn = scsi_try_host_reset(scmd);
  1633. if (rtn == SUCCESS) {
  1634. list_splice_init(work_q, &check_list);
  1635. } else if (rtn == FAST_IO_FAIL) {
  1636. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1637. scsi_eh_finish_cmd(scmd, done_q);
  1638. }
  1639. } else {
  1640. SCSI_LOG_ERROR_RECOVERY(3,
  1641. shost_printk(KERN_INFO, shost,
  1642. "%s: HRST failed\n",
  1643. current->comm));
  1644. }
  1645. }
  1646. return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
  1647. }
  1648. /**
  1649. * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
  1650. * @work_q: &list_head for pending commands.
  1651. * @done_q: &list_head for processed commands.
  1652. */
  1653. static void scsi_eh_offline_sdevs(struct list_head *work_q,
  1654. struct list_head *done_q)
  1655. {
  1656. struct scsi_cmnd *scmd, *next;
  1657. struct scsi_device *sdev;
  1658. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1659. sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
  1660. "not ready after error recovery\n");
  1661. sdev = scmd->device;
  1662. mutex_lock(&sdev->state_mutex);
  1663. scsi_device_set_state(sdev, SDEV_OFFLINE);
  1664. mutex_unlock(&sdev->state_mutex);
  1665. scsi_eh_finish_cmd(scmd, done_q);
  1666. }
  1667. return;
  1668. }
  1669. /**
  1670. * scsi_noretry_cmd - determine if command should be failed fast
  1671. * @scmd: SCSI cmd to examine.
  1672. */
  1673. bool scsi_noretry_cmd(struct scsi_cmnd *scmd)
  1674. {
  1675. struct request *req = scsi_cmd_to_rq(scmd);
  1676. switch (host_byte(scmd->result)) {
  1677. case DID_OK:
  1678. break;
  1679. case DID_TIME_OUT:
  1680. goto check_type;
  1681. case DID_BUS_BUSY:
  1682. return !!(req->cmd_flags & REQ_FAILFAST_TRANSPORT);
  1683. case DID_PARITY:
  1684. return !!(req->cmd_flags & REQ_FAILFAST_DEV);
  1685. case DID_ERROR:
  1686. if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
  1687. return false;
  1688. fallthrough;
  1689. case DID_SOFT_ERROR:
  1690. return !!(req->cmd_flags & REQ_FAILFAST_DRIVER);
  1691. }
  1692. /* Never retry commands aborted due to a duration limit timeout */
  1693. if (scsi_ml_byte(scmd->result) == SCSIML_STAT_DL_TIMEOUT)
  1694. return true;
  1695. if (!scsi_status_is_check_condition(scmd->result))
  1696. return false;
  1697. check_type:
  1698. /*
  1699. * assume caller has checked sense and determined
  1700. * the check condition was retryable.
  1701. */
  1702. if (req->cmd_flags & REQ_FAILFAST_DEV || blk_rq_is_passthrough(req))
  1703. return true;
  1704. return false;
  1705. }
  1706. /**
  1707. * scsi_decide_disposition - Disposition a cmd on return from LLD.
  1708. * @scmd: SCSI cmd to examine.
  1709. *
  1710. * Notes:
  1711. * This is *only* called when we are examining the status after sending
  1712. * out the actual data command. any commands that are queued for error
  1713. * recovery (e.g. test_unit_ready) do *not* come through here.
  1714. *
  1715. * When this routine returns failed, it means the error handler thread
  1716. * is woken. In cases where the error code indicates an error that
  1717. * doesn't require the error handler read (i.e. we don't need to
  1718. * abort/reset), this function should return SUCCESS.
  1719. */
  1720. enum scsi_disposition scsi_decide_disposition(struct scsi_cmnd *scmd)
  1721. {
  1722. enum scsi_disposition rtn;
  1723. /*
  1724. * if the device is offline, then we clearly just pass the result back
  1725. * up to the top level.
  1726. */
  1727. if (!scsi_device_online(scmd->device)) {
  1728. SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
  1729. "%s: device offline - report as SUCCESS\n", __func__));
  1730. return SUCCESS;
  1731. }
  1732. /*
  1733. * first check the host byte, to see if there is anything in there
  1734. * that would indicate what we need to do.
  1735. */
  1736. switch (host_byte(scmd->result)) {
  1737. case DID_PASSTHROUGH:
  1738. /*
  1739. * no matter what, pass this through to the upper layer.
  1740. * nuke this special code so that it looks like we are saying
  1741. * did_ok.
  1742. */
  1743. scmd->result &= 0xff00ffff;
  1744. return SUCCESS;
  1745. case DID_OK:
  1746. /*
  1747. * looks good. drop through, and check the next byte.
  1748. */
  1749. break;
  1750. case DID_ABORT:
  1751. if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
  1752. set_host_byte(scmd, DID_TIME_OUT);
  1753. return SUCCESS;
  1754. }
  1755. fallthrough;
  1756. case DID_NO_CONNECT:
  1757. case DID_BAD_TARGET:
  1758. /*
  1759. * note - this means that we just report the status back
  1760. * to the top level driver, not that we actually think
  1761. * that it indicates SUCCESS.
  1762. */
  1763. return SUCCESS;
  1764. case DID_SOFT_ERROR:
  1765. /*
  1766. * when the low level driver returns did_soft_error,
  1767. * it is responsible for keeping an internal retry counter
  1768. * in order to avoid endless loops (db)
  1769. */
  1770. goto maybe_retry;
  1771. case DID_IMM_RETRY:
  1772. return NEEDS_RETRY;
  1773. case DID_REQUEUE:
  1774. return ADD_TO_MLQUEUE;
  1775. case DID_TRANSPORT_DISRUPTED:
  1776. /*
  1777. * LLD/transport was disrupted during processing of the IO.
  1778. * The transport class is now blocked/blocking,
  1779. * and the transport will decide what to do with the IO
  1780. * based on its timers and recovery capablilities if
  1781. * there are enough retries.
  1782. */
  1783. goto maybe_retry;
  1784. case DID_TRANSPORT_FAILFAST:
  1785. /*
  1786. * The transport decided to failfast the IO (most likely
  1787. * the fast io fail tmo fired), so send IO directly upwards.
  1788. */
  1789. return SUCCESS;
  1790. case DID_TRANSPORT_MARGINAL:
  1791. /*
  1792. * caller has decided not to do retries on
  1793. * abort success, so send IO directly upwards
  1794. */
  1795. return SUCCESS;
  1796. case DID_ERROR:
  1797. if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
  1798. /*
  1799. * execute reservation conflict processing code
  1800. * lower down
  1801. */
  1802. break;
  1803. fallthrough;
  1804. case DID_BUS_BUSY:
  1805. case DID_PARITY:
  1806. goto maybe_retry;
  1807. case DID_TIME_OUT:
  1808. /*
  1809. * when we scan the bus, we get timeout messages for
  1810. * these commands if there is no device available.
  1811. * other hosts report did_no_connect for the same thing.
  1812. */
  1813. if ((scmd->cmnd[0] == TEST_UNIT_READY ||
  1814. scmd->cmnd[0] == INQUIRY)) {
  1815. return SUCCESS;
  1816. } else {
  1817. return FAILED;
  1818. }
  1819. case DID_RESET:
  1820. return SUCCESS;
  1821. default:
  1822. return FAILED;
  1823. }
  1824. /*
  1825. * check the status byte to see if this indicates anything special.
  1826. */
  1827. switch (get_status_byte(scmd)) {
  1828. case SAM_STAT_TASK_SET_FULL:
  1829. scsi_handle_queue_full(scmd->device);
  1830. /*
  1831. * the case of trying to send too many commands to a
  1832. * tagged queueing device.
  1833. */
  1834. fallthrough;
  1835. case SAM_STAT_BUSY:
  1836. /*
  1837. * device can't talk to us at the moment. Should only
  1838. * occur (SAM-3) when the task queue is empty, so will cause
  1839. * the empty queue handling to trigger a stall in the
  1840. * device.
  1841. */
  1842. return ADD_TO_MLQUEUE;
  1843. case SAM_STAT_GOOD:
  1844. if (scmd->cmnd[0] == REPORT_LUNS)
  1845. scmd->device->sdev_target->expecting_lun_change = 0;
  1846. scsi_handle_queue_ramp_up(scmd->device);
  1847. if (scmd->sense_buffer && SCSI_SENSE_VALID(scmd))
  1848. /*
  1849. * If we have sense data, call scsi_check_sense() in
  1850. * order to set the correct SCSI ML byte (if any).
  1851. * No point in checking the return value, since the
  1852. * command has already completed successfully.
  1853. */
  1854. scsi_check_sense(scmd);
  1855. fallthrough;
  1856. case SAM_STAT_COMMAND_TERMINATED:
  1857. return SUCCESS;
  1858. case SAM_STAT_TASK_ABORTED:
  1859. goto maybe_retry;
  1860. case SAM_STAT_CHECK_CONDITION:
  1861. rtn = scsi_check_sense(scmd);
  1862. if (rtn == NEEDS_RETRY)
  1863. goto maybe_retry;
  1864. /* if rtn == FAILED, we have no sense information;
  1865. * returning FAILED will wake the error handler thread
  1866. * to collect the sense and redo the decide
  1867. * disposition */
  1868. return rtn;
  1869. case SAM_STAT_CONDITION_MET:
  1870. case SAM_STAT_INTERMEDIATE:
  1871. case SAM_STAT_INTERMEDIATE_CONDITION_MET:
  1872. case SAM_STAT_ACA_ACTIVE:
  1873. /*
  1874. * who knows? FIXME(eric)
  1875. */
  1876. return SUCCESS;
  1877. case SAM_STAT_RESERVATION_CONFLICT:
  1878. sdev_printk(KERN_INFO, scmd->device,
  1879. "reservation conflict\n");
  1880. set_scsi_ml_byte(scmd, SCSIML_STAT_RESV_CONFLICT);
  1881. return SUCCESS; /* causes immediate i/o error */
  1882. }
  1883. return FAILED;
  1884. maybe_retry:
  1885. /* we requeue for retry because the error was retryable, and
  1886. * the request was not marked fast fail. Note that above,
  1887. * even if the request is marked fast fail, we still requeue
  1888. * for queue congestion conditions (QUEUE_FULL or BUSY) */
  1889. if (scsi_cmd_retry_allowed(scmd) && !scsi_noretry_cmd(scmd)) {
  1890. return NEEDS_RETRY;
  1891. } else {
  1892. /*
  1893. * no more retries - report this one back to upper level.
  1894. */
  1895. return SUCCESS;
  1896. }
  1897. }
  1898. static enum rq_end_io_ret eh_lock_door_done(struct request *req,
  1899. blk_status_t status,
  1900. const struct io_comp_batch *iob)
  1901. {
  1902. blk_mq_free_request(req);
  1903. return RQ_END_IO_NONE;
  1904. }
  1905. /**
  1906. * scsi_eh_lock_door - Prevent medium removal for the specified device
  1907. * @sdev: SCSI device to prevent medium removal
  1908. *
  1909. * Locking:
  1910. * We must be called from process context.
  1911. *
  1912. * Notes:
  1913. * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
  1914. * head of the devices request queue, and continue.
  1915. */
  1916. static void scsi_eh_lock_door(struct scsi_device *sdev)
  1917. {
  1918. struct scsi_cmnd *scmd;
  1919. struct request *req;
  1920. req = scsi_alloc_request(sdev->request_queue, REQ_OP_DRV_IN, 0);
  1921. if (IS_ERR(req))
  1922. return;
  1923. scmd = blk_mq_rq_to_pdu(req);
  1924. scmd->cmnd[0] = ALLOW_MEDIUM_REMOVAL;
  1925. scmd->cmnd[1] = 0;
  1926. scmd->cmnd[2] = 0;
  1927. scmd->cmnd[3] = 0;
  1928. scmd->cmnd[4] = SCSI_REMOVAL_PREVENT;
  1929. scmd->cmnd[5] = 0;
  1930. scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
  1931. scmd->allowed = 5;
  1932. req->rq_flags |= RQF_QUIET;
  1933. req->timeout = 10 * HZ;
  1934. req->end_io = eh_lock_door_done;
  1935. blk_execute_rq_nowait(req, true);
  1936. }
  1937. /**
  1938. * scsi_restart_operations - restart io operations to the specified host.
  1939. * @shost: Host we are restarting.
  1940. *
  1941. * Notes:
  1942. * When we entered the error handler, we blocked all further i/o to
  1943. * this device. we need to 'reverse' this process.
  1944. */
  1945. static void scsi_restart_operations(struct Scsi_Host *shost)
  1946. {
  1947. struct scsi_device *sdev;
  1948. unsigned long flags;
  1949. /*
  1950. * If the door was locked, we need to insert a door lock request
  1951. * onto the head of the SCSI request queue for the device. There
  1952. * is no point trying to lock the door of an off-line device.
  1953. */
  1954. shost_for_each_device(sdev, shost) {
  1955. if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
  1956. scsi_eh_lock_door(sdev);
  1957. sdev->was_reset = 0;
  1958. }
  1959. }
  1960. /*
  1961. * next free up anything directly waiting upon the host. this
  1962. * will be requests for character device operations, and also for
  1963. * ioctls to queued block devices.
  1964. */
  1965. SCSI_LOG_ERROR_RECOVERY(3,
  1966. shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
  1967. spin_lock_irqsave(shost->host_lock, flags);
  1968. if (scsi_host_set_state(shost, SHOST_RUNNING))
  1969. if (scsi_host_set_state(shost, SHOST_CANCEL))
  1970. BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
  1971. spin_unlock_irqrestore(shost->host_lock, flags);
  1972. wake_up(&shost->host_wait);
  1973. /*
  1974. * finally we need to re-initiate requests that may be pending. we will
  1975. * have had everything blocked while error handling is taking place, and
  1976. * now that error recovery is done, we will need to ensure that these
  1977. * requests are started.
  1978. */
  1979. scsi_run_host_queues(shost);
  1980. /*
  1981. * if eh is active and host_eh_scheduled is pending we need to re-run
  1982. * recovery. we do this check after scsi_run_host_queues() to allow
  1983. * everything pent up since the last eh run a chance to make forward
  1984. * progress before we sync again. Either we'll immediately re-run
  1985. * recovery or scsi_device_unbusy() will wake us again when these
  1986. * pending commands complete.
  1987. */
  1988. spin_lock_irqsave(shost->host_lock, flags);
  1989. if (shost->host_eh_scheduled)
  1990. if (scsi_host_set_state(shost, SHOST_RECOVERY))
  1991. WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
  1992. spin_unlock_irqrestore(shost->host_lock, flags);
  1993. }
  1994. /**
  1995. * scsi_eh_ready_devs - check device ready state and recover if not.
  1996. * @shost: host to be recovered.
  1997. * @work_q: &list_head for pending commands.
  1998. * @done_q: &list_head for processed commands.
  1999. */
  2000. void scsi_eh_ready_devs(struct Scsi_Host *shost,
  2001. struct list_head *work_q,
  2002. struct list_head *done_q)
  2003. {
  2004. if (!scsi_eh_stu(shost, work_q, done_q))
  2005. if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
  2006. if (!scsi_eh_target_reset(shost, work_q, done_q))
  2007. if (!scsi_eh_bus_reset(shost, work_q, done_q))
  2008. if (!scsi_eh_host_reset(shost, work_q, done_q))
  2009. scsi_eh_offline_sdevs(work_q,
  2010. done_q);
  2011. }
  2012. EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
  2013. /**
  2014. * scsi_eh_flush_done_q - finish processed commands or retry them.
  2015. * @done_q: list_head of processed commands.
  2016. */
  2017. void scsi_eh_flush_done_q(struct list_head *done_q)
  2018. {
  2019. struct scsi_cmnd *scmd, *next;
  2020. list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
  2021. struct scsi_device *sdev = scmd->device;
  2022. list_del_init(&scmd->eh_entry);
  2023. if (scsi_device_online(sdev) && !scsi_noretry_cmd(scmd) &&
  2024. scsi_cmd_retry_allowed(scmd) &&
  2025. scsi_eh_should_retry_cmd(scmd)) {
  2026. SCSI_LOG_ERROR_RECOVERY(3,
  2027. scmd_printk(KERN_INFO, scmd,
  2028. "%s: flush retry cmd\n",
  2029. current->comm));
  2030. scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
  2031. blk_mq_kick_requeue_list(sdev->request_queue);
  2032. } else {
  2033. /*
  2034. * If just we got sense for the device (called
  2035. * scsi_eh_get_sense), scmd->result is already
  2036. * set, do not set DID_TIME_OUT.
  2037. */
  2038. if (!scmd->result &&
  2039. !(scmd->flags & SCMD_FORCE_EH_SUCCESS))
  2040. scmd->result |= (DID_TIME_OUT << 16);
  2041. SCSI_LOG_ERROR_RECOVERY(3,
  2042. scmd_printk(KERN_INFO, scmd,
  2043. "%s: flush finish cmd\n",
  2044. current->comm));
  2045. scsi_finish_command(scmd);
  2046. }
  2047. }
  2048. }
  2049. EXPORT_SYMBOL(scsi_eh_flush_done_q);
  2050. /**
  2051. * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
  2052. * @shost: Host to unjam.
  2053. *
  2054. * Notes:
  2055. * When we come in here, we *know* that all commands on the bus have
  2056. * either completed, failed or timed out. we also know that no further
  2057. * commands are being sent to the host, so things are relatively quiet
  2058. * and we have freedom to fiddle with things as we wish.
  2059. *
  2060. * This is only the *default* implementation. it is possible for
  2061. * individual drivers to supply their own version of this function, and
  2062. * if the maintainer wishes to do this, it is strongly suggested that
  2063. * this function be taken as a template and modified. this function
  2064. * was designed to correctly handle problems for about 95% of the
  2065. * different cases out there, and it should always provide at least a
  2066. * reasonable amount of error recovery.
  2067. *
  2068. * Any command marked 'failed' or 'timeout' must eventually have
  2069. * scsi_finish_cmd() called for it. we do all of the retry stuff
  2070. * here, so when we restart the host after we return it should have an
  2071. * empty queue.
  2072. */
  2073. static void scsi_unjam_host(struct Scsi_Host *shost)
  2074. {
  2075. unsigned long flags;
  2076. LIST_HEAD(eh_work_q);
  2077. LIST_HEAD(eh_done_q);
  2078. spin_lock_irqsave(shost->host_lock, flags);
  2079. list_splice_init(&shost->eh_cmd_q, &eh_work_q);
  2080. spin_unlock_irqrestore(shost->host_lock, flags);
  2081. SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
  2082. if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
  2083. scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
  2084. spin_lock_irqsave(shost->host_lock, flags);
  2085. if (shost->eh_deadline != -1)
  2086. shost->last_reset = 0;
  2087. spin_unlock_irqrestore(shost->host_lock, flags);
  2088. scsi_eh_flush_done_q(&eh_done_q);
  2089. }
  2090. /**
  2091. * scsi_error_handler - SCSI error handler thread
  2092. * @data: Host for which we are running.
  2093. *
  2094. * Notes:
  2095. * This is the main error handling loop. This is run as a kernel thread
  2096. * for every SCSI host and handles all error handling activity.
  2097. */
  2098. int scsi_error_handler(void *data)
  2099. {
  2100. struct Scsi_Host *shost = data;
  2101. /*
  2102. * We use TASK_INTERRUPTIBLE so that the thread is not
  2103. * counted against the load average as a running process.
  2104. * We never actually get interrupted because kthread_run
  2105. * disables signal delivery for the created thread.
  2106. */
  2107. while (true) {
  2108. /*
  2109. * The sequence in kthread_stop() sets the stop flag first
  2110. * then wakes the process. To avoid missed wakeups, the task
  2111. * should always be in a non running state before the stop
  2112. * flag is checked
  2113. */
  2114. set_current_state(TASK_INTERRUPTIBLE);
  2115. if (kthread_should_stop())
  2116. break;
  2117. if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
  2118. shost->host_failed != scsi_host_busy(shost)) {
  2119. SCSI_LOG_ERROR_RECOVERY(1,
  2120. shost_printk(KERN_INFO, shost,
  2121. "scsi_eh_%d: sleeping\n",
  2122. shost->host_no));
  2123. schedule();
  2124. continue;
  2125. }
  2126. __set_current_state(TASK_RUNNING);
  2127. SCSI_LOG_ERROR_RECOVERY(1,
  2128. shost_printk(KERN_INFO, shost,
  2129. "scsi_eh_%d: waking up %d/%d/%d\n",
  2130. shost->host_no, shost->host_eh_scheduled,
  2131. shost->host_failed,
  2132. scsi_host_busy(shost)));
  2133. /*
  2134. * We have a host that is failing for some reason. Figure out
  2135. * what we need to do to get it up and online again (if we can).
  2136. * If we fail, we end up taking the thing offline.
  2137. */
  2138. if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
  2139. SCSI_LOG_ERROR_RECOVERY(1,
  2140. shost_printk(KERN_ERR, shost,
  2141. "scsi_eh_%d: unable to autoresume\n",
  2142. shost->host_no));
  2143. continue;
  2144. }
  2145. if (shost->transportt->eh_strategy_handler)
  2146. shost->transportt->eh_strategy_handler(shost);
  2147. else
  2148. scsi_unjam_host(shost);
  2149. /* All scmds have been handled */
  2150. shost->host_failed = 0;
  2151. /*
  2152. * Note - if the above fails completely, the action is to take
  2153. * individual devices offline and flush the queue of any
  2154. * outstanding requests that may have been pending. When we
  2155. * restart, we restart any I/O to any other devices on the bus
  2156. * which are still online.
  2157. */
  2158. scsi_restart_operations(shost);
  2159. if (!shost->eh_noresume)
  2160. scsi_autopm_put_host(shost);
  2161. }
  2162. __set_current_state(TASK_RUNNING);
  2163. SCSI_LOG_ERROR_RECOVERY(1,
  2164. shost_printk(KERN_INFO, shost,
  2165. "Error handler scsi_eh_%d exiting\n",
  2166. shost->host_no));
  2167. shost->ehandler = NULL;
  2168. return 0;
  2169. }
  2170. /**
  2171. * scsi_report_bus_reset() - report bus reset observed
  2172. *
  2173. * Utility function used by low-level drivers to report that
  2174. * they have observed a bus reset on the bus being handled.
  2175. *
  2176. * @shost: Host in question
  2177. * @channel: channel on which reset was observed.
  2178. *
  2179. * Returns: Nothing
  2180. *
  2181. * Lock status: Host lock must be held.
  2182. *
  2183. * Notes: This only needs to be called if the reset is one which
  2184. * originates from an unknown location. Resets originated
  2185. * by the mid-level itself don't need to call this, but there
  2186. * should be no harm.
  2187. *
  2188. * The main purpose of this is to make sure that a CHECK_CONDITION
  2189. * is properly treated.
  2190. */
  2191. void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
  2192. {
  2193. struct scsi_device *sdev;
  2194. __shost_for_each_device(sdev, shost) {
  2195. if (channel == sdev_channel(sdev))
  2196. __scsi_report_device_reset(sdev, NULL);
  2197. }
  2198. }
  2199. EXPORT_SYMBOL(scsi_report_bus_reset);
  2200. /**
  2201. * scsi_report_device_reset() - report device reset observed
  2202. *
  2203. * Utility function used by low-level drivers to report that
  2204. * they have observed a device reset on the device being handled.
  2205. *
  2206. * @shost: Host in question
  2207. * @channel: channel on which reset was observed
  2208. * @target: target on which reset was observed
  2209. *
  2210. * Returns: Nothing
  2211. *
  2212. * Lock status: Host lock must be held
  2213. *
  2214. * Notes: This only needs to be called if the reset is one which
  2215. * originates from an unknown location. Resets originated
  2216. * by the mid-level itself don't need to call this, but there
  2217. * should be no harm.
  2218. *
  2219. * The main purpose of this is to make sure that a CHECK_CONDITION
  2220. * is properly treated.
  2221. */
  2222. void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
  2223. {
  2224. struct scsi_device *sdev;
  2225. __shost_for_each_device(sdev, shost) {
  2226. if (channel == sdev_channel(sdev) &&
  2227. target == sdev_id(sdev))
  2228. __scsi_report_device_reset(sdev, NULL);
  2229. }
  2230. }
  2231. EXPORT_SYMBOL(scsi_report_device_reset);
  2232. /**
  2233. * scsi_ioctl_reset: explicitly reset a host/bus/target/device
  2234. * @dev: scsi_device to operate on
  2235. * @arg: reset type (see sg.h)
  2236. */
  2237. int
  2238. scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
  2239. {
  2240. struct scsi_cmnd *scmd;
  2241. struct Scsi_Host *shost = dev->host;
  2242. struct request *rq;
  2243. unsigned long flags;
  2244. int error = 0, val;
  2245. enum scsi_disposition rtn;
  2246. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  2247. return -EACCES;
  2248. error = get_user(val, arg);
  2249. if (error)
  2250. return error;
  2251. if (scsi_autopm_get_host(shost) < 0)
  2252. return -EIO;
  2253. error = -EIO;
  2254. rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
  2255. shost->hostt->cmd_size, GFP_KERNEL);
  2256. if (!rq)
  2257. goto out_put_autopm_host;
  2258. blk_rq_init(NULL, rq);
  2259. scmd = (struct scsi_cmnd *)(rq + 1);
  2260. scsi_init_command(dev, scmd);
  2261. scmd->submitter = SUBMITTED_BY_SCSI_RESET_IOCTL;
  2262. scmd->flags |= SCMD_LAST;
  2263. memset(&scmd->sdb, 0, sizeof(scmd->sdb));
  2264. scmd->cmd_len = 0;
  2265. scmd->sc_data_direction = DMA_BIDIRECTIONAL;
  2266. spin_lock_irqsave(shost->host_lock, flags);
  2267. shost->tmf_in_progress = 1;
  2268. spin_unlock_irqrestore(shost->host_lock, flags);
  2269. switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
  2270. case SG_SCSI_RESET_NOTHING:
  2271. rtn = SUCCESS;
  2272. break;
  2273. case SG_SCSI_RESET_DEVICE:
  2274. rtn = scsi_try_bus_device_reset(scmd);
  2275. if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
  2276. break;
  2277. fallthrough;
  2278. case SG_SCSI_RESET_TARGET:
  2279. rtn = scsi_try_target_reset(scmd);
  2280. if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
  2281. break;
  2282. fallthrough;
  2283. case SG_SCSI_RESET_BUS:
  2284. rtn = scsi_try_bus_reset(scmd);
  2285. if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
  2286. break;
  2287. fallthrough;
  2288. case SG_SCSI_RESET_HOST:
  2289. rtn = scsi_try_host_reset(scmd);
  2290. if (rtn == SUCCESS)
  2291. break;
  2292. fallthrough;
  2293. default:
  2294. rtn = FAILED;
  2295. break;
  2296. }
  2297. error = (rtn == SUCCESS) ? 0 : -EIO;
  2298. spin_lock_irqsave(shost->host_lock, flags);
  2299. shost->tmf_in_progress = 0;
  2300. spin_unlock_irqrestore(shost->host_lock, flags);
  2301. /*
  2302. * be sure to wake up anyone who was sleeping or had their queue
  2303. * suspended while we performed the TMF.
  2304. */
  2305. SCSI_LOG_ERROR_RECOVERY(3,
  2306. shost_printk(KERN_INFO, shost,
  2307. "waking up host to restart after TMF\n"));
  2308. wake_up(&shost->host_wait);
  2309. scsi_run_host_queues(shost);
  2310. kfree(rq);
  2311. out_put_autopm_host:
  2312. scsi_autopm_put_host(shost);
  2313. return error;
  2314. }
  2315. bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
  2316. struct scsi_sense_hdr *sshdr)
  2317. {
  2318. return scsi_normalize_sense(cmd->sense_buffer,
  2319. SCSI_SENSE_BUFFERSIZE, sshdr);
  2320. }
  2321. EXPORT_SYMBOL(scsi_command_normalize_sense);
  2322. /**
  2323. * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
  2324. * @sense_buffer: byte array of sense data
  2325. * @sb_len: number of valid bytes in sense_buffer
  2326. * @info_out: pointer to 64 integer where 8 or 4 byte information
  2327. * field will be placed if found.
  2328. *
  2329. * Return value:
  2330. * true if information field found, false if not found.
  2331. */
  2332. bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
  2333. u64 *info_out)
  2334. {
  2335. const u8 * ucp;
  2336. if (sb_len < 7)
  2337. return false;
  2338. switch (sense_buffer[0] & 0x7f) {
  2339. case 0x70:
  2340. case 0x71:
  2341. if (sense_buffer[0] & 0x80) {
  2342. *info_out = get_unaligned_be32(&sense_buffer[3]);
  2343. return true;
  2344. }
  2345. return false;
  2346. case 0x72:
  2347. case 0x73:
  2348. ucp = scsi_sense_desc_find(sense_buffer, sb_len,
  2349. 0 /* info desc */);
  2350. if (ucp && (0xa == ucp[1])) {
  2351. *info_out = get_unaligned_be64(&ucp[4]);
  2352. return true;
  2353. }
  2354. return false;
  2355. default:
  2356. return false;
  2357. }
  2358. }
  2359. EXPORT_SYMBOL(scsi_get_sense_info_fld);