1
2
3
4
5#include <linux/kernel.h>
6#include <linux/module.h>
7#include <linux/init.h>
8#include <linux/bio.h>
9#include <linux/blkdev.h>
10#include <linux/memblock.h>
11#include <linux/gcd.h>
12#include <linux/lcm.h>
13#include <linux/jiffies.h>
14#include <linux/gfp.h>
15
16#include "blk.h"
17#include "blk-wbt.h"
18
19unsigned long blk_max_low_pfn;
20EXPORT_SYMBOL(blk_max_low_pfn);
21
22unsigned long blk_max_pfn;
23
24void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout)
25{
26 q->rq_timeout = timeout;
27}
28EXPORT_SYMBOL_GPL(blk_queue_rq_timeout);
29
30
31
32
33
34
35
36
37void blk_set_default_limits(struct queue_limits *lim)
38{
39 lim->max_segments = BLK_MAX_SEGMENTS;
40 lim->max_discard_segments = 1;
41 lim->max_integrity_segments = 0;
42 lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
43 lim->virt_boundary_mask = 0;
44 lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
45 lim->max_sectors = lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS;
46 lim->max_dev_sectors = 0;
47 lim->chunk_sectors = 0;
48 lim->max_write_same_sectors = 0;
49 lim->max_write_zeroes_sectors = 0;
50 lim->max_discard_sectors = 0;
51 lim->max_hw_discard_sectors = 0;
52 lim->discard_granularity = 0;
53 lim->discard_alignment = 0;
54 lim->discard_misaligned = 0;
55 lim->logical_block_size = lim->physical_block_size = lim->io_min = 512;
56 lim->bounce_pfn = (unsigned long)(BLK_BOUNCE_ANY >> PAGE_SHIFT);
57 lim->alignment_offset = 0;
58 lim->io_opt = 0;
59 lim->misaligned = 0;
60 lim->zoned = BLK_ZONED_NONE;
61}
62EXPORT_SYMBOL(blk_set_default_limits);
63
64
65
66
67
68
69
70
71
72void blk_set_stacking_limits(struct queue_limits *lim)
73{
74 blk_set_default_limits(lim);
75
76
77 lim->max_segments = USHRT_MAX;
78 lim->max_discard_segments = USHRT_MAX;
79 lim->max_hw_sectors = UINT_MAX;
80 lim->max_segment_size = UINT_MAX;
81 lim->max_sectors = UINT_MAX;
82 lim->max_dev_sectors = UINT_MAX;
83 lim->max_write_same_sectors = UINT_MAX;
84 lim->max_write_zeroes_sectors = UINT_MAX;
85}
86EXPORT_SYMBOL(blk_set_stacking_limits);
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110void blk_queue_make_request(struct request_queue *q, make_request_fn *mfn)
111{
112
113
114
115 q->nr_requests = BLKDEV_MAX_RQ;
116
117 q->make_request_fn = mfn;
118 blk_queue_dma_alignment(q, 511);
119
120 blk_set_default_limits(&q->limits);
121}
122EXPORT_SYMBOL(blk_queue_make_request);
123
124
125
126
127
128
129
130
131
132
133
134
135void blk_queue_bounce_limit(struct request_queue *q, u64 max_addr)
136{
137 unsigned long b_pfn = max_addr >> PAGE_SHIFT;
138 int dma = 0;
139
140 q->bounce_gfp = GFP_NOIO;
141#if BITS_PER_LONG == 64
142
143
144
145
146
147 if (b_pfn < (min_t(u64, 0xffffffffUL, BLK_BOUNCE_HIGH) >> PAGE_SHIFT))
148 dma = 1;
149 q->limits.bounce_pfn = max(max_low_pfn, b_pfn);
150#else
151 if (b_pfn < blk_max_low_pfn)
152 dma = 1;
153 q->limits.bounce_pfn = b_pfn;
154#endif
155 if (dma) {
156 init_emergency_isa_pool();
157 q->bounce_gfp = GFP_NOIO | GFP_DMA;
158 q->limits.bounce_pfn = b_pfn;
159 }
160}
161EXPORT_SYMBOL(blk_queue_bounce_limit);
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
183{
184 struct queue_limits *limits = &q->limits;
185 unsigned int max_sectors;
186
187 if ((max_hw_sectors << 9) < PAGE_SIZE) {
188 max_hw_sectors = 1 << (PAGE_SHIFT - 9);
189 printk(KERN_INFO "%s: set to minimum %d\n",
190 __func__, max_hw_sectors);
191 }
192
193 limits->max_hw_sectors = max_hw_sectors;
194 max_sectors = min_not_zero(max_hw_sectors, limits->max_dev_sectors);
195 max_sectors = min_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
196 limits->max_sectors = max_sectors;
197 q->backing_dev_info->io_pages = max_sectors >> (PAGE_SHIFT - 9);
198}
199EXPORT_SYMBOL(blk_queue_max_hw_sectors);
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214void blk_queue_chunk_sectors(struct request_queue *q, unsigned int chunk_sectors)
215{
216 BUG_ON(!is_power_of_2(chunk_sectors));
217 q->limits.chunk_sectors = chunk_sectors;
218}
219EXPORT_SYMBOL(blk_queue_chunk_sectors);
220
221
222
223
224
225
226void blk_queue_max_discard_sectors(struct request_queue *q,
227 unsigned int max_discard_sectors)
228{
229 q->limits.max_hw_discard_sectors = max_discard_sectors;
230 q->limits.max_discard_sectors = max_discard_sectors;
231}
232EXPORT_SYMBOL(blk_queue_max_discard_sectors);
233
234
235
236
237
238
239void blk_queue_max_write_same_sectors(struct request_queue *q,
240 unsigned int max_write_same_sectors)
241{
242 q->limits.max_write_same_sectors = max_write_same_sectors;
243}
244EXPORT_SYMBOL(blk_queue_max_write_same_sectors);
245
246
247
248
249
250
251
252void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
253 unsigned int max_write_zeroes_sectors)
254{
255 q->limits.max_write_zeroes_sectors = max_write_zeroes_sectors;
256}
257EXPORT_SYMBOL(blk_queue_max_write_zeroes_sectors);
258
259
260
261
262
263
264
265
266
267
268void blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
269{
270 if (!max_segments) {
271 max_segments = 1;
272 printk(KERN_INFO "%s: set to minimum %d\n",
273 __func__, max_segments);
274 }
275
276 q->limits.max_segments = max_segments;
277}
278EXPORT_SYMBOL(blk_queue_max_segments);
279
280
281
282
283
284
285
286
287
288
289void blk_queue_max_discard_segments(struct request_queue *q,
290 unsigned short max_segments)
291{
292 q->limits.max_discard_segments = max_segments;
293}
294EXPORT_SYMBOL_GPL(blk_queue_max_discard_segments);
295
296
297
298
299
300
301
302
303
304
305void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
306{
307 if (max_size < PAGE_SIZE) {
308 max_size = PAGE_SIZE;
309 printk(KERN_INFO "%s: set to minimum %d\n",
310 __func__, max_size);
311 }
312
313
314 WARN_ON_ONCE(q->limits.virt_boundary_mask);
315
316 q->limits.max_segment_size = max_size;
317}
318EXPORT_SYMBOL(blk_queue_max_segment_size);
319
320
321
322
323
324
325
326
327
328
329
330void blk_queue_logical_block_size(struct request_queue *q, unsigned short size)
331{
332 q->limits.logical_block_size = size;
333
334 if (q->limits.physical_block_size < size)
335 q->limits.physical_block_size = size;
336
337 if (q->limits.io_min < q->limits.physical_block_size)
338 q->limits.io_min = q->limits.physical_block_size;
339}
340EXPORT_SYMBOL(blk_queue_logical_block_size);
341
342
343
344
345
346
347
348
349
350
351
352void blk_queue_physical_block_size(struct request_queue *q, unsigned int size)
353{
354 q->limits.physical_block_size = size;
355
356 if (q->limits.physical_block_size < q->limits.logical_block_size)
357 q->limits.physical_block_size = q->limits.logical_block_size;
358
359 if (q->limits.io_min < q->limits.physical_block_size)
360 q->limits.io_min = q->limits.physical_block_size;
361}
362EXPORT_SYMBOL(blk_queue_physical_block_size);
363
364
365
366
367
368
369
370
371
372
373
374
375void blk_queue_alignment_offset(struct request_queue *q, unsigned int offset)
376{
377 q->limits.alignment_offset =
378 offset & (q->limits.physical_block_size - 1);
379 q->limits.misaligned = 0;
380}
381EXPORT_SYMBOL(blk_queue_alignment_offset);
382
383
384
385
386
387
388
389
390
391
392
393
394void blk_limits_io_min(struct queue_limits *limits, unsigned int min)
395{
396 limits->io_min = min;
397
398 if (limits->io_min < limits->logical_block_size)
399 limits->io_min = limits->logical_block_size;
400
401 if (limits->io_min < limits->physical_block_size)
402 limits->io_min = limits->physical_block_size;
403}
404EXPORT_SYMBOL(blk_limits_io_min);
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420void blk_queue_io_min(struct request_queue *q, unsigned int min)
421{
422 blk_limits_io_min(&q->limits, min);
423}
424EXPORT_SYMBOL(blk_queue_io_min);
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt)
440{
441 limits->io_opt = opt;
442}
443EXPORT_SYMBOL(blk_limits_io_opt);
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458void blk_queue_io_opt(struct request_queue *q, unsigned int opt)
459{
460 blk_limits_io_opt(&q->limits, opt);
461}
462EXPORT_SYMBOL(blk_queue_io_opt);
463
464
465
466
467
468
469void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b)
470{
471 blk_stack_limits(&t->limits, &b->limits, 0);
472}
473EXPORT_SYMBOL(blk_queue_stack_limits);
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
497 sector_t start)
498{
499 unsigned int top, bottom, alignment, ret = 0;
500
501 t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
502 t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
503 t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
504 t->max_write_same_sectors = min(t->max_write_same_sectors,
505 b->max_write_same_sectors);
506 t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors,
507 b->max_write_zeroes_sectors);
508 t->bounce_pfn = min_not_zero(t->bounce_pfn, b->bounce_pfn);
509
510 t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
511 b->seg_boundary_mask);
512 t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask,
513 b->virt_boundary_mask);
514
515 t->max_segments = min_not_zero(t->max_segments, b->max_segments);
516 t->max_discard_segments = min_not_zero(t->max_discard_segments,
517 b->max_discard_segments);
518 t->max_integrity_segments = min_not_zero(t->max_integrity_segments,
519 b->max_integrity_segments);
520
521 t->max_segment_size = min_not_zero(t->max_segment_size,
522 b->max_segment_size);
523
524 t->misaligned |= b->misaligned;
525
526 alignment = queue_limit_alignment_offset(b, start);
527
528
529
530
531 if (t->alignment_offset != alignment) {
532
533 top = max(t->physical_block_size, t->io_min)
534 + t->alignment_offset;
535 bottom = max(b->physical_block_size, b->io_min) + alignment;
536
537
538 if (max(top, bottom) % min(top, bottom)) {
539 t->misaligned = 1;
540 ret = -1;
541 }
542 }
543
544 t->logical_block_size = max(t->logical_block_size,
545 b->logical_block_size);
546
547 t->physical_block_size = max(t->physical_block_size,
548 b->physical_block_size);
549
550 t->io_min = max(t->io_min, b->io_min);
551 t->io_opt = lcm_not_zero(t->io_opt, b->io_opt);
552
553
554 if (t->physical_block_size & (t->logical_block_size - 1)) {
555 t->physical_block_size = t->logical_block_size;
556 t->misaligned = 1;
557 ret = -1;
558 }
559
560
561 if (t->io_min & (t->physical_block_size - 1)) {
562 t->io_min = t->physical_block_size;
563 t->misaligned = 1;
564 ret = -1;
565 }
566
567
568 if (t->io_opt & (t->physical_block_size - 1)) {
569 t->io_opt = 0;
570 t->misaligned = 1;
571 ret = -1;
572 }
573
574 t->raid_partial_stripes_expensive =
575 max(t->raid_partial_stripes_expensive,
576 b->raid_partial_stripes_expensive);
577
578
579 t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment)
580 % max(t->physical_block_size, t->io_min);
581
582
583 if (t->alignment_offset & (t->logical_block_size - 1)) {
584 t->misaligned = 1;
585 ret = -1;
586 }
587
588
589 if (b->discard_granularity) {
590 alignment = queue_limit_discard_alignment(b, start);
591
592 if (t->discard_granularity != 0 &&
593 t->discard_alignment != alignment) {
594 top = t->discard_granularity + t->discard_alignment;
595 bottom = b->discard_granularity + alignment;
596
597
598 if ((max(top, bottom) % min(top, bottom)) != 0)
599 t->discard_misaligned = 1;
600 }
601
602 t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
603 b->max_discard_sectors);
604 t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
605 b->max_hw_discard_sectors);
606 t->discard_granularity = max(t->discard_granularity,
607 b->discard_granularity);
608 t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
609 t->discard_granularity;
610 }
611
612 if (b->chunk_sectors)
613 t->chunk_sectors = min_not_zero(t->chunk_sectors,
614 b->chunk_sectors);
615
616 return ret;
617}
618EXPORT_SYMBOL(blk_stack_limits);
619
620
621
622
623
624
625
626
627
628
629
630
631int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev,
632 sector_t start)
633{
634 struct request_queue *bq = bdev_get_queue(bdev);
635
636 start += get_start_sect(bdev);
637
638 return blk_stack_limits(t, &bq->limits, start);
639}
640EXPORT_SYMBOL(bdev_stack_limits);
641
642
643
644
645
646
647
648
649
650
651
652void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
653 sector_t offset)
654{
655 struct request_queue *t = disk->queue;
656
657 if (bdev_stack_limits(&t->limits, bdev, offset >> 9) < 0) {
658 char top[BDEVNAME_SIZE], bottom[BDEVNAME_SIZE];
659
660 disk_name(disk, 0, top);
661 bdevname(bdev, bottom);
662
663 printk(KERN_NOTICE "%s: Warning: Device %s is misaligned\n",
664 top, bottom);
665 }
666}
667EXPORT_SYMBOL(disk_stack_limits);
668
669
670
671
672
673
674
675
676
677
678
679void blk_queue_update_dma_pad(struct request_queue *q, unsigned int mask)
680{
681 if (mask > q->dma_pad_mask)
682 q->dma_pad_mask = mask;
683}
684EXPORT_SYMBOL(blk_queue_update_dma_pad);
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707int blk_queue_dma_drain(struct request_queue *q,
708 dma_drain_needed_fn *dma_drain_needed,
709 void *buf, unsigned int size)
710{
711 if (queue_max_segments(q) < 2)
712 return -EINVAL;
713
714 blk_queue_max_segments(q, queue_max_segments(q) - 1);
715 q->dma_drain_needed = dma_drain_needed;
716 q->dma_drain_buffer = buf;
717 q->dma_drain_size = size;
718
719 return 0;
720}
721EXPORT_SYMBOL_GPL(blk_queue_dma_drain);
722
723
724
725
726
727
728void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
729{
730 if (mask < PAGE_SIZE - 1) {
731 mask = PAGE_SIZE - 1;
732 printk(KERN_INFO "%s: set to minimum %lx\n",
733 __func__, mask);
734 }
735
736 q->limits.seg_boundary_mask = mask;
737}
738EXPORT_SYMBOL(blk_queue_segment_boundary);
739
740
741
742
743
744
745void blk_queue_virt_boundary(struct request_queue *q, unsigned long mask)
746{
747 q->limits.virt_boundary_mask = mask;
748
749
750
751
752
753
754
755 if (mask)
756 q->limits.max_segment_size = UINT_MAX;
757}
758EXPORT_SYMBOL(blk_queue_virt_boundary);
759
760
761
762
763
764
765
766
767
768
769
770void blk_queue_dma_alignment(struct request_queue *q, int mask)
771{
772 q->dma_alignment = mask;
773}
774EXPORT_SYMBOL(blk_queue_dma_alignment);
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
791{
792 BUG_ON(mask > PAGE_SIZE);
793
794 if (mask > q->dma_alignment)
795 q->dma_alignment = mask;
796}
797EXPORT_SYMBOL(blk_queue_update_dma_alignment);
798
799
800
801
802
803
804
805void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
806{
807 q->queue_depth = depth;
808 wbt_set_queue_depth(q, depth);
809}
810EXPORT_SYMBOL(blk_set_queue_depth);
811
812
813
814
815
816
817
818
819
820void blk_queue_write_cache(struct request_queue *q, bool wc, bool fua)
821{
822 if (wc)
823 blk_queue_flag_set(QUEUE_FLAG_WC, q);
824 else
825 blk_queue_flag_clear(QUEUE_FLAG_WC, q);
826 if (fua)
827 blk_queue_flag_set(QUEUE_FLAG_FUA, q);
828 else
829 blk_queue_flag_clear(QUEUE_FLAG_FUA, q);
830
831 wbt_set_write_cache(q, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
832}
833EXPORT_SYMBOL_GPL(blk_queue_write_cache);
834
835static int __init blk_settings_init(void)
836{
837 blk_max_low_pfn = max_low_pfn - 1;
838 blk_max_pfn = max_pfn - 1;
839 return 0;
840}
841subsys_initcall(blk_settings_init);
842