scsi_error.c 71 KB

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