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