blk-settings.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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/jiffies.h>
  12. #include "blk.h"
  13. unsigned long blk_max_low_pfn;
  14. EXPORT_SYMBOL(blk_max_low_pfn);
  15. unsigned long blk_max_pfn;
  16. /**
  17. * blk_queue_prep_rq - set a prepare_request function for queue
  18. * @q: queue
  19. * @pfn: prepare_request function
  20. *
  21. * It's possible for a queue to register a prepare_request callback which
  22. * is invoked before the request is handed to the request_fn. The goal of
  23. * the function is to prepare a request for I/O, it can be used to build a
  24. * cdb from the request data for instance.
  25. *
  26. */
  27. void blk_queue_prep_rq(struct request_queue *q, prep_rq_fn *pfn)
  28. {
  29. q->prep_rq_fn = pfn;
  30. }
  31. EXPORT_SYMBOL(blk_queue_prep_rq);
  32. /**
  33. * blk_queue_merge_bvec - set a merge_bvec function for queue
  34. * @q: queue
  35. * @mbfn: merge_bvec_fn
  36. *
  37. * Usually queues have static limitations on the max sectors or segments that
  38. * we can put in a request. Stacking drivers may have some settings that
  39. * are dynamic, and thus we have to query the queue whether it is ok to
  40. * add a new bio_vec to a bio at a given offset or not. If the block device
  41. * has such limitations, it needs to register a merge_bvec_fn to control
  42. * the size of bio's sent to it. Note that a block device *must* allow a
  43. * single page to be added to an empty bio. The block device driver may want
  44. * to use the bio_split() function to deal with these bio's. By default
  45. * no merge_bvec_fn is defined for a queue, and only the fixed limits are
  46. * honored.
  47. */
  48. void blk_queue_merge_bvec(struct request_queue *q, merge_bvec_fn *mbfn)
  49. {
  50. q->merge_bvec_fn = mbfn;
  51. }
  52. EXPORT_SYMBOL(blk_queue_merge_bvec);
  53. void blk_queue_softirq_done(struct request_queue *q, softirq_done_fn *fn)
  54. {
  55. q->softirq_done_fn = fn;
  56. }
  57. EXPORT_SYMBOL(blk_queue_softirq_done);
  58. void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout)
  59. {
  60. q->rq_timeout = timeout;
  61. }
  62. EXPORT_SYMBOL_GPL(blk_queue_rq_timeout);
  63. void blk_queue_rq_timed_out(struct request_queue *q, rq_timed_out_fn *fn)
  64. {
  65. q->rq_timed_out_fn = fn;
  66. }
  67. EXPORT_SYMBOL_GPL(blk_queue_rq_timed_out);
  68. void blk_queue_lld_busy(struct request_queue *q, lld_busy_fn *fn)
  69. {
  70. q->lld_busy_fn = fn;
  71. }
  72. EXPORT_SYMBOL_GPL(blk_queue_lld_busy);
  73. /**
  74. * blk_set_default_limits - reset limits to default values
  75. * @lim: the queue_limits structure to reset
  76. *
  77. * Description:
  78. * Returns a queue_limit struct to its default state. Can be used by
  79. * stacking drivers like DM that stage table swaps and reuse an
  80. * existing device queue.
  81. */
  82. void blk_set_default_limits(struct queue_limits *lim)
  83. {
  84. lim->max_phys_segments = MAX_PHYS_SEGMENTS;
  85. lim->max_hw_segments = MAX_HW_SEGMENTS;
  86. lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
  87. lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
  88. lim->max_sectors = BLK_DEF_MAX_SECTORS;
  89. lim->max_hw_sectors = INT_MAX;
  90. lim->max_discard_sectors = 0;
  91. lim->discard_granularity = 0;
  92. lim->discard_alignment = 0;
  93. lim->discard_misaligned = 0;
  94. lim->discard_zeroes_data = -1;
  95. lim->logical_block_size = lim->physical_block_size = lim->io_min = 512;
  96. lim->bounce_pfn = (unsigned long)(BLK_BOUNCE_ANY >> PAGE_SHIFT);
  97. lim->alignment_offset = 0;
  98. lim->io_opt = 0;
  99. lim->misaligned = 0;
  100. lim->no_cluster = 0;
  101. }
  102. EXPORT_SYMBOL(blk_set_default_limits);
  103. /**
  104. * blk_queue_make_request - define an alternate make_request function for a device
  105. * @q: the request queue for the device to be affected
  106. * @mfn: the alternate make_request function
  107. *
  108. * Description:
  109. * The normal way for &struct bios to be passed to a device
  110. * driver is for them to be collected into requests on a request
  111. * queue, and then to allow the device driver to select requests
  112. * off that queue when it is ready. This works well for many block
  113. * devices. However some block devices (typically virtual devices
  114. * such as md or lvm) do not benefit from the processing on the
  115. * request queue, and are served best by having the requests passed
  116. * directly to them. This can be achieved by providing a function
  117. * to blk_queue_make_request().
  118. *
  119. * Caveat:
  120. * The driver that does this *must* be able to deal appropriately
  121. * with buffers in "highmemory". This can be accomplished by either calling
  122. * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
  123. * blk_queue_bounce() to create a buffer in normal memory.
  124. **/
  125. void blk_queue_make_request(struct request_queue *q, make_request_fn *mfn)
  126. {
  127. /*
  128. * set defaults
  129. */
  130. q->nr_requests = BLKDEV_MAX_RQ;
  131. q->make_request_fn = mfn;
  132. blk_queue_dma_alignment(q, 511);
  133. blk_queue_congestion_threshold(q);
  134. q->nr_batching = BLK_BATCH_REQ;
  135. q->unplug_thresh = 4; /* hmm */
  136. q->unplug_delay = msecs_to_jiffies(3); /* 3 milliseconds */
  137. if (q->unplug_delay == 0)
  138. q->unplug_delay = 1;
  139. q->unplug_timer.function = blk_unplug_timeout;
  140. q->unplug_timer.data = (unsigned long)q;
  141. blk_set_default_limits(&q->limits);
  142. blk_queue_max_sectors(q, BLK_SAFE_MAX_SECTORS);
  143. /*
  144. * If the caller didn't supply a lock, fall back to our embedded
  145. * per-queue locks
  146. */
  147. if (!q->queue_lock)
  148. q->queue_lock = &q->__queue_lock;
  149. /*
  150. * by default assume old behaviour and bounce for any highmem page
  151. */
  152. blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
  153. }
  154. EXPORT_SYMBOL(blk_queue_make_request);
  155. /**
  156. * blk_queue_bounce_limit - set bounce buffer limit for queue
  157. * @q: the request queue for the device
  158. * @dma_mask: the maximum address the device can handle
  159. *
  160. * Description:
  161. * Different hardware can have different requirements as to what pages
  162. * it can do I/O directly to. A low level driver can call
  163. * blk_queue_bounce_limit to have lower memory pages allocated as bounce
  164. * buffers for doing I/O to pages residing above @dma_mask.
  165. **/
  166. void blk_queue_bounce_limit(struct request_queue *q, u64 dma_mask)
  167. {
  168. unsigned long b_pfn = dma_mask >> PAGE_SHIFT;
  169. int dma = 0;
  170. q->bounce_gfp = GFP_NOIO;
  171. #if BITS_PER_LONG == 64
  172. /*
  173. * Assume anything <= 4GB can be handled by IOMMU. Actually
  174. * some IOMMUs can handle everything, but I don't know of a
  175. * way to test this here.
  176. */
  177. if (b_pfn < (min_t(u64, 0xffffffffUL, BLK_BOUNCE_HIGH) >> PAGE_SHIFT))
  178. dma = 1;
  179. q->limits.bounce_pfn = max_low_pfn;
  180. #else
  181. if (b_pfn < blk_max_low_pfn)
  182. dma = 1;
  183. q->limits.bounce_pfn = b_pfn;
  184. #endif
  185. if (dma) {
  186. init_emergency_isa_pool();
  187. q->bounce_gfp = GFP_NOIO | GFP_DMA;
  188. q->limits.bounce_pfn = b_pfn;
  189. }
  190. }
  191. EXPORT_SYMBOL(blk_queue_bounce_limit);
  192. /**
  193. * blk_queue_max_sectors - set max sectors for a request for this queue
  194. * @q: the request queue for the device
  195. * @max_hw_sectors: max hardware sectors in the usual 512b unit
  196. *
  197. * Description:
  198. * Enables a low level driver to set a hard upper limit,
  199. * max_hw_sectors, on the size of requests. max_hw_sectors is set by
  200. * the device driver based upon the combined capabilities of I/O
  201. * controller and storage device.
  202. *
  203. * max_sectors is a soft limit imposed by the block layer for
  204. * filesystem type requests. This value can be overridden on a
  205. * per-device basis in /sys/block/<device>/queue/max_sectors_kb.
  206. * The soft limit can not exceed max_hw_sectors.
  207. **/
  208. void blk_queue_max_sectors(struct request_queue *q, unsigned int max_hw_sectors)
  209. {
  210. if ((max_hw_sectors << 9) < PAGE_CACHE_SIZE) {
  211. max_hw_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
  212. printk(KERN_INFO "%s: set to minimum %d\n",
  213. __func__, max_hw_sectors);
  214. }
  215. q->limits.max_hw_sectors = max_hw_sectors;
  216. q->limits.max_sectors = min_t(unsigned int, max_hw_sectors,
  217. BLK_DEF_MAX_SECTORS);
  218. }
  219. EXPORT_SYMBOL(blk_queue_max_sectors);
  220. /**
  221. * blk_queue_max_discard_sectors - set max sectors for a single discard
  222. * @q: the request queue for the device
  223. * @max_discard_sectors: maximum number of sectors to discard
  224. **/
  225. void blk_queue_max_discard_sectors(struct request_queue *q,
  226. unsigned int max_discard_sectors)
  227. {
  228. q->limits.max_discard_sectors = max_discard_sectors;
  229. }
  230. EXPORT_SYMBOL(blk_queue_max_discard_sectors);
  231. /**
  232. * blk_queue_max_phys_segments - set max phys segments for a request for this queue
  233. * @q: the request queue for the device
  234. * @max_segments: max number of segments
  235. *
  236. * Description:
  237. * Enables a low level driver to set an upper limit on the number of
  238. * physical data segments in a request. This would be the largest sized
  239. * scatter list the driver could handle.
  240. **/
  241. void blk_queue_max_phys_segments(struct request_queue *q,
  242. unsigned short max_segments)
  243. {
  244. if (!max_segments) {
  245. max_segments = 1;
  246. printk(KERN_INFO "%s: set to minimum %d\n",
  247. __func__, max_segments);
  248. }
  249. q->limits.max_phys_segments = max_segments;
  250. }
  251. EXPORT_SYMBOL(blk_queue_max_phys_segments);
  252. /**
  253. * blk_queue_max_hw_segments - set max hw segments for a request for this queue
  254. * @q: the request queue for the device
  255. * @max_segments: max number of segments
  256. *
  257. * Description:
  258. * Enables a low level driver to set an upper limit on the number of
  259. * hw data segments in a request. This would be the largest number of
  260. * address/length pairs the host adapter can actually give at once
  261. * to the device.
  262. **/
  263. void blk_queue_max_hw_segments(struct request_queue *q,
  264. unsigned short max_segments)
  265. {
  266. if (!max_segments) {
  267. max_segments = 1;
  268. printk(KERN_INFO "%s: set to minimum %d\n",
  269. __func__, max_segments);
  270. }
  271. q->limits.max_hw_segments = max_segments;
  272. }
  273. EXPORT_SYMBOL(blk_queue_max_hw_segments);
  274. /**
  275. * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
  276. * @q: the request queue for the device
  277. * @max_size: max size of segment in bytes
  278. *
  279. * Description:
  280. * Enables a low level driver to set an upper limit on the size of a
  281. * coalesced segment
  282. **/
  283. void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
  284. {
  285. if (max_size < PAGE_CACHE_SIZE) {
  286. max_size = PAGE_CACHE_SIZE;
  287. printk(KERN_INFO "%s: set to minimum %d\n",
  288. __func__, max_size);
  289. }
  290. q->limits.max_segment_size = max_size;
  291. }
  292. EXPORT_SYMBOL(blk_queue_max_segment_size);
  293. /**
  294. * blk_queue_logical_block_size - set logical block size for the queue
  295. * @q: the request queue for the device
  296. * @size: the logical block size, in bytes
  297. *
  298. * Description:
  299. * This should be set to the lowest possible block size that the
  300. * storage device can address. The default of 512 covers most
  301. * hardware.
  302. **/
  303. void blk_queue_logical_block_size(struct request_queue *q, unsigned short size)
  304. {
  305. q->limits.logical_block_size = size;
  306. if (q->limits.physical_block_size < size)
  307. q->limits.physical_block_size = size;
  308. if (q->limits.io_min < q->limits.physical_block_size)
  309. q->limits.io_min = q->limits.physical_block_size;
  310. }
  311. EXPORT_SYMBOL(blk_queue_logical_block_size);
  312. /**
  313. * blk_queue_physical_block_size - set physical block size for the queue
  314. * @q: the request queue for the device
  315. * @size: the physical block size, in bytes
  316. *
  317. * Description:
  318. * This should be set to the lowest possible sector size that the
  319. * hardware can operate on without reverting to read-modify-write
  320. * operations.
  321. */
  322. void blk_queue_physical_block_size(struct request_queue *q, unsigned short size)
  323. {
  324. q->limits.physical_block_size = size;
  325. if (q->limits.physical_block_size < q->limits.logical_block_size)
  326. q->limits.physical_block_size = q->limits.logical_block_size;
  327. if (q->limits.io_min < q->limits.physical_block_size)
  328. q->limits.io_min = q->limits.physical_block_size;
  329. }
  330. EXPORT_SYMBOL(blk_queue_physical_block_size);
  331. /**
  332. * blk_queue_alignment_offset - set physical block alignment offset
  333. * @q: the request queue for the device
  334. * @offset: alignment offset in bytes
  335. *
  336. * Description:
  337. * Some devices are naturally misaligned to compensate for things like
  338. * the legacy DOS partition table 63-sector offset. Low-level drivers
  339. * should call this function for devices whose first sector is not
  340. * naturally aligned.
  341. */
  342. void blk_queue_alignment_offset(struct request_queue *q, unsigned int offset)
  343. {
  344. q->limits.alignment_offset =
  345. offset & (q->limits.physical_block_size - 1);
  346. q->limits.misaligned = 0;
  347. }
  348. EXPORT_SYMBOL(blk_queue_alignment_offset);
  349. /**
  350. * blk_limits_io_min - set minimum request size for a device
  351. * @limits: the queue limits
  352. * @min: smallest I/O size in bytes
  353. *
  354. * Description:
  355. * Some devices have an internal block size bigger than the reported
  356. * hardware sector size. This function can be used to signal the
  357. * smallest I/O the device can perform without incurring a performance
  358. * penalty.
  359. */
  360. void blk_limits_io_min(struct queue_limits *limits, unsigned int min)
  361. {
  362. limits->io_min = min;
  363. if (limits->io_min < limits->logical_block_size)
  364. limits->io_min = limits->logical_block_size;
  365. if (limits->io_min < limits->physical_block_size)
  366. limits->io_min = limits->physical_block_size;
  367. }
  368. EXPORT_SYMBOL(blk_limits_io_min);
  369. /**
  370. * blk_queue_io_min - set minimum request size for the queue
  371. * @q: the request queue for the device
  372. * @min: smallest I/O size in bytes
  373. *
  374. * Description:
  375. * Storage devices may report a granularity or preferred minimum I/O
  376. * size which is the smallest request the device can perform without
  377. * incurring a performance penalty. For disk drives this is often the
  378. * physical block size. For RAID arrays it is often the stripe chunk
  379. * size. A properly aligned multiple of minimum_io_size is the
  380. * preferred request size for workloads where a high number of I/O
  381. * operations is desired.
  382. */
  383. void blk_queue_io_min(struct request_queue *q, unsigned int min)
  384. {
  385. blk_limits_io_min(&q->limits, min);
  386. }
  387. EXPORT_SYMBOL(blk_queue_io_min);
  388. /**
  389. * blk_limits_io_opt - set optimal request size for a device
  390. * @limits: the queue limits
  391. * @opt: smallest I/O size in bytes
  392. *
  393. * Description:
  394. * Storage devices may report an optimal I/O size, which is the
  395. * device's preferred unit for sustained I/O. This is rarely reported
  396. * for disk drives. For RAID arrays it is usually the stripe width or
  397. * the internal track size. A properly aligned multiple of
  398. * optimal_io_size is the preferred request size for workloads where
  399. * sustained throughput is desired.
  400. */
  401. void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt)
  402. {
  403. limits->io_opt = opt;
  404. }
  405. EXPORT_SYMBOL(blk_limits_io_opt);
  406. /**
  407. * blk_queue_io_opt - set optimal request size for the queue
  408. * @q: the request queue for the device
  409. * @opt: optimal request size in bytes
  410. *
  411. * Description:
  412. * Storage devices may report an optimal I/O size, which is the
  413. * device's preferred unit for sustained I/O. This is rarely reported
  414. * for disk drives. For RAID arrays it is usually the stripe width or
  415. * the internal track size. A properly aligned multiple of
  416. * optimal_io_size is the preferred request size for workloads where
  417. * sustained throughput is desired.
  418. */
  419. void blk_queue_io_opt(struct request_queue *q, unsigned int opt)
  420. {
  421. blk_limits_io_opt(&q->limits, opt);
  422. }
  423. EXPORT_SYMBOL(blk_queue_io_opt);
  424. /*
  425. * Returns the minimum that is _not_ zero, unless both are zero.
  426. */
  427. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  428. /**
  429. * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
  430. * @t: the stacking driver (top)
  431. * @b: the underlying device (bottom)
  432. **/
  433. void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b)
  434. {
  435. blk_stack_limits(&t->limits, &b->limits, 0);
  436. if (!t->queue_lock)
  437. WARN_ON_ONCE(1);
  438. else if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags)) {
  439. unsigned long flags;
  440. spin_lock_irqsave(t->queue_lock, flags);
  441. queue_flag_clear(QUEUE_FLAG_CLUSTER, t);
  442. spin_unlock_irqrestore(t->queue_lock, flags);
  443. }
  444. }
  445. EXPORT_SYMBOL(blk_queue_stack_limits);
  446. static unsigned int lcm(unsigned int a, unsigned int b)
  447. {
  448. if (a && b)
  449. return (a * b) / gcd(a, b);
  450. else if (b)
  451. return b;
  452. return a;
  453. }
  454. /**
  455. * blk_stack_limits - adjust queue_limits for stacked devices
  456. * @t: the stacking driver limits (top device)
  457. * @b: the underlying queue limits (bottom, component device)
  458. * @start: first data sector within component device
  459. *
  460. * Description:
  461. * This function is used by stacking drivers like MD and DM to ensure
  462. * that all component devices have compatible block sizes and
  463. * alignments. The stacking driver must provide a queue_limits
  464. * struct (top) and then iteratively call the stacking function for
  465. * all component (bottom) devices. The stacking function will
  466. * attempt to combine the values and ensure proper alignment.
  467. *
  468. * Returns 0 if the top and bottom queue_limits are compatible. The
  469. * top device's block sizes and alignment offsets may be adjusted to
  470. * ensure alignment with the bottom device. If no compatible sizes
  471. * and alignments exist, -1 is returned and the resulting top
  472. * queue_limits will have the misaligned flag set to indicate that
  473. * the alignment_offset is undefined.
  474. */
  475. int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
  476. sector_t start)
  477. {
  478. unsigned int top, bottom, alignment, ret = 0;
  479. t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
  480. t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
  481. t->bounce_pfn = min_not_zero(t->bounce_pfn, b->bounce_pfn);
  482. t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
  483. b->seg_boundary_mask);
  484. t->max_phys_segments = min_not_zero(t->max_phys_segments,
  485. b->max_phys_segments);
  486. t->max_hw_segments = min_not_zero(t->max_hw_segments,
  487. b->max_hw_segments);
  488. t->max_segment_size = min_not_zero(t->max_segment_size,
  489. b->max_segment_size);
  490. t->misaligned |= b->misaligned;
  491. alignment = queue_limit_alignment_offset(b, start);
  492. /* Bottom device has different alignment. Check that it is
  493. * compatible with the current top alignment.
  494. */
  495. if (t->alignment_offset != alignment) {
  496. top = max(t->physical_block_size, t->io_min)
  497. + t->alignment_offset;
  498. bottom = max(b->physical_block_size, b->io_min) + alignment;
  499. /* Verify that top and bottom intervals line up */
  500. if (max(top, bottom) & (min(top, bottom) - 1)) {
  501. t->misaligned = 1;
  502. ret = -1;
  503. }
  504. }
  505. t->logical_block_size = max(t->logical_block_size,
  506. b->logical_block_size);
  507. t->physical_block_size = max(t->physical_block_size,
  508. b->physical_block_size);
  509. t->io_min = max(t->io_min, b->io_min);
  510. t->io_opt = lcm(t->io_opt, b->io_opt);
  511. t->no_cluster |= b->no_cluster;
  512. t->discard_zeroes_data &= b->discard_zeroes_data;
  513. /* Physical block size a multiple of the logical block size? */
  514. if (t->physical_block_size & (t->logical_block_size - 1)) {
  515. t->physical_block_size = t->logical_block_size;
  516. t->misaligned = 1;
  517. ret = -1;
  518. }
  519. /* Minimum I/O a multiple of the physical block size? */
  520. if (t->io_min & (t->physical_block_size - 1)) {
  521. t->io_min = t->physical_block_size;
  522. t->misaligned = 1;
  523. ret = -1;
  524. }
  525. /* Optimal I/O a multiple of the physical block size? */
  526. if (t->io_opt & (t->physical_block_size - 1)) {
  527. t->io_opt = 0;
  528. t->misaligned = 1;
  529. ret = -1;
  530. }
  531. /* Find lowest common alignment_offset */
  532. t->alignment_offset = lcm(t->alignment_offset, alignment)
  533. & (max(t->physical_block_size, t->io_min) - 1);
  534. /* Verify that new alignment_offset is on a logical block boundary */
  535. if (t->alignment_offset & (t->logical_block_size - 1)) {
  536. t->misaligned = 1;
  537. ret = -1;
  538. }
  539. /* Discard alignment and granularity */
  540. if (b->discard_granularity) {
  541. alignment = queue_limit_discard_alignment(b, start);
  542. if (t->discard_granularity != 0 &&
  543. t->discard_alignment != alignment) {
  544. top = t->discard_granularity + t->discard_alignment;
  545. bottom = b->discard_granularity + alignment;
  546. /* Verify that top and bottom intervals line up */
  547. if (max(top, bottom) & (min(top, bottom) - 1))
  548. t->discard_misaligned = 1;
  549. }
  550. t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
  551. b->max_discard_sectors);
  552. t->discard_granularity = max(t->discard_granularity,
  553. b->discard_granularity);
  554. t->discard_alignment = lcm(t->discard_alignment, alignment) &
  555. (t->discard_granularity - 1);
  556. }
  557. return ret;
  558. }
  559. EXPORT_SYMBOL(blk_stack_limits);
  560. /**
  561. * bdev_stack_limits - adjust queue limits for stacked drivers
  562. * @t: the stacking driver limits (top device)
  563. * @bdev: the component block_device (bottom)
  564. * @start: first data sector within component device
  565. *
  566. * Description:
  567. * Merges queue limits for a top device and a block_device. Returns
  568. * 0 if alignment didn't change. Returns -1 if adding the bottom
  569. * device caused misalignment.
  570. */
  571. int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev,
  572. sector_t start)
  573. {
  574. struct request_queue *bq = bdev_get_queue(bdev);
  575. start += get_start_sect(bdev);
  576. return blk_stack_limits(t, &bq->limits, start);
  577. }
  578. EXPORT_SYMBOL(bdev_stack_limits);
  579. /**
  580. * disk_stack_limits - adjust queue limits for stacked drivers
  581. * @disk: MD/DM gendisk (top)
  582. * @bdev: the underlying block device (bottom)
  583. * @offset: offset to beginning of data within component device
  584. *
  585. * Description:
  586. * Merges the limits for a top level gendisk and a bottom level
  587. * block_device.
  588. */
  589. void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
  590. sector_t offset)
  591. {
  592. struct request_queue *t = disk->queue;
  593. struct request_queue *b = bdev_get_queue(bdev);
  594. if (bdev_stack_limits(&t->limits, bdev, offset >> 9) < 0) {
  595. char top[BDEVNAME_SIZE], bottom[BDEVNAME_SIZE];
  596. disk_name(disk, 0, top);
  597. bdevname(bdev, bottom);
  598. printk(KERN_NOTICE "%s: Warning: Device %s is misaligned\n",
  599. top, bottom);
  600. }
  601. if (!t->queue_lock)
  602. WARN_ON_ONCE(1);
  603. else if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags)) {
  604. unsigned long flags;
  605. spin_lock_irqsave(t->queue_lock, flags);
  606. if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags))
  607. queue_flag_clear(QUEUE_FLAG_CLUSTER, t);
  608. spin_unlock_irqrestore(t->queue_lock, flags);
  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
  660. * appending the drain buffer. If you call
  661. * blk_queue_max_hw_segments() or blk_queue_max_phys_segments() after
  662. * calling this routine, you must set the limit to one fewer than your
  663. * device can support otherwise there won't be room for the drain
  664. * buffer.
  665. */
  666. int blk_queue_dma_drain(struct request_queue *q,
  667. dma_drain_needed_fn *dma_drain_needed,
  668. void *buf, unsigned int size)
  669. {
  670. if (queue_max_hw_segments(q) < 2 || queue_max_phys_segments(q) < 2)
  671. return -EINVAL;
  672. /* make room for appending the drain */
  673. blk_queue_max_hw_segments(q, queue_max_hw_segments(q) - 1);
  674. blk_queue_max_phys_segments(q, queue_max_phys_segments(q) - 1);
  675. q->dma_drain_needed = dma_drain_needed;
  676. q->dma_drain_buffer = buf;
  677. q->dma_drain_size = size;
  678. return 0;
  679. }
  680. EXPORT_SYMBOL_GPL(blk_queue_dma_drain);
  681. /**
  682. * blk_queue_segment_boundary - set boundary rules for segment merging
  683. * @q: the request queue for the device
  684. * @mask: the memory boundary mask
  685. **/
  686. void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
  687. {
  688. if (mask < PAGE_CACHE_SIZE - 1) {
  689. mask = PAGE_CACHE_SIZE - 1;
  690. printk(KERN_INFO "%s: set to minimum %lx\n",
  691. __func__, mask);
  692. }
  693. q->limits.seg_boundary_mask = mask;
  694. }
  695. EXPORT_SYMBOL(blk_queue_segment_boundary);
  696. /**
  697. * blk_queue_dma_alignment - set dma length and memory alignment
  698. * @q: the request queue for the device
  699. * @mask: alignment mask
  700. *
  701. * description:
  702. * set required memory and length alignment for direct dma transactions.
  703. * this is used when building direct io requests for the queue.
  704. *
  705. **/
  706. void blk_queue_dma_alignment(struct request_queue *q, int mask)
  707. {
  708. q->dma_alignment = mask;
  709. }
  710. EXPORT_SYMBOL(blk_queue_dma_alignment);
  711. /**
  712. * blk_queue_update_dma_alignment - update dma length and memory alignment
  713. * @q: the request queue for the device
  714. * @mask: alignment mask
  715. *
  716. * description:
  717. * update required memory and length alignment for direct dma transactions.
  718. * If the requested alignment is larger than the current alignment, then
  719. * the current queue alignment is updated to the new value, otherwise it
  720. * is left alone. The design of this is to allow multiple objects
  721. * (driver, device, transport etc) to set their respective
  722. * alignments without having them interfere.
  723. *
  724. **/
  725. void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
  726. {
  727. BUG_ON(mask > PAGE_SIZE);
  728. if (mask > q->dma_alignment)
  729. q->dma_alignment = mask;
  730. }
  731. EXPORT_SYMBOL(blk_queue_update_dma_alignment);
  732. static int __init blk_settings_init(void)
  733. {
  734. blk_max_low_pfn = max_low_pfn - 1;
  735. blk_max_pfn = max_pfn - 1;
  736. return 0;
  737. }
  738. subsys_initcall(blk_settings_init);