blob: 44766fee1f4e2cacb2d971f7858ad6e3eb4c4875 [file] [log] [blame]
Devesh Sharma71ee6732015-07-24 05:03:59 +05301/* 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.
Parav Panditfe2caef2012-03-21 04:09:06 +053034 *
35 * Contact Information:
36 * linux-drivers@emulex.com
37 *
38 * Emulex
39 * 3333 Susan Street
40 * Costa Mesa, CA 92626
Devesh Sharma71ee6732015-07-24 05:03:59 +053041 */
Parav Panditfe2caef2012-03-21 04:09:06 +053042
43#include <net/neighbour.h>
44#include <net/netevent.h>
45
46#include <rdma/ib_addr.h>
Mitesh Ahujacad1fbb2014-12-18 14:12:55 +053047#include <rdma/ib_mad.h>
Parav Panditfe2caef2012-03-21 04:09:06 +053048
49#include "ocrdma.h"
50#include "ocrdma_verbs.h"
51#include "ocrdma_ah.h"
52#include "ocrdma_hw.h"
Mitesh Ahujacad1fbb2014-12-18 14:12:55 +053053#include "ocrdma_stats.h"
Parav Panditfe2caef2012-03-21 04:09:06 +053054
Devesh Sharma0ea87262014-07-02 11:36:04 +053055#define OCRDMA_VID_PCP_SHIFT 0xD
56
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +053057static inline int set_av_attr(struct ocrdma_dev *dev, struct ocrdma_ah *ah,
Devesh Sharma29565f22014-12-18 14:13:07 +053058 struct ib_ah_attr *attr, union ib_gid *sgid,
59 int pdid, bool *isvlan)
Parav Panditfe2caef2012-03-21 04:09:06 +053060{
61 int status = 0;
Devesh Sharma29565f22014-12-18 14:13:07 +053062 u16 vlan_tag;
Parav Panditfe2caef2012-03-21 04:09:06 +053063 struct ocrdma_eth_vlan eth;
64 struct ocrdma_grh grh;
65 int eth_sz;
66
67 memset(&eth, 0, sizeof(eth));
68 memset(&grh, 0, sizeof(grh));
69
devesh.sharma@emulex.com1be528b2014-09-05 15:09:48 +053070 /* VLAN */
Moni Shoua40aca6f2013-12-12 18:03:15 +020071 vlan_tag = attr->vlan_id;
Naresh Gottumukkala84b105d2013-08-26 15:27:50 +053072 if (!vlan_tag || (vlan_tag > 0xFFF))
73 vlan_tag = dev->pvid;
Devesh Sharma6f5deab2015-05-19 11:32:35 +053074 if (vlan_tag || dev->pfc_state) {
75 if (!vlan_tag) {
76 pr_err("ocrdma%d:Using VLAN with PFC is recommended\n",
77 dev->id);
78 pr_err("ocrdma%d:Using VLAN 0 for this connection\n",
79 dev->id);
80 }
Parav Panditfe2caef2012-03-21 04:09:06 +053081 eth.eth_type = cpu_to_be16(0x8100);
82 eth.roce_eth_type = cpu_to_be16(OCRDMA_ROCE_ETH_TYPE);
Devesh Sharma0ea87262014-07-02 11:36:04 +053083 vlan_tag |= (dev->sl & 0x07) << OCRDMA_VID_PCP_SHIFT;
Parav Panditfe2caef2012-03-21 04:09:06 +053084 eth.vlan_tag = cpu_to_be16(vlan_tag);
85 eth_sz = sizeof(struct ocrdma_eth_vlan);
Devesh Sharma29565f22014-12-18 14:13:07 +053086 *isvlan = true;
Parav Panditfe2caef2012-03-21 04:09:06 +053087 } else {
88 eth.eth_type = cpu_to_be16(OCRDMA_ROCE_ETH_TYPE);
89 eth_sz = sizeof(struct ocrdma_eth_basic);
90 }
devesh.sharma@emulex.com1be528b2014-09-05 15:09:48 +053091 /* MAC */
Parav Panditfe2caef2012-03-21 04:09:06 +053092 memcpy(&eth.smac[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
Moni Shoua40aca6f2013-12-12 18:03:15 +020093 status = ocrdma_resolve_dmac(dev, attr, &eth.dmac[0]);
Parav Panditfe2caef2012-03-21 04:09:06 +053094 if (status)
95 return status;
devesh.sharma@emulex.com1be528b2014-09-05 15:09:48 +053096 ah->sgid_index = attr->grh.sgid_index;
97 memcpy(&grh.sgid[0], sgid->raw, sizeof(union ib_gid));
98 memcpy(&grh.dgid[0], attr->grh.dgid.raw, sizeof(attr->grh.dgid.raw));
Parav Panditfe2caef2012-03-21 04:09:06 +053099
100 grh.tclass_flow = cpu_to_be32((6 << 28) |
101 (attr->grh.traffic_class << 24) |
102 attr->grh.flow_label);
103 /* 0x1b is next header value in GRH */
104 grh.pdid_hoplimit = cpu_to_be32((pdid << 16) |
105 (0x1b << 8) | attr->grh.hop_limit);
devesh.sharma@emulex.com1be528b2014-09-05 15:09:48 +0530106 /* Eth HDR */
Parav Panditfe2caef2012-03-21 04:09:06 +0530107 memcpy(&ah->av->eth_hdr, &eth, eth_sz);
108 memcpy((u8 *)ah->av + eth_sz, &grh, sizeof(struct ocrdma_grh));
Devesh Sharma29565f22014-12-18 14:13:07 +0530109 if (*isvlan)
Parav Panditfe2caef2012-03-21 04:09:06 +0530110 ah->av->valid |= OCRDMA_AV_VLAN_VALID;
Devesh Sharmafe5e8a12013-12-05 15:48:01 +0530111 ah->av->valid = cpu_to_le32(ah->av->valid);
Parav Panditfe2caef2012-03-21 04:09:06 +0530112 return status;
113}
114
115struct ib_ah *ocrdma_create_ah(struct ib_pd *ibpd, struct ib_ah_attr *attr)
116{
117 u32 *ahid_addr;
Devesh Sharma29565f22014-12-18 14:13:07 +0530118 bool isvlan = false;
Parav Panditfe2caef2012-03-21 04:09:06 +0530119 int status;
120 struct ocrdma_ah *ah;
121 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530122 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
devesh.sharma@emulex.com1be528b2014-09-05 15:09:48 +0530123 union ib_gid sgid;
Parav Panditfe2caef2012-03-21 04:09:06 +0530124
125 if (!(attr->ah_flags & IB_AH_GRH))
126 return ERR_PTR(-EINVAL);
127
Selvin Xavier31dbdd92014-06-10 19:32:13 +0530128 if (atomic_cmpxchg(&dev->update_sl, 1, 0))
129 ocrdma_init_service_level(dev);
Devesh Sharmafad51b72014-02-04 11:57:10 +0530130 ah = kzalloc(sizeof(*ah), GFP_ATOMIC);
Parav Panditfe2caef2012-03-21 04:09:06 +0530131 if (!ah)
132 return ERR_PTR(-ENOMEM);
Parav Panditfe2caef2012-03-21 04:09:06 +0530133
134 status = ocrdma_alloc_av(dev, ah);
135 if (status)
136 goto av_err;
devesh.sharma@emulex.com1be528b2014-09-05 15:09:48 +0530137
138 status = ocrdma_query_gid(&dev->ibdev, 1, attr->grh.sgid_index, &sgid);
139 if (status) {
140 pr_err("%s(): Failed to query sgid, status = %d\n",
141 __func__, status);
142 goto av_conf_err;
143 }
144
Mitesh Ahujad27b2f12015-05-19 11:32:38 +0530145 if ((pd->uctx) &&
146 (!rdma_is_multicast_addr((struct in6_addr *)attr->grh.dgid.raw)) &&
147 (!rdma_link_local_addr((struct in6_addr *)attr->grh.dgid.raw))) {
devesh.sharma@emulex.com1be528b2014-09-05 15:09:48 +0530148 status = rdma_addr_find_dmac_by_grh(&sgid, &attr->grh.dgid,
149 attr->dmac, &attr->vlan_id);
150 if (status) {
151 pr_err("%s(): Failed to resolve dmac from gid."
152 "status = %d\n", __func__, status);
153 goto av_conf_err;
154 }
155 }
156
Devesh Sharma29565f22014-12-18 14:13:07 +0530157 status = set_av_attr(dev, ah, attr, &sgid, pd->id, &isvlan);
Parav Panditfe2caef2012-03-21 04:09:06 +0530158 if (status)
159 goto av_conf_err;
160
161 /* if pd is for the user process, pass the ah_id to user space */
162 if ((pd->uctx) && (pd->uctx->ah_tbl.va)) {
163 ahid_addr = pd->uctx->ah_tbl.va + attr->dlid;
Devesh Sharma29565f22014-12-18 14:13:07 +0530164 *ahid_addr = 0;
165 *ahid_addr |= ah->id & OCRDMA_AH_ID_MASK;
166 if (isvlan)
167 *ahid_addr |= (OCRDMA_AH_VLAN_VALID_MASK <<
168 OCRDMA_AH_VLAN_VALID_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +0530169 }
Devesh Sharma29565f22014-12-18 14:13:07 +0530170
Parav Panditfe2caef2012-03-21 04:09:06 +0530171 return &ah->ibah;
172
173av_conf_err:
174 ocrdma_free_av(dev, ah);
175av_err:
176 kfree(ah);
177 return ERR_PTR(status);
178}
179
180int ocrdma_destroy_ah(struct ib_ah *ibah)
181{
182 struct ocrdma_ah *ah = get_ocrdma_ah(ibah);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530183 struct ocrdma_dev *dev = get_ocrdma_dev(ibah->device);
184
185 ocrdma_free_av(dev, ah);
Parav Panditfe2caef2012-03-21 04:09:06 +0530186 kfree(ah);
187 return 0;
188}
189
190int ocrdma_query_ah(struct ib_ah *ibah, struct ib_ah_attr *attr)
191{
192 struct ocrdma_ah *ah = get_ocrdma_ah(ibah);
193 struct ocrdma_av *av = ah->av;
194 struct ocrdma_grh *grh;
195 attr->ah_flags |= IB_AH_GRH;
devesh.sharma@emulex.comf0c2c222014-09-05 15:09:49 +0530196 if (ah->av->valid & OCRDMA_AV_VALID) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530197 grh = (struct ocrdma_grh *)((u8 *)ah->av +
198 sizeof(struct ocrdma_eth_vlan));
199 attr->sl = be16_to_cpu(av->eth_hdr.vlan_tag) >> 13;
200 } else {
201 grh = (struct ocrdma_grh *)((u8 *)ah->av +
202 sizeof(struct ocrdma_eth_basic));
203 attr->sl = 0;
204 }
205 memcpy(&attr->grh.dgid.raw[0], &grh->dgid[0], sizeof(grh->dgid));
206 attr->grh.sgid_index = ah->sgid_index;
207 attr->grh.hop_limit = be32_to_cpu(grh->pdid_hoplimit) & 0xff;
208 attr->grh.traffic_class = be32_to_cpu(grh->tclass_flow) >> 24;
209 attr->grh.flow_label = be32_to_cpu(grh->tclass_flow) & 0x00ffffffff;
210 return 0;
211}
212
213int ocrdma_modify_ah(struct ib_ah *ibah, struct ib_ah_attr *attr)
214{
215 /* modify_ah is unsupported */
216 return -ENOSYS;
217}
218
219int ocrdma_process_mad(struct ib_device *ibdev,
220 int process_mad_flags,
221 u8 port_num,
Ira Weinya97e2d82015-05-31 17:15:30 -0400222 const struct ib_wc *in_wc,
223 const struct ib_grh *in_grh,
Ira Weiny4cd7c942015-06-06 14:38:31 -0400224 const struct ib_mad_hdr *in, size_t in_mad_size,
225 struct ib_mad_hdr *out, size_t *out_mad_size,
226 u16 *out_mad_pkey_index)
Parav Panditfe2caef2012-03-21 04:09:06 +0530227{
Mitesh Ahujacad1fbb2014-12-18 14:12:55 +0530228 int status;
229 struct ocrdma_dev *dev;
Ira Weiny4cd7c942015-06-06 14:38:31 -0400230 const struct ib_mad *in_mad = (const struct ib_mad *)in;
231 struct ib_mad *out_mad = (struct ib_mad *)out;
232
Ira Weiny3b8ab702015-06-25 09:52:50 -0400233 if (WARN_ON_ONCE(in_mad_size != sizeof(*in_mad) ||
234 *out_mad_size != sizeof(*out_mad)))
235 return IB_MAD_RESULT_FAILURE;
Mitesh Ahujacad1fbb2014-12-18 14:12:55 +0530236
237 switch (in_mad->mad_hdr.mgmt_class) {
238 case IB_MGMT_CLASS_PERF_MGMT:
239 dev = get_ocrdma_dev(ibdev);
240 if (!ocrdma_pma_counters(dev, out_mad))
241 status = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
242 else
243 status = IB_MAD_RESULT_SUCCESS;
244 break;
245 default:
246 status = IB_MAD_RESULT_SUCCESS;
247 break;
248 }
249 return status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530250}