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