scsi_lib.c 63 KB

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