blk-settings.c 28 KB

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