xref: /OK3568_Linux_fs/kernel/drivers/infiniband/sw/rdmavt/ah.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright(c) 2016 - 2019 Intel Corporation.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This file is provided under a dual BSD/GPLv2 license.  When using or
5*4882a593Smuzhiyun  * redistributing this file, you may do so under either license.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * GPL LICENSE SUMMARY
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  * it under the terms of version 2 of the GNU General Public License as
11*4882a593Smuzhiyun  * published by the Free Software Foundation.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun  * WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16*4882a593Smuzhiyun  * General Public License for more details.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * BSD LICENSE
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
21*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
22*4882a593Smuzhiyun  * are met:
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  *  - Redistributions of source code must retain the above copyright
25*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
26*4882a593Smuzhiyun  *  - Redistributions in binary form must reproduce the above copyright
27*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in
28*4882a593Smuzhiyun  *    the documentation and/or other materials provided with the
29*4882a593Smuzhiyun  *    distribution.
30*4882a593Smuzhiyun  *  - Neither the name of Intel Corporation nor the names of its
31*4882a593Smuzhiyun  *    contributors may be used to endorse or promote products derived
32*4882a593Smuzhiyun  *    from this software without specific prior written permission.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include <linux/slab.h>
49*4882a593Smuzhiyun #include "ah.h"
50*4882a593Smuzhiyun #include "vt.h" /* for prints */
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /**
53*4882a593Smuzhiyun  * rvt_check_ah - validate the attributes of AH
54*4882a593Smuzhiyun  * @ibdev: the ib device
55*4882a593Smuzhiyun  * @ah_attr: the attributes of the AH
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * If driver supports a more detailed check_ah function call back to it
58*4882a593Smuzhiyun  * otherwise just check the basics.
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * Return: 0 on success
61*4882a593Smuzhiyun  */
rvt_check_ah(struct ib_device * ibdev,struct rdma_ah_attr * ah_attr)62*4882a593Smuzhiyun int rvt_check_ah(struct ib_device *ibdev,
63*4882a593Smuzhiyun 		 struct rdma_ah_attr *ah_attr)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	int err;
66*4882a593Smuzhiyun 	int port_num = rdma_ah_get_port_num(ah_attr);
67*4882a593Smuzhiyun 	struct ib_port_attr port_attr;
68*4882a593Smuzhiyun 	struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
69*4882a593Smuzhiyun 	u8 ah_flags = rdma_ah_get_ah_flags(ah_attr);
70*4882a593Smuzhiyun 	u8 static_rate = rdma_ah_get_static_rate(ah_attr);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	err = ib_query_port(ibdev, port_num, &port_attr);
73*4882a593Smuzhiyun 	if (err)
74*4882a593Smuzhiyun 		return -EINVAL;
75*4882a593Smuzhiyun 	if (port_num < 1 ||
76*4882a593Smuzhiyun 	    port_num > ibdev->phys_port_cnt)
77*4882a593Smuzhiyun 		return -EINVAL;
78*4882a593Smuzhiyun 	if (static_rate != IB_RATE_PORT_CURRENT &&
79*4882a593Smuzhiyun 	    ib_rate_to_mbps(static_rate) < 0)
80*4882a593Smuzhiyun 		return -EINVAL;
81*4882a593Smuzhiyun 	if ((ah_flags & IB_AH_GRH) &&
82*4882a593Smuzhiyun 	    rdma_ah_read_grh(ah_attr)->sgid_index >= port_attr.gid_tbl_len)
83*4882a593Smuzhiyun 		return -EINVAL;
84*4882a593Smuzhiyun 	if (rdi->driver_f.check_ah)
85*4882a593Smuzhiyun 		return rdi->driver_f.check_ah(ibdev, ah_attr);
86*4882a593Smuzhiyun 	return 0;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun EXPORT_SYMBOL(rvt_check_ah);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /**
91*4882a593Smuzhiyun  * rvt_create_ah - create an address handle
92*4882a593Smuzhiyun  * @ibah: the IB address handle
93*4882a593Smuzhiyun  * @init_attr: the attributes of the AH
94*4882a593Smuzhiyun  * @udata: pointer to user's input output buffer information.
95*4882a593Smuzhiyun  *
96*4882a593Smuzhiyun  * This may be called from interrupt context.
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * Return: 0 on success
99*4882a593Smuzhiyun  */
rvt_create_ah(struct ib_ah * ibah,struct rdma_ah_init_attr * init_attr,struct ib_udata * udata)100*4882a593Smuzhiyun int rvt_create_ah(struct ib_ah *ibah, struct rdma_ah_init_attr *init_attr,
101*4882a593Smuzhiyun 		  struct ib_udata *udata)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	struct rvt_ah *ah = ibah_to_rvtah(ibah);
104*4882a593Smuzhiyun 	struct rvt_dev_info *dev = ib_to_rvt(ibah->device);
105*4882a593Smuzhiyun 	unsigned long flags;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	if (rvt_check_ah(ibah->device, init_attr->ah_attr))
108*4882a593Smuzhiyun 		return -EINVAL;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->n_ahs_lock, flags);
111*4882a593Smuzhiyun 	if (dev->n_ahs_allocated == dev->dparms.props.max_ah) {
112*4882a593Smuzhiyun 		spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
113*4882a593Smuzhiyun 		return -ENOMEM;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	dev->n_ahs_allocated++;
117*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	rdma_copy_ah_attr(&ah->attr, init_attr->ah_attr);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	if (dev->driver_f.notify_new_ah)
122*4882a593Smuzhiyun 		dev->driver_f.notify_new_ah(ibah->device,
123*4882a593Smuzhiyun 					    init_attr->ah_attr, ah);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	return 0;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun /**
129*4882a593Smuzhiyun  * rvt_destory_ah - Destory an address handle
130*4882a593Smuzhiyun  * @ibah: address handle
131*4882a593Smuzhiyun  * @destroy_flags: destroy address handle flags (see enum rdma_destroy_ah_flags)
132*4882a593Smuzhiyun  *
133*4882a593Smuzhiyun  * Return: 0 on success
134*4882a593Smuzhiyun  */
rvt_destroy_ah(struct ib_ah * ibah,u32 destroy_flags)135*4882a593Smuzhiyun int rvt_destroy_ah(struct ib_ah *ibah, u32 destroy_flags)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	struct rvt_dev_info *dev = ib_to_rvt(ibah->device);
138*4882a593Smuzhiyun 	struct rvt_ah *ah = ibah_to_rvtah(ibah);
139*4882a593Smuzhiyun 	unsigned long flags;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	spin_lock_irqsave(&dev->n_ahs_lock, flags);
142*4882a593Smuzhiyun 	dev->n_ahs_allocated--;
143*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	rdma_destroy_ah_attr(&ah->attr);
146*4882a593Smuzhiyun 	return 0;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun /**
150*4882a593Smuzhiyun  * rvt_modify_ah - modify an ah with given attrs
151*4882a593Smuzhiyun  * @ibah: address handle to modify
152*4882a593Smuzhiyun  * @ah_attr: attrs to apply
153*4882a593Smuzhiyun  *
154*4882a593Smuzhiyun  * Return: 0 on success
155*4882a593Smuzhiyun  */
rvt_modify_ah(struct ib_ah * ibah,struct rdma_ah_attr * ah_attr)156*4882a593Smuzhiyun int rvt_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	struct rvt_ah *ah = ibah_to_rvtah(ibah);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	if (rvt_check_ah(ibah->device, ah_attr))
161*4882a593Smuzhiyun 		return -EINVAL;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	ah->attr = *ah_attr;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	return 0;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun /**
169*4882a593Smuzhiyun  * rvt_query_ah - return attrs for ah
170*4882a593Smuzhiyun  * @ibah: address handle to query
171*4882a593Smuzhiyun  * @ah_attr: return info in this
172*4882a593Smuzhiyun  *
173*4882a593Smuzhiyun  * Return: always 0
174*4882a593Smuzhiyun  */
rvt_query_ah(struct ib_ah * ibah,struct rdma_ah_attr * ah_attr)175*4882a593Smuzhiyun int rvt_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	struct rvt_ah *ah = ibah_to_rvtah(ibah);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	*ah_attr = ah->attr;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	return 0;
182*4882a593Smuzhiyun }
183