scsi_error.c 52 KB

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