scsi_error.c 54 KB

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