scsi_error.c 70 KB

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