scsi_error.c 63 KB

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