scsi_error.c 60 KB

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