scsi_lib.c 63 KB

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