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 <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <rdma/ib_addr.h>
37 #include <rdma/ib_cache.h>
38
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/etherdevice.h>
42
43 #include "mlx4_ib.h"
44
create_ib_ah(struct ib_ah * ib_ah,struct rdma_ah_attr * ah_attr)45 static int create_ib_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr)
46 {
47 struct ib_pd *pd = ib_ah->pd;
48 struct mlx4_ib_ah *ah = to_mah(ib_ah);
49 struct mlx4_dev *dev = to_mdev(ib_ah->device)->dev;
50
51 ah->av.ib.port_pd = cpu_to_be32(to_mpd(pd)->pdn |
52 (rdma_ah_get_port_num(ah_attr) << 24));
53 ah->av.ib.g_slid = rdma_ah_get_path_bits(ah_attr);
54 ah->av.ib.sl_tclass_flowlabel = cpu_to_be32(rdma_ah_get_sl(ah_attr) << 28);
55 if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
56 const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
57
58 ah->av.ib.g_slid |= 0x80;
59 ah->av.ib.gid_index = grh->sgid_index;
60 ah->av.ib.hop_limit = grh->hop_limit;
61 ah->av.ib.sl_tclass_flowlabel |=
62 cpu_to_be32((grh->traffic_class << 20) |
63 grh->flow_label);
64 memcpy(ah->av.ib.dgid, grh->dgid.raw, 16);
65 }
66
67 ah->av.ib.dlid = cpu_to_be16(rdma_ah_get_dlid(ah_attr));
68 if (rdma_ah_get_static_rate(ah_attr)) {
69 ah->av.ib.stat_rate = rdma_ah_get_static_rate(ah_attr) +
70 MLX4_STAT_RATE_OFFSET;
71 while (ah->av.ib.stat_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
72 !(1 << ah->av.ib.stat_rate & dev->caps.stat_rate_support))
73 --ah->av.ib.stat_rate;
74 }
75 return 0;
76 }
77
create_iboe_ah(struct ib_ah * ib_ah,struct rdma_ah_attr * ah_attr)78 static int create_iboe_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr)
79 {
80 struct ib_pd *pd = ib_ah->pd;
81 struct mlx4_ib_dev *ibdev = to_mdev(ib_ah->device);
82 const struct ib_gid_attr *gid_attr;
83 struct mlx4_ib_ah *ah = to_mah(ib_ah);
84 struct mlx4_dev *dev = ibdev->dev;
85 const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
86 int is_mcast = 0;
87 struct in6_addr in6;
88 u16 vlan_tag = 0xffff;
89 int ret;
90
91 memcpy(&in6, grh->dgid.raw, sizeof(in6));
92 if (rdma_is_multicast_addr(&in6))
93 is_mcast = 1;
94
95 memcpy(ah->av.eth.mac, ah_attr->roce.dmac, ETH_ALEN);
96 eth_zero_addr(ah->av.eth.s_mac);
97 gid_attr = ah_attr->grh.sgid_attr;
98 ret = rdma_read_gid_l2_fields(gid_attr, &vlan_tag,
99 &ah->av.eth.s_mac[0]);
100 if (ret)
101 return ret;
102
103 if (vlan_tag < 0x1000)
104 vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13;
105 ah->av.eth.port_pd = cpu_to_be32(to_mpd(pd)->pdn |
106 (rdma_ah_get_port_num(ah_attr) << 24));
107 ret = mlx4_ib_gid_index_to_real_index(ibdev, gid_attr);
108 if (ret < 0)
109 return ret;
110 ah->av.eth.gid_index = ret;
111 ah->av.eth.vlan = cpu_to_be16(vlan_tag);
112 ah->av.eth.hop_limit = grh->hop_limit;
113 if (rdma_ah_get_static_rate(ah_attr)) {
114 ah->av.eth.stat_rate = rdma_ah_get_static_rate(ah_attr) +
115 MLX4_STAT_RATE_OFFSET;
116 while (ah->av.eth.stat_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
117 !(1 << ah->av.eth.stat_rate & dev->caps.stat_rate_support))
118 --ah->av.eth.stat_rate;
119 }
120
121 /*
122 * HW requires multicast LID so we just choose one.
123 */
124 if (is_mcast)
125 ah->av.ib.dlid = cpu_to_be16(0xc000);
126
127 memcpy(ah->av.eth.dgid, grh->dgid.raw, 16);
128 ah->av.eth.sl_tclass_flowlabel =
129 cpu_to_be32(rdma_ah_get_sl(ah_attr) << 29);
130
131 return 0;
132 }
133
mlx4_ib_create_ah(struct ib_ah * ib_ah,struct rdma_ah_attr * ah_attr,u32 flags,struct ib_udata * udata)134 int mlx4_ib_create_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr,
135 u32 flags, struct ib_udata *udata)
136 {
137 if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
138 if (!(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH)) {
139 return -EINVAL;
140 } else {
141 /*
142 * TBD: need to handle the case when we get
143 * called in an atomic context and there we
144 * might sleep. We don't expect this
145 * currently since we're working with link
146 * local addresses which we can translate
147 * without going to sleep.
148 */
149 return create_iboe_ah(ib_ah, ah_attr);
150 }
151 }
152 return create_ib_ah(ib_ah, ah_attr);
153 }
154
mlx4_ib_create_ah_slave(struct ib_ah * ah,struct rdma_ah_attr * ah_attr,int slave_sgid_index,u8 * s_mac,u16 vlan_tag)155 int mlx4_ib_create_ah_slave(struct ib_ah *ah, struct rdma_ah_attr *ah_attr,
156 int slave_sgid_index, u8 *s_mac, u16 vlan_tag)
157 {
158 struct rdma_ah_attr slave_attr = *ah_attr;
159 struct mlx4_ib_ah *mah = to_mah(ah);
160 int ret;
161
162 slave_attr.grh.sgid_index = slave_sgid_index;
163 ret = mlx4_ib_create_ah(ah, &slave_attr, 0, NULL);
164 if (ret)
165 return ret;
166
167 ah->type = ah_attr->type;
168
169 /* get rid of force-loopback bit */
170 mah->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF);
171
172 if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE)
173 memcpy(mah->av.eth.s_mac, s_mac, 6);
174
175 if (vlan_tag < 0x1000)
176 vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13;
177 mah->av.eth.vlan = cpu_to_be16(vlan_tag);
178
179 return 0;
180 }
181
mlx4_ib_query_ah(struct ib_ah * ibah,struct rdma_ah_attr * ah_attr)182 int mlx4_ib_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr)
183 {
184 struct mlx4_ib_ah *ah = to_mah(ibah);
185 int port_num = be32_to_cpu(ah->av.ib.port_pd) >> 24;
186
187 memset(ah_attr, 0, sizeof *ah_attr);
188 ah_attr->type = ibah->type;
189
190 if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
191 rdma_ah_set_dlid(ah_attr, 0);
192 rdma_ah_set_sl(ah_attr,
193 be32_to_cpu(ah->av.eth.sl_tclass_flowlabel)
194 >> 29);
195 } else {
196 rdma_ah_set_dlid(ah_attr, be16_to_cpu(ah->av.ib.dlid));
197 rdma_ah_set_sl(ah_attr,
198 be32_to_cpu(ah->av.ib.sl_tclass_flowlabel)
199 >> 28);
200 }
201
202 rdma_ah_set_port_num(ah_attr, port_num);
203 if (ah->av.ib.stat_rate)
204 rdma_ah_set_static_rate(ah_attr,
205 ah->av.ib.stat_rate -
206 MLX4_STAT_RATE_OFFSET);
207 rdma_ah_set_path_bits(ah_attr, ah->av.ib.g_slid & 0x7F);
208 if (mlx4_ib_ah_grh_present(ah)) {
209 u32 tc_fl = be32_to_cpu(ah->av.ib.sl_tclass_flowlabel);
210
211 rdma_ah_set_grh(ah_attr, NULL, tc_fl & 0xfffff,
212 ah->av.ib.gid_index, ah->av.ib.hop_limit,
213 tc_fl >> 20);
214 rdma_ah_set_dgid_raw(ah_attr, ah->av.ib.dgid);
215 }
216
217 return 0;
218 }
219
mlx4_ib_destroy_ah(struct ib_ah * ah,u32 flags)220 void mlx4_ib_destroy_ah(struct ib_ah *ah, u32 flags)
221 {
222 return;
223 }
224