1 /*
2  * Linux driver for VMware's vmxnet3 ethernet NIC.
3  *
4  * Copyright (C) 2008-2016, 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 int vmxnet3_set_features(struct net_device *netdev, netdev_features_t features)
261 {
262 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
263 	unsigned long flags;
264 	netdev_features_t changed = features ^ netdev->features;
265 
266 	if (changed & (NETIF_F_RXCSUM | NETIF_F_LRO |
267 		       NETIF_F_HW_VLAN_CTAG_RX)) {
268 		if (features & NETIF_F_RXCSUM)
269 			adapter->shared->devRead.misc.uptFeatures |=
270 			UPT1_F_RXCSUM;
271 		else
272 			adapter->shared->devRead.misc.uptFeatures &=
273 			~UPT1_F_RXCSUM;
274 
275 		/* update hardware LRO capability accordingly */
276 		if (features & NETIF_F_LRO)
277 			adapter->shared->devRead.misc.uptFeatures |=
278 							UPT1_F_LRO;
279 		else
280 			adapter->shared->devRead.misc.uptFeatures &=
281 							~UPT1_F_LRO;
282 
283 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
284 			adapter->shared->devRead.misc.uptFeatures |=
285 			UPT1_F_RXVLAN;
286 		else
287 			adapter->shared->devRead.misc.uptFeatures &=
288 			~UPT1_F_RXVLAN;
289 
290 		spin_lock_irqsave(&adapter->cmd_lock, flags);
291 		VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
292 				       VMXNET3_CMD_UPDATE_FEATURE);
293 		spin_unlock_irqrestore(&adapter->cmd_lock, flags);
294 	}
295 	return 0;
296 }
297 
298 static void
299 vmxnet3_get_ethtool_stats(struct net_device *netdev,
300 			  struct ethtool_stats *stats, u64  *buf)
301 {
302 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
303 	unsigned long flags;
304 	u8 *base;
305 	int i;
306 	int j = 0;
307 
308 	spin_lock_irqsave(&adapter->cmd_lock, flags);
309 	VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_STATS);
310 	spin_unlock_irqrestore(&adapter->cmd_lock, flags);
311 
312 	/* this does assume each counter is 64-bit wide */
313 	for (j = 0; j < adapter->num_tx_queues; j++) {
314 		base = (u8 *)&adapter->tqd_start[j].stats;
315 		*buf++ = (u64)j;
316 		for (i = 1; i < ARRAY_SIZE(vmxnet3_tq_dev_stats); i++)
317 			*buf++ = *(u64 *)(base +
318 					  vmxnet3_tq_dev_stats[i].offset);
319 
320 		base = (u8 *)&adapter->tx_queue[j].stats;
321 		for (i = 0; i < ARRAY_SIZE(vmxnet3_tq_driver_stats); i++)
322 			*buf++ = *(u64 *)(base +
323 					  vmxnet3_tq_driver_stats[i].offset);
324 	}
325 
326 	for (j = 0; j < adapter->num_rx_queues; j++) {
327 		base = (u8 *)&adapter->rqd_start[j].stats;
328 		*buf++ = (u64) j;
329 		for (i = 1; i < ARRAY_SIZE(vmxnet3_rq_dev_stats); i++)
330 			*buf++ = *(u64 *)(base +
331 					  vmxnet3_rq_dev_stats[i].offset);
332 
333 		base = (u8 *)&adapter->rx_queue[j].stats;
334 		for (i = 0; i < ARRAY_SIZE(vmxnet3_rq_driver_stats); i++)
335 			*buf++ = *(u64 *)(base +
336 					  vmxnet3_rq_driver_stats[i].offset);
337 	}
338 
339 	base = (u8 *)adapter;
340 	for (i = 0; i < ARRAY_SIZE(vmxnet3_global_stats); i++)
341 		*buf++ = *(u64 *)(base + vmxnet3_global_stats[i].offset);
342 }
343 
344 
345 /* This is a version 2 of the vmxnet3 ethtool_regs which goes hand in hand with
346  * the version 2 of the vmxnet3 support for ethtool(8) --register-dump.
347  * Therefore, if any registers are added, removed or modified, then a version
348  * bump and a corresponding change in the vmxnet3 support for ethtool(8)
349  * --register-dump would be required.
350  */
351 static void
352 vmxnet3_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
353 {
354 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
355 	u32 *buf = p;
356 	int i = 0, j = 0;
357 
358 	memset(p, 0, vmxnet3_get_regs_len(netdev));
359 
360 	regs->version = 2;
361 
362 	/* Update vmxnet3_get_regs_len if we want to dump more registers */
363 
364 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
365 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
366 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_DSAL);
367 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_DSAH);
368 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
369 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
370 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
371 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
372 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ECR);
373 
374 	buf[j++] = adapter->intr.num_intrs;
375 	for (i = 0; i < adapter->intr.num_intrs; i++) {
376 		buf[j++] = VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_IMR
377 						 + i * VMXNET3_REG_ALIGN);
378 	}
379 
380 	buf[j++] = adapter->num_tx_queues;
381 	for (i = 0; i < adapter->num_tx_queues; i++) {
382 		struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
383 
384 		buf[j++] = VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_TXPROD +
385 						 i * VMXNET3_REG_ALIGN);
386 
387 		buf[j++] = VMXNET3_GET_ADDR_LO(tq->tx_ring.basePA);
388 		buf[j++] = VMXNET3_GET_ADDR_HI(tq->tx_ring.basePA);
389 		buf[j++] = tq->tx_ring.size;
390 		buf[j++] = tq->tx_ring.next2fill;
391 		buf[j++] = tq->tx_ring.next2comp;
392 		buf[j++] = tq->tx_ring.gen;
393 
394 		buf[j++] = VMXNET3_GET_ADDR_LO(tq->data_ring.basePA);
395 		buf[j++] = VMXNET3_GET_ADDR_HI(tq->data_ring.basePA);
396 		buf[j++] = tq->data_ring.size;
397 		buf[j++] = tq->txdata_desc_size;
398 
399 		buf[j++] = VMXNET3_GET_ADDR_LO(tq->comp_ring.basePA);
400 		buf[j++] = VMXNET3_GET_ADDR_HI(tq->comp_ring.basePA);
401 		buf[j++] = tq->comp_ring.size;
402 		buf[j++] = tq->comp_ring.next2proc;
403 		buf[j++] = tq->comp_ring.gen;
404 
405 		buf[j++] = tq->stopped;
406 	}
407 
408 	buf[j++] = adapter->num_rx_queues;
409 	for (i = 0; i < adapter->num_rx_queues; i++) {
410 		struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
411 
412 		buf[j++] =  VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_RXPROD +
413 						  i * VMXNET3_REG_ALIGN);
414 		buf[j++] =  VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_RXPROD2 +
415 						  i * VMXNET3_REG_ALIGN);
416 
417 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->rx_ring[0].basePA);
418 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->rx_ring[0].basePA);
419 		buf[j++] = rq->rx_ring[0].size;
420 		buf[j++] = rq->rx_ring[0].next2fill;
421 		buf[j++] = rq->rx_ring[0].next2comp;
422 		buf[j++] = rq->rx_ring[0].gen;
423 
424 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->rx_ring[1].basePA);
425 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->rx_ring[1].basePA);
426 		buf[j++] = rq->rx_ring[1].size;
427 		buf[j++] = rq->rx_ring[1].next2fill;
428 		buf[j++] = rq->rx_ring[1].next2comp;
429 		buf[j++] = rq->rx_ring[1].gen;
430 
431 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->data_ring.basePA);
432 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->data_ring.basePA);
433 		buf[j++] = rq->rx_ring[0].size;
434 		buf[j++] = rq->data_ring.desc_size;
435 
436 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->comp_ring.basePA);
437 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->comp_ring.basePA);
438 		buf[j++] = rq->comp_ring.size;
439 		buf[j++] = rq->comp_ring.next2proc;
440 		buf[j++] = rq->comp_ring.gen;
441 	}
442 }
443 
444 
445 static void
446 vmxnet3_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
447 {
448 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
449 
450 	wol->supported = WAKE_UCAST | WAKE_ARP | WAKE_MAGIC;
451 	wol->wolopts = adapter->wol;
452 }
453 
454 
455 static int
456 vmxnet3_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
457 {
458 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
459 
460 	if (wol->wolopts & (WAKE_PHY | WAKE_MCAST | WAKE_BCAST |
461 			    WAKE_MAGICSECURE)) {
462 		return -EOPNOTSUPP;
463 	}
464 
465 	adapter->wol = wol->wolopts;
466 
467 	device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
468 
469 	return 0;
470 }
471 
472 
473 static int
474 vmxnet3_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
475 {
476 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
477 
478 	ecmd->supported = SUPPORTED_10000baseT_Full | SUPPORTED_1000baseT_Full |
479 			  SUPPORTED_TP;
480 	ecmd->advertising = ADVERTISED_TP;
481 	ecmd->port = PORT_TP;
482 	ecmd->transceiver = XCVR_INTERNAL;
483 
484 	if (adapter->link_speed) {
485 		ethtool_cmd_speed_set(ecmd, adapter->link_speed);
486 		ecmd->duplex = DUPLEX_FULL;
487 	} else {
488 		ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
489 		ecmd->duplex = DUPLEX_UNKNOWN;
490 	}
491 	return 0;
492 }
493 
494 
495 static void
496 vmxnet3_get_ringparam(struct net_device *netdev,
497 		      struct ethtool_ringparam *param)
498 {
499 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
500 
501 	param->rx_max_pending = VMXNET3_RX_RING_MAX_SIZE;
502 	param->tx_max_pending = VMXNET3_TX_RING_MAX_SIZE;
503 	param->rx_mini_max_pending = VMXNET3_VERSION_GE_3(adapter) ?
504 		VMXNET3_RXDATA_DESC_MAX_SIZE : 0;
505 	param->rx_jumbo_max_pending = VMXNET3_RX_RING2_MAX_SIZE;
506 
507 	param->rx_pending = adapter->rx_ring_size;
508 	param->tx_pending = adapter->tx_ring_size;
509 	param->rx_mini_pending = VMXNET3_VERSION_GE_3(adapter) ?
510 		adapter->rxdata_desc_size : 0;
511 	param->rx_jumbo_pending = adapter->rx_ring2_size;
512 }
513 
514 
515 static int
516 vmxnet3_set_ringparam(struct net_device *netdev,
517 		      struct ethtool_ringparam *param)
518 {
519 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
520 	u32 new_tx_ring_size, new_rx_ring_size, new_rx_ring2_size;
521 	u16 new_rxdata_desc_size;
522 	u32 sz;
523 	int err = 0;
524 
525 	if (param->tx_pending == 0 || param->tx_pending >
526 						VMXNET3_TX_RING_MAX_SIZE)
527 		return -EINVAL;
528 
529 	if (param->rx_pending == 0 || param->rx_pending >
530 						VMXNET3_RX_RING_MAX_SIZE)
531 		return -EINVAL;
532 
533 	if (param->rx_jumbo_pending == 0 ||
534 	    param->rx_jumbo_pending > VMXNET3_RX_RING2_MAX_SIZE)
535 		return -EINVAL;
536 
537 	/* if adapter not yet initialized, do nothing */
538 	if (adapter->rx_buf_per_pkt == 0) {
539 		netdev_err(netdev, "adapter not completely initialized, "
540 			   "ring size cannot be changed yet\n");
541 		return -EOPNOTSUPP;
542 	}
543 
544 	if (VMXNET3_VERSION_GE_3(adapter)) {
545 		if (param->rx_mini_pending < 0 ||
546 		    param->rx_mini_pending > VMXNET3_RXDATA_DESC_MAX_SIZE) {
547 			return -EINVAL;
548 		}
549 	} else if (param->rx_mini_pending != 0) {
550 		return -EINVAL;
551 	}
552 
553 	/* round it up to a multiple of VMXNET3_RING_SIZE_ALIGN */
554 	new_tx_ring_size = (param->tx_pending + VMXNET3_RING_SIZE_MASK) &
555 							~VMXNET3_RING_SIZE_MASK;
556 	new_tx_ring_size = min_t(u32, new_tx_ring_size,
557 				 VMXNET3_TX_RING_MAX_SIZE);
558 	if (new_tx_ring_size > VMXNET3_TX_RING_MAX_SIZE || (new_tx_ring_size %
559 						VMXNET3_RING_SIZE_ALIGN) != 0)
560 		return -EINVAL;
561 
562 	/* ring0 has to be a multiple of
563 	 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
564 	 */
565 	sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
566 	new_rx_ring_size = (param->rx_pending + sz - 1) / sz * sz;
567 	new_rx_ring_size = min_t(u32, new_rx_ring_size,
568 				 VMXNET3_RX_RING_MAX_SIZE / sz * sz);
569 	if (new_rx_ring_size > VMXNET3_RX_RING_MAX_SIZE || (new_rx_ring_size %
570 							   sz) != 0)
571 		return -EINVAL;
572 
573 	/* ring2 has to be a multiple of VMXNET3_RING_SIZE_ALIGN */
574 	new_rx_ring2_size = (param->rx_jumbo_pending + VMXNET3_RING_SIZE_MASK) &
575 				~VMXNET3_RING_SIZE_MASK;
576 	new_rx_ring2_size = min_t(u32, new_rx_ring2_size,
577 				  VMXNET3_RX_RING2_MAX_SIZE);
578 
579 	/* rx data ring buffer size has to be a multiple of
580 	 * VMXNET3_RXDATA_DESC_SIZE_ALIGN
581 	 */
582 	new_rxdata_desc_size =
583 		(param->rx_mini_pending + VMXNET3_RXDATA_DESC_SIZE_MASK) &
584 		~VMXNET3_RXDATA_DESC_SIZE_MASK;
585 	new_rxdata_desc_size = min_t(u16, new_rxdata_desc_size,
586 				     VMXNET3_RXDATA_DESC_MAX_SIZE);
587 
588 	if (new_tx_ring_size == adapter->tx_ring_size &&
589 	    new_rx_ring_size == adapter->rx_ring_size &&
590 	    new_rx_ring2_size == adapter->rx_ring2_size &&
591 	    new_rxdata_desc_size == adapter->rxdata_desc_size) {
592 		return 0;
593 	}
594 
595 	/*
596 	 * Reset_work may be in the middle of resetting the device, wait for its
597 	 * completion.
598 	 */
599 	while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
600 		msleep(1);
601 
602 	if (netif_running(netdev)) {
603 		vmxnet3_quiesce_dev(adapter);
604 		vmxnet3_reset_dev(adapter);
605 
606 		/* recreate the rx queue and the tx queue based on the
607 		 * new sizes */
608 		vmxnet3_tq_destroy_all(adapter);
609 		vmxnet3_rq_destroy_all(adapter);
610 
611 		err = vmxnet3_create_queues(adapter, new_tx_ring_size,
612 					    new_rx_ring_size, new_rx_ring2_size,
613 					    adapter->txdata_desc_size,
614 					    new_rxdata_desc_size);
615 		if (err) {
616 			/* failed, most likely because of OOM, try default
617 			 * size */
618 			netdev_err(netdev, "failed to apply new sizes, "
619 				   "try the default ones\n");
620 			new_rx_ring_size = VMXNET3_DEF_RX_RING_SIZE;
621 			new_rx_ring2_size = VMXNET3_DEF_RX_RING2_SIZE;
622 			new_tx_ring_size = VMXNET3_DEF_TX_RING_SIZE;
623 			new_rxdata_desc_size = VMXNET3_VERSION_GE_3(adapter) ?
624 				VMXNET3_DEF_RXDATA_DESC_SIZE : 0;
625 
626 			err = vmxnet3_create_queues(adapter,
627 						    new_tx_ring_size,
628 						    new_rx_ring_size,
629 						    new_rx_ring2_size,
630 						    adapter->txdata_desc_size,
631 						    new_rxdata_desc_size);
632 			if (err) {
633 				netdev_err(netdev, "failed to create queues "
634 					   "with default sizes. Closing it\n");
635 				goto out;
636 			}
637 		}
638 
639 		err = vmxnet3_activate_dev(adapter);
640 		if (err)
641 			netdev_err(netdev, "failed to re-activate, error %d."
642 				   " Closing it\n", err);
643 	}
644 	adapter->tx_ring_size = new_tx_ring_size;
645 	adapter->rx_ring_size = new_rx_ring_size;
646 	adapter->rx_ring2_size = new_rx_ring2_size;
647 	adapter->rxdata_desc_size = new_rxdata_desc_size;
648 
649 out:
650 	clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
651 	if (err)
652 		vmxnet3_force_close(adapter);
653 
654 	return err;
655 }
656 
657 
658 static int
659 vmxnet3_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
660 		  u32 *rules)
661 {
662 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
663 	switch (info->cmd) {
664 	case ETHTOOL_GRXRINGS:
665 		info->data = adapter->num_rx_queues;
666 		return 0;
667 	}
668 	return -EOPNOTSUPP;
669 }
670 
671 #ifdef VMXNET3_RSS
672 static u32
673 vmxnet3_get_rss_indir_size(struct net_device *netdev)
674 {
675 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
676 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
677 
678 	return rssConf->indTableSize;
679 }
680 
681 static int
682 vmxnet3_get_rss(struct net_device *netdev, u32 *p, u8 *key, u8 *hfunc)
683 {
684 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
685 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
686 	unsigned int n = rssConf->indTableSize;
687 
688 	if (hfunc)
689 		*hfunc = ETH_RSS_HASH_TOP;
690 	if (!p)
691 		return 0;
692 	while (n--)
693 		p[n] = rssConf->indTable[n];
694 	return 0;
695 
696 }
697 
698 static int
699 vmxnet3_set_rss(struct net_device *netdev, const u32 *p, const u8 *key,
700 		const u8 hfunc)
701 {
702 	unsigned int i;
703 	unsigned long flags;
704 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
705 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
706 
707 	/* We do not allow change in unsupported parameters */
708 	if (key ||
709 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
710 		return -EOPNOTSUPP;
711 	if (!p)
712 		return 0;
713 	for (i = 0; i < rssConf->indTableSize; i++)
714 		rssConf->indTable[i] = p[i];
715 
716 	spin_lock_irqsave(&adapter->cmd_lock, flags);
717 	VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
718 			       VMXNET3_CMD_UPDATE_RSSIDT);
719 	spin_unlock_irqrestore(&adapter->cmd_lock, flags);
720 
721 	return 0;
722 
723 }
724 #endif
725 
726 static int
727 vmxnet3_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
728 {
729 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
730 
731 	if (!VMXNET3_VERSION_GE_3(adapter))
732 		return -EOPNOTSUPP;
733 
734 	switch (adapter->coal_conf->coalMode) {
735 	case VMXNET3_COALESCE_DISABLED:
736 		/* struct ethtool_coalesce is already initialized to 0 */
737 		break;
738 	case VMXNET3_COALESCE_ADAPT:
739 		ec->use_adaptive_rx_coalesce = true;
740 		break;
741 	case VMXNET3_COALESCE_STATIC:
742 		ec->tx_max_coalesced_frames =
743 			adapter->coal_conf->coalPara.coalStatic.tx_comp_depth;
744 		ec->rx_max_coalesced_frames =
745 			adapter->coal_conf->coalPara.coalStatic.rx_depth;
746 		break;
747 	case VMXNET3_COALESCE_RBC: {
748 		u32 rbc_rate;
749 
750 		rbc_rate = adapter->coal_conf->coalPara.coalRbc.rbc_rate;
751 		ec->rx_coalesce_usecs = VMXNET3_COAL_RBC_USECS(rbc_rate);
752 	}
753 		break;
754 	default:
755 		return -EOPNOTSUPP;
756 	}
757 
758 	return 0;
759 }
760 
761 static int
762 vmxnet3_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
763 {
764 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
765 	struct Vmxnet3_DriverShared *shared = adapter->shared;
766 	union Vmxnet3_CmdInfo *cmdInfo = &shared->cu.cmdInfo;
767 	unsigned long flags;
768 
769 	if (!VMXNET3_VERSION_GE_3(adapter))
770 		return -EOPNOTSUPP;
771 
772 	if (ec->rx_coalesce_usecs_irq ||
773 	    ec->rx_max_coalesced_frames_irq ||
774 	    ec->tx_coalesce_usecs ||
775 	    ec->tx_coalesce_usecs_irq ||
776 	    ec->tx_max_coalesced_frames_irq ||
777 	    ec->stats_block_coalesce_usecs ||
778 	    ec->use_adaptive_tx_coalesce ||
779 	    ec->pkt_rate_low ||
780 	    ec->rx_coalesce_usecs_low ||
781 	    ec->rx_max_coalesced_frames_low ||
782 	    ec->tx_coalesce_usecs_low ||
783 	    ec->tx_max_coalesced_frames_low ||
784 	    ec->pkt_rate_high ||
785 	    ec->rx_coalesce_usecs_high ||
786 	    ec->rx_max_coalesced_frames_high ||
787 	    ec->tx_coalesce_usecs_high ||
788 	    ec->tx_max_coalesced_frames_high ||
789 	    ec->rate_sample_interval) {
790 		return -EINVAL;
791 	}
792 
793 	if ((ec->rx_coalesce_usecs == 0) &&
794 	    (ec->use_adaptive_rx_coalesce == 0) &&
795 	    (ec->tx_max_coalesced_frames == 0) &&
796 	    (ec->rx_max_coalesced_frames == 0)) {
797 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
798 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_DISABLED;
799 		goto done;
800 	}
801 
802 	if (ec->rx_coalesce_usecs != 0) {
803 		u32 rbc_rate;
804 
805 		if ((ec->use_adaptive_rx_coalesce != 0) ||
806 		    (ec->tx_max_coalesced_frames != 0) ||
807 		    (ec->rx_max_coalesced_frames != 0)) {
808 			return -EINVAL;
809 		}
810 
811 		rbc_rate = VMXNET3_COAL_RBC_RATE(ec->rx_coalesce_usecs);
812 		if (rbc_rate < VMXNET3_COAL_RBC_MIN_RATE ||
813 		    rbc_rate > VMXNET3_COAL_RBC_MAX_RATE) {
814 			return -EINVAL;
815 		}
816 
817 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
818 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_RBC;
819 		adapter->coal_conf->coalPara.coalRbc.rbc_rate = rbc_rate;
820 		goto done;
821 	}
822 
823 	if (ec->use_adaptive_rx_coalesce != 0) {
824 		if ((ec->rx_coalesce_usecs != 0) ||
825 		    (ec->tx_max_coalesced_frames != 0) ||
826 		    (ec->rx_max_coalesced_frames != 0)) {
827 			return -EINVAL;
828 		}
829 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
830 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_ADAPT;
831 		goto done;
832 	}
833 
834 	if ((ec->tx_max_coalesced_frames != 0) ||
835 	    (ec->rx_max_coalesced_frames != 0)) {
836 		if ((ec->rx_coalesce_usecs != 0) ||
837 		    (ec->use_adaptive_rx_coalesce != 0)) {
838 			return -EINVAL;
839 		}
840 
841 		if ((ec->tx_max_coalesced_frames >
842 		    VMXNET3_COAL_STATIC_MAX_DEPTH) ||
843 		    (ec->rx_max_coalesced_frames >
844 		     VMXNET3_COAL_STATIC_MAX_DEPTH)) {
845 			return -EINVAL;
846 		}
847 
848 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
849 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_STATIC;
850 
851 		adapter->coal_conf->coalPara.coalStatic.tx_comp_depth =
852 			(ec->tx_max_coalesced_frames ?
853 			 ec->tx_max_coalesced_frames :
854 			 VMXNET3_COAL_STATIC_DEFAULT_DEPTH);
855 
856 		adapter->coal_conf->coalPara.coalStatic.rx_depth =
857 			(ec->rx_max_coalesced_frames ?
858 			 ec->rx_max_coalesced_frames :
859 			 VMXNET3_COAL_STATIC_DEFAULT_DEPTH);
860 
861 		adapter->coal_conf->coalPara.coalStatic.tx_depth =
862 			 VMXNET3_COAL_STATIC_DEFAULT_DEPTH;
863 		goto done;
864 	}
865 
866 done:
867 	adapter->default_coal_mode = false;
868 	if (netif_running(netdev)) {
869 		spin_lock_irqsave(&adapter->cmd_lock, flags);
870 		cmdInfo->varConf.confVer = 1;
871 		cmdInfo->varConf.confLen =
872 			cpu_to_le32(sizeof(*adapter->coal_conf));
873 		cmdInfo->varConf.confPA  = cpu_to_le64(adapter->coal_conf_pa);
874 		VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
875 				       VMXNET3_CMD_SET_COALESCE);
876 		spin_unlock_irqrestore(&adapter->cmd_lock, flags);
877 	}
878 
879 	return 0;
880 }
881 
882 static const struct ethtool_ops vmxnet3_ethtool_ops = {
883 	.get_settings      = vmxnet3_get_settings,
884 	.get_drvinfo       = vmxnet3_get_drvinfo,
885 	.get_regs_len      = vmxnet3_get_regs_len,
886 	.get_regs          = vmxnet3_get_regs,
887 	.get_wol           = vmxnet3_get_wol,
888 	.set_wol           = vmxnet3_set_wol,
889 	.get_link          = ethtool_op_get_link,
890 	.get_coalesce      = vmxnet3_get_coalesce,
891 	.set_coalesce      = vmxnet3_set_coalesce,
892 	.get_strings       = vmxnet3_get_strings,
893 	.get_sset_count	   = vmxnet3_get_sset_count,
894 	.get_ethtool_stats = vmxnet3_get_ethtool_stats,
895 	.get_ringparam     = vmxnet3_get_ringparam,
896 	.set_ringparam     = vmxnet3_set_ringparam,
897 	.get_rxnfc         = vmxnet3_get_rxnfc,
898 #ifdef VMXNET3_RSS
899 	.get_rxfh_indir_size = vmxnet3_get_rss_indir_size,
900 	.get_rxfh          = vmxnet3_get_rss,
901 	.set_rxfh          = vmxnet3_set_rss,
902 #endif
903 };
904 
905 void vmxnet3_set_ethtool_ops(struct net_device *netdev)
906 {
907 	netdev->ethtool_ops = &vmxnet3_ethtool_ops;
908 }
909