/kernel/2.6.32_froyo_photon_nightly/block/blk-settings.c

http://photon-android.googlecode.com/ · C · 804 lines · 370 code · 87 blank · 347 comment · 42 complexity · 0de0d61f78a5b18f68198b682cee24e8 MD5 · raw file

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