1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43#include <net/neighbour.h>
44#include <net/netevent.h>
45
46#include <rdma/ib_addr.h>
47#include <rdma/ib_mad.h>
48#include <rdma/ib_cache.h>
49
50#include "ocrdma.h"
51#include "ocrdma_verbs.h"
52#include "ocrdma_ah.h"
53#include "ocrdma_hw.h"
54#include "ocrdma_stats.h"
55
56#define OCRDMA_VID_PCP_SHIFT 0xD
57
58static u16 ocrdma_hdr_type_to_proto_num(int devid, u8 hdr_type)
59{
60 switch (hdr_type) {
61 case OCRDMA_L3_TYPE_IB_GRH:
62 return (u16)ETH_P_IBOE;
63 case OCRDMA_L3_TYPE_IPV4:
64 return (u16)0x0800;
65 case OCRDMA_L3_TYPE_IPV6:
66 return (u16)0x86dd;
67 default:
68 pr_err("ocrdma%d: Invalid network header\n", devid);
69 return 0;
70 }
71}
72
73static inline int set_av_attr(struct ocrdma_dev *dev, struct ocrdma_ah *ah,
74 struct rdma_ah_attr *attr, union ib_gid *sgid,
75 int pdid, bool *isvlan, u16 vlan_tag)
76{
77 int status;
78 struct ocrdma_eth_vlan eth;
79 struct ocrdma_grh grh;
80 int eth_sz;
81 u16 proto_num = 0;
82 u8 nxthdr = 0x11;
83 struct iphdr ipv4;
84 const struct ib_global_route *ib_grh;
85 union {
86 struct sockaddr _sockaddr;
87 struct sockaddr_in _sockaddr_in;
88 struct sockaddr_in6 _sockaddr_in6;
89 } sgid_addr, dgid_addr;
90
91 memset(ð, 0, sizeof(eth));
92 memset(&grh, 0, sizeof(grh));
93
94
95 proto_num = ocrdma_hdr_type_to_proto_num(dev->id, ah->hdr_type);
96 if (!proto_num)
97 return -EINVAL;
98 nxthdr = (proto_num == ETH_P_IBOE) ? 0x1b : 0x11;
99
100 if (!vlan_tag || (vlan_tag > 0xFFF))
101 vlan_tag = dev->pvid;
102 if (vlan_tag || dev->pfc_state) {
103 if (!vlan_tag) {
104 pr_err("ocrdma%d:Using VLAN with PFC is recommended\n",
105 dev->id);
106 pr_err("ocrdma%d:Using VLAN 0 for this connection\n",
107 dev->id);
108 }
109 eth.eth_type = cpu_to_be16(0x8100);
110 eth.roce_eth_type = cpu_to_be16(proto_num);
111 vlan_tag |= (dev->sl & 0x07) << OCRDMA_VID_PCP_SHIFT;
112 eth.vlan_tag = cpu_to_be16(vlan_tag);
113 eth_sz = sizeof(struct ocrdma_eth_vlan);
114 *isvlan = true;
115 } else {
116 eth.eth_type = cpu_to_be16(proto_num);
117 eth_sz = sizeof(struct ocrdma_eth_basic);
118 }
119
120 memcpy(ð.smac[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
121 status = ocrdma_resolve_dmac(dev, attr, ð.dmac[0]);
122 if (status)
123 return status;
124 ib_grh = rdma_ah_read_grh(attr);
125 ah->sgid_index = ib_grh->sgid_index;
126
127 memcpy(&ah->av->eth_hdr, ð, eth_sz);
128 if (ah->hdr_type == RDMA_NETWORK_IPV4) {
129 *((__be16 *)&ipv4) = htons((4 << 12) | (5 << 8) |
130 ib_grh->traffic_class);
131 ipv4.id = cpu_to_be16(pdid);
132 ipv4.frag_off = htons(IP_DF);
133 ipv4.tot_len = htons(0);
134 ipv4.ttl = ib_grh->hop_limit;
135 ipv4.protocol = nxthdr;
136 rdma_gid2ip(&sgid_addr._sockaddr, sgid);
137 ipv4.saddr = sgid_addr._sockaddr_in.sin_addr.s_addr;
138 rdma_gid2ip(&dgid_addr._sockaddr, &ib_grh->dgid);
139 ipv4.daddr = dgid_addr._sockaddr_in.sin_addr.s_addr;
140 memcpy((u8 *)ah->av + eth_sz, &ipv4, sizeof(struct iphdr));
141 } else {
142 memcpy(&grh.sgid[0], sgid->raw, sizeof(union ib_gid));
143 grh.tclass_flow = cpu_to_be32((6 << 28) |
144 (ib_grh->traffic_class << 24) |
145 ib_grh->flow_label);
146 memcpy(&grh.dgid[0], ib_grh->dgid.raw,
147 sizeof(ib_grh->dgid.raw));
148 grh.pdid_hoplimit = cpu_to_be32((pdid << 16) |
149 (nxthdr << 8) |
150 ib_grh->hop_limit);
151 memcpy((u8 *)ah->av + eth_sz, &grh, sizeof(struct ocrdma_grh));
152 }
153 if (*isvlan)
154 ah->av->valid |= OCRDMA_AV_VLAN_VALID;
155 ah->av->valid = cpu_to_le32(ah->av->valid);
156 return status;
157}
158
159struct ib_ah *ocrdma_create_ah(struct ib_pd *ibpd, struct rdma_ah_attr *attr,
160 struct ib_udata *udata)
161{
162 u32 *ahid_addr;
163 int status;
164 struct ocrdma_ah *ah;
165 bool isvlan = false;
166 u16 vlan_tag = 0xffff;
167 struct ib_gid_attr sgid_attr;
168 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
169 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
170 const struct ib_global_route *grh;
171 union ib_gid sgid;
172
173 if ((attr->type != RDMA_AH_ATTR_TYPE_ROCE) ||
174 !(rdma_ah_get_ah_flags(attr) & IB_AH_GRH))
175 return ERR_PTR(-EINVAL);
176
177 grh = rdma_ah_read_grh(attr);
178 if (atomic_cmpxchg(&dev->update_sl, 1, 0))
179 ocrdma_init_service_level(dev);
180
181 ah = kzalloc(sizeof(*ah), GFP_ATOMIC);
182 if (!ah)
183 return ERR_PTR(-ENOMEM);
184
185 status = ocrdma_alloc_av(dev, ah);
186 if (status)
187 goto av_err;
188
189 status = ib_get_cached_gid(&dev->ibdev, 1, grh->sgid_index, &sgid,
190 &sgid_attr);
191 if (status) {
192 pr_err("%s(): Failed to query sgid, status = %d\n",
193 __func__, status);
194 goto av_conf_err;
195 }
196 if (sgid_attr.ndev) {
197 if (is_vlan_dev(sgid_attr.ndev))
198 vlan_tag = vlan_dev_vlan_id(sgid_attr.ndev);
199 dev_put(sgid_attr.ndev);
200 }
201
202 ah->hdr_type = ib_gid_to_network_type(sgid_attr.gid_type, &sgid);
203
204 status = set_av_attr(dev, ah, attr, &sgid, pd->id, &isvlan, vlan_tag);
205 if (status)
206 goto av_conf_err;
207
208
209 if ((pd->uctx) && (pd->uctx->ah_tbl.va)) {
210 ahid_addr = pd->uctx->ah_tbl.va + rdma_ah_get_dlid(attr);
211 *ahid_addr = 0;
212 *ahid_addr |= ah->id & OCRDMA_AH_ID_MASK;
213 if (ocrdma_is_udp_encap_supported(dev)) {
214 *ahid_addr |= ((u32)ah->hdr_type &
215 OCRDMA_AH_L3_TYPE_MASK) <<
216 OCRDMA_AH_L3_TYPE_SHIFT;
217 }
218 if (isvlan)
219 *ahid_addr |= (OCRDMA_AH_VLAN_VALID_MASK <<
220 OCRDMA_AH_VLAN_VALID_SHIFT);
221 }
222
223 return &ah->ibah;
224
225av_conf_err:
226 ocrdma_free_av(dev, ah);
227av_err:
228 kfree(ah);
229 return ERR_PTR(status);
230}
231
232int ocrdma_destroy_ah(struct ib_ah *ibah)
233{
234 struct ocrdma_ah *ah = get_ocrdma_ah(ibah);
235 struct ocrdma_dev *dev = get_ocrdma_dev(ibah->device);
236
237 ocrdma_free_av(dev, ah);
238 kfree(ah);
239 return 0;
240}
241
242int ocrdma_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
243{
244 struct ocrdma_ah *ah = get_ocrdma_ah(ibah);
245 struct ocrdma_av *av = ah->av;
246 struct ocrdma_grh *grh;
247
248 attr->type = ibah->type;
249 if (ah->av->valid & OCRDMA_AV_VALID) {
250 grh = (struct ocrdma_grh *)((u8 *)ah->av +
251 sizeof(struct ocrdma_eth_vlan));
252 rdma_ah_set_sl(attr, be16_to_cpu(av->eth_hdr.vlan_tag) >> 13);
253 } else {
254 grh = (struct ocrdma_grh *)((u8 *)ah->av +
255 sizeof(struct ocrdma_eth_basic));
256 rdma_ah_set_sl(attr, 0);
257 }
258 rdma_ah_set_grh(attr, NULL,
259 be32_to_cpu(grh->tclass_flow) & 0xffffffff,
260 ah->sgid_index,
261 be32_to_cpu(grh->pdid_hoplimit) & 0xff,
262 be32_to_cpu(grh->tclass_flow) >> 24);
263 rdma_ah_set_dgid_raw(attr, &grh->dgid[0]);
264 return 0;
265}
266
267int ocrdma_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
268{
269
270 return -ENOSYS;
271}
272
273int ocrdma_process_mad(struct ib_device *ibdev,
274 int process_mad_flags,
275 u8 port_num,
276 const struct ib_wc *in_wc,
277 const struct ib_grh *in_grh,
278 const struct ib_mad_hdr *in, size_t in_mad_size,
279 struct ib_mad_hdr *out, size_t *out_mad_size,
280 u16 *out_mad_pkey_index)
281{
282 int status;
283 struct ocrdma_dev *dev;
284 const struct ib_mad *in_mad = (const struct ib_mad *)in;
285 struct ib_mad *out_mad = (struct ib_mad *)out;
286
287 if (WARN_ON_ONCE(in_mad_size != sizeof(*in_mad) ||
288 *out_mad_size != sizeof(*out_mad)))
289 return IB_MAD_RESULT_FAILURE;
290
291 switch (in_mad->mad_hdr.mgmt_class) {
292 case IB_MGMT_CLASS_PERF_MGMT:
293 dev = get_ocrdma_dev(ibdev);
294 if (!ocrdma_pma_counters(dev, out_mad))
295 status = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
296 else
297 status = IB_MAD_RESULT_SUCCESS;
298 break;
299 default:
300 status = IB_MAD_RESULT_SUCCESS;
301 break;
302 }
303 return status;
304}
305