scsi_lib.c 62 KB

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