2 * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/workqueue.h>
28 #include <linux/pci.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
32 #include <linux/if_ether.h>
33 #include <linux/if_vlan.h>
34 #include <linux/ethtool.h>
37 #include <linux/ipv6.h>
38 #include <linux/tcp.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/prefetch.h>
41 #include <net/ip6_checksum.h>
43 #include "cq_enet_desc.h"
45 #include "vnic_intr.h"
46 #include "vnic_stats.h"
53 #define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
54 #define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
55 #define MAX_TSO (1 << 16)
56 #define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
58 #define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
59 #define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
60 #define PCI_DEVICE_ID_CISCO_VIC_ENET_VF 0x0071 /* enet SRIOV VF */
62 /* Supported devices */
63 static DEFINE_PCI_DEVICE_TABLE(enic_id_table
) = {
64 { PCI_VDEVICE(CISCO
, PCI_DEVICE_ID_CISCO_VIC_ENET
) },
65 { PCI_VDEVICE(CISCO
, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN
) },
66 { PCI_VDEVICE(CISCO
, PCI_DEVICE_ID_CISCO_VIC_ENET_VF
) },
67 { 0, } /* end of table */
70 MODULE_DESCRIPTION(DRV_DESCRIPTION
);
71 MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
72 MODULE_LICENSE("GPL");
73 MODULE_VERSION(DRV_VERSION
);
74 MODULE_DEVICE_TABLE(pci
, enic_id_table
);
77 char name
[ETH_GSTRING_LEN
];
81 #define ENIC_TX_STAT(stat) \
82 { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
83 #define ENIC_RX_STAT(stat) \
84 { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
86 static const struct enic_stat enic_tx_stats
[] = {
87 ENIC_TX_STAT(tx_frames_ok
),
88 ENIC_TX_STAT(tx_unicast_frames_ok
),
89 ENIC_TX_STAT(tx_multicast_frames_ok
),
90 ENIC_TX_STAT(tx_broadcast_frames_ok
),
91 ENIC_TX_STAT(tx_bytes_ok
),
92 ENIC_TX_STAT(tx_unicast_bytes_ok
),
93 ENIC_TX_STAT(tx_multicast_bytes_ok
),
94 ENIC_TX_STAT(tx_broadcast_bytes_ok
),
95 ENIC_TX_STAT(tx_drops
),
96 ENIC_TX_STAT(tx_errors
),
100 static const struct enic_stat enic_rx_stats
[] = {
101 ENIC_RX_STAT(rx_frames_ok
),
102 ENIC_RX_STAT(rx_frames_total
),
103 ENIC_RX_STAT(rx_unicast_frames_ok
),
104 ENIC_RX_STAT(rx_multicast_frames_ok
),
105 ENIC_RX_STAT(rx_broadcast_frames_ok
),
106 ENIC_RX_STAT(rx_bytes_ok
),
107 ENIC_RX_STAT(rx_unicast_bytes_ok
),
108 ENIC_RX_STAT(rx_multicast_bytes_ok
),
109 ENIC_RX_STAT(rx_broadcast_bytes_ok
),
110 ENIC_RX_STAT(rx_drop
),
111 ENIC_RX_STAT(rx_no_bufs
),
112 ENIC_RX_STAT(rx_errors
),
113 ENIC_RX_STAT(rx_rss
),
114 ENIC_RX_STAT(rx_crc_errors
),
115 ENIC_RX_STAT(rx_frames_64
),
116 ENIC_RX_STAT(rx_frames_127
),
117 ENIC_RX_STAT(rx_frames_255
),
118 ENIC_RX_STAT(rx_frames_511
),
119 ENIC_RX_STAT(rx_frames_1023
),
120 ENIC_RX_STAT(rx_frames_1518
),
121 ENIC_RX_STAT(rx_frames_to_max
),
124 static const unsigned int enic_n_tx_stats
= ARRAY_SIZE(enic_tx_stats
);
125 static const unsigned int enic_n_rx_stats
= ARRAY_SIZE(enic_rx_stats
);
127 int enic_is_dynamic(struct enic
*enic
)
129 return enic
->pdev
->device
== PCI_DEVICE_ID_CISCO_VIC_ENET_DYN
;
132 int enic_sriov_enabled(struct enic
*enic
)
134 return (enic
->priv_flags
& ENIC_SRIOV_ENABLED
) ? 1 : 0;
137 static int enic_is_sriov_vf(struct enic
*enic
)
139 return enic
->pdev
->device
== PCI_DEVICE_ID_CISCO_VIC_ENET_VF
;
142 int enic_is_valid_vf(struct enic
*enic
, int vf
)
144 #ifdef CONFIG_PCI_IOV
145 return vf
>= 0 && vf
< enic
->num_vfs
;
151 static inline unsigned int enic_cq_rq(struct enic
*enic
, unsigned int rq
)
156 static inline unsigned int enic_cq_wq(struct enic
*enic
, unsigned int wq
)
158 return enic
->rq_count
+ wq
;
161 static inline unsigned int enic_legacy_io_intr(void)
166 static inline unsigned int enic_legacy_err_intr(void)
171 static inline unsigned int enic_legacy_notify_intr(void)
176 static inline unsigned int enic_msix_rq_intr(struct enic
*enic
, unsigned int rq
)
178 return enic
->cq
[enic_cq_rq(enic
, rq
)].interrupt_offset
;
181 static inline unsigned int enic_msix_wq_intr(struct enic
*enic
, unsigned int wq
)
183 return enic
->cq
[enic_cq_wq(enic
, wq
)].interrupt_offset
;
186 static inline unsigned int enic_msix_err_intr(struct enic
*enic
)
188 return enic
->rq_count
+ enic
->wq_count
;
191 static inline unsigned int enic_msix_notify_intr(struct enic
*enic
)
193 return enic
->rq_count
+ enic
->wq_count
+ 1;
196 static int enic_get_settings(struct net_device
*netdev
,
197 struct ethtool_cmd
*ecmd
)
199 struct enic
*enic
= netdev_priv(netdev
);
201 ecmd
->supported
= (SUPPORTED_10000baseT_Full
| SUPPORTED_FIBRE
);
202 ecmd
->advertising
= (ADVERTISED_10000baseT_Full
| ADVERTISED_FIBRE
);
203 ecmd
->port
= PORT_FIBRE
;
204 ecmd
->transceiver
= XCVR_EXTERNAL
;
206 if (netif_carrier_ok(netdev
)) {
207 ethtool_cmd_speed_set(ecmd
, vnic_dev_port_speed(enic
->vdev
));
208 ecmd
->duplex
= DUPLEX_FULL
;
210 ethtool_cmd_speed_set(ecmd
, -1);
214 ecmd
->autoneg
= AUTONEG_DISABLE
;
219 static void enic_get_drvinfo(struct net_device
*netdev
,
220 struct ethtool_drvinfo
*drvinfo
)
222 struct enic
*enic
= netdev_priv(netdev
);
223 struct vnic_devcmd_fw_info
*fw_info
;
225 enic_dev_fw_info(enic
, &fw_info
);
227 strlcpy(drvinfo
->driver
, DRV_NAME
, sizeof(drvinfo
->driver
));
228 strlcpy(drvinfo
->version
, DRV_VERSION
, sizeof(drvinfo
->version
));
229 strlcpy(drvinfo
->fw_version
, fw_info
->fw_version
,
230 sizeof(drvinfo
->fw_version
));
231 strlcpy(drvinfo
->bus_info
, pci_name(enic
->pdev
),
232 sizeof(drvinfo
->bus_info
));
235 static void enic_get_strings(struct net_device
*netdev
, u32 stringset
, u8
*data
)
241 for (i
= 0; i
< enic_n_tx_stats
; i
++) {
242 memcpy(data
, enic_tx_stats
[i
].name
, ETH_GSTRING_LEN
);
243 data
+= ETH_GSTRING_LEN
;
245 for (i
= 0; i
< enic_n_rx_stats
; i
++) {
246 memcpy(data
, enic_rx_stats
[i
].name
, ETH_GSTRING_LEN
);
247 data
+= ETH_GSTRING_LEN
;
253 static int enic_get_sset_count(struct net_device
*netdev
, int sset
)
257 return enic_n_tx_stats
+ enic_n_rx_stats
;
263 static void enic_get_ethtool_stats(struct net_device
*netdev
,
264 struct ethtool_stats
*stats
, u64
*data
)
266 struct enic
*enic
= netdev_priv(netdev
);
267 struct vnic_stats
*vstats
;
270 enic_dev_stats_dump(enic
, &vstats
);
272 for (i
= 0; i
< enic_n_tx_stats
; i
++)
273 *(data
++) = ((u64
*)&vstats
->tx
)[enic_tx_stats
[i
].offset
];
274 for (i
= 0; i
< enic_n_rx_stats
; i
++)
275 *(data
++) = ((u64
*)&vstats
->rx
)[enic_rx_stats
[i
].offset
];
278 static u32
enic_get_msglevel(struct net_device
*netdev
)
280 struct enic
*enic
= netdev_priv(netdev
);
281 return enic
->msg_enable
;
284 static void enic_set_msglevel(struct net_device
*netdev
, u32 value
)
286 struct enic
*enic
= netdev_priv(netdev
);
287 enic
->msg_enable
= value
;
290 static int enic_get_coalesce(struct net_device
*netdev
,
291 struct ethtool_coalesce
*ecmd
)
293 struct enic
*enic
= netdev_priv(netdev
);
295 ecmd
->tx_coalesce_usecs
= enic
->tx_coalesce_usecs
;
296 ecmd
->rx_coalesce_usecs
= enic
->rx_coalesce_usecs
;
301 static int enic_set_coalesce(struct net_device
*netdev
,
302 struct ethtool_coalesce
*ecmd
)
304 struct enic
*enic
= netdev_priv(netdev
);
305 u32 tx_coalesce_usecs
;
306 u32 rx_coalesce_usecs
;
307 unsigned int i
, intr
;
309 tx_coalesce_usecs
= min_t(u32
, ecmd
->tx_coalesce_usecs
,
310 vnic_dev_get_intr_coal_timer_max(enic
->vdev
));
311 rx_coalesce_usecs
= min_t(u32
, ecmd
->rx_coalesce_usecs
,
312 vnic_dev_get_intr_coal_timer_max(enic
->vdev
));
314 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
315 case VNIC_DEV_INTR_MODE_INTX
:
316 if (tx_coalesce_usecs
!= rx_coalesce_usecs
)
319 intr
= enic_legacy_io_intr();
320 vnic_intr_coalescing_timer_set(&enic
->intr
[intr
],
323 case VNIC_DEV_INTR_MODE_MSI
:
324 if (tx_coalesce_usecs
!= rx_coalesce_usecs
)
327 vnic_intr_coalescing_timer_set(&enic
->intr
[0],
330 case VNIC_DEV_INTR_MODE_MSIX
:
331 for (i
= 0; i
< enic
->wq_count
; i
++) {
332 intr
= enic_msix_wq_intr(enic
, i
);
333 vnic_intr_coalescing_timer_set(&enic
->intr
[intr
],
337 for (i
= 0; i
< enic
->rq_count
; i
++) {
338 intr
= enic_msix_rq_intr(enic
, i
);
339 vnic_intr_coalescing_timer_set(&enic
->intr
[intr
],
348 enic
->tx_coalesce_usecs
= tx_coalesce_usecs
;
349 enic
->rx_coalesce_usecs
= rx_coalesce_usecs
;
354 static const struct ethtool_ops enic_ethtool_ops
= {
355 .get_settings
= enic_get_settings
,
356 .get_drvinfo
= enic_get_drvinfo
,
357 .get_msglevel
= enic_get_msglevel
,
358 .set_msglevel
= enic_set_msglevel
,
359 .get_link
= ethtool_op_get_link
,
360 .get_strings
= enic_get_strings
,
361 .get_sset_count
= enic_get_sset_count
,
362 .get_ethtool_stats
= enic_get_ethtool_stats
,
363 .get_coalesce
= enic_get_coalesce
,
364 .set_coalesce
= enic_set_coalesce
,
367 static void enic_free_wq_buf(struct vnic_wq
*wq
, struct vnic_wq_buf
*buf
)
369 struct enic
*enic
= vnic_dev_priv(wq
->vdev
);
372 pci_unmap_single(enic
->pdev
, buf
->dma_addr
,
373 buf
->len
, PCI_DMA_TODEVICE
);
375 pci_unmap_page(enic
->pdev
, buf
->dma_addr
,
376 buf
->len
, PCI_DMA_TODEVICE
);
379 dev_kfree_skb_any(buf
->os_buf
);
382 static void enic_wq_free_buf(struct vnic_wq
*wq
,
383 struct cq_desc
*cq_desc
, struct vnic_wq_buf
*buf
, void *opaque
)
385 enic_free_wq_buf(wq
, buf
);
388 static int enic_wq_service(struct vnic_dev
*vdev
, struct cq_desc
*cq_desc
,
389 u8 type
, u16 q_number
, u16 completed_index
, void *opaque
)
391 struct enic
*enic
= vnic_dev_priv(vdev
);
393 spin_lock(&enic
->wq_lock
[q_number
]);
395 vnic_wq_service(&enic
->wq
[q_number
], cq_desc
,
396 completed_index
, enic_wq_free_buf
,
399 if (netif_queue_stopped(enic
->netdev
) &&
400 vnic_wq_desc_avail(&enic
->wq
[q_number
]) >=
401 (MAX_SKB_FRAGS
+ ENIC_DESC_MAX_SPLITS
))
402 netif_wake_queue(enic
->netdev
);
404 spin_unlock(&enic
->wq_lock
[q_number
]);
409 static void enic_log_q_error(struct enic
*enic
)
414 for (i
= 0; i
< enic
->wq_count
; i
++) {
415 error_status
= vnic_wq_error_status(&enic
->wq
[i
]);
417 netdev_err(enic
->netdev
, "WQ[%d] error_status %d\n",
421 for (i
= 0; i
< enic
->rq_count
; i
++) {
422 error_status
= vnic_rq_error_status(&enic
->rq
[i
]);
424 netdev_err(enic
->netdev
, "RQ[%d] error_status %d\n",
429 static void enic_msglvl_check(struct enic
*enic
)
431 u32 msg_enable
= vnic_dev_msg_lvl(enic
->vdev
);
433 if (msg_enable
!= enic
->msg_enable
) {
434 netdev_info(enic
->netdev
, "msg lvl changed from 0x%x to 0x%x\n",
435 enic
->msg_enable
, msg_enable
);
436 enic
->msg_enable
= msg_enable
;
440 static void enic_mtu_check(struct enic
*enic
)
442 u32 mtu
= vnic_dev_mtu(enic
->vdev
);
443 struct net_device
*netdev
= enic
->netdev
;
445 if (mtu
&& mtu
!= enic
->port_mtu
) {
446 enic
->port_mtu
= mtu
;
447 if (enic_is_dynamic(enic
) || enic_is_sriov_vf(enic
)) {
448 mtu
= max_t(int, ENIC_MIN_MTU
,
449 min_t(int, ENIC_MAX_MTU
, mtu
));
450 if (mtu
!= netdev
->mtu
)
451 schedule_work(&enic
->change_mtu_work
);
453 if (mtu
< netdev
->mtu
)
455 "interface MTU (%d) set higher "
456 "than switch port MTU (%d)\n",
462 static void enic_link_check(struct enic
*enic
)
464 int link_status
= vnic_dev_link_status(enic
->vdev
);
465 int carrier_ok
= netif_carrier_ok(enic
->netdev
);
467 if (link_status
&& !carrier_ok
) {
468 netdev_info(enic
->netdev
, "Link UP\n");
469 netif_carrier_on(enic
->netdev
);
470 } else if (!link_status
&& carrier_ok
) {
471 netdev_info(enic
->netdev
, "Link DOWN\n");
472 netif_carrier_off(enic
->netdev
);
476 static void enic_notify_check(struct enic
*enic
)
478 enic_msglvl_check(enic
);
479 enic_mtu_check(enic
);
480 enic_link_check(enic
);
483 #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
485 static irqreturn_t
enic_isr_legacy(int irq
, void *data
)
487 struct net_device
*netdev
= data
;
488 struct enic
*enic
= netdev_priv(netdev
);
489 unsigned int io_intr
= enic_legacy_io_intr();
490 unsigned int err_intr
= enic_legacy_err_intr();
491 unsigned int notify_intr
= enic_legacy_notify_intr();
494 vnic_intr_mask(&enic
->intr
[io_intr
]);
496 pba
= vnic_intr_legacy_pba(enic
->legacy_pba
);
498 vnic_intr_unmask(&enic
->intr
[io_intr
]);
499 return IRQ_NONE
; /* not our interrupt */
502 if (ENIC_TEST_INTR(pba
, notify_intr
)) {
503 vnic_intr_return_all_credits(&enic
->intr
[notify_intr
]);
504 enic_notify_check(enic
);
507 if (ENIC_TEST_INTR(pba
, err_intr
)) {
508 vnic_intr_return_all_credits(&enic
->intr
[err_intr
]);
509 enic_log_q_error(enic
);
510 /* schedule recovery from WQ/RQ error */
511 schedule_work(&enic
->reset
);
515 if (ENIC_TEST_INTR(pba
, io_intr
)) {
516 if (napi_schedule_prep(&enic
->napi
[0]))
517 __napi_schedule(&enic
->napi
[0]);
519 vnic_intr_unmask(&enic
->intr
[io_intr
]);
525 static irqreturn_t
enic_isr_msi(int irq
, void *data
)
527 struct enic
*enic
= data
;
529 /* With MSI, there is no sharing of interrupts, so this is
530 * our interrupt and there is no need to ack it. The device
531 * is not providing per-vector masking, so the OS will not
532 * write to PCI config space to mask/unmask the interrupt.
533 * We're using mask_on_assertion for MSI, so the device
534 * automatically masks the interrupt when the interrupt is
535 * generated. Later, when exiting polling, the interrupt
536 * will be unmasked (see enic_poll).
538 * Also, the device uses the same PCIe Traffic Class (TC)
539 * for Memory Write data and MSI, so there are no ordering
540 * issues; the MSI will always arrive at the Root Complex
541 * _after_ corresponding Memory Writes (i.e. descriptor
545 napi_schedule(&enic
->napi
[0]);
550 static irqreturn_t
enic_isr_msix_rq(int irq
, void *data
)
552 struct napi_struct
*napi
= data
;
554 /* schedule NAPI polling for RQ cleanup */
560 static irqreturn_t
enic_isr_msix_wq(int irq
, void *data
)
562 struct enic
*enic
= data
;
563 unsigned int cq
= enic_cq_wq(enic
, 0);
564 unsigned int intr
= enic_msix_wq_intr(enic
, 0);
565 unsigned int wq_work_to_do
= -1; /* no limit */
566 unsigned int wq_work_done
;
568 wq_work_done
= vnic_cq_service(&enic
->cq
[cq
],
569 wq_work_to_do
, enic_wq_service
, NULL
);
571 vnic_intr_return_credits(&enic
->intr
[intr
],
574 1 /* reset intr timer */);
579 static irqreturn_t
enic_isr_msix_err(int irq
, void *data
)
581 struct enic
*enic
= data
;
582 unsigned int intr
= enic_msix_err_intr(enic
);
584 vnic_intr_return_all_credits(&enic
->intr
[intr
]);
586 enic_log_q_error(enic
);
588 /* schedule recovery from WQ/RQ error */
589 schedule_work(&enic
->reset
);
594 static irqreturn_t
enic_isr_msix_notify(int irq
, void *data
)
596 struct enic
*enic
= data
;
597 unsigned int intr
= enic_msix_notify_intr(enic
);
599 vnic_intr_return_all_credits(&enic
->intr
[intr
]);
600 enic_notify_check(enic
);
605 static inline void enic_queue_wq_skb_cont(struct enic
*enic
,
606 struct vnic_wq
*wq
, struct sk_buff
*skb
,
607 unsigned int len_left
, int loopback
)
609 const skb_frag_t
*frag
;
611 /* Queue additional data fragments */
612 for (frag
= skb_shinfo(skb
)->frags
; len_left
; frag
++) {
613 len_left
-= skb_frag_size(frag
);
614 enic_queue_wq_desc_cont(wq
, skb
,
615 skb_frag_dma_map(&enic
->pdev
->dev
,
616 frag
, 0, skb_frag_size(frag
),
619 (len_left
== 0), /* EOP? */
624 static inline void enic_queue_wq_skb_vlan(struct enic
*enic
,
625 struct vnic_wq
*wq
, struct sk_buff
*skb
,
626 int vlan_tag_insert
, unsigned int vlan_tag
, int loopback
)
628 unsigned int head_len
= skb_headlen(skb
);
629 unsigned int len_left
= skb
->len
- head_len
;
630 int eop
= (len_left
== 0);
632 /* Queue the main skb fragment. The fragments are no larger
633 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
634 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
635 * per fragment is queued.
637 enic_queue_wq_desc(wq
, skb
,
638 pci_map_single(enic
->pdev
, skb
->data
,
639 head_len
, PCI_DMA_TODEVICE
),
641 vlan_tag_insert
, vlan_tag
,
645 enic_queue_wq_skb_cont(enic
, wq
, skb
, len_left
, loopback
);
648 static inline void enic_queue_wq_skb_csum_l4(struct enic
*enic
,
649 struct vnic_wq
*wq
, struct sk_buff
*skb
,
650 int vlan_tag_insert
, unsigned int vlan_tag
, int loopback
)
652 unsigned int head_len
= skb_headlen(skb
);
653 unsigned int len_left
= skb
->len
- head_len
;
654 unsigned int hdr_len
= skb_checksum_start_offset(skb
);
655 unsigned int csum_offset
= hdr_len
+ skb
->csum_offset
;
656 int eop
= (len_left
== 0);
658 /* Queue the main skb fragment. The fragments are no larger
659 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
660 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
661 * per fragment is queued.
663 enic_queue_wq_desc_csum_l4(wq
, skb
,
664 pci_map_single(enic
->pdev
, skb
->data
,
665 head_len
, PCI_DMA_TODEVICE
),
669 vlan_tag_insert
, vlan_tag
,
673 enic_queue_wq_skb_cont(enic
, wq
, skb
, len_left
, loopback
);
676 static inline void enic_queue_wq_skb_tso(struct enic
*enic
,
677 struct vnic_wq
*wq
, struct sk_buff
*skb
, unsigned int mss
,
678 int vlan_tag_insert
, unsigned int vlan_tag
, int loopback
)
680 unsigned int frag_len_left
= skb_headlen(skb
);
681 unsigned int len_left
= skb
->len
- frag_len_left
;
682 unsigned int hdr_len
= skb_transport_offset(skb
) + tcp_hdrlen(skb
);
683 int eop
= (len_left
== 0);
686 unsigned int offset
= 0;
689 /* Preload TCP csum field with IP pseudo hdr calculated
690 * with IP length set to zero. HW will later add in length
691 * to each TCP segment resulting from the TSO.
694 if (skb
->protocol
== cpu_to_be16(ETH_P_IP
)) {
695 ip_hdr(skb
)->check
= 0;
696 tcp_hdr(skb
)->check
= ~csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
697 ip_hdr(skb
)->daddr
, 0, IPPROTO_TCP
, 0);
698 } else if (skb
->protocol
== cpu_to_be16(ETH_P_IPV6
)) {
699 tcp_hdr(skb
)->check
= ~csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
700 &ipv6_hdr(skb
)->daddr
, 0, IPPROTO_TCP
, 0);
703 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
704 * for the main skb fragment
706 while (frag_len_left
) {
707 len
= min(frag_len_left
, (unsigned int)WQ_ENET_MAX_DESC_LEN
);
708 dma_addr
= pci_map_single(enic
->pdev
, skb
->data
+ offset
,
709 len
, PCI_DMA_TODEVICE
);
710 enic_queue_wq_desc_tso(wq
, skb
,
714 vlan_tag_insert
, vlan_tag
,
715 eop
&& (len
== frag_len_left
), loopback
);
716 frag_len_left
-= len
;
723 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
724 * for additional data fragments
726 for (frag
= skb_shinfo(skb
)->frags
; len_left
; frag
++) {
727 len_left
-= skb_frag_size(frag
);
728 frag_len_left
= skb_frag_size(frag
);
731 while (frag_len_left
) {
732 len
= min(frag_len_left
,
733 (unsigned int)WQ_ENET_MAX_DESC_LEN
);
734 dma_addr
= skb_frag_dma_map(&enic
->pdev
->dev
, frag
,
737 enic_queue_wq_desc_cont(wq
, skb
,
741 (len
== frag_len_left
), /* EOP? */
743 frag_len_left
-= len
;
749 static inline void enic_queue_wq_skb(struct enic
*enic
,
750 struct vnic_wq
*wq
, struct sk_buff
*skb
)
752 unsigned int mss
= skb_shinfo(skb
)->gso_size
;
753 unsigned int vlan_tag
= 0;
754 int vlan_tag_insert
= 0;
757 if (vlan_tx_tag_present(skb
)) {
758 /* VLAN tag from trunking driver */
760 vlan_tag
= vlan_tx_tag_get(skb
);
761 } else if (enic
->loop_enable
) {
762 vlan_tag
= enic
->loop_tag
;
767 enic_queue_wq_skb_tso(enic
, wq
, skb
, mss
,
768 vlan_tag_insert
, vlan_tag
, loopback
);
769 else if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
770 enic_queue_wq_skb_csum_l4(enic
, wq
, skb
,
771 vlan_tag_insert
, vlan_tag
, loopback
);
773 enic_queue_wq_skb_vlan(enic
, wq
, skb
,
774 vlan_tag_insert
, vlan_tag
, loopback
);
777 /* netif_tx_lock held, process context with BHs disabled, or BH */
778 static netdev_tx_t
enic_hard_start_xmit(struct sk_buff
*skb
,
779 struct net_device
*netdev
)
781 struct enic
*enic
= netdev_priv(netdev
);
782 struct vnic_wq
*wq
= &enic
->wq
[0];
790 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
791 * which is very likely. In the off chance it's going to take
792 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
795 if (skb_shinfo(skb
)->gso_size
== 0 &&
796 skb_shinfo(skb
)->nr_frags
+ 1 > ENIC_NON_TSO_MAX_DESC
&&
797 skb_linearize(skb
)) {
802 spin_lock_irqsave(&enic
->wq_lock
[0], flags
);
804 if (vnic_wq_desc_avail(wq
) <
805 skb_shinfo(skb
)->nr_frags
+ ENIC_DESC_MAX_SPLITS
) {
806 netif_stop_queue(netdev
);
807 /* This is a hard error, log it */
808 netdev_err(netdev
, "BUG! Tx ring full when queue awake!\n");
809 spin_unlock_irqrestore(&enic
->wq_lock
[0], flags
);
810 return NETDEV_TX_BUSY
;
813 enic_queue_wq_skb(enic
, wq
, skb
);
815 if (vnic_wq_desc_avail(wq
) < MAX_SKB_FRAGS
+ ENIC_DESC_MAX_SPLITS
)
816 netif_stop_queue(netdev
);
818 spin_unlock_irqrestore(&enic
->wq_lock
[0], flags
);
823 /* dev_base_lock rwlock held, nominally process context */
824 static struct rtnl_link_stats64
*enic_get_stats(struct net_device
*netdev
,
825 struct rtnl_link_stats64
*net_stats
)
827 struct enic
*enic
= netdev_priv(netdev
);
828 struct vnic_stats
*stats
;
830 enic_dev_stats_dump(enic
, &stats
);
832 net_stats
->tx_packets
= stats
->tx
.tx_frames_ok
;
833 net_stats
->tx_bytes
= stats
->tx
.tx_bytes_ok
;
834 net_stats
->tx_errors
= stats
->tx
.tx_errors
;
835 net_stats
->tx_dropped
= stats
->tx
.tx_drops
;
837 net_stats
->rx_packets
= stats
->rx
.rx_frames_ok
;
838 net_stats
->rx_bytes
= stats
->rx
.rx_bytes_ok
;
839 net_stats
->rx_errors
= stats
->rx
.rx_errors
;
840 net_stats
->multicast
= stats
->rx
.rx_multicast_frames_ok
;
841 net_stats
->rx_over_errors
= enic
->rq_truncated_pkts
;
842 net_stats
->rx_crc_errors
= enic
->rq_bad_fcs
;
843 net_stats
->rx_dropped
= stats
->rx
.rx_no_bufs
+ stats
->rx
.rx_drop
;
848 void enic_reset_addr_lists(struct enic
*enic
)
855 static int enic_set_mac_addr(struct net_device
*netdev
, char *addr
)
857 struct enic
*enic
= netdev_priv(netdev
);
859 if (enic_is_dynamic(enic
) || enic_is_sriov_vf(enic
)) {
860 if (!is_valid_ether_addr(addr
) && !is_zero_ether_addr(addr
))
861 return -EADDRNOTAVAIL
;
863 if (!is_valid_ether_addr(addr
))
864 return -EADDRNOTAVAIL
;
867 memcpy(netdev
->dev_addr
, addr
, netdev
->addr_len
);
872 static int enic_set_mac_address_dynamic(struct net_device
*netdev
, void *p
)
874 struct enic
*enic
= netdev_priv(netdev
);
875 struct sockaddr
*saddr
= p
;
876 char *addr
= saddr
->sa_data
;
879 if (netif_running(enic
->netdev
)) {
880 err
= enic_dev_del_station_addr(enic
);
885 err
= enic_set_mac_addr(netdev
, addr
);
889 if (netif_running(enic
->netdev
)) {
890 err
= enic_dev_add_station_addr(enic
);
898 static int enic_set_mac_address(struct net_device
*netdev
, void *p
)
900 struct sockaddr
*saddr
= p
;
901 char *addr
= saddr
->sa_data
;
902 struct enic
*enic
= netdev_priv(netdev
);
905 err
= enic_dev_del_station_addr(enic
);
909 err
= enic_set_mac_addr(netdev
, addr
);
913 return enic_dev_add_station_addr(enic
);
916 static void enic_update_multicast_addr_list(struct enic
*enic
)
918 struct net_device
*netdev
= enic
->netdev
;
919 struct netdev_hw_addr
*ha
;
920 unsigned int mc_count
= netdev_mc_count(netdev
);
921 u8 mc_addr
[ENIC_MULTICAST_PERFECT_FILTERS
][ETH_ALEN
];
924 if (mc_count
> ENIC_MULTICAST_PERFECT_FILTERS
) {
925 netdev_warn(netdev
, "Registering only %d out of %d "
926 "multicast addresses\n",
927 ENIC_MULTICAST_PERFECT_FILTERS
, mc_count
);
928 mc_count
= ENIC_MULTICAST_PERFECT_FILTERS
;
931 /* Is there an easier way? Trying to minimize to
932 * calls to add/del multicast addrs. We keep the
933 * addrs from the last call in enic->mc_addr and
934 * look for changes to add/del.
938 netdev_for_each_mc_addr(ha
, netdev
) {
941 memcpy(mc_addr
[i
++], ha
->addr
, ETH_ALEN
);
944 for (i
= 0; i
< enic
->mc_count
; i
++) {
945 for (j
= 0; j
< mc_count
; j
++)
946 if (ether_addr_equal(enic
->mc_addr
[i
], mc_addr
[j
]))
949 enic_dev_del_addr(enic
, enic
->mc_addr
[i
]);
952 for (i
= 0; i
< mc_count
; i
++) {
953 for (j
= 0; j
< enic
->mc_count
; j
++)
954 if (ether_addr_equal(mc_addr
[i
], enic
->mc_addr
[j
]))
956 if (j
== enic
->mc_count
)
957 enic_dev_add_addr(enic
, mc_addr
[i
]);
960 /* Save the list to compare against next time
963 for (i
= 0; i
< mc_count
; i
++)
964 memcpy(enic
->mc_addr
[i
], mc_addr
[i
], ETH_ALEN
);
966 enic
->mc_count
= mc_count
;
969 static void enic_update_unicast_addr_list(struct enic
*enic
)
971 struct net_device
*netdev
= enic
->netdev
;
972 struct netdev_hw_addr
*ha
;
973 unsigned int uc_count
= netdev_uc_count(netdev
);
974 u8 uc_addr
[ENIC_UNICAST_PERFECT_FILTERS
][ETH_ALEN
];
977 if (uc_count
> ENIC_UNICAST_PERFECT_FILTERS
) {
978 netdev_warn(netdev
, "Registering only %d out of %d "
979 "unicast addresses\n",
980 ENIC_UNICAST_PERFECT_FILTERS
, uc_count
);
981 uc_count
= ENIC_UNICAST_PERFECT_FILTERS
;
984 /* Is there an easier way? Trying to minimize to
985 * calls to add/del unicast addrs. We keep the
986 * addrs from the last call in enic->uc_addr and
987 * look for changes to add/del.
991 netdev_for_each_uc_addr(ha
, netdev
) {
994 memcpy(uc_addr
[i
++], ha
->addr
, ETH_ALEN
);
997 for (i
= 0; i
< enic
->uc_count
; i
++) {
998 for (j
= 0; j
< uc_count
; j
++)
999 if (ether_addr_equal(enic
->uc_addr
[i
], uc_addr
[j
]))
1002 enic_dev_del_addr(enic
, enic
->uc_addr
[i
]);
1005 for (i
= 0; i
< uc_count
; i
++) {
1006 for (j
= 0; j
< enic
->uc_count
; j
++)
1007 if (ether_addr_equal(uc_addr
[i
], enic
->uc_addr
[j
]))
1009 if (j
== enic
->uc_count
)
1010 enic_dev_add_addr(enic
, uc_addr
[i
]);
1013 /* Save the list to compare against next time
1016 for (i
= 0; i
< uc_count
; i
++)
1017 memcpy(enic
->uc_addr
[i
], uc_addr
[i
], ETH_ALEN
);
1019 enic
->uc_count
= uc_count
;
1022 /* netif_tx_lock held, BHs disabled */
1023 static void enic_set_rx_mode(struct net_device
*netdev
)
1025 struct enic
*enic
= netdev_priv(netdev
);
1027 int multicast
= (netdev
->flags
& IFF_MULTICAST
) ? 1 : 0;
1028 int broadcast
= (netdev
->flags
& IFF_BROADCAST
) ? 1 : 0;
1029 int promisc
= (netdev
->flags
& IFF_PROMISC
) ||
1030 netdev_uc_count(netdev
) > ENIC_UNICAST_PERFECT_FILTERS
;
1031 int allmulti
= (netdev
->flags
& IFF_ALLMULTI
) ||
1032 netdev_mc_count(netdev
) > ENIC_MULTICAST_PERFECT_FILTERS
;
1033 unsigned int flags
= netdev
->flags
|
1034 (allmulti
? IFF_ALLMULTI
: 0) |
1035 (promisc
? IFF_PROMISC
: 0);
1037 if (enic
->flags
!= flags
) {
1038 enic
->flags
= flags
;
1039 enic_dev_packet_filter(enic
, directed
,
1040 multicast
, broadcast
, promisc
, allmulti
);
1044 enic_update_unicast_addr_list(enic
);
1046 enic_update_multicast_addr_list(enic
);
1050 /* netif_tx_lock held, BHs disabled */
1051 static void enic_tx_timeout(struct net_device
*netdev
)
1053 struct enic
*enic
= netdev_priv(netdev
);
1054 schedule_work(&enic
->reset
);
1057 static int enic_set_vf_mac(struct net_device
*netdev
, int vf
, u8
*mac
)
1059 struct enic
*enic
= netdev_priv(netdev
);
1060 struct enic_port_profile
*pp
;
1063 ENIC_PP_BY_INDEX(enic
, vf
, pp
, &err
);
1067 if (is_valid_ether_addr(mac
) || is_zero_ether_addr(mac
)) {
1068 if (vf
== PORT_SELF_VF
) {
1069 memcpy(pp
->vf_mac
, mac
, ETH_ALEN
);
1073 * For sriov vf's set the mac in hw
1075 ENIC_DEVCMD_PROXY_BY_INDEX(vf
, err
, enic
,
1076 vnic_dev_set_mac_addr
, mac
);
1077 return enic_dev_status_to_errno(err
);
1083 static int enic_set_vf_port(struct net_device
*netdev
, int vf
,
1084 struct nlattr
*port
[])
1086 struct enic
*enic
= netdev_priv(netdev
);
1087 struct enic_port_profile prev_pp
;
1088 struct enic_port_profile
*pp
;
1089 int err
= 0, restore_pp
= 1;
1091 ENIC_PP_BY_INDEX(enic
, vf
, pp
, &err
);
1095 if (!port
[IFLA_PORT_REQUEST
])
1098 memcpy(&prev_pp
, pp
, sizeof(*enic
->pp
));
1099 memset(pp
, 0, sizeof(*enic
->pp
));
1101 pp
->set
|= ENIC_SET_REQUEST
;
1102 pp
->request
= nla_get_u8(port
[IFLA_PORT_REQUEST
]);
1104 if (port
[IFLA_PORT_PROFILE
]) {
1105 pp
->set
|= ENIC_SET_NAME
;
1106 memcpy(pp
->name
, nla_data(port
[IFLA_PORT_PROFILE
]),
1110 if (port
[IFLA_PORT_INSTANCE_UUID
]) {
1111 pp
->set
|= ENIC_SET_INSTANCE
;
1112 memcpy(pp
->instance_uuid
,
1113 nla_data(port
[IFLA_PORT_INSTANCE_UUID
]), PORT_UUID_MAX
);
1116 if (port
[IFLA_PORT_HOST_UUID
]) {
1117 pp
->set
|= ENIC_SET_HOST
;
1118 memcpy(pp
->host_uuid
,
1119 nla_data(port
[IFLA_PORT_HOST_UUID
]), PORT_UUID_MAX
);
1122 if (vf
== PORT_SELF_VF
) {
1123 /* Special case handling: mac came from IFLA_VF_MAC */
1124 if (!is_zero_ether_addr(prev_pp
.vf_mac
))
1125 memcpy(pp
->mac_addr
, prev_pp
.vf_mac
, ETH_ALEN
);
1127 if (is_zero_ether_addr(netdev
->dev_addr
))
1128 eth_hw_addr_random(netdev
);
1130 /* SR-IOV VF: get mac from adapter */
1131 ENIC_DEVCMD_PROXY_BY_INDEX(vf
, err
, enic
,
1132 vnic_dev_get_mac_addr
, pp
->mac_addr
);
1134 netdev_err(netdev
, "Error getting mac for vf %d\n", vf
);
1135 memcpy(pp
, &prev_pp
, sizeof(*pp
));
1136 return enic_dev_status_to_errno(err
);
1140 err
= enic_process_set_pp_request(enic
, vf
, &prev_pp
, &restore_pp
);
1143 /* Things are still the way they were: Implicit
1144 * DISASSOCIATE failed
1146 memcpy(pp
, &prev_pp
, sizeof(*pp
));
1148 memset(pp
, 0, sizeof(*pp
));
1149 if (vf
== PORT_SELF_VF
)
1150 memset(netdev
->dev_addr
, 0, ETH_ALEN
);
1153 /* Set flag to indicate that the port assoc/disassoc
1154 * request has been sent out to fw
1156 pp
->set
|= ENIC_PORT_REQUEST_APPLIED
;
1158 /* If DISASSOCIATE, clean up all assigned/saved macaddresses */
1159 if (pp
->request
== PORT_REQUEST_DISASSOCIATE
) {
1160 memset(pp
->mac_addr
, 0, ETH_ALEN
);
1161 if (vf
== PORT_SELF_VF
)
1162 memset(netdev
->dev_addr
, 0, ETH_ALEN
);
1166 if (vf
== PORT_SELF_VF
)
1167 memset(pp
->vf_mac
, 0, ETH_ALEN
);
1172 static int enic_get_vf_port(struct net_device
*netdev
, int vf
,
1173 struct sk_buff
*skb
)
1175 struct enic
*enic
= netdev_priv(netdev
);
1176 u16 response
= PORT_PROFILE_RESPONSE_SUCCESS
;
1177 struct enic_port_profile
*pp
;
1180 ENIC_PP_BY_INDEX(enic
, vf
, pp
, &err
);
1184 if (!(pp
->set
& ENIC_PORT_REQUEST_APPLIED
))
1187 err
= enic_process_get_pp_request(enic
, vf
, pp
->request
, &response
);
1191 if (nla_put_u16(skb
, IFLA_PORT_REQUEST
, pp
->request
) ||
1192 nla_put_u16(skb
, IFLA_PORT_RESPONSE
, response
) ||
1193 ((pp
->set
& ENIC_SET_NAME
) &&
1194 nla_put(skb
, IFLA_PORT_PROFILE
, PORT_PROFILE_MAX
, pp
->name
)) ||
1195 ((pp
->set
& ENIC_SET_INSTANCE
) &&
1196 nla_put(skb
, IFLA_PORT_INSTANCE_UUID
, PORT_UUID_MAX
,
1197 pp
->instance_uuid
)) ||
1198 ((pp
->set
& ENIC_SET_HOST
) &&
1199 nla_put(skb
, IFLA_PORT_HOST_UUID
, PORT_UUID_MAX
, pp
->host_uuid
)))
1200 goto nla_put_failure
;
1207 static void enic_free_rq_buf(struct vnic_rq
*rq
, struct vnic_rq_buf
*buf
)
1209 struct enic
*enic
= vnic_dev_priv(rq
->vdev
);
1214 pci_unmap_single(enic
->pdev
, buf
->dma_addr
,
1215 buf
->len
, PCI_DMA_FROMDEVICE
);
1216 dev_kfree_skb_any(buf
->os_buf
);
1219 static int enic_rq_alloc_buf(struct vnic_rq
*rq
)
1221 struct enic
*enic
= vnic_dev_priv(rq
->vdev
);
1222 struct net_device
*netdev
= enic
->netdev
;
1223 struct sk_buff
*skb
;
1224 unsigned int len
= netdev
->mtu
+ VLAN_ETH_HLEN
;
1225 unsigned int os_buf_index
= 0;
1226 dma_addr_t dma_addr
;
1228 skb
= netdev_alloc_skb_ip_align(netdev
, len
);
1232 dma_addr
= pci_map_single(enic
->pdev
, skb
->data
,
1233 len
, PCI_DMA_FROMDEVICE
);
1235 enic_queue_rq_desc(rq
, skb
, os_buf_index
,
1241 static void enic_rq_indicate_buf(struct vnic_rq
*rq
,
1242 struct cq_desc
*cq_desc
, struct vnic_rq_buf
*buf
,
1243 int skipped
, void *opaque
)
1245 struct enic
*enic
= vnic_dev_priv(rq
->vdev
);
1246 struct net_device
*netdev
= enic
->netdev
;
1247 struct sk_buff
*skb
;
1249 u8 type
, color
, eop
, sop
, ingress_port
, vlan_stripped
;
1250 u8 fcoe
, fcoe_sof
, fcoe_fc_crc_ok
, fcoe_enc_error
, fcoe_eof
;
1251 u8 tcp_udp_csum_ok
, udp
, tcp
, ipv4_csum_ok
;
1252 u8 ipv6
, ipv4
, ipv4_fragment
, fcs_ok
, rss_type
, csum_not_calc
;
1254 u16 q_number
, completed_index
, bytes_written
, vlan_tci
, checksum
;
1261 prefetch(skb
->data
- NET_IP_ALIGN
);
1262 pci_unmap_single(enic
->pdev
, buf
->dma_addr
,
1263 buf
->len
, PCI_DMA_FROMDEVICE
);
1265 cq_enet_rq_desc_dec((struct cq_enet_rq_desc
*)cq_desc
,
1266 &type
, &color
, &q_number
, &completed_index
,
1267 &ingress_port
, &fcoe
, &eop
, &sop
, &rss_type
,
1268 &csum_not_calc
, &rss_hash
, &bytes_written
,
1269 &packet_error
, &vlan_stripped
, &vlan_tci
, &checksum
,
1270 &fcoe_sof
, &fcoe_fc_crc_ok
, &fcoe_enc_error
,
1271 &fcoe_eof
, &tcp_udp_csum_ok
, &udp
, &tcp
,
1272 &ipv4_csum_ok
, &ipv6
, &ipv4
, &ipv4_fragment
,
1278 if (bytes_written
> 0)
1280 else if (bytes_written
== 0)
1281 enic
->rq_truncated_pkts
++;
1284 dev_kfree_skb_any(skb
);
1289 if (eop
&& bytes_written
> 0) {
1294 skb_put(skb
, bytes_written
);
1295 skb
->protocol
= eth_type_trans(skb
, netdev
);
1297 if ((netdev
->features
& NETIF_F_RXCSUM
) && !csum_not_calc
) {
1298 skb
->csum
= htons(checksum
);
1299 skb
->ip_summed
= CHECKSUM_COMPLETE
;
1303 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
), vlan_tci
);
1305 if (netdev
->features
& NETIF_F_GRO
)
1306 napi_gro_receive(&enic
->napi
[q_number
], skb
);
1308 netif_receive_skb(skb
);
1314 dev_kfree_skb_any(skb
);
1318 static int enic_rq_service(struct vnic_dev
*vdev
, struct cq_desc
*cq_desc
,
1319 u8 type
, u16 q_number
, u16 completed_index
, void *opaque
)
1321 struct enic
*enic
= vnic_dev_priv(vdev
);
1323 vnic_rq_service(&enic
->rq
[q_number
], cq_desc
,
1324 completed_index
, VNIC_RQ_RETURN_DESC
,
1325 enic_rq_indicate_buf
, opaque
);
1330 static int enic_poll(struct napi_struct
*napi
, int budget
)
1332 struct net_device
*netdev
= napi
->dev
;
1333 struct enic
*enic
= netdev_priv(netdev
);
1334 unsigned int cq_rq
= enic_cq_rq(enic
, 0);
1335 unsigned int cq_wq
= enic_cq_wq(enic
, 0);
1336 unsigned int intr
= enic_legacy_io_intr();
1337 unsigned int rq_work_to_do
= budget
;
1338 unsigned int wq_work_to_do
= -1; /* no limit */
1339 unsigned int work_done
, rq_work_done
, wq_work_done
;
1342 /* Service RQ (first) and WQ
1345 rq_work_done
= vnic_cq_service(&enic
->cq
[cq_rq
],
1346 rq_work_to_do
, enic_rq_service
, NULL
);
1348 wq_work_done
= vnic_cq_service(&enic
->cq
[cq_wq
],
1349 wq_work_to_do
, enic_wq_service
, NULL
);
1351 /* Accumulate intr event credits for this polling
1352 * cycle. An intr event is the completion of a
1353 * a WQ or RQ packet.
1356 work_done
= rq_work_done
+ wq_work_done
;
1359 vnic_intr_return_credits(&enic
->intr
[intr
],
1361 0 /* don't unmask intr */,
1362 0 /* don't reset intr timer */);
1364 err
= vnic_rq_fill(&enic
->rq
[0], enic_rq_alloc_buf
);
1366 /* Buffer allocation failed. Stay in polling
1367 * mode so we can try to fill the ring again.
1371 rq_work_done
= rq_work_to_do
;
1373 if (rq_work_done
< rq_work_to_do
) {
1375 /* Some work done, but not enough to stay in polling,
1379 napi_complete(napi
);
1380 vnic_intr_unmask(&enic
->intr
[intr
]);
1383 return rq_work_done
;
1386 static int enic_poll_msix(struct napi_struct
*napi
, int budget
)
1388 struct net_device
*netdev
= napi
->dev
;
1389 struct enic
*enic
= netdev_priv(netdev
);
1390 unsigned int rq
= (napi
- &enic
->napi
[0]);
1391 unsigned int cq
= enic_cq_rq(enic
, rq
);
1392 unsigned int intr
= enic_msix_rq_intr(enic
, rq
);
1393 unsigned int work_to_do
= budget
;
1394 unsigned int work_done
;
1400 work_done
= vnic_cq_service(&enic
->cq
[cq
],
1401 work_to_do
, enic_rq_service
, NULL
);
1403 /* Return intr event credits for this polling
1404 * cycle. An intr event is the completion of a
1409 vnic_intr_return_credits(&enic
->intr
[intr
],
1411 0 /* don't unmask intr */,
1412 0 /* don't reset intr timer */);
1414 err
= vnic_rq_fill(&enic
->rq
[rq
], enic_rq_alloc_buf
);
1416 /* Buffer allocation failed. Stay in polling mode
1417 * so we can try to fill the ring again.
1421 work_done
= work_to_do
;
1423 if (work_done
< work_to_do
) {
1425 /* Some work done, but not enough to stay in polling,
1429 napi_complete(napi
);
1430 vnic_intr_unmask(&enic
->intr
[intr
]);
1436 static void enic_notify_timer(unsigned long data
)
1438 struct enic
*enic
= (struct enic
*)data
;
1440 enic_notify_check(enic
);
1442 mod_timer(&enic
->notify_timer
,
1443 round_jiffies(jiffies
+ ENIC_NOTIFY_TIMER_PERIOD
));
1446 static void enic_free_intr(struct enic
*enic
)
1448 struct net_device
*netdev
= enic
->netdev
;
1451 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
1452 case VNIC_DEV_INTR_MODE_INTX
:
1453 free_irq(enic
->pdev
->irq
, netdev
);
1455 case VNIC_DEV_INTR_MODE_MSI
:
1456 free_irq(enic
->pdev
->irq
, enic
);
1458 case VNIC_DEV_INTR_MODE_MSIX
:
1459 for (i
= 0; i
< ARRAY_SIZE(enic
->msix
); i
++)
1460 if (enic
->msix
[i
].requested
)
1461 free_irq(enic
->msix_entry
[i
].vector
,
1462 enic
->msix
[i
].devid
);
1469 static int enic_request_intr(struct enic
*enic
)
1471 struct net_device
*netdev
= enic
->netdev
;
1472 unsigned int i
, intr
;
1475 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
1477 case VNIC_DEV_INTR_MODE_INTX
:
1479 err
= request_irq(enic
->pdev
->irq
, enic_isr_legacy
,
1480 IRQF_SHARED
, netdev
->name
, netdev
);
1483 case VNIC_DEV_INTR_MODE_MSI
:
1485 err
= request_irq(enic
->pdev
->irq
, enic_isr_msi
,
1486 0, netdev
->name
, enic
);
1489 case VNIC_DEV_INTR_MODE_MSIX
:
1491 for (i
= 0; i
< enic
->rq_count
; i
++) {
1492 intr
= enic_msix_rq_intr(enic
, i
);
1493 snprintf(enic
->msix
[intr
].devname
,
1494 sizeof(enic
->msix
[intr
].devname
),
1495 "%.11s-rx-%d", netdev
->name
, i
);
1496 enic
->msix
[intr
].isr
= enic_isr_msix_rq
;
1497 enic
->msix
[intr
].devid
= &enic
->napi
[i
];
1500 for (i
= 0; i
< enic
->wq_count
; i
++) {
1501 intr
= enic_msix_wq_intr(enic
, i
);
1502 snprintf(enic
->msix
[intr
].devname
,
1503 sizeof(enic
->msix
[intr
].devname
),
1504 "%.11s-tx-%d", netdev
->name
, i
);
1505 enic
->msix
[intr
].isr
= enic_isr_msix_wq
;
1506 enic
->msix
[intr
].devid
= enic
;
1509 intr
= enic_msix_err_intr(enic
);
1510 snprintf(enic
->msix
[intr
].devname
,
1511 sizeof(enic
->msix
[intr
].devname
),
1512 "%.11s-err", netdev
->name
);
1513 enic
->msix
[intr
].isr
= enic_isr_msix_err
;
1514 enic
->msix
[intr
].devid
= enic
;
1516 intr
= enic_msix_notify_intr(enic
);
1517 snprintf(enic
->msix
[intr
].devname
,
1518 sizeof(enic
->msix
[intr
].devname
),
1519 "%.11s-notify", netdev
->name
);
1520 enic
->msix
[intr
].isr
= enic_isr_msix_notify
;
1521 enic
->msix
[intr
].devid
= enic
;
1523 for (i
= 0; i
< ARRAY_SIZE(enic
->msix
); i
++)
1524 enic
->msix
[i
].requested
= 0;
1526 for (i
= 0; i
< enic
->intr_count
; i
++) {
1527 err
= request_irq(enic
->msix_entry
[i
].vector
,
1528 enic
->msix
[i
].isr
, 0,
1529 enic
->msix
[i
].devname
,
1530 enic
->msix
[i
].devid
);
1532 enic_free_intr(enic
);
1535 enic
->msix
[i
].requested
= 1;
1547 static void enic_synchronize_irqs(struct enic
*enic
)
1551 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
1552 case VNIC_DEV_INTR_MODE_INTX
:
1553 case VNIC_DEV_INTR_MODE_MSI
:
1554 synchronize_irq(enic
->pdev
->irq
);
1556 case VNIC_DEV_INTR_MODE_MSIX
:
1557 for (i
= 0; i
< enic
->intr_count
; i
++)
1558 synchronize_irq(enic
->msix_entry
[i
].vector
);
1565 static int enic_dev_notify_set(struct enic
*enic
)
1569 spin_lock(&enic
->devcmd_lock
);
1570 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
1571 case VNIC_DEV_INTR_MODE_INTX
:
1572 err
= vnic_dev_notify_set(enic
->vdev
,
1573 enic_legacy_notify_intr());
1575 case VNIC_DEV_INTR_MODE_MSIX
:
1576 err
= vnic_dev_notify_set(enic
->vdev
,
1577 enic_msix_notify_intr(enic
));
1580 err
= vnic_dev_notify_set(enic
->vdev
, -1 /* no intr */);
1583 spin_unlock(&enic
->devcmd_lock
);
1588 static void enic_notify_timer_start(struct enic
*enic
)
1590 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
1591 case VNIC_DEV_INTR_MODE_MSI
:
1592 mod_timer(&enic
->notify_timer
, jiffies
);
1595 /* Using intr for notification for INTx/MSI-X */
1600 /* rtnl lock is held, process context */
1601 static int enic_open(struct net_device
*netdev
)
1603 struct enic
*enic
= netdev_priv(netdev
);
1607 err
= enic_request_intr(enic
);
1609 netdev_err(netdev
, "Unable to request irq.\n");
1613 err
= enic_dev_notify_set(enic
);
1616 "Failed to alloc notify buffer, aborting.\n");
1617 goto err_out_free_intr
;
1620 for (i
= 0; i
< enic
->rq_count
; i
++) {
1621 vnic_rq_fill(&enic
->rq
[i
], enic_rq_alloc_buf
);
1622 /* Need at least one buffer on ring to get going */
1623 if (vnic_rq_desc_used(&enic
->rq
[i
]) == 0) {
1624 netdev_err(netdev
, "Unable to alloc receive buffers\n");
1626 goto err_out_notify_unset
;
1630 for (i
= 0; i
< enic
->wq_count
; i
++)
1631 vnic_wq_enable(&enic
->wq
[i
]);
1632 for (i
= 0; i
< enic
->rq_count
; i
++)
1633 vnic_rq_enable(&enic
->rq
[i
]);
1635 if (!enic_is_dynamic(enic
) && !enic_is_sriov_vf(enic
))
1636 enic_dev_add_station_addr(enic
);
1638 enic_set_rx_mode(netdev
);
1640 netif_wake_queue(netdev
);
1642 for (i
= 0; i
< enic
->rq_count
; i
++)
1643 napi_enable(&enic
->napi
[i
]);
1645 enic_dev_enable(enic
);
1647 for (i
= 0; i
< enic
->intr_count
; i
++)
1648 vnic_intr_unmask(&enic
->intr
[i
]);
1650 enic_notify_timer_start(enic
);
1654 err_out_notify_unset
:
1655 enic_dev_notify_unset(enic
);
1657 enic_free_intr(enic
);
1662 /* rtnl lock is held, process context */
1663 static int enic_stop(struct net_device
*netdev
)
1665 struct enic
*enic
= netdev_priv(netdev
);
1669 for (i
= 0; i
< enic
->intr_count
; i
++) {
1670 vnic_intr_mask(&enic
->intr
[i
]);
1671 (void)vnic_intr_masked(&enic
->intr
[i
]); /* flush write */
1674 enic_synchronize_irqs(enic
);
1676 del_timer_sync(&enic
->notify_timer
);
1678 enic_dev_disable(enic
);
1680 for (i
= 0; i
< enic
->rq_count
; i
++)
1681 napi_disable(&enic
->napi
[i
]);
1683 netif_carrier_off(netdev
);
1684 netif_tx_disable(netdev
);
1686 if (!enic_is_dynamic(enic
) && !enic_is_sriov_vf(enic
))
1687 enic_dev_del_station_addr(enic
);
1689 for (i
= 0; i
< enic
->wq_count
; i
++) {
1690 err
= vnic_wq_disable(&enic
->wq
[i
]);
1694 for (i
= 0; i
< enic
->rq_count
; i
++) {
1695 err
= vnic_rq_disable(&enic
->rq
[i
]);
1700 enic_dev_notify_unset(enic
);
1701 enic_free_intr(enic
);
1703 for (i
= 0; i
< enic
->wq_count
; i
++)
1704 vnic_wq_clean(&enic
->wq
[i
], enic_free_wq_buf
);
1705 for (i
= 0; i
< enic
->rq_count
; i
++)
1706 vnic_rq_clean(&enic
->rq
[i
], enic_free_rq_buf
);
1707 for (i
= 0; i
< enic
->cq_count
; i
++)
1708 vnic_cq_clean(&enic
->cq
[i
]);
1709 for (i
= 0; i
< enic
->intr_count
; i
++)
1710 vnic_intr_clean(&enic
->intr
[i
]);
1715 static int enic_change_mtu(struct net_device
*netdev
, int new_mtu
)
1717 struct enic
*enic
= netdev_priv(netdev
);
1718 int running
= netif_running(netdev
);
1720 if (new_mtu
< ENIC_MIN_MTU
|| new_mtu
> ENIC_MAX_MTU
)
1723 if (enic_is_dynamic(enic
) || enic_is_sriov_vf(enic
))
1729 netdev
->mtu
= new_mtu
;
1731 if (netdev
->mtu
> enic
->port_mtu
)
1733 "interface MTU (%d) set higher than port MTU (%d)\n",
1734 netdev
->mtu
, enic
->port_mtu
);
1742 static void enic_change_mtu_work(struct work_struct
*work
)
1744 struct enic
*enic
= container_of(work
, struct enic
, change_mtu_work
);
1745 struct net_device
*netdev
= enic
->netdev
;
1746 int new_mtu
= vnic_dev_mtu(enic
->vdev
);
1750 new_mtu
= max_t(int, ENIC_MIN_MTU
, min_t(int, ENIC_MAX_MTU
, new_mtu
));
1755 del_timer_sync(&enic
->notify_timer
);
1757 for (i
= 0; i
< enic
->rq_count
; i
++)
1758 napi_disable(&enic
->napi
[i
]);
1760 vnic_intr_mask(&enic
->intr
[0]);
1761 enic_synchronize_irqs(enic
);
1762 err
= vnic_rq_disable(&enic
->rq
[0]);
1765 netdev_err(netdev
, "Unable to disable RQ.\n");
1768 vnic_rq_clean(&enic
->rq
[0], enic_free_rq_buf
);
1769 vnic_cq_clean(&enic
->cq
[0]);
1770 vnic_intr_clean(&enic
->intr
[0]);
1772 /* Fill RQ with new_mtu-sized buffers */
1773 netdev
->mtu
= new_mtu
;
1774 vnic_rq_fill(&enic
->rq
[0], enic_rq_alloc_buf
);
1775 /* Need at least one buffer on ring to get going */
1776 if (vnic_rq_desc_used(&enic
->rq
[0]) == 0) {
1778 netdev_err(netdev
, "Unable to alloc receive buffers.\n");
1783 vnic_rq_enable(&enic
->rq
[0]);
1784 napi_enable(&enic
->napi
[0]);
1785 vnic_intr_unmask(&enic
->intr
[0]);
1786 enic_notify_timer_start(enic
);
1790 netdev_info(netdev
, "interface MTU set as %d\n", netdev
->mtu
);
1793 #ifdef CONFIG_NET_POLL_CONTROLLER
1794 static void enic_poll_controller(struct net_device
*netdev
)
1796 struct enic
*enic
= netdev_priv(netdev
);
1797 struct vnic_dev
*vdev
= enic
->vdev
;
1798 unsigned int i
, intr
;
1800 switch (vnic_dev_get_intr_mode(vdev
)) {
1801 case VNIC_DEV_INTR_MODE_MSIX
:
1802 for (i
= 0; i
< enic
->rq_count
; i
++) {
1803 intr
= enic_msix_rq_intr(enic
, i
);
1804 enic_isr_msix_rq(enic
->msix_entry
[intr
].vector
,
1808 for (i
= 0; i
< enic
->wq_count
; i
++) {
1809 intr
= enic_msix_wq_intr(enic
, i
);
1810 enic_isr_msix_wq(enic
->msix_entry
[intr
].vector
, enic
);
1814 case VNIC_DEV_INTR_MODE_MSI
:
1815 enic_isr_msi(enic
->pdev
->irq
, enic
);
1817 case VNIC_DEV_INTR_MODE_INTX
:
1818 enic_isr_legacy(enic
->pdev
->irq
, netdev
);
1826 static int enic_dev_wait(struct vnic_dev
*vdev
,
1827 int (*start
)(struct vnic_dev
*, int),
1828 int (*finished
)(struct vnic_dev
*, int *),
1835 BUG_ON(in_interrupt());
1837 err
= start(vdev
, arg
);
1841 /* Wait for func to complete...2 seconds max
1844 time
= jiffies
+ (HZ
* 2);
1847 err
= finished(vdev
, &done
);
1854 schedule_timeout_uninterruptible(HZ
/ 10);
1856 } while (time_after(time
, jiffies
));
1861 static int enic_dev_open(struct enic
*enic
)
1865 err
= enic_dev_wait(enic
->vdev
, vnic_dev_open
,
1866 vnic_dev_open_done
, 0);
1868 dev_err(enic_get_dev(enic
), "vNIC device open failed, err %d\n",
1874 static int enic_dev_hang_reset(struct enic
*enic
)
1878 err
= enic_dev_wait(enic
->vdev
, vnic_dev_hang_reset
,
1879 vnic_dev_hang_reset_done
, 0);
1881 netdev_err(enic
->netdev
, "vNIC hang reset failed, err %d\n",
1887 static int enic_set_rsskey(struct enic
*enic
)
1889 dma_addr_t rss_key_buf_pa
;
1890 union vnic_rss_key
*rss_key_buf_va
= NULL
;
1891 union vnic_rss_key rss_key
= {
1892 .key
[0].b
= {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
1893 .key
[1].b
= {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
1894 .key
[2].b
= {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
1895 .key
[3].b
= {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
1899 rss_key_buf_va
= pci_alloc_consistent(enic
->pdev
,
1900 sizeof(union vnic_rss_key
), &rss_key_buf_pa
);
1901 if (!rss_key_buf_va
)
1904 memcpy(rss_key_buf_va
, &rss_key
, sizeof(union vnic_rss_key
));
1906 spin_lock(&enic
->devcmd_lock
);
1907 err
= enic_set_rss_key(enic
,
1909 sizeof(union vnic_rss_key
));
1910 spin_unlock(&enic
->devcmd_lock
);
1912 pci_free_consistent(enic
->pdev
, sizeof(union vnic_rss_key
),
1913 rss_key_buf_va
, rss_key_buf_pa
);
1918 static int enic_set_rsscpu(struct enic
*enic
, u8 rss_hash_bits
)
1920 dma_addr_t rss_cpu_buf_pa
;
1921 union vnic_rss_cpu
*rss_cpu_buf_va
= NULL
;
1925 rss_cpu_buf_va
= pci_alloc_consistent(enic
->pdev
,
1926 sizeof(union vnic_rss_cpu
), &rss_cpu_buf_pa
);
1927 if (!rss_cpu_buf_va
)
1930 for (i
= 0; i
< (1 << rss_hash_bits
); i
++)
1931 (*rss_cpu_buf_va
).cpu
[i
/4].b
[i
%4] = i
% enic
->rq_count
;
1933 spin_lock(&enic
->devcmd_lock
);
1934 err
= enic_set_rss_cpu(enic
,
1936 sizeof(union vnic_rss_cpu
));
1937 spin_unlock(&enic
->devcmd_lock
);
1939 pci_free_consistent(enic
->pdev
, sizeof(union vnic_rss_cpu
),
1940 rss_cpu_buf_va
, rss_cpu_buf_pa
);
1945 static int enic_set_niccfg(struct enic
*enic
, u8 rss_default_cpu
,
1946 u8 rss_hash_type
, u8 rss_hash_bits
, u8 rss_base_cpu
, u8 rss_enable
)
1948 const u8 tso_ipid_split_en
= 0;
1949 const u8 ig_vlan_strip_en
= 1;
1952 /* Enable VLAN tag stripping.
1955 spin_lock(&enic
->devcmd_lock
);
1956 err
= enic_set_nic_cfg(enic
,
1957 rss_default_cpu
, rss_hash_type
,
1958 rss_hash_bits
, rss_base_cpu
,
1959 rss_enable
, tso_ipid_split_en
,
1961 spin_unlock(&enic
->devcmd_lock
);
1966 static int enic_set_rss_nic_cfg(struct enic
*enic
)
1968 struct device
*dev
= enic_get_dev(enic
);
1969 const u8 rss_default_cpu
= 0;
1970 const u8 rss_hash_type
= NIC_CFG_RSS_HASH_TYPE_IPV4
|
1971 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4
|
1972 NIC_CFG_RSS_HASH_TYPE_IPV6
|
1973 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6
;
1974 const u8 rss_hash_bits
= 7;
1975 const u8 rss_base_cpu
= 0;
1976 u8 rss_enable
= ENIC_SETTING(enic
, RSS
) && (enic
->rq_count
> 1);
1979 if (!enic_set_rsskey(enic
)) {
1980 if (enic_set_rsscpu(enic
, rss_hash_bits
)) {
1982 dev_warn(dev
, "RSS disabled, "
1983 "Failed to set RSS cpu indirection table.");
1987 dev_warn(dev
, "RSS disabled, Failed to set RSS key.\n");
1991 return enic_set_niccfg(enic
, rss_default_cpu
, rss_hash_type
,
1992 rss_hash_bits
, rss_base_cpu
, rss_enable
);
1995 static void enic_reset(struct work_struct
*work
)
1997 struct enic
*enic
= container_of(work
, struct enic
, reset
);
1999 if (!netif_running(enic
->netdev
))
2004 enic_dev_hang_notify(enic
);
2005 enic_stop(enic
->netdev
);
2006 enic_dev_hang_reset(enic
);
2007 enic_reset_addr_lists(enic
);
2008 enic_init_vnic_resources(enic
);
2009 enic_set_rss_nic_cfg(enic
);
2010 enic_dev_set_ig_vlan_rewrite_mode(enic
);
2011 enic_open(enic
->netdev
);
2016 static int enic_set_intr_mode(struct enic
*enic
)
2018 unsigned int n
= min_t(unsigned int, enic
->rq_count
, ENIC_RQ_MAX
);
2019 unsigned int m
= min_t(unsigned int, enic
->wq_count
, ENIC_WQ_MAX
);
2022 /* Set interrupt mode (INTx, MSI, MSI-X) depending
2023 * on system capabilities.
2027 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
2028 * (the second to last INTR is used for WQ/RQ errors)
2029 * (the last INTR is used for notifications)
2032 BUG_ON(ARRAY_SIZE(enic
->msix_entry
) < n
+ m
+ 2);
2033 for (i
= 0; i
< n
+ m
+ 2; i
++)
2034 enic
->msix_entry
[i
].entry
= i
;
2036 /* Use multiple RQs if RSS is enabled
2039 if (ENIC_SETTING(enic
, RSS
) &&
2040 enic
->config
.intr_mode
< 1 &&
2041 enic
->rq_count
>= n
&&
2042 enic
->wq_count
>= m
&&
2043 enic
->cq_count
>= n
+ m
&&
2044 enic
->intr_count
>= n
+ m
+ 2) {
2046 if (!pci_enable_msix(enic
->pdev
, enic
->msix_entry
, n
+ m
+ 2)) {
2050 enic
->cq_count
= n
+ m
;
2051 enic
->intr_count
= n
+ m
+ 2;
2053 vnic_dev_set_intr_mode(enic
->vdev
,
2054 VNIC_DEV_INTR_MODE_MSIX
);
2060 if (enic
->config
.intr_mode
< 1 &&
2061 enic
->rq_count
>= 1 &&
2062 enic
->wq_count
>= m
&&
2063 enic
->cq_count
>= 1 + m
&&
2064 enic
->intr_count
>= 1 + m
+ 2) {
2065 if (!pci_enable_msix(enic
->pdev
, enic
->msix_entry
, 1 + m
+ 2)) {
2069 enic
->cq_count
= 1 + m
;
2070 enic
->intr_count
= 1 + m
+ 2;
2072 vnic_dev_set_intr_mode(enic
->vdev
,
2073 VNIC_DEV_INTR_MODE_MSIX
);
2081 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
2084 if (enic
->config
.intr_mode
< 2 &&
2085 enic
->rq_count
>= 1 &&
2086 enic
->wq_count
>= 1 &&
2087 enic
->cq_count
>= 2 &&
2088 enic
->intr_count
>= 1 &&
2089 !pci_enable_msi(enic
->pdev
)) {
2094 enic
->intr_count
= 1;
2096 vnic_dev_set_intr_mode(enic
->vdev
, VNIC_DEV_INTR_MODE_MSI
);
2103 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2104 * (the first INTR is used for WQ/RQ)
2105 * (the second INTR is used for WQ/RQ errors)
2106 * (the last INTR is used for notifications)
2109 if (enic
->config
.intr_mode
< 3 &&
2110 enic
->rq_count
>= 1 &&
2111 enic
->wq_count
>= 1 &&
2112 enic
->cq_count
>= 2 &&
2113 enic
->intr_count
>= 3) {
2118 enic
->intr_count
= 3;
2120 vnic_dev_set_intr_mode(enic
->vdev
, VNIC_DEV_INTR_MODE_INTX
);
2125 vnic_dev_set_intr_mode(enic
->vdev
, VNIC_DEV_INTR_MODE_UNKNOWN
);
2130 static void enic_clear_intr_mode(struct enic
*enic
)
2132 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
2133 case VNIC_DEV_INTR_MODE_MSIX
:
2134 pci_disable_msix(enic
->pdev
);
2136 case VNIC_DEV_INTR_MODE_MSI
:
2137 pci_disable_msi(enic
->pdev
);
2143 vnic_dev_set_intr_mode(enic
->vdev
, VNIC_DEV_INTR_MODE_UNKNOWN
);
2146 static const struct net_device_ops enic_netdev_dynamic_ops
= {
2147 .ndo_open
= enic_open
,
2148 .ndo_stop
= enic_stop
,
2149 .ndo_start_xmit
= enic_hard_start_xmit
,
2150 .ndo_get_stats64
= enic_get_stats
,
2151 .ndo_validate_addr
= eth_validate_addr
,
2152 .ndo_set_rx_mode
= enic_set_rx_mode
,
2153 .ndo_set_mac_address
= enic_set_mac_address_dynamic
,
2154 .ndo_change_mtu
= enic_change_mtu
,
2155 .ndo_vlan_rx_add_vid
= enic_vlan_rx_add_vid
,
2156 .ndo_vlan_rx_kill_vid
= enic_vlan_rx_kill_vid
,
2157 .ndo_tx_timeout
= enic_tx_timeout
,
2158 .ndo_set_vf_port
= enic_set_vf_port
,
2159 .ndo_get_vf_port
= enic_get_vf_port
,
2160 .ndo_set_vf_mac
= enic_set_vf_mac
,
2161 #ifdef CONFIG_NET_POLL_CONTROLLER
2162 .ndo_poll_controller
= enic_poll_controller
,
2166 static const struct net_device_ops enic_netdev_ops
= {
2167 .ndo_open
= enic_open
,
2168 .ndo_stop
= enic_stop
,
2169 .ndo_start_xmit
= enic_hard_start_xmit
,
2170 .ndo_get_stats64
= enic_get_stats
,
2171 .ndo_validate_addr
= eth_validate_addr
,
2172 .ndo_set_mac_address
= enic_set_mac_address
,
2173 .ndo_set_rx_mode
= enic_set_rx_mode
,
2174 .ndo_change_mtu
= enic_change_mtu
,
2175 .ndo_vlan_rx_add_vid
= enic_vlan_rx_add_vid
,
2176 .ndo_vlan_rx_kill_vid
= enic_vlan_rx_kill_vid
,
2177 .ndo_tx_timeout
= enic_tx_timeout
,
2178 .ndo_set_vf_port
= enic_set_vf_port
,
2179 .ndo_get_vf_port
= enic_get_vf_port
,
2180 .ndo_set_vf_mac
= enic_set_vf_mac
,
2181 #ifdef CONFIG_NET_POLL_CONTROLLER
2182 .ndo_poll_controller
= enic_poll_controller
,
2186 static void enic_dev_deinit(struct enic
*enic
)
2190 for (i
= 0; i
< enic
->rq_count
; i
++)
2191 netif_napi_del(&enic
->napi
[i
]);
2193 enic_free_vnic_resources(enic
);
2194 enic_clear_intr_mode(enic
);
2197 static int enic_dev_init(struct enic
*enic
)
2199 struct device
*dev
= enic_get_dev(enic
);
2200 struct net_device
*netdev
= enic
->netdev
;
2204 /* Get interrupt coalesce timer info */
2205 err
= enic_dev_intr_coal_timer_info(enic
);
2207 dev_warn(dev
, "Using default conversion factor for "
2208 "interrupt coalesce timer\n");
2209 vnic_dev_intr_coal_timer_info_default(enic
->vdev
);
2212 /* Get vNIC configuration
2215 err
= enic_get_vnic_config(enic
);
2217 dev_err(dev
, "Get vNIC configuration failed, aborting\n");
2221 /* Get available resource counts
2224 enic_get_res_counts(enic
);
2226 /* Set interrupt mode based on resource counts and system
2230 err
= enic_set_intr_mode(enic
);
2232 dev_err(dev
, "Failed to set intr mode based on resource "
2233 "counts and system capabilities, aborting\n");
2237 /* Allocate and configure vNIC resources
2240 err
= enic_alloc_vnic_resources(enic
);
2242 dev_err(dev
, "Failed to alloc vNIC resources, aborting\n");
2243 goto err_out_free_vnic_resources
;
2246 enic_init_vnic_resources(enic
);
2248 err
= enic_set_rss_nic_cfg(enic
);
2250 dev_err(dev
, "Failed to config nic, aborting\n");
2251 goto err_out_free_vnic_resources
;
2254 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
2256 netif_napi_add(netdev
, &enic
->napi
[0], enic_poll
, 64);
2258 case VNIC_DEV_INTR_MODE_MSIX
:
2259 for (i
= 0; i
< enic
->rq_count
; i
++)
2260 netif_napi_add(netdev
, &enic
->napi
[i
],
2261 enic_poll_msix
, 64);
2267 err_out_free_vnic_resources
:
2268 enic_clear_intr_mode(enic
);
2269 enic_free_vnic_resources(enic
);
2274 static void enic_iounmap(struct enic
*enic
)
2278 for (i
= 0; i
< ARRAY_SIZE(enic
->bar
); i
++)
2279 if (enic
->bar
[i
].vaddr
)
2280 iounmap(enic
->bar
[i
].vaddr
);
2283 static int enic_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
2285 struct device
*dev
= &pdev
->dev
;
2286 struct net_device
*netdev
;
2291 #ifdef CONFIG_PCI_IOV
2296 /* Allocate net device structure and initialize. Private
2297 * instance data is initialized to zero.
2300 netdev
= alloc_etherdev(sizeof(struct enic
));
2304 pci_set_drvdata(pdev
, netdev
);
2306 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
2308 enic
= netdev_priv(netdev
);
2309 enic
->netdev
= netdev
;
2312 /* Setup PCI resources
2315 err
= pci_enable_device_mem(pdev
);
2317 dev_err(dev
, "Cannot enable PCI device, aborting\n");
2318 goto err_out_free_netdev
;
2321 err
= pci_request_regions(pdev
, DRV_NAME
);
2323 dev_err(dev
, "Cannot request PCI regions, aborting\n");
2324 goto err_out_disable_device
;
2327 pci_set_master(pdev
);
2329 /* Query PCI controller on system for DMA addressing
2330 * limitation for the device. Try 40-bit first, and
2334 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(40));
2336 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
2338 dev_err(dev
, "No usable DMA configuration, aborting\n");
2339 goto err_out_release_regions
;
2341 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
2343 dev_err(dev
, "Unable to obtain %u-bit DMA "
2344 "for consistent allocations, aborting\n", 32);
2345 goto err_out_release_regions
;
2348 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(40));
2350 dev_err(dev
, "Unable to obtain %u-bit DMA "
2351 "for consistent allocations, aborting\n", 40);
2352 goto err_out_release_regions
;
2357 /* Map vNIC resources from BAR0-5
2360 for (i
= 0; i
< ARRAY_SIZE(enic
->bar
); i
++) {
2361 if (!(pci_resource_flags(pdev
, i
) & IORESOURCE_MEM
))
2363 enic
->bar
[i
].len
= pci_resource_len(pdev
, i
);
2364 enic
->bar
[i
].vaddr
= pci_iomap(pdev
, i
, enic
->bar
[i
].len
);
2365 if (!enic
->bar
[i
].vaddr
) {
2366 dev_err(dev
, "Cannot memory-map BAR %d, aborting\n", i
);
2368 goto err_out_iounmap
;
2370 enic
->bar
[i
].bus_addr
= pci_resource_start(pdev
, i
);
2373 /* Register vNIC device
2376 enic
->vdev
= vnic_dev_register(NULL
, enic
, pdev
, enic
->bar
,
2377 ARRAY_SIZE(enic
->bar
));
2379 dev_err(dev
, "vNIC registration failed, aborting\n");
2381 goto err_out_iounmap
;
2384 #ifdef CONFIG_PCI_IOV
2385 /* Get number of subvnics */
2386 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_SRIOV
);
2388 pci_read_config_word(pdev
, pos
+ PCI_SRIOV_TOTAL_VF
,
2390 if (enic
->num_vfs
) {
2391 err
= pci_enable_sriov(pdev
, enic
->num_vfs
);
2393 dev_err(dev
, "SRIOV enable failed, aborting."
2394 " pci_enable_sriov() returned %d\n",
2396 goto err_out_vnic_unregister
;
2398 enic
->priv_flags
|= ENIC_SRIOV_ENABLED
;
2399 num_pps
= enic
->num_vfs
;
2404 /* Allocate structure for port profiles */
2405 enic
->pp
= kcalloc(num_pps
, sizeof(*enic
->pp
), GFP_KERNEL
);
2408 goto err_out_disable_sriov_pp
;
2411 /* Issue device open to get device in known state
2414 err
= enic_dev_open(enic
);
2416 dev_err(dev
, "vNIC dev open failed, aborting\n");
2417 goto err_out_disable_sriov
;
2420 /* Setup devcmd lock
2423 spin_lock_init(&enic
->devcmd_lock
);
2426 * Set ingress vlan rewrite mode before vnic initialization
2429 err
= enic_dev_set_ig_vlan_rewrite_mode(enic
);
2432 "Failed to set ingress vlan rewrite mode, aborting.\n");
2433 goto err_out_dev_close
;
2436 /* Issue device init to initialize the vnic-to-switch link.
2437 * We'll start with carrier off and wait for link UP
2438 * notification later to turn on carrier. We don't need
2439 * to wait here for the vnic-to-switch link initialization
2440 * to complete; link UP notification is the indication that
2441 * the process is complete.
2444 netif_carrier_off(netdev
);
2446 /* Do not call dev_init for a dynamic vnic.
2447 * For a dynamic vnic, init_prov_info will be
2448 * called later by an upper layer.
2451 if (!enic_is_dynamic(enic
)) {
2452 err
= vnic_dev_init(enic
->vdev
, 0);
2454 dev_err(dev
, "vNIC dev init failed, aborting\n");
2455 goto err_out_dev_close
;
2459 err
= enic_dev_init(enic
);
2461 dev_err(dev
, "Device initialization failed, aborting\n");
2462 goto err_out_dev_close
;
2465 /* Setup notification timer, HW reset task, and wq locks
2468 init_timer(&enic
->notify_timer
);
2469 enic
->notify_timer
.function
= enic_notify_timer
;
2470 enic
->notify_timer
.data
= (unsigned long)enic
;
2472 INIT_WORK(&enic
->reset
, enic_reset
);
2473 INIT_WORK(&enic
->change_mtu_work
, enic_change_mtu_work
);
2475 for (i
= 0; i
< enic
->wq_count
; i
++)
2476 spin_lock_init(&enic
->wq_lock
[i
]);
2478 /* Register net device
2481 enic
->port_mtu
= enic
->config
.mtu
;
2482 (void)enic_change_mtu(netdev
, enic
->port_mtu
);
2484 err
= enic_set_mac_addr(netdev
, enic
->mac_addr
);
2486 dev_err(dev
, "Invalid MAC address, aborting\n");
2487 goto err_out_dev_deinit
;
2490 enic
->tx_coalesce_usecs
= enic
->config
.intr_timer_usec
;
2491 enic
->rx_coalesce_usecs
= enic
->tx_coalesce_usecs
;
2493 if (enic_is_dynamic(enic
) || enic_is_sriov_vf(enic
))
2494 netdev
->netdev_ops
= &enic_netdev_dynamic_ops
;
2496 netdev
->netdev_ops
= &enic_netdev_ops
;
2498 netdev
->watchdog_timeo
= 2 * HZ
;
2499 netdev
->ethtool_ops
= &enic_ethtool_ops
;
2501 netdev
->features
|= NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_CTAG_RX
;
2502 if (ENIC_SETTING(enic
, LOOP
)) {
2503 netdev
->features
&= ~NETIF_F_HW_VLAN_CTAG_TX
;
2504 enic
->loop_enable
= 1;
2505 enic
->loop_tag
= enic
->config
.loop_tag
;
2506 dev_info(dev
, "loopback tag=0x%04x\n", enic
->loop_tag
);
2508 if (ENIC_SETTING(enic
, TXCSUM
))
2509 netdev
->hw_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2510 if (ENIC_SETTING(enic
, TSO
))
2511 netdev
->hw_features
|= NETIF_F_TSO
|
2512 NETIF_F_TSO6
| NETIF_F_TSO_ECN
;
2513 if (ENIC_SETTING(enic
, RXCSUM
))
2514 netdev
->hw_features
|= NETIF_F_RXCSUM
;
2516 netdev
->features
|= netdev
->hw_features
;
2519 netdev
->features
|= NETIF_F_HIGHDMA
;
2521 netdev
->priv_flags
|= IFF_UNICAST_FLT
;
2523 err
= register_netdev(netdev
);
2525 dev_err(dev
, "Cannot register net device, aborting\n");
2526 goto err_out_dev_deinit
;
2532 enic_dev_deinit(enic
);
2534 vnic_dev_close(enic
->vdev
);
2535 err_out_disable_sriov
:
2537 err_out_disable_sriov_pp
:
2538 #ifdef CONFIG_PCI_IOV
2539 if (enic_sriov_enabled(enic
)) {
2540 pci_disable_sriov(pdev
);
2541 enic
->priv_flags
&= ~ENIC_SRIOV_ENABLED
;
2543 err_out_vnic_unregister
:
2545 vnic_dev_unregister(enic
->vdev
);
2548 err_out_release_regions
:
2549 pci_release_regions(pdev
);
2550 err_out_disable_device
:
2551 pci_disable_device(pdev
);
2552 err_out_free_netdev
:
2553 pci_set_drvdata(pdev
, NULL
);
2554 free_netdev(netdev
);
2559 static void enic_remove(struct pci_dev
*pdev
)
2561 struct net_device
*netdev
= pci_get_drvdata(pdev
);
2564 struct enic
*enic
= netdev_priv(netdev
);
2566 cancel_work_sync(&enic
->reset
);
2567 cancel_work_sync(&enic
->change_mtu_work
);
2568 unregister_netdev(netdev
);
2569 enic_dev_deinit(enic
);
2570 vnic_dev_close(enic
->vdev
);
2571 #ifdef CONFIG_PCI_IOV
2572 if (enic_sriov_enabled(enic
)) {
2573 pci_disable_sriov(pdev
);
2574 enic
->priv_flags
&= ~ENIC_SRIOV_ENABLED
;
2578 vnic_dev_unregister(enic
->vdev
);
2580 pci_release_regions(pdev
);
2581 pci_disable_device(pdev
);
2582 pci_set_drvdata(pdev
, NULL
);
2583 free_netdev(netdev
);
2587 static struct pci_driver enic_driver
= {
2589 .id_table
= enic_id_table
,
2590 .probe
= enic_probe
,
2591 .remove
= enic_remove
,
2594 static int __init
enic_init_module(void)
2596 pr_info("%s, ver %s\n", DRV_DESCRIPTION
, DRV_VERSION
);
2598 return pci_register_driver(&enic_driver
);
2601 static void __exit
enic_cleanup_module(void)
2603 pci_unregister_driver(&enic_driver
);
2606 module_init(enic_init_module
);
2607 module_exit(enic_cleanup_module
);