1 /*
2  * Linux driver for VMware's vmxnet3 ethernet NIC.
3  *
4  * Copyright (C) 2008-2021, VMware, Inc. All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; version 2 of the License and no later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13  * NON INFRINGEMENT.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * The full GNU General Public License is included in this distribution in
21  * the file called "COPYING".
22  *
23  * Maintained by: pv-drivers@vmware.com
24  *
25  */
26 
27 
28 #include "vmxnet3_int.h"
29 #include <net/vxlan.h>
30 #include <net/geneve.h>
31 
32 #define VXLAN_UDP_PORT 8472
33 
34 struct vmxnet3_stat_desc {
35 	char desc[ETH_GSTRING_LEN];
36 	int  offset;
37 };
38 
39 
40 /* per tq stats maintained by the device */
41 static const struct vmxnet3_stat_desc
42 vmxnet3_tq_dev_stats[] = {
43 	/* description,         offset */
44 	{ "Tx Queue#",        0 },
45 	{ "  TSO pkts tx",	offsetof(struct UPT1_TxStats, TSOPktsTxOK) },
46 	{ "  TSO bytes tx",	offsetof(struct UPT1_TxStats, TSOBytesTxOK) },
47 	{ "  ucast pkts tx",	offsetof(struct UPT1_TxStats, ucastPktsTxOK) },
48 	{ "  ucast bytes tx",	offsetof(struct UPT1_TxStats, ucastBytesTxOK) },
49 	{ "  mcast pkts tx",	offsetof(struct UPT1_TxStats, mcastPktsTxOK) },
50 	{ "  mcast bytes tx",	offsetof(struct UPT1_TxStats, mcastBytesTxOK) },
51 	{ "  bcast pkts tx",	offsetof(struct UPT1_TxStats, bcastPktsTxOK) },
52 	{ "  bcast bytes tx",	offsetof(struct UPT1_TxStats, bcastBytesTxOK) },
53 	{ "  pkts tx err",	offsetof(struct UPT1_TxStats, pktsTxError) },
54 	{ "  pkts tx discard",	offsetof(struct UPT1_TxStats, pktsTxDiscard) },
55 };
56 
57 /* per tq stats maintained by the driver */
58 static const struct vmxnet3_stat_desc
59 vmxnet3_tq_driver_stats[] = {
60 	/* description,         offset */
61 	{"  drv dropped tx total",	offsetof(struct vmxnet3_tq_driver_stats,
62 						 drop_total) },
63 	{ "     too many frags", offsetof(struct vmxnet3_tq_driver_stats,
64 					  drop_too_many_frags) },
65 	{ "     giant hdr",	offsetof(struct vmxnet3_tq_driver_stats,
66 					 drop_oversized_hdr) },
67 	{ "     hdr err",	offsetof(struct vmxnet3_tq_driver_stats,
68 					 drop_hdr_inspect_err) },
69 	{ "     tso",		offsetof(struct vmxnet3_tq_driver_stats,
70 					 drop_tso) },
71 	{ "  ring full",	offsetof(struct vmxnet3_tq_driver_stats,
72 					 tx_ring_full) },
73 	{ "  pkts linearized",	offsetof(struct vmxnet3_tq_driver_stats,
74 					 linearized) },
75 	{ "  hdr cloned",	offsetof(struct vmxnet3_tq_driver_stats,
76 					 copy_skb_header) },
77 	{ "  giant hdr",	offsetof(struct vmxnet3_tq_driver_stats,
78 					 oversized_hdr) },
79 };
80 
81 /* per rq stats maintained by the device */
82 static const struct vmxnet3_stat_desc
83 vmxnet3_rq_dev_stats[] = {
84 	{ "Rx Queue#",        0 },
85 	{ "  LRO pkts rx",	offsetof(struct UPT1_RxStats, LROPktsRxOK) },
86 	{ "  LRO byte rx",	offsetof(struct UPT1_RxStats, LROBytesRxOK) },
87 	{ "  ucast pkts rx",	offsetof(struct UPT1_RxStats, ucastPktsRxOK) },
88 	{ "  ucast bytes rx",	offsetof(struct UPT1_RxStats, ucastBytesRxOK) },
89 	{ "  mcast pkts rx",	offsetof(struct UPT1_RxStats, mcastPktsRxOK) },
90 	{ "  mcast bytes rx",	offsetof(struct UPT1_RxStats, mcastBytesRxOK) },
91 	{ "  bcast pkts rx",	offsetof(struct UPT1_RxStats, bcastPktsRxOK) },
92 	{ "  bcast bytes rx",	offsetof(struct UPT1_RxStats, bcastBytesRxOK) },
93 	{ "  pkts rx OOB",	offsetof(struct UPT1_RxStats, pktsRxOutOfBuf) },
94 	{ "  pkts rx err",	offsetof(struct UPT1_RxStats, pktsRxError) },
95 };
96 
97 /* per rq stats maintained by the driver */
98 static const struct vmxnet3_stat_desc
99 vmxnet3_rq_driver_stats[] = {
100 	/* description,         offset */
101 	{ "  drv dropped rx total", offsetof(struct vmxnet3_rq_driver_stats,
102 					     drop_total) },
103 	{ "     err",		offsetof(struct vmxnet3_rq_driver_stats,
104 					 drop_err) },
105 	{ "     fcs",		offsetof(struct vmxnet3_rq_driver_stats,
106 					 drop_fcs) },
107 	{ "  rx buf alloc fail", offsetof(struct vmxnet3_rq_driver_stats,
108 					  rx_buf_alloc_failure) },
109 };
110 
111 /* global stats maintained by the driver */
112 static const struct vmxnet3_stat_desc
113 vmxnet3_global_stats[] = {
114 	/* description,         offset */
115 	{ "tx timeout count",	offsetof(struct vmxnet3_adapter,
116 					 tx_timeout_count) }
117 };
118 
119 
120 void
vmxnet3_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)121 vmxnet3_get_stats64(struct net_device *netdev,
122 		   struct rtnl_link_stats64 *stats)
123 {
124 	struct vmxnet3_adapter *adapter;
125 	struct vmxnet3_tq_driver_stats *drvTxStats;
126 	struct vmxnet3_rq_driver_stats *drvRxStats;
127 	struct UPT1_TxStats *devTxStats;
128 	struct UPT1_RxStats *devRxStats;
129 	unsigned long flags;
130 	int i;
131 
132 	adapter = netdev_priv(netdev);
133 
134 	/* Collect the dev stats into the shared area */
135 	spin_lock_irqsave(&adapter->cmd_lock, flags);
136 	VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_STATS);
137 	spin_unlock_irqrestore(&adapter->cmd_lock, flags);
138 
139 	for (i = 0; i < adapter->num_tx_queues; i++) {
140 		devTxStats = &adapter->tqd_start[i].stats;
141 		drvTxStats = &adapter->tx_queue[i].stats;
142 		stats->tx_packets += devTxStats->ucastPktsTxOK +
143 				     devTxStats->mcastPktsTxOK +
144 				     devTxStats->bcastPktsTxOK;
145 		stats->tx_bytes += devTxStats->ucastBytesTxOK +
146 				   devTxStats->mcastBytesTxOK +
147 				   devTxStats->bcastBytesTxOK;
148 		stats->tx_errors += devTxStats->pktsTxError;
149 		stats->tx_dropped += drvTxStats->drop_total;
150 	}
151 
152 	for (i = 0; i < adapter->num_rx_queues; i++) {
153 		devRxStats = &adapter->rqd_start[i].stats;
154 		drvRxStats = &adapter->rx_queue[i].stats;
155 		stats->rx_packets += devRxStats->ucastPktsRxOK +
156 				     devRxStats->mcastPktsRxOK +
157 				     devRxStats->bcastPktsRxOK;
158 
159 		stats->rx_bytes += devRxStats->ucastBytesRxOK +
160 				   devRxStats->mcastBytesRxOK +
161 				   devRxStats->bcastBytesRxOK;
162 
163 		stats->rx_errors += devRxStats->pktsRxError;
164 		stats->rx_dropped += drvRxStats->drop_total;
165 		stats->multicast +=  devRxStats->mcastPktsRxOK;
166 	}
167 }
168 
169 static int
vmxnet3_get_sset_count(struct net_device * netdev,int sset)170 vmxnet3_get_sset_count(struct net_device *netdev, int sset)
171 {
172 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
173 	switch (sset) {
174 	case ETH_SS_STATS:
175 		return (ARRAY_SIZE(vmxnet3_tq_dev_stats) +
176 			ARRAY_SIZE(vmxnet3_tq_driver_stats)) *
177 		       adapter->num_tx_queues +
178 		       (ARRAY_SIZE(vmxnet3_rq_dev_stats) +
179 			ARRAY_SIZE(vmxnet3_rq_driver_stats)) *
180 		       adapter->num_rx_queues +
181 			ARRAY_SIZE(vmxnet3_global_stats);
182 	default:
183 		return -EOPNOTSUPP;
184 	}
185 }
186 
187 
188 /* This is a version 2 of the vmxnet3 ethtool_regs which goes hand in hand with
189  * the version 2 of the vmxnet3 support for ethtool(8) --register-dump.
190  * Therefore, if any registers are added, removed or modified, then a version
191  * bump and a corresponding change in the vmxnet3 support for ethtool(8)
192  * --register-dump would be required.
193  */
194 static int
vmxnet3_get_regs_len(struct net_device * netdev)195 vmxnet3_get_regs_len(struct net_device *netdev)
196 {
197 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
198 
199 	return ((9 /* BAR1 registers */ +
200 		(1 + adapter->intr.num_intrs) +
201 		(1 + adapter->num_tx_queues * 17 /* Tx queue registers */) +
202 		(1 + adapter->num_rx_queues * 23 /* Rx queue registers */)) *
203 		sizeof(u32));
204 }
205 
206 
207 static void
vmxnet3_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * drvinfo)208 vmxnet3_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
209 {
210 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
211 
212 	strlcpy(drvinfo->driver, vmxnet3_driver_name, sizeof(drvinfo->driver));
213 
214 	strlcpy(drvinfo->version, VMXNET3_DRIVER_VERSION_REPORT,
215 		sizeof(drvinfo->version));
216 
217 	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
218 		sizeof(drvinfo->bus_info));
219 }
220 
221 
222 static void
vmxnet3_get_strings(struct net_device * netdev,u32 stringset,u8 * buf)223 vmxnet3_get_strings(struct net_device *netdev, u32 stringset, u8 *buf)
224 {
225 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
226 	int i, j;
227 
228 	if (stringset != ETH_SS_STATS)
229 		return;
230 
231 	for (j = 0; j < adapter->num_tx_queues; j++) {
232 		for (i = 0; i < ARRAY_SIZE(vmxnet3_tq_dev_stats); i++)
233 			ethtool_sprintf(&buf, vmxnet3_tq_dev_stats[i].desc);
234 		for (i = 0; i < ARRAY_SIZE(vmxnet3_tq_driver_stats); i++)
235 			ethtool_sprintf(&buf, vmxnet3_tq_driver_stats[i].desc);
236 	}
237 
238 	for (j = 0; j < adapter->num_rx_queues; j++) {
239 		for (i = 0; i < ARRAY_SIZE(vmxnet3_rq_dev_stats); i++)
240 			ethtool_sprintf(&buf, vmxnet3_rq_dev_stats[i].desc);
241 		for (i = 0; i < ARRAY_SIZE(vmxnet3_rq_driver_stats); i++)
242 			ethtool_sprintf(&buf, vmxnet3_rq_driver_stats[i].desc);
243 	}
244 
245 	for (i = 0; i < ARRAY_SIZE(vmxnet3_global_stats); i++)
246 		ethtool_sprintf(&buf, vmxnet3_global_stats[i].desc);
247 }
248 
vmxnet3_fix_features(struct net_device * netdev,netdev_features_t features)249 netdev_features_t vmxnet3_fix_features(struct net_device *netdev,
250 				       netdev_features_t features)
251 {
252 	/* If Rx checksum is disabled, then LRO should also be disabled */
253 	if (!(features & NETIF_F_RXCSUM))
254 		features &= ~NETIF_F_LRO;
255 
256 	return features;
257 }
258 
vmxnet3_features_check(struct sk_buff * skb,struct net_device * netdev,netdev_features_t features)259 netdev_features_t vmxnet3_features_check(struct sk_buff *skb,
260 					 struct net_device *netdev,
261 					 netdev_features_t features)
262 {
263 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
264 
265 	/* Validate if the tunneled packet is being offloaded by the device */
266 	if (VMXNET3_VERSION_GE_4(adapter) &&
267 	    skb->encapsulation && skb->ip_summed == CHECKSUM_PARTIAL) {
268 		u8 l4_proto = 0;
269 		u16 port;
270 		struct udphdr *udph;
271 
272 		switch (vlan_get_protocol(skb)) {
273 		case htons(ETH_P_IP):
274 			l4_proto = ip_hdr(skb)->protocol;
275 			break;
276 		case htons(ETH_P_IPV6):
277 			l4_proto = ipv6_hdr(skb)->nexthdr;
278 			break;
279 		default:
280 			return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
281 		}
282 
283 		switch (l4_proto) {
284 		case IPPROTO_UDP:
285 			udph = udp_hdr(skb);
286 			port = be16_to_cpu(udph->dest);
287 			/* Check if offloaded port is supported */
288 			if (port != GENEVE_UDP_PORT &&
289 			    port != IANA_VXLAN_UDP_PORT &&
290 			    port != VXLAN_UDP_PORT) {
291 				return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
292 			}
293 			break;
294 		default:
295 			return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
296 		}
297 	}
298 	return features;
299 }
300 
vmxnet3_enable_encap_offloads(struct net_device * netdev)301 static void vmxnet3_enable_encap_offloads(struct net_device *netdev)
302 {
303 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
304 
305 	if (VMXNET3_VERSION_GE_4(adapter)) {
306 		netdev->hw_enc_features |= NETIF_F_SG | NETIF_F_RXCSUM |
307 			NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_CTAG_TX |
308 			NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_TSO | NETIF_F_TSO6 |
309 			NETIF_F_LRO | NETIF_F_GSO_UDP_TUNNEL |
310 			NETIF_F_GSO_UDP_TUNNEL_CSUM;
311 	}
312 }
313 
vmxnet3_disable_encap_offloads(struct net_device * netdev)314 static void vmxnet3_disable_encap_offloads(struct net_device *netdev)
315 {
316 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
317 
318 	if (VMXNET3_VERSION_GE_4(adapter)) {
319 		netdev->hw_enc_features &= ~(NETIF_F_SG | NETIF_F_RXCSUM |
320 			NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_CTAG_TX |
321 			NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_TSO | NETIF_F_TSO6 |
322 			NETIF_F_LRO | NETIF_F_GSO_UDP_TUNNEL |
323 			NETIF_F_GSO_UDP_TUNNEL_CSUM);
324 	}
325 }
326 
vmxnet3_set_features(struct net_device * netdev,netdev_features_t features)327 int vmxnet3_set_features(struct net_device *netdev, netdev_features_t features)
328 {
329 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
330 	unsigned long flags;
331 	netdev_features_t changed = features ^ netdev->features;
332 	netdev_features_t tun_offload_mask = NETIF_F_GSO_UDP_TUNNEL |
333 					     NETIF_F_GSO_UDP_TUNNEL_CSUM;
334 	u8 udp_tun_enabled = (netdev->features & tun_offload_mask) != 0;
335 
336 	if (changed & (NETIF_F_RXCSUM | NETIF_F_LRO |
337 		       NETIF_F_HW_VLAN_CTAG_RX | tun_offload_mask)) {
338 		if (features & NETIF_F_RXCSUM)
339 			adapter->shared->devRead.misc.uptFeatures |=
340 			UPT1_F_RXCSUM;
341 		else
342 			adapter->shared->devRead.misc.uptFeatures &=
343 			~UPT1_F_RXCSUM;
344 
345 		/* update hardware LRO capability accordingly */
346 		if (features & NETIF_F_LRO)
347 			adapter->shared->devRead.misc.uptFeatures |=
348 							UPT1_F_LRO;
349 		else
350 			adapter->shared->devRead.misc.uptFeatures &=
351 							~UPT1_F_LRO;
352 
353 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
354 			adapter->shared->devRead.misc.uptFeatures |=
355 			UPT1_F_RXVLAN;
356 		else
357 			adapter->shared->devRead.misc.uptFeatures &=
358 			~UPT1_F_RXVLAN;
359 
360 		if ((features & tun_offload_mask) != 0 && !udp_tun_enabled) {
361 			vmxnet3_enable_encap_offloads(netdev);
362 			adapter->shared->devRead.misc.uptFeatures |=
363 			UPT1_F_RXINNEROFLD;
364 		} else if ((features & tun_offload_mask) == 0 &&
365 			   udp_tun_enabled) {
366 			vmxnet3_disable_encap_offloads(netdev);
367 			adapter->shared->devRead.misc.uptFeatures &=
368 			~UPT1_F_RXINNEROFLD;
369 		}
370 
371 		spin_lock_irqsave(&adapter->cmd_lock, flags);
372 		VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
373 				       VMXNET3_CMD_UPDATE_FEATURE);
374 		spin_unlock_irqrestore(&adapter->cmd_lock, flags);
375 	}
376 	return 0;
377 }
378 
379 static void
vmxnet3_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * buf)380 vmxnet3_get_ethtool_stats(struct net_device *netdev,
381 			  struct ethtool_stats *stats, u64  *buf)
382 {
383 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
384 	unsigned long flags;
385 	u8 *base;
386 	int i;
387 	int j = 0;
388 
389 	spin_lock_irqsave(&adapter->cmd_lock, flags);
390 	VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_STATS);
391 	spin_unlock_irqrestore(&adapter->cmd_lock, flags);
392 
393 	/* this does assume each counter is 64-bit wide */
394 	for (j = 0; j < adapter->num_tx_queues; j++) {
395 		base = (u8 *)&adapter->tqd_start[j].stats;
396 		*buf++ = (u64)j;
397 		for (i = 1; i < ARRAY_SIZE(vmxnet3_tq_dev_stats); i++)
398 			*buf++ = *(u64 *)(base +
399 					  vmxnet3_tq_dev_stats[i].offset);
400 
401 		base = (u8 *)&adapter->tx_queue[j].stats;
402 		for (i = 0; i < ARRAY_SIZE(vmxnet3_tq_driver_stats); i++)
403 			*buf++ = *(u64 *)(base +
404 					  vmxnet3_tq_driver_stats[i].offset);
405 	}
406 
407 	for (j = 0; j < adapter->num_rx_queues; j++) {
408 		base = (u8 *)&adapter->rqd_start[j].stats;
409 		*buf++ = (u64) j;
410 		for (i = 1; i < ARRAY_SIZE(vmxnet3_rq_dev_stats); i++)
411 			*buf++ = *(u64 *)(base +
412 					  vmxnet3_rq_dev_stats[i].offset);
413 
414 		base = (u8 *)&adapter->rx_queue[j].stats;
415 		for (i = 0; i < ARRAY_SIZE(vmxnet3_rq_driver_stats); i++)
416 			*buf++ = *(u64 *)(base +
417 					  vmxnet3_rq_driver_stats[i].offset);
418 	}
419 
420 	base = (u8 *)adapter;
421 	for (i = 0; i < ARRAY_SIZE(vmxnet3_global_stats); i++)
422 		*buf++ = *(u64 *)(base + vmxnet3_global_stats[i].offset);
423 }
424 
425 
426 /* This is a version 2 of the vmxnet3 ethtool_regs which goes hand in hand with
427  * the version 2 of the vmxnet3 support for ethtool(8) --register-dump.
428  * Therefore, if any registers are added, removed or modified, then a version
429  * bump and a corresponding change in the vmxnet3 support for ethtool(8)
430  * --register-dump would be required.
431  */
432 static void
vmxnet3_get_regs(struct net_device * netdev,struct ethtool_regs * regs,void * p)433 vmxnet3_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
434 {
435 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
436 	u32 *buf = p;
437 	int i = 0, j = 0;
438 
439 	memset(p, 0, vmxnet3_get_regs_len(netdev));
440 
441 	regs->version = 2;
442 
443 	/* Update vmxnet3_get_regs_len if we want to dump more registers */
444 
445 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
446 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
447 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_DSAL);
448 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_DSAH);
449 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
450 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
451 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
452 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
453 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ECR);
454 
455 	buf[j++] = adapter->intr.num_intrs;
456 	for (i = 0; i < adapter->intr.num_intrs; i++) {
457 		buf[j++] = VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_IMR
458 						 + i * VMXNET3_REG_ALIGN);
459 	}
460 
461 	buf[j++] = adapter->num_tx_queues;
462 	for (i = 0; i < adapter->num_tx_queues; i++) {
463 		struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
464 
465 		buf[j++] = VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_TXPROD +
466 						 i * VMXNET3_REG_ALIGN);
467 
468 		buf[j++] = VMXNET3_GET_ADDR_LO(tq->tx_ring.basePA);
469 		buf[j++] = VMXNET3_GET_ADDR_HI(tq->tx_ring.basePA);
470 		buf[j++] = tq->tx_ring.size;
471 		buf[j++] = tq->tx_ring.next2fill;
472 		buf[j++] = tq->tx_ring.next2comp;
473 		buf[j++] = tq->tx_ring.gen;
474 
475 		buf[j++] = VMXNET3_GET_ADDR_LO(tq->data_ring.basePA);
476 		buf[j++] = VMXNET3_GET_ADDR_HI(tq->data_ring.basePA);
477 		buf[j++] = tq->data_ring.size;
478 		buf[j++] = tq->txdata_desc_size;
479 
480 		buf[j++] = VMXNET3_GET_ADDR_LO(tq->comp_ring.basePA);
481 		buf[j++] = VMXNET3_GET_ADDR_HI(tq->comp_ring.basePA);
482 		buf[j++] = tq->comp_ring.size;
483 		buf[j++] = tq->comp_ring.next2proc;
484 		buf[j++] = tq->comp_ring.gen;
485 
486 		buf[j++] = tq->stopped;
487 	}
488 
489 	buf[j++] = adapter->num_rx_queues;
490 	for (i = 0; i < adapter->num_rx_queues; i++) {
491 		struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
492 
493 		buf[j++] =  VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_RXPROD +
494 						  i * VMXNET3_REG_ALIGN);
495 		buf[j++] =  VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_RXPROD2 +
496 						  i * VMXNET3_REG_ALIGN);
497 
498 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->rx_ring[0].basePA);
499 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->rx_ring[0].basePA);
500 		buf[j++] = rq->rx_ring[0].size;
501 		buf[j++] = rq->rx_ring[0].next2fill;
502 		buf[j++] = rq->rx_ring[0].next2comp;
503 		buf[j++] = rq->rx_ring[0].gen;
504 
505 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->rx_ring[1].basePA);
506 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->rx_ring[1].basePA);
507 		buf[j++] = rq->rx_ring[1].size;
508 		buf[j++] = rq->rx_ring[1].next2fill;
509 		buf[j++] = rq->rx_ring[1].next2comp;
510 		buf[j++] = rq->rx_ring[1].gen;
511 
512 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->data_ring.basePA);
513 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->data_ring.basePA);
514 		buf[j++] = rq->rx_ring[0].size;
515 		buf[j++] = rq->data_ring.desc_size;
516 
517 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->comp_ring.basePA);
518 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->comp_ring.basePA);
519 		buf[j++] = rq->comp_ring.size;
520 		buf[j++] = rq->comp_ring.next2proc;
521 		buf[j++] = rq->comp_ring.gen;
522 	}
523 }
524 
525 
526 static void
vmxnet3_get_wol(struct net_device * netdev,struct ethtool_wolinfo * wol)527 vmxnet3_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
528 {
529 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
530 
531 	wol->supported = WAKE_UCAST | WAKE_ARP | WAKE_MAGIC;
532 	wol->wolopts = adapter->wol;
533 }
534 
535 
536 static int
vmxnet3_set_wol(struct net_device * netdev,struct ethtool_wolinfo * wol)537 vmxnet3_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
538 {
539 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
540 
541 	if (wol->wolopts & (WAKE_PHY | WAKE_MCAST | WAKE_BCAST |
542 			    WAKE_MAGICSECURE)) {
543 		return -EOPNOTSUPP;
544 	}
545 
546 	adapter->wol = wol->wolopts;
547 
548 	device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
549 
550 	return 0;
551 }
552 
553 
554 static int
vmxnet3_get_link_ksettings(struct net_device * netdev,struct ethtool_link_ksettings * ecmd)555 vmxnet3_get_link_ksettings(struct net_device *netdev,
556 			   struct ethtool_link_ksettings *ecmd)
557 {
558 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
559 
560 	ethtool_link_ksettings_zero_link_mode(ecmd, supported);
561 	ethtool_link_ksettings_add_link_mode(ecmd, supported, 10000baseT_Full);
562 	ethtool_link_ksettings_add_link_mode(ecmd, supported, 1000baseT_Full);
563 	ethtool_link_ksettings_add_link_mode(ecmd, supported, TP);
564 	ethtool_link_ksettings_zero_link_mode(ecmd, advertising);
565 	ethtool_link_ksettings_add_link_mode(ecmd, advertising, TP);
566 	ecmd->base.port = PORT_TP;
567 
568 	if (adapter->link_speed) {
569 		ecmd->base.speed = adapter->link_speed;
570 		ecmd->base.duplex = DUPLEX_FULL;
571 	} else {
572 		ecmd->base.speed = SPEED_UNKNOWN;
573 		ecmd->base.duplex = DUPLEX_UNKNOWN;
574 	}
575 	return 0;
576 }
577 
578 static void
vmxnet3_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * param,struct kernel_ethtool_ringparam * kernel_param,struct netlink_ext_ack * extack)579 vmxnet3_get_ringparam(struct net_device *netdev,
580 		      struct ethtool_ringparam *param,
581 		      struct kernel_ethtool_ringparam *kernel_param,
582 		      struct netlink_ext_ack *extack)
583 {
584 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
585 
586 	param->rx_max_pending = VMXNET3_RX_RING_MAX_SIZE;
587 	param->tx_max_pending = VMXNET3_TX_RING_MAX_SIZE;
588 	param->rx_mini_max_pending = VMXNET3_VERSION_GE_3(adapter) ?
589 		VMXNET3_RXDATA_DESC_MAX_SIZE : 0;
590 	param->rx_jumbo_max_pending = VMXNET3_RX_RING2_MAX_SIZE;
591 
592 	param->rx_pending = adapter->rx_ring_size;
593 	param->tx_pending = adapter->tx_ring_size;
594 	param->rx_mini_pending = VMXNET3_VERSION_GE_3(adapter) ?
595 		adapter->rxdata_desc_size : 0;
596 	param->rx_jumbo_pending = adapter->rx_ring2_size;
597 }
598 
599 static int
vmxnet3_set_ringparam(struct net_device * netdev,struct ethtool_ringparam * param,struct kernel_ethtool_ringparam * kernel_param,struct netlink_ext_ack * extack)600 vmxnet3_set_ringparam(struct net_device *netdev,
601 		      struct ethtool_ringparam *param,
602 		      struct kernel_ethtool_ringparam *kernel_param,
603 		      struct netlink_ext_ack *extack)
604 {
605 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
606 	u32 new_tx_ring_size, new_rx_ring_size, new_rx_ring2_size;
607 	u16 new_rxdata_desc_size;
608 	u32 sz;
609 	int err = 0;
610 
611 	if (param->tx_pending == 0 || param->tx_pending >
612 						VMXNET3_TX_RING_MAX_SIZE)
613 		return -EINVAL;
614 
615 	if (param->rx_pending == 0 || param->rx_pending >
616 						VMXNET3_RX_RING_MAX_SIZE)
617 		return -EINVAL;
618 
619 	if (param->rx_jumbo_pending == 0 ||
620 	    param->rx_jumbo_pending > VMXNET3_RX_RING2_MAX_SIZE)
621 		return -EINVAL;
622 
623 	/* if adapter not yet initialized, do nothing */
624 	if (adapter->rx_buf_per_pkt == 0) {
625 		netdev_err(netdev, "adapter not completely initialized, "
626 			   "ring size cannot be changed yet\n");
627 		return -EOPNOTSUPP;
628 	}
629 
630 	if (VMXNET3_VERSION_GE_3(adapter)) {
631 		if (param->rx_mini_pending > VMXNET3_RXDATA_DESC_MAX_SIZE)
632 			return -EINVAL;
633 	} else if (param->rx_mini_pending != 0) {
634 		return -EINVAL;
635 	}
636 
637 	/* round it up to a multiple of VMXNET3_RING_SIZE_ALIGN */
638 	new_tx_ring_size = (param->tx_pending + VMXNET3_RING_SIZE_MASK) &
639 							~VMXNET3_RING_SIZE_MASK;
640 	new_tx_ring_size = min_t(u32, new_tx_ring_size,
641 				 VMXNET3_TX_RING_MAX_SIZE);
642 	if (new_tx_ring_size > VMXNET3_TX_RING_MAX_SIZE || (new_tx_ring_size %
643 						VMXNET3_RING_SIZE_ALIGN) != 0)
644 		return -EINVAL;
645 
646 	/* ring0 has to be a multiple of
647 	 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
648 	 */
649 	sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
650 	new_rx_ring_size = (param->rx_pending + sz - 1) / sz * sz;
651 	new_rx_ring_size = min_t(u32, new_rx_ring_size,
652 				 VMXNET3_RX_RING_MAX_SIZE / sz * sz);
653 	if (new_rx_ring_size > VMXNET3_RX_RING_MAX_SIZE || (new_rx_ring_size %
654 							   sz) != 0)
655 		return -EINVAL;
656 
657 	/* ring2 has to be a multiple of VMXNET3_RING_SIZE_ALIGN */
658 	new_rx_ring2_size = (param->rx_jumbo_pending + VMXNET3_RING_SIZE_MASK) &
659 				~VMXNET3_RING_SIZE_MASK;
660 	new_rx_ring2_size = min_t(u32, new_rx_ring2_size,
661 				  VMXNET3_RX_RING2_MAX_SIZE);
662 
663 	/* rx data ring buffer size has to be a multiple of
664 	 * VMXNET3_RXDATA_DESC_SIZE_ALIGN
665 	 */
666 	new_rxdata_desc_size =
667 		(param->rx_mini_pending + VMXNET3_RXDATA_DESC_SIZE_MASK) &
668 		~VMXNET3_RXDATA_DESC_SIZE_MASK;
669 	new_rxdata_desc_size = min_t(u16, new_rxdata_desc_size,
670 				     VMXNET3_RXDATA_DESC_MAX_SIZE);
671 
672 	if (new_tx_ring_size == adapter->tx_ring_size &&
673 	    new_rx_ring_size == adapter->rx_ring_size &&
674 	    new_rx_ring2_size == adapter->rx_ring2_size &&
675 	    new_rxdata_desc_size == adapter->rxdata_desc_size) {
676 		return 0;
677 	}
678 
679 	/*
680 	 * Reset_work may be in the middle of resetting the device, wait for its
681 	 * completion.
682 	 */
683 	while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
684 		usleep_range(1000, 2000);
685 
686 	if (netif_running(netdev)) {
687 		vmxnet3_quiesce_dev(adapter);
688 		vmxnet3_reset_dev(adapter);
689 
690 		/* recreate the rx queue and the tx queue based on the
691 		 * new sizes */
692 		vmxnet3_tq_destroy_all(adapter);
693 		vmxnet3_rq_destroy_all(adapter);
694 
695 		err = vmxnet3_create_queues(adapter, new_tx_ring_size,
696 					    new_rx_ring_size, new_rx_ring2_size,
697 					    adapter->txdata_desc_size,
698 					    new_rxdata_desc_size);
699 		if (err) {
700 			/* failed, most likely because of OOM, try default
701 			 * size */
702 			netdev_err(netdev, "failed to apply new sizes, "
703 				   "try the default ones\n");
704 			new_rx_ring_size = VMXNET3_DEF_RX_RING_SIZE;
705 			new_rx_ring2_size = VMXNET3_DEF_RX_RING2_SIZE;
706 			new_tx_ring_size = VMXNET3_DEF_TX_RING_SIZE;
707 			new_rxdata_desc_size = VMXNET3_VERSION_GE_3(adapter) ?
708 				VMXNET3_DEF_RXDATA_DESC_SIZE : 0;
709 
710 			err = vmxnet3_create_queues(adapter,
711 						    new_tx_ring_size,
712 						    new_rx_ring_size,
713 						    new_rx_ring2_size,
714 						    adapter->txdata_desc_size,
715 						    new_rxdata_desc_size);
716 			if (err) {
717 				netdev_err(netdev, "failed to create queues "
718 					   "with default sizes. Closing it\n");
719 				goto out;
720 			}
721 		}
722 
723 		err = vmxnet3_activate_dev(adapter);
724 		if (err)
725 			netdev_err(netdev, "failed to re-activate, error %d."
726 				   " Closing it\n", err);
727 	}
728 	adapter->tx_ring_size = new_tx_ring_size;
729 	adapter->rx_ring_size = new_rx_ring_size;
730 	adapter->rx_ring2_size = new_rx_ring2_size;
731 	adapter->rxdata_desc_size = new_rxdata_desc_size;
732 
733 out:
734 	clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
735 	if (err)
736 		vmxnet3_force_close(adapter);
737 
738 	return err;
739 }
740 
741 static int
vmxnet3_get_rss_hash_opts(struct vmxnet3_adapter * adapter,struct ethtool_rxnfc * info)742 vmxnet3_get_rss_hash_opts(struct vmxnet3_adapter *adapter,
743 			  struct ethtool_rxnfc *info)
744 {
745 	enum Vmxnet3_RSSField rss_fields;
746 
747 	if (netif_running(adapter->netdev)) {
748 		unsigned long flags;
749 
750 		spin_lock_irqsave(&adapter->cmd_lock, flags);
751 
752 		VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
753 				       VMXNET3_CMD_GET_RSS_FIELDS);
754 		rss_fields = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
755 		spin_unlock_irqrestore(&adapter->cmd_lock, flags);
756 	} else {
757 		rss_fields = adapter->rss_fields;
758 	}
759 
760 	info->data = 0;
761 
762 	/* Report default options for RSS on vmxnet3 */
763 	switch (info->flow_type) {
764 	case TCP_V4_FLOW:
765 	case TCP_V6_FLOW:
766 		info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3 |
767 			      RXH_IP_SRC | RXH_IP_DST;
768 		break;
769 	case UDP_V4_FLOW:
770 		if (rss_fields & VMXNET3_RSS_FIELDS_UDPIP4)
771 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
772 		info->data |= RXH_IP_SRC | RXH_IP_DST;
773 		break;
774 	case AH_ESP_V4_FLOW:
775 	case AH_V4_FLOW:
776 	case ESP_V4_FLOW:
777 		if (rss_fields & VMXNET3_RSS_FIELDS_ESPIP4)
778 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
779 		fallthrough;
780 	case SCTP_V4_FLOW:
781 	case IPV4_FLOW:
782 		info->data |= RXH_IP_SRC | RXH_IP_DST;
783 		break;
784 	case UDP_V6_FLOW:
785 		if (rss_fields & VMXNET3_RSS_FIELDS_UDPIP6)
786 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
787 		info->data |= RXH_IP_SRC | RXH_IP_DST;
788 		break;
789 	case AH_ESP_V6_FLOW:
790 	case AH_V6_FLOW:
791 	case ESP_V6_FLOW:
792 		if (VMXNET3_VERSION_GE_6(adapter) &&
793 		    (rss_fields & VMXNET3_RSS_FIELDS_ESPIP6))
794 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
795 		fallthrough;
796 	case SCTP_V6_FLOW:
797 	case IPV6_FLOW:
798 		info->data |= RXH_IP_SRC | RXH_IP_DST;
799 		break;
800 	default:
801 		return -EINVAL;
802 	}
803 
804 	return 0;
805 }
806 
807 static int
vmxnet3_set_rss_hash_opt(struct net_device * netdev,struct vmxnet3_adapter * adapter,struct ethtool_rxnfc * nfc)808 vmxnet3_set_rss_hash_opt(struct net_device *netdev,
809 			 struct vmxnet3_adapter *adapter,
810 			 struct ethtool_rxnfc *nfc)
811 {
812 	enum Vmxnet3_RSSField rss_fields = adapter->rss_fields;
813 
814 	/* RSS does not support anything other than hashing
815 	 * to queues on src and dst IPs and ports
816 	 */
817 	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
818 			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
819 		return -EINVAL;
820 
821 	switch (nfc->flow_type) {
822 	case TCP_V4_FLOW:
823 	case TCP_V6_FLOW:
824 		if (!(nfc->data & RXH_IP_SRC) ||
825 		    !(nfc->data & RXH_IP_DST) ||
826 		    !(nfc->data & RXH_L4_B_0_1) ||
827 		    !(nfc->data & RXH_L4_B_2_3))
828 			return -EINVAL;
829 		break;
830 	case UDP_V4_FLOW:
831 		if (!(nfc->data & RXH_IP_SRC) ||
832 		    !(nfc->data & RXH_IP_DST))
833 			return -EINVAL;
834 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
835 		case 0:
836 			rss_fields &= ~VMXNET3_RSS_FIELDS_UDPIP4;
837 			break;
838 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
839 			rss_fields |= VMXNET3_RSS_FIELDS_UDPIP4;
840 			break;
841 		default:
842 			return -EINVAL;
843 		}
844 		break;
845 	case UDP_V6_FLOW:
846 		if (!(nfc->data & RXH_IP_SRC) ||
847 		    !(nfc->data & RXH_IP_DST))
848 			return -EINVAL;
849 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
850 		case 0:
851 			rss_fields &= ~VMXNET3_RSS_FIELDS_UDPIP6;
852 			break;
853 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
854 			rss_fields |= VMXNET3_RSS_FIELDS_UDPIP6;
855 			break;
856 		default:
857 			return -EINVAL;
858 		}
859 		break;
860 	case ESP_V4_FLOW:
861 	case AH_V4_FLOW:
862 	case AH_ESP_V4_FLOW:
863 		if (!(nfc->data & RXH_IP_SRC) ||
864 		    !(nfc->data & RXH_IP_DST))
865 			return -EINVAL;
866 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
867 		case 0:
868 			rss_fields &= ~VMXNET3_RSS_FIELDS_ESPIP4;
869 			break;
870 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
871 			rss_fields |= VMXNET3_RSS_FIELDS_ESPIP4;
872 		break;
873 		default:
874 			return -EINVAL;
875 		}
876 		break;
877 	case ESP_V6_FLOW:
878 	case AH_V6_FLOW:
879 	case AH_ESP_V6_FLOW:
880 		if (!VMXNET3_VERSION_GE_6(adapter))
881 			return -EOPNOTSUPP;
882 		if (!(nfc->data & RXH_IP_SRC) ||
883 		    !(nfc->data & RXH_IP_DST))
884 			return -EINVAL;
885 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
886 		case 0:
887 			rss_fields &= ~VMXNET3_RSS_FIELDS_ESPIP6;
888 			break;
889 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
890 			rss_fields |= VMXNET3_RSS_FIELDS_ESPIP6;
891 			break;
892 		default:
893 			return -EINVAL;
894 		}
895 		break;
896 	case SCTP_V4_FLOW:
897 	case SCTP_V6_FLOW:
898 		if (!(nfc->data & RXH_IP_SRC) ||
899 		    !(nfc->data & RXH_IP_DST) ||
900 		    (nfc->data & RXH_L4_B_0_1) ||
901 		    (nfc->data & RXH_L4_B_2_3))
902 			return -EINVAL;
903 		break;
904 	default:
905 		return -EINVAL;
906 	}
907 
908 	/* if we changed something we need to update flags */
909 	if (rss_fields != adapter->rss_fields) {
910 		adapter->default_rss_fields = false;
911 		if (netif_running(netdev)) {
912 			struct Vmxnet3_DriverShared *shared = adapter->shared;
913 			union Vmxnet3_CmdInfo *cmdInfo = &shared->cu.cmdInfo;
914 			unsigned long flags;
915 
916 			spin_lock_irqsave(&adapter->cmd_lock, flags);
917 			cmdInfo->setRssFields = rss_fields;
918 			VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
919 					       VMXNET3_CMD_SET_RSS_FIELDS);
920 
921 			/* Not all requested RSS may get applied, so get and
922 			 * cache what was actually applied.
923 			 */
924 			VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
925 					       VMXNET3_CMD_GET_RSS_FIELDS);
926 			adapter->rss_fields =
927 				VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
928 			spin_unlock_irqrestore(&adapter->cmd_lock, flags);
929 		} else {
930 			/* When the device is activated, we will try to apply
931 			 * these rules and cache the applied value later.
932 			 */
933 			adapter->rss_fields = rss_fields;
934 		}
935 	}
936 	return 0;
937 }
938 
939 static int
vmxnet3_get_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * info,u32 * rules)940 vmxnet3_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
941 		  u32 *rules)
942 {
943 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
944 	int err = 0;
945 
946 	switch (info->cmd) {
947 	case ETHTOOL_GRXRINGS:
948 		info->data = adapter->num_rx_queues;
949 		break;
950 	case ETHTOOL_GRXFH:
951 		if (!VMXNET3_VERSION_GE_4(adapter)) {
952 			err = -EOPNOTSUPP;
953 			break;
954 		}
955 #ifdef VMXNET3_RSS
956 		if (!adapter->rss) {
957 			err = -EOPNOTSUPP;
958 			break;
959 		}
960 #endif
961 		err = vmxnet3_get_rss_hash_opts(adapter, info);
962 		break;
963 	default:
964 		err = -EOPNOTSUPP;
965 		break;
966 	}
967 
968 	return err;
969 }
970 
971 static int
vmxnet3_set_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * info)972 vmxnet3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
973 {
974 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
975 	int err = 0;
976 
977 	if (!VMXNET3_VERSION_GE_4(adapter)) {
978 		err = -EOPNOTSUPP;
979 		goto done;
980 	}
981 #ifdef VMXNET3_RSS
982 	if (!adapter->rss) {
983 		err = -EOPNOTSUPP;
984 		goto done;
985 	}
986 #endif
987 
988 	switch (info->cmd) {
989 	case ETHTOOL_SRXFH:
990 		err = vmxnet3_set_rss_hash_opt(netdev, adapter, info);
991 		break;
992 	default:
993 		err = -EOPNOTSUPP;
994 		break;
995 	}
996 
997 done:
998 	return err;
999 }
1000 
1001 #ifdef VMXNET3_RSS
1002 static u32
vmxnet3_get_rss_indir_size(struct net_device * netdev)1003 vmxnet3_get_rss_indir_size(struct net_device *netdev)
1004 {
1005 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1006 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
1007 
1008 	return rssConf->indTableSize;
1009 }
1010 
1011 static int
vmxnet3_get_rss(struct net_device * netdev,u32 * p,u8 * key,u8 * hfunc)1012 vmxnet3_get_rss(struct net_device *netdev, u32 *p, u8 *key, u8 *hfunc)
1013 {
1014 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1015 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
1016 	unsigned int n = rssConf->indTableSize;
1017 
1018 	if (hfunc)
1019 		*hfunc = ETH_RSS_HASH_TOP;
1020 	if (!p)
1021 		return 0;
1022 	if (n > UPT1_RSS_MAX_IND_TABLE_SIZE)
1023 		return 0;
1024 	while (n--)
1025 		p[n] = rssConf->indTable[n];
1026 	return 0;
1027 
1028 }
1029 
1030 static int
vmxnet3_set_rss(struct net_device * netdev,const u32 * p,const u8 * key,const u8 hfunc)1031 vmxnet3_set_rss(struct net_device *netdev, const u32 *p, const u8 *key,
1032 		const u8 hfunc)
1033 {
1034 	unsigned int i;
1035 	unsigned long flags;
1036 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1037 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
1038 
1039 	/* We do not allow change in unsupported parameters */
1040 	if (key ||
1041 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
1042 		return -EOPNOTSUPP;
1043 	if (!p)
1044 		return 0;
1045 	for (i = 0; i < rssConf->indTableSize; i++)
1046 		rssConf->indTable[i] = p[i];
1047 
1048 	spin_lock_irqsave(&adapter->cmd_lock, flags);
1049 	VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1050 			       VMXNET3_CMD_UPDATE_RSSIDT);
1051 	spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1052 
1053 	return 0;
1054 
1055 }
1056 #endif
1057 
vmxnet3_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)1058 static int vmxnet3_get_coalesce(struct net_device *netdev,
1059 				struct ethtool_coalesce *ec,
1060 				struct kernel_ethtool_coalesce *kernel_coal,
1061 				struct netlink_ext_ack *extack)
1062 {
1063 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1064 
1065 	if (!VMXNET3_VERSION_GE_3(adapter))
1066 		return -EOPNOTSUPP;
1067 
1068 	switch (adapter->coal_conf->coalMode) {
1069 	case VMXNET3_COALESCE_DISABLED:
1070 		/* struct ethtool_coalesce is already initialized to 0 */
1071 		break;
1072 	case VMXNET3_COALESCE_ADAPT:
1073 		ec->use_adaptive_rx_coalesce = true;
1074 		break;
1075 	case VMXNET3_COALESCE_STATIC:
1076 		ec->tx_max_coalesced_frames =
1077 			adapter->coal_conf->coalPara.coalStatic.tx_comp_depth;
1078 		ec->rx_max_coalesced_frames =
1079 			adapter->coal_conf->coalPara.coalStatic.rx_depth;
1080 		break;
1081 	case VMXNET3_COALESCE_RBC: {
1082 		u32 rbc_rate;
1083 
1084 		rbc_rate = adapter->coal_conf->coalPara.coalRbc.rbc_rate;
1085 		ec->rx_coalesce_usecs = VMXNET3_COAL_RBC_USECS(rbc_rate);
1086 	}
1087 		break;
1088 	default:
1089 		return -EOPNOTSUPP;
1090 	}
1091 
1092 	return 0;
1093 }
1094 
vmxnet3_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)1095 static int vmxnet3_set_coalesce(struct net_device *netdev,
1096 				struct ethtool_coalesce *ec,
1097 				struct kernel_ethtool_coalesce *kernel_coal,
1098 				struct netlink_ext_ack *extack)
1099 {
1100 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1101 	struct Vmxnet3_DriverShared *shared = adapter->shared;
1102 	union Vmxnet3_CmdInfo *cmdInfo = &shared->cu.cmdInfo;
1103 	unsigned long flags;
1104 
1105 	if (!VMXNET3_VERSION_GE_3(adapter))
1106 		return -EOPNOTSUPP;
1107 
1108 	if ((ec->rx_coalesce_usecs == 0) &&
1109 	    (ec->use_adaptive_rx_coalesce == 0) &&
1110 	    (ec->tx_max_coalesced_frames == 0) &&
1111 	    (ec->rx_max_coalesced_frames == 0)) {
1112 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
1113 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_DISABLED;
1114 		goto done;
1115 	}
1116 
1117 	if (ec->rx_coalesce_usecs != 0) {
1118 		u32 rbc_rate;
1119 
1120 		if ((ec->use_adaptive_rx_coalesce != 0) ||
1121 		    (ec->tx_max_coalesced_frames != 0) ||
1122 		    (ec->rx_max_coalesced_frames != 0)) {
1123 			return -EINVAL;
1124 		}
1125 
1126 		rbc_rate = VMXNET3_COAL_RBC_RATE(ec->rx_coalesce_usecs);
1127 		if (rbc_rate < VMXNET3_COAL_RBC_MIN_RATE ||
1128 		    rbc_rate > VMXNET3_COAL_RBC_MAX_RATE) {
1129 			return -EINVAL;
1130 		}
1131 
1132 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
1133 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_RBC;
1134 		adapter->coal_conf->coalPara.coalRbc.rbc_rate = rbc_rate;
1135 		goto done;
1136 	}
1137 
1138 	if (ec->use_adaptive_rx_coalesce != 0) {
1139 		if (ec->tx_max_coalesced_frames != 0 ||
1140 		    ec->rx_max_coalesced_frames != 0) {
1141 			return -EINVAL;
1142 		}
1143 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
1144 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_ADAPT;
1145 		goto done;
1146 	}
1147 
1148 	if ((ec->tx_max_coalesced_frames != 0) ||
1149 	    (ec->rx_max_coalesced_frames != 0)) {
1150 		if ((ec->tx_max_coalesced_frames >
1151 		    VMXNET3_COAL_STATIC_MAX_DEPTH) ||
1152 		    (ec->rx_max_coalesced_frames >
1153 		     VMXNET3_COAL_STATIC_MAX_DEPTH)) {
1154 			return -EINVAL;
1155 		}
1156 
1157 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
1158 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_STATIC;
1159 
1160 		adapter->coal_conf->coalPara.coalStatic.tx_comp_depth =
1161 			(ec->tx_max_coalesced_frames ?
1162 			 ec->tx_max_coalesced_frames :
1163 			 VMXNET3_COAL_STATIC_DEFAULT_DEPTH);
1164 
1165 		adapter->coal_conf->coalPara.coalStatic.rx_depth =
1166 			(ec->rx_max_coalesced_frames ?
1167 			 ec->rx_max_coalesced_frames :
1168 			 VMXNET3_COAL_STATIC_DEFAULT_DEPTH);
1169 
1170 		adapter->coal_conf->coalPara.coalStatic.tx_depth =
1171 			 VMXNET3_COAL_STATIC_DEFAULT_DEPTH;
1172 		goto done;
1173 	}
1174 
1175 done:
1176 	adapter->default_coal_mode = false;
1177 	if (netif_running(netdev)) {
1178 		spin_lock_irqsave(&adapter->cmd_lock, flags);
1179 		cmdInfo->varConf.confVer = 1;
1180 		cmdInfo->varConf.confLen =
1181 			cpu_to_le32(sizeof(*adapter->coal_conf));
1182 		cmdInfo->varConf.confPA  = cpu_to_le64(adapter->coal_conf_pa);
1183 		VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1184 				       VMXNET3_CMD_SET_COALESCE);
1185 		spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1186 	}
1187 
1188 	return 0;
1189 }
1190 
1191 static const struct ethtool_ops vmxnet3_ethtool_ops = {
1192 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
1193 				     ETHTOOL_COALESCE_MAX_FRAMES |
1194 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
1195 	.get_drvinfo       = vmxnet3_get_drvinfo,
1196 	.get_regs_len      = vmxnet3_get_regs_len,
1197 	.get_regs          = vmxnet3_get_regs,
1198 	.get_wol           = vmxnet3_get_wol,
1199 	.set_wol           = vmxnet3_set_wol,
1200 	.get_link          = ethtool_op_get_link,
1201 	.get_coalesce      = vmxnet3_get_coalesce,
1202 	.set_coalesce      = vmxnet3_set_coalesce,
1203 	.get_strings       = vmxnet3_get_strings,
1204 	.get_sset_count	   = vmxnet3_get_sset_count,
1205 	.get_ethtool_stats = vmxnet3_get_ethtool_stats,
1206 	.get_ringparam     = vmxnet3_get_ringparam,
1207 	.set_ringparam     = vmxnet3_set_ringparam,
1208 	.get_rxnfc         = vmxnet3_get_rxnfc,
1209 	.set_rxnfc         = vmxnet3_set_rxnfc,
1210 #ifdef VMXNET3_RSS
1211 	.get_rxfh_indir_size = vmxnet3_get_rss_indir_size,
1212 	.get_rxfh          = vmxnet3_get_rss,
1213 	.set_rxfh          = vmxnet3_set_rss,
1214 #endif
1215 	.get_link_ksettings = vmxnet3_get_link_ksettings,
1216 };
1217 
vmxnet3_set_ethtool_ops(struct net_device * netdev)1218 void vmxnet3_set_ethtool_ops(struct net_device *netdev)
1219 {
1220 	netdev->ethtool_ops = &vmxnet3_ethtool_ops;
1221 }
1222