blk-settings.c 25 KB

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