scsi_error.c 59 KB

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