scsi_error.c 68 KB

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