scsi_error.c 68 KB

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