pvrdma_verbs.c 17 KB

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