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 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 		buf[j++] = tq->txdata_desc_size;
400 
401 		buf[j++] = VMXNET3_GET_ADDR_LO(tq->comp_ring.basePA);
402 		buf[j++] = VMXNET3_GET_ADDR_HI(tq->comp_ring.basePA);
403 		buf[j++] = tq->comp_ring.size;
404 		buf[j++] = tq->comp_ring.next2proc;
405 		buf[j++] = tq->comp_ring.gen;
406 
407 		buf[j++] = tq->stopped;
408 	}
409 
410 	buf[j++] = adapter->num_rx_queues;
411 	for (i = 0; i < adapter->num_rx_queues; i++) {
412 		struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
413 
414 		buf[j++] =  VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_RXPROD +
415 						  i * VMXNET3_REG_ALIGN);
416 		buf[j++] =  VMXNET3_READ_BAR0_REG(adapter, VMXNET3_REG_RXPROD2 +
417 						  i * VMXNET3_REG_ALIGN);
418 
419 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->rx_ring[0].basePA);
420 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->rx_ring[0].basePA);
421 		buf[j++] = rq->rx_ring[0].size;
422 		buf[j++] = rq->rx_ring[0].next2fill;
423 		buf[j++] = rq->rx_ring[0].next2comp;
424 		buf[j++] = rq->rx_ring[0].gen;
425 
426 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->rx_ring[1].basePA);
427 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->rx_ring[1].basePA);
428 		buf[j++] = rq->rx_ring[1].size;
429 		buf[j++] = rq->rx_ring[1].next2fill;
430 		buf[j++] = rq->rx_ring[1].next2comp;
431 		buf[j++] = rq->rx_ring[1].gen;
432 
433 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->data_ring.basePA);
434 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->data_ring.basePA);
435 		buf[j++] = rq->rx_ring[0].size;
436 		buf[j++] = rq->data_ring.desc_size;
437 
438 		buf[j++] = VMXNET3_GET_ADDR_LO(rq->comp_ring.basePA);
439 		buf[j++] = VMXNET3_GET_ADDR_HI(rq->comp_ring.basePA);
440 		buf[j++] = rq->comp_ring.size;
441 		buf[j++] = rq->comp_ring.next2proc;
442 		buf[j++] = rq->comp_ring.gen;
443 	}
444 }
445 
446 
447 static void
448 vmxnet3_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
449 {
450 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
451 
452 	wol->supported = WAKE_UCAST | WAKE_ARP | WAKE_MAGIC;
453 	wol->wolopts = adapter->wol;
454 }
455 
456 
457 static int
458 vmxnet3_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
459 {
460 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
461 
462 	if (wol->wolopts & (WAKE_PHY | WAKE_MCAST | WAKE_BCAST |
463 			    WAKE_MAGICSECURE)) {
464 		return -EOPNOTSUPP;
465 	}
466 
467 	adapter->wol = wol->wolopts;
468 
469 	device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
470 
471 	return 0;
472 }
473 
474 
475 static int
476 vmxnet3_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
477 {
478 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
479 
480 	ecmd->supported = SUPPORTED_10000baseT_Full | SUPPORTED_1000baseT_Full |
481 			  SUPPORTED_TP;
482 	ecmd->advertising = ADVERTISED_TP;
483 	ecmd->port = PORT_TP;
484 	ecmd->transceiver = XCVR_INTERNAL;
485 
486 	if (adapter->link_speed) {
487 		ethtool_cmd_speed_set(ecmd, adapter->link_speed);
488 		ecmd->duplex = DUPLEX_FULL;
489 	} else {
490 		ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
491 		ecmd->duplex = DUPLEX_UNKNOWN;
492 	}
493 	return 0;
494 }
495 
496 
497 static void
498 vmxnet3_get_ringparam(struct net_device *netdev,
499 		      struct ethtool_ringparam *param)
500 {
501 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
502 
503 	param->rx_max_pending = VMXNET3_RX_RING_MAX_SIZE;
504 	param->tx_max_pending = VMXNET3_TX_RING_MAX_SIZE;
505 	param->rx_mini_max_pending = VMXNET3_VERSION_GE_3(adapter) ?
506 		VMXNET3_RXDATA_DESC_MAX_SIZE : 0;
507 	param->rx_jumbo_max_pending = VMXNET3_RX_RING2_MAX_SIZE;
508 
509 	param->rx_pending = adapter->rx_ring_size;
510 	param->tx_pending = adapter->tx_ring_size;
511 	param->rx_mini_pending = VMXNET3_VERSION_GE_3(adapter) ?
512 		adapter->rxdata_desc_size : 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 	u16 new_rxdata_desc_size;
524 	u32 sz;
525 	int err = 0;
526 
527 	if (param->tx_pending == 0 || param->tx_pending >
528 						VMXNET3_TX_RING_MAX_SIZE)
529 		return -EINVAL;
530 
531 	if (param->rx_pending == 0 || param->rx_pending >
532 						VMXNET3_RX_RING_MAX_SIZE)
533 		return -EINVAL;
534 
535 	if (param->rx_jumbo_pending == 0 ||
536 	    param->rx_jumbo_pending > VMXNET3_RX_RING2_MAX_SIZE)
537 		return -EINVAL;
538 
539 	/* if adapter not yet initialized, do nothing */
540 	if (adapter->rx_buf_per_pkt == 0) {
541 		netdev_err(netdev, "adapter not completely initialized, "
542 			   "ring size cannot be changed yet\n");
543 		return -EOPNOTSUPP;
544 	}
545 
546 	if (VMXNET3_VERSION_GE_3(adapter)) {
547 		if (param->rx_mini_pending < 0 ||
548 		    param->rx_mini_pending > VMXNET3_RXDATA_DESC_MAX_SIZE) {
549 			return -EINVAL;
550 		}
551 	} else if (param->rx_mini_pending != 0) {
552 		return -EINVAL;
553 	}
554 
555 	/* round it up to a multiple of VMXNET3_RING_SIZE_ALIGN */
556 	new_tx_ring_size = (param->tx_pending + VMXNET3_RING_SIZE_MASK) &
557 							~VMXNET3_RING_SIZE_MASK;
558 	new_tx_ring_size = min_t(u32, new_tx_ring_size,
559 				 VMXNET3_TX_RING_MAX_SIZE);
560 	if (new_tx_ring_size > VMXNET3_TX_RING_MAX_SIZE || (new_tx_ring_size %
561 						VMXNET3_RING_SIZE_ALIGN) != 0)
562 		return -EINVAL;
563 
564 	/* ring0 has to be a multiple of
565 	 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
566 	 */
567 	sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
568 	new_rx_ring_size = (param->rx_pending + sz - 1) / sz * sz;
569 	new_rx_ring_size = min_t(u32, new_rx_ring_size,
570 				 VMXNET3_RX_RING_MAX_SIZE / sz * sz);
571 	if (new_rx_ring_size > VMXNET3_RX_RING_MAX_SIZE || (new_rx_ring_size %
572 							   sz) != 0)
573 		return -EINVAL;
574 
575 	/* ring2 has to be a multiple of VMXNET3_RING_SIZE_ALIGN */
576 	new_rx_ring2_size = (param->rx_jumbo_pending + VMXNET3_RING_SIZE_MASK) &
577 				~VMXNET3_RING_SIZE_MASK;
578 	new_rx_ring2_size = min_t(u32, new_rx_ring2_size,
579 				  VMXNET3_RX_RING2_MAX_SIZE);
580 
581 	/* rx data ring buffer size has to be a multiple of
582 	 * VMXNET3_RXDATA_DESC_SIZE_ALIGN
583 	 */
584 	new_rxdata_desc_size =
585 		(param->rx_mini_pending + VMXNET3_RXDATA_DESC_SIZE_MASK) &
586 		~VMXNET3_RXDATA_DESC_SIZE_MASK;
587 	new_rxdata_desc_size = min_t(u16, new_rxdata_desc_size,
588 				     VMXNET3_RXDATA_DESC_MAX_SIZE);
589 
590 	if (new_tx_ring_size == adapter->tx_ring_size &&
591 	    new_rx_ring_size == adapter->rx_ring_size &&
592 	    new_rx_ring2_size == adapter->rx_ring2_size &&
593 	    new_rxdata_desc_size == adapter->rxdata_desc_size) {
594 		return 0;
595 	}
596 
597 	/*
598 	 * Reset_work may be in the middle of resetting the device, wait for its
599 	 * completion.
600 	 */
601 	while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
602 		msleep(1);
603 
604 	if (netif_running(netdev)) {
605 		vmxnet3_quiesce_dev(adapter);
606 		vmxnet3_reset_dev(adapter);
607 
608 		/* recreate the rx queue and the tx queue based on the
609 		 * new sizes */
610 		vmxnet3_tq_destroy_all(adapter);
611 		vmxnet3_rq_destroy_all(adapter);
612 
613 		err = vmxnet3_create_queues(adapter, new_tx_ring_size,
614 					    new_rx_ring_size, new_rx_ring2_size,
615 					    adapter->txdata_desc_size,
616 					    new_rxdata_desc_size);
617 		if (err) {
618 			/* failed, most likely because of OOM, try default
619 			 * size */
620 			netdev_err(netdev, "failed to apply new sizes, "
621 				   "try the default ones\n");
622 			new_rx_ring_size = VMXNET3_DEF_RX_RING_SIZE;
623 			new_rx_ring2_size = VMXNET3_DEF_RX_RING2_SIZE;
624 			new_tx_ring_size = VMXNET3_DEF_TX_RING_SIZE;
625 			new_rxdata_desc_size = VMXNET3_VERSION_GE_3(adapter) ?
626 				VMXNET3_DEF_RXDATA_DESC_SIZE : 0;
627 
628 			err = vmxnet3_create_queues(adapter,
629 						    new_tx_ring_size,
630 						    new_rx_ring_size,
631 						    new_rx_ring2_size,
632 						    adapter->txdata_desc_size,
633 						    new_rxdata_desc_size);
634 			if (err) {
635 				netdev_err(netdev, "failed to create queues "
636 					   "with default sizes. Closing it\n");
637 				goto out;
638 			}
639 		}
640 
641 		err = vmxnet3_activate_dev(adapter);
642 		if (err)
643 			netdev_err(netdev, "failed to re-activate, error %d."
644 				   " Closing it\n", err);
645 	}
646 	adapter->tx_ring_size = new_tx_ring_size;
647 	adapter->rx_ring_size = new_rx_ring_size;
648 	adapter->rx_ring2_size = new_rx_ring2_size;
649 	adapter->rxdata_desc_size = new_rxdata_desc_size;
650 
651 out:
652 	clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
653 	if (err)
654 		vmxnet3_force_close(adapter);
655 
656 	return err;
657 }
658 
659 
660 static int
661 vmxnet3_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *info,
662 		  u32 *rules)
663 {
664 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
665 	switch (info->cmd) {
666 	case ETHTOOL_GRXRINGS:
667 		info->data = adapter->num_rx_queues;
668 		return 0;
669 	}
670 	return -EOPNOTSUPP;
671 }
672 
673 #ifdef VMXNET3_RSS
674 static u32
675 vmxnet3_get_rss_indir_size(struct net_device *netdev)
676 {
677 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
678 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
679 
680 	return rssConf->indTableSize;
681 }
682 
683 static int
684 vmxnet3_get_rss(struct net_device *netdev, u32 *p, u8 *key, u8 *hfunc)
685 {
686 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
687 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
688 	unsigned int n = rssConf->indTableSize;
689 
690 	if (hfunc)
691 		*hfunc = ETH_RSS_HASH_TOP;
692 	if (!p)
693 		return 0;
694 	while (n--)
695 		p[n] = rssConf->indTable[n];
696 	return 0;
697 
698 }
699 
700 static int
701 vmxnet3_set_rss(struct net_device *netdev, const u32 *p, const u8 *key,
702 		const u8 hfunc)
703 {
704 	unsigned int i;
705 	unsigned long flags;
706 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
707 	struct UPT1_RSSConf *rssConf = adapter->rss_conf;
708 
709 	/* We do not allow change in unsupported parameters */
710 	if (key ||
711 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
712 		return -EOPNOTSUPP;
713 	if (!p)
714 		return 0;
715 	for (i = 0; i < rssConf->indTableSize; i++)
716 		rssConf->indTable[i] = p[i];
717 
718 	spin_lock_irqsave(&adapter->cmd_lock, flags);
719 	VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
720 			       VMXNET3_CMD_UPDATE_RSSIDT);
721 	spin_unlock_irqrestore(&adapter->cmd_lock, flags);
722 
723 	return 0;
724 
725 }
726 #endif
727 
728 static int
729 vmxnet3_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
730 {
731 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
732 
733 	if (!VMXNET3_VERSION_GE_3(adapter))
734 		return -EOPNOTSUPP;
735 
736 	switch (adapter->coal_conf->coalMode) {
737 	case VMXNET3_COALESCE_DISABLED:
738 		/* struct ethtool_coalesce is already initialized to 0 */
739 		break;
740 	case VMXNET3_COALESCE_ADAPT:
741 		ec->use_adaptive_rx_coalesce = true;
742 		break;
743 	case VMXNET3_COALESCE_STATIC:
744 		ec->tx_max_coalesced_frames =
745 			adapter->coal_conf->coalPara.coalStatic.tx_comp_depth;
746 		ec->rx_max_coalesced_frames =
747 			adapter->coal_conf->coalPara.coalStatic.rx_depth;
748 		break;
749 	case VMXNET3_COALESCE_RBC: {
750 		u32 rbc_rate;
751 
752 		rbc_rate = adapter->coal_conf->coalPara.coalRbc.rbc_rate;
753 		ec->rx_coalesce_usecs = VMXNET3_COAL_RBC_USECS(rbc_rate);
754 	}
755 		break;
756 	default:
757 		return -EOPNOTSUPP;
758 	}
759 
760 	return 0;
761 }
762 
763 static int
764 vmxnet3_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
765 {
766 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
767 	struct Vmxnet3_DriverShared *shared = adapter->shared;
768 	union Vmxnet3_CmdInfo *cmdInfo = &shared->cu.cmdInfo;
769 	unsigned long flags;
770 
771 	if (!VMXNET3_VERSION_GE_3(adapter))
772 		return -EOPNOTSUPP;
773 
774 	if (ec->rx_coalesce_usecs_irq ||
775 	    ec->rx_max_coalesced_frames_irq ||
776 	    ec->tx_coalesce_usecs ||
777 	    ec->tx_coalesce_usecs_irq ||
778 	    ec->tx_max_coalesced_frames_irq ||
779 	    ec->stats_block_coalesce_usecs ||
780 	    ec->use_adaptive_tx_coalesce ||
781 	    ec->pkt_rate_low ||
782 	    ec->rx_coalesce_usecs_low ||
783 	    ec->rx_max_coalesced_frames_low ||
784 	    ec->tx_coalesce_usecs_low ||
785 	    ec->tx_max_coalesced_frames_low ||
786 	    ec->pkt_rate_high ||
787 	    ec->rx_coalesce_usecs_high ||
788 	    ec->rx_max_coalesced_frames_high ||
789 	    ec->tx_coalesce_usecs_high ||
790 	    ec->tx_max_coalesced_frames_high ||
791 	    ec->rate_sample_interval) {
792 		return -EINVAL;
793 	}
794 
795 	if ((ec->rx_coalesce_usecs == 0) &&
796 	    (ec->use_adaptive_rx_coalesce == 0) &&
797 	    (ec->tx_max_coalesced_frames == 0) &&
798 	    (ec->rx_max_coalesced_frames == 0)) {
799 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
800 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_DISABLED;
801 		goto done;
802 	}
803 
804 	if (ec->rx_coalesce_usecs != 0) {
805 		u32 rbc_rate;
806 
807 		if ((ec->use_adaptive_rx_coalesce != 0) ||
808 		    (ec->tx_max_coalesced_frames != 0) ||
809 		    (ec->rx_max_coalesced_frames != 0)) {
810 			return -EINVAL;
811 		}
812 
813 		rbc_rate = VMXNET3_COAL_RBC_RATE(ec->rx_coalesce_usecs);
814 		if (rbc_rate < VMXNET3_COAL_RBC_MIN_RATE ||
815 		    rbc_rate > VMXNET3_COAL_RBC_MAX_RATE) {
816 			return -EINVAL;
817 		}
818 
819 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
820 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_RBC;
821 		adapter->coal_conf->coalPara.coalRbc.rbc_rate = rbc_rate;
822 		goto done;
823 	}
824 
825 	if (ec->use_adaptive_rx_coalesce != 0) {
826 		if ((ec->rx_coalesce_usecs != 0) ||
827 		    (ec->tx_max_coalesced_frames != 0) ||
828 		    (ec->rx_max_coalesced_frames != 0)) {
829 			return -EINVAL;
830 		}
831 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
832 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_ADAPT;
833 		goto done;
834 	}
835 
836 	if ((ec->tx_max_coalesced_frames != 0) ||
837 	    (ec->rx_max_coalesced_frames != 0)) {
838 		if ((ec->rx_coalesce_usecs != 0) ||
839 		    (ec->use_adaptive_rx_coalesce != 0)) {
840 			return -EINVAL;
841 		}
842 
843 		if ((ec->tx_max_coalesced_frames >
844 		    VMXNET3_COAL_STATIC_MAX_DEPTH) ||
845 		    (ec->rx_max_coalesced_frames >
846 		     VMXNET3_COAL_STATIC_MAX_DEPTH)) {
847 			return -EINVAL;
848 		}
849 
850 		memset(adapter->coal_conf, 0, sizeof(*adapter->coal_conf));
851 		adapter->coal_conf->coalMode = VMXNET3_COALESCE_STATIC;
852 
853 		adapter->coal_conf->coalPara.coalStatic.tx_comp_depth =
854 			(ec->tx_max_coalesced_frames ?
855 			 ec->tx_max_coalesced_frames :
856 			 VMXNET3_COAL_STATIC_DEFAULT_DEPTH);
857 
858 		adapter->coal_conf->coalPara.coalStatic.rx_depth =
859 			(ec->rx_max_coalesced_frames ?
860 			 ec->rx_max_coalesced_frames :
861 			 VMXNET3_COAL_STATIC_DEFAULT_DEPTH);
862 
863 		adapter->coal_conf->coalPara.coalStatic.tx_depth =
864 			 VMXNET3_COAL_STATIC_DEFAULT_DEPTH;
865 		goto done;
866 	}
867 
868 done:
869 	adapter->default_coal_mode = false;
870 	if (netif_running(netdev)) {
871 		spin_lock_irqsave(&adapter->cmd_lock, flags);
872 		cmdInfo->varConf.confVer = 1;
873 		cmdInfo->varConf.confLen =
874 			cpu_to_le32(sizeof(*adapter->coal_conf));
875 		cmdInfo->varConf.confPA  = cpu_to_le64(adapter->coal_conf_pa);
876 		VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
877 				       VMXNET3_CMD_SET_COALESCE);
878 		spin_unlock_irqrestore(&adapter->cmd_lock, flags);
879 	}
880 
881 	return 0;
882 }
883 
884 static const struct ethtool_ops vmxnet3_ethtool_ops = {
885 	.get_settings      = vmxnet3_get_settings,
886 	.get_drvinfo       = vmxnet3_get_drvinfo,
887 	.get_regs_len      = vmxnet3_get_regs_len,
888 	.get_regs          = vmxnet3_get_regs,
889 	.get_wol           = vmxnet3_get_wol,
890 	.set_wol           = vmxnet3_set_wol,
891 	.get_link          = ethtool_op_get_link,
892 	.get_coalesce      = vmxnet3_get_coalesce,
893 	.set_coalesce      = vmxnet3_set_coalesce,
894 	.get_strings       = vmxnet3_get_strings,
895 	.get_sset_count	   = vmxnet3_get_sset_count,
896 	.get_ethtool_stats = vmxnet3_get_ethtool_stats,
897 	.get_ringparam     = vmxnet3_get_ringparam,
898 	.set_ringparam     = vmxnet3_set_ringparam,
899 	.get_rxnfc         = vmxnet3_get_rxnfc,
900 #ifdef VMXNET3_RSS
901 	.get_rxfh_indir_size = vmxnet3_get_rss_indir_size,
902 	.get_rxfh          = vmxnet3_get_rss,
903 	.set_rxfh          = vmxnet3_set_rss,
904 #endif
905 };
906 
907 void vmxnet3_set_ethtool_ops(struct net_device *netdev)
908 {
909 	netdev->ethtool_ops = &vmxnet3_ethtool_ops;
910 }
911