blk-settings.c 26 KB

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