scsi_error.c 67 KB

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