ib_fmr.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2016 Oracle. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include "ib_mr.h"
  33. struct rds_ib_mr *rds_ib_alloc_fmr(struct rds_ib_device *rds_ibdev, int npages)
  34. {
  35. struct rds_ib_mr_pool *pool;
  36. struct rds_ib_mr *ibmr = NULL;
  37. struct rds_ib_fmr *fmr;
  38. int err = 0;
  39. if (npages <= RDS_MR_8K_MSG_SIZE)
  40. pool = rds_ibdev->mr_8k_pool;
  41. else
  42. pool = rds_ibdev->mr_1m_pool;
  43. ibmr = rds_ib_try_reuse_ibmr(pool);
  44. if (ibmr)
  45. return ibmr;
  46. ibmr = kzalloc_node(sizeof(*ibmr), GFP_KERNEL,
  47. rdsibdev_to_node(rds_ibdev));
  48. if (!ibmr) {
  49. err = -ENOMEM;
  50. goto out_no_cigar;
  51. }
  52. fmr = &ibmr->u.fmr;
  53. fmr->fmr = ib_alloc_fmr(rds_ibdev->pd,
  54. (IB_ACCESS_LOCAL_WRITE |
  55. IB_ACCESS_REMOTE_READ |
  56. IB_ACCESS_REMOTE_WRITE |
  57. IB_ACCESS_REMOTE_ATOMIC),
  58. &pool->fmr_attr);
  59. if (IS_ERR(fmr->fmr)) {
  60. err = PTR_ERR(fmr->fmr);
  61. fmr->fmr = NULL;
  62. pr_warn("RDS/IB: %s failed (err=%d)\n", __func__, err);
  63. goto out_no_cigar;
  64. }
  65. ibmr->pool = pool;
  66. if (pool->pool_type == RDS_IB_MR_8K_POOL)
  67. rds_ib_stats_inc(s_ib_rdma_mr_8k_alloc);
  68. else
  69. rds_ib_stats_inc(s_ib_rdma_mr_1m_alloc);
  70. return ibmr;
  71. out_no_cigar:
  72. if (ibmr) {
  73. if (fmr->fmr)
  74. ib_dealloc_fmr(fmr->fmr);
  75. kfree(ibmr);
  76. }
  77. atomic_dec(&pool->item_count);
  78. return ERR_PTR(err);
  79. }
  80. int rds_ib_map_fmr(struct rds_ib_device *rds_ibdev, struct rds_ib_mr *ibmr,
  81. struct scatterlist *sg, unsigned int nents)
  82. {
  83. struct ib_device *dev = rds_ibdev->dev;
  84. struct rds_ib_fmr *fmr = &ibmr->u.fmr;
  85. struct scatterlist *scat = sg;
  86. u64 io_addr = 0;
  87. u64 *dma_pages;
  88. u32 len;
  89. int page_cnt, sg_dma_len;
  90. int i, j;
  91. int ret;
  92. sg_dma_len = ib_dma_map_sg(dev, sg, nents, DMA_BIDIRECTIONAL);
  93. if (unlikely(!sg_dma_len)) {
  94. pr_warn("RDS/IB: %s failed!\n", __func__);
  95. return -EBUSY;
  96. }
  97. len = 0;
  98. page_cnt = 0;
  99. for (i = 0; i < sg_dma_len; ++i) {
  100. unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]);
  101. u64 dma_addr = ib_sg_dma_address(dev, &scat[i]);
  102. if (dma_addr & ~PAGE_MASK) {
  103. if (i > 0)
  104. return -EINVAL;
  105. else
  106. ++page_cnt;
  107. }
  108. if ((dma_addr + dma_len) & ~PAGE_MASK) {
  109. if (i < sg_dma_len - 1)
  110. return -EINVAL;
  111. else
  112. ++page_cnt;
  113. }
  114. len += dma_len;
  115. }
  116. page_cnt += len >> PAGE_SHIFT;
  117. if (page_cnt > ibmr->pool->fmr_attr.max_pages)
  118. return -EINVAL;
  119. dma_pages = kmalloc_node(sizeof(u64) * page_cnt, GFP_ATOMIC,
  120. rdsibdev_to_node(rds_ibdev));
  121. if (!dma_pages)
  122. return -ENOMEM;
  123. page_cnt = 0;
  124. for (i = 0; i < sg_dma_len; ++i) {
  125. unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]);
  126. u64 dma_addr = ib_sg_dma_address(dev, &scat[i]);
  127. for (j = 0; j < dma_len; j += PAGE_SIZE)
  128. dma_pages[page_cnt++] =
  129. (dma_addr & PAGE_MASK) + j;
  130. }
  131. ret = ib_map_phys_fmr(fmr->fmr, dma_pages, page_cnt, io_addr);
  132. if (ret)
  133. goto out;
  134. /* Success - we successfully remapped the MR, so we can
  135. * safely tear down the old mapping.
  136. */
  137. rds_ib_teardown_mr(ibmr);
  138. ibmr->sg = scat;
  139. ibmr->sg_len = nents;
  140. ibmr->sg_dma_len = sg_dma_len;
  141. ibmr->remap_count++;
  142. if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
  143. rds_ib_stats_inc(s_ib_rdma_mr_8k_used);
  144. else
  145. rds_ib_stats_inc(s_ib_rdma_mr_1m_used);
  146. ret = 0;
  147. out:
  148. kfree(dma_pages);
  149. return ret;
  150. }
  151. struct rds_ib_mr *rds_ib_reg_fmr(struct rds_ib_device *rds_ibdev,
  152. struct scatterlist *sg,
  153. unsigned long nents,
  154. u32 *key)
  155. {
  156. struct rds_ib_mr *ibmr = NULL;
  157. struct rds_ib_fmr *fmr;
  158. int ret;
  159. ibmr = rds_ib_alloc_fmr(rds_ibdev, nents);
  160. if (IS_ERR(ibmr))
  161. return ibmr;
  162. ibmr->device = rds_ibdev;
  163. fmr = &ibmr->u.fmr;
  164. ret = rds_ib_map_fmr(rds_ibdev, ibmr, sg, nents);
  165. if (ret == 0)
  166. *key = fmr->fmr->rkey;
  167. else
  168. rds_ib_free_mr(ibmr, 0);
  169. return ibmr;
  170. }
  171. void rds_ib_unreg_fmr(struct list_head *list, unsigned int *nfreed,
  172. unsigned long *unpinned, unsigned int goal)
  173. {
  174. struct rds_ib_mr *ibmr, *next;
  175. struct rds_ib_fmr *fmr;
  176. LIST_HEAD(fmr_list);
  177. int ret = 0;
  178. unsigned int freed = *nfreed;
  179. /* String all ib_mr's onto one list and hand them to ib_unmap_fmr */
  180. list_for_each_entry(ibmr, list, unmap_list) {
  181. fmr = &ibmr->u.fmr;
  182. list_add(&fmr->fmr->list, &fmr_list);
  183. }
  184. ret = ib_unmap_fmr(&fmr_list);
  185. if (ret)
  186. pr_warn("RDS/IB: FMR invalidation failed (err=%d)\n", ret);
  187. /* Now we can destroy the DMA mapping and unpin any pages */
  188. list_for_each_entry_safe(ibmr, next, list, unmap_list) {
  189. fmr = &ibmr->u.fmr;
  190. *unpinned += ibmr->sg_len;
  191. __rds_ib_teardown_mr(ibmr);
  192. if (freed < goal ||
  193. ibmr->remap_count >= ibmr->pool->fmr_attr.max_maps) {
  194. if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
  195. rds_ib_stats_inc(s_ib_rdma_mr_8k_free);
  196. else
  197. rds_ib_stats_inc(s_ib_rdma_mr_1m_free);
  198. list_del(&ibmr->unmap_list);
  199. ib_dealloc_fmr(fmr->fmr);
  200. kfree(ibmr);
  201. freed++;
  202. }
  203. }
  204. *nfreed = freed;
  205. }
  206. void rds_ib_free_fmr_list(struct rds_ib_mr *ibmr)
  207. {
  208. struct rds_ib_mr_pool *pool = ibmr->pool;
  209. if (ibmr->remap_count >= pool->fmr_attr.max_maps)
  210. llist_add(&ibmr->llnode, &pool->drop_list);
  211. else
  212. llist_add(&ibmr->llnode, &pool->free_list);
  213. }