17d6f728cSVishwanathapura, Niranjana /*
27d6f728cSVishwanathapura, Niranjana  * Copyright(c) 2017 Intel Corporation.
37d6f728cSVishwanathapura, Niranjana  *
47d6f728cSVishwanathapura, Niranjana  * This file is provided under a dual BSD/GPLv2 license.  When using or
57d6f728cSVishwanathapura, Niranjana  * redistributing this file, you may do so under either license.
67d6f728cSVishwanathapura, Niranjana  *
77d6f728cSVishwanathapura, Niranjana  * GPL LICENSE SUMMARY
87d6f728cSVishwanathapura, Niranjana  *
97d6f728cSVishwanathapura, Niranjana  * This program is free software; you can redistribute it and/or modify
107d6f728cSVishwanathapura, Niranjana  * it under the terms of version 2 of the GNU General Public License as
117d6f728cSVishwanathapura, Niranjana  * published by the Free Software Foundation.
127d6f728cSVishwanathapura, Niranjana  *
137d6f728cSVishwanathapura, Niranjana  * This program is distributed in the hope that it will be useful, but
147d6f728cSVishwanathapura, Niranjana  * WITHOUT ANY WARRANTY; without even the implied warranty of
157d6f728cSVishwanathapura, Niranjana  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
167d6f728cSVishwanathapura, Niranjana  * General Public License for more details.
177d6f728cSVishwanathapura, Niranjana  *
187d6f728cSVishwanathapura, Niranjana  * BSD LICENSE
197d6f728cSVishwanathapura, Niranjana  *
207d6f728cSVishwanathapura, Niranjana  * Redistribution and use in source and binary forms, with or without
217d6f728cSVishwanathapura, Niranjana  * modification, are permitted provided that the following conditions
227d6f728cSVishwanathapura, Niranjana  * are met:
237d6f728cSVishwanathapura, Niranjana  *
247d6f728cSVishwanathapura, Niranjana  *  - Redistributions of source code must retain the above copyright
257d6f728cSVishwanathapura, Niranjana  *    notice, this list of conditions and the following disclaimer.
267d6f728cSVishwanathapura, Niranjana  *  - Redistributions in binary form must reproduce the above copyright
277d6f728cSVishwanathapura, Niranjana  *    notice, this list of conditions and the following disclaimer in
287d6f728cSVishwanathapura, Niranjana  *    the documentation and/or other materials provided with the
297d6f728cSVishwanathapura, Niranjana  *    distribution.
307d6f728cSVishwanathapura, Niranjana  *  - Neither the name of Intel Corporation nor the names of its
317d6f728cSVishwanathapura, Niranjana  *    contributors may be used to endorse or promote products derived
327d6f728cSVishwanathapura, Niranjana  *    from this software without specific prior written permission.
337d6f728cSVishwanathapura, Niranjana  *
347d6f728cSVishwanathapura, Niranjana  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
357d6f728cSVishwanathapura, Niranjana  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
367d6f728cSVishwanathapura, Niranjana  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
377d6f728cSVishwanathapura, Niranjana  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
387d6f728cSVishwanathapura, Niranjana  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
397d6f728cSVishwanathapura, Niranjana  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
407d6f728cSVishwanathapura, Niranjana  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
417d6f728cSVishwanathapura, Niranjana  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
427d6f728cSVishwanathapura, Niranjana  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
437d6f728cSVishwanathapura, Niranjana  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
447d6f728cSVishwanathapura, Niranjana  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
457d6f728cSVishwanathapura, Niranjana  *
467d6f728cSVishwanathapura, Niranjana  */
477d6f728cSVishwanathapura, Niranjana 
487d6f728cSVishwanathapura, Niranjana /*
497d6f728cSVishwanathapura, Niranjana  * This file contains OPA Virtual Network Interface Controller (VNIC) driver
507d6f728cSVishwanathapura, Niranjana  * netdev functionality.
517d6f728cSVishwanathapura, Niranjana  */
527d6f728cSVishwanathapura, Niranjana 
537d6f728cSVishwanathapura, Niranjana #include <linux/module.h>
547d6f728cSVishwanathapura, Niranjana #include <linux/if_vlan.h>
55cfd34f8eSVishwanathapura, Niranjana #include <linux/crc32.h>
567d6f728cSVishwanathapura, Niranjana 
577d6f728cSVishwanathapura, Niranjana #include "opa_vnic_internal.h"
587d6f728cSVishwanathapura, Niranjana 
597d6f728cSVishwanathapura, Niranjana #define OPA_TX_TIMEOUT_MS 1000
607d6f728cSVishwanathapura, Niranjana 
617d6f728cSVishwanathapura, Niranjana #define OPA_VNIC_SKB_HEADROOM  \
627d6f728cSVishwanathapura, Niranjana 			ALIGN((OPA_VNIC_HDR_LEN + OPA_VNIC_SKB_MDATA_LEN), 8)
637d6f728cSVishwanathapura, Niranjana 
64009b7dd4SVishwanathapura, Niranjana /* This function is overloaded for opa_vnic specific implementation */
65009b7dd4SVishwanathapura, Niranjana static void opa_vnic_get_stats64(struct net_device *netdev,
66009b7dd4SVishwanathapura, Niranjana 				 struct rtnl_link_stats64 *stats)
67009b7dd4SVishwanathapura, Niranjana {
68009b7dd4SVishwanathapura, Niranjana 	struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
69009b7dd4SVishwanathapura, Niranjana 	struct opa_vnic_stats vstats;
70009b7dd4SVishwanathapura, Niranjana 
71009b7dd4SVishwanathapura, Niranjana 	memset(&vstats, 0, sizeof(vstats));
72a379d69fSVishwanathapura, Niranjana 	spin_lock(&adapter->stats_lock);
73009b7dd4SVishwanathapura, Niranjana 	adapter->rn_ops->ndo_get_stats64(netdev, &vstats.netstats);
74a379d69fSVishwanathapura, Niranjana 	spin_unlock(&adapter->stats_lock);
75009b7dd4SVishwanathapura, Niranjana 	memcpy(stats, &vstats.netstats, sizeof(*stats));
76009b7dd4SVishwanathapura, Niranjana }
77009b7dd4SVishwanathapura, Niranjana 
787d6f728cSVishwanathapura, Niranjana /* opa_netdev_start_xmit - transmit function */
797d6f728cSVishwanathapura, Niranjana static netdev_tx_t opa_netdev_start_xmit(struct sk_buff *skb,
807d6f728cSVishwanathapura, Niranjana 					 struct net_device *netdev)
817d6f728cSVishwanathapura, Niranjana {
827d6f728cSVishwanathapura, Niranjana 	struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
837d6f728cSVishwanathapura, Niranjana 
847d6f728cSVishwanathapura, Niranjana 	v_dbg("xmit: queue %d skb len %d\n", skb->queue_mapping, skb->len);
857d6f728cSVishwanathapura, Niranjana 	/* pad to ensure mininum ethernet packet length */
867d6f728cSVishwanathapura, Niranjana 	if (unlikely(skb->len < ETH_ZLEN)) {
877d6f728cSVishwanathapura, Niranjana 		if (skb_padto(skb, ETH_ZLEN))
887d6f728cSVishwanathapura, Niranjana 			return NETDEV_TX_OK;
897d6f728cSVishwanathapura, Niranjana 
907d6f728cSVishwanathapura, Niranjana 		skb_put(skb, ETH_ZLEN - skb->len);
917d6f728cSVishwanathapura, Niranjana 	}
927d6f728cSVishwanathapura, Niranjana 
937d6f728cSVishwanathapura, Niranjana 	opa_vnic_encap_skb(adapter, skb);
947d6f728cSVishwanathapura, Niranjana 	return adapter->rn_ops->ndo_start_xmit(skb, netdev);
957d6f728cSVishwanathapura, Niranjana }
967d6f728cSVishwanathapura, Niranjana 
977d6f728cSVishwanathapura, Niranjana static u16 opa_vnic_select_queue(struct net_device *netdev, struct sk_buff *skb,
987d6f728cSVishwanathapura, Niranjana 				 void *accel_priv,
997d6f728cSVishwanathapura, Niranjana 				 select_queue_fallback_t fallback)
1007d6f728cSVishwanathapura, Niranjana {
1017d6f728cSVishwanathapura, Niranjana 	struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
1027d6f728cSVishwanathapura, Niranjana 	struct opa_vnic_skb_mdata *mdata;
1037d6f728cSVishwanathapura, Niranjana 	int rc;
1047d6f728cSVishwanathapura, Niranjana 
1057d6f728cSVishwanathapura, Niranjana 	/* pass entropy and vl as metadata in skb */
106d58ff351SJohannes Berg 	mdata = skb_push(skb, sizeof(*mdata));
1077d6f728cSVishwanathapura, Niranjana 	mdata->entropy =  opa_vnic_calc_entropy(adapter, skb);
1087d6f728cSVishwanathapura, Niranjana 	mdata->vl = opa_vnic_get_vl(adapter, skb);
1097d6f728cSVishwanathapura, Niranjana 	rc = adapter->rn_ops->ndo_select_queue(netdev, skb,
1107d6f728cSVishwanathapura, Niranjana 					       accel_priv, fallback);
1117d6f728cSVishwanathapura, Niranjana 	skb_pull(skb, sizeof(*mdata));
1127d6f728cSVishwanathapura, Niranjana 	return rc;
1137d6f728cSVishwanathapura, Niranjana }
1147d6f728cSVishwanathapura, Niranjana 
115cfd34f8eSVishwanathapura, Niranjana /* opa_vnic_process_vema_config - process vema configuration updates */
116cfd34f8eSVishwanathapura, Niranjana void opa_vnic_process_vema_config(struct opa_vnic_adapter *adapter)
117cfd34f8eSVishwanathapura, Niranjana {
118cfd34f8eSVishwanathapura, Niranjana 	struct __opa_veswport_info *info = &adapter->info;
119cfd34f8eSVishwanathapura, Niranjana 	struct rdma_netdev *rn = netdev_priv(adapter->netdev);
120cfd34f8eSVishwanathapura, Niranjana 	u8 port_num[OPA_VESW_MAX_NUM_DEF_PORT] = { 0 };
121cfd34f8eSVishwanathapura, Niranjana 	struct net_device *netdev = adapter->netdev;
122cfd34f8eSVishwanathapura, Niranjana 	u8 i, port_count = 0;
123cfd34f8eSVishwanathapura, Niranjana 	u16 port_mask;
124cfd34f8eSVishwanathapura, Niranjana 
125cfd34f8eSVishwanathapura, Niranjana 	/* If the base_mac_addr is changed, update the interface mac address */
126cfd34f8eSVishwanathapura, Niranjana 	if (memcmp(info->vport.base_mac_addr, adapter->vema_mac_addr,
127cfd34f8eSVishwanathapura, Niranjana 		   ARRAY_SIZE(info->vport.base_mac_addr))) {
128cfd34f8eSVishwanathapura, Niranjana 		struct sockaddr saddr;
129cfd34f8eSVishwanathapura, Niranjana 
130cfd34f8eSVishwanathapura, Niranjana 		memcpy(saddr.sa_data, info->vport.base_mac_addr,
131cfd34f8eSVishwanathapura, Niranjana 		       ARRAY_SIZE(info->vport.base_mac_addr));
132cfd34f8eSVishwanathapura, Niranjana 		mutex_lock(&adapter->lock);
1335a5a85daSNiranjana Vishwanathapura 		eth_commit_mac_addr_change(netdev, &saddr);
134cfd34f8eSVishwanathapura, Niranjana 		memcpy(adapter->vema_mac_addr,
135cfd34f8eSVishwanathapura, Niranjana 		       info->vport.base_mac_addr, ETH_ALEN);
136cfd34f8eSVishwanathapura, Niranjana 		mutex_unlock(&adapter->lock);
137cfd34f8eSVishwanathapura, Niranjana 	}
138cfd34f8eSVishwanathapura, Niranjana 
139cfd34f8eSVishwanathapura, Niranjana 	rn->set_id(netdev, info->vesw.vesw_id);
140cfd34f8eSVishwanathapura, Niranjana 
141cfd34f8eSVishwanathapura, Niranjana 	/* Handle MTU limit change */
142cfd34f8eSVishwanathapura, Niranjana 	rtnl_lock();
14362f1e84eSNiranjana Vishwanathapura 	netdev->max_mtu = max_t(unsigned int, info->vesw.eth_mtu,
144cfd34f8eSVishwanathapura, Niranjana 				netdev->min_mtu);
145cfd34f8eSVishwanathapura, Niranjana 	if (netdev->mtu > netdev->max_mtu)
146cfd34f8eSVishwanathapura, Niranjana 		dev_set_mtu(netdev, netdev->max_mtu);
147cfd34f8eSVishwanathapura, Niranjana 	rtnl_unlock();
148cfd34f8eSVishwanathapura, Niranjana 
149cfd34f8eSVishwanathapura, Niranjana 	/* Update flow to default port redirection table */
150cfd34f8eSVishwanathapura, Niranjana 	port_mask = info->vesw.def_port_mask;
151cfd34f8eSVishwanathapura, Niranjana 	for (i = 0; i < OPA_VESW_MAX_NUM_DEF_PORT; i++) {
152cfd34f8eSVishwanathapura, Niranjana 		if (port_mask & 1)
153cfd34f8eSVishwanathapura, Niranjana 			port_num[port_count++] = i;
154cfd34f8eSVishwanathapura, Niranjana 		port_mask >>= 1;
155cfd34f8eSVishwanathapura, Niranjana 	}
156cfd34f8eSVishwanathapura, Niranjana 
157cfd34f8eSVishwanathapura, Niranjana 	/*
158cfd34f8eSVishwanathapura, Niranjana 	 * Build the flow table. Flow table is required when destination LID
159cfd34f8eSVishwanathapura, Niranjana 	 * is not available. Up to OPA_VNIC_FLOW_TBL_SIZE flows supported.
160cfd34f8eSVishwanathapura, Niranjana 	 * Each flow need a default port number to get its dlid from the
161cfd34f8eSVishwanathapura, Niranjana 	 * u_ucast_dlid array.
162cfd34f8eSVishwanathapura, Niranjana 	 */
163cfd34f8eSVishwanathapura, Niranjana 	for (i = 0; i < OPA_VNIC_FLOW_TBL_SIZE; i++)
164cfd34f8eSVishwanathapura, Niranjana 		adapter->flow_tbl[i] = port_count ? port_num[i % port_count] :
165cfd34f8eSVishwanathapura, Niranjana 						    OPA_VNIC_INVALID_PORT;
166cfd34f8eSVishwanathapura, Niranjana 
167cfd34f8eSVishwanathapura, Niranjana 	/* Operational state can only be DROP_ALL or FORWARDING */
168cfd34f8eSVishwanathapura, Niranjana 	if (info->vport.config_state == OPA_VNIC_STATE_FORWARDING) {
169cfd34f8eSVishwanathapura, Niranjana 		info->vport.oper_state = OPA_VNIC_STATE_FORWARDING;
170cfd34f8eSVishwanathapura, Niranjana 		netif_dormant_off(netdev);
171cfd34f8eSVishwanathapura, Niranjana 	} else {
172cfd34f8eSVishwanathapura, Niranjana 		info->vport.oper_state = OPA_VNIC_STATE_DROP_ALL;
173cfd34f8eSVishwanathapura, Niranjana 		netif_dormant_on(netdev);
174cfd34f8eSVishwanathapura, Niranjana 	}
175cfd34f8eSVishwanathapura, Niranjana }
176cfd34f8eSVishwanathapura, Niranjana 
177cfd34f8eSVishwanathapura, Niranjana /*
178cfd34f8eSVishwanathapura, Niranjana  * Set the power on default values in adapter's vema interface structure.
179cfd34f8eSVishwanathapura, Niranjana  */
180cfd34f8eSVishwanathapura, Niranjana static inline void opa_vnic_set_pod_values(struct opa_vnic_adapter *adapter)
181cfd34f8eSVishwanathapura, Niranjana {
182cfd34f8eSVishwanathapura, Niranjana 	adapter->info.vport.max_mac_tbl_ent = OPA_VNIC_MAC_TBL_MAX_ENTRIES;
183cfd34f8eSVishwanathapura, Niranjana 	adapter->info.vport.max_smac_ent = OPA_VNIC_MAX_SMAC_LIMIT;
184cfd34f8eSVishwanathapura, Niranjana 	adapter->info.vport.config_state = OPA_VNIC_STATE_DROP_ALL;
185cfd34f8eSVishwanathapura, Niranjana 	adapter->info.vport.eth_link_status = OPA_VNIC_ETH_LINK_DOWN;
1867f291d28SNiranjana Vishwanathapura 	adapter->info.vesw.eth_mtu = ETH_DATA_LEN;
187cfd34f8eSVishwanathapura, Niranjana }
188cfd34f8eSVishwanathapura, Niranjana 
1897d6f728cSVishwanathapura, Niranjana /* opa_vnic_set_mac_addr - change mac address */
1907d6f728cSVishwanathapura, Niranjana static int opa_vnic_set_mac_addr(struct net_device *netdev, void *addr)
1917d6f728cSVishwanathapura, Niranjana {
1927d6f728cSVishwanathapura, Niranjana 	struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
1937d6f728cSVishwanathapura, Niranjana 	struct sockaddr *sa = addr;
1947d6f728cSVishwanathapura, Niranjana 	int rc;
1957d6f728cSVishwanathapura, Niranjana 
1967d6f728cSVishwanathapura, Niranjana 	if (!memcmp(netdev->dev_addr, sa->sa_data, ETH_ALEN))
1977d6f728cSVishwanathapura, Niranjana 		return 0;
1987d6f728cSVishwanathapura, Niranjana 
1997d6f728cSVishwanathapura, Niranjana 	mutex_lock(&adapter->lock);
2007d6f728cSVishwanathapura, Niranjana 	rc = eth_mac_addr(netdev, addr);
2017d6f728cSVishwanathapura, Niranjana 	mutex_unlock(&adapter->lock);
202cfd34f8eSVishwanathapura, Niranjana 	if (rc)
2037d6f728cSVishwanathapura, Niranjana 		return rc;
204cfd34f8eSVishwanathapura, Niranjana 
205cfd34f8eSVishwanathapura, Niranjana 	adapter->info.vport.uc_macs_gen_count++;
206cfd34f8eSVishwanathapura, Niranjana 	opa_vnic_vema_report_event(adapter,
207cfd34f8eSVishwanathapura, Niranjana 				   OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE);
208cfd34f8eSVishwanathapura, Niranjana 	return 0;
209cfd34f8eSVishwanathapura, Niranjana }
210cfd34f8eSVishwanathapura, Niranjana 
211cfd34f8eSVishwanathapura, Niranjana /*
212cfd34f8eSVishwanathapura, Niranjana  * opa_vnic_mac_send_event - post event on possible mac list exchange
213cfd34f8eSVishwanathapura, Niranjana  *  Send trap when digest from uc/mc mac list differs from previous run.
214cfd34f8eSVishwanathapura, Niranjana  *  Digest is evaluated similar to how cksum does.
215cfd34f8eSVishwanathapura, Niranjana  */
216cfd34f8eSVishwanathapura, Niranjana static void opa_vnic_mac_send_event(struct net_device *netdev, u8 event)
217cfd34f8eSVishwanathapura, Niranjana {
218cfd34f8eSVishwanathapura, Niranjana 	struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
219cfd34f8eSVishwanathapura, Niranjana 	struct netdev_hw_addr *ha;
220cfd34f8eSVishwanathapura, Niranjana 	struct netdev_hw_addr_list *hw_list;
221cfd34f8eSVishwanathapura, Niranjana 	u32 *ref_crc;
222cfd34f8eSVishwanathapura, Niranjana 	u32 l, crc = 0;
223cfd34f8eSVishwanathapura, Niranjana 
224cfd34f8eSVishwanathapura, Niranjana 	switch (event) {
225cfd34f8eSVishwanathapura, Niranjana 	case OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE:
226cfd34f8eSVishwanathapura, Niranjana 		hw_list = &netdev->uc;
227cfd34f8eSVishwanathapura, Niranjana 		adapter->info.vport.uc_macs_gen_count++;
228cfd34f8eSVishwanathapura, Niranjana 		ref_crc = &adapter->umac_hash;
229cfd34f8eSVishwanathapura, Niranjana 		break;
230cfd34f8eSVishwanathapura, Niranjana 	case OPA_VESWPORT_TRAP_IFACE_MCAST_MAC_CHANGE:
231cfd34f8eSVishwanathapura, Niranjana 		hw_list = &netdev->mc;
232cfd34f8eSVishwanathapura, Niranjana 		adapter->info.vport.mc_macs_gen_count++;
233cfd34f8eSVishwanathapura, Niranjana 		ref_crc = &adapter->mmac_hash;
234cfd34f8eSVishwanathapura, Niranjana 		break;
235cfd34f8eSVishwanathapura, Niranjana 	default:
236cfd34f8eSVishwanathapura, Niranjana 		return;
237cfd34f8eSVishwanathapura, Niranjana 	}
238cfd34f8eSVishwanathapura, Niranjana 	netdev_hw_addr_list_for_each(ha, hw_list) {
239cfd34f8eSVishwanathapura, Niranjana 		crc = crc32_le(crc, ha->addr, ETH_ALEN);
240cfd34f8eSVishwanathapura, Niranjana 	}
241cfd34f8eSVishwanathapura, Niranjana 	l = netdev_hw_addr_list_count(hw_list) * ETH_ALEN;
242cfd34f8eSVishwanathapura, Niranjana 	crc = ~crc32_le(crc, (void *)&l, sizeof(l));
243cfd34f8eSVishwanathapura, Niranjana 
244cfd34f8eSVishwanathapura, Niranjana 	if (crc != *ref_crc) {
245cfd34f8eSVishwanathapura, Niranjana 		*ref_crc = crc;
246cfd34f8eSVishwanathapura, Niranjana 		opa_vnic_vema_report_event(adapter, event);
247cfd34f8eSVishwanathapura, Niranjana 	}
248cfd34f8eSVishwanathapura, Niranjana }
249cfd34f8eSVishwanathapura, Niranjana 
250cfd34f8eSVishwanathapura, Niranjana /* opa_vnic_set_rx_mode - handle uc/mc mac list change */
251cfd34f8eSVishwanathapura, Niranjana static void opa_vnic_set_rx_mode(struct net_device *netdev)
252cfd34f8eSVishwanathapura, Niranjana {
253cfd34f8eSVishwanathapura, Niranjana 	opa_vnic_mac_send_event(netdev,
254cfd34f8eSVishwanathapura, Niranjana 				OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE);
255cfd34f8eSVishwanathapura, Niranjana 
256cfd34f8eSVishwanathapura, Niranjana 	opa_vnic_mac_send_event(netdev,
257cfd34f8eSVishwanathapura, Niranjana 				OPA_VESWPORT_TRAP_IFACE_MCAST_MAC_CHANGE);
2587d6f728cSVishwanathapura, Niranjana }
2597d6f728cSVishwanathapura, Niranjana 
2607d6f728cSVishwanathapura, Niranjana /* opa_netdev_open - activate network interface */
2617d6f728cSVishwanathapura, Niranjana static int opa_netdev_open(struct net_device *netdev)
2627d6f728cSVishwanathapura, Niranjana {
2637d6f728cSVishwanathapura, Niranjana 	struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
2647d6f728cSVishwanathapura, Niranjana 	int rc;
2657d6f728cSVishwanathapura, Niranjana 
2667d6f728cSVishwanathapura, Niranjana 	rc = adapter->rn_ops->ndo_open(adapter->netdev);
2677d6f728cSVishwanathapura, Niranjana 	if (rc) {
2687d6f728cSVishwanathapura, Niranjana 		v_dbg("open failed %d\n", rc);
2697d6f728cSVishwanathapura, Niranjana 		return rc;
2707d6f728cSVishwanathapura, Niranjana 	}
2717d6f728cSVishwanathapura, Niranjana 
272cfd34f8eSVishwanathapura, Niranjana 	/* Update eth link status and send trap */
273cfd34f8eSVishwanathapura, Niranjana 	adapter->info.vport.eth_link_status = OPA_VNIC_ETH_LINK_UP;
274cfd34f8eSVishwanathapura, Niranjana 	opa_vnic_vema_report_event(adapter,
275cfd34f8eSVishwanathapura, Niranjana 				   OPA_VESWPORT_TRAP_ETH_LINK_STATUS_CHANGE);
2767d6f728cSVishwanathapura, Niranjana 	return 0;
2777d6f728cSVishwanathapura, Niranjana }
2787d6f728cSVishwanathapura, Niranjana 
2797d6f728cSVishwanathapura, Niranjana /* opa_netdev_close - disable network interface */
2807d6f728cSVishwanathapura, Niranjana static int opa_netdev_close(struct net_device *netdev)
2817d6f728cSVishwanathapura, Niranjana {
2827d6f728cSVishwanathapura, Niranjana 	struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
2837d6f728cSVishwanathapura, Niranjana 	int rc;
2847d6f728cSVishwanathapura, Niranjana 
2857d6f728cSVishwanathapura, Niranjana 	rc = adapter->rn_ops->ndo_stop(adapter->netdev);
2867d6f728cSVishwanathapura, Niranjana 	if (rc) {
2877d6f728cSVishwanathapura, Niranjana 		v_dbg("close failed %d\n", rc);
2887d6f728cSVishwanathapura, Niranjana 		return rc;
2897d6f728cSVishwanathapura, Niranjana 	}
2907d6f728cSVishwanathapura, Niranjana 
291cfd34f8eSVishwanathapura, Niranjana 	/* Update eth link status and send trap */
292cfd34f8eSVishwanathapura, Niranjana 	adapter->info.vport.eth_link_status = OPA_VNIC_ETH_LINK_DOWN;
293cfd34f8eSVishwanathapura, Niranjana 	opa_vnic_vema_report_event(adapter,
294cfd34f8eSVishwanathapura, Niranjana 				   OPA_VESWPORT_TRAP_ETH_LINK_STATUS_CHANGE);
2957d6f728cSVishwanathapura, Niranjana 	return 0;
2967d6f728cSVishwanathapura, Niranjana }
2977d6f728cSVishwanathapura, Niranjana 
2987d6f728cSVishwanathapura, Niranjana /* netdev ops */
2997d6f728cSVishwanathapura, Niranjana static const struct net_device_ops opa_netdev_ops = {
3007d6f728cSVishwanathapura, Niranjana 	.ndo_open = opa_netdev_open,
3017d6f728cSVishwanathapura, Niranjana 	.ndo_stop = opa_netdev_close,
3027d6f728cSVishwanathapura, Niranjana 	.ndo_start_xmit = opa_netdev_start_xmit,
303009b7dd4SVishwanathapura, Niranjana 	.ndo_get_stats64 = opa_vnic_get_stats64,
304cfd34f8eSVishwanathapura, Niranjana 	.ndo_set_rx_mode = opa_vnic_set_rx_mode,
3057d6f728cSVishwanathapura, Niranjana 	.ndo_select_queue = opa_vnic_select_queue,
3067d6f728cSVishwanathapura, Niranjana 	.ndo_set_mac_address = opa_vnic_set_mac_addr,
3077d6f728cSVishwanathapura, Niranjana };
3087d6f728cSVishwanathapura, Niranjana 
3097d6f728cSVishwanathapura, Niranjana /* opa_vnic_add_netdev - create vnic netdev interface */
3107d6f728cSVishwanathapura, Niranjana struct opa_vnic_adapter *opa_vnic_add_netdev(struct ib_device *ibdev,
3117d6f728cSVishwanathapura, Niranjana 					     u8 port_num, u8 vport_num)
3127d6f728cSVishwanathapura, Niranjana {
3137d6f728cSVishwanathapura, Niranjana 	struct opa_vnic_adapter *adapter;
3147d6f728cSVishwanathapura, Niranjana 	struct net_device *netdev;
3157d6f728cSVishwanathapura, Niranjana 	struct rdma_netdev *rn;
3167d6f728cSVishwanathapura, Niranjana 	int rc;
3177d6f728cSVishwanathapura, Niranjana 
3187d6f728cSVishwanathapura, Niranjana 	netdev = ibdev->alloc_rdma_netdev(ibdev, port_num,
3197d6f728cSVishwanathapura, Niranjana 					  RDMA_NETDEV_OPA_VNIC,
3207d6f728cSVishwanathapura, Niranjana 					  "veth%d", NET_NAME_UNKNOWN,
3217d6f728cSVishwanathapura, Niranjana 					  ether_setup);
3227d6f728cSVishwanathapura, Niranjana 	if (!netdev)
3237d6f728cSVishwanathapura, Niranjana 		return ERR_PTR(-ENOMEM);
3247d6f728cSVishwanathapura, Niranjana 	else if (IS_ERR(netdev))
3257d6f728cSVishwanathapura, Niranjana 		return ERR_CAST(netdev);
3267d6f728cSVishwanathapura, Niranjana 
3278e959601SNiranjana Vishwanathapura 	rn = netdev_priv(netdev);
3287d6f728cSVishwanathapura, Niranjana 	adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
3297d6f728cSVishwanathapura, Niranjana 	if (!adapter) {
3307d6f728cSVishwanathapura, Niranjana 		rc = -ENOMEM;
3317d6f728cSVishwanathapura, Niranjana 		goto adapter_err;
3327d6f728cSVishwanathapura, Niranjana 	}
3337d6f728cSVishwanathapura, Niranjana 
3347d6f728cSVishwanathapura, Niranjana 	rn->clnt_priv = adapter;
3357d6f728cSVishwanathapura, Niranjana 	rn->hca = ibdev;
3367d6f728cSVishwanathapura, Niranjana 	rn->port_num = port_num;
3377d6f728cSVishwanathapura, Niranjana 	adapter->netdev = netdev;
3387d6f728cSVishwanathapura, Niranjana 	adapter->ibdev = ibdev;
3397d6f728cSVishwanathapura, Niranjana 	adapter->port_num = port_num;
3407d6f728cSVishwanathapura, Niranjana 	adapter->vport_num = vport_num;
3417d6f728cSVishwanathapura, Niranjana 	adapter->rn_ops = netdev->netdev_ops;
3427d6f728cSVishwanathapura, Niranjana 
3437d6f728cSVishwanathapura, Niranjana 	netdev->netdev_ops = &opa_netdev_ops;
3447d6f728cSVishwanathapura, Niranjana 	netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3457d6f728cSVishwanathapura, Niranjana 	netdev->hard_header_len += OPA_VNIC_SKB_HEADROOM;
3467d6f728cSVishwanathapura, Niranjana 	mutex_init(&adapter->lock);
347174e03d7SVishwanathapura, Niranjana 	mutex_init(&adapter->mactbl_lock);
348a379d69fSVishwanathapura, Niranjana 	spin_lock_init(&adapter->stats_lock);
3497d6f728cSVishwanathapura, Niranjana 
3507d6f728cSVishwanathapura, Niranjana 	SET_NETDEV_DEV(netdev, ibdev->dev.parent);
3517d6f728cSVishwanathapura, Niranjana 
3527d6f728cSVishwanathapura, Niranjana 	opa_vnic_set_ethtool_ops(netdev);
353cfd34f8eSVishwanathapura, Niranjana 
354cfd34f8eSVishwanathapura, Niranjana 	opa_vnic_set_pod_values(adapter);
355cfd34f8eSVishwanathapura, Niranjana 
3567d6f728cSVishwanathapura, Niranjana 	rc = register_netdev(netdev);
3577d6f728cSVishwanathapura, Niranjana 	if (rc)
3587d6f728cSVishwanathapura, Niranjana 		goto netdev_err;
3597d6f728cSVishwanathapura, Niranjana 
3607d6f728cSVishwanathapura, Niranjana 	netif_carrier_off(netdev);
3617d6f728cSVishwanathapura, Niranjana 	netif_dormant_on(netdev);
3627d6f728cSVishwanathapura, Niranjana 	v_info("initialized\n");
3637d6f728cSVishwanathapura, Niranjana 
3647d6f728cSVishwanathapura, Niranjana 	return adapter;
3657d6f728cSVishwanathapura, Niranjana netdev_err:
3667d6f728cSVishwanathapura, Niranjana 	mutex_destroy(&adapter->lock);
367174e03d7SVishwanathapura, Niranjana 	mutex_destroy(&adapter->mactbl_lock);
3687d6f728cSVishwanathapura, Niranjana 	kfree(adapter);
3697d6f728cSVishwanathapura, Niranjana adapter_err:
3708e959601SNiranjana Vishwanathapura 	rn->free_rdma_netdev(netdev);
3717d6f728cSVishwanathapura, Niranjana 
3727d6f728cSVishwanathapura, Niranjana 	return ERR_PTR(rc);
3737d6f728cSVishwanathapura, Niranjana }
3747d6f728cSVishwanathapura, Niranjana 
3757d6f728cSVishwanathapura, Niranjana /* opa_vnic_rem_netdev - remove vnic netdev interface */
3767d6f728cSVishwanathapura, Niranjana void opa_vnic_rem_netdev(struct opa_vnic_adapter *adapter)
3777d6f728cSVishwanathapura, Niranjana {
3787d6f728cSVishwanathapura, Niranjana 	struct net_device *netdev = adapter->netdev;
3798e959601SNiranjana Vishwanathapura 	struct rdma_netdev *rn = netdev_priv(netdev);
3807d6f728cSVishwanathapura, Niranjana 
3817d6f728cSVishwanathapura, Niranjana 	v_info("removing\n");
3827d6f728cSVishwanathapura, Niranjana 	unregister_netdev(netdev);
383174e03d7SVishwanathapura, Niranjana 	opa_vnic_release_mac_tbl(adapter);
3847d6f728cSVishwanathapura, Niranjana 	mutex_destroy(&adapter->lock);
385174e03d7SVishwanathapura, Niranjana 	mutex_destroy(&adapter->mactbl_lock);
3867d6f728cSVishwanathapura, Niranjana 	kfree(adapter);
3878e959601SNiranjana Vishwanathapura 	rn->free_rdma_netdev(netdev);
3887d6f728cSVishwanathapura, Niranjana }
389