scsi_error.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039
  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. ses->prot_op = scmd->prot_op;
  586. scmd->prot_op = SCSI_PROT_NORMAL;
  587. scmd->cmnd = ses->eh_cmnd;
  588. memset(scmd->cmnd, 0, BLK_MAX_CDB);
  589. memset(&scmd->sdb, 0, sizeof(scmd->sdb));
  590. scmd->request->next_rq = NULL;
  591. if (sense_bytes) {
  592. scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
  593. sense_bytes);
  594. sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
  595. scmd->sdb.length);
  596. scmd->sdb.table.sgl = &ses->sense_sgl;
  597. scmd->sc_data_direction = DMA_FROM_DEVICE;
  598. scmd->sdb.table.nents = 1;
  599. scmd->cmnd[0] = REQUEST_SENSE;
  600. scmd->cmnd[4] = scmd->sdb.length;
  601. scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
  602. } else {
  603. scmd->sc_data_direction = DMA_NONE;
  604. if (cmnd) {
  605. BUG_ON(cmnd_size > BLK_MAX_CDB);
  606. memcpy(scmd->cmnd, cmnd, cmnd_size);
  607. scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
  608. }
  609. }
  610. scmd->underflow = 0;
  611. if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
  612. scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
  613. (sdev->lun << 5 & 0xe0);
  614. /*
  615. * Zero the sense buffer. The scsi spec mandates that any
  616. * untransferred sense data should be interpreted as being zero.
  617. */
  618. memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  619. }
  620. EXPORT_SYMBOL(scsi_eh_prep_cmnd);
  621. /**
  622. * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recory
  623. * @scmd: SCSI command structure to restore
  624. * @ses: saved information from a coresponding call to scsi_prep_eh_cmnd
  625. *
  626. * Undo any damage done by above scsi_prep_eh_cmnd().
  627. */
  628. void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
  629. {
  630. /*
  631. * Restore original data
  632. */
  633. scmd->cmd_len = ses->cmd_len;
  634. scmd->cmnd = ses->cmnd;
  635. scmd->sc_data_direction = ses->data_direction;
  636. scmd->sdb = ses->sdb;
  637. scmd->request->next_rq = ses->next_rq;
  638. scmd->result = ses->result;
  639. scmd->prot_op = ses->prot_op;
  640. }
  641. EXPORT_SYMBOL(scsi_eh_restore_cmnd);
  642. /**
  643. * scsi_send_eh_cmnd - submit a scsi command as part of error recory
  644. * @scmd: SCSI command structure to hijack
  645. * @cmnd: CDB to send
  646. * @cmnd_size: size in bytes of @cmnd
  647. * @timeout: timeout for this request
  648. * @sense_bytes: size of sense data to copy or 0
  649. *
  650. * This function is used to send a scsi command down to a target device
  651. * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
  652. *
  653. * Return value:
  654. * SUCCESS or FAILED or NEEDS_RETRY
  655. */
  656. static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
  657. int cmnd_size, int timeout, unsigned sense_bytes)
  658. {
  659. struct scsi_device *sdev = scmd->device;
  660. struct Scsi_Host *shost = sdev->host;
  661. DECLARE_COMPLETION_ONSTACK(done);
  662. unsigned long timeleft;
  663. unsigned long flags;
  664. struct scsi_eh_save ses;
  665. int rtn;
  666. scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
  667. shost->eh_action = &done;
  668. spin_lock_irqsave(shost->host_lock, flags);
  669. scsi_log_send(scmd);
  670. shost->hostt->queuecommand(scmd, scsi_eh_done);
  671. spin_unlock_irqrestore(shost->host_lock, flags);
  672. timeleft = wait_for_completion_timeout(&done, timeout);
  673. shost->eh_action = NULL;
  674. scsi_log_completion(scmd, SUCCESS);
  675. SCSI_LOG_ERROR_RECOVERY(3,
  676. printk("%s: scmd: %p, timeleft: %ld\n",
  677. __FUNCTION__, scmd, timeleft));
  678. /*
  679. * If there is time left scsi_eh_done got called, and we will
  680. * examine the actual status codes to see whether the command
  681. * actually did complete normally, else tell the host to forget
  682. * about this command.
  683. */
  684. if (timeleft) {
  685. rtn = scsi_eh_completed_normally(scmd);
  686. SCSI_LOG_ERROR_RECOVERY(3,
  687. printk("%s: scsi_eh_completed_normally %x\n",
  688. __FUNCTION__, rtn));
  689. switch (rtn) {
  690. case SUCCESS:
  691. case NEEDS_RETRY:
  692. case FAILED:
  693. break;
  694. default:
  695. rtn = FAILED;
  696. break;
  697. }
  698. } else {
  699. scsi_abort_eh_cmnd(scmd);
  700. rtn = FAILED;
  701. }
  702. scsi_eh_restore_cmnd(scmd, &ses);
  703. return rtn;
  704. }
  705. /**
  706. * scsi_request_sense - Request sense data from a particular target.
  707. * @scmd: SCSI cmd for request sense.
  708. *
  709. * Notes:
  710. * Some hosts automatically obtain this information, others require
  711. * that we obtain it on our own. This function will *not* return until
  712. * the command either times out, or it completes.
  713. */
  714. static int scsi_request_sense(struct scsi_cmnd *scmd)
  715. {
  716. return scsi_send_eh_cmnd(scmd, NULL, 0, SENSE_TIMEOUT, ~0);
  717. }
  718. /**
  719. * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
  720. * @scmd: Original SCSI cmd that eh has finished.
  721. * @done_q: Queue for processed commands.
  722. *
  723. * Notes:
  724. * We don't want to use the normal command completion while we are are
  725. * still handling errors - it may cause other commands to be queued,
  726. * and that would disturb what we are doing. Thus we really want to
  727. * keep a list of pending commands for final completion, and once we
  728. * are ready to leave error handling we handle completion for real.
  729. */
  730. void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
  731. {
  732. scmd->device->host->host_failed--;
  733. scmd->eh_eflags = 0;
  734. list_move_tail(&scmd->eh_entry, done_q);
  735. }
  736. EXPORT_SYMBOL(scsi_eh_finish_cmd);
  737. /**
  738. * scsi_eh_get_sense - Get device sense data.
  739. * @work_q: Queue of commands to process.
  740. * @done_q: Queue of processed commands.
  741. *
  742. * Description:
  743. * See if we need to request sense information. if so, then get it
  744. * now, so we have a better idea of what to do.
  745. *
  746. * Notes:
  747. * This has the unfortunate side effect that if a shost adapter does
  748. * not automatically request sense information, we end up shutting
  749. * it down before we request it.
  750. *
  751. * All drivers should request sense information internally these days,
  752. * so for now all I have to say is tough noogies if you end up in here.
  753. *
  754. * XXX: Long term this code should go away, but that needs an audit of
  755. * all LLDDs first.
  756. */
  757. int scsi_eh_get_sense(struct list_head *work_q,
  758. struct list_head *done_q)
  759. {
  760. struct scsi_cmnd *scmd, *next;
  761. int rtn;
  762. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  763. if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
  764. SCSI_SENSE_VALID(scmd))
  765. continue;
  766. SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
  767. "%s: requesting sense\n",
  768. current->comm));
  769. rtn = scsi_request_sense(scmd);
  770. if (rtn != SUCCESS)
  771. continue;
  772. SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
  773. " result %x\n", scmd,
  774. scmd->result));
  775. SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
  776. rtn = scsi_decide_disposition(scmd);
  777. /*
  778. * if the result was normal, then just pass it along to the
  779. * upper level.
  780. */
  781. if (rtn == SUCCESS)
  782. /* we don't want this command reissued, just
  783. * finished with the sense data, so set
  784. * retries to the max allowed to ensure it
  785. * won't get reissued */
  786. scmd->retries = scmd->allowed;
  787. else if (rtn != NEEDS_RETRY)
  788. continue;
  789. scsi_eh_finish_cmd(scmd, done_q);
  790. }
  791. return list_empty(work_q);
  792. }
  793. EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
  794. /**
  795. * scsi_eh_tur - Send TUR to device.
  796. * @scmd: &scsi_cmnd to send TUR
  797. *
  798. * Return value:
  799. * 0 - Device is ready. 1 - Device NOT ready.
  800. */
  801. static int scsi_eh_tur(struct scsi_cmnd *scmd)
  802. {
  803. static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
  804. int retry_cnt = 1, rtn;
  805. retry_tur:
  806. rtn = scsi_send_eh_cmnd(scmd, tur_command, 6, SENSE_TIMEOUT, 0);
  807. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
  808. __FUNCTION__, scmd, rtn));
  809. switch (rtn) {
  810. case NEEDS_RETRY:
  811. if (retry_cnt--)
  812. goto retry_tur;
  813. /*FALLTHRU*/
  814. case SUCCESS:
  815. return 0;
  816. default:
  817. return 1;
  818. }
  819. }
  820. /**
  821. * scsi_eh_abort_cmds - abort pending commands.
  822. * @work_q: &list_head for pending commands.
  823. * @done_q: &list_head for processed commands.
  824. *
  825. * Decription:
  826. * Try and see whether or not it makes sense to try and abort the
  827. * running command. This only works out to be the case if we have one
  828. * command that has timed out. If the command simply failed, it makes
  829. * no sense to try and abort the command, since as far as the shost
  830. * adapter is concerned, it isn't running.
  831. */
  832. static int scsi_eh_abort_cmds(struct list_head *work_q,
  833. struct list_head *done_q)
  834. {
  835. struct scsi_cmnd *scmd, *next;
  836. int rtn;
  837. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  838. if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
  839. continue;
  840. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
  841. "0x%p\n", current->comm,
  842. scmd));
  843. rtn = scsi_try_to_abort_cmd(scmd);
  844. if (rtn == SUCCESS) {
  845. scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
  846. if (!scsi_device_online(scmd->device) ||
  847. !scsi_eh_tur(scmd)) {
  848. scsi_eh_finish_cmd(scmd, done_q);
  849. }
  850. } else
  851. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
  852. " cmd failed:"
  853. "0x%p\n",
  854. current->comm,
  855. scmd));
  856. }
  857. return list_empty(work_q);
  858. }
  859. /**
  860. * scsi_eh_try_stu - Send START_UNIT to device.
  861. * @scmd: &scsi_cmnd to send START_UNIT
  862. *
  863. * Return value:
  864. * 0 - Device is ready. 1 - Device NOT ready.
  865. */
  866. static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
  867. {
  868. static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
  869. if (scmd->device->allow_restart) {
  870. int i, rtn = NEEDS_RETRY;
  871. for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
  872. rtn = scsi_send_eh_cmnd(scmd, stu_command, 6,
  873. scmd->device->timeout, 0);
  874. if (rtn == SUCCESS)
  875. return 0;
  876. }
  877. return 1;
  878. }
  879. /**
  880. * scsi_eh_stu - send START_UNIT if needed
  881. * @shost: &scsi host being recovered.
  882. * @work_q: &list_head for pending commands.
  883. * @done_q: &list_head for processed commands.
  884. *
  885. * Notes:
  886. * If commands are failing due to not ready, initializing command required,
  887. * try revalidating the device, which will end up sending a start unit.
  888. */
  889. static int scsi_eh_stu(struct Scsi_Host *shost,
  890. struct list_head *work_q,
  891. struct list_head *done_q)
  892. {
  893. struct scsi_cmnd *scmd, *stu_scmd, *next;
  894. struct scsi_device *sdev;
  895. shost_for_each_device(sdev, shost) {
  896. stu_scmd = NULL;
  897. list_for_each_entry(scmd, work_q, eh_entry)
  898. if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
  899. scsi_check_sense(scmd) == FAILED ) {
  900. stu_scmd = scmd;
  901. break;
  902. }
  903. if (!stu_scmd)
  904. continue;
  905. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
  906. " 0x%p\n", current->comm, sdev));
  907. if (!scsi_eh_try_stu(stu_scmd)) {
  908. if (!scsi_device_online(sdev) ||
  909. !scsi_eh_tur(stu_scmd)) {
  910. list_for_each_entry_safe(scmd, next,
  911. work_q, eh_entry) {
  912. if (scmd->device == sdev)
  913. scsi_eh_finish_cmd(scmd, done_q);
  914. }
  915. }
  916. } else {
  917. SCSI_LOG_ERROR_RECOVERY(3,
  918. printk("%s: START_UNIT failed to sdev:"
  919. " 0x%p\n", current->comm, sdev));
  920. }
  921. }
  922. return list_empty(work_q);
  923. }
  924. /**
  925. * scsi_eh_bus_device_reset - send bdr if needed
  926. * @shost: scsi host being recovered.
  927. * @work_q: &list_head for pending commands.
  928. * @done_q: &list_head for processed commands.
  929. *
  930. * Notes:
  931. * Try a bus device reset. Still, look to see whether we have multiple
  932. * devices that are jammed or not - if we have multiple devices, it
  933. * makes no sense to try bus_device_reset - we really would need to try
  934. * a bus_reset instead.
  935. */
  936. static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
  937. struct list_head *work_q,
  938. struct list_head *done_q)
  939. {
  940. struct scsi_cmnd *scmd, *bdr_scmd, *next;
  941. struct scsi_device *sdev;
  942. int rtn;
  943. shost_for_each_device(sdev, shost) {
  944. bdr_scmd = NULL;
  945. list_for_each_entry(scmd, work_q, eh_entry)
  946. if (scmd->device == sdev) {
  947. bdr_scmd = scmd;
  948. break;
  949. }
  950. if (!bdr_scmd)
  951. continue;
  952. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
  953. " 0x%p\n", current->comm,
  954. sdev));
  955. rtn = scsi_try_bus_device_reset(bdr_scmd);
  956. if (rtn == SUCCESS) {
  957. if (!scsi_device_online(sdev) ||
  958. !scsi_eh_tur(bdr_scmd)) {
  959. list_for_each_entry_safe(scmd, next,
  960. work_q, eh_entry) {
  961. if (scmd->device == sdev)
  962. scsi_eh_finish_cmd(scmd,
  963. done_q);
  964. }
  965. }
  966. } else {
  967. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
  968. " failed sdev:"
  969. "0x%p\n",
  970. current->comm,
  971. sdev));
  972. }
  973. }
  974. return list_empty(work_q);
  975. }
  976. /**
  977. * scsi_eh_target_reset - send target reset if needed
  978. * @shost: scsi host being recovered.
  979. * @work_q: &list_head for pending commands.
  980. * @done_q: &list_head for processed commands.
  981. *
  982. * Notes:
  983. * Try a target reset.
  984. */
  985. static int scsi_eh_target_reset(struct Scsi_Host *shost,
  986. struct list_head *work_q,
  987. struct list_head *done_q)
  988. {
  989. struct scsi_cmnd *scmd, *tgtr_scmd, *next;
  990. unsigned int id;
  991. int rtn;
  992. for (id = 0; id <= shost->max_id; id++) {
  993. tgtr_scmd = NULL;
  994. list_for_each_entry(scmd, work_q, eh_entry) {
  995. if (id == scmd_id(scmd)) {
  996. tgtr_scmd = scmd;
  997. break;
  998. }
  999. }
  1000. if (!tgtr_scmd)
  1001. continue;
  1002. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending target reset "
  1003. "to target %d\n",
  1004. current->comm, id));
  1005. rtn = scsi_try_target_reset(tgtr_scmd);
  1006. if (rtn == SUCCESS) {
  1007. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1008. if (id == scmd_id(scmd))
  1009. if (!scsi_device_online(scmd->device) ||
  1010. !scsi_eh_tur(tgtr_scmd))
  1011. scsi_eh_finish_cmd(scmd,
  1012. done_q);
  1013. }
  1014. } else
  1015. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Target reset"
  1016. " failed target: "
  1017. "%d\n",
  1018. current->comm, id));
  1019. }
  1020. return list_empty(work_q);
  1021. }
  1022. /**
  1023. * scsi_eh_bus_reset - send a bus reset
  1024. * @shost: &scsi host being recovered.
  1025. * @work_q: &list_head for pending commands.
  1026. * @done_q: &list_head for processed commands.
  1027. */
  1028. static int scsi_eh_bus_reset(struct Scsi_Host *shost,
  1029. struct list_head *work_q,
  1030. struct list_head *done_q)
  1031. {
  1032. struct scsi_cmnd *scmd, *chan_scmd, *next;
  1033. unsigned int channel;
  1034. int rtn;
  1035. /*
  1036. * we really want to loop over the various channels, and do this on
  1037. * a channel by channel basis. we should also check to see if any
  1038. * of the failed commands are on soft_reset devices, and if so, skip
  1039. * the reset.
  1040. */
  1041. for (channel = 0; channel <= shost->max_channel; channel++) {
  1042. chan_scmd = NULL;
  1043. list_for_each_entry(scmd, work_q, eh_entry) {
  1044. if (channel == scmd_channel(scmd)) {
  1045. chan_scmd = scmd;
  1046. break;
  1047. /*
  1048. * FIXME add back in some support for
  1049. * soft_reset devices.
  1050. */
  1051. }
  1052. }
  1053. if (!chan_scmd)
  1054. continue;
  1055. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
  1056. " %d\n", current->comm,
  1057. channel));
  1058. rtn = scsi_try_bus_reset(chan_scmd);
  1059. if (rtn == SUCCESS) {
  1060. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1061. if (channel == scmd_channel(scmd))
  1062. if (!scsi_device_online(scmd->device) ||
  1063. !scsi_eh_tur(scmd))
  1064. scsi_eh_finish_cmd(scmd,
  1065. done_q);
  1066. }
  1067. } else {
  1068. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
  1069. " failed chan: %d\n",
  1070. current->comm,
  1071. channel));
  1072. }
  1073. }
  1074. return list_empty(work_q);
  1075. }
  1076. /**
  1077. * scsi_eh_host_reset - send a host reset
  1078. * @work_q: list_head for processed commands.
  1079. * @done_q: list_head for processed commands.
  1080. */
  1081. static int scsi_eh_host_reset(struct list_head *work_q,
  1082. struct list_head *done_q)
  1083. {
  1084. struct scsi_cmnd *scmd, *next;
  1085. int rtn;
  1086. if (!list_empty(work_q)) {
  1087. scmd = list_entry(work_q->next,
  1088. struct scsi_cmnd, eh_entry);
  1089. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
  1090. , current->comm));
  1091. rtn = scsi_try_host_reset(scmd);
  1092. if (rtn == SUCCESS) {
  1093. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1094. if (!scsi_device_online(scmd->device) ||
  1095. (!scsi_eh_try_stu(scmd) && !scsi_eh_tur(scmd)) ||
  1096. !scsi_eh_tur(scmd))
  1097. scsi_eh_finish_cmd(scmd, done_q);
  1098. }
  1099. } else {
  1100. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
  1101. " failed\n",
  1102. current->comm));
  1103. }
  1104. }
  1105. return list_empty(work_q);
  1106. }
  1107. /**
  1108. * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
  1109. * @work_q: list_head for processed commands.
  1110. * @done_q: list_head for processed commands.
  1111. */
  1112. static void scsi_eh_offline_sdevs(struct list_head *work_q,
  1113. struct list_head *done_q)
  1114. {
  1115. struct scsi_cmnd *scmd, *next;
  1116. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1117. sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
  1118. "not ready after error recovery\n");
  1119. scsi_device_set_state(scmd->device, SDEV_OFFLINE);
  1120. if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
  1121. /*
  1122. * FIXME: Handle lost cmds.
  1123. */
  1124. }
  1125. scsi_eh_finish_cmd(scmd, done_q);
  1126. }
  1127. return;
  1128. }
  1129. /**
  1130. * scsi_decide_disposition - Disposition a cmd on return from LLD.
  1131. * @scmd: SCSI cmd to examine.
  1132. *
  1133. * Notes:
  1134. * This is *only* called when we are examining the status after sending
  1135. * out the actual data command. any commands that are queued for error
  1136. * recovery (e.g. test_unit_ready) do *not* come through here.
  1137. *
  1138. * When this routine returns failed, it means the error handler thread
  1139. * is woken. In cases where the error code indicates an error that
  1140. * doesn't require the error handler read (i.e. we don't need to
  1141. * abort/reset), this function should return SUCCESS.
  1142. */
  1143. int scsi_decide_disposition(struct scsi_cmnd *scmd)
  1144. {
  1145. int rtn;
  1146. /*
  1147. * if the device is offline, then we clearly just pass the result back
  1148. * up to the top level.
  1149. */
  1150. if (!scsi_device_online(scmd->device)) {
  1151. SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
  1152. " as SUCCESS\n",
  1153. __FUNCTION__));
  1154. return SUCCESS;
  1155. }
  1156. /*
  1157. * first check the host byte, to see if there is anything in there
  1158. * that would indicate what we need to do.
  1159. */
  1160. switch (host_byte(scmd->result)) {
  1161. case DID_PASSTHROUGH:
  1162. /*
  1163. * no matter what, pass this through to the upper layer.
  1164. * nuke this special code so that it looks like we are saying
  1165. * did_ok.
  1166. */
  1167. scmd->result &= 0xff00ffff;
  1168. return SUCCESS;
  1169. case DID_OK:
  1170. /*
  1171. * looks good. drop through, and check the next byte.
  1172. */
  1173. break;
  1174. case DID_NO_CONNECT:
  1175. case DID_BAD_TARGET:
  1176. case DID_ABORT:
  1177. /*
  1178. * note - this means that we just report the status back
  1179. * to the top level driver, not that we actually think
  1180. * that it indicates SUCCESS.
  1181. */
  1182. return SUCCESS;
  1183. /*
  1184. * when the low level driver returns did_soft_error,
  1185. * it is responsible for keeping an internal retry counter
  1186. * in order to avoid endless loops (db)
  1187. *
  1188. * actually this is a bug in this function here. we should
  1189. * be mindful of the maximum number of retries specified
  1190. * and not get stuck in a loop.
  1191. */
  1192. case DID_SOFT_ERROR:
  1193. goto maybe_retry;
  1194. case DID_IMM_RETRY:
  1195. return NEEDS_RETRY;
  1196. case DID_REQUEUE:
  1197. return ADD_TO_MLQUEUE;
  1198. case DID_ERROR:
  1199. if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
  1200. status_byte(scmd->result) == RESERVATION_CONFLICT)
  1201. /*
  1202. * execute reservation conflict processing code
  1203. * lower down
  1204. */
  1205. break;
  1206. /* fallthrough */
  1207. case DID_BUS_BUSY:
  1208. case DID_PARITY:
  1209. goto maybe_retry;
  1210. case DID_TIME_OUT:
  1211. /*
  1212. * when we scan the bus, we get timeout messages for
  1213. * these commands if there is no device available.
  1214. * other hosts report did_no_connect for the same thing.
  1215. */
  1216. if ((scmd->cmnd[0] == TEST_UNIT_READY ||
  1217. scmd->cmnd[0] == INQUIRY)) {
  1218. return SUCCESS;
  1219. } else {
  1220. return FAILED;
  1221. }
  1222. case DID_RESET:
  1223. return SUCCESS;
  1224. default:
  1225. return FAILED;
  1226. }
  1227. /*
  1228. * next, check the message byte.
  1229. */
  1230. if (msg_byte(scmd->result) != COMMAND_COMPLETE)
  1231. return FAILED;
  1232. /*
  1233. * check the status byte to see if this indicates anything special.
  1234. */
  1235. switch (status_byte(scmd->result)) {
  1236. case QUEUE_FULL:
  1237. /*
  1238. * the case of trying to send too many commands to a
  1239. * tagged queueing device.
  1240. */
  1241. case BUSY:
  1242. /*
  1243. * device can't talk to us at the moment. Should only
  1244. * occur (SAM-3) when the task queue is empty, so will cause
  1245. * the empty queue handling to trigger a stall in the
  1246. * device.
  1247. */
  1248. return ADD_TO_MLQUEUE;
  1249. case GOOD:
  1250. case COMMAND_TERMINATED:
  1251. case TASK_ABORTED:
  1252. return SUCCESS;
  1253. case CHECK_CONDITION:
  1254. rtn = scsi_check_sense(scmd);
  1255. if (rtn == NEEDS_RETRY)
  1256. goto maybe_retry;
  1257. /* if rtn == FAILED, we have no sense information;
  1258. * returning FAILED will wake the error handler thread
  1259. * to collect the sense and redo the decide
  1260. * disposition */
  1261. return rtn;
  1262. case CONDITION_GOOD:
  1263. case INTERMEDIATE_GOOD:
  1264. case INTERMEDIATE_C_GOOD:
  1265. case ACA_ACTIVE:
  1266. /*
  1267. * who knows? FIXME(eric)
  1268. */
  1269. return SUCCESS;
  1270. case RESERVATION_CONFLICT:
  1271. sdev_printk(KERN_INFO, scmd->device,
  1272. "reservation conflict\n");
  1273. return SUCCESS; /* causes immediate i/o error */
  1274. default:
  1275. return FAILED;
  1276. }
  1277. return FAILED;
  1278. maybe_retry:
  1279. /* we requeue for retry because the error was retryable, and
  1280. * the request was not marked fast fail. Note that above,
  1281. * even if the request is marked fast fail, we still requeue
  1282. * for queue congestion conditions (QUEUE_FULL or BUSY) */
  1283. if ((++scmd->retries) <= scmd->allowed
  1284. && !blk_noretry_request(scmd->request)) {
  1285. return NEEDS_RETRY;
  1286. } else {
  1287. /*
  1288. * no more retries - report this one back to upper level.
  1289. */
  1290. return SUCCESS;
  1291. }
  1292. }
  1293. /**
  1294. * scsi_eh_lock_door - Prevent medium removal for the specified device
  1295. * @sdev: SCSI device to prevent medium removal
  1296. *
  1297. * Locking:
  1298. * We must be called from process context; scsi_allocate_request()
  1299. * may sleep.
  1300. *
  1301. * Notes:
  1302. * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
  1303. * head of the devices request queue, and continue.
  1304. *
  1305. * Bugs:
  1306. * scsi_allocate_request() may sleep waiting for existing requests to
  1307. * be processed. However, since we haven't kicked off any request
  1308. * processing for this host, this may deadlock.
  1309. *
  1310. * If scsi_allocate_request() fails for what ever reason, we
  1311. * completely forget to lock the door.
  1312. */
  1313. static void scsi_eh_lock_door(struct scsi_device *sdev)
  1314. {
  1315. unsigned char cmnd[MAX_COMMAND_SIZE];
  1316. cmnd[0] = ALLOW_MEDIUM_REMOVAL;
  1317. cmnd[1] = 0;
  1318. cmnd[2] = 0;
  1319. cmnd[3] = 0;
  1320. cmnd[4] = SCSI_REMOVAL_PREVENT;
  1321. cmnd[5] = 0;
  1322. scsi_execute_async(sdev, cmnd, 6, DMA_NONE, NULL, 0, 0, 10 * HZ,
  1323. 5, NULL, NULL, GFP_KERNEL);
  1324. }
  1325. /**
  1326. * scsi_restart_operations - restart io operations to the specified host.
  1327. * @shost: Host we are restarting.
  1328. *
  1329. * Notes:
  1330. * When we entered the error handler, we blocked all further i/o to
  1331. * this device. we need to 'reverse' this process.
  1332. */
  1333. static void scsi_restart_operations(struct Scsi_Host *shost)
  1334. {
  1335. struct scsi_device *sdev;
  1336. unsigned long flags;
  1337. /*
  1338. * If the door was locked, we need to insert a door lock request
  1339. * onto the head of the SCSI request queue for the device. There
  1340. * is no point trying to lock the door of an off-line device.
  1341. */
  1342. shost_for_each_device(sdev, shost) {
  1343. if (scsi_device_online(sdev) && sdev->locked)
  1344. scsi_eh_lock_door(sdev);
  1345. }
  1346. /*
  1347. * next free up anything directly waiting upon the host. this
  1348. * will be requests for character device operations, and also for
  1349. * ioctls to queued block devices.
  1350. */
  1351. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: waking up host to restart\n",
  1352. __FUNCTION__));
  1353. spin_lock_irqsave(shost->host_lock, flags);
  1354. if (scsi_host_set_state(shost, SHOST_RUNNING))
  1355. if (scsi_host_set_state(shost, SHOST_CANCEL))
  1356. BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
  1357. spin_unlock_irqrestore(shost->host_lock, flags);
  1358. wake_up(&shost->host_wait);
  1359. /*
  1360. * finally we need to re-initiate requests that may be pending. we will
  1361. * have had everything blocked while error handling is taking place, and
  1362. * now that error recovery is done, we will need to ensure that these
  1363. * requests are started.
  1364. */
  1365. scsi_run_host_queues(shost);
  1366. }
  1367. /**
  1368. * scsi_eh_ready_devs - check device ready state and recover if not.
  1369. * @shost: host to be recovered.
  1370. * @work_q: &list_head for pending commands.
  1371. * @done_q: &list_head for processed commands.
  1372. */
  1373. void scsi_eh_ready_devs(struct Scsi_Host *shost,
  1374. struct list_head *work_q,
  1375. struct list_head *done_q)
  1376. {
  1377. if (!scsi_eh_stu(shost, work_q, done_q))
  1378. if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
  1379. if (!scsi_eh_target_reset(shost, work_q, done_q))
  1380. if (!scsi_eh_bus_reset(shost, work_q, done_q))
  1381. if (!scsi_eh_host_reset(work_q, done_q))
  1382. scsi_eh_offline_sdevs(work_q,
  1383. done_q);
  1384. }
  1385. EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
  1386. /**
  1387. * scsi_eh_flush_done_q - finish processed commands or retry them.
  1388. * @done_q: list_head of processed commands.
  1389. */
  1390. void scsi_eh_flush_done_q(struct list_head *done_q)
  1391. {
  1392. struct scsi_cmnd *scmd, *next;
  1393. list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
  1394. list_del_init(&scmd->eh_entry);
  1395. if (scsi_device_online(scmd->device) &&
  1396. !blk_noretry_request(scmd->request) &&
  1397. (++scmd->retries <= scmd->allowed)) {
  1398. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
  1399. " retry cmd: %p\n",
  1400. current->comm,
  1401. scmd));
  1402. scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
  1403. } else {
  1404. /*
  1405. * If just we got sense for the device (called
  1406. * scsi_eh_get_sense), scmd->result is already
  1407. * set, do not set DRIVER_TIMEOUT.
  1408. */
  1409. if (!scmd->result)
  1410. scmd->result |= (DRIVER_TIMEOUT << 24);
  1411. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
  1412. " cmd: %p\n",
  1413. current->comm, scmd));
  1414. scsi_finish_command(scmd);
  1415. }
  1416. }
  1417. }
  1418. EXPORT_SYMBOL(scsi_eh_flush_done_q);
  1419. /**
  1420. * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
  1421. * @shost: Host to unjam.
  1422. *
  1423. * Notes:
  1424. * When we come in here, we *know* that all commands on the bus have
  1425. * either completed, failed or timed out. we also know that no further
  1426. * commands are being sent to the host, so things are relatively quiet
  1427. * and we have freedom to fiddle with things as we wish.
  1428. *
  1429. * This is only the *default* implementation. it is possible for
  1430. * individual drivers to supply their own version of this function, and
  1431. * if the maintainer wishes to do this, it is strongly suggested that
  1432. * this function be taken as a template and modified. this function
  1433. * was designed to correctly handle problems for about 95% of the
  1434. * different cases out there, and it should always provide at least a
  1435. * reasonable amount of error recovery.
  1436. *
  1437. * Any command marked 'failed' or 'timeout' must eventually have
  1438. * scsi_finish_cmd() called for it. we do all of the retry stuff
  1439. * here, so when we restart the host after we return it should have an
  1440. * empty queue.
  1441. */
  1442. static void scsi_unjam_host(struct Scsi_Host *shost)
  1443. {
  1444. unsigned long flags;
  1445. LIST_HEAD(eh_work_q);
  1446. LIST_HEAD(eh_done_q);
  1447. spin_lock_irqsave(shost->host_lock, flags);
  1448. list_splice_init(&shost->eh_cmd_q, &eh_work_q);
  1449. spin_unlock_irqrestore(shost->host_lock, flags);
  1450. SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
  1451. if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
  1452. if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
  1453. scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
  1454. scsi_eh_flush_done_q(&eh_done_q);
  1455. }
  1456. /**
  1457. * scsi_error_handler - SCSI error handler thread
  1458. * @data: Host for which we are running.
  1459. *
  1460. * Notes:
  1461. * This is the main error handling loop. This is run as a kernel thread
  1462. * for every SCSI host and handles all error handling activity.
  1463. */
  1464. int scsi_error_handler(void *data)
  1465. {
  1466. struct Scsi_Host *shost = data;
  1467. /*
  1468. * We use TASK_INTERRUPTIBLE so that the thread is not
  1469. * counted against the load average as a running process.
  1470. * We never actually get interrupted because kthread_run
  1471. * disables singal delivery for the created thread.
  1472. */
  1473. set_current_state(TASK_INTERRUPTIBLE);
  1474. while (!kthread_should_stop()) {
  1475. if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
  1476. shost->host_failed != shost->host_busy) {
  1477. SCSI_LOG_ERROR_RECOVERY(1,
  1478. printk("Error handler scsi_eh_%d sleeping\n",
  1479. shost->host_no));
  1480. schedule();
  1481. set_current_state(TASK_INTERRUPTIBLE);
  1482. continue;
  1483. }
  1484. __set_current_state(TASK_RUNNING);
  1485. SCSI_LOG_ERROR_RECOVERY(1,
  1486. printk("Error handler scsi_eh_%d waking up\n",
  1487. shost->host_no));
  1488. /*
  1489. * We have a host that is failing for some reason. Figure out
  1490. * what we need to do to get it up and online again (if we can).
  1491. * If we fail, we end up taking the thing offline.
  1492. */
  1493. if (shost->transportt->eh_strategy_handler)
  1494. shost->transportt->eh_strategy_handler(shost);
  1495. else
  1496. scsi_unjam_host(shost);
  1497. /*
  1498. * Note - if the above fails completely, the action is to take
  1499. * individual devices offline and flush the queue of any
  1500. * outstanding requests that may have been pending. When we
  1501. * restart, we restart any I/O to any other devices on the bus
  1502. * which are still online.
  1503. */
  1504. scsi_restart_operations(shost);
  1505. set_current_state(TASK_INTERRUPTIBLE);
  1506. }
  1507. __set_current_state(TASK_RUNNING);
  1508. SCSI_LOG_ERROR_RECOVERY(1,
  1509. printk("Error handler scsi_eh_%d exiting\n", shost->host_no));
  1510. shost->ehandler = NULL;
  1511. return 0;
  1512. }
  1513. /*
  1514. * Function: scsi_report_bus_reset()
  1515. *
  1516. * Purpose: Utility function used by low-level drivers to report that
  1517. * they have observed a bus reset on the bus being handled.
  1518. *
  1519. * Arguments: shost - Host in question
  1520. * channel - channel on which reset was observed.
  1521. *
  1522. * Returns: Nothing
  1523. *
  1524. * Lock status: Host lock must be held.
  1525. *
  1526. * Notes: This only needs to be called if the reset is one which
  1527. * originates from an unknown location. Resets originated
  1528. * by the mid-level itself don't need to call this, but there
  1529. * should be no harm.
  1530. *
  1531. * The main purpose of this is to make sure that a CHECK_CONDITION
  1532. * is properly treated.
  1533. */
  1534. void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
  1535. {
  1536. struct scsi_device *sdev;
  1537. __shost_for_each_device(sdev, shost) {
  1538. if (channel == sdev_channel(sdev))
  1539. __scsi_report_device_reset(sdev, NULL);
  1540. }
  1541. }
  1542. EXPORT_SYMBOL(scsi_report_bus_reset);
  1543. /*
  1544. * Function: scsi_report_device_reset()
  1545. *
  1546. * Purpose: Utility function used by low-level drivers to report that
  1547. * they have observed a device reset on the device being handled.
  1548. *
  1549. * Arguments: shost - Host in question
  1550. * channel - channel on which reset was observed
  1551. * target - target on which reset was observed
  1552. *
  1553. * Returns: Nothing
  1554. *
  1555. * Lock status: Host lock must be held
  1556. *
  1557. * Notes: This only needs to be called if the reset is one which
  1558. * originates from an unknown location. Resets originated
  1559. * by the mid-level itself don't need to call this, but there
  1560. * should be no harm.
  1561. *
  1562. * The main purpose of this is to make sure that a CHECK_CONDITION
  1563. * is properly treated.
  1564. */
  1565. void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
  1566. {
  1567. struct scsi_device *sdev;
  1568. __shost_for_each_device(sdev, shost) {
  1569. if (channel == sdev_channel(sdev) &&
  1570. target == sdev_id(sdev))
  1571. __scsi_report_device_reset(sdev, NULL);
  1572. }
  1573. }
  1574. EXPORT_SYMBOL(scsi_report_device_reset);
  1575. static void
  1576. scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
  1577. {
  1578. }
  1579. /*
  1580. * Function: scsi_reset_provider
  1581. *
  1582. * Purpose: Send requested reset to a bus or device at any phase.
  1583. *
  1584. * Arguments: device - device to send reset to
  1585. * flag - reset type (see scsi.h)
  1586. *
  1587. * Returns: SUCCESS/FAILURE.
  1588. *
  1589. * Notes: This is used by the SCSI Generic driver to provide
  1590. * Bus/Device reset capability.
  1591. */
  1592. int
  1593. scsi_reset_provider(struct scsi_device *dev, int flag)
  1594. {
  1595. struct scsi_cmnd *scmd = scsi_get_command(dev, GFP_KERNEL);
  1596. struct Scsi_Host *shost = dev->host;
  1597. struct request req;
  1598. unsigned long flags;
  1599. int rtn;
  1600. blk_rq_init(NULL, &req);
  1601. scmd->request = &req;
  1602. memset(&scmd->eh_timeout, 0, sizeof(scmd->eh_timeout));
  1603. scmd->cmnd = req.cmd;
  1604. scmd->scsi_done = scsi_reset_provider_done_command;
  1605. memset(&scmd->sdb, 0, sizeof(scmd->sdb));
  1606. scmd->cmd_len = 0;
  1607. scmd->sc_data_direction = DMA_BIDIRECTIONAL;
  1608. init_timer(&scmd->eh_timeout);
  1609. spin_lock_irqsave(shost->host_lock, flags);
  1610. shost->tmf_in_progress = 1;
  1611. spin_unlock_irqrestore(shost->host_lock, flags);
  1612. switch (flag) {
  1613. case SCSI_TRY_RESET_DEVICE:
  1614. rtn = scsi_try_bus_device_reset(scmd);
  1615. if (rtn == SUCCESS)
  1616. break;
  1617. /* FALLTHROUGH */
  1618. case SCSI_TRY_RESET_TARGET:
  1619. rtn = scsi_try_target_reset(scmd);
  1620. if (rtn == SUCCESS)
  1621. break;
  1622. /* FALLTHROUGH */
  1623. case SCSI_TRY_RESET_BUS:
  1624. rtn = scsi_try_bus_reset(scmd);
  1625. if (rtn == SUCCESS)
  1626. break;
  1627. /* FALLTHROUGH */
  1628. case SCSI_TRY_RESET_HOST:
  1629. rtn = scsi_try_host_reset(scmd);
  1630. break;
  1631. default:
  1632. rtn = FAILED;
  1633. }
  1634. spin_lock_irqsave(shost->host_lock, flags);
  1635. shost->tmf_in_progress = 0;
  1636. spin_unlock_irqrestore(shost->host_lock, flags);
  1637. /*
  1638. * be sure to wake up anyone who was sleeping or had their queue
  1639. * suspended while we performed the TMF.
  1640. */
  1641. SCSI_LOG_ERROR_RECOVERY(3,
  1642. printk("%s: waking up host to restart after TMF\n",
  1643. __FUNCTION__));
  1644. wake_up(&shost->host_wait);
  1645. scsi_run_host_queues(shost);
  1646. scsi_next_command(scmd);
  1647. return rtn;
  1648. }
  1649. EXPORT_SYMBOL(scsi_reset_provider);
  1650. /**
  1651. * scsi_normalize_sense - normalize main elements from either fixed or
  1652. * descriptor sense data format into a common format.
  1653. *
  1654. * @sense_buffer: byte array containing sense data returned by device
  1655. * @sb_len: number of valid bytes in sense_buffer
  1656. * @sshdr: pointer to instance of structure that common
  1657. * elements are written to.
  1658. *
  1659. * Notes:
  1660. * The "main elements" from sense data are: response_code, sense_key,
  1661. * asc, ascq and additional_length (only for descriptor format).
  1662. *
  1663. * Typically this function can be called after a device has
  1664. * responded to a SCSI command with the CHECK_CONDITION status.
  1665. *
  1666. * Return value:
  1667. * 1 if valid sense data information found, else 0;
  1668. */
  1669. int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
  1670. struct scsi_sense_hdr *sshdr)
  1671. {
  1672. if (!sense_buffer || !sb_len)
  1673. return 0;
  1674. memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
  1675. sshdr->response_code = (sense_buffer[0] & 0x7f);
  1676. if (!scsi_sense_valid(sshdr))
  1677. return 0;
  1678. if (sshdr->response_code >= 0x72) {
  1679. /*
  1680. * descriptor format
  1681. */
  1682. if (sb_len > 1)
  1683. sshdr->sense_key = (sense_buffer[1] & 0xf);
  1684. if (sb_len > 2)
  1685. sshdr->asc = sense_buffer[2];
  1686. if (sb_len > 3)
  1687. sshdr->ascq = sense_buffer[3];
  1688. if (sb_len > 7)
  1689. sshdr->additional_length = sense_buffer[7];
  1690. } else {
  1691. /*
  1692. * fixed format
  1693. */
  1694. if (sb_len > 2)
  1695. sshdr->sense_key = (sense_buffer[2] & 0xf);
  1696. if (sb_len > 7) {
  1697. sb_len = (sb_len < (sense_buffer[7] + 8)) ?
  1698. sb_len : (sense_buffer[7] + 8);
  1699. if (sb_len > 12)
  1700. sshdr->asc = sense_buffer[12];
  1701. if (sb_len > 13)
  1702. sshdr->ascq = sense_buffer[13];
  1703. }
  1704. }
  1705. return 1;
  1706. }
  1707. EXPORT_SYMBOL(scsi_normalize_sense);
  1708. int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
  1709. struct scsi_sense_hdr *sshdr)
  1710. {
  1711. return scsi_normalize_sense(cmd->sense_buffer,
  1712. SCSI_SENSE_BUFFERSIZE, sshdr);
  1713. }
  1714. EXPORT_SYMBOL(scsi_command_normalize_sense);
  1715. /**
  1716. * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
  1717. * @sense_buffer: byte array of descriptor format sense data
  1718. * @sb_len: number of valid bytes in sense_buffer
  1719. * @desc_type: value of descriptor type to find
  1720. * (e.g. 0 -> information)
  1721. *
  1722. * Notes:
  1723. * only valid when sense data is in descriptor format
  1724. *
  1725. * Return value:
  1726. * pointer to start of (first) descriptor if found else NULL
  1727. */
  1728. const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
  1729. int desc_type)
  1730. {
  1731. int add_sen_len, add_len, desc_len, k;
  1732. const u8 * descp;
  1733. if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
  1734. return NULL;
  1735. if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
  1736. return NULL;
  1737. add_sen_len = (add_sen_len < (sb_len - 8)) ?
  1738. add_sen_len : (sb_len - 8);
  1739. descp = &sense_buffer[8];
  1740. for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
  1741. descp += desc_len;
  1742. add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
  1743. desc_len = add_len + 2;
  1744. if (descp[0] == desc_type)
  1745. return descp;
  1746. if (add_len < 0) // short descriptor ??
  1747. break;
  1748. }
  1749. return NULL;
  1750. }
  1751. EXPORT_SYMBOL(scsi_sense_desc_find);
  1752. /**
  1753. * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
  1754. * @sense_buffer: byte array of sense data
  1755. * @sb_len: number of valid bytes in sense_buffer
  1756. * @info_out: pointer to 64 integer where 8 or 4 byte information
  1757. * field will be placed if found.
  1758. *
  1759. * Return value:
  1760. * 1 if information field found, 0 if not found.
  1761. */
  1762. int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
  1763. u64 * info_out)
  1764. {
  1765. int j;
  1766. const u8 * ucp;
  1767. u64 ull;
  1768. if (sb_len < 7)
  1769. return 0;
  1770. switch (sense_buffer[0] & 0x7f) {
  1771. case 0x70:
  1772. case 0x71:
  1773. if (sense_buffer[0] & 0x80) {
  1774. *info_out = (sense_buffer[3] << 24) +
  1775. (sense_buffer[4] << 16) +
  1776. (sense_buffer[5] << 8) + sense_buffer[6];
  1777. return 1;
  1778. } else
  1779. return 0;
  1780. case 0x72:
  1781. case 0x73:
  1782. ucp = scsi_sense_desc_find(sense_buffer, sb_len,
  1783. 0 /* info desc */);
  1784. if (ucp && (0xa == ucp[1])) {
  1785. ull = 0;
  1786. for (j = 0; j < 8; ++j) {
  1787. if (j > 0)
  1788. ull <<= 8;
  1789. ull |= ucp[4 + j];
  1790. }
  1791. *info_out = ull;
  1792. return 1;
  1793. } else
  1794. return 0;
  1795. default:
  1796. return 0;
  1797. }
  1798. }
  1799. EXPORT_SYMBOL(scsi_get_sense_info_fld);
  1800. /**
  1801. * scsi_build_sense_buffer - build sense data in a buffer
  1802. * @desc: Sense format (non zero == descriptor format,
  1803. * 0 == fixed format)
  1804. * @buf: Where to build sense data
  1805. * @key: Sense key
  1806. * @asc: Additional sense code
  1807. * @ascq: Additional sense code qualifier
  1808. *
  1809. **/
  1810. void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq)
  1811. {
  1812. if (desc) {
  1813. buf[0] = 0x72; /* descriptor, current */
  1814. buf[1] = key;
  1815. buf[2] = asc;
  1816. buf[3] = ascq;
  1817. buf[7] = 0;
  1818. } else {
  1819. buf[0] = 0x70; /* fixed, current */
  1820. buf[2] = key;
  1821. buf[7] = 0xa;
  1822. buf[12] = asc;
  1823. buf[13] = ascq;
  1824. }
  1825. }
  1826. EXPORT_SYMBOL(scsi_build_sense_buffer);