scsi_error.c 54 KB

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