scsi_error.c 54 KB

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