scsi_error.c 56 KB

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