blk-settings.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Functions related to setting various queue properties from drivers
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/bio.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/memblock.h> /* for max_pfn/max_low_pfn */
  11. #include <linux/gcd.h>
  12. #include <linux/lcm.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/gfp.h>
  15. #include <linux/dma-mapping.h>
  16. #include "blk.h"
  17. #include "blk-wbt.h"
  18. unsigned long blk_max_low_pfn;
  19. EXPORT_SYMBOL(blk_max_low_pfn);
  20. unsigned long blk_max_pfn;
  21. void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout)
  22. {
  23. q->rq_timeout = timeout;
  24. }
  25. EXPORT_SYMBOL_GPL(blk_queue_rq_timeout);
  26. /**
  27. * blk_set_default_limits - reset limits to default values
  28. * @lim: the queue_limits structure to reset
  29. *
  30. * Description:
  31. * Returns a queue_limit struct to its default state.
  32. */
  33. void blk_set_default_limits(struct queue_limits *lim)
  34. {
  35. lim->max_segments = BLK_MAX_SEGMENTS;
  36. lim->max_discard_segments = 1;
  37. lim->max_integrity_segments = 0;
  38. lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
  39. lim->virt_boundary_mask = 0;
  40. lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
  41. lim->max_sectors = lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS;
  42. lim->max_dev_sectors = 0;
  43. lim->chunk_sectors = 0;
  44. lim->max_write_same_sectors = 0;
  45. lim->max_write_zeroes_sectors = 0;
  46. lim->max_zone_append_sectors = 0;
  47. lim->max_discard_sectors = 0;
  48. lim->max_hw_discard_sectors = 0;
  49. lim->discard_granularity = 0;
  50. lim->discard_alignment = 0;
  51. lim->discard_misaligned = 0;
  52. lim->logical_block_size = lim->physical_block_size = lim->io_min = 512;
  53. lim->bounce_pfn = (unsigned long)(BLK_BOUNCE_ANY >> PAGE_SHIFT);
  54. lim->alignment_offset = 0;
  55. lim->io_opt = 0;
  56. lim->misaligned = 0;
  57. lim->zoned = BLK_ZONED_NONE;
  58. }
  59. EXPORT_SYMBOL(blk_set_default_limits);
  60. /**
  61. * blk_set_stacking_limits - set default limits for stacking devices
  62. * @lim: the queue_limits structure to reset
  63. *
  64. * Description:
  65. * Returns a queue_limit struct to its default state. Should be used
  66. * by stacking drivers like DM that have no internal limits.
  67. */
  68. void blk_set_stacking_limits(struct queue_limits *lim)
  69. {
  70. blk_set_default_limits(lim);
  71. /* Inherit limits from component devices */
  72. lim->max_segments = USHRT_MAX;
  73. lim->max_discard_segments = USHRT_MAX;
  74. lim->max_hw_sectors = UINT_MAX;
  75. lim->max_segment_size = UINT_MAX;
  76. lim->max_sectors = UINT_MAX;
  77. lim->max_dev_sectors = UINT_MAX;
  78. lim->max_write_same_sectors = UINT_MAX;
  79. lim->max_write_zeroes_sectors = UINT_MAX;
  80. lim->max_zone_append_sectors = UINT_MAX;
  81. }
  82. EXPORT_SYMBOL(blk_set_stacking_limits);
  83. /**
  84. * blk_queue_bounce_limit - set bounce buffer limit for queue
  85. * @q: the request queue for the device
  86. * @max_addr: the maximum address the device can handle
  87. *
  88. * Description:
  89. * Different hardware can have different requirements as to what pages
  90. * it can do I/O directly to. A low level driver can call
  91. * blk_queue_bounce_limit to have lower memory pages allocated as bounce
  92. * buffers for doing I/O to pages residing above @max_addr.
  93. **/
  94. void blk_queue_bounce_limit(struct request_queue *q, u64 max_addr)
  95. {
  96. unsigned long b_pfn = max_addr >> PAGE_SHIFT;
  97. int dma = 0;
  98. q->bounce_gfp = GFP_NOIO;
  99. #if BITS_PER_LONG == 64
  100. /*
  101. * Assume anything <= 4GB can be handled by IOMMU. Actually
  102. * some IOMMUs can handle everything, but I don't know of a
  103. * way to test this here.
  104. */
  105. if (b_pfn < (min_t(u64, 0xffffffffUL, BLK_BOUNCE_HIGH) >> PAGE_SHIFT))
  106. dma = 1;
  107. q->limits.bounce_pfn = max(max_low_pfn, b_pfn);
  108. #else
  109. if (b_pfn < blk_max_low_pfn)
  110. dma = 1;
  111. q->limits.bounce_pfn = b_pfn;
  112. #endif
  113. if (dma) {
  114. init_emergency_isa_pool();
  115. q->bounce_gfp = GFP_NOIO | GFP_DMA;
  116. q->limits.bounce_pfn = b_pfn;
  117. }
  118. }
  119. EXPORT_SYMBOL(blk_queue_bounce_limit);
  120. /**
  121. * blk_queue_max_hw_sectors - set max sectors for a request for this queue
  122. * @q: the request queue for the device
  123. * @max_hw_sectors: max hardware sectors in the usual 512b unit
  124. *
  125. * Description:
  126. * Enables a low level driver to set a hard upper limit,
  127. * max_hw_sectors, on the size of requests. max_hw_sectors is set by
  128. * the device driver based upon the capabilities of the I/O
  129. * controller.
  130. *
  131. * max_dev_sectors is a hard limit imposed by the storage device for
  132. * READ/WRITE requests. It is set by the disk driver.
  133. *
  134. * max_sectors is a soft limit imposed by the block layer for
  135. * filesystem type requests. This value can be overridden on a
  136. * per-device basis in /sys/block/<device>/queue/max_sectors_kb.
  137. * The soft limit can not exceed max_hw_sectors.
  138. **/
  139. void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
  140. {
  141. struct queue_limits *limits = &q->limits;
  142. unsigned int max_sectors;
  143. if ((max_hw_sectors << 9) < PAGE_SIZE) {
  144. max_hw_sectors = 1 << (PAGE_SHIFT - 9);
  145. printk(KERN_INFO "%s: set to minimum %d\n",
  146. __func__, max_hw_sectors);
  147. }
  148. limits->max_hw_sectors = max_hw_sectors;
  149. max_sectors = min_not_zero(max_hw_sectors, limits->max_dev_sectors);
  150. max_sectors = min_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
  151. limits->max_sectors = max_sectors;
  152. q->backing_dev_info->io_pages = max_sectors >> (PAGE_SHIFT - 9);
  153. }
  154. EXPORT_SYMBOL(blk_queue_max_hw_sectors);
  155. /**
  156. * blk_queue_chunk_sectors - set size of the chunk for this queue
  157. * @q: the request queue for the device
  158. * @chunk_sectors: chunk sectors in the usual 512b unit
  159. *
  160. * Description:
  161. * If a driver doesn't want IOs to cross a given chunk size, it can set
  162. * this limit and prevent merging across chunks. Note that the block layer
  163. * must accept a page worth of data at any offset. So if the crossing of
  164. * chunks is a hard limitation in the driver, it must still be prepared
  165. * to split single page bios.
  166. **/
  167. void blk_queue_chunk_sectors(struct request_queue *q, unsigned int chunk_sectors)
  168. {
  169. q->limits.chunk_sectors = chunk_sectors;
  170. }
  171. EXPORT_SYMBOL(blk_queue_chunk_sectors);
  172. /**
  173. * blk_queue_max_discard_sectors - set max sectors for a single discard
  174. * @q: the request queue for the device
  175. * @max_discard_sectors: maximum number of sectors to discard
  176. **/
  177. void blk_queue_max_discard_sectors(struct request_queue *q,
  178. unsigned int max_discard_sectors)
  179. {
  180. q->limits.max_hw_discard_sectors = max_discard_sectors;
  181. q->limits.max_discard_sectors = max_discard_sectors;
  182. }
  183. EXPORT_SYMBOL(blk_queue_max_discard_sectors);
  184. /**
  185. * blk_queue_max_write_same_sectors - set max sectors for a single write same
  186. * @q: the request queue for the device
  187. * @max_write_same_sectors: maximum number of sectors to write per command
  188. **/
  189. void blk_queue_max_write_same_sectors(struct request_queue *q,
  190. unsigned int max_write_same_sectors)
  191. {
  192. q->limits.max_write_same_sectors = max_write_same_sectors;
  193. }
  194. EXPORT_SYMBOL(blk_queue_max_write_same_sectors);
  195. /**
  196. * blk_queue_max_write_zeroes_sectors - set max sectors for a single
  197. * write zeroes
  198. * @q: the request queue for the device
  199. * @max_write_zeroes_sectors: maximum number of sectors to write per command
  200. **/
  201. void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
  202. unsigned int max_write_zeroes_sectors)
  203. {
  204. q->limits.max_write_zeroes_sectors = max_write_zeroes_sectors;
  205. }
  206. EXPORT_SYMBOL(blk_queue_max_write_zeroes_sectors);
  207. /**
  208. * blk_queue_max_zone_append_sectors - set max sectors for a single zone append
  209. * @q: the request queue for the device
  210. * @max_zone_append_sectors: maximum number of sectors to write per command
  211. **/
  212. void blk_queue_max_zone_append_sectors(struct request_queue *q,
  213. unsigned int max_zone_append_sectors)
  214. {
  215. unsigned int max_sectors;
  216. if (WARN_ON(!blk_queue_is_zoned(q)))
  217. return;
  218. max_sectors = min(q->limits.max_hw_sectors, max_zone_append_sectors);
  219. max_sectors = min(q->limits.chunk_sectors, max_sectors);
  220. /*
  221. * Signal eventual driver bugs resulting in the max_zone_append sectors limit
  222. * being 0 due to a 0 argument, the chunk_sectors limit (zone size) not set,
  223. * or the max_hw_sectors limit not set.
  224. */
  225. WARN_ON(!max_sectors);
  226. q->limits.max_zone_append_sectors = max_sectors;
  227. }
  228. EXPORT_SYMBOL_GPL(blk_queue_max_zone_append_sectors);
  229. /**
  230. * blk_queue_max_segments - set max hw segments for a request for this queue
  231. * @q: the request queue for the device
  232. * @max_segments: max number of segments
  233. *
  234. * Description:
  235. * Enables a low level driver to set an upper limit on the number of
  236. * hw data segments in a request.
  237. **/
  238. void blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
  239. {
  240. if (!max_segments) {
  241. max_segments = 1;
  242. printk(KERN_INFO "%s: set to minimum %d\n",
  243. __func__, max_segments);
  244. }
  245. q->limits.max_segments = max_segments;
  246. }
  247. EXPORT_SYMBOL(blk_queue_max_segments);
  248. /**
  249. * blk_queue_max_discard_segments - set max segments for discard requests
  250. * @q: the request queue for the device
  251. * @max_segments: max number of segments
  252. *
  253. * Description:
  254. * Enables a low level driver to set an upper limit on the number of
  255. * segments in a discard request.
  256. **/
  257. void blk_queue_max_discard_segments(struct request_queue *q,
  258. unsigned short max_segments)
  259. {
  260. q->limits.max_discard_segments = max_segments;
  261. }
  262. EXPORT_SYMBOL_GPL(blk_queue_max_discard_segments);
  263. /**
  264. * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
  265. * @q: the request queue for the device
  266. * @max_size: max size of segment in bytes
  267. *
  268. * Description:
  269. * Enables a low level driver to set an upper limit on the size of a
  270. * coalesced segment
  271. **/
  272. void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
  273. {
  274. if (max_size < PAGE_SIZE) {
  275. max_size = PAGE_SIZE;
  276. printk(KERN_INFO "%s: set to minimum %d\n",
  277. __func__, max_size);
  278. }
  279. /* see blk_queue_virt_boundary() for the explanation */
  280. WARN_ON_ONCE(q->limits.virt_boundary_mask);
  281. q->limits.max_segment_size = max_size;
  282. }
  283. EXPORT_SYMBOL(blk_queue_max_segment_size);
  284. /**
  285. * blk_queue_logical_block_size - set logical block size for the queue
  286. * @q: the request queue for the device
  287. * @size: the logical block size, in bytes
  288. *
  289. * Description:
  290. * This should be set to the lowest possible block size that the
  291. * storage device can address. The default of 512 covers most
  292. * hardware.
  293. **/
  294. void blk_queue_logical_block_size(struct request_queue *q, unsigned int size)
  295. {
  296. q->limits.logical_block_size = size;
  297. if (q->limits.physical_block_size < size)
  298. q->limits.physical_block_size = size;
  299. if (q->limits.io_min < q->limits.physical_block_size)
  300. q->limits.io_min = q->limits.physical_block_size;
  301. }
  302. EXPORT_SYMBOL(blk_queue_logical_block_size);
  303. /**
  304. * blk_queue_physical_block_size - set physical block size for the queue
  305. * @q: the request queue for the device
  306. * @size: the physical block size, in bytes
  307. *
  308. * Description:
  309. * This should be set to the lowest possible sector size that the
  310. * hardware can operate on without reverting to read-modify-write
  311. * operations.
  312. */
  313. void blk_queue_physical_block_size(struct request_queue *q, unsigned int size)
  314. {
  315. q->limits.physical_block_size = size;
  316. if (q->limits.physical_block_size < q->limits.logical_block_size)
  317. q->limits.physical_block_size = q->limits.logical_block_size;
  318. if (q->limits.io_min < q->limits.physical_block_size)
  319. q->limits.io_min = q->limits.physical_block_size;
  320. }
  321. EXPORT_SYMBOL(blk_queue_physical_block_size);
  322. /**
  323. * blk_queue_alignment_offset - set physical block alignment offset
  324. * @q: the request queue for the device
  325. * @offset: alignment offset in bytes
  326. *
  327. * Description:
  328. * Some devices are naturally misaligned to compensate for things like
  329. * the legacy DOS partition table 63-sector offset. Low-level drivers
  330. * should call this function for devices whose first sector is not
  331. * naturally aligned.
  332. */
  333. void blk_queue_alignment_offset(struct request_queue *q, unsigned int offset)
  334. {
  335. q->limits.alignment_offset =
  336. offset & (q->limits.physical_block_size - 1);
  337. q->limits.misaligned = 0;
  338. }
  339. EXPORT_SYMBOL(blk_queue_alignment_offset);
  340. void blk_queue_update_readahead(struct request_queue *q)
  341. {
  342. /*
  343. * For read-ahead of large files to be effective, we need to read ahead
  344. * at least twice the optimal I/O size.
  345. */
  346. q->backing_dev_info->ra_pages =
  347. max(queue_io_opt(q) * 2 / PAGE_SIZE, VM_READAHEAD_PAGES);
  348. q->backing_dev_info->io_pages =
  349. queue_max_sectors(q) >> (PAGE_SHIFT - 9);
  350. }
  351. EXPORT_SYMBOL_GPL(blk_queue_update_readahead);
  352. /**
  353. * blk_limits_io_min - set minimum request size for a device
  354. * @limits: the queue limits
  355. * @min: smallest I/O size in bytes
  356. *
  357. * Description:
  358. * Some devices have an internal block size bigger than the reported
  359. * hardware sector size. This function can be used to signal the
  360. * smallest I/O the device can perform without incurring a performance
  361. * penalty.
  362. */
  363. void blk_limits_io_min(struct queue_limits *limits, unsigned int min)
  364. {
  365. limits->io_min = min;
  366. if (limits->io_min < limits->logical_block_size)
  367. limits->io_min = limits->logical_block_size;
  368. if (limits->io_min < limits->physical_block_size)
  369. limits->io_min = limits->physical_block_size;
  370. }
  371. EXPORT_SYMBOL(blk_limits_io_min);
  372. /**
  373. * blk_queue_io_min - set minimum request size for the queue
  374. * @q: the request queue for the device
  375. * @min: smallest I/O size in bytes
  376. *
  377. * Description:
  378. * Storage devices may report a granularity or preferred minimum I/O
  379. * size which is the smallest request the device can perform without
  380. * incurring a performance penalty. For disk drives this is often the
  381. * physical block size. For RAID arrays it is often the stripe chunk
  382. * size. A properly aligned multiple of minimum_io_size is the
  383. * preferred request size for workloads where a high number of I/O
  384. * operations is desired.
  385. */
  386. void blk_queue_io_min(struct request_queue *q, unsigned int min)
  387. {
  388. blk_limits_io_min(&q->limits, min);
  389. }
  390. EXPORT_SYMBOL(blk_queue_io_min);
  391. /**
  392. * blk_limits_io_opt - set optimal request size for a device
  393. * @limits: the queue limits
  394. * @opt: smallest I/O size in bytes
  395. *
  396. * Description:
  397. * Storage devices may report an optimal I/O size, which is the
  398. * device's preferred unit for sustained I/O. This is rarely reported
  399. * for disk drives. For RAID arrays it is usually the stripe width or
  400. * the internal track size. A properly aligned multiple of
  401. * optimal_io_size is the preferred request size for workloads where
  402. * sustained throughput is desired.
  403. */
  404. void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt)
  405. {
  406. limits->io_opt = opt;
  407. }
  408. EXPORT_SYMBOL(blk_limits_io_opt);
  409. /**
  410. * blk_queue_io_opt - set optimal request size for the queue
  411. * @q: the request queue for the device
  412. * @opt: optimal request 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_queue_io_opt(struct request_queue *q, unsigned int opt)
  423. {
  424. blk_limits_io_opt(&q->limits, opt);
  425. q->backing_dev_info->ra_pages =
  426. max(queue_io_opt(q) * 2 / PAGE_SIZE, VM_READAHEAD_PAGES);
  427. }
  428. EXPORT_SYMBOL(blk_queue_io_opt);
  429. static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs)
  430. {
  431. sectors = round_down(sectors, lbs >> SECTOR_SHIFT);
  432. if (sectors < PAGE_SIZE >> SECTOR_SHIFT)
  433. sectors = PAGE_SIZE >> SECTOR_SHIFT;
  434. return sectors;
  435. }
  436. /**
  437. * blk_stack_limits - adjust queue_limits for stacked devices
  438. * @t: the stacking driver limits (top device)
  439. * @b: the underlying queue limits (bottom, component device)
  440. * @start: first data sector within component device
  441. *
  442. * Description:
  443. * This function is used by stacking drivers like MD and DM to ensure
  444. * that all component devices have compatible block sizes and
  445. * alignments. The stacking driver must provide a queue_limits
  446. * struct (top) and then iteratively call the stacking function for
  447. * all component (bottom) devices. The stacking function will
  448. * attempt to combine the values and ensure proper alignment.
  449. *
  450. * Returns 0 if the top and bottom queue_limits are compatible. The
  451. * top device's block sizes and alignment offsets may be adjusted to
  452. * ensure alignment with the bottom device. If no compatible sizes
  453. * and alignments exist, -1 is returned and the resulting top
  454. * queue_limits will have the misaligned flag set to indicate that
  455. * the alignment_offset is undefined.
  456. */
  457. int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
  458. sector_t start)
  459. {
  460. unsigned int top, bottom, alignment, ret = 0;
  461. t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
  462. t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
  463. t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
  464. t->max_write_same_sectors = min(t->max_write_same_sectors,
  465. b->max_write_same_sectors);
  466. t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors,
  467. b->max_write_zeroes_sectors);
  468. t->max_zone_append_sectors = min(t->max_zone_append_sectors,
  469. b->max_zone_append_sectors);
  470. t->bounce_pfn = min_not_zero(t->bounce_pfn, b->bounce_pfn);
  471. t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
  472. b->seg_boundary_mask);
  473. t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask,
  474. b->virt_boundary_mask);
  475. t->max_segments = min_not_zero(t->max_segments, b->max_segments);
  476. t->max_discard_segments = min_not_zero(t->max_discard_segments,
  477. b->max_discard_segments);
  478. t->max_integrity_segments = min_not_zero(t->max_integrity_segments,
  479. b->max_integrity_segments);
  480. t->max_segment_size = min_not_zero(t->max_segment_size,
  481. b->max_segment_size);
  482. t->misaligned |= b->misaligned;
  483. alignment = queue_limit_alignment_offset(b, start);
  484. /* Bottom device has different alignment. Check that it is
  485. * compatible with the current top alignment.
  486. */
  487. if (t->alignment_offset != alignment) {
  488. top = max(t->physical_block_size, t->io_min)
  489. + t->alignment_offset;
  490. bottom = max(b->physical_block_size, b->io_min) + alignment;
  491. /* Verify that top and bottom intervals line up */
  492. if (max(top, bottom) % min(top, bottom)) {
  493. t->misaligned = 1;
  494. ret = -1;
  495. }
  496. }
  497. t->logical_block_size = max(t->logical_block_size,
  498. b->logical_block_size);
  499. t->physical_block_size = max(t->physical_block_size,
  500. b->physical_block_size);
  501. t->io_min = max(t->io_min, b->io_min);
  502. t->io_opt = lcm_not_zero(t->io_opt, b->io_opt);
  503. /* Set non-power-of-2 compatible chunk_sectors boundary */
  504. if (b->chunk_sectors)
  505. t->chunk_sectors = gcd(t->chunk_sectors, b->chunk_sectors);
  506. /* Physical block size a multiple of the logical block size? */
  507. if (t->physical_block_size & (t->logical_block_size - 1)) {
  508. t->physical_block_size = t->logical_block_size;
  509. t->misaligned = 1;
  510. ret = -1;
  511. }
  512. /* Minimum I/O a multiple of the physical block size? */
  513. if (t->io_min & (t->physical_block_size - 1)) {
  514. t->io_min = t->physical_block_size;
  515. t->misaligned = 1;
  516. ret = -1;
  517. }
  518. /* Optimal I/O a multiple of the physical block size? */
  519. if (t->io_opt & (t->physical_block_size - 1)) {
  520. t->io_opt = 0;
  521. t->misaligned = 1;
  522. ret = -1;
  523. }
  524. /* chunk_sectors a multiple of the physical block size? */
  525. if ((t->chunk_sectors << 9) & (t->physical_block_size - 1)) {
  526. t->chunk_sectors = 0;
  527. t->misaligned = 1;
  528. ret = -1;
  529. }
  530. t->raid_partial_stripes_expensive =
  531. max(t->raid_partial_stripes_expensive,
  532. b->raid_partial_stripes_expensive);
  533. /* Find lowest common alignment_offset */
  534. t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment)
  535. % max(t->physical_block_size, t->io_min);
  536. /* Verify that new alignment_offset is on a logical block boundary */
  537. if (t->alignment_offset & (t->logical_block_size - 1)) {
  538. t->misaligned = 1;
  539. ret = -1;
  540. }
  541. t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size);
  542. t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size);
  543. t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size);
  544. /* Discard alignment and granularity */
  545. if (b->discard_granularity) {
  546. alignment = queue_limit_discard_alignment(b, start);
  547. if (t->discard_granularity != 0 &&
  548. t->discard_alignment != alignment) {
  549. top = t->discard_granularity + t->discard_alignment;
  550. bottom = b->discard_granularity + alignment;
  551. /* Verify that top and bottom intervals line up */
  552. if ((max(top, bottom) % min(top, bottom)) != 0)
  553. t->discard_misaligned = 1;
  554. }
  555. t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
  556. b->max_discard_sectors);
  557. t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
  558. b->max_hw_discard_sectors);
  559. t->discard_granularity = max(t->discard_granularity,
  560. b->discard_granularity);
  561. t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
  562. t->discard_granularity;
  563. }
  564. t->zoned = max(t->zoned, b->zoned);
  565. return ret;
  566. }
  567. EXPORT_SYMBOL(blk_stack_limits);
  568. /**
  569. * disk_stack_limits - adjust queue limits for stacked drivers
  570. * @disk: MD/DM gendisk (top)
  571. * @bdev: the underlying block device (bottom)
  572. * @offset: offset to beginning of data within component device
  573. *
  574. * Description:
  575. * Merges the limits for a top level gendisk and a bottom level
  576. * block_device.
  577. */
  578. void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
  579. sector_t offset)
  580. {
  581. struct request_queue *t = disk->queue;
  582. if (blk_stack_limits(&t->limits, &bdev_get_queue(bdev)->limits,
  583. get_start_sect(bdev) + (offset >> 9)) < 0) {
  584. char top[BDEVNAME_SIZE], bottom[BDEVNAME_SIZE];
  585. disk_name(disk, 0, top);
  586. bdevname(bdev, bottom);
  587. printk(KERN_NOTICE "%s: Warning: Device %s is misaligned\n",
  588. top, bottom);
  589. }
  590. blk_queue_update_readahead(disk->queue);
  591. }
  592. EXPORT_SYMBOL(disk_stack_limits);
  593. /**
  594. * blk_queue_update_dma_pad - update pad mask
  595. * @q: the request queue for the device
  596. * @mask: pad mask
  597. *
  598. * Update dma pad mask.
  599. *
  600. * Appending pad buffer to a request modifies the last entry of a
  601. * scatter list such that it includes the pad buffer.
  602. **/
  603. void blk_queue_update_dma_pad(struct request_queue *q, unsigned int mask)
  604. {
  605. if (mask > q->dma_pad_mask)
  606. q->dma_pad_mask = mask;
  607. }
  608. EXPORT_SYMBOL(blk_queue_update_dma_pad);
  609. /**
  610. * blk_queue_segment_boundary - set boundary rules for segment merging
  611. * @q: the request queue for the device
  612. * @mask: the memory boundary mask
  613. **/
  614. void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
  615. {
  616. if (mask < PAGE_SIZE - 1) {
  617. mask = PAGE_SIZE - 1;
  618. printk(KERN_INFO "%s: set to minimum %lx\n",
  619. __func__, mask);
  620. }
  621. q->limits.seg_boundary_mask = mask;
  622. }
  623. EXPORT_SYMBOL(blk_queue_segment_boundary);
  624. /**
  625. * blk_queue_virt_boundary - set boundary rules for bio merging
  626. * @q: the request queue for the device
  627. * @mask: the memory boundary mask
  628. **/
  629. void blk_queue_virt_boundary(struct request_queue *q, unsigned long mask)
  630. {
  631. q->limits.virt_boundary_mask = mask;
  632. /*
  633. * Devices that require a virtual boundary do not support scatter/gather
  634. * I/O natively, but instead require a descriptor list entry for each
  635. * page (which might not be idential to the Linux PAGE_SIZE). Because
  636. * of that they are not limited by our notion of "segment size".
  637. */
  638. if (mask)
  639. q->limits.max_segment_size = UINT_MAX;
  640. }
  641. EXPORT_SYMBOL(blk_queue_virt_boundary);
  642. /**
  643. * blk_queue_dma_alignment - set dma length and memory alignment
  644. * @q: the request queue for the device
  645. * @mask: alignment mask
  646. *
  647. * description:
  648. * set required memory and length alignment for direct dma transactions.
  649. * this is used when building direct io requests for the queue.
  650. *
  651. **/
  652. void blk_queue_dma_alignment(struct request_queue *q, int mask)
  653. {
  654. q->dma_alignment = mask;
  655. }
  656. EXPORT_SYMBOL(blk_queue_dma_alignment);
  657. /**
  658. * blk_queue_update_dma_alignment - update dma length and memory alignment
  659. * @q: the request queue for the device
  660. * @mask: alignment mask
  661. *
  662. * description:
  663. * update required memory and length alignment for direct dma transactions.
  664. * If the requested alignment is larger than the current alignment, then
  665. * the current queue alignment is updated to the new value, otherwise it
  666. * is left alone. The design of this is to allow multiple objects
  667. * (driver, device, transport etc) to set their respective
  668. * alignments without having them interfere.
  669. *
  670. **/
  671. void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
  672. {
  673. BUG_ON(mask > PAGE_SIZE);
  674. if (mask > q->dma_alignment)
  675. q->dma_alignment = mask;
  676. }
  677. EXPORT_SYMBOL(blk_queue_update_dma_alignment);
  678. /**
  679. * blk_set_queue_depth - tell the block layer about the device queue depth
  680. * @q: the request queue for the device
  681. * @depth: queue depth
  682. *
  683. */
  684. void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
  685. {
  686. q->queue_depth = depth;
  687. rq_qos_queue_depth_changed(q);
  688. }
  689. EXPORT_SYMBOL(blk_set_queue_depth);
  690. /**
  691. * blk_queue_write_cache - configure queue's write cache
  692. * @q: the request queue for the device
  693. * @wc: write back cache on or off
  694. * @fua: device supports FUA writes, if true
  695. *
  696. * Tell the block layer about the write cache of @q.
  697. */
  698. void blk_queue_write_cache(struct request_queue *q, bool wc, bool fua)
  699. {
  700. if (wc)
  701. blk_queue_flag_set(QUEUE_FLAG_WC, q);
  702. else
  703. blk_queue_flag_clear(QUEUE_FLAG_WC, q);
  704. if (fua)
  705. blk_queue_flag_set(QUEUE_FLAG_FUA, q);
  706. else
  707. blk_queue_flag_clear(QUEUE_FLAG_FUA, q);
  708. wbt_set_write_cache(q, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
  709. }
  710. EXPORT_SYMBOL_GPL(blk_queue_write_cache);
  711. /**
  712. * blk_queue_required_elevator_features - Set a queue required elevator features
  713. * @q: the request queue for the target device
  714. * @features: Required elevator features OR'ed together
  715. *
  716. * Tell the block layer that for the device controlled through @q, only the
  717. * only elevators that can be used are those that implement at least the set of
  718. * features specified by @features.
  719. */
  720. void blk_queue_required_elevator_features(struct request_queue *q,
  721. unsigned int features)
  722. {
  723. q->required_elevator_features = features;
  724. }
  725. EXPORT_SYMBOL_GPL(blk_queue_required_elevator_features);
  726. /**
  727. * blk_queue_can_use_dma_map_merging - configure queue for merging segments.
  728. * @q: the request queue for the device
  729. * @dev: the device pointer for dma
  730. *
  731. * Tell the block layer about merging the segments by dma map of @q.
  732. */
  733. bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
  734. struct device *dev)
  735. {
  736. unsigned long boundary = dma_get_merge_boundary(dev);
  737. if (!boundary)
  738. return false;
  739. /* No need to update max_segment_size. see blk_queue_virt_boundary() */
  740. blk_queue_virt_boundary(q, boundary);
  741. return true;
  742. }
  743. EXPORT_SYMBOL_GPL(blk_queue_can_use_dma_map_merging);
  744. /**
  745. * blk_queue_set_zoned - configure a disk queue zoned model.
  746. * @disk: the gendisk of the queue to configure
  747. * @model: the zoned model to set
  748. *
  749. * Set the zoned model of the request queue of @disk according to @model.
  750. * When @model is BLK_ZONED_HM (host managed), this should be called only
  751. * if zoned block device support is enabled (CONFIG_BLK_DEV_ZONED option).
  752. * If @model specifies BLK_ZONED_HA (host aware), the effective model used
  753. * depends on CONFIG_BLK_DEV_ZONED settings and on the existence of partitions
  754. * on the disk.
  755. */
  756. void blk_queue_set_zoned(struct gendisk *disk, enum blk_zoned_model model)
  757. {
  758. switch (model) {
  759. case BLK_ZONED_HM:
  760. /*
  761. * Host managed devices are supported only if
  762. * CONFIG_BLK_DEV_ZONED is enabled.
  763. */
  764. WARN_ON_ONCE(!IS_ENABLED(CONFIG_BLK_DEV_ZONED));
  765. break;
  766. case BLK_ZONED_HA:
  767. /*
  768. * Host aware devices can be treated either as regular block
  769. * devices (similar to drive managed devices) or as zoned block
  770. * devices to take advantage of the zone command set, similarly
  771. * to host managed devices. We try the latter if there are no
  772. * partitions and zoned block device support is enabled, else
  773. * we do nothing special as far as the block layer is concerned.
  774. */
  775. if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED) ||
  776. disk_has_partitions(disk))
  777. model = BLK_ZONED_NONE;
  778. break;
  779. case BLK_ZONED_NONE:
  780. default:
  781. if (WARN_ON_ONCE(model != BLK_ZONED_NONE))
  782. model = BLK_ZONED_NONE;
  783. break;
  784. }
  785. disk->queue->limits.zoned = model;
  786. }
  787. EXPORT_SYMBOL_GPL(blk_queue_set_zoned);
  788. static int __init blk_settings_init(void)
  789. {
  790. blk_max_low_pfn = max_low_pfn - 1;
  791. blk_max_pfn = max_pfn - 1;
  792. return 0;
  793. }
  794. subsys_initcall(blk_settings_init);