scsi_error.c 66 KB

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