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