scsi_error.c 50 KB

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