scsi_error.c 55 KB

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