blob: c8d0c572409826a109410d0d197c91fa305aeb13 [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. */
Jens Axboe6728cb02008-01-31 13:03:55 +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/**
296 * blk_queue_dma_drain - Set up a drain buffer for excess dma.
297 *
298 * @q: the request queue for the device
299 * @buf: physically contiguous buffer
300 * @size: size of the buffer in bytes
301 *
302 * Some devices have excess DMA problems and can't simply discard (or
303 * zero fill) the unwanted piece of the transfer. They have to have a
304 * real area of memory to transfer it into. The use case for this is
305 * ATAPI devices in DMA mode. If the packet command causes a transfer
306 * bigger than the transfer size some HBAs will lock up if there
307 * aren't DMA elements to contain the excess transfer. What this API
308 * does is adjust the queue so that the buf is always appended
309 * silently to the scatterlist.
310 *
311 * Note: This routine adjusts max_hw_segments to make room for
312 * appending the drain buffer. If you call
313 * blk_queue_max_hw_segments() or blk_queue_max_phys_segments() after
314 * calling this routine, you must set the limit to one fewer than your
315 * device can support otherwise there won't be room for the drain
316 * buffer.
317 */
318int blk_queue_dma_drain(struct request_queue *q, void *buf,
319 unsigned int size)
320{
321 if (q->max_hw_segments < 2 || q->max_phys_segments < 2)
322 return -EINVAL;
323 /* make room for appending the drain */
324 --q->max_hw_segments;
325 --q->max_phys_segments;
326 q->dma_drain_buffer = buf;
327 q->dma_drain_size = size;
328
329 return 0;
330}
Jens Axboe86db1e22008-01-29 14:53:40 +0100331EXPORT_SYMBOL_GPL(blk_queue_dma_drain);
332
333/**
334 * blk_queue_segment_boundary - set boundary rules for segment merging
335 * @q: the request queue for the device
336 * @mask: the memory boundary mask
337 **/
338void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
339{
340 if (mask < PAGE_CACHE_SIZE - 1) {
341 mask = PAGE_CACHE_SIZE - 1;
Jens Axboe6728cb02008-01-31 13:03:55 +0100342 printk(KERN_INFO "%s: set to minimum %lx\n", __FUNCTION__,
343 mask);
Jens Axboe86db1e22008-01-29 14:53:40 +0100344 }
345
346 q->seg_boundary_mask = mask;
347}
Jens Axboe86db1e22008-01-29 14:53:40 +0100348EXPORT_SYMBOL(blk_queue_segment_boundary);
349
350/**
351 * blk_queue_dma_alignment - set dma length and memory alignment
352 * @q: the request queue for the device
353 * @mask: alignment mask
354 *
355 * description:
356 * set required memory and length aligment for direct dma transactions.
357 * this is used when buiding direct io requests for the queue.
358 *
359 **/
360void blk_queue_dma_alignment(struct request_queue *q, int mask)
361{
362 q->dma_alignment = mask;
363}
Jens Axboe86db1e22008-01-29 14:53:40 +0100364EXPORT_SYMBOL(blk_queue_dma_alignment);
365
366/**
367 * blk_queue_update_dma_alignment - update dma length and memory alignment
368 * @q: the request queue for the device
369 * @mask: alignment mask
370 *
371 * description:
372 * update required memory and length aligment for direct dma transactions.
373 * If the requested alignment is larger than the current alignment, then
374 * the current queue alignment is updated to the new value, otherwise it
375 * is left alone. The design of this is to allow multiple objects
376 * (driver, device, transport etc) to set their respective
377 * alignments without having them interfere.
378 *
379 **/
380void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
381{
382 BUG_ON(mask > PAGE_SIZE);
383
384 if (mask > q->dma_alignment)
385 q->dma_alignment = mask;
386}
Jens Axboe86db1e22008-01-29 14:53:40 +0100387EXPORT_SYMBOL(blk_queue_update_dma_alignment);
388
389int __init blk_settings_init(void)
390{
391 blk_max_low_pfn = max_low_pfn - 1;
392 blk_max_pfn = max_pfn - 1;
393 return 0;
394}
395subsys_initcall(blk_settings_init);