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
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
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
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
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
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 
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 
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 
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 
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 
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
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
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
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
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
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 
579 static void
580 vmxnet3_get_ringparam(struct net_device *netdev,
581 		      struct ethtool_ringparam *param)
582 {
583 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
584 
585 	param->rx_max_pending = VMXNET3_RX_RING_MAX_SIZE;
586 	param->tx_max_pending = VMXNET3_TX_RING_MAX_SIZE;
587 	param->rx_mini_max_pending = VMXNET3_VERSION_GE_3(adapter) ?
588 		VMXNET3_RXDATA_DESC_MAX_SIZE : 0;
589 	param->rx_jumbo_max_pending = VMXNET3_RX_RING2_MAX_SIZE;
590 
591 	param->rx_pending = adapter->rx_ring_size;
592 	param->tx_pending = adapter->tx_ring_size;
593 	param->rx_mini_pending = VMXNET3_VERSION_GE_3(adapter) ?
594 		adapter->rxdata_desc_size : 0;
595 	param->rx_jumbo_pending = adapter->rx_ring2_size;
596 }
597 
598 
599 static int
600 vmxnet3_set_ringparam(struct net_device *netdev,
601 		      struct ethtool_ringparam *param)
602 {
603 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
604 	u32 new_tx_ring_size, new_rx_ring_size, new_rx_ring2_size;
605 	u16 new_rxdata_desc_size;
606 	u32 sz;
607 	int err = 0;
608 
609 	if (param->tx_pending == 0 || param->tx_pending >
610 						VMXNET3_TX_RING_MAX_SIZE)
611 		return -EINVAL;
612 
613 	if (param->rx_pending == 0 || param->rx_pending >
614 						VMXNET3_RX_RING_MAX_SIZE)
615 		return -EINVAL;
616 
617 	if (param->rx_jumbo_pending == 0 ||
618 	    param->rx_jumbo_pending > VMXNET3_RX_RING2_MAX_SIZE)
619 		return -EINVAL;
620 
621 	/* if adapter not yet initialized, do nothing */
622 	if (adapter->rx_buf_per_pkt == 0) {
623 		netdev_err(netdev, "adapter not completely initialized, "
624 			   "ring size cannot be changed yet\n");
625 		return -EOPNOTSUPP;
626 	}
627 
628 	if (VMXNET3_VERSION_GE_3(adapter)) {
629 		if (param->rx_mini_pending > VMXNET3_RXDATA_DESC_MAX_SIZE)
630 			return -EINVAL;
631 	} else if (param->rx_mini_pending != 0) {
632 		return -EINVAL;
633 	}
634 
635 	/* round it up to a multiple of VMXNET3_RING_SIZE_ALIGN */
636 	new_tx_ring_size = (param->tx_pending + VMXNET3_RING_SIZE_MASK) &
637 							~VMXNET3_RING_SIZE_MASK;
638 	new_tx_ring_size = min_t(u32, new_tx_ring_size,
639 				 VMXNET3_TX_RING_MAX_SIZE);
640 	if (new_tx_ring_size > VMXNET3_TX_RING_MAX_SIZE || (new_tx_ring_size %
641 						VMXNET3_RING_SIZE_ALIGN) != 0)
642 		return -EINVAL;
643 
644 	/* ring0 has to be a multiple of
645 	 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
646 	 */
647 	sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
648 	new_rx_ring_size = (param->rx_pending + sz - 1) / sz * sz;
649 	new_rx_ring_size = min_t(u32, new_rx_ring_size,
650 				 VMXNET3_RX_RING_MAX_SIZE / sz * sz);
651 	if (new_rx_ring_size > VMXNET3_RX_RING_MAX_SIZE || (new_rx_ring_size %
652 							   sz) != 0)
653 		return -EINVAL;
654 
655 	/* ring2 has to be a multiple of VMXNET3_RING_SIZE_ALIGN */
656 	new_rx_ring2_size = (param->rx_jumbo_pending + VMXNET3_RING_SIZE_MASK) &
657 				~VMXNET3_RING_SIZE_MASK;
658 	new_rx_ring2_size = min_t(u32, new_rx_ring2_size,
659 				  VMXNET3_RX_RING2_MAX_SIZE);
660 
661 	/* rx data ring buffer size has to be a multiple of
662 	 * VMXNET3_RXDATA_DESC_SIZE_ALIGN
663 	 */
664 	new_rxdata_desc_size =
665 		(param->rx_mini_pending + VMXNET3_RXDATA_DESC_SIZE_MASK) &
666 		~VMXNET3_RXDATA_DESC_SIZE_MASK;
667 	new_rxdata_desc_size = min_t(u16, new_rxdata_desc_size,
668 				     VMXNET3_RXDATA_DESC_MAX_SIZE);
669 
670 	if (new_tx_ring_size == adapter->tx_ring_size &&
671 	    new_rx_ring_size == adapter->rx_ring_size &&
672 	    new_rx_ring2_size == adapter->rx_ring2_size &&
673 	    new_rxdata_desc_size == adapter->rxdata_desc_size) {
674 		return 0;
675 	}
676 
677 	/*
678 	 * Reset_work may be in the middle of resetting the device, wait for its
679 	 * completion.
680 	 */
681 	while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
682 		usleep_range(1000, 2000);
683 
684 	if (netif_running(netdev)) {
685 		vmxnet3_quiesce_dev(adapter);
686 		vmxnet3_reset_dev(adapter);
687 
688 		/* recreate the rx queue and the tx queue based on the
689 		 * new sizes */
690 		vmxnet3_tq_destroy_all(adapter);
691 		vmxnet3_rq_destroy_all(adapter);
692 
693 		err = vmxnet3_create_queues(adapter, new_tx_ring_size,
694 					    new_rx_ring_size, new_rx_ring2_size,
695 					    adapter->txdata_desc_size,
696 					    new_rxdata_desc_size);
697 		if (err) {
698 			/* failed, most likely because of OOM, try default
699 			 * size */
700 			netdev_err(netdev, "failed to apply new sizes, "
701 				   "try the default ones\n");
702 			new_rx_ring_size = VMXNET3_DEF_RX_RING_SIZE;
703 			new_rx_ring2_size = VMXNET3_DEF_RX_RING2_SIZE;
704 			new_tx_ring_size = VMXNET3_DEF_TX_RING_SIZE;
705 			new_rxdata_desc_size = VMXNET3_VERSION_GE_3(adapter) ?
706 				VMXNET3_DEF_RXDATA_DESC_SIZE : 0;
707 
708 			err = vmxnet3_create_queues(adapter,
709 						    new_tx_ring_size,
710 						    new_rx_ring_size,
711 						    new_rx_ring2_size,
712 						    adapter->txdata_desc_size,
713 						    new_rxdata_desc_size);
714 			if (err) {
715 				netdev_err(netdev, "failed to create queues "
716 					   "with default sizes. Closing it\n");
717 				goto out;
718 			}
719 		}
720 
721 		err = vmxnet3_activate_dev(adapter);
722 		if (err)
723 			netdev_err(netdev, "failed to re-activate, error %d."
724 				   " Closing it\n", err);
725 	}
726 	adapter->tx_ring_size = new_tx_ring_size;
727 	adapter->rx_ring_size = new_rx_ring_size;
728 	adapter->rx_ring2_size = new_rx_ring2_size;
729 	adapter->rxdata_desc_size = new_rxdata_desc_size;
730 
731 out:
732 	clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
733 	if (err)
734 		vmxnet3_force_close(adapter);
735 
736 	return err;
737 }
738 
739 static int
740 vmxnet3_get_rss_hash_opts(struct vmxnet3_adapter *adapter,
741 			  struct ethtool_rxnfc *info)
742 {
743 	enum Vmxnet3_RSSField rss_fields;
744 
745 	if (netif_running(adapter->netdev)) {
746 		unsigned long flags;
747 
748 		spin_lock_irqsave(&adapter->cmd_lock, flags);
749 
750 		VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
751 				       VMXNET3_CMD_GET_RSS_FIELDS);
752 		rss_fields = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
753 		spin_unlock_irqrestore(&adapter->cmd_lock, flags);
754 	} else {
755 		rss_fields = adapter->rss_fields;
756 	}
757 
758 	info->data = 0;
759 
760 	/* Report default options for RSS on vmxnet3 */
761 	switch (info->flow_type) {
762 	case TCP_V4_FLOW:
763 	case TCP_V6_FLOW:
764 		info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3 |
765 			      RXH_IP_SRC | RXH_IP_DST;
766 		break;
767 	case UDP_V4_FLOW:
768 		if (rss_fields & VMXNET3_RSS_FIELDS_UDPIP4)
769 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
770 		info->data |= RXH_IP_SRC | RXH_IP_DST;
771 		break;
772 	case AH_ESP_V4_FLOW:
773 	case AH_V4_FLOW:
774 	case ESP_V4_FLOW:
775 		if (rss_fields & VMXNET3_RSS_FIELDS_ESPIP4)
776 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
777 		fallthrough;
778 	case SCTP_V4_FLOW:
779 	case IPV4_FLOW:
780 		info->data |= RXH_IP_SRC | RXH_IP_DST;
781 		break;
782 	case UDP_V6_FLOW:
783 		if (rss_fields & VMXNET3_RSS_FIELDS_UDPIP6)
784 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
785 		info->data |= RXH_IP_SRC | RXH_IP_DST;
786 		break;
787 	case AH_ESP_V6_FLOW:
788 	case AH_V6_FLOW:
789 	case ESP_V6_FLOW:
790 		if (VMXNET3_VERSION_GE_6(adapter) &&
791 		    (rss_fields & VMXNET3_RSS_FIELDS_ESPIP6))
792 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
793 		fallthrough;
794 	case SCTP_V6_FLOW:
795 	case IPV6_FLOW:
796 		info->data |= RXH_IP_SRC | RXH_IP_DST;
797 		break;
798 	default:
799 		return -EINVAL;
800 	}
801 
802 	return 0;
803 }
804 
805 static int
806 vmxnet3_set_rss_hash_opt(struct net_device *netdev,
807 			 struct vmxnet3_adapter *adapter,
808 			 struct ethtool_rxnfc *nfc)
809 {
810 	enum Vmxnet3_RSSField rss_fields = adapter->rss_fields;
811 
812 	/* RSS does not support anything other than hashing
813 	 * to queues on src and dst IPs and ports
814 	 */
815 	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
816 			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
817 		return -EINVAL;
818 
819 	switch (nfc->flow_type) {
820 	case TCP_V4_FLOW:
821 	case TCP_V6_FLOW:
822 		if (!(nfc->data & RXH_IP_SRC) ||
823 		    !(nfc->data & RXH_IP_DST) ||
824 		    !(nfc->data & RXH_L4_B_0_1) ||
825 		    !(nfc->data & RXH_L4_B_2_3))
826 			return -EINVAL;
827 		break;
828 	case UDP_V4_FLOW:
829 		if (!(nfc->data & RXH_IP_SRC) ||
830 		    !(nfc->data & RXH_IP_DST))
831 			return -EINVAL;
832 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
833 		case 0:
834 			rss_fields &= ~VMXNET3_RSS_FIELDS_UDPIP4;
835 			break;
836 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
837 			rss_fields |= VMXNET3_RSS_FIELDS_UDPIP4;
838 			break;
839 		default:
840 			return -EINVAL;
841 		}
842 		break;
843 	case UDP_V6_FLOW:
844 		if (!(nfc->data & RXH_IP_SRC) ||
845 		    !(nfc->data & RXH_IP_DST))
846 			return -EINVAL;
847 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
848 		case 0:
849 			rss_fields &= ~VMXNET3_RSS_FIELDS_UDPIP6;
850 			break;
851 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
852 			rss_fields |= VMXNET3_RSS_FIELDS_UDPIP6;
853 			break;
854 		default:
855 			return -EINVAL;
856 		}
857 		break;
858 	case ESP_V4_FLOW:
859 	case AH_V4_FLOW:
860 	case AH_ESP_V4_FLOW:
861 		if (!(nfc->data & RXH_IP_SRC) ||
862 		    !(nfc->data & RXH_IP_DST))
863 			return -EINVAL;
864 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
865 		case 0:
866 			rss_fields &= ~VMXNET3_RSS_FIELDS_ESPIP4;
867 			break;
868 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
869 			rss_fields |= VMXNET3_RSS_FIELDS_ESPIP4;
870 		break;
871 		default:
872 			return -EINVAL;
873 		}
874 		break;
875 	case ESP_V6_FLOW:
876 	case AH_V6_FLOW:
877 	case AH_ESP_V6_FLOW:
878 		if (!VMXNET3_VERSION_GE_6(adapter))
879 			return -EOPNOTSUPP;
880 		if (!(nfc->data & RXH_IP_SRC) ||
881 		    !(nfc->data & RXH_IP_DST))
882 			return -EINVAL;
883 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
884 		case 0:
885 			rss_fields &= ~VMXNET3_RSS_FIELDS_ESPIP6;
886 			break;
887 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
888 			rss_fields |= VMXNET3_RSS_FIELDS_ESPIP6;
889 			break;
890 		default:
891 			return -EINVAL;
892 		}
893 		break;
894 	case SCTP_V4_FLOW:
895 	case SCTP_V6_FLOW:
896 		if (!(nfc->data & RXH_IP_SRC) ||
897 		    !(nfc->data & RXH_IP_DST) ||
898 		    (nfc->data & RXH_L4_B_0_1) ||
899 		    (nfc->data & RXH_L4_B_2_3))
900 			return -EINVAL;
901 		break;
902 	default:
903 		return -EINVAL;
904 	}
905 
906 	/* if we changed something we need to update flags */
907 	if (rss_fields != adapter->rss_fields) {
908 		adapter->default_rss_fields = false;
909 		if (netif_running(netdev)) {
910 			struct Vmxnet3_DriverShared *shared = adapter->shared;
911 			union Vmxnet3_CmdInfo *cmdInfo = &shared->cu.cmdInfo;
912 			unsigned long flags;
913 
914 			spin_lock_irqsave(&adapter->cmd_lock, flags);
915 			cmdInfo->setRssFields = rss_fields;
916 			VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
917 					       VMXNET3_CMD_SET_RSS_FIELDS);
918 
919 			/* Not all requested RSS may get applied, so get and
920 			 * cache what was actually applied.
921 			 */
922 			VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
923 					       VMXNET3_CMD_GET_RSS_FIELDS);
924 			adapter->rss_fields =
925 				VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
926 			spin_unlock_irqrestore(&adapter->cmd_lock, flags);
927 		} else {
928 			/* When the device is activated, we will try to apply
929 			 * these rules and cache the applied value later.
930 			 */
931 			adapter->rss_fields = rss_fields;
932 		}
933 	}
934 	return 0;
935 }
936 
937 static int
938 vmxnet3_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
939 		  u32 *rules)
940 {
941 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
942 	int err = 0;
943 
944 	switch (info->cmd) {
945 	case ETHTOOL_GRXRINGS:
946 		info->data = adapter->num_rx_queues;
947 		break;
948 	case ETHTOOL_GRXFH:
949 		if (!VMXNET3_VERSION_GE_4(adapter)) {
950 			err = -EOPNOTSUPP;
951 			break;
952 		}
953 #ifdef VMXNET3_RSS
954 		if (!adapter->rss) {
955 			err = -EOPNOTSUPP;
956 			break;
957 		}
958 #endif
959 		err = vmxnet3_get_rss_hash_opts(adapter, info);
960 		break;
961 	default:
962 		err = -EOPNOTSUPP;
963 		break;
964 	}
965 
966 	return err;
967 }
968 
969 static int
970 vmxnet3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info)
971 {
972 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
973 	int err = 0;
974 
975 	if (!VMXNET3_VERSION_GE_4(adapter)) {
976 		err = -EOPNOTSUPP;
977 		goto done;
978 	}
979 #ifdef VMXNET3_RSS
980 	if (!adapter->rss) {
981 		err = -EOPNOTSUPP;
982 		goto done;
983 	}
984 #endif
985 
986 	switch (info->cmd) {
987 	case ETHTOOL_SRXFH:
988 		err = vmxnet3_set_rss_hash_opt(netdev, adapter, info);
989 		break;
990 	default:
991 		err = -EOPNOTSUPP;
992 		break;
993 	}
994 
995 done:
996 	return err;
997 }
998 
999 #ifdef VMXNET3_RSS
1000 static u32
1001 vmxnet3_get_rss_indir_size(struct net_device *netdev)
1002 {
1003 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1004 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
1005 
1006 	return rssConf->indTableSize;
1007 }
1008 
1009 static int
1010 vmxnet3_get_rss(struct net_device *netdev, u32 *p, u8 *key, u8 *hfunc)
1011 {
1012 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1013 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
1014 	unsigned int n = rssConf->indTableSize;
1015 
1016 	if (hfunc)
1017 		*hfunc = ETH_RSS_HASH_TOP;
1018 	if (!p)
1019 		return 0;
1020 	if (n > UPT1_RSS_MAX_IND_TABLE_SIZE)
1021 		return 0;
1022 	while (n--)
1023 		p[n] = rssConf->indTable[n];
1024 	return 0;
1025 
1026 }
1027 
1028 static int
1029 vmxnet3_set_rss(struct net_device *netdev, const u32 *p, const u8 *key,
1030 		const u8 hfunc)
1031 {
1032 	unsigned int i;
1033 	unsigned long flags;
1034 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1035 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
1036 
1037 	/* We do not allow change in unsupported parameters */
1038 	if (key ||
1039 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
1040 		return -EOPNOTSUPP;
1041 	if (!p)
1042 		return 0;
1043 	for (i = 0; i < rssConf->indTableSize; i++)
1044 		rssConf->indTable[i] = p[i];
1045 
1046 	spin_lock_irqsave(&adapter->cmd_lock, flags);
1047 	VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1048 			       VMXNET3_CMD_UPDATE_RSSIDT);
1049 	spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1050 
1051 	return 0;
1052 
1053 }
1054 #endif
1055 
1056 static int vmxnet3_get_coalesce(struct net_device *netdev,
1057 				struct ethtool_coalesce *ec,
1058 				struct kernel_ethtool_coalesce *kernel_coal,
1059 				struct netlink_ext_ack *extack)
1060 {
1061 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1062 
1063 	if (!VMXNET3_VERSION_GE_3(adapter))
1064 		return -EOPNOTSUPP;
1065 
1066 	switch (adapter->coal_conf->coalMode) {
1067 	case VMXNET3_COALESCE_DISABLED:
1068 		/* struct ethtool_coalesce is already initialized to 0 */
1069 		break;
1070 	case VMXNET3_COALESCE_ADAPT:
1071 		ec->use_adaptive_rx_coalesce = true;
1072 		break;
1073 	case VMXNET3_COALESCE_STATIC:
1074 		ec->tx_max_coalesced_frames =
1075 			adapter->coal_conf->coalPara.coalStatic.tx_comp_depth;
1076 		ec->rx_max_coalesced_frames =
1077 			adapter->coal_conf->coalPara.coalStatic.rx_depth;
1078 		break;
1079 	case VMXNET3_COALESCE_RBC: {
1080 		u32 rbc_rate;
1081 
1082 		rbc_rate = adapter->coal_conf->coalPara.coalRbc.rbc_rate;
1083 		ec->rx_coalesce_usecs = VMXNET3_COAL_RBC_USECS(rbc_rate);
1084 	}
1085 		break;
1086 	default:
1087 		return -EOPNOTSUPP;
1088 	}
1089 
1090 	return 0;
1091 }
1092 
1093 static int vmxnet3_set_coalesce(struct net_device *netdev,
1094 				struct ethtool_coalesce *ec,
1095 				struct kernel_ethtool_coalesce *kernel_coal,
1096 				struct netlink_ext_ack *extack)
1097 {
1098 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
1099 	struct Vmxnet3_DriverShared *shared = adapter->shared;
1100 	union Vmxnet3_CmdInfo *cmdInfo = &shared->cu.cmdInfo;
1101 	unsigned long flags;
1102 
1103 	if (!VMXNET3_VERSION_GE_3(adapter))
1104 		return -EOPNOTSUPP;
1105 
1106 	if ((ec->rx_coalesce_usecs == 0) &&
1107 	    (ec->use_adaptive_rx_coalesce == 0) &&
1108 	    (ec->tx_max_coalesced_frames == 0) &&
1109 	    (ec->rx_max_coalesced_frames == 0)) {
1110 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
1111 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_DISABLED;
1112 		goto done;
1113 	}
1114 
1115 	if (ec->rx_coalesce_usecs != 0) {
1116 		u32 rbc_rate;
1117 
1118 		if ((ec->use_adaptive_rx_coalesce != 0) ||
1119 		    (ec->tx_max_coalesced_frames != 0) ||
1120 		    (ec->rx_max_coalesced_frames != 0)) {
1121 			return -EINVAL;
1122 		}
1123 
1124 		rbc_rate = VMXNET3_COAL_RBC_RATE(ec->rx_coalesce_usecs);
1125 		if (rbc_rate < VMXNET3_COAL_RBC_MIN_RATE ||
1126 		    rbc_rate > VMXNET3_COAL_RBC_MAX_RATE) {
1127 			return -EINVAL;
1128 		}
1129 
1130 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
1131 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_RBC;
1132 		adapter->coal_conf->coalPara.coalRbc.rbc_rate = rbc_rate;
1133 		goto done;
1134 	}
1135 
1136 	if (ec->use_adaptive_rx_coalesce != 0) {
1137 		if (ec->tx_max_coalesced_frames != 0 ||
1138 		    ec->rx_max_coalesced_frames != 0) {
1139 			return -EINVAL;
1140 		}
1141 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
1142 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_ADAPT;
1143 		goto done;
1144 	}
1145 
1146 	if ((ec->tx_max_coalesced_frames != 0) ||
1147 	    (ec->rx_max_coalesced_frames != 0)) {
1148 		if ((ec->tx_max_coalesced_frames >
1149 		    VMXNET3_COAL_STATIC_MAX_DEPTH) ||
1150 		    (ec->rx_max_coalesced_frames >
1151 		     VMXNET3_COAL_STATIC_MAX_DEPTH)) {
1152 			return -EINVAL;
1153 		}
1154 
1155 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
1156 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_STATIC;
1157 
1158 		adapter->coal_conf->coalPara.coalStatic.tx_comp_depth =
1159 			(ec->tx_max_coalesced_frames ?
1160 			 ec->tx_max_coalesced_frames :
1161 			 VMXNET3_COAL_STATIC_DEFAULT_DEPTH);
1162 
1163 		adapter->coal_conf->coalPara.coalStatic.rx_depth =
1164 			(ec->rx_max_coalesced_frames ?
1165 			 ec->rx_max_coalesced_frames :
1166 			 VMXNET3_COAL_STATIC_DEFAULT_DEPTH);
1167 
1168 		adapter->coal_conf->coalPara.coalStatic.tx_depth =
1169 			 VMXNET3_COAL_STATIC_DEFAULT_DEPTH;
1170 		goto done;
1171 	}
1172 
1173 done:
1174 	adapter->default_coal_mode = false;
1175 	if (netif_running(netdev)) {
1176 		spin_lock_irqsave(&adapter->cmd_lock, flags);
1177 		cmdInfo->varConf.confVer = 1;
1178 		cmdInfo->varConf.confLen =
1179 			cpu_to_le32(sizeof(*adapter->coal_conf));
1180 		cmdInfo->varConf.confPA  = cpu_to_le64(adapter->coal_conf_pa);
1181 		VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
1182 				       VMXNET3_CMD_SET_COALESCE);
1183 		spin_unlock_irqrestore(&adapter->cmd_lock, flags);
1184 	}
1185 
1186 	return 0;
1187 }
1188 
1189 static const struct ethtool_ops vmxnet3_ethtool_ops = {
1190 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
1191 				     ETHTOOL_COALESCE_MAX_FRAMES |
1192 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
1193 	.get_drvinfo       = vmxnet3_get_drvinfo,
1194 	.get_regs_len      = vmxnet3_get_regs_len,
1195 	.get_regs          = vmxnet3_get_regs,
1196 	.get_wol           = vmxnet3_get_wol,
1197 	.set_wol           = vmxnet3_set_wol,
1198 	.get_link          = ethtool_op_get_link,
1199 	.get_coalesce      = vmxnet3_get_coalesce,
1200 	.set_coalesce      = vmxnet3_set_coalesce,
1201 	.get_strings       = vmxnet3_get_strings,
1202 	.get_sset_count	   = vmxnet3_get_sset_count,
1203 	.get_ethtool_stats = vmxnet3_get_ethtool_stats,
1204 	.get_ringparam     = vmxnet3_get_ringparam,
1205 	.set_ringparam     = vmxnet3_set_ringparam,
1206 	.get_rxnfc         = vmxnet3_get_rxnfc,
1207 	.set_rxnfc         = vmxnet3_set_rxnfc,
1208 #ifdef VMXNET3_RSS
1209 	.get_rxfh_indir_size = vmxnet3_get_rss_indir_size,
1210 	.get_rxfh          = vmxnet3_get_rss,
1211 	.set_rxfh          = vmxnet3_set_rss,
1212 #endif
1213 	.get_link_ksettings = vmxnet3_get_link_ksettings,
1214 };
1215 
1216 void vmxnet3_set_ethtool_ops(struct net_device *netdev)
1217 {
1218 	netdev->ethtool_ops = &vmxnet3_ethtool_ops;
1219 }
1220