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