scsi_lib.c 65 KB

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