scsi_error.c 69 KB

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