PageRenderTime 79ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 1ms

/Linux/block/blk-settings.c

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