block: use queue_limits_commit_update in queue_max_sectors_store
[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>
4ee60ec1 10#include <linux/pagemap.h>
edb0872f 11#include <linux/backing-dev-defs.h>
70dd5bf3 12#include <linux/gcd.h>
2cda2728 13#include <linux/lcm.h>
ad5ebd2f 14#include <linux/jiffies.h>
5a0e3ad6 15#include <linux/gfp.h>
45147fb5 16#include <linux/dma-mapping.h>
86db1e29
JA
17
18#include "blk.h"
0bc65bd4 19#include "blk-rq-qos.h"
87760e5e 20#include "blk-wbt.h"
86db1e29 21
242f9dcb
JA
22void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout)
23{
24 q->rq_timeout = timeout;
25}
26EXPORT_SYMBOL_GPL(blk_queue_rq_timeout);
27
b1bd055d
MP
28/**
29 * blk_set_stacking_limits - set default limits for stacking devices
30 * @lim: the queue_limits structure to reset
31 *
c490f226
CH
32 * Prepare queue limits for applying limits from underlying devices using
33 * blk_stack_limits().
b1bd055d
MP
34 */
35void blk_set_stacking_limits(struct queue_limits *lim)
36{
c490f226
CH
37 memset(lim, 0, sizeof(*lim));
38 lim->logical_block_size = SECTOR_SIZE;
39 lim->physical_block_size = SECTOR_SIZE;
40 lim->io_min = SECTOR_SIZE;
41 lim->discard_granularity = SECTOR_SIZE;
42 lim->dma_alignment = SECTOR_SIZE - 1;
43 lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
b1bd055d
MP
44
45 /* Inherit limits from component devices */
b1bd055d 46 lim->max_segments = USHRT_MAX;
42c9cdfe 47 lim->max_discard_segments = USHRT_MAX;
b1bd055d 48 lim->max_hw_sectors = UINT_MAX;
d82ae52e 49 lim->max_segment_size = UINT_MAX;
fe86cdce 50 lim->max_sectors = UINT_MAX;
ca369d51 51 lim->max_dev_sectors = UINT_MAX;
a6f0788e 52 lim->max_write_zeroes_sectors = UINT_MAX;
0512a75b 53 lim->max_zone_append_sectors = UINT_MAX;
b1bd055d
MP
54}
55EXPORT_SYMBOL(blk_set_stacking_limits);
56
b9947297
CH
57static void blk_apply_bdi_limits(struct backing_dev_info *bdi,
58 struct queue_limits *lim)
59{
60 /*
61 * For read-ahead of large files to be effective, we need to read ahead
62 * at least twice the optimal I/O size.
63 */
64 bdi->ra_pages = max(lim->io_opt * 2 / PAGE_SIZE, VM_READAHEAD_PAGES);
65 bdi->io_pages = lim->max_sectors >> PAGE_SECTORS_SHIFT;
66}
67
d690cb8a
CH
68static int blk_validate_zoned_limits(struct queue_limits *lim)
69{
70 if (!lim->zoned) {
71 if (WARN_ON_ONCE(lim->max_open_zones) ||
72 WARN_ON_ONCE(lim->max_active_zones) ||
73 WARN_ON_ONCE(lim->zone_write_granularity) ||
74 WARN_ON_ONCE(lim->max_zone_append_sectors))
75 return -EINVAL;
76 return 0;
77 }
78
79 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_BLK_DEV_ZONED)))
80 return -EINVAL;
81
82 if (lim->zone_write_granularity < lim->logical_block_size)
83 lim->zone_write_granularity = lim->logical_block_size;
84
85 if (lim->max_zone_append_sectors) {
86 /*
87 * The Zone Append size is limited by the maximum I/O size
88 * and the zone size given that it can't span zones.
89 */
90 lim->max_zone_append_sectors =
91 min3(lim->max_hw_sectors,
92 lim->max_zone_append_sectors,
93 lim->chunk_sectors);
94 }
95
96 return 0;
97}
98
99/*
100 * Check that the limits in lim are valid, initialize defaults for unset
101 * values, and cap values based on others where needed.
102 */
103static int blk_validate_limits(struct queue_limits *lim)
104{
105 unsigned int max_hw_sectors;
106
107 /*
108 * Unless otherwise specified, default to 512 byte logical blocks and a
109 * physical block size equal to the logical block size.
110 */
111 if (!lim->logical_block_size)
112 lim->logical_block_size = SECTOR_SIZE;
113 if (lim->physical_block_size < lim->logical_block_size)
114 lim->physical_block_size = lim->logical_block_size;
115
116 /*
117 * The minimum I/O size defaults to the physical block size unless
118 * explicitly overridden.
119 */
120 if (lim->io_min < lim->physical_block_size)
121 lim->io_min = lim->physical_block_size;
122
123 /*
124 * max_hw_sectors has a somewhat weird default for historical reason,
125 * but driver really should set their own instead of relying on this
126 * value.
127 *
128 * The block layer relies on the fact that every driver can
129 * handle at lest a page worth of data per I/O, and needs the value
130 * aligned to the logical block size.
131 */
132 if (!lim->max_hw_sectors)
133 lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS;
134 if (WARN_ON_ONCE(lim->max_hw_sectors < PAGE_SECTORS))
135 return -EINVAL;
136 lim->max_hw_sectors = round_down(lim->max_hw_sectors,
137 lim->logical_block_size >> SECTOR_SHIFT);
138
139 /*
140 * The actual max_sectors value is a complex beast and also takes the
141 * max_dev_sectors value (set by SCSI ULPs) and a user configurable
142 * value into account. The ->max_sectors value is always calculated
143 * from these, so directly setting it won't have any effect.
144 */
145 max_hw_sectors = min_not_zero(lim->max_hw_sectors,
146 lim->max_dev_sectors);
147 if (lim->max_user_sectors) {
148 if (lim->max_user_sectors > max_hw_sectors ||
149 lim->max_user_sectors < PAGE_SIZE / SECTOR_SIZE)
150 return -EINVAL;
151 lim->max_sectors = min(max_hw_sectors, lim->max_user_sectors);
152 } else {
153 lim->max_sectors = min(max_hw_sectors, BLK_DEF_MAX_SECTORS_CAP);
154 }
155 lim->max_sectors = round_down(lim->max_sectors,
156 lim->logical_block_size >> SECTOR_SHIFT);
157
158 /*
159 * Random default for the maximum number of segments. Driver should not
160 * rely on this and set their own.
161 */
162 if (!lim->max_segments)
163 lim->max_segments = BLK_MAX_SEGMENTS;
164
165 lim->max_discard_sectors = lim->max_hw_discard_sectors;
166 if (!lim->max_discard_segments)
167 lim->max_discard_segments = 1;
168
169 if (lim->discard_granularity < lim->physical_block_size)
170 lim->discard_granularity = lim->physical_block_size;
171
172 /*
173 * By default there is no limit on the segment boundary alignment,
174 * but if there is one it can't be smaller than the page size as
175 * that would break all the normal I/O patterns.
176 */
177 if (!lim->seg_boundary_mask)
178 lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
179 if (WARN_ON_ONCE(lim->seg_boundary_mask < PAGE_SIZE - 1))
180 return -EINVAL;
181
182 /*
183 * The maximum segment size has an odd historic 64k default that
184 * drivers probably should override. Just like the I/O size we
185 * require drivers to at least handle a full page per segment.
186 */
187 if (!lim->max_segment_size)
188 lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
189 if (WARN_ON_ONCE(lim->max_segment_size < PAGE_SIZE))
190 return -EINVAL;
191
192 /*
193 * Devices that require a virtual boundary do not support scatter/gather
194 * I/O natively, but instead require a descriptor list entry for each
195 * page (which might not be identical to the Linux PAGE_SIZE). Because
196 * of that they are not limited by our notion of "segment size".
197 */
198 if (lim->virt_boundary_mask) {
199 if (WARN_ON_ONCE(lim->max_segment_size &&
200 lim->max_segment_size != UINT_MAX))
201 return -EINVAL;
202 lim->max_segment_size = UINT_MAX;
203 }
204
205 /*
206 * We require drivers to at least do logical block aligned I/O, but
207 * historically could not check for that due to the separate calls
208 * to set the limits. Once the transition is finished the check
209 * below should be narrowed down to check the logical block size.
210 */
211 if (!lim->dma_alignment)
212 lim->dma_alignment = SECTOR_SIZE - 1;
213 if (WARN_ON_ONCE(lim->dma_alignment > PAGE_SIZE))
214 return -EINVAL;
215
216 if (lim->alignment_offset) {
217 lim->alignment_offset &= (lim->physical_block_size - 1);
218 lim->misaligned = 0;
219 }
220
221 return blk_validate_zoned_limits(lim);
222}
223
224/*
225 * Set the default limits for a newly allocated queue. @lim contains the
226 * initial limits set by the driver, which could be no limit in which case
227 * all fields are cleared to zero.
228 */
229int blk_set_default_limits(struct queue_limits *lim)
230{
231 return blk_validate_limits(lim);
232}
233
234/**
235 * queue_limits_commit_update - commit an atomic update of queue limits
236 * @q: queue to update
237 * @lim: limits to apply
238 *
239 * Apply the limits in @lim that were obtained from queue_limits_start_update()
240 * and updated by the caller to @q.
241 *
242 * Returns 0 if successful, else a negative error code.
243 */
244int queue_limits_commit_update(struct request_queue *q,
245 struct queue_limits *lim)
246 __releases(q->limits_lock)
247{
248 int error = blk_validate_limits(lim);
249
250 if (!error) {
251 q->limits = *lim;
252 if (q->disk)
253 blk_apply_bdi_limits(q->disk->bdi, lim);
254 }
255 mutex_unlock(&q->limits_lock);
256 return error;
257}
258EXPORT_SYMBOL_GPL(queue_limits_commit_update);
259
86db1e29
JA
260/**
261 * blk_queue_bounce_limit - set bounce buffer limit for queue
cd0aca2d 262 * @q: the request queue for the device
9bb33f24 263 * @bounce: bounce limit to enforce
86db1e29
JA
264 *
265 * Description:
9bb33f24
CH
266 * Force bouncing for ISA DMA ranges or highmem.
267 *
268 * DEPRECATED, don't use in new code.
86db1e29 269 **/
9bb33f24 270void blk_queue_bounce_limit(struct request_queue *q, enum blk_bounce bounce)
86db1e29 271{
9bb33f24 272 q->limits.bounce = bounce;
86db1e29 273}
86db1e29
JA
274EXPORT_SYMBOL(blk_queue_bounce_limit);
275
276/**
ca369d51
MP
277 * blk_queue_max_hw_sectors - set max sectors for a request for this queue
278 * @q: the request queue for the device
2800aac1 279 * @max_hw_sectors: max hardware sectors in the usual 512b unit
86db1e29
JA
280 *
281 * Description:
2800aac1
MP
282 * Enables a low level driver to set a hard upper limit,
283 * max_hw_sectors, on the size of requests. max_hw_sectors is set by
4f258a46
MP
284 * the device driver based upon the capabilities of the I/O
285 * controller.
2800aac1 286 *
ca369d51
MP
287 * max_dev_sectors is a hard limit imposed by the storage device for
288 * READ/WRITE requests. It is set by the disk driver.
289 *
2800aac1
MP
290 * max_sectors is a soft limit imposed by the block layer for
291 * filesystem type requests. This value can be overridden on a
292 * per-device basis in /sys/block/<device>/queue/max_sectors_kb.
293 * The soft limit can not exceed max_hw_sectors.
86db1e29 294 **/
ca369d51 295void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
86db1e29 296{
ca369d51
MP
297 struct queue_limits *limits = &q->limits;
298 unsigned int max_sectors;
299
09cbfeaf
KS
300 if ((max_hw_sectors << 9) < PAGE_SIZE) {
301 max_hw_sectors = 1 << (PAGE_SHIFT - 9);
f19d1e3b 302 pr_info("%s: set to minimum %u\n", __func__, max_hw_sectors);
86db1e29
JA
303 }
304
817046ec
DLM
305 max_hw_sectors = round_down(max_hw_sectors,
306 limits->logical_block_size >> SECTOR_SHIFT);
30e2bc08 307 limits->max_hw_sectors = max_hw_sectors;
817046ec 308
ca369d51 309 max_sectors = min_not_zero(max_hw_sectors, limits->max_dev_sectors);
c9c77418
KB
310
311 if (limits->max_user_sectors)
312 max_sectors = min(max_sectors, limits->max_user_sectors);
313 else
d6b9f4e6 314 max_sectors = min(max_sectors, BLK_DEF_MAX_SECTORS_CAP);
c9c77418 315
817046ec
DLM
316 max_sectors = round_down(max_sectors,
317 limits->logical_block_size >> SECTOR_SHIFT);
ca369d51 318 limits->max_sectors = max_sectors;
817046ec 319
d152c682 320 if (!q->disk)
edb0872f 321 return;
d152c682 322 q->disk->bdi->io_pages = max_sectors >> (PAGE_SHIFT - 9);
86db1e29 323}
086fa5ff 324EXPORT_SYMBOL(blk_queue_max_hw_sectors);
86db1e29 325
762380ad
JA
326/**
327 * blk_queue_chunk_sectors - set size of the chunk for this queue
328 * @q: the request queue for the device
329 * @chunk_sectors: chunk sectors in the usual 512b unit
330 *
331 * Description:
332 * If a driver doesn't want IOs to cross a given chunk size, it can set
07d098e6
MS
333 * this limit and prevent merging across chunks. Note that the block layer
334 * must accept a page worth of data at any offset. So if the crossing of
335 * chunks is a hard limitation in the driver, it must still be prepared
336 * to split single page bios.
762380ad
JA
337 **/
338void blk_queue_chunk_sectors(struct request_queue *q, unsigned int chunk_sectors)
339{
762380ad
JA
340 q->limits.chunk_sectors = chunk_sectors;
341}
342EXPORT_SYMBOL(blk_queue_chunk_sectors);
343
67efc925
CH
344/**
345 * blk_queue_max_discard_sectors - set max sectors for a single discard
346 * @q: the request queue for the device
c7ebf065 347 * @max_discard_sectors: maximum number of sectors to discard
67efc925
CH
348 **/
349void blk_queue_max_discard_sectors(struct request_queue *q,
350 unsigned int max_discard_sectors)
351{
0034af03 352 q->limits.max_hw_discard_sectors = max_discard_sectors;
67efc925
CH
353 q->limits.max_discard_sectors = max_discard_sectors;
354}
355EXPORT_SYMBOL(blk_queue_max_discard_sectors);
356
44abff2c
CH
357/**
358 * blk_queue_max_secure_erase_sectors - set max sectors for a secure erase
359 * @q: the request queue for the device
360 * @max_sectors: maximum number of sectors to secure_erase
361 **/
362void blk_queue_max_secure_erase_sectors(struct request_queue *q,
363 unsigned int max_sectors)
364{
365 q->limits.max_secure_erase_sectors = max_sectors;
366}
367EXPORT_SYMBOL(blk_queue_max_secure_erase_sectors);
368
a6f0788e
CK
369/**
370 * blk_queue_max_write_zeroes_sectors - set max sectors for a single
371 * write zeroes
372 * @q: the request queue for the device
373 * @max_write_zeroes_sectors: maximum number of sectors to write per command
374 **/
375void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
376 unsigned int max_write_zeroes_sectors)
377{
378 q->limits.max_write_zeroes_sectors = max_write_zeroes_sectors;
379}
380EXPORT_SYMBOL(blk_queue_max_write_zeroes_sectors);
381
0512a75b
KB
382/**
383 * blk_queue_max_zone_append_sectors - set max sectors for a single zone append
384 * @q: the request queue for the device
385 * @max_zone_append_sectors: maximum number of sectors to write per command
386 **/
387void blk_queue_max_zone_append_sectors(struct request_queue *q,
388 unsigned int max_zone_append_sectors)
389{
390 unsigned int max_sectors;
391
392 if (WARN_ON(!blk_queue_is_zoned(q)))
393 return;
394
395 max_sectors = min(q->limits.max_hw_sectors, max_zone_append_sectors);
396 max_sectors = min(q->limits.chunk_sectors, max_sectors);
397
398 /*
399 * Signal eventual driver bugs resulting in the max_zone_append sectors limit
400 * being 0 due to a 0 argument, the chunk_sectors limit (zone size) not set,
401 * or the max_hw_sectors limit not set.
402 */
403 WARN_ON(!max_sectors);
404
405 q->limits.max_zone_append_sectors = max_sectors;
406}
407EXPORT_SYMBOL_GPL(blk_queue_max_zone_append_sectors);
408
86db1e29 409/**
8a78362c 410 * blk_queue_max_segments - set max hw segments for a request for this queue
86db1e29
JA
411 * @q: the request queue for the device
412 * @max_segments: max number of segments
413 *
414 * Description:
415 * Enables a low level driver to set an upper limit on the number of
8a78362c 416 * hw data segments in a request.
86db1e29 417 **/
8a78362c 418void blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
86db1e29
JA
419{
420 if (!max_segments) {
421 max_segments = 1;
f19d1e3b 422 pr_info("%s: set to minimum %u\n", __func__, max_segments);
86db1e29
JA
423 }
424
8a78362c 425 q->limits.max_segments = max_segments;
86db1e29 426}
8a78362c 427EXPORT_SYMBOL(blk_queue_max_segments);
86db1e29 428
1e739730
CH
429/**
430 * blk_queue_max_discard_segments - set max segments for discard requests
431 * @q: the request queue for the device
432 * @max_segments: max number of segments
433 *
434 * Description:
435 * Enables a low level driver to set an upper limit on the number of
436 * segments in a discard request.
437 **/
438void blk_queue_max_discard_segments(struct request_queue *q,
439 unsigned short max_segments)
440{
441 q->limits.max_discard_segments = max_segments;
442}
443EXPORT_SYMBOL_GPL(blk_queue_max_discard_segments);
444
86db1e29
JA
445/**
446 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
447 * @q: the request queue for the device
448 * @max_size: max size of segment in bytes
449 *
450 * Description:
451 * Enables a low level driver to set an upper limit on the size of a
452 * coalesced segment
453 **/
454void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
455{
09cbfeaf
KS
456 if (max_size < PAGE_SIZE) {
457 max_size = PAGE_SIZE;
f19d1e3b 458 pr_info("%s: set to minimum %u\n", __func__, max_size);
86db1e29
JA
459 }
460
09324d32
CH
461 /* see blk_queue_virt_boundary() for the explanation */
462 WARN_ON_ONCE(q->limits.virt_boundary_mask);
463
025146e1 464 q->limits.max_segment_size = max_size;
86db1e29 465}
86db1e29
JA
466EXPORT_SYMBOL(blk_queue_max_segment_size);
467
468/**
e1defc4f 469 * blk_queue_logical_block_size - set logical block size for the queue
86db1e29 470 * @q: the request queue for the device
e1defc4f 471 * @size: the logical block size, in bytes
86db1e29
JA
472 *
473 * Description:
e1defc4f
MP
474 * This should be set to the lowest possible block size that the
475 * storage device can address. The default of 512 covers most
476 * hardware.
86db1e29 477 **/
ad6bf88a 478void blk_queue_logical_block_size(struct request_queue *q, unsigned int size)
86db1e29 479{
817046ec
DLM
480 struct queue_limits *limits = &q->limits;
481
482 limits->logical_block_size = size;
483
3c407dc7
CH
484 if (limits->discard_granularity < limits->logical_block_size)
485 limits->discard_granularity = limits->logical_block_size;
486
817046ec
DLM
487 if (limits->physical_block_size < size)
488 limits->physical_block_size = size;
c72758f3 489
817046ec
DLM
490 if (limits->io_min < limits->physical_block_size)
491 limits->io_min = limits->physical_block_size;
c72758f3 492
817046ec
DLM
493 limits->max_hw_sectors =
494 round_down(limits->max_hw_sectors, size >> SECTOR_SHIFT);
495 limits->max_sectors =
496 round_down(limits->max_sectors, size >> SECTOR_SHIFT);
86db1e29 497}
e1defc4f 498EXPORT_SYMBOL(blk_queue_logical_block_size);
86db1e29 499
c72758f3
MP
500/**
501 * blk_queue_physical_block_size - set physical block size for the queue
502 * @q: the request queue for the device
503 * @size: the physical block size, in bytes
504 *
505 * Description:
506 * This should be set to the lowest possible sector size that the
507 * hardware can operate on without reverting to read-modify-write
508 * operations.
509 */
892b6f90 510void blk_queue_physical_block_size(struct request_queue *q, unsigned int size)
c72758f3
MP
511{
512 q->limits.physical_block_size = size;
513
514 if (q->limits.physical_block_size < q->limits.logical_block_size)
515 q->limits.physical_block_size = q->limits.logical_block_size;
516
458aa1a0
CH
517 if (q->limits.discard_granularity < q->limits.physical_block_size)
518 q->limits.discard_granularity = q->limits.physical_block_size;
519
c72758f3
MP
520 if (q->limits.io_min < q->limits.physical_block_size)
521 q->limits.io_min = q->limits.physical_block_size;
522}
523EXPORT_SYMBOL(blk_queue_physical_block_size);
524
a805a4fa
DLM
525/**
526 * blk_queue_zone_write_granularity - set zone write granularity for the queue
527 * @q: the request queue for the zoned device
528 * @size: the zone write granularity size, in bytes
529 *
530 * Description:
531 * This should be set to the lowest possible size allowing to write in
532 * sequential zones of a zoned block device.
533 */
534void blk_queue_zone_write_granularity(struct request_queue *q,
535 unsigned int size)
536{
537 if (WARN_ON_ONCE(!blk_queue_is_zoned(q)))
538 return;
539
540 q->limits.zone_write_granularity = size;
541
542 if (q->limits.zone_write_granularity < q->limits.logical_block_size)
543 q->limits.zone_write_granularity = q->limits.logical_block_size;
544}
545EXPORT_SYMBOL_GPL(blk_queue_zone_write_granularity);
546
c72758f3
MP
547/**
548 * blk_queue_alignment_offset - set physical block alignment offset
549 * @q: the request queue for the device
8ebf9756 550 * @offset: alignment offset in bytes
c72758f3
MP
551 *
552 * Description:
553 * Some devices are naturally misaligned to compensate for things like
554 * the legacy DOS partition table 63-sector offset. Low-level drivers
555 * should call this function for devices whose first sector is not
556 * naturally aligned.
557 */
558void blk_queue_alignment_offset(struct request_queue *q, unsigned int offset)
559{
560 q->limits.alignment_offset =
561 offset & (q->limits.physical_block_size - 1);
562 q->limits.misaligned = 0;
563}
564EXPORT_SYMBOL(blk_queue_alignment_offset);
565
471aa704 566void disk_update_readahead(struct gendisk *disk)
c2e4cd57 567{
b9947297 568 blk_apply_bdi_limits(disk->bdi, &disk->queue->limits);
c2e4cd57 569}
471aa704 570EXPORT_SYMBOL_GPL(disk_update_readahead);
c2e4cd57 571
7c958e32
MP
572/**
573 * blk_limits_io_min - set minimum request size for a device
574 * @limits: the queue limits
575 * @min: smallest I/O size in bytes
576 *
577 * Description:
578 * Some devices have an internal block size bigger than the reported
579 * hardware sector size. This function can be used to signal the
580 * smallest I/O the device can perform without incurring a performance
581 * penalty.
582 */
583void blk_limits_io_min(struct queue_limits *limits, unsigned int min)
584{
585 limits->io_min = min;
586
587 if (limits->io_min < limits->logical_block_size)
588 limits->io_min = limits->logical_block_size;
589
590 if (limits->io_min < limits->physical_block_size)
591 limits->io_min = limits->physical_block_size;
592}
593EXPORT_SYMBOL(blk_limits_io_min);
594
c72758f3
MP
595/**
596 * blk_queue_io_min - set minimum request size for the queue
597 * @q: the request queue for the device
8ebf9756 598 * @min: smallest I/O size in bytes
c72758f3
MP
599 *
600 * Description:
7e5f5fb0
MP
601 * Storage devices may report a granularity or preferred minimum I/O
602 * size which is the smallest request the device can perform without
603 * incurring a performance penalty. For disk drives this is often the
604 * physical block size. For RAID arrays it is often the stripe chunk
605 * size. A properly aligned multiple of minimum_io_size is the
606 * preferred request size for workloads where a high number of I/O
607 * operations is desired.
c72758f3
MP
608 */
609void blk_queue_io_min(struct request_queue *q, unsigned int min)
610{
7c958e32 611 blk_limits_io_min(&q->limits, min);
c72758f3
MP
612}
613EXPORT_SYMBOL(blk_queue_io_min);
614
3c5820c7
MP
615/**
616 * blk_limits_io_opt - set optimal request size for a device
617 * @limits: the queue limits
618 * @opt: smallest I/O size in bytes
619 *
620 * Description:
621 * Storage devices may report an optimal I/O size, which is the
622 * device's preferred unit for sustained I/O. This is rarely reported
623 * for disk drives. For RAID arrays it is usually the stripe width or
624 * the internal track size. A properly aligned multiple of
625 * optimal_io_size is the preferred request size for workloads where
626 * sustained throughput is desired.
627 */
628void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt)
629{
630 limits->io_opt = opt;
631}
632EXPORT_SYMBOL(blk_limits_io_opt);
633
c72758f3
MP
634/**
635 * blk_queue_io_opt - set optimal request size for the queue
636 * @q: the request queue for the device
8ebf9756 637 * @opt: optimal request size in bytes
c72758f3
MP
638 *
639 * Description:
7e5f5fb0
MP
640 * Storage devices may report an optimal I/O size, which is the
641 * device's preferred unit for sustained I/O. This is rarely reported
642 * for disk drives. For RAID arrays it is usually the stripe width or
643 * the internal track size. A properly aligned multiple of
644 * optimal_io_size is the preferred request size for workloads where
645 * sustained throughput is desired.
c72758f3
MP
646 */
647void blk_queue_io_opt(struct request_queue *q, unsigned int opt)
648{
3c5820c7 649 blk_limits_io_opt(&q->limits, opt);
d152c682 650 if (!q->disk)
edb0872f 651 return;
d152c682 652 q->disk->bdi->ra_pages =
c2e4cd57 653 max(queue_io_opt(q) * 2 / PAGE_SIZE, VM_READAHEAD_PAGES);
c72758f3
MP
654}
655EXPORT_SYMBOL(blk_queue_io_opt);
656
aa261f20 657static int queue_limit_alignment_offset(const struct queue_limits *lim,
89098b07
CH
658 sector_t sector)
659{
660 unsigned int granularity = max(lim->physical_block_size, lim->io_min);
661 unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
662 << SECTOR_SHIFT;
663
664 return (granularity + lim->alignment_offset - alignment) % granularity;
665}
666
aa261f20
BVA
667static unsigned int queue_limit_discard_alignment(
668 const struct queue_limits *lim, sector_t sector)
5c4b4a5c
CH
669{
670 unsigned int alignment, granularity, offset;
671
672 if (!lim->max_discard_sectors)
673 return 0;
674
675 /* Why are these in bytes, not sectors? */
676 alignment = lim->discard_alignment >> SECTOR_SHIFT;
677 granularity = lim->discard_granularity >> SECTOR_SHIFT;
678 if (!granularity)
679 return 0;
680
681 /* Offset of the partition start in 'granularity' sectors */
682 offset = sector_div(sector, granularity);
683
684 /* And why do we do this modulus *again* in blkdev_issue_discard()? */
685 offset = (granularity + alignment - offset) % granularity;
686
687 /* Turn it back into bytes, gaah */
688 return offset << SECTOR_SHIFT;
689}
690
97f433c3
MP
691static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs)
692{
693 sectors = round_down(sectors, lbs >> SECTOR_SHIFT);
694 if (sectors < PAGE_SIZE >> SECTOR_SHIFT)
695 sectors = PAGE_SIZE >> SECTOR_SHIFT;
696 return sectors;
697}
698
c72758f3
MP
699/**
700 * blk_stack_limits - adjust queue_limits for stacked devices
81744ee4
MP
701 * @t: the stacking driver limits (top device)
702 * @b: the underlying queue limits (bottom, component device)
e03a72e1 703 * @start: first data sector within component device
c72758f3
MP
704 *
705 * Description:
81744ee4
MP
706 * This function is used by stacking drivers like MD and DM to ensure
707 * that all component devices have compatible block sizes and
708 * alignments. The stacking driver must provide a queue_limits
709 * struct (top) and then iteratively call the stacking function for
710 * all component (bottom) devices. The stacking function will
711 * attempt to combine the values and ensure proper alignment.
712 *
713 * Returns 0 if the top and bottom queue_limits are compatible. The
714 * top device's block sizes and alignment offsets may be adjusted to
715 * ensure alignment with the bottom device. If no compatible sizes
716 * and alignments exist, -1 is returned and the resulting top
717 * queue_limits will have the misaligned flag set to indicate that
718 * the alignment_offset is undefined.
c72758f3
MP
719 */
720int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
e03a72e1 721 sector_t start)
c72758f3 722{
e03a72e1 723 unsigned int top, bottom, alignment, ret = 0;
86b37281 724
c72758f3
MP
725 t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
726 t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
ca369d51 727 t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
a6f0788e
CK
728 t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors,
729 b->max_write_zeroes_sectors);
0512a75b
KB
730 t->max_zone_append_sectors = min(t->max_zone_append_sectors,
731 b->max_zone_append_sectors);
9bb33f24 732 t->bounce = max(t->bounce, b->bounce);
c72758f3
MP
733
734 t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
735 b->seg_boundary_mask);
03100aad
KB
736 t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask,
737 b->virt_boundary_mask);
c72758f3 738
8a78362c 739 t->max_segments = min_not_zero(t->max_segments, b->max_segments);
1e739730
CH
740 t->max_discard_segments = min_not_zero(t->max_discard_segments,
741 b->max_discard_segments);
13f05c8d
MP
742 t->max_integrity_segments = min_not_zero(t->max_integrity_segments,
743 b->max_integrity_segments);
c72758f3
MP
744
745 t->max_segment_size = min_not_zero(t->max_segment_size,
746 b->max_segment_size);
747
fe0b393f
MP
748 t->misaligned |= b->misaligned;
749
e03a72e1 750 alignment = queue_limit_alignment_offset(b, start);
9504e086 751
81744ee4
MP
752 /* Bottom device has different alignment. Check that it is
753 * compatible with the current top alignment.
754 */
9504e086
MP
755 if (t->alignment_offset != alignment) {
756
757 top = max(t->physical_block_size, t->io_min)
758 + t->alignment_offset;
81744ee4 759 bottom = max(b->physical_block_size, b->io_min) + alignment;
9504e086 760
81744ee4 761 /* Verify that top and bottom intervals line up */
b8839b8c 762 if (max(top, bottom) % min(top, bottom)) {
9504e086 763 t->misaligned = 1;
fe0b393f
MP
764 ret = -1;
765 }
9504e086
MP
766 }
767
c72758f3
MP
768 t->logical_block_size = max(t->logical_block_size,
769 b->logical_block_size);
770
771 t->physical_block_size = max(t->physical_block_size,
772 b->physical_block_size);
773
774 t->io_min = max(t->io_min, b->io_min);
e9637415 775 t->io_opt = lcm_not_zero(t->io_opt, b->io_opt);
c964d62f 776 t->dma_alignment = max(t->dma_alignment, b->dma_alignment);
7e7986f9
MS
777
778 /* Set non-power-of-2 compatible chunk_sectors boundary */
779 if (b->chunk_sectors)
780 t->chunk_sectors = gcd(t->chunk_sectors, b->chunk_sectors);
9504e086 781
81744ee4 782 /* Physical block size a multiple of the logical block size? */
9504e086
MP
783 if (t->physical_block_size & (t->logical_block_size - 1)) {
784 t->physical_block_size = t->logical_block_size;
c72758f3 785 t->misaligned = 1;
fe0b393f 786 ret = -1;
86b37281
MP
787 }
788
81744ee4 789 /* Minimum I/O a multiple of the physical block size? */
9504e086
MP
790 if (t->io_min & (t->physical_block_size - 1)) {
791 t->io_min = t->physical_block_size;
792 t->misaligned = 1;
fe0b393f 793 ret = -1;
c72758f3
MP
794 }
795
81744ee4 796 /* Optimal I/O a multiple of the physical block size? */
9504e086
MP
797 if (t->io_opt & (t->physical_block_size - 1)) {
798 t->io_opt = 0;
799 t->misaligned = 1;
fe0b393f 800 ret = -1;
9504e086 801 }
c72758f3 802
22ada802
MS
803 /* chunk_sectors a multiple of the physical block size? */
804 if ((t->chunk_sectors << 9) & (t->physical_block_size - 1)) {
805 t->chunk_sectors = 0;
806 t->misaligned = 1;
807 ret = -1;
808 }
809
c78afc62
KO
810 t->raid_partial_stripes_expensive =
811 max(t->raid_partial_stripes_expensive,
812 b->raid_partial_stripes_expensive);
813
81744ee4 814 /* Find lowest common alignment_offset */
e9637415 815 t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment)
b8839b8c 816 % max(t->physical_block_size, t->io_min);
86b37281 817
81744ee4 818 /* Verify that new alignment_offset is on a logical block boundary */
fe0b393f 819 if (t->alignment_offset & (t->logical_block_size - 1)) {
c72758f3 820 t->misaligned = 1;
fe0b393f
MP
821 ret = -1;
822 }
c72758f3 823
97f433c3
MP
824 t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size);
825 t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size);
826 t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size);
827
9504e086
MP
828 /* Discard alignment and granularity */
829 if (b->discard_granularity) {
e03a72e1 830 alignment = queue_limit_discard_alignment(b, start);
9504e086
MP
831
832 if (t->discard_granularity != 0 &&
833 t->discard_alignment != alignment) {
834 top = t->discard_granularity + t->discard_alignment;
835 bottom = b->discard_granularity + alignment;
70dd5bf3 836
9504e086 837 /* Verify that top and bottom intervals line up */
8dd2cb7e 838 if ((max(top, bottom) % min(top, bottom)) != 0)
9504e086
MP
839 t->discard_misaligned = 1;
840 }
841
81744ee4
MP
842 t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
843 b->max_discard_sectors);
0034af03
JA
844 t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
845 b->max_hw_discard_sectors);
9504e086
MP
846 t->discard_granularity = max(t->discard_granularity,
847 b->discard_granularity);
e9637415 848 t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
8dd2cb7e 849 t->discard_granularity;
9504e086 850 }
44abff2c
CH
851 t->max_secure_erase_sectors = min_not_zero(t->max_secure_erase_sectors,
852 b->max_secure_erase_sectors);
a805a4fa
DLM
853 t->zone_write_granularity = max(t->zone_write_granularity,
854 b->zone_write_granularity);
3093a479 855 t->zoned = max(t->zoned, b->zoned);
fe0b393f 856 return ret;
c72758f3 857}
5d85d324 858EXPORT_SYMBOL(blk_stack_limits);
c72758f3
MP
859
860/**
861 * disk_stack_limits - adjust queue limits for stacked drivers
77634f33 862 * @disk: MD/DM gendisk (top)
c72758f3
MP
863 * @bdev: the underlying block device (bottom)
864 * @offset: offset to beginning of data within component device
865 *
866 * Description:
e03a72e1
MP
867 * Merges the limits for a top level gendisk and a bottom level
868 * block_device.
c72758f3
MP
869 */
870void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
871 sector_t offset)
872{
873 struct request_queue *t = disk->queue;
c72758f3 874
9efa82ef 875 if (blk_stack_limits(&t->limits, &bdev_get_queue(bdev)->limits,
453b8ab6
CH
876 get_start_sect(bdev) + (offset >> 9)) < 0)
877 pr_notice("%s: Warning: Device %pg is misaligned\n",
878 disk->disk_name, bdev);
e74d93e9 879
471aa704 880 disk_update_readahead(disk);
c72758f3
MP
881}
882EXPORT_SYMBOL(disk_stack_limits);
883
27f8221a
FT
884/**
885 * blk_queue_update_dma_pad - update pad mask
886 * @q: the request queue for the device
887 * @mask: pad mask
888 *
889 * Update dma pad mask.
890 *
891 * Appending pad buffer to a request modifies the last entry of a
892 * scatter list such that it includes the pad buffer.
893 **/
894void blk_queue_update_dma_pad(struct request_queue *q, unsigned int mask)
895{
896 if (mask > q->dma_pad_mask)
897 q->dma_pad_mask = mask;
898}
899EXPORT_SYMBOL(blk_queue_update_dma_pad);
900
86db1e29
JA
901/**
902 * blk_queue_segment_boundary - set boundary rules for segment merging
903 * @q: the request queue for the device
904 * @mask: the memory boundary mask
905 **/
906void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
907{
09cbfeaf
KS
908 if (mask < PAGE_SIZE - 1) {
909 mask = PAGE_SIZE - 1;
f19d1e3b 910 pr_info("%s: set to minimum %lx\n", __func__, mask);
86db1e29
JA
911 }
912
025146e1 913 q->limits.seg_boundary_mask = mask;
86db1e29 914}
86db1e29
JA
915EXPORT_SYMBOL(blk_queue_segment_boundary);
916
03100aad
KB
917/**
918 * blk_queue_virt_boundary - set boundary rules for bio merging
919 * @q: the request queue for the device
920 * @mask: the memory boundary mask
921 **/
922void blk_queue_virt_boundary(struct request_queue *q, unsigned long mask)
923{
924 q->limits.virt_boundary_mask = mask;
09324d32
CH
925
926 /*
927 * Devices that require a virtual boundary do not support scatter/gather
928 * I/O natively, but instead require a descriptor list entry for each
929 * page (which might not be idential to the Linux PAGE_SIZE). Because
930 * of that they are not limited by our notion of "segment size".
931 */
c6c84f78
CH
932 if (mask)
933 q->limits.max_segment_size = UINT_MAX;
03100aad
KB
934}
935EXPORT_SYMBOL(blk_queue_virt_boundary);
936
86db1e29
JA
937/**
938 * blk_queue_dma_alignment - set dma length and memory alignment
939 * @q: the request queue for the device
940 * @mask: alignment mask
941 *
942 * description:
710027a4 943 * set required memory and length alignment for direct dma transactions.
8feb4d20 944 * this is used when building direct io requests for the queue.
86db1e29
JA
945 *
946 **/
947void blk_queue_dma_alignment(struct request_queue *q, int mask)
948{
c964d62f 949 q->limits.dma_alignment = mask;
86db1e29 950}
86db1e29
JA
951EXPORT_SYMBOL(blk_queue_dma_alignment);
952
953/**
954 * blk_queue_update_dma_alignment - update dma length and memory alignment
955 * @q: the request queue for the device
956 * @mask: alignment mask
957 *
958 * description:
710027a4 959 * update required memory and length alignment for direct dma transactions.
86db1e29
JA
960 * If the requested alignment is larger than the current alignment, then
961 * the current queue alignment is updated to the new value, otherwise it
962 * is left alone. The design of this is to allow multiple objects
963 * (driver, device, transport etc) to set their respective
964 * alignments without having them interfere.
965 *
966 **/
967void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
968{
969 BUG_ON(mask > PAGE_SIZE);
970
c964d62f
KB
971 if (mask > q->limits.dma_alignment)
972 q->limits.dma_alignment = mask;
86db1e29 973}
86db1e29
JA
974EXPORT_SYMBOL(blk_queue_update_dma_alignment);
975
d278d4a8
JA
976/**
977 * blk_set_queue_depth - tell the block layer about the device queue depth
978 * @q: the request queue for the device
979 * @depth: queue depth
980 *
981 */
982void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
983{
984 q->queue_depth = depth;
9677a3e0 985 rq_qos_queue_depth_changed(q);
d278d4a8
JA
986}
987EXPORT_SYMBOL(blk_set_queue_depth);
988
93e9d8e8
JA
989/**
990 * blk_queue_write_cache - configure queue's write cache
991 * @q: the request queue for the device
992 * @wc: write back cache on or off
993 * @fua: device supports FUA writes, if true
994 *
995 * Tell the block layer about the write cache of @q.
996 */
997void blk_queue_write_cache(struct request_queue *q, bool wc, bool fua)
998{
43c9835b
CH
999 if (wc) {
1000 blk_queue_flag_set(QUEUE_FLAG_HW_WC, q);
57d74df9 1001 blk_queue_flag_set(QUEUE_FLAG_WC, q);
43c9835b
CH
1002 } else {
1003 blk_queue_flag_clear(QUEUE_FLAG_HW_WC, q);
57d74df9 1004 blk_queue_flag_clear(QUEUE_FLAG_WC, q);
43c9835b 1005 }
c888a8f9 1006 if (fua)
57d74df9 1007 blk_queue_flag_set(QUEUE_FLAG_FUA, q);
c888a8f9 1008 else
57d74df9 1009 blk_queue_flag_clear(QUEUE_FLAG_FUA, q);
93e9d8e8
JA
1010}
1011EXPORT_SYMBOL_GPL(blk_queue_write_cache);
1012
68c43f13
DLM
1013/**
1014 * blk_queue_required_elevator_features - Set a queue required elevator features
1015 * @q: the request queue for the target device
1016 * @features: Required elevator features OR'ed together
1017 *
1018 * Tell the block layer that for the device controlled through @q, only the
1019 * only elevators that can be used are those that implement at least the set of
1020 * features specified by @features.
1021 */
1022void blk_queue_required_elevator_features(struct request_queue *q,
1023 unsigned int features)
1024{
1025 q->required_elevator_features = features;
1026}
1027EXPORT_SYMBOL_GPL(blk_queue_required_elevator_features);
1028
45147fb5
YS
1029/**
1030 * blk_queue_can_use_dma_map_merging - configure queue for merging segments.
1031 * @q: the request queue for the device
1032 * @dev: the device pointer for dma
1033 *
1034 * Tell the block layer about merging the segments by dma map of @q.
1035 */
1036bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
1037 struct device *dev)
1038{
1039 unsigned long boundary = dma_get_merge_boundary(dev);
1040
1041 if (!boundary)
1042 return false;
1043
1044 /* No need to update max_segment_size. see blk_queue_virt_boundary() */
1045 blk_queue_virt_boundary(q, boundary);
1046
1047 return true;
1048}
1049EXPORT_SYMBOL_GPL(blk_queue_can_use_dma_map_merging);
1050
27ba3e8f 1051/**
d73e93b4
CH
1052 * disk_set_zoned - inidicate a zoned device
1053 * @disk: gendisk to configure
27ba3e8f 1054 */
d73e93b4 1055void disk_set_zoned(struct gendisk *disk)
27ba3e8f 1056{
a805a4fa
DLM
1057 struct request_queue *q = disk->queue;
1058
d73e93b4
CH
1059 WARN_ON_ONCE(!IS_ENABLED(CONFIG_BLK_DEV_ZONED));
1060
1061 /*
1062 * Set the zone write granularity to the device logical block
1063 * size by default. The driver can change this value if needed.
1064 */
1065 q->limits.zoned = true;
1066 blk_queue_zone_write_granularity(q, queue_logical_block_size(q));
27ba3e8f 1067}
6b2bd274 1068EXPORT_SYMBOL_GPL(disk_set_zoned);
89098b07
CH
1069
1070int bdev_alignment_offset(struct block_device *bdev)
1071{
1072 struct request_queue *q = bdev_get_queue(bdev);
1073
1074 if (q->limits.misaligned)
1075 return -1;
1076 if (bdev_is_partition(bdev))
1077 return queue_limit_alignment_offset(&q->limits,
1078 bdev->bd_start_sect);
1079 return q->limits.alignment_offset;
1080}
1081EXPORT_SYMBOL_GPL(bdev_alignment_offset);
5c4b4a5c
CH
1082
1083unsigned int bdev_discard_alignment(struct block_device *bdev)
1084{
1085 struct request_queue *q = bdev_get_queue(bdev);
1086
1087 if (bdev_is_partition(bdev))
1088 return queue_limit_discard_alignment(&q->limits,
1089 bdev->bd_start_sect);
1090 return q->limits.discard_alignment;
1091}
1092EXPORT_SYMBOL_GPL(bdev_discard_alignment);