mr.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/slab.h>
  34. #include "mlx4_ib.h"
  35. static u32 convert_access(int acc)
  36. {
  37. return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX4_PERM_ATOMIC : 0) |
  38. (acc & IB_ACCESS_REMOTE_WRITE ? MLX4_PERM_REMOTE_WRITE : 0) |
  39. (acc & IB_ACCESS_REMOTE_READ ? MLX4_PERM_REMOTE_READ : 0) |
  40. (acc & IB_ACCESS_LOCAL_WRITE ? MLX4_PERM_LOCAL_WRITE : 0) |
  41. MLX4_PERM_LOCAL_READ;
  42. }
  43. struct ib_mr *mlx4_ib_get_dma_mr(struct ib_pd *pd, int acc)
  44. {
  45. struct mlx4_ib_mr *mr;
  46. int err;
  47. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  48. if (!mr)
  49. return ERR_PTR(-ENOMEM);
  50. err = mlx4_mr_alloc(to_mdev(pd->device)->dev, to_mpd(pd)->pdn, 0,
  51. ~0ull, convert_access(acc), 0, 0, &mr->mmr);
  52. if (err)
  53. goto err_free;
  54. err = mlx4_mr_enable(to_mdev(pd->device)->dev, &mr->mmr);
  55. if (err)
  56. goto err_mr;
  57. mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
  58. mr->umem = NULL;
  59. return &mr->ibmr;
  60. err_mr:
  61. mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr);
  62. err_free:
  63. kfree(mr);
  64. return ERR_PTR(err);
  65. }
  66. int mlx4_ib_umem_write_mtt(struct mlx4_ib_dev *dev, struct mlx4_mtt *mtt,
  67. struct ib_umem *umem)
  68. {
  69. u64 *pages;
  70. struct ib_umem_chunk *chunk;
  71. int i, j, k;
  72. int n;
  73. int len;
  74. int err = 0;
  75. pages = (u64 *) __get_free_page(GFP_KERNEL);
  76. if (!pages)
  77. return -ENOMEM;
  78. i = n = 0;
  79. list_for_each_entry(chunk, &umem->chunk_list, list)
  80. for (j = 0; j < chunk->nmap; ++j) {
  81. len = sg_dma_len(&chunk->page_list[j]) >> mtt->page_shift;
  82. for (k = 0; k < len; ++k) {
  83. pages[i++] = sg_dma_address(&chunk->page_list[j]) +
  84. umem->page_size * k;
  85. /*
  86. * Be friendly to mlx4_write_mtt() and
  87. * pass it chunks of appropriate size.
  88. */
  89. if (i == PAGE_SIZE / sizeof (u64)) {
  90. err = mlx4_write_mtt(dev->dev, mtt, n,
  91. i, pages);
  92. if (err)
  93. goto out;
  94. n += i;
  95. i = 0;
  96. }
  97. }
  98. }
  99. if (i)
  100. err = mlx4_write_mtt(dev->dev, mtt, n, i, pages);
  101. out:
  102. free_page((unsigned long) pages);
  103. return err;
  104. }
  105. struct ib_mr *mlx4_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  106. u64 virt_addr, int access_flags,
  107. struct ib_udata *udata)
  108. {
  109. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  110. struct mlx4_ib_mr *mr;
  111. int shift;
  112. int err;
  113. int n;
  114. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  115. if (!mr)
  116. return ERR_PTR(-ENOMEM);
  117. mr->umem = ib_umem_get(pd->uobject->context, start, length,
  118. access_flags, 0);
  119. if (IS_ERR(mr->umem)) {
  120. err = PTR_ERR(mr->umem);
  121. goto err_free;
  122. }
  123. n = ib_umem_page_count(mr->umem);
  124. shift = ilog2(mr->umem->page_size);
  125. err = mlx4_mr_alloc(dev->dev, to_mpd(pd)->pdn, virt_addr, length,
  126. convert_access(access_flags), n, shift, &mr->mmr);
  127. if (err)
  128. goto err_umem;
  129. err = mlx4_ib_umem_write_mtt(dev, &mr->mmr.mtt, mr->umem);
  130. if (err)
  131. goto err_mr;
  132. err = mlx4_mr_enable(dev->dev, &mr->mmr);
  133. if (err)
  134. goto err_mr;
  135. mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
  136. return &mr->ibmr;
  137. err_mr:
  138. mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr);
  139. err_umem:
  140. ib_umem_release(mr->umem);
  141. err_free:
  142. kfree(mr);
  143. return ERR_PTR(err);
  144. }
  145. int mlx4_ib_dereg_mr(struct ib_mr *ibmr)
  146. {
  147. struct mlx4_ib_mr *mr = to_mmr(ibmr);
  148. mlx4_mr_free(to_mdev(ibmr->device)->dev, &mr->mmr);
  149. if (mr->umem)
  150. ib_umem_release(mr->umem);
  151. kfree(mr);
  152. return 0;
  153. }
  154. struct ib_mr *mlx4_ib_alloc_fast_reg_mr(struct ib_pd *pd,
  155. int max_page_list_len)
  156. {
  157. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  158. struct mlx4_ib_mr *mr;
  159. int err;
  160. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  161. if (!mr)
  162. return ERR_PTR(-ENOMEM);
  163. err = mlx4_mr_alloc(dev->dev, to_mpd(pd)->pdn, 0, 0, 0,
  164. max_page_list_len, 0, &mr->mmr);
  165. if (err)
  166. goto err_free;
  167. err = mlx4_mr_enable(dev->dev, &mr->mmr);
  168. if (err)
  169. goto err_mr;
  170. mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
  171. mr->umem = NULL;
  172. return &mr->ibmr;
  173. err_mr:
  174. mlx4_mr_free(dev->dev, &mr->mmr);
  175. err_free:
  176. kfree(mr);
  177. return ERR_PTR(err);
  178. }
  179. struct ib_fast_reg_page_list *mlx4_ib_alloc_fast_reg_page_list(struct ib_device *ibdev,
  180. int page_list_len)
  181. {
  182. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  183. struct mlx4_ib_fast_reg_page_list *mfrpl;
  184. int size = page_list_len * sizeof (u64);
  185. if (page_list_len > MLX4_MAX_FAST_REG_PAGES)
  186. return ERR_PTR(-EINVAL);
  187. mfrpl = kmalloc(sizeof *mfrpl, GFP_KERNEL);
  188. if (!mfrpl)
  189. return ERR_PTR(-ENOMEM);
  190. mfrpl->ibfrpl.page_list = kmalloc(size, GFP_KERNEL);
  191. if (!mfrpl->ibfrpl.page_list)
  192. goto err_free;
  193. mfrpl->mapped_page_list = dma_alloc_coherent(&dev->dev->pdev->dev,
  194. size, &mfrpl->map,
  195. GFP_KERNEL);
  196. if (!mfrpl->mapped_page_list)
  197. goto err_free;
  198. WARN_ON(mfrpl->map & 0x3f);
  199. return &mfrpl->ibfrpl;
  200. err_free:
  201. kfree(mfrpl->ibfrpl.page_list);
  202. kfree(mfrpl);
  203. return ERR_PTR(-ENOMEM);
  204. }
  205. void mlx4_ib_free_fast_reg_page_list(struct ib_fast_reg_page_list *page_list)
  206. {
  207. struct mlx4_ib_dev *dev = to_mdev(page_list->device);
  208. struct mlx4_ib_fast_reg_page_list *mfrpl = to_mfrpl(page_list);
  209. int size = page_list->max_page_list_len * sizeof (u64);
  210. dma_free_coherent(&dev->dev->pdev->dev, size, mfrpl->mapped_page_list,
  211. mfrpl->map);
  212. kfree(mfrpl->ibfrpl.page_list);
  213. kfree(mfrpl);
  214. }
  215. struct ib_fmr *mlx4_ib_fmr_alloc(struct ib_pd *pd, int acc,
  216. struct ib_fmr_attr *fmr_attr)
  217. {
  218. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  219. struct mlx4_ib_fmr *fmr;
  220. int err = -ENOMEM;
  221. fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
  222. if (!fmr)
  223. return ERR_PTR(-ENOMEM);
  224. err = mlx4_fmr_alloc(dev->dev, to_mpd(pd)->pdn, convert_access(acc),
  225. fmr_attr->max_pages, fmr_attr->max_maps,
  226. fmr_attr->page_shift, &fmr->mfmr);
  227. if (err)
  228. goto err_free;
  229. err = mlx4_fmr_enable(to_mdev(pd->device)->dev, &fmr->mfmr);
  230. if (err)
  231. goto err_mr;
  232. fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mfmr.mr.key;
  233. return &fmr->ibfmr;
  234. err_mr:
  235. mlx4_mr_free(to_mdev(pd->device)->dev, &fmr->mfmr.mr);
  236. err_free:
  237. kfree(fmr);
  238. return ERR_PTR(err);
  239. }
  240. int mlx4_ib_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
  241. int npages, u64 iova)
  242. {
  243. struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
  244. struct mlx4_ib_dev *dev = to_mdev(ifmr->ibfmr.device);
  245. return mlx4_map_phys_fmr(dev->dev, &ifmr->mfmr, page_list, npages, iova,
  246. &ifmr->ibfmr.lkey, &ifmr->ibfmr.rkey);
  247. }
  248. int mlx4_ib_unmap_fmr(struct list_head *fmr_list)
  249. {
  250. struct ib_fmr *ibfmr;
  251. int err;
  252. struct mlx4_dev *mdev = NULL;
  253. list_for_each_entry(ibfmr, fmr_list, list) {
  254. if (mdev && to_mdev(ibfmr->device)->dev != mdev)
  255. return -EINVAL;
  256. mdev = to_mdev(ibfmr->device)->dev;
  257. }
  258. if (!mdev)
  259. return 0;
  260. list_for_each_entry(ibfmr, fmr_list, list) {
  261. struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
  262. mlx4_fmr_unmap(mdev, &ifmr->mfmr, &ifmr->ibfmr.lkey, &ifmr->ibfmr.rkey);
  263. }
  264. /*
  265. * Make sure all MPT status updates are visible before issuing
  266. * SYNC_TPT firmware command.
  267. */
  268. wmb();
  269. err = mlx4_SYNC_TPT(mdev);
  270. if (err)
  271. printk(KERN_WARNING "mlx4_ib: SYNC_TPT error %d when "
  272. "unmapping FMRs\n", err);
  273. return 0;
  274. }
  275. int mlx4_ib_fmr_dealloc(struct ib_fmr *ibfmr)
  276. {
  277. struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
  278. struct mlx4_ib_dev *dev = to_mdev(ibfmr->device);
  279. int err;
  280. err = mlx4_fmr_free(dev->dev, &ifmr->mfmr);
  281. if (!err)
  282. kfree(ifmr);
  283. return err;
  284. }