scsi_error.c 72 KB

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