1*145eba1aSCai Huoqing // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
2d4829ea6SVishwanathapura, Niranjana /*
389dcaa36SGrzegorz Andrejczuk  * Copyright(c) 2017 - 2020 Intel Corporation.
4d4829ea6SVishwanathapura, Niranjana  */
5d4829ea6SVishwanathapura, Niranjana 
6d4829ea6SVishwanathapura, Niranjana /*
7d4829ea6SVishwanathapura, Niranjana  * This file contains HFI1 support for VNIC functionality
8d4829ea6SVishwanathapura, Niranjana  */
9d4829ea6SVishwanathapura, Niranjana 
10d4829ea6SVishwanathapura, Niranjana #include <linux/io.h>
11d4829ea6SVishwanathapura, Niranjana #include <linux/if_vlan.h>
12d4829ea6SVishwanathapura, Niranjana 
13d4829ea6SVishwanathapura, Niranjana #include "vnic.h"
144730f4a6SGrzegorz Andrejczuk #include "netdev.h"
15d4829ea6SVishwanathapura, Niranjana 
16d4829ea6SVishwanathapura, Niranjana #define HFI_TX_TIMEOUT_MS 1000
17d4829ea6SVishwanathapura, Niranjana 
18d4829ea6SVishwanathapura, Niranjana #define HFI1_VNIC_RCV_Q_SIZE   1024
19d4829ea6SVishwanathapura, Niranjana 
20d4829ea6SVishwanathapura, Niranjana #define HFI1_VNIC_UP 0
21d4829ea6SVishwanathapura, Niranjana 
22d4829ea6SVishwanathapura, Niranjana static DEFINE_SPINLOCK(vport_cntr_lock);
23d4829ea6SVishwanathapura, Niranjana 
24d4829ea6SVishwanathapura, Niranjana #define SUM_GRP_COUNTERS(stats, qstats, x_grp) do {            \
25d4829ea6SVishwanathapura, Niranjana 		u64 *src64, *dst64;                            \
26d4829ea6SVishwanathapura, Niranjana 		for (src64 = &qstats->x_grp.unicast,           \
27d4829ea6SVishwanathapura, Niranjana 			dst64 = &stats->x_grp.unicast;         \
28d4829ea6SVishwanathapura, Niranjana 			dst64 <= &stats->x_grp.s_1519_max;) {  \
29d4829ea6SVishwanathapura, Niranjana 			*dst64++ += *src64++;                  \
30d4829ea6SVishwanathapura, Niranjana 		}                                              \
31d4829ea6SVishwanathapura, Niranjana 	} while (0)
32d4829ea6SVishwanathapura, Niranjana 
334730f4a6SGrzegorz Andrejczuk #define VNIC_MASK (0xFF)
344730f4a6SGrzegorz Andrejczuk #define VNIC_ID(val) ((1ull << 24) | ((val) & VNIC_MASK))
354730f4a6SGrzegorz Andrejczuk 
36d4829ea6SVishwanathapura, Niranjana /* hfi1_vnic_update_stats - update statistics */
hfi1_vnic_update_stats(struct hfi1_vnic_vport_info * vinfo,struct opa_vnic_stats * stats)37d4829ea6SVishwanathapura, Niranjana static void hfi1_vnic_update_stats(struct hfi1_vnic_vport_info *vinfo,
38d4829ea6SVishwanathapura, Niranjana 				   struct opa_vnic_stats *stats)
39d4829ea6SVishwanathapura, Niranjana {
40d4829ea6SVishwanathapura, Niranjana 	struct net_device *netdev = vinfo->netdev;
41d4829ea6SVishwanathapura, Niranjana 	u8 i;
42d4829ea6SVishwanathapura, Niranjana 
43d4829ea6SVishwanathapura, Niranjana 	/* add tx counters on different queues */
44d4829ea6SVishwanathapura, Niranjana 	for (i = 0; i < vinfo->num_tx_q; i++) {
45d4829ea6SVishwanathapura, Niranjana 		struct opa_vnic_stats *qstats = &vinfo->stats[i];
46d4829ea6SVishwanathapura, Niranjana 		struct rtnl_link_stats64 *qnstats = &vinfo->stats[i].netstats;
47d4829ea6SVishwanathapura, Niranjana 
48d4829ea6SVishwanathapura, Niranjana 		stats->netstats.tx_fifo_errors += qnstats->tx_fifo_errors;
49d4829ea6SVishwanathapura, Niranjana 		stats->netstats.tx_carrier_errors += qnstats->tx_carrier_errors;
50d4829ea6SVishwanathapura, Niranjana 		stats->tx_drop_state += qstats->tx_drop_state;
51d4829ea6SVishwanathapura, Niranjana 		stats->tx_dlid_zero += qstats->tx_dlid_zero;
52d4829ea6SVishwanathapura, Niranjana 
53d4829ea6SVishwanathapura, Niranjana 		SUM_GRP_COUNTERS(stats, qstats, tx_grp);
54d4829ea6SVishwanathapura, Niranjana 		stats->netstats.tx_packets += qnstats->tx_packets;
55d4829ea6SVishwanathapura, Niranjana 		stats->netstats.tx_bytes += qnstats->tx_bytes;
56d4829ea6SVishwanathapura, Niranjana 	}
57d4829ea6SVishwanathapura, Niranjana 
58d4829ea6SVishwanathapura, Niranjana 	/* add rx counters on different queues */
59d4829ea6SVishwanathapura, Niranjana 	for (i = 0; i < vinfo->num_rx_q; i++) {
60d4829ea6SVishwanathapura, Niranjana 		struct opa_vnic_stats *qstats = &vinfo->stats[i];
61d4829ea6SVishwanathapura, Niranjana 		struct rtnl_link_stats64 *qnstats = &vinfo->stats[i].netstats;
62d4829ea6SVishwanathapura, Niranjana 
63d4829ea6SVishwanathapura, Niranjana 		stats->netstats.rx_fifo_errors += qnstats->rx_fifo_errors;
64d4829ea6SVishwanathapura, Niranjana 		stats->netstats.rx_nohandler += qnstats->rx_nohandler;
65d4829ea6SVishwanathapura, Niranjana 		stats->rx_drop_state += qstats->rx_drop_state;
66d4829ea6SVishwanathapura, Niranjana 		stats->rx_oversize += qstats->rx_oversize;
67d4829ea6SVishwanathapura, Niranjana 		stats->rx_runt += qstats->rx_runt;
68d4829ea6SVishwanathapura, Niranjana 
69d4829ea6SVishwanathapura, Niranjana 		SUM_GRP_COUNTERS(stats, qstats, rx_grp);
70d4829ea6SVishwanathapura, Niranjana 		stats->netstats.rx_packets += qnstats->rx_packets;
71d4829ea6SVishwanathapura, Niranjana 		stats->netstats.rx_bytes += qnstats->rx_bytes;
72d4829ea6SVishwanathapura, Niranjana 	}
73d4829ea6SVishwanathapura, Niranjana 
74d4829ea6SVishwanathapura, Niranjana 	stats->netstats.tx_errors = stats->netstats.tx_fifo_errors +
75d4829ea6SVishwanathapura, Niranjana 				    stats->netstats.tx_carrier_errors +
76d4829ea6SVishwanathapura, Niranjana 				    stats->tx_drop_state + stats->tx_dlid_zero;
77d4829ea6SVishwanathapura, Niranjana 	stats->netstats.tx_dropped = stats->netstats.tx_errors;
78d4829ea6SVishwanathapura, Niranjana 
79d4829ea6SVishwanathapura, Niranjana 	stats->netstats.rx_errors = stats->netstats.rx_fifo_errors +
80d4829ea6SVishwanathapura, Niranjana 				    stats->netstats.rx_nohandler +
81d4829ea6SVishwanathapura, Niranjana 				    stats->rx_drop_state + stats->rx_oversize +
82d4829ea6SVishwanathapura, Niranjana 				    stats->rx_runt;
83d4829ea6SVishwanathapura, Niranjana 	stats->netstats.rx_dropped = stats->netstats.rx_errors;
84d4829ea6SVishwanathapura, Niranjana 
85d4829ea6SVishwanathapura, Niranjana 	netdev->stats.tx_packets = stats->netstats.tx_packets;
86d4829ea6SVishwanathapura, Niranjana 	netdev->stats.tx_bytes = stats->netstats.tx_bytes;
87d4829ea6SVishwanathapura, Niranjana 	netdev->stats.tx_fifo_errors = stats->netstats.tx_fifo_errors;
88d4829ea6SVishwanathapura, Niranjana 	netdev->stats.tx_carrier_errors = stats->netstats.tx_carrier_errors;
89d4829ea6SVishwanathapura, Niranjana 	netdev->stats.tx_errors = stats->netstats.tx_errors;
90d4829ea6SVishwanathapura, Niranjana 	netdev->stats.tx_dropped = stats->netstats.tx_dropped;
91d4829ea6SVishwanathapura, Niranjana 
92d4829ea6SVishwanathapura, Niranjana 	netdev->stats.rx_packets = stats->netstats.rx_packets;
93d4829ea6SVishwanathapura, Niranjana 	netdev->stats.rx_bytes = stats->netstats.rx_bytes;
94d4829ea6SVishwanathapura, Niranjana 	netdev->stats.rx_fifo_errors = stats->netstats.rx_fifo_errors;
95d4829ea6SVishwanathapura, Niranjana 	netdev->stats.multicast = stats->rx_grp.mcastbcast;
96d4829ea6SVishwanathapura, Niranjana 	netdev->stats.rx_length_errors = stats->rx_oversize + stats->rx_runt;
97d4829ea6SVishwanathapura, Niranjana 	netdev->stats.rx_errors = stats->netstats.rx_errors;
98d4829ea6SVishwanathapura, Niranjana 	netdev->stats.rx_dropped = stats->netstats.rx_dropped;
99d4829ea6SVishwanathapura, Niranjana }
100d4829ea6SVishwanathapura, Niranjana 
101d4829ea6SVishwanathapura, Niranjana /* update_len_counters - update pkt's len histogram counters */
update_len_counters(struct opa_vnic_grp_stats * grp,int len)102d4829ea6SVishwanathapura, Niranjana static inline void update_len_counters(struct opa_vnic_grp_stats *grp,
103d4829ea6SVishwanathapura, Niranjana 				       int len)
104d4829ea6SVishwanathapura, Niranjana {
105d4829ea6SVishwanathapura, Niranjana 	/* account for 4 byte FCS */
106d4829ea6SVishwanathapura, Niranjana 	if (len >= 1515)
107d4829ea6SVishwanathapura, Niranjana 		grp->s_1519_max++;
108d4829ea6SVishwanathapura, Niranjana 	else if (len >= 1020)
109d4829ea6SVishwanathapura, Niranjana 		grp->s_1024_1518++;
110d4829ea6SVishwanathapura, Niranjana 	else if (len >= 508)
111d4829ea6SVishwanathapura, Niranjana 		grp->s_512_1023++;
112d4829ea6SVishwanathapura, Niranjana 	else if (len >= 252)
113d4829ea6SVishwanathapura, Niranjana 		grp->s_256_511++;
114d4829ea6SVishwanathapura, Niranjana 	else if (len >= 124)
115d4829ea6SVishwanathapura, Niranjana 		grp->s_128_255++;
116d4829ea6SVishwanathapura, Niranjana 	else if (len >= 61)
117d4829ea6SVishwanathapura, Niranjana 		grp->s_65_127++;
118d4829ea6SVishwanathapura, Niranjana 	else
119d4829ea6SVishwanathapura, Niranjana 		grp->s_64++;
120d4829ea6SVishwanathapura, Niranjana }
121d4829ea6SVishwanathapura, Niranjana 
122d4829ea6SVishwanathapura, Niranjana /* hfi1_vnic_update_tx_counters - update transmit counters */
hfi1_vnic_update_tx_counters(struct hfi1_vnic_vport_info * vinfo,u8 q_idx,struct sk_buff * skb,int err)123d4829ea6SVishwanathapura, Niranjana static void hfi1_vnic_update_tx_counters(struct hfi1_vnic_vport_info *vinfo,
124d4829ea6SVishwanathapura, Niranjana 					 u8 q_idx, struct sk_buff *skb, int err)
125d4829ea6SVishwanathapura, Niranjana {
126d4829ea6SVishwanathapura, Niranjana 	struct ethhdr *mac_hdr = (struct ethhdr *)skb_mac_header(skb);
127d4829ea6SVishwanathapura, Niranjana 	struct opa_vnic_stats *stats = &vinfo->stats[q_idx];
128d4829ea6SVishwanathapura, Niranjana 	struct opa_vnic_grp_stats *tx_grp = &stats->tx_grp;
129d4829ea6SVishwanathapura, Niranjana 	u16 vlan_tci;
130d4829ea6SVishwanathapura, Niranjana 
131d4829ea6SVishwanathapura, Niranjana 	stats->netstats.tx_packets++;
132d4829ea6SVishwanathapura, Niranjana 	stats->netstats.tx_bytes += skb->len + ETH_FCS_LEN;
133d4829ea6SVishwanathapura, Niranjana 
134d4829ea6SVishwanathapura, Niranjana 	update_len_counters(tx_grp, skb->len);
135d4829ea6SVishwanathapura, Niranjana 
136d4829ea6SVishwanathapura, Niranjana 	/* rest of the counts are for good packets only */
137d4829ea6SVishwanathapura, Niranjana 	if (unlikely(err))
138d4829ea6SVishwanathapura, Niranjana 		return;
139d4829ea6SVishwanathapura, Niranjana 
140d4829ea6SVishwanathapura, Niranjana 	if (is_multicast_ether_addr(mac_hdr->h_dest))
141d4829ea6SVishwanathapura, Niranjana 		tx_grp->mcastbcast++;
142d4829ea6SVishwanathapura, Niranjana 	else
143d4829ea6SVishwanathapura, Niranjana 		tx_grp->unicast++;
144d4829ea6SVishwanathapura, Niranjana 
145d4829ea6SVishwanathapura, Niranjana 	if (!__vlan_get_tag(skb, &vlan_tci))
146d4829ea6SVishwanathapura, Niranjana 		tx_grp->vlan++;
147d4829ea6SVishwanathapura, Niranjana 	else
148d4829ea6SVishwanathapura, Niranjana 		tx_grp->untagged++;
149d4829ea6SVishwanathapura, Niranjana }
150d4829ea6SVishwanathapura, Niranjana 
151d4829ea6SVishwanathapura, Niranjana /* hfi1_vnic_update_rx_counters - update receive counters */
hfi1_vnic_update_rx_counters(struct hfi1_vnic_vport_info * vinfo,u8 q_idx,struct sk_buff * skb,int err)152d4829ea6SVishwanathapura, Niranjana static void hfi1_vnic_update_rx_counters(struct hfi1_vnic_vport_info *vinfo,
153d4829ea6SVishwanathapura, Niranjana 					 u8 q_idx, struct sk_buff *skb, int err)
154d4829ea6SVishwanathapura, Niranjana {
155d4829ea6SVishwanathapura, Niranjana 	struct ethhdr *mac_hdr = (struct ethhdr *)skb->data;
156d4829ea6SVishwanathapura, Niranjana 	struct opa_vnic_stats *stats = &vinfo->stats[q_idx];
157d4829ea6SVishwanathapura, Niranjana 	struct opa_vnic_grp_stats *rx_grp = &stats->rx_grp;
158d4829ea6SVishwanathapura, Niranjana 	u16 vlan_tci;
159d4829ea6SVishwanathapura, Niranjana 
160d4829ea6SVishwanathapura, Niranjana 	stats->netstats.rx_packets++;
161d4829ea6SVishwanathapura, Niranjana 	stats->netstats.rx_bytes += skb->len + ETH_FCS_LEN;
162d4829ea6SVishwanathapura, Niranjana 
163d4829ea6SVishwanathapura, Niranjana 	update_len_counters(rx_grp, skb->len);
164d4829ea6SVishwanathapura, Niranjana 
165d4829ea6SVishwanathapura, Niranjana 	/* rest of the counts are for good packets only */
166d4829ea6SVishwanathapura, Niranjana 	if (unlikely(err))
167d4829ea6SVishwanathapura, Niranjana 		return;
168d4829ea6SVishwanathapura, Niranjana 
169d4829ea6SVishwanathapura, Niranjana 	if (is_multicast_ether_addr(mac_hdr->h_dest))
170d4829ea6SVishwanathapura, Niranjana 		rx_grp->mcastbcast++;
171d4829ea6SVishwanathapura, Niranjana 	else
172d4829ea6SVishwanathapura, Niranjana 		rx_grp->unicast++;
173d4829ea6SVishwanathapura, Niranjana 
174d4829ea6SVishwanathapura, Niranjana 	if (!__vlan_get_tag(skb, &vlan_tci))
175d4829ea6SVishwanathapura, Niranjana 		rx_grp->vlan++;
176d4829ea6SVishwanathapura, Niranjana 	else
177d4829ea6SVishwanathapura, Niranjana 		rx_grp->untagged++;
178d4829ea6SVishwanathapura, Niranjana }
179d4829ea6SVishwanathapura, Niranjana 
180d4829ea6SVishwanathapura, Niranjana /* This function is overloaded for opa_vnic specific implementation */
hfi1_vnic_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)181d4829ea6SVishwanathapura, Niranjana static void hfi1_vnic_get_stats64(struct net_device *netdev,
182d4829ea6SVishwanathapura, Niranjana 				  struct rtnl_link_stats64 *stats)
183d4829ea6SVishwanathapura, Niranjana {
184d4829ea6SVishwanathapura, Niranjana 	struct opa_vnic_stats *vstats = (struct opa_vnic_stats *)stats;
185d4829ea6SVishwanathapura, Niranjana 	struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
186d4829ea6SVishwanathapura, Niranjana 
187d4829ea6SVishwanathapura, Niranjana 	hfi1_vnic_update_stats(vinfo, vstats);
188d4829ea6SVishwanathapura, Niranjana }
189d4829ea6SVishwanathapura, Niranjana 
create_bypass_pbc(u32 vl,u32 dw_len)190d4829ea6SVishwanathapura, Niranjana static u64 create_bypass_pbc(u32 vl, u32 dw_len)
191d4829ea6SVishwanathapura, Niranjana {
192d4829ea6SVishwanathapura, Niranjana 	u64 pbc;
193d4829ea6SVishwanathapura, Niranjana 
194d4829ea6SVishwanathapura, Niranjana 	pbc = ((u64)PBC_IHCRC_NONE << PBC_INSERT_HCRC_SHIFT)
195d4829ea6SVishwanathapura, Niranjana 		| PBC_INSERT_BYPASS_ICRC | PBC_CREDIT_RETURN
196d4829ea6SVishwanathapura, Niranjana 		| PBC_PACKET_BYPASS
197d4829ea6SVishwanathapura, Niranjana 		| ((vl & PBC_VL_MASK) << PBC_VL_SHIFT)
198d4829ea6SVishwanathapura, Niranjana 		| (dw_len & PBC_LENGTH_DWS_MASK) << PBC_LENGTH_DWS_SHIFT;
199d4829ea6SVishwanathapura, Niranjana 
200d4829ea6SVishwanathapura, Niranjana 	return pbc;
201d4829ea6SVishwanathapura, Niranjana }
202d4829ea6SVishwanathapura, Niranjana 
203d4829ea6SVishwanathapura, Niranjana /* hfi1_vnic_maybe_stop_tx - stop tx queue if required */
hfi1_vnic_maybe_stop_tx(struct hfi1_vnic_vport_info * vinfo,u8 q_idx)204d4829ea6SVishwanathapura, Niranjana static void hfi1_vnic_maybe_stop_tx(struct hfi1_vnic_vport_info *vinfo,
205d4829ea6SVishwanathapura, Niranjana 				    u8 q_idx)
206d4829ea6SVishwanathapura, Niranjana {
207d4829ea6SVishwanathapura, Niranjana 	netif_stop_subqueue(vinfo->netdev, q_idx);
20864551edeSVishwanathapura, Niranjana 	if (!hfi1_vnic_sdma_write_avail(vinfo, q_idx))
20964551edeSVishwanathapura, Niranjana 		return;
21064551edeSVishwanathapura, Niranjana 
21164551edeSVishwanathapura, Niranjana 	netif_start_subqueue(vinfo->netdev, q_idx);
212d4829ea6SVishwanathapura, Niranjana }
213d4829ea6SVishwanathapura, Niranjana 
hfi1_netdev_start_xmit(struct sk_buff * skb,struct net_device * netdev)214d4829ea6SVishwanathapura, Niranjana static netdev_tx_t hfi1_netdev_start_xmit(struct sk_buff *skb,
215d4829ea6SVishwanathapura, Niranjana 					  struct net_device *netdev)
216d4829ea6SVishwanathapura, Niranjana {
217d4829ea6SVishwanathapura, Niranjana 	struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
218d4829ea6SVishwanathapura, Niranjana 	u8 pad_len, q_idx = skb->queue_mapping;
219d4829ea6SVishwanathapura, Niranjana 	struct hfi1_devdata *dd = vinfo->dd;
220d4829ea6SVishwanathapura, Niranjana 	struct opa_vnic_skb_mdata *mdata;
221d4829ea6SVishwanathapura, Niranjana 	u32 pkt_len, total_len;
222d4829ea6SVishwanathapura, Niranjana 	int err = -EINVAL;
223d4829ea6SVishwanathapura, Niranjana 	u64 pbc;
224d4829ea6SVishwanathapura, Niranjana 
225d4829ea6SVishwanathapura, Niranjana 	v_dbg("xmit: queue %d skb len %d\n", q_idx, skb->len);
226d4829ea6SVishwanathapura, Niranjana 	if (unlikely(!netif_oper_up(netdev))) {
227d4829ea6SVishwanathapura, Niranjana 		vinfo->stats[q_idx].tx_drop_state++;
228d4829ea6SVishwanathapura, Niranjana 		goto tx_finish;
229d4829ea6SVishwanathapura, Niranjana 	}
230d4829ea6SVishwanathapura, Niranjana 
231d4829ea6SVishwanathapura, Niranjana 	/* take out meta data */
232d4829ea6SVishwanathapura, Niranjana 	mdata = (struct opa_vnic_skb_mdata *)skb->data;
233d4829ea6SVishwanathapura, Niranjana 	skb_pull(skb, sizeof(*mdata));
234d4829ea6SVishwanathapura, Niranjana 	if (unlikely(mdata->flags & OPA_VNIC_SKB_MDATA_ENCAP_ERR)) {
235d4829ea6SVishwanathapura, Niranjana 		vinfo->stats[q_idx].tx_dlid_zero++;
236d4829ea6SVishwanathapura, Niranjana 		goto tx_finish;
237d4829ea6SVishwanathapura, Niranjana 	}
238d4829ea6SVishwanathapura, Niranjana 
239d4829ea6SVishwanathapura, Niranjana 	/* add tail padding (for 8 bytes size alignment) and icrc */
240d4829ea6SVishwanathapura, Niranjana 	pad_len = -(skb->len + OPA_VNIC_ICRC_TAIL_LEN) & 0x7;
241d4829ea6SVishwanathapura, Niranjana 	pad_len += OPA_VNIC_ICRC_TAIL_LEN;
242d4829ea6SVishwanathapura, Niranjana 
243d4829ea6SVishwanathapura, Niranjana 	/*
244d4829ea6SVishwanathapura, Niranjana 	 * pkt_len is how much data we have to write, includes header and data.
245d4829ea6SVishwanathapura, Niranjana 	 * total_len is length of the packet in Dwords plus the PBC should not
246d4829ea6SVishwanathapura, Niranjana 	 * include the CRC.
247d4829ea6SVishwanathapura, Niranjana 	 */
248d4829ea6SVishwanathapura, Niranjana 	pkt_len = (skb->len + pad_len) >> 2;
249d4829ea6SVishwanathapura, Niranjana 	total_len = pkt_len + 2; /* PBC + packet */
250d4829ea6SVishwanathapura, Niranjana 
251d4829ea6SVishwanathapura, Niranjana 	pbc = create_bypass_pbc(mdata->vl, total_len);
252d4829ea6SVishwanathapura, Niranjana 
253d4829ea6SVishwanathapura, Niranjana 	skb_get(skb);
254d4829ea6SVishwanathapura, Niranjana 	v_dbg("pbc 0x%016llX len %d pad_len %d\n", pbc, skb->len, pad_len);
255d4829ea6SVishwanathapura, Niranjana 	err = dd->process_vnic_dma_send(dd, q_idx, vinfo, skb, pbc, pad_len);
256d4829ea6SVishwanathapura, Niranjana 	if (unlikely(err)) {
257d4829ea6SVishwanathapura, Niranjana 		if (err == -ENOMEM)
258d4829ea6SVishwanathapura, Niranjana 			vinfo->stats[q_idx].netstats.tx_fifo_errors++;
259d4829ea6SVishwanathapura, Niranjana 		else if (err != -EBUSY)
260d4829ea6SVishwanathapura, Niranjana 			vinfo->stats[q_idx].netstats.tx_carrier_errors++;
261d4829ea6SVishwanathapura, Niranjana 	}
262d4829ea6SVishwanathapura, Niranjana 	/* remove the header before updating tx counters */
263d4829ea6SVishwanathapura, Niranjana 	skb_pull(skb, OPA_VNIC_HDR_LEN);
264d4829ea6SVishwanathapura, Niranjana 
265d4829ea6SVishwanathapura, Niranjana 	if (unlikely(err == -EBUSY)) {
266d4829ea6SVishwanathapura, Niranjana 		hfi1_vnic_maybe_stop_tx(vinfo, q_idx);
267d4829ea6SVishwanathapura, Niranjana 		dev_kfree_skb_any(skb);
268d4829ea6SVishwanathapura, Niranjana 		return NETDEV_TX_BUSY;
269d4829ea6SVishwanathapura, Niranjana 	}
270d4829ea6SVishwanathapura, Niranjana 
271d4829ea6SVishwanathapura, Niranjana tx_finish:
272d4829ea6SVishwanathapura, Niranjana 	/* update tx counters */
273d4829ea6SVishwanathapura, Niranjana 	hfi1_vnic_update_tx_counters(vinfo, q_idx, skb, err);
274d4829ea6SVishwanathapura, Niranjana 	dev_kfree_skb_any(skb);
275d4829ea6SVishwanathapura, Niranjana 	return NETDEV_TX_OK;
276d4829ea6SVishwanathapura, Niranjana }
277d4829ea6SVishwanathapura, Niranjana 
hfi1_vnic_select_queue(struct net_device * netdev,struct sk_buff * skb,struct net_device * sb_dev)278d4829ea6SVishwanathapura, Niranjana static u16 hfi1_vnic_select_queue(struct net_device *netdev,
279d4829ea6SVishwanathapura, Niranjana 				  struct sk_buff *skb,
280a350ecceSPaolo Abeni 				  struct net_device *sb_dev)
281d4829ea6SVishwanathapura, Niranjana {
28264551edeSVishwanathapura, Niranjana 	struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
28364551edeSVishwanathapura, Niranjana 	struct opa_vnic_skb_mdata *mdata;
28464551edeSVishwanathapura, Niranjana 	struct sdma_engine *sde;
28564551edeSVishwanathapura, Niranjana 
28664551edeSVishwanathapura, Niranjana 	mdata = (struct opa_vnic_skb_mdata *)skb->data;
28764551edeSVishwanathapura, Niranjana 	sde = sdma_select_engine_vl(vinfo->dd, mdata->entropy, mdata->vl);
28864551edeSVishwanathapura, Niranjana 	return sde->this_idx;
289d4829ea6SVishwanathapura, Niranjana }
290d4829ea6SVishwanathapura, Niranjana 
291d4829ea6SVishwanathapura, Niranjana /* hfi1_vnic_decap_skb - strip OPA header from the skb (ethernet) packet */
hfi1_vnic_decap_skb(struct hfi1_vnic_rx_queue * rxq,struct sk_buff * skb)292d4829ea6SVishwanathapura, Niranjana static inline int hfi1_vnic_decap_skb(struct hfi1_vnic_rx_queue *rxq,
293d4829ea6SVishwanathapura, Niranjana 				      struct sk_buff *skb)
294d4829ea6SVishwanathapura, Niranjana {
295d4829ea6SVishwanathapura, Niranjana 	struct hfi1_vnic_vport_info *vinfo = rxq->vinfo;
296d4829ea6SVishwanathapura, Niranjana 	int max_len = vinfo->netdev->mtu + VLAN_ETH_HLEN;
297d4829ea6SVishwanathapura, Niranjana 	int rc = -EFAULT;
298d4829ea6SVishwanathapura, Niranjana 
299d4829ea6SVishwanathapura, Niranjana 	skb_pull(skb, OPA_VNIC_HDR_LEN);
300d4829ea6SVishwanathapura, Niranjana 
301d4829ea6SVishwanathapura, Niranjana 	/* Validate Packet length */
302d4829ea6SVishwanathapura, Niranjana 	if (unlikely(skb->len > max_len))
303d4829ea6SVishwanathapura, Niranjana 		vinfo->stats[rxq->idx].rx_oversize++;
304d4829ea6SVishwanathapura, Niranjana 	else if (unlikely(skb->len < ETH_ZLEN))
305d4829ea6SVishwanathapura, Niranjana 		vinfo->stats[rxq->idx].rx_runt++;
306d4829ea6SVishwanathapura, Niranjana 	else
307d4829ea6SVishwanathapura, Niranjana 		rc = 0;
308d4829ea6SVishwanathapura, Niranjana 	return rc;
309d4829ea6SVishwanathapura, Niranjana }
310d4829ea6SVishwanathapura, Niranjana 
get_vnic_port(struct hfi1_devdata * dd,int vesw_id)3114730f4a6SGrzegorz Andrejczuk static struct hfi1_vnic_vport_info *get_vnic_port(struct hfi1_devdata *dd,
3124730f4a6SGrzegorz Andrejczuk 						  int vesw_id)
313d4829ea6SVishwanathapura, Niranjana {
3144730f4a6SGrzegorz Andrejczuk 	int vnic_id = VNIC_ID(vesw_id);
315d4829ea6SVishwanathapura, Niranjana 
3164730f4a6SGrzegorz Andrejczuk 	return hfi1_netdev_get_data(dd, vnic_id);
3174730f4a6SGrzegorz Andrejczuk }
3184730f4a6SGrzegorz Andrejczuk 
get_first_vnic_port(struct hfi1_devdata * dd)3194730f4a6SGrzegorz Andrejczuk static struct hfi1_vnic_vport_info *get_first_vnic_port(struct hfi1_devdata *dd)
3204730f4a6SGrzegorz Andrejczuk {
3214730f4a6SGrzegorz Andrejczuk 	struct hfi1_vnic_vport_info *vinfo;
3224730f4a6SGrzegorz Andrejczuk 	int next_id = VNIC_ID(0);
3234730f4a6SGrzegorz Andrejczuk 
3244730f4a6SGrzegorz Andrejczuk 	vinfo = hfi1_netdev_get_first_data(dd, &next_id);
3254730f4a6SGrzegorz Andrejczuk 
3264730f4a6SGrzegorz Andrejczuk 	if (next_id > VNIC_ID(VNIC_MASK))
327d4829ea6SVishwanathapura, Niranjana 		return NULL;
328d4829ea6SVishwanathapura, Niranjana 
3294730f4a6SGrzegorz Andrejczuk 	return vinfo;
330d4829ea6SVishwanathapura, Niranjana }
331d4829ea6SVishwanathapura, Niranjana 
hfi1_vnic_bypass_rcv(struct hfi1_packet * packet)332d4829ea6SVishwanathapura, Niranjana void hfi1_vnic_bypass_rcv(struct hfi1_packet *packet)
333d4829ea6SVishwanathapura, Niranjana {
334d4829ea6SVishwanathapura, Niranjana 	struct hfi1_devdata *dd = packet->rcd->dd;
335d4829ea6SVishwanathapura, Niranjana 	struct hfi1_vnic_vport_info *vinfo = NULL;
336d4829ea6SVishwanathapura, Niranjana 	struct hfi1_vnic_rx_queue *rxq;
337d4829ea6SVishwanathapura, Niranjana 	struct sk_buff *skb;
3384730f4a6SGrzegorz Andrejczuk 	int l4_type, vesw_id = -1, rc;
339d4829ea6SVishwanathapura, Niranjana 	u8 q_idx;
3404730f4a6SGrzegorz Andrejczuk 	unsigned char *pad_info;
341d4829ea6SVishwanathapura, Niranjana 
34272c07e2bSDon Hiatt 	l4_type = hfi1_16B_get_l4(packet->ebuf);
34372c07e2bSDon Hiatt 	if (likely(l4_type == OPA_16B_L4_ETHR)) {
344d4829ea6SVishwanathapura, Niranjana 		vesw_id = HFI1_VNIC_GET_VESWID(packet->ebuf);
3454730f4a6SGrzegorz Andrejczuk 		vinfo = get_vnic_port(dd, vesw_id);
346d4829ea6SVishwanathapura, Niranjana 
347d4829ea6SVishwanathapura, Niranjana 		/*
348d4829ea6SVishwanathapura, Niranjana 		 * In case of invalid vesw id, count the error on
349d4829ea6SVishwanathapura, Niranjana 		 * the first available vport.
350d4829ea6SVishwanathapura, Niranjana 		 */
351d4829ea6SVishwanathapura, Niranjana 		if (unlikely(!vinfo)) {
352d4829ea6SVishwanathapura, Niranjana 			struct hfi1_vnic_vport_info *vinfo_tmp;
353d4829ea6SVishwanathapura, Niranjana 
3544730f4a6SGrzegorz Andrejczuk 			vinfo_tmp = get_first_vnic_port(dd);
355d4829ea6SVishwanathapura, Niranjana 			if (vinfo_tmp) {
356d4829ea6SVishwanathapura, Niranjana 				spin_lock(&vport_cntr_lock);
357d4829ea6SVishwanathapura, Niranjana 				vinfo_tmp->stats[0].netstats.rx_nohandler++;
358d4829ea6SVishwanathapura, Niranjana 				spin_unlock(&vport_cntr_lock);
359d4829ea6SVishwanathapura, Niranjana 			}
360d4829ea6SVishwanathapura, Niranjana 		}
361d4829ea6SVishwanathapura, Niranjana 	}
362d4829ea6SVishwanathapura, Niranjana 
363d4829ea6SVishwanathapura, Niranjana 	if (unlikely(!vinfo)) {
364d4829ea6SVishwanathapura, Niranjana 		dd_dev_warn(dd, "vnic rcv err: l4 %d vesw id %d ctx %d\n",
365d4829ea6SVishwanathapura, Niranjana 			    l4_type, vesw_id, packet->rcd->ctxt);
366d4829ea6SVishwanathapura, Niranjana 		return;
367d4829ea6SVishwanathapura, Niranjana 	}
368d4829ea6SVishwanathapura, Niranjana 
369d4829ea6SVishwanathapura, Niranjana 	q_idx = packet->rcd->vnic_q_idx;
370d4829ea6SVishwanathapura, Niranjana 	rxq = &vinfo->rxq[q_idx];
371d4829ea6SVishwanathapura, Niranjana 	if (unlikely(!netif_oper_up(vinfo->netdev))) {
372d4829ea6SVishwanathapura, Niranjana 		vinfo->stats[q_idx].rx_drop_state++;
373d4829ea6SVishwanathapura, Niranjana 		return;
374d4829ea6SVishwanathapura, Niranjana 	}
375d4829ea6SVishwanathapura, Niranjana 
376d4829ea6SVishwanathapura, Niranjana 	skb = netdev_alloc_skb(vinfo->netdev, packet->tlen);
377d4829ea6SVishwanathapura, Niranjana 	if (unlikely(!skb)) {
378d4829ea6SVishwanathapura, Niranjana 		vinfo->stats[q_idx].netstats.rx_fifo_errors++;
379d4829ea6SVishwanathapura, Niranjana 		return;
380d4829ea6SVishwanathapura, Niranjana 	}
381d4829ea6SVishwanathapura, Niranjana 
382d4829ea6SVishwanathapura, Niranjana 	memcpy(skb->data, packet->ebuf, packet->tlen);
383d4829ea6SVishwanathapura, Niranjana 	skb_put(skb, packet->tlen);
384d4829ea6SVishwanathapura, Niranjana 
3854730f4a6SGrzegorz Andrejczuk 	pad_info = skb->data + skb->len - 1;
3864730f4a6SGrzegorz Andrejczuk 	skb_trim(skb, (skb->len - OPA_VNIC_ICRC_TAIL_LEN -
3874730f4a6SGrzegorz Andrejczuk 		       ((*pad_info) & 0x7)));
3884730f4a6SGrzegorz Andrejczuk 
3894730f4a6SGrzegorz Andrejczuk 	rc = hfi1_vnic_decap_skb(rxq, skb);
3904730f4a6SGrzegorz Andrejczuk 
3914730f4a6SGrzegorz Andrejczuk 	/* update rx counters */
3924730f4a6SGrzegorz Andrejczuk 	hfi1_vnic_update_rx_counters(vinfo, rxq->idx, skb, rc);
3934730f4a6SGrzegorz Andrejczuk 	if (unlikely(rc)) {
3944730f4a6SGrzegorz Andrejczuk 		dev_kfree_skb_any(skb);
3954730f4a6SGrzegorz Andrejczuk 		return;
396d4829ea6SVishwanathapura, Niranjana 	}
3974730f4a6SGrzegorz Andrejczuk 
3984730f4a6SGrzegorz Andrejczuk 	skb_checksum_none_assert(skb);
3994730f4a6SGrzegorz Andrejczuk 	skb->protocol = eth_type_trans(skb, rxq->netdev);
4004730f4a6SGrzegorz Andrejczuk 
4014730f4a6SGrzegorz Andrejczuk 	napi_gro_receive(&rxq->napi, skb);
402d4829ea6SVishwanathapura, Niranjana }
403d4829ea6SVishwanathapura, Niranjana 
hfi1_vnic_up(struct hfi1_vnic_vport_info * vinfo)404d4829ea6SVishwanathapura, Niranjana static int hfi1_vnic_up(struct hfi1_vnic_vport_info *vinfo)
405d4829ea6SVishwanathapura, Niranjana {
406d4829ea6SVishwanathapura, Niranjana 	struct hfi1_devdata *dd = vinfo->dd;
407d4829ea6SVishwanathapura, Niranjana 	struct net_device *netdev = vinfo->netdev;
4084730f4a6SGrzegorz Andrejczuk 	int rc;
409d4829ea6SVishwanathapura, Niranjana 
410d4829ea6SVishwanathapura, Niranjana 	/* ensure virtual eth switch id is valid */
411d4829ea6SVishwanathapura, Niranjana 	if (!vinfo->vesw_id)
412d4829ea6SVishwanathapura, Niranjana 		return -EINVAL;
413d4829ea6SVishwanathapura, Niranjana 
4144730f4a6SGrzegorz Andrejczuk 	rc = hfi1_netdev_add_data(dd, VNIC_ID(vinfo->vesw_id), vinfo);
415d4829ea6SVishwanathapura, Niranjana 	if (rc < 0)
416d4829ea6SVishwanathapura, Niranjana 		return rc;
417d4829ea6SVishwanathapura, Niranjana 
41824c567ffSDan Carpenter 	rc = hfi1_netdev_rx_init(dd);
41924c567ffSDan Carpenter 	if (rc)
42024c567ffSDan Carpenter 		goto err_remove;
421d4829ea6SVishwanathapura, Niranjana 
422d4829ea6SVishwanathapura, Niranjana 	netif_carrier_on(netdev);
423d4829ea6SVishwanathapura, Niranjana 	netif_tx_start_all_queues(netdev);
424d4829ea6SVishwanathapura, Niranjana 	set_bit(HFI1_VNIC_UP, &vinfo->flags);
425d4829ea6SVishwanathapura, Niranjana 
426d4829ea6SVishwanathapura, Niranjana 	return 0;
42724c567ffSDan Carpenter 
42824c567ffSDan Carpenter err_remove:
42924c567ffSDan Carpenter 	hfi1_netdev_remove_data(dd, VNIC_ID(vinfo->vesw_id));
43024c567ffSDan Carpenter 	return rc;
431d4829ea6SVishwanathapura, Niranjana }
432d4829ea6SVishwanathapura, Niranjana 
hfi1_vnic_down(struct hfi1_vnic_vport_info * vinfo)433d4829ea6SVishwanathapura, Niranjana static void hfi1_vnic_down(struct hfi1_vnic_vport_info *vinfo)
434d4829ea6SVishwanathapura, Niranjana {
435d4829ea6SVishwanathapura, Niranjana 	struct hfi1_devdata *dd = vinfo->dd;
436d4829ea6SVishwanathapura, Niranjana 
437d4829ea6SVishwanathapura, Niranjana 	clear_bit(HFI1_VNIC_UP, &vinfo->flags);
438d4829ea6SVishwanathapura, Niranjana 	netif_carrier_off(vinfo->netdev);
439d4829ea6SVishwanathapura, Niranjana 	netif_tx_disable(vinfo->netdev);
4404730f4a6SGrzegorz Andrejczuk 	hfi1_netdev_remove_data(dd, VNIC_ID(vinfo->vesw_id));
441d4829ea6SVishwanathapura, Niranjana 
4424730f4a6SGrzegorz Andrejczuk 	hfi1_netdev_rx_destroy(dd);
443d4829ea6SVishwanathapura, Niranjana }
444d4829ea6SVishwanathapura, Niranjana 
hfi1_netdev_open(struct net_device * netdev)445d4829ea6SVishwanathapura, Niranjana static int hfi1_netdev_open(struct net_device *netdev)
446d4829ea6SVishwanathapura, Niranjana {
447d4829ea6SVishwanathapura, Niranjana 	struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
448d4829ea6SVishwanathapura, Niranjana 	int rc;
449d4829ea6SVishwanathapura, Niranjana 
450d4829ea6SVishwanathapura, Niranjana 	mutex_lock(&vinfo->lock);
451d4829ea6SVishwanathapura, Niranjana 	rc = hfi1_vnic_up(vinfo);
452d4829ea6SVishwanathapura, Niranjana 	mutex_unlock(&vinfo->lock);
453d4829ea6SVishwanathapura, Niranjana 	return rc;
454d4829ea6SVishwanathapura, Niranjana }
455d4829ea6SVishwanathapura, Niranjana 
hfi1_netdev_close(struct net_device * netdev)456d4829ea6SVishwanathapura, Niranjana static int hfi1_netdev_close(struct net_device *netdev)
457d4829ea6SVishwanathapura, Niranjana {
458d4829ea6SVishwanathapura, Niranjana 	struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
459d4829ea6SVishwanathapura, Niranjana 
460d4829ea6SVishwanathapura, Niranjana 	mutex_lock(&vinfo->lock);
461d4829ea6SVishwanathapura, Niranjana 	if (test_bit(HFI1_VNIC_UP, &vinfo->flags))
462d4829ea6SVishwanathapura, Niranjana 		hfi1_vnic_down(vinfo);
463d4829ea6SVishwanathapura, Niranjana 	mutex_unlock(&vinfo->lock);
464d4829ea6SVishwanathapura, Niranjana 	return 0;
465d4829ea6SVishwanathapura, Niranjana }
466d4829ea6SVishwanathapura, Niranjana 
hfi1_vnic_init(struct hfi1_vnic_vport_info * vinfo)4672280740fSVishwanathapura, Niranjana static int hfi1_vnic_init(struct hfi1_vnic_vport_info *vinfo)
4682280740fSVishwanathapura, Niranjana {
4692280740fSVishwanathapura, Niranjana 	struct hfi1_devdata *dd = vinfo->dd;
4704730f4a6SGrzegorz Andrejczuk 	int rc = 0;
4712280740fSVishwanathapura, Niranjana 
4722280740fSVishwanathapura, Niranjana 	mutex_lock(&hfi1_mutex);
4734730f4a6SGrzegorz Andrejczuk 	if (!dd->vnic_num_vports) {
47464551edeSVishwanathapura, Niranjana 		rc = hfi1_vnic_txreq_init(dd);
47564551edeSVishwanathapura, Niranjana 		if (rc)
47664551edeSVishwanathapura, Niranjana 			goto txreq_fail;
47764551edeSVishwanathapura, Niranjana 	}
4782280740fSVishwanathapura, Niranjana 
47924c567ffSDan Carpenter 	rc = hfi1_netdev_rx_init(dd);
48024c567ffSDan Carpenter 	if (rc) {
4814730f4a6SGrzegorz Andrejczuk 		dd_dev_err(dd, "Unable to initialize netdev contexts\n");
4822280740fSVishwanathapura, Niranjana 		goto alloc_fail;
4832280740fSVishwanathapura, Niranjana 	}
4842280740fSVishwanathapura, Niranjana 
4852280740fSVishwanathapura, Niranjana 	hfi1_init_vnic_rsm(dd);
4862280740fSVishwanathapura, Niranjana 
4874730f4a6SGrzegorz Andrejczuk 	dd->vnic_num_vports++;
48864551edeSVishwanathapura, Niranjana 	hfi1_vnic_sdma_init(vinfo);
4894730f4a6SGrzegorz Andrejczuk 
4902280740fSVishwanathapura, Niranjana alloc_fail:
4914730f4a6SGrzegorz Andrejczuk 	if (!dd->vnic_num_vports)
49264551edeSVishwanathapura, Niranjana 		hfi1_vnic_txreq_deinit(dd);
49364551edeSVishwanathapura, Niranjana txreq_fail:
4942280740fSVishwanathapura, Niranjana 	mutex_unlock(&hfi1_mutex);
4952280740fSVishwanathapura, Niranjana 	return rc;
4962280740fSVishwanathapura, Niranjana }
4972280740fSVishwanathapura, Niranjana 
hfi1_vnic_deinit(struct hfi1_vnic_vport_info * vinfo)4982280740fSVishwanathapura, Niranjana static void hfi1_vnic_deinit(struct hfi1_vnic_vport_info *vinfo)
4992280740fSVishwanathapura, Niranjana {
5002280740fSVishwanathapura, Niranjana 	struct hfi1_devdata *dd = vinfo->dd;
5012280740fSVishwanathapura, Niranjana 
5022280740fSVishwanathapura, Niranjana 	mutex_lock(&hfi1_mutex);
5034730f4a6SGrzegorz Andrejczuk 	if (--dd->vnic_num_vports == 0) {
5042280740fSVishwanathapura, Niranjana 		hfi1_deinit_vnic_rsm(dd);
50564551edeSVishwanathapura, Niranjana 		hfi1_vnic_txreq_deinit(dd);
5062280740fSVishwanathapura, Niranjana 	}
5072280740fSVishwanathapura, Niranjana 	mutex_unlock(&hfi1_mutex);
5084730f4a6SGrzegorz Andrejczuk 	hfi1_netdev_rx_destroy(dd);
5092280740fSVishwanathapura, Niranjana }
5102280740fSVishwanathapura, Niranjana 
hfi1_vnic_set_vesw_id(struct net_device * netdev,int id)511d4829ea6SVishwanathapura, Niranjana static void hfi1_vnic_set_vesw_id(struct net_device *netdev, int id)
512d4829ea6SVishwanathapura, Niranjana {
513d4829ea6SVishwanathapura, Niranjana 	struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
514d4829ea6SVishwanathapura, Niranjana 	bool reopen = false;
515d4829ea6SVishwanathapura, Niranjana 
516d4829ea6SVishwanathapura, Niranjana 	/*
517d4829ea6SVishwanathapura, Niranjana 	 * If vesw_id is being changed, and if the vnic port is up,
518d4829ea6SVishwanathapura, Niranjana 	 * reset the vnic port to ensure new vesw_id gets picked up
519d4829ea6SVishwanathapura, Niranjana 	 */
520d4829ea6SVishwanathapura, Niranjana 	if (id != vinfo->vesw_id) {
521d4829ea6SVishwanathapura, Niranjana 		mutex_lock(&vinfo->lock);
522d4829ea6SVishwanathapura, Niranjana 		if (test_bit(HFI1_VNIC_UP, &vinfo->flags)) {
523d4829ea6SVishwanathapura, Niranjana 			hfi1_vnic_down(vinfo);
524d4829ea6SVishwanathapura, Niranjana 			reopen = true;
525d4829ea6SVishwanathapura, Niranjana 		}
526d4829ea6SVishwanathapura, Niranjana 
527d4829ea6SVishwanathapura, Niranjana 		vinfo->vesw_id = id;
528d4829ea6SVishwanathapura, Niranjana 		if (reopen)
529d4829ea6SVishwanathapura, Niranjana 			hfi1_vnic_up(vinfo);
530d4829ea6SVishwanathapura, Niranjana 
531d4829ea6SVishwanathapura, Niranjana 		mutex_unlock(&vinfo->lock);
532d4829ea6SVishwanathapura, Niranjana 	}
533d4829ea6SVishwanathapura, Niranjana }
534d4829ea6SVishwanathapura, Niranjana 
535d4829ea6SVishwanathapura, Niranjana /* netdev ops */
536d4829ea6SVishwanathapura, Niranjana static const struct net_device_ops hfi1_netdev_ops = {
537d4829ea6SVishwanathapura, Niranjana 	.ndo_open = hfi1_netdev_open,
538d4829ea6SVishwanathapura, Niranjana 	.ndo_stop = hfi1_netdev_close,
539d4829ea6SVishwanathapura, Niranjana 	.ndo_start_xmit = hfi1_netdev_start_xmit,
540d4829ea6SVishwanathapura, Niranjana 	.ndo_select_queue = hfi1_vnic_select_queue,
541d4829ea6SVishwanathapura, Niranjana 	.ndo_get_stats64 = hfi1_vnic_get_stats64,
542d4829ea6SVishwanathapura, Niranjana };
543d4829ea6SVishwanathapura, Niranjana 
hfi1_vnic_free_rn(struct net_device * netdev)5448e959601SNiranjana Vishwanathapura static void hfi1_vnic_free_rn(struct net_device *netdev)
5458e959601SNiranjana Vishwanathapura {
5468e959601SNiranjana Vishwanathapura 	struct hfi1_vnic_vport_info *vinfo = opa_vnic_dev_priv(netdev);
5478e959601SNiranjana Vishwanathapura 
5488e959601SNiranjana Vishwanathapura 	hfi1_vnic_deinit(vinfo);
5498e959601SNiranjana Vishwanathapura 	mutex_destroy(&vinfo->lock);
5508e959601SNiranjana Vishwanathapura 	free_netdev(netdev);
5518e959601SNiranjana Vishwanathapura }
5528e959601SNiranjana Vishwanathapura 
hfi1_vnic_alloc_rn(struct ib_device * device,u32 port_num,enum rdma_netdev_t type,const char * name,unsigned char name_assign_type,void (* setup)(struct net_device *))553d4829ea6SVishwanathapura, Niranjana struct net_device *hfi1_vnic_alloc_rn(struct ib_device *device,
5541fb7f897SMark Bloch 				      u32 port_num,
555d4829ea6SVishwanathapura, Niranjana 				      enum rdma_netdev_t type,
556d4829ea6SVishwanathapura, Niranjana 				      const char *name,
557d4829ea6SVishwanathapura, Niranjana 				      unsigned char name_assign_type,
558d4829ea6SVishwanathapura, Niranjana 				      void (*setup)(struct net_device *))
559d4829ea6SVishwanathapura, Niranjana {
560d4829ea6SVishwanathapura, Niranjana 	struct hfi1_devdata *dd = dd_from_ibdev(device);
561d4829ea6SVishwanathapura, Niranjana 	struct hfi1_vnic_vport_info *vinfo;
562d4829ea6SVishwanathapura, Niranjana 	struct net_device *netdev;
563d4829ea6SVishwanathapura, Niranjana 	struct rdma_netdev *rn;
5642280740fSVishwanathapura, Niranjana 	int i, size, rc;
565d4829ea6SVishwanathapura, Niranjana 
56689dcaa36SGrzegorz Andrejczuk 	if (!dd->num_netdev_contexts)
567d7d62617SMichael J. Ruhl 		return ERR_PTR(-ENOMEM);
568d7d62617SMichael J. Ruhl 
569d4829ea6SVishwanathapura, Niranjana 	if (!port_num || (port_num > dd->num_pports))
570d4829ea6SVishwanathapura, Niranjana 		return ERR_PTR(-EINVAL);
571d4829ea6SVishwanathapura, Niranjana 
572d4829ea6SVishwanathapura, Niranjana 	if (type != RDMA_NETDEV_OPA_VNIC)
573d4829ea6SVishwanathapura, Niranjana 		return ERR_PTR(-EOPNOTSUPP);
574d4829ea6SVishwanathapura, Niranjana 
575d4829ea6SVishwanathapura, Niranjana 	size = sizeof(struct opa_vnic_rdma_netdev) + sizeof(*vinfo);
576d4829ea6SVishwanathapura, Niranjana 	netdev = alloc_netdev_mqs(size, name, name_assign_type, setup,
5774730f4a6SGrzegorz Andrejczuk 				  chip_sdma_engines(dd),
5784730f4a6SGrzegorz Andrejczuk 				  dd->num_netdev_contexts);
579d4829ea6SVishwanathapura, Niranjana 	if (!netdev)
580d4829ea6SVishwanathapura, Niranjana 		return ERR_PTR(-ENOMEM);
581d4829ea6SVishwanathapura, Niranjana 
582d4829ea6SVishwanathapura, Niranjana 	rn = netdev_priv(netdev);
583d4829ea6SVishwanathapura, Niranjana 	vinfo = opa_vnic_dev_priv(netdev);
584d4829ea6SVishwanathapura, Niranjana 	vinfo->dd = dd;
5854730f4a6SGrzegorz Andrejczuk 	vinfo->num_tx_q = chip_sdma_engines(dd);
58689dcaa36SGrzegorz Andrejczuk 	vinfo->num_rx_q = dd->num_netdev_contexts;
587d4829ea6SVishwanathapura, Niranjana 	vinfo->netdev = netdev;
5888e959601SNiranjana Vishwanathapura 	rn->free_rdma_netdev = hfi1_vnic_free_rn;
589d4829ea6SVishwanathapura, Niranjana 	rn->set_id = hfi1_vnic_set_vesw_id;
590d4829ea6SVishwanathapura, Niranjana 
591d4829ea6SVishwanathapura, Niranjana 	netdev->features = NETIF_F_HIGHDMA | NETIF_F_SG;
592d4829ea6SVishwanathapura, Niranjana 	netdev->hw_features = netdev->features;
593d4829ea6SVishwanathapura, Niranjana 	netdev->vlan_features = netdev->features;
594d4829ea6SVishwanathapura, Niranjana 	netdev->watchdog_timeo = msecs_to_jiffies(HFI_TX_TIMEOUT_MS);
595d4829ea6SVishwanathapura, Niranjana 	netdev->netdev_ops = &hfi1_netdev_ops;
596d4829ea6SVishwanathapura, Niranjana 	mutex_init(&vinfo->lock);
597d4829ea6SVishwanathapura, Niranjana 
598d4829ea6SVishwanathapura, Niranjana 	for (i = 0; i < vinfo->num_rx_q; i++) {
599d4829ea6SVishwanathapura, Niranjana 		struct hfi1_vnic_rx_queue *rxq = &vinfo->rxq[i];
600d4829ea6SVishwanathapura, Niranjana 
601d4829ea6SVishwanathapura, Niranjana 		rxq->idx = i;
602d4829ea6SVishwanathapura, Niranjana 		rxq->vinfo = vinfo;
603d4829ea6SVishwanathapura, Niranjana 		rxq->netdev = netdev;
604d4829ea6SVishwanathapura, Niranjana 	}
605d4829ea6SVishwanathapura, Niranjana 
6062280740fSVishwanathapura, Niranjana 	rc = hfi1_vnic_init(vinfo);
6072280740fSVishwanathapura, Niranjana 	if (rc)
6082280740fSVishwanathapura, Niranjana 		goto init_fail;
6092280740fSVishwanathapura, Niranjana 
610d4829ea6SVishwanathapura, Niranjana 	return netdev;
6112280740fSVishwanathapura, Niranjana init_fail:
6122280740fSVishwanathapura, Niranjana 	mutex_destroy(&vinfo->lock);
6132280740fSVishwanathapura, Niranjana 	free_netdev(netdev);
6142280740fSVishwanathapura, Niranjana 	return ERR_PTR(rc);
615d4829ea6SVishwanathapura, Niranjana }
616