scsi_lib.c 57 KB

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