blk-settings.c 30 KB

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