scsi_error.c 55 KB

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