pvrdma_verbs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 <asm/page.h>
  46. #include <linux/inet.h>
  47. #include <linux/io.h>
  48. #include <rdma/ib_addr.h>
  49. #include <rdma/ib_smi.h>
  50. #include <rdma/ib_user_verbs.h>
  51. #include <rdma/vmw_pvrdma-abi.h>
  52. #include "pvrdma.h"
  53. /**
  54. * pvrdma_query_device - query device
  55. * @ibdev: the device to query
  56. * @props: the device properties
  57. * @uhw: user data
  58. *
  59. * @return: 0 on success, otherwise negative errno
  60. */
  61. int pvrdma_query_device(struct ib_device *ibdev,
  62. struct ib_device_attr *props,
  63. struct ib_udata *uhw)
  64. {
  65. struct pvrdma_dev *dev = to_vdev(ibdev);
  66. if (uhw->inlen || uhw->outlen)
  67. return -EINVAL;
  68. memset(props, 0, sizeof(*props));
  69. props->fw_ver = dev->dsr->caps.fw_ver;
  70. props->sys_image_guid = dev->dsr->caps.sys_image_guid;
  71. props->max_mr_size = dev->dsr->caps.max_mr_size;
  72. props->page_size_cap = dev->dsr->caps.page_size_cap;
  73. props->vendor_id = dev->dsr->caps.vendor_id;
  74. props->vendor_part_id = dev->pdev->device;
  75. props->hw_ver = dev->dsr->caps.hw_ver;
  76. props->max_qp = dev->dsr->caps.max_qp;
  77. props->max_qp_wr = dev->dsr->caps.max_qp_wr;
  78. props->device_cap_flags = dev->dsr->caps.device_cap_flags;
  79. props->max_send_sge = dev->dsr->caps.max_sge;
  80. props->max_recv_sge = dev->dsr->caps.max_sge;
  81. props->max_sge_rd = PVRDMA_GET_CAP(dev, dev->dsr->caps.max_sge,
  82. dev->dsr->caps.max_sge_rd);
  83. props->max_srq = dev->dsr->caps.max_srq;
  84. props->max_srq_wr = dev->dsr->caps.max_srq_wr;
  85. props->max_srq_sge = dev->dsr->caps.max_srq_sge;
  86. props->max_cq = dev->dsr->caps.max_cq;
  87. props->max_cqe = dev->dsr->caps.max_cqe;
  88. props->max_mr = dev->dsr->caps.max_mr;
  89. props->max_pd = dev->dsr->caps.max_pd;
  90. props->max_qp_rd_atom = dev->dsr->caps.max_qp_rd_atom;
  91. props->max_qp_init_rd_atom = dev->dsr->caps.max_qp_init_rd_atom;
  92. props->atomic_cap =
  93. dev->dsr->caps.atomic_ops &
  94. (PVRDMA_ATOMIC_OP_COMP_SWAP | PVRDMA_ATOMIC_OP_FETCH_ADD) ?
  95. IB_ATOMIC_HCA : IB_ATOMIC_NONE;
  96. props->masked_atomic_cap = props->atomic_cap;
  97. props->max_ah = dev->dsr->caps.max_ah;
  98. props->max_pkeys = dev->dsr->caps.max_pkeys;
  99. props->local_ca_ack_delay = dev->dsr->caps.local_ca_ack_delay;
  100. if ((dev->dsr->caps.bmme_flags & PVRDMA_BMME_FLAG_LOCAL_INV) &&
  101. (dev->dsr->caps.bmme_flags & PVRDMA_BMME_FLAG_REMOTE_INV) &&
  102. (dev->dsr->caps.bmme_flags & PVRDMA_BMME_FLAG_FAST_REG_WR)) {
  103. props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
  104. props->max_fast_reg_page_list_len = PVRDMA_GET_CAP(dev,
  105. PVRDMA_MAX_FAST_REG_PAGES,
  106. dev->dsr->caps.max_fast_reg_page_list_len);
  107. }
  108. props->device_cap_flags |= IB_DEVICE_PORT_ACTIVE_EVENT |
  109. IB_DEVICE_RC_RNR_NAK_GEN;
  110. return 0;
  111. }
  112. /**
  113. * pvrdma_query_port - query device port attributes
  114. * @ibdev: the device to query
  115. * @port: the port number
  116. * @props: the device properties
  117. *
  118. * @return: 0 on success, otherwise negative errno
  119. */
  120. int pvrdma_query_port(struct ib_device *ibdev, u8 port,
  121. struct ib_port_attr *props)
  122. {
  123. struct pvrdma_dev *dev = to_vdev(ibdev);
  124. union pvrdma_cmd_req req;
  125. union pvrdma_cmd_resp rsp;
  126. struct pvrdma_cmd_query_port *cmd = &req.query_port;
  127. struct pvrdma_cmd_query_port_resp *resp = &rsp.query_port_resp;
  128. int err;
  129. memset(cmd, 0, sizeof(*cmd));
  130. cmd->hdr.cmd = PVRDMA_CMD_QUERY_PORT;
  131. cmd->port_num = port;
  132. err = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_QUERY_PORT_RESP);
  133. if (err < 0) {
  134. dev_warn(&dev->pdev->dev,
  135. "could not query port, error: %d\n", err);
  136. return err;
  137. }
  138. /* props being zeroed by the caller, avoid zeroing it here */
  139. props->state = pvrdma_port_state_to_ib(resp->attrs.state);
  140. props->max_mtu = pvrdma_mtu_to_ib(resp->attrs.max_mtu);
  141. props->active_mtu = pvrdma_mtu_to_ib(resp->attrs.active_mtu);
  142. props->gid_tbl_len = resp->attrs.gid_tbl_len;
  143. props->port_cap_flags =
  144. pvrdma_port_cap_flags_to_ib(resp->attrs.port_cap_flags);
  145. props->port_cap_flags |= IB_PORT_CM_SUP;
  146. props->ip_gids = true;
  147. props->max_msg_sz = resp->attrs.max_msg_sz;
  148. props->bad_pkey_cntr = resp->attrs.bad_pkey_cntr;
  149. props->qkey_viol_cntr = resp->attrs.qkey_viol_cntr;
  150. props->pkey_tbl_len = resp->attrs.pkey_tbl_len;
  151. props->lid = resp->attrs.lid;
  152. props->sm_lid = resp->attrs.sm_lid;
  153. props->lmc = resp->attrs.lmc;
  154. props->max_vl_num = resp->attrs.max_vl_num;
  155. props->sm_sl = resp->attrs.sm_sl;
  156. props->subnet_timeout = resp->attrs.subnet_timeout;
  157. props->init_type_reply = resp->attrs.init_type_reply;
  158. props->active_width = pvrdma_port_width_to_ib(resp->attrs.active_width);
  159. props->active_speed = pvrdma_port_speed_to_ib(resp->attrs.active_speed);
  160. props->phys_state = resp->attrs.phys_state;
  161. return 0;
  162. }
  163. /**
  164. * pvrdma_query_gid - query device gid
  165. * @ibdev: the device to query
  166. * @port: the port number
  167. * @index: the index
  168. * @gid: the device gid value
  169. *
  170. * @return: 0 on success, otherwise negative errno
  171. */
  172. int pvrdma_query_gid(struct ib_device *ibdev, u8 port, int index,
  173. union ib_gid *gid)
  174. {
  175. struct pvrdma_dev *dev = to_vdev(ibdev);
  176. if (index >= dev->dsr->caps.gid_tbl_len)
  177. return -EINVAL;
  178. memcpy(gid, &dev->sgid_tbl[index], sizeof(union ib_gid));
  179. return 0;
  180. }
  181. /**
  182. * pvrdma_query_pkey - query device port's P_Key table
  183. * @ibdev: the device to query
  184. * @port: the port number
  185. * @index: the index
  186. * @pkey: the device P_Key value
  187. *
  188. * @return: 0 on success, otherwise negative errno
  189. */
  190. int pvrdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  191. u16 *pkey)
  192. {
  193. int err = 0;
  194. union pvrdma_cmd_req req;
  195. union pvrdma_cmd_resp rsp;
  196. struct pvrdma_cmd_query_pkey *cmd = &req.query_pkey;
  197. memset(cmd, 0, sizeof(*cmd));
  198. cmd->hdr.cmd = PVRDMA_CMD_QUERY_PKEY;
  199. cmd->port_num = port;
  200. cmd->index = index;
  201. err = pvrdma_cmd_post(to_vdev(ibdev), &req, &rsp,
  202. PVRDMA_CMD_QUERY_PKEY_RESP);
  203. if (err < 0) {
  204. dev_warn(&to_vdev(ibdev)->pdev->dev,
  205. "could not query pkey, error: %d\n", err);
  206. return err;
  207. }
  208. *pkey = rsp.query_pkey_resp.pkey;
  209. return 0;
  210. }
  211. enum rdma_link_layer pvrdma_port_link_layer(struct ib_device *ibdev,
  212. u8 port)
  213. {
  214. return IB_LINK_LAYER_ETHERNET;
  215. }
  216. int pvrdma_modify_device(struct ib_device *ibdev, int mask,
  217. struct ib_device_modify *props)
  218. {
  219. unsigned long flags;
  220. if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
  221. IB_DEVICE_MODIFY_NODE_DESC)) {
  222. dev_warn(&to_vdev(ibdev)->pdev->dev,
  223. "unsupported device modify mask %#x\n", mask);
  224. return -EOPNOTSUPP;
  225. }
  226. if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
  227. spin_lock_irqsave(&to_vdev(ibdev)->desc_lock, flags);
  228. memcpy(ibdev->node_desc, props->node_desc, 64);
  229. spin_unlock_irqrestore(&to_vdev(ibdev)->desc_lock, flags);
  230. }
  231. if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
  232. mutex_lock(&to_vdev(ibdev)->port_mutex);
  233. to_vdev(ibdev)->sys_image_guid =
  234. cpu_to_be64(props->sys_image_guid);
  235. mutex_unlock(&to_vdev(ibdev)->port_mutex);
  236. }
  237. return 0;
  238. }
  239. /**
  240. * pvrdma_modify_port - modify device port attributes
  241. * @ibdev: the device to modify
  242. * @port: the port number
  243. * @mask: attributes to modify
  244. * @props: the device properties
  245. *
  246. * @return: 0 on success, otherwise negative errno
  247. */
  248. int pvrdma_modify_port(struct ib_device *ibdev, u8 port, int mask,
  249. struct ib_port_modify *props)
  250. {
  251. struct ib_port_attr attr;
  252. struct pvrdma_dev *vdev = to_vdev(ibdev);
  253. int ret;
  254. if (mask & ~IB_PORT_SHUTDOWN) {
  255. dev_warn(&vdev->pdev->dev,
  256. "unsupported port modify mask %#x\n", mask);
  257. return -EOPNOTSUPP;
  258. }
  259. mutex_lock(&vdev->port_mutex);
  260. ret = ib_query_port(ibdev, port, &attr);
  261. if (ret)
  262. goto out;
  263. vdev->port_cap_mask |= props->set_port_cap_mask;
  264. vdev->port_cap_mask &= ~props->clr_port_cap_mask;
  265. if (mask & IB_PORT_SHUTDOWN)
  266. vdev->ib_active = false;
  267. out:
  268. mutex_unlock(&vdev->port_mutex);
  269. return ret;
  270. }
  271. /**
  272. * pvrdma_alloc_ucontext - allocate ucontext
  273. * @ibdev: the IB device
  274. * @udata: user data
  275. *
  276. * @return: the ib_ucontext pointer on success, otherwise errno.
  277. */
  278. struct ib_ucontext *pvrdma_alloc_ucontext(struct ib_device *ibdev,
  279. struct ib_udata *udata)
  280. {
  281. struct pvrdma_dev *vdev = to_vdev(ibdev);
  282. struct pvrdma_ucontext *context;
  283. union pvrdma_cmd_req req;
  284. union pvrdma_cmd_resp rsp;
  285. struct pvrdma_cmd_create_uc *cmd = &req.create_uc;
  286. struct pvrdma_cmd_create_uc_resp *resp = &rsp.create_uc_resp;
  287. struct pvrdma_alloc_ucontext_resp uresp = {0};
  288. int ret;
  289. void *ptr;
  290. if (!vdev->ib_active)
  291. return ERR_PTR(-EAGAIN);
  292. context = kmalloc(sizeof(*context), GFP_KERNEL);
  293. if (!context)
  294. return ERR_PTR(-ENOMEM);
  295. context->dev = vdev;
  296. ret = pvrdma_uar_alloc(vdev, &context->uar);
  297. if (ret) {
  298. kfree(context);
  299. return ERR_PTR(-ENOMEM);
  300. }
  301. /* get ctx_handle from host */
  302. memset(cmd, 0, sizeof(*cmd));
  303. cmd->pfn = context->uar.pfn;
  304. cmd->hdr.cmd = PVRDMA_CMD_CREATE_UC;
  305. ret = pvrdma_cmd_post(vdev, &req, &rsp, PVRDMA_CMD_CREATE_UC_RESP);
  306. if (ret < 0) {
  307. dev_warn(&vdev->pdev->dev,
  308. "could not create ucontext, error: %d\n", ret);
  309. ptr = ERR_PTR(ret);
  310. goto err;
  311. }
  312. context->ctx_handle = resp->ctx_handle;
  313. /* copy back to user */
  314. uresp.qp_tab_size = vdev->dsr->caps.max_qp;
  315. ret = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
  316. if (ret) {
  317. pvrdma_uar_free(vdev, &context->uar);
  318. context->ibucontext.device = ibdev;
  319. pvrdma_dealloc_ucontext(&context->ibucontext);
  320. return ERR_PTR(-EFAULT);
  321. }
  322. return &context->ibucontext;
  323. err:
  324. pvrdma_uar_free(vdev, &context->uar);
  325. kfree(context);
  326. return ptr;
  327. }
  328. /**
  329. * pvrdma_dealloc_ucontext - deallocate ucontext
  330. * @ibcontext: the ucontext
  331. *
  332. * @return: 0 on success, otherwise errno.
  333. */
  334. int pvrdma_dealloc_ucontext(struct ib_ucontext *ibcontext)
  335. {
  336. struct pvrdma_ucontext *context = to_vucontext(ibcontext);
  337. union pvrdma_cmd_req req;
  338. struct pvrdma_cmd_destroy_uc *cmd = &req.destroy_uc;
  339. int ret;
  340. memset(cmd, 0, sizeof(*cmd));
  341. cmd->hdr.cmd = PVRDMA_CMD_DESTROY_UC;
  342. cmd->ctx_handle = context->ctx_handle;
  343. ret = pvrdma_cmd_post(context->dev, &req, NULL, 0);
  344. if (ret < 0)
  345. dev_warn(&context->dev->pdev->dev,
  346. "destroy ucontext failed, error: %d\n", ret);
  347. /* Free the UAR even if the device command failed */
  348. pvrdma_uar_free(to_vdev(ibcontext->device), &context->uar);
  349. kfree(context);
  350. return ret;
  351. }
  352. /**
  353. * pvrdma_mmap - create mmap region
  354. * @ibcontext: the user context
  355. * @vma: the VMA
  356. *
  357. * @return: 0 on success, otherwise errno.
  358. */
  359. int pvrdma_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
  360. {
  361. struct pvrdma_ucontext *context = to_vucontext(ibcontext);
  362. unsigned long start = vma->vm_start;
  363. unsigned long size = vma->vm_end - vma->vm_start;
  364. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  365. dev_dbg(&context->dev->pdev->dev, "create mmap region\n");
  366. if ((size != PAGE_SIZE) || (offset & ~PAGE_MASK)) {
  367. dev_warn(&context->dev->pdev->dev,
  368. "invalid params for mmap region\n");
  369. return -EINVAL;
  370. }
  371. /* Map UAR to kernel space, VM_LOCKED? */
  372. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  373. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  374. if (io_remap_pfn_range(vma, start, context->uar.pfn, size,
  375. vma->vm_page_prot))
  376. return -EAGAIN;
  377. return 0;
  378. }
  379. /**
  380. * pvrdma_alloc_pd - allocate protection domain
  381. * @ibdev: the IB device
  382. * @context: user context
  383. * @udata: user data
  384. *
  385. * @return: the ib_pd protection domain pointer on success, otherwise errno.
  386. */
  387. struct ib_pd *pvrdma_alloc_pd(struct ib_device *ibdev,
  388. struct ib_ucontext *context,
  389. struct ib_udata *udata)
  390. {
  391. struct pvrdma_pd *pd;
  392. struct pvrdma_dev *dev = to_vdev(ibdev);
  393. union pvrdma_cmd_req req;
  394. union pvrdma_cmd_resp rsp;
  395. struct pvrdma_cmd_create_pd *cmd = &req.create_pd;
  396. struct pvrdma_cmd_create_pd_resp *resp = &rsp.create_pd_resp;
  397. struct pvrdma_alloc_pd_resp pd_resp = {0};
  398. int ret;
  399. void *ptr;
  400. /* Check allowed max pds */
  401. if (!atomic_add_unless(&dev->num_pds, 1, dev->dsr->caps.max_pd))
  402. return ERR_PTR(-ENOMEM);
  403. pd = kmalloc(sizeof(*pd), GFP_KERNEL);
  404. if (!pd) {
  405. ptr = ERR_PTR(-ENOMEM);
  406. goto err;
  407. }
  408. memset(cmd, 0, sizeof(*cmd));
  409. cmd->hdr.cmd = PVRDMA_CMD_CREATE_PD;
  410. cmd->ctx_handle = (context) ? to_vucontext(context)->ctx_handle : 0;
  411. ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_PD_RESP);
  412. if (ret < 0) {
  413. dev_warn(&dev->pdev->dev,
  414. "failed to allocate protection domain, error: %d\n",
  415. ret);
  416. ptr = ERR_PTR(ret);
  417. goto freepd;
  418. }
  419. pd->privileged = !context;
  420. pd->pd_handle = resp->pd_handle;
  421. pd->pdn = resp->pd_handle;
  422. pd_resp.pdn = resp->pd_handle;
  423. if (context) {
  424. if (ib_copy_to_udata(udata, &pd_resp, sizeof(pd_resp))) {
  425. dev_warn(&dev->pdev->dev,
  426. "failed to copy back protection domain\n");
  427. pvrdma_dealloc_pd(&pd->ibpd);
  428. return ERR_PTR(-EFAULT);
  429. }
  430. }
  431. /* u32 pd handle */
  432. return &pd->ibpd;
  433. freepd:
  434. kfree(pd);
  435. err:
  436. atomic_dec(&dev->num_pds);
  437. return ptr;
  438. }
  439. /**
  440. * pvrdma_dealloc_pd - deallocate protection domain
  441. * @pd: the protection domain to be released
  442. *
  443. * @return: 0 on success, otherwise errno.
  444. */
  445. int pvrdma_dealloc_pd(struct ib_pd *pd)
  446. {
  447. struct pvrdma_dev *dev = to_vdev(pd->device);
  448. union pvrdma_cmd_req req;
  449. struct pvrdma_cmd_destroy_pd *cmd = &req.destroy_pd;
  450. int ret;
  451. memset(cmd, 0, sizeof(*cmd));
  452. cmd->hdr.cmd = PVRDMA_CMD_DESTROY_PD;
  453. cmd->pd_handle = to_vpd(pd)->pd_handle;
  454. ret = pvrdma_cmd_post(dev, &req, NULL, 0);
  455. if (ret)
  456. dev_warn(&dev->pdev->dev,
  457. "could not dealloc protection domain, error: %d\n",
  458. ret);
  459. kfree(to_vpd(pd));
  460. atomic_dec(&dev->num_pds);
  461. return 0;
  462. }
  463. /**
  464. * pvrdma_create_ah - create an address handle
  465. * @pd: the protection domain
  466. * @ah_attr: the attributes of the AH
  467. * @udata: user data blob
  468. *
  469. * @return: the ib_ah pointer on success, otherwise errno.
  470. */
  471. struct ib_ah *pvrdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr,
  472. struct ib_udata *udata)
  473. {
  474. struct pvrdma_dev *dev = to_vdev(pd->device);
  475. struct pvrdma_ah *ah;
  476. const struct ib_global_route *grh;
  477. u8 port_num = rdma_ah_get_port_num(ah_attr);
  478. if (!(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH))
  479. return ERR_PTR(-EINVAL);
  480. grh = rdma_ah_read_grh(ah_attr);
  481. if ((ah_attr->type != RDMA_AH_ATTR_TYPE_ROCE) ||
  482. rdma_is_multicast_addr((struct in6_addr *)grh->dgid.raw))
  483. return ERR_PTR(-EINVAL);
  484. if (!atomic_add_unless(&dev->num_ahs, 1, dev->dsr->caps.max_ah))
  485. return ERR_PTR(-ENOMEM);
  486. ah = kzalloc(sizeof(*ah), GFP_ATOMIC);
  487. if (!ah) {
  488. atomic_dec(&dev->num_ahs);
  489. return ERR_PTR(-ENOMEM);
  490. }
  491. ah->av.port_pd = to_vpd(pd)->pd_handle | (port_num << 24);
  492. ah->av.src_path_bits = rdma_ah_get_path_bits(ah_attr);
  493. ah->av.src_path_bits |= 0x80;
  494. ah->av.gid_index = grh->sgid_index;
  495. ah->av.hop_limit = grh->hop_limit;
  496. ah->av.sl_tclass_flowlabel = (grh->traffic_class << 20) |
  497. grh->flow_label;
  498. memcpy(ah->av.dgid, grh->dgid.raw, 16);
  499. memcpy(ah->av.dmac, ah_attr->roce.dmac, ETH_ALEN);
  500. ah->ibah.device = pd->device;
  501. ah->ibah.pd = pd;
  502. ah->ibah.uobject = NULL;
  503. return &ah->ibah;
  504. }
  505. /**
  506. * pvrdma_destroy_ah - destroy an address handle
  507. * @ah: the address handle to destroyed
  508. *
  509. * @return: 0 on success.
  510. */
  511. int pvrdma_destroy_ah(struct ib_ah *ah)
  512. {
  513. struct pvrdma_dev *dev = to_vdev(ah->device);
  514. kfree(to_vah(ah));
  515. atomic_dec(&dev->num_ahs);
  516. return 0;
  517. }