scsi_error.c 55 KB

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