scsi_error.c 54 KB

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