GNU Linux-libre 4.9.333-gnu1
[releases.git] / drivers / infiniband / hw / ocrdma / ocrdma_ah.c
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.
5  * www.emulex.com
6  *
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:
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  *
16  * - Redistributions of source code must retain the above copyright notice,
17  *   this list of conditions and the following disclaimer.
18  *
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.
22  *
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.
34  *
35  * Contact Information:
36  * linux-drivers@emulex.com
37  *
38  * Emulex
39  * 3333 Susan Street
40  * Costa Mesa, CA 92626
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
58 static 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)0x8915;
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
73 static inline int set_av_attr(struct ocrdma_dev *dev, struct ocrdma_ah *ah,
74                         struct ib_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         union {
85                 struct sockaddr_in  _sockaddr_in;
86                 struct sockaddr_in6 _sockaddr_in6;
87         } sgid_addr, dgid_addr;
88
89         memset(&eth, 0, sizeof(eth));
90         memset(&grh, 0, sizeof(grh));
91
92         /* Protocol Number */
93         proto_num = ocrdma_hdr_type_to_proto_num(dev->id, ah->hdr_type);
94         if (!proto_num)
95                 return -EINVAL;
96         nxthdr = (proto_num == 0x8915) ? 0x1b : 0x11;
97         /* VLAN */
98         if (!vlan_tag || (vlan_tag > 0xFFF))
99                 vlan_tag = dev->pvid;
100         if (vlan_tag || dev->pfc_state) {
101                 if (!vlan_tag) {
102                         pr_err("ocrdma%d:Using VLAN with PFC is recommended\n",
103                                 dev->id);
104                         pr_err("ocrdma%d:Using VLAN 0 for this connection\n",
105                                 dev->id);
106                 }
107                 eth.eth_type = cpu_to_be16(0x8100);
108                 eth.roce_eth_type = cpu_to_be16(proto_num);
109                 vlan_tag |= (dev->sl & 0x07) << OCRDMA_VID_PCP_SHIFT;
110                 eth.vlan_tag = cpu_to_be16(vlan_tag);
111                 eth_sz = sizeof(struct ocrdma_eth_vlan);
112                 *isvlan = true;
113         } else {
114                 eth.eth_type = cpu_to_be16(proto_num);
115                 eth_sz = sizeof(struct ocrdma_eth_basic);
116         }
117         /* MAC */
118         memcpy(&eth.smac[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
119         status = ocrdma_resolve_dmac(dev, attr, &eth.dmac[0]);
120         if (status)
121                 return status;
122         ah->sgid_index = attr->grh.sgid_index;
123         /* Eth HDR */
124         memcpy(&ah->av->eth_hdr, &eth, eth_sz);
125         if (ah->hdr_type == RDMA_NETWORK_IPV4) {
126                 *((__be16 *)&ipv4) = htons((4 << 12) | (5 << 8) |
127                                            attr->grh.traffic_class);
128                 ipv4.id = cpu_to_be16(pdid);
129                 ipv4.frag_off = htons(IP_DF);
130                 ipv4.tot_len = htons(0);
131                 ipv4.ttl = attr->grh.hop_limit;
132                 ipv4.protocol = nxthdr;
133                 rdma_gid2ip((struct sockaddr *)&sgid_addr, sgid);
134                 ipv4.saddr = sgid_addr._sockaddr_in.sin_addr.s_addr;
135                 rdma_gid2ip((struct sockaddr *)&dgid_addr, &attr->grh.dgid);
136                 ipv4.daddr = dgid_addr._sockaddr_in.sin_addr.s_addr;
137                 memcpy((u8 *)ah->av + eth_sz, &ipv4, sizeof(struct iphdr));
138         } else {
139                 memcpy(&grh.sgid[0], sgid->raw, sizeof(union ib_gid));
140                 grh.tclass_flow = cpu_to_be32((6 << 28) |
141                                               (attr->grh.traffic_class << 24) |
142                                               attr->grh.flow_label);
143                 memcpy(&grh.dgid[0], attr->grh.dgid.raw,
144                        sizeof(attr->grh.dgid.raw));
145                 grh.pdid_hoplimit = cpu_to_be32((pdid << 16) |
146                                                 (nxthdr << 8) |
147                                                 attr->grh.hop_limit);
148                 memcpy((u8 *)ah->av + eth_sz, &grh, sizeof(struct ocrdma_grh));
149         }
150         if (*isvlan)
151                 ah->av->valid |= OCRDMA_AV_VLAN_VALID;
152         ah->av->valid = cpu_to_le32(ah->av->valid);
153         return status;
154 }
155
156 struct ib_ah *ocrdma_create_ah(struct ib_pd *ibpd, struct ib_ah_attr *attr)
157 {
158         u32 *ahid_addr;
159         int status;
160         struct ocrdma_ah *ah;
161         bool isvlan = false;
162         u16 vlan_tag = 0xffff;
163         struct ib_gid_attr sgid_attr;
164         struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
165         struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
166         union ib_gid sgid;
167
168         if (!(attr->ah_flags & IB_AH_GRH))
169                 return ERR_PTR(-EINVAL);
170
171         if (atomic_cmpxchg(&dev->update_sl, 1, 0))
172                 ocrdma_init_service_level(dev);
173
174         ah = kzalloc(sizeof(*ah), GFP_ATOMIC);
175         if (!ah)
176                 return ERR_PTR(-ENOMEM);
177
178         status = ocrdma_alloc_av(dev, ah);
179         if (status)
180                 goto av_err;
181
182         status = ib_get_cached_gid(&dev->ibdev, 1, attr->grh.sgid_index, &sgid,
183                                    &sgid_attr);
184         if (status) {
185                 pr_err("%s(): Failed to query sgid, status = %d\n",
186                       __func__, status);
187                 goto av_conf_err;
188         }
189         if (sgid_attr.ndev) {
190                 if (is_vlan_dev(sgid_attr.ndev))
191                         vlan_tag = vlan_dev_vlan_id(sgid_attr.ndev);
192                 dev_put(sgid_attr.ndev);
193         }
194         /* Get network header type for this GID */
195         ah->hdr_type = ib_gid_to_network_type(sgid_attr.gid_type, &sgid);
196
197         if ((pd->uctx) &&
198             (!rdma_is_multicast_addr((struct in6_addr *)attr->grh.dgid.raw)) &&
199             (!rdma_link_local_addr((struct in6_addr *)attr->grh.dgid.raw))) {
200                 status = rdma_addr_find_l2_eth_by_grh(&sgid, &attr->grh.dgid,
201                                                       attr->dmac, &vlan_tag,
202                                                       &sgid_attr.ndev->ifindex,
203                                                       NULL);
204                 if (status) {
205                         pr_err("%s(): Failed to resolve dmac from gid." 
206                                 "status = %d\n", __func__, status);
207                         goto av_conf_err;
208                 }
209         }
210
211         status = set_av_attr(dev, ah, attr, &sgid, pd->id, &isvlan, vlan_tag);
212         if (status)
213                 goto av_conf_err;
214
215         /* if pd is for the user process, pass the ah_id to user space */
216         if ((pd->uctx) && (pd->uctx->ah_tbl.va)) {
217                 ahid_addr = pd->uctx->ah_tbl.va + attr->dlid;
218                 *ahid_addr = 0;
219                 *ahid_addr |= ah->id & OCRDMA_AH_ID_MASK;
220                 if (ocrdma_is_udp_encap_supported(dev)) {
221                         *ahid_addr |= ((u32)ah->hdr_type &
222                                        OCRDMA_AH_L3_TYPE_MASK) <<
223                                        OCRDMA_AH_L3_TYPE_SHIFT;
224                 }
225                 if (isvlan)
226                         *ahid_addr |= (OCRDMA_AH_VLAN_VALID_MASK <<
227                                        OCRDMA_AH_VLAN_VALID_SHIFT);
228         }
229
230         return &ah->ibah;
231
232 av_conf_err:
233         ocrdma_free_av(dev, ah);
234 av_err:
235         kfree(ah);
236         return ERR_PTR(status);
237 }
238
239 int ocrdma_destroy_ah(struct ib_ah *ibah)
240 {
241         struct ocrdma_ah *ah = get_ocrdma_ah(ibah);
242         struct ocrdma_dev *dev = get_ocrdma_dev(ibah->device);
243
244         ocrdma_free_av(dev, ah);
245         kfree(ah);
246         return 0;
247 }
248
249 int ocrdma_query_ah(struct ib_ah *ibah, struct ib_ah_attr *attr)
250 {
251         struct ocrdma_ah *ah = get_ocrdma_ah(ibah);
252         struct ocrdma_av *av = ah->av;
253         struct ocrdma_grh *grh;
254         attr->ah_flags |= IB_AH_GRH;
255         if (ah->av->valid & OCRDMA_AV_VALID) {
256                 grh = (struct ocrdma_grh *)((u8 *)ah->av +
257                                 sizeof(struct ocrdma_eth_vlan));
258                 attr->sl = be16_to_cpu(av->eth_hdr.vlan_tag) >> 13;
259         } else {
260                 grh = (struct ocrdma_grh *)((u8 *)ah->av +
261                                         sizeof(struct ocrdma_eth_basic));
262                 attr->sl = 0;
263         }
264         memcpy(&attr->grh.dgid.raw[0], &grh->dgid[0], sizeof(grh->dgid));
265         attr->grh.sgid_index = ah->sgid_index;
266         attr->grh.hop_limit = be32_to_cpu(grh->pdid_hoplimit) & 0xff;
267         attr->grh.traffic_class = be32_to_cpu(grh->tclass_flow) >> 24;
268         attr->grh.flow_label = be32_to_cpu(grh->tclass_flow) & 0x00ffffffff;
269         return 0;
270 }
271
272 int ocrdma_modify_ah(struct ib_ah *ibah, struct ib_ah_attr *attr)
273 {
274         /* modify_ah is unsupported */
275         return -ENOSYS;
276 }
277
278 int ocrdma_process_mad(struct ib_device *ibdev,
279                        int process_mad_flags,
280                        u8 port_num,
281                        const struct ib_wc *in_wc,
282                        const struct ib_grh *in_grh,
283                        const struct ib_mad_hdr *in, size_t in_mad_size,
284                        struct ib_mad_hdr *out, size_t *out_mad_size,
285                        u16 *out_mad_pkey_index)
286 {
287         int status;
288         struct ocrdma_dev *dev;
289         const struct ib_mad *in_mad = (const struct ib_mad *)in;
290         struct ib_mad *out_mad = (struct ib_mad *)out;
291
292         if (WARN_ON_ONCE(in_mad_size != sizeof(*in_mad) ||
293                          *out_mad_size != sizeof(*out_mad)))
294                 return IB_MAD_RESULT_FAILURE;
295
296         switch (in_mad->mad_hdr.mgmt_class) {
297         case IB_MGMT_CLASS_PERF_MGMT:
298                 dev = get_ocrdma_dev(ibdev);
299                 if (!ocrdma_pma_counters(dev, out_mad))
300                         status = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
301                 else
302                         status = IB_MAD_RESULT_SUCCESS;
303                 break;
304         default:
305                 status = IB_MAD_RESULT_SUCCESS;
306                 break;
307         }
308         return status;
309 }