scsi_error.c 56 KB

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