186db1e29SJens Axboe /* 286db1e29SJens Axboe * Functions related to setting various queue properties from drivers 386db1e29SJens Axboe */ 486db1e29SJens Axboe #include <linux/kernel.h> 586db1e29SJens Axboe #include <linux/module.h> 686db1e29SJens Axboe #include <linux/init.h> 786db1e29SJens Axboe #include <linux/bio.h> 886db1e29SJens Axboe #include <linux/blkdev.h> 986db1e29SJens Axboe #include <linux/bootmem.h> /* for max_pfn/max_low_pfn */ 1086db1e29SJens Axboe 1186db1e29SJens Axboe #include "blk.h" 1286db1e29SJens Axboe 136728cb0eSJens Axboe unsigned long blk_max_low_pfn; 1486db1e29SJens Axboe EXPORT_SYMBOL(blk_max_low_pfn); 156728cb0eSJens Axboe 166728cb0eSJens Axboe unsigned long blk_max_pfn; 1786db1e29SJens Axboe 1886db1e29SJens Axboe /** 1986db1e29SJens Axboe * blk_queue_prep_rq - set a prepare_request function for queue 2086db1e29SJens Axboe * @q: queue 2186db1e29SJens Axboe * @pfn: prepare_request function 2286db1e29SJens Axboe * 2386db1e29SJens Axboe * It's possible for a queue to register a prepare_request callback which 2486db1e29SJens Axboe * is invoked before the request is handed to the request_fn. The goal of 2586db1e29SJens Axboe * the function is to prepare a request for I/O, it can be used to build a 2686db1e29SJens Axboe * cdb from the request data for instance. 2786db1e29SJens Axboe * 2886db1e29SJens Axboe */ 2986db1e29SJens Axboe void blk_queue_prep_rq(struct request_queue *q, prep_rq_fn *pfn) 3086db1e29SJens Axboe { 3186db1e29SJens Axboe q->prep_rq_fn = pfn; 3286db1e29SJens Axboe } 3386db1e29SJens Axboe EXPORT_SYMBOL(blk_queue_prep_rq); 3486db1e29SJens Axboe 3586db1e29SJens Axboe /** 36fb2dce86SDavid Woodhouse * blk_queue_set_discard - set a discard_sectors function for queue 37fb2dce86SDavid Woodhouse * @q: queue 38fb2dce86SDavid Woodhouse * @dfn: prepare_discard function 39fb2dce86SDavid Woodhouse * 40fb2dce86SDavid Woodhouse * It's possible for a queue to register a discard callback which is used 41fb2dce86SDavid Woodhouse * to transform a discard request into the appropriate type for the 42fb2dce86SDavid Woodhouse * hardware. If none is registered, then discard requests are failed 43fb2dce86SDavid Woodhouse * with %EOPNOTSUPP. 44fb2dce86SDavid Woodhouse * 45fb2dce86SDavid Woodhouse */ 46fb2dce86SDavid Woodhouse void blk_queue_set_discard(struct request_queue *q, prepare_discard_fn *dfn) 47fb2dce86SDavid Woodhouse { 48fb2dce86SDavid Woodhouse q->prepare_discard_fn = dfn; 49fb2dce86SDavid Woodhouse } 50fb2dce86SDavid Woodhouse EXPORT_SYMBOL(blk_queue_set_discard); 51fb2dce86SDavid Woodhouse 52fb2dce86SDavid Woodhouse /** 5386db1e29SJens Axboe * blk_queue_merge_bvec - set a merge_bvec function for queue 5486db1e29SJens Axboe * @q: queue 5586db1e29SJens Axboe * @mbfn: merge_bvec_fn 5686db1e29SJens Axboe * 5786db1e29SJens Axboe * Usually queues have static limitations on the max sectors or segments that 5886db1e29SJens Axboe * we can put in a request. Stacking drivers may have some settings that 5986db1e29SJens Axboe * are dynamic, and thus we have to query the queue whether it is ok to 6086db1e29SJens Axboe * add a new bio_vec to a bio at a given offset or not. If the block device 6186db1e29SJens Axboe * has such limitations, it needs to register a merge_bvec_fn to control 6286db1e29SJens Axboe * the size of bio's sent to it. Note that a block device *must* allow a 6386db1e29SJens Axboe * single page to be added to an empty bio. The block device driver may want 6486db1e29SJens Axboe * to use the bio_split() function to deal with these bio's. By default 6586db1e29SJens Axboe * no merge_bvec_fn is defined for a queue, and only the fixed limits are 6686db1e29SJens Axboe * honored. 6786db1e29SJens Axboe */ 6886db1e29SJens Axboe void blk_queue_merge_bvec(struct request_queue *q, merge_bvec_fn *mbfn) 6986db1e29SJens Axboe { 7086db1e29SJens Axboe q->merge_bvec_fn = mbfn; 7186db1e29SJens Axboe } 7286db1e29SJens Axboe EXPORT_SYMBOL(blk_queue_merge_bvec); 7386db1e29SJens Axboe 7486db1e29SJens Axboe void blk_queue_softirq_done(struct request_queue *q, softirq_done_fn *fn) 7586db1e29SJens Axboe { 7686db1e29SJens Axboe q->softirq_done_fn = fn; 7786db1e29SJens Axboe } 7886db1e29SJens Axboe EXPORT_SYMBOL(blk_queue_softirq_done); 7986db1e29SJens Axboe 80242f9dcbSJens Axboe void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout) 81242f9dcbSJens Axboe { 82242f9dcbSJens Axboe q->rq_timeout = timeout; 83242f9dcbSJens Axboe } 84242f9dcbSJens Axboe EXPORT_SYMBOL_GPL(blk_queue_rq_timeout); 85242f9dcbSJens Axboe 86242f9dcbSJens Axboe void blk_queue_rq_timed_out(struct request_queue *q, rq_timed_out_fn *fn) 87242f9dcbSJens Axboe { 88242f9dcbSJens Axboe q->rq_timed_out_fn = fn; 89242f9dcbSJens Axboe } 90242f9dcbSJens Axboe EXPORT_SYMBOL_GPL(blk_queue_rq_timed_out); 91242f9dcbSJens Axboe 92ef9e3facSKiyoshi Ueda void blk_queue_lld_busy(struct request_queue *q, lld_busy_fn *fn) 93ef9e3facSKiyoshi Ueda { 94ef9e3facSKiyoshi Ueda q->lld_busy_fn = fn; 95ef9e3facSKiyoshi Ueda } 96ef9e3facSKiyoshi Ueda EXPORT_SYMBOL_GPL(blk_queue_lld_busy); 97ef9e3facSKiyoshi Ueda 9886db1e29SJens Axboe /** 99e475bba2SMartin K. Petersen * blk_set_default_limits - reset limits to default values 100f740f5caSRandy Dunlap * @lim: the queue_limits structure to reset 101e475bba2SMartin K. Petersen * 102e475bba2SMartin K. Petersen * Description: 103e475bba2SMartin K. Petersen * Returns a queue_limit struct to its default state. Can be used by 104e475bba2SMartin K. Petersen * stacking drivers like DM that stage table swaps and reuse an 105e475bba2SMartin K. Petersen * existing device queue. 106e475bba2SMartin K. Petersen */ 107e475bba2SMartin K. Petersen void blk_set_default_limits(struct queue_limits *lim) 108e475bba2SMartin K. Petersen { 109e475bba2SMartin K. Petersen lim->max_phys_segments = MAX_PHYS_SEGMENTS; 110e475bba2SMartin K. Petersen lim->max_hw_segments = MAX_HW_SEGMENTS; 111e475bba2SMartin K. Petersen lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK; 112e475bba2SMartin K. Petersen lim->max_segment_size = MAX_SEGMENT_SIZE; 113e475bba2SMartin K. Petersen lim->max_sectors = lim->max_hw_sectors = SAFE_MAX_SECTORS; 114e475bba2SMartin K. Petersen lim->logical_block_size = lim->physical_block_size = lim->io_min = 512; 1153a02c8e8SMartin K. Petersen lim->bounce_pfn = (unsigned long)(BLK_BOUNCE_ANY >> PAGE_SHIFT); 116e475bba2SMartin K. Petersen lim->alignment_offset = 0; 117e475bba2SMartin K. Petersen lim->io_opt = 0; 118e475bba2SMartin K. Petersen lim->misaligned = 0; 119e475bba2SMartin K. Petersen lim->no_cluster = 0; 120e475bba2SMartin K. Petersen } 121e475bba2SMartin K. Petersen EXPORT_SYMBOL(blk_set_default_limits); 122e475bba2SMartin K. Petersen 123e475bba2SMartin K. Petersen /** 12486db1e29SJens Axboe * blk_queue_make_request - define an alternate make_request function for a device 12586db1e29SJens Axboe * @q: the request queue for the device to be affected 12686db1e29SJens Axboe * @mfn: the alternate make_request function 12786db1e29SJens Axboe * 12886db1e29SJens Axboe * Description: 12986db1e29SJens Axboe * The normal way for &struct bios to be passed to a device 13086db1e29SJens Axboe * driver is for them to be collected into requests on a request 13186db1e29SJens Axboe * queue, and then to allow the device driver to select requests 13286db1e29SJens Axboe * off that queue when it is ready. This works well for many block 13386db1e29SJens Axboe * devices. However some block devices (typically virtual devices 13486db1e29SJens Axboe * such as md or lvm) do not benefit from the processing on the 13586db1e29SJens Axboe * request queue, and are served best by having the requests passed 13686db1e29SJens Axboe * directly to them. This can be achieved by providing a function 13786db1e29SJens Axboe * to blk_queue_make_request(). 13886db1e29SJens Axboe * 13986db1e29SJens Axboe * Caveat: 14086db1e29SJens Axboe * The driver that does this *must* be able to deal appropriately 14186db1e29SJens Axboe * with buffers in "highmemory". This can be accomplished by either calling 14286db1e29SJens Axboe * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling 14386db1e29SJens Axboe * blk_queue_bounce() to create a buffer in normal memory. 14486db1e29SJens Axboe **/ 14586db1e29SJens Axboe void blk_queue_make_request(struct request_queue *q, make_request_fn *mfn) 14686db1e29SJens Axboe { 14786db1e29SJens Axboe /* 14886db1e29SJens Axboe * set defaults 14986db1e29SJens Axboe */ 15086db1e29SJens Axboe q->nr_requests = BLKDEV_MAX_RQ; 1510e435ac2SMilan Broz 15286db1e29SJens Axboe q->make_request_fn = mfn; 15386db1e29SJens Axboe blk_queue_dma_alignment(q, 511); 15486db1e29SJens Axboe blk_queue_congestion_threshold(q); 15586db1e29SJens Axboe q->nr_batching = BLK_BATCH_REQ; 15686db1e29SJens Axboe 15786db1e29SJens Axboe q->unplug_thresh = 4; /* hmm */ 15886db1e29SJens Axboe q->unplug_delay = (3 * HZ) / 1000; /* 3 milliseconds */ 15986db1e29SJens Axboe if (q->unplug_delay == 0) 16086db1e29SJens Axboe q->unplug_delay = 1; 16186db1e29SJens Axboe 16286db1e29SJens Axboe q->unplug_timer.function = blk_unplug_timeout; 16386db1e29SJens Axboe q->unplug_timer.data = (unsigned long)q; 16486db1e29SJens Axboe 165e475bba2SMartin K. Petersen blk_set_default_limits(&q->limits); 166e475bba2SMartin K. Petersen 16786db1e29SJens Axboe /* 168*a4e7d464SJens Axboe * If the caller didn't supply a lock, fall back to our embedded 169*a4e7d464SJens Axboe * per-queue locks 170*a4e7d464SJens Axboe */ 171*a4e7d464SJens Axboe if (!q->queue_lock) 172*a4e7d464SJens Axboe q->queue_lock = &q->__queue_lock; 173*a4e7d464SJens Axboe 174*a4e7d464SJens Axboe /* 17586db1e29SJens Axboe * by default assume old behaviour and bounce for any highmem page 17686db1e29SJens Axboe */ 17786db1e29SJens Axboe blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH); 17886db1e29SJens Axboe } 17986db1e29SJens Axboe EXPORT_SYMBOL(blk_queue_make_request); 18086db1e29SJens Axboe 18186db1e29SJens Axboe /** 18286db1e29SJens Axboe * blk_queue_bounce_limit - set bounce buffer limit for queue 18386db1e29SJens Axboe * @q: the request queue for the device 184cd0aca2dSTejun Heo * @dma_mask: the maximum address the device can handle 18586db1e29SJens Axboe * 18686db1e29SJens Axboe * Description: 18786db1e29SJens Axboe * Different hardware can have different requirements as to what pages 18886db1e29SJens Axboe * it can do I/O directly to. A low level driver can call 18986db1e29SJens Axboe * blk_queue_bounce_limit to have lower memory pages allocated as bounce 190cd0aca2dSTejun Heo * buffers for doing I/O to pages residing above @dma_mask. 19186db1e29SJens Axboe **/ 192cd0aca2dSTejun Heo void blk_queue_bounce_limit(struct request_queue *q, u64 dma_mask) 19386db1e29SJens Axboe { 194cd0aca2dSTejun Heo unsigned long b_pfn = dma_mask >> PAGE_SHIFT; 19586db1e29SJens Axboe int dma = 0; 19686db1e29SJens Axboe 19786db1e29SJens Axboe q->bounce_gfp = GFP_NOIO; 19886db1e29SJens Axboe #if BITS_PER_LONG == 64 199cd0aca2dSTejun Heo /* 200cd0aca2dSTejun Heo * Assume anything <= 4GB can be handled by IOMMU. Actually 201cd0aca2dSTejun Heo * some IOMMUs can handle everything, but I don't know of a 202cd0aca2dSTejun Heo * way to test this here. 203cd0aca2dSTejun Heo */ 204cd0aca2dSTejun Heo if (b_pfn < (min_t(u64, 0xffffffffUL, BLK_BOUNCE_HIGH) >> PAGE_SHIFT)) 20586db1e29SJens Axboe dma = 1; 206025146e1SMartin K. Petersen q->limits.bounce_pfn = max_low_pfn; 20786db1e29SJens Axboe #else 2086728cb0eSJens Axboe if (b_pfn < blk_max_low_pfn) 20986db1e29SJens Axboe dma = 1; 210025146e1SMartin K. Petersen q->limits.bounce_pfn = b_pfn; 21186db1e29SJens Axboe #endif 21286db1e29SJens Axboe if (dma) { 21386db1e29SJens Axboe init_emergency_isa_pool(); 21486db1e29SJens Axboe q->bounce_gfp = GFP_NOIO | GFP_DMA; 215025146e1SMartin K. Petersen q->limits.bounce_pfn = b_pfn; 21686db1e29SJens Axboe } 21786db1e29SJens Axboe } 21886db1e29SJens Axboe EXPORT_SYMBOL(blk_queue_bounce_limit); 21986db1e29SJens Axboe 22086db1e29SJens Axboe /** 22186db1e29SJens Axboe * blk_queue_max_sectors - set max sectors for a request for this queue 22286db1e29SJens Axboe * @q: the request queue for the device 22386db1e29SJens Axboe * @max_sectors: max sectors in the usual 512b unit 22486db1e29SJens Axboe * 22586db1e29SJens Axboe * Description: 22686db1e29SJens Axboe * Enables a low level driver to set an upper limit on the size of 22786db1e29SJens Axboe * received requests. 22886db1e29SJens Axboe **/ 22986db1e29SJens Axboe void blk_queue_max_sectors(struct request_queue *q, unsigned int max_sectors) 23086db1e29SJens Axboe { 23186db1e29SJens Axboe if ((max_sectors << 9) < PAGE_CACHE_SIZE) { 23286db1e29SJens Axboe max_sectors = 1 << (PAGE_CACHE_SHIFT - 9); 23324c03d47SHarvey Harrison printk(KERN_INFO "%s: set to minimum %d\n", 23424c03d47SHarvey Harrison __func__, max_sectors); 23586db1e29SJens Axboe } 23686db1e29SJens Axboe 23786db1e29SJens Axboe if (BLK_DEF_MAX_SECTORS > max_sectors) 238025146e1SMartin K. Petersen q->limits.max_hw_sectors = q->limits.max_sectors = max_sectors; 23986db1e29SJens Axboe else { 240025146e1SMartin K. Petersen q->limits.max_sectors = BLK_DEF_MAX_SECTORS; 241025146e1SMartin K. Petersen q->limits.max_hw_sectors = max_sectors; 24286db1e29SJens Axboe } 24386db1e29SJens Axboe } 24486db1e29SJens Axboe EXPORT_SYMBOL(blk_queue_max_sectors); 24586db1e29SJens Axboe 246ae03bf63SMartin K. Petersen void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_sectors) 247ae03bf63SMartin K. Petersen { 248ae03bf63SMartin K. Petersen if (BLK_DEF_MAX_SECTORS > max_sectors) 249025146e1SMartin K. Petersen q->limits.max_hw_sectors = BLK_DEF_MAX_SECTORS; 250ae03bf63SMartin K. Petersen else 251025146e1SMartin K. Petersen q->limits.max_hw_sectors = max_sectors; 252ae03bf63SMartin K. Petersen } 253ae03bf63SMartin K. Petersen EXPORT_SYMBOL(blk_queue_max_hw_sectors); 254ae03bf63SMartin K. Petersen 25586db1e29SJens Axboe /** 25686db1e29SJens Axboe * blk_queue_max_phys_segments - set max phys segments for a request for this queue 25786db1e29SJens Axboe * @q: the request queue for the device 25886db1e29SJens Axboe * @max_segments: max number of segments 25986db1e29SJens Axboe * 26086db1e29SJens Axboe * Description: 26186db1e29SJens Axboe * Enables a low level driver to set an upper limit on the number of 26286db1e29SJens Axboe * physical data segments in a request. This would be the largest sized 26386db1e29SJens Axboe * scatter list the driver could handle. 26486db1e29SJens Axboe **/ 26586db1e29SJens Axboe void blk_queue_max_phys_segments(struct request_queue *q, 26686db1e29SJens Axboe unsigned short max_segments) 26786db1e29SJens Axboe { 26886db1e29SJens Axboe if (!max_segments) { 26986db1e29SJens Axboe max_segments = 1; 27024c03d47SHarvey Harrison printk(KERN_INFO "%s: set to minimum %d\n", 27124c03d47SHarvey Harrison __func__, max_segments); 27286db1e29SJens Axboe } 27386db1e29SJens Axboe 274025146e1SMartin K. Petersen q->limits.max_phys_segments = max_segments; 27586db1e29SJens Axboe } 27686db1e29SJens Axboe EXPORT_SYMBOL(blk_queue_max_phys_segments); 27786db1e29SJens Axboe 27886db1e29SJens Axboe /** 27986db1e29SJens Axboe * blk_queue_max_hw_segments - set max hw segments for a request for this queue 28086db1e29SJens Axboe * @q: the request queue for the device 28186db1e29SJens Axboe * @max_segments: max number of segments 28286db1e29SJens Axboe * 28386db1e29SJens Axboe * Description: 28486db1e29SJens Axboe * Enables a low level driver to set an upper limit on the number of 28586db1e29SJens Axboe * hw data segments in a request. This would be the largest number of 286710027a4SRandy Dunlap * address/length pairs the host adapter can actually give at once 28786db1e29SJens Axboe * to the device. 28886db1e29SJens Axboe **/ 28986db1e29SJens Axboe void blk_queue_max_hw_segments(struct request_queue *q, 29086db1e29SJens Axboe unsigned short max_segments) 29186db1e29SJens Axboe { 29286db1e29SJens Axboe if (!max_segments) { 29386db1e29SJens Axboe max_segments = 1; 29424c03d47SHarvey Harrison printk(KERN_INFO "%s: set to minimum %d\n", 29524c03d47SHarvey Harrison __func__, max_segments); 29686db1e29SJens Axboe } 29786db1e29SJens Axboe 298025146e1SMartin K. Petersen q->limits.max_hw_segments = max_segments; 29986db1e29SJens Axboe } 30086db1e29SJens Axboe EXPORT_SYMBOL(blk_queue_max_hw_segments); 30186db1e29SJens Axboe 30286db1e29SJens Axboe /** 30386db1e29SJens Axboe * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg 30486db1e29SJens Axboe * @q: the request queue for the device 30586db1e29SJens Axboe * @max_size: max size of segment in bytes 30686db1e29SJens Axboe * 30786db1e29SJens Axboe * Description: 30886db1e29SJens Axboe * Enables a low level driver to set an upper limit on the size of a 30986db1e29SJens Axboe * coalesced segment 31086db1e29SJens Axboe **/ 31186db1e29SJens Axboe void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size) 31286db1e29SJens Axboe { 31386db1e29SJens Axboe if (max_size < PAGE_CACHE_SIZE) { 31486db1e29SJens Axboe max_size = PAGE_CACHE_SIZE; 31524c03d47SHarvey Harrison printk(KERN_INFO "%s: set to minimum %d\n", 31624c03d47SHarvey Harrison __func__, max_size); 31786db1e29SJens Axboe } 31886db1e29SJens Axboe 319025146e1SMartin K. Petersen q->limits.max_segment_size = max_size; 32086db1e29SJens Axboe } 32186db1e29SJens Axboe EXPORT_SYMBOL(blk_queue_max_segment_size); 32286db1e29SJens Axboe 32386db1e29SJens Axboe /** 324e1defc4fSMartin K. Petersen * blk_queue_logical_block_size - set logical block size for the queue 32586db1e29SJens Axboe * @q: the request queue for the device 326e1defc4fSMartin K. Petersen * @size: the logical block size, in bytes 32786db1e29SJens Axboe * 32886db1e29SJens Axboe * Description: 329e1defc4fSMartin K. Petersen * This should be set to the lowest possible block size that the 330e1defc4fSMartin K. Petersen * storage device can address. The default of 512 covers most 331e1defc4fSMartin K. Petersen * hardware. 33286db1e29SJens Axboe **/ 333e1defc4fSMartin K. Petersen void blk_queue_logical_block_size(struct request_queue *q, unsigned short size) 33486db1e29SJens Axboe { 335025146e1SMartin K. Petersen q->limits.logical_block_size = size; 336c72758f3SMartin K. Petersen 337c72758f3SMartin K. Petersen if (q->limits.physical_block_size < size) 338c72758f3SMartin K. Petersen q->limits.physical_block_size = size; 339c72758f3SMartin K. Petersen 340c72758f3SMartin K. Petersen if (q->limits.io_min < q->limits.physical_block_size) 341c72758f3SMartin K. Petersen q->limits.io_min = q->limits.physical_block_size; 34286db1e29SJens Axboe } 343e1defc4fSMartin K. Petersen EXPORT_SYMBOL(blk_queue_logical_block_size); 34486db1e29SJens Axboe 345c72758f3SMartin K. Petersen /** 346c72758f3SMartin K. Petersen * blk_queue_physical_block_size - set physical block size for the queue 347c72758f3SMartin K. Petersen * @q: the request queue for the device 348c72758f3SMartin K. Petersen * @size: the physical block size, in bytes 349c72758f3SMartin K. Petersen * 350c72758f3SMartin K. Petersen * Description: 351c72758f3SMartin K. Petersen * This should be set to the lowest possible sector size that the 352c72758f3SMartin K. Petersen * hardware can operate on without reverting to read-modify-write 353c72758f3SMartin K. Petersen * operations. 354c72758f3SMartin K. Petersen */ 355c72758f3SMartin K. Petersen void blk_queue_physical_block_size(struct request_queue *q, unsigned short size) 356c72758f3SMartin K. Petersen { 357c72758f3SMartin K. Petersen q->limits.physical_block_size = size; 358c72758f3SMartin K. Petersen 359c72758f3SMartin K. Petersen if (q->limits.physical_block_size < q->limits.logical_block_size) 360c72758f3SMartin K. Petersen q->limits.physical_block_size = q->limits.logical_block_size; 361c72758f3SMartin K. Petersen 362c72758f3SMartin K. Petersen if (q->limits.io_min < q->limits.physical_block_size) 363c72758f3SMartin K. Petersen q->limits.io_min = q->limits.physical_block_size; 364c72758f3SMartin K. Petersen } 365c72758f3SMartin K. Petersen EXPORT_SYMBOL(blk_queue_physical_block_size); 366c72758f3SMartin K. Petersen 367c72758f3SMartin K. Petersen /** 368c72758f3SMartin K. Petersen * blk_queue_alignment_offset - set physical block alignment offset 369c72758f3SMartin K. Petersen * @q: the request queue for the device 3708ebf9756SRandy Dunlap * @offset: alignment offset in bytes 371c72758f3SMartin K. Petersen * 372c72758f3SMartin K. Petersen * Description: 373c72758f3SMartin K. Petersen * Some devices are naturally misaligned to compensate for things like 374c72758f3SMartin K. Petersen * the legacy DOS partition table 63-sector offset. Low-level drivers 375c72758f3SMartin K. Petersen * should call this function for devices whose first sector is not 376c72758f3SMartin K. Petersen * naturally aligned. 377c72758f3SMartin K. Petersen */ 378c72758f3SMartin K. Petersen void blk_queue_alignment_offset(struct request_queue *q, unsigned int offset) 379c72758f3SMartin K. Petersen { 380c72758f3SMartin K. Petersen q->limits.alignment_offset = 381c72758f3SMartin K. Petersen offset & (q->limits.physical_block_size - 1); 382c72758f3SMartin K. Petersen q->limits.misaligned = 0; 383c72758f3SMartin K. Petersen } 384c72758f3SMartin K. Petersen EXPORT_SYMBOL(blk_queue_alignment_offset); 385c72758f3SMartin K. Petersen 386c72758f3SMartin K. Petersen /** 387c72758f3SMartin K. Petersen * blk_queue_io_min - set minimum request size for the queue 388c72758f3SMartin K. Petersen * @q: the request queue for the device 3898ebf9756SRandy Dunlap * @min: smallest I/O size in bytes 390c72758f3SMartin K. Petersen * 391c72758f3SMartin K. Petersen * Description: 392c72758f3SMartin K. Petersen * Some devices have an internal block size bigger than the reported 393c72758f3SMartin K. Petersen * hardware sector size. This function can be used to signal the 394c72758f3SMartin K. Petersen * smallest I/O the device can perform without incurring a performance 395c72758f3SMartin K. Petersen * penalty. 396c72758f3SMartin K. Petersen */ 397c72758f3SMartin K. Petersen void blk_queue_io_min(struct request_queue *q, unsigned int min) 398c72758f3SMartin K. Petersen { 399c72758f3SMartin K. Petersen q->limits.io_min = min; 400c72758f3SMartin K. Petersen 401c72758f3SMartin K. Petersen if (q->limits.io_min < q->limits.logical_block_size) 402c72758f3SMartin K. Petersen q->limits.io_min = q->limits.logical_block_size; 403c72758f3SMartin K. Petersen 404c72758f3SMartin K. Petersen if (q->limits.io_min < q->limits.physical_block_size) 405c72758f3SMartin K. Petersen q->limits.io_min = q->limits.physical_block_size; 406c72758f3SMartin K. Petersen } 407c72758f3SMartin K. Petersen EXPORT_SYMBOL(blk_queue_io_min); 408c72758f3SMartin K. Petersen 409c72758f3SMartin K. Petersen /** 410c72758f3SMartin K. Petersen * blk_queue_io_opt - set optimal request size for the queue 411c72758f3SMartin K. Petersen * @q: the request queue for the device 4128ebf9756SRandy Dunlap * @opt: optimal request size in bytes 413c72758f3SMartin K. Petersen * 414c72758f3SMartin K. Petersen * Description: 415c72758f3SMartin K. Petersen * Drivers can call this function to set the preferred I/O request 416c72758f3SMartin K. Petersen * size for devices that report such a value. 417c72758f3SMartin K. Petersen */ 418c72758f3SMartin K. Petersen void blk_queue_io_opt(struct request_queue *q, unsigned int opt) 419c72758f3SMartin K. Petersen { 420c72758f3SMartin K. Petersen q->limits.io_opt = opt; 421c72758f3SMartin K. Petersen } 422c72758f3SMartin K. Petersen EXPORT_SYMBOL(blk_queue_io_opt); 423c72758f3SMartin K. Petersen 42486db1e29SJens Axboe /* 42586db1e29SJens Axboe * Returns the minimum that is _not_ zero, unless both are zero. 42686db1e29SJens Axboe */ 42786db1e29SJens Axboe #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r)) 42886db1e29SJens Axboe 42986db1e29SJens Axboe /** 43086db1e29SJens Axboe * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers 43186db1e29SJens Axboe * @t: the stacking driver (top) 43286db1e29SJens Axboe * @b: the underlying device (bottom) 43386db1e29SJens Axboe **/ 43486db1e29SJens Axboe void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b) 43586db1e29SJens Axboe { 43686db1e29SJens Axboe /* zero is "infinity" */ 437025146e1SMartin K. Petersen t->limits.max_sectors = min_not_zero(queue_max_sectors(t), 438025146e1SMartin K. Petersen queue_max_sectors(b)); 43986db1e29SJens Axboe 440025146e1SMartin K. Petersen t->limits.max_hw_sectors = min_not_zero(queue_max_hw_sectors(t), 441025146e1SMartin K. Petersen queue_max_hw_sectors(b)); 442025146e1SMartin K. Petersen 443025146e1SMartin K. Petersen t->limits.seg_boundary_mask = min_not_zero(queue_segment_boundary(t), 444025146e1SMartin K. Petersen queue_segment_boundary(b)); 445025146e1SMartin K. Petersen 446025146e1SMartin K. Petersen t->limits.max_phys_segments = min_not_zero(queue_max_phys_segments(t), 447025146e1SMartin K. Petersen queue_max_phys_segments(b)); 448025146e1SMartin K. Petersen 449025146e1SMartin K. Petersen t->limits.max_hw_segments = min_not_zero(queue_max_hw_segments(t), 450025146e1SMartin K. Petersen queue_max_hw_segments(b)); 451025146e1SMartin K. Petersen 452025146e1SMartin K. Petersen t->limits.max_segment_size = min_not_zero(queue_max_segment_size(t), 453025146e1SMartin K. Petersen queue_max_segment_size(b)); 454025146e1SMartin K. Petersen 455025146e1SMartin K. Petersen t->limits.logical_block_size = max(queue_logical_block_size(t), 456025146e1SMartin K. Petersen queue_logical_block_size(b)); 457025146e1SMartin K. Petersen 458e7e72bf6SNeil Brown if (!t->queue_lock) 459e7e72bf6SNeil Brown WARN_ON_ONCE(1); 460e7e72bf6SNeil Brown else if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags)) { 461e7e72bf6SNeil Brown unsigned long flags; 462e7e72bf6SNeil Brown spin_lock_irqsave(t->queue_lock, flags); 46375ad23bcSNick Piggin queue_flag_clear(QUEUE_FLAG_CLUSTER, t); 464e7e72bf6SNeil Brown spin_unlock_irqrestore(t->queue_lock, flags); 465e7e72bf6SNeil Brown } 46686db1e29SJens Axboe } 46786db1e29SJens Axboe EXPORT_SYMBOL(blk_queue_stack_limits); 46886db1e29SJens Axboe 46986db1e29SJens Axboe /** 470c72758f3SMartin K. Petersen * blk_stack_limits - adjust queue_limits for stacked devices 471c72758f3SMartin K. Petersen * @t: the stacking driver limits (top) 47277634f33SMartin K. Petersen * @b: the underlying queue limits (bottom) 473c72758f3SMartin K. Petersen * @offset: offset to beginning of data within component device 474c72758f3SMartin K. Petersen * 475c72758f3SMartin K. Petersen * Description: 476c72758f3SMartin K. Petersen * Merges two queue_limit structs. Returns 0 if alignment didn't 477c72758f3SMartin K. Petersen * change. Returns -1 if adding the bottom device caused 478c72758f3SMartin K. Petersen * misalignment. 479c72758f3SMartin K. Petersen */ 480c72758f3SMartin K. Petersen int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, 481c72758f3SMartin K. Petersen sector_t offset) 482c72758f3SMartin K. Petersen { 483c72758f3SMartin K. Petersen t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors); 484c72758f3SMartin K. Petersen t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors); 48577634f33SMartin K. Petersen t->bounce_pfn = min_not_zero(t->bounce_pfn, b->bounce_pfn); 486c72758f3SMartin K. Petersen 487c72758f3SMartin K. Petersen t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask, 488c72758f3SMartin K. Petersen b->seg_boundary_mask); 489c72758f3SMartin K. Petersen 490c72758f3SMartin K. Petersen t->max_phys_segments = min_not_zero(t->max_phys_segments, 491c72758f3SMartin K. Petersen b->max_phys_segments); 492c72758f3SMartin K. Petersen 493c72758f3SMartin K. Petersen t->max_hw_segments = min_not_zero(t->max_hw_segments, 494c72758f3SMartin K. Petersen b->max_hw_segments); 495c72758f3SMartin K. Petersen 496c72758f3SMartin K. Petersen t->max_segment_size = min_not_zero(t->max_segment_size, 497c72758f3SMartin K. Petersen b->max_segment_size); 498c72758f3SMartin K. Petersen 499c72758f3SMartin K. Petersen t->logical_block_size = max(t->logical_block_size, 500c72758f3SMartin K. Petersen b->logical_block_size); 501c72758f3SMartin K. Petersen 502c72758f3SMartin K. Petersen t->physical_block_size = max(t->physical_block_size, 503c72758f3SMartin K. Petersen b->physical_block_size); 504c72758f3SMartin K. Petersen 505c72758f3SMartin K. Petersen t->io_min = max(t->io_min, b->io_min); 506c72758f3SMartin K. Petersen t->no_cluster |= b->no_cluster; 507c72758f3SMartin K. Petersen 508c72758f3SMartin K. Petersen /* Bottom device offset aligned? */ 509c72758f3SMartin K. Petersen if (offset && 510c72758f3SMartin K. Petersen (offset & (b->physical_block_size - 1)) != b->alignment_offset) { 511c72758f3SMartin K. Petersen t->misaligned = 1; 512c72758f3SMartin K. Petersen return -1; 513c72758f3SMartin K. Petersen } 514c72758f3SMartin K. Petersen 515c72758f3SMartin K. Petersen /* If top has no alignment offset, inherit from bottom */ 516c72758f3SMartin K. Petersen if (!t->alignment_offset) 517c72758f3SMartin K. Petersen t->alignment_offset = 518c72758f3SMartin K. Petersen b->alignment_offset & (b->physical_block_size - 1); 519c72758f3SMartin K. Petersen 520c72758f3SMartin K. Petersen /* Top device aligned on logical block boundary? */ 521c72758f3SMartin K. Petersen if (t->alignment_offset & (t->logical_block_size - 1)) { 522c72758f3SMartin K. Petersen t->misaligned = 1; 523c72758f3SMartin K. Petersen return -1; 524c72758f3SMartin K. Petersen } 525c72758f3SMartin K. Petersen 526c72758f3SMartin K. Petersen return 0; 527c72758f3SMartin K. Petersen } 5285d85d324SMike Snitzer EXPORT_SYMBOL(blk_stack_limits); 529c72758f3SMartin K. Petersen 530c72758f3SMartin K. Petersen /** 531c72758f3SMartin K. Petersen * disk_stack_limits - adjust queue limits for stacked drivers 53277634f33SMartin K. Petersen * @disk: MD/DM gendisk (top) 533c72758f3SMartin K. Petersen * @bdev: the underlying block device (bottom) 534c72758f3SMartin K. Petersen * @offset: offset to beginning of data within component device 535c72758f3SMartin K. Petersen * 536c72758f3SMartin K. Petersen * Description: 537c72758f3SMartin K. Petersen * Merges the limits for two queues. Returns 0 if alignment 538c72758f3SMartin K. Petersen * didn't change. Returns -1 if adding the bottom device caused 539c72758f3SMartin K. Petersen * misalignment. 540c72758f3SMartin K. Petersen */ 541c72758f3SMartin K. Petersen void disk_stack_limits(struct gendisk *disk, struct block_device *bdev, 542c72758f3SMartin K. Petersen sector_t offset) 543c72758f3SMartin K. Petersen { 544c72758f3SMartin K. Petersen struct request_queue *t = disk->queue; 545c72758f3SMartin K. Petersen struct request_queue *b = bdev_get_queue(bdev); 546c72758f3SMartin K. Petersen 547c72758f3SMartin K. Petersen offset += get_start_sect(bdev) << 9; 548c72758f3SMartin K. Petersen 549c72758f3SMartin K. Petersen if (blk_stack_limits(&t->limits, &b->limits, offset) < 0) { 550c72758f3SMartin K. Petersen char top[BDEVNAME_SIZE], bottom[BDEVNAME_SIZE]; 551c72758f3SMartin K. Petersen 552c72758f3SMartin K. Petersen disk_name(disk, 0, top); 553c72758f3SMartin K. Petersen bdevname(bdev, bottom); 554c72758f3SMartin K. Petersen 555c72758f3SMartin K. Petersen printk(KERN_NOTICE "%s: Warning: Device %s is misaligned\n", 556c72758f3SMartin K. Petersen top, bottom); 557c72758f3SMartin K. Petersen } 558c72758f3SMartin K. Petersen 559c72758f3SMartin K. Petersen if (!t->queue_lock) 560c72758f3SMartin K. Petersen WARN_ON_ONCE(1); 561c72758f3SMartin K. Petersen else if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags)) { 562c72758f3SMartin K. Petersen unsigned long flags; 563c72758f3SMartin K. Petersen 564c72758f3SMartin K. Petersen spin_lock_irqsave(t->queue_lock, flags); 565c72758f3SMartin K. Petersen if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags)) 566c72758f3SMartin K. Petersen queue_flag_clear(QUEUE_FLAG_CLUSTER, t); 567c72758f3SMartin K. Petersen spin_unlock_irqrestore(t->queue_lock, flags); 568c72758f3SMartin K. Petersen } 569c72758f3SMartin K. Petersen } 570c72758f3SMartin K. Petersen EXPORT_SYMBOL(disk_stack_limits); 571c72758f3SMartin K. Petersen 572c72758f3SMartin K. Petersen /** 573e3790c7dSTejun Heo * blk_queue_dma_pad - set pad mask 574e3790c7dSTejun Heo * @q: the request queue for the device 575e3790c7dSTejun Heo * @mask: pad mask 576e3790c7dSTejun Heo * 57727f8221aSFUJITA Tomonori * Set dma pad mask. 578e3790c7dSTejun Heo * 57927f8221aSFUJITA Tomonori * Appending pad buffer to a request modifies the last entry of a 58027f8221aSFUJITA Tomonori * scatter list such that it includes the pad buffer. 581e3790c7dSTejun Heo **/ 582e3790c7dSTejun Heo void blk_queue_dma_pad(struct request_queue *q, unsigned int mask) 583e3790c7dSTejun Heo { 584e3790c7dSTejun Heo q->dma_pad_mask = mask; 585e3790c7dSTejun Heo } 586e3790c7dSTejun Heo EXPORT_SYMBOL(blk_queue_dma_pad); 587e3790c7dSTejun Heo 588e3790c7dSTejun Heo /** 58927f8221aSFUJITA Tomonori * blk_queue_update_dma_pad - update pad mask 59027f8221aSFUJITA Tomonori * @q: the request queue for the device 59127f8221aSFUJITA Tomonori * @mask: pad mask 59227f8221aSFUJITA Tomonori * 59327f8221aSFUJITA Tomonori * Update dma pad mask. 59427f8221aSFUJITA Tomonori * 59527f8221aSFUJITA Tomonori * Appending pad buffer to a request modifies the last entry of a 59627f8221aSFUJITA Tomonori * scatter list such that it includes the pad buffer. 59727f8221aSFUJITA Tomonori **/ 59827f8221aSFUJITA Tomonori void blk_queue_update_dma_pad(struct request_queue *q, unsigned int mask) 59927f8221aSFUJITA Tomonori { 60027f8221aSFUJITA Tomonori if (mask > q->dma_pad_mask) 60127f8221aSFUJITA Tomonori q->dma_pad_mask = mask; 60227f8221aSFUJITA Tomonori } 60327f8221aSFUJITA Tomonori EXPORT_SYMBOL(blk_queue_update_dma_pad); 60427f8221aSFUJITA Tomonori 60527f8221aSFUJITA Tomonori /** 60686db1e29SJens Axboe * blk_queue_dma_drain - Set up a drain buffer for excess dma. 60786db1e29SJens Axboe * @q: the request queue for the device 6082fb98e84STejun Heo * @dma_drain_needed: fn which returns non-zero if drain is necessary 60986db1e29SJens Axboe * @buf: physically contiguous buffer 61086db1e29SJens Axboe * @size: size of the buffer in bytes 61186db1e29SJens Axboe * 61286db1e29SJens Axboe * Some devices have excess DMA problems and can't simply discard (or 61386db1e29SJens Axboe * zero fill) the unwanted piece of the transfer. They have to have a 61486db1e29SJens Axboe * real area of memory to transfer it into. The use case for this is 61586db1e29SJens Axboe * ATAPI devices in DMA mode. If the packet command causes a transfer 61686db1e29SJens Axboe * bigger than the transfer size some HBAs will lock up if there 61786db1e29SJens Axboe * aren't DMA elements to contain the excess transfer. What this API 61886db1e29SJens Axboe * does is adjust the queue so that the buf is always appended 61986db1e29SJens Axboe * silently to the scatterlist. 62086db1e29SJens Axboe * 62186db1e29SJens Axboe * Note: This routine adjusts max_hw_segments to make room for 62286db1e29SJens Axboe * appending the drain buffer. If you call 62386db1e29SJens Axboe * blk_queue_max_hw_segments() or blk_queue_max_phys_segments() after 62486db1e29SJens Axboe * calling this routine, you must set the limit to one fewer than your 62586db1e29SJens Axboe * device can support otherwise there won't be room for the drain 62686db1e29SJens Axboe * buffer. 62786db1e29SJens Axboe */ 628448da4d2SHarvey Harrison int blk_queue_dma_drain(struct request_queue *q, 6292fb98e84STejun Heo dma_drain_needed_fn *dma_drain_needed, 6302fb98e84STejun Heo void *buf, unsigned int size) 63186db1e29SJens Axboe { 632ae03bf63SMartin K. Petersen if (queue_max_hw_segments(q) < 2 || queue_max_phys_segments(q) < 2) 63386db1e29SJens Axboe return -EINVAL; 63486db1e29SJens Axboe /* make room for appending the drain */ 635ae03bf63SMartin K. Petersen blk_queue_max_hw_segments(q, queue_max_hw_segments(q) - 1); 636ae03bf63SMartin K. Petersen blk_queue_max_phys_segments(q, queue_max_phys_segments(q) - 1); 6372fb98e84STejun Heo q->dma_drain_needed = dma_drain_needed; 63886db1e29SJens Axboe q->dma_drain_buffer = buf; 63986db1e29SJens Axboe q->dma_drain_size = size; 64086db1e29SJens Axboe 64186db1e29SJens Axboe return 0; 64286db1e29SJens Axboe } 64386db1e29SJens Axboe EXPORT_SYMBOL_GPL(blk_queue_dma_drain); 64486db1e29SJens Axboe 64586db1e29SJens Axboe /** 64686db1e29SJens Axboe * blk_queue_segment_boundary - set boundary rules for segment merging 64786db1e29SJens Axboe * @q: the request queue for the device 64886db1e29SJens Axboe * @mask: the memory boundary mask 64986db1e29SJens Axboe **/ 65086db1e29SJens Axboe void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask) 65186db1e29SJens Axboe { 65286db1e29SJens Axboe if (mask < PAGE_CACHE_SIZE - 1) { 65386db1e29SJens Axboe mask = PAGE_CACHE_SIZE - 1; 65424c03d47SHarvey Harrison printk(KERN_INFO "%s: set to minimum %lx\n", 65524c03d47SHarvey Harrison __func__, mask); 65686db1e29SJens Axboe } 65786db1e29SJens Axboe 658025146e1SMartin K. Petersen q->limits.seg_boundary_mask = mask; 65986db1e29SJens Axboe } 66086db1e29SJens Axboe EXPORT_SYMBOL(blk_queue_segment_boundary); 66186db1e29SJens Axboe 66286db1e29SJens Axboe /** 66386db1e29SJens Axboe * blk_queue_dma_alignment - set dma length and memory alignment 66486db1e29SJens Axboe * @q: the request queue for the device 66586db1e29SJens Axboe * @mask: alignment mask 66686db1e29SJens Axboe * 66786db1e29SJens Axboe * description: 668710027a4SRandy Dunlap * set required memory and length alignment for direct dma transactions. 6698feb4d20SAlan Cox * this is used when building direct io requests for the queue. 67086db1e29SJens Axboe * 67186db1e29SJens Axboe **/ 67286db1e29SJens Axboe void blk_queue_dma_alignment(struct request_queue *q, int mask) 67386db1e29SJens Axboe { 67486db1e29SJens Axboe q->dma_alignment = mask; 67586db1e29SJens Axboe } 67686db1e29SJens Axboe EXPORT_SYMBOL(blk_queue_dma_alignment); 67786db1e29SJens Axboe 67886db1e29SJens Axboe /** 67986db1e29SJens Axboe * blk_queue_update_dma_alignment - update dma length and memory alignment 68086db1e29SJens Axboe * @q: the request queue for the device 68186db1e29SJens Axboe * @mask: alignment mask 68286db1e29SJens Axboe * 68386db1e29SJens Axboe * description: 684710027a4SRandy Dunlap * update required memory and length alignment for direct dma transactions. 68586db1e29SJens Axboe * If the requested alignment is larger than the current alignment, then 68686db1e29SJens Axboe * the current queue alignment is updated to the new value, otherwise it 68786db1e29SJens Axboe * is left alone. The design of this is to allow multiple objects 68886db1e29SJens Axboe * (driver, device, transport etc) to set their respective 68986db1e29SJens Axboe * alignments without having them interfere. 69086db1e29SJens Axboe * 69186db1e29SJens Axboe **/ 69286db1e29SJens Axboe void blk_queue_update_dma_alignment(struct request_queue *q, int mask) 69386db1e29SJens Axboe { 69486db1e29SJens Axboe BUG_ON(mask > PAGE_SIZE); 69586db1e29SJens Axboe 69686db1e29SJens Axboe if (mask > q->dma_alignment) 69786db1e29SJens Axboe q->dma_alignment = mask; 69886db1e29SJens Axboe } 69986db1e29SJens Axboe EXPORT_SYMBOL(blk_queue_update_dma_alignment); 70086db1e29SJens Axboe 701aeb3d3a8SHarvey Harrison static int __init blk_settings_init(void) 70286db1e29SJens Axboe { 70386db1e29SJens Axboe blk_max_low_pfn = max_low_pfn - 1; 70486db1e29SJens Axboe blk_max_pfn = max_pfn - 1; 70586db1e29SJens Axboe return 0; 70686db1e29SJens Axboe } 70786db1e29SJens Axboe subsys_initcall(blk_settings_init); 708