blk-settings.c 25 KB

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