scsi_error.c 58 KB

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