scsi_error.c 50 KB

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