blob: 1344a0ea5cc6c00a89ef9536cbbfda7183d2b28b [file] [log] [blame]
Jens Axboe86db1e22008-01-29 14:53:40 +01001/*
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
11#include "blk.h"
12
Jens Axboe6728cb02008-01-31 13:03:55 +010013unsigned long blk_max_low_pfn;
Jens Axboe86db1e22008-01-29 14:53:40 +010014EXPORT_SYMBOL(blk_max_low_pfn);
Jens Axboe6728cb02008-01-31 13:03:55 +010015
16unsigned long blk_max_pfn;
Jens Axboe86db1e22008-01-29 14:53:40 +010017EXPORT_SYMBOL(blk_max_pfn);
18
19/**
20 * blk_queue_prep_rq - set a prepare_request function for queue
21 * @q: queue
22 * @pfn: prepare_request function
23 *
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.
28 *
29 */
30void blk_queue_prep_rq(struct request_queue *q, prep_rq_fn *pfn)
31{
32 q->prep_rq_fn = pfn;
33}
Jens Axboe86db1e22008-01-29 14:53:40 +010034EXPORT_SYMBOL(blk_queue_prep_rq);
35
36/**
37 * blk_queue_merge_bvec - set a merge_bvec function for queue
38 * @q: queue
39 * @mbfn: merge_bvec_fn
40 *
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
50 * honored.
51 */
52void blk_queue_merge_bvec(struct request_queue *q, merge_bvec_fn *mbfn)
53{
54 q->merge_bvec_fn = mbfn;
55}
Jens Axboe86db1e22008-01-29 14:53:40 +010056EXPORT_SYMBOL(blk_queue_merge_bvec);
57
58void blk_queue_softirq_done(struct request_queue *q, softirq_done_fn *fn)
59{
60 q->softirq_done_fn = fn;
61}
Jens Axboe86db1e22008-01-29 14:53:40 +010062EXPORT_SYMBOL(blk_queue_softirq_done);
63
64/**
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
68 *
69 * Description:
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().
79 *
80 * Caveat:
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.
85 **/
Jens Axboe6728cb02008-01-31 13:03:55 +010086void blk_queue_make_request(struct request_queue *q, make_request_fn *mfn)
Jens Axboe86db1e22008-01-29 14:53:40 +010087{
88 /*
89 * set defaults
90 */
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;
Jens Axboe6728cb02008-01-31 13:03:55 +010095 q->backing_dev_info.ra_pages =
96 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
Jens Axboe86db1e22008-01-29 14:53:40 +010097 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;
104
105 q->unplug_thresh = 4; /* hmm */
106 q->unplug_delay = (3 * HZ) / 1000; /* 3 milliseconds */
107 if (q->unplug_delay == 0)
108 q->unplug_delay = 1;
109
110 INIT_WORK(&q->unplug_work, blk_unplug_work);
111
112 q->unplug_timer.function = blk_unplug_timeout;
113 q->unplug_timer.data = (unsigned long)q;
114
115 /*
116 * by default assume old behaviour and bounce for any highmem page
117 */
118 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
119}
Jens Axboe86db1e22008-01-29 14:53:40 +0100120EXPORT_SYMBOL(blk_queue_make_request);
121
122/**
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
126 *
127 * Description:
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.
132 **/
133void blk_queue_bounce_limit(struct request_queue *q, u64 dma_addr)
134{
Jens Axboe6728cb02008-01-31 13:03:55 +0100135 unsigned long b_pfn = dma_addr >> PAGE_SHIFT;
Jens Axboe86db1e22008-01-29 14:53:40 +0100136 int dma = 0;
137
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. */
Yang Shi419c4342008-03-04 11:20:51 +0100143 if (b_pfn <= (min_t(u64, 0xffffffff, BLK_BOUNCE_HIGH) >> PAGE_SHIFT))
Jens Axboe86db1e22008-01-29 14:53:40 +0100144 dma = 1;
145 q->bounce_pfn = max_low_pfn;
146#else
Jens Axboe6728cb02008-01-31 13:03:55 +0100147 if (b_pfn < blk_max_low_pfn)
Jens Axboe86db1e22008-01-29 14:53:40 +0100148 dma = 1;
Jens Axboe6728cb02008-01-31 13:03:55 +0100149 q->bounce_pfn = b_pfn;
Jens Axboe86db1e22008-01-29 14:53:40 +0100150#endif
151 if (dma) {
152 init_emergency_isa_pool();
153 q->bounce_gfp = GFP_NOIO | GFP_DMA;
Jens Axboe6728cb02008-01-31 13:03:55 +0100154 q->bounce_pfn = b_pfn;
Jens Axboe86db1e22008-01-29 14:53:40 +0100155 }
156}
Jens Axboe86db1e22008-01-29 14:53:40 +0100157EXPORT_SYMBOL(blk_queue_bounce_limit);
158
159/**
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
163 *
164 * Description:
165 * Enables a low level driver to set an upper limit on the size of
166 * received requests.
167 **/
168void blk_queue_max_sectors(struct request_queue *q, unsigned int max_sectors)
169{
170 if ((max_sectors << 9) < PAGE_CACHE_SIZE) {
171 max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
Jens Axboe6728cb02008-01-31 13:03:55 +0100172 printk(KERN_INFO "%s: set to minimum %d\n", __FUNCTION__,
173 max_sectors);
Jens Axboe86db1e22008-01-29 14:53:40 +0100174 }
175
176 if (BLK_DEF_MAX_SECTORS > max_sectors)
177 q->max_hw_sectors = q->max_sectors = max_sectors;
178 else {
179 q->max_sectors = BLK_DEF_MAX_SECTORS;
180 q->max_hw_sectors = max_sectors;
181 }
182}
Jens Axboe86db1e22008-01-29 14:53:40 +0100183EXPORT_SYMBOL(blk_queue_max_sectors);
184
185/**
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
189 *
190 * Description:
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.
194 **/
195void blk_queue_max_phys_segments(struct request_queue *q,
196 unsigned short max_segments)
197{
198 if (!max_segments) {
199 max_segments = 1;
Jens Axboe6728cb02008-01-31 13:03:55 +0100200 printk(KERN_INFO "%s: set to minimum %d\n", __FUNCTION__,
201 max_segments);
Jens Axboe86db1e22008-01-29 14:53:40 +0100202 }
203
204 q->max_phys_segments = max_segments;
205}
Jens Axboe86db1e22008-01-29 14:53:40 +0100206EXPORT_SYMBOL(blk_queue_max_phys_segments);
207
208/**
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
212 *
213 * Description:
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
217 * to the device.
218 **/
219void blk_queue_max_hw_segments(struct request_queue *q,
220 unsigned short max_segments)
221{
222 if (!max_segments) {
223 max_segments = 1;
Jens Axboe6728cb02008-01-31 13:03:55 +0100224 printk(KERN_INFO "%s: set to minimum %d\n", __FUNCTION__,
225 max_segments);
Jens Axboe86db1e22008-01-29 14:53:40 +0100226 }
227
228 q->max_hw_segments = max_segments;
229}
Jens Axboe86db1e22008-01-29 14:53:40 +0100230EXPORT_SYMBOL(blk_queue_max_hw_segments);
231
232/**
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
236 *
237 * Description:
238 * Enables a low level driver to set an upper limit on the size of a
239 * coalesced segment
240 **/
241void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
242{
243 if (max_size < PAGE_CACHE_SIZE) {
244 max_size = PAGE_CACHE_SIZE;
Jens Axboe6728cb02008-01-31 13:03:55 +0100245 printk(KERN_INFO "%s: set to minimum %d\n", __FUNCTION__,
246 max_size);
Jens Axboe86db1e22008-01-29 14:53:40 +0100247 }
248
249 q->max_segment_size = max_size;
250}
Jens Axboe86db1e22008-01-29 14:53:40 +0100251EXPORT_SYMBOL(blk_queue_max_segment_size);
252
253/**
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
257 *
258 * Description:
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.
263 **/
264void blk_queue_hardsect_size(struct request_queue *q, unsigned short size)
265{
266 q->hardsect_size = size;
267}
Jens Axboe86db1e22008-01-29 14:53:40 +0100268EXPORT_SYMBOL(blk_queue_hardsect_size);
269
270/*
271 * Returns the minimum that is _not_ zero, unless both are zero.
272 */
273#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
274
275/**
276 * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
277 * @t: the stacking driver (top)
278 * @b: the underlying device (bottom)
279 **/
280void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b)
281{
282 /* zero is "infinity" */
Jens Axboe6728cb02008-01-31 13:03:55 +0100283 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);
Jens Axboe86db1e22008-01-29 14:53:40 +0100285
Jens Axboe6728cb02008-01-31 13:03:55 +0100286 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);
Jens Axboe86db1e22008-01-29 14:53:40 +0100290 if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags))
291 clear_bit(QUEUE_FLAG_CLUSTER, &t->queue_flags);
292}
Jens Axboe86db1e22008-01-29 14:53:40 +0100293EXPORT_SYMBOL(blk_queue_stack_limits);
294
295/**
Tejun Heoe3790c72008-03-04 11:18:17 +0100296 * blk_queue_dma_pad - set pad mask
297 * @q: the request queue for the device
298 * @mask: pad mask
299 *
300 * Set pad mask. Direct IO requests are padded to the mask specified.
301 *
302 * Appending pad buffer to a request modifies ->data_len such that it
303 * includes the pad buffer. The original requested data length can be
304 * obtained using blk_rq_raw_data_len().
305 **/
306void blk_queue_dma_pad(struct request_queue *q, unsigned int mask)
307{
308 q->dma_pad_mask = mask;
309}
310EXPORT_SYMBOL(blk_queue_dma_pad);
311
312/**
Jens Axboe86db1e22008-01-29 14:53:40 +0100313 * blk_queue_dma_drain - Set up a drain buffer for excess dma.
Jens Axboe86db1e22008-01-29 14:53:40 +0100314 * @q: the request queue for the device
Tejun Heo2fb98e82008-02-19 11:36:53 +0100315 * @dma_drain_needed: fn which returns non-zero if drain is necessary
Jens Axboe86db1e22008-01-29 14:53:40 +0100316 * @buf: physically contiguous buffer
317 * @size: size of the buffer in bytes
318 *
319 * Some devices have excess DMA problems and can't simply discard (or
320 * zero fill) the unwanted piece of the transfer. They have to have a
321 * real area of memory to transfer it into. The use case for this is
322 * ATAPI devices in DMA mode. If the packet command causes a transfer
323 * bigger than the transfer size some HBAs will lock up if there
324 * aren't DMA elements to contain the excess transfer. What this API
325 * does is adjust the queue so that the buf is always appended
326 * silently to the scatterlist.
327 *
328 * Note: This routine adjusts max_hw_segments to make room for
329 * appending the drain buffer. If you call
330 * blk_queue_max_hw_segments() or blk_queue_max_phys_segments() after
331 * calling this routine, you must set the limit to one fewer than your
332 * device can support otherwise there won't be room for the drain
333 * buffer.
334 */
Harvey Harrison448da4d2008-03-04 11:30:18 +0100335int blk_queue_dma_drain(struct request_queue *q,
Tejun Heo2fb98e82008-02-19 11:36:53 +0100336 dma_drain_needed_fn *dma_drain_needed,
337 void *buf, unsigned int size)
Jens Axboe86db1e22008-01-29 14:53:40 +0100338{
339 if (q->max_hw_segments < 2 || q->max_phys_segments < 2)
340 return -EINVAL;
341 /* make room for appending the drain */
342 --q->max_hw_segments;
343 --q->max_phys_segments;
Tejun Heo2fb98e82008-02-19 11:36:53 +0100344 q->dma_drain_needed = dma_drain_needed;
Jens Axboe86db1e22008-01-29 14:53:40 +0100345 q->dma_drain_buffer = buf;
346 q->dma_drain_size = size;
347
348 return 0;
349}
Jens Axboe86db1e22008-01-29 14:53:40 +0100350EXPORT_SYMBOL_GPL(blk_queue_dma_drain);
351
352/**
353 * blk_queue_segment_boundary - set boundary rules for segment merging
354 * @q: the request queue for the device
355 * @mask: the memory boundary mask
356 **/
357void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
358{
359 if (mask < PAGE_CACHE_SIZE - 1) {
360 mask = PAGE_CACHE_SIZE - 1;
Jens Axboe6728cb02008-01-31 13:03:55 +0100361 printk(KERN_INFO "%s: set to minimum %lx\n", __FUNCTION__,
362 mask);
Jens Axboe86db1e22008-01-29 14:53:40 +0100363 }
364
365 q->seg_boundary_mask = mask;
366}
Jens Axboe86db1e22008-01-29 14:53:40 +0100367EXPORT_SYMBOL(blk_queue_segment_boundary);
368
369/**
370 * blk_queue_dma_alignment - set dma length and memory alignment
371 * @q: the request queue for the device
372 * @mask: alignment mask
373 *
374 * description:
375 * set required memory and length aligment for direct dma transactions.
376 * this is used when buiding direct io requests for the queue.
377 *
378 **/
379void blk_queue_dma_alignment(struct request_queue *q, int mask)
380{
381 q->dma_alignment = mask;
382}
Jens Axboe86db1e22008-01-29 14:53:40 +0100383EXPORT_SYMBOL(blk_queue_dma_alignment);
384
385/**
386 * blk_queue_update_dma_alignment - update dma length and memory alignment
387 * @q: the request queue for the device
388 * @mask: alignment mask
389 *
390 * description:
391 * update required memory and length aligment for direct dma transactions.
392 * If the requested alignment is larger than the current alignment, then
393 * the current queue alignment is updated to the new value, otherwise it
394 * is left alone. The design of this is to allow multiple objects
395 * (driver, device, transport etc) to set their respective
396 * alignments without having them interfere.
397 *
398 **/
399void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
400{
401 BUG_ON(mask > PAGE_SIZE);
402
403 if (mask > q->dma_alignment)
404 q->dma_alignment = mask;
405}
Jens Axboe86db1e22008-01-29 14:53:40 +0100406EXPORT_SYMBOL(blk_queue_update_dma_alignment);
407
Adrian Bunk52ff4ca2008-02-18 13:45:55 +0100408static int __init blk_settings_init(void)
Jens Axboe86db1e22008-01-29 14:53:40 +0100409{
410 blk_max_low_pfn = max_low_pfn - 1;
411 blk_max_pfn = max_pfn - 1;
412 return 0;
413}
414subsys_initcall(blk_settings_init);