scsi_error.c 54 KB

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