1 /* This file is part of the Emulex RoCE Device Driver for
2 * RoCE (RDMA over Converged Ethernet) adapters.
3 * Copyright (C) 2012-2015 Emulex. All rights reserved.
4 * EMULEX and SLI are trademarks of Emulex.
7 * This software is available to you under a choice of one of two licenses.
8 * You may choose to be licensed under the terms of the GNU General Public
9 * License (GPL) Version 2, available from the file COPYING in the main
10 * directory of this source tree, or the BSD license below:
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * - Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Contact Information:
36 * linux-drivers@emulex.com
40 * Costa Mesa, CA 92626
43 #include <net/neighbour.h>
44 #include <net/netevent.h>
46 #include <rdma/ib_addr.h>
47 #include <rdma/ib_mad.h>
48 #include <rdma/ib_cache.h>
51 #include "ocrdma_verbs.h"
52 #include "ocrdma_ah.h"
53 #include "ocrdma_hw.h"
54 #include "ocrdma_stats.h"
56 #define OCRDMA_VID_PCP_SHIFT 0xD
58 static u16 ocrdma_hdr_type_to_proto_num(int devid, u8 hdr_type)
61 case OCRDMA_L3_TYPE_IB_GRH:
62 return (u16)ETH_P_IBOE;
63 case OCRDMA_L3_TYPE_IPV4:
65 case OCRDMA_L3_TYPE_IPV6:
68 pr_err("ocrdma%d: Invalid network header\n", devid);
73 static 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)
78 struct ocrdma_eth_vlan eth;
79 struct ocrdma_grh grh;
84 const struct ib_global_route *ib_grh;
86 struct sockaddr _sockaddr;
87 struct sockaddr_in _sockaddr_in;
88 struct sockaddr_in6 _sockaddr_in6;
89 } sgid_addr, dgid_addr;
91 memset(ð, 0, sizeof(eth));
92 memset(&grh, 0, sizeof(grh));
95 proto_num = ocrdma_hdr_type_to_proto_num(dev->id, ah->hdr_type);
98 nxthdr = (proto_num == ETH_P_IBOE) ? 0x1b : 0x11;
100 if (!vlan_tag || (vlan_tag > 0xFFF))
101 vlan_tag = dev->pvid;
102 if (vlan_tag || dev->pfc_state) {
104 pr_err("ocrdma%d:Using VLAN with PFC is recommended\n",
106 pr_err("ocrdma%d:Using VLAN 0 for this connection\n",
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);
116 eth.eth_type = cpu_to_be16(proto_num);
117 eth_sz = sizeof(struct ocrdma_eth_basic);
120 memcpy(ð.smac[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
121 status = ocrdma_resolve_dmac(dev, attr, ð.dmac[0]);
124 ib_grh = rdma_ah_read_grh(attr);
125 ah->sgid_index = ib_grh->sgid_index;
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));
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) |
146 memcpy(&grh.dgid[0], ib_grh->dgid.raw,
147 sizeof(ib_grh->dgid.raw));
148 grh.pdid_hoplimit = cpu_to_be32((pdid << 16) |
151 memcpy((u8 *)ah->av + eth_sz, &grh, sizeof(struct ocrdma_grh));
154 ah->av->valid |= OCRDMA_AV_VLAN_VALID;
155 ah->av->valid = cpu_to_le32(ah->av->valid);
159 struct ib_ah *ocrdma_create_ah(struct ib_pd *ibpd, struct rdma_ah_attr *attr,
160 struct ib_udata *udata)
164 struct ocrdma_ah *ah;
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;
173 if ((attr->type != RDMA_AH_ATTR_TYPE_ROCE) ||
174 !(rdma_ah_get_ah_flags(attr) & IB_AH_GRH))
175 return ERR_PTR(-EINVAL);
177 grh = rdma_ah_read_grh(attr);
178 if (atomic_cmpxchg(&dev->update_sl, 1, 0))
179 ocrdma_init_service_level(dev);
181 ah = kzalloc(sizeof(*ah), GFP_ATOMIC);
183 return ERR_PTR(-ENOMEM);
185 status = ocrdma_alloc_av(dev, ah);
189 status = ib_get_cached_gid(&dev->ibdev, 1, grh->sgid_index, &sgid,
192 pr_err("%s(): Failed to query sgid, status = %d\n",
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);
201 /* Get network header type for this GID */
202 ah->hdr_type = ib_gid_to_network_type(sgid_attr.gid_type, &sgid);
205 (!rdma_is_multicast_addr((struct in6_addr *)grh->dgid.raw)) &&
206 (!rdma_link_local_addr((struct in6_addr *)grh->dgid.raw))) {
207 status = rdma_addr_find_l2_eth_by_grh(&sgid, &grh->dgid,
210 &sgid_attr.ndev->ifindex,
213 pr_err("%s(): Failed to resolve dmac from gid."
214 "status = %d\n", __func__, status);
219 status = set_av_attr(dev, ah, attr, &sgid, pd->id, &isvlan, vlan_tag);
223 /* if pd is for the user process, pass the ah_id to user space */
224 if ((pd->uctx) && (pd->uctx->ah_tbl.va)) {
225 ahid_addr = pd->uctx->ah_tbl.va + rdma_ah_get_dlid(attr);
227 *ahid_addr |= ah->id & OCRDMA_AH_ID_MASK;
228 if (ocrdma_is_udp_encap_supported(dev)) {
229 *ahid_addr |= ((u32)ah->hdr_type &
230 OCRDMA_AH_L3_TYPE_MASK) <<
231 OCRDMA_AH_L3_TYPE_SHIFT;
234 *ahid_addr |= (OCRDMA_AH_VLAN_VALID_MASK <<
235 OCRDMA_AH_VLAN_VALID_SHIFT);
241 ocrdma_free_av(dev, ah);
244 return ERR_PTR(status);
247 int ocrdma_destroy_ah(struct ib_ah *ibah)
249 struct ocrdma_ah *ah = get_ocrdma_ah(ibah);
250 struct ocrdma_dev *dev = get_ocrdma_dev(ibah->device);
252 ocrdma_free_av(dev, ah);
257 int ocrdma_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
259 struct ocrdma_ah *ah = get_ocrdma_ah(ibah);
260 struct ocrdma_av *av = ah->av;
261 struct ocrdma_grh *grh;
263 attr->type = ibah->type;
264 if (ah->av->valid & OCRDMA_AV_VALID) {
265 grh = (struct ocrdma_grh *)((u8 *)ah->av +
266 sizeof(struct ocrdma_eth_vlan));
267 rdma_ah_set_sl(attr, be16_to_cpu(av->eth_hdr.vlan_tag) >> 13);
269 grh = (struct ocrdma_grh *)((u8 *)ah->av +
270 sizeof(struct ocrdma_eth_basic));
271 rdma_ah_set_sl(attr, 0);
273 rdma_ah_set_grh(attr, NULL,
274 be32_to_cpu(grh->tclass_flow) & 0xffffffff,
276 be32_to_cpu(grh->pdid_hoplimit) & 0xff,
277 be32_to_cpu(grh->tclass_flow) >> 24);
278 rdma_ah_set_dgid_raw(attr, &grh->dgid[0]);
282 int ocrdma_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
284 /* modify_ah is unsupported */
288 int ocrdma_process_mad(struct ib_device *ibdev,
289 int process_mad_flags,
291 const struct ib_wc *in_wc,
292 const struct ib_grh *in_grh,
293 const struct ib_mad_hdr *in, size_t in_mad_size,
294 struct ib_mad_hdr *out, size_t *out_mad_size,
295 u16 *out_mad_pkey_index)
298 struct ocrdma_dev *dev;
299 const struct ib_mad *in_mad = (const struct ib_mad *)in;
300 struct ib_mad *out_mad = (struct ib_mad *)out;
302 if (WARN_ON_ONCE(in_mad_size != sizeof(*in_mad) ||
303 *out_mad_size != sizeof(*out_mad)))
304 return IB_MAD_RESULT_FAILURE;
306 switch (in_mad->mad_hdr.mgmt_class) {
307 case IB_MGMT_CLASS_PERF_MGMT:
308 dev = get_ocrdma_dev(ibdev);
309 if (!ocrdma_pma_counters(dev, out_mad))
310 status = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
312 status = IB_MAD_RESULT_SUCCESS;
315 status = IB_MAD_RESULT_SUCCESS;