scsi_lib.c 67 KB

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