blk-settings.c 23 KB

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