scsi_lib.c 57 KB

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