scsi_error.c 67 KB

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