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