2 * Functions related to setting various queue properties from drivers
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/init.h>
8 #include <linux/blkdev.h>
9 #include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
13 unsigned long blk_max_low_pfn
;
14 EXPORT_SYMBOL(blk_max_low_pfn
);
16 unsigned long blk_max_pfn
;
17 EXPORT_SYMBOL(blk_max_pfn
);
20 * blk_queue_prep_rq - set a prepare_request function for queue
22 * @pfn: prepare_request function
24 * It's possible for a queue to register a prepare_request callback which
25 * is invoked before the request is handed to the request_fn. The goal of
26 * the function is to prepare a request for I/O, it can be used to build a
27 * cdb from the request data for instance.
30 void blk_queue_prep_rq(struct request_queue
*q
, prep_rq_fn
*pfn
)
34 EXPORT_SYMBOL(blk_queue_prep_rq
);
37 * blk_queue_merge_bvec - set a merge_bvec function for queue
39 * @mbfn: merge_bvec_fn
41 * Usually queues have static limitations on the max sectors or segments that
42 * we can put in a request. Stacking drivers may have some settings that
43 * are dynamic, and thus we have to query the queue whether it is ok to
44 * add a new bio_vec to a bio at a given offset or not. If the block device
45 * has such limitations, it needs to register a merge_bvec_fn to control
46 * the size of bio's sent to it. Note that a block device *must* allow a
47 * single page to be added to an empty bio. The block device driver may want
48 * to use the bio_split() function to deal with these bio's. By default
49 * no merge_bvec_fn is defined for a queue, and only the fixed limits are
52 void blk_queue_merge_bvec(struct request_queue
*q
, merge_bvec_fn
*mbfn
)
54 q
->merge_bvec_fn
= mbfn
;
56 EXPORT_SYMBOL(blk_queue_merge_bvec
);
58 void blk_queue_softirq_done(struct request_queue
*q
, softirq_done_fn
*fn
)
60 q
->softirq_done_fn
= fn
;
62 EXPORT_SYMBOL(blk_queue_softirq_done
);
65 * blk_queue_make_request - define an alternate make_request function for a device
66 * @q: the request queue for the device to be affected
67 * @mfn: the alternate make_request function
70 * The normal way for &struct bios to be passed to a device
71 * driver is for them to be collected into requests on a request
72 * queue, and then to allow the device driver to select requests
73 * off that queue when it is ready. This works well for many block
74 * devices. However some block devices (typically virtual devices
75 * such as md or lvm) do not benefit from the processing on the
76 * request queue, and are served best by having the requests passed
77 * directly to them. This can be achieved by providing a function
78 * to blk_queue_make_request().
81 * The driver that does this *must* be able to deal appropriately
82 * with buffers in "highmemory". This can be accomplished by either calling
83 * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
84 * blk_queue_bounce() to create a buffer in normal memory.
86 void blk_queue_make_request(struct request_queue
*q
, make_request_fn
*mfn
)
91 q
->nr_requests
= BLKDEV_MAX_RQ
;
92 blk_queue_max_phys_segments(q
, MAX_PHYS_SEGMENTS
);
93 blk_queue_max_hw_segments(q
, MAX_HW_SEGMENTS
);
94 q
->make_request_fn
= mfn
;
95 q
->backing_dev_info
.ra_pages
=
96 (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
97 q
->backing_dev_info
.state
= 0;
98 q
->backing_dev_info
.capabilities
= BDI_CAP_MAP_COPY
;
99 blk_queue_max_sectors(q
, SAFE_MAX_SECTORS
);
100 blk_queue_hardsect_size(q
, 512);
101 blk_queue_dma_alignment(q
, 511);
102 blk_queue_congestion_threshold(q
);
103 q
->nr_batching
= BLK_BATCH_REQ
;
105 q
->unplug_thresh
= 4; /* hmm */
106 q
->unplug_delay
= (3 * HZ
) / 1000; /* 3 milliseconds */
107 if (q
->unplug_delay
== 0)
110 INIT_WORK(&q
->unplug_work
, blk_unplug_work
);
112 q
->unplug_timer
.function
= blk_unplug_timeout
;
113 q
->unplug_timer
.data
= (unsigned long)q
;
116 * by default assume old behaviour and bounce for any highmem page
118 blk_queue_bounce_limit(q
, BLK_BOUNCE_HIGH
);
120 EXPORT_SYMBOL(blk_queue_make_request
);
123 * blk_queue_bounce_limit - set bounce buffer limit for queue
124 * @q: the request queue for the device
125 * @dma_addr: bus address limit
128 * Different hardware can have different requirements as to what pages
129 * it can do I/O directly to. A low level driver can call
130 * blk_queue_bounce_limit to have lower memory pages allocated as bounce
131 * buffers for doing I/O to pages residing above @page.
133 void blk_queue_bounce_limit(struct request_queue
*q
, u64 dma_addr
)
135 unsigned long b_pfn
= dma_addr
>> PAGE_SHIFT
;
138 q
->bounce_gfp
= GFP_NOIO
;
139 #if BITS_PER_LONG == 64
140 /* Assume anything <= 4GB can be handled by IOMMU.
141 Actually some IOMMUs can handle everything, but I don't
142 know of a way to test this here. */
143 if (b_pfn
< (min_t(u64
, 0xffffffff, BLK_BOUNCE_HIGH
) >> PAGE_SHIFT
))
145 q
->bounce_pfn
= max_low_pfn
;
147 if (b_pfn
< blk_max_low_pfn
)
149 q
->bounce_pfn
= b_pfn
;
152 init_emergency_isa_pool();
153 q
->bounce_gfp
= GFP_NOIO
| GFP_DMA
;
154 q
->bounce_pfn
= b_pfn
;
157 EXPORT_SYMBOL(blk_queue_bounce_limit
);
160 * blk_queue_max_sectors - set max sectors for a request for this queue
161 * @q: the request queue for the device
162 * @max_sectors: max sectors in the usual 512b unit
165 * Enables a low level driver to set an upper limit on the size of
168 void blk_queue_max_sectors(struct request_queue
*q
, unsigned int max_sectors
)
170 if ((max_sectors
<< 9) < PAGE_CACHE_SIZE
) {
171 max_sectors
= 1 << (PAGE_CACHE_SHIFT
- 9);
172 printk(KERN_INFO
"%s: set to minimum %d\n", __FUNCTION__
,
176 if (BLK_DEF_MAX_SECTORS
> max_sectors
)
177 q
->max_hw_sectors
= q
->max_sectors
= max_sectors
;
179 q
->max_sectors
= BLK_DEF_MAX_SECTORS
;
180 q
->max_hw_sectors
= max_sectors
;
183 EXPORT_SYMBOL(blk_queue_max_sectors
);
186 * blk_queue_max_phys_segments - set max phys segments for a request for this queue
187 * @q: the request queue for the device
188 * @max_segments: max number of segments
191 * Enables a low level driver to set an upper limit on the number of
192 * physical data segments in a request. This would be the largest sized
193 * scatter list the driver could handle.
195 void blk_queue_max_phys_segments(struct request_queue
*q
,
196 unsigned short max_segments
)
200 printk(KERN_INFO
"%s: set to minimum %d\n", __FUNCTION__
,
204 q
->max_phys_segments
= max_segments
;
206 EXPORT_SYMBOL(blk_queue_max_phys_segments
);
209 * blk_queue_max_hw_segments - set max hw segments for a request for this queue
210 * @q: the request queue for the device
211 * @max_segments: max number of segments
214 * Enables a low level driver to set an upper limit on the number of
215 * hw data segments in a request. This would be the largest number of
216 * address/length pairs the host adapter can actually give as once
219 void blk_queue_max_hw_segments(struct request_queue
*q
,
220 unsigned short max_segments
)
224 printk(KERN_INFO
"%s: set to minimum %d\n", __FUNCTION__
,
228 q
->max_hw_segments
= max_segments
;
230 EXPORT_SYMBOL(blk_queue_max_hw_segments
);
233 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
234 * @q: the request queue for the device
235 * @max_size: max size of segment in bytes
238 * Enables a low level driver to set an upper limit on the size of a
241 void blk_queue_max_segment_size(struct request_queue
*q
, unsigned int max_size
)
243 if (max_size
< PAGE_CACHE_SIZE
) {
244 max_size
= PAGE_CACHE_SIZE
;
245 printk(KERN_INFO
"%s: set to minimum %d\n", __FUNCTION__
,
249 q
->max_segment_size
= max_size
;
251 EXPORT_SYMBOL(blk_queue_max_segment_size
);
254 * blk_queue_hardsect_size - set hardware sector size for the queue
255 * @q: the request queue for the device
256 * @size: the hardware sector size, in bytes
259 * This should typically be set to the lowest possible sector size
260 * that the hardware can operate on (possible without reverting to
261 * even internal read-modify-write operations). Usually the default
262 * of 512 covers most hardware.
264 void blk_queue_hardsect_size(struct request_queue
*q
, unsigned short size
)
266 q
->hardsect_size
= size
;
268 EXPORT_SYMBOL(blk_queue_hardsect_size
);
271 * Returns the minimum that is _not_ zero, unless both are zero.
273 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
276 * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
277 * @t: the stacking driver (top)
278 * @b: the underlying device (bottom)
280 void blk_queue_stack_limits(struct request_queue
*t
, struct request_queue
*b
)
282 /* zero is "infinity" */
283 t
->max_sectors
= min_not_zero(t
->max_sectors
, b
->max_sectors
);
284 t
->max_hw_sectors
= min_not_zero(t
->max_hw_sectors
, b
->max_hw_sectors
);
286 t
->max_phys_segments
= min(t
->max_phys_segments
, b
->max_phys_segments
);
287 t
->max_hw_segments
= min(t
->max_hw_segments
, b
->max_hw_segments
);
288 t
->max_segment_size
= min(t
->max_segment_size
, b
->max_segment_size
);
289 t
->hardsect_size
= max(t
->hardsect_size
, b
->hardsect_size
);
290 if (!test_bit(QUEUE_FLAG_CLUSTER
, &b
->queue_flags
))
291 clear_bit(QUEUE_FLAG_CLUSTER
, &t
->queue_flags
);
293 EXPORT_SYMBOL(blk_queue_stack_limits
);
296 * blk_queue_dma_drain - Set up a drain buffer for excess dma.
298 * @q: the request queue for the device
299 * @dma_drain_needed: fn which returns non-zero if drain is necessary
300 * @buf: physically contiguous buffer
301 * @size: size of the buffer in bytes
303 * Some devices have excess DMA problems and can't simply discard (or
304 * zero fill) the unwanted piece of the transfer. They have to have a
305 * real area of memory to transfer it into. The use case for this is
306 * ATAPI devices in DMA mode. If the packet command causes a transfer
307 * bigger than the transfer size some HBAs will lock up if there
308 * aren't DMA elements to contain the excess transfer. What this API
309 * does is adjust the queue so that the buf is always appended
310 * silently to the scatterlist.
312 * Note: This routine adjusts max_hw_segments to make room for
313 * appending the drain buffer. If you call
314 * blk_queue_max_hw_segments() or blk_queue_max_phys_segments() after
315 * calling this routine, you must set the limit to one fewer than your
316 * device can support otherwise there won't be room for the drain
319 extern int blk_queue_dma_drain(struct request_queue
*q
,
320 dma_drain_needed_fn
*dma_drain_needed
,
321 void *buf
, unsigned int size
)
323 if (q
->max_hw_segments
< 2 || q
->max_phys_segments
< 2)
325 /* make room for appending the drain */
326 --q
->max_hw_segments
;
327 --q
->max_phys_segments
;
328 q
->dma_drain_needed
= dma_drain_needed
;
329 q
->dma_drain_buffer
= buf
;
330 q
->dma_drain_size
= size
;
334 EXPORT_SYMBOL_GPL(blk_queue_dma_drain
);
337 * blk_queue_segment_boundary - set boundary rules for segment merging
338 * @q: the request queue for the device
339 * @mask: the memory boundary mask
341 void blk_queue_segment_boundary(struct request_queue
*q
, unsigned long mask
)
343 if (mask
< PAGE_CACHE_SIZE
- 1) {
344 mask
= PAGE_CACHE_SIZE
- 1;
345 printk(KERN_INFO
"%s: set to minimum %lx\n", __FUNCTION__
,
349 q
->seg_boundary_mask
= mask
;
351 EXPORT_SYMBOL(blk_queue_segment_boundary
);
354 * blk_queue_dma_alignment - set dma length and memory alignment
355 * @q: the request queue for the device
356 * @mask: alignment mask
359 * set required memory and length aligment for direct dma transactions.
360 * this is used when buiding direct io requests for the queue.
363 void blk_queue_dma_alignment(struct request_queue
*q
, int mask
)
365 q
->dma_alignment
= mask
;
367 EXPORT_SYMBOL(blk_queue_dma_alignment
);
370 * blk_queue_update_dma_alignment - update dma length and memory alignment
371 * @q: the request queue for the device
372 * @mask: alignment mask
375 * update required memory and length aligment for direct dma transactions.
376 * If the requested alignment is larger than the current alignment, then
377 * the current queue alignment is updated to the new value, otherwise it
378 * is left alone. The design of this is to allow multiple objects
379 * (driver, device, transport etc) to set their respective
380 * alignments without having them interfere.
383 void blk_queue_update_dma_alignment(struct request_queue
*q
, int mask
)
385 BUG_ON(mask
> PAGE_SIZE
);
387 if (mask
> q
->dma_alignment
)
388 q
->dma_alignment
= mask
;
390 EXPORT_SYMBOL(blk_queue_update_dma_alignment
);
392 static int __init
blk_settings_init(void)
394 blk_max_low_pfn
= max_low_pfn
- 1;
395 blk_max_pfn
= max_pfn
- 1;
398 subsys_initcall(blk_settings_init
);