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