scsi_lib.c 57 KB

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