blk-settings.c 29 KB

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