blk-settings.c 27 KB

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