1 // SPDX-License-Identifier: GPL-2.0
3 * Functions related to setting various queue properties from drivers
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
9 #include <linux/blkdev.h>
10 #include <linux/memblock.h> /* for max_pfn/max_low_pfn */
11 #include <linux/gcd.h>
12 #include <linux/lcm.h>
13 #include <linux/jiffies.h>
14 #include <linux/gfp.h>
15 #include <linux/dma-mapping.h>
20 unsigned long blk_max_low_pfn;
21 EXPORT_SYMBOL(blk_max_low_pfn);
23 unsigned long blk_max_pfn;
25 void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout)
27 q->rq_timeout = timeout;
29 EXPORT_SYMBOL_GPL(blk_queue_rq_timeout);
32 * blk_set_default_limits - reset limits to default values
33 * @lim: the queue_limits structure to reset
36 * Returns a queue_limit struct to its default state.
38 void blk_set_default_limits(struct queue_limits *lim)
40 lim->max_segments = BLK_MAX_SEGMENTS;
41 lim->max_discard_segments = 1;
42 lim->max_integrity_segments = 0;
43 lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
44 lim->virt_boundary_mask = 0;
45 lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
46 lim->max_sectors = lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS;
47 lim->max_dev_sectors = 0;
48 lim->chunk_sectors = 0;
49 lim->max_write_same_sectors = 0;
50 lim->max_write_zeroes_sectors = 0;
51 lim->max_zone_append_sectors = 0;
52 lim->max_discard_sectors = 0;
53 lim->max_hw_discard_sectors = 0;
54 lim->discard_granularity = 0;
55 lim->discard_alignment = 0;
56 lim->discard_misaligned = 0;
57 lim->logical_block_size = lim->physical_block_size = lim->io_min = 512;
58 lim->bounce_pfn = (unsigned long)(BLK_BOUNCE_ANY >> PAGE_SHIFT);
59 lim->alignment_offset = 0;
62 lim->zoned = BLK_ZONED_NONE;
63 lim->zone_write_granularity = 0;
65 EXPORT_SYMBOL(blk_set_default_limits);
68 * blk_set_stacking_limits - set default limits for stacking devices
69 * @lim: the queue_limits structure to reset
72 * Returns a queue_limit struct to its default state. Should be used
73 * by stacking drivers like DM that have no internal limits.
75 void blk_set_stacking_limits(struct queue_limits *lim)
77 blk_set_default_limits(lim);
79 /* Inherit limits from component devices */
80 lim->max_segments = USHRT_MAX;
81 lim->max_discard_segments = USHRT_MAX;
82 lim->max_hw_sectors = UINT_MAX;
83 lim->max_segment_size = UINT_MAX;
84 lim->max_sectors = UINT_MAX;
85 lim->max_dev_sectors = UINT_MAX;
86 lim->max_write_same_sectors = UINT_MAX;
87 lim->max_write_zeroes_sectors = UINT_MAX;
88 lim->max_zone_append_sectors = UINT_MAX;
90 EXPORT_SYMBOL(blk_set_stacking_limits);
93 * blk_queue_bounce_limit - set bounce buffer limit for queue
94 * @q: the request queue for the device
95 * @max_addr: the maximum address the device can handle
98 * Different hardware can have different requirements as to what pages
99 * it can do I/O directly to. A low level driver can call
100 * blk_queue_bounce_limit to have lower memory pages allocated as bounce
101 * buffers for doing I/O to pages residing above @max_addr.
103 void blk_queue_bounce_limit(struct request_queue *q, u64 max_addr)
105 unsigned long b_pfn = max_addr >> PAGE_SHIFT;
108 q->bounce_gfp = GFP_NOIO;
109 #if BITS_PER_LONG == 64
111 * Assume anything <= 4GB can be handled by IOMMU. Actually
112 * some IOMMUs can handle everything, but I don't know of a
113 * way to test this here.
115 if (b_pfn < (min_t(u64, 0xffffffffUL, BLK_BOUNCE_HIGH) >> PAGE_SHIFT))
117 q->limits.bounce_pfn = max(max_low_pfn, b_pfn);
119 if (b_pfn < blk_max_low_pfn)
121 q->limits.bounce_pfn = b_pfn;
124 init_emergency_isa_pool();
125 q->bounce_gfp = GFP_NOIO | GFP_DMA;
126 q->limits.bounce_pfn = b_pfn;
129 EXPORT_SYMBOL(blk_queue_bounce_limit);
132 * blk_queue_max_hw_sectors - set max sectors for a request for this queue
133 * @q: the request queue for the device
134 * @max_hw_sectors: max hardware sectors in the usual 512b unit
137 * Enables a low level driver to set a hard upper limit,
138 * max_hw_sectors, on the size of requests. max_hw_sectors is set by
139 * the device driver based upon the capabilities of the I/O
142 * max_dev_sectors is a hard limit imposed by the storage device for
143 * READ/WRITE requests. It is set by the disk driver.
145 * max_sectors is a soft limit imposed by the block layer for
146 * filesystem type requests. This value can be overridden on a
147 * per-device basis in /sys/block/<device>/queue/max_sectors_kb.
148 * The soft limit can not exceed max_hw_sectors.
150 void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
152 struct queue_limits *limits = &q->limits;
153 unsigned int max_sectors;
155 if ((max_hw_sectors << 9) < PAGE_SIZE) {
156 max_hw_sectors = 1 << (PAGE_SHIFT - 9);
157 printk(KERN_INFO "%s: set to minimum %d\n",
158 __func__, max_hw_sectors);
161 limits->max_hw_sectors = max_hw_sectors;
162 max_sectors = min_not_zero(max_hw_sectors, limits->max_dev_sectors);
163 max_sectors = min_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
164 limits->max_sectors = max_sectors;
165 q->backing_dev_info->io_pages = max_sectors >> (PAGE_SHIFT - 9);
167 EXPORT_SYMBOL(blk_queue_max_hw_sectors);
170 * blk_queue_chunk_sectors - set size of the chunk for this queue
171 * @q: the request queue for the device
172 * @chunk_sectors: chunk sectors in the usual 512b unit
175 * If a driver doesn't want IOs to cross a given chunk size, it can set
176 * this limit and prevent merging across chunks. Note that the block layer
177 * must accept a page worth of data at any offset. So if the crossing of
178 * chunks is a hard limitation in the driver, it must still be prepared
179 * to split single page bios.
181 void blk_queue_chunk_sectors(struct request_queue *q, unsigned int chunk_sectors)
183 q->limits.chunk_sectors = chunk_sectors;
185 EXPORT_SYMBOL(blk_queue_chunk_sectors);
188 * blk_queue_max_discard_sectors - set max sectors for a single discard
189 * @q: the request queue for the device
190 * @max_discard_sectors: maximum number of sectors to discard
192 void blk_queue_max_discard_sectors(struct request_queue *q,
193 unsigned int max_discard_sectors)
195 q->limits.max_hw_discard_sectors = max_discard_sectors;
196 q->limits.max_discard_sectors = max_discard_sectors;
198 EXPORT_SYMBOL(blk_queue_max_discard_sectors);
201 * blk_queue_max_write_same_sectors - set max sectors for a single write same
202 * @q: the request queue for the device
203 * @max_write_same_sectors: maximum number of sectors to write per command
205 void blk_queue_max_write_same_sectors(struct request_queue *q,
206 unsigned int max_write_same_sectors)
208 q->limits.max_write_same_sectors = max_write_same_sectors;
210 EXPORT_SYMBOL(blk_queue_max_write_same_sectors);
213 * blk_queue_max_write_zeroes_sectors - set max sectors for a single
215 * @q: the request queue for the device
216 * @max_write_zeroes_sectors: maximum number of sectors to write per command
218 void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
219 unsigned int max_write_zeroes_sectors)
221 q->limits.max_write_zeroes_sectors = max_write_zeroes_sectors;
223 EXPORT_SYMBOL(blk_queue_max_write_zeroes_sectors);
226 * blk_queue_max_zone_append_sectors - set max sectors for a single zone append
227 * @q: the request queue for the device
228 * @max_zone_append_sectors: maximum number of sectors to write per command
230 void blk_queue_max_zone_append_sectors(struct request_queue *q,
231 unsigned int max_zone_append_sectors)
233 unsigned int max_sectors;
235 if (WARN_ON(!blk_queue_is_zoned(q)))
238 max_sectors = min(q->limits.max_hw_sectors, max_zone_append_sectors);
239 max_sectors = min(q->limits.chunk_sectors, max_sectors);
242 * Signal eventual driver bugs resulting in the max_zone_append sectors limit
243 * being 0 due to a 0 argument, the chunk_sectors limit (zone size) not set,
244 * or the max_hw_sectors limit not set.
246 WARN_ON(!max_sectors);
248 q->limits.max_zone_append_sectors = max_sectors;
250 EXPORT_SYMBOL_GPL(blk_queue_max_zone_append_sectors);
253 * blk_queue_max_segments - set max hw segments for a request for this queue
254 * @q: the request queue for the device
255 * @max_segments: max number of segments
258 * Enables a low level driver to set an upper limit on the number of
259 * hw data segments in a request.
261 void blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
265 printk(KERN_INFO "%s: set to minimum %d\n",
266 __func__, max_segments);
269 q->limits.max_segments = max_segments;
271 EXPORT_SYMBOL(blk_queue_max_segments);
274 * blk_queue_max_discard_segments - set max segments for discard requests
275 * @q: the request queue for the device
276 * @max_segments: max number of segments
279 * Enables a low level driver to set an upper limit on the number of
280 * segments in a discard request.
282 void blk_queue_max_discard_segments(struct request_queue *q,
283 unsigned short max_segments)
285 q->limits.max_discard_segments = max_segments;
287 EXPORT_SYMBOL_GPL(blk_queue_max_discard_segments);
290 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
291 * @q: the request queue for the device
292 * @max_size: max size of segment in bytes
295 * Enables a low level driver to set an upper limit on the size of a
298 void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
300 if (max_size < PAGE_SIZE) {
301 max_size = PAGE_SIZE;
302 printk(KERN_INFO "%s: set to minimum %d\n",
306 /* see blk_queue_virt_boundary() for the explanation */
307 WARN_ON_ONCE(q->limits.virt_boundary_mask);
309 q->limits.max_segment_size = max_size;
311 EXPORT_SYMBOL(blk_queue_max_segment_size);
314 * blk_queue_logical_block_size - set logical block size for the queue
315 * @q: the request queue for the device
316 * @size: the logical block size, in bytes
319 * This should be set to the lowest possible block size that the
320 * storage device can address. The default of 512 covers most
323 void blk_queue_logical_block_size(struct request_queue *q, unsigned int size)
325 q->limits.logical_block_size = size;
327 if (q->limits.physical_block_size < size)
328 q->limits.physical_block_size = size;
330 if (q->limits.io_min < q->limits.physical_block_size)
331 q->limits.io_min = q->limits.physical_block_size;
333 EXPORT_SYMBOL(blk_queue_logical_block_size);
336 * blk_queue_physical_block_size - set physical block size for the queue
337 * @q: the request queue for the device
338 * @size: the physical block size, in bytes
341 * This should be set to the lowest possible sector size that the
342 * hardware can operate on without reverting to read-modify-write
345 void blk_queue_physical_block_size(struct request_queue *q, unsigned int size)
347 q->limits.physical_block_size = size;
349 if (q->limits.physical_block_size < q->limits.logical_block_size)
350 q->limits.physical_block_size = q->limits.logical_block_size;
352 if (q->limits.io_min < q->limits.physical_block_size)
353 q->limits.io_min = q->limits.physical_block_size;
355 EXPORT_SYMBOL(blk_queue_physical_block_size);
358 * blk_queue_zone_write_granularity - set zone write granularity for the queue
359 * @q: the request queue for the zoned device
360 * @size: the zone write granularity size, in bytes
363 * This should be set to the lowest possible size allowing to write in
364 * sequential zones of a zoned block device.
366 void blk_queue_zone_write_granularity(struct request_queue *q,
369 if (WARN_ON_ONCE(!blk_queue_is_zoned(q)))
372 q->limits.zone_write_granularity = size;
374 if (q->limits.zone_write_granularity < q->limits.logical_block_size)
375 q->limits.zone_write_granularity = q->limits.logical_block_size;
377 EXPORT_SYMBOL_GPL(blk_queue_zone_write_granularity);
380 * blk_queue_alignment_offset - set physical block alignment offset
381 * @q: the request queue for the device
382 * @offset: alignment offset in bytes
385 * Some devices are naturally misaligned to compensate for things like
386 * the legacy DOS partition table 63-sector offset. Low-level drivers
387 * should call this function for devices whose first sector is not
390 void blk_queue_alignment_offset(struct request_queue *q, unsigned int offset)
392 q->limits.alignment_offset =
393 offset & (q->limits.physical_block_size - 1);
394 q->limits.misaligned = 0;
396 EXPORT_SYMBOL(blk_queue_alignment_offset);
398 void blk_queue_update_readahead(struct request_queue *q)
401 * For read-ahead of large files to be effective, we need to read ahead
402 * at least twice the optimal I/O size.
404 q->backing_dev_info->ra_pages =
405 max(queue_io_opt(q) * 2 / PAGE_SIZE, VM_READAHEAD_PAGES);
406 q->backing_dev_info->io_pages =
407 queue_max_sectors(q) >> (PAGE_SHIFT - 9);
409 EXPORT_SYMBOL_GPL(blk_queue_update_readahead);
412 * blk_limits_io_min - set minimum request size for a device
413 * @limits: the queue limits
414 * @min: smallest I/O size in bytes
417 * Some devices have an internal block size bigger than the reported
418 * hardware sector size. This function can be used to signal the
419 * smallest I/O the device can perform without incurring a performance
422 void blk_limits_io_min(struct queue_limits *limits, unsigned int min)
424 limits->io_min = min;
426 if (limits->io_min < limits->logical_block_size)
427 limits->io_min = limits->logical_block_size;
429 if (limits->io_min < limits->physical_block_size)
430 limits->io_min = limits->physical_block_size;
432 EXPORT_SYMBOL(blk_limits_io_min);
435 * blk_queue_io_min - set minimum request size for the queue
436 * @q: the request queue for the device
437 * @min: smallest I/O size in bytes
440 * Storage devices may report a granularity or preferred minimum I/O
441 * size which is the smallest request the device can perform without
442 * incurring a performance penalty. For disk drives this is often the
443 * physical block size. For RAID arrays it is often the stripe chunk
444 * size. A properly aligned multiple of minimum_io_size is the
445 * preferred request size for workloads where a high number of I/O
446 * operations is desired.
448 void blk_queue_io_min(struct request_queue *q, unsigned int min)
450 blk_limits_io_min(&q->limits, min);
452 EXPORT_SYMBOL(blk_queue_io_min);
455 * blk_limits_io_opt - set optimal request size for a device
456 * @limits: the queue limits
457 * @opt: smallest I/O size in bytes
460 * Storage devices may report an optimal I/O size, which is the
461 * device's preferred unit for sustained I/O. This is rarely reported
462 * for disk drives. For RAID arrays it is usually the stripe width or
463 * the internal track size. A properly aligned multiple of
464 * optimal_io_size is the preferred request size for workloads where
465 * sustained throughput is desired.
467 void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt)
469 limits->io_opt = opt;
471 EXPORT_SYMBOL(blk_limits_io_opt);
474 * blk_queue_io_opt - set optimal request size for the queue
475 * @q: the request queue for the device
476 * @opt: optimal request size in bytes
479 * Storage devices may report an optimal I/O size, which is the
480 * device's preferred unit for sustained I/O. This is rarely reported
481 * for disk drives. For RAID arrays it is usually the stripe width or
482 * the internal track size. A properly aligned multiple of
483 * optimal_io_size is the preferred request size for workloads where
484 * sustained throughput is desired.
486 void blk_queue_io_opt(struct request_queue *q, unsigned int opt)
488 blk_limits_io_opt(&q->limits, opt);
489 q->backing_dev_info->ra_pages =
490 max(queue_io_opt(q) * 2 / PAGE_SIZE, VM_READAHEAD_PAGES);
492 EXPORT_SYMBOL(blk_queue_io_opt);
494 static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs)
496 sectors = round_down(sectors, lbs >> SECTOR_SHIFT);
497 if (sectors < PAGE_SIZE >> SECTOR_SHIFT)
498 sectors = PAGE_SIZE >> SECTOR_SHIFT;
503 * blk_stack_limits - adjust queue_limits for stacked devices
504 * @t: the stacking driver limits (top device)
505 * @b: the underlying queue limits (bottom, component device)
506 * @start: first data sector within component device
509 * This function is used by stacking drivers like MD and DM to ensure
510 * that all component devices have compatible block sizes and
511 * alignments. The stacking driver must provide a queue_limits
512 * struct (top) and then iteratively call the stacking function for
513 * all component (bottom) devices. The stacking function will
514 * attempt to combine the values and ensure proper alignment.
516 * Returns 0 if the top and bottom queue_limits are compatible. The
517 * top device's block sizes and alignment offsets may be adjusted to
518 * ensure alignment with the bottom device. If no compatible sizes
519 * and alignments exist, -1 is returned and the resulting top
520 * queue_limits will have the misaligned flag set to indicate that
521 * the alignment_offset is undefined.
523 int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
526 unsigned int top, bottom, alignment, ret = 0;
528 t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
529 t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
530 t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
531 t->max_write_same_sectors = min(t->max_write_same_sectors,
532 b->max_write_same_sectors);
533 t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors,
534 b->max_write_zeroes_sectors);
535 t->max_zone_append_sectors = min(t->max_zone_append_sectors,
536 b->max_zone_append_sectors);
537 t->bounce_pfn = min_not_zero(t->bounce_pfn, b->bounce_pfn);
539 t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
540 b->seg_boundary_mask);
541 t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask,
542 b->virt_boundary_mask);
544 t->max_segments = min_not_zero(t->max_segments, b->max_segments);
545 t->max_discard_segments = min_not_zero(t->max_discard_segments,
546 b->max_discard_segments);
547 t->max_integrity_segments = min_not_zero(t->max_integrity_segments,
548 b->max_integrity_segments);
550 t->max_segment_size = min_not_zero(t->max_segment_size,
551 b->max_segment_size);
553 t->misaligned |= b->misaligned;
555 alignment = queue_limit_alignment_offset(b, start);
557 /* Bottom device has different alignment. Check that it is
558 * compatible with the current top alignment.
560 if (t->alignment_offset != alignment) {
562 top = max(t->physical_block_size, t->io_min)
563 + t->alignment_offset;
564 bottom = max(b->physical_block_size, b->io_min) + alignment;
566 /* Verify that top and bottom intervals line up */
567 if (max(top, bottom) % min(top, bottom)) {
573 t->logical_block_size = max(t->logical_block_size,
574 b->logical_block_size);
576 t->physical_block_size = max(t->physical_block_size,
577 b->physical_block_size);
579 t->io_min = max(t->io_min, b->io_min);
580 t->io_opt = lcm_not_zero(t->io_opt, b->io_opt);
582 /* Set non-power-of-2 compatible chunk_sectors boundary */
583 if (b->chunk_sectors)
584 t->chunk_sectors = gcd(t->chunk_sectors, b->chunk_sectors);
586 /* Physical block size a multiple of the logical block size? */
587 if (t->physical_block_size & (t->logical_block_size - 1)) {
588 t->physical_block_size = t->logical_block_size;
593 /* Minimum I/O a multiple of the physical block size? */
594 if (t->io_min & (t->physical_block_size - 1)) {
595 t->io_min = t->physical_block_size;
600 /* Optimal I/O a multiple of the physical block size? */
601 if (t->io_opt & (t->physical_block_size - 1)) {
607 /* chunk_sectors a multiple of the physical block size? */
608 if ((t->chunk_sectors << 9) & (t->physical_block_size - 1)) {
609 t->chunk_sectors = 0;
614 t->raid_partial_stripes_expensive =
615 max(t->raid_partial_stripes_expensive,
616 b->raid_partial_stripes_expensive);
618 /* Find lowest common alignment_offset */
619 t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment)
620 % max(t->physical_block_size, t->io_min);
622 /* Verify that new alignment_offset is on a logical block boundary */
623 if (t->alignment_offset & (t->logical_block_size - 1)) {
628 t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size);
629 t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size);
630 t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size);
632 /* Discard alignment and granularity */
633 if (b->discard_granularity) {
634 alignment = queue_limit_discard_alignment(b, start);
636 if (t->discard_granularity != 0 &&
637 t->discard_alignment != alignment) {
638 top = t->discard_granularity + t->discard_alignment;
639 bottom = b->discard_granularity + alignment;
641 /* Verify that top and bottom intervals line up */
642 if ((max(top, bottom) % min(top, bottom)) != 0)
643 t->discard_misaligned = 1;
646 t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
647 b->max_discard_sectors);
648 t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
649 b->max_hw_discard_sectors);
650 t->discard_granularity = max(t->discard_granularity,
651 b->discard_granularity);
652 t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
653 t->discard_granularity;
656 t->zone_write_granularity = max(t->zone_write_granularity,
657 b->zone_write_granularity);
658 t->zoned = max(t->zoned, b->zoned);
660 t->zone_write_granularity = 0;
661 t->max_zone_append_sectors = 0;
665 EXPORT_SYMBOL(blk_stack_limits);
668 * disk_stack_limits - adjust queue limits for stacked drivers
669 * @disk: MD/DM gendisk (top)
670 * @bdev: the underlying block device (bottom)
671 * @offset: offset to beginning of data within component device
674 * Merges the limits for a top level gendisk and a bottom level
677 void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
680 struct request_queue *t = disk->queue;
682 if (blk_stack_limits(&t->limits, &bdev_get_queue(bdev)->limits,
683 get_start_sect(bdev) + (offset >> 9)) < 0) {
684 char top[BDEVNAME_SIZE], bottom[BDEVNAME_SIZE];
686 disk_name(disk, 0, top);
687 bdevname(bdev, bottom);
689 printk(KERN_NOTICE "%s: Warning: Device %s is misaligned\n",
693 blk_queue_update_readahead(disk->queue);
695 EXPORT_SYMBOL(disk_stack_limits);
698 * blk_queue_update_dma_pad - update pad mask
699 * @q: the request queue for the device
702 * Update dma pad mask.
704 * Appending pad buffer to a request modifies the last entry of a
705 * scatter list such that it includes the pad buffer.
707 void blk_queue_update_dma_pad(struct request_queue *q, unsigned int mask)
709 if (mask > q->dma_pad_mask)
710 q->dma_pad_mask = mask;
712 EXPORT_SYMBOL(blk_queue_update_dma_pad);
715 * blk_queue_segment_boundary - set boundary rules for segment merging
716 * @q: the request queue for the device
717 * @mask: the memory boundary mask
719 void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
721 if (mask < PAGE_SIZE - 1) {
722 mask = PAGE_SIZE - 1;
723 printk(KERN_INFO "%s: set to minimum %lx\n",
727 q->limits.seg_boundary_mask = mask;
729 EXPORT_SYMBOL(blk_queue_segment_boundary);
732 * blk_queue_virt_boundary - set boundary rules for bio merging
733 * @q: the request queue for the device
734 * @mask: the memory boundary mask
736 void blk_queue_virt_boundary(struct request_queue *q, unsigned long mask)
738 q->limits.virt_boundary_mask = mask;
741 * Devices that require a virtual boundary do not support scatter/gather
742 * I/O natively, but instead require a descriptor list entry for each
743 * page (which might not be idential to the Linux PAGE_SIZE). Because
744 * of that they are not limited by our notion of "segment size".
747 q->limits.max_segment_size = UINT_MAX;
749 EXPORT_SYMBOL(blk_queue_virt_boundary);
752 * blk_queue_dma_alignment - set dma length and memory alignment
753 * @q: the request queue for the device
754 * @mask: alignment mask
757 * set required memory and length alignment for direct dma transactions.
758 * this is used when building direct io requests for the queue.
761 void blk_queue_dma_alignment(struct request_queue *q, int mask)
763 q->dma_alignment = mask;
765 EXPORT_SYMBOL(blk_queue_dma_alignment);
768 * blk_queue_update_dma_alignment - update dma length and memory alignment
769 * @q: the request queue for the device
770 * @mask: alignment mask
773 * update required memory and length alignment for direct dma transactions.
774 * If the requested alignment is larger than the current alignment, then
775 * the current queue alignment is updated to the new value, otherwise it
776 * is left alone. The design of this is to allow multiple objects
777 * (driver, device, transport etc) to set their respective
778 * alignments without having them interfere.
781 void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
783 BUG_ON(mask > PAGE_SIZE);
785 if (mask > q->dma_alignment)
786 q->dma_alignment = mask;
788 EXPORT_SYMBOL(blk_queue_update_dma_alignment);
791 * blk_set_queue_depth - tell the block layer about the device queue depth
792 * @q: the request queue for the device
793 * @depth: queue depth
796 void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
798 q->queue_depth = depth;
799 rq_qos_queue_depth_changed(q);
801 EXPORT_SYMBOL(blk_set_queue_depth);
804 * blk_queue_write_cache - configure queue's write cache
805 * @q: the request queue for the device
806 * @wc: write back cache on or off
807 * @fua: device supports FUA writes, if true
809 * Tell the block layer about the write cache of @q.
811 void blk_queue_write_cache(struct request_queue *q, bool wc, bool fua)
814 blk_queue_flag_set(QUEUE_FLAG_WC, q);
816 blk_queue_flag_clear(QUEUE_FLAG_WC, q);
818 blk_queue_flag_set(QUEUE_FLAG_FUA, q);
820 blk_queue_flag_clear(QUEUE_FLAG_FUA, q);
822 wbt_set_write_cache(q, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
824 EXPORT_SYMBOL_GPL(blk_queue_write_cache);
827 * blk_queue_required_elevator_features - Set a queue required elevator features
828 * @q: the request queue for the target device
829 * @features: Required elevator features OR'ed together
831 * Tell the block layer that for the device controlled through @q, only the
832 * only elevators that can be used are those that implement at least the set of
833 * features specified by @features.
835 void blk_queue_required_elevator_features(struct request_queue *q,
836 unsigned int features)
838 q->required_elevator_features = features;
840 EXPORT_SYMBOL_GPL(blk_queue_required_elevator_features);
843 * blk_queue_can_use_dma_map_merging - configure queue for merging segments.
844 * @q: the request queue for the device
845 * @dev: the device pointer for dma
847 * Tell the block layer about merging the segments by dma map of @q.
849 bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
852 unsigned long boundary = dma_get_merge_boundary(dev);
857 /* No need to update max_segment_size. see blk_queue_virt_boundary() */
858 blk_queue_virt_boundary(q, boundary);
862 EXPORT_SYMBOL_GPL(blk_queue_can_use_dma_map_merging);
865 * blk_queue_set_zoned - configure a disk queue zoned model.
866 * @disk: the gendisk of the queue to configure
867 * @model: the zoned model to set
869 * Set the zoned model of the request queue of @disk according to @model.
870 * When @model is BLK_ZONED_HM (host managed), this should be called only
871 * if zoned block device support is enabled (CONFIG_BLK_DEV_ZONED option).
872 * If @model specifies BLK_ZONED_HA (host aware), the effective model used
873 * depends on CONFIG_BLK_DEV_ZONED settings and on the existence of partitions
876 void blk_queue_set_zoned(struct gendisk *disk, enum blk_zoned_model model)
878 struct request_queue *q = disk->queue;
883 * Host managed devices are supported only if
884 * CONFIG_BLK_DEV_ZONED is enabled.
886 WARN_ON_ONCE(!IS_ENABLED(CONFIG_BLK_DEV_ZONED));
890 * Host aware devices can be treated either as regular block
891 * devices (similar to drive managed devices) or as zoned block
892 * devices to take advantage of the zone command set, similarly
893 * to host managed devices. We try the latter if there are no
894 * partitions and zoned block device support is enabled, else
895 * we do nothing special as far as the block layer is concerned.
897 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED) ||
898 disk_has_partitions(disk))
899 model = BLK_ZONED_NONE;
903 if (WARN_ON_ONCE(model != BLK_ZONED_NONE))
904 model = BLK_ZONED_NONE;
908 q->limits.zoned = model;
909 if (model != BLK_ZONED_NONE) {
911 * Set the zone write granularity to the device logical block
912 * size by default. The driver can change this value if needed.
914 blk_queue_zone_write_granularity(q,
915 queue_logical_block_size(q));
918 EXPORT_SYMBOL_GPL(blk_queue_set_zoned);
920 static int __init blk_settings_init(void)
922 blk_max_low_pfn = max_low_pfn - 1;
923 blk_max_pfn = max_pfn - 1;
926 subsys_initcall(blk_settings_init);