blk-settings.c 27 KB

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