scsi_error.c 58 KB

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