blk-settings.c 23 KB

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