scsi_error.c 70 KB

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