scsi_error.c 58 KB

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