blk-settings.c 22 KB

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