2 * Linux driver for VMware's vmxnet3 ethernet NIC.
4 * Copyright (C) 2008-2009, VMware, Inc. All Rights Reserved.
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.
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
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.
20 * The full GNU General Public License is included in this distribution in
21 * the file called "COPYING".
23 * Maintained by: Shreyas Bhatewara <pv-drivers@vmware.com>
28 #include "vmxnet3_int.h"
30 struct vmxnet3_stat_desc
{
31 char desc
[ETH_GSTRING_LEN
];
37 vmxnet3_get_rx_csum(struct net_device
*netdev
)
39 struct vmxnet3_adapter
*adapter
= netdev_priv(netdev
);
40 return adapter
->rxcsum
;
45 vmxnet3_set_rx_csum(struct net_device
*netdev
, u32 val
)
47 struct vmxnet3_adapter
*adapter
= netdev_priv(netdev
);
49 if (adapter
->rxcsum
!= val
) {
50 adapter
->rxcsum
= val
;
51 if (netif_running(netdev
)) {
54 &adapter
->shared
->devRead
.misc
.uptFeatures
,
58 &adapter
->shared
->devRead
.misc
.uptFeatures
,
61 VMXNET3_WRITE_BAR1_REG(adapter
, VMXNET3_REG_CMD
,
62 VMXNET3_CMD_UPDATE_FEATURE
);
69 /* per tq stats maintained by the device */
70 static const struct vmxnet3_stat_desc
71 vmxnet3_tq_dev_stats
[] = {
72 /* description, offset */
73 { "TSO pkts tx", offsetof(struct UPT1_TxStats
, TSOPktsTxOK
) },
74 { "TSO bytes tx", offsetof(struct UPT1_TxStats
, TSOBytesTxOK
) },
75 { "ucast pkts tx", offsetof(struct UPT1_TxStats
, ucastPktsTxOK
) },
76 { "ucast bytes tx", offsetof(struct UPT1_TxStats
, ucastBytesTxOK
) },
77 { "mcast pkts tx", offsetof(struct UPT1_TxStats
, mcastPktsTxOK
) },
78 { "mcast bytes tx", offsetof(struct UPT1_TxStats
, mcastBytesTxOK
) },
79 { "bcast pkts tx", offsetof(struct UPT1_TxStats
, bcastPktsTxOK
) },
80 { "bcast bytes tx", offsetof(struct UPT1_TxStats
, bcastBytesTxOK
) },
81 { "pkts tx err", offsetof(struct UPT1_TxStats
, pktsTxError
) },
82 { "pkts tx discard", offsetof(struct UPT1_TxStats
, pktsTxDiscard
) },
85 /* per tq stats maintained by the driver */
86 static const struct vmxnet3_stat_desc
87 vmxnet3_tq_driver_stats
[] = {
88 /* description, offset */
89 {"drv dropped tx total", offsetof(struct vmxnet3_tq_driver_stats
,
91 { " too many frags", offsetof(struct vmxnet3_tq_driver_stats
,
92 drop_too_many_frags
) },
93 { " giant hdr", offsetof(struct vmxnet3_tq_driver_stats
,
94 drop_oversized_hdr
) },
95 { " hdr err", offsetof(struct vmxnet3_tq_driver_stats
,
96 drop_hdr_inspect_err
) },
97 { " tso", offsetof(struct vmxnet3_tq_driver_stats
,
99 { "ring full", offsetof(struct vmxnet3_tq_driver_stats
,
101 { "pkts linearized", offsetof(struct vmxnet3_tq_driver_stats
,
103 { "hdr cloned", offsetof(struct vmxnet3_tq_driver_stats
,
105 { "giant hdr", offsetof(struct vmxnet3_tq_driver_stats
,
109 /* per rq stats maintained by the device */
110 static const struct vmxnet3_stat_desc
111 vmxnet3_rq_dev_stats
[] = {
112 { "LRO pkts rx", offsetof(struct UPT1_RxStats
, LROPktsRxOK
) },
113 { "LRO byte rx", offsetof(struct UPT1_RxStats
, LROBytesRxOK
) },
114 { "ucast pkts rx", offsetof(struct UPT1_RxStats
, ucastPktsRxOK
) },
115 { "ucast bytes rx", offsetof(struct UPT1_RxStats
, ucastBytesRxOK
) },
116 { "mcast pkts rx", offsetof(struct UPT1_RxStats
, mcastPktsRxOK
) },
117 { "mcast bytes rx", offsetof(struct UPT1_RxStats
, mcastBytesRxOK
) },
118 { "bcast pkts rx", offsetof(struct UPT1_RxStats
, bcastPktsRxOK
) },
119 { "bcast bytes rx", offsetof(struct UPT1_RxStats
, bcastBytesRxOK
) },
120 { "pkts rx out of buf", offsetof(struct UPT1_RxStats
, pktsRxOutOfBuf
) },
121 { "pkts rx err", offsetof(struct UPT1_RxStats
, pktsRxError
) },
124 /* per rq stats maintained by the driver */
125 static const struct vmxnet3_stat_desc
126 vmxnet3_rq_driver_stats
[] = {
127 /* description, offset */
128 { "drv dropped rx total", offsetof(struct vmxnet3_rq_driver_stats
,
130 { " err", offsetof(struct vmxnet3_rq_driver_stats
,
132 { " fcs", offsetof(struct vmxnet3_rq_driver_stats
,
134 { "rx buf alloc fail", offsetof(struct vmxnet3_rq_driver_stats
,
135 rx_buf_alloc_failure
) },
138 /* gloabl stats maintained by the driver */
139 static const struct vmxnet3_stat_desc
140 vmxnet3_global_stats
[] = {
141 /* description, offset */
142 { "tx timeout count", offsetof(struct vmxnet3_adapter
,
147 struct net_device_stats
*
148 vmxnet3_get_stats(struct net_device
*netdev
)
150 struct vmxnet3_adapter
*adapter
;
151 struct vmxnet3_tq_driver_stats
*drvTxStats
;
152 struct vmxnet3_rq_driver_stats
*drvRxStats
;
153 struct UPT1_TxStats
*devTxStats
;
154 struct UPT1_RxStats
*devRxStats
;
155 struct net_device_stats
*net_stats
= &netdev
->stats
;
157 adapter
= netdev_priv(netdev
);
159 /* Collect the dev stats into the shared area */
160 VMXNET3_WRITE_BAR1_REG(adapter
, VMXNET3_REG_CMD
, VMXNET3_CMD_GET_STATS
);
162 /* Assuming that we have a single queue device */
163 devTxStats
= &adapter
->tqd_start
->stats
;
164 devRxStats
= &adapter
->rqd_start
->stats
;
166 /* Get access to the driver stats per queue */
167 drvTxStats
= &adapter
->tx_queue
.stats
;
168 drvRxStats
= &adapter
->rx_queue
.stats
;
170 memset(net_stats
, 0, sizeof(*net_stats
));
172 net_stats
->rx_packets
= devRxStats
->ucastPktsRxOK
+
173 devRxStats
->mcastPktsRxOK
+
174 devRxStats
->bcastPktsRxOK
;
176 net_stats
->tx_packets
= devTxStats
->ucastPktsTxOK
+
177 devTxStats
->mcastPktsTxOK
+
178 devTxStats
->bcastPktsTxOK
;
180 net_stats
->rx_bytes
= devRxStats
->ucastBytesRxOK
+
181 devRxStats
->mcastBytesRxOK
+
182 devRxStats
->bcastBytesRxOK
;
184 net_stats
->tx_bytes
= devTxStats
->ucastBytesTxOK
+
185 devTxStats
->mcastBytesTxOK
+
186 devTxStats
->bcastBytesTxOK
;
188 net_stats
->rx_errors
= devRxStats
->pktsRxError
;
189 net_stats
->tx_errors
= devTxStats
->pktsTxError
;
190 net_stats
->rx_dropped
= drvRxStats
->drop_total
;
191 net_stats
->tx_dropped
= drvTxStats
->drop_total
;
192 net_stats
->multicast
= devRxStats
->mcastPktsRxOK
;
198 vmxnet3_get_sset_count(struct net_device
*netdev
, int sset
)
202 return ARRAY_SIZE(vmxnet3_tq_dev_stats
) +
203 ARRAY_SIZE(vmxnet3_tq_driver_stats
) +
204 ARRAY_SIZE(vmxnet3_rq_dev_stats
) +
205 ARRAY_SIZE(vmxnet3_rq_driver_stats
) +
206 ARRAY_SIZE(vmxnet3_global_stats
);
214 vmxnet3_get_regs_len(struct net_device
*netdev
)
216 return 20 * sizeof(u32
);
221 vmxnet3_get_drvinfo(struct net_device
*netdev
, struct ethtool_drvinfo
*drvinfo
)
223 struct vmxnet3_adapter
*adapter
= netdev_priv(netdev
);
225 strlcpy(drvinfo
->driver
, vmxnet3_driver_name
, sizeof(drvinfo
->driver
));
226 drvinfo
->driver
[sizeof(drvinfo
->driver
) - 1] = '\0';
228 strlcpy(drvinfo
->version
, VMXNET3_DRIVER_VERSION_REPORT
,
229 sizeof(drvinfo
->version
));
230 drvinfo
->driver
[sizeof(drvinfo
->version
) - 1] = '\0';
232 strlcpy(drvinfo
->fw_version
, "N/A", sizeof(drvinfo
->fw_version
));
233 drvinfo
->fw_version
[sizeof(drvinfo
->fw_version
) - 1] = '\0';
235 strlcpy(drvinfo
->bus_info
, pci_name(adapter
->pdev
),
236 ETHTOOL_BUSINFO_LEN
);
237 drvinfo
->n_stats
= vmxnet3_get_sset_count(netdev
, ETH_SS_STATS
);
238 drvinfo
->testinfo_len
= 0;
239 drvinfo
->eedump_len
= 0;
240 drvinfo
->regdump_len
= vmxnet3_get_regs_len(netdev
);
245 vmxnet3_get_strings(struct net_device
*netdev
, u32 stringset
, u8
*buf
)
247 if (stringset
== ETH_SS_STATS
) {
250 for (i
= 0; i
< ARRAY_SIZE(vmxnet3_tq_dev_stats
); i
++) {
251 memcpy(buf
, vmxnet3_tq_dev_stats
[i
].desc
,
253 buf
+= ETH_GSTRING_LEN
;
255 for (i
= 0; i
< ARRAY_SIZE(vmxnet3_tq_driver_stats
); i
++) {
256 memcpy(buf
, vmxnet3_tq_driver_stats
[i
].desc
,
258 buf
+= ETH_GSTRING_LEN
;
260 for (i
= 0; i
< ARRAY_SIZE(vmxnet3_rq_dev_stats
); i
++) {
261 memcpy(buf
, vmxnet3_rq_dev_stats
[i
].desc
,
263 buf
+= ETH_GSTRING_LEN
;
265 for (i
= 0; i
< ARRAY_SIZE(vmxnet3_rq_driver_stats
); i
++) {
266 memcpy(buf
, vmxnet3_rq_driver_stats
[i
].desc
,
268 buf
+= ETH_GSTRING_LEN
;
270 for (i
= 0; i
< ARRAY_SIZE(vmxnet3_global_stats
); i
++) {
271 memcpy(buf
, vmxnet3_global_stats
[i
].desc
,
273 buf
+= ETH_GSTRING_LEN
;
279 vmxnet3_set_flags(struct net_device
*netdev
, u32 data
)
281 struct vmxnet3_adapter
*adapter
= netdev_priv(netdev
);
282 u8 lro_requested
= (data
& ETH_FLAG_LRO
) == 0 ? 0 : 1;
283 u8 lro_present
= (netdev
->features
& NETIF_F_LRO
) == 0 ? 0 : 1;
285 if (data
& ~ETH_FLAG_LRO
)
288 if (lro_requested
^ lro_present
) {
289 /* toggle the LRO feature*/
290 netdev
->features
^= NETIF_F_LRO
;
292 /* update harware LRO capability accordingly */
294 adapter
->shared
->devRead
.misc
.uptFeatures
&= UPT1_F_LRO
;
296 adapter
->shared
->devRead
.misc
.uptFeatures
&=
298 VMXNET3_WRITE_BAR1_REG(adapter
, VMXNET3_REG_CMD
,
299 VMXNET3_CMD_UPDATE_FEATURE
);
305 vmxnet3_get_ethtool_stats(struct net_device
*netdev
,
306 struct ethtool_stats
*stats
, u64
*buf
)
308 struct vmxnet3_adapter
*adapter
= netdev_priv(netdev
);
312 VMXNET3_WRITE_BAR1_REG(adapter
, VMXNET3_REG_CMD
, VMXNET3_CMD_GET_STATS
);
314 /* this does assume each counter is 64-bit wide */
316 base
= (u8
*)&adapter
->tqd_start
->stats
;
317 for (i
= 0; i
< ARRAY_SIZE(vmxnet3_tq_dev_stats
); i
++)
318 *buf
++ = *(u64
*)(base
+ vmxnet3_tq_dev_stats
[i
].offset
);
320 base
= (u8
*)&adapter
->tx_queue
.stats
;
321 for (i
= 0; i
< ARRAY_SIZE(vmxnet3_tq_driver_stats
); i
++)
322 *buf
++ = *(u64
*)(base
+ vmxnet3_tq_driver_stats
[i
].offset
);
324 base
= (u8
*)&adapter
->rqd_start
->stats
;
325 for (i
= 0; i
< ARRAY_SIZE(vmxnet3_rq_dev_stats
); i
++)
326 *buf
++ = *(u64
*)(base
+ vmxnet3_rq_dev_stats
[i
].offset
);
328 base
= (u8
*)&adapter
->rx_queue
.stats
;
329 for (i
= 0; i
< ARRAY_SIZE(vmxnet3_rq_driver_stats
); i
++)
330 *buf
++ = *(u64
*)(base
+ vmxnet3_rq_driver_stats
[i
].offset
);
332 base
= (u8
*)adapter
;
333 for (i
= 0; i
< ARRAY_SIZE(vmxnet3_global_stats
); i
++)
334 *buf
++ = *(u64
*)(base
+ vmxnet3_global_stats
[i
].offset
);
339 vmxnet3_get_regs(struct net_device
*netdev
, struct ethtool_regs
*regs
, void *p
)
341 struct vmxnet3_adapter
*adapter
= netdev_priv(netdev
);
344 memset(p
, 0, vmxnet3_get_regs_len(netdev
));
348 /* Update vmxnet3_get_regs_len if we want to dump more registers */
350 /* make each ring use multiple of 16 bytes */
351 buf
[0] = adapter
->tx_queue
.tx_ring
.next2fill
;
352 buf
[1] = adapter
->tx_queue
.tx_ring
.next2comp
;
353 buf
[2] = adapter
->tx_queue
.tx_ring
.gen
;
356 buf
[4] = adapter
->tx_queue
.comp_ring
.next2proc
;
357 buf
[5] = adapter
->tx_queue
.comp_ring
.gen
;
358 buf
[6] = adapter
->tx_queue
.stopped
;
361 buf
[8] = adapter
->rx_queue
.rx_ring
[0].next2fill
;
362 buf
[9] = adapter
->rx_queue
.rx_ring
[0].next2comp
;
363 buf
[10] = adapter
->rx_queue
.rx_ring
[0].gen
;
366 buf
[12] = adapter
->rx_queue
.rx_ring
[1].next2fill
;
367 buf
[13] = adapter
->rx_queue
.rx_ring
[1].next2comp
;
368 buf
[14] = adapter
->rx_queue
.rx_ring
[1].gen
;
371 buf
[16] = adapter
->rx_queue
.comp_ring
.next2proc
;
372 buf
[17] = adapter
->rx_queue
.comp_ring
.gen
;
379 vmxnet3_get_wol(struct net_device
*netdev
, struct ethtool_wolinfo
*wol
)
381 struct vmxnet3_adapter
*adapter
= netdev_priv(netdev
);
383 wol
->supported
= WAKE_UCAST
| WAKE_ARP
| WAKE_MAGIC
;
384 wol
->wolopts
= adapter
->wol
;
389 vmxnet3_set_wol(struct net_device
*netdev
, struct ethtool_wolinfo
*wol
)
391 struct vmxnet3_adapter
*adapter
= netdev_priv(netdev
);
393 if (wol
->wolopts
& (WAKE_PHY
| WAKE_MCAST
| WAKE_BCAST
|
398 adapter
->wol
= wol
->wolopts
;
400 device_set_wakeup_enable(&adapter
->pdev
->dev
, adapter
->wol
);
407 vmxnet3_get_settings(struct net_device
*netdev
, struct ethtool_cmd
*ecmd
)
409 struct vmxnet3_adapter
*adapter
= netdev_priv(netdev
);
411 ecmd
->supported
= SUPPORTED_10000baseT_Full
| SUPPORTED_1000baseT_Full
|
413 ecmd
->advertising
= ADVERTISED_TP
;
414 ecmd
->port
= PORT_TP
;
415 ecmd
->transceiver
= XCVR_INTERNAL
;
417 if (adapter
->link_speed
) {
418 ecmd
->speed
= adapter
->link_speed
;
419 ecmd
->duplex
= DUPLEX_FULL
;
429 vmxnet3_get_ringparam(struct net_device
*netdev
,
430 struct ethtool_ringparam
*param
)
432 struct vmxnet3_adapter
*adapter
= netdev_priv(netdev
);
434 param
->rx_max_pending
= VMXNET3_RX_RING_MAX_SIZE
;
435 param
->tx_max_pending
= VMXNET3_TX_RING_MAX_SIZE
;
436 param
->rx_mini_max_pending
= 0;
437 param
->rx_jumbo_max_pending
= 0;
439 param
->rx_pending
= adapter
->rx_queue
.rx_ring
[0].size
;
440 param
->tx_pending
= adapter
->tx_queue
.tx_ring
.size
;
441 param
->rx_mini_pending
= 0;
442 param
->rx_jumbo_pending
= 0;
447 vmxnet3_set_ringparam(struct net_device
*netdev
,
448 struct ethtool_ringparam
*param
)
450 struct vmxnet3_adapter
*adapter
= netdev_priv(netdev
);
451 u32 new_tx_ring_size
, new_rx_ring_size
;
455 if (param
->tx_pending
== 0 || param
->tx_pending
>
456 VMXNET3_TX_RING_MAX_SIZE
)
459 if (param
->rx_pending
== 0 || param
->rx_pending
>
460 VMXNET3_RX_RING_MAX_SIZE
)
464 /* round it up to a multiple of VMXNET3_RING_SIZE_ALIGN */
465 new_tx_ring_size
= (param
->tx_pending
+ VMXNET3_RING_SIZE_MASK
) &
466 ~VMXNET3_RING_SIZE_MASK
;
467 new_tx_ring_size
= min_t(u32
, new_tx_ring_size
,
468 VMXNET3_TX_RING_MAX_SIZE
);
469 if (new_tx_ring_size
> VMXNET3_TX_RING_MAX_SIZE
|| (new_tx_ring_size
%
470 VMXNET3_RING_SIZE_ALIGN
) != 0)
473 /* ring0 has to be a multiple of
474 * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
476 sz
= adapter
->rx_buf_per_pkt
* VMXNET3_RING_SIZE_ALIGN
;
477 new_rx_ring_size
= (param
->rx_pending
+ sz
- 1) / sz
* sz
;
478 new_rx_ring_size
= min_t(u32
, new_rx_ring_size
,
479 VMXNET3_RX_RING_MAX_SIZE
/ sz
* sz
);
480 if (new_rx_ring_size
> VMXNET3_RX_RING_MAX_SIZE
|| (new_rx_ring_size
%
484 if (new_tx_ring_size
== adapter
->tx_queue
.tx_ring
.size
&&
485 new_rx_ring_size
== adapter
->rx_queue
.rx_ring
[0].size
) {
490 * Reset_work may be in the middle of resetting the device, wait for its
493 while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING
, &adapter
->state
))
496 if (netif_running(netdev
)) {
497 vmxnet3_quiesce_dev(adapter
);
498 vmxnet3_reset_dev(adapter
);
500 /* recreate the rx queue and the tx queue based on the
502 vmxnet3_tq_destroy(&adapter
->tx_queue
, adapter
);
503 vmxnet3_rq_destroy(&adapter
->rx_queue
, adapter
);
505 err
= vmxnet3_create_queues(adapter
, new_tx_ring_size
,
506 new_rx_ring_size
, VMXNET3_DEF_RX_RING_SIZE
);
508 /* failed, most likely because of OOM, try default
510 printk(KERN_ERR
"%s: failed to apply new sizes, try the"
511 " default ones\n", netdev
->name
);
512 err
= vmxnet3_create_queues(adapter
,
513 VMXNET3_DEF_TX_RING_SIZE
,
514 VMXNET3_DEF_RX_RING_SIZE
,
515 VMXNET3_DEF_RX_RING_SIZE
);
517 printk(KERN_ERR
"%s: failed to create queues "
518 "with default sizes. Closing it\n",
524 err
= vmxnet3_activate_dev(adapter
);
526 printk(KERN_ERR
"%s: failed to re-activate, error %d."
527 " Closing it\n", netdev
->name
, err
);
531 clear_bit(VMXNET3_STATE_BIT_RESETTING
, &adapter
->state
);
533 vmxnet3_force_close(adapter
);
539 static struct ethtool_ops vmxnet3_ethtool_ops
= {
540 .get_settings
= vmxnet3_get_settings
,
541 .get_drvinfo
= vmxnet3_get_drvinfo
,
542 .get_regs_len
= vmxnet3_get_regs_len
,
543 .get_regs
= vmxnet3_get_regs
,
544 .get_wol
= vmxnet3_get_wol
,
545 .set_wol
= vmxnet3_set_wol
,
546 .get_link
= ethtool_op_get_link
,
547 .get_rx_csum
= vmxnet3_get_rx_csum
,
548 .set_rx_csum
= vmxnet3_set_rx_csum
,
549 .get_tx_csum
= ethtool_op_get_tx_csum
,
550 .set_tx_csum
= ethtool_op_set_tx_hw_csum
,
551 .get_sg
= ethtool_op_get_sg
,
552 .set_sg
= ethtool_op_set_sg
,
553 .get_tso
= ethtool_op_get_tso
,
554 .set_tso
= ethtool_op_set_tso
,
555 .get_strings
= vmxnet3_get_strings
,
556 .get_flags
= ethtool_op_get_flags
,
557 .set_flags
= vmxnet3_set_flags
,
558 .get_sset_count
= vmxnet3_get_sset_count
,
559 .get_ethtool_stats
= vmxnet3_get_ethtool_stats
,
560 .get_ringparam
= vmxnet3_get_ringparam
,
561 .set_ringparam
= vmxnet3_set_ringparam
,
564 void vmxnet3_set_ethtool_ops(struct net_device
*netdev
)
566 SET_ETHTOOL_OPS(netdev
, &vmxnet3_ethtool_ops
);