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