scsi_error.c 70 KB

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