scsi_error.c 54 KB

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