scsi_error.c 67 KB

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