scsi_error.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  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/timer.h>
  19. #include <linux/string.h>
  20. #include <linux/kernel.h>
  21. #include <linux/freezer.h>
  22. #include <linux/kthread.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/delay.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_dbg.h>
  29. #include <scsi/scsi_device.h>
  30. #include <scsi/scsi_eh.h>
  31. #include <scsi/scsi_transport.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_ioctl.h>
  34. #include "scsi_priv.h"
  35. #include "scsi_logging.h"
  36. #include "scsi_transport_api.h"
  37. #define SENSE_TIMEOUT (10*HZ)
  38. /*
  39. * These should *probably* be handled by the host itself.
  40. * Since it is allowed to sleep, it probably should.
  41. */
  42. #define BUS_RESET_SETTLE_TIME (10)
  43. #define HOST_RESET_SETTLE_TIME (10)
  44. /* called with shost->host_lock held */
  45. void scsi_eh_wakeup(struct Scsi_Host *shost)
  46. {
  47. if (shost->host_busy == shost->host_failed) {
  48. wake_up_process(shost->ehandler);
  49. SCSI_LOG_ERROR_RECOVERY(5,
  50. printk("Waking error handler thread\n"));
  51. }
  52. }
  53. /**
  54. * scsi_schedule_eh - schedule EH for SCSI host
  55. * @shost: SCSI host to invoke error handling on.
  56. *
  57. * Schedule SCSI EH without scmd.
  58. */
  59. void scsi_schedule_eh(struct Scsi_Host *shost)
  60. {
  61. unsigned long flags;
  62. spin_lock_irqsave(shost->host_lock, flags);
  63. if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
  64. scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
  65. shost->host_eh_scheduled++;
  66. scsi_eh_wakeup(shost);
  67. }
  68. spin_unlock_irqrestore(shost->host_lock, flags);
  69. }
  70. EXPORT_SYMBOL_GPL(scsi_schedule_eh);
  71. /**
  72. * scsi_eh_scmd_add - add scsi cmd to error handling.
  73. * @scmd: scmd to run eh on.
  74. * @eh_flag: optional SCSI_EH flag.
  75. *
  76. * Return value:
  77. * 0 on failure.
  78. */
  79. int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
  80. {
  81. struct Scsi_Host *shost = scmd->device->host;
  82. unsigned long flags;
  83. int ret = 0;
  84. if (!shost->ehandler)
  85. return 0;
  86. spin_lock_irqsave(shost->host_lock, flags);
  87. if (scsi_host_set_state(shost, SHOST_RECOVERY))
  88. if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY))
  89. goto out_unlock;
  90. ret = 1;
  91. scmd->eh_eflags |= eh_flag;
  92. list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
  93. shost->host_failed++;
  94. scsi_eh_wakeup(shost);
  95. out_unlock:
  96. spin_unlock_irqrestore(shost->host_lock, flags);
  97. return ret;
  98. }
  99. /**
  100. * scsi_times_out - Timeout function for normal scsi commands.
  101. * @req: request that is timing out.
  102. *
  103. * Notes:
  104. * We do not need to lock this. There is the potential for a race
  105. * only in that the normal completion handling might run, but if the
  106. * normal completion function determines that the timer has already
  107. * fired, then it mustn't do anything.
  108. */
  109. enum blk_eh_timer_return scsi_times_out(struct request *req)
  110. {
  111. struct scsi_cmnd *scmd = req->special;
  112. enum blk_eh_timer_return (*eh_timed_out)(struct scsi_cmnd *);
  113. enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
  114. scsi_log_completion(scmd, TIMEOUT_ERROR);
  115. if (scmd->device->host->transportt->eh_timed_out)
  116. eh_timed_out = scmd->device->host->transportt->eh_timed_out;
  117. else if (scmd->device->host->hostt->eh_timed_out)
  118. eh_timed_out = scmd->device->host->hostt->eh_timed_out;
  119. else
  120. eh_timed_out = NULL;
  121. if (eh_timed_out)
  122. rtn = eh_timed_out(scmd);
  123. switch (rtn) {
  124. case BLK_EH_NOT_HANDLED:
  125. break;
  126. default:
  127. return rtn;
  128. }
  129. if (unlikely(!scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))) {
  130. scmd->result |= DID_TIME_OUT << 16;
  131. return BLK_EH_HANDLED;
  132. }
  133. return BLK_EH_NOT_HANDLED;
  134. }
  135. /**
  136. * scsi_block_when_processing_errors - Prevent cmds from being queued.
  137. * @sdev: Device on which we are performing recovery.
  138. *
  139. * Description:
  140. * We block until the host is out of error recovery, and then check to
  141. * see whether the host or the device is offline.
  142. *
  143. * Return value:
  144. * 0 when dev was taken offline by error recovery. 1 OK to proceed.
  145. */
  146. int scsi_block_when_processing_errors(struct scsi_device *sdev)
  147. {
  148. int online;
  149. wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
  150. online = scsi_device_online(sdev);
  151. SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __func__,
  152. online));
  153. return online;
  154. }
  155. EXPORT_SYMBOL(scsi_block_when_processing_errors);
  156. #ifdef CONFIG_SCSI_LOGGING
  157. /**
  158. * scsi_eh_prt_fail_stats - Log info on failures.
  159. * @shost: scsi host being recovered.
  160. * @work_q: Queue of scsi cmds to process.
  161. */
  162. static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
  163. struct list_head *work_q)
  164. {
  165. struct scsi_cmnd *scmd;
  166. struct scsi_device *sdev;
  167. int total_failures = 0;
  168. int cmd_failed = 0;
  169. int cmd_cancel = 0;
  170. int devices_failed = 0;
  171. shost_for_each_device(sdev, shost) {
  172. list_for_each_entry(scmd, work_q, eh_entry) {
  173. if (scmd->device == sdev) {
  174. ++total_failures;
  175. if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
  176. ++cmd_cancel;
  177. else
  178. ++cmd_failed;
  179. }
  180. }
  181. if (cmd_cancel || cmd_failed) {
  182. SCSI_LOG_ERROR_RECOVERY(3,
  183. sdev_printk(KERN_INFO, sdev,
  184. "%s: cmds failed: %d, cancel: %d\n",
  185. __func__, cmd_failed,
  186. cmd_cancel));
  187. cmd_cancel = 0;
  188. cmd_failed = 0;
  189. ++devices_failed;
  190. }
  191. }
  192. SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
  193. " devices require eh work\n",
  194. total_failures, devices_failed));
  195. }
  196. #endif
  197. /**
  198. * scsi_check_sense - Examine scsi cmd sense
  199. * @scmd: Cmd to have sense checked.
  200. *
  201. * Return value:
  202. * SUCCESS or FAILED or NEEDS_RETRY
  203. *
  204. * Notes:
  205. * When a deferred error is detected the current command has
  206. * not been executed and needs retrying.
  207. */
  208. static int scsi_check_sense(struct scsi_cmnd *scmd)
  209. {
  210. struct scsi_device *sdev = scmd->device;
  211. struct scsi_sense_hdr sshdr;
  212. if (! scsi_command_normalize_sense(scmd, &sshdr))
  213. return FAILED; /* no valid sense data */
  214. if (scsi_sense_is_deferred(&sshdr))
  215. return NEEDS_RETRY;
  216. if (sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh &&
  217. sdev->scsi_dh_data->scsi_dh->check_sense) {
  218. int rc;
  219. rc = sdev->scsi_dh_data->scsi_dh->check_sense(sdev, &sshdr);
  220. if (rc != SCSI_RETURN_NOT_HANDLED)
  221. return rc;
  222. /* handler does not care. Drop down to default handling */
  223. }
  224. /*
  225. * Previous logic looked for FILEMARK, EOM or ILI which are
  226. * mainly associated with tapes and returned SUCCESS.
  227. */
  228. if (sshdr.response_code == 0x70) {
  229. /* fixed format */
  230. if (scmd->sense_buffer[2] & 0xe0)
  231. return SUCCESS;
  232. } else {
  233. /*
  234. * descriptor format: look for "stream commands sense data
  235. * descriptor" (see SSC-3). Assume single sense data
  236. * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
  237. */
  238. if ((sshdr.additional_length > 3) &&
  239. (scmd->sense_buffer[8] == 0x4) &&
  240. (scmd->sense_buffer[11] & 0xe0))
  241. return SUCCESS;
  242. }
  243. switch (sshdr.sense_key) {
  244. case NO_SENSE:
  245. return SUCCESS;
  246. case RECOVERED_ERROR:
  247. return /* soft_error */ SUCCESS;
  248. case ABORTED_COMMAND:
  249. if (sshdr.asc == 0x10) /* DIF */
  250. return SUCCESS;
  251. return NEEDS_RETRY;
  252. case NOT_READY:
  253. case UNIT_ATTENTION:
  254. /*
  255. * if we are expecting a cc/ua because of a bus reset that we
  256. * performed, treat this just as a retry. otherwise this is
  257. * information that we should pass up to the upper-level driver
  258. * so that we can deal with it there.
  259. */
  260. if (scmd->device->expecting_cc_ua) {
  261. scmd->device->expecting_cc_ua = 0;
  262. return NEEDS_RETRY;
  263. }
  264. /*
  265. * if the device is in the process of becoming ready, we
  266. * should retry.
  267. */
  268. if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
  269. return NEEDS_RETRY;
  270. /*
  271. * if the device is not started, we need to wake
  272. * the error handler to start the motor
  273. */
  274. if (scmd->device->allow_restart &&
  275. (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
  276. return FAILED;
  277. return SUCCESS;
  278. /* these three are not supported */
  279. case COPY_ABORTED:
  280. case VOLUME_OVERFLOW:
  281. case MISCOMPARE:
  282. return SUCCESS;
  283. case MEDIUM_ERROR:
  284. if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
  285. sshdr.asc == 0x13 || /* AMNF DATA FIELD */
  286. sshdr.asc == 0x14) { /* RECORD NOT FOUND */
  287. return SUCCESS;
  288. }
  289. return NEEDS_RETRY;
  290. case HARDWARE_ERROR:
  291. if (scmd->device->retry_hwerror)
  292. return ADD_TO_MLQUEUE;
  293. else
  294. return SUCCESS;
  295. case ILLEGAL_REQUEST:
  296. case BLANK_CHECK:
  297. case DATA_PROTECT:
  298. default:
  299. return SUCCESS;
  300. }
  301. }
  302. /**
  303. * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
  304. * @scmd: SCSI cmd to examine.
  305. *
  306. * Notes:
  307. * This is *only* called when we are examining the status of commands
  308. * queued during error recovery. the main difference here is that we
  309. * don't allow for the possibility of retries here, and we are a lot
  310. * more restrictive about what we consider acceptable.
  311. */
  312. static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
  313. {
  314. /*
  315. * first check the host byte, to see if there is anything in there
  316. * that would indicate what we need to do.
  317. */
  318. if (host_byte(scmd->result) == DID_RESET) {
  319. /*
  320. * rats. we are already in the error handler, so we now
  321. * get to try and figure out what to do next. if the sense
  322. * is valid, we have a pretty good idea of what to do.
  323. * if not, we mark it as FAILED.
  324. */
  325. return scsi_check_sense(scmd);
  326. }
  327. if (host_byte(scmd->result) != DID_OK)
  328. return FAILED;
  329. /*
  330. * next, check the message byte.
  331. */
  332. if (msg_byte(scmd->result) != COMMAND_COMPLETE)
  333. return FAILED;
  334. /*
  335. * now, check the status byte to see if this indicates
  336. * anything special.
  337. */
  338. switch (status_byte(scmd->result)) {
  339. case GOOD:
  340. case COMMAND_TERMINATED:
  341. return SUCCESS;
  342. case CHECK_CONDITION:
  343. return scsi_check_sense(scmd);
  344. case CONDITION_GOOD:
  345. case INTERMEDIATE_GOOD:
  346. case INTERMEDIATE_C_GOOD:
  347. /*
  348. * who knows? FIXME(eric)
  349. */
  350. return SUCCESS;
  351. case BUSY:
  352. case QUEUE_FULL:
  353. case RESERVATION_CONFLICT:
  354. default:
  355. return FAILED;
  356. }
  357. return FAILED;
  358. }
  359. /**
  360. * scsi_eh_done - Completion function for error handling.
  361. * @scmd: Cmd that is done.
  362. */
  363. static void scsi_eh_done(struct scsi_cmnd *scmd)
  364. {
  365. struct completion *eh_action;
  366. SCSI_LOG_ERROR_RECOVERY(3,
  367. printk("%s scmd: %p result: %x\n",
  368. __func__, scmd, scmd->result));
  369. eh_action = scmd->device->host->eh_action;
  370. if (eh_action)
  371. complete(eh_action);
  372. }
  373. /**
  374. * scsi_try_host_reset - ask host adapter to reset itself
  375. * @scmd: SCSI cmd to send hsot reset.
  376. */
  377. static int scsi_try_host_reset(struct scsi_cmnd *scmd)
  378. {
  379. unsigned long flags;
  380. int rtn;
  381. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
  382. __func__));
  383. if (!scmd->device->host->hostt->eh_host_reset_handler)
  384. return FAILED;
  385. rtn = scmd->device->host->hostt->eh_host_reset_handler(scmd);
  386. if (rtn == SUCCESS) {
  387. if (!scmd->device->host->hostt->skip_settle_delay)
  388. ssleep(HOST_RESET_SETTLE_TIME);
  389. spin_lock_irqsave(scmd->device->host->host_lock, flags);
  390. scsi_report_bus_reset(scmd->device->host,
  391. scmd_channel(scmd));
  392. spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
  393. }
  394. return rtn;
  395. }
  396. /**
  397. * scsi_try_bus_reset - ask host to perform a bus reset
  398. * @scmd: SCSI cmd to send bus reset.
  399. */
  400. static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
  401. {
  402. unsigned long flags;
  403. int rtn;
  404. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
  405. __func__));
  406. if (!scmd->device->host->hostt->eh_bus_reset_handler)
  407. return FAILED;
  408. rtn = scmd->device->host->hostt->eh_bus_reset_handler(scmd);
  409. if (rtn == SUCCESS) {
  410. if (!scmd->device->host->hostt->skip_settle_delay)
  411. ssleep(BUS_RESET_SETTLE_TIME);
  412. spin_lock_irqsave(scmd->device->host->host_lock, flags);
  413. scsi_report_bus_reset(scmd->device->host,
  414. scmd_channel(scmd));
  415. spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
  416. }
  417. return rtn;
  418. }
  419. static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
  420. {
  421. sdev->was_reset = 1;
  422. sdev->expecting_cc_ua = 1;
  423. }
  424. /**
  425. * scsi_try_target_reset - Ask host to perform a target reset
  426. * @scmd: SCSI cmd used to send a target reset
  427. *
  428. * Notes:
  429. * There is no timeout for this operation. if this operation is
  430. * unreliable for a given host, then the host itself needs to put a
  431. * timer on it, and set the host back to a consistent state prior to
  432. * returning.
  433. */
  434. static int scsi_try_target_reset(struct scsi_cmnd *scmd)
  435. {
  436. unsigned long flags;
  437. int rtn;
  438. if (!scmd->device->host->hostt->eh_target_reset_handler)
  439. return FAILED;
  440. rtn = scmd->device->host->hostt->eh_target_reset_handler(scmd);
  441. if (rtn == SUCCESS) {
  442. spin_lock_irqsave(scmd->device->host->host_lock, flags);
  443. __starget_for_each_device(scsi_target(scmd->device), NULL,
  444. __scsi_report_device_reset);
  445. spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
  446. }
  447. return rtn;
  448. }
  449. /**
  450. * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
  451. * @scmd: SCSI cmd used to send BDR
  452. *
  453. * Notes:
  454. * There is no timeout for this operation. if this operation is
  455. * unreliable for a given host, then the host itself needs to put a
  456. * timer on it, and set the host back to a consistent state prior to
  457. * returning.
  458. */
  459. static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
  460. {
  461. int rtn;
  462. if (!scmd->device->host->hostt->eh_device_reset_handler)
  463. return FAILED;
  464. rtn = scmd->device->host->hostt->eh_device_reset_handler(scmd);
  465. if (rtn == SUCCESS)
  466. __scsi_report_device_reset(scmd->device, NULL);
  467. return rtn;
  468. }
  469. static int __scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
  470. {
  471. if (!scmd->device->host->hostt->eh_abort_handler)
  472. return FAILED;
  473. return scmd->device->host->hostt->eh_abort_handler(scmd);
  474. }
  475. /**
  476. * scsi_try_to_abort_cmd - Ask host to abort a running command.
  477. * @scmd: SCSI cmd to abort from Lower Level.
  478. *
  479. * Notes:
  480. * This function will not return until the user's completion function
  481. * has been called. there is no timeout on this operation. if the
  482. * author of the low-level driver wishes this operation to be timed,
  483. * they can provide this facility themselves. helper functions in
  484. * scsi_error.c can be supplied to make this easier to do.
  485. */
  486. static int scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
  487. {
  488. /*
  489. * scsi_done was called just after the command timed out and before
  490. * we had a chance to process it. (db)
  491. */
  492. if (scmd->serial_number == 0)
  493. return SUCCESS;
  494. return __scsi_try_to_abort_cmd(scmd);
  495. }
  496. static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
  497. {
  498. if (__scsi_try_to_abort_cmd(scmd) != SUCCESS)
  499. if (scsi_try_bus_device_reset(scmd) != SUCCESS)
  500. if (scsi_try_target_reset(scmd) != SUCCESS)
  501. if (scsi_try_bus_reset(scmd) != SUCCESS)
  502. scsi_try_host_reset(scmd);
  503. }
  504. /**
  505. * scsi_eh_prep_cmnd - Save a scsi command info as part of error recory
  506. * @scmd: SCSI command structure to hijack
  507. * @ses: structure to save restore information
  508. * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
  509. * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
  510. * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
  511. *
  512. * This function is used to save a scsi command information before re-execution
  513. * as part of the error recovery process. If @sense_bytes is 0 the command
  514. * sent must be one that does not transfer any data. If @sense_bytes != 0
  515. * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
  516. * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
  517. */
  518. void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
  519. unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
  520. {
  521. struct scsi_device *sdev = scmd->device;
  522. /*
  523. * We need saved copies of a number of fields - this is because
  524. * error handling may need to overwrite these with different values
  525. * to run different commands, and once error handling is complete,
  526. * we will need to restore these values prior to running the actual
  527. * command.
  528. */
  529. ses->cmd_len = scmd->cmd_len;
  530. ses->cmnd = scmd->cmnd;
  531. ses->data_direction = scmd->sc_data_direction;
  532. ses->sdb = scmd->sdb;
  533. ses->next_rq = scmd->request->next_rq;
  534. ses->result = scmd->result;
  535. ses->underflow = scmd->underflow;
  536. ses->prot_op = scmd->prot_op;
  537. scmd->prot_op = SCSI_PROT_NORMAL;
  538. scmd->cmnd = ses->eh_cmnd;
  539. memset(scmd->cmnd, 0, BLK_MAX_CDB);
  540. memset(&scmd->sdb, 0, sizeof(scmd->sdb));
  541. scmd->request->next_rq = NULL;
  542. if (sense_bytes) {
  543. scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
  544. sense_bytes);
  545. sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
  546. scmd->sdb.length);
  547. scmd->sdb.table.sgl = &ses->sense_sgl;
  548. scmd->sc_data_direction = DMA_FROM_DEVICE;
  549. scmd->sdb.table.nents = 1;
  550. scmd->cmnd[0] = REQUEST_SENSE;
  551. scmd->cmnd[4] = scmd->sdb.length;
  552. scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
  553. } else {
  554. scmd->sc_data_direction = DMA_NONE;
  555. if (cmnd) {
  556. BUG_ON(cmnd_size > BLK_MAX_CDB);
  557. memcpy(scmd->cmnd, cmnd, cmnd_size);
  558. scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
  559. }
  560. }
  561. scmd->underflow = 0;
  562. if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
  563. scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
  564. (sdev->lun << 5 & 0xe0);
  565. /*
  566. * Zero the sense buffer. The scsi spec mandates that any
  567. * untransferred sense data should be interpreted as being zero.
  568. */
  569. memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  570. }
  571. EXPORT_SYMBOL(scsi_eh_prep_cmnd);
  572. /**
  573. * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recory
  574. * @scmd: SCSI command structure to restore
  575. * @ses: saved information from a coresponding call to scsi_prep_eh_cmnd
  576. *
  577. * Undo any damage done by above scsi_prep_eh_cmnd().
  578. */
  579. void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
  580. {
  581. /*
  582. * Restore original data
  583. */
  584. scmd->cmd_len = ses->cmd_len;
  585. scmd->cmnd = ses->cmnd;
  586. scmd->sc_data_direction = ses->data_direction;
  587. scmd->sdb = ses->sdb;
  588. scmd->request->next_rq = ses->next_rq;
  589. scmd->result = ses->result;
  590. scmd->underflow = ses->underflow;
  591. scmd->prot_op = ses->prot_op;
  592. }
  593. EXPORT_SYMBOL(scsi_eh_restore_cmnd);
  594. /**
  595. * scsi_send_eh_cmnd - submit a scsi command as part of error recory
  596. * @scmd: SCSI command structure to hijack
  597. * @cmnd: CDB to send
  598. * @cmnd_size: size in bytes of @cmnd
  599. * @timeout: timeout for this request
  600. * @sense_bytes: size of sense data to copy or 0
  601. *
  602. * This function is used to send a scsi command down to a target device
  603. * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
  604. *
  605. * Return value:
  606. * SUCCESS or FAILED or NEEDS_RETRY
  607. */
  608. static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
  609. int cmnd_size, int timeout, unsigned sense_bytes)
  610. {
  611. struct scsi_device *sdev = scmd->device;
  612. struct Scsi_Host *shost = sdev->host;
  613. DECLARE_COMPLETION_ONSTACK(done);
  614. unsigned long timeleft;
  615. unsigned long flags;
  616. struct scsi_eh_save ses;
  617. int rtn;
  618. scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
  619. shost->eh_action = &done;
  620. spin_lock_irqsave(shost->host_lock, flags);
  621. scsi_log_send(scmd);
  622. shost->hostt->queuecommand(scmd, scsi_eh_done);
  623. spin_unlock_irqrestore(shost->host_lock, flags);
  624. timeleft = wait_for_completion_timeout(&done, timeout);
  625. shost->eh_action = NULL;
  626. scsi_log_completion(scmd, SUCCESS);
  627. SCSI_LOG_ERROR_RECOVERY(3,
  628. printk("%s: scmd: %p, timeleft: %ld\n",
  629. __func__, scmd, timeleft));
  630. /*
  631. * If there is time left scsi_eh_done got called, and we will
  632. * examine the actual status codes to see whether the command
  633. * actually did complete normally, else tell the host to forget
  634. * about this command.
  635. */
  636. if (timeleft) {
  637. rtn = scsi_eh_completed_normally(scmd);
  638. SCSI_LOG_ERROR_RECOVERY(3,
  639. printk("%s: scsi_eh_completed_normally %x\n",
  640. __func__, rtn));
  641. switch (rtn) {
  642. case SUCCESS:
  643. case NEEDS_RETRY:
  644. case FAILED:
  645. break;
  646. default:
  647. rtn = FAILED;
  648. break;
  649. }
  650. } else {
  651. scsi_abort_eh_cmnd(scmd);
  652. rtn = FAILED;
  653. }
  654. scsi_eh_restore_cmnd(scmd, &ses);
  655. return rtn;
  656. }
  657. /**
  658. * scsi_request_sense - Request sense data from a particular target.
  659. * @scmd: SCSI cmd for request sense.
  660. *
  661. * Notes:
  662. * Some hosts automatically obtain this information, others require
  663. * that we obtain it on our own. This function will *not* return until
  664. * the command either times out, or it completes.
  665. */
  666. static int scsi_request_sense(struct scsi_cmnd *scmd)
  667. {
  668. return scsi_send_eh_cmnd(scmd, NULL, 0, SENSE_TIMEOUT, ~0);
  669. }
  670. /**
  671. * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
  672. * @scmd: Original SCSI cmd that eh has finished.
  673. * @done_q: Queue for processed commands.
  674. *
  675. * Notes:
  676. * We don't want to use the normal command completion while we are are
  677. * still handling errors - it may cause other commands to be queued,
  678. * and that would disturb what we are doing. Thus we really want to
  679. * keep a list of pending commands for final completion, and once we
  680. * are ready to leave error handling we handle completion for real.
  681. */
  682. void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
  683. {
  684. scmd->device->host->host_failed--;
  685. scmd->eh_eflags = 0;
  686. list_move_tail(&scmd->eh_entry, done_q);
  687. }
  688. EXPORT_SYMBOL(scsi_eh_finish_cmd);
  689. /**
  690. * scsi_eh_get_sense - Get device sense data.
  691. * @work_q: Queue of commands to process.
  692. * @done_q: Queue of processed commands.
  693. *
  694. * Description:
  695. * See if we need to request sense information. if so, then get it
  696. * now, so we have a better idea of what to do.
  697. *
  698. * Notes:
  699. * This has the unfortunate side effect that if a shost adapter does
  700. * not automatically request sense information, we end up shutting
  701. * it down before we request it.
  702. *
  703. * All drivers should request sense information internally these days,
  704. * so for now all I have to say is tough noogies if you end up in here.
  705. *
  706. * XXX: Long term this code should go away, but that needs an audit of
  707. * all LLDDs first.
  708. */
  709. int scsi_eh_get_sense(struct list_head *work_q,
  710. struct list_head *done_q)
  711. {
  712. struct scsi_cmnd *scmd, *next;
  713. int rtn;
  714. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  715. if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
  716. SCSI_SENSE_VALID(scmd))
  717. continue;
  718. SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
  719. "%s: requesting sense\n",
  720. current->comm));
  721. rtn = scsi_request_sense(scmd);
  722. if (rtn != SUCCESS)
  723. continue;
  724. SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
  725. " result %x\n", scmd,
  726. scmd->result));
  727. SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
  728. rtn = scsi_decide_disposition(scmd);
  729. /*
  730. * if the result was normal, then just pass it along to the
  731. * upper level.
  732. */
  733. if (rtn == SUCCESS)
  734. /* we don't want this command reissued, just
  735. * finished with the sense data, so set
  736. * retries to the max allowed to ensure it
  737. * won't get reissued */
  738. scmd->retries = scmd->allowed;
  739. else if (rtn != NEEDS_RETRY)
  740. continue;
  741. scsi_eh_finish_cmd(scmd, done_q);
  742. }
  743. return list_empty(work_q);
  744. }
  745. EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
  746. /**
  747. * scsi_eh_tur - Send TUR to device.
  748. * @scmd: &scsi_cmnd to send TUR
  749. *
  750. * Return value:
  751. * 0 - Device is ready. 1 - Device NOT ready.
  752. */
  753. static int scsi_eh_tur(struct scsi_cmnd *scmd)
  754. {
  755. static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
  756. int retry_cnt = 1, rtn;
  757. retry_tur:
  758. rtn = scsi_send_eh_cmnd(scmd, tur_command, 6, SENSE_TIMEOUT, 0);
  759. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
  760. __func__, scmd, rtn));
  761. switch (rtn) {
  762. case NEEDS_RETRY:
  763. if (retry_cnt--)
  764. goto retry_tur;
  765. /*FALLTHRU*/
  766. case SUCCESS:
  767. return 0;
  768. default:
  769. return 1;
  770. }
  771. }
  772. /**
  773. * scsi_eh_abort_cmds - abort pending commands.
  774. * @work_q: &list_head for pending commands.
  775. * @done_q: &list_head for processed commands.
  776. *
  777. * Decription:
  778. * Try and see whether or not it makes sense to try and abort the
  779. * running command. This only works out to be the case if we have one
  780. * command that has timed out. If the command simply failed, it makes
  781. * no sense to try and abort the command, since as far as the shost
  782. * adapter is concerned, it isn't running.
  783. */
  784. static int scsi_eh_abort_cmds(struct list_head *work_q,
  785. struct list_head *done_q)
  786. {
  787. struct scsi_cmnd *scmd, *next;
  788. int rtn;
  789. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  790. if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
  791. continue;
  792. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
  793. "0x%p\n", current->comm,
  794. scmd));
  795. rtn = scsi_try_to_abort_cmd(scmd);
  796. if (rtn == SUCCESS) {
  797. scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
  798. if (!scsi_device_online(scmd->device) ||
  799. !scsi_eh_tur(scmd)) {
  800. scsi_eh_finish_cmd(scmd, done_q);
  801. }
  802. } else
  803. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
  804. " cmd failed:"
  805. "0x%p\n",
  806. current->comm,
  807. scmd));
  808. }
  809. return list_empty(work_q);
  810. }
  811. /**
  812. * scsi_eh_try_stu - Send START_UNIT to device.
  813. * @scmd: &scsi_cmnd to send START_UNIT
  814. *
  815. * Return value:
  816. * 0 - Device is ready. 1 - Device NOT ready.
  817. */
  818. static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
  819. {
  820. static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
  821. if (scmd->device->allow_restart) {
  822. int i, rtn = NEEDS_RETRY;
  823. for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
  824. rtn = scsi_send_eh_cmnd(scmd, stu_command, 6,
  825. scmd->device->timeout, 0);
  826. if (rtn == SUCCESS)
  827. return 0;
  828. }
  829. return 1;
  830. }
  831. /**
  832. * scsi_eh_stu - send START_UNIT if needed
  833. * @shost: &scsi host being recovered.
  834. * @work_q: &list_head for pending commands.
  835. * @done_q: &list_head for processed commands.
  836. *
  837. * Notes:
  838. * If commands are failing due to not ready, initializing command required,
  839. * try revalidating the device, which will end up sending a start unit.
  840. */
  841. static int scsi_eh_stu(struct Scsi_Host *shost,
  842. struct list_head *work_q,
  843. struct list_head *done_q)
  844. {
  845. struct scsi_cmnd *scmd, *stu_scmd, *next;
  846. struct scsi_device *sdev;
  847. shost_for_each_device(sdev, shost) {
  848. stu_scmd = NULL;
  849. list_for_each_entry(scmd, work_q, eh_entry)
  850. if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
  851. scsi_check_sense(scmd) == FAILED ) {
  852. stu_scmd = scmd;
  853. break;
  854. }
  855. if (!stu_scmd)
  856. continue;
  857. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
  858. " 0x%p\n", current->comm, sdev));
  859. if (!scsi_eh_try_stu(stu_scmd)) {
  860. if (!scsi_device_online(sdev) ||
  861. !scsi_eh_tur(stu_scmd)) {
  862. list_for_each_entry_safe(scmd, next,
  863. work_q, eh_entry) {
  864. if (scmd->device == sdev)
  865. scsi_eh_finish_cmd(scmd, done_q);
  866. }
  867. }
  868. } else {
  869. SCSI_LOG_ERROR_RECOVERY(3,
  870. printk("%s: START_UNIT failed to sdev:"
  871. " 0x%p\n", current->comm, sdev));
  872. }
  873. }
  874. return list_empty(work_q);
  875. }
  876. /**
  877. * scsi_eh_bus_device_reset - send bdr if needed
  878. * @shost: scsi host being recovered.
  879. * @work_q: &list_head for pending commands.
  880. * @done_q: &list_head for processed commands.
  881. *
  882. * Notes:
  883. * Try a bus device reset. Still, look to see whether we have multiple
  884. * devices that are jammed or not - if we have multiple devices, it
  885. * makes no sense to try bus_device_reset - we really would need to try
  886. * a bus_reset instead.
  887. */
  888. static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
  889. struct list_head *work_q,
  890. struct list_head *done_q)
  891. {
  892. struct scsi_cmnd *scmd, *bdr_scmd, *next;
  893. struct scsi_device *sdev;
  894. int rtn;
  895. shost_for_each_device(sdev, shost) {
  896. bdr_scmd = NULL;
  897. list_for_each_entry(scmd, work_q, eh_entry)
  898. if (scmd->device == sdev) {
  899. bdr_scmd = scmd;
  900. break;
  901. }
  902. if (!bdr_scmd)
  903. continue;
  904. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
  905. " 0x%p\n", current->comm,
  906. sdev));
  907. rtn = scsi_try_bus_device_reset(bdr_scmd);
  908. if (rtn == SUCCESS) {
  909. if (!scsi_device_online(sdev) ||
  910. !scsi_eh_tur(bdr_scmd)) {
  911. list_for_each_entry_safe(scmd, next,
  912. work_q, eh_entry) {
  913. if (scmd->device == sdev)
  914. scsi_eh_finish_cmd(scmd,
  915. done_q);
  916. }
  917. }
  918. } else {
  919. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
  920. " failed sdev:"
  921. "0x%p\n",
  922. current->comm,
  923. sdev));
  924. }
  925. }
  926. return list_empty(work_q);
  927. }
  928. /**
  929. * scsi_eh_target_reset - send target reset if needed
  930. * @shost: scsi host being recovered.
  931. * @work_q: &list_head for pending commands.
  932. * @done_q: &list_head for processed commands.
  933. *
  934. * Notes:
  935. * Try a target reset.
  936. */
  937. static int scsi_eh_target_reset(struct Scsi_Host *shost,
  938. struct list_head *work_q,
  939. struct list_head *done_q)
  940. {
  941. struct scsi_cmnd *scmd, *tgtr_scmd, *next;
  942. unsigned int id;
  943. int rtn;
  944. for (id = 0; id <= shost->max_id; id++) {
  945. tgtr_scmd = NULL;
  946. list_for_each_entry(scmd, work_q, eh_entry) {
  947. if (id == scmd_id(scmd)) {
  948. tgtr_scmd = scmd;
  949. break;
  950. }
  951. }
  952. if (!tgtr_scmd)
  953. continue;
  954. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending target reset "
  955. "to target %d\n",
  956. current->comm, id));
  957. rtn = scsi_try_target_reset(tgtr_scmd);
  958. if (rtn == SUCCESS) {
  959. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  960. if (id == scmd_id(scmd))
  961. if (!scsi_device_online(scmd->device) ||
  962. !scsi_eh_tur(tgtr_scmd))
  963. scsi_eh_finish_cmd(scmd,
  964. done_q);
  965. }
  966. } else
  967. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Target reset"
  968. " failed target: "
  969. "%d\n",
  970. current->comm, id));
  971. }
  972. return list_empty(work_q);
  973. }
  974. /**
  975. * scsi_eh_bus_reset - send a bus reset
  976. * @shost: &scsi host being recovered.
  977. * @work_q: &list_head for pending commands.
  978. * @done_q: &list_head for processed commands.
  979. */
  980. static int scsi_eh_bus_reset(struct Scsi_Host *shost,
  981. struct list_head *work_q,
  982. struct list_head *done_q)
  983. {
  984. struct scsi_cmnd *scmd, *chan_scmd, *next;
  985. unsigned int channel;
  986. int rtn;
  987. /*
  988. * we really want to loop over the various channels, and do this on
  989. * a channel by channel basis. we should also check to see if any
  990. * of the failed commands are on soft_reset devices, and if so, skip
  991. * the reset.
  992. */
  993. for (channel = 0; channel <= shost->max_channel; channel++) {
  994. chan_scmd = NULL;
  995. list_for_each_entry(scmd, work_q, eh_entry) {
  996. if (channel == scmd_channel(scmd)) {
  997. chan_scmd = scmd;
  998. break;
  999. /*
  1000. * FIXME add back in some support for
  1001. * soft_reset devices.
  1002. */
  1003. }
  1004. }
  1005. if (!chan_scmd)
  1006. continue;
  1007. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
  1008. " %d\n", current->comm,
  1009. channel));
  1010. rtn = scsi_try_bus_reset(chan_scmd);
  1011. if (rtn == SUCCESS) {
  1012. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1013. if (channel == scmd_channel(scmd))
  1014. if (!scsi_device_online(scmd->device) ||
  1015. !scsi_eh_tur(scmd))
  1016. scsi_eh_finish_cmd(scmd,
  1017. done_q);
  1018. }
  1019. } else {
  1020. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
  1021. " failed chan: %d\n",
  1022. current->comm,
  1023. channel));
  1024. }
  1025. }
  1026. return list_empty(work_q);
  1027. }
  1028. /**
  1029. * scsi_eh_host_reset - send a host reset
  1030. * @work_q: list_head for processed commands.
  1031. * @done_q: list_head for processed commands.
  1032. */
  1033. static int scsi_eh_host_reset(struct list_head *work_q,
  1034. struct list_head *done_q)
  1035. {
  1036. struct scsi_cmnd *scmd, *next;
  1037. int rtn;
  1038. if (!list_empty(work_q)) {
  1039. scmd = list_entry(work_q->next,
  1040. struct scsi_cmnd, eh_entry);
  1041. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
  1042. , current->comm));
  1043. rtn = scsi_try_host_reset(scmd);
  1044. if (rtn == SUCCESS) {
  1045. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1046. if (!scsi_device_online(scmd->device) ||
  1047. (!scsi_eh_try_stu(scmd) && !scsi_eh_tur(scmd)) ||
  1048. !scsi_eh_tur(scmd))
  1049. scsi_eh_finish_cmd(scmd, done_q);
  1050. }
  1051. } else {
  1052. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
  1053. " failed\n",
  1054. current->comm));
  1055. }
  1056. }
  1057. return list_empty(work_q);
  1058. }
  1059. /**
  1060. * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
  1061. * @work_q: list_head for processed commands.
  1062. * @done_q: list_head for processed commands.
  1063. */
  1064. static void scsi_eh_offline_sdevs(struct list_head *work_q,
  1065. struct list_head *done_q)
  1066. {
  1067. struct scsi_cmnd *scmd, *next;
  1068. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1069. sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
  1070. "not ready after error recovery\n");
  1071. scsi_device_set_state(scmd->device, SDEV_OFFLINE);
  1072. if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
  1073. /*
  1074. * FIXME: Handle lost cmds.
  1075. */
  1076. }
  1077. scsi_eh_finish_cmd(scmd, done_q);
  1078. }
  1079. return;
  1080. }
  1081. /**
  1082. * scsi_decide_disposition - Disposition a cmd on return from LLD.
  1083. * @scmd: SCSI cmd to examine.
  1084. *
  1085. * Notes:
  1086. * This is *only* called when we are examining the status after sending
  1087. * out the actual data command. any commands that are queued for error
  1088. * recovery (e.g. test_unit_ready) do *not* come through here.
  1089. *
  1090. * When this routine returns failed, it means the error handler thread
  1091. * is woken. In cases where the error code indicates an error that
  1092. * doesn't require the error handler read (i.e. we don't need to
  1093. * abort/reset), this function should return SUCCESS.
  1094. */
  1095. int scsi_decide_disposition(struct scsi_cmnd *scmd)
  1096. {
  1097. int rtn;
  1098. /*
  1099. * if the device is offline, then we clearly just pass the result back
  1100. * up to the top level.
  1101. */
  1102. if (!scsi_device_online(scmd->device)) {
  1103. SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
  1104. " as SUCCESS\n",
  1105. __func__));
  1106. return SUCCESS;
  1107. }
  1108. /*
  1109. * first check the host byte, to see if there is anything in there
  1110. * that would indicate what we need to do.
  1111. */
  1112. switch (host_byte(scmd->result)) {
  1113. case DID_PASSTHROUGH:
  1114. /*
  1115. * no matter what, pass this through to the upper layer.
  1116. * nuke this special code so that it looks like we are saying
  1117. * did_ok.
  1118. */
  1119. scmd->result &= 0xff00ffff;
  1120. return SUCCESS;
  1121. case DID_OK:
  1122. /*
  1123. * looks good. drop through, and check the next byte.
  1124. */
  1125. break;
  1126. case DID_NO_CONNECT:
  1127. case DID_BAD_TARGET:
  1128. case DID_ABORT:
  1129. /*
  1130. * note - this means that we just report the status back
  1131. * to the top level driver, not that we actually think
  1132. * that it indicates SUCCESS.
  1133. */
  1134. return SUCCESS;
  1135. /*
  1136. * when the low level driver returns did_soft_error,
  1137. * it is responsible for keeping an internal retry counter
  1138. * in order to avoid endless loops (db)
  1139. *
  1140. * actually this is a bug in this function here. we should
  1141. * be mindful of the maximum number of retries specified
  1142. * and not get stuck in a loop.
  1143. */
  1144. case DID_SOFT_ERROR:
  1145. goto maybe_retry;
  1146. case DID_IMM_RETRY:
  1147. return NEEDS_RETRY;
  1148. case DID_REQUEUE:
  1149. return ADD_TO_MLQUEUE;
  1150. case DID_ERROR:
  1151. if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
  1152. status_byte(scmd->result) == RESERVATION_CONFLICT)
  1153. /*
  1154. * execute reservation conflict processing code
  1155. * lower down
  1156. */
  1157. break;
  1158. /* fallthrough */
  1159. case DID_BUS_BUSY:
  1160. case DID_PARITY:
  1161. goto maybe_retry;
  1162. case DID_TIME_OUT:
  1163. /*
  1164. * when we scan the bus, we get timeout messages for
  1165. * these commands if there is no device available.
  1166. * other hosts report did_no_connect for the same thing.
  1167. */
  1168. if ((scmd->cmnd[0] == TEST_UNIT_READY ||
  1169. scmd->cmnd[0] == INQUIRY)) {
  1170. return SUCCESS;
  1171. } else {
  1172. return FAILED;
  1173. }
  1174. case DID_RESET:
  1175. return SUCCESS;
  1176. default:
  1177. return FAILED;
  1178. }
  1179. /*
  1180. * next, check the message byte.
  1181. */
  1182. if (msg_byte(scmd->result) != COMMAND_COMPLETE)
  1183. return FAILED;
  1184. /*
  1185. * check the status byte to see if this indicates anything special.
  1186. */
  1187. switch (status_byte(scmd->result)) {
  1188. case QUEUE_FULL:
  1189. /*
  1190. * the case of trying to send too many commands to a
  1191. * tagged queueing device.
  1192. */
  1193. case BUSY:
  1194. /*
  1195. * device can't talk to us at the moment. Should only
  1196. * occur (SAM-3) when the task queue is empty, so will cause
  1197. * the empty queue handling to trigger a stall in the
  1198. * device.
  1199. */
  1200. return ADD_TO_MLQUEUE;
  1201. case GOOD:
  1202. case COMMAND_TERMINATED:
  1203. case TASK_ABORTED:
  1204. return SUCCESS;
  1205. case CHECK_CONDITION:
  1206. rtn = scsi_check_sense(scmd);
  1207. if (rtn == NEEDS_RETRY)
  1208. goto maybe_retry;
  1209. /* if rtn == FAILED, we have no sense information;
  1210. * returning FAILED will wake the error handler thread
  1211. * to collect the sense and redo the decide
  1212. * disposition */
  1213. return rtn;
  1214. case CONDITION_GOOD:
  1215. case INTERMEDIATE_GOOD:
  1216. case INTERMEDIATE_C_GOOD:
  1217. case ACA_ACTIVE:
  1218. /*
  1219. * who knows? FIXME(eric)
  1220. */
  1221. return SUCCESS;
  1222. case RESERVATION_CONFLICT:
  1223. sdev_printk(KERN_INFO, scmd->device,
  1224. "reservation conflict\n");
  1225. return SUCCESS; /* causes immediate i/o error */
  1226. default:
  1227. return FAILED;
  1228. }
  1229. return FAILED;
  1230. maybe_retry:
  1231. /* we requeue for retry because the error was retryable, and
  1232. * the request was not marked fast fail. Note that above,
  1233. * even if the request is marked fast fail, we still requeue
  1234. * for queue congestion conditions (QUEUE_FULL or BUSY) */
  1235. if ((++scmd->retries) <= scmd->allowed
  1236. && !blk_noretry_request(scmd->request)) {
  1237. return NEEDS_RETRY;
  1238. } else {
  1239. /*
  1240. * no more retries - report this one back to upper level.
  1241. */
  1242. return SUCCESS;
  1243. }
  1244. }
  1245. /**
  1246. * scsi_eh_lock_door - Prevent medium removal for the specified device
  1247. * @sdev: SCSI device to prevent medium removal
  1248. *
  1249. * Locking:
  1250. * We must be called from process context; scsi_allocate_request()
  1251. * may sleep.
  1252. *
  1253. * Notes:
  1254. * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
  1255. * head of the devices request queue, and continue.
  1256. *
  1257. * Bugs:
  1258. * scsi_allocate_request() may sleep waiting for existing requests to
  1259. * be processed. However, since we haven't kicked off any request
  1260. * processing for this host, this may deadlock.
  1261. *
  1262. * If scsi_allocate_request() fails for what ever reason, we
  1263. * completely forget to lock the door.
  1264. */
  1265. static void scsi_eh_lock_door(struct scsi_device *sdev)
  1266. {
  1267. unsigned char cmnd[MAX_COMMAND_SIZE];
  1268. cmnd[0] = ALLOW_MEDIUM_REMOVAL;
  1269. cmnd[1] = 0;
  1270. cmnd[2] = 0;
  1271. cmnd[3] = 0;
  1272. cmnd[4] = SCSI_REMOVAL_PREVENT;
  1273. cmnd[5] = 0;
  1274. scsi_execute_async(sdev, cmnd, 6, DMA_NONE, NULL, 0, 0, 10 * HZ,
  1275. 5, NULL, NULL, GFP_KERNEL);
  1276. }
  1277. /**
  1278. * scsi_restart_operations - restart io operations to the specified host.
  1279. * @shost: Host we are restarting.
  1280. *
  1281. * Notes:
  1282. * When we entered the error handler, we blocked all further i/o to
  1283. * this device. we need to 'reverse' this process.
  1284. */
  1285. static void scsi_restart_operations(struct Scsi_Host *shost)
  1286. {
  1287. struct scsi_device *sdev;
  1288. unsigned long flags;
  1289. /*
  1290. * If the door was locked, we need to insert a door lock request
  1291. * onto the head of the SCSI request queue for the device. There
  1292. * is no point trying to lock the door of an off-line device.
  1293. */
  1294. shost_for_each_device(sdev, shost) {
  1295. if (scsi_device_online(sdev) && sdev->locked)
  1296. scsi_eh_lock_door(sdev);
  1297. }
  1298. /*
  1299. * next free up anything directly waiting upon the host. this
  1300. * will be requests for character device operations, and also for
  1301. * ioctls to queued block devices.
  1302. */
  1303. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: waking up host to restart\n",
  1304. __func__));
  1305. spin_lock_irqsave(shost->host_lock, flags);
  1306. if (scsi_host_set_state(shost, SHOST_RUNNING))
  1307. if (scsi_host_set_state(shost, SHOST_CANCEL))
  1308. BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
  1309. spin_unlock_irqrestore(shost->host_lock, flags);
  1310. wake_up(&shost->host_wait);
  1311. /*
  1312. * finally we need to re-initiate requests that may be pending. we will
  1313. * have had everything blocked while error handling is taking place, and
  1314. * now that error recovery is done, we will need to ensure that these
  1315. * requests are started.
  1316. */
  1317. scsi_run_host_queues(shost);
  1318. }
  1319. /**
  1320. * scsi_eh_ready_devs - check device ready state and recover if not.
  1321. * @shost: host to be recovered.
  1322. * @work_q: &list_head for pending commands.
  1323. * @done_q: &list_head for processed commands.
  1324. */
  1325. void scsi_eh_ready_devs(struct Scsi_Host *shost,
  1326. struct list_head *work_q,
  1327. struct list_head *done_q)
  1328. {
  1329. if (!scsi_eh_stu(shost, work_q, done_q))
  1330. if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
  1331. if (!scsi_eh_target_reset(shost, work_q, done_q))
  1332. if (!scsi_eh_bus_reset(shost, work_q, done_q))
  1333. if (!scsi_eh_host_reset(work_q, done_q))
  1334. scsi_eh_offline_sdevs(work_q,
  1335. done_q);
  1336. }
  1337. EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
  1338. /**
  1339. * scsi_eh_flush_done_q - finish processed commands or retry them.
  1340. * @done_q: list_head of processed commands.
  1341. */
  1342. void scsi_eh_flush_done_q(struct list_head *done_q)
  1343. {
  1344. struct scsi_cmnd *scmd, *next;
  1345. list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
  1346. list_del_init(&scmd->eh_entry);
  1347. if (scsi_device_online(scmd->device) &&
  1348. !blk_noretry_request(scmd->request) &&
  1349. (++scmd->retries <= scmd->allowed)) {
  1350. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
  1351. " retry cmd: %p\n",
  1352. current->comm,
  1353. scmd));
  1354. scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
  1355. } else {
  1356. /*
  1357. * If just we got sense for the device (called
  1358. * scsi_eh_get_sense), scmd->result is already
  1359. * set, do not set DRIVER_TIMEOUT.
  1360. */
  1361. if (!scmd->result)
  1362. scmd->result |= (DRIVER_TIMEOUT << 24);
  1363. SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
  1364. " cmd: %p\n",
  1365. current->comm, scmd));
  1366. scsi_finish_command(scmd);
  1367. }
  1368. }
  1369. }
  1370. EXPORT_SYMBOL(scsi_eh_flush_done_q);
  1371. /**
  1372. * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
  1373. * @shost: Host to unjam.
  1374. *
  1375. * Notes:
  1376. * When we come in here, we *know* that all commands on the bus have
  1377. * either completed, failed or timed out. we also know that no further
  1378. * commands are being sent to the host, so things are relatively quiet
  1379. * and we have freedom to fiddle with things as we wish.
  1380. *
  1381. * This is only the *default* implementation. it is possible for
  1382. * individual drivers to supply their own version of this function, and
  1383. * if the maintainer wishes to do this, it is strongly suggested that
  1384. * this function be taken as a template and modified. this function
  1385. * was designed to correctly handle problems for about 95% of the
  1386. * different cases out there, and it should always provide at least a
  1387. * reasonable amount of error recovery.
  1388. *
  1389. * Any command marked 'failed' or 'timeout' must eventually have
  1390. * scsi_finish_cmd() called for it. we do all of the retry stuff
  1391. * here, so when we restart the host after we return it should have an
  1392. * empty queue.
  1393. */
  1394. static void scsi_unjam_host(struct Scsi_Host *shost)
  1395. {
  1396. unsigned long flags;
  1397. LIST_HEAD(eh_work_q);
  1398. LIST_HEAD(eh_done_q);
  1399. spin_lock_irqsave(shost->host_lock, flags);
  1400. list_splice_init(&shost->eh_cmd_q, &eh_work_q);
  1401. spin_unlock_irqrestore(shost->host_lock, flags);
  1402. SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
  1403. if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
  1404. if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
  1405. scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
  1406. scsi_eh_flush_done_q(&eh_done_q);
  1407. }
  1408. /**
  1409. * scsi_error_handler - SCSI error handler thread
  1410. * @data: Host for which we are running.
  1411. *
  1412. * Notes:
  1413. * This is the main error handling loop. This is run as a kernel thread
  1414. * for every SCSI host and handles all error handling activity.
  1415. */
  1416. int scsi_error_handler(void *data)
  1417. {
  1418. struct Scsi_Host *shost = data;
  1419. /*
  1420. * We use TASK_INTERRUPTIBLE so that the thread is not
  1421. * counted against the load average as a running process.
  1422. * We never actually get interrupted because kthread_run
  1423. * disables singal delivery for the created thread.
  1424. */
  1425. set_current_state(TASK_INTERRUPTIBLE);
  1426. while (!kthread_should_stop()) {
  1427. if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
  1428. shost->host_failed != shost->host_busy) {
  1429. SCSI_LOG_ERROR_RECOVERY(1,
  1430. printk("Error handler scsi_eh_%d sleeping\n",
  1431. shost->host_no));
  1432. schedule();
  1433. set_current_state(TASK_INTERRUPTIBLE);
  1434. continue;
  1435. }
  1436. __set_current_state(TASK_RUNNING);
  1437. SCSI_LOG_ERROR_RECOVERY(1,
  1438. printk("Error handler scsi_eh_%d waking up\n",
  1439. shost->host_no));
  1440. /*
  1441. * We have a host that is failing for some reason. Figure out
  1442. * what we need to do to get it up and online again (if we can).
  1443. * If we fail, we end up taking the thing offline.
  1444. */
  1445. if (shost->transportt->eh_strategy_handler)
  1446. shost->transportt->eh_strategy_handler(shost);
  1447. else
  1448. scsi_unjam_host(shost);
  1449. /*
  1450. * Note - if the above fails completely, the action is to take
  1451. * individual devices offline and flush the queue of any
  1452. * outstanding requests that may have been pending. When we
  1453. * restart, we restart any I/O to any other devices on the bus
  1454. * which are still online.
  1455. */
  1456. scsi_restart_operations(shost);
  1457. set_current_state(TASK_INTERRUPTIBLE);
  1458. }
  1459. __set_current_state(TASK_RUNNING);
  1460. SCSI_LOG_ERROR_RECOVERY(1,
  1461. printk("Error handler scsi_eh_%d exiting\n", shost->host_no));
  1462. shost->ehandler = NULL;
  1463. return 0;
  1464. }
  1465. /*
  1466. * Function: scsi_report_bus_reset()
  1467. *
  1468. * Purpose: Utility function used by low-level drivers to report that
  1469. * they have observed a bus reset on the bus being handled.
  1470. *
  1471. * Arguments: shost - Host in question
  1472. * channel - channel on which reset was observed.
  1473. *
  1474. * Returns: Nothing
  1475. *
  1476. * Lock status: Host lock must be held.
  1477. *
  1478. * Notes: This only needs to be called if the reset is one which
  1479. * originates from an unknown location. Resets originated
  1480. * by the mid-level itself don't need to call this, but there
  1481. * should be no harm.
  1482. *
  1483. * The main purpose of this is to make sure that a CHECK_CONDITION
  1484. * is properly treated.
  1485. */
  1486. void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
  1487. {
  1488. struct scsi_device *sdev;
  1489. __shost_for_each_device(sdev, shost) {
  1490. if (channel == sdev_channel(sdev))
  1491. __scsi_report_device_reset(sdev, NULL);
  1492. }
  1493. }
  1494. EXPORT_SYMBOL(scsi_report_bus_reset);
  1495. /*
  1496. * Function: scsi_report_device_reset()
  1497. *
  1498. * Purpose: Utility function used by low-level drivers to report that
  1499. * they have observed a device reset on the device being handled.
  1500. *
  1501. * Arguments: shost - Host in question
  1502. * channel - channel on which reset was observed
  1503. * target - target on which reset was observed
  1504. *
  1505. * Returns: Nothing
  1506. *
  1507. * Lock status: Host lock must be held
  1508. *
  1509. * Notes: This only needs to be called if the reset is one which
  1510. * originates from an unknown location. Resets originated
  1511. * by the mid-level itself don't need to call this, but there
  1512. * should be no harm.
  1513. *
  1514. * The main purpose of this is to make sure that a CHECK_CONDITION
  1515. * is properly treated.
  1516. */
  1517. void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
  1518. {
  1519. struct scsi_device *sdev;
  1520. __shost_for_each_device(sdev, shost) {
  1521. if (channel == sdev_channel(sdev) &&
  1522. target == sdev_id(sdev))
  1523. __scsi_report_device_reset(sdev, NULL);
  1524. }
  1525. }
  1526. EXPORT_SYMBOL(scsi_report_device_reset);
  1527. static void
  1528. scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
  1529. {
  1530. }
  1531. /*
  1532. * Function: scsi_reset_provider
  1533. *
  1534. * Purpose: Send requested reset to a bus or device at any phase.
  1535. *
  1536. * Arguments: device - device to send reset to
  1537. * flag - reset type (see scsi.h)
  1538. *
  1539. * Returns: SUCCESS/FAILURE.
  1540. *
  1541. * Notes: This is used by the SCSI Generic driver to provide
  1542. * Bus/Device reset capability.
  1543. */
  1544. int
  1545. scsi_reset_provider(struct scsi_device *dev, int flag)
  1546. {
  1547. struct scsi_cmnd *scmd = scsi_get_command(dev, GFP_KERNEL);
  1548. struct Scsi_Host *shost = dev->host;
  1549. struct request req;
  1550. unsigned long flags;
  1551. int rtn;
  1552. blk_rq_init(NULL, &req);
  1553. scmd->request = &req;
  1554. scmd->cmnd = req.cmd;
  1555. scmd->scsi_done = scsi_reset_provider_done_command;
  1556. memset(&scmd->sdb, 0, sizeof(scmd->sdb));
  1557. scmd->cmd_len = 0;
  1558. scmd->sc_data_direction = DMA_BIDIRECTIONAL;
  1559. spin_lock_irqsave(shost->host_lock, flags);
  1560. shost->tmf_in_progress = 1;
  1561. spin_unlock_irqrestore(shost->host_lock, flags);
  1562. switch (flag) {
  1563. case SCSI_TRY_RESET_DEVICE:
  1564. rtn = scsi_try_bus_device_reset(scmd);
  1565. if (rtn == SUCCESS)
  1566. break;
  1567. /* FALLTHROUGH */
  1568. case SCSI_TRY_RESET_TARGET:
  1569. rtn = scsi_try_target_reset(scmd);
  1570. if (rtn == SUCCESS)
  1571. break;
  1572. /* FALLTHROUGH */
  1573. case SCSI_TRY_RESET_BUS:
  1574. rtn = scsi_try_bus_reset(scmd);
  1575. if (rtn == SUCCESS)
  1576. break;
  1577. /* FALLTHROUGH */
  1578. case SCSI_TRY_RESET_HOST:
  1579. rtn = scsi_try_host_reset(scmd);
  1580. break;
  1581. default:
  1582. rtn = FAILED;
  1583. }
  1584. spin_lock_irqsave(shost->host_lock, flags);
  1585. shost->tmf_in_progress = 0;
  1586. spin_unlock_irqrestore(shost->host_lock, flags);
  1587. /*
  1588. * be sure to wake up anyone who was sleeping or had their queue
  1589. * suspended while we performed the TMF.
  1590. */
  1591. SCSI_LOG_ERROR_RECOVERY(3,
  1592. printk("%s: waking up host to restart after TMF\n",
  1593. __func__));
  1594. wake_up(&shost->host_wait);
  1595. scsi_run_host_queues(shost);
  1596. scsi_next_command(scmd);
  1597. return rtn;
  1598. }
  1599. EXPORT_SYMBOL(scsi_reset_provider);
  1600. /**
  1601. * scsi_normalize_sense - normalize main elements from either fixed or
  1602. * descriptor sense data format into a common format.
  1603. *
  1604. * @sense_buffer: byte array containing sense data returned by device
  1605. * @sb_len: number of valid bytes in sense_buffer
  1606. * @sshdr: pointer to instance of structure that common
  1607. * elements are written to.
  1608. *
  1609. * Notes:
  1610. * The "main elements" from sense data are: response_code, sense_key,
  1611. * asc, ascq and additional_length (only for descriptor format).
  1612. *
  1613. * Typically this function can be called after a device has
  1614. * responded to a SCSI command with the CHECK_CONDITION status.
  1615. *
  1616. * Return value:
  1617. * 1 if valid sense data information found, else 0;
  1618. */
  1619. int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
  1620. struct scsi_sense_hdr *sshdr)
  1621. {
  1622. if (!sense_buffer || !sb_len)
  1623. return 0;
  1624. memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
  1625. sshdr->response_code = (sense_buffer[0] & 0x7f);
  1626. if (!scsi_sense_valid(sshdr))
  1627. return 0;
  1628. if (sshdr->response_code >= 0x72) {
  1629. /*
  1630. * descriptor format
  1631. */
  1632. if (sb_len > 1)
  1633. sshdr->sense_key = (sense_buffer[1] & 0xf);
  1634. if (sb_len > 2)
  1635. sshdr->asc = sense_buffer[2];
  1636. if (sb_len > 3)
  1637. sshdr->ascq = sense_buffer[3];
  1638. if (sb_len > 7)
  1639. sshdr->additional_length = sense_buffer[7];
  1640. } else {
  1641. /*
  1642. * fixed format
  1643. */
  1644. if (sb_len > 2)
  1645. sshdr->sense_key = (sense_buffer[2] & 0xf);
  1646. if (sb_len > 7) {
  1647. sb_len = (sb_len < (sense_buffer[7] + 8)) ?
  1648. sb_len : (sense_buffer[7] + 8);
  1649. if (sb_len > 12)
  1650. sshdr->asc = sense_buffer[12];
  1651. if (sb_len > 13)
  1652. sshdr->ascq = sense_buffer[13];
  1653. }
  1654. }
  1655. return 1;
  1656. }
  1657. EXPORT_SYMBOL(scsi_normalize_sense);
  1658. int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
  1659. struct scsi_sense_hdr *sshdr)
  1660. {
  1661. return scsi_normalize_sense(cmd->sense_buffer,
  1662. SCSI_SENSE_BUFFERSIZE, sshdr);
  1663. }
  1664. EXPORT_SYMBOL(scsi_command_normalize_sense);
  1665. /**
  1666. * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
  1667. * @sense_buffer: byte array of descriptor format sense data
  1668. * @sb_len: number of valid bytes in sense_buffer
  1669. * @desc_type: value of descriptor type to find
  1670. * (e.g. 0 -> information)
  1671. *
  1672. * Notes:
  1673. * only valid when sense data is in descriptor format
  1674. *
  1675. * Return value:
  1676. * pointer to start of (first) descriptor if found else NULL
  1677. */
  1678. const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
  1679. int desc_type)
  1680. {
  1681. int add_sen_len, add_len, desc_len, k;
  1682. const u8 * descp;
  1683. if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
  1684. return NULL;
  1685. if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
  1686. return NULL;
  1687. add_sen_len = (add_sen_len < (sb_len - 8)) ?
  1688. add_sen_len : (sb_len - 8);
  1689. descp = &sense_buffer[8];
  1690. for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
  1691. descp += desc_len;
  1692. add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
  1693. desc_len = add_len + 2;
  1694. if (descp[0] == desc_type)
  1695. return descp;
  1696. if (add_len < 0) // short descriptor ??
  1697. break;
  1698. }
  1699. return NULL;
  1700. }
  1701. EXPORT_SYMBOL(scsi_sense_desc_find);
  1702. /**
  1703. * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
  1704. * @sense_buffer: byte array of sense data
  1705. * @sb_len: number of valid bytes in sense_buffer
  1706. * @info_out: pointer to 64 integer where 8 or 4 byte information
  1707. * field will be placed if found.
  1708. *
  1709. * Return value:
  1710. * 1 if information field found, 0 if not found.
  1711. */
  1712. int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
  1713. u64 * info_out)
  1714. {
  1715. int j;
  1716. const u8 * ucp;
  1717. u64 ull;
  1718. if (sb_len < 7)
  1719. return 0;
  1720. switch (sense_buffer[0] & 0x7f) {
  1721. case 0x70:
  1722. case 0x71:
  1723. if (sense_buffer[0] & 0x80) {
  1724. *info_out = (sense_buffer[3] << 24) +
  1725. (sense_buffer[4] << 16) +
  1726. (sense_buffer[5] << 8) + sense_buffer[6];
  1727. return 1;
  1728. } else
  1729. return 0;
  1730. case 0x72:
  1731. case 0x73:
  1732. ucp = scsi_sense_desc_find(sense_buffer, sb_len,
  1733. 0 /* info desc */);
  1734. if (ucp && (0xa == ucp[1])) {
  1735. ull = 0;
  1736. for (j = 0; j < 8; ++j) {
  1737. if (j > 0)
  1738. ull <<= 8;
  1739. ull |= ucp[4 + j];
  1740. }
  1741. *info_out = ull;
  1742. return 1;
  1743. } else
  1744. return 0;
  1745. default:
  1746. return 0;
  1747. }
  1748. }
  1749. EXPORT_SYMBOL(scsi_get_sense_info_fld);
  1750. /**
  1751. * scsi_build_sense_buffer - build sense data in a buffer
  1752. * @desc: Sense format (non zero == descriptor format,
  1753. * 0 == fixed format)
  1754. * @buf: Where to build sense data
  1755. * @key: Sense key
  1756. * @asc: Additional sense code
  1757. * @ascq: Additional sense code qualifier
  1758. *
  1759. **/
  1760. void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq)
  1761. {
  1762. if (desc) {
  1763. buf[0] = 0x72; /* descriptor, current */
  1764. buf[1] = key;
  1765. buf[2] = asc;
  1766. buf[3] = ascq;
  1767. buf[7] = 0;
  1768. } else {
  1769. buf[0] = 0x70; /* fixed, current */
  1770. buf[2] = key;
  1771. buf[7] = 0xa;
  1772. buf[12] = asc;
  1773. buf[13] = ascq;
  1774. }
  1775. }
  1776. EXPORT_SYMBOL(scsi_build_sense_buffer);