scsi_lib.c 56 KB

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