proc/sysctl: fix function name error in comments
[linux-block.git] / block / blk-settings.c
CommitLineData
3dcf60bc 1// SPDX-License-Identifier: GPL-2.0
86db1e29
JA
2/*
3 * Functions related to setting various queue properties from drivers
4 */
5#include <linux/kernel.h>
6#include <linux/module.h>
7#include <linux/init.h>
8#include <linux/bio.h>
9#include <linux/blkdev.h>
70dd5bf3 10#include <linux/gcd.h>
2cda2728 11#include <linux/lcm.h>
ad5ebd2f 12#include <linux/jiffies.h>
5a0e3ad6 13#include <linux/gfp.h>
45147fb5 14#include <linux/dma-mapping.h>
86db1e29
JA
15
16#include "blk.h"
87760e5e 17#include "blk-wbt.h"
86db1e29 18
242f9dcb
JA
19void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout)
20{
21 q->rq_timeout = timeout;
22}
23EXPORT_SYMBOL_GPL(blk_queue_rq_timeout);
24
e475bba2
MP
25/**
26 * blk_set_default_limits - reset limits to default values
f740f5ca 27 * @lim: the queue_limits structure to reset
e475bba2
MP
28 *
29 * Description:
b1bd055d 30 * Returns a queue_limit struct to its default state.
e475bba2
MP
31 */
32void blk_set_default_limits(struct queue_limits *lim)
33{
8a78362c 34 lim->max_segments = BLK_MAX_SEGMENTS;
1e739730 35 lim->max_discard_segments = 1;
13f05c8d 36 lim->max_integrity_segments = 0;
e475bba2 37 lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
03100aad 38 lim->virt_boundary_mask = 0;
eb28d31b 39 lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
5f009d3f
KB
40 lim->max_sectors = lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS;
41 lim->max_dev_sectors = 0;
762380ad 42 lim->chunk_sectors = 0;
4363ac7c 43 lim->max_write_same_sectors = 0;
a6f0788e 44 lim->max_write_zeroes_sectors = 0;
0512a75b 45 lim->max_zone_append_sectors = 0;
86b37281 46 lim->max_discard_sectors = 0;
0034af03 47 lim->max_hw_discard_sectors = 0;
86b37281
MP
48 lim->discard_granularity = 0;
49 lim->discard_alignment = 0;
50 lim->discard_misaligned = 0;
e475bba2 51 lim->logical_block_size = lim->physical_block_size = lim->io_min = 512;
9bb33f24 52 lim->bounce = BLK_BOUNCE_NONE;
e475bba2
MP
53 lim->alignment_offset = 0;
54 lim->io_opt = 0;
55 lim->misaligned = 0;
797476b8 56 lim->zoned = BLK_ZONED_NONE;
a805a4fa 57 lim->zone_write_granularity = 0;
e475bba2
MP
58}
59EXPORT_SYMBOL(blk_set_default_limits);
60
b1bd055d
MP
61/**
62 * blk_set_stacking_limits - set default limits for stacking devices
63 * @lim: the queue_limits structure to reset
64 *
65 * Description:
66 * Returns a queue_limit struct to its default state. Should be used
67 * by stacking drivers like DM that have no internal limits.
68 */
69void blk_set_stacking_limits(struct queue_limits *lim)
70{
71 blk_set_default_limits(lim);
72
73 /* Inherit limits from component devices */
b1bd055d 74 lim->max_segments = USHRT_MAX;
42c9cdfe 75 lim->max_discard_segments = USHRT_MAX;
b1bd055d 76 lim->max_hw_sectors = UINT_MAX;
d82ae52e 77 lim->max_segment_size = UINT_MAX;
fe86cdce 78 lim->max_sectors = UINT_MAX;
ca369d51 79 lim->max_dev_sectors = UINT_MAX;
4363ac7c 80 lim->max_write_same_sectors = UINT_MAX;
a6f0788e 81 lim->max_write_zeroes_sectors = UINT_MAX;
0512a75b 82 lim->max_zone_append_sectors = UINT_MAX;
b1bd055d
MP
83}
84EXPORT_SYMBOL(blk_set_stacking_limits);
85
86db1e29
JA
86/**
87 * blk_queue_bounce_limit - set bounce buffer limit for queue
cd0aca2d 88 * @q: the request queue for the device
9bb33f24 89 * @bounce: bounce limit to enforce
86db1e29
JA
90 *
91 * Description:
9bb33f24
CH
92 * Force bouncing for ISA DMA ranges or highmem.
93 *
94 * DEPRECATED, don't use in new code.
86db1e29 95 **/
9bb33f24 96void blk_queue_bounce_limit(struct request_queue *q, enum blk_bounce bounce)
86db1e29 97{
9bb33f24 98 q->limits.bounce = bounce;
86db1e29 99}
86db1e29
JA
100EXPORT_SYMBOL(blk_queue_bounce_limit);
101
102/**
ca369d51
MP
103 * blk_queue_max_hw_sectors - set max sectors for a request for this queue
104 * @q: the request queue for the device
2800aac1 105 * @max_hw_sectors: max hardware sectors in the usual 512b unit
86db1e29
JA
106 *
107 * Description:
2800aac1
MP
108 * Enables a low level driver to set a hard upper limit,
109 * max_hw_sectors, on the size of requests. max_hw_sectors is set by
4f258a46
MP
110 * the device driver based upon the capabilities of the I/O
111 * controller.
2800aac1 112 *
ca369d51
MP
113 * max_dev_sectors is a hard limit imposed by the storage device for
114 * READ/WRITE requests. It is set by the disk driver.
115 *
2800aac1
MP
116 * max_sectors is a soft limit imposed by the block layer for
117 * filesystem type requests. This value can be overridden on a
118 * per-device basis in /sys/block/<device>/queue/max_sectors_kb.
119 * The soft limit can not exceed max_hw_sectors.
86db1e29 120 **/
ca369d51 121void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
86db1e29 122{
ca369d51
MP
123 struct queue_limits *limits = &q->limits;
124 unsigned int max_sectors;
125
09cbfeaf
KS
126 if ((max_hw_sectors << 9) < PAGE_SIZE) {
127 max_hw_sectors = 1 << (PAGE_SHIFT - 9);
24c03d47 128 printk(KERN_INFO "%s: set to minimum %d\n",
2800aac1 129 __func__, max_hw_sectors);
86db1e29
JA
130 }
131
817046ec
DLM
132 max_hw_sectors = round_down(max_hw_sectors,
133 limits->logical_block_size >> SECTOR_SHIFT);
30e2bc08 134 limits->max_hw_sectors = max_hw_sectors;
817046ec 135
ca369d51
MP
136 max_sectors = min_not_zero(max_hw_sectors, limits->max_dev_sectors);
137 max_sectors = min_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
817046ec
DLM
138 max_sectors = round_down(max_sectors,
139 limits->logical_block_size >> SECTOR_SHIFT);
ca369d51 140 limits->max_sectors = max_sectors;
817046ec 141
dc3b17cc 142 q->backing_dev_info->io_pages = max_sectors >> (PAGE_SHIFT - 9);
86db1e29 143}
086fa5ff 144EXPORT_SYMBOL(blk_queue_max_hw_sectors);
86db1e29 145
762380ad
JA
146/**
147 * blk_queue_chunk_sectors - set size of the chunk for this queue
148 * @q: the request queue for the device
149 * @chunk_sectors: chunk sectors in the usual 512b unit
150 *
151 * Description:
152 * If a driver doesn't want IOs to cross a given chunk size, it can set
07d098e6
MS
153 * this limit and prevent merging across chunks. Note that the block layer
154 * must accept a page worth of data at any offset. So if the crossing of
155 * chunks is a hard limitation in the driver, it must still be prepared
156 * to split single page bios.
762380ad
JA
157 **/
158void blk_queue_chunk_sectors(struct request_queue *q, unsigned int chunk_sectors)
159{
762380ad
JA
160 q->limits.chunk_sectors = chunk_sectors;
161}
162EXPORT_SYMBOL(blk_queue_chunk_sectors);
163
67efc925
CH
164/**
165 * blk_queue_max_discard_sectors - set max sectors for a single discard
166 * @q: the request queue for the device
c7ebf065 167 * @max_discard_sectors: maximum number of sectors to discard
67efc925
CH
168 **/
169void blk_queue_max_discard_sectors(struct request_queue *q,
170 unsigned int max_discard_sectors)
171{
0034af03 172 q->limits.max_hw_discard_sectors = max_discard_sectors;
67efc925
CH
173 q->limits.max_discard_sectors = max_discard_sectors;
174}
175EXPORT_SYMBOL(blk_queue_max_discard_sectors);
176
4363ac7c
MP
177/**
178 * blk_queue_max_write_same_sectors - set max sectors for a single write same
179 * @q: the request queue for the device
180 * @max_write_same_sectors: maximum number of sectors to write per command
181 **/
182void blk_queue_max_write_same_sectors(struct request_queue *q,
183 unsigned int max_write_same_sectors)
184{
185 q->limits.max_write_same_sectors = max_write_same_sectors;
186}
187EXPORT_SYMBOL(blk_queue_max_write_same_sectors);
188
a6f0788e
CK
189/**
190 * blk_queue_max_write_zeroes_sectors - set max sectors for a single
191 * write zeroes
192 * @q: the request queue for the device
193 * @max_write_zeroes_sectors: maximum number of sectors to write per command
194 **/
195void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
196 unsigned int max_write_zeroes_sectors)
197{
198 q->limits.max_write_zeroes_sectors = max_write_zeroes_sectors;
199}
200EXPORT_SYMBOL(blk_queue_max_write_zeroes_sectors);
201
0512a75b
KB
202/**
203 * blk_queue_max_zone_append_sectors - set max sectors for a single zone append
204 * @q: the request queue for the device
205 * @max_zone_append_sectors: maximum number of sectors to write per command
206 **/
207void blk_queue_max_zone_append_sectors(struct request_queue *q,
208 unsigned int max_zone_append_sectors)
209{
210 unsigned int max_sectors;
211
212 if (WARN_ON(!blk_queue_is_zoned(q)))
213 return;
214
215 max_sectors = min(q->limits.max_hw_sectors, max_zone_append_sectors);
216 max_sectors = min(q->limits.chunk_sectors, max_sectors);
217
218 /*
219 * Signal eventual driver bugs resulting in the max_zone_append sectors limit
220 * being 0 due to a 0 argument, the chunk_sectors limit (zone size) not set,
221 * or the max_hw_sectors limit not set.
222 */
223 WARN_ON(!max_sectors);
224
225 q->limits.max_zone_append_sectors = max_sectors;
226}
227EXPORT_SYMBOL_GPL(blk_queue_max_zone_append_sectors);
228
86db1e29 229/**
8a78362c 230 * blk_queue_max_segments - set max hw segments for a request for this queue
86db1e29
JA
231 * @q: the request queue for the device
232 * @max_segments: max number of segments
233 *
234 * Description:
235 * Enables a low level driver to set an upper limit on the number of
8a78362c 236 * hw data segments in a request.
86db1e29 237 **/
8a78362c 238void blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
86db1e29
JA
239{
240 if (!max_segments) {
241 max_segments = 1;
24c03d47
HH
242 printk(KERN_INFO "%s: set to minimum %d\n",
243 __func__, max_segments);
86db1e29
JA
244 }
245
8a78362c 246 q->limits.max_segments = max_segments;
86db1e29 247}
8a78362c 248EXPORT_SYMBOL(blk_queue_max_segments);
86db1e29 249
1e739730
CH
250/**
251 * blk_queue_max_discard_segments - set max segments for discard requests
252 * @q: the request queue for the device
253 * @max_segments: max number of segments
254 *
255 * Description:
256 * Enables a low level driver to set an upper limit on the number of
257 * segments in a discard request.
258 **/
259void blk_queue_max_discard_segments(struct request_queue *q,
260 unsigned short max_segments)
261{
262 q->limits.max_discard_segments = max_segments;
263}
264EXPORT_SYMBOL_GPL(blk_queue_max_discard_segments);
265
86db1e29
JA
266/**
267 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
268 * @q: the request queue for the device
269 * @max_size: max size of segment in bytes
270 *
271 * Description:
272 * Enables a low level driver to set an upper limit on the size of a
273 * coalesced segment
274 **/
275void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
276{
09cbfeaf
KS
277 if (max_size < PAGE_SIZE) {
278 max_size = PAGE_SIZE;
24c03d47
HH
279 printk(KERN_INFO "%s: set to minimum %d\n",
280 __func__, max_size);
86db1e29
JA
281 }
282
09324d32
CH
283 /* see blk_queue_virt_boundary() for the explanation */
284 WARN_ON_ONCE(q->limits.virt_boundary_mask);
285
025146e1 286 q->limits.max_segment_size = max_size;
86db1e29 287}
86db1e29
JA
288EXPORT_SYMBOL(blk_queue_max_segment_size);
289
290/**
e1defc4f 291 * blk_queue_logical_block_size - set logical block size for the queue
86db1e29 292 * @q: the request queue for the device
e1defc4f 293 * @size: the logical block size, in bytes
86db1e29
JA
294 *
295 * Description:
e1defc4f
MP
296 * This should be set to the lowest possible block size that the
297 * storage device can address. The default of 512 covers most
298 * hardware.
86db1e29 299 **/
ad6bf88a 300void blk_queue_logical_block_size(struct request_queue *q, unsigned int size)
86db1e29 301{
817046ec
DLM
302 struct queue_limits *limits = &q->limits;
303
304 limits->logical_block_size = size;
305
306 if (limits->physical_block_size < size)
307 limits->physical_block_size = size;
c72758f3 308
817046ec
DLM
309 if (limits->io_min < limits->physical_block_size)
310 limits->io_min = limits->physical_block_size;
c72758f3 311
817046ec
DLM
312 limits->max_hw_sectors =
313 round_down(limits->max_hw_sectors, size >> SECTOR_SHIFT);
314 limits->max_sectors =
315 round_down(limits->max_sectors, size >> SECTOR_SHIFT);
86db1e29 316}
e1defc4f 317EXPORT_SYMBOL(blk_queue_logical_block_size);
86db1e29 318
c72758f3
MP
319/**
320 * blk_queue_physical_block_size - set physical block size for the queue
321 * @q: the request queue for the device
322 * @size: the physical block size, in bytes
323 *
324 * Description:
325 * This should be set to the lowest possible sector size that the
326 * hardware can operate on without reverting to read-modify-write
327 * operations.
328 */
892b6f90 329void blk_queue_physical_block_size(struct request_queue *q, unsigned int size)
c72758f3
MP
330{
331 q->limits.physical_block_size = size;
332
333 if (q->limits.physical_block_size < q->limits.logical_block_size)
334 q->limits.physical_block_size = q->limits.logical_block_size;
335
336 if (q->limits.io_min < q->limits.physical_block_size)
337 q->limits.io_min = q->limits.physical_block_size;
338}
339EXPORT_SYMBOL(blk_queue_physical_block_size);
340
a805a4fa
DLM
341/**
342 * blk_queue_zone_write_granularity - set zone write granularity for the queue
343 * @q: the request queue for the zoned device
344 * @size: the zone write granularity size, in bytes
345 *
346 * Description:
347 * This should be set to the lowest possible size allowing to write in
348 * sequential zones of a zoned block device.
349 */
350void blk_queue_zone_write_granularity(struct request_queue *q,
351 unsigned int size)
352{
353 if (WARN_ON_ONCE(!blk_queue_is_zoned(q)))
354 return;
355
356 q->limits.zone_write_granularity = size;
357
358 if (q->limits.zone_write_granularity < q->limits.logical_block_size)
359 q->limits.zone_write_granularity = q->limits.logical_block_size;
360}
361EXPORT_SYMBOL_GPL(blk_queue_zone_write_granularity);
362
c72758f3
MP
363/**
364 * blk_queue_alignment_offset - set physical block alignment offset
365 * @q: the request queue for the device
8ebf9756 366 * @offset: alignment offset in bytes
c72758f3
MP
367 *
368 * Description:
369 * Some devices are naturally misaligned to compensate for things like
370 * the legacy DOS partition table 63-sector offset. Low-level drivers
371 * should call this function for devices whose first sector is not
372 * naturally aligned.
373 */
374void blk_queue_alignment_offset(struct request_queue *q, unsigned int offset)
375{
376 q->limits.alignment_offset =
377 offset & (q->limits.physical_block_size - 1);
378 q->limits.misaligned = 0;
379}
380EXPORT_SYMBOL(blk_queue_alignment_offset);
381
c2e4cd57
CH
382void blk_queue_update_readahead(struct request_queue *q)
383{
384 /*
385 * For read-ahead of large files to be effective, we need to read ahead
386 * at least twice the optimal I/O size.
387 */
388 q->backing_dev_info->ra_pages =
389 max(queue_io_opt(q) * 2 / PAGE_SIZE, VM_READAHEAD_PAGES);
390 q->backing_dev_info->io_pages =
391 queue_max_sectors(q) >> (PAGE_SHIFT - 9);
392}
393EXPORT_SYMBOL_GPL(blk_queue_update_readahead);
394
7c958e32
MP
395/**
396 * blk_limits_io_min - set minimum request size for a device
397 * @limits: the queue limits
398 * @min: smallest I/O size in bytes
399 *
400 * Description:
401 * Some devices have an internal block size bigger than the reported
402 * hardware sector size. This function can be used to signal the
403 * smallest I/O the device can perform without incurring a performance
404 * penalty.
405 */
406void blk_limits_io_min(struct queue_limits *limits, unsigned int min)
407{
408 limits->io_min = min;
409
410 if (limits->io_min < limits->logical_block_size)
411 limits->io_min = limits->logical_block_size;
412
413 if (limits->io_min < limits->physical_block_size)
414 limits->io_min = limits->physical_block_size;
415}
416EXPORT_SYMBOL(blk_limits_io_min);
417
c72758f3
MP
418/**
419 * blk_queue_io_min - set minimum request size for the queue
420 * @q: the request queue for the device
8ebf9756 421 * @min: smallest I/O size in bytes
c72758f3
MP
422 *
423 * Description:
7e5f5fb0
MP
424 * Storage devices may report a granularity or preferred minimum I/O
425 * size which is the smallest request the device can perform without
426 * incurring a performance penalty. For disk drives this is often the
427 * physical block size. For RAID arrays it is often the stripe chunk
428 * size. A properly aligned multiple of minimum_io_size is the
429 * preferred request size for workloads where a high number of I/O
430 * operations is desired.
c72758f3
MP
431 */
432void blk_queue_io_min(struct request_queue *q, unsigned int min)
433{
7c958e32 434 blk_limits_io_min(&q->limits, min);
c72758f3
MP
435}
436EXPORT_SYMBOL(blk_queue_io_min);
437
3c5820c7
MP
438/**
439 * blk_limits_io_opt - set optimal request size for a device
440 * @limits: the queue limits
441 * @opt: smallest I/O size in bytes
442 *
443 * Description:
444 * Storage devices may report an optimal I/O size, which is the
445 * device's preferred unit for sustained I/O. This is rarely reported
446 * for disk drives. For RAID arrays it is usually the stripe width or
447 * the internal track size. A properly aligned multiple of
448 * optimal_io_size is the preferred request size for workloads where
449 * sustained throughput is desired.
450 */
451void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt)
452{
453 limits->io_opt = opt;
454}
455EXPORT_SYMBOL(blk_limits_io_opt);
456
c72758f3
MP
457/**
458 * blk_queue_io_opt - set optimal request size for the queue
459 * @q: the request queue for the device
8ebf9756 460 * @opt: optimal request size in bytes
c72758f3
MP
461 *
462 * Description:
7e5f5fb0
MP
463 * Storage devices may report an optimal I/O size, which is the
464 * device's preferred unit for sustained I/O. This is rarely reported
465 * for disk drives. For RAID arrays it is usually the stripe width or
466 * the internal track size. A properly aligned multiple of
467 * optimal_io_size is the preferred request size for workloads where
468 * sustained throughput is desired.
c72758f3
MP
469 */
470void blk_queue_io_opt(struct request_queue *q, unsigned int opt)
471{
3c5820c7 472 blk_limits_io_opt(&q->limits, opt);
c2e4cd57
CH
473 q->backing_dev_info->ra_pages =
474 max(queue_io_opt(q) * 2 / PAGE_SIZE, VM_READAHEAD_PAGES);
c72758f3
MP
475}
476EXPORT_SYMBOL(blk_queue_io_opt);
477
97f433c3
MP
478static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs)
479{
480 sectors = round_down(sectors, lbs >> SECTOR_SHIFT);
481 if (sectors < PAGE_SIZE >> SECTOR_SHIFT)
482 sectors = PAGE_SIZE >> SECTOR_SHIFT;
483 return sectors;
484}
485
c72758f3
MP
486/**
487 * blk_stack_limits - adjust queue_limits for stacked devices
81744ee4
MP
488 * @t: the stacking driver limits (top device)
489 * @b: the underlying queue limits (bottom, component device)
e03a72e1 490 * @start: first data sector within component device
c72758f3
MP
491 *
492 * Description:
81744ee4
MP
493 * This function is used by stacking drivers like MD and DM to ensure
494 * that all component devices have compatible block sizes and
495 * alignments. The stacking driver must provide a queue_limits
496 * struct (top) and then iteratively call the stacking function for
497 * all component (bottom) devices. The stacking function will
498 * attempt to combine the values and ensure proper alignment.
499 *
500 * Returns 0 if the top and bottom queue_limits are compatible. The
501 * top device's block sizes and alignment offsets may be adjusted to
502 * ensure alignment with the bottom device. If no compatible sizes
503 * and alignments exist, -1 is returned and the resulting top
504 * queue_limits will have the misaligned flag set to indicate that
505 * the alignment_offset is undefined.
c72758f3
MP
506 */
507int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
e03a72e1 508 sector_t start)
c72758f3 509{
e03a72e1 510 unsigned int top, bottom, alignment, ret = 0;
86b37281 511
c72758f3
MP
512 t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
513 t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
ca369d51 514 t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
4363ac7c
MP
515 t->max_write_same_sectors = min(t->max_write_same_sectors,
516 b->max_write_same_sectors);
a6f0788e
CK
517 t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors,
518 b->max_write_zeroes_sectors);
0512a75b
KB
519 t->max_zone_append_sectors = min(t->max_zone_append_sectors,
520 b->max_zone_append_sectors);
9bb33f24 521 t->bounce = max(t->bounce, b->bounce);
c72758f3
MP
522
523 t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
524 b->seg_boundary_mask);
03100aad
KB
525 t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask,
526 b->virt_boundary_mask);
c72758f3 527
8a78362c 528 t->max_segments = min_not_zero(t->max_segments, b->max_segments);
1e739730
CH
529 t->max_discard_segments = min_not_zero(t->max_discard_segments,
530 b->max_discard_segments);
13f05c8d
MP
531 t->max_integrity_segments = min_not_zero(t->max_integrity_segments,
532 b->max_integrity_segments);
c72758f3
MP
533
534 t->max_segment_size = min_not_zero(t->max_segment_size,
535 b->max_segment_size);
536
fe0b393f
MP
537 t->misaligned |= b->misaligned;
538
e03a72e1 539 alignment = queue_limit_alignment_offset(b, start);
9504e086 540
81744ee4
MP
541 /* Bottom device has different alignment. Check that it is
542 * compatible with the current top alignment.
543 */
9504e086
MP
544 if (t->alignment_offset != alignment) {
545
546 top = max(t->physical_block_size, t->io_min)
547 + t->alignment_offset;
81744ee4 548 bottom = max(b->physical_block_size, b->io_min) + alignment;
9504e086 549
81744ee4 550 /* Verify that top and bottom intervals line up */
b8839b8c 551 if (max(top, bottom) % min(top, bottom)) {
9504e086 552 t->misaligned = 1;
fe0b393f
MP
553 ret = -1;
554 }
9504e086
MP
555 }
556
c72758f3
MP
557 t->logical_block_size = max(t->logical_block_size,
558 b->logical_block_size);
559
560 t->physical_block_size = max(t->physical_block_size,
561 b->physical_block_size);
562
563 t->io_min = max(t->io_min, b->io_min);
e9637415 564 t->io_opt = lcm_not_zero(t->io_opt, b->io_opt);
7e7986f9
MS
565
566 /* Set non-power-of-2 compatible chunk_sectors boundary */
567 if (b->chunk_sectors)
568 t->chunk_sectors = gcd(t->chunk_sectors, b->chunk_sectors);
9504e086 569
81744ee4 570 /* Physical block size a multiple of the logical block size? */
9504e086
MP
571 if (t->physical_block_size & (t->logical_block_size - 1)) {
572 t->physical_block_size = t->logical_block_size;
c72758f3 573 t->misaligned = 1;
fe0b393f 574 ret = -1;
86b37281
MP
575 }
576
81744ee4 577 /* Minimum I/O a multiple of the physical block size? */
9504e086
MP
578 if (t->io_min & (t->physical_block_size - 1)) {
579 t->io_min = t->physical_block_size;
580 t->misaligned = 1;
fe0b393f 581 ret = -1;
c72758f3
MP
582 }
583
81744ee4 584 /* Optimal I/O a multiple of the physical block size? */
9504e086
MP
585 if (t->io_opt & (t->physical_block_size - 1)) {
586 t->io_opt = 0;
587 t->misaligned = 1;
fe0b393f 588 ret = -1;
9504e086 589 }
c72758f3 590
22ada802
MS
591 /* chunk_sectors a multiple of the physical block size? */
592 if ((t->chunk_sectors << 9) & (t->physical_block_size - 1)) {
593 t->chunk_sectors = 0;
594 t->misaligned = 1;
595 ret = -1;
596 }
597
c78afc62
KO
598 t->raid_partial_stripes_expensive =
599 max(t->raid_partial_stripes_expensive,
600 b->raid_partial_stripes_expensive);
601
81744ee4 602 /* Find lowest common alignment_offset */
e9637415 603 t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment)
b8839b8c 604 % max(t->physical_block_size, t->io_min);
86b37281 605
81744ee4 606 /* Verify that new alignment_offset is on a logical block boundary */
fe0b393f 607 if (t->alignment_offset & (t->logical_block_size - 1)) {
c72758f3 608 t->misaligned = 1;
fe0b393f
MP
609 ret = -1;
610 }
c72758f3 611
97f433c3
MP
612 t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size);
613 t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size);
614 t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size);
615
9504e086
MP
616 /* Discard alignment and granularity */
617 if (b->discard_granularity) {
e03a72e1 618 alignment = queue_limit_discard_alignment(b, start);
9504e086
MP
619
620 if (t->discard_granularity != 0 &&
621 t->discard_alignment != alignment) {
622 top = t->discard_granularity + t->discard_alignment;
623 bottom = b->discard_granularity + alignment;
70dd5bf3 624
9504e086 625 /* Verify that top and bottom intervals line up */
8dd2cb7e 626 if ((max(top, bottom) % min(top, bottom)) != 0)
9504e086
MP
627 t->discard_misaligned = 1;
628 }
629
81744ee4
MP
630 t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
631 b->max_discard_sectors);
0034af03
JA
632 t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
633 b->max_hw_discard_sectors);
9504e086
MP
634 t->discard_granularity = max(t->discard_granularity,
635 b->discard_granularity);
e9637415 636 t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
8dd2cb7e 637 t->discard_granularity;
9504e086 638 }
70dd5bf3 639
a805a4fa
DLM
640 t->zone_write_granularity = max(t->zone_write_granularity,
641 b->zone_write_granularity);
3093a479 642 t->zoned = max(t->zoned, b->zoned);
fe0b393f 643 return ret;
c72758f3 644}
5d85d324 645EXPORT_SYMBOL(blk_stack_limits);
c72758f3
MP
646
647/**
648 * disk_stack_limits - adjust queue limits for stacked drivers
77634f33 649 * @disk: MD/DM gendisk (top)
c72758f3
MP
650 * @bdev: the underlying block device (bottom)
651 * @offset: offset to beginning of data within component device
652 *
653 * Description:
e03a72e1
MP
654 * Merges the limits for a top level gendisk and a bottom level
655 * block_device.
c72758f3
MP
656 */
657void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
658 sector_t offset)
659{
660 struct request_queue *t = disk->queue;
c72758f3 661
9efa82ef
CH
662 if (blk_stack_limits(&t->limits, &bdev_get_queue(bdev)->limits,
663 get_start_sect(bdev) + (offset >> 9)) < 0) {
c72758f3
MP
664 char top[BDEVNAME_SIZE], bottom[BDEVNAME_SIZE];
665
666 disk_name(disk, 0, top);
667 bdevname(bdev, bottom);
668
669 printk(KERN_NOTICE "%s: Warning: Device %s is misaligned\n",
670 top, bottom);
671 }
e74d93e9 672
c2e4cd57 673 blk_queue_update_readahead(disk->queue);
c72758f3
MP
674}
675EXPORT_SYMBOL(disk_stack_limits);
676
27f8221a
FT
677/**
678 * blk_queue_update_dma_pad - update pad mask
679 * @q: the request queue for the device
680 * @mask: pad mask
681 *
682 * Update dma pad mask.
683 *
684 * Appending pad buffer to a request modifies the last entry of a
685 * scatter list such that it includes the pad buffer.
686 **/
687void blk_queue_update_dma_pad(struct request_queue *q, unsigned int mask)
688{
689 if (mask > q->dma_pad_mask)
690 q->dma_pad_mask = mask;
691}
692EXPORT_SYMBOL(blk_queue_update_dma_pad);
693
86db1e29
JA
694/**
695 * blk_queue_segment_boundary - set boundary rules for segment merging
696 * @q: the request queue for the device
697 * @mask: the memory boundary mask
698 **/
699void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
700{
09cbfeaf
KS
701 if (mask < PAGE_SIZE - 1) {
702 mask = PAGE_SIZE - 1;
24c03d47
HH
703 printk(KERN_INFO "%s: set to minimum %lx\n",
704 __func__, mask);
86db1e29
JA
705 }
706
025146e1 707 q->limits.seg_boundary_mask = mask;
86db1e29 708}
86db1e29
JA
709EXPORT_SYMBOL(blk_queue_segment_boundary);
710
03100aad
KB
711/**
712 * blk_queue_virt_boundary - set boundary rules for bio merging
713 * @q: the request queue for the device
714 * @mask: the memory boundary mask
715 **/
716void blk_queue_virt_boundary(struct request_queue *q, unsigned long mask)
717{
718 q->limits.virt_boundary_mask = mask;
09324d32
CH
719
720 /*
721 * Devices that require a virtual boundary do not support scatter/gather
722 * I/O natively, but instead require a descriptor list entry for each
723 * page (which might not be idential to the Linux PAGE_SIZE). Because
724 * of that they are not limited by our notion of "segment size".
725 */
c6c84f78
CH
726 if (mask)
727 q->limits.max_segment_size = UINT_MAX;
03100aad
KB
728}
729EXPORT_SYMBOL(blk_queue_virt_boundary);
730
86db1e29
JA
731/**
732 * blk_queue_dma_alignment - set dma length and memory alignment
733 * @q: the request queue for the device
734 * @mask: alignment mask
735 *
736 * description:
710027a4 737 * set required memory and length alignment for direct dma transactions.
8feb4d20 738 * this is used when building direct io requests for the queue.
86db1e29
JA
739 *
740 **/
741void blk_queue_dma_alignment(struct request_queue *q, int mask)
742{
743 q->dma_alignment = mask;
744}
86db1e29
JA
745EXPORT_SYMBOL(blk_queue_dma_alignment);
746
747/**
748 * blk_queue_update_dma_alignment - update dma length and memory alignment
749 * @q: the request queue for the device
750 * @mask: alignment mask
751 *
752 * description:
710027a4 753 * update required memory and length alignment for direct dma transactions.
86db1e29
JA
754 * If the requested alignment is larger than the current alignment, then
755 * the current queue alignment is updated to the new value, otherwise it
756 * is left alone. The design of this is to allow multiple objects
757 * (driver, device, transport etc) to set their respective
758 * alignments without having them interfere.
759 *
760 **/
761void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
762{
763 BUG_ON(mask > PAGE_SIZE);
764
765 if (mask > q->dma_alignment)
766 q->dma_alignment = mask;
767}
86db1e29
JA
768EXPORT_SYMBOL(blk_queue_update_dma_alignment);
769
d278d4a8
JA
770/**
771 * blk_set_queue_depth - tell the block layer about the device queue depth
772 * @q: the request queue for the device
773 * @depth: queue depth
774 *
775 */
776void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
777{
778 q->queue_depth = depth;
9677a3e0 779 rq_qos_queue_depth_changed(q);
d278d4a8
JA
780}
781EXPORT_SYMBOL(blk_set_queue_depth);
782
93e9d8e8
JA
783/**
784 * blk_queue_write_cache - configure queue's write cache
785 * @q: the request queue for the device
786 * @wc: write back cache on or off
787 * @fua: device supports FUA writes, if true
788 *
789 * Tell the block layer about the write cache of @q.
790 */
791void blk_queue_write_cache(struct request_queue *q, bool wc, bool fua)
792{
c888a8f9 793 if (wc)
57d74df9 794 blk_queue_flag_set(QUEUE_FLAG_WC, q);
c888a8f9 795 else
57d74df9 796 blk_queue_flag_clear(QUEUE_FLAG_WC, q);
c888a8f9 797 if (fua)
57d74df9 798 blk_queue_flag_set(QUEUE_FLAG_FUA, q);
c888a8f9 799 else
57d74df9 800 blk_queue_flag_clear(QUEUE_FLAG_FUA, q);
87760e5e 801
a7905043 802 wbt_set_write_cache(q, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
93e9d8e8
JA
803}
804EXPORT_SYMBOL_GPL(blk_queue_write_cache);
805
68c43f13
DLM
806/**
807 * blk_queue_required_elevator_features - Set a queue required elevator features
808 * @q: the request queue for the target device
809 * @features: Required elevator features OR'ed together
810 *
811 * Tell the block layer that for the device controlled through @q, only the
812 * only elevators that can be used are those that implement at least the set of
813 * features specified by @features.
814 */
815void blk_queue_required_elevator_features(struct request_queue *q,
816 unsigned int features)
817{
818 q->required_elevator_features = features;
819}
820EXPORT_SYMBOL_GPL(blk_queue_required_elevator_features);
821
45147fb5
YS
822/**
823 * blk_queue_can_use_dma_map_merging - configure queue for merging segments.
824 * @q: the request queue for the device
825 * @dev: the device pointer for dma
826 *
827 * Tell the block layer about merging the segments by dma map of @q.
828 */
829bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
830 struct device *dev)
831{
832 unsigned long boundary = dma_get_merge_boundary(dev);
833
834 if (!boundary)
835 return false;
836
837 /* No need to update max_segment_size. see blk_queue_virt_boundary() */
838 blk_queue_virt_boundary(q, boundary);
839
840 return true;
841}
842EXPORT_SYMBOL_GPL(blk_queue_can_use_dma_map_merging);
843
27ba3e8f
DLM
844/**
845 * blk_queue_set_zoned - configure a disk queue zoned model.
846 * @disk: the gendisk of the queue to configure
847 * @model: the zoned model to set
848 *
849 * Set the zoned model of the request queue of @disk according to @model.
850 * When @model is BLK_ZONED_HM (host managed), this should be called only
851 * if zoned block device support is enabled (CONFIG_BLK_DEV_ZONED option).
852 * If @model specifies BLK_ZONED_HA (host aware), the effective model used
853 * depends on CONFIG_BLK_DEV_ZONED settings and on the existence of partitions
854 * on the disk.
855 */
856void blk_queue_set_zoned(struct gendisk *disk, enum blk_zoned_model model)
857{
a805a4fa
DLM
858 struct request_queue *q = disk->queue;
859
27ba3e8f
DLM
860 switch (model) {
861 case BLK_ZONED_HM:
862 /*
863 * Host managed devices are supported only if
864 * CONFIG_BLK_DEV_ZONED is enabled.
865 */
866 WARN_ON_ONCE(!IS_ENABLED(CONFIG_BLK_DEV_ZONED));
867 break;
868 case BLK_ZONED_HA:
869 /*
870 * Host aware devices can be treated either as regular block
871 * devices (similar to drive managed devices) or as zoned block
872 * devices to take advantage of the zone command set, similarly
873 * to host managed devices. We try the latter if there are no
874 * partitions and zoned block device support is enabled, else
875 * we do nothing special as far as the block layer is concerned.
876 */
877 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED) ||
a33df75c 878 !xa_empty(&disk->part_tbl))
27ba3e8f
DLM
879 model = BLK_ZONED_NONE;
880 break;
881 case BLK_ZONED_NONE:
882 default:
883 if (WARN_ON_ONCE(model != BLK_ZONED_NONE))
884 model = BLK_ZONED_NONE;
885 break;
886 }
887
a805a4fa
DLM
888 q->limits.zoned = model;
889 if (model != BLK_ZONED_NONE) {
890 /*
891 * Set the zone write granularity to the device logical block
892 * size by default. The driver can change this value if needed.
893 */
894 blk_queue_zone_write_granularity(q,
895 queue_logical_block_size(q));
508aebb8
DLM
896 } else {
897 blk_queue_clear_zone_settings(q);
a805a4fa 898 }
27ba3e8f
DLM
899}
900EXPORT_SYMBOL_GPL(blk_queue_set_zoned);