scsi_error.c 69 KB

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