scsi_lib.c 70 KB

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