scsi_error.c 50 KB

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