pvrdma_verbs.c 16 KB

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