block: use normal I/O path for discard requests
[safe/jmp/linux-2.6] / block / blk-settings.c
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 <linux/gcd.h>
11
12 #include "blk.h"
13
14 unsigned long blk_max_low_pfn;
15 EXPORT_SYMBOL(blk_max_low_pfn);
16
17 unsigned long 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  */
30 void blk_queue_prep_rq(struct request_queue *q, prep_rq_fn *pfn)
31 {
32         q->prep_rq_fn = pfn;
33 }
34 EXPORT_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  */
52 void blk_queue_merge_bvec(struct request_queue *q, merge_bvec_fn *mbfn)
53 {
54         q->merge_bvec_fn = mbfn;
55 }
56 EXPORT_SYMBOL(blk_queue_merge_bvec);
57
58 void blk_queue_softirq_done(struct request_queue *q, softirq_done_fn *fn)
59 {
60         q->softirq_done_fn = fn;
61 }
62 EXPORT_SYMBOL(blk_queue_softirq_done);
63
64 void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout)
65 {
66         q->rq_timeout = timeout;
67 }
68 EXPORT_SYMBOL_GPL(blk_queue_rq_timeout);
69
70 void blk_queue_rq_timed_out(struct request_queue *q, rq_timed_out_fn *fn)
71 {
72         q->rq_timed_out_fn = fn;
73 }
74 EXPORT_SYMBOL_GPL(blk_queue_rq_timed_out);
75
76 void blk_queue_lld_busy(struct request_queue *q, lld_busy_fn *fn)
77 {
78         q->lld_busy_fn = fn;
79 }
80 EXPORT_SYMBOL_GPL(blk_queue_lld_busy);
81
82 /**
83  * blk_set_default_limits - reset limits to default values
84  * @lim:  the queue_limits structure to reset
85  *
86  * Description:
87  *   Returns a queue_limit struct to its default state.  Can be used by
88  *   stacking drivers like DM that stage table swaps and reuse an
89  *   existing device queue.
90  */
91 void blk_set_default_limits(struct queue_limits *lim)
92 {
93         lim->max_phys_segments = MAX_PHYS_SEGMENTS;
94         lim->max_hw_segments = MAX_HW_SEGMENTS;
95         lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
96         lim->max_segment_size = MAX_SEGMENT_SIZE;
97         lim->max_sectors = BLK_DEF_MAX_SECTORS;
98         lim->max_hw_sectors = INT_MAX;
99         lim->logical_block_size = lim->physical_block_size = lim->io_min = 512;
100         lim->bounce_pfn = (unsigned long)(BLK_BOUNCE_ANY >> PAGE_SHIFT);
101         lim->alignment_offset = 0;
102         lim->io_opt = 0;
103         lim->misaligned = 0;
104         lim->no_cluster = 0;
105 }
106 EXPORT_SYMBOL(blk_set_default_limits);
107
108 /**
109  * blk_queue_make_request - define an alternate make_request function for a device
110  * @q:  the request queue for the device to be affected
111  * @mfn: the alternate make_request function
112  *
113  * Description:
114  *    The normal way for &struct bios to be passed to a device
115  *    driver is for them to be collected into requests on a request
116  *    queue, and then to allow the device driver to select requests
117  *    off that queue when it is ready.  This works well for many block
118  *    devices. However some block devices (typically virtual devices
119  *    such as md or lvm) do not benefit from the processing on the
120  *    request queue, and are served best by having the requests passed
121  *    directly to them.  This can be achieved by providing a function
122  *    to blk_queue_make_request().
123  *
124  * Caveat:
125  *    The driver that does this *must* be able to deal appropriately
126  *    with buffers in "highmemory". This can be accomplished by either calling
127  *    __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
128  *    blk_queue_bounce() to create a buffer in normal memory.
129  **/
130 void blk_queue_make_request(struct request_queue *q, make_request_fn *mfn)
131 {
132         /*
133          * set defaults
134          */
135         q->nr_requests = BLKDEV_MAX_RQ;
136
137         q->make_request_fn = mfn;
138         blk_queue_dma_alignment(q, 511);
139         blk_queue_congestion_threshold(q);
140         q->nr_batching = BLK_BATCH_REQ;
141
142         q->unplug_thresh = 4;           /* hmm */
143         q->unplug_delay = (3 * HZ) / 1000;      /* 3 milliseconds */
144         if (q->unplug_delay == 0)
145                 q->unplug_delay = 1;
146
147         q->unplug_timer.function = blk_unplug_timeout;
148         q->unplug_timer.data = (unsigned long)q;
149
150         blk_set_default_limits(&q->limits);
151         blk_queue_max_sectors(q, SAFE_MAX_SECTORS);
152
153         /*
154          * If the caller didn't supply a lock, fall back to our embedded
155          * per-queue locks
156          */
157         if (!q->queue_lock)
158                 q->queue_lock = &q->__queue_lock;
159
160         /*
161          * by default assume old behaviour and bounce for any highmem page
162          */
163         blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
164 }
165 EXPORT_SYMBOL(blk_queue_make_request);
166
167 /**
168  * blk_queue_bounce_limit - set bounce buffer limit for queue
169  * @q: the request queue for the device
170  * @dma_mask: the maximum address the device can handle
171  *
172  * Description:
173  *    Different hardware can have different requirements as to what pages
174  *    it can do I/O directly to. A low level driver can call
175  *    blk_queue_bounce_limit to have lower memory pages allocated as bounce
176  *    buffers for doing I/O to pages residing above @dma_mask.
177  **/
178 void blk_queue_bounce_limit(struct request_queue *q, u64 dma_mask)
179 {
180         unsigned long b_pfn = dma_mask >> PAGE_SHIFT;
181         int dma = 0;
182
183         q->bounce_gfp = GFP_NOIO;
184 #if BITS_PER_LONG == 64
185         /*
186          * Assume anything <= 4GB can be handled by IOMMU.  Actually
187          * some IOMMUs can handle everything, but I don't know of a
188          * way to test this here.
189          */
190         if (b_pfn < (min_t(u64, 0xffffffffUL, BLK_BOUNCE_HIGH) >> PAGE_SHIFT))
191                 dma = 1;
192         q->limits.bounce_pfn = max_low_pfn;
193 #else
194         if (b_pfn < blk_max_low_pfn)
195                 dma = 1;
196         q->limits.bounce_pfn = b_pfn;
197 #endif
198         if (dma) {
199                 init_emergency_isa_pool();
200                 q->bounce_gfp = GFP_NOIO | GFP_DMA;
201                 q->limits.bounce_pfn = b_pfn;
202         }
203 }
204 EXPORT_SYMBOL(blk_queue_bounce_limit);
205
206 /**
207  * blk_queue_max_sectors - set max sectors for a request for this queue
208  * @q:  the request queue for the device
209  * @max_sectors:  max sectors in the usual 512b unit
210  *
211  * Description:
212  *    Enables a low level driver to set an upper limit on the size of
213  *    received requests.
214  **/
215 void blk_queue_max_sectors(struct request_queue *q, unsigned int max_sectors)
216 {
217         if ((max_sectors << 9) < PAGE_CACHE_SIZE) {
218                 max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
219                 printk(KERN_INFO "%s: set to minimum %d\n",
220                        __func__, max_sectors);
221         }
222
223         if (BLK_DEF_MAX_SECTORS > max_sectors)
224                 q->limits.max_hw_sectors = q->limits.max_sectors = max_sectors;
225         else {
226                 q->limits.max_sectors = BLK_DEF_MAX_SECTORS;
227                 q->limits.max_hw_sectors = max_sectors;
228         }
229 }
230 EXPORT_SYMBOL(blk_queue_max_sectors);
231
232 void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_sectors)
233 {
234         if (BLK_DEF_MAX_SECTORS > max_sectors)
235                 q->limits.max_hw_sectors = BLK_DEF_MAX_SECTORS;
236         else
237                 q->limits.max_hw_sectors = max_sectors;
238 }
239 EXPORT_SYMBOL(blk_queue_max_hw_sectors);
240
241 /**
242  * blk_queue_max_phys_segments - set max phys segments for a request for this queue
243  * @q:  the request queue for the device
244  * @max_segments:  max number of segments
245  *
246  * Description:
247  *    Enables a low level driver to set an upper limit on the number of
248  *    physical data segments in a request.  This would be the largest sized
249  *    scatter list the driver could handle.
250  **/
251 void blk_queue_max_phys_segments(struct request_queue *q,
252                                  unsigned short max_segments)
253 {
254         if (!max_segments) {
255                 max_segments = 1;
256                 printk(KERN_INFO "%s: set to minimum %d\n",
257                        __func__, max_segments);
258         }
259
260         q->limits.max_phys_segments = max_segments;
261 }
262 EXPORT_SYMBOL(blk_queue_max_phys_segments);
263
264 /**
265  * blk_queue_max_hw_segments - set max hw segments for a request for this queue
266  * @q:  the request queue for the device
267  * @max_segments:  max number of segments
268  *
269  * Description:
270  *    Enables a low level driver to set an upper limit on the number of
271  *    hw data segments in a request.  This would be the largest number of
272  *    address/length pairs the host adapter can actually give at once
273  *    to the device.
274  **/
275 void blk_queue_max_hw_segments(struct request_queue *q,
276                                unsigned short max_segments)
277 {
278         if (!max_segments) {
279                 max_segments = 1;
280                 printk(KERN_INFO "%s: set to minimum %d\n",
281                        __func__, max_segments);
282         }
283
284         q->limits.max_hw_segments = max_segments;
285 }
286 EXPORT_SYMBOL(blk_queue_max_hw_segments);
287
288 /**
289  * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
290  * @q:  the request queue for the device
291  * @max_size:  max size of segment in bytes
292  *
293  * Description:
294  *    Enables a low level driver to set an upper limit on the size of a
295  *    coalesced segment
296  **/
297 void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
298 {
299         if (max_size < PAGE_CACHE_SIZE) {
300                 max_size = PAGE_CACHE_SIZE;
301                 printk(KERN_INFO "%s: set to minimum %d\n",
302                        __func__, max_size);
303         }
304
305         q->limits.max_segment_size = max_size;
306 }
307 EXPORT_SYMBOL(blk_queue_max_segment_size);
308
309 /**
310  * blk_queue_logical_block_size - set logical block size for the queue
311  * @q:  the request queue for the device
312  * @size:  the logical block size, in bytes
313  *
314  * Description:
315  *   This should be set to the lowest possible block size that the
316  *   storage device can address.  The default of 512 covers most
317  *   hardware.
318  **/
319 void blk_queue_logical_block_size(struct request_queue *q, unsigned short size)
320 {
321         q->limits.logical_block_size = size;
322
323         if (q->limits.physical_block_size < size)
324                 q->limits.physical_block_size = size;
325
326         if (q->limits.io_min < q->limits.physical_block_size)
327                 q->limits.io_min = q->limits.physical_block_size;
328 }
329 EXPORT_SYMBOL(blk_queue_logical_block_size);
330
331 /**
332  * blk_queue_physical_block_size - set physical block size for the queue
333  * @q:  the request queue for the device
334  * @size:  the physical block size, in bytes
335  *
336  * Description:
337  *   This should be set to the lowest possible sector size that the
338  *   hardware can operate on without reverting to read-modify-write
339  *   operations.
340  */
341 void blk_queue_physical_block_size(struct request_queue *q, unsigned short size)
342 {
343         q->limits.physical_block_size = size;
344
345         if (q->limits.physical_block_size < q->limits.logical_block_size)
346                 q->limits.physical_block_size = q->limits.logical_block_size;
347
348         if (q->limits.io_min < q->limits.physical_block_size)
349                 q->limits.io_min = q->limits.physical_block_size;
350 }
351 EXPORT_SYMBOL(blk_queue_physical_block_size);
352
353 /**
354  * blk_queue_alignment_offset - set physical block alignment offset
355  * @q:  the request queue for the device
356  * @offset: alignment offset in bytes
357  *
358  * Description:
359  *   Some devices are naturally misaligned to compensate for things like
360  *   the legacy DOS partition table 63-sector offset.  Low-level drivers
361  *   should call this function for devices whose first sector is not
362  *   naturally aligned.
363  */
364 void blk_queue_alignment_offset(struct request_queue *q, unsigned int offset)
365 {
366         q->limits.alignment_offset =
367                 offset & (q->limits.physical_block_size - 1);
368         q->limits.misaligned = 0;
369 }
370 EXPORT_SYMBOL(blk_queue_alignment_offset);
371
372 /**
373  * blk_limits_io_min - set minimum request size for a device
374  * @limits: the queue limits
375  * @min:  smallest I/O size in bytes
376  *
377  * Description:
378  *   Some devices have an internal block size bigger than the reported
379  *   hardware sector size.  This function can be used to signal the
380  *   smallest I/O the device can perform without incurring a performance
381  *   penalty.
382  */
383 void blk_limits_io_min(struct queue_limits *limits, unsigned int min)
384 {
385         limits->io_min = min;
386
387         if (limits->io_min < limits->logical_block_size)
388                 limits->io_min = limits->logical_block_size;
389
390         if (limits->io_min < limits->physical_block_size)
391                 limits->io_min = limits->physical_block_size;
392 }
393 EXPORT_SYMBOL(blk_limits_io_min);
394
395 /**
396  * blk_queue_io_min - set minimum request size for the queue
397  * @q:  the request queue for the device
398  * @min:  smallest I/O size in bytes
399  *
400  * Description:
401  *   Storage devices may report a granularity or preferred minimum I/O
402  *   size which is the smallest request the device can perform without
403  *   incurring a performance penalty.  For disk drives this is often the
404  *   physical block size.  For RAID arrays it is often the stripe chunk
405  *   size.  A properly aligned multiple of minimum_io_size is the
406  *   preferred request size for workloads where a high number of I/O
407  *   operations is desired.
408  */
409 void blk_queue_io_min(struct request_queue *q, unsigned int min)
410 {
411         blk_limits_io_min(&q->limits, min);
412 }
413 EXPORT_SYMBOL(blk_queue_io_min);
414
415 /**
416  * blk_limits_io_opt - set optimal request size for a device
417  * @limits: the queue limits
418  * @opt:  smallest I/O size in bytes
419  *
420  * Description:
421  *   Storage devices may report an optimal I/O size, which is the
422  *   device's preferred unit for sustained I/O.  This is rarely reported
423  *   for disk drives.  For RAID arrays it is usually the stripe width or
424  *   the internal track size.  A properly aligned multiple of
425  *   optimal_io_size is the preferred request size for workloads where
426  *   sustained throughput is desired.
427  */
428 void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt)
429 {
430         limits->io_opt = opt;
431 }
432 EXPORT_SYMBOL(blk_limits_io_opt);
433
434 /**
435  * blk_queue_io_opt - set optimal request size for the queue
436  * @q:  the request queue for the device
437  * @opt:  optimal request size in bytes
438  *
439  * Description:
440  *   Storage devices may report an optimal I/O size, which is the
441  *   device's preferred unit for sustained I/O.  This is rarely reported
442  *   for disk drives.  For RAID arrays it is usually the stripe width or
443  *   the internal track size.  A properly aligned multiple of
444  *   optimal_io_size is the preferred request size for workloads where
445  *   sustained throughput is desired.
446  */
447 void blk_queue_io_opt(struct request_queue *q, unsigned int opt)
448 {
449         blk_limits_io_opt(&q->limits, opt);
450 }
451 EXPORT_SYMBOL(blk_queue_io_opt);
452
453 /*
454  * Returns the minimum that is _not_ zero, unless both are zero.
455  */
456 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
457
458 /**
459  * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
460  * @t:  the stacking driver (top)
461  * @b:  the underlying device (bottom)
462  **/
463 void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b)
464 {
465         blk_stack_limits(&t->limits, &b->limits, 0);
466
467         if (!t->queue_lock)
468                 WARN_ON_ONCE(1);
469         else if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags)) {
470                 unsigned long flags;
471                 spin_lock_irqsave(t->queue_lock, flags);
472                 queue_flag_clear(QUEUE_FLAG_CLUSTER, t);
473                 spin_unlock_irqrestore(t->queue_lock, flags);
474         }
475 }
476 EXPORT_SYMBOL(blk_queue_stack_limits);
477
478 /**
479  * blk_stack_limits - adjust queue_limits for stacked devices
480  * @t:  the stacking driver limits (top)
481  * @b:  the underlying queue limits (bottom)
482  * @offset:  offset to beginning of data within component device
483  *
484  * Description:
485  *    Merges two queue_limit structs.  Returns 0 if alignment didn't
486  *    change.  Returns -1 if adding the bottom device caused
487  *    misalignment.
488  */
489 int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
490                      sector_t offset)
491 {
492         t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
493         t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
494         t->bounce_pfn = min_not_zero(t->bounce_pfn, b->bounce_pfn);
495
496         t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
497                                             b->seg_boundary_mask);
498
499         t->max_phys_segments = min_not_zero(t->max_phys_segments,
500                                             b->max_phys_segments);
501
502         t->max_hw_segments = min_not_zero(t->max_hw_segments,
503                                           b->max_hw_segments);
504
505         t->max_segment_size = min_not_zero(t->max_segment_size,
506                                            b->max_segment_size);
507
508         t->logical_block_size = max(t->logical_block_size,
509                                     b->logical_block_size);
510
511         t->physical_block_size = max(t->physical_block_size,
512                                      b->physical_block_size);
513
514         t->io_min = max(t->io_min, b->io_min);
515         t->no_cluster |= b->no_cluster;
516
517         /* Bottom device offset aligned? */
518         if (offset &&
519             (offset & (b->physical_block_size - 1)) != b->alignment_offset) {
520                 t->misaligned = 1;
521                 return -1;
522         }
523
524         /* If top has no alignment offset, inherit from bottom */
525         if (!t->alignment_offset)
526                 t->alignment_offset =
527                         b->alignment_offset & (b->physical_block_size - 1);
528
529         /* Top device aligned on logical block boundary? */
530         if (t->alignment_offset & (t->logical_block_size - 1)) {
531                 t->misaligned = 1;
532                 return -1;
533         }
534
535         /* Find lcm() of optimal I/O size */
536         if (t->io_opt && b->io_opt)
537                 t->io_opt = (t->io_opt * b->io_opt) / gcd(t->io_opt, b->io_opt);
538         else if (b->io_opt)
539                 t->io_opt = b->io_opt;
540
541         /* Verify that optimal I/O size is a multiple of io_min */
542         if (t->io_min && t->io_opt % t->io_min)
543                 return -1;
544
545         return 0;
546 }
547 EXPORT_SYMBOL(blk_stack_limits);
548
549 /**
550  * disk_stack_limits - adjust queue limits for stacked drivers
551  * @disk:  MD/DM gendisk (top)
552  * @bdev:  the underlying block device (bottom)
553  * @offset:  offset to beginning of data within component device
554  *
555  * Description:
556  *    Merges the limits for two queues.  Returns 0 if alignment
557  *    didn't change.  Returns -1 if adding the bottom device caused
558  *    misalignment.
559  */
560 void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
561                        sector_t offset)
562 {
563         struct request_queue *t = disk->queue;
564         struct request_queue *b = bdev_get_queue(bdev);
565
566         offset += get_start_sect(bdev) << 9;
567
568         if (blk_stack_limits(&t->limits, &b->limits, offset) < 0) {
569                 char top[BDEVNAME_SIZE], bottom[BDEVNAME_SIZE];
570
571                 disk_name(disk, 0, top);
572                 bdevname(bdev, bottom);
573
574                 printk(KERN_NOTICE "%s: Warning: Device %s is misaligned\n",
575                        top, bottom);
576         }
577
578         if (!t->queue_lock)
579                 WARN_ON_ONCE(1);
580         else if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags)) {
581                 unsigned long flags;
582
583                 spin_lock_irqsave(t->queue_lock, flags);
584                 if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags))
585                         queue_flag_clear(QUEUE_FLAG_CLUSTER, t);
586                 spin_unlock_irqrestore(t->queue_lock, flags);
587         }
588 }
589 EXPORT_SYMBOL(disk_stack_limits);
590
591 /**
592  * blk_queue_dma_pad - set pad mask
593  * @q:     the request queue for the device
594  * @mask:  pad mask
595  *
596  * Set dma pad mask.
597  *
598  * Appending pad buffer to a request modifies the last entry of a
599  * scatter list such that it includes the pad buffer.
600  **/
601 void blk_queue_dma_pad(struct request_queue *q, unsigned int mask)
602 {
603         q->dma_pad_mask = mask;
604 }
605 EXPORT_SYMBOL(blk_queue_dma_pad);
606
607 /**
608  * blk_queue_update_dma_pad - update pad mask
609  * @q:     the request queue for the device
610  * @mask:  pad mask
611  *
612  * Update dma pad mask.
613  *
614  * Appending pad buffer to a request modifies the last entry of a
615  * scatter list such that it includes the pad buffer.
616  **/
617 void blk_queue_update_dma_pad(struct request_queue *q, unsigned int mask)
618 {
619         if (mask > q->dma_pad_mask)
620                 q->dma_pad_mask = mask;
621 }
622 EXPORT_SYMBOL(blk_queue_update_dma_pad);
623
624 /**
625  * blk_queue_dma_drain - Set up a drain buffer for excess dma.
626  * @q:  the request queue for the device
627  * @dma_drain_needed: fn which returns non-zero if drain is necessary
628  * @buf:        physically contiguous buffer
629  * @size:       size of the buffer in bytes
630  *
631  * Some devices have excess DMA problems and can't simply discard (or
632  * zero fill) the unwanted piece of the transfer.  They have to have a
633  * real area of memory to transfer it into.  The use case for this is
634  * ATAPI devices in DMA mode.  If the packet command causes a transfer
635  * bigger than the transfer size some HBAs will lock up if there
636  * aren't DMA elements to contain the excess transfer.  What this API
637  * does is adjust the queue so that the buf is always appended
638  * silently to the scatterlist.
639  *
640  * Note: This routine adjusts max_hw_segments to make room for
641  * appending the drain buffer.  If you call
642  * blk_queue_max_hw_segments() or blk_queue_max_phys_segments() after
643  * calling this routine, you must set the limit to one fewer than your
644  * device can support otherwise there won't be room for the drain
645  * buffer.
646  */
647 int blk_queue_dma_drain(struct request_queue *q,
648                                dma_drain_needed_fn *dma_drain_needed,
649                                void *buf, unsigned int size)
650 {
651         if (queue_max_hw_segments(q) < 2 || queue_max_phys_segments(q) < 2)
652                 return -EINVAL;
653         /* make room for appending the drain */
654         blk_queue_max_hw_segments(q, queue_max_hw_segments(q) - 1);
655         blk_queue_max_phys_segments(q, queue_max_phys_segments(q) - 1);
656         q->dma_drain_needed = dma_drain_needed;
657         q->dma_drain_buffer = buf;
658         q->dma_drain_size = size;
659
660         return 0;
661 }
662 EXPORT_SYMBOL_GPL(blk_queue_dma_drain);
663
664 /**
665  * blk_queue_segment_boundary - set boundary rules for segment merging
666  * @q:  the request queue for the device
667  * @mask:  the memory boundary mask
668  **/
669 void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
670 {
671         if (mask < PAGE_CACHE_SIZE - 1) {
672                 mask = PAGE_CACHE_SIZE - 1;
673                 printk(KERN_INFO "%s: set to minimum %lx\n",
674                        __func__, mask);
675         }
676
677         q->limits.seg_boundary_mask = mask;
678 }
679 EXPORT_SYMBOL(blk_queue_segment_boundary);
680
681 /**
682  * blk_queue_dma_alignment - set dma length and memory alignment
683  * @q:     the request queue for the device
684  * @mask:  alignment mask
685  *
686  * description:
687  *    set required memory and length alignment for direct dma transactions.
688  *    this is used when building direct io requests for the queue.
689  *
690  **/
691 void blk_queue_dma_alignment(struct request_queue *q, int mask)
692 {
693         q->dma_alignment = mask;
694 }
695 EXPORT_SYMBOL(blk_queue_dma_alignment);
696
697 /**
698  * blk_queue_update_dma_alignment - update dma length and memory alignment
699  * @q:     the request queue for the device
700  * @mask:  alignment mask
701  *
702  * description:
703  *    update required memory and length alignment for direct dma transactions.
704  *    If the requested alignment is larger than the current alignment, then
705  *    the current queue alignment is updated to the new value, otherwise it
706  *    is left alone.  The design of this is to allow multiple objects
707  *    (driver, device, transport etc) to set their respective
708  *    alignments without having them interfere.
709  *
710  **/
711 void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
712 {
713         BUG_ON(mask > PAGE_SIZE);
714
715         if (mask > q->dma_alignment)
716                 q->dma_alignment = mask;
717 }
718 EXPORT_SYMBOL(blk_queue_update_dma_alignment);
719
720 static int __init blk_settings_init(void)
721 {
722         blk_max_low_pfn = max_low_pfn - 1;
723         blk_max_pfn = max_pfn - 1;
724         return 0;
725 }
726 subsys_initcall(blk_settings_init);