pvrdma_misc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of EITHER the GNU General Public License
  6. * version 2 as published by the Free Software Foundation or the BSD
  7. * 2-Clause License. This program is distributed in the hope that it
  8. * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
  9. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License version 2 for more details at
  11. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program available in the file COPYING in the main
  15. * directory of this source tree.
  16. *
  17. * The BSD 2-Clause License
  18. *
  19. * Redistribution and use in source and binary forms, with or
  20. * without modification, are permitted provided that the following
  21. * conditions are met:
  22. *
  23. * - Redistributions of source code must retain the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer.
  26. *
  27. * - Redistributions in binary form must reproduce the above
  28. * copyright notice, this list of conditions and the following
  29. * disclaimer in the documentation and/or other materials
  30. * provided with the distribution.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  33. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  34. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  35. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  36. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  37. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  38. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  39. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  43. * OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. #include <linux/errno.h>
  46. #include <linux/slab.h>
  47. #include <linux/bitmap.h>
  48. #include "pvrdma.h"
  49. int pvrdma_page_dir_init(struct pvrdma_dev *dev, struct pvrdma_page_dir *pdir,
  50. u64 npages, bool alloc_pages)
  51. {
  52. u64 i;
  53. if (npages > PVRDMA_PAGE_DIR_MAX_PAGES)
  54. return -EINVAL;
  55. memset(pdir, 0, sizeof(*pdir));
  56. pdir->dir = dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
  57. &pdir->dir_dma, GFP_KERNEL);
  58. if (!pdir->dir)
  59. goto err;
  60. pdir->ntables = PVRDMA_PAGE_DIR_TABLE(npages - 1) + 1;
  61. pdir->tables = kcalloc(pdir->ntables, sizeof(*pdir->tables),
  62. GFP_KERNEL);
  63. if (!pdir->tables)
  64. goto err;
  65. for (i = 0; i < pdir->ntables; i++) {
  66. pdir->tables[i] = dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
  67. (dma_addr_t *)&pdir->dir[i],
  68. GFP_KERNEL);
  69. if (!pdir->tables[i])
  70. goto err;
  71. }
  72. pdir->npages = npages;
  73. if (alloc_pages) {
  74. pdir->pages = kcalloc(npages, sizeof(*pdir->pages),
  75. GFP_KERNEL);
  76. if (!pdir->pages)
  77. goto err;
  78. for (i = 0; i < pdir->npages; i++) {
  79. dma_addr_t page_dma;
  80. pdir->pages[i] = dma_alloc_coherent(&dev->pdev->dev,
  81. PAGE_SIZE,
  82. &page_dma,
  83. GFP_KERNEL);
  84. if (!pdir->pages[i])
  85. goto err;
  86. pvrdma_page_dir_insert_dma(pdir, i, page_dma);
  87. }
  88. }
  89. return 0;
  90. err:
  91. pvrdma_page_dir_cleanup(dev, pdir);
  92. return -ENOMEM;
  93. }
  94. static u64 *pvrdma_page_dir_table(struct pvrdma_page_dir *pdir, u64 idx)
  95. {
  96. return pdir->tables[PVRDMA_PAGE_DIR_TABLE(idx)];
  97. }
  98. dma_addr_t pvrdma_page_dir_get_dma(struct pvrdma_page_dir *pdir, u64 idx)
  99. {
  100. return pvrdma_page_dir_table(pdir, idx)[PVRDMA_PAGE_DIR_PAGE(idx)];
  101. }
  102. static void pvrdma_page_dir_cleanup_pages(struct pvrdma_dev *dev,
  103. struct pvrdma_page_dir *pdir)
  104. {
  105. if (pdir->pages) {
  106. u64 i;
  107. for (i = 0; i < pdir->npages && pdir->pages[i]; i++) {
  108. dma_addr_t page_dma = pvrdma_page_dir_get_dma(pdir, i);
  109. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  110. pdir->pages[i], page_dma);
  111. }
  112. kfree(pdir->pages);
  113. }
  114. }
  115. static void pvrdma_page_dir_cleanup_tables(struct pvrdma_dev *dev,
  116. struct pvrdma_page_dir *pdir)
  117. {
  118. if (pdir->tables) {
  119. int i;
  120. pvrdma_page_dir_cleanup_pages(dev, pdir);
  121. for (i = 0; i < pdir->ntables; i++) {
  122. u64 *table = pdir->tables[i];
  123. if (table)
  124. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  125. table, pdir->dir[i]);
  126. }
  127. kfree(pdir->tables);
  128. }
  129. }
  130. void pvrdma_page_dir_cleanup(struct pvrdma_dev *dev,
  131. struct pvrdma_page_dir *pdir)
  132. {
  133. if (pdir->dir) {
  134. pvrdma_page_dir_cleanup_tables(dev, pdir);
  135. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  136. pdir->dir, pdir->dir_dma);
  137. }
  138. }
  139. int pvrdma_page_dir_insert_dma(struct pvrdma_page_dir *pdir, u64 idx,
  140. dma_addr_t daddr)
  141. {
  142. u64 *table;
  143. if (idx >= pdir->npages)
  144. return -EINVAL;
  145. table = pvrdma_page_dir_table(pdir, idx);
  146. table[PVRDMA_PAGE_DIR_PAGE(idx)] = daddr;
  147. return 0;
  148. }
  149. int pvrdma_page_dir_insert_umem(struct pvrdma_page_dir *pdir,
  150. struct ib_umem *umem, u64 offset)
  151. {
  152. u64 i = offset;
  153. int j, entry;
  154. int ret = 0, len = 0;
  155. struct scatterlist *sg;
  156. if (offset >= pdir->npages)
  157. return -EINVAL;
  158. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  159. len = sg_dma_len(sg) >> PAGE_SHIFT;
  160. for (j = 0; j < len; j++) {
  161. dma_addr_t addr = sg_dma_address(sg) +
  162. (j << umem->page_shift);
  163. ret = pvrdma_page_dir_insert_dma(pdir, i, addr);
  164. if (ret)
  165. goto exit;
  166. i++;
  167. }
  168. }
  169. exit:
  170. return ret;
  171. }
  172. int pvrdma_page_dir_insert_page_list(struct pvrdma_page_dir *pdir,
  173. u64 *page_list,
  174. int num_pages)
  175. {
  176. int i;
  177. int ret;
  178. if (num_pages > pdir->npages)
  179. return -EINVAL;
  180. for (i = 0; i < num_pages; i++) {
  181. ret = pvrdma_page_dir_insert_dma(pdir, i, page_list[i]);
  182. if (ret)
  183. return ret;
  184. }
  185. return 0;
  186. }
  187. void pvrdma_qp_cap_to_ib(struct ib_qp_cap *dst, const struct pvrdma_qp_cap *src)
  188. {
  189. dst->max_send_wr = src->max_send_wr;
  190. dst->max_recv_wr = src->max_recv_wr;
  191. dst->max_send_sge = src->max_send_sge;
  192. dst->max_recv_sge = src->max_recv_sge;
  193. dst->max_inline_data = src->max_inline_data;
  194. }
  195. void ib_qp_cap_to_pvrdma(struct pvrdma_qp_cap *dst, const struct ib_qp_cap *src)
  196. {
  197. dst->max_send_wr = src->max_send_wr;
  198. dst->max_recv_wr = src->max_recv_wr;
  199. dst->max_send_sge = src->max_send_sge;
  200. dst->max_recv_sge = src->max_recv_sge;
  201. dst->max_inline_data = src->max_inline_data;
  202. }
  203. void pvrdma_gid_to_ib(union ib_gid *dst, const union pvrdma_gid *src)
  204. {
  205. BUILD_BUG_ON(sizeof(union pvrdma_gid) != sizeof(union ib_gid));
  206. memcpy(dst, src, sizeof(*src));
  207. }
  208. void ib_gid_to_pvrdma(union pvrdma_gid *dst, const union ib_gid *src)
  209. {
  210. BUILD_BUG_ON(sizeof(union pvrdma_gid) != sizeof(union ib_gid));
  211. memcpy(dst, src, sizeof(*src));
  212. }
  213. void pvrdma_global_route_to_ib(struct ib_global_route *dst,
  214. const struct pvrdma_global_route *src)
  215. {
  216. pvrdma_gid_to_ib(&dst->dgid, &src->dgid);
  217. dst->flow_label = src->flow_label;
  218. dst->sgid_index = src->sgid_index;
  219. dst->hop_limit = src->hop_limit;
  220. dst->traffic_class = src->traffic_class;
  221. }
  222. void ib_global_route_to_pvrdma(struct pvrdma_global_route *dst,
  223. const struct ib_global_route *src)
  224. {
  225. ib_gid_to_pvrdma(&dst->dgid, &src->dgid);
  226. dst->flow_label = src->flow_label;
  227. dst->sgid_index = src->sgid_index;
  228. dst->hop_limit = src->hop_limit;
  229. dst->traffic_class = src->traffic_class;
  230. }
  231. void pvrdma_ah_attr_to_rdma(struct rdma_ah_attr *dst,
  232. const struct pvrdma_ah_attr *src)
  233. {
  234. dst->type = RDMA_AH_ATTR_TYPE_ROCE;
  235. pvrdma_global_route_to_ib(rdma_ah_retrieve_grh(dst), &src->grh);
  236. rdma_ah_set_dlid(dst, src->dlid);
  237. rdma_ah_set_sl(dst, src->sl);
  238. rdma_ah_set_path_bits(dst, src->src_path_bits);
  239. rdma_ah_set_static_rate(dst, src->static_rate);
  240. rdma_ah_set_ah_flags(dst, src->ah_flags);
  241. rdma_ah_set_port_num(dst, src->port_num);
  242. memcpy(dst->roce.dmac, &src->dmac, ETH_ALEN);
  243. }
  244. void rdma_ah_attr_to_pvrdma(struct pvrdma_ah_attr *dst,
  245. const struct rdma_ah_attr *src)
  246. {
  247. ib_global_route_to_pvrdma(&dst->grh, rdma_ah_read_grh(src));
  248. dst->dlid = rdma_ah_get_dlid(src);
  249. dst->sl = rdma_ah_get_sl(src);
  250. dst->src_path_bits = rdma_ah_get_path_bits(src);
  251. dst->static_rate = rdma_ah_get_static_rate(src);
  252. dst->ah_flags = rdma_ah_get_ah_flags(src);
  253. dst->port_num = rdma_ah_get_port_num(src);
  254. memcpy(&dst->dmac, src->roce.dmac, sizeof(dst->dmac));
  255. }
  256. u8 ib_gid_type_to_pvrdma(enum ib_gid_type gid_type)
  257. {
  258. return (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ?
  259. PVRDMA_GID_TYPE_FLAG_ROCE_V2 :
  260. PVRDMA_GID_TYPE_FLAG_ROCE_V1;
  261. }