scsi_error.c 60 KB

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