blk-settings.c 23 KB

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