scsi_lib.c 57 KB

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