scsi_error.c 59 KB

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