scsi_lib.c 60 KB

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