scsi_error.c 68 KB

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