scsi_error.c 71 KB

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