xref: /openbmc/linux/drivers/infiniband/hw/mlx4/ah.c (revision d2574c33)
1 /*
2  * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <rdma/ib_addr.h>
34 #include <rdma/ib_cache.h>
35 
36 #include <linux/slab.h>
37 #include <linux/inet.h>
38 #include <linux/string.h>
39 #include <linux/mlx4/driver.h>
40 
41 #include "mlx4_ib.h"
42 
43 static struct ib_ah *create_ib_ah(struct ib_pd *pd,
44 				  struct rdma_ah_attr *ah_attr,
45 				  struct mlx4_ib_ah *ah)
46 {
47 	struct mlx4_dev *dev = to_mdev(pd->device)->dev;
48 
49 	ah->av.ib.port_pd = cpu_to_be32(to_mpd(pd)->pdn |
50 			    (rdma_ah_get_port_num(ah_attr) << 24));
51 	ah->av.ib.g_slid  = rdma_ah_get_path_bits(ah_attr);
52 	ah->av.ib.sl_tclass_flowlabel =
53 			cpu_to_be32(rdma_ah_get_sl(ah_attr) << 28);
54 	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
55 		const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
56 
57 		ah->av.ib.g_slid   |= 0x80;
58 		ah->av.ib.gid_index = grh->sgid_index;
59 		ah->av.ib.hop_limit = grh->hop_limit;
60 		ah->av.ib.sl_tclass_flowlabel |=
61 			cpu_to_be32((grh->traffic_class << 20) |
62 				    grh->flow_label);
63 		memcpy(ah->av.ib.dgid, grh->dgid.raw, 16);
64 	}
65 
66 	ah->av.ib.dlid = cpu_to_be16(rdma_ah_get_dlid(ah_attr));
67 	if (rdma_ah_get_static_rate(ah_attr)) {
68 		u8 static_rate = rdma_ah_get_static_rate(ah_attr) +
69 					MLX4_STAT_RATE_OFFSET;
70 
71 		while (static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
72 		       !(1 << static_rate & dev->caps.stat_rate_support))
73 			--static_rate;
74 		ah->av.ib.stat_rate = static_rate;
75 	}
76 
77 	return &ah->ibah;
78 }
79 
80 static struct ib_ah *create_iboe_ah(struct ib_pd *pd,
81 				    struct rdma_ah_attr *ah_attr,
82 				    struct mlx4_ib_ah *ah)
83 {
84 	struct mlx4_ib_dev *ibdev = to_mdev(pd->device);
85 	const struct ib_gid_attr *gid_attr;
86 	struct mlx4_dev *dev = ibdev->dev;
87 	int is_mcast = 0;
88 	struct in6_addr in6;
89 	u16 vlan_tag = 0xffff;
90 	const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
91 	int ret;
92 
93 	memcpy(&in6, grh->dgid.raw, sizeof(in6));
94 	if (rdma_is_multicast_addr(&in6))
95 		is_mcast = 1;
96 
97 	memcpy(ah->av.eth.mac, ah_attr->roce.dmac, ETH_ALEN);
98 	eth_zero_addr(ah->av.eth.s_mac);
99 
100 	/*
101 	 * If sgid_attr is NULL we are being called by mlx4_ib_create_ah_slave
102 	 * and we are directly creating an AV for a slave's gid_index.
103 	 */
104 	gid_attr = ah_attr->grh.sgid_attr;
105 	if (gid_attr) {
106 		if (is_vlan_dev(gid_attr->ndev))
107 			vlan_tag = vlan_dev_vlan_id(gid_attr->ndev);
108 		memcpy(ah->av.eth.s_mac, gid_attr->ndev->dev_addr, ETH_ALEN);
109 		ret = mlx4_ib_gid_index_to_real_index(ibdev, gid_attr);
110 		if (ret < 0)
111 			return ERR_PTR(ret);
112 		ah->av.eth.gid_index = ret;
113 	} else {
114 		/* mlx4_ib_create_ah_slave fills in the s_mac and the vlan */
115 		ah->av.eth.gid_index = ah_attr->grh.sgid_index;
116 	}
117 
118 	if (vlan_tag < 0x1000)
119 		vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13;
120 	ah->av.eth.port_pd = cpu_to_be32(to_mpd(pd)->pdn |
121 					 (rdma_ah_get_port_num(ah_attr) << 24));
122 	ah->av.eth.vlan = cpu_to_be16(vlan_tag);
123 	ah->av.eth.hop_limit = grh->hop_limit;
124 	if (rdma_ah_get_static_rate(ah_attr)) {
125 		ah->av.eth.stat_rate = rdma_ah_get_static_rate(ah_attr) +
126 					MLX4_STAT_RATE_OFFSET;
127 		while (ah->av.eth.stat_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
128 		       !(1 << ah->av.eth.stat_rate & dev->caps.stat_rate_support))
129 			--ah->av.eth.stat_rate;
130 	}
131 	ah->av.eth.sl_tclass_flowlabel |=
132 			cpu_to_be32((grh->traffic_class << 20) |
133 				    grh->flow_label);
134 	/*
135 	 * HW requires multicast LID so we just choose one.
136 	 */
137 	if (is_mcast)
138 		ah->av.ib.dlid = cpu_to_be16(0xc000);
139 
140 	memcpy(ah->av.eth.dgid, grh->dgid.raw, 16);
141 	ah->av.eth.sl_tclass_flowlabel |= cpu_to_be32(rdma_ah_get_sl(ah_attr)
142 						      << 29);
143 	return &ah->ibah;
144 }
145 
146 struct ib_ah *mlx4_ib_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr,
147 				u32 flags, struct ib_udata *udata)
148 
149 {
150 	struct mlx4_ib_ah *ah;
151 	struct ib_ah *ret;
152 
153 	ah = kzalloc(sizeof *ah, GFP_ATOMIC);
154 	if (!ah)
155 		return ERR_PTR(-ENOMEM);
156 
157 	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
158 		if (!(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH)) {
159 			ret = ERR_PTR(-EINVAL);
160 		} else {
161 			/*
162 			 * TBD: need to handle the case when we get
163 			 * called in an atomic context and there we
164 			 * might sleep.  We don't expect this
165 			 * currently since we're working with link
166 			 * local addresses which we can translate
167 			 * without going to sleep.
168 			 */
169 			ret = create_iboe_ah(pd, ah_attr, ah);
170 		}
171 
172 		if (IS_ERR(ret))
173 			kfree(ah);
174 
175 		return ret;
176 	} else
177 		return create_ib_ah(pd, ah_attr, ah); /* never fails */
178 }
179 
180 /* AH's created via this call must be free'd by mlx4_ib_destroy_ah. */
181 struct ib_ah *mlx4_ib_create_ah_slave(struct ib_pd *pd,
182 				      struct rdma_ah_attr *ah_attr,
183 				      int slave_sgid_index, u8 *s_mac,
184 				      u16 vlan_tag)
185 {
186 	struct rdma_ah_attr slave_attr = *ah_attr;
187 	struct mlx4_ib_ah *mah;
188 	struct ib_ah *ah;
189 
190 	slave_attr.grh.sgid_attr = NULL;
191 	slave_attr.grh.sgid_index = slave_sgid_index;
192 	ah = mlx4_ib_create_ah(pd, &slave_attr, 0, NULL);
193 	if (IS_ERR(ah))
194 		return ah;
195 
196 	ah->device = pd->device;
197 	ah->pd = pd;
198 	ah->type = ah_attr->type;
199 	mah = to_mah(ah);
200 
201 	/* get rid of force-loopback bit */
202 	mah->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF);
203 
204 	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE)
205 		memcpy(mah->av.eth.s_mac, s_mac, 6);
206 
207 	if (vlan_tag < 0x1000)
208 		vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13;
209 	mah->av.eth.vlan = cpu_to_be16(vlan_tag);
210 
211 	return ah;
212 }
213 
214 int mlx4_ib_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr)
215 {
216 	struct mlx4_ib_ah *ah = to_mah(ibah);
217 	int port_num = be32_to_cpu(ah->av.ib.port_pd) >> 24;
218 
219 	memset(ah_attr, 0, sizeof *ah_attr);
220 	ah_attr->type = ibah->type;
221 
222 	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
223 		rdma_ah_set_dlid(ah_attr, 0);
224 		rdma_ah_set_sl(ah_attr,
225 			       be32_to_cpu(ah->av.eth.sl_tclass_flowlabel)
226 			       >> 29);
227 	} else {
228 		rdma_ah_set_dlid(ah_attr, be16_to_cpu(ah->av.ib.dlid));
229 		rdma_ah_set_sl(ah_attr,
230 			       be32_to_cpu(ah->av.ib.sl_tclass_flowlabel)
231 			       >> 28);
232 	}
233 
234 	rdma_ah_set_port_num(ah_attr, port_num);
235 	if (ah->av.ib.stat_rate)
236 		rdma_ah_set_static_rate(ah_attr,
237 					ah->av.ib.stat_rate -
238 					MLX4_STAT_RATE_OFFSET);
239 	rdma_ah_set_path_bits(ah_attr, ah->av.ib.g_slid & 0x7F);
240 	if (mlx4_ib_ah_grh_present(ah)) {
241 		u32 tc_fl = be32_to_cpu(ah->av.ib.sl_tclass_flowlabel);
242 
243 		rdma_ah_set_grh(ah_attr, NULL,
244 				tc_fl & 0xfffff, ah->av.ib.gid_index,
245 				ah->av.ib.hop_limit,
246 				tc_fl >> 20);
247 		rdma_ah_set_dgid_raw(ah_attr, ah->av.ib.dgid);
248 	}
249 
250 	return 0;
251 }
252 
253 int mlx4_ib_destroy_ah(struct ib_ah *ah, u32 flags)
254 {
255 	kfree(to_mah(ah));
256 	return 0;
257 }
258