xref: /openbmc/linux/block/blk-settings.c (revision 83c4a4ee)
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 }
64 EXPORT_SYMBOL(blk_set_default_limits);
65 
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  */
74 void blk_set_stacking_limits(struct queue_limits *lim)
75 {
76 	blk_set_default_limits(lim);
77 
78 	/* Inherit limits from component devices */
79 	lim->max_segments = USHRT_MAX;
80 	lim->max_discard_segments = USHRT_MAX;
81 	lim->max_hw_sectors = UINT_MAX;
82 	lim->max_segment_size = UINT_MAX;
83 	lim->max_sectors = UINT_MAX;
84 	lim->max_dev_sectors = UINT_MAX;
85 	lim->max_write_same_sectors = UINT_MAX;
86 	lim->max_write_zeroes_sectors = UINT_MAX;
87 	lim->max_zone_append_sectors = UINT_MAX;
88 }
89 EXPORT_SYMBOL(blk_set_stacking_limits);
90 
91 /**
92  * blk_queue_bounce_limit - set bounce buffer limit for queue
93  * @q: the request queue for the device
94  * @max_addr: the maximum address the device can handle
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
100  *    buffers for doing I/O to pages residing above @max_addr.
101  **/
102 void blk_queue_bounce_limit(struct request_queue *q, u64 max_addr)
103 {
104 	unsigned long b_pfn = max_addr >> PAGE_SHIFT;
105 	int dma = 0;
106 
107 	q->bounce_gfp = GFP_NOIO;
108 #if BITS_PER_LONG == 64
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))
115 		dma = 1;
116 	q->limits.bounce_pfn = max(max_low_pfn, b_pfn);
117 #else
118 	if (b_pfn < blk_max_low_pfn)
119 		dma = 1;
120 	q->limits.bounce_pfn = b_pfn;
121 #endif
122 	if (dma) {
123 		init_emergency_isa_pool();
124 		q->bounce_gfp = GFP_NOIO | GFP_DMA;
125 		q->limits.bounce_pfn = b_pfn;
126 	}
127 }
128 EXPORT_SYMBOL(blk_queue_bounce_limit);
129 
130 /**
131  * blk_queue_max_hw_sectors - set max sectors for a request for this queue
132  * @q:  the request queue for the device
133  * @max_hw_sectors:  max hardware sectors in the usual 512b unit
134  *
135  * Description:
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
138  *    the device driver based upon the capabilities of the I/O
139  *    controller.
140  *
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  *
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.
148  **/
149 void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
150 {
151 	struct queue_limits *limits = &q->limits;
152 	unsigned int max_sectors;
153 
154 	if ((max_hw_sectors << 9) < PAGE_SIZE) {
155 		max_hw_sectors = 1 << (PAGE_SHIFT - 9);
156 		printk(KERN_INFO "%s: set to minimum %d\n",
157 		       __func__, max_hw_sectors);
158 	}
159 
160 	max_hw_sectors = round_down(max_hw_sectors,
161 				    limits->logical_block_size >> SECTOR_SHIFT);
162 	limits->max_hw_sectors = max_hw_sectors;
163 
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);
166 	max_sectors = round_down(max_sectors,
167 				 limits->logical_block_size >> SECTOR_SHIFT);
168 	limits->max_sectors = max_sectors;
169 
170 	q->backing_dev_info->io_pages = max_sectors >> (PAGE_SHIFT - 9);
171 }
172 EXPORT_SYMBOL(blk_queue_max_hw_sectors);
173 
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
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.
185  **/
186 void blk_queue_chunk_sectors(struct request_queue *q, unsigned int chunk_sectors)
187 {
188 	q->limits.chunk_sectors = chunk_sectors;
189 }
190 EXPORT_SYMBOL(blk_queue_chunk_sectors);
191 
192 /**
193  * blk_queue_max_discard_sectors - set max sectors for a single discard
194  * @q:  the request queue for the device
195  * @max_discard_sectors: maximum number of sectors to discard
196  **/
197 void blk_queue_max_discard_sectors(struct request_queue *q,
198 		unsigned int max_discard_sectors)
199 {
200 	q->limits.max_hw_discard_sectors = max_discard_sectors;
201 	q->limits.max_discard_sectors = max_discard_sectors;
202 }
203 EXPORT_SYMBOL(blk_queue_max_discard_sectors);
204 
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  **/
210 void 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 }
215 EXPORT_SYMBOL(blk_queue_max_write_same_sectors);
216 
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  **/
223 void 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 }
228 EXPORT_SYMBOL(blk_queue_max_write_zeroes_sectors);
229 
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  **/
235 void 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 }
255 EXPORT_SYMBOL_GPL(blk_queue_max_zone_append_sectors);
256 
257 /**
258  * blk_queue_max_segments - set max hw segments for a request for this queue
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
264  *    hw data segments in a request.
265  **/
266 void blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
267 {
268 	if (!max_segments) {
269 		max_segments = 1;
270 		printk(KERN_INFO "%s: set to minimum %d\n",
271 		       __func__, max_segments);
272 	}
273 
274 	q->limits.max_segments = max_segments;
275 }
276 EXPORT_SYMBOL(blk_queue_max_segments);
277 
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  **/
287 void blk_queue_max_discard_segments(struct request_queue *q,
288 		unsigned short max_segments)
289 {
290 	q->limits.max_discard_segments = max_segments;
291 }
292 EXPORT_SYMBOL_GPL(blk_queue_max_discard_segments);
293 
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  **/
303 void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
304 {
305 	if (max_size < PAGE_SIZE) {
306 		max_size = PAGE_SIZE;
307 		printk(KERN_INFO "%s: set to minimum %d\n",
308 		       __func__, max_size);
309 	}
310 
311 	/* see blk_queue_virt_boundary() for the explanation */
312 	WARN_ON_ONCE(q->limits.virt_boundary_mask);
313 
314 	q->limits.max_segment_size = max_size;
315 }
316 EXPORT_SYMBOL(blk_queue_max_segment_size);
317 
318 /**
319  * blk_queue_logical_block_size - set logical block size for the queue
320  * @q:  the request queue for the device
321  * @size:  the logical block size, in bytes
322  *
323  * Description:
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.
327  **/
328 void blk_queue_logical_block_size(struct request_queue *q, unsigned int size)
329 {
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;
336 
337 	if (limits->io_min < limits->physical_block_size)
338 		limits->io_min = limits->physical_block_size;
339 
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);
344 }
345 EXPORT_SYMBOL(blk_queue_logical_block_size);
346 
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  */
357 void blk_queue_physical_block_size(struct request_queue *q, unsigned int size)
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 }
367 EXPORT_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
372  * @offset: alignment offset in bytes
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  */
380 void 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 }
386 EXPORT_SYMBOL(blk_queue_alignment_offset);
387 
388 void 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 }
399 EXPORT_SYMBOL_GPL(blk_queue_update_readahead);
400 
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  */
412 void 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 }
422 EXPORT_SYMBOL(blk_limits_io_min);
423 
424 /**
425  * blk_queue_io_min - set minimum request size for the queue
426  * @q:	the request queue for the device
427  * @min:  smallest I/O size in bytes
428  *
429  * Description:
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.
437  */
438 void blk_queue_io_min(struct request_queue *q, unsigned int min)
439 {
440 	blk_limits_io_min(&q->limits, min);
441 }
442 EXPORT_SYMBOL(blk_queue_io_min);
443 
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  */
457 void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt)
458 {
459 	limits->io_opt = opt;
460 }
461 EXPORT_SYMBOL(blk_limits_io_opt);
462 
463 /**
464  * blk_queue_io_opt - set optimal request size for the queue
465  * @q:	the request queue for the device
466  * @opt:  optimal request size in bytes
467  *
468  * Description:
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.
475  */
476 void blk_queue_io_opt(struct request_queue *q, unsigned int opt)
477 {
478 	blk_limits_io_opt(&q->limits, opt);
479 	q->backing_dev_info->ra_pages =
480 		max(queue_io_opt(q) * 2 / PAGE_SIZE, VM_READAHEAD_PAGES);
481 }
482 EXPORT_SYMBOL(blk_queue_io_opt);
483 
484 /**
485  * blk_stack_limits - adjust queue_limits for stacked devices
486  * @t:	the stacking driver limits (top device)
487  * @b:  the underlying queue limits (bottom, component device)
488  * @start:  first data sector within component device
489  *
490  * Description:
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.
504  */
505 int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
506 		     sector_t start)
507 {
508 	unsigned int top, bottom, alignment, ret = 0;
509 
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);
512 	t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
513 	t->max_write_same_sectors = min(t->max_write_same_sectors,
514 					b->max_write_same_sectors);
515 	t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors,
516 					b->max_write_zeroes_sectors);
517 	t->max_zone_append_sectors = min(t->max_zone_append_sectors,
518 					b->max_zone_append_sectors);
519 	t->bounce_pfn = min_not_zero(t->bounce_pfn, b->bounce_pfn);
520 
521 	t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
522 					    b->seg_boundary_mask);
523 	t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask,
524 					    b->virt_boundary_mask);
525 
526 	t->max_segments = min_not_zero(t->max_segments, b->max_segments);
527 	t->max_discard_segments = min_not_zero(t->max_discard_segments,
528 					       b->max_discard_segments);
529 	t->max_integrity_segments = min_not_zero(t->max_integrity_segments,
530 						 b->max_integrity_segments);
531 
532 	t->max_segment_size = min_not_zero(t->max_segment_size,
533 					   b->max_segment_size);
534 
535 	t->misaligned |= b->misaligned;
536 
537 	alignment = queue_limit_alignment_offset(b, start);
538 
539 	/* Bottom device has different alignment.  Check that it is
540 	 * compatible with the current top alignment.
541 	 */
542 	if (t->alignment_offset != alignment) {
543 
544 		top = max(t->physical_block_size, t->io_min)
545 			+ t->alignment_offset;
546 		bottom = max(b->physical_block_size, b->io_min) + alignment;
547 
548 		/* Verify that top and bottom intervals line up */
549 		if (max(top, bottom) % min(top, bottom)) {
550 			t->misaligned = 1;
551 			ret = -1;
552 		}
553 	}
554 
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);
562 	t->io_opt = lcm_not_zero(t->io_opt, b->io_opt);
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);
567 
568 	/* Physical block size a multiple of the logical block size? */
569 	if (t->physical_block_size & (t->logical_block_size - 1)) {
570 		t->physical_block_size = t->logical_block_size;
571 		t->misaligned = 1;
572 		ret = -1;
573 	}
574 
575 	/* Minimum I/O a multiple of the physical block size? */
576 	if (t->io_min & (t->physical_block_size - 1)) {
577 		t->io_min = t->physical_block_size;
578 		t->misaligned = 1;
579 		ret = -1;
580 	}
581 
582 	/* Optimal I/O a multiple of the physical block size? */
583 	if (t->io_opt & (t->physical_block_size - 1)) {
584 		t->io_opt = 0;
585 		t->misaligned = 1;
586 		ret = -1;
587 	}
588 
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 
596 	t->raid_partial_stripes_expensive =
597 		max(t->raid_partial_stripes_expensive,
598 		    b->raid_partial_stripes_expensive);
599 
600 	/* Find lowest common alignment_offset */
601 	t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment)
602 		% max(t->physical_block_size, t->io_min);
603 
604 	/* Verify that new alignment_offset is on a logical block boundary */
605 	if (t->alignment_offset & (t->logical_block_size - 1)) {
606 		t->misaligned = 1;
607 		ret = -1;
608 	}
609 
610 	/* Discard alignment and granularity */
611 	if (b->discard_granularity) {
612 		alignment = queue_limit_discard_alignment(b, start);
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;
618 
619 			/* Verify that top and bottom intervals line up */
620 			if ((max(top, bottom) % min(top, bottom)) != 0)
621 				t->discard_misaligned = 1;
622 		}
623 
624 		t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
625 						      b->max_discard_sectors);
626 		t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
627 							 b->max_hw_discard_sectors);
628 		t->discard_granularity = max(t->discard_granularity,
629 					     b->discard_granularity);
630 		t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
631 			t->discard_granularity;
632 	}
633 
634 	t->zoned = max(t->zoned, b->zoned);
635 	return ret;
636 }
637 EXPORT_SYMBOL(blk_stack_limits);
638 
639 /**
640  * disk_stack_limits - adjust queue limits for stacked drivers
641  * @disk:  MD/DM gendisk (top)
642  * @bdev:  the underlying block device (bottom)
643  * @offset:  offset to beginning of data within component device
644  *
645  * Description:
646  *    Merges the limits for a top level gendisk and a bottom level
647  *    block_device.
648  */
649 void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
650 		       sector_t offset)
651 {
652 	struct request_queue *t = disk->queue;
653 
654 	if (blk_stack_limits(&t->limits, &bdev_get_queue(bdev)->limits,
655 			get_start_sect(bdev) + (offset >> 9)) < 0) {
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 	}
664 
665 	blk_queue_update_readahead(disk->queue);
666 }
667 EXPORT_SYMBOL(disk_stack_limits);
668 
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  **/
679 void 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 }
684 EXPORT_SYMBOL(blk_queue_update_dma_pad);
685 
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  **/
691 void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
692 {
693 	if (mask < PAGE_SIZE - 1) {
694 		mask = PAGE_SIZE - 1;
695 		printk(KERN_INFO "%s: set to minimum %lx\n",
696 		       __func__, mask);
697 	}
698 
699 	q->limits.seg_boundary_mask = mask;
700 }
701 EXPORT_SYMBOL(blk_queue_segment_boundary);
702 
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  **/
708 void blk_queue_virt_boundary(struct request_queue *q, unsigned long mask)
709 {
710 	q->limits.virt_boundary_mask = mask;
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 	 */
718 	if (mask)
719 		q->limits.max_segment_size = UINT_MAX;
720 }
721 EXPORT_SYMBOL(blk_queue_virt_boundary);
722 
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:
729  *    set required memory and length alignment for direct dma transactions.
730  *    this is used when building direct io requests for the queue.
731  *
732  **/
733 void blk_queue_dma_alignment(struct request_queue *q, int mask)
734 {
735 	q->dma_alignment = mask;
736 }
737 EXPORT_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:
745  *    update required memory and length alignment for direct dma transactions.
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  **/
753 void 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 }
760 EXPORT_SYMBOL(blk_queue_update_dma_alignment);
761 
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  */
768 void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
769 {
770 	q->queue_depth = depth;
771 	rq_qos_queue_depth_changed(q);
772 }
773 EXPORT_SYMBOL(blk_set_queue_depth);
774 
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  */
783 void blk_queue_write_cache(struct request_queue *q, bool wc, bool fua)
784 {
785 	if (wc)
786 		blk_queue_flag_set(QUEUE_FLAG_WC, q);
787 	else
788 		blk_queue_flag_clear(QUEUE_FLAG_WC, q);
789 	if (fua)
790 		blk_queue_flag_set(QUEUE_FLAG_FUA, q);
791 	else
792 		blk_queue_flag_clear(QUEUE_FLAG_FUA, q);
793 
794 	wbt_set_write_cache(q, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
795 }
796 EXPORT_SYMBOL_GPL(blk_queue_write_cache);
797 
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  */
807 void blk_queue_required_elevator_features(struct request_queue *q,
808 					  unsigned int features)
809 {
810 	q->required_elevator_features = features;
811 }
812 EXPORT_SYMBOL_GPL(blk_queue_required_elevator_features);
813 
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  */
821 bool 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 }
834 EXPORT_SYMBOL_GPL(blk_queue_can_use_dma_map_merging);
835 
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  */
848 void 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) ||
868 		    disk_has_partitions(disk))
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 }
880 EXPORT_SYMBOL_GPL(blk_queue_set_zoned);
881 
882 static int __init blk_settings_init(void)
883 {
884 	blk_max_low_pfn = max_low_pfn - 1;
885 	blk_max_pfn = max_pfn - 1;
886 	return 0;
887 }
888 subsys_initcall(blk_settings_init);
889