scsi_error.c 52 KB

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