blob: 9c009090c4b5bf58168564cf62e8d7b08011d08e [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Jens Axboe86db1e22008-01-29 14:53:40 +01002/*
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>
Martin K. Petersen70dd5bf2009-07-31 11:49:12 -040010#include <linux/gcd.h>
Martin K. Petersen2cda2722010-03-15 12:46:51 +010011#include <linux/lcm.h>
Randy Dunlapad5ebd22009-11-11 13:47:45 +010012#include <linux/jiffies.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/gfp.h>
Yoshihiro Shimoda45147fb2019-08-28 21:35:42 +090014#include <linux/dma-mapping.h>
Jens Axboe86db1e22008-01-29 14:53:40 +010015
16#include "blk.h"
Jens Axboe87760e52016-11-09 12:38:14 -070017#include "blk-wbt.h"
Jens Axboe86db1e22008-01-29 14:53:40 +010018
Jens Axboe242f9dc2008-09-14 05:55:09 -070019void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout)
20{
21 q->rq_timeout = timeout;
22}
23EXPORT_SYMBOL_GPL(blk_queue_rq_timeout);
24
Jens Axboe86db1e22008-01-29 14:53:40 +010025/**
Martin K. Petersene475bba2009-06-16 08:23:52 +020026 * blk_set_default_limits - reset limits to default values
Randy Dunlapf740f5ca2009-06-19 09:18:32 +020027 * @lim: the queue_limits structure to reset
Martin K. Petersene475bba2009-06-16 08:23:52 +020028 *
29 * Description:
Martin K. Petersenb1bd0552012-01-11 16:27:11 +010030 * Returns a queue_limit struct to its default state.
Martin K. Petersene475bba2009-06-16 08:23:52 +020031 */
32void blk_set_default_limits(struct queue_limits *lim)
33{
Martin K. Petersen8a783622010-02-26 00:20:39 -050034 lim->max_segments = BLK_MAX_SEGMENTS;
Christoph Hellwig1e739732017-02-08 14:46:49 +010035 lim->max_discard_segments = 1;
Martin K. Petersen13f05c82010-09-10 20:50:10 +020036 lim->max_integrity_segments = 0;
Martin K. Petersene475bba2009-06-16 08:23:52 +020037 lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
Keith Busch03100aa2015-08-19 14:24:05 -070038 lim->virt_boundary_mask = 0;
Martin K. Peterseneb28d312010-02-26 00:20:37 -050039 lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
Keith Busch5f009d32016-02-10 16:52:47 -070040 lim->max_sectors = lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS;
41 lim->max_dev_sectors = 0;
Jens Axboe762380a2014-06-05 13:38:39 -060042 lim->chunk_sectors = 0;
Martin K. Petersen4363ac72012-09-18 12:19:27 -040043 lim->max_write_same_sectors = 0;
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -080044 lim->max_write_zeroes_sectors = 0;
Keith Busch0512a752020-05-12 17:55:47 +090045 lim->max_zone_append_sectors = 0;
Martin K. Petersen86b37282009-11-10 11:50:21 +010046 lim->max_discard_sectors = 0;
Jens Axboe0034af02015-07-16 09:14:26 -060047 lim->max_hw_discard_sectors = 0;
Martin K. Petersen86b37282009-11-10 11:50:21 +010048 lim->discard_granularity = 0;
49 lim->discard_alignment = 0;
50 lim->discard_misaligned = 0;
Martin K. Petersene475bba2009-06-16 08:23:52 +020051 lim->logical_block_size = lim->physical_block_size = lim->io_min = 512;
Christoph Hellwig9bb33f22021-03-31 09:30:00 +020052 lim->bounce = BLK_BOUNCE_NONE;
Martin K. Petersene475bba2009-06-16 08:23:52 +020053 lim->alignment_offset = 0;
54 lim->io_opt = 0;
55 lim->misaligned = 0;
Damien Le Moal797476b2016-10-18 15:40:29 +090056 lim->zoned = BLK_ZONED_NONE;
Damien Le Moala805a4f2021-01-28 13:47:30 +090057 lim->zone_write_granularity = 0;
Martin K. Petersene475bba2009-06-16 08:23:52 +020058}
59EXPORT_SYMBOL(blk_set_default_limits);
60
61/**
Martin K. Petersenb1bd0552012-01-11 16:27:11 +010062 * blk_set_stacking_limits - set default limits for stacking devices
63 * @lim: the queue_limits structure to reset
64 *
65 * Description:
66 * Returns a queue_limit struct to its default state. Should be used
67 * by stacking drivers like DM that have no internal limits.
68 */
69void blk_set_stacking_limits(struct queue_limits *lim)
70{
71 blk_set_default_limits(lim);
72
73 /* Inherit limits from component devices */
Martin K. Petersenb1bd0552012-01-11 16:27:11 +010074 lim->max_segments = USHRT_MAX;
Mike Snitzer42c9cdf2018-07-20 14:57:38 -040075 lim->max_discard_segments = USHRT_MAX;
Martin K. Petersenb1bd0552012-01-11 16:27:11 +010076 lim->max_hw_sectors = UINT_MAX;
Mike Snitzerd82ae522013-10-18 09:44:49 -060077 lim->max_segment_size = UINT_MAX;
Mike Snitzerfe86cdc2012-08-01 10:44:28 +020078 lim->max_sectors = UINT_MAX;
Martin K. Petersenca369d52015-11-13 16:46:48 -050079 lim->max_dev_sectors = UINT_MAX;
Martin K. Petersen4363ac72012-09-18 12:19:27 -040080 lim->max_write_same_sectors = UINT_MAX;
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -080081 lim->max_write_zeroes_sectors = UINT_MAX;
Keith Busch0512a752020-05-12 17:55:47 +090082 lim->max_zone_append_sectors = UINT_MAX;
Martin K. Petersenb1bd0552012-01-11 16:27:11 +010083}
84EXPORT_SYMBOL(blk_set_stacking_limits);
85
86/**
Jens Axboe86db1e22008-01-29 14:53:40 +010087 * blk_queue_bounce_limit - set bounce buffer limit for queue
Tejun Heocd0aca22009-04-15 22:10:25 +090088 * @q: the request queue for the device
Christoph Hellwig9bb33f22021-03-31 09:30:00 +020089 * @bounce: bounce limit to enforce
Jens Axboe86db1e22008-01-29 14:53:40 +010090 *
91 * Description:
Christoph Hellwig9bb33f22021-03-31 09:30:00 +020092 * Force bouncing for ISA DMA ranges or highmem.
93 *
94 * DEPRECATED, don't use in new code.
Jens Axboe86db1e22008-01-29 14:53:40 +010095 **/
Christoph Hellwig9bb33f22021-03-31 09:30:00 +020096void blk_queue_bounce_limit(struct request_queue *q, enum blk_bounce bounce)
Jens Axboe86db1e22008-01-29 14:53:40 +010097{
Christoph Hellwig9bb33f22021-03-31 09:30:00 +020098 q->limits.bounce = bounce;
Jens Axboe86db1e22008-01-29 14:53:40 +010099}
Jens Axboe86db1e22008-01-29 14:53:40 +0100100EXPORT_SYMBOL(blk_queue_bounce_limit);
101
102/**
Martin K. Petersenca369d52015-11-13 16:46:48 -0500103 * blk_queue_max_hw_sectors - set max sectors for a request for this queue
104 * @q: the request queue for the device
Martin K. Petersen2800aac2010-02-26 00:20:35 -0500105 * @max_hw_sectors: max hardware sectors in the usual 512b unit
Jens Axboe86db1e22008-01-29 14:53:40 +0100106 *
107 * Description:
Martin K. Petersen2800aac2010-02-26 00:20:35 -0500108 * Enables a low level driver to set a hard upper limit,
109 * max_hw_sectors, on the size of requests. max_hw_sectors is set by
Martin K. Petersen4f258a42015-06-23 12:13:59 -0400110 * the device driver based upon the capabilities of the I/O
111 * controller.
Martin K. Petersen2800aac2010-02-26 00:20:35 -0500112 *
Martin K. Petersenca369d52015-11-13 16:46:48 -0500113 * max_dev_sectors is a hard limit imposed by the storage device for
114 * READ/WRITE requests. It is set by the disk driver.
115 *
Martin K. Petersen2800aac2010-02-26 00:20:35 -0500116 * max_sectors is a soft limit imposed by the block layer for
117 * filesystem type requests. This value can be overridden on a
118 * per-device basis in /sys/block/<device>/queue/max_sectors_kb.
119 * The soft limit can not exceed max_hw_sectors.
Jens Axboe86db1e22008-01-29 14:53:40 +0100120 **/
Martin K. Petersenca369d52015-11-13 16:46:48 -0500121void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
Jens Axboe86db1e22008-01-29 14:53:40 +0100122{
Martin K. Petersenca369d52015-11-13 16:46:48 -0500123 struct queue_limits *limits = &q->limits;
124 unsigned int max_sectors;
125
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300126 if ((max_hw_sectors << 9) < PAGE_SIZE) {
127 max_hw_sectors = 1 << (PAGE_SHIFT - 9);
Harvey Harrison24c03d42008-05-01 04:35:17 -0700128 printk(KERN_INFO "%s: set to minimum %d\n",
Martin K. Petersen2800aac2010-02-26 00:20:35 -0500129 __func__, max_hw_sectors);
Jens Axboe86db1e22008-01-29 14:53:40 +0100130 }
131
Damien Le Moal817046e2020-11-20 10:55:13 +0900132 max_hw_sectors = round_down(max_hw_sectors,
133 limits->logical_block_size >> SECTOR_SHIFT);
Jeff Moyer30e2bc02015-08-13 14:57:56 -0400134 limits->max_hw_sectors = max_hw_sectors;
Damien Le Moal817046e2020-11-20 10:55:13 +0900135
Martin K. Petersenca369d52015-11-13 16:46:48 -0500136 max_sectors = min_not_zero(max_hw_sectors, limits->max_dev_sectors);
137 max_sectors = min_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
Damien Le Moal817046e2020-11-20 10:55:13 +0900138 max_sectors = round_down(max_sectors,
139 limits->logical_block_size >> SECTOR_SHIFT);
Martin K. Petersenca369d52015-11-13 16:46:48 -0500140 limits->max_sectors = max_sectors;
Damien Le Moal817046e2020-11-20 10:55:13 +0900141
Jan Karadc3b17c2017-02-02 15:56:50 +0100142 q->backing_dev_info->io_pages = max_sectors >> (PAGE_SHIFT - 9);
Jens Axboe86db1e22008-01-29 14:53:40 +0100143}
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500144EXPORT_SYMBOL(blk_queue_max_hw_sectors);
Jens Axboe86db1e22008-01-29 14:53:40 +0100145
146/**
Jens Axboe762380a2014-06-05 13:38:39 -0600147 * blk_queue_chunk_sectors - set size of the chunk for this queue
148 * @q: the request queue for the device
149 * @chunk_sectors: chunk sectors in the usual 512b unit
150 *
151 * Description:
152 * If a driver doesn't want IOs to cross a given chunk size, it can set
Mike Snitzer07d098e2020-09-21 22:32:49 -0400153 * this limit and prevent merging across chunks. Note that the block layer
154 * must accept a page worth of data at any offset. So if the crossing of
155 * chunks is a hard limitation in the driver, it must still be prepared
156 * to split single page bios.
Jens Axboe762380a2014-06-05 13:38:39 -0600157 **/
158void blk_queue_chunk_sectors(struct request_queue *q, unsigned int chunk_sectors)
159{
Jens Axboe762380a2014-06-05 13:38:39 -0600160 q->limits.chunk_sectors = chunk_sectors;
161}
162EXPORT_SYMBOL(blk_queue_chunk_sectors);
163
164/**
Christoph Hellwig67efc922009-09-30 13:54:20 +0200165 * blk_queue_max_discard_sectors - set max sectors for a single discard
166 * @q: the request queue for the device
Randy Dunlapc7ebf062009-10-12 08:20:47 +0200167 * @max_discard_sectors: maximum number of sectors to discard
Christoph Hellwig67efc922009-09-30 13:54:20 +0200168 **/
169void blk_queue_max_discard_sectors(struct request_queue *q,
170 unsigned int max_discard_sectors)
171{
Jens Axboe0034af02015-07-16 09:14:26 -0600172 q->limits.max_hw_discard_sectors = max_discard_sectors;
Christoph Hellwig67efc922009-09-30 13:54:20 +0200173 q->limits.max_discard_sectors = max_discard_sectors;
174}
175EXPORT_SYMBOL(blk_queue_max_discard_sectors);
176
177/**
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400178 * blk_queue_max_write_same_sectors - set max sectors for a single write same
179 * @q: the request queue for the device
180 * @max_write_same_sectors: maximum number of sectors to write per command
181 **/
182void blk_queue_max_write_same_sectors(struct request_queue *q,
183 unsigned int max_write_same_sectors)
184{
185 q->limits.max_write_same_sectors = max_write_same_sectors;
186}
187EXPORT_SYMBOL(blk_queue_max_write_same_sectors);
188
189/**
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800190 * blk_queue_max_write_zeroes_sectors - set max sectors for a single
191 * write zeroes
192 * @q: the request queue for the device
193 * @max_write_zeroes_sectors: maximum number of sectors to write per command
194 **/
195void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
196 unsigned int max_write_zeroes_sectors)
197{
198 q->limits.max_write_zeroes_sectors = max_write_zeroes_sectors;
199}
200EXPORT_SYMBOL(blk_queue_max_write_zeroes_sectors);
201
202/**
Keith Busch0512a752020-05-12 17:55:47 +0900203 * blk_queue_max_zone_append_sectors - set max sectors for a single zone append
204 * @q: the request queue for the device
205 * @max_zone_append_sectors: maximum number of sectors to write per command
206 **/
207void blk_queue_max_zone_append_sectors(struct request_queue *q,
208 unsigned int max_zone_append_sectors)
209{
210 unsigned int max_sectors;
211
212 if (WARN_ON(!blk_queue_is_zoned(q)))
213 return;
214
215 max_sectors = min(q->limits.max_hw_sectors, max_zone_append_sectors);
216 max_sectors = min(q->limits.chunk_sectors, max_sectors);
217
218 /*
219 * Signal eventual driver bugs resulting in the max_zone_append sectors limit
220 * being 0 due to a 0 argument, the chunk_sectors limit (zone size) not set,
221 * or the max_hw_sectors limit not set.
222 */
223 WARN_ON(!max_sectors);
224
225 q->limits.max_zone_append_sectors = max_sectors;
226}
227EXPORT_SYMBOL_GPL(blk_queue_max_zone_append_sectors);
228
229/**
Martin K. Petersen8a783622010-02-26 00:20:39 -0500230 * blk_queue_max_segments - set max hw segments for a request for this queue
Jens Axboe86db1e22008-01-29 14:53:40 +0100231 * @q: the request queue for the device
232 * @max_segments: max number of segments
233 *
234 * Description:
235 * Enables a low level driver to set an upper limit on the number of
Martin K. Petersen8a783622010-02-26 00:20:39 -0500236 * hw data segments in a request.
Jens Axboe86db1e22008-01-29 14:53:40 +0100237 **/
Martin K. Petersen8a783622010-02-26 00:20:39 -0500238void blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
Jens Axboe86db1e22008-01-29 14:53:40 +0100239{
240 if (!max_segments) {
241 max_segments = 1;
Harvey Harrison24c03d42008-05-01 04:35:17 -0700242 printk(KERN_INFO "%s: set to minimum %d\n",
243 __func__, max_segments);
Jens Axboe86db1e22008-01-29 14:53:40 +0100244 }
245
Martin K. Petersen8a783622010-02-26 00:20:39 -0500246 q->limits.max_segments = max_segments;
Jens Axboe86db1e22008-01-29 14:53:40 +0100247}
Martin K. Petersen8a783622010-02-26 00:20:39 -0500248EXPORT_SYMBOL(blk_queue_max_segments);
Jens Axboe86db1e22008-01-29 14:53:40 +0100249
250/**
Christoph Hellwig1e739732017-02-08 14:46:49 +0100251 * blk_queue_max_discard_segments - set max segments for discard requests
252 * @q: the request queue for the device
253 * @max_segments: max number of segments
254 *
255 * Description:
256 * Enables a low level driver to set an upper limit on the number of
257 * segments in a discard request.
258 **/
259void blk_queue_max_discard_segments(struct request_queue *q,
260 unsigned short max_segments)
261{
262 q->limits.max_discard_segments = max_segments;
263}
264EXPORT_SYMBOL_GPL(blk_queue_max_discard_segments);
265
266/**
Jens Axboe86db1e22008-01-29 14:53:40 +0100267 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
268 * @q: the request queue for the device
269 * @max_size: max size of segment in bytes
270 *
271 * Description:
272 * Enables a low level driver to set an upper limit on the size of a
273 * coalesced segment
274 **/
275void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
276{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300277 if (max_size < PAGE_SIZE) {
278 max_size = PAGE_SIZE;
Harvey Harrison24c03d42008-05-01 04:35:17 -0700279 printk(KERN_INFO "%s: set to minimum %d\n",
280 __func__, max_size);
Jens Axboe86db1e22008-01-29 14:53:40 +0100281 }
282
Christoph Hellwig09324d32019-05-21 09:01:41 +0200283 /* see blk_queue_virt_boundary() for the explanation */
284 WARN_ON_ONCE(q->limits.virt_boundary_mask);
285
Martin K. Petersen025146e2009-05-22 17:17:51 -0400286 q->limits.max_segment_size = max_size;
Jens Axboe86db1e22008-01-29 14:53:40 +0100287}
Jens Axboe86db1e22008-01-29 14:53:40 +0100288EXPORT_SYMBOL(blk_queue_max_segment_size);
289
290/**
Martin K. Petersene1defc42009-05-22 17:17:49 -0400291 * blk_queue_logical_block_size - set logical block size for the queue
Jens Axboe86db1e22008-01-29 14:53:40 +0100292 * @q: the request queue for the device
Martin K. Petersene1defc42009-05-22 17:17:49 -0400293 * @size: the logical block size, in bytes
Jens Axboe86db1e22008-01-29 14:53:40 +0100294 *
295 * Description:
Martin K. Petersene1defc42009-05-22 17:17:49 -0400296 * This should be set to the lowest possible block size that the
297 * storage device can address. The default of 512 covers most
298 * hardware.
Jens Axboe86db1e22008-01-29 14:53:40 +0100299 **/
Mikulas Patockaad6bf882020-01-15 08:35:25 -0500300void blk_queue_logical_block_size(struct request_queue *q, unsigned int size)
Jens Axboe86db1e22008-01-29 14:53:40 +0100301{
Damien Le Moal817046e2020-11-20 10:55:13 +0900302 struct queue_limits *limits = &q->limits;
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400303
Damien Le Moal817046e2020-11-20 10:55:13 +0900304 limits->logical_block_size = size;
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400305
Damien Le Moal817046e2020-11-20 10:55:13 +0900306 if (limits->physical_block_size < size)
307 limits->physical_block_size = size;
308
309 if (limits->io_min < limits->physical_block_size)
310 limits->io_min = limits->physical_block_size;
311
312 limits->max_hw_sectors =
313 round_down(limits->max_hw_sectors, size >> SECTOR_SHIFT);
314 limits->max_sectors =
315 round_down(limits->max_sectors, size >> SECTOR_SHIFT);
Jens Axboe86db1e22008-01-29 14:53:40 +0100316}
Martin K. Petersene1defc42009-05-22 17:17:49 -0400317EXPORT_SYMBOL(blk_queue_logical_block_size);
Jens Axboe86db1e22008-01-29 14:53:40 +0100318
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400319/**
320 * blk_queue_physical_block_size - set physical block size for the queue
321 * @q: the request queue for the device
322 * @size: the physical block size, in bytes
323 *
324 * Description:
325 * This should be set to the lowest possible sector size that the
326 * hardware can operate on without reverting to read-modify-write
327 * operations.
328 */
Martin K. Petersen892b6f92010-10-13 21:18:03 +0200329void blk_queue_physical_block_size(struct request_queue *q, unsigned int size)
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400330{
331 q->limits.physical_block_size = size;
332
333 if (q->limits.physical_block_size < q->limits.logical_block_size)
334 q->limits.physical_block_size = q->limits.logical_block_size;
335
336 if (q->limits.io_min < q->limits.physical_block_size)
337 q->limits.io_min = q->limits.physical_block_size;
338}
339EXPORT_SYMBOL(blk_queue_physical_block_size);
340
341/**
Damien Le Moala805a4f2021-01-28 13:47:30 +0900342 * blk_queue_zone_write_granularity - set zone write granularity for the queue
343 * @q: the request queue for the zoned device
344 * @size: the zone write granularity size, in bytes
345 *
346 * Description:
347 * This should be set to the lowest possible size allowing to write in
348 * sequential zones of a zoned block device.
349 */
350void blk_queue_zone_write_granularity(struct request_queue *q,
351 unsigned int size)
352{
353 if (WARN_ON_ONCE(!blk_queue_is_zoned(q)))
354 return;
355
356 q->limits.zone_write_granularity = size;
357
358 if (q->limits.zone_write_granularity < q->limits.logical_block_size)
359 q->limits.zone_write_granularity = q->limits.logical_block_size;
360}
361EXPORT_SYMBOL_GPL(blk_queue_zone_write_granularity);
362
363/**
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400364 * blk_queue_alignment_offset - set physical block alignment offset
365 * @q: the request queue for the device
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700366 * @offset: alignment offset in bytes
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400367 *
368 * Description:
369 * Some devices are naturally misaligned to compensate for things like
370 * the legacy DOS partition table 63-sector offset. Low-level drivers
371 * should call this function for devices whose first sector is not
372 * naturally aligned.
373 */
374void blk_queue_alignment_offset(struct request_queue *q, unsigned int offset)
375{
376 q->limits.alignment_offset =
377 offset & (q->limits.physical_block_size - 1);
378 q->limits.misaligned = 0;
379}
380EXPORT_SYMBOL(blk_queue_alignment_offset);
381
Christoph Hellwigc2e4cd52020-09-24 08:51:34 +0200382void blk_queue_update_readahead(struct request_queue *q)
383{
384 /*
385 * For read-ahead of large files to be effective, we need to read ahead
386 * at least twice the optimal I/O size.
387 */
388 q->backing_dev_info->ra_pages =
389 max(queue_io_opt(q) * 2 / PAGE_SIZE, VM_READAHEAD_PAGES);
390 q->backing_dev_info->io_pages =
391 queue_max_sectors(q) >> (PAGE_SHIFT - 9);
392}
393EXPORT_SYMBOL_GPL(blk_queue_update_readahead);
394
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400395/**
Martin K. Petersen7c958e32009-07-31 11:49:11 -0400396 * blk_limits_io_min - set minimum request size for a device
397 * @limits: the queue limits
398 * @min: smallest I/O size in bytes
399 *
400 * Description:
401 * Some devices have an internal block size bigger than the reported
402 * hardware sector size. This function can be used to signal the
403 * smallest I/O the device can perform without incurring a performance
404 * penalty.
405 */
406void blk_limits_io_min(struct queue_limits *limits, unsigned int min)
407{
408 limits->io_min = min;
409
410 if (limits->io_min < limits->logical_block_size)
411 limits->io_min = limits->logical_block_size;
412
413 if (limits->io_min < limits->physical_block_size)
414 limits->io_min = limits->physical_block_size;
415}
416EXPORT_SYMBOL(blk_limits_io_min);
417
418/**
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400419 * blk_queue_io_min - set minimum request size for the queue
420 * @q: the request queue for the device
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700421 * @min: smallest I/O size in bytes
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400422 *
423 * Description:
Martin K. Petersen7e5f5fb2009-07-31 11:49:13 -0400424 * Storage devices may report a granularity or preferred minimum I/O
425 * size which is the smallest request the device can perform without
426 * incurring a performance penalty. For disk drives this is often the
427 * physical block size. For RAID arrays it is often the stripe chunk
428 * size. A properly aligned multiple of minimum_io_size is the
429 * preferred request size for workloads where a high number of I/O
430 * operations is desired.
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400431 */
432void blk_queue_io_min(struct request_queue *q, unsigned int min)
433{
Martin K. Petersen7c958e32009-07-31 11:49:11 -0400434 blk_limits_io_min(&q->limits, min);
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400435}
436EXPORT_SYMBOL(blk_queue_io_min);
437
438/**
Martin K. Petersen3c5820c2009-09-11 21:54:52 +0200439 * blk_limits_io_opt - set optimal request size for a device
440 * @limits: the queue limits
441 * @opt: smallest I/O size in bytes
442 *
443 * Description:
444 * Storage devices may report an optimal I/O size, which is the
445 * device's preferred unit for sustained I/O. This is rarely reported
446 * for disk drives. For RAID arrays it is usually the stripe width or
447 * the internal track size. A properly aligned multiple of
448 * optimal_io_size is the preferred request size for workloads where
449 * sustained throughput is desired.
450 */
451void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt)
452{
453 limits->io_opt = opt;
454}
455EXPORT_SYMBOL(blk_limits_io_opt);
456
457/**
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400458 * blk_queue_io_opt - set optimal request size for the queue
459 * @q: the request queue for the device
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700460 * @opt: optimal request size in bytes
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400461 *
462 * Description:
Martin K. Petersen7e5f5fb2009-07-31 11:49:13 -0400463 * Storage devices may report an optimal I/O size, which is the
464 * device's preferred unit for sustained I/O. This is rarely reported
465 * for disk drives. For RAID arrays it is usually the stripe width or
466 * the internal track size. A properly aligned multiple of
467 * optimal_io_size is the preferred request size for workloads where
468 * sustained throughput is desired.
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400469 */
470void blk_queue_io_opt(struct request_queue *q, unsigned int opt)
471{
Martin K. Petersen3c5820c2009-09-11 21:54:52 +0200472 blk_limits_io_opt(&q->limits, opt);
Christoph Hellwigc2e4cd52020-09-24 08:51:34 +0200473 q->backing_dev_info->ra_pages =
474 max(queue_io_opt(q) * 2 / PAGE_SIZE, VM_READAHEAD_PAGES);
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400475}
476EXPORT_SYMBOL(blk_queue_io_opt);
477
Mikulas Patocka97f433c2021-02-23 19:25:30 -0700478static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs)
479{
480 sectors = round_down(sectors, lbs >> SECTOR_SHIFT);
481 if (sectors < PAGE_SIZE >> SECTOR_SHIFT)
482 sectors = PAGE_SIZE >> SECTOR_SHIFT;
483 return sectors;
484}
485
Jens Axboe86db1e22008-01-29 14:53:40 +0100486/**
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400487 * blk_stack_limits - adjust queue_limits for stacked devices
Martin K. Petersen81744ee2009-12-29 08:35:35 +0100488 * @t: the stacking driver limits (top device)
489 * @b: the underlying queue limits (bottom, component device)
Martin K. Petersene03a72e2010-01-11 03:21:51 -0500490 * @start: first data sector within component device
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400491 *
492 * Description:
Martin K. Petersen81744ee2009-12-29 08:35:35 +0100493 * This function is used by stacking drivers like MD and DM to ensure
494 * that all component devices have compatible block sizes and
495 * alignments. The stacking driver must provide a queue_limits
496 * struct (top) and then iteratively call the stacking function for
497 * all component (bottom) devices. The stacking function will
498 * attempt to combine the values and ensure proper alignment.
499 *
500 * Returns 0 if the top and bottom queue_limits are compatible. The
501 * top device's block sizes and alignment offsets may be adjusted to
502 * ensure alignment with the bottom device. If no compatible sizes
503 * and alignments exist, -1 is returned and the resulting top
504 * queue_limits will have the misaligned flag set to indicate that
505 * the alignment_offset is undefined.
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400506 */
507int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
Martin K. Petersene03a72e2010-01-11 03:21:51 -0500508 sector_t start)
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400509{
Martin K. Petersene03a72e2010-01-11 03:21:51 -0500510 unsigned int top, bottom, alignment, ret = 0;
Martin K. Petersen86b37282009-11-10 11:50:21 +0100511
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400512 t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
513 t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
Martin K. Petersenca369d52015-11-13 16:46:48 -0500514 t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400515 t->max_write_same_sectors = min(t->max_write_same_sectors,
516 b->max_write_same_sectors);
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800517 t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors,
518 b->max_write_zeroes_sectors);
Keith Busch0512a752020-05-12 17:55:47 +0900519 t->max_zone_append_sectors = min(t->max_zone_append_sectors,
520 b->max_zone_append_sectors);
Christoph Hellwig9bb33f22021-03-31 09:30:00 +0200521 t->bounce = max(t->bounce, b->bounce);
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400522
523 t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
524 b->seg_boundary_mask);
Keith Busch03100aa2015-08-19 14:24:05 -0700525 t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask,
526 b->virt_boundary_mask);
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400527
Martin K. Petersen8a783622010-02-26 00:20:39 -0500528 t->max_segments = min_not_zero(t->max_segments, b->max_segments);
Christoph Hellwig1e739732017-02-08 14:46:49 +0100529 t->max_discard_segments = min_not_zero(t->max_discard_segments,
530 b->max_discard_segments);
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200531 t->max_integrity_segments = min_not_zero(t->max_integrity_segments,
532 b->max_integrity_segments);
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400533
534 t->max_segment_size = min_not_zero(t->max_segment_size,
535 b->max_segment_size);
536
Martin K. Petersenfe0b3932010-01-11 03:21:47 -0500537 t->misaligned |= b->misaligned;
538
Martin K. Petersene03a72e2010-01-11 03:21:51 -0500539 alignment = queue_limit_alignment_offset(b, start);
Martin K. Petersen9504e082009-12-21 15:55:51 +0100540
Martin K. Petersen81744ee2009-12-29 08:35:35 +0100541 /* Bottom device has different alignment. Check that it is
542 * compatible with the current top alignment.
543 */
Martin K. Petersen9504e082009-12-21 15:55:51 +0100544 if (t->alignment_offset != alignment) {
545
546 top = max(t->physical_block_size, t->io_min)
547 + t->alignment_offset;
Martin K. Petersen81744ee2009-12-29 08:35:35 +0100548 bottom = max(b->physical_block_size, b->io_min) + alignment;
Martin K. Petersen9504e082009-12-21 15:55:51 +0100549
Martin K. Petersen81744ee2009-12-29 08:35:35 +0100550 /* Verify that top and bottom intervals line up */
Mike Snitzerb8839b82014-10-08 18:26:13 -0400551 if (max(top, bottom) % min(top, bottom)) {
Martin K. Petersen9504e082009-12-21 15:55:51 +0100552 t->misaligned = 1;
Martin K. Petersenfe0b3932010-01-11 03:21:47 -0500553 ret = -1;
554 }
Martin K. Petersen9504e082009-12-21 15:55:51 +0100555 }
556
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400557 t->logical_block_size = max(t->logical_block_size,
558 b->logical_block_size);
559
560 t->physical_block_size = max(t->physical_block_size,
561 b->physical_block_size);
562
563 t->io_min = max(t->io_min, b->io_min);
Mike Snitzere9637412015-03-30 13:39:09 -0400564 t->io_opt = lcm_not_zero(t->io_opt, b->io_opt);
Mike Snitzer7e7986f2020-12-01 11:07:09 -0500565
566 /* Set non-power-of-2 compatible chunk_sectors boundary */
567 if (b->chunk_sectors)
568 t->chunk_sectors = gcd(t->chunk_sectors, b->chunk_sectors);
Martin K. Petersen9504e082009-12-21 15:55:51 +0100569
Martin K. Petersen81744ee2009-12-29 08:35:35 +0100570 /* Physical block size a multiple of the logical block size? */
Martin K. Petersen9504e082009-12-21 15:55:51 +0100571 if (t->physical_block_size & (t->logical_block_size - 1)) {
572 t->physical_block_size = t->logical_block_size;
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400573 t->misaligned = 1;
Martin K. Petersenfe0b3932010-01-11 03:21:47 -0500574 ret = -1;
Martin K. Petersen86b37282009-11-10 11:50:21 +0100575 }
576
Martin K. Petersen81744ee2009-12-29 08:35:35 +0100577 /* Minimum I/O a multiple of the physical block size? */
Martin K. Petersen9504e082009-12-21 15:55:51 +0100578 if (t->io_min & (t->physical_block_size - 1)) {
579 t->io_min = t->physical_block_size;
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400580 t->misaligned = 1;
Martin K. Petersenfe0b3932010-01-11 03:21:47 -0500581 ret = -1;
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400582 }
583
Martin K. Petersen81744ee2009-12-29 08:35:35 +0100584 /* Optimal I/O a multiple of the physical block size? */
Martin K. Petersen9504e082009-12-21 15:55:51 +0100585 if (t->io_opt & (t->physical_block_size - 1)) {
586 t->io_opt = 0;
587 t->misaligned = 1;
Martin K. Petersenfe0b3932010-01-11 03:21:47 -0500588 ret = -1;
Martin K. Petersen9504e082009-12-21 15:55:51 +0100589 }
Martin K. Petersen70dd5bf2009-07-31 11:49:12 -0400590
Mike Snitzer22ada802020-09-21 22:32:48 -0400591 /* chunk_sectors a multiple of the physical block size? */
592 if ((t->chunk_sectors << 9) & (t->physical_block_size - 1)) {
593 t->chunk_sectors = 0;
594 t->misaligned = 1;
595 ret = -1;
596 }
597
Kent Overstreetc78afc62013-07-11 22:39:53 -0700598 t->raid_partial_stripes_expensive =
599 max(t->raid_partial_stripes_expensive,
600 b->raid_partial_stripes_expensive);
601
Martin K. Petersen81744ee2009-12-29 08:35:35 +0100602 /* Find lowest common alignment_offset */
Mike Snitzere9637412015-03-30 13:39:09 -0400603 t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment)
Mike Snitzerb8839b82014-10-08 18:26:13 -0400604 % max(t->physical_block_size, t->io_min);
Martin K. Petersen70dd5bf2009-07-31 11:49:12 -0400605
Martin K. Petersen81744ee2009-12-29 08:35:35 +0100606 /* Verify that new alignment_offset is on a logical block boundary */
Martin K. Petersenfe0b3932010-01-11 03:21:47 -0500607 if (t->alignment_offset & (t->logical_block_size - 1)) {
Martin K. Petersen9504e082009-12-21 15:55:51 +0100608 t->misaligned = 1;
Martin K. Petersenfe0b3932010-01-11 03:21:47 -0500609 ret = -1;
610 }
Martin K. Petersen9504e082009-12-21 15:55:51 +0100611
Mikulas Patocka97f433c2021-02-23 19:25:30 -0700612 t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size);
613 t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size);
614 t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size);
615
Martin K. Petersen9504e082009-12-21 15:55:51 +0100616 /* Discard alignment and granularity */
617 if (b->discard_granularity) {
Martin K. Petersene03a72e2010-01-11 03:21:51 -0500618 alignment = queue_limit_discard_alignment(b, start);
Martin K. Petersen9504e082009-12-21 15:55:51 +0100619
620 if (t->discard_granularity != 0 &&
621 t->discard_alignment != alignment) {
622 top = t->discard_granularity + t->discard_alignment;
623 bottom = b->discard_granularity + alignment;
624
625 /* Verify that top and bottom intervals line up */
Shaohua Li8dd2cb72012-12-14 11:15:36 +0800626 if ((max(top, bottom) % min(top, bottom)) != 0)
Martin K. Petersen9504e082009-12-21 15:55:51 +0100627 t->discard_misaligned = 1;
628 }
629
Martin K. Petersen81744ee2009-12-29 08:35:35 +0100630 t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
631 b->max_discard_sectors);
Jens Axboe0034af02015-07-16 09:14:26 -0600632 t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
633 b->max_hw_discard_sectors);
Martin K. Petersen9504e082009-12-21 15:55:51 +0100634 t->discard_granularity = max(t->discard_granularity,
635 b->discard_granularity);
Mike Snitzere9637412015-03-30 13:39:09 -0400636 t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
Shaohua Li8dd2cb72012-12-14 11:15:36 +0800637 t->discard_granularity;
Martin K. Petersen9504e082009-12-21 15:55:51 +0100638 }
639
Damien Le Moala805a4f2021-01-28 13:47:30 +0900640 t->zone_write_granularity = max(t->zone_write_granularity,
641 b->zone_write_granularity);
Christoph Hellwig3093a472020-07-20 08:12:49 +0200642 t->zoned = max(t->zoned, b->zoned);
Martin K. Petersenfe0b3932010-01-11 03:21:47 -0500643 return ret;
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400644}
Mike Snitzer5d85d322009-05-28 11:04:53 +0200645EXPORT_SYMBOL(blk_stack_limits);
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400646
647/**
648 * disk_stack_limits - adjust queue limits for stacked drivers
Martin K. Petersen77634f32009-06-09 06:23:22 +0200649 * @disk: MD/DM gendisk (top)
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400650 * @bdev: the underlying block device (bottom)
651 * @offset: offset to beginning of data within component device
652 *
653 * Description:
Martin K. Petersene03a72e2010-01-11 03:21:51 -0500654 * Merges the limits for a top level gendisk and a bottom level
655 * block_device.
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400656 */
657void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
658 sector_t offset)
659{
660 struct request_queue *t = disk->queue;
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400661
Christoph Hellwig9efa82e2020-07-20 08:12:50 +0200662 if (blk_stack_limits(&t->limits, &bdev_get_queue(bdev)->limits,
663 get_start_sect(bdev) + (offset >> 9)) < 0) {
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400664 char top[BDEVNAME_SIZE], bottom[BDEVNAME_SIZE];
665
666 disk_name(disk, 0, top);
667 bdevname(bdev, bottom);
668
669 printk(KERN_NOTICE "%s: Warning: Device %s is misaligned\n",
670 top, bottom);
671 }
Konstantin Khlebnikove74d93e2020-02-28 17:51:48 +0300672
Christoph Hellwigc2e4cd52020-09-24 08:51:34 +0200673 blk_queue_update_readahead(disk->queue);
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400674}
675EXPORT_SYMBOL(disk_stack_limits);
676
677/**
FUJITA Tomonori27f82212008-07-04 09:30:03 +0200678 * blk_queue_update_dma_pad - update pad mask
679 * @q: the request queue for the device
680 * @mask: pad mask
681 *
682 * Update dma pad mask.
683 *
684 * Appending pad buffer to a request modifies the last entry of a
685 * scatter list such that it includes the pad buffer.
686 **/
687void blk_queue_update_dma_pad(struct request_queue *q, unsigned int mask)
688{
689 if (mask > q->dma_pad_mask)
690 q->dma_pad_mask = mask;
691}
692EXPORT_SYMBOL(blk_queue_update_dma_pad);
693
694/**
Jens Axboe86db1e22008-01-29 14:53:40 +0100695 * blk_queue_segment_boundary - set boundary rules for segment merging
696 * @q: the request queue for the device
697 * @mask: the memory boundary mask
698 **/
699void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
700{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300701 if (mask < PAGE_SIZE - 1) {
702 mask = PAGE_SIZE - 1;
Harvey Harrison24c03d42008-05-01 04:35:17 -0700703 printk(KERN_INFO "%s: set to minimum %lx\n",
704 __func__, mask);
Jens Axboe86db1e22008-01-29 14:53:40 +0100705 }
706
Martin K. Petersen025146e2009-05-22 17:17:51 -0400707 q->limits.seg_boundary_mask = mask;
Jens Axboe86db1e22008-01-29 14:53:40 +0100708}
Jens Axboe86db1e22008-01-29 14:53:40 +0100709EXPORT_SYMBOL(blk_queue_segment_boundary);
710
711/**
Keith Busch03100aa2015-08-19 14:24:05 -0700712 * blk_queue_virt_boundary - set boundary rules for bio merging
713 * @q: the request queue for the device
714 * @mask: the memory boundary mask
715 **/
716void blk_queue_virt_boundary(struct request_queue *q, unsigned long mask)
717{
718 q->limits.virt_boundary_mask = mask;
Christoph Hellwig09324d32019-05-21 09:01:41 +0200719
720 /*
721 * Devices that require a virtual boundary do not support scatter/gather
722 * I/O natively, but instead require a descriptor list entry for each
723 * page (which might not be idential to the Linux PAGE_SIZE). Because
724 * of that they are not limited by our notion of "segment size".
725 */
Christoph Hellwigc6c84f72019-07-24 18:26:56 +0200726 if (mask)
727 q->limits.max_segment_size = UINT_MAX;
Keith Busch03100aa2015-08-19 14:24:05 -0700728}
729EXPORT_SYMBOL(blk_queue_virt_boundary);
730
731/**
Jens Axboe86db1e22008-01-29 14:53:40 +0100732 * blk_queue_dma_alignment - set dma length and memory alignment
733 * @q: the request queue for the device
734 * @mask: alignment mask
735 *
736 * description:
Randy Dunlap710027a2008-08-19 20:13:11 +0200737 * set required memory and length alignment for direct dma transactions.
Alan Cox8feb4d22009-04-01 15:01:39 +0100738 * this is used when building direct io requests for the queue.
Jens Axboe86db1e22008-01-29 14:53:40 +0100739 *
740 **/
741void blk_queue_dma_alignment(struct request_queue *q, int mask)
742{
743 q->dma_alignment = mask;
744}
Jens Axboe86db1e22008-01-29 14:53:40 +0100745EXPORT_SYMBOL(blk_queue_dma_alignment);
746
747/**
748 * blk_queue_update_dma_alignment - update dma length and memory alignment
749 * @q: the request queue for the device
750 * @mask: alignment mask
751 *
752 * description:
Randy Dunlap710027a2008-08-19 20:13:11 +0200753 * update required memory and length alignment for direct dma transactions.
Jens Axboe86db1e22008-01-29 14:53:40 +0100754 * If the requested alignment is larger than the current alignment, then
755 * the current queue alignment is updated to the new value, otherwise it
756 * is left alone. The design of this is to allow multiple objects
757 * (driver, device, transport etc) to set their respective
758 * alignments without having them interfere.
759 *
760 **/
761void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
762{
763 BUG_ON(mask > PAGE_SIZE);
764
765 if (mask > q->dma_alignment)
766 q->dma_alignment = mask;
767}
Jens Axboe86db1e22008-01-29 14:53:40 +0100768EXPORT_SYMBOL(blk_queue_update_dma_alignment);
769
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600770/**
Jens Axboed278d4a2016-03-30 10:21:08 -0600771 * blk_set_queue_depth - tell the block layer about the device queue depth
772 * @q: the request queue for the device
773 * @depth: queue depth
774 *
775 */
776void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
777{
778 q->queue_depth = depth;
Tejun Heo9677a3e2019-08-28 15:05:55 -0700779 rq_qos_queue_depth_changed(q);
Jens Axboed278d4a2016-03-30 10:21:08 -0600780}
781EXPORT_SYMBOL(blk_set_queue_depth);
782
783/**
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600784 * blk_queue_write_cache - configure queue's write cache
785 * @q: the request queue for the device
786 * @wc: write back cache on or off
787 * @fua: device supports FUA writes, if true
788 *
789 * Tell the block layer about the write cache of @q.
790 */
791void blk_queue_write_cache(struct request_queue *q, bool wc, bool fua)
792{
Jens Axboec888a8f2016-04-13 13:33:19 -0600793 if (wc)
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100794 blk_queue_flag_set(QUEUE_FLAG_WC, q);
Jens Axboec888a8f2016-04-13 13:33:19 -0600795 else
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100796 blk_queue_flag_clear(QUEUE_FLAG_WC, q);
Jens Axboec888a8f2016-04-13 13:33:19 -0600797 if (fua)
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100798 blk_queue_flag_set(QUEUE_FLAG_FUA, q);
Jens Axboec888a8f2016-04-13 13:33:19 -0600799 else
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100800 blk_queue_flag_clear(QUEUE_FLAG_FUA, q);
Jens Axboe87760e52016-11-09 12:38:14 -0700801
Josef Bacika7905042018-07-03 09:32:35 -0600802 wbt_set_write_cache(q, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600803}
804EXPORT_SYMBOL_GPL(blk_queue_write_cache);
805
Damien Le Moal68c43f12019-09-05 18:51:31 +0900806/**
807 * blk_queue_required_elevator_features - Set a queue required elevator features
808 * @q: the request queue for the target device
809 * @features: Required elevator features OR'ed together
810 *
811 * Tell the block layer that for the device controlled through @q, only the
812 * only elevators that can be used are those that implement at least the set of
813 * features specified by @features.
814 */
815void blk_queue_required_elevator_features(struct request_queue *q,
816 unsigned int features)
817{
818 q->required_elevator_features = features;
819}
820EXPORT_SYMBOL_GPL(blk_queue_required_elevator_features);
821
Linus Torvalds671df182019-09-19 13:27:23 -0700822/**
Yoshihiro Shimoda45147fb2019-08-28 21:35:42 +0900823 * blk_queue_can_use_dma_map_merging - configure queue for merging segments.
824 * @q: the request queue for the device
825 * @dev: the device pointer for dma
826 *
827 * Tell the block layer about merging the segments by dma map of @q.
828 */
829bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
830 struct device *dev)
831{
832 unsigned long boundary = dma_get_merge_boundary(dev);
833
834 if (!boundary)
835 return false;
836
837 /* No need to update max_segment_size. see blk_queue_virt_boundary() */
838 blk_queue_virt_boundary(q, boundary);
839
840 return true;
841}
842EXPORT_SYMBOL_GPL(blk_queue_can_use_dma_map_merging);
843
Damien Le Moal27ba3e82020-09-15 16:33:46 +0900844/**
845 * blk_queue_set_zoned - configure a disk queue zoned model.
846 * @disk: the gendisk of the queue to configure
847 * @model: the zoned model to set
848 *
849 * Set the zoned model of the request queue of @disk according to @model.
850 * When @model is BLK_ZONED_HM (host managed), this should be called only
851 * if zoned block device support is enabled (CONFIG_BLK_DEV_ZONED option).
852 * If @model specifies BLK_ZONED_HA (host aware), the effective model used
853 * depends on CONFIG_BLK_DEV_ZONED settings and on the existence of partitions
854 * on the disk.
855 */
856void blk_queue_set_zoned(struct gendisk *disk, enum blk_zoned_model model)
857{
Damien Le Moala805a4f2021-01-28 13:47:30 +0900858 struct request_queue *q = disk->queue;
859
Damien Le Moal27ba3e82020-09-15 16:33:46 +0900860 switch (model) {
861 case BLK_ZONED_HM:
862 /*
863 * Host managed devices are supported only if
864 * CONFIG_BLK_DEV_ZONED is enabled.
865 */
866 WARN_ON_ONCE(!IS_ENABLED(CONFIG_BLK_DEV_ZONED));
867 break;
868 case BLK_ZONED_HA:
869 /*
870 * Host aware devices can be treated either as regular block
871 * devices (similar to drive managed devices) or as zoned block
872 * devices to take advantage of the zone command set, similarly
873 * to host managed devices. We try the latter if there are no
874 * partitions and zoned block device support is enabled, else
875 * we do nothing special as far as the block layer is concerned.
876 */
877 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED) ||
Christoph Hellwiga33df752021-01-24 11:02:41 +0100878 !xa_empty(&disk->part_tbl))
Damien Le Moal27ba3e82020-09-15 16:33:46 +0900879 model = BLK_ZONED_NONE;
880 break;
881 case BLK_ZONED_NONE:
882 default:
883 if (WARN_ON_ONCE(model != BLK_ZONED_NONE))
884 model = BLK_ZONED_NONE;
885 break;
886 }
887
Damien Le Moala805a4f2021-01-28 13:47:30 +0900888 q->limits.zoned = model;
889 if (model != BLK_ZONED_NONE) {
890 /*
891 * Set the zone write granularity to the device logical block
892 * size by default. The driver can change this value if needed.
893 */
894 blk_queue_zone_write_granularity(q,
895 queue_logical_block_size(q));
Damien Le Moal508aebb2021-01-28 13:47:32 +0900896 } else {
897 blk_queue_clear_zone_settings(q);
Damien Le Moala805a4f2021-01-28 13:47:30 +0900898 }
Damien Le Moal27ba3e82020-09-15 16:33:46 +0900899}
900EXPORT_SYMBOL_GPL(blk_queue_set_zoned);