scsi_lib.c 60 KB

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