scsi_error.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  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 == FAILED) {
  1060. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
  1061. " cmd failed:"
  1062. "0x%p\n",
  1063. current->comm,
  1064. scmd));
  1065. list_splice_init(&check_list, work_q);
  1066. return list_empty(work_q);
  1067. }
  1068. scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
  1069. if (rtn == FAST_IO_FAIL)
  1070. scsi_eh_finish_cmd(scmd, done_q);
  1071. else
  1072. list_move_tail(&scmd->eh_entry, &check_list);
  1073. }
  1074. return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
  1075. }
  1076. /**
  1077. * scsi_eh_try_stu - Send START_UNIT to device.
  1078. * @scmd: &scsi_cmnd to send START_UNIT
  1079. *
  1080. * Return value:
  1081. * 0 - Device is ready. 1 - Device NOT ready.
  1082. */
  1083. static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
  1084. {
  1085. static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
  1086. if (scmd->device->allow_restart) {
  1087. int i, rtn = NEEDS_RETRY;
  1088. for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
  1089. rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
  1090. if (rtn == SUCCESS)
  1091. return 0;
  1092. }
  1093. return 1;
  1094. }
  1095. /**
  1096. * scsi_eh_stu - send START_UNIT if needed
  1097. * @shost: &scsi host being recovered.
  1098. * @work_q: &list_head for pending commands.
  1099. * @done_q: &list_head for processed commands.
  1100. *
  1101. * Notes:
  1102. * If commands are failing due to not ready, initializing command required,
  1103. * try revalidating the device, which will end up sending a start unit.
  1104. */
  1105. static int scsi_eh_stu(struct Scsi_Host *shost,
  1106. struct list_head *work_q,
  1107. struct list_head *done_q)
  1108. {
  1109. struct scsi_cmnd *scmd, *stu_scmd, *next;
  1110. struct scsi_device *sdev;
  1111. unsigned long flags;
  1112. shost_for_each_device(sdev, shost) {
  1113. spin_lock_irqsave(shost->host_lock, flags);
  1114. if (scsi_host_eh_past_deadline(shost)) {
  1115. spin_unlock_irqrestore(shost->host_lock, flags);
  1116. SCSI_LOG_ERROR_RECOVERY(3,
  1117. shost_printk(KERN_INFO, shost,
  1118. "skip %s, past eh deadline\n",
  1119. __func__));
  1120. break;
  1121. }
  1122. spin_unlock_irqrestore(shost->host_lock, flags);
  1123. stu_scmd = NULL;
  1124. list_for_each_entry(scmd, work_q, eh_entry)
  1125. if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
  1126. scsi_check_sense(scmd) == FAILED ) {
  1127. stu_scmd = scmd;
  1128. break;
  1129. }
  1130. if (!stu_scmd)
  1131. continue;
  1132. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
  1133. " 0x%p\n", current->comm, sdev));
  1134. if (!scsi_eh_try_stu(stu_scmd)) {
  1135. if (!scsi_device_online(sdev) ||
  1136. !scsi_eh_tur(stu_scmd)) {
  1137. list_for_each_entry_safe(scmd, next,
  1138. work_q, eh_entry) {
  1139. if (scmd->device == sdev)
  1140. scsi_eh_finish_cmd(scmd, done_q);
  1141. }
  1142. }
  1143. } else {
  1144. SCSI_LOG_ERROR_RECOVERY(3,
  1145. printk("%s: START_UNIT failed to sdev:"
  1146. " 0x%p\n", current->comm, sdev));
  1147. }
  1148. }
  1149. return list_empty(work_q);
  1150. }
  1151. /**
  1152. * scsi_eh_bus_device_reset - send bdr if needed
  1153. * @shost: scsi host being recovered.
  1154. * @work_q: &list_head for pending commands.
  1155. * @done_q: &list_head for processed commands.
  1156. *
  1157. * Notes:
  1158. * Try a bus device reset. Still, look to see whether we have multiple
  1159. * devices that are jammed or not - if we have multiple devices, it
  1160. * makes no sense to try bus_device_reset - we really would need to try
  1161. * a bus_reset instead.
  1162. */
  1163. static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
  1164. struct list_head *work_q,
  1165. struct list_head *done_q)
  1166. {
  1167. struct scsi_cmnd *scmd, *bdr_scmd, *next;
  1168. struct scsi_device *sdev;
  1169. unsigned long flags;
  1170. int rtn;
  1171. shost_for_each_device(sdev, shost) {
  1172. spin_lock_irqsave(shost->host_lock, flags);
  1173. if (scsi_host_eh_past_deadline(shost)) {
  1174. spin_unlock_irqrestore(shost->host_lock, flags);
  1175. SCSI_LOG_ERROR_RECOVERY(3,
  1176. shost_printk(KERN_INFO, shost,
  1177. "skip %s, past eh deadline\n",
  1178. __func__));
  1179. break;
  1180. }
  1181. spin_unlock_irqrestore(shost->host_lock, flags);
  1182. bdr_scmd = NULL;
  1183. list_for_each_entry(scmd, work_q, eh_entry)
  1184. if (scmd->device == sdev) {
  1185. bdr_scmd = scmd;
  1186. break;
  1187. }
  1188. if (!bdr_scmd)
  1189. continue;
  1190. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
  1191. " 0x%p\n", current->comm,
  1192. sdev));
  1193. rtn = scsi_try_bus_device_reset(bdr_scmd);
  1194. if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
  1195. if (!scsi_device_online(sdev) ||
  1196. rtn == FAST_IO_FAIL ||
  1197. !scsi_eh_tur(bdr_scmd)) {
  1198. list_for_each_entry_safe(scmd, next,
  1199. work_q, eh_entry) {
  1200. if (scmd->device == sdev)
  1201. scsi_eh_finish_cmd(scmd,
  1202. done_q);
  1203. }
  1204. }
  1205. } else {
  1206. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
  1207. " failed sdev:"
  1208. "0x%p\n",
  1209. current->comm,
  1210. sdev));
  1211. }
  1212. }
  1213. return list_empty(work_q);
  1214. }
  1215. /**
  1216. * scsi_eh_target_reset - send target reset if needed
  1217. * @shost: scsi host being recovered.
  1218. * @work_q: &list_head for pending commands.
  1219. * @done_q: &list_head for processed commands.
  1220. *
  1221. * Notes:
  1222. * Try a target reset.
  1223. */
  1224. static int scsi_eh_target_reset(struct Scsi_Host *shost,
  1225. struct list_head *work_q,
  1226. struct list_head *done_q)
  1227. {
  1228. LIST_HEAD(tmp_list);
  1229. LIST_HEAD(check_list);
  1230. list_splice_init(work_q, &tmp_list);
  1231. while (!list_empty(&tmp_list)) {
  1232. struct scsi_cmnd *next, *scmd;
  1233. int rtn;
  1234. unsigned int id;
  1235. unsigned long flags;
  1236. spin_lock_irqsave(shost->host_lock, flags);
  1237. if (scsi_host_eh_past_deadline(shost)) {
  1238. spin_unlock_irqrestore(shost->host_lock, flags);
  1239. /* push back on work queue for further processing */
  1240. list_splice_init(&check_list, work_q);
  1241. list_splice_init(&tmp_list, work_q);
  1242. SCSI_LOG_ERROR_RECOVERY(3,
  1243. shost_printk(KERN_INFO, shost,
  1244. "skip %s, past eh deadline\n",
  1245. __func__));
  1246. return list_empty(work_q);
  1247. }
  1248. spin_unlock_irqrestore(shost->host_lock, flags);
  1249. scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
  1250. id = scmd_id(scmd);
  1251. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending target reset "
  1252. "to target %d\n",
  1253. current->comm, id));
  1254. rtn = scsi_try_target_reset(scmd);
  1255. if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
  1256. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Target reset"
  1257. " failed target: "
  1258. "%d\n",
  1259. current->comm, id));
  1260. list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
  1261. if (scmd_id(scmd) != id)
  1262. continue;
  1263. if (rtn == SUCCESS)
  1264. list_move_tail(&scmd->eh_entry, &check_list);
  1265. else if (rtn == FAST_IO_FAIL)
  1266. scsi_eh_finish_cmd(scmd, done_q);
  1267. else
  1268. /* push back on work queue for further processing */
  1269. list_move(&scmd->eh_entry, work_q);
  1270. }
  1271. }
  1272. return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
  1273. }
  1274. /**
  1275. * scsi_eh_bus_reset - send a bus reset
  1276. * @shost: &scsi host being recovered.
  1277. * @work_q: &list_head for pending commands.
  1278. * @done_q: &list_head for processed commands.
  1279. */
  1280. static int scsi_eh_bus_reset(struct Scsi_Host *shost,
  1281. struct list_head *work_q,
  1282. struct list_head *done_q)
  1283. {
  1284. struct scsi_cmnd *scmd, *chan_scmd, *next;
  1285. LIST_HEAD(check_list);
  1286. unsigned int channel;
  1287. int rtn;
  1288. unsigned long flags;
  1289. /*
  1290. * we really want to loop over the various channels, and do this on
  1291. * a channel by channel basis. we should also check to see if any
  1292. * of the failed commands are on soft_reset devices, and if so, skip
  1293. * the reset.
  1294. */
  1295. for (channel = 0; channel <= shost->max_channel; channel++) {
  1296. spin_lock_irqsave(shost->host_lock, flags);
  1297. if (scsi_host_eh_past_deadline(shost)) {
  1298. spin_unlock_irqrestore(shost->host_lock, flags);
  1299. list_splice_init(&check_list, work_q);
  1300. SCSI_LOG_ERROR_RECOVERY(3,
  1301. shost_printk(KERN_INFO, shost,
  1302. "skip %s, past eh deadline\n",
  1303. __func__));
  1304. return list_empty(work_q);
  1305. }
  1306. spin_unlock_irqrestore(shost->host_lock, flags);
  1307. chan_scmd = NULL;
  1308. list_for_each_entry(scmd, work_q, eh_entry) {
  1309. if (channel == scmd_channel(scmd)) {
  1310. chan_scmd = scmd;
  1311. break;
  1312. /*
  1313. * FIXME add back in some support for
  1314. * soft_reset devices.
  1315. */
  1316. }
  1317. }
  1318. if (!chan_scmd)
  1319. continue;
  1320. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
  1321. " %d\n", current->comm,
  1322. channel));
  1323. rtn = scsi_try_bus_reset(chan_scmd);
  1324. if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
  1325. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1326. if (channel == scmd_channel(scmd)) {
  1327. if (rtn == FAST_IO_FAIL)
  1328. scsi_eh_finish_cmd(scmd,
  1329. done_q);
  1330. else
  1331. list_move_tail(&scmd->eh_entry,
  1332. &check_list);
  1333. }
  1334. }
  1335. } else {
  1336. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
  1337. " failed chan: %d\n",
  1338. current->comm,
  1339. channel));
  1340. }
  1341. }
  1342. return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
  1343. }
  1344. /**
  1345. * scsi_eh_host_reset - send a host reset
  1346. * @work_q: list_head for processed commands.
  1347. * @done_q: list_head for processed commands.
  1348. */
  1349. static int scsi_eh_host_reset(struct list_head *work_q,
  1350. struct list_head *done_q)
  1351. {
  1352. struct scsi_cmnd *scmd, *next;
  1353. LIST_HEAD(check_list);
  1354. int rtn;
  1355. if (!list_empty(work_q)) {
  1356. scmd = list_entry(work_q->next,
  1357. struct scsi_cmnd, eh_entry);
  1358. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
  1359. , current->comm));
  1360. rtn = scsi_try_host_reset(scmd);
  1361. if (rtn == SUCCESS) {
  1362. list_splice_init(work_q, &check_list);
  1363. } else if (rtn == FAST_IO_FAIL) {
  1364. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1365. scsi_eh_finish_cmd(scmd, done_q);
  1366. }
  1367. } else {
  1368. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
  1369. " failed\n",
  1370. current->comm));
  1371. }
  1372. }
  1373. return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
  1374. }
  1375. /**
  1376. * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
  1377. * @work_q: list_head for processed commands.
  1378. * @done_q: list_head for processed commands.
  1379. */
  1380. static void scsi_eh_offline_sdevs(struct list_head *work_q,
  1381. struct list_head *done_q)
  1382. {
  1383. struct scsi_cmnd *scmd, *next;
  1384. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1385. sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
  1386. "not ready after error recovery\n");
  1387. scsi_device_set_state(scmd->device, SDEV_OFFLINE);
  1388. if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
  1389. /*
  1390. * FIXME: Handle lost cmds.
  1391. */
  1392. }
  1393. scsi_eh_finish_cmd(scmd, done_q);
  1394. }
  1395. return;
  1396. }
  1397. /**
  1398. * scsi_noretry_cmd - determinte if command should be failed fast
  1399. * @scmd: SCSI cmd to examine.
  1400. */
  1401. int scsi_noretry_cmd(struct scsi_cmnd *scmd)
  1402. {
  1403. switch (host_byte(scmd->result)) {
  1404. case DID_OK:
  1405. break;
  1406. case DID_BUS_BUSY:
  1407. return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
  1408. case DID_PARITY:
  1409. return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
  1410. case DID_ERROR:
  1411. if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
  1412. status_byte(scmd->result) == RESERVATION_CONFLICT)
  1413. return 0;
  1414. /* fall through */
  1415. case DID_SOFT_ERROR:
  1416. return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
  1417. }
  1418. switch (status_byte(scmd->result)) {
  1419. case CHECK_CONDITION:
  1420. /*
  1421. * assume caller has checked sense and determinted
  1422. * the check condition was retryable.
  1423. */
  1424. if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
  1425. scmd->request->cmd_type == REQ_TYPE_BLOCK_PC)
  1426. return 1;
  1427. }
  1428. return 0;
  1429. }
  1430. /**
  1431. * scsi_decide_disposition - Disposition a cmd on return from LLD.
  1432. * @scmd: SCSI cmd to examine.
  1433. *
  1434. * Notes:
  1435. * This is *only* called when we are examining the status after sending
  1436. * out the actual data command. any commands that are queued for error
  1437. * recovery (e.g. test_unit_ready) do *not* come through here.
  1438. *
  1439. * When this routine returns failed, it means the error handler thread
  1440. * is woken. In cases where the error code indicates an error that
  1441. * doesn't require the error handler read (i.e. we don't need to
  1442. * abort/reset), this function should return SUCCESS.
  1443. */
  1444. int scsi_decide_disposition(struct scsi_cmnd *scmd)
  1445. {
  1446. int rtn;
  1447. /*
  1448. * if the device is offline, then we clearly just pass the result back
  1449. * up to the top level.
  1450. */
  1451. if (!scsi_device_online(scmd->device)) {
  1452. SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
  1453. " as SUCCESS\n",
  1454. __func__));
  1455. return SUCCESS;
  1456. }
  1457. /*
  1458. * first check the host byte, to see if there is anything in there
  1459. * that would indicate what we need to do.
  1460. */
  1461. switch (host_byte(scmd->result)) {
  1462. case DID_PASSTHROUGH:
  1463. /*
  1464. * no matter what, pass this through to the upper layer.
  1465. * nuke this special code so that it looks like we are saying
  1466. * did_ok.
  1467. */
  1468. scmd->result &= 0xff00ffff;
  1469. return SUCCESS;
  1470. case DID_OK:
  1471. /*
  1472. * looks good. drop through, and check the next byte.
  1473. */
  1474. break;
  1475. case DID_NO_CONNECT:
  1476. case DID_BAD_TARGET:
  1477. case DID_ABORT:
  1478. /*
  1479. * note - this means that we just report the status back
  1480. * to the top level driver, not that we actually think
  1481. * that it indicates SUCCESS.
  1482. */
  1483. return SUCCESS;
  1484. /*
  1485. * when the low level driver returns did_soft_error,
  1486. * it is responsible for keeping an internal retry counter
  1487. * in order to avoid endless loops (db)
  1488. *
  1489. * actually this is a bug in this function here. we should
  1490. * be mindful of the maximum number of retries specified
  1491. * and not get stuck in a loop.
  1492. */
  1493. case DID_SOFT_ERROR:
  1494. goto maybe_retry;
  1495. case DID_IMM_RETRY:
  1496. return NEEDS_RETRY;
  1497. case DID_REQUEUE:
  1498. return ADD_TO_MLQUEUE;
  1499. case DID_TRANSPORT_DISRUPTED:
  1500. /*
  1501. * LLD/transport was disrupted during processing of the IO.
  1502. * The transport class is now blocked/blocking,
  1503. * and the transport will decide what to do with the IO
  1504. * based on its timers and recovery capablilities if
  1505. * there are enough retries.
  1506. */
  1507. goto maybe_retry;
  1508. case DID_TRANSPORT_FAILFAST:
  1509. /*
  1510. * The transport decided to failfast the IO (most likely
  1511. * the fast io fail tmo fired), so send IO directly upwards.
  1512. */
  1513. return SUCCESS;
  1514. case DID_ERROR:
  1515. if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
  1516. status_byte(scmd->result) == RESERVATION_CONFLICT)
  1517. /*
  1518. * execute reservation conflict processing code
  1519. * lower down
  1520. */
  1521. break;
  1522. /* fallthrough */
  1523. case DID_BUS_BUSY:
  1524. case DID_PARITY:
  1525. goto maybe_retry;
  1526. case DID_TIME_OUT:
  1527. /*
  1528. * when we scan the bus, we get timeout messages for
  1529. * these commands if there is no device available.
  1530. * other hosts report did_no_connect for the same thing.
  1531. */
  1532. if ((scmd->cmnd[0] == TEST_UNIT_READY ||
  1533. scmd->cmnd[0] == INQUIRY)) {
  1534. return SUCCESS;
  1535. } else {
  1536. return FAILED;
  1537. }
  1538. case DID_RESET:
  1539. return SUCCESS;
  1540. default:
  1541. return FAILED;
  1542. }
  1543. /*
  1544. * next, check the message byte.
  1545. */
  1546. if (msg_byte(scmd->result) != COMMAND_COMPLETE)
  1547. return FAILED;
  1548. /*
  1549. * check the status byte to see if this indicates anything special.
  1550. */
  1551. switch (status_byte(scmd->result)) {
  1552. case QUEUE_FULL:
  1553. scsi_handle_queue_full(scmd->device);
  1554. /*
  1555. * the case of trying to send too many commands to a
  1556. * tagged queueing device.
  1557. */
  1558. case BUSY:
  1559. /*
  1560. * device can't talk to us at the moment. Should only
  1561. * occur (SAM-3) when the task queue is empty, so will cause
  1562. * the empty queue handling to trigger a stall in the
  1563. * device.
  1564. */
  1565. return ADD_TO_MLQUEUE;
  1566. case GOOD:
  1567. if (scmd->cmnd[0] == REPORT_LUNS)
  1568. scmd->device->sdev_target->expecting_lun_change = 0;
  1569. scsi_handle_queue_ramp_up(scmd->device);
  1570. case COMMAND_TERMINATED:
  1571. return SUCCESS;
  1572. case TASK_ABORTED:
  1573. goto maybe_retry;
  1574. case CHECK_CONDITION:
  1575. rtn = scsi_check_sense(scmd);
  1576. if (rtn == NEEDS_RETRY)
  1577. goto maybe_retry;
  1578. /* if rtn == FAILED, we have no sense information;
  1579. * returning FAILED will wake the error handler thread
  1580. * to collect the sense and redo the decide
  1581. * disposition */
  1582. return rtn;
  1583. case CONDITION_GOOD:
  1584. case INTERMEDIATE_GOOD:
  1585. case INTERMEDIATE_C_GOOD:
  1586. case ACA_ACTIVE:
  1587. /*
  1588. * who knows? FIXME(eric)
  1589. */
  1590. return SUCCESS;
  1591. case RESERVATION_CONFLICT:
  1592. sdev_printk(KERN_INFO, scmd->device,
  1593. "reservation conflict\n");
  1594. set_host_byte(scmd, DID_NEXUS_FAILURE);
  1595. return SUCCESS; /* causes immediate i/o error */
  1596. default:
  1597. return FAILED;
  1598. }
  1599. return FAILED;
  1600. maybe_retry:
  1601. /* we requeue for retry because the error was retryable, and
  1602. * the request was not marked fast fail. Note that above,
  1603. * even if the request is marked fast fail, we still requeue
  1604. * for queue congestion conditions (QUEUE_FULL or BUSY) */
  1605. if ((++scmd->retries) <= scmd->allowed
  1606. && !scsi_noretry_cmd(scmd)) {
  1607. return NEEDS_RETRY;
  1608. } else {
  1609. /*
  1610. * no more retries - report this one back to upper level.
  1611. */
  1612. return SUCCESS;
  1613. }
  1614. }
  1615. static void eh_lock_door_done(struct request *req, int uptodate)
  1616. {
  1617. __blk_put_request(req->q, req);
  1618. }
  1619. /**
  1620. * scsi_eh_lock_door - Prevent medium removal for the specified device
  1621. * @sdev: SCSI device to prevent medium removal
  1622. *
  1623. * Locking:
  1624. * We must be called from process context.
  1625. *
  1626. * Notes:
  1627. * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
  1628. * head of the devices request queue, and continue.
  1629. */
  1630. static void scsi_eh_lock_door(struct scsi_device *sdev)
  1631. {
  1632. struct request *req;
  1633. /*
  1634. * blk_get_request with GFP_KERNEL (__GFP_WAIT) sleeps until a
  1635. * request becomes available
  1636. */
  1637. req = blk_get_request(sdev->request_queue, READ, GFP_KERNEL);
  1638. req->cmd[0] = ALLOW_MEDIUM_REMOVAL;
  1639. req->cmd[1] = 0;
  1640. req->cmd[2] = 0;
  1641. req->cmd[3] = 0;
  1642. req->cmd[4] = SCSI_REMOVAL_PREVENT;
  1643. req->cmd[5] = 0;
  1644. req->cmd_len = COMMAND_SIZE(req->cmd[0]);
  1645. req->cmd_type = REQ_TYPE_BLOCK_PC;
  1646. req->cmd_flags |= REQ_QUIET;
  1647. req->timeout = 10 * HZ;
  1648. req->retries = 5;
  1649. blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
  1650. }
  1651. /**
  1652. * scsi_restart_operations - restart io operations to the specified host.
  1653. * @shost: Host we are restarting.
  1654. *
  1655. * Notes:
  1656. * When we entered the error handler, we blocked all further i/o to
  1657. * this device. we need to 'reverse' this process.
  1658. */
  1659. static void scsi_restart_operations(struct Scsi_Host *shost)
  1660. {
  1661. struct scsi_device *sdev;
  1662. unsigned long flags;
  1663. /*
  1664. * If the door was locked, we need to insert a door lock request
  1665. * onto the head of the SCSI request queue for the device. There
  1666. * is no point trying to lock the door of an off-line device.
  1667. */
  1668. shost_for_each_device(sdev, shost) {
  1669. if (scsi_device_online(sdev) && sdev->locked)
  1670. scsi_eh_lock_door(sdev);
  1671. }
  1672. /*
  1673. * next free up anything directly waiting upon the host. this
  1674. * will be requests for character device operations, and also for
  1675. * ioctls to queued block devices.
  1676. */
  1677. SCSI_LOG_ERROR_RECOVERY(3,
  1678. printk("scsi_eh_%d waking up host to restart\n",
  1679. shost->host_no));
  1680. spin_lock_irqsave(shost->host_lock, flags);
  1681. if (scsi_host_set_state(shost, SHOST_RUNNING))
  1682. if (scsi_host_set_state(shost, SHOST_CANCEL))
  1683. BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
  1684. spin_unlock_irqrestore(shost->host_lock, flags);
  1685. wake_up(&shost->host_wait);
  1686. /*
  1687. * finally we need to re-initiate requests that may be pending. we will
  1688. * have had everything blocked while error handling is taking place, and
  1689. * now that error recovery is done, we will need to ensure that these
  1690. * requests are started.
  1691. */
  1692. scsi_run_host_queues(shost);
  1693. /*
  1694. * if eh is active and host_eh_scheduled is pending we need to re-run
  1695. * recovery. we do this check after scsi_run_host_queues() to allow
  1696. * everything pent up since the last eh run a chance to make forward
  1697. * progress before we sync again. Either we'll immediately re-run
  1698. * recovery or scsi_device_unbusy() will wake us again when these
  1699. * pending commands complete.
  1700. */
  1701. spin_lock_irqsave(shost->host_lock, flags);
  1702. if (shost->host_eh_scheduled)
  1703. if (scsi_host_set_state(shost, SHOST_RECOVERY))
  1704. WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
  1705. spin_unlock_irqrestore(shost->host_lock, flags);
  1706. }
  1707. /**
  1708. * scsi_eh_ready_devs - check device ready state and recover if not.
  1709. * @shost: host to be recovered.
  1710. * @work_q: &list_head for pending commands.
  1711. * @done_q: &list_head for processed commands.
  1712. */
  1713. void scsi_eh_ready_devs(struct Scsi_Host *shost,
  1714. struct list_head *work_q,
  1715. struct list_head *done_q)
  1716. {
  1717. if (!scsi_eh_stu(shost, work_q, done_q))
  1718. if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
  1719. if (!scsi_eh_target_reset(shost, work_q, done_q))
  1720. if (!scsi_eh_bus_reset(shost, work_q, done_q))
  1721. if (!scsi_eh_host_reset(work_q, done_q))
  1722. scsi_eh_offline_sdevs(work_q,
  1723. done_q);
  1724. }
  1725. EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
  1726. /**
  1727. * scsi_eh_flush_done_q - finish processed commands or retry them.
  1728. * @done_q: list_head of processed commands.
  1729. */
  1730. void scsi_eh_flush_done_q(struct list_head *done_q)
  1731. {
  1732. struct scsi_cmnd *scmd, *next;
  1733. list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
  1734. list_del_init(&scmd->eh_entry);
  1735. if (scsi_device_online(scmd->device) &&
  1736. !scsi_noretry_cmd(scmd) &&
  1737. (++scmd->retries <= scmd->allowed)) {
  1738. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
  1739. " retry cmd: %p\n",
  1740. current->comm,
  1741. scmd));
  1742. scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
  1743. } else {
  1744. /*
  1745. * If just we got sense for the device (called
  1746. * scsi_eh_get_sense), scmd->result is already
  1747. * set, do not set DRIVER_TIMEOUT.
  1748. */
  1749. if (!scmd->result)
  1750. scmd->result |= (DRIVER_TIMEOUT << 24);
  1751. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
  1752. " cmd: %p\n",
  1753. current->comm, scmd));
  1754. scsi_finish_command(scmd);
  1755. }
  1756. }
  1757. }
  1758. EXPORT_SYMBOL(scsi_eh_flush_done_q);
  1759. /**
  1760. * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
  1761. * @shost: Host to unjam.
  1762. *
  1763. * Notes:
  1764. * When we come in here, we *know* that all commands on the bus have
  1765. * either completed, failed or timed out. we also know that no further
  1766. * commands are being sent to the host, so things are relatively quiet
  1767. * and we have freedom to fiddle with things as we wish.
  1768. *
  1769. * This is only the *default* implementation. it is possible for
  1770. * individual drivers to supply their own version of this function, and
  1771. * if the maintainer wishes to do this, it is strongly suggested that
  1772. * this function be taken as a template and modified. this function
  1773. * was designed to correctly handle problems for about 95% of the
  1774. * different cases out there, and it should always provide at least a
  1775. * reasonable amount of error recovery.
  1776. *
  1777. * Any command marked 'failed' or 'timeout' must eventually have
  1778. * scsi_finish_cmd() called for it. we do all of the retry stuff
  1779. * here, so when we restart the host after we return it should have an
  1780. * empty queue.
  1781. */
  1782. static void scsi_unjam_host(struct Scsi_Host *shost)
  1783. {
  1784. unsigned long flags;
  1785. LIST_HEAD(eh_work_q);
  1786. LIST_HEAD(eh_done_q);
  1787. spin_lock_irqsave(shost->host_lock, flags);
  1788. list_splice_init(&shost->eh_cmd_q, &eh_work_q);
  1789. spin_unlock_irqrestore(shost->host_lock, flags);
  1790. SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
  1791. if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
  1792. if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
  1793. scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
  1794. spin_lock_irqsave(shost->host_lock, flags);
  1795. if (shost->eh_deadline)
  1796. shost->last_reset = 0;
  1797. spin_unlock_irqrestore(shost->host_lock, flags);
  1798. scsi_eh_flush_done_q(&eh_done_q);
  1799. }
  1800. /**
  1801. * scsi_error_handler - SCSI error handler thread
  1802. * @data: Host for which we are running.
  1803. *
  1804. * Notes:
  1805. * This is the main error handling loop. This is run as a kernel thread
  1806. * for every SCSI host and handles all error handling activity.
  1807. */
  1808. int scsi_error_handler(void *data)
  1809. {
  1810. struct Scsi_Host *shost = data;
  1811. /*
  1812. * We use TASK_INTERRUPTIBLE so that the thread is not
  1813. * counted against the load average as a running process.
  1814. * We never actually get interrupted because kthread_run
  1815. * disables signal delivery for the created thread.
  1816. */
  1817. while (!kthread_should_stop()) {
  1818. set_current_state(TASK_INTERRUPTIBLE);
  1819. if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
  1820. shost->host_failed != shost->host_busy) {
  1821. SCSI_LOG_ERROR_RECOVERY(1,
  1822. printk("scsi_eh_%d: sleeping\n",
  1823. shost->host_no));
  1824. schedule();
  1825. continue;
  1826. }
  1827. __set_current_state(TASK_RUNNING);
  1828. SCSI_LOG_ERROR_RECOVERY(1,
  1829. printk("scsi_eh_%d: waking up %d/%d/%d\n",
  1830. shost->host_no, shost->host_eh_scheduled,
  1831. shost->host_failed, shost->host_busy));
  1832. /*
  1833. * We have a host that is failing for some reason. Figure out
  1834. * what we need to do to get it up and online again (if we can).
  1835. * If we fail, we end up taking the thing offline.
  1836. */
  1837. if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
  1838. SCSI_LOG_ERROR_RECOVERY(1,
  1839. printk(KERN_ERR "Error handler scsi_eh_%d "
  1840. "unable to autoresume\n",
  1841. shost->host_no));
  1842. continue;
  1843. }
  1844. if (shost->transportt->eh_strategy_handler)
  1845. shost->transportt->eh_strategy_handler(shost);
  1846. else
  1847. scsi_unjam_host(shost);
  1848. /*
  1849. * Note - if the above fails completely, the action is to take
  1850. * individual devices offline and flush the queue of any
  1851. * outstanding requests that may have been pending. When we
  1852. * restart, we restart any I/O to any other devices on the bus
  1853. * which are still online.
  1854. */
  1855. scsi_restart_operations(shost);
  1856. if (!shost->eh_noresume)
  1857. scsi_autopm_put_host(shost);
  1858. }
  1859. __set_current_state(TASK_RUNNING);
  1860. SCSI_LOG_ERROR_RECOVERY(1,
  1861. printk("Error handler scsi_eh_%d exiting\n", shost->host_no));
  1862. shost->ehandler = NULL;
  1863. return 0;
  1864. }
  1865. /*
  1866. * Function: scsi_report_bus_reset()
  1867. *
  1868. * Purpose: Utility function used by low-level drivers to report that
  1869. * they have observed a bus reset on the bus being handled.
  1870. *
  1871. * Arguments: shost - Host in question
  1872. * channel - channel on which reset was observed.
  1873. *
  1874. * Returns: Nothing
  1875. *
  1876. * Lock status: Host lock must be held.
  1877. *
  1878. * Notes: This only needs to be called if the reset is one which
  1879. * originates from an unknown location. Resets originated
  1880. * by the mid-level itself don't need to call this, but there
  1881. * should be no harm.
  1882. *
  1883. * The main purpose of this is to make sure that a CHECK_CONDITION
  1884. * is properly treated.
  1885. */
  1886. void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
  1887. {
  1888. struct scsi_device *sdev;
  1889. __shost_for_each_device(sdev, shost) {
  1890. if (channel == sdev_channel(sdev))
  1891. __scsi_report_device_reset(sdev, NULL);
  1892. }
  1893. }
  1894. EXPORT_SYMBOL(scsi_report_bus_reset);
  1895. /*
  1896. * Function: scsi_report_device_reset()
  1897. *
  1898. * Purpose: Utility function used by low-level drivers to report that
  1899. * they have observed a device reset on the device being handled.
  1900. *
  1901. * Arguments: shost - Host in question
  1902. * channel - channel on which reset was observed
  1903. * target - target on which reset was observed
  1904. *
  1905. * Returns: Nothing
  1906. *
  1907. * Lock status: Host lock must be held
  1908. *
  1909. * Notes: This only needs to be called if the reset is one which
  1910. * originates from an unknown location. Resets originated
  1911. * by the mid-level itself don't need to call this, but there
  1912. * should be no harm.
  1913. *
  1914. * The main purpose of this is to make sure that a CHECK_CONDITION
  1915. * is properly treated.
  1916. */
  1917. void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
  1918. {
  1919. struct scsi_device *sdev;
  1920. __shost_for_each_device(sdev, shost) {
  1921. if (channel == sdev_channel(sdev) &&
  1922. target == sdev_id(sdev))
  1923. __scsi_report_device_reset(sdev, NULL);
  1924. }
  1925. }
  1926. EXPORT_SYMBOL(scsi_report_device_reset);
  1927. static void
  1928. scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
  1929. {
  1930. }
  1931. /*
  1932. * Function: scsi_reset_provider
  1933. *
  1934. * Purpose: Send requested reset to a bus or device at any phase.
  1935. *
  1936. * Arguments: device - device to send reset to
  1937. * flag - reset type (see scsi.h)
  1938. *
  1939. * Returns: SUCCESS/FAILURE.
  1940. *
  1941. * Notes: This is used by the SCSI Generic driver to provide
  1942. * Bus/Device reset capability.
  1943. */
  1944. int
  1945. scsi_reset_provider(struct scsi_device *dev, int flag)
  1946. {
  1947. struct scsi_cmnd *scmd;
  1948. struct Scsi_Host *shost = dev->host;
  1949. struct request req;
  1950. unsigned long flags;
  1951. int rtn;
  1952. if (scsi_autopm_get_host(shost) < 0)
  1953. return FAILED;
  1954. scmd = scsi_get_command(dev, GFP_KERNEL);
  1955. blk_rq_init(NULL, &req);
  1956. scmd->request = &req;
  1957. scmd->cmnd = req.cmd;
  1958. scmd->scsi_done = scsi_reset_provider_done_command;
  1959. memset(&scmd->sdb, 0, sizeof(scmd->sdb));
  1960. scmd->cmd_len = 0;
  1961. scmd->sc_data_direction = DMA_BIDIRECTIONAL;
  1962. spin_lock_irqsave(shost->host_lock, flags);
  1963. shost->tmf_in_progress = 1;
  1964. spin_unlock_irqrestore(shost->host_lock, flags);
  1965. switch (flag) {
  1966. case SCSI_TRY_RESET_DEVICE:
  1967. rtn = scsi_try_bus_device_reset(scmd);
  1968. if (rtn == SUCCESS)
  1969. break;
  1970. /* FALLTHROUGH */
  1971. case SCSI_TRY_RESET_TARGET:
  1972. rtn = scsi_try_target_reset(scmd);
  1973. if (rtn == SUCCESS)
  1974. break;
  1975. /* FALLTHROUGH */
  1976. case SCSI_TRY_RESET_BUS:
  1977. rtn = scsi_try_bus_reset(scmd);
  1978. if (rtn == SUCCESS)
  1979. break;
  1980. /* FALLTHROUGH */
  1981. case SCSI_TRY_RESET_HOST:
  1982. rtn = scsi_try_host_reset(scmd);
  1983. break;
  1984. default:
  1985. rtn = FAILED;
  1986. }
  1987. spin_lock_irqsave(shost->host_lock, flags);
  1988. shost->tmf_in_progress = 0;
  1989. spin_unlock_irqrestore(shost->host_lock, flags);
  1990. /*
  1991. * be sure to wake up anyone who was sleeping or had their queue
  1992. * suspended while we performed the TMF.
  1993. */
  1994. SCSI_LOG_ERROR_RECOVERY(3,
  1995. printk("%s: waking up host to restart after TMF\n",
  1996. __func__));
  1997. wake_up(&shost->host_wait);
  1998. scsi_run_host_queues(shost);
  1999. scsi_next_command(scmd);
  2000. scsi_autopm_put_host(shost);
  2001. return rtn;
  2002. }
  2003. EXPORT_SYMBOL(scsi_reset_provider);
  2004. /**
  2005. * scsi_normalize_sense - normalize main elements from either fixed or
  2006. * descriptor sense data format into a common format.
  2007. *
  2008. * @sense_buffer: byte array containing sense data returned by device
  2009. * @sb_len: number of valid bytes in sense_buffer
  2010. * @sshdr: pointer to instance of structure that common
  2011. * elements are written to.
  2012. *
  2013. * Notes:
  2014. * The "main elements" from sense data are: response_code, sense_key,
  2015. * asc, ascq and additional_length (only for descriptor format).
  2016. *
  2017. * Typically this function can be called after a device has
  2018. * responded to a SCSI command with the CHECK_CONDITION status.
  2019. *
  2020. * Return value:
  2021. * 1 if valid sense data information found, else 0;
  2022. */
  2023. int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
  2024. struct scsi_sense_hdr *sshdr)
  2025. {
  2026. if (!sense_buffer || !sb_len)
  2027. return 0;
  2028. memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
  2029. sshdr->response_code = (sense_buffer[0] & 0x7f);
  2030. if (!scsi_sense_valid(sshdr))
  2031. return 0;
  2032. if (sshdr->response_code >= 0x72) {
  2033. /*
  2034. * descriptor format
  2035. */
  2036. if (sb_len > 1)
  2037. sshdr->sense_key = (sense_buffer[1] & 0xf);
  2038. if (sb_len > 2)
  2039. sshdr->asc = sense_buffer[2];
  2040. if (sb_len > 3)
  2041. sshdr->ascq = sense_buffer[3];
  2042. if (sb_len > 7)
  2043. sshdr->additional_length = sense_buffer[7];
  2044. } else {
  2045. /*
  2046. * fixed format
  2047. */
  2048. if (sb_len > 2)
  2049. sshdr->sense_key = (sense_buffer[2] & 0xf);
  2050. if (sb_len > 7) {
  2051. sb_len = (sb_len < (sense_buffer[7] + 8)) ?
  2052. sb_len : (sense_buffer[7] + 8);
  2053. if (sb_len > 12)
  2054. sshdr->asc = sense_buffer[12];
  2055. if (sb_len > 13)
  2056. sshdr->ascq = sense_buffer[13];
  2057. }
  2058. }
  2059. return 1;
  2060. }
  2061. EXPORT_SYMBOL(scsi_normalize_sense);
  2062. int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
  2063. struct scsi_sense_hdr *sshdr)
  2064. {
  2065. return scsi_normalize_sense(cmd->sense_buffer,
  2066. SCSI_SENSE_BUFFERSIZE, sshdr);
  2067. }
  2068. EXPORT_SYMBOL(scsi_command_normalize_sense);
  2069. /**
  2070. * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
  2071. * @sense_buffer: byte array of descriptor format sense data
  2072. * @sb_len: number of valid bytes in sense_buffer
  2073. * @desc_type: value of descriptor type to find
  2074. * (e.g. 0 -> information)
  2075. *
  2076. * Notes:
  2077. * only valid when sense data is in descriptor format
  2078. *
  2079. * Return value:
  2080. * pointer to start of (first) descriptor if found else NULL
  2081. */
  2082. const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
  2083. int desc_type)
  2084. {
  2085. int add_sen_len, add_len, desc_len, k;
  2086. const u8 * descp;
  2087. if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
  2088. return NULL;
  2089. if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
  2090. return NULL;
  2091. add_sen_len = (add_sen_len < (sb_len - 8)) ?
  2092. add_sen_len : (sb_len - 8);
  2093. descp = &sense_buffer[8];
  2094. for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
  2095. descp += desc_len;
  2096. add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
  2097. desc_len = add_len + 2;
  2098. if (descp[0] == desc_type)
  2099. return descp;
  2100. if (add_len < 0) // short descriptor ??
  2101. break;
  2102. }
  2103. return NULL;
  2104. }
  2105. EXPORT_SYMBOL(scsi_sense_desc_find);
  2106. /**
  2107. * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
  2108. * @sense_buffer: byte array of sense data
  2109. * @sb_len: number of valid bytes in sense_buffer
  2110. * @info_out: pointer to 64 integer where 8 or 4 byte information
  2111. * field will be placed if found.
  2112. *
  2113. * Return value:
  2114. * 1 if information field found, 0 if not found.
  2115. */
  2116. int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
  2117. u64 * info_out)
  2118. {
  2119. int j;
  2120. const u8 * ucp;
  2121. u64 ull;
  2122. if (sb_len < 7)
  2123. return 0;
  2124. switch (sense_buffer[0] & 0x7f) {
  2125. case 0x70:
  2126. case 0x71:
  2127. if (sense_buffer[0] & 0x80) {
  2128. *info_out = (sense_buffer[3] << 24) +
  2129. (sense_buffer[4] << 16) +
  2130. (sense_buffer[5] << 8) + sense_buffer[6];
  2131. return 1;
  2132. } else
  2133. return 0;
  2134. case 0x72:
  2135. case 0x73:
  2136. ucp = scsi_sense_desc_find(sense_buffer, sb_len,
  2137. 0 /* info desc */);
  2138. if (ucp && (0xa == ucp[1])) {
  2139. ull = 0;
  2140. for (j = 0; j < 8; ++j) {
  2141. if (j > 0)
  2142. ull <<= 8;
  2143. ull |= ucp[4 + j];
  2144. }
  2145. *info_out = ull;
  2146. return 1;
  2147. } else
  2148. return 0;
  2149. default:
  2150. return 0;
  2151. }
  2152. }
  2153. EXPORT_SYMBOL(scsi_get_sense_info_fld);
  2154. /**
  2155. * scsi_build_sense_buffer - build sense data in a buffer
  2156. * @desc: Sense format (non zero == descriptor format,
  2157. * 0 == fixed format)
  2158. * @buf: Where to build sense data
  2159. * @key: Sense key
  2160. * @asc: Additional sense code
  2161. * @ascq: Additional sense code qualifier
  2162. *
  2163. **/
  2164. void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq)
  2165. {
  2166. if (desc) {
  2167. buf[0] = 0x72; /* descriptor, current */
  2168. buf[1] = key;
  2169. buf[2] = asc;
  2170. buf[3] = ascq;
  2171. buf[7] = 0;
  2172. } else {
  2173. buf[0] = 0x70; /* fixed, current */
  2174. buf[2] = key;
  2175. buf[7] = 0xa;
  2176. buf[12] = asc;
  2177. buf[13] = ascq;
  2178. }
  2179. }
  2180. EXPORT_SYMBOL(scsi_build_sense_buffer);