scsi_error.c 59 KB

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