scsi_lib.c 62 KB

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