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