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