scsi_lib.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  1. /*
  2. * scsi_lib.c Copyright (C) 1999 Eric Youngdale
  3. *
  4. * SCSI queueing library.
  5. * Initial versions: Eric Youngdale (eric@andante.org).
  6. * Based upon conversations with large numbers
  7. * of people at Linux Expo.
  8. */
  9. #include <linux/bio.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/completion.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mempool.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/pci.h>
  17. #include <linux/delay.h>
  18. #include <scsi/scsi.h>
  19. #include <scsi/scsi_dbg.h>
  20. #include <scsi/scsi_device.h>
  21. #include <scsi/scsi_driver.h>
  22. #include <scsi/scsi_eh.h>
  23. #include <scsi/scsi_host.h>
  24. #include <scsi/scsi_request.h>
  25. #include "scsi_priv.h"
  26. #include "scsi_logging.h"
  27. #define SG_MEMPOOL_NR (sizeof(scsi_sg_pools)/sizeof(struct scsi_host_sg_pool))
  28. #define SG_MEMPOOL_SIZE 32
  29. struct scsi_host_sg_pool {
  30. size_t size;
  31. char *name;
  32. kmem_cache_t *slab;
  33. mempool_t *pool;
  34. };
  35. #if (SCSI_MAX_PHYS_SEGMENTS < 32)
  36. #error SCSI_MAX_PHYS_SEGMENTS is too small
  37. #endif
  38. #define SP(x) { x, "sgpool-" #x }
  39. static struct scsi_host_sg_pool scsi_sg_pools[] = {
  40. SP(8),
  41. SP(16),
  42. SP(32),
  43. #if (SCSI_MAX_PHYS_SEGMENTS > 32)
  44. SP(64),
  45. #if (SCSI_MAX_PHYS_SEGMENTS > 64)
  46. SP(128),
  47. #if (SCSI_MAX_PHYS_SEGMENTS > 128)
  48. SP(256),
  49. #if (SCSI_MAX_PHYS_SEGMENTS > 256)
  50. #error SCSI_MAX_PHYS_SEGMENTS is too large
  51. #endif
  52. #endif
  53. #endif
  54. #endif
  55. };
  56. #undef SP
  57. static void scsi_run_queue(struct request_queue *q);
  58. /*
  59. * Function: scsi_unprep_request()
  60. *
  61. * Purpose: Remove all preparation done for a request, including its
  62. * associated scsi_cmnd, so that it can be requeued.
  63. *
  64. * Arguments: req - request to unprepare
  65. *
  66. * Lock status: Assumed that no locks are held upon entry.
  67. *
  68. * Returns: Nothing.
  69. */
  70. static void scsi_unprep_request(struct request *req)
  71. {
  72. struct scsi_cmnd *cmd = req->special;
  73. req->flags &= ~REQ_DONTPREP;
  74. req->special = (req->flags & REQ_SPECIAL) ? cmd->sc_request : NULL;
  75. scsi_put_command(cmd);
  76. }
  77. /*
  78. * Function: scsi_queue_insert()
  79. *
  80. * Purpose: Insert a command in the midlevel queue.
  81. *
  82. * Arguments: cmd - command that we are adding to queue.
  83. * reason - why we are inserting command to queue.
  84. *
  85. * Lock status: Assumed that lock is not held upon entry.
  86. *
  87. * Returns: Nothing.
  88. *
  89. * Notes: We do this for one of two cases. Either the host is busy
  90. * and it cannot accept any more commands for the time being,
  91. * or the device returned QUEUE_FULL and can accept no more
  92. * commands.
  93. * Notes: This could be called either from an interrupt context or a
  94. * normal process context.
  95. */
  96. int scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
  97. {
  98. struct Scsi_Host *host = cmd->device->host;
  99. struct scsi_device *device = cmd->device;
  100. struct request_queue *q = device->request_queue;
  101. unsigned long flags;
  102. SCSI_LOG_MLQUEUE(1,
  103. printk("Inserting command %p into mlqueue\n", cmd));
  104. /*
  105. * Set the appropriate busy bit for the device/host.
  106. *
  107. * If the host/device isn't busy, assume that something actually
  108. * completed, and that we should be able to queue a command now.
  109. *
  110. * Note that the prior mid-layer assumption that any host could
  111. * always queue at least one command is now broken. The mid-layer
  112. * will implement a user specifiable stall (see
  113. * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
  114. * if a command is requeued with no other commands outstanding
  115. * either for the device or for the host.
  116. */
  117. if (reason == SCSI_MLQUEUE_HOST_BUSY)
  118. host->host_blocked = host->max_host_blocked;
  119. else if (reason == SCSI_MLQUEUE_DEVICE_BUSY)
  120. device->device_blocked = device->max_device_blocked;
  121. /*
  122. * Decrement the counters, since these commands are no longer
  123. * active on the host/device.
  124. */
  125. scsi_device_unbusy(device);
  126. /*
  127. * Requeue this command. It will go before all other commands
  128. * that are already in the queue.
  129. *
  130. * NOTE: there is magic here about the way the queue is plugged if
  131. * we have no outstanding commands.
  132. *
  133. * Although we *don't* plug the queue, we call the request
  134. * function. The SCSI request function detects the blocked condition
  135. * and plugs the queue appropriately.
  136. */
  137. spin_lock_irqsave(q->queue_lock, flags);
  138. blk_requeue_request(q, cmd->request);
  139. spin_unlock_irqrestore(q->queue_lock, flags);
  140. scsi_run_queue(q);
  141. return 0;
  142. }
  143. /*
  144. * Function: scsi_do_req
  145. *
  146. * Purpose: Queue a SCSI request
  147. *
  148. * Arguments: sreq - command descriptor.
  149. * cmnd - actual SCSI command to be performed.
  150. * buffer - data buffer.
  151. * bufflen - size of data buffer.
  152. * done - completion function to be run.
  153. * timeout - how long to let it run before timeout.
  154. * retries - number of retries we allow.
  155. *
  156. * Lock status: No locks held upon entry.
  157. *
  158. * Returns: Nothing.
  159. *
  160. * Notes: This function is only used for queueing requests for things
  161. * like ioctls and character device requests - this is because
  162. * we essentially just inject a request into the queue for the
  163. * device.
  164. *
  165. * In order to support the scsi_device_quiesce function, we
  166. * now inject requests on the *head* of the device queue
  167. * rather than the tail.
  168. */
  169. void scsi_do_req(struct scsi_request *sreq, const void *cmnd,
  170. void *buffer, unsigned bufflen,
  171. void (*done)(struct scsi_cmnd *),
  172. int timeout, int retries)
  173. {
  174. /*
  175. * If the upper level driver is reusing these things, then
  176. * we should release the low-level block now. Another one will
  177. * be allocated later when this request is getting queued.
  178. */
  179. __scsi_release_request(sreq);
  180. /*
  181. * Our own function scsi_done (which marks the host as not busy,
  182. * disables the timeout counter, etc) will be called by us or by the
  183. * scsi_hosts[host].queuecommand() function needs to also call
  184. * the completion function for the high level driver.
  185. */
  186. memcpy(sreq->sr_cmnd, cmnd, sizeof(sreq->sr_cmnd));
  187. sreq->sr_bufflen = bufflen;
  188. sreq->sr_buffer = buffer;
  189. sreq->sr_allowed = retries;
  190. sreq->sr_done = done;
  191. sreq->sr_timeout_per_command = timeout;
  192. if (sreq->sr_cmd_len == 0)
  193. sreq->sr_cmd_len = COMMAND_SIZE(sreq->sr_cmnd[0]);
  194. /*
  195. * head injection *required* here otherwise quiesce won't work
  196. *
  197. * Because users of this function are apt to reuse requests with no
  198. * modification, we have to sanitise the request flags here
  199. */
  200. sreq->sr_request->flags &= ~REQ_DONTPREP;
  201. blk_insert_request(sreq->sr_device->request_queue, sreq->sr_request,
  202. 1, sreq);
  203. }
  204. EXPORT_SYMBOL(scsi_do_req);
  205. /**
  206. * scsi_execute - insert request and wait for the result
  207. * @sdev: scsi device
  208. * @cmd: scsi command
  209. * @data_direction: data direction
  210. * @buffer: data buffer
  211. * @bufflen: len of buffer
  212. * @sense: optional sense buffer
  213. * @timeout: request timeout in seconds
  214. * @retries: number of times to retry request
  215. * @flags: or into request flags;
  216. *
  217. * returns the req->errors value which is the the scsi_cmnd result
  218. * field.
  219. **/
  220. int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
  221. int data_direction, void *buffer, unsigned bufflen,
  222. unsigned char *sense, int timeout, int retries, int flags)
  223. {
  224. struct request *req;
  225. int write = (data_direction == DMA_TO_DEVICE);
  226. int ret = DRIVER_ERROR << 24;
  227. req = blk_get_request(sdev->request_queue, write, __GFP_WAIT);
  228. if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
  229. buffer, bufflen, __GFP_WAIT))
  230. goto out;
  231. req->cmd_len = COMMAND_SIZE(cmd[0]);
  232. memcpy(req->cmd, cmd, req->cmd_len);
  233. req->sense = sense;
  234. req->sense_len = 0;
  235. req->timeout = timeout;
  236. req->flags |= flags | REQ_BLOCK_PC | REQ_SPECIAL | REQ_QUIET;
  237. /*
  238. * head injection *required* here otherwise quiesce won't work
  239. */
  240. blk_execute_rq(req->q, NULL, req, 1);
  241. ret = req->errors;
  242. out:
  243. blk_put_request(req);
  244. return ret;
  245. }
  246. EXPORT_SYMBOL(scsi_execute);
  247. int scsi_execute_req(struct scsi_device *sdev, const unsigned char *cmd,
  248. int data_direction, void *buffer, unsigned bufflen,
  249. struct scsi_sense_hdr *sshdr, int timeout, int retries)
  250. {
  251. char *sense = NULL;
  252. int result;
  253. if (sshdr) {
  254. sense = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
  255. if (!sense)
  256. return DRIVER_ERROR << 24;
  257. memset(sense, 0, SCSI_SENSE_BUFFERSIZE);
  258. }
  259. result = scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
  260. sense, timeout, retries, 0);
  261. if (sshdr)
  262. scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr);
  263. kfree(sense);
  264. return result;
  265. }
  266. EXPORT_SYMBOL(scsi_execute_req);
  267. struct scsi_io_context {
  268. void *data;
  269. void (*done)(void *data, char *sense, int result, int resid);
  270. char sense[SCSI_SENSE_BUFFERSIZE];
  271. };
  272. static void scsi_end_async(struct request *req)
  273. {
  274. struct scsi_io_context *sioc = req->end_io_data;
  275. if (sioc->done)
  276. sioc->done(sioc->data, sioc->sense, req->errors, req->data_len);
  277. kfree(sioc);
  278. __blk_put_request(req->q, req);
  279. }
  280. static int scsi_merge_bio(struct request *rq, struct bio *bio)
  281. {
  282. struct request_queue *q = rq->q;
  283. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  284. if (rq_data_dir(rq) == WRITE)
  285. bio->bi_rw |= (1 << BIO_RW);
  286. blk_queue_bounce(q, &bio);
  287. if (!rq->bio)
  288. blk_rq_bio_prep(q, rq, bio);
  289. else if (!q->back_merge_fn(q, rq, bio))
  290. return -EINVAL;
  291. else {
  292. rq->biotail->bi_next = bio;
  293. rq->biotail = bio;
  294. rq->hard_nr_sectors += bio_sectors(bio);
  295. rq->nr_sectors = rq->hard_nr_sectors;
  296. }
  297. return 0;
  298. }
  299. static int scsi_bi_endio(struct bio *bio, unsigned int bytes_done, int error)
  300. {
  301. if (bio->bi_size)
  302. return 1;
  303. bio_put(bio);
  304. return 0;
  305. }
  306. /**
  307. * scsi_req_map_sg - map a scatterlist into a request
  308. * @rq: request to fill
  309. * @sg: scatterlist
  310. * @nsegs: number of elements
  311. * @bufflen: len of buffer
  312. * @gfp: memory allocation flags
  313. *
  314. * scsi_req_map_sg maps a scatterlist into a request so that the
  315. * request can be sent to the block layer. We do not trust the scatterlist
  316. * sent to use, as some ULDs use that struct to only organize the pages.
  317. */
  318. static int scsi_req_map_sg(struct request *rq, struct scatterlist *sgl,
  319. int nsegs, unsigned bufflen, gfp_t gfp)
  320. {
  321. struct request_queue *q = rq->q;
  322. int nr_pages = (bufflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  323. unsigned int data_len = 0, len, bytes, off;
  324. struct page *page;
  325. struct bio *bio = NULL;
  326. int i, err, nr_vecs = 0;
  327. for (i = 0; i < nsegs; i++) {
  328. page = sgl[i].page;
  329. off = sgl[i].offset;
  330. len = sgl[i].length;
  331. data_len += len;
  332. while (len > 0) {
  333. bytes = min_t(unsigned int, len, PAGE_SIZE - off);
  334. if (!bio) {
  335. nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
  336. nr_pages -= nr_vecs;
  337. bio = bio_alloc(gfp, nr_vecs);
  338. if (!bio) {
  339. err = -ENOMEM;
  340. goto free_bios;
  341. }
  342. bio->bi_end_io = scsi_bi_endio;
  343. }
  344. if (bio_add_pc_page(q, bio, page, bytes, off) !=
  345. bytes) {
  346. bio_put(bio);
  347. err = -EINVAL;
  348. goto free_bios;
  349. }
  350. if (bio->bi_vcnt >= nr_vecs) {
  351. err = scsi_merge_bio(rq, bio);
  352. if (err) {
  353. bio_endio(bio, bio->bi_size, 0);
  354. goto free_bios;
  355. }
  356. bio = NULL;
  357. }
  358. page++;
  359. len -= bytes;
  360. off = 0;
  361. }
  362. }
  363. rq->buffer = rq->data = NULL;
  364. rq->data_len = data_len;
  365. return 0;
  366. free_bios:
  367. while ((bio = rq->bio) != NULL) {
  368. rq->bio = bio->bi_next;
  369. /*
  370. * call endio instead of bio_put incase it was bounced
  371. */
  372. bio_endio(bio, bio->bi_size, 0);
  373. }
  374. return err;
  375. }
  376. /**
  377. * scsi_execute_async - insert request
  378. * @sdev: scsi device
  379. * @cmd: scsi command
  380. * @data_direction: data direction
  381. * @buffer: data buffer (this can be a kernel buffer or scatterlist)
  382. * @bufflen: len of buffer
  383. * @use_sg: if buffer is a scatterlist this is the number of elements
  384. * @timeout: request timeout in seconds
  385. * @retries: number of times to retry request
  386. * @flags: or into request flags
  387. **/
  388. int scsi_execute_async(struct scsi_device *sdev, const unsigned char *cmd,
  389. int data_direction, void *buffer, unsigned bufflen,
  390. int use_sg, int timeout, int retries, void *privdata,
  391. void (*done)(void *, char *, int, int), gfp_t gfp)
  392. {
  393. struct request *req;
  394. struct scsi_io_context *sioc;
  395. int err = 0;
  396. int write = (data_direction == DMA_TO_DEVICE);
  397. sioc = kzalloc(sizeof(*sioc), gfp);
  398. if (!sioc)
  399. return DRIVER_ERROR << 24;
  400. req = blk_get_request(sdev->request_queue, write, gfp);
  401. if (!req)
  402. goto free_sense;
  403. if (use_sg)
  404. err = scsi_req_map_sg(req, buffer, use_sg, bufflen, gfp);
  405. else if (bufflen)
  406. err = blk_rq_map_kern(req->q, req, buffer, bufflen, gfp);
  407. if (err)
  408. goto free_req;
  409. req->cmd_len = COMMAND_SIZE(cmd[0]);
  410. memcpy(req->cmd, cmd, req->cmd_len);
  411. req->sense = sioc->sense;
  412. req->sense_len = 0;
  413. req->timeout = timeout;
  414. req->flags |= REQ_BLOCK_PC | REQ_QUIET;
  415. req->end_io_data = sioc;
  416. sioc->data = privdata;
  417. sioc->done = done;
  418. blk_execute_rq_nowait(req->q, NULL, req, 1, scsi_end_async);
  419. return 0;
  420. free_req:
  421. blk_put_request(req);
  422. free_sense:
  423. kfree(sioc);
  424. return DRIVER_ERROR << 24;
  425. }
  426. EXPORT_SYMBOL_GPL(scsi_execute_async);
  427. /*
  428. * Function: scsi_init_cmd_errh()
  429. *
  430. * Purpose: Initialize cmd fields related to error handling.
  431. *
  432. * Arguments: cmd - command that is ready to be queued.
  433. *
  434. * Returns: Nothing
  435. *
  436. * Notes: This function has the job of initializing a number of
  437. * fields related to error handling. Typically this will
  438. * be called once for each command, as required.
  439. */
  440. static int scsi_init_cmd_errh(struct scsi_cmnd *cmd)
  441. {
  442. cmd->serial_number = 0;
  443. memset(cmd->sense_buffer, 0, sizeof cmd->sense_buffer);
  444. if (cmd->cmd_len == 0)
  445. cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
  446. /*
  447. * We need saved copies of a number of fields - this is because
  448. * error handling may need to overwrite these with different values
  449. * to run different commands, and once error handling is complete,
  450. * we will need to restore these values prior to running the actual
  451. * command.
  452. */
  453. cmd->old_use_sg = cmd->use_sg;
  454. cmd->old_cmd_len = cmd->cmd_len;
  455. cmd->sc_old_data_direction = cmd->sc_data_direction;
  456. cmd->old_underflow = cmd->underflow;
  457. memcpy(cmd->data_cmnd, cmd->cmnd, sizeof(cmd->cmnd));
  458. cmd->buffer = cmd->request_buffer;
  459. cmd->bufflen = cmd->request_bufflen;
  460. return 1;
  461. }
  462. /*
  463. * Function: scsi_setup_cmd_retry()
  464. *
  465. * Purpose: Restore the command state for a retry
  466. *
  467. * Arguments: cmd - command to be restored
  468. *
  469. * Returns: Nothing
  470. *
  471. * Notes: Immediately prior to retrying a command, we need
  472. * to restore certain fields that we saved above.
  473. */
  474. void scsi_setup_cmd_retry(struct scsi_cmnd *cmd)
  475. {
  476. memcpy(cmd->cmnd, cmd->data_cmnd, sizeof(cmd->data_cmnd));
  477. cmd->request_buffer = cmd->buffer;
  478. cmd->request_bufflen = cmd->bufflen;
  479. cmd->use_sg = cmd->old_use_sg;
  480. cmd->cmd_len = cmd->old_cmd_len;
  481. cmd->sc_data_direction = cmd->sc_old_data_direction;
  482. cmd->underflow = cmd->old_underflow;
  483. }
  484. void scsi_device_unbusy(struct scsi_device *sdev)
  485. {
  486. struct Scsi_Host *shost = sdev->host;
  487. unsigned long flags;
  488. spin_lock_irqsave(shost->host_lock, flags);
  489. shost->host_busy--;
  490. if (unlikely(scsi_host_in_recovery(shost) &&
  491. shost->host_failed))
  492. scsi_eh_wakeup(shost);
  493. spin_unlock(shost->host_lock);
  494. spin_lock(sdev->request_queue->queue_lock);
  495. sdev->device_busy--;
  496. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  497. }
  498. /*
  499. * Called for single_lun devices on IO completion. Clear starget_sdev_user,
  500. * and call blk_run_queue for all the scsi_devices on the target -
  501. * including current_sdev first.
  502. *
  503. * Called with *no* scsi locks held.
  504. */
  505. static void scsi_single_lun_run(struct scsi_device *current_sdev)
  506. {
  507. struct Scsi_Host *shost = current_sdev->host;
  508. struct scsi_device *sdev, *tmp;
  509. struct scsi_target *starget = scsi_target(current_sdev);
  510. unsigned long flags;
  511. spin_lock_irqsave(shost->host_lock, flags);
  512. starget->starget_sdev_user = NULL;
  513. spin_unlock_irqrestore(shost->host_lock, flags);
  514. /*
  515. * Call blk_run_queue for all LUNs on the target, starting with
  516. * current_sdev. We race with others (to set starget_sdev_user),
  517. * but in most cases, we will be first. Ideally, each LU on the
  518. * target would get some limited time or requests on the target.
  519. */
  520. blk_run_queue(current_sdev->request_queue);
  521. spin_lock_irqsave(shost->host_lock, flags);
  522. if (starget->starget_sdev_user)
  523. goto out;
  524. list_for_each_entry_safe(sdev, tmp, &starget->devices,
  525. same_target_siblings) {
  526. if (sdev == current_sdev)
  527. continue;
  528. if (scsi_device_get(sdev))
  529. continue;
  530. spin_unlock_irqrestore(shost->host_lock, flags);
  531. blk_run_queue(sdev->request_queue);
  532. spin_lock_irqsave(shost->host_lock, flags);
  533. scsi_device_put(sdev);
  534. }
  535. out:
  536. spin_unlock_irqrestore(shost->host_lock, flags);
  537. }
  538. /*
  539. * Function: scsi_run_queue()
  540. *
  541. * Purpose: Select a proper request queue to serve next
  542. *
  543. * Arguments: q - last request's queue
  544. *
  545. * Returns: Nothing
  546. *
  547. * Notes: The previous command was completely finished, start
  548. * a new one if possible.
  549. */
  550. static void scsi_run_queue(struct request_queue *q)
  551. {
  552. struct scsi_device *sdev = q->queuedata;
  553. struct Scsi_Host *shost = sdev->host;
  554. unsigned long flags;
  555. if (sdev->single_lun)
  556. scsi_single_lun_run(sdev);
  557. spin_lock_irqsave(shost->host_lock, flags);
  558. while (!list_empty(&shost->starved_list) &&
  559. !shost->host_blocked && !shost->host_self_blocked &&
  560. !((shost->can_queue > 0) &&
  561. (shost->host_busy >= shost->can_queue))) {
  562. /*
  563. * As long as shost is accepting commands and we have
  564. * starved queues, call blk_run_queue. scsi_request_fn
  565. * drops the queue_lock and can add us back to the
  566. * starved_list.
  567. *
  568. * host_lock protects the starved_list and starved_entry.
  569. * scsi_request_fn must get the host_lock before checking
  570. * or modifying starved_list or starved_entry.
  571. */
  572. sdev = list_entry(shost->starved_list.next,
  573. struct scsi_device, starved_entry);
  574. list_del_init(&sdev->starved_entry);
  575. spin_unlock_irqrestore(shost->host_lock, flags);
  576. blk_run_queue(sdev->request_queue);
  577. spin_lock_irqsave(shost->host_lock, flags);
  578. if (unlikely(!list_empty(&sdev->starved_entry)))
  579. /*
  580. * sdev lost a race, and was put back on the
  581. * starved list. This is unlikely but without this
  582. * in theory we could loop forever.
  583. */
  584. break;
  585. }
  586. spin_unlock_irqrestore(shost->host_lock, flags);
  587. blk_run_queue(q);
  588. }
  589. /*
  590. * Function: scsi_requeue_command()
  591. *
  592. * Purpose: Handle post-processing of completed commands.
  593. *
  594. * Arguments: q - queue to operate on
  595. * cmd - command that may need to be requeued.
  596. *
  597. * Returns: Nothing
  598. *
  599. * Notes: After command completion, there may be blocks left
  600. * over which weren't finished by the previous command
  601. * this can be for a number of reasons - the main one is
  602. * I/O errors in the middle of the request, in which case
  603. * we need to request the blocks that come after the bad
  604. * sector.
  605. * Notes: Upon return, cmd is a stale pointer.
  606. */
  607. static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
  608. {
  609. struct request *req = cmd->request;
  610. unsigned long flags;
  611. scsi_unprep_request(req);
  612. spin_lock_irqsave(q->queue_lock, flags);
  613. blk_requeue_request(q, req);
  614. spin_unlock_irqrestore(q->queue_lock, flags);
  615. scsi_run_queue(q);
  616. }
  617. void scsi_next_command(struct scsi_cmnd *cmd)
  618. {
  619. struct scsi_device *sdev = cmd->device;
  620. struct request_queue *q = sdev->request_queue;
  621. /* need to hold a reference on the device before we let go of the cmd */
  622. get_device(&sdev->sdev_gendev);
  623. scsi_put_command(cmd);
  624. scsi_run_queue(q);
  625. /* ok to remove device now */
  626. put_device(&sdev->sdev_gendev);
  627. }
  628. void scsi_run_host_queues(struct Scsi_Host *shost)
  629. {
  630. struct scsi_device *sdev;
  631. shost_for_each_device(sdev, shost)
  632. scsi_run_queue(sdev->request_queue);
  633. }
  634. /*
  635. * Function: scsi_end_request()
  636. *
  637. * Purpose: Post-processing of completed commands (usually invoked at end
  638. * of upper level post-processing and scsi_io_completion).
  639. *
  640. * Arguments: cmd - command that is complete.
  641. * uptodate - 1 if I/O indicates success, <= 0 for I/O error.
  642. * bytes - number of bytes of completed I/O
  643. * requeue - indicates whether we should requeue leftovers.
  644. *
  645. * Lock status: Assumed that lock is not held upon entry.
  646. *
  647. * Returns: cmd if requeue required, NULL otherwise.
  648. *
  649. * Notes: This is called for block device requests in order to
  650. * mark some number of sectors as complete.
  651. *
  652. * We are guaranteeing that the request queue will be goosed
  653. * at some point during this call.
  654. * Notes: If cmd was requeued, upon return it will be a stale pointer.
  655. */
  656. static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int uptodate,
  657. int bytes, int requeue)
  658. {
  659. request_queue_t *q = cmd->device->request_queue;
  660. struct request *req = cmd->request;
  661. unsigned long flags;
  662. /*
  663. * If there are blocks left over at the end, set up the command
  664. * to queue the remainder of them.
  665. */
  666. if (end_that_request_chunk(req, uptodate, bytes)) {
  667. int leftover = (req->hard_nr_sectors << 9);
  668. if (blk_pc_request(req))
  669. leftover = req->data_len;
  670. /* kill remainder if no retrys */
  671. if (!uptodate && blk_noretry_request(req))
  672. end_that_request_chunk(req, 0, leftover);
  673. else {
  674. if (requeue) {
  675. /*
  676. * Bleah. Leftovers again. Stick the
  677. * leftovers in the front of the
  678. * queue, and goose the queue again.
  679. */
  680. scsi_requeue_command(q, cmd);
  681. cmd = NULL;
  682. }
  683. return cmd;
  684. }
  685. }
  686. add_disk_randomness(req->rq_disk);
  687. spin_lock_irqsave(q->queue_lock, flags);
  688. if (blk_rq_tagged(req))
  689. blk_queue_end_tag(q, req);
  690. end_that_request_last(req);
  691. spin_unlock_irqrestore(q->queue_lock, flags);
  692. /*
  693. * This will goose the queue request function at the end, so we don't
  694. * need to worry about launching another command.
  695. */
  696. scsi_next_command(cmd);
  697. return NULL;
  698. }
  699. static struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, gfp_t gfp_mask)
  700. {
  701. struct scsi_host_sg_pool *sgp;
  702. struct scatterlist *sgl;
  703. BUG_ON(!cmd->use_sg);
  704. switch (cmd->use_sg) {
  705. case 1 ... 8:
  706. cmd->sglist_len = 0;
  707. break;
  708. case 9 ... 16:
  709. cmd->sglist_len = 1;
  710. break;
  711. case 17 ... 32:
  712. cmd->sglist_len = 2;
  713. break;
  714. #if (SCSI_MAX_PHYS_SEGMENTS > 32)
  715. case 33 ... 64:
  716. cmd->sglist_len = 3;
  717. break;
  718. #if (SCSI_MAX_PHYS_SEGMENTS > 64)
  719. case 65 ... 128:
  720. cmd->sglist_len = 4;
  721. break;
  722. #if (SCSI_MAX_PHYS_SEGMENTS > 128)
  723. case 129 ... 256:
  724. cmd->sglist_len = 5;
  725. break;
  726. #endif
  727. #endif
  728. #endif
  729. default:
  730. return NULL;
  731. }
  732. sgp = scsi_sg_pools + cmd->sglist_len;
  733. sgl = mempool_alloc(sgp->pool, gfp_mask);
  734. return sgl;
  735. }
  736. static void scsi_free_sgtable(struct scatterlist *sgl, int index)
  737. {
  738. struct scsi_host_sg_pool *sgp;
  739. BUG_ON(index >= SG_MEMPOOL_NR);
  740. sgp = scsi_sg_pools + index;
  741. mempool_free(sgl, sgp->pool);
  742. }
  743. /*
  744. * Function: scsi_release_buffers()
  745. *
  746. * Purpose: Completion processing for block device I/O requests.
  747. *
  748. * Arguments: cmd - command that we are bailing.
  749. *
  750. * Lock status: Assumed that no lock is held upon entry.
  751. *
  752. * Returns: Nothing
  753. *
  754. * Notes: In the event that an upper level driver rejects a
  755. * command, we must release resources allocated during
  756. * the __init_io() function. Primarily this would involve
  757. * the scatter-gather table, and potentially any bounce
  758. * buffers.
  759. */
  760. static void scsi_release_buffers(struct scsi_cmnd *cmd)
  761. {
  762. struct request *req = cmd->request;
  763. /*
  764. * Free up any indirection buffers we allocated for DMA purposes.
  765. */
  766. if (cmd->use_sg)
  767. scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
  768. else if (cmd->request_buffer != req->buffer)
  769. kfree(cmd->request_buffer);
  770. /*
  771. * Zero these out. They now point to freed memory, and it is
  772. * dangerous to hang onto the pointers.
  773. */
  774. cmd->buffer = NULL;
  775. cmd->bufflen = 0;
  776. cmd->request_buffer = NULL;
  777. cmd->request_bufflen = 0;
  778. }
  779. /*
  780. * Function: scsi_io_completion()
  781. *
  782. * Purpose: Completion processing for block device I/O requests.
  783. *
  784. * Arguments: cmd - command that is finished.
  785. *
  786. * Lock status: Assumed that no lock is held upon entry.
  787. *
  788. * Returns: Nothing
  789. *
  790. * Notes: This function is matched in terms of capabilities to
  791. * the function that created the scatter-gather list.
  792. * In other words, if there are no bounce buffers
  793. * (the normal case for most drivers), we don't need
  794. * the logic to deal with cleaning up afterwards.
  795. *
  796. * We must do one of several things here:
  797. *
  798. * a) Call scsi_end_request. This will finish off the
  799. * specified number of sectors. If we are done, the
  800. * command block will be released, and the queue
  801. * function will be goosed. If we are not done, then
  802. * scsi_end_request will directly goose the queue.
  803. *
  804. * b) We can just use scsi_requeue_command() here. This would
  805. * be used if we just wanted to retry, for example.
  806. */
  807. void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes,
  808. unsigned int block_bytes)
  809. {
  810. int result = cmd->result;
  811. int this_count = cmd->bufflen;
  812. request_queue_t *q = cmd->device->request_queue;
  813. struct request *req = cmd->request;
  814. int clear_errors = 1;
  815. struct scsi_sense_hdr sshdr;
  816. int sense_valid = 0;
  817. int sense_deferred = 0;
  818. if (blk_complete_barrier_rq(q, req, good_bytes >> 9))
  819. return;
  820. /*
  821. * Free up any indirection buffers we allocated for DMA purposes.
  822. * For the case of a READ, we need to copy the data out of the
  823. * bounce buffer and into the real buffer.
  824. */
  825. if (cmd->use_sg)
  826. scsi_free_sgtable(cmd->buffer, cmd->sglist_len);
  827. else if (cmd->buffer != req->buffer) {
  828. if (rq_data_dir(req) == READ) {
  829. unsigned long flags;
  830. char *to = bio_kmap_irq(req->bio, &flags);
  831. memcpy(to, cmd->buffer, cmd->bufflen);
  832. bio_kunmap_irq(to, &flags);
  833. }
  834. kfree(cmd->buffer);
  835. }
  836. if (result) {
  837. sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
  838. if (sense_valid)
  839. sense_deferred = scsi_sense_is_deferred(&sshdr);
  840. }
  841. if (blk_pc_request(req)) { /* SG_IO ioctl from block level */
  842. req->errors = result;
  843. if (result) {
  844. clear_errors = 0;
  845. if (sense_valid && req->sense) {
  846. /*
  847. * SG_IO wants current and deferred errors
  848. */
  849. int len = 8 + cmd->sense_buffer[7];
  850. if (len > SCSI_SENSE_BUFFERSIZE)
  851. len = SCSI_SENSE_BUFFERSIZE;
  852. memcpy(req->sense, cmd->sense_buffer, len);
  853. req->sense_len = len;
  854. }
  855. } else
  856. req->data_len = cmd->resid;
  857. }
  858. /*
  859. * Zero these out. They now point to freed memory, and it is
  860. * dangerous to hang onto the pointers.
  861. */
  862. cmd->buffer = NULL;
  863. cmd->bufflen = 0;
  864. cmd->request_buffer = NULL;
  865. cmd->request_bufflen = 0;
  866. /*
  867. * Next deal with any sectors which we were able to correctly
  868. * handle.
  869. */
  870. if (good_bytes >= 0) {
  871. SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, %d bytes done.\n",
  872. req->nr_sectors, good_bytes));
  873. SCSI_LOG_HLCOMPLETE(1, printk("use_sg is %d\n", cmd->use_sg));
  874. if (clear_errors)
  875. req->errors = 0;
  876. /*
  877. * If multiple sectors are requested in one buffer, then
  878. * they will have been finished off by the first command.
  879. * If not, then we have a multi-buffer command.
  880. *
  881. * If block_bytes != 0, it means we had a medium error
  882. * of some sort, and that we want to mark some number of
  883. * sectors as not uptodate. Thus we want to inhibit
  884. * requeueing right here - we will requeue down below
  885. * when we handle the bad sectors.
  886. */
  887. /*
  888. * If the command completed without error, then either
  889. * finish off the rest of the command, or start a new one.
  890. */
  891. if (scsi_end_request(cmd, 1, good_bytes, result == 0) == NULL)
  892. return;
  893. }
  894. /*
  895. * Now, if we were good little boys and girls, Santa left us a request
  896. * sense buffer. We can extract information from this, so we
  897. * can choose a block to remap, etc.
  898. */
  899. if (sense_valid && !sense_deferred) {
  900. switch (sshdr.sense_key) {
  901. case UNIT_ATTENTION:
  902. if (cmd->device->removable) {
  903. /* detected disc change. set a bit
  904. * and quietly refuse further access.
  905. */
  906. cmd->device->changed = 1;
  907. scsi_end_request(cmd, 0,
  908. this_count, 1);
  909. return;
  910. } else {
  911. /*
  912. * Must have been a power glitch, or a
  913. * bus reset. Could not have been a
  914. * media change, so we just retry the
  915. * request and see what happens.
  916. */
  917. scsi_requeue_command(q, cmd);
  918. return;
  919. }
  920. break;
  921. case ILLEGAL_REQUEST:
  922. /*
  923. * If we had an ILLEGAL REQUEST returned, then we may
  924. * have performed an unsupported command. The only
  925. * thing this should be would be a ten byte read where
  926. * only a six byte read was supported. Also, on a
  927. * system where READ CAPACITY failed, we may have read
  928. * past the end of the disk.
  929. */
  930. if ((cmd->device->use_10_for_rw &&
  931. sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
  932. (cmd->cmnd[0] == READ_10 ||
  933. cmd->cmnd[0] == WRITE_10)) {
  934. cmd->device->use_10_for_rw = 0;
  935. /*
  936. * This will cause a retry with a 6-byte
  937. * command.
  938. */
  939. scsi_requeue_command(q, cmd);
  940. result = 0;
  941. } else {
  942. scsi_end_request(cmd, 0, this_count, 1);
  943. return;
  944. }
  945. break;
  946. case NOT_READY:
  947. /*
  948. * If the device is in the process of becoming ready,
  949. * retry.
  950. */
  951. if (sshdr.asc == 0x04 && sshdr.ascq == 0x01) {
  952. scsi_requeue_command(q, cmd);
  953. return;
  954. }
  955. if (!(req->flags & REQ_QUIET))
  956. scmd_printk(KERN_INFO, cmd,
  957. "Device not ready.\n");
  958. scsi_end_request(cmd, 0, this_count, 1);
  959. return;
  960. case VOLUME_OVERFLOW:
  961. if (!(req->flags & REQ_QUIET)) {
  962. scmd_printk(KERN_INFO, cmd,
  963. "Volume overflow, CDB: ");
  964. __scsi_print_command(cmd->data_cmnd);
  965. scsi_print_sense("", cmd);
  966. }
  967. scsi_end_request(cmd, 0, block_bytes, 1);
  968. return;
  969. default:
  970. break;
  971. }
  972. } /* driver byte != 0 */
  973. if (host_byte(result) == DID_RESET) {
  974. /*
  975. * Third party bus reset or reset for error
  976. * recovery reasons. Just retry the request
  977. * and see what happens.
  978. */
  979. scsi_requeue_command(q, cmd);
  980. return;
  981. }
  982. if (result) {
  983. if (!(req->flags & REQ_QUIET)) {
  984. scmd_printk(KERN_INFO, cmd,
  985. "SCSI error: return code = 0x%x\n", result);
  986. if (driver_byte(result) & DRIVER_SENSE)
  987. scsi_print_sense("", cmd);
  988. }
  989. /*
  990. * Mark a single buffer as not uptodate. Queue the remainder.
  991. * We sometimes get this cruft in the event that a medium error
  992. * isn't properly reported.
  993. */
  994. block_bytes = req->hard_cur_sectors << 9;
  995. if (!block_bytes)
  996. block_bytes = req->data_len;
  997. scsi_end_request(cmd, 0, block_bytes, 1);
  998. }
  999. }
  1000. EXPORT_SYMBOL(scsi_io_completion);
  1001. /*
  1002. * Function: scsi_init_io()
  1003. *
  1004. * Purpose: SCSI I/O initialize function.
  1005. *
  1006. * Arguments: cmd - Command descriptor we wish to initialize
  1007. *
  1008. * Returns: 0 on success
  1009. * BLKPREP_DEFER if the failure is retryable
  1010. * BLKPREP_KILL if the failure is fatal
  1011. */
  1012. static int scsi_init_io(struct scsi_cmnd *cmd)
  1013. {
  1014. struct request *req = cmd->request;
  1015. struct scatterlist *sgpnt;
  1016. int count;
  1017. /*
  1018. * if this is a rq->data based REQ_BLOCK_PC, setup for a non-sg xfer
  1019. */
  1020. if ((req->flags & REQ_BLOCK_PC) && !req->bio) {
  1021. cmd->request_bufflen = req->data_len;
  1022. cmd->request_buffer = req->data;
  1023. req->buffer = req->data;
  1024. cmd->use_sg = 0;
  1025. return 0;
  1026. }
  1027. /*
  1028. * we used to not use scatter-gather for single segment request,
  1029. * but now we do (it makes highmem I/O easier to support without
  1030. * kmapping pages)
  1031. */
  1032. cmd->use_sg = req->nr_phys_segments;
  1033. /*
  1034. * if sg table allocation fails, requeue request later.
  1035. */
  1036. sgpnt = scsi_alloc_sgtable(cmd, GFP_ATOMIC);
  1037. if (unlikely(!sgpnt)) {
  1038. scsi_unprep_request(req);
  1039. return BLKPREP_DEFER;
  1040. }
  1041. cmd->request_buffer = (char *) sgpnt;
  1042. cmd->request_bufflen = req->nr_sectors << 9;
  1043. if (blk_pc_request(req))
  1044. cmd->request_bufflen = req->data_len;
  1045. req->buffer = NULL;
  1046. /*
  1047. * Next, walk the list, and fill in the addresses and sizes of
  1048. * each segment.
  1049. */
  1050. count = blk_rq_map_sg(req->q, req, cmd->request_buffer);
  1051. /*
  1052. * mapped well, send it off
  1053. */
  1054. if (likely(count <= cmd->use_sg)) {
  1055. cmd->use_sg = count;
  1056. return 0;
  1057. }
  1058. printk(KERN_ERR "Incorrect number of segments after building list\n");
  1059. printk(KERN_ERR "counted %d, received %d\n", count, cmd->use_sg);
  1060. printk(KERN_ERR "req nr_sec %lu, cur_nr_sec %u\n", req->nr_sectors,
  1061. req->current_nr_sectors);
  1062. /* release the command and kill it */
  1063. scsi_release_buffers(cmd);
  1064. scsi_put_command(cmd);
  1065. return BLKPREP_KILL;
  1066. }
  1067. static int scsi_prepare_flush_fn(request_queue_t *q, struct request *rq)
  1068. {
  1069. struct scsi_device *sdev = q->queuedata;
  1070. struct scsi_driver *drv;
  1071. if (sdev->sdev_state == SDEV_RUNNING) {
  1072. drv = *(struct scsi_driver **) rq->rq_disk->private_data;
  1073. if (drv->prepare_flush)
  1074. return drv->prepare_flush(q, rq);
  1075. }
  1076. return 0;
  1077. }
  1078. static void scsi_end_flush_fn(request_queue_t *q, struct request *rq)
  1079. {
  1080. struct scsi_device *sdev = q->queuedata;
  1081. struct request *flush_rq = rq->end_io_data;
  1082. struct scsi_driver *drv;
  1083. if (flush_rq->errors) {
  1084. printk("scsi: barrier error, disabling flush support\n");
  1085. blk_queue_ordered(q, QUEUE_ORDERED_NONE);
  1086. }
  1087. if (sdev->sdev_state == SDEV_RUNNING) {
  1088. drv = *(struct scsi_driver **) rq->rq_disk->private_data;
  1089. drv->end_flush(q, rq);
  1090. }
  1091. }
  1092. static int scsi_issue_flush_fn(request_queue_t *q, struct gendisk *disk,
  1093. sector_t *error_sector)
  1094. {
  1095. struct scsi_device *sdev = q->queuedata;
  1096. struct scsi_driver *drv;
  1097. if (sdev->sdev_state != SDEV_RUNNING)
  1098. return -ENXIO;
  1099. drv = *(struct scsi_driver **) disk->private_data;
  1100. if (drv->issue_flush)
  1101. return drv->issue_flush(&sdev->sdev_gendev, error_sector);
  1102. return -EOPNOTSUPP;
  1103. }
  1104. static void scsi_generic_done(struct scsi_cmnd *cmd)
  1105. {
  1106. BUG_ON(!blk_pc_request(cmd->request));
  1107. scsi_io_completion(cmd, cmd->result == 0 ? cmd->bufflen : 0, 0);
  1108. }
  1109. static int scsi_prep_fn(struct request_queue *q, struct request *req)
  1110. {
  1111. struct scsi_device *sdev = q->queuedata;
  1112. struct scsi_cmnd *cmd;
  1113. int specials_only = 0;
  1114. /*
  1115. * Just check to see if the device is online. If it isn't, we
  1116. * refuse to process any commands. The device must be brought
  1117. * online before trying any recovery commands
  1118. */
  1119. if (unlikely(!scsi_device_online(sdev))) {
  1120. sdev_printk(KERN_ERR, sdev,
  1121. "rejecting I/O to offline device\n");
  1122. goto kill;
  1123. }
  1124. if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
  1125. /* OK, we're not in a running state don't prep
  1126. * user commands */
  1127. if (sdev->sdev_state == SDEV_DEL) {
  1128. /* Device is fully deleted, no commands
  1129. * at all allowed down */
  1130. sdev_printk(KERN_ERR, sdev,
  1131. "rejecting I/O to dead device\n");
  1132. goto kill;
  1133. }
  1134. /* OK, we only allow special commands (i.e. not
  1135. * user initiated ones */
  1136. specials_only = sdev->sdev_state;
  1137. }
  1138. /*
  1139. * Find the actual device driver associated with this command.
  1140. * The SPECIAL requests are things like character device or
  1141. * ioctls, which did not originate from ll_rw_blk. Note that
  1142. * the special field is also used to indicate the cmd for
  1143. * the remainder of a partially fulfilled request that can
  1144. * come up when there is a medium error. We have to treat
  1145. * these two cases differently. We differentiate by looking
  1146. * at request->cmd, as this tells us the real story.
  1147. */
  1148. if (req->flags & REQ_SPECIAL && req->special) {
  1149. struct scsi_request *sreq = req->special;
  1150. if (sreq->sr_magic == SCSI_REQ_MAGIC) {
  1151. cmd = scsi_get_command(sreq->sr_device, GFP_ATOMIC);
  1152. if (unlikely(!cmd))
  1153. goto defer;
  1154. scsi_init_cmd_from_req(cmd, sreq);
  1155. } else
  1156. cmd = req->special;
  1157. } else if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
  1158. if(unlikely(specials_only) && !(req->flags & REQ_SPECIAL)) {
  1159. if(specials_only == SDEV_QUIESCE ||
  1160. specials_only == SDEV_BLOCK)
  1161. goto defer;
  1162. sdev_printk(KERN_ERR, sdev,
  1163. "rejecting I/O to device being removed\n");
  1164. goto kill;
  1165. }
  1166. /*
  1167. * Now try and find a command block that we can use.
  1168. */
  1169. if (!req->special) {
  1170. cmd = scsi_get_command(sdev, GFP_ATOMIC);
  1171. if (unlikely(!cmd))
  1172. goto defer;
  1173. } else
  1174. cmd = req->special;
  1175. /* pull a tag out of the request if we have one */
  1176. cmd->tag = req->tag;
  1177. } else {
  1178. blk_dump_rq_flags(req, "SCSI bad req");
  1179. goto kill;
  1180. }
  1181. /* note the overloading of req->special. When the tag
  1182. * is active it always means cmd. If the tag goes
  1183. * back for re-queueing, it may be reset */
  1184. req->special = cmd;
  1185. cmd->request = req;
  1186. /*
  1187. * FIXME: drop the lock here because the functions below
  1188. * expect to be called without the queue lock held. Also,
  1189. * previously, we dequeued the request before dropping the
  1190. * lock. We hope REQ_STARTED prevents anything untoward from
  1191. * happening now.
  1192. */
  1193. if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
  1194. struct scsi_driver *drv;
  1195. int ret;
  1196. /*
  1197. * This will do a couple of things:
  1198. * 1) Fill in the actual SCSI command.
  1199. * 2) Fill in any other upper-level specific fields
  1200. * (timeout).
  1201. *
  1202. * If this returns 0, it means that the request failed
  1203. * (reading past end of disk, reading offline device,
  1204. * etc). This won't actually talk to the device, but
  1205. * some kinds of consistency checking may cause the
  1206. * request to be rejected immediately.
  1207. */
  1208. /*
  1209. * This sets up the scatter-gather table (allocating if
  1210. * required).
  1211. */
  1212. ret = scsi_init_io(cmd);
  1213. switch(ret) {
  1214. /* For BLKPREP_KILL/DEFER the cmd was released */
  1215. case BLKPREP_KILL:
  1216. goto kill;
  1217. case BLKPREP_DEFER:
  1218. goto defer;
  1219. }
  1220. /*
  1221. * Initialize the actual SCSI command for this request.
  1222. */
  1223. if (req->rq_disk) {
  1224. drv = *(struct scsi_driver **)req->rq_disk->private_data;
  1225. if (unlikely(!drv->init_command(cmd))) {
  1226. scsi_release_buffers(cmd);
  1227. scsi_put_command(cmd);
  1228. goto kill;
  1229. }
  1230. } else {
  1231. memcpy(cmd->cmnd, req->cmd, sizeof(cmd->cmnd));
  1232. cmd->cmd_len = req->cmd_len;
  1233. if (rq_data_dir(req) == WRITE)
  1234. cmd->sc_data_direction = DMA_TO_DEVICE;
  1235. else if (req->data_len)
  1236. cmd->sc_data_direction = DMA_FROM_DEVICE;
  1237. else
  1238. cmd->sc_data_direction = DMA_NONE;
  1239. cmd->transfersize = req->data_len;
  1240. cmd->allowed = 3;
  1241. cmd->timeout_per_command = req->timeout;
  1242. cmd->done = scsi_generic_done;
  1243. }
  1244. }
  1245. /*
  1246. * The request is now prepped, no need to come back here
  1247. */
  1248. req->flags |= REQ_DONTPREP;
  1249. return BLKPREP_OK;
  1250. defer:
  1251. /* If we defer, the elv_next_request() returns NULL, but the
  1252. * queue must be restarted, so we plug here if no returning
  1253. * command will automatically do that. */
  1254. if (sdev->device_busy == 0)
  1255. blk_plug_device(q);
  1256. return BLKPREP_DEFER;
  1257. kill:
  1258. req->errors = DID_NO_CONNECT << 16;
  1259. return BLKPREP_KILL;
  1260. }
  1261. /*
  1262. * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
  1263. * return 0.
  1264. *
  1265. * Called with the queue_lock held.
  1266. */
  1267. static inline int scsi_dev_queue_ready(struct request_queue *q,
  1268. struct scsi_device *sdev)
  1269. {
  1270. if (sdev->device_busy >= sdev->queue_depth)
  1271. return 0;
  1272. if (sdev->device_busy == 0 && sdev->device_blocked) {
  1273. /*
  1274. * unblock after device_blocked iterates to zero
  1275. */
  1276. if (--sdev->device_blocked == 0) {
  1277. SCSI_LOG_MLQUEUE(3,
  1278. sdev_printk(KERN_INFO, sdev,
  1279. "unblocking device at zero depth\n"));
  1280. } else {
  1281. blk_plug_device(q);
  1282. return 0;
  1283. }
  1284. }
  1285. if (sdev->device_blocked)
  1286. return 0;
  1287. return 1;
  1288. }
  1289. /*
  1290. * scsi_host_queue_ready: if we can send requests to shost, return 1 else
  1291. * return 0. We must end up running the queue again whenever 0 is
  1292. * returned, else IO can hang.
  1293. *
  1294. * Called with host_lock held.
  1295. */
  1296. static inline int scsi_host_queue_ready(struct request_queue *q,
  1297. struct Scsi_Host *shost,
  1298. struct scsi_device *sdev)
  1299. {
  1300. if (scsi_host_in_recovery(shost))
  1301. return 0;
  1302. if (shost->host_busy == 0 && shost->host_blocked) {
  1303. /*
  1304. * unblock after host_blocked iterates to zero
  1305. */
  1306. if (--shost->host_blocked == 0) {
  1307. SCSI_LOG_MLQUEUE(3,
  1308. printk("scsi%d unblocking host at zero depth\n",
  1309. shost->host_no));
  1310. } else {
  1311. blk_plug_device(q);
  1312. return 0;
  1313. }
  1314. }
  1315. if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) ||
  1316. shost->host_blocked || shost->host_self_blocked) {
  1317. if (list_empty(&sdev->starved_entry))
  1318. list_add_tail(&sdev->starved_entry, &shost->starved_list);
  1319. return 0;
  1320. }
  1321. /* We're OK to process the command, so we can't be starved */
  1322. if (!list_empty(&sdev->starved_entry))
  1323. list_del_init(&sdev->starved_entry);
  1324. return 1;
  1325. }
  1326. /*
  1327. * Kill a request for a dead device
  1328. */
  1329. static void scsi_kill_request(struct request *req, request_queue_t *q)
  1330. {
  1331. struct scsi_cmnd *cmd = req->special;
  1332. blkdev_dequeue_request(req);
  1333. if (unlikely(cmd == NULL)) {
  1334. printk(KERN_CRIT "impossible request in %s.\n",
  1335. __FUNCTION__);
  1336. BUG();
  1337. }
  1338. scsi_init_cmd_errh(cmd);
  1339. cmd->result = DID_NO_CONNECT << 16;
  1340. atomic_inc(&cmd->device->iorequest_cnt);
  1341. __scsi_done(cmd);
  1342. }
  1343. /*
  1344. * Function: scsi_request_fn()
  1345. *
  1346. * Purpose: Main strategy routine for SCSI.
  1347. *
  1348. * Arguments: q - Pointer to actual queue.
  1349. *
  1350. * Returns: Nothing
  1351. *
  1352. * Lock status: IO request lock assumed to be held when called.
  1353. */
  1354. static void scsi_request_fn(struct request_queue *q)
  1355. {
  1356. struct scsi_device *sdev = q->queuedata;
  1357. struct Scsi_Host *shost;
  1358. struct scsi_cmnd *cmd;
  1359. struct request *req;
  1360. if (!sdev) {
  1361. printk("scsi: killing requests for dead queue\n");
  1362. while ((req = elv_next_request(q)) != NULL)
  1363. scsi_kill_request(req, q);
  1364. return;
  1365. }
  1366. if(!get_device(&sdev->sdev_gendev))
  1367. /* We must be tearing the block queue down already */
  1368. return;
  1369. /*
  1370. * To start with, we keep looping until the queue is empty, or until
  1371. * the host is no longer able to accept any more requests.
  1372. */
  1373. shost = sdev->host;
  1374. while (!blk_queue_plugged(q)) {
  1375. int rtn;
  1376. /*
  1377. * get next queueable request. We do this early to make sure
  1378. * that the request is fully prepared even if we cannot
  1379. * accept it.
  1380. */
  1381. req = elv_next_request(q);
  1382. if (!req || !scsi_dev_queue_ready(q, sdev))
  1383. break;
  1384. if (unlikely(!scsi_device_online(sdev))) {
  1385. sdev_printk(KERN_ERR, sdev,
  1386. "rejecting I/O to offline device\n");
  1387. scsi_kill_request(req, q);
  1388. continue;
  1389. }
  1390. /*
  1391. * Remove the request from the request list.
  1392. */
  1393. if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
  1394. blkdev_dequeue_request(req);
  1395. sdev->device_busy++;
  1396. spin_unlock(q->queue_lock);
  1397. cmd = req->special;
  1398. if (unlikely(cmd == NULL)) {
  1399. printk(KERN_CRIT "impossible request in %s.\n"
  1400. "please mail a stack trace to "
  1401. "linux-scsi@vger.kernel.org",
  1402. __FUNCTION__);
  1403. BUG();
  1404. }
  1405. spin_lock(shost->host_lock);
  1406. if (!scsi_host_queue_ready(q, shost, sdev))
  1407. goto not_ready;
  1408. if (sdev->single_lun) {
  1409. if (scsi_target(sdev)->starget_sdev_user &&
  1410. scsi_target(sdev)->starget_sdev_user != sdev)
  1411. goto not_ready;
  1412. scsi_target(sdev)->starget_sdev_user = sdev;
  1413. }
  1414. shost->host_busy++;
  1415. /*
  1416. * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
  1417. * take the lock again.
  1418. */
  1419. spin_unlock_irq(shost->host_lock);
  1420. /*
  1421. * Finally, initialize any error handling parameters, and set up
  1422. * the timers for timeouts.
  1423. */
  1424. scsi_init_cmd_errh(cmd);
  1425. /*
  1426. * Dispatch the command to the low-level driver.
  1427. */
  1428. rtn = scsi_dispatch_cmd(cmd);
  1429. spin_lock_irq(q->queue_lock);
  1430. if(rtn) {
  1431. /* we're refusing the command; because of
  1432. * the way locks get dropped, we need to
  1433. * check here if plugging is required */
  1434. if(sdev->device_busy == 0)
  1435. blk_plug_device(q);
  1436. break;
  1437. }
  1438. }
  1439. goto out;
  1440. not_ready:
  1441. spin_unlock_irq(shost->host_lock);
  1442. /*
  1443. * lock q, handle tag, requeue req, and decrement device_busy. We
  1444. * must return with queue_lock held.
  1445. *
  1446. * Decrementing device_busy without checking it is OK, as all such
  1447. * cases (host limits or settings) should run the queue at some
  1448. * later time.
  1449. */
  1450. spin_lock_irq(q->queue_lock);
  1451. blk_requeue_request(q, req);
  1452. sdev->device_busy--;
  1453. if(sdev->device_busy == 0)
  1454. blk_plug_device(q);
  1455. out:
  1456. /* must be careful here...if we trigger the ->remove() function
  1457. * we cannot be holding the q lock */
  1458. spin_unlock_irq(q->queue_lock);
  1459. put_device(&sdev->sdev_gendev);
  1460. spin_lock_irq(q->queue_lock);
  1461. }
  1462. u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
  1463. {
  1464. struct device *host_dev;
  1465. u64 bounce_limit = 0xffffffff;
  1466. if (shost->unchecked_isa_dma)
  1467. return BLK_BOUNCE_ISA;
  1468. /*
  1469. * Platforms with virtual-DMA translation
  1470. * hardware have no practical limit.
  1471. */
  1472. if (!PCI_DMA_BUS_IS_PHYS)
  1473. return BLK_BOUNCE_ANY;
  1474. host_dev = scsi_get_device(shost);
  1475. if (host_dev && host_dev->dma_mask)
  1476. bounce_limit = *host_dev->dma_mask;
  1477. return bounce_limit;
  1478. }
  1479. EXPORT_SYMBOL(scsi_calculate_bounce_limit);
  1480. struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
  1481. {
  1482. struct Scsi_Host *shost = sdev->host;
  1483. struct request_queue *q;
  1484. q = blk_init_queue(scsi_request_fn, NULL);
  1485. if (!q)
  1486. return NULL;
  1487. blk_queue_prep_rq(q, scsi_prep_fn);
  1488. blk_queue_max_hw_segments(q, shost->sg_tablesize);
  1489. blk_queue_max_phys_segments(q, SCSI_MAX_PHYS_SEGMENTS);
  1490. blk_queue_max_sectors(q, shost->max_sectors);
  1491. blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
  1492. blk_queue_segment_boundary(q, shost->dma_boundary);
  1493. blk_queue_issue_flush_fn(q, scsi_issue_flush_fn);
  1494. /*
  1495. * ordered tags are superior to flush ordering
  1496. */
  1497. if (shost->ordered_tag)
  1498. blk_queue_ordered(q, QUEUE_ORDERED_TAG);
  1499. else if (shost->ordered_flush) {
  1500. blk_queue_ordered(q, QUEUE_ORDERED_FLUSH);
  1501. q->prepare_flush_fn = scsi_prepare_flush_fn;
  1502. q->end_flush_fn = scsi_end_flush_fn;
  1503. }
  1504. if (!shost->use_clustering)
  1505. clear_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  1506. return q;
  1507. }
  1508. void scsi_free_queue(struct request_queue *q)
  1509. {
  1510. blk_cleanup_queue(q);
  1511. }
  1512. /*
  1513. * Function: scsi_block_requests()
  1514. *
  1515. * Purpose: Utility function used by low-level drivers to prevent further
  1516. * commands from being queued to the device.
  1517. *
  1518. * Arguments: shost - Host in question
  1519. *
  1520. * Returns: Nothing
  1521. *
  1522. * Lock status: No locks are assumed held.
  1523. *
  1524. * Notes: There is no timer nor any other means by which the requests
  1525. * get unblocked other than the low-level driver calling
  1526. * scsi_unblock_requests().
  1527. */
  1528. void scsi_block_requests(struct Scsi_Host *shost)
  1529. {
  1530. shost->host_self_blocked = 1;
  1531. }
  1532. EXPORT_SYMBOL(scsi_block_requests);
  1533. /*
  1534. * Function: scsi_unblock_requests()
  1535. *
  1536. * Purpose: Utility function used by low-level drivers to allow further
  1537. * commands from being queued to the device.
  1538. *
  1539. * Arguments: shost - Host in question
  1540. *
  1541. * Returns: Nothing
  1542. *
  1543. * Lock status: No locks are assumed held.
  1544. *
  1545. * Notes: There is no timer nor any other means by which the requests
  1546. * get unblocked other than the low-level driver calling
  1547. * scsi_unblock_requests().
  1548. *
  1549. * This is done as an API function so that changes to the
  1550. * internals of the scsi mid-layer won't require wholesale
  1551. * changes to drivers that use this feature.
  1552. */
  1553. void scsi_unblock_requests(struct Scsi_Host *shost)
  1554. {
  1555. shost->host_self_blocked = 0;
  1556. scsi_run_host_queues(shost);
  1557. }
  1558. EXPORT_SYMBOL(scsi_unblock_requests);
  1559. int __init scsi_init_queue(void)
  1560. {
  1561. int i;
  1562. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1563. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1564. int size = sgp->size * sizeof(struct scatterlist);
  1565. sgp->slab = kmem_cache_create(sgp->name, size, 0,
  1566. SLAB_HWCACHE_ALIGN, NULL, NULL);
  1567. if (!sgp->slab) {
  1568. printk(KERN_ERR "SCSI: can't init sg slab %s\n",
  1569. sgp->name);
  1570. }
  1571. sgp->pool = mempool_create(SG_MEMPOOL_SIZE,
  1572. mempool_alloc_slab, mempool_free_slab,
  1573. sgp->slab);
  1574. if (!sgp->pool) {
  1575. printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
  1576. sgp->name);
  1577. }
  1578. }
  1579. return 0;
  1580. }
  1581. void scsi_exit_queue(void)
  1582. {
  1583. int i;
  1584. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1585. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1586. mempool_destroy(sgp->pool);
  1587. kmem_cache_destroy(sgp->slab);
  1588. }
  1589. }
  1590. /**
  1591. * scsi_mode_sense - issue a mode sense, falling back from 10 to
  1592. * six bytes if necessary.
  1593. * @sdev: SCSI device to be queried
  1594. * @dbd: set if mode sense will allow block descriptors to be returned
  1595. * @modepage: mode page being requested
  1596. * @buffer: request buffer (may not be smaller than eight bytes)
  1597. * @len: length of request buffer.
  1598. * @timeout: command timeout
  1599. * @retries: number of retries before failing
  1600. * @data: returns a structure abstracting the mode header data
  1601. * @sense: place to put sense data (or NULL if no sense to be collected).
  1602. * must be SCSI_SENSE_BUFFERSIZE big.
  1603. *
  1604. * Returns zero if unsuccessful, or the header offset (either 4
  1605. * or 8 depending on whether a six or ten byte command was
  1606. * issued) if successful.
  1607. **/
  1608. int
  1609. scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
  1610. unsigned char *buffer, int len, int timeout, int retries,
  1611. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr) {
  1612. unsigned char cmd[12];
  1613. int use_10_for_ms;
  1614. int header_length;
  1615. int result;
  1616. struct scsi_sense_hdr my_sshdr;
  1617. memset(data, 0, sizeof(*data));
  1618. memset(&cmd[0], 0, 12);
  1619. cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
  1620. cmd[2] = modepage;
  1621. /* caller might not be interested in sense, but we need it */
  1622. if (!sshdr)
  1623. sshdr = &my_sshdr;
  1624. retry:
  1625. use_10_for_ms = sdev->use_10_for_ms;
  1626. if (use_10_for_ms) {
  1627. if (len < 8)
  1628. len = 8;
  1629. cmd[0] = MODE_SENSE_10;
  1630. cmd[8] = len;
  1631. header_length = 8;
  1632. } else {
  1633. if (len < 4)
  1634. len = 4;
  1635. cmd[0] = MODE_SENSE;
  1636. cmd[4] = len;
  1637. header_length = 4;
  1638. }
  1639. memset(buffer, 0, len);
  1640. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
  1641. sshdr, timeout, retries);
  1642. /* This code looks awful: what it's doing is making sure an
  1643. * ILLEGAL REQUEST sense return identifies the actual command
  1644. * byte as the problem. MODE_SENSE commands can return
  1645. * ILLEGAL REQUEST if the code page isn't supported */
  1646. if (use_10_for_ms && !scsi_status_is_good(result) &&
  1647. (driver_byte(result) & DRIVER_SENSE)) {
  1648. if (scsi_sense_valid(sshdr)) {
  1649. if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
  1650. (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
  1651. /*
  1652. * Invalid command operation code
  1653. */
  1654. sdev->use_10_for_ms = 0;
  1655. goto retry;
  1656. }
  1657. }
  1658. }
  1659. if(scsi_status_is_good(result)) {
  1660. data->header_length = header_length;
  1661. if(use_10_for_ms) {
  1662. data->length = buffer[0]*256 + buffer[1] + 2;
  1663. data->medium_type = buffer[2];
  1664. data->device_specific = buffer[3];
  1665. data->longlba = buffer[4] & 0x01;
  1666. data->block_descriptor_length = buffer[6]*256
  1667. + buffer[7];
  1668. } else {
  1669. data->length = buffer[0] + 1;
  1670. data->medium_type = buffer[1];
  1671. data->device_specific = buffer[2];
  1672. data->block_descriptor_length = buffer[3];
  1673. }
  1674. }
  1675. return result;
  1676. }
  1677. EXPORT_SYMBOL(scsi_mode_sense);
  1678. int
  1679. scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries)
  1680. {
  1681. char cmd[] = {
  1682. TEST_UNIT_READY, 0, 0, 0, 0, 0,
  1683. };
  1684. struct scsi_sense_hdr sshdr;
  1685. int result;
  1686. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, &sshdr,
  1687. timeout, retries);
  1688. if ((driver_byte(result) & DRIVER_SENSE) && sdev->removable) {
  1689. if ((scsi_sense_valid(&sshdr)) &&
  1690. ((sshdr.sense_key == UNIT_ATTENTION) ||
  1691. (sshdr.sense_key == NOT_READY))) {
  1692. sdev->changed = 1;
  1693. result = 0;
  1694. }
  1695. }
  1696. return result;
  1697. }
  1698. EXPORT_SYMBOL(scsi_test_unit_ready);
  1699. /**
  1700. * scsi_device_set_state - Take the given device through the device
  1701. * state model.
  1702. * @sdev: scsi device to change the state of.
  1703. * @state: state to change to.
  1704. *
  1705. * Returns zero if unsuccessful or an error if the requested
  1706. * transition is illegal.
  1707. **/
  1708. int
  1709. scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
  1710. {
  1711. enum scsi_device_state oldstate = sdev->sdev_state;
  1712. if (state == oldstate)
  1713. return 0;
  1714. switch (state) {
  1715. case SDEV_CREATED:
  1716. /* There are no legal states that come back to
  1717. * created. This is the manually initialised start
  1718. * state */
  1719. goto illegal;
  1720. case SDEV_RUNNING:
  1721. switch (oldstate) {
  1722. case SDEV_CREATED:
  1723. case SDEV_OFFLINE:
  1724. case SDEV_QUIESCE:
  1725. case SDEV_BLOCK:
  1726. break;
  1727. default:
  1728. goto illegal;
  1729. }
  1730. break;
  1731. case SDEV_QUIESCE:
  1732. switch (oldstate) {
  1733. case SDEV_RUNNING:
  1734. case SDEV_OFFLINE:
  1735. break;
  1736. default:
  1737. goto illegal;
  1738. }
  1739. break;
  1740. case SDEV_OFFLINE:
  1741. switch (oldstate) {
  1742. case SDEV_CREATED:
  1743. case SDEV_RUNNING:
  1744. case SDEV_QUIESCE:
  1745. case SDEV_BLOCK:
  1746. break;
  1747. default:
  1748. goto illegal;
  1749. }
  1750. break;
  1751. case SDEV_BLOCK:
  1752. switch (oldstate) {
  1753. case SDEV_CREATED:
  1754. case SDEV_RUNNING:
  1755. break;
  1756. default:
  1757. goto illegal;
  1758. }
  1759. break;
  1760. case SDEV_CANCEL:
  1761. switch (oldstate) {
  1762. case SDEV_CREATED:
  1763. case SDEV_RUNNING:
  1764. case SDEV_OFFLINE:
  1765. case SDEV_BLOCK:
  1766. break;
  1767. default:
  1768. goto illegal;
  1769. }
  1770. break;
  1771. case SDEV_DEL:
  1772. switch (oldstate) {
  1773. case SDEV_CANCEL:
  1774. break;
  1775. default:
  1776. goto illegal;
  1777. }
  1778. break;
  1779. }
  1780. sdev->sdev_state = state;
  1781. return 0;
  1782. illegal:
  1783. SCSI_LOG_ERROR_RECOVERY(1,
  1784. sdev_printk(KERN_ERR, sdev,
  1785. "Illegal state transition %s->%s\n",
  1786. scsi_device_state_name(oldstate),
  1787. scsi_device_state_name(state))
  1788. );
  1789. return -EINVAL;
  1790. }
  1791. EXPORT_SYMBOL(scsi_device_set_state);
  1792. /**
  1793. * scsi_device_quiesce - Block user issued commands.
  1794. * @sdev: scsi device to quiesce.
  1795. *
  1796. * This works by trying to transition to the SDEV_QUIESCE state
  1797. * (which must be a legal transition). When the device is in this
  1798. * state, only special requests will be accepted, all others will
  1799. * be deferred. Since special requests may also be requeued requests,
  1800. * a successful return doesn't guarantee the device will be
  1801. * totally quiescent.
  1802. *
  1803. * Must be called with user context, may sleep.
  1804. *
  1805. * Returns zero if unsuccessful or an error if not.
  1806. **/
  1807. int
  1808. scsi_device_quiesce(struct scsi_device *sdev)
  1809. {
  1810. int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
  1811. if (err)
  1812. return err;
  1813. scsi_run_queue(sdev->request_queue);
  1814. while (sdev->device_busy) {
  1815. msleep_interruptible(200);
  1816. scsi_run_queue(sdev->request_queue);
  1817. }
  1818. return 0;
  1819. }
  1820. EXPORT_SYMBOL(scsi_device_quiesce);
  1821. /**
  1822. * scsi_device_resume - Restart user issued commands to a quiesced device.
  1823. * @sdev: scsi device to resume.
  1824. *
  1825. * Moves the device from quiesced back to running and restarts the
  1826. * queues.
  1827. *
  1828. * Must be called with user context, may sleep.
  1829. **/
  1830. void
  1831. scsi_device_resume(struct scsi_device *sdev)
  1832. {
  1833. if(scsi_device_set_state(sdev, SDEV_RUNNING))
  1834. return;
  1835. scsi_run_queue(sdev->request_queue);
  1836. }
  1837. EXPORT_SYMBOL(scsi_device_resume);
  1838. static void
  1839. device_quiesce_fn(struct scsi_device *sdev, void *data)
  1840. {
  1841. scsi_device_quiesce(sdev);
  1842. }
  1843. void
  1844. scsi_target_quiesce(struct scsi_target *starget)
  1845. {
  1846. starget_for_each_device(starget, NULL, device_quiesce_fn);
  1847. }
  1848. EXPORT_SYMBOL(scsi_target_quiesce);
  1849. static void
  1850. device_resume_fn(struct scsi_device *sdev, void *data)
  1851. {
  1852. scsi_device_resume(sdev);
  1853. }
  1854. void
  1855. scsi_target_resume(struct scsi_target *starget)
  1856. {
  1857. starget_for_each_device(starget, NULL, device_resume_fn);
  1858. }
  1859. EXPORT_SYMBOL(scsi_target_resume);
  1860. /**
  1861. * scsi_internal_device_block - internal function to put a device
  1862. * temporarily into the SDEV_BLOCK state
  1863. * @sdev: device to block
  1864. *
  1865. * Block request made by scsi lld's to temporarily stop all
  1866. * scsi commands on the specified device. Called from interrupt
  1867. * or normal process context.
  1868. *
  1869. * Returns zero if successful or error if not
  1870. *
  1871. * Notes:
  1872. * This routine transitions the device to the SDEV_BLOCK state
  1873. * (which must be a legal transition). When the device is in this
  1874. * state, all commands are deferred until the scsi lld reenables
  1875. * the device with scsi_device_unblock or device_block_tmo fires.
  1876. * This routine assumes the host_lock is held on entry.
  1877. **/
  1878. int
  1879. scsi_internal_device_block(struct scsi_device *sdev)
  1880. {
  1881. request_queue_t *q = sdev->request_queue;
  1882. unsigned long flags;
  1883. int err = 0;
  1884. err = scsi_device_set_state(sdev, SDEV_BLOCK);
  1885. if (err)
  1886. return err;
  1887. /*
  1888. * The device has transitioned to SDEV_BLOCK. Stop the
  1889. * block layer from calling the midlayer with this device's
  1890. * request queue.
  1891. */
  1892. spin_lock_irqsave(q->queue_lock, flags);
  1893. blk_stop_queue(q);
  1894. spin_unlock_irqrestore(q->queue_lock, flags);
  1895. return 0;
  1896. }
  1897. EXPORT_SYMBOL_GPL(scsi_internal_device_block);
  1898. /**
  1899. * scsi_internal_device_unblock - resume a device after a block request
  1900. * @sdev: device to resume
  1901. *
  1902. * Called by scsi lld's or the midlayer to restart the device queue
  1903. * for the previously suspended scsi device. Called from interrupt or
  1904. * normal process context.
  1905. *
  1906. * Returns zero if successful or error if not.
  1907. *
  1908. * Notes:
  1909. * This routine transitions the device to the SDEV_RUNNING state
  1910. * (which must be a legal transition) allowing the midlayer to
  1911. * goose the queue for this device. This routine assumes the
  1912. * host_lock is held upon entry.
  1913. **/
  1914. int
  1915. scsi_internal_device_unblock(struct scsi_device *sdev)
  1916. {
  1917. request_queue_t *q = sdev->request_queue;
  1918. int err;
  1919. unsigned long flags;
  1920. /*
  1921. * Try to transition the scsi device to SDEV_RUNNING
  1922. * and goose the device queue if successful.
  1923. */
  1924. err = scsi_device_set_state(sdev, SDEV_RUNNING);
  1925. if (err)
  1926. return err;
  1927. spin_lock_irqsave(q->queue_lock, flags);
  1928. blk_start_queue(q);
  1929. spin_unlock_irqrestore(q->queue_lock, flags);
  1930. return 0;
  1931. }
  1932. EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
  1933. static void
  1934. device_block(struct scsi_device *sdev, void *data)
  1935. {
  1936. scsi_internal_device_block(sdev);
  1937. }
  1938. static int
  1939. target_block(struct device *dev, void *data)
  1940. {
  1941. if (scsi_is_target_device(dev))
  1942. starget_for_each_device(to_scsi_target(dev), NULL,
  1943. device_block);
  1944. return 0;
  1945. }
  1946. void
  1947. scsi_target_block(struct device *dev)
  1948. {
  1949. if (scsi_is_target_device(dev))
  1950. starget_for_each_device(to_scsi_target(dev), NULL,
  1951. device_block);
  1952. else
  1953. device_for_each_child(dev, NULL, target_block);
  1954. }
  1955. EXPORT_SYMBOL_GPL(scsi_target_block);
  1956. static void
  1957. device_unblock(struct scsi_device *sdev, void *data)
  1958. {
  1959. scsi_internal_device_unblock(sdev);
  1960. }
  1961. static int
  1962. target_unblock(struct device *dev, void *data)
  1963. {
  1964. if (scsi_is_target_device(dev))
  1965. starget_for_each_device(to_scsi_target(dev), NULL,
  1966. device_unblock);
  1967. return 0;
  1968. }
  1969. void
  1970. scsi_target_unblock(struct device *dev)
  1971. {
  1972. if (scsi_is_target_device(dev))
  1973. starget_for_each_device(to_scsi_target(dev), NULL,
  1974. device_unblock);
  1975. else
  1976. device_for_each_child(dev, NULL, target_unblock);
  1977. }
  1978. EXPORT_SYMBOL_GPL(scsi_target_unblock);