scsi_error.c 67 KB

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