linux/block/blk-settings.c
<<
>>
Prefs
   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
  17unsigned long blk_max_low_pfn;
  18EXPORT_SYMBOL(blk_max_low_pfn);
  19
  20unsigned long blk_max_pfn;
  21
  22/**
  23 * blk_queue_prep_rq - set a prepare_request function for queue
  24 * @q:          queue
  25 * @pfn:        prepare_request function
  26 *
  27 * It's possible for a queue to register a prepare_request callback which
  28 * is invoked before the request is handed to the request_fn. The goal of
  29 * the function is to prepare a request for I/O, it can be used to build a
  30 * cdb from the request data for instance.
  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 * blk_queue_unprep_rq - set an unprepare_request function for queue
  41 * @q:          queue
  42 * @ufn:        unprepare_request function
  43 *
  44 * It's possible for a queue to register an unprepare_request callback
  45 * which is invoked before the request is finally completed. The goal
  46 * of the function is to deallocate any data that was allocated in the
  47 * prepare_request callback.
  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 * blk_queue_merge_bvec - set a merge_bvec function for queue
  58 * @q:          queue
  59 * @mbfn:       merge_bvec_fn
  60 *
  61 * Usually queues have static limitations on the max sectors or segments that
  62 * we can put in a request. Stacking drivers may have some settings that
  63 * are dynamic, and thus we have to query the queue whether it is ok to
  64 * add a new bio_vec to a bio at a given offset or not. If the block device
  65 * has such limitations, it needs to register a merge_bvec_fn to control
  66 * the size of bio's sent to it. Note that a block device *must* allow a
  67 * single page to be added to an empty bio. The block device driver may want
  68 * to use the bio_split() function to deal with these bio's. By default
  69 * no merge_bvec_fn is defined for a queue, and only the fixed limits are
  70 * honored.
  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 * blk_set_default_limits - reset limits to default values
 104 * @lim:  the queue_limits structure to reset
 105 *
 106 * Description:
 107 *   Returns a queue_limit struct to its default state.
 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 * blk_set_stacking_limits - set default limits for stacking devices
 137 * @lim:  the queue_limits structure to reset
 138 *
 139 * Description:
 140 *   Returns a queue_limit struct to its default state. Should be used
 141 *   by stacking drivers like DM that have no internal limits.
 142 */
 143void blk_set_stacking_limits(struct queue_limits *lim)
 144{
 145        blk_set_default_limits(lim);
 146
 147        /* Inherit limits from component devices */
 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 * blk_queue_make_request - define an alternate make_request function for a device
 160 * @q:  the request queue for the device to be affected
 161 * @mfn: the alternate make_request function
 162 *
 163 * Description:
 164 *    The normal way for &struct bios to be passed to a device
 165 *    driver is for them to be collected into requests on a request
 166 *    queue, and then to allow the device driver to select requests
 167 *    off that queue when it is ready.  This works well for many block
 168 *    devices. However some block devices (typically virtual devices
 169 *    such as md or lvm) do not benefit from the processing on the
 170 *    request queue, and are served best by having the requests passed
 171 *    directly to them.  This can be achieved by providing a function
 172 *    to blk_queue_make_request().
 173 *
 174 * Caveat:
 175 *    The driver that does this *must* be able to deal appropriately
 176 *    with buffers in "highmemory". This can be accomplished by either calling
 177 *    __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
 178 *    blk_queue_bounce() to create a buffer in normal memory.
 179 **/
 180void blk_queue_make_request(struct request_queue *q, make_request_fn *mfn)
 181{
 182        /*
 183         * set defaults
 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         * by default assume old behaviour and bounce for any highmem page
 196         */
 197        blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
 198}
 199EXPORT_SYMBOL(blk_queue_make_request);
 200
 201/**
 202 * blk_queue_bounce_limit - set bounce buffer limit for queue
 203 * @q: the request queue for the device
 204 * @dma_mask: the maximum address the device can handle
 205 *
 206 * Description:
 207 *    Different hardware can have different requirements as to what pages
 208 *    it can do I/O directly to. A low level driver can call
 209 *    blk_queue_bounce_limit to have lower memory pages allocated as bounce
 210 *    buffers for doing I/O to pages residing above @dma_mask.
 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         * Assume anything <= 4GB can be handled by IOMMU.  Actually
 221         * some IOMMUs can handle everything, but I don't know of a
 222         * way to test this here.
 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 * blk_limits_max_hw_sectors - set hard and soft limit of max sectors for request
 242 * @limits: the queue limits
 243 * @max_hw_sectors:  max hardware sectors in the usual 512b unit
 244 *
 245 * Description:
 246 *    Enables a low level driver to set a hard upper limit,
 247 *    max_hw_sectors, on the size of requests.  max_hw_sectors is set by
 248 *    the device driver based upon the capabilities of the I/O
 249 *    controller.
 250 *
 251 *    max_dev_sectors is a hard limit imposed by the storage device for
 252 *    READ/WRITE requests. It is set by the disk driver.
 253 *
 254 *    max_sectors is a soft limit imposed by the block layer for
 255 *    filesystem type requests.  This value can be overridden on a
 256 *    per-device basis in /sys/block/<device>/queue/max_sectors_kb.
 257 *    The soft limit can not exceed max_hw_sectors.
 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 * blk_queue_max_hw_sectors - set max sectors for a request for this queue
 278 * @q:  the request queue for the device
 279 * @max_hw_sectors:  max hardware sectors in the usual 512b unit
 280 *
 281 * Description:
 282 *    See description for blk_limits_max_hw_sectors().
 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 * blk_queue_chunk_sectors - set size of the chunk for this queue
 292 * @q:  the request queue for the device
 293 * @chunk_sectors:  chunk sectors in the usual 512b unit
 294 *
 295 * Description:
 296 *    If a driver doesn't want IOs to cross a given chunk size, it can set
 297 *    this limit and prevent merging across chunks. Note that the chunk size
 298 *    must currently be a power-of-2 in sectors. Also note that the block
 299 *    layer must accept a page worth of data at any offset. So if the
 300 *    crossing of chunks is a hard limitation in the driver, it must still be
 301 *    prepared to split single page bios.
 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 * blk_queue_max_discard_sectors - set max sectors for a single discard
 312 * @q:  the request queue for the device
 313 * @max_discard_sectors: maximum number of sectors to discard
 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 * blk_queue_max_write_same_sectors - set max sectors for a single write same
 324 * @q:  the request queue for the device
 325 * @max_write_same_sectors: maximum number of sectors to write per command
 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 * blk_queue_max_segments - set max hw segments for a request for this queue
 336 * @q:  the request queue for the device
 337 * @max_segments:  max number of segments
 338 *
 339 * Description:
 340 *    Enables a low level driver to set an upper limit on the number of
 341 *    hw data segments in a request.
 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 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
 357 * @q:  the request queue for the device
 358 * @max_size:  max size of segment in bytes
 359 *
 360 * Description:
 361 *    Enables a low level driver to set an upper limit on the size of a
 362 *    coalesced segment
 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 * blk_queue_logical_block_size - set logical block size for the queue
 378 * @q:  the request queue for the device
 379 * @size:  the logical block size, in bytes
 380 *
 381 * Description:
 382 *   This should be set to the lowest possible block size that the
 383 *   storage device can address.  The default of 512 covers most
 384 *   hardware.
 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 * blk_queue_physical_block_size - set physical block size for the queue
 400 * @q:  the request queue for the device
 401 * @size:  the physical block size, in bytes
 402 *
 403 * Description:
 404 *   This should be set to the lowest possible sector size that the
 405 *   hardware can operate on without reverting to read-modify-write
 406 *   operations.
 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 * blk_queue_alignment_offset - set physical block alignment offset
 422 * @q:  the request queue for the device
 423 * @offset: alignment offset in bytes
 424 *
 425 * Description:
 426 *   Some devices are naturally misaligned to compensate for things like
 427 *   the legacy DOS partition table 63-sector offset.  Low-level drivers
 428 *   should call this function for devices whose first sector is not
 429 *   naturally aligned.
 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 * blk_limits_io_min - set minimum request size for a device
 441 * @limits: the queue limits
 442 * @min:  smallest I/O size in bytes
 443 *
 444 * Description:
 445 *   Some devices have an internal block size bigger than the reported
 446 *   hardware sector size.  This function can be used to signal the
 447 *   smallest I/O the device can perform without incurring a performance
 448 *   penalty.
 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 * blk_queue_io_min - set minimum request size for the queue
 464 * @q:  the request queue for the device
 465 * @min:  smallest I/O size in bytes
 466 *
 467 * Description:
 468 *   Storage devices may report a granularity or preferred minimum I/O
 469 *   size which is the smallest request the device can perform without
 470 *   incurring a performance penalty.  For disk drives this is often the
 471 *   physical block size.  For RAID arrays it is often the stripe chunk
 472 *   size.  A properly aligned multiple of minimum_io_size is the
 473 *   preferred request size for workloads where a high number of I/O
 474 *   operations is desired.
 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 * blk_limits_io_opt - set optimal request size for a device
 484 * @limits: the queue limits
 485 * @opt:  smallest I/O size in bytes
 486 *
 487 * Description:
 488 *   Storage devices may report an optimal I/O size, which is the
 489 *   device's preferred unit for sustained I/O.  This is rarely reported
 490 *   for disk drives.  For RAID arrays it is usually the stripe width or
 491 *   the internal track size.  A properly aligned multiple of
 492 *   optimal_io_size is the preferred request size for workloads where
 493 *   sustained throughput is desired.
 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 * blk_queue_io_opt - set optimal request size for the queue
 503 * @q:  the request queue for the device
 504 * @opt:  optimal request size in bytes
 505 *
 506 * Description:
 507 *   Storage devices may report an optimal I/O size, which is the
 508 *   device's preferred unit for sustained I/O.  This is rarely reported
 509 *   for disk drives.  For RAID arrays it is usually the stripe width or
 510 *   the internal track size.  A properly aligned multiple of
 511 *   optimal_io_size is the preferred request size for workloads where
 512 *   sustained throughput is desired.
 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 * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
 522 * @t:  the stacking driver (top)
 523 * @b:  the underlying device (bottom)
 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 * blk_stack_limits - adjust queue_limits for stacked devices
 533 * @t:  the stacking driver limits (top device)
 534 * @b:  the underlying queue limits (bottom, component device)
 535 * @start:  first data sector within component device
 536 *
 537 * Description:
 538 *    This function is used by stacking drivers like MD and DM to ensure
 539 *    that all component devices have compatible block sizes and
 540 *    alignments.  The stacking driver must provide a queue_limits
 541 *    struct (top) and then iteratively call the stacking function for
 542 *    all component (bottom) devices.  The stacking function will
 543 *    attempt to combine the values and ensure proper alignment.
 544 *
 545 *    Returns 0 if the top and bottom queue_limits are compatible.  The
 546 *    top device's block sizes and alignment offsets may be adjusted to
 547 *    ensure alignment with the bottom device. If no compatible sizes
 548 *    and alignments exist, -1 is returned and the resulting top
 549 *    queue_limits will have the misaligned flag set to indicate that
 550 *    the alignment_offset is undefined.
 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        /* Bottom device has different alignment.  Check that it is
 582         * compatible with the current top alignment.
 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                /* Verify that top and bottom intervals line up */
 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        /* Physical block size a multiple of the logical block size? */
 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        /* Minimum I/O a multiple of the physical block size? */
 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        /* Optimal I/O a multiple of the physical block size? */
 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        /* Find lowest common alignment_offset */
 631        t->alignment_offset = lcm(t->alignment_offset, alignment)
 632                % max(t->physical_block_size, t->io_min);
 633
 634        /* Verify that new alignment_offset is on a logical block boundary */
 635        if (t->alignment_offset & (t->logical_block_size - 1)) {
 636                t->misaligned = 1;
 637                ret = -1;
 638        }
 639
 640        /* Discard alignment and granularity */
 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                        /* Verify that top and bottom intervals line up */
 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 * bdev_stack_limits - adjust queue limits for stacked drivers
 672 * @t:  the stacking driver limits (top device)
 673 * @bdev:  the component block_device (bottom)
 674 * @start:  first data sector within component device
 675 *
 676 * Description:
 677 *    Merges queue limits for a top device and a block_device.  Returns
 678 *    0 if alignment didn't change.  Returns -1 if adding the bottom
 679 *    device caused misalignment.
 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 * disk_stack_limits - adjust queue limits for stacked drivers
 694 * @disk:  MD/DM gendisk (top)
 695 * @bdev:  the underlying block device (bottom)
 696 * @offset:  offset to beginning of data within component device
 697 *
 698 * Description:
 699 *    Merges the limits for a top level gendisk and a bottom level
 700 *    block_device.
 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 * blk_queue_dma_pad - set pad mask
 721 * @q:     the request queue for the device
 722 * @mask:  pad mask
 723 *
 724 * Set dma pad mask.
 725 *
 726 * Appending pad buffer to a request modifies the last entry of a
 727 * scatter list such that it includes the pad buffer.
 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 * blk_queue_update_dma_pad - update pad mask
 737 * @q:     the request queue for the device
 738 * @mask:  pad mask
 739 *
 740 * Update dma pad mask.
 741 *
 742 * Appending pad buffer to a request modifies the last entry of a
 743 * scatter list such that it includes the pad buffer.
 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 * blk_queue_dma_drain - Set up a drain buffer for excess dma.
 754 * @q:  the request queue for the device
 755 * @dma_drain_needed: fn which returns non-zero if drain is necessary
 756 * @buf:        physically contiguous buffer
 757 * @size:       size of the buffer in bytes
 758 *
 759 * Some devices have excess DMA problems and can't simply discard (or
 760 * zero fill) the unwanted piece of the transfer.  They have to have a
 761 * real area of memory to transfer it into.  The use case for this is
 762 * ATAPI devices in DMA mode.  If the packet command causes a transfer
 763 * bigger than the transfer size some HBAs will lock up if there
 764 * aren't DMA elements to contain the excess transfer.  What this API
 765 * does is adjust the queue so that the buf is always appended
 766 * silently to the scatterlist.
 767 *
 768 * Note: This routine adjusts max_hw_segments to make room for appending
 769 * the drain buffer.  If you call blk_queue_max_segments() after calling
 770 * this routine, you must set the limit to one fewer than your device
 771 * can support otherwise there won't be room for the drain buffer.
 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        /* make room for appending the drain */
 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 * blk_queue_segment_boundary - set boundary rules for segment merging
 791 * @q:  the request queue for the device
 792 * @mask:  the memory boundary mask
 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 * blk_queue_virt_boundary - set boundary rules for bio merging
 808 * @q:  the request queue for the device
 809 * @mask:  the memory boundary mask
 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 * blk_queue_dma_alignment - set dma length and memory alignment
 822 * @q:     the request queue for the device
 823 * @mask:  alignment mask
 824 *
 825 * description:
 826 *    set required memory and length alignment for direct dma transactions.
 827 *    this is used when building direct io requests for the queue.
 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 * blk_queue_update_dma_alignment - update dma length and memory alignment
 838 * @q:     the request queue for the device
 839 * @mask:  alignment mask
 840 *
 841 * description:
 842 *    update required memory and length alignment for direct dma transactions.
 843 *    If the requested alignment is larger than the current alignment, then
 844 *    the current queue alignment is updated to the new value, otherwise it
 845 *    is left alone.  The design of this is to allow multiple objects
 846 *    (driver, device, transport etc) to set their respective
 847 *    alignments without having them interfere.
 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 * blk_queue_flush - configure queue's cache flush capability
 861 * @q:          the request queue for the device
 862 * @flush:      0, REQ_FLUSH or REQ_FLUSH | REQ_FUA
 863 *
 864 * Tell block layer cache flush capability of @q.  If it supports
 865 * flushing, REQ_FLUSH should be set.  If it supports bypassing
 866 * write cache for individual writes, REQ_FUA should be set.
 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 * blk_set_queue_depth - tell the block layer about the device queue depth
 887 * @q:          the request queue for the device
 888 * @depth:              queue depth
 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