1 /*
2  * Linux driver for VMware's vmxnet3 ethernet NIC.
3  *
4  * Copyright (C) 2008-2009, 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: Shreyas Bhatewara <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 struct rtnl_link_stats64 *
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 	return stats;
165 }
166 
167 static int
168 vmxnet3_get_sset_count(struct net_device *netdev, int sset)
169 {
170 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
171 	switch (sset) {
172 	case ETH_SS_STATS:
173 		return (ARRAY_SIZE(vmxnet3_tq_dev_stats) +
174 			ARRAY_SIZE(vmxnet3_tq_driver_stats)) *
175 		       adapter->num_tx_queues +
176 		       (ARRAY_SIZE(vmxnet3_rq_dev_stats) +
177 			ARRAY_SIZE(vmxnet3_rq_driver_stats)) *
178 		       adapter->num_rx_queues +
179 			ARRAY_SIZE(vmxnet3_global_stats);
180 	default:
181 		return -EOPNOTSUPP;
182 	}
183 }
184 
185 
186 /* This is a version 2 of the vmxnet3 ethtool_regs which goes hand in hand with
187  * the version 2 of the vmxnet3 support for ethtool(8) --register-dump.
188  * Therefore, if any registers are added, removed or modified, then a version
189  * bump and a corresponding change in the vmxnet3 support for ethtool(8)
190  * --register-dump would be required.
191  */
192 static int
193 vmxnet3_get_regs_len(struct net_device *netdev)
194 {
195 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
196 
197 	return ((9 /* BAR1 registers */ +
198 		(1 + adapter->intr.num_intrs) +
199 		(1 + adapter->num_tx_queues * 17 /* Tx queue registers */) +
200 		(1 + adapter->num_rx_queues * 23 /* Rx queue registers */)) *
201 		sizeof(u32));
202 }
203 
204 
205 static void
206 vmxnet3_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
207 {
208 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
209 
210 	strlcpy(drvinfo->driver, vmxnet3_driver_name, sizeof(drvinfo->driver));
211 
212 	strlcpy(drvinfo->version, VMXNET3_DRIVER_VERSION_REPORT,
213 		sizeof(drvinfo->version));
214 
215 	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
216 		sizeof(drvinfo->bus_info));
217 }
218 
219 
220 static void
221 vmxnet3_get_strings(struct net_device *netdev, u32 stringset, u8 *buf)
222 {
223 	 struct vmxnet3_adapter *adapter = netdev_priv(netdev);
224 	if (stringset == ETH_SS_STATS) {
225 		int i, j;
226 		for (j = 0; j < adapter->num_tx_queues; j++) {
227 			for (i = 0; i < ARRAY_SIZE(vmxnet3_tq_dev_stats); i++) {
228 				memcpy(buf, vmxnet3_tq_dev_stats[i].desc,
229 				       ETH_GSTRING_LEN);
230 				buf += ETH_GSTRING_LEN;
231 			}
232 			for (i = 0; i < ARRAY_SIZE(vmxnet3_tq_driver_stats);
233 			     i++) {
234 				memcpy(buf, vmxnet3_tq_driver_stats[i].desc,
235 				       ETH_GSTRING_LEN);
236 				buf += ETH_GSTRING_LEN;
237 			}
238 		}
239 
240 		for (j = 0; j < adapter->num_rx_queues; j++) {
241 			for (i = 0; i < ARRAY_SIZE(vmxnet3_rq_dev_stats); i++) {
242 				memcpy(buf, vmxnet3_rq_dev_stats[i].desc,
243 				       ETH_GSTRING_LEN);
244 				buf += ETH_GSTRING_LEN;
245 			}
246 			for (i = 0; i < ARRAY_SIZE(vmxnet3_rq_driver_stats);
247 			     i++) {
248 				memcpy(buf, vmxnet3_rq_driver_stats[i].desc,
249 				       ETH_GSTRING_LEN);
250 				buf += ETH_GSTRING_LEN;
251 			}
252 		}
253 
254 		for (i = 0; i < ARRAY_SIZE(vmxnet3_global_stats); i++) {
255 			memcpy(buf, vmxnet3_global_stats[i].desc,
256 				ETH_GSTRING_LEN);
257 			buf += ETH_GSTRING_LEN;
258 		}
259 	}
260 }
261 
262 int vmxnet3_set_features(struct net_device *netdev, netdev_features_t features)
263 {
264 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
265 	unsigned long flags;
266 	netdev_features_t changed = features ^ netdev->features;
267 
268 	if (changed & (NETIF_F_RXCSUM | NETIF_F_LRO |
269 		       NETIF_F_HW_VLAN_CTAG_RX)) {
270 		if (features & NETIF_F_RXCSUM)
271 			adapter->shared->devRead.misc.uptFeatures |=
272 			UPT1_F_RXCSUM;
273 		else
274 			adapter->shared->devRead.misc.uptFeatures &=
275 			~UPT1_F_RXCSUM;
276 
277 		/* update hardware LRO capability accordingly */
278 		if (features & NETIF_F_LRO)
279 			adapter->shared->devRead.misc.uptFeatures |=
280 							UPT1_F_LRO;
281 		else
282 			adapter->shared->devRead.misc.uptFeatures &=
283 							~UPT1_F_LRO;
284 
285 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
286 			adapter->shared->devRead.misc.uptFeatures |=
287 			UPT1_F_RXVLAN;
288 		else
289 			adapter->shared->devRead.misc.uptFeatures &=
290 			~UPT1_F_RXVLAN;
291 
292 		spin_lock_irqsave(&adapter->cmd_lock, flags);
293 		VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
294 				       VMXNET3_CMD_UPDATE_FEATURE);
295 		spin_unlock_irqrestore(&adapter->cmd_lock, flags);
296 	}
297 	return 0;
298 }
299 
300 static void
301 vmxnet3_get_ethtool_stats(struct net_device *netdev,
302 			  struct ethtool_stats *stats, u64  *buf)
303 {
304 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
305 	unsigned long flags;
306 	u8 *base;
307 	int i;
308 	int j = 0;
309 
310 	spin_lock_irqsave(&adapter->cmd_lock, flags);
311 	VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_STATS);
312 	spin_unlock_irqrestore(&adapter->cmd_lock, flags);
313 
314 	/* this does assume each counter is 64-bit wide */
315 	for (j = 0; j < adapter->num_tx_queues; j++) {
316 		base = (u8 *)&adapter->tqd_start[j].stats;
317 		*buf++ = (u64)j;
318 		for (i = 1; i < ARRAY_SIZE(vmxnet3_tq_dev_stats); i++)
319 			*buf++ = *(u64 *)(base +
320 					  vmxnet3_tq_dev_stats[i].offset);
321 
322 		base = (u8 *)&adapter->tx_queue[j].stats;
323 		for (i = 0; i < ARRAY_SIZE(vmxnet3_tq_driver_stats); i++)
324 			*buf++ = *(u64 *)(base +
325 					  vmxnet3_tq_driver_stats[i].offset);
326 	}
327 
328 	for (j = 0; j < adapter->num_rx_queues; j++) {
329 		base = (u8 *)&adapter->rqd_start[j].stats;
330 		*buf++ = (u64) j;
331 		for (i = 1; i < ARRAY_SIZE(vmxnet3_rq_dev_stats); i++)
332 			*buf++ = *(u64 *)(base +
333 					  vmxnet3_rq_dev_stats[i].offset);
334 
335 		base = (u8 *)&adapter->rx_queue[j].stats;
336 		for (i = 0; i < ARRAY_SIZE(vmxnet3_rq_driver_stats); i++)
337 			*buf++ = *(u64 *)(base +
338 					  vmxnet3_rq_driver_stats[i].offset);
339 	}
340 
341 	base = (u8 *)adapter;
342 	for (i = 0; i < ARRAY_SIZE(vmxnet3_global_stats); i++)
343 		*buf++ = *(u64 *)(base + vmxnet3_global_stats[i].offset);
344 }
345 
346 
347 /* This is a version 2 of the vmxnet3 ethtool_regs which goes hand in hand with
348  * the version 2 of the vmxnet3 support for ethtool(8) --register-dump.
349  * Therefore, if any registers are added, removed or modified, then a version
350  * bump and a corresponding change in the vmxnet3 support for ethtool(8)
351  * --register-dump would be required.
352  */
353 static void
354 vmxnet3_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
355 {
356 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
357 	u32 *buf = p;
358 	int i = 0, j = 0;
359 
360 	memset(p, 0, vmxnet3_get_regs_len(netdev));
361 
362 	regs->version = 2;
363 
364 	/* Update vmxnet3_get_regs_len if we want to dump more registers */
365 
366 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
367 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
368 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_DSAL);
369 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_DSAH);
370 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
371 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
372 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
373 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
374 	buf[j++] = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ECR);
375 
376 	buf[j++] = adapter->intr.num_intrs;
377 	for (i = 0; i < adapter->intr.num_intrs; i++) {
378 		buf[j++] = VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_IMR
379 						 + i * VMXNET3_REG_ALIGN);
380 	}
381 
382 	buf[j++] = adapter->num_tx_queues;
383 	for (i = 0; i < adapter->num_tx_queues; i++) {
384 		struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
385 
386 		buf[j++] = VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_TXPROD +
387 						 i * VMXNET3_REG_ALIGN);
388 
389 		buf[j++] = VMXNET3_GET_ADDR_LO(tq->tx_ring.basePA);
390 		buf[j++] = VMXNET3_GET_ADDR_HI(tq->tx_ring.basePA);
391 		buf[j++] = tq->tx_ring.size;
392 		buf[j++] = tq->tx_ring.next2fill;
393 		buf[j++] = tq->tx_ring.next2comp;
394 		buf[j++] = tq->tx_ring.gen;
395 
396 		buf[j++] = VMXNET3_GET_ADDR_LO(tq->data_ring.basePA);
397 		buf[j++] = VMXNET3_GET_ADDR_HI(tq->data_ring.basePA);
398 		buf[j++] = tq->data_ring.size;
399 		/* transmit data ring buffer size */
400 		buf[j++] = VMXNET3_HDR_COPY_SIZE;
401 
402 		buf[j++] = VMXNET3_GET_ADDR_LO(tq->comp_ring.basePA);
403 		buf[j++] = VMXNET3_GET_ADDR_HI(tq->comp_ring.basePA);
404 		buf[j++] = tq->comp_ring.size;
405 		buf[j++] = tq->comp_ring.next2proc;
406 		buf[j++] = tq->comp_ring.gen;
407 
408 		buf[j++] = tq->stopped;
409 	}
410 
411 	buf[j++] = adapter->num_rx_queues;
412 	for (i = 0; i < adapter->num_rx_queues; i++) {
413 		struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
414 
415 		buf[j++] =  VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_RXPROD +
416 						  i * VMXNET3_REG_ALIGN);
417 		buf[j++] =  VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_RXPROD2 +
418 						  i * VMXNET3_REG_ALIGN);
419 
420 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->rx_ring[0].basePA);
421 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->rx_ring[0].basePA);
422 		buf[j++] = rq->rx_ring[0].size;
423 		buf[j++] = rq->rx_ring[0].next2fill;
424 		buf[j++] = rq->rx_ring[0].next2comp;
425 		buf[j++] = rq->rx_ring[0].gen;
426 
427 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->rx_ring[1].basePA);
428 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->rx_ring[1].basePA);
429 		buf[j++] = rq->rx_ring[1].size;
430 		buf[j++] = rq->rx_ring[1].next2fill;
431 		buf[j++] = rq->rx_ring[1].next2comp;
432 		buf[j++] = rq->rx_ring[1].gen;
433 
434 		/* receive data ring */
435 		buf[j++] = 0;
436 		buf[j++] = 0;
437 		buf[j++] = 0;
438 		buf[j++] = 0;
439 
440 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->comp_ring.basePA);
441 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->comp_ring.basePA);
442 		buf[j++] = rq->comp_ring.size;
443 		buf[j++] = rq->comp_ring.next2proc;
444 		buf[j++] = rq->comp_ring.gen;
445 	}
446 }
447 
448 
449 static void
450 vmxnet3_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
451 {
452 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
453 
454 	wol->supported = WAKE_UCAST | WAKE_ARP | WAKE_MAGIC;
455 	wol->wolopts = adapter->wol;
456 }
457 
458 
459 static int
460 vmxnet3_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
461 {
462 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
463 
464 	if (wol->wolopts & (WAKE_PHY | WAKE_MCAST | WAKE_BCAST |
465 			    WAKE_MAGICSECURE)) {
466 		return -EOPNOTSUPP;
467 	}
468 
469 	adapter->wol = wol->wolopts;
470 
471 	device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
472 
473 	return 0;
474 }
475 
476 
477 static int
478 vmxnet3_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
479 {
480 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
481 
482 	ecmd->supported = SUPPORTED_10000baseT_Full | SUPPORTED_1000baseT_Full |
483 			  SUPPORTED_TP;
484 	ecmd->advertising = ADVERTISED_TP;
485 	ecmd->port = PORT_TP;
486 	ecmd->transceiver = XCVR_INTERNAL;
487 
488 	if (adapter->link_speed) {
489 		ethtool_cmd_speed_set(ecmd, adapter->link_speed);
490 		ecmd->duplex = DUPLEX_FULL;
491 	} else {
492 		ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
493 		ecmd->duplex = DUPLEX_UNKNOWN;
494 	}
495 	return 0;
496 }
497 
498 
499 static void
500 vmxnet3_get_ringparam(struct net_device *netdev,
501 		      struct ethtool_ringparam *param)
502 {
503 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
504 
505 	param->rx_max_pending = VMXNET3_RX_RING_MAX_SIZE;
506 	param->tx_max_pending = VMXNET3_TX_RING_MAX_SIZE;
507 	param->rx_mini_max_pending = 0;
508 	param->rx_jumbo_max_pending = VMXNET3_RX_RING2_MAX_SIZE;
509 
510 	param->rx_pending = adapter->rx_ring_size;
511 	param->tx_pending = adapter->tx_ring_size;
512 	param->rx_mini_pending = 0;
513 	param->rx_jumbo_pending = adapter->rx_ring2_size;
514 }
515 
516 
517 static int
518 vmxnet3_set_ringparam(struct net_device *netdev,
519 		      struct ethtool_ringparam *param)
520 {
521 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
522 	u32 new_tx_ring_size, new_rx_ring_size, new_rx_ring2_size;
523 	u32 sz;
524 	int err = 0;
525 
526 	if (param->tx_pending == 0 || param->tx_pending >
527 						VMXNET3_TX_RING_MAX_SIZE)
528 		return -EINVAL;
529 
530 	if (param->rx_pending == 0 || param->rx_pending >
531 						VMXNET3_RX_RING_MAX_SIZE)
532 		return -EINVAL;
533 
534 	if (param->rx_jumbo_pending == 0 ||
535 	    param->rx_jumbo_pending > VMXNET3_RX_RING2_MAX_SIZE)
536 		return -EINVAL;
537 
538 	/* if adapter not yet initialized, do nothing */
539 	if (adapter->rx_buf_per_pkt == 0) {
540 		netdev_err(netdev, "adapter not completely initialized, "
541 			   "ring size cannot be changed yet\n");
542 		return -EOPNOTSUPP;
543 	}
544 
545 	/* round it up to a multiple of VMXNET3_RING_SIZE_ALIGN */
546 	new_tx_ring_size = (param->tx_pending + VMXNET3_RING_SIZE_MASK) &
547 							~VMXNET3_RING_SIZE_MASK;
548 	new_tx_ring_size = min_t(u32, new_tx_ring_size,
549 				 VMXNET3_TX_RING_MAX_SIZE);
550 	if (new_tx_ring_size > VMXNET3_TX_RING_MAX_SIZE || (new_tx_ring_size %
551 						VMXNET3_RING_SIZE_ALIGN) != 0)
552 		return -EINVAL;
553 
554 	/* ring0 has to be a multiple of
555 	 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
556 	 */
557 	sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
558 	new_rx_ring_size = (param->rx_pending + sz - 1) / sz * sz;
559 	new_rx_ring_size = min_t(u32, new_rx_ring_size,
560 				 VMXNET3_RX_RING_MAX_SIZE / sz * sz);
561 	if (new_rx_ring_size > VMXNET3_RX_RING_MAX_SIZE || (new_rx_ring_size %
562 							   sz) != 0)
563 		return -EINVAL;
564 
565 	/* ring2 has to be a multiple of VMXNET3_RING_SIZE_ALIGN */
566 	new_rx_ring2_size = (param->rx_jumbo_pending + VMXNET3_RING_SIZE_MASK) &
567 				~VMXNET3_RING_SIZE_MASK;
568 	new_rx_ring2_size = min_t(u32, new_rx_ring2_size,
569 				  VMXNET3_RX_RING2_MAX_SIZE);
570 
571 	if (new_tx_ring_size == adapter->tx_ring_size &&
572 	    new_rx_ring_size == adapter->rx_ring_size &&
573 	    new_rx_ring2_size == adapter->rx_ring2_size) {
574 		return 0;
575 	}
576 
577 	/*
578 	 * Reset_work may be in the middle of resetting the device, wait for its
579 	 * completion.
580 	 */
581 	while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
582 		msleep(1);
583 
584 	if (netif_running(netdev)) {
585 		vmxnet3_quiesce_dev(adapter);
586 		vmxnet3_reset_dev(adapter);
587 
588 		/* recreate the rx queue and the tx queue based on the
589 		 * new sizes */
590 		vmxnet3_tq_destroy_all(adapter);
591 		vmxnet3_rq_destroy_all(adapter);
592 
593 		err = vmxnet3_create_queues(adapter, new_tx_ring_size,
594 			new_rx_ring_size, new_rx_ring2_size);
595 
596 		if (err) {
597 			/* failed, most likely because of OOM, try default
598 			 * size */
599 			netdev_err(netdev, "failed to apply new sizes, "
600 				   "try the default ones\n");
601 			new_rx_ring_size = VMXNET3_DEF_RX_RING_SIZE;
602 			new_rx_ring2_size = VMXNET3_DEF_RX_RING2_SIZE;
603 			new_tx_ring_size = VMXNET3_DEF_TX_RING_SIZE;
604 			err = vmxnet3_create_queues(adapter,
605 						    new_tx_ring_size,
606 						    new_rx_ring_size,
607 						    new_rx_ring2_size);
608 			if (err) {
609 				netdev_err(netdev, "failed to create queues "
610 					   "with default sizes. Closing it\n");
611 				goto out;
612 			}
613 		}
614 
615 		err = vmxnet3_activate_dev(adapter);
616 		if (err)
617 			netdev_err(netdev, "failed to re-activate, error %d."
618 				   " Closing it\n", err);
619 	}
620 	adapter->tx_ring_size = new_tx_ring_size;
621 	adapter->rx_ring_size = new_rx_ring_size;
622 	adapter->rx_ring2_size = new_rx_ring2_size;
623 
624 out:
625 	clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
626 	if (err)
627 		vmxnet3_force_close(adapter);
628 
629 	return err;
630 }
631 
632 
633 static int
634 vmxnet3_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
635 		  u32 *rules)
636 {
637 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
638 	switch (info->cmd) {
639 	case ETHTOOL_GRXRINGS:
640 		info->data = adapter->num_rx_queues;
641 		return 0;
642 	}
643 	return -EOPNOTSUPP;
644 }
645 
646 #ifdef VMXNET3_RSS
647 static u32
648 vmxnet3_get_rss_indir_size(struct net_device *netdev)
649 {
650 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
651 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
652 
653 	return rssConf->indTableSize;
654 }
655 
656 static int
657 vmxnet3_get_rss(struct net_device *netdev, u32 *p, u8 *key, u8 *hfunc)
658 {
659 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
660 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
661 	unsigned int n = rssConf->indTableSize;
662 
663 	if (hfunc)
664 		*hfunc = ETH_RSS_HASH_TOP;
665 	if (!p)
666 		return 0;
667 	while (n--)
668 		p[n] = rssConf->indTable[n];
669 	return 0;
670 
671 }
672 
673 static int
674 vmxnet3_set_rss(struct net_device *netdev, const u32 *p, const u8 *key,
675 		const u8 hfunc)
676 {
677 	unsigned int i;
678 	unsigned long flags;
679 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
680 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
681 
682 	/* We do not allow change in unsupported parameters */
683 	if (key ||
684 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
685 		return -EOPNOTSUPP;
686 	if (!p)
687 		return 0;
688 	for (i = 0; i < rssConf->indTableSize; i++)
689 		rssConf->indTable[i] = p[i];
690 
691 	spin_lock_irqsave(&adapter->cmd_lock, flags);
692 	VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
693 			       VMXNET3_CMD_UPDATE_RSSIDT);
694 	spin_unlock_irqrestore(&adapter->cmd_lock, flags);
695 
696 	return 0;
697 
698 }
699 #endif
700 
701 static const struct ethtool_ops vmxnet3_ethtool_ops = {
702 	.get_settings      = vmxnet3_get_settings,
703 	.get_drvinfo       = vmxnet3_get_drvinfo,
704 	.get_regs_len      = vmxnet3_get_regs_len,
705 	.get_regs          = vmxnet3_get_regs,
706 	.get_wol           = vmxnet3_get_wol,
707 	.set_wol           = vmxnet3_set_wol,
708 	.get_link          = ethtool_op_get_link,
709 	.get_strings       = vmxnet3_get_strings,
710 	.get_sset_count	   = vmxnet3_get_sset_count,
711 	.get_ethtool_stats = vmxnet3_get_ethtool_stats,
712 	.get_ringparam     = vmxnet3_get_ringparam,
713 	.set_ringparam     = vmxnet3_set_ringparam,
714 	.get_rxnfc         = vmxnet3_get_rxnfc,
715 #ifdef VMXNET3_RSS
716 	.get_rxfh_indir_size = vmxnet3_get_rss_indir_size,
717 	.get_rxfh          = vmxnet3_get_rss,
718 	.set_rxfh          = vmxnet3_set_rss,
719 #endif
720 };
721 
722 void vmxnet3_set_ethtool_ops(struct net_device *netdev)
723 {
724 	netdev->ethtool_ops = &vmxnet3_ethtool_ops;
725 }
726