blk-settings.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Functions related to setting various queue properties from drivers
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/bio.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
  10. #include "blk.h"
  11. unsigned long blk_max_low_pfn, blk_max_pfn;
  12. EXPORT_SYMBOL(blk_max_low_pfn);
  13. EXPORT_SYMBOL(blk_max_pfn);
  14. /**
  15. * blk_queue_prep_rq - set a prepare_request function for queue
  16. * @q: queue
  17. * @pfn: prepare_request function
  18. *
  19. * It's possible for a queue to register a prepare_request callback which
  20. * is invoked before the request is handed to the request_fn. The goal of
  21. * the function is to prepare a request for I/O, it can be used to build a
  22. * cdb from the request data for instance.
  23. *
  24. */
  25. void blk_queue_prep_rq(struct request_queue *q, prep_rq_fn *pfn)
  26. {
  27. q->prep_rq_fn = pfn;
  28. }
  29. EXPORT_SYMBOL(blk_queue_prep_rq);
  30. /**
  31. * blk_queue_merge_bvec - set a merge_bvec function for queue
  32. * @q: queue
  33. * @mbfn: merge_bvec_fn
  34. *
  35. * Usually queues have static limitations on the max sectors or segments that
  36. * we can put in a request. Stacking drivers may have some settings that
  37. * are dynamic, and thus we have to query the queue whether it is ok to
  38. * add a new bio_vec to a bio at a given offset or not. If the block device
  39. * has such limitations, it needs to register a merge_bvec_fn to control
  40. * the size of bio's sent to it. Note that a block device *must* allow a
  41. * single page to be added to an empty bio. The block device driver may want
  42. * to use the bio_split() function to deal with these bio's. By default
  43. * no merge_bvec_fn is defined for a queue, and only the fixed limits are
  44. * honored.
  45. */
  46. void blk_queue_merge_bvec(struct request_queue *q, merge_bvec_fn *mbfn)
  47. {
  48. q->merge_bvec_fn = mbfn;
  49. }
  50. EXPORT_SYMBOL(blk_queue_merge_bvec);
  51. void blk_queue_softirq_done(struct request_queue *q, softirq_done_fn *fn)
  52. {
  53. q->softirq_done_fn = fn;
  54. }
  55. EXPORT_SYMBOL(blk_queue_softirq_done);
  56. /**
  57. * blk_queue_make_request - define an alternate make_request function for a device
  58. * @q: the request queue for the device to be affected
  59. * @mfn: the alternate make_request function
  60. *
  61. * Description:
  62. * The normal way for &struct bios to be passed to a device
  63. * driver is for them to be collected into requests on a request
  64. * queue, and then to allow the device driver to select requests
  65. * off that queue when it is ready. This works well for many block
  66. * devices. However some block devices (typically virtual devices
  67. * such as md or lvm) do not benefit from the processing on the
  68. * request queue, and are served best by having the requests passed
  69. * directly to them. This can be achieved by providing a function
  70. * to blk_queue_make_request().
  71. *
  72. * Caveat:
  73. * The driver that does this *must* be able to deal appropriately
  74. * with buffers in "highmemory". This can be accomplished by either calling
  75. * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
  76. * blk_queue_bounce() to create a buffer in normal memory.
  77. **/
  78. void blk_queue_make_request(struct request_queue * q, make_request_fn * mfn)
  79. {
  80. /*
  81. * set defaults
  82. */
  83. q->nr_requests = BLKDEV_MAX_RQ;
  84. blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
  85. blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
  86. q->make_request_fn = mfn;
  87. q->backing_dev_info.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  88. q->backing_dev_info.state = 0;
  89. q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
  90. blk_queue_max_sectors(q, SAFE_MAX_SECTORS);
  91. blk_queue_hardsect_size(q, 512);
  92. blk_queue_dma_alignment(q, 511);
  93. blk_queue_congestion_threshold(q);
  94. q->nr_batching = BLK_BATCH_REQ;
  95. q->unplug_thresh = 4; /* hmm */
  96. q->unplug_delay = (3 * HZ) / 1000; /* 3 milliseconds */
  97. if (q->unplug_delay == 0)
  98. q->unplug_delay = 1;
  99. INIT_WORK(&q->unplug_work, blk_unplug_work);
  100. q->unplug_timer.function = blk_unplug_timeout;
  101. q->unplug_timer.data = (unsigned long)q;
  102. /*
  103. * by default assume old behaviour and bounce for any highmem page
  104. */
  105. blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
  106. }
  107. EXPORT_SYMBOL(blk_queue_make_request);
  108. /**
  109. * blk_queue_bounce_limit - set bounce buffer limit for queue
  110. * @q: the request queue for the device
  111. * @dma_addr: bus address limit
  112. *
  113. * Description:
  114. * Different hardware can have different requirements as to what pages
  115. * it can do I/O directly to. A low level driver can call
  116. * blk_queue_bounce_limit to have lower memory pages allocated as bounce
  117. * buffers for doing I/O to pages residing above @page.
  118. **/
  119. void blk_queue_bounce_limit(struct request_queue *q, u64 dma_addr)
  120. {
  121. unsigned long bounce_pfn = dma_addr >> PAGE_SHIFT;
  122. int dma = 0;
  123. q->bounce_gfp = GFP_NOIO;
  124. #if BITS_PER_LONG == 64
  125. /* Assume anything <= 4GB can be handled by IOMMU.
  126. Actually some IOMMUs can handle everything, but I don't
  127. know of a way to test this here. */
  128. if (bounce_pfn < (min_t(u64,0xffffffff,BLK_BOUNCE_HIGH) >> PAGE_SHIFT))
  129. dma = 1;
  130. q->bounce_pfn = max_low_pfn;
  131. #else
  132. if (bounce_pfn < blk_max_low_pfn)
  133. dma = 1;
  134. q->bounce_pfn = bounce_pfn;
  135. #endif
  136. if (dma) {
  137. init_emergency_isa_pool();
  138. q->bounce_gfp = GFP_NOIO | GFP_DMA;
  139. q->bounce_pfn = bounce_pfn;
  140. }
  141. }
  142. EXPORT_SYMBOL(blk_queue_bounce_limit);
  143. /**
  144. * blk_queue_max_sectors - set max sectors for a request for this queue
  145. * @q: the request queue for the device
  146. * @max_sectors: max sectors in the usual 512b unit
  147. *
  148. * Description:
  149. * Enables a low level driver to set an upper limit on the size of
  150. * received requests.
  151. **/
  152. void blk_queue_max_sectors(struct request_queue *q, unsigned int max_sectors)
  153. {
  154. if ((max_sectors << 9) < PAGE_CACHE_SIZE) {
  155. max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
  156. printk("%s: set to minimum %d\n", __FUNCTION__, max_sectors);
  157. }
  158. if (BLK_DEF_MAX_SECTORS > max_sectors)
  159. q->max_hw_sectors = q->max_sectors = max_sectors;
  160. else {
  161. q->max_sectors = BLK_DEF_MAX_SECTORS;
  162. q->max_hw_sectors = max_sectors;
  163. }
  164. }
  165. EXPORT_SYMBOL(blk_queue_max_sectors);
  166. /**
  167. * blk_queue_max_phys_segments - set max phys segments for a request for this queue
  168. * @q: the request queue for the device
  169. * @max_segments: max number of segments
  170. *
  171. * Description:
  172. * Enables a low level driver to set an upper limit on the number of
  173. * physical data segments in a request. This would be the largest sized
  174. * scatter list the driver could handle.
  175. **/
  176. void blk_queue_max_phys_segments(struct request_queue *q,
  177. unsigned short max_segments)
  178. {
  179. if (!max_segments) {
  180. max_segments = 1;
  181. printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
  182. }
  183. q->max_phys_segments = max_segments;
  184. }
  185. EXPORT_SYMBOL(blk_queue_max_phys_segments);
  186. /**
  187. * blk_queue_max_hw_segments - set max hw segments for a request for this queue
  188. * @q: the request queue for the device
  189. * @max_segments: max number of segments
  190. *
  191. * Description:
  192. * Enables a low level driver to set an upper limit on the number of
  193. * hw data segments in a request. This would be the largest number of
  194. * address/length pairs the host adapter can actually give as once
  195. * to the device.
  196. **/
  197. void blk_queue_max_hw_segments(struct request_queue *q,
  198. unsigned short max_segments)
  199. {
  200. if (!max_segments) {
  201. max_segments = 1;
  202. printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
  203. }
  204. q->max_hw_segments = max_segments;
  205. }
  206. EXPORT_SYMBOL(blk_queue_max_hw_segments);
  207. /**
  208. * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
  209. * @q: the request queue for the device
  210. * @max_size: max size of segment in bytes
  211. *
  212. * Description:
  213. * Enables a low level driver to set an upper limit on the size of a
  214. * coalesced segment
  215. **/
  216. void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
  217. {
  218. if (max_size < PAGE_CACHE_SIZE) {
  219. max_size = PAGE_CACHE_SIZE;
  220. printk("%s: set to minimum %d\n", __FUNCTION__, max_size);
  221. }
  222. q->max_segment_size = max_size;
  223. }
  224. EXPORT_SYMBOL(blk_queue_max_segment_size);
  225. /**
  226. * blk_queue_hardsect_size - set hardware sector size for the queue
  227. * @q: the request queue for the device
  228. * @size: the hardware sector size, in bytes
  229. *
  230. * Description:
  231. * This should typically be set to the lowest possible sector size
  232. * that the hardware can operate on (possible without reverting to
  233. * even internal read-modify-write operations). Usually the default
  234. * of 512 covers most hardware.
  235. **/
  236. void blk_queue_hardsect_size(struct request_queue *q, unsigned short size)
  237. {
  238. q->hardsect_size = size;
  239. }
  240. EXPORT_SYMBOL(blk_queue_hardsect_size);
  241. /*
  242. * Returns the minimum that is _not_ zero, unless both are zero.
  243. */
  244. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  245. /**
  246. * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
  247. * @t: the stacking driver (top)
  248. * @b: the underlying device (bottom)
  249. **/
  250. void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b)
  251. {
  252. /* zero is "infinity" */
  253. t->max_sectors = min_not_zero(t->max_sectors,b->max_sectors);
  254. t->max_hw_sectors = min_not_zero(t->max_hw_sectors,b->max_hw_sectors);
  255. t->max_phys_segments = min(t->max_phys_segments,b->max_phys_segments);
  256. t->max_hw_segments = min(t->max_hw_segments,b->max_hw_segments);
  257. t->max_segment_size = min(t->max_segment_size,b->max_segment_size);
  258. t->hardsect_size = max(t->hardsect_size,b->hardsect_size);
  259. if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags))
  260. clear_bit(QUEUE_FLAG_CLUSTER, &t->queue_flags);
  261. }
  262. EXPORT_SYMBOL(blk_queue_stack_limits);
  263. /**
  264. * blk_queue_dma_drain - Set up a drain buffer for excess dma.
  265. *
  266. * @q: the request queue for the device
  267. * @buf: physically contiguous buffer
  268. * @size: size of the buffer in bytes
  269. *
  270. * Some devices have excess DMA problems and can't simply discard (or
  271. * zero fill) the unwanted piece of the transfer. They have to have a
  272. * real area of memory to transfer it into. The use case for this is
  273. * ATAPI devices in DMA mode. If the packet command causes a transfer
  274. * bigger than the transfer size some HBAs will lock up if there
  275. * aren't DMA elements to contain the excess transfer. What this API
  276. * does is adjust the queue so that the buf is always appended
  277. * silently to the scatterlist.
  278. *
  279. * Note: This routine adjusts max_hw_segments to make room for
  280. * appending the drain buffer. If you call
  281. * blk_queue_max_hw_segments() or blk_queue_max_phys_segments() after
  282. * calling this routine, you must set the limit to one fewer than your
  283. * device can support otherwise there won't be room for the drain
  284. * buffer.
  285. */
  286. int blk_queue_dma_drain(struct request_queue *q, void *buf,
  287. unsigned int size)
  288. {
  289. if (q->max_hw_segments < 2 || q->max_phys_segments < 2)
  290. return -EINVAL;
  291. /* make room for appending the drain */
  292. --q->max_hw_segments;
  293. --q->max_phys_segments;
  294. q->dma_drain_buffer = buf;
  295. q->dma_drain_size = size;
  296. return 0;
  297. }
  298. EXPORT_SYMBOL_GPL(blk_queue_dma_drain);
  299. /**
  300. * blk_queue_segment_boundary - set boundary rules for segment merging
  301. * @q: the request queue for the device
  302. * @mask: the memory boundary mask
  303. **/
  304. void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
  305. {
  306. if (mask < PAGE_CACHE_SIZE - 1) {
  307. mask = PAGE_CACHE_SIZE - 1;
  308. printk("%s: set to minimum %lx\n", __FUNCTION__, mask);
  309. }
  310. q->seg_boundary_mask = mask;
  311. }
  312. EXPORT_SYMBOL(blk_queue_segment_boundary);
  313. /**
  314. * blk_queue_dma_alignment - set dma length and memory alignment
  315. * @q: the request queue for the device
  316. * @mask: alignment mask
  317. *
  318. * description:
  319. * set required memory and length aligment for direct dma transactions.
  320. * this is used when buiding direct io requests for the queue.
  321. *
  322. **/
  323. void blk_queue_dma_alignment(struct request_queue *q, int mask)
  324. {
  325. q->dma_alignment = mask;
  326. }
  327. EXPORT_SYMBOL(blk_queue_dma_alignment);
  328. /**
  329. * blk_queue_update_dma_alignment - update dma length and memory alignment
  330. * @q: the request queue for the device
  331. * @mask: alignment mask
  332. *
  333. * description:
  334. * update required memory and length aligment for direct dma transactions.
  335. * If the requested alignment is larger than the current alignment, then
  336. * the current queue alignment is updated to the new value, otherwise it
  337. * is left alone. The design of this is to allow multiple objects
  338. * (driver, device, transport etc) to set their respective
  339. * alignments without having them interfere.
  340. *
  341. **/
  342. void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
  343. {
  344. BUG_ON(mask > PAGE_SIZE);
  345. if (mask > q->dma_alignment)
  346. q->dma_alignment = mask;
  347. }
  348. EXPORT_SYMBOL(blk_queue_update_dma_alignment);
  349. int __init blk_settings_init(void)
  350. {
  351. blk_max_low_pfn = max_low_pfn - 1;
  352. blk_max_pfn = max_pfn - 1;
  353. return 0;
  354. }
  355. subsys_initcall(blk_settings_init);