scsi_error.c 56 KB

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