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