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->transportt->eh_timed_out)
  251. rtn = host->transportt->eh_timed_out(scmd);
  252. else if (host->hostt->eh_timed_out)
  253. rtn = host->hostt->eh_timed_out(scmd);
  254. if (rtn == BLK_EH_NOT_HANDLED) {
  255. if (!host->hostt->no_async_abort &&
  256. scsi_abort_command(scmd) == SUCCESS)
  257. return BLK_EH_NOT_HANDLED;
  258. set_host_byte(scmd, DID_TIME_OUT);
  259. if (!scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))
  260. rtn = BLK_EH_HANDLED;
  261. }
  262. return rtn;
  263. }
  264. /**
  265. * scsi_block_when_processing_errors - Prevent cmds from being queued.
  266. * @sdev: Device on which we are performing recovery.
  267. *
  268. * Description:
  269. * We block until the host is out of error recovery, and then check to
  270. * see whether the host or the device is offline.
  271. *
  272. * Return value:
  273. * 0 when dev was taken offline by error recovery. 1 OK to proceed.
  274. */
  275. int scsi_block_when_processing_errors(struct scsi_device *sdev)
  276. {
  277. int online;
  278. wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
  279. online = scsi_device_online(sdev);
  280. SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev,
  281. "%s: rtn: %d\n", __func__, online));
  282. return online;
  283. }
  284. EXPORT_SYMBOL(scsi_block_when_processing_errors);
  285. #ifdef CONFIG_SCSI_LOGGING
  286. /**
  287. * scsi_eh_prt_fail_stats - Log info on failures.
  288. * @shost: scsi host being recovered.
  289. * @work_q: Queue of scsi cmds to process.
  290. */
  291. static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
  292. struct list_head *work_q)
  293. {
  294. struct scsi_cmnd *scmd;
  295. struct scsi_device *sdev;
  296. int total_failures = 0;
  297. int cmd_failed = 0;
  298. int cmd_cancel = 0;
  299. int devices_failed = 0;
  300. shost_for_each_device(sdev, shost) {
  301. list_for_each_entry(scmd, work_q, eh_entry) {
  302. if (scmd->device == sdev) {
  303. ++total_failures;
  304. if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
  305. ++cmd_cancel;
  306. else
  307. ++cmd_failed;
  308. }
  309. }
  310. if (cmd_cancel || cmd_failed) {
  311. SCSI_LOG_ERROR_RECOVERY(3,
  312. shost_printk(KERN_INFO, shost,
  313. "%s: cmds failed: %d, cancel: %d\n",
  314. __func__, cmd_failed,
  315. cmd_cancel));
  316. cmd_cancel = 0;
  317. cmd_failed = 0;
  318. ++devices_failed;
  319. }
  320. }
  321. SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
  322. "Total of %d commands on %d"
  323. " devices require eh work\n",
  324. total_failures, devices_failed));
  325. }
  326. #endif
  327. /**
  328. * scsi_report_lun_change - Set flag on all *other* devices on the same target
  329. * to indicate that a UNIT ATTENTION is expected.
  330. * @sdev: Device reporting the UNIT ATTENTION
  331. */
  332. static void scsi_report_lun_change(struct scsi_device *sdev)
  333. {
  334. sdev->sdev_target->expecting_lun_change = 1;
  335. }
  336. /**
  337. * scsi_report_sense - Examine scsi sense information and log messages for
  338. * certain conditions, also issue uevents for some of them.
  339. * @sdev: Device reporting the sense code
  340. * @sshdr: sshdr to be examined
  341. */
  342. static void scsi_report_sense(struct scsi_device *sdev,
  343. struct scsi_sense_hdr *sshdr)
  344. {
  345. enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
  346. if (sshdr->sense_key == UNIT_ATTENTION) {
  347. if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
  348. evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
  349. sdev_printk(KERN_WARNING, sdev,
  350. "Inquiry data has changed");
  351. } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
  352. evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
  353. scsi_report_lun_change(sdev);
  354. sdev_printk(KERN_WARNING, sdev,
  355. "Warning! Received an indication that the "
  356. "LUN assignments on this target have "
  357. "changed. The Linux SCSI layer does not "
  358. "automatically remap LUN assignments.\n");
  359. } else if (sshdr->asc == 0x3f)
  360. sdev_printk(KERN_WARNING, sdev,
  361. "Warning! Received an indication that the "
  362. "operating parameters on this target have "
  363. "changed. The Linux SCSI layer does not "
  364. "automatically adjust these parameters.\n");
  365. if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
  366. evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
  367. sdev_printk(KERN_WARNING, sdev,
  368. "Warning! Received an indication that the "
  369. "LUN reached a thin provisioning soft "
  370. "threshold.\n");
  371. }
  372. if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
  373. evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
  374. sdev_printk(KERN_WARNING, sdev,
  375. "Mode parameters changed");
  376. } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
  377. evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
  378. sdev_printk(KERN_WARNING, sdev,
  379. "Asymmetric access state changed");
  380. } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
  381. evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
  382. sdev_printk(KERN_WARNING, sdev,
  383. "Capacity data has changed");
  384. } else if (sshdr->asc == 0x2a)
  385. sdev_printk(KERN_WARNING, sdev,
  386. "Parameters changed");
  387. }
  388. if (evt_type != SDEV_EVT_MAXBITS) {
  389. set_bit(evt_type, sdev->pending_events);
  390. schedule_work(&sdev->event_work);
  391. }
  392. }
  393. /**
  394. * scsi_check_sense - Examine scsi cmd sense
  395. * @scmd: Cmd to have sense checked.
  396. *
  397. * Return value:
  398. * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
  399. *
  400. * Notes:
  401. * When a deferred error is detected the current command has
  402. * not been executed and needs retrying.
  403. */
  404. int scsi_check_sense(struct scsi_cmnd *scmd)
  405. {
  406. struct scsi_device *sdev = scmd->device;
  407. struct scsi_sense_hdr sshdr;
  408. if (! scsi_command_normalize_sense(scmd, &sshdr))
  409. return FAILED; /* no valid sense data */
  410. scsi_report_sense(sdev, &sshdr);
  411. if (scsi_sense_is_deferred(&sshdr))
  412. return NEEDS_RETRY;
  413. if (sdev->handler && sdev->handler->check_sense) {
  414. int rc;
  415. rc = sdev->handler->check_sense(sdev, &sshdr);
  416. if (rc != SCSI_RETURN_NOT_HANDLED)
  417. return rc;
  418. /* handler does not care. Drop down to default handling */
  419. }
  420. if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
  421. /*
  422. * nasty: for mid-layer issued TURs, we need to return the
  423. * actual sense data without any recovery attempt. For eh
  424. * issued ones, we need to try to recover and interpret
  425. */
  426. return SUCCESS;
  427. /*
  428. * Previous logic looked for FILEMARK, EOM or ILI which are
  429. * mainly associated with tapes and returned SUCCESS.
  430. */
  431. if (sshdr.response_code == 0x70) {
  432. /* fixed format */
  433. if (scmd->sense_buffer[2] & 0xe0)
  434. return SUCCESS;
  435. } else {
  436. /*
  437. * descriptor format: look for "stream commands sense data
  438. * descriptor" (see SSC-3). Assume single sense data
  439. * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
  440. */
  441. if ((sshdr.additional_length > 3) &&
  442. (scmd->sense_buffer[8] == 0x4) &&
  443. (scmd->sense_buffer[11] & 0xe0))
  444. return SUCCESS;
  445. }
  446. switch (sshdr.sense_key) {
  447. case NO_SENSE:
  448. return SUCCESS;
  449. case RECOVERED_ERROR:
  450. return /* soft_error */ SUCCESS;
  451. case ABORTED_COMMAND:
  452. if (sshdr.asc == 0x10) /* DIF */
  453. return SUCCESS;
  454. return NEEDS_RETRY;
  455. case NOT_READY:
  456. case UNIT_ATTENTION:
  457. /*
  458. * if we are expecting a cc/ua because of a bus reset that we
  459. * performed, treat this just as a retry. otherwise this is
  460. * information that we should pass up to the upper-level driver
  461. * so that we can deal with it there.
  462. */
  463. if (scmd->device->expecting_cc_ua) {
  464. /*
  465. * Because some device does not queue unit
  466. * attentions correctly, we carefully check
  467. * additional sense code and qualifier so as
  468. * not to squash media change unit attention.
  469. */
  470. if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
  471. scmd->device->expecting_cc_ua = 0;
  472. return NEEDS_RETRY;
  473. }
  474. }
  475. /*
  476. * we might also expect a cc/ua if another LUN on the target
  477. * reported a UA with an ASC/ASCQ of 3F 0E -
  478. * REPORTED LUNS DATA HAS CHANGED.
  479. */
  480. if (scmd->device->sdev_target->expecting_lun_change &&
  481. sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
  482. return NEEDS_RETRY;
  483. /*
  484. * if the device is in the process of becoming ready, we
  485. * should retry.
  486. */
  487. if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
  488. return NEEDS_RETRY;
  489. /*
  490. * if the device is not started, we need to wake
  491. * the error handler to start the motor
  492. */
  493. if (scmd->device->allow_restart &&
  494. (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
  495. return FAILED;
  496. /*
  497. * Pass the UA upwards for a determination in the completion
  498. * functions.
  499. */
  500. return SUCCESS;
  501. /* these are not supported */
  502. case DATA_PROTECT:
  503. if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
  504. /* Thin provisioning hard threshold reached */
  505. set_host_byte(scmd, DID_ALLOC_FAILURE);
  506. return SUCCESS;
  507. }
  508. case COPY_ABORTED:
  509. case VOLUME_OVERFLOW:
  510. case MISCOMPARE:
  511. case BLANK_CHECK:
  512. set_host_byte(scmd, DID_TARGET_FAILURE);
  513. return SUCCESS;
  514. case MEDIUM_ERROR:
  515. if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
  516. sshdr.asc == 0x13 || /* AMNF DATA FIELD */
  517. sshdr.asc == 0x14) { /* RECORD NOT FOUND */
  518. set_host_byte(scmd, DID_MEDIUM_ERROR);
  519. return SUCCESS;
  520. }
  521. return NEEDS_RETRY;
  522. case HARDWARE_ERROR:
  523. if (scmd->device->retry_hwerror)
  524. return ADD_TO_MLQUEUE;
  525. else
  526. set_host_byte(scmd, DID_TARGET_FAILURE);
  527. case ILLEGAL_REQUEST:
  528. if (sshdr.asc == 0x20 || /* Invalid command operation code */
  529. sshdr.asc == 0x21 || /* Logical block address out of range */
  530. sshdr.asc == 0x24 || /* Invalid field in cdb */
  531. sshdr.asc == 0x26) { /* Parameter value invalid */
  532. set_host_byte(scmd, DID_TARGET_FAILURE);
  533. }
  534. return SUCCESS;
  535. default:
  536. return SUCCESS;
  537. }
  538. }
  539. EXPORT_SYMBOL_GPL(scsi_check_sense);
  540. static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
  541. {
  542. struct scsi_host_template *sht = sdev->host->hostt;
  543. struct scsi_device *tmp_sdev;
  544. if (!sht->track_queue_depth ||
  545. sdev->queue_depth >= sdev->max_queue_depth)
  546. return;
  547. if (time_before(jiffies,
  548. sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
  549. return;
  550. if (time_before(jiffies,
  551. sdev->last_queue_full_time + sdev->queue_ramp_up_period))
  552. return;
  553. /*
  554. * Walk all devices of a target and do
  555. * ramp up on them.
  556. */
  557. shost_for_each_device(tmp_sdev, sdev->host) {
  558. if (tmp_sdev->channel != sdev->channel ||
  559. tmp_sdev->id != sdev->id ||
  560. tmp_sdev->queue_depth == sdev->max_queue_depth)
  561. continue;
  562. scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
  563. sdev->last_queue_ramp_up = jiffies;
  564. }
  565. }
  566. static void scsi_handle_queue_full(struct scsi_device *sdev)
  567. {
  568. struct scsi_host_template *sht = sdev->host->hostt;
  569. struct scsi_device *tmp_sdev;
  570. if (!sht->track_queue_depth)
  571. return;
  572. shost_for_each_device(tmp_sdev, sdev->host) {
  573. if (tmp_sdev->channel != sdev->channel ||
  574. tmp_sdev->id != sdev->id)
  575. continue;
  576. /*
  577. * We do not know the number of commands that were at
  578. * the device when we got the queue full so we start
  579. * from the highest possible value and work our way down.
  580. */
  581. scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
  582. }
  583. }
  584. /**
  585. * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
  586. * @scmd: SCSI cmd to examine.
  587. *
  588. * Notes:
  589. * This is *only* called when we are examining the status of commands
  590. * queued during error recovery. the main difference here is that we
  591. * don't allow for the possibility of retries here, and we are a lot
  592. * more restrictive about what we consider acceptable.
  593. */
  594. static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
  595. {
  596. /*
  597. * first check the host byte, to see if there is anything in there
  598. * that would indicate what we need to do.
  599. */
  600. if (host_byte(scmd->result) == DID_RESET) {
  601. /*
  602. * rats. we are already in the error handler, so we now
  603. * get to try and figure out what to do next. if the sense
  604. * is valid, we have a pretty good idea of what to do.
  605. * if not, we mark it as FAILED.
  606. */
  607. return scsi_check_sense(scmd);
  608. }
  609. if (host_byte(scmd->result) != DID_OK)
  610. return FAILED;
  611. /*
  612. * next, check the message byte.
  613. */
  614. if (msg_byte(scmd->result) != COMMAND_COMPLETE)
  615. return FAILED;
  616. /*
  617. * now, check the status byte to see if this indicates
  618. * anything special.
  619. */
  620. switch (status_byte(scmd->result)) {
  621. case GOOD:
  622. scsi_handle_queue_ramp_up(scmd->device);
  623. case COMMAND_TERMINATED:
  624. return SUCCESS;
  625. case CHECK_CONDITION:
  626. return scsi_check_sense(scmd);
  627. case CONDITION_GOOD:
  628. case INTERMEDIATE_GOOD:
  629. case INTERMEDIATE_C_GOOD:
  630. /*
  631. * who knows? FIXME(eric)
  632. */
  633. return SUCCESS;
  634. case RESERVATION_CONFLICT:
  635. if (scmd->cmnd[0] == TEST_UNIT_READY)
  636. /* it is a success, we probed the device and
  637. * found it */
  638. return SUCCESS;
  639. /* otherwise, we failed to send the command */
  640. return FAILED;
  641. case QUEUE_FULL:
  642. scsi_handle_queue_full(scmd->device);
  643. /* fall through */
  644. case BUSY:
  645. return NEEDS_RETRY;
  646. default:
  647. return FAILED;
  648. }
  649. return FAILED;
  650. }
  651. /**
  652. * scsi_eh_done - Completion function for error handling.
  653. * @scmd: Cmd that is done.
  654. */
  655. static void scsi_eh_done(struct scsi_cmnd *scmd)
  656. {
  657. struct completion *eh_action;
  658. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  659. "%s result: %x\n", __func__, scmd->result));
  660. eh_action = scmd->device->host->eh_action;
  661. if (eh_action)
  662. complete(eh_action);
  663. }
  664. /**
  665. * scsi_try_host_reset - ask host adapter to reset itself
  666. * @scmd: SCSI cmd to send host reset.
  667. */
  668. static int scsi_try_host_reset(struct scsi_cmnd *scmd)
  669. {
  670. unsigned long flags;
  671. int rtn;
  672. struct Scsi_Host *host = scmd->device->host;
  673. struct scsi_host_template *hostt = host->hostt;
  674. SCSI_LOG_ERROR_RECOVERY(3,
  675. shost_printk(KERN_INFO, host, "Snd Host RST\n"));
  676. if (!hostt->eh_host_reset_handler)
  677. return FAILED;
  678. rtn = hostt->eh_host_reset_handler(scmd);
  679. if (rtn == SUCCESS) {
  680. if (!hostt->skip_settle_delay)
  681. ssleep(HOST_RESET_SETTLE_TIME);
  682. spin_lock_irqsave(host->host_lock, flags);
  683. scsi_report_bus_reset(host, scmd_channel(scmd));
  684. spin_unlock_irqrestore(host->host_lock, flags);
  685. }
  686. return rtn;
  687. }
  688. /**
  689. * scsi_try_bus_reset - ask host to perform a bus reset
  690. * @scmd: SCSI cmd to send bus reset.
  691. */
  692. static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
  693. {
  694. unsigned long flags;
  695. int rtn;
  696. struct Scsi_Host *host = scmd->device->host;
  697. struct scsi_host_template *hostt = host->hostt;
  698. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  699. "%s: Snd Bus RST\n", __func__));
  700. if (!hostt->eh_bus_reset_handler)
  701. return FAILED;
  702. rtn = hostt->eh_bus_reset_handler(scmd);
  703. if (rtn == SUCCESS) {
  704. if (!hostt->skip_settle_delay)
  705. ssleep(BUS_RESET_SETTLE_TIME);
  706. spin_lock_irqsave(host->host_lock, flags);
  707. scsi_report_bus_reset(host, scmd_channel(scmd));
  708. spin_unlock_irqrestore(host->host_lock, flags);
  709. }
  710. return rtn;
  711. }
  712. static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
  713. {
  714. sdev->was_reset = 1;
  715. sdev->expecting_cc_ua = 1;
  716. }
  717. /**
  718. * scsi_try_target_reset - Ask host to perform a target reset
  719. * @scmd: SCSI cmd used to send a target reset
  720. *
  721. * Notes:
  722. * There is no timeout for this operation. if this operation is
  723. * unreliable for a given host, then the host itself needs to put a
  724. * timer on it, and set the host back to a consistent state prior to
  725. * returning.
  726. */
  727. static int scsi_try_target_reset(struct scsi_cmnd *scmd)
  728. {
  729. unsigned long flags;
  730. int rtn;
  731. struct Scsi_Host *host = scmd->device->host;
  732. struct scsi_host_template *hostt = host->hostt;
  733. if (!hostt->eh_target_reset_handler)
  734. return FAILED;
  735. rtn = hostt->eh_target_reset_handler(scmd);
  736. if (rtn == SUCCESS) {
  737. spin_lock_irqsave(host->host_lock, flags);
  738. __starget_for_each_device(scsi_target(scmd->device), NULL,
  739. __scsi_report_device_reset);
  740. spin_unlock_irqrestore(host->host_lock, flags);
  741. }
  742. return rtn;
  743. }
  744. /**
  745. * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
  746. * @scmd: SCSI cmd used to send BDR
  747. *
  748. * Notes:
  749. * There is no timeout for this operation. if this operation is
  750. * unreliable for a given host, then the host itself needs to put a
  751. * timer on it, and set the host back to a consistent state prior to
  752. * returning.
  753. */
  754. static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
  755. {
  756. int rtn;
  757. struct scsi_host_template *hostt = scmd->device->host->hostt;
  758. if (!hostt->eh_device_reset_handler)
  759. return FAILED;
  760. rtn = hostt->eh_device_reset_handler(scmd);
  761. if (rtn == SUCCESS)
  762. __scsi_report_device_reset(scmd->device, NULL);
  763. return rtn;
  764. }
  765. /**
  766. * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
  767. * @hostt: SCSI driver host template
  768. * @scmd: SCSI cmd used to send a target reset
  769. *
  770. * Return value:
  771. * SUCCESS, FAILED, or FAST_IO_FAIL
  772. *
  773. * Notes:
  774. * SUCCESS does not necessarily indicate that the command
  775. * has been aborted; it only indicates that the LLDDs
  776. * has cleared all references to that command.
  777. * LLDDs should return FAILED only if an abort was required
  778. * but could not be executed. LLDDs should return FAST_IO_FAIL
  779. * if the device is temporarily unavailable (eg due to a
  780. * link down on FibreChannel)
  781. */
  782. static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt,
  783. struct scsi_cmnd *scmd)
  784. {
  785. if (!hostt->eh_abort_handler)
  786. return FAILED;
  787. return hostt->eh_abort_handler(scmd);
  788. }
  789. static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
  790. {
  791. if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
  792. if (scsi_try_bus_device_reset(scmd) != SUCCESS)
  793. if (scsi_try_target_reset(scmd) != SUCCESS)
  794. if (scsi_try_bus_reset(scmd) != SUCCESS)
  795. scsi_try_host_reset(scmd);
  796. }
  797. /**
  798. * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
  799. * @scmd: SCSI command structure to hijack
  800. * @ses: structure to save restore information
  801. * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
  802. * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
  803. * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
  804. *
  805. * This function is used to save a scsi command information before re-execution
  806. * as part of the error recovery process. If @sense_bytes is 0 the command
  807. * sent must be one that does not transfer any data. If @sense_bytes != 0
  808. * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
  809. * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
  810. */
  811. void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
  812. unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
  813. {
  814. struct scsi_device *sdev = scmd->device;
  815. /*
  816. * We need saved copies of a number of fields - this is because
  817. * error handling may need to overwrite these with different values
  818. * to run different commands, and once error handling is complete,
  819. * we will need to restore these values prior to running the actual
  820. * command.
  821. */
  822. ses->cmd_len = scmd->cmd_len;
  823. ses->cmnd = scmd->cmnd;
  824. ses->data_direction = scmd->sc_data_direction;
  825. ses->sdb = scmd->sdb;
  826. ses->next_rq = scmd->request->next_rq;
  827. ses->result = scmd->result;
  828. ses->underflow = scmd->underflow;
  829. ses->prot_op = scmd->prot_op;
  830. scmd->prot_op = SCSI_PROT_NORMAL;
  831. scmd->eh_eflags = 0;
  832. scmd->cmnd = ses->eh_cmnd;
  833. memset(scmd->cmnd, 0, BLK_MAX_CDB);
  834. memset(&scmd->sdb, 0, sizeof(scmd->sdb));
  835. scmd->request->next_rq = NULL;
  836. scmd->result = 0;
  837. if (sense_bytes) {
  838. scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
  839. sense_bytes);
  840. sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
  841. scmd->sdb.length);
  842. scmd->sdb.table.sgl = &ses->sense_sgl;
  843. scmd->sc_data_direction = DMA_FROM_DEVICE;
  844. scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
  845. scmd->cmnd[0] = REQUEST_SENSE;
  846. scmd->cmnd[4] = scmd->sdb.length;
  847. scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
  848. } else {
  849. scmd->sc_data_direction = DMA_NONE;
  850. if (cmnd) {
  851. BUG_ON(cmnd_size > BLK_MAX_CDB);
  852. memcpy(scmd->cmnd, cmnd, cmnd_size);
  853. scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
  854. }
  855. }
  856. scmd->underflow = 0;
  857. if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
  858. scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
  859. (sdev->lun << 5 & 0xe0);
  860. /*
  861. * Zero the sense buffer. The scsi spec mandates that any
  862. * untransferred sense data should be interpreted as being zero.
  863. */
  864. memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  865. }
  866. EXPORT_SYMBOL(scsi_eh_prep_cmnd);
  867. /**
  868. * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
  869. * @scmd: SCSI command structure to restore
  870. * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
  871. *
  872. * Undo any damage done by above scsi_eh_prep_cmnd().
  873. */
  874. void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
  875. {
  876. /*
  877. * Restore original data
  878. */
  879. scmd->cmd_len = ses->cmd_len;
  880. scmd->cmnd = ses->cmnd;
  881. scmd->sc_data_direction = ses->data_direction;
  882. scmd->sdb = ses->sdb;
  883. scmd->request->next_rq = ses->next_rq;
  884. scmd->result = ses->result;
  885. scmd->underflow = ses->underflow;
  886. scmd->prot_op = ses->prot_op;
  887. }
  888. EXPORT_SYMBOL(scsi_eh_restore_cmnd);
  889. /**
  890. * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
  891. * @scmd: SCSI command structure to hijack
  892. * @cmnd: CDB to send
  893. * @cmnd_size: size in bytes of @cmnd
  894. * @timeout: timeout for this request
  895. * @sense_bytes: size of sense data to copy or 0
  896. *
  897. * This function is used to send a scsi command down to a target device
  898. * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
  899. *
  900. * Return value:
  901. * SUCCESS or FAILED or NEEDS_RETRY
  902. */
  903. static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
  904. int cmnd_size, int timeout, unsigned sense_bytes)
  905. {
  906. struct scsi_device *sdev = scmd->device;
  907. struct Scsi_Host *shost = sdev->host;
  908. DECLARE_COMPLETION_ONSTACK(done);
  909. unsigned long timeleft = timeout;
  910. struct scsi_eh_save ses;
  911. const unsigned long stall_for = msecs_to_jiffies(100);
  912. int rtn;
  913. retry:
  914. scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
  915. shost->eh_action = &done;
  916. scsi_log_send(scmd);
  917. scmd->scsi_done = scsi_eh_done;
  918. rtn = shost->hostt->queuecommand(shost, scmd);
  919. if (rtn) {
  920. if (timeleft > stall_for) {
  921. scsi_eh_restore_cmnd(scmd, &ses);
  922. timeleft -= stall_for;
  923. msleep(jiffies_to_msecs(stall_for));
  924. goto retry;
  925. }
  926. /* signal not to enter either branch of the if () below */
  927. timeleft = 0;
  928. rtn = FAILED;
  929. } else {
  930. timeleft = wait_for_completion_timeout(&done, timeout);
  931. rtn = SUCCESS;
  932. }
  933. shost->eh_action = NULL;
  934. scsi_log_completion(scmd, rtn);
  935. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  936. "%s timeleft: %ld\n",
  937. __func__, timeleft));
  938. /*
  939. * If there is time left scsi_eh_done got called, and we will examine
  940. * the actual status codes to see whether the command actually did
  941. * complete normally, else if we have a zero return and no time left,
  942. * the command must still be pending, so abort it and return FAILED.
  943. * If we never actually managed to issue the command, because
  944. * ->queuecommand() kept returning non zero, use the rtn = FAILED
  945. * value above (so don't execute either branch of the if)
  946. */
  947. if (timeleft) {
  948. rtn = scsi_eh_completed_normally(scmd);
  949. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  950. "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
  951. switch (rtn) {
  952. case SUCCESS:
  953. case NEEDS_RETRY:
  954. case FAILED:
  955. break;
  956. case ADD_TO_MLQUEUE:
  957. rtn = NEEDS_RETRY;
  958. break;
  959. default:
  960. rtn = FAILED;
  961. break;
  962. }
  963. } else if (rtn != FAILED) {
  964. scsi_abort_eh_cmnd(scmd);
  965. rtn = FAILED;
  966. }
  967. scsi_eh_restore_cmnd(scmd, &ses);
  968. return rtn;
  969. }
  970. /**
  971. * scsi_request_sense - Request sense data from a particular target.
  972. * @scmd: SCSI cmd for request sense.
  973. *
  974. * Notes:
  975. * Some hosts automatically obtain this information, others require
  976. * that we obtain it on our own. This function will *not* return until
  977. * the command either times out, or it completes.
  978. */
  979. static int scsi_request_sense(struct scsi_cmnd *scmd)
  980. {
  981. return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
  982. }
  983. static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
  984. {
  985. if (scmd->request->cmd_type != REQ_TYPE_BLOCK_PC) {
  986. struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
  987. if (sdrv->eh_action)
  988. rtn = sdrv->eh_action(scmd, rtn);
  989. }
  990. return rtn;
  991. }
  992. /**
  993. * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
  994. * @scmd: Original SCSI cmd that eh has finished.
  995. * @done_q: Queue for processed commands.
  996. *
  997. * Notes:
  998. * We don't want to use the normal command completion while we are are
  999. * still handling errors - it may cause other commands to be queued,
  1000. * and that would disturb what we are doing. Thus we really want to
  1001. * keep a list of pending commands for final completion, and once we
  1002. * are ready to leave error handling we handle completion for real.
  1003. */
  1004. void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
  1005. {
  1006. scmd->eh_eflags = 0;
  1007. list_move_tail(&scmd->eh_entry, done_q);
  1008. }
  1009. EXPORT_SYMBOL(scsi_eh_finish_cmd);
  1010. /**
  1011. * scsi_eh_get_sense - Get device sense data.
  1012. * @work_q: Queue of commands to process.
  1013. * @done_q: Queue of processed commands.
  1014. *
  1015. * Description:
  1016. * See if we need to request sense information. if so, then get it
  1017. * now, so we have a better idea of what to do.
  1018. *
  1019. * Notes:
  1020. * This has the unfortunate side effect that if a shost adapter does
  1021. * not automatically request sense information, we end up shutting
  1022. * it down before we request it.
  1023. *
  1024. * All drivers should request sense information internally these days,
  1025. * so for now all I have to say is tough noogies if you end up in here.
  1026. *
  1027. * XXX: Long term this code should go away, but that needs an audit of
  1028. * all LLDDs first.
  1029. */
  1030. int scsi_eh_get_sense(struct list_head *work_q,
  1031. struct list_head *done_q)
  1032. {
  1033. struct scsi_cmnd *scmd, *next;
  1034. struct Scsi_Host *shost;
  1035. int rtn;
  1036. /*
  1037. * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
  1038. * should not get sense.
  1039. */
  1040. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1041. if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
  1042. (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
  1043. SCSI_SENSE_VALID(scmd))
  1044. continue;
  1045. shost = scmd->device->host;
  1046. if (scsi_host_eh_past_deadline(shost)) {
  1047. SCSI_LOG_ERROR_RECOVERY(3,
  1048. scmd_printk(KERN_INFO, scmd,
  1049. "%s: skip request sense, past eh deadline\n",
  1050. current->comm));
  1051. break;
  1052. }
  1053. if (status_byte(scmd->result) != CHECK_CONDITION)
  1054. /*
  1055. * don't request sense if there's no check condition
  1056. * status because the error we're processing isn't one
  1057. * that has a sense code (and some devices get
  1058. * confused by sense requests out of the blue)
  1059. */
  1060. continue;
  1061. SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
  1062. "%s: requesting sense\n",
  1063. current->comm));
  1064. rtn = scsi_request_sense(scmd);
  1065. if (rtn != SUCCESS)
  1066. continue;
  1067. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  1068. "sense requested, result %x\n", scmd->result));
  1069. SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
  1070. rtn = scsi_decide_disposition(scmd);
  1071. /*
  1072. * if the result was normal, then just pass it along to the
  1073. * upper level.
  1074. */
  1075. if (rtn == SUCCESS)
  1076. /* we don't want this command reissued, just
  1077. * finished with the sense data, so set
  1078. * retries to the max allowed to ensure it
  1079. * won't get reissued */
  1080. scmd->retries = scmd->allowed;
  1081. else if (rtn != NEEDS_RETRY)
  1082. continue;
  1083. scsi_eh_finish_cmd(scmd, done_q);
  1084. }
  1085. return list_empty(work_q);
  1086. }
  1087. EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
  1088. /**
  1089. * scsi_eh_tur - Send TUR to device.
  1090. * @scmd: &scsi_cmnd to send TUR
  1091. *
  1092. * Return value:
  1093. * 0 - Device is ready. 1 - Device NOT ready.
  1094. */
  1095. static int scsi_eh_tur(struct scsi_cmnd *scmd)
  1096. {
  1097. static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
  1098. int retry_cnt = 1, rtn;
  1099. retry_tur:
  1100. rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
  1101. scmd->device->eh_timeout, 0);
  1102. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  1103. "%s return: %x\n", __func__, rtn));
  1104. switch (rtn) {
  1105. case NEEDS_RETRY:
  1106. if (retry_cnt--)
  1107. goto retry_tur;
  1108. /*FALLTHRU*/
  1109. case SUCCESS:
  1110. return 0;
  1111. default:
  1112. return 1;
  1113. }
  1114. }
  1115. /**
  1116. * scsi_eh_test_devices - check if devices are responding from error recovery.
  1117. * @cmd_list: scsi commands in error recovery.
  1118. * @work_q: queue for commands which still need more error recovery
  1119. * @done_q: queue for commands which are finished
  1120. * @try_stu: boolean on if a STU command should be tried in addition to TUR.
  1121. *
  1122. * Decription:
  1123. * Tests if devices are in a working state. Commands to devices now in
  1124. * a working state are sent to the done_q while commands to devices which
  1125. * are still failing to respond are returned to the work_q for more
  1126. * processing.
  1127. **/
  1128. static int scsi_eh_test_devices(struct list_head *cmd_list,
  1129. struct list_head *work_q,
  1130. struct list_head *done_q, int try_stu)
  1131. {
  1132. struct scsi_cmnd *scmd, *next;
  1133. struct scsi_device *sdev;
  1134. int finish_cmds;
  1135. while (!list_empty(cmd_list)) {
  1136. scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
  1137. sdev = scmd->device;
  1138. if (!try_stu) {
  1139. if (scsi_host_eh_past_deadline(sdev->host)) {
  1140. /* Push items back onto work_q */
  1141. list_splice_init(cmd_list, work_q);
  1142. SCSI_LOG_ERROR_RECOVERY(3,
  1143. sdev_printk(KERN_INFO, sdev,
  1144. "%s: skip test device, past eh deadline",
  1145. current->comm));
  1146. break;
  1147. }
  1148. }
  1149. finish_cmds = !scsi_device_online(scmd->device) ||
  1150. (try_stu && !scsi_eh_try_stu(scmd) &&
  1151. !scsi_eh_tur(scmd)) ||
  1152. !scsi_eh_tur(scmd);
  1153. list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
  1154. if (scmd->device == sdev) {
  1155. if (finish_cmds &&
  1156. (try_stu ||
  1157. scsi_eh_action(scmd, SUCCESS) == SUCCESS))
  1158. scsi_eh_finish_cmd(scmd, done_q);
  1159. else
  1160. list_move_tail(&scmd->eh_entry, work_q);
  1161. }
  1162. }
  1163. return list_empty(work_q);
  1164. }
  1165. /**
  1166. * scsi_eh_abort_cmds - abort pending commands.
  1167. * @work_q: &list_head for pending commands.
  1168. * @done_q: &list_head for processed commands.
  1169. *
  1170. * Decription:
  1171. * Try and see whether or not it makes sense to try and abort the
  1172. * running command. This only works out to be the case if we have one
  1173. * command that has timed out. If the command simply failed, it makes
  1174. * no sense to try and abort the command, since as far as the shost
  1175. * adapter is concerned, it isn't running.
  1176. */
  1177. static int scsi_eh_abort_cmds(struct list_head *work_q,
  1178. struct list_head *done_q)
  1179. {
  1180. struct scsi_cmnd *scmd, *next;
  1181. LIST_HEAD(check_list);
  1182. int rtn;
  1183. struct Scsi_Host *shost;
  1184. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1185. if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
  1186. continue;
  1187. shost = scmd->device->host;
  1188. if (scsi_host_eh_past_deadline(shost)) {
  1189. list_splice_init(&check_list, work_q);
  1190. SCSI_LOG_ERROR_RECOVERY(3,
  1191. scmd_printk(KERN_INFO, scmd,
  1192. "%s: skip aborting cmd, past eh deadline\n",
  1193. current->comm));
  1194. return list_empty(work_q);
  1195. }
  1196. SCSI_LOG_ERROR_RECOVERY(3,
  1197. scmd_printk(KERN_INFO, scmd,
  1198. "%s: aborting cmd\n", current->comm));
  1199. rtn = scsi_try_to_abort_cmd(shost->hostt, scmd);
  1200. if (rtn == FAILED) {
  1201. SCSI_LOG_ERROR_RECOVERY(3,
  1202. scmd_printk(KERN_INFO, scmd,
  1203. "%s: aborting cmd failed\n",
  1204. current->comm));
  1205. list_splice_init(&check_list, work_q);
  1206. return list_empty(work_q);
  1207. }
  1208. scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
  1209. if (rtn == FAST_IO_FAIL)
  1210. scsi_eh_finish_cmd(scmd, done_q);
  1211. else
  1212. list_move_tail(&scmd->eh_entry, &check_list);
  1213. }
  1214. return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
  1215. }
  1216. /**
  1217. * scsi_eh_try_stu - Send START_UNIT to device.
  1218. * @scmd: &scsi_cmnd to send START_UNIT
  1219. *
  1220. * Return value:
  1221. * 0 - Device is ready. 1 - Device NOT ready.
  1222. */
  1223. static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
  1224. {
  1225. static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
  1226. if (scmd->device->allow_restart) {
  1227. int i, rtn = NEEDS_RETRY;
  1228. for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
  1229. rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
  1230. if (rtn == SUCCESS)
  1231. return 0;
  1232. }
  1233. return 1;
  1234. }
  1235. /**
  1236. * scsi_eh_stu - send START_UNIT if needed
  1237. * @shost: &scsi host being recovered.
  1238. * @work_q: &list_head for pending commands.
  1239. * @done_q: &list_head for processed commands.
  1240. *
  1241. * Notes:
  1242. * If commands are failing due to not ready, initializing command required,
  1243. * try revalidating the device, which will end up sending a start unit.
  1244. */
  1245. static int scsi_eh_stu(struct Scsi_Host *shost,
  1246. struct list_head *work_q,
  1247. struct list_head *done_q)
  1248. {
  1249. struct scsi_cmnd *scmd, *stu_scmd, *next;
  1250. struct scsi_device *sdev;
  1251. shost_for_each_device(sdev, shost) {
  1252. if (scsi_host_eh_past_deadline(shost)) {
  1253. SCSI_LOG_ERROR_RECOVERY(3,
  1254. sdev_printk(KERN_INFO, sdev,
  1255. "%s: skip START_UNIT, past eh deadline\n",
  1256. current->comm));
  1257. break;
  1258. }
  1259. stu_scmd = NULL;
  1260. list_for_each_entry(scmd, work_q, eh_entry)
  1261. if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
  1262. scsi_check_sense(scmd) == FAILED ) {
  1263. stu_scmd = scmd;
  1264. break;
  1265. }
  1266. if (!stu_scmd)
  1267. continue;
  1268. SCSI_LOG_ERROR_RECOVERY(3,
  1269. sdev_printk(KERN_INFO, sdev,
  1270. "%s: Sending START_UNIT\n",
  1271. current->comm));
  1272. if (!scsi_eh_try_stu(stu_scmd)) {
  1273. if (!scsi_device_online(sdev) ||
  1274. !scsi_eh_tur(stu_scmd)) {
  1275. list_for_each_entry_safe(scmd, next,
  1276. work_q, eh_entry) {
  1277. if (scmd->device == sdev &&
  1278. scsi_eh_action(scmd, SUCCESS) == SUCCESS)
  1279. scsi_eh_finish_cmd(scmd, done_q);
  1280. }
  1281. }
  1282. } else {
  1283. SCSI_LOG_ERROR_RECOVERY(3,
  1284. sdev_printk(KERN_INFO, sdev,
  1285. "%s: START_UNIT failed\n",
  1286. current->comm));
  1287. }
  1288. }
  1289. return list_empty(work_q);
  1290. }
  1291. /**
  1292. * scsi_eh_bus_device_reset - send bdr if needed
  1293. * @shost: scsi host being recovered.
  1294. * @work_q: &list_head for pending commands.
  1295. * @done_q: &list_head for processed commands.
  1296. *
  1297. * Notes:
  1298. * Try a bus device reset. Still, look to see whether we have multiple
  1299. * devices that are jammed or not - if we have multiple devices, it
  1300. * makes no sense to try bus_device_reset - we really would need to try
  1301. * a bus_reset instead.
  1302. */
  1303. static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
  1304. struct list_head *work_q,
  1305. struct list_head *done_q)
  1306. {
  1307. struct scsi_cmnd *scmd, *bdr_scmd, *next;
  1308. struct scsi_device *sdev;
  1309. int rtn;
  1310. shost_for_each_device(sdev, shost) {
  1311. if (scsi_host_eh_past_deadline(shost)) {
  1312. SCSI_LOG_ERROR_RECOVERY(3,
  1313. sdev_printk(KERN_INFO, sdev,
  1314. "%s: skip BDR, past eh deadline\n",
  1315. current->comm));
  1316. break;
  1317. }
  1318. bdr_scmd = NULL;
  1319. list_for_each_entry(scmd, work_q, eh_entry)
  1320. if (scmd->device == sdev) {
  1321. bdr_scmd = scmd;
  1322. break;
  1323. }
  1324. if (!bdr_scmd)
  1325. continue;
  1326. SCSI_LOG_ERROR_RECOVERY(3,
  1327. sdev_printk(KERN_INFO, sdev,
  1328. "%s: Sending BDR\n", current->comm));
  1329. rtn = scsi_try_bus_device_reset(bdr_scmd);
  1330. if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
  1331. if (!scsi_device_online(sdev) ||
  1332. rtn == FAST_IO_FAIL ||
  1333. !scsi_eh_tur(bdr_scmd)) {
  1334. list_for_each_entry_safe(scmd, next,
  1335. work_q, eh_entry) {
  1336. if (scmd->device == sdev &&
  1337. scsi_eh_action(scmd, rtn) != FAILED)
  1338. scsi_eh_finish_cmd(scmd,
  1339. done_q);
  1340. }
  1341. }
  1342. } else {
  1343. SCSI_LOG_ERROR_RECOVERY(3,
  1344. sdev_printk(KERN_INFO, sdev,
  1345. "%s: BDR failed\n", current->comm));
  1346. }
  1347. }
  1348. return list_empty(work_q);
  1349. }
  1350. /**
  1351. * scsi_eh_target_reset - send target reset if needed
  1352. * @shost: scsi host being recovered.
  1353. * @work_q: &list_head for pending commands.
  1354. * @done_q: &list_head for processed commands.
  1355. *
  1356. * Notes:
  1357. * Try a target reset.
  1358. */
  1359. static int scsi_eh_target_reset(struct Scsi_Host *shost,
  1360. struct list_head *work_q,
  1361. struct list_head *done_q)
  1362. {
  1363. LIST_HEAD(tmp_list);
  1364. LIST_HEAD(check_list);
  1365. list_splice_init(work_q, &tmp_list);
  1366. while (!list_empty(&tmp_list)) {
  1367. struct scsi_cmnd *next, *scmd;
  1368. int rtn;
  1369. unsigned int id;
  1370. if (scsi_host_eh_past_deadline(shost)) {
  1371. /* push back on work queue for further processing */
  1372. list_splice_init(&check_list, work_q);
  1373. list_splice_init(&tmp_list, work_q);
  1374. SCSI_LOG_ERROR_RECOVERY(3,
  1375. shost_printk(KERN_INFO, shost,
  1376. "%s: Skip target reset, past eh deadline\n",
  1377. current->comm));
  1378. return list_empty(work_q);
  1379. }
  1380. scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
  1381. id = scmd_id(scmd);
  1382. SCSI_LOG_ERROR_RECOVERY(3,
  1383. shost_printk(KERN_INFO, shost,
  1384. "%s: Sending target reset to target %d\n",
  1385. current->comm, id));
  1386. rtn = scsi_try_target_reset(scmd);
  1387. if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
  1388. SCSI_LOG_ERROR_RECOVERY(3,
  1389. shost_printk(KERN_INFO, shost,
  1390. "%s: Target reset failed"
  1391. " target: %d\n",
  1392. current->comm, id));
  1393. list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
  1394. if (scmd_id(scmd) != id)
  1395. continue;
  1396. if (rtn == SUCCESS)
  1397. list_move_tail(&scmd->eh_entry, &check_list);
  1398. else if (rtn == FAST_IO_FAIL)
  1399. scsi_eh_finish_cmd(scmd, done_q);
  1400. else
  1401. /* push back on work queue for further processing */
  1402. list_move(&scmd->eh_entry, work_q);
  1403. }
  1404. }
  1405. return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
  1406. }
  1407. /**
  1408. * scsi_eh_bus_reset - send a bus reset
  1409. * @shost: &scsi host being recovered.
  1410. * @work_q: &list_head for pending commands.
  1411. * @done_q: &list_head for processed commands.
  1412. */
  1413. static int scsi_eh_bus_reset(struct Scsi_Host *shost,
  1414. struct list_head *work_q,
  1415. struct list_head *done_q)
  1416. {
  1417. struct scsi_cmnd *scmd, *chan_scmd, *next;
  1418. LIST_HEAD(check_list);
  1419. unsigned int channel;
  1420. int rtn;
  1421. /*
  1422. * we really want to loop over the various channels, and do this on
  1423. * a channel by channel basis. we should also check to see if any
  1424. * of the failed commands are on soft_reset devices, and if so, skip
  1425. * the reset.
  1426. */
  1427. for (channel = 0; channel <= shost->max_channel; channel++) {
  1428. if (scsi_host_eh_past_deadline(shost)) {
  1429. list_splice_init(&check_list, work_q);
  1430. SCSI_LOG_ERROR_RECOVERY(3,
  1431. shost_printk(KERN_INFO, shost,
  1432. "%s: skip BRST, past eh deadline\n",
  1433. current->comm));
  1434. return list_empty(work_q);
  1435. }
  1436. chan_scmd = NULL;
  1437. list_for_each_entry(scmd, work_q, eh_entry) {
  1438. if (channel == scmd_channel(scmd)) {
  1439. chan_scmd = scmd;
  1440. break;
  1441. /*
  1442. * FIXME add back in some support for
  1443. * soft_reset devices.
  1444. */
  1445. }
  1446. }
  1447. if (!chan_scmd)
  1448. continue;
  1449. SCSI_LOG_ERROR_RECOVERY(3,
  1450. shost_printk(KERN_INFO, shost,
  1451. "%s: Sending BRST chan: %d\n",
  1452. current->comm, channel));
  1453. rtn = scsi_try_bus_reset(chan_scmd);
  1454. if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
  1455. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1456. if (channel == scmd_channel(scmd)) {
  1457. if (rtn == FAST_IO_FAIL)
  1458. scsi_eh_finish_cmd(scmd,
  1459. done_q);
  1460. else
  1461. list_move_tail(&scmd->eh_entry,
  1462. &check_list);
  1463. }
  1464. }
  1465. } else {
  1466. SCSI_LOG_ERROR_RECOVERY(3,
  1467. shost_printk(KERN_INFO, shost,
  1468. "%s: BRST failed chan: %d\n",
  1469. current->comm, channel));
  1470. }
  1471. }
  1472. return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
  1473. }
  1474. /**
  1475. * scsi_eh_host_reset - send a host reset
  1476. * @shost: host to be reset.
  1477. * @work_q: &list_head for pending commands.
  1478. * @done_q: &list_head for processed commands.
  1479. */
  1480. static int scsi_eh_host_reset(struct Scsi_Host *shost,
  1481. struct list_head *work_q,
  1482. struct list_head *done_q)
  1483. {
  1484. struct scsi_cmnd *scmd, *next;
  1485. LIST_HEAD(check_list);
  1486. int rtn;
  1487. if (!list_empty(work_q)) {
  1488. scmd = list_entry(work_q->next,
  1489. struct scsi_cmnd, eh_entry);
  1490. SCSI_LOG_ERROR_RECOVERY(3,
  1491. shost_printk(KERN_INFO, shost,
  1492. "%s: Sending HRST\n",
  1493. current->comm));
  1494. rtn = scsi_try_host_reset(scmd);
  1495. if (rtn == SUCCESS) {
  1496. list_splice_init(work_q, &check_list);
  1497. } else if (rtn == FAST_IO_FAIL) {
  1498. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1499. scsi_eh_finish_cmd(scmd, done_q);
  1500. }
  1501. } else {
  1502. SCSI_LOG_ERROR_RECOVERY(3,
  1503. shost_printk(KERN_INFO, shost,
  1504. "%s: HRST failed\n",
  1505. current->comm));
  1506. }
  1507. }
  1508. return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
  1509. }
  1510. /**
  1511. * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
  1512. * @work_q: &list_head for pending commands.
  1513. * @done_q: &list_head for processed commands.
  1514. */
  1515. static void scsi_eh_offline_sdevs(struct list_head *work_q,
  1516. struct list_head *done_q)
  1517. {
  1518. struct scsi_cmnd *scmd, *next;
  1519. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1520. sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
  1521. "not ready after error recovery\n");
  1522. scsi_device_set_state(scmd->device, SDEV_OFFLINE);
  1523. if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
  1524. /*
  1525. * FIXME: Handle lost cmds.
  1526. */
  1527. }
  1528. scsi_eh_finish_cmd(scmd, done_q);
  1529. }
  1530. return;
  1531. }
  1532. /**
  1533. * scsi_noretry_cmd - determine if command should be failed fast
  1534. * @scmd: SCSI cmd to examine.
  1535. */
  1536. int scsi_noretry_cmd(struct scsi_cmnd *scmd)
  1537. {
  1538. switch (host_byte(scmd->result)) {
  1539. case DID_OK:
  1540. break;
  1541. case DID_TIME_OUT:
  1542. goto check_type;
  1543. case DID_BUS_BUSY:
  1544. return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
  1545. case DID_PARITY:
  1546. return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
  1547. case DID_ERROR:
  1548. if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
  1549. status_byte(scmd->result) == RESERVATION_CONFLICT)
  1550. return 0;
  1551. /* fall through */
  1552. case DID_SOFT_ERROR:
  1553. return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
  1554. }
  1555. if (status_byte(scmd->result) != CHECK_CONDITION)
  1556. return 0;
  1557. check_type:
  1558. /*
  1559. * assume caller has checked sense and determined
  1560. * the check condition was retryable.
  1561. */
  1562. if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
  1563. scmd->request->cmd_type == REQ_TYPE_BLOCK_PC)
  1564. return 1;
  1565. else
  1566. return 0;
  1567. }
  1568. /**
  1569. * scsi_decide_disposition - Disposition a cmd on return from LLD.
  1570. * @scmd: SCSI cmd to examine.
  1571. *
  1572. * Notes:
  1573. * This is *only* called when we are examining the status after sending
  1574. * out the actual data command. any commands that are queued for error
  1575. * recovery (e.g. test_unit_ready) do *not* come through here.
  1576. *
  1577. * When this routine returns failed, it means the error handler thread
  1578. * is woken. In cases where the error code indicates an error that
  1579. * doesn't require the error handler read (i.e. we don't need to
  1580. * abort/reset), this function should return SUCCESS.
  1581. */
  1582. int scsi_decide_disposition(struct scsi_cmnd *scmd)
  1583. {
  1584. int rtn;
  1585. /*
  1586. * if the device is offline, then we clearly just pass the result back
  1587. * up to the top level.
  1588. */
  1589. if (!scsi_device_online(scmd->device)) {
  1590. SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
  1591. "%s: device offline - report as SUCCESS\n", __func__));
  1592. return SUCCESS;
  1593. }
  1594. /*
  1595. * first check the host byte, to see if there is anything in there
  1596. * that would indicate what we need to do.
  1597. */
  1598. switch (host_byte(scmd->result)) {
  1599. case DID_PASSTHROUGH:
  1600. /*
  1601. * no matter what, pass this through to the upper layer.
  1602. * nuke this special code so that it looks like we are saying
  1603. * did_ok.
  1604. */
  1605. scmd->result &= 0xff00ffff;
  1606. return SUCCESS;
  1607. case DID_OK:
  1608. /*
  1609. * looks good. drop through, and check the next byte.
  1610. */
  1611. break;
  1612. case DID_ABORT:
  1613. if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
  1614. set_host_byte(scmd, DID_TIME_OUT);
  1615. return SUCCESS;
  1616. }
  1617. case DID_NO_CONNECT:
  1618. case DID_BAD_TARGET:
  1619. /*
  1620. * note - this means that we just report the status back
  1621. * to the top level driver, not that we actually think
  1622. * that it indicates SUCCESS.
  1623. */
  1624. return SUCCESS;
  1625. /*
  1626. * when the low level driver returns did_soft_error,
  1627. * it is responsible for keeping an internal retry counter
  1628. * in order to avoid endless loops (db)
  1629. *
  1630. * actually this is a bug in this function here. we should
  1631. * be mindful of the maximum number of retries specified
  1632. * and not get stuck in a loop.
  1633. */
  1634. case DID_SOFT_ERROR:
  1635. goto maybe_retry;
  1636. case DID_IMM_RETRY:
  1637. return NEEDS_RETRY;
  1638. case DID_REQUEUE:
  1639. return ADD_TO_MLQUEUE;
  1640. case DID_TRANSPORT_DISRUPTED:
  1641. /*
  1642. * LLD/transport was disrupted during processing of the IO.
  1643. * The transport class is now blocked/blocking,
  1644. * and the transport will decide what to do with the IO
  1645. * based on its timers and recovery capablilities if
  1646. * there are enough retries.
  1647. */
  1648. goto maybe_retry;
  1649. case DID_TRANSPORT_FAILFAST:
  1650. /*
  1651. * The transport decided to failfast the IO (most likely
  1652. * the fast io fail tmo fired), so send IO directly upwards.
  1653. */
  1654. return SUCCESS;
  1655. case DID_ERROR:
  1656. if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
  1657. status_byte(scmd->result) == RESERVATION_CONFLICT)
  1658. /*
  1659. * execute reservation conflict processing code
  1660. * lower down
  1661. */
  1662. break;
  1663. /* fallthrough */
  1664. case DID_BUS_BUSY:
  1665. case DID_PARITY:
  1666. goto maybe_retry;
  1667. case DID_TIME_OUT:
  1668. /*
  1669. * when we scan the bus, we get timeout messages for
  1670. * these commands if there is no device available.
  1671. * other hosts report did_no_connect for the same thing.
  1672. */
  1673. if ((scmd->cmnd[0] == TEST_UNIT_READY ||
  1674. scmd->cmnd[0] == INQUIRY)) {
  1675. return SUCCESS;
  1676. } else {
  1677. return FAILED;
  1678. }
  1679. case DID_RESET:
  1680. return SUCCESS;
  1681. default:
  1682. return FAILED;
  1683. }
  1684. /*
  1685. * next, check the message byte.
  1686. */
  1687. if (msg_byte(scmd->result) != COMMAND_COMPLETE)
  1688. return FAILED;
  1689. /*
  1690. * check the status byte to see if this indicates anything special.
  1691. */
  1692. switch (status_byte(scmd->result)) {
  1693. case QUEUE_FULL:
  1694. scsi_handle_queue_full(scmd->device);
  1695. /*
  1696. * the case of trying to send too many commands to a
  1697. * tagged queueing device.
  1698. */
  1699. case BUSY:
  1700. /*
  1701. * device can't talk to us at the moment. Should only
  1702. * occur (SAM-3) when the task queue is empty, so will cause
  1703. * the empty queue handling to trigger a stall in the
  1704. * device.
  1705. */
  1706. return ADD_TO_MLQUEUE;
  1707. case GOOD:
  1708. if (scmd->cmnd[0] == REPORT_LUNS)
  1709. scmd->device->sdev_target->expecting_lun_change = 0;
  1710. scsi_handle_queue_ramp_up(scmd->device);
  1711. case COMMAND_TERMINATED:
  1712. return SUCCESS;
  1713. case TASK_ABORTED:
  1714. goto maybe_retry;
  1715. case CHECK_CONDITION:
  1716. rtn = scsi_check_sense(scmd);
  1717. if (rtn == NEEDS_RETRY)
  1718. goto maybe_retry;
  1719. /* if rtn == FAILED, we have no sense information;
  1720. * returning FAILED will wake the error handler thread
  1721. * to collect the sense and redo the decide
  1722. * disposition */
  1723. return rtn;
  1724. case CONDITION_GOOD:
  1725. case INTERMEDIATE_GOOD:
  1726. case INTERMEDIATE_C_GOOD:
  1727. case ACA_ACTIVE:
  1728. /*
  1729. * who knows? FIXME(eric)
  1730. */
  1731. return SUCCESS;
  1732. case RESERVATION_CONFLICT:
  1733. sdev_printk(KERN_INFO, scmd->device,
  1734. "reservation conflict\n");
  1735. set_host_byte(scmd, DID_NEXUS_FAILURE);
  1736. return SUCCESS; /* causes immediate i/o error */
  1737. default:
  1738. return FAILED;
  1739. }
  1740. return FAILED;
  1741. maybe_retry:
  1742. /* we requeue for retry because the error was retryable, and
  1743. * the request was not marked fast fail. Note that above,
  1744. * even if the request is marked fast fail, we still requeue
  1745. * for queue congestion conditions (QUEUE_FULL or BUSY) */
  1746. if ((++scmd->retries) <= scmd->allowed
  1747. && !scsi_noretry_cmd(scmd)) {
  1748. return NEEDS_RETRY;
  1749. } else {
  1750. /*
  1751. * no more retries - report this one back to upper level.
  1752. */
  1753. return SUCCESS;
  1754. }
  1755. }
  1756. static void eh_lock_door_done(struct request *req, int uptodate)
  1757. {
  1758. __blk_put_request(req->q, req);
  1759. }
  1760. /**
  1761. * scsi_eh_lock_door - Prevent medium removal for the specified device
  1762. * @sdev: SCSI device to prevent medium removal
  1763. *
  1764. * Locking:
  1765. * We must be called from process context.
  1766. *
  1767. * Notes:
  1768. * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
  1769. * head of the devices request queue, and continue.
  1770. */
  1771. static void scsi_eh_lock_door(struct scsi_device *sdev)
  1772. {
  1773. struct request *req;
  1774. /*
  1775. * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a
  1776. * request becomes available
  1777. */
  1778. req = blk_get_request(sdev->request_queue, READ, GFP_KERNEL);
  1779. if (IS_ERR(req))
  1780. return;
  1781. blk_rq_set_block_pc(req);
  1782. req->cmd[0] = ALLOW_MEDIUM_REMOVAL;
  1783. req->cmd[1] = 0;
  1784. req->cmd[2] = 0;
  1785. req->cmd[3] = 0;
  1786. req->cmd[4] = SCSI_REMOVAL_PREVENT;
  1787. req->cmd[5] = 0;
  1788. req->cmd_len = COMMAND_SIZE(req->cmd[0]);
  1789. req->cmd_flags |= REQ_QUIET;
  1790. req->timeout = 10 * HZ;
  1791. req->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 req;
  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. scmd = scsi_get_command(dev, GFP_KERNEL);
  2113. if (!scmd)
  2114. goto out_put_autopm_host;
  2115. blk_rq_init(NULL, &req);
  2116. scmd->request = &req;
  2117. scmd->cmnd = req.cmd;
  2118. scmd->scsi_done = scsi_reset_provider_done_command;
  2119. memset(&scmd->sdb, 0, sizeof(scmd->sdb));
  2120. scmd->cmd_len = 0;
  2121. scmd->sc_data_direction = DMA_BIDIRECTIONAL;
  2122. spin_lock_irqsave(shost->host_lock, flags);
  2123. shost->tmf_in_progress = 1;
  2124. spin_unlock_irqrestore(shost->host_lock, flags);
  2125. switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
  2126. case SG_SCSI_RESET_NOTHING:
  2127. rtn = SUCCESS;
  2128. break;
  2129. case SG_SCSI_RESET_DEVICE:
  2130. rtn = scsi_try_bus_device_reset(scmd);
  2131. if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
  2132. break;
  2133. /* FALLTHROUGH */
  2134. case SG_SCSI_RESET_TARGET:
  2135. rtn = scsi_try_target_reset(scmd);
  2136. if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
  2137. break;
  2138. /* FALLTHROUGH */
  2139. case SG_SCSI_RESET_BUS:
  2140. rtn = scsi_try_bus_reset(scmd);
  2141. if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
  2142. break;
  2143. /* FALLTHROUGH */
  2144. case SG_SCSI_RESET_HOST:
  2145. rtn = scsi_try_host_reset(scmd);
  2146. if (rtn == SUCCESS)
  2147. break;
  2148. default:
  2149. /* FALLTHROUGH */
  2150. rtn = FAILED;
  2151. break;
  2152. }
  2153. error = (rtn == SUCCESS) ? 0 : -EIO;
  2154. spin_lock_irqsave(shost->host_lock, flags);
  2155. shost->tmf_in_progress = 0;
  2156. spin_unlock_irqrestore(shost->host_lock, flags);
  2157. /*
  2158. * be sure to wake up anyone who was sleeping or had their queue
  2159. * suspended while we performed the TMF.
  2160. */
  2161. SCSI_LOG_ERROR_RECOVERY(3,
  2162. shost_printk(KERN_INFO, shost,
  2163. "waking up host to restart after TMF\n"));
  2164. wake_up(&shost->host_wait);
  2165. scsi_run_host_queues(shost);
  2166. scsi_put_command(scmd);
  2167. out_put_autopm_host:
  2168. scsi_autopm_put_host(shost);
  2169. return error;
  2170. }
  2171. EXPORT_SYMBOL(scsi_ioctl_reset);
  2172. bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
  2173. struct scsi_sense_hdr *sshdr)
  2174. {
  2175. return scsi_normalize_sense(cmd->sense_buffer,
  2176. SCSI_SENSE_BUFFERSIZE, sshdr);
  2177. }
  2178. EXPORT_SYMBOL(scsi_command_normalize_sense);
  2179. /**
  2180. * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
  2181. * @sense_buffer: byte array of sense data
  2182. * @sb_len: number of valid bytes in sense_buffer
  2183. * @info_out: pointer to 64 integer where 8 or 4 byte information
  2184. * field will be placed if found.
  2185. *
  2186. * Return value:
  2187. * 1 if information field found, 0 if not found.
  2188. */
  2189. int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
  2190. u64 * info_out)
  2191. {
  2192. int j;
  2193. const u8 * ucp;
  2194. u64 ull;
  2195. if (sb_len < 7)
  2196. return 0;
  2197. switch (sense_buffer[0] & 0x7f) {
  2198. case 0x70:
  2199. case 0x71:
  2200. if (sense_buffer[0] & 0x80) {
  2201. *info_out = (sense_buffer[3] << 24) +
  2202. (sense_buffer[4] << 16) +
  2203. (sense_buffer[5] << 8) + sense_buffer[6];
  2204. return 1;
  2205. } else
  2206. return 0;
  2207. case 0x72:
  2208. case 0x73:
  2209. ucp = scsi_sense_desc_find(sense_buffer, sb_len,
  2210. 0 /* info desc */);
  2211. if (ucp && (0xa == ucp[1])) {
  2212. ull = 0;
  2213. for (j = 0; j < 8; ++j) {
  2214. if (j > 0)
  2215. ull <<= 8;
  2216. ull |= ucp[4 + j];
  2217. }
  2218. *info_out = ull;
  2219. return 1;
  2220. } else
  2221. return 0;
  2222. default:
  2223. return 0;
  2224. }
  2225. }
  2226. EXPORT_SYMBOL(scsi_get_sense_info_fld);