blk-settings.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. * __bio_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. static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs)
  474. {
  475. sectors = round_down(sectors, lbs >> SECTOR_SHIFT);
  476. if (sectors < PAGE_SIZE >> SECTOR_SHIFT)
  477. sectors = PAGE_SIZE >> SECTOR_SHIFT;
  478. return sectors;
  479. }
  480. /**
  481. * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
  482. * @t: the stacking driver (top)
  483. * @b: the underlying device (bottom)
  484. **/
  485. void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b)
  486. {
  487. blk_stack_limits(&t->limits, &b->limits, 0);
  488. }
  489. EXPORT_SYMBOL(blk_queue_stack_limits);
  490. /**
  491. * blk_stack_limits - adjust queue_limits for stacked devices
  492. * @t: the stacking driver limits (top device)
  493. * @b: the underlying queue limits (bottom, component device)
  494. * @start: first data sector within component device
  495. *
  496. * Description:
  497. * This function is used by stacking drivers like MD and DM to ensure
  498. * that all component devices have compatible block sizes and
  499. * alignments. The stacking driver must provide a queue_limits
  500. * struct (top) and then iteratively call the stacking function for
  501. * all component (bottom) devices. The stacking function will
  502. * attempt to combine the values and ensure proper alignment.
  503. *
  504. * Returns 0 if the top and bottom queue_limits are compatible. The
  505. * top device's block sizes and alignment offsets may be adjusted to
  506. * ensure alignment with the bottom device. If no compatible sizes
  507. * and alignments exist, -1 is returned and the resulting top
  508. * queue_limits will have the misaligned flag set to indicate that
  509. * the alignment_offset is undefined.
  510. */
  511. int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
  512. sector_t start)
  513. {
  514. unsigned int top, bottom, alignment, ret = 0;
  515. t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
  516. t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
  517. t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
  518. t->max_write_same_sectors = min(t->max_write_same_sectors,
  519. b->max_write_same_sectors);
  520. t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors,
  521. b->max_write_zeroes_sectors);
  522. t->bounce_pfn = min_not_zero(t->bounce_pfn, b->bounce_pfn);
  523. t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
  524. b->seg_boundary_mask);
  525. t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask,
  526. b->virt_boundary_mask);
  527. t->max_segments = min_not_zero(t->max_segments, b->max_segments);
  528. t->max_discard_segments = min_not_zero(t->max_discard_segments,
  529. b->max_discard_segments);
  530. t->max_integrity_segments = min_not_zero(t->max_integrity_segments,
  531. b->max_integrity_segments);
  532. t->max_segment_size = min_not_zero(t->max_segment_size,
  533. b->max_segment_size);
  534. t->misaligned |= b->misaligned;
  535. alignment = queue_limit_alignment_offset(b, start);
  536. /* Bottom device has different alignment. Check that it is
  537. * compatible with the current top alignment.
  538. */
  539. if (t->alignment_offset != alignment) {
  540. top = max(t->physical_block_size, t->io_min)
  541. + t->alignment_offset;
  542. bottom = max(b->physical_block_size, b->io_min) + alignment;
  543. /* Verify that top and bottom intervals line up */
  544. if (max(top, bottom) % min(top, bottom)) {
  545. t->misaligned = 1;
  546. ret = -1;
  547. }
  548. }
  549. t->logical_block_size = max(t->logical_block_size,
  550. b->logical_block_size);
  551. t->physical_block_size = max(t->physical_block_size,
  552. b->physical_block_size);
  553. t->io_min = max(t->io_min, b->io_min);
  554. t->io_opt = lcm_not_zero(t->io_opt, b->io_opt);
  555. t->cluster &= b->cluster;
  556. /* Physical block size a multiple of the logical block size? */
  557. if (t->physical_block_size & (t->logical_block_size - 1)) {
  558. t->physical_block_size = t->logical_block_size;
  559. t->misaligned = 1;
  560. ret = -1;
  561. }
  562. /* Minimum I/O a multiple of the physical block size? */
  563. if (t->io_min & (t->physical_block_size - 1)) {
  564. t->io_min = t->physical_block_size;
  565. t->misaligned = 1;
  566. ret = -1;
  567. }
  568. /* Optimal I/O a multiple of the physical block size? */
  569. if (t->io_opt & (t->physical_block_size - 1)) {
  570. t->io_opt = 0;
  571. t->misaligned = 1;
  572. ret = -1;
  573. }
  574. t->raid_partial_stripes_expensive =
  575. max(t->raid_partial_stripes_expensive,
  576. b->raid_partial_stripes_expensive);
  577. /* Find lowest common alignment_offset */
  578. t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment)
  579. % max(t->physical_block_size, t->io_min);
  580. /* Verify that new alignment_offset is on a logical block boundary */
  581. if (t->alignment_offset & (t->logical_block_size - 1)) {
  582. t->misaligned = 1;
  583. ret = -1;
  584. }
  585. t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size);
  586. t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size);
  587. t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size);
  588. /* Discard alignment and granularity */
  589. if (b->discard_granularity) {
  590. alignment = queue_limit_discard_alignment(b, start);
  591. if (t->discard_granularity != 0 &&
  592. t->discard_alignment != alignment) {
  593. top = t->discard_granularity + t->discard_alignment;
  594. bottom = b->discard_granularity + alignment;
  595. /* Verify that top and bottom intervals line up */
  596. if ((max(top, bottom) % min(top, bottom)) != 0)
  597. t->discard_misaligned = 1;
  598. }
  599. t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
  600. b->max_discard_sectors);
  601. t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
  602. b->max_hw_discard_sectors);
  603. t->discard_granularity = max(t->discard_granularity,
  604. b->discard_granularity);
  605. t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
  606. t->discard_granularity;
  607. }
  608. if (b->chunk_sectors)
  609. t->chunk_sectors = min_not_zero(t->chunk_sectors,
  610. b->chunk_sectors);
  611. return ret;
  612. }
  613. EXPORT_SYMBOL(blk_stack_limits);
  614. /**
  615. * bdev_stack_limits - adjust queue limits for stacked drivers
  616. * @t: the stacking driver limits (top device)
  617. * @bdev: the component block_device (bottom)
  618. * @start: first data sector within component device
  619. *
  620. * Description:
  621. * Merges queue limits for a top device and a block_device. Returns
  622. * 0 if alignment didn't change. Returns -1 if adding the bottom
  623. * device caused misalignment.
  624. */
  625. int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev,
  626. sector_t start)
  627. {
  628. struct request_queue *bq = bdev_get_queue(bdev);
  629. start += get_start_sect(bdev);
  630. return blk_stack_limits(t, &bq->limits, start);
  631. }
  632. EXPORT_SYMBOL(bdev_stack_limits);
  633. /**
  634. * disk_stack_limits - adjust queue limits for stacked drivers
  635. * @disk: MD/DM gendisk (top)
  636. * @bdev: the underlying block device (bottom)
  637. * @offset: offset to beginning of data within component device
  638. *
  639. * Description:
  640. * Merges the limits for a top level gendisk and a bottom level
  641. * block_device.
  642. */
  643. void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
  644. sector_t offset)
  645. {
  646. struct request_queue *t = disk->queue;
  647. if (bdev_stack_limits(&t->limits, bdev, offset >> 9) < 0) {
  648. char top[BDEVNAME_SIZE], bottom[BDEVNAME_SIZE];
  649. disk_name(disk, 0, top);
  650. bdevname(bdev, bottom);
  651. printk(KERN_NOTICE "%s: Warning: Device %s is misaligned\n",
  652. top, bottom);
  653. }
  654. t->backing_dev_info->io_pages =
  655. t->limits.max_sectors >> (PAGE_SHIFT - 9);
  656. }
  657. EXPORT_SYMBOL(disk_stack_limits);
  658. /**
  659. * blk_queue_dma_pad - set pad mask
  660. * @q: the request queue for the device
  661. * @mask: pad mask
  662. *
  663. * Set dma pad mask.
  664. *
  665. * Appending pad buffer to a request modifies the last entry of a
  666. * scatter list such that it includes the pad buffer.
  667. **/
  668. void blk_queue_dma_pad(struct request_queue *q, unsigned int mask)
  669. {
  670. q->dma_pad_mask = mask;
  671. }
  672. EXPORT_SYMBOL(blk_queue_dma_pad);
  673. /**
  674. * blk_queue_update_dma_pad - update pad mask
  675. * @q: the request queue for the device
  676. * @mask: pad mask
  677. *
  678. * Update dma pad mask.
  679. *
  680. * Appending pad buffer to a request modifies the last entry of a
  681. * scatter list such that it includes the pad buffer.
  682. **/
  683. void blk_queue_update_dma_pad(struct request_queue *q, unsigned int mask)
  684. {
  685. if (mask > q->dma_pad_mask)
  686. q->dma_pad_mask = mask;
  687. }
  688. EXPORT_SYMBOL(blk_queue_update_dma_pad);
  689. /**
  690. * blk_queue_dma_drain - Set up a drain buffer for excess dma.
  691. * @q: the request queue for the device
  692. * @dma_drain_needed: fn which returns non-zero if drain is necessary
  693. * @buf: physically contiguous buffer
  694. * @size: size of the buffer in bytes
  695. *
  696. * Some devices have excess DMA problems and can't simply discard (or
  697. * zero fill) the unwanted piece of the transfer. They have to have a
  698. * real area of memory to transfer it into. The use case for this is
  699. * ATAPI devices in DMA mode. If the packet command causes a transfer
  700. * bigger than the transfer size some HBAs will lock up if there
  701. * aren't DMA elements to contain the excess transfer. What this API
  702. * does is adjust the queue so that the buf is always appended
  703. * silently to the scatterlist.
  704. *
  705. * Note: This routine adjusts max_hw_segments to make room for appending
  706. * the drain buffer. If you call blk_queue_max_segments() after calling
  707. * this routine, you must set the limit to one fewer than your device
  708. * can support otherwise there won't be room for the drain buffer.
  709. */
  710. int blk_queue_dma_drain(struct request_queue *q,
  711. dma_drain_needed_fn *dma_drain_needed,
  712. void *buf, unsigned int size)
  713. {
  714. if (queue_max_segments(q) < 2)
  715. return -EINVAL;
  716. /* make room for appending the drain */
  717. blk_queue_max_segments(q, queue_max_segments(q) - 1);
  718. q->dma_drain_needed = dma_drain_needed;
  719. q->dma_drain_buffer = buf;
  720. q->dma_drain_size = size;
  721. return 0;
  722. }
  723. EXPORT_SYMBOL_GPL(blk_queue_dma_drain);
  724. /**
  725. * blk_queue_segment_boundary - set boundary rules for segment merging
  726. * @q: the request queue for the device
  727. * @mask: the memory boundary mask
  728. **/
  729. void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
  730. {
  731. if (mask < PAGE_SIZE - 1) {
  732. mask = PAGE_SIZE - 1;
  733. printk(KERN_INFO "%s: set to minimum %lx\n",
  734. __func__, mask);
  735. }
  736. q->limits.seg_boundary_mask = mask;
  737. }
  738. EXPORT_SYMBOL(blk_queue_segment_boundary);
  739. /**
  740. * blk_queue_virt_boundary - set boundary rules for bio merging
  741. * @q: the request queue for the device
  742. * @mask: the memory boundary mask
  743. **/
  744. void blk_queue_virt_boundary(struct request_queue *q, unsigned long mask)
  745. {
  746. q->limits.virt_boundary_mask = mask;
  747. }
  748. EXPORT_SYMBOL(blk_queue_virt_boundary);
  749. /**
  750. * blk_queue_dma_alignment - set dma length and memory alignment
  751. * @q: the request queue for the device
  752. * @mask: alignment mask
  753. *
  754. * description:
  755. * set required memory and length alignment for direct dma transactions.
  756. * this is used when building direct io requests for the queue.
  757. *
  758. **/
  759. void blk_queue_dma_alignment(struct request_queue *q, int mask)
  760. {
  761. q->dma_alignment = mask;
  762. }
  763. EXPORT_SYMBOL(blk_queue_dma_alignment);
  764. /**
  765. * blk_queue_update_dma_alignment - update dma length and memory alignment
  766. * @q: the request queue for the device
  767. * @mask: alignment mask
  768. *
  769. * description:
  770. * update required memory and length alignment for direct dma transactions.
  771. * If the requested alignment is larger than the current alignment, then
  772. * the current queue alignment is updated to the new value, otherwise it
  773. * is left alone. The design of this is to allow multiple objects
  774. * (driver, device, transport etc) to set their respective
  775. * alignments without having them interfere.
  776. *
  777. **/
  778. void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
  779. {
  780. BUG_ON(mask > PAGE_SIZE);
  781. if (mask > q->dma_alignment)
  782. q->dma_alignment = mask;
  783. }
  784. EXPORT_SYMBOL(blk_queue_update_dma_alignment);
  785. void blk_queue_flush_queueable(struct request_queue *q, bool queueable)
  786. {
  787. spin_lock_irq(q->queue_lock);
  788. if (queueable)
  789. clear_bit(QUEUE_FLAG_FLUSH_NQ, &q->queue_flags);
  790. else
  791. set_bit(QUEUE_FLAG_FLUSH_NQ, &q->queue_flags);
  792. spin_unlock_irq(q->queue_lock);
  793. }
  794. EXPORT_SYMBOL_GPL(blk_queue_flush_queueable);
  795. /**
  796. * blk_set_queue_depth - tell the block layer about the device queue depth
  797. * @q: the request queue for the device
  798. * @depth: queue depth
  799. *
  800. */
  801. void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
  802. {
  803. q->queue_depth = depth;
  804. wbt_set_queue_depth(q->rq_wb, depth);
  805. }
  806. EXPORT_SYMBOL(blk_set_queue_depth);
  807. /**
  808. * blk_queue_write_cache - configure queue's write cache
  809. * @q: the request queue for the device
  810. * @wc: write back cache on or off
  811. * @fua: device supports FUA writes, if true
  812. *
  813. * Tell the block layer about the write cache of @q.
  814. */
  815. void blk_queue_write_cache(struct request_queue *q, bool wc, bool fua)
  816. {
  817. spin_lock_irq(q->queue_lock);
  818. if (wc)
  819. queue_flag_set(QUEUE_FLAG_WC, q);
  820. else
  821. queue_flag_clear(QUEUE_FLAG_WC, q);
  822. if (fua)
  823. queue_flag_set(QUEUE_FLAG_FUA, q);
  824. else
  825. queue_flag_clear(QUEUE_FLAG_FUA, q);
  826. spin_unlock_irq(q->queue_lock);
  827. wbt_set_write_cache(q->rq_wb, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
  828. }
  829. EXPORT_SYMBOL_GPL(blk_queue_write_cache);
  830. static int __init blk_settings_init(void)
  831. {
  832. blk_max_low_pfn = max_low_pfn - 1;
  833. blk_max_pfn = max_pfn - 1;
  834. return 0;
  835. }
  836. subsys_initcall(blk_settings_init);