scsi_error.c 53 KB

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