blk-settings.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. /**
  73. * blk_queue_make_request - define an alternate make_request function for a device
  74. * @q: the request queue for the device to be affected
  75. * @mfn: the alternate make_request function
  76. *
  77. * Description:
  78. * The normal way for &struct bios to be passed to a device
  79. * driver is for them to be collected into requests on a request
  80. * queue, and then to allow the device driver to select requests
  81. * off that queue when it is ready. This works well for many block
  82. * devices. However some block devices (typically virtual devices
  83. * such as md or lvm) do not benefit from the processing on the
  84. * request queue, and are served best by having the requests passed
  85. * directly to them. This can be achieved by providing a function
  86. * to blk_queue_make_request().
  87. *
  88. * Caveat:
  89. * The driver that does this *must* be able to deal appropriately
  90. * with buffers in "highmemory". This can be accomplished by either calling
  91. * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
  92. * blk_queue_bounce() to create a buffer in normal memory.
  93. **/
  94. void blk_queue_make_request(struct request_queue *q, make_request_fn *mfn)
  95. {
  96. /*
  97. * set defaults
  98. */
  99. q->nr_requests = BLKDEV_MAX_RQ;
  100. blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
  101. blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
  102. q->make_request_fn = mfn;
  103. q->backing_dev_info.ra_pages =
  104. (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  105. q->backing_dev_info.state = 0;
  106. q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
  107. blk_queue_max_sectors(q, SAFE_MAX_SECTORS);
  108. blk_queue_hardsect_size(q, 512);
  109. blk_queue_dma_alignment(q, 511);
  110. blk_queue_congestion_threshold(q);
  111. q->nr_batching = BLK_BATCH_REQ;
  112. q->unplug_thresh = 4; /* hmm */
  113. q->unplug_delay = (3 * HZ) / 1000; /* 3 milliseconds */
  114. if (q->unplug_delay == 0)
  115. q->unplug_delay = 1;
  116. INIT_WORK(&q->unplug_work, blk_unplug_work);
  117. q->unplug_timer.function = blk_unplug_timeout;
  118. q->unplug_timer.data = (unsigned long)q;
  119. /*
  120. * by default assume old behaviour and bounce for any highmem page
  121. */
  122. blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
  123. }
  124. EXPORT_SYMBOL(blk_queue_make_request);
  125. /**
  126. * blk_queue_bounce_limit - set bounce buffer limit for queue
  127. * @q: the request queue for the device
  128. * @dma_addr: bus address limit
  129. *
  130. * Description:
  131. * Different hardware can have different requirements as to what pages
  132. * it can do I/O directly to. A low level driver can call
  133. * blk_queue_bounce_limit to have lower memory pages allocated as bounce
  134. * buffers for doing I/O to pages residing above @dma_addr.
  135. **/
  136. void blk_queue_bounce_limit(struct request_queue *q, u64 dma_addr)
  137. {
  138. unsigned long b_pfn = dma_addr >> PAGE_SHIFT;
  139. int dma = 0;
  140. q->bounce_gfp = GFP_NOIO;
  141. #if BITS_PER_LONG == 64
  142. /* Assume anything <= 4GB can be handled by IOMMU.
  143. Actually some IOMMUs can handle everything, but I don't
  144. know of a way to test this here. */
  145. if (b_pfn < (min_t(u64, 0x100000000UL, BLK_BOUNCE_HIGH) >> PAGE_SHIFT))
  146. dma = 1;
  147. q->bounce_pfn = max_low_pfn;
  148. #else
  149. if (b_pfn < blk_max_low_pfn)
  150. dma = 1;
  151. q->bounce_pfn = b_pfn;
  152. #endif
  153. if (dma) {
  154. init_emergency_isa_pool();
  155. q->bounce_gfp = GFP_NOIO | GFP_DMA;
  156. q->bounce_pfn = b_pfn;
  157. }
  158. }
  159. EXPORT_SYMBOL(blk_queue_bounce_limit);
  160. /**
  161. * blk_queue_max_sectors - set max sectors for a request for this queue
  162. * @q: the request queue for the device
  163. * @max_sectors: max sectors in the usual 512b unit
  164. *
  165. * Description:
  166. * Enables a low level driver to set an upper limit on the size of
  167. * received requests.
  168. **/
  169. void blk_queue_max_sectors(struct request_queue *q, unsigned int max_sectors)
  170. {
  171. if ((max_sectors << 9) < PAGE_CACHE_SIZE) {
  172. max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
  173. printk(KERN_INFO "%s: set to minimum %d\n",
  174. __func__, max_sectors);
  175. }
  176. if (BLK_DEF_MAX_SECTORS > max_sectors)
  177. q->max_hw_sectors = q->max_sectors = max_sectors;
  178. else {
  179. q->max_sectors = BLK_DEF_MAX_SECTORS;
  180. q->max_hw_sectors = max_sectors;
  181. }
  182. }
  183. EXPORT_SYMBOL(blk_queue_max_sectors);
  184. /**
  185. * blk_queue_max_phys_segments - set max phys segments for a request for this queue
  186. * @q: the request queue for the device
  187. * @max_segments: max number of segments
  188. *
  189. * Description:
  190. * Enables a low level driver to set an upper limit on the number of
  191. * physical data segments in a request. This would be the largest sized
  192. * scatter list the driver could handle.
  193. **/
  194. void blk_queue_max_phys_segments(struct request_queue *q,
  195. unsigned short max_segments)
  196. {
  197. if (!max_segments) {
  198. max_segments = 1;
  199. printk(KERN_INFO "%s: set to minimum %d\n",
  200. __func__, max_segments);
  201. }
  202. q->max_phys_segments = max_segments;
  203. }
  204. EXPORT_SYMBOL(blk_queue_max_phys_segments);
  205. /**
  206. * blk_queue_max_hw_segments - set max hw segments for a request for this queue
  207. * @q: the request queue for the device
  208. * @max_segments: max number of segments
  209. *
  210. * Description:
  211. * Enables a low level driver to set an upper limit on the number of
  212. * hw data segments in a request. This would be the largest number of
  213. * address/length pairs the host adapter can actually give at once
  214. * to the device.
  215. **/
  216. void blk_queue_max_hw_segments(struct request_queue *q,
  217. unsigned short max_segments)
  218. {
  219. if (!max_segments) {
  220. max_segments = 1;
  221. printk(KERN_INFO "%s: set to minimum %d\n",
  222. __func__, max_segments);
  223. }
  224. q->max_hw_segments = max_segments;
  225. }
  226. EXPORT_SYMBOL(blk_queue_max_hw_segments);
  227. /**
  228. * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
  229. * @q: the request queue for the device
  230. * @max_size: max size of segment in bytes
  231. *
  232. * Description:
  233. * Enables a low level driver to set an upper limit on the size of a
  234. * coalesced segment
  235. **/
  236. void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
  237. {
  238. if (max_size < PAGE_CACHE_SIZE) {
  239. max_size = PAGE_CACHE_SIZE;
  240. printk(KERN_INFO "%s: set to minimum %d\n",
  241. __func__, max_size);
  242. }
  243. q->max_segment_size = max_size;
  244. }
  245. EXPORT_SYMBOL(blk_queue_max_segment_size);
  246. /**
  247. * blk_queue_hardsect_size - set hardware sector size for the queue
  248. * @q: the request queue for the device
  249. * @size: the hardware sector size, in bytes
  250. *
  251. * Description:
  252. * This should typically be set to the lowest possible sector size
  253. * that the hardware can operate on (possible without reverting to
  254. * even internal read-modify-write operations). Usually the default
  255. * of 512 covers most hardware.
  256. **/
  257. void blk_queue_hardsect_size(struct request_queue *q, unsigned short size)
  258. {
  259. q->hardsect_size = size;
  260. }
  261. EXPORT_SYMBOL(blk_queue_hardsect_size);
  262. /*
  263. * Returns the minimum that is _not_ zero, unless both are zero.
  264. */
  265. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  266. /**
  267. * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
  268. * @t: the stacking driver (top)
  269. * @b: the underlying device (bottom)
  270. **/
  271. void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b)
  272. {
  273. /* zero is "infinity" */
  274. t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
  275. t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
  276. t->max_phys_segments = min(t->max_phys_segments, b->max_phys_segments);
  277. t->max_hw_segments = min(t->max_hw_segments, b->max_hw_segments);
  278. t->max_segment_size = min(t->max_segment_size, b->max_segment_size);
  279. t->hardsect_size = max(t->hardsect_size, b->hardsect_size);
  280. if (!t->queue_lock)
  281. WARN_ON_ONCE(1);
  282. else if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags)) {
  283. unsigned long flags;
  284. spin_lock_irqsave(t->queue_lock, flags);
  285. queue_flag_clear(QUEUE_FLAG_CLUSTER, t);
  286. spin_unlock_irqrestore(t->queue_lock, flags);
  287. }
  288. }
  289. EXPORT_SYMBOL(blk_queue_stack_limits);
  290. /**
  291. * blk_queue_dma_pad - set pad mask
  292. * @q: the request queue for the device
  293. * @mask: pad mask
  294. *
  295. * Set dma pad mask.
  296. *
  297. * Appending pad buffer to a request modifies the last entry of a
  298. * scatter list such that it includes the pad buffer.
  299. **/
  300. void blk_queue_dma_pad(struct request_queue *q, unsigned int mask)
  301. {
  302. q->dma_pad_mask = mask;
  303. }
  304. EXPORT_SYMBOL(blk_queue_dma_pad);
  305. /**
  306. * blk_queue_update_dma_pad - update pad mask
  307. * @q: the request queue for the device
  308. * @mask: pad mask
  309. *
  310. * Update dma pad mask.
  311. *
  312. * Appending pad buffer to a request modifies the last entry of a
  313. * scatter list such that it includes the pad buffer.
  314. **/
  315. void blk_queue_update_dma_pad(struct request_queue *q, unsigned int mask)
  316. {
  317. if (mask > q->dma_pad_mask)
  318. q->dma_pad_mask = mask;
  319. }
  320. EXPORT_SYMBOL(blk_queue_update_dma_pad);
  321. /**
  322. * blk_queue_dma_drain - Set up a drain buffer for excess dma.
  323. * @q: the request queue for the device
  324. * @dma_drain_needed: fn which returns non-zero if drain is necessary
  325. * @buf: physically contiguous buffer
  326. * @size: size of the buffer in bytes
  327. *
  328. * Some devices have excess DMA problems and can't simply discard (or
  329. * zero fill) the unwanted piece of the transfer. They have to have a
  330. * real area of memory to transfer it into. The use case for this is
  331. * ATAPI devices in DMA mode. If the packet command causes a transfer
  332. * bigger than the transfer size some HBAs will lock up if there
  333. * aren't DMA elements to contain the excess transfer. What this API
  334. * does is adjust the queue so that the buf is always appended
  335. * silently to the scatterlist.
  336. *
  337. * Note: This routine adjusts max_hw_segments to make room for
  338. * appending the drain buffer. If you call
  339. * blk_queue_max_hw_segments() or blk_queue_max_phys_segments() after
  340. * calling this routine, you must set the limit to one fewer than your
  341. * device can support otherwise there won't be room for the drain
  342. * buffer.
  343. */
  344. int blk_queue_dma_drain(struct request_queue *q,
  345. dma_drain_needed_fn *dma_drain_needed,
  346. void *buf, unsigned int size)
  347. {
  348. if (q->max_hw_segments < 2 || q->max_phys_segments < 2)
  349. return -EINVAL;
  350. /* make room for appending the drain */
  351. --q->max_hw_segments;
  352. --q->max_phys_segments;
  353. q->dma_drain_needed = dma_drain_needed;
  354. q->dma_drain_buffer = buf;
  355. q->dma_drain_size = size;
  356. return 0;
  357. }
  358. EXPORT_SYMBOL_GPL(blk_queue_dma_drain);
  359. /**
  360. * blk_queue_segment_boundary - set boundary rules for segment merging
  361. * @q: the request queue for the device
  362. * @mask: the memory boundary mask
  363. **/
  364. void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
  365. {
  366. if (mask < PAGE_CACHE_SIZE - 1) {
  367. mask = PAGE_CACHE_SIZE - 1;
  368. printk(KERN_INFO "%s: set to minimum %lx\n",
  369. __func__, mask);
  370. }
  371. q->seg_boundary_mask = mask;
  372. }
  373. EXPORT_SYMBOL(blk_queue_segment_boundary);
  374. /**
  375. * blk_queue_dma_alignment - set dma length and memory alignment
  376. * @q: the request queue for the device
  377. * @mask: alignment mask
  378. *
  379. * description:
  380. * set required memory and length alignment for direct dma transactions.
  381. * this is used when buiding direct io requests for the queue.
  382. *
  383. **/
  384. void blk_queue_dma_alignment(struct request_queue *q, int mask)
  385. {
  386. q->dma_alignment = mask;
  387. }
  388. EXPORT_SYMBOL(blk_queue_dma_alignment);
  389. /**
  390. * blk_queue_update_dma_alignment - update dma length and memory alignment
  391. * @q: the request queue for the device
  392. * @mask: alignment mask
  393. *
  394. * description:
  395. * update required memory and length alignment for direct dma transactions.
  396. * If the requested alignment is larger than the current alignment, then
  397. * the current queue alignment is updated to the new value, otherwise it
  398. * is left alone. The design of this is to allow multiple objects
  399. * (driver, device, transport etc) to set their respective
  400. * alignments without having them interfere.
  401. *
  402. **/
  403. void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
  404. {
  405. BUG_ON(mask > PAGE_SIZE);
  406. if (mask > q->dma_alignment)
  407. q->dma_alignment = mask;
  408. }
  409. EXPORT_SYMBOL(blk_queue_update_dma_alignment);
  410. int __init blk_settings_init(void)
  411. {
  412. blk_max_low_pfn = max_low_pfn - 1;
  413. blk_max_pfn = max_pfn - 1;
  414. return 0;
  415. }
  416. subsys_initcall(blk_settings_init);