scsi_lib.c 68 KB

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