blk-settings.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. #include "blk.h"
  15. unsigned long blk_max_low_pfn;
  16. EXPORT_SYMBOL(blk_max_low_pfn);
  17. unsigned long blk_max_pfn;
  18. /**
  19. * blk_queue_prep_rq - set a prepare_request function for queue
  20. * @q: queue
  21. * @pfn: prepare_request function
  22. *
  23. * It's possible for a queue to register a prepare_request callback which
  24. * is invoked before the request is handed to the request_fn. The goal of
  25. * the function is to prepare a request for I/O, it can be used to build a
  26. * cdb from the request data for instance.
  27. *
  28. */
  29. void blk_queue_prep_rq(struct request_queue *q, prep_rq_fn *pfn)
  30. {
  31. q->prep_rq_fn = pfn;
  32. }
  33. EXPORT_SYMBOL(blk_queue_prep_rq);
  34. /**
  35. * blk_queue_unprep_rq - set an unprepare_request function for queue
  36. * @q: queue
  37. * @ufn: unprepare_request function
  38. *
  39. * It's possible for a queue to register an unprepare_request callback
  40. * which is invoked before the request is finally completed. The goal
  41. * of the function is to deallocate any data that was allocated in the
  42. * prepare_request callback.
  43. *
  44. */
  45. void blk_queue_unprep_rq(struct request_queue *q, unprep_rq_fn *ufn)
  46. {
  47. q->unprep_rq_fn = ufn;
  48. }
  49. EXPORT_SYMBOL(blk_queue_unprep_rq);
  50. /**
  51. * blk_queue_merge_bvec - set a merge_bvec function for queue
  52. * @q: queue
  53. * @mbfn: merge_bvec_fn
  54. *
  55. * Usually queues have static limitations on the max sectors or segments that
  56. * we can put in a request. Stacking drivers may have some settings that
  57. * are dynamic, and thus we have to query the queue whether it is ok to
  58. * add a new bio_vec to a bio at a given offset or not. If the block device
  59. * has such limitations, it needs to register a merge_bvec_fn to control
  60. * the size of bio's sent to it. Note that a block device *must* allow a
  61. * single page to be added to an empty bio. The block device driver may want
  62. * to use the bio_split() function to deal with these bio's. By default
  63. * no merge_bvec_fn is defined for a queue, and only the fixed limits are
  64. * honored.
  65. */
  66. void blk_queue_merge_bvec(struct request_queue *q, merge_bvec_fn *mbfn)
  67. {
  68. q->merge_bvec_fn = mbfn;
  69. }
  70. EXPORT_SYMBOL(blk_queue_merge_bvec);
  71. void blk_queue_softirq_done(struct request_queue *q, softirq_done_fn *fn)
  72. {
  73. q->softirq_done_fn = fn;
  74. }
  75. EXPORT_SYMBOL(blk_queue_softirq_done);
  76. void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout)
  77. {
  78. q->rq_timeout = timeout;
  79. }
  80. EXPORT_SYMBOL_GPL(blk_queue_rq_timeout);
  81. void blk_queue_rq_timed_out(struct request_queue *q, rq_timed_out_fn *fn)
  82. {
  83. q->rq_timed_out_fn = fn;
  84. }
  85. EXPORT_SYMBOL_GPL(blk_queue_rq_timed_out);
  86. void blk_queue_lld_busy(struct request_queue *q, lld_busy_fn *fn)
  87. {
  88. q->lld_busy_fn = fn;
  89. }
  90. EXPORT_SYMBOL_GPL(blk_queue_lld_busy);
  91. /**
  92. * blk_set_default_limits - reset limits to default values
  93. * @lim: the queue_limits structure to reset
  94. *
  95. * Description:
  96. * Returns a queue_limit struct to its default state. Can be used by
  97. * stacking drivers like DM that stage table swaps and reuse an
  98. * existing device queue.
  99. */
  100. void blk_set_default_limits(struct queue_limits *lim)
  101. {
  102. lim->max_segments = BLK_MAX_SEGMENTS;
  103. lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
  104. lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
  105. lim->max_sectors = BLK_DEF_MAX_SECTORS;
  106. lim->max_hw_sectors = INT_MAX;
  107. lim->max_discard_sectors = 0;
  108. lim->discard_granularity = 0;
  109. lim->discard_alignment = 0;
  110. lim->discard_misaligned = 0;
  111. lim->discard_zeroes_data = -1;
  112. lim->logical_block_size = lim->physical_block_size = lim->io_min = 512;
  113. lim->bounce_pfn = (unsigned long)(BLK_BOUNCE_ANY >> PAGE_SHIFT);
  114. lim->alignment_offset = 0;
  115. lim->io_opt = 0;
  116. lim->misaligned = 0;
  117. lim->no_cluster = 0;
  118. }
  119. EXPORT_SYMBOL(blk_set_default_limits);
  120. /**
  121. * blk_queue_make_request - define an alternate make_request function for a device
  122. * @q: the request queue for the device to be affected
  123. * @mfn: the alternate make_request function
  124. *
  125. * Description:
  126. * The normal way for &struct bios to be passed to a device
  127. * driver is for them to be collected into requests on a request
  128. * queue, and then to allow the device driver to select requests
  129. * off that queue when it is ready. This works well for many block
  130. * devices. However some block devices (typically virtual devices
  131. * such as md or lvm) do not benefit from the processing on the
  132. * request queue, and are served best by having the requests passed
  133. * directly to them. This can be achieved by providing a function
  134. * to blk_queue_make_request().
  135. *
  136. * Caveat:
  137. * The driver that does this *must* be able to deal appropriately
  138. * with buffers in "highmemory". This can be accomplished by either calling
  139. * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
  140. * blk_queue_bounce() to create a buffer in normal memory.
  141. **/
  142. void blk_queue_make_request(struct request_queue *q, make_request_fn *mfn)
  143. {
  144. /*
  145. * set defaults
  146. */
  147. q->nr_requests = BLKDEV_MAX_RQ;
  148. q->make_request_fn = mfn;
  149. blk_queue_dma_alignment(q, 511);
  150. blk_queue_congestion_threshold(q);
  151. q->nr_batching = BLK_BATCH_REQ;
  152. q->unplug_thresh = 4; /* hmm */
  153. q->unplug_delay = msecs_to_jiffies(3); /* 3 milliseconds */
  154. if (q->unplug_delay == 0)
  155. q->unplug_delay = 1;
  156. q->unplug_timer.function = blk_unplug_timeout;
  157. q->unplug_timer.data = (unsigned long)q;
  158. blk_set_default_limits(&q->limits);
  159. blk_queue_max_hw_sectors(q, BLK_SAFE_MAX_SECTORS);
  160. /*
  161. * If the caller didn't supply a lock, fall back to our embedded
  162. * per-queue locks
  163. */
  164. if (!q->queue_lock)
  165. q->queue_lock = &q->__queue_lock;
  166. /*
  167. * by default assume old behaviour and bounce for any highmem page
  168. */
  169. blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
  170. }
  171. EXPORT_SYMBOL(blk_queue_make_request);
  172. /**
  173. * blk_queue_bounce_limit - set bounce buffer limit for queue
  174. * @q: the request queue for the device
  175. * @dma_mask: the maximum address the device can handle
  176. *
  177. * Description:
  178. * Different hardware can have different requirements as to what pages
  179. * it can do I/O directly to. A low level driver can call
  180. * blk_queue_bounce_limit to have lower memory pages allocated as bounce
  181. * buffers for doing I/O to pages residing above @dma_mask.
  182. **/
  183. void blk_queue_bounce_limit(struct request_queue *q, u64 dma_mask)
  184. {
  185. unsigned long b_pfn = dma_mask >> PAGE_SHIFT;
  186. int dma = 0;
  187. q->bounce_gfp = GFP_NOIO;
  188. #if BITS_PER_LONG == 64
  189. /*
  190. * Assume anything <= 4GB can be handled by IOMMU. Actually
  191. * some IOMMUs can handle everything, but I don't know of a
  192. * way to test this here.
  193. */
  194. if (b_pfn < (min_t(u64, 0xffffffffUL, BLK_BOUNCE_HIGH) >> PAGE_SHIFT))
  195. dma = 1;
  196. q->limits.bounce_pfn = max_low_pfn;
  197. #else
  198. if (b_pfn < blk_max_low_pfn)
  199. dma = 1;
  200. q->limits.bounce_pfn = b_pfn;
  201. #endif
  202. if (dma) {
  203. init_emergency_isa_pool();
  204. q->bounce_gfp = GFP_NOIO | GFP_DMA;
  205. q->limits.bounce_pfn = b_pfn;
  206. }
  207. }
  208. EXPORT_SYMBOL(blk_queue_bounce_limit);
  209. /**
  210. * blk_queue_max_hw_sectors - set max sectors for a request for this queue
  211. * @q: the request queue for the device
  212. * @max_hw_sectors: max hardware sectors in the usual 512b unit
  213. *
  214. * Description:
  215. * Enables a low level driver to set a hard upper limit,
  216. * max_hw_sectors, on the size of requests. max_hw_sectors is set by
  217. * the device driver based upon the combined capabilities of I/O
  218. * controller and storage device.
  219. *
  220. * max_sectors is a soft limit imposed by the block layer for
  221. * filesystem type requests. This value can be overridden on a
  222. * per-device basis in /sys/block/<device>/queue/max_sectors_kb.
  223. * The soft limit can not exceed max_hw_sectors.
  224. **/
  225. void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
  226. {
  227. if ((max_hw_sectors << 9) < PAGE_CACHE_SIZE) {
  228. max_hw_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
  229. printk(KERN_INFO "%s: set to minimum %d\n",
  230. __func__, max_hw_sectors);
  231. }
  232. q->limits.max_hw_sectors = max_hw_sectors;
  233. q->limits.max_sectors = min_t(unsigned int, max_hw_sectors,
  234. BLK_DEF_MAX_SECTORS);
  235. }
  236. EXPORT_SYMBOL(blk_queue_max_hw_sectors);
  237. /**
  238. * blk_queue_max_discard_sectors - set max sectors for a single discard
  239. * @q: the request queue for the device
  240. * @max_discard_sectors: maximum number of sectors to discard
  241. **/
  242. void blk_queue_max_discard_sectors(struct request_queue *q,
  243. unsigned int max_discard_sectors)
  244. {
  245. q->limits.max_discard_sectors = max_discard_sectors;
  246. }
  247. EXPORT_SYMBOL(blk_queue_max_discard_sectors);
  248. /**
  249. * blk_queue_max_segments - set max hw segments for a request for this queue
  250. * @q: the request queue for the device
  251. * @max_segments: max number of segments
  252. *
  253. * Description:
  254. * Enables a low level driver to set an upper limit on the number of
  255. * hw data segments in a request.
  256. **/
  257. void blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
  258. {
  259. if (!max_segments) {
  260. max_segments = 1;
  261. printk(KERN_INFO "%s: set to minimum %d\n",
  262. __func__, max_segments);
  263. }
  264. q->limits.max_segments = max_segments;
  265. }
  266. EXPORT_SYMBOL(blk_queue_max_segments);
  267. /**
  268. * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
  269. * @q: the request queue for the device
  270. * @max_size: max size of segment in bytes
  271. *
  272. * Description:
  273. * Enables a low level driver to set an upper limit on the size of a
  274. * coalesced segment
  275. **/
  276. void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
  277. {
  278. if (max_size < PAGE_CACHE_SIZE) {
  279. max_size = PAGE_CACHE_SIZE;
  280. printk(KERN_INFO "%s: set to minimum %d\n",
  281. __func__, max_size);
  282. }
  283. q->limits.max_segment_size = max_size;
  284. }
  285. EXPORT_SYMBOL(blk_queue_max_segment_size);
  286. /**
  287. * blk_queue_logical_block_size - set logical block size for the queue
  288. * @q: the request queue for the device
  289. * @size: the logical block size, in bytes
  290. *
  291. * Description:
  292. * This should be set to the lowest possible block size that the
  293. * storage device can address. The default of 512 covers most
  294. * hardware.
  295. **/
  296. void blk_queue_logical_block_size(struct request_queue *q, unsigned short size)
  297. {
  298. q->limits.logical_block_size = size;
  299. if (q->limits.physical_block_size < size)
  300. q->limits.physical_block_size = size;
  301. if (q->limits.io_min < q->limits.physical_block_size)
  302. q->limits.io_min = q->limits.physical_block_size;
  303. }
  304. EXPORT_SYMBOL(blk_queue_logical_block_size);
  305. /**
  306. * blk_queue_physical_block_size - set physical block size for the queue
  307. * @q: the request queue for the device
  308. * @size: the physical block size, in bytes
  309. *
  310. * Description:
  311. * This should be set to the lowest possible sector size that the
  312. * hardware can operate on without reverting to read-modify-write
  313. * operations.
  314. */
  315. void blk_queue_physical_block_size(struct request_queue *q, unsigned short size)
  316. {
  317. q->limits.physical_block_size = size;
  318. if (q->limits.physical_block_size < q->limits.logical_block_size)
  319. q->limits.physical_block_size = q->limits.logical_block_size;
  320. if (q->limits.io_min < q->limits.physical_block_size)
  321. q->limits.io_min = q->limits.physical_block_size;
  322. }
  323. EXPORT_SYMBOL(blk_queue_physical_block_size);
  324. /**
  325. * blk_queue_alignment_offset - set physical block alignment offset
  326. * @q: the request queue for the device
  327. * @offset: alignment offset in bytes
  328. *
  329. * Description:
  330. * Some devices are naturally misaligned to compensate for things like
  331. * the legacy DOS partition table 63-sector offset. Low-level drivers
  332. * should call this function for devices whose first sector is not
  333. * naturally aligned.
  334. */
  335. void blk_queue_alignment_offset(struct request_queue *q, unsigned int offset)
  336. {
  337. q->limits.alignment_offset =
  338. offset & (q->limits.physical_block_size - 1);
  339. q->limits.misaligned = 0;
  340. }
  341. EXPORT_SYMBOL(blk_queue_alignment_offset);
  342. /**
  343. * blk_limits_io_min - set minimum request size for a device
  344. * @limits: the queue limits
  345. * @min: smallest I/O size in bytes
  346. *
  347. * Description:
  348. * Some devices have an internal block size bigger than the reported
  349. * hardware sector size. This function can be used to signal the
  350. * smallest I/O the device can perform without incurring a performance
  351. * penalty.
  352. */
  353. void blk_limits_io_min(struct queue_limits *limits, unsigned int min)
  354. {
  355. limits->io_min = min;
  356. if (limits->io_min < limits->logical_block_size)
  357. limits->io_min = limits->logical_block_size;
  358. if (limits->io_min < limits->physical_block_size)
  359. limits->io_min = limits->physical_block_size;
  360. }
  361. EXPORT_SYMBOL(blk_limits_io_min);
  362. /**
  363. * blk_queue_io_min - set minimum request size for the queue
  364. * @q: the request queue for the device
  365. * @min: smallest I/O size in bytes
  366. *
  367. * Description:
  368. * Storage devices may report a granularity or preferred minimum I/O
  369. * size which is the smallest request the device can perform without
  370. * incurring a performance penalty. For disk drives this is often the
  371. * physical block size. For RAID arrays it is often the stripe chunk
  372. * size. A properly aligned multiple of minimum_io_size is the
  373. * preferred request size for workloads where a high number of I/O
  374. * operations is desired.
  375. */
  376. void blk_queue_io_min(struct request_queue *q, unsigned int min)
  377. {
  378. blk_limits_io_min(&q->limits, min);
  379. }
  380. EXPORT_SYMBOL(blk_queue_io_min);
  381. /**
  382. * blk_limits_io_opt - set optimal request size for a device
  383. * @limits: the queue limits
  384. * @opt: smallest I/O size in bytes
  385. *
  386. * Description:
  387. * Storage devices may report an optimal I/O size, which is the
  388. * device's preferred unit for sustained I/O. This is rarely reported
  389. * for disk drives. For RAID arrays it is usually the stripe width or
  390. * the internal track size. A properly aligned multiple of
  391. * optimal_io_size is the preferred request size for workloads where
  392. * sustained throughput is desired.
  393. */
  394. void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt)
  395. {
  396. limits->io_opt = opt;
  397. }
  398. EXPORT_SYMBOL(blk_limits_io_opt);
  399. /**
  400. * blk_queue_io_opt - set optimal request size for the queue
  401. * @q: the request queue for the device
  402. * @opt: optimal request size in bytes
  403. *
  404. * Description:
  405. * Storage devices may report an optimal I/O size, which is the
  406. * device's preferred unit for sustained I/O. This is rarely reported
  407. * for disk drives. For RAID arrays it is usually the stripe width or
  408. * the internal track size. A properly aligned multiple of
  409. * optimal_io_size is the preferred request size for workloads where
  410. * sustained throughput is desired.
  411. */
  412. void blk_queue_io_opt(struct request_queue *q, unsigned int opt)
  413. {
  414. blk_limits_io_opt(&q->limits, opt);
  415. }
  416. EXPORT_SYMBOL(blk_queue_io_opt);
  417. /*
  418. * Returns the minimum that is _not_ zero, unless both are zero.
  419. */
  420. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  421. /**
  422. * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
  423. * @t: the stacking driver (top)
  424. * @b: the underlying device (bottom)
  425. **/
  426. void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b)
  427. {
  428. blk_stack_limits(&t->limits, &b->limits, 0);
  429. if (!t->queue_lock)
  430. WARN_ON_ONCE(1);
  431. else if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags)) {
  432. unsigned long flags;
  433. spin_lock_irqsave(t->queue_lock, flags);
  434. queue_flag_clear(QUEUE_FLAG_CLUSTER, t);
  435. spin_unlock_irqrestore(t->queue_lock, flags);
  436. }
  437. }
  438. EXPORT_SYMBOL(blk_queue_stack_limits);
  439. /**
  440. * blk_stack_limits - adjust queue_limits for stacked devices
  441. * @t: the stacking driver limits (top device)
  442. * @b: the underlying queue limits (bottom, component device)
  443. * @start: first data sector within component device
  444. *
  445. * Description:
  446. * This function is used by stacking drivers like MD and DM to ensure
  447. * that all component devices have compatible block sizes and
  448. * alignments. The stacking driver must provide a queue_limits
  449. * struct (top) and then iteratively call the stacking function for
  450. * all component (bottom) devices. The stacking function will
  451. * attempt to combine the values and ensure proper alignment.
  452. *
  453. * Returns 0 if the top and bottom queue_limits are compatible. The
  454. * top device's block sizes and alignment offsets may be adjusted to
  455. * ensure alignment with the bottom device. If no compatible sizes
  456. * and alignments exist, -1 is returned and the resulting top
  457. * queue_limits will have the misaligned flag set to indicate that
  458. * the alignment_offset is undefined.
  459. */
  460. int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
  461. sector_t start)
  462. {
  463. unsigned int top, bottom, alignment, ret = 0;
  464. t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
  465. t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
  466. t->bounce_pfn = min_not_zero(t->bounce_pfn, b->bounce_pfn);
  467. t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
  468. b->seg_boundary_mask);
  469. t->max_segments = min_not_zero(t->max_segments, b->max_segments);
  470. t->max_segment_size = min_not_zero(t->max_segment_size,
  471. b->max_segment_size);
  472. t->misaligned |= b->misaligned;
  473. alignment = queue_limit_alignment_offset(b, start);
  474. /* Bottom device has different alignment. Check that it is
  475. * compatible with the current top alignment.
  476. */
  477. if (t->alignment_offset != alignment) {
  478. top = max(t->physical_block_size, t->io_min)
  479. + t->alignment_offset;
  480. bottom = max(b->physical_block_size, b->io_min) + alignment;
  481. /* Verify that top and bottom intervals line up */
  482. if (max(top, bottom) & (min(top, bottom) - 1)) {
  483. t->misaligned = 1;
  484. ret = -1;
  485. }
  486. }
  487. t->logical_block_size = max(t->logical_block_size,
  488. b->logical_block_size);
  489. t->physical_block_size = max(t->physical_block_size,
  490. b->physical_block_size);
  491. t->io_min = max(t->io_min, b->io_min);
  492. t->io_opt = lcm(t->io_opt, b->io_opt);
  493. t->no_cluster |= b->no_cluster;
  494. t->discard_zeroes_data &= b->discard_zeroes_data;
  495. /* Physical block size a multiple of the logical block size? */
  496. if (t->physical_block_size & (t->logical_block_size - 1)) {
  497. t->physical_block_size = t->logical_block_size;
  498. t->misaligned = 1;
  499. ret = -1;
  500. }
  501. /* Minimum I/O a multiple of the physical block size? */
  502. if (t->io_min & (t->physical_block_size - 1)) {
  503. t->io_min = t->physical_block_size;
  504. t->misaligned = 1;
  505. ret = -1;
  506. }
  507. /* Optimal I/O a multiple of the physical block size? */
  508. if (t->io_opt & (t->physical_block_size - 1)) {
  509. t->io_opt = 0;
  510. t->misaligned = 1;
  511. ret = -1;
  512. }
  513. /* Find lowest common alignment_offset */
  514. t->alignment_offset = lcm(t->alignment_offset, alignment)
  515. & (max(t->physical_block_size, t->io_min) - 1);
  516. /* Verify that new alignment_offset is on a logical block boundary */
  517. if (t->alignment_offset & (t->logical_block_size - 1)) {
  518. t->misaligned = 1;
  519. ret = -1;
  520. }
  521. /* Discard alignment and granularity */
  522. if (b->discard_granularity) {
  523. alignment = queue_limit_discard_alignment(b, start);
  524. if (t->discard_granularity != 0 &&
  525. t->discard_alignment != alignment) {
  526. top = t->discard_granularity + t->discard_alignment;
  527. bottom = b->discard_granularity + alignment;
  528. /* Verify that top and bottom intervals line up */
  529. if (max(top, bottom) & (min(top, bottom) - 1))
  530. t->discard_misaligned = 1;
  531. }
  532. t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
  533. b->max_discard_sectors);
  534. t->discard_granularity = max(t->discard_granularity,
  535. b->discard_granularity);
  536. t->discard_alignment = lcm(t->discard_alignment, alignment) &
  537. (t->discard_granularity - 1);
  538. }
  539. return ret;
  540. }
  541. EXPORT_SYMBOL(blk_stack_limits);
  542. /**
  543. * bdev_stack_limits - adjust queue limits for stacked drivers
  544. * @t: the stacking driver limits (top device)
  545. * @bdev: the component block_device (bottom)
  546. * @start: first data sector within component device
  547. *
  548. * Description:
  549. * Merges queue limits for a top device and a block_device. Returns
  550. * 0 if alignment didn't change. Returns -1 if adding the bottom
  551. * device caused misalignment.
  552. */
  553. int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev,
  554. sector_t start)
  555. {
  556. struct request_queue *bq = bdev_get_queue(bdev);
  557. start += get_start_sect(bdev);
  558. return blk_stack_limits(t, &bq->limits, start);
  559. }
  560. EXPORT_SYMBOL(bdev_stack_limits);
  561. /**
  562. * disk_stack_limits - adjust queue limits for stacked drivers
  563. * @disk: MD/DM gendisk (top)
  564. * @bdev: the underlying block device (bottom)
  565. * @offset: offset to beginning of data within component device
  566. *
  567. * Description:
  568. * Merges the limits for a top level gendisk and a bottom level
  569. * block_device.
  570. */
  571. void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
  572. sector_t offset)
  573. {
  574. struct request_queue *t = disk->queue;
  575. struct request_queue *b = bdev_get_queue(bdev);
  576. if (bdev_stack_limits(&t->limits, bdev, offset >> 9) < 0) {
  577. char top[BDEVNAME_SIZE], bottom[BDEVNAME_SIZE];
  578. disk_name(disk, 0, top);
  579. bdevname(bdev, bottom);
  580. printk(KERN_NOTICE "%s: Warning: Device %s is misaligned\n",
  581. top, bottom);
  582. }
  583. if (!t->queue_lock)
  584. WARN_ON_ONCE(1);
  585. else if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags)) {
  586. unsigned long flags;
  587. spin_lock_irqsave(t->queue_lock, flags);
  588. if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags))
  589. queue_flag_clear(QUEUE_FLAG_CLUSTER, t);
  590. spin_unlock_irqrestore(t->queue_lock, flags);
  591. }
  592. }
  593. EXPORT_SYMBOL(disk_stack_limits);
  594. /**
  595. * blk_queue_dma_pad - set pad mask
  596. * @q: the request queue for the device
  597. * @mask: pad mask
  598. *
  599. * Set dma pad mask.
  600. *
  601. * Appending pad buffer to a request modifies the last entry of a
  602. * scatter list such that it includes the pad buffer.
  603. **/
  604. void blk_queue_dma_pad(struct request_queue *q, unsigned int mask)
  605. {
  606. q->dma_pad_mask = mask;
  607. }
  608. EXPORT_SYMBOL(blk_queue_dma_pad);
  609. /**
  610. * blk_queue_update_dma_pad - update pad mask
  611. * @q: the request queue for the device
  612. * @mask: pad mask
  613. *
  614. * Update dma pad mask.
  615. *
  616. * Appending pad buffer to a request modifies the last entry of a
  617. * scatter list such that it includes the pad buffer.
  618. **/
  619. void blk_queue_update_dma_pad(struct request_queue *q, unsigned int mask)
  620. {
  621. if (mask > q->dma_pad_mask)
  622. q->dma_pad_mask = mask;
  623. }
  624. EXPORT_SYMBOL(blk_queue_update_dma_pad);
  625. /**
  626. * blk_queue_dma_drain - Set up a drain buffer for excess dma.
  627. * @q: the request queue for the device
  628. * @dma_drain_needed: fn which returns non-zero if drain is necessary
  629. * @buf: physically contiguous buffer
  630. * @size: size of the buffer in bytes
  631. *
  632. * Some devices have excess DMA problems and can't simply discard (or
  633. * zero fill) the unwanted piece of the transfer. They have to have a
  634. * real area of memory to transfer it into. The use case for this is
  635. * ATAPI devices in DMA mode. If the packet command causes a transfer
  636. * bigger than the transfer size some HBAs will lock up if there
  637. * aren't DMA elements to contain the excess transfer. What this API
  638. * does is adjust the queue so that the buf is always appended
  639. * silently to the scatterlist.
  640. *
  641. * Note: This routine adjusts max_hw_segments to make room for appending
  642. * the drain buffer. If you call blk_queue_max_segments() after calling
  643. * this routine, you must set the limit to one fewer than your device
  644. * can support otherwise there won't be room for the drain buffer.
  645. */
  646. int blk_queue_dma_drain(struct request_queue *q,
  647. dma_drain_needed_fn *dma_drain_needed,
  648. void *buf, unsigned int size)
  649. {
  650. if (queue_max_segments(q) < 2)
  651. return -EINVAL;
  652. /* make room for appending the drain */
  653. blk_queue_max_segments(q, queue_max_segments(q) - 1);
  654. q->dma_drain_needed = dma_drain_needed;
  655. q->dma_drain_buffer = buf;
  656. q->dma_drain_size = size;
  657. return 0;
  658. }
  659. EXPORT_SYMBOL_GPL(blk_queue_dma_drain);
  660. /**
  661. * blk_queue_segment_boundary - set boundary rules for segment merging
  662. * @q: the request queue for the device
  663. * @mask: the memory boundary mask
  664. **/
  665. void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
  666. {
  667. if (mask < PAGE_CACHE_SIZE - 1) {
  668. mask = PAGE_CACHE_SIZE - 1;
  669. printk(KERN_INFO "%s: set to minimum %lx\n",
  670. __func__, mask);
  671. }
  672. q->limits.seg_boundary_mask = mask;
  673. }
  674. EXPORT_SYMBOL(blk_queue_segment_boundary);
  675. /**
  676. * blk_queue_dma_alignment - set dma length and memory alignment
  677. * @q: the request queue for the device
  678. * @mask: alignment mask
  679. *
  680. * description:
  681. * set required memory and length alignment for direct dma transactions.
  682. * this is used when building direct io requests for the queue.
  683. *
  684. **/
  685. void blk_queue_dma_alignment(struct request_queue *q, int mask)
  686. {
  687. q->dma_alignment = mask;
  688. }
  689. EXPORT_SYMBOL(blk_queue_dma_alignment);
  690. /**
  691. * blk_queue_update_dma_alignment - update dma length and memory alignment
  692. * @q: the request queue for the device
  693. * @mask: alignment mask
  694. *
  695. * description:
  696. * update required memory and length alignment for direct dma transactions.
  697. * If the requested alignment is larger than the current alignment, then
  698. * the current queue alignment is updated to the new value, otherwise it
  699. * is left alone. The design of this is to allow multiple objects
  700. * (driver, device, transport etc) to set their respective
  701. * alignments without having them interfere.
  702. *
  703. **/
  704. void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
  705. {
  706. BUG_ON(mask > PAGE_SIZE);
  707. if (mask > q->dma_alignment)
  708. q->dma_alignment = mask;
  709. }
  710. EXPORT_SYMBOL(blk_queue_update_dma_alignment);
  711. static int __init blk_settings_init(void)
  712. {
  713. blk_max_low_pfn = max_low_pfn - 1;
  714. blk_max_pfn = max_pfn - 1;
  715. return 0;
  716. }
  717. subsys_initcall(blk_settings_init);