scsi_lib.c 56 KB

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