blk-settings.c 21 KB

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