scsi_error.c 57 KB

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