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