scsi_error.c 57 KB

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