scsi_error.c 56 KB

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