blob: 61558788b3fadb7546660f3b907820b264e3b1aa [file] [log] [blame]
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -07001/*
2 * Copyright(c) 2017 Intel Corporation.
3 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
48/*
49 * This file contains OPA Virtual Network Interface Controller (VNIC) driver
50 * netdev functionality.
51 */
52
53#include <linux/module.h>
54#include <linux/if_vlan.h>
Vishwanathapura, Niranjanacfd34f82017-04-12 20:29:26 -070055#include <linux/crc32.h>
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -070056
57#include "opa_vnic_internal.h"
58
59#define OPA_TX_TIMEOUT_MS 1000
60
61#define OPA_VNIC_SKB_HEADROOM \
62 ALIGN((OPA_VNIC_HDR_LEN + OPA_VNIC_SKB_MDATA_LEN), 8)
63
Vishwanathapura, Niranjana009b7dd2017-04-12 20:29:24 -070064/* This function is overloaded for opa_vnic specific implementation */
65static void opa_vnic_get_stats64(struct net_device *netdev,
66 struct rtnl_link_stats64 *stats)
67{
68 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
69 struct opa_vnic_stats vstats;
70
71 memset(&vstats, 0, sizeof(vstats));
Vishwanathapura, Niranjanaa379d692017-06-14 12:34:42 -070072 spin_lock(&adapter->stats_lock);
Vishwanathapura, Niranjana009b7dd2017-04-12 20:29:24 -070073 adapter->rn_ops->ndo_get_stats64(netdev, &vstats.netstats);
Vishwanathapura, Niranjanaa379d692017-06-14 12:34:42 -070074 spin_unlock(&adapter->stats_lock);
Vishwanathapura, Niranjana009b7dd2017-04-12 20:29:24 -070075 memcpy(stats, &vstats.netstats, sizeof(*stats));
76}
77
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -070078/* opa_netdev_start_xmit - transmit function */
79static netdev_tx_t opa_netdev_start_xmit(struct sk_buff *skb,
80 struct net_device *netdev)
81{
82 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
83
84 v_dbg("xmit: queue %d skb len %d\n", skb->queue_mapping, skb->len);
85 /* pad to ensure mininum ethernet packet length */
86 if (unlikely(skb->len < ETH_ZLEN)) {
87 if (skb_padto(skb, ETH_ZLEN))
88 return NETDEV_TX_OK;
89
90 skb_put(skb, ETH_ZLEN - skb->len);
91 }
92
93 opa_vnic_encap_skb(adapter, skb);
94 return adapter->rn_ops->ndo_start_xmit(skb, netdev);
95}
96
97static u16 opa_vnic_select_queue(struct net_device *netdev, struct sk_buff *skb,
Alexander Duyck4f49dec2018-07-09 12:19:59 -040098 struct net_device *sb_dev,
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -070099 select_queue_fallback_t fallback)
100{
101 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
102 struct opa_vnic_skb_mdata *mdata;
103 int rc;
104
105 /* pass entropy and vl as metadata in skb */
Johannes Bergd58ff352017-06-16 14:29:23 +0200106 mdata = skb_push(skb, sizeof(*mdata));
Alexander Duyckd80d8d52018-04-27 14:06:35 -0400107 mdata->entropy = opa_vnic_calc_entropy(skb);
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700108 mdata->vl = opa_vnic_get_vl(adapter, skb);
109 rc = adapter->rn_ops->ndo_select_queue(netdev, skb,
Alexander Duyck4f49dec2018-07-09 12:19:59 -0400110 sb_dev, fallback);
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700111 skb_pull(skb, sizeof(*mdata));
112 return rc;
113}
114
Niranjana Vishwanathapurae82b7c32017-09-26 06:44:20 -0700115static void opa_vnic_update_state(struct opa_vnic_adapter *adapter, bool up)
116{
117 struct __opa_veswport_info *info = &adapter->info;
118
119 mutex_lock(&adapter->lock);
120 /* Operational state can only be DROP_ALL or FORWARDING */
121 if ((info->vport.config_state == OPA_VNIC_STATE_FORWARDING) && up) {
122 info->vport.oper_state = OPA_VNIC_STATE_FORWARDING;
123 info->vport.eth_link_status = OPA_VNIC_ETH_LINK_UP;
124 } else {
125 info->vport.oper_state = OPA_VNIC_STATE_DROP_ALL;
126 info->vport.eth_link_status = OPA_VNIC_ETH_LINK_DOWN;
127 }
128
129 if (info->vport.config_state == OPA_VNIC_STATE_FORWARDING)
130 netif_dormant_off(adapter->netdev);
131 else
132 netif_dormant_on(adapter->netdev);
133 mutex_unlock(&adapter->lock);
134}
135
Vishwanathapura, Niranjanacfd34f82017-04-12 20:29:26 -0700136/* opa_vnic_process_vema_config - process vema configuration updates */
137void opa_vnic_process_vema_config(struct opa_vnic_adapter *adapter)
138{
139 struct __opa_veswport_info *info = &adapter->info;
140 struct rdma_netdev *rn = netdev_priv(adapter->netdev);
141 u8 port_num[OPA_VESW_MAX_NUM_DEF_PORT] = { 0 };
142 struct net_device *netdev = adapter->netdev;
143 u8 i, port_count = 0;
144 u16 port_mask;
145
146 /* If the base_mac_addr is changed, update the interface mac address */
147 if (memcmp(info->vport.base_mac_addr, adapter->vema_mac_addr,
148 ARRAY_SIZE(info->vport.base_mac_addr))) {
149 struct sockaddr saddr;
150
151 memcpy(saddr.sa_data, info->vport.base_mac_addr,
152 ARRAY_SIZE(info->vport.base_mac_addr));
153 mutex_lock(&adapter->lock);
Niranjana Vishwanathapura5a5a85d2017-09-26 06:44:01 -0700154 eth_commit_mac_addr_change(netdev, &saddr);
Vishwanathapura, Niranjanacfd34f82017-04-12 20:29:26 -0700155 memcpy(adapter->vema_mac_addr,
156 info->vport.base_mac_addr, ETH_ALEN);
157 mutex_unlock(&adapter->lock);
158 }
159
160 rn->set_id(netdev, info->vesw.vesw_id);
161
162 /* Handle MTU limit change */
163 rtnl_lock();
Niranjana Vishwanathapura62f1e842017-09-26 06:43:48 -0700164 netdev->max_mtu = max_t(unsigned int, info->vesw.eth_mtu,
Vishwanathapura, Niranjanacfd34f82017-04-12 20:29:26 -0700165 netdev->min_mtu);
166 if (netdev->mtu > netdev->max_mtu)
167 dev_set_mtu(netdev, netdev->max_mtu);
168 rtnl_unlock();
169
170 /* Update flow to default port redirection table */
171 port_mask = info->vesw.def_port_mask;
172 for (i = 0; i < OPA_VESW_MAX_NUM_DEF_PORT; i++) {
173 if (port_mask & 1)
174 port_num[port_count++] = i;
175 port_mask >>= 1;
176 }
177
178 /*
179 * Build the flow table. Flow table is required when destination LID
180 * is not available. Up to OPA_VNIC_FLOW_TBL_SIZE flows supported.
181 * Each flow need a default port number to get its dlid from the
182 * u_ucast_dlid array.
183 */
184 for (i = 0; i < OPA_VNIC_FLOW_TBL_SIZE; i++)
185 adapter->flow_tbl[i] = port_count ? port_num[i % port_count] :
186 OPA_VNIC_INVALID_PORT;
187
Niranjana Vishwanathapurae82b7c32017-09-26 06:44:20 -0700188 /* update state */
189 opa_vnic_update_state(adapter, !!(netdev->flags & IFF_UP));
Vishwanathapura, Niranjanacfd34f82017-04-12 20:29:26 -0700190}
191
192/*
193 * Set the power on default values in adapter's vema interface structure.
194 */
195static inline void opa_vnic_set_pod_values(struct opa_vnic_adapter *adapter)
196{
197 adapter->info.vport.max_mac_tbl_ent = OPA_VNIC_MAC_TBL_MAX_ENTRIES;
198 adapter->info.vport.max_smac_ent = OPA_VNIC_MAX_SMAC_LIMIT;
199 adapter->info.vport.config_state = OPA_VNIC_STATE_DROP_ALL;
200 adapter->info.vport.eth_link_status = OPA_VNIC_ETH_LINK_DOWN;
Niranjana Vishwanathapura7f291d22017-09-26 06:43:54 -0700201 adapter->info.vesw.eth_mtu = ETH_DATA_LEN;
Vishwanathapura, Niranjanacfd34f82017-04-12 20:29:26 -0700202}
203
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700204/* opa_vnic_set_mac_addr - change mac address */
205static int opa_vnic_set_mac_addr(struct net_device *netdev, void *addr)
206{
207 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
208 struct sockaddr *sa = addr;
209 int rc;
210
211 if (!memcmp(netdev->dev_addr, sa->sa_data, ETH_ALEN))
212 return 0;
213
214 mutex_lock(&adapter->lock);
215 rc = eth_mac_addr(netdev, addr);
216 mutex_unlock(&adapter->lock);
Vishwanathapura, Niranjanacfd34f82017-04-12 20:29:26 -0700217 if (rc)
218 return rc;
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700219
Vishwanathapura, Niranjanacfd34f82017-04-12 20:29:26 -0700220 adapter->info.vport.uc_macs_gen_count++;
221 opa_vnic_vema_report_event(adapter,
222 OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE);
223 return 0;
224}
225
226/*
227 * opa_vnic_mac_send_event - post event on possible mac list exchange
228 * Send trap when digest from uc/mc mac list differs from previous run.
229 * Digest is evaluated similar to how cksum does.
230 */
231static void opa_vnic_mac_send_event(struct net_device *netdev, u8 event)
232{
233 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
234 struct netdev_hw_addr *ha;
235 struct netdev_hw_addr_list *hw_list;
236 u32 *ref_crc;
237 u32 l, crc = 0;
238
239 switch (event) {
240 case OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE:
241 hw_list = &netdev->uc;
242 adapter->info.vport.uc_macs_gen_count++;
243 ref_crc = &adapter->umac_hash;
244 break;
245 case OPA_VESWPORT_TRAP_IFACE_MCAST_MAC_CHANGE:
246 hw_list = &netdev->mc;
247 adapter->info.vport.mc_macs_gen_count++;
248 ref_crc = &adapter->mmac_hash;
249 break;
250 default:
251 return;
252 }
253 netdev_hw_addr_list_for_each(ha, hw_list) {
254 crc = crc32_le(crc, ha->addr, ETH_ALEN);
255 }
256 l = netdev_hw_addr_list_count(hw_list) * ETH_ALEN;
257 crc = ~crc32_le(crc, (void *)&l, sizeof(l));
258
259 if (crc != *ref_crc) {
260 *ref_crc = crc;
261 opa_vnic_vema_report_event(adapter, event);
262 }
263}
264
265/* opa_vnic_set_rx_mode - handle uc/mc mac list change */
266static void opa_vnic_set_rx_mode(struct net_device *netdev)
267{
268 opa_vnic_mac_send_event(netdev,
269 OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE);
270
271 opa_vnic_mac_send_event(netdev,
272 OPA_VESWPORT_TRAP_IFACE_MCAST_MAC_CHANGE);
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700273}
274
275/* opa_netdev_open - activate network interface */
276static int opa_netdev_open(struct net_device *netdev)
277{
278 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
279 int rc;
280
281 rc = adapter->rn_ops->ndo_open(adapter->netdev);
282 if (rc) {
283 v_dbg("open failed %d\n", rc);
284 return rc;
285 }
286
Niranjana Vishwanathapurae82b7c32017-09-26 06:44:20 -0700287 /* Update status and send trap */
288 opa_vnic_update_state(adapter, true);
Vishwanathapura, Niranjanacfd34f82017-04-12 20:29:26 -0700289 opa_vnic_vema_report_event(adapter,
290 OPA_VESWPORT_TRAP_ETH_LINK_STATUS_CHANGE);
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700291 return 0;
292}
293
294/* opa_netdev_close - disable network interface */
295static int opa_netdev_close(struct net_device *netdev)
296{
297 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
298 int rc;
299
300 rc = adapter->rn_ops->ndo_stop(adapter->netdev);
301 if (rc) {
302 v_dbg("close failed %d\n", rc);
303 return rc;
304 }
305
Niranjana Vishwanathapurae82b7c32017-09-26 06:44:20 -0700306 /* Update status and send trap */
307 opa_vnic_update_state(adapter, false);
Vishwanathapura, Niranjanacfd34f82017-04-12 20:29:26 -0700308 opa_vnic_vema_report_event(adapter,
309 OPA_VESWPORT_TRAP_ETH_LINK_STATUS_CHANGE);
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700310 return 0;
311}
312
313/* netdev ops */
314static const struct net_device_ops opa_netdev_ops = {
315 .ndo_open = opa_netdev_open,
316 .ndo_stop = opa_netdev_close,
317 .ndo_start_xmit = opa_netdev_start_xmit,
Vishwanathapura, Niranjana009b7dd2017-04-12 20:29:24 -0700318 .ndo_get_stats64 = opa_vnic_get_stats64,
Vishwanathapura, Niranjanacfd34f82017-04-12 20:29:26 -0700319 .ndo_set_rx_mode = opa_vnic_set_rx_mode,
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700320 .ndo_select_queue = opa_vnic_select_queue,
321 .ndo_set_mac_address = opa_vnic_set_mac_addr,
322};
323
324/* opa_vnic_add_netdev - create vnic netdev interface */
325struct opa_vnic_adapter *opa_vnic_add_netdev(struct ib_device *ibdev,
326 u8 port_num, u8 vport_num)
327{
328 struct opa_vnic_adapter *adapter;
329 struct net_device *netdev;
330 struct rdma_netdev *rn;
331 int rc;
332
333 netdev = ibdev->alloc_rdma_netdev(ibdev, port_num,
334 RDMA_NETDEV_OPA_VNIC,
335 "veth%d", NET_NAME_UNKNOWN,
336 ether_setup);
337 if (!netdev)
338 return ERR_PTR(-ENOMEM);
339 else if (IS_ERR(netdev))
340 return ERR_CAST(netdev);
341
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -0700342 rn = netdev_priv(netdev);
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700343 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
344 if (!adapter) {
345 rc = -ENOMEM;
346 goto adapter_err;
347 }
348
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700349 rn->clnt_priv = adapter;
350 rn->hca = ibdev;
351 rn->port_num = port_num;
352 adapter->netdev = netdev;
353 adapter->ibdev = ibdev;
354 adapter->port_num = port_num;
355 adapter->vport_num = vport_num;
356 adapter->rn_ops = netdev->netdev_ops;
357
358 netdev->netdev_ops = &opa_netdev_ops;
359 netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
360 netdev->hard_header_len += OPA_VNIC_SKB_HEADROOM;
361 mutex_init(&adapter->lock);
Vishwanathapura, Niranjana174e03d2017-04-12 20:29:25 -0700362 mutex_init(&adapter->mactbl_lock);
Vishwanathapura, Niranjanaa379d692017-06-14 12:34:42 -0700363 spin_lock_init(&adapter->stats_lock);
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700364
365 SET_NETDEV_DEV(netdev, ibdev->dev.parent);
366
367 opa_vnic_set_ethtool_ops(netdev);
Vishwanathapura, Niranjanacfd34f82017-04-12 20:29:26 -0700368
369 opa_vnic_set_pod_values(adapter);
370
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700371 rc = register_netdev(netdev);
372 if (rc)
373 goto netdev_err;
374
375 netif_carrier_off(netdev);
376 netif_dormant_on(netdev);
377 v_info("initialized\n");
378
379 return adapter;
380netdev_err:
381 mutex_destroy(&adapter->lock);
Vishwanathapura, Niranjana174e03d2017-04-12 20:29:25 -0700382 mutex_destroy(&adapter->mactbl_lock);
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700383 kfree(adapter);
384adapter_err:
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -0700385 rn->free_rdma_netdev(netdev);
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700386
387 return ERR_PTR(rc);
388}
389
390/* opa_vnic_rem_netdev - remove vnic netdev interface */
391void opa_vnic_rem_netdev(struct opa_vnic_adapter *adapter)
392{
393 struct net_device *netdev = adapter->netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -0700394 struct rdma_netdev *rn = netdev_priv(netdev);
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700395
396 v_info("removing\n");
397 unregister_netdev(netdev);
Vishwanathapura, Niranjana174e03d2017-04-12 20:29:25 -0700398 opa_vnic_release_mac_tbl(adapter);
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700399 mutex_destroy(&adapter->lock);
Vishwanathapura, Niranjana174e03d2017-04-12 20:29:25 -0700400 mutex_destroy(&adapter->mactbl_lock);
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700401 kfree(adapter);
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -0700402 rn->free_rdma_netdev(netdev);
Vishwanathapura, Niranjana7d6f7282017-04-12 20:29:22 -0700403}