scsi_error.c 57 KB

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