scsi_error.c 71 KB

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