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/workqueue.h>
27 #include <linux/pci.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
32 #include <linux/ethtool.h>
35 #include <linux/ipv6.h>
36 #include <linux/tcp.h>
37 #include <linux/rtnetlink.h>
38 #include <net/ip6_checksum.h>
40 #include "cq_enet_desc.h"
42 #include "vnic_intr.h"
43 #include "vnic_stats.h"
48 #define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
49 #define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
50 #define MAX_TSO (1 << 16)
51 #define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
53 #define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
54 #define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
56 /* Supported devices */
57 static DEFINE_PCI_DEVICE_TABLE(enic_id_table
) = {
58 { PCI_VDEVICE(CISCO
, PCI_DEVICE_ID_CISCO_VIC_ENET
) },
59 { PCI_VDEVICE(CISCO
, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN
) },
60 { 0, } /* end of table */
63 MODULE_DESCRIPTION(DRV_DESCRIPTION
);
64 MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
65 MODULE_LICENSE("GPL");
66 MODULE_VERSION(DRV_VERSION
);
67 MODULE_DEVICE_TABLE(pci
, enic_id_table
);
70 char name
[ETH_GSTRING_LEN
];
74 #define ENIC_TX_STAT(stat) \
75 { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
76 #define ENIC_RX_STAT(stat) \
77 { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
79 static const struct enic_stat enic_tx_stats
[] = {
80 ENIC_TX_STAT(tx_frames_ok
),
81 ENIC_TX_STAT(tx_unicast_frames_ok
),
82 ENIC_TX_STAT(tx_multicast_frames_ok
),
83 ENIC_TX_STAT(tx_broadcast_frames_ok
),
84 ENIC_TX_STAT(tx_bytes_ok
),
85 ENIC_TX_STAT(tx_unicast_bytes_ok
),
86 ENIC_TX_STAT(tx_multicast_bytes_ok
),
87 ENIC_TX_STAT(tx_broadcast_bytes_ok
),
88 ENIC_TX_STAT(tx_drops
),
89 ENIC_TX_STAT(tx_errors
),
93 static const struct enic_stat enic_rx_stats
[] = {
94 ENIC_RX_STAT(rx_frames_ok
),
95 ENIC_RX_STAT(rx_frames_total
),
96 ENIC_RX_STAT(rx_unicast_frames_ok
),
97 ENIC_RX_STAT(rx_multicast_frames_ok
),
98 ENIC_RX_STAT(rx_broadcast_frames_ok
),
99 ENIC_RX_STAT(rx_bytes_ok
),
100 ENIC_RX_STAT(rx_unicast_bytes_ok
),
101 ENIC_RX_STAT(rx_multicast_bytes_ok
),
102 ENIC_RX_STAT(rx_broadcast_bytes_ok
),
103 ENIC_RX_STAT(rx_drop
),
104 ENIC_RX_STAT(rx_no_bufs
),
105 ENIC_RX_STAT(rx_errors
),
106 ENIC_RX_STAT(rx_rss
),
107 ENIC_RX_STAT(rx_crc_errors
),
108 ENIC_RX_STAT(rx_frames_64
),
109 ENIC_RX_STAT(rx_frames_127
),
110 ENIC_RX_STAT(rx_frames_255
),
111 ENIC_RX_STAT(rx_frames_511
),
112 ENIC_RX_STAT(rx_frames_1023
),
113 ENIC_RX_STAT(rx_frames_1518
),
114 ENIC_RX_STAT(rx_frames_to_max
),
117 static const unsigned int enic_n_tx_stats
= ARRAY_SIZE(enic_tx_stats
);
118 static const unsigned int enic_n_rx_stats
= ARRAY_SIZE(enic_rx_stats
);
120 static int enic_is_dynamic(struct enic
*enic
)
122 return enic
->pdev
->device
== PCI_DEVICE_ID_CISCO_VIC_ENET_DYN
;
125 static inline unsigned int enic_cq_rq(struct enic
*enic
, unsigned int rq
)
130 static inline unsigned int enic_cq_wq(struct enic
*enic
, unsigned int wq
)
132 return enic
->rq_count
+ wq
;
135 static inline unsigned int enic_legacy_io_intr(void)
140 static inline unsigned int enic_legacy_err_intr(void)
145 static inline unsigned int enic_legacy_notify_intr(void)
150 static inline unsigned int enic_msix_rq_intr(struct enic
*enic
, unsigned int rq
)
155 static inline unsigned int enic_msix_wq_intr(struct enic
*enic
, unsigned int wq
)
157 return enic
->rq_count
+ wq
;
160 static inline unsigned int enic_msix_err_intr(struct enic
*enic
)
162 return enic
->rq_count
+ enic
->wq_count
;
165 static inline unsigned int enic_msix_notify_intr(struct enic
*enic
)
167 return enic
->rq_count
+ enic
->wq_count
+ 1;
170 static int enic_get_settings(struct net_device
*netdev
,
171 struct ethtool_cmd
*ecmd
)
173 struct enic
*enic
= netdev_priv(netdev
);
175 ecmd
->supported
= (SUPPORTED_10000baseT_Full
| SUPPORTED_FIBRE
);
176 ecmd
->advertising
= (ADVERTISED_10000baseT_Full
| ADVERTISED_FIBRE
);
177 ecmd
->port
= PORT_FIBRE
;
178 ecmd
->transceiver
= XCVR_EXTERNAL
;
180 if (netif_carrier_ok(netdev
)) {
181 ecmd
->speed
= vnic_dev_port_speed(enic
->vdev
);
182 ecmd
->duplex
= DUPLEX_FULL
;
188 ecmd
->autoneg
= AUTONEG_DISABLE
;
193 static int enic_dev_fw_info(struct enic
*enic
,
194 struct vnic_devcmd_fw_info
**fw_info
)
198 spin_lock(&enic
->devcmd_lock
);
199 err
= vnic_dev_fw_info(enic
->vdev
, fw_info
);
200 spin_unlock(&enic
->devcmd_lock
);
205 static void enic_get_drvinfo(struct net_device
*netdev
,
206 struct ethtool_drvinfo
*drvinfo
)
208 struct enic
*enic
= netdev_priv(netdev
);
209 struct vnic_devcmd_fw_info
*fw_info
;
211 enic_dev_fw_info(enic
, &fw_info
);
213 strncpy(drvinfo
->driver
, DRV_NAME
, sizeof(drvinfo
->driver
));
214 strncpy(drvinfo
->version
, DRV_VERSION
, sizeof(drvinfo
->version
));
215 strncpy(drvinfo
->fw_version
, fw_info
->fw_version
,
216 sizeof(drvinfo
->fw_version
));
217 strncpy(drvinfo
->bus_info
, pci_name(enic
->pdev
),
218 sizeof(drvinfo
->bus_info
));
221 static void enic_get_strings(struct net_device
*netdev
, u32 stringset
, u8
*data
)
227 for (i
= 0; i
< enic_n_tx_stats
; i
++) {
228 memcpy(data
, enic_tx_stats
[i
].name
, ETH_GSTRING_LEN
);
229 data
+= ETH_GSTRING_LEN
;
231 for (i
= 0; i
< enic_n_rx_stats
; i
++) {
232 memcpy(data
, enic_rx_stats
[i
].name
, ETH_GSTRING_LEN
);
233 data
+= ETH_GSTRING_LEN
;
239 static int enic_get_sset_count(struct net_device
*netdev
, int sset
)
243 return enic_n_tx_stats
+ enic_n_rx_stats
;
249 static int enic_dev_stats_dump(struct enic
*enic
, struct vnic_stats
**vstats
)
253 spin_lock(&enic
->devcmd_lock
);
254 err
= vnic_dev_stats_dump(enic
->vdev
, vstats
);
255 spin_unlock(&enic
->devcmd_lock
);
260 static void enic_get_ethtool_stats(struct net_device
*netdev
,
261 struct ethtool_stats
*stats
, u64
*data
)
263 struct enic
*enic
= netdev_priv(netdev
);
264 struct vnic_stats
*vstats
;
267 enic_dev_stats_dump(enic
, &vstats
);
269 for (i
= 0; i
< enic_n_tx_stats
; i
++)
270 *(data
++) = ((u64
*)&vstats
->tx
)[enic_tx_stats
[i
].offset
];
271 for (i
= 0; i
< enic_n_rx_stats
; i
++)
272 *(data
++) = ((u64
*)&vstats
->rx
)[enic_rx_stats
[i
].offset
];
275 static u32
enic_get_rx_csum(struct net_device
*netdev
)
277 struct enic
*enic
= netdev_priv(netdev
);
278 return enic
->csum_rx_enabled
;
281 static int enic_set_rx_csum(struct net_device
*netdev
, u32 data
)
283 struct enic
*enic
= netdev_priv(netdev
);
285 if (data
&& !ENIC_SETTING(enic
, RXCSUM
))
288 enic
->csum_rx_enabled
= !!data
;
293 static int enic_set_tx_csum(struct net_device
*netdev
, u32 data
)
295 struct enic
*enic
= netdev_priv(netdev
);
297 if (data
&& !ENIC_SETTING(enic
, TXCSUM
))
301 netdev
->features
|= NETIF_F_HW_CSUM
;
303 netdev
->features
&= ~NETIF_F_HW_CSUM
;
308 static int enic_set_tso(struct net_device
*netdev
, u32 data
)
310 struct enic
*enic
= netdev_priv(netdev
);
312 if (data
&& !ENIC_SETTING(enic
, TSO
))
317 NETIF_F_TSO
| NETIF_F_TSO6
| NETIF_F_TSO_ECN
;
320 ~(NETIF_F_TSO
| NETIF_F_TSO6
| NETIF_F_TSO_ECN
);
325 static u32
enic_get_msglevel(struct net_device
*netdev
)
327 struct enic
*enic
= netdev_priv(netdev
);
328 return enic
->msg_enable
;
331 static void enic_set_msglevel(struct net_device
*netdev
, u32 value
)
333 struct enic
*enic
= netdev_priv(netdev
);
334 enic
->msg_enable
= value
;
337 static int enic_get_coalesce(struct net_device
*netdev
,
338 struct ethtool_coalesce
*ecmd
)
340 struct enic
*enic
= netdev_priv(netdev
);
342 ecmd
->tx_coalesce_usecs
= enic
->tx_coalesce_usecs
;
343 ecmd
->rx_coalesce_usecs
= enic
->rx_coalesce_usecs
;
348 static int enic_set_coalesce(struct net_device
*netdev
,
349 struct ethtool_coalesce
*ecmd
)
351 struct enic
*enic
= netdev_priv(netdev
);
352 u32 tx_coalesce_usecs
;
353 u32 rx_coalesce_usecs
;
354 unsigned int i
, intr
;
356 tx_coalesce_usecs
= min_t(u32
,
357 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX
),
358 ecmd
->tx_coalesce_usecs
);
359 rx_coalesce_usecs
= min_t(u32
,
360 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX
),
361 ecmd
->rx_coalesce_usecs
);
363 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
364 case VNIC_DEV_INTR_MODE_INTX
:
365 if (tx_coalesce_usecs
!= rx_coalesce_usecs
)
368 intr
= enic_legacy_io_intr();
369 vnic_intr_coalescing_timer_set(&enic
->intr
[intr
],
370 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs
));
372 case VNIC_DEV_INTR_MODE_MSI
:
373 if (tx_coalesce_usecs
!= rx_coalesce_usecs
)
376 vnic_intr_coalescing_timer_set(&enic
->intr
[0],
377 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs
));
379 case VNIC_DEV_INTR_MODE_MSIX
:
380 for (i
= 0; i
< enic
->wq_count
; i
++) {
381 intr
= enic_msix_wq_intr(enic
, i
);
382 vnic_intr_coalescing_timer_set(&enic
->intr
[intr
],
383 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs
));
386 for (i
= 0; i
< enic
->rq_count
; i
++) {
387 intr
= enic_msix_rq_intr(enic
, i
);
388 vnic_intr_coalescing_timer_set(&enic
->intr
[intr
],
389 INTR_COALESCE_USEC_TO_HW(rx_coalesce_usecs
));
397 enic
->tx_coalesce_usecs
= tx_coalesce_usecs
;
398 enic
->rx_coalesce_usecs
= rx_coalesce_usecs
;
403 static const struct ethtool_ops enic_ethtool_ops
= {
404 .get_settings
= enic_get_settings
,
405 .get_drvinfo
= enic_get_drvinfo
,
406 .get_msglevel
= enic_get_msglevel
,
407 .set_msglevel
= enic_set_msglevel
,
408 .get_link
= ethtool_op_get_link
,
409 .get_strings
= enic_get_strings
,
410 .get_sset_count
= enic_get_sset_count
,
411 .get_ethtool_stats
= enic_get_ethtool_stats
,
412 .get_rx_csum
= enic_get_rx_csum
,
413 .set_rx_csum
= enic_set_rx_csum
,
414 .get_tx_csum
= ethtool_op_get_tx_csum
,
415 .set_tx_csum
= enic_set_tx_csum
,
416 .get_sg
= ethtool_op_get_sg
,
417 .set_sg
= ethtool_op_set_sg
,
418 .get_tso
= ethtool_op_get_tso
,
419 .set_tso
= enic_set_tso
,
420 .get_coalesce
= enic_get_coalesce
,
421 .set_coalesce
= enic_set_coalesce
,
422 .get_flags
= ethtool_op_get_flags
,
425 static void enic_free_wq_buf(struct vnic_wq
*wq
, struct vnic_wq_buf
*buf
)
427 struct enic
*enic
= vnic_dev_priv(wq
->vdev
);
430 pci_unmap_single(enic
->pdev
, buf
->dma_addr
,
431 buf
->len
, PCI_DMA_TODEVICE
);
433 pci_unmap_page(enic
->pdev
, buf
->dma_addr
,
434 buf
->len
, PCI_DMA_TODEVICE
);
437 dev_kfree_skb_any(buf
->os_buf
);
440 static void enic_wq_free_buf(struct vnic_wq
*wq
,
441 struct cq_desc
*cq_desc
, struct vnic_wq_buf
*buf
, void *opaque
)
443 enic_free_wq_buf(wq
, buf
);
446 static int enic_wq_service(struct vnic_dev
*vdev
, struct cq_desc
*cq_desc
,
447 u8 type
, u16 q_number
, u16 completed_index
, void *opaque
)
449 struct enic
*enic
= vnic_dev_priv(vdev
);
451 spin_lock(&enic
->wq_lock
[q_number
]);
453 vnic_wq_service(&enic
->wq
[q_number
], cq_desc
,
454 completed_index
, enic_wq_free_buf
,
457 if (netif_queue_stopped(enic
->netdev
) &&
458 vnic_wq_desc_avail(&enic
->wq
[q_number
]) >=
459 (MAX_SKB_FRAGS
+ ENIC_DESC_MAX_SPLITS
))
460 netif_wake_queue(enic
->netdev
);
462 spin_unlock(&enic
->wq_lock
[q_number
]);
467 static void enic_log_q_error(struct enic
*enic
)
472 for (i
= 0; i
< enic
->wq_count
; i
++) {
473 error_status
= vnic_wq_error_status(&enic
->wq
[i
]);
475 netdev_err(enic
->netdev
, "WQ[%d] error_status %d\n",
479 for (i
= 0; i
< enic
->rq_count
; i
++) {
480 error_status
= vnic_rq_error_status(&enic
->rq
[i
]);
482 netdev_err(enic
->netdev
, "RQ[%d] error_status %d\n",
487 static void enic_msglvl_check(struct enic
*enic
)
489 u32 msg_enable
= vnic_dev_msg_lvl(enic
->vdev
);
491 if (msg_enable
!= enic
->msg_enable
) {
492 netdev_info(enic
->netdev
, "msg lvl changed from 0x%x to 0x%x\n",
493 enic
->msg_enable
, msg_enable
);
494 enic
->msg_enable
= msg_enable
;
498 static void enic_mtu_check(struct enic
*enic
)
500 u32 mtu
= vnic_dev_mtu(enic
->vdev
);
501 struct net_device
*netdev
= enic
->netdev
;
503 if (mtu
&& mtu
!= enic
->port_mtu
) {
504 enic
->port_mtu
= mtu
;
505 if (mtu
< netdev
->mtu
)
507 "interface MTU (%d) set higher "
508 "than switch port MTU (%d)\n",
513 static void enic_link_check(struct enic
*enic
)
515 int link_status
= vnic_dev_link_status(enic
->vdev
);
516 int carrier_ok
= netif_carrier_ok(enic
->netdev
);
518 if (link_status
&& !carrier_ok
) {
519 netdev_info(enic
->netdev
, "Link UP\n");
520 netif_carrier_on(enic
->netdev
);
521 } else if (!link_status
&& carrier_ok
) {
522 netdev_info(enic
->netdev
, "Link DOWN\n");
523 netif_carrier_off(enic
->netdev
);
527 static void enic_notify_check(struct enic
*enic
)
529 enic_msglvl_check(enic
);
530 enic_mtu_check(enic
);
531 enic_link_check(enic
);
534 #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
536 static irqreturn_t
enic_isr_legacy(int irq
, void *data
)
538 struct net_device
*netdev
= data
;
539 struct enic
*enic
= netdev_priv(netdev
);
540 unsigned int io_intr
= enic_legacy_io_intr();
541 unsigned int err_intr
= enic_legacy_err_intr();
542 unsigned int notify_intr
= enic_legacy_notify_intr();
545 vnic_intr_mask(&enic
->intr
[io_intr
]);
547 pba
= vnic_intr_legacy_pba(enic
->legacy_pba
);
549 vnic_intr_unmask(&enic
->intr
[io_intr
]);
550 return IRQ_NONE
; /* not our interrupt */
553 if (ENIC_TEST_INTR(pba
, notify_intr
)) {
554 vnic_intr_return_all_credits(&enic
->intr
[notify_intr
]);
555 enic_notify_check(enic
);
558 if (ENIC_TEST_INTR(pba
, err_intr
)) {
559 vnic_intr_return_all_credits(&enic
->intr
[err_intr
]);
560 enic_log_q_error(enic
);
561 /* schedule recovery from WQ/RQ error */
562 schedule_work(&enic
->reset
);
566 if (ENIC_TEST_INTR(pba
, io_intr
)) {
567 if (napi_schedule_prep(&enic
->napi
[0]))
568 __napi_schedule(&enic
->napi
[0]);
570 vnic_intr_unmask(&enic
->intr
[io_intr
]);
576 static irqreturn_t
enic_isr_msi(int irq
, void *data
)
578 struct enic
*enic
= data
;
580 /* With MSI, there is no sharing of interrupts, so this is
581 * our interrupt and there is no need to ack it. The device
582 * is not providing per-vector masking, so the OS will not
583 * write to PCI config space to mask/unmask the interrupt.
584 * We're using mask_on_assertion for MSI, so the device
585 * automatically masks the interrupt when the interrupt is
586 * generated. Later, when exiting polling, the interrupt
587 * will be unmasked (see enic_poll).
589 * Also, the device uses the same PCIe Traffic Class (TC)
590 * for Memory Write data and MSI, so there are no ordering
591 * issues; the MSI will always arrive at the Root Complex
592 * _after_ corresponding Memory Writes (i.e. descriptor
596 napi_schedule(&enic
->napi
[0]);
601 static irqreturn_t
enic_isr_msix_rq(int irq
, void *data
)
603 struct napi_struct
*napi
= data
;
605 /* schedule NAPI polling for RQ cleanup */
611 static irqreturn_t
enic_isr_msix_wq(int irq
, void *data
)
613 struct enic
*enic
= data
;
614 unsigned int cq
= enic_cq_wq(enic
, 0);
615 unsigned int intr
= enic_msix_wq_intr(enic
, 0);
616 unsigned int wq_work_to_do
= -1; /* no limit */
617 unsigned int wq_work_done
;
619 wq_work_done
= vnic_cq_service(&enic
->cq
[cq
],
620 wq_work_to_do
, enic_wq_service
, NULL
);
622 vnic_intr_return_credits(&enic
->intr
[intr
],
625 1 /* reset intr timer */);
630 static irqreturn_t
enic_isr_msix_err(int irq
, void *data
)
632 struct enic
*enic
= data
;
633 unsigned int intr
= enic_msix_err_intr(enic
);
635 vnic_intr_return_all_credits(&enic
->intr
[intr
]);
637 enic_log_q_error(enic
);
639 /* schedule recovery from WQ/RQ error */
640 schedule_work(&enic
->reset
);
645 static irqreturn_t
enic_isr_msix_notify(int irq
, void *data
)
647 struct enic
*enic
= data
;
648 unsigned int intr
= enic_msix_notify_intr(enic
);
650 vnic_intr_return_all_credits(&enic
->intr
[intr
]);
651 enic_notify_check(enic
);
656 static inline void enic_queue_wq_skb_cont(struct enic
*enic
,
657 struct vnic_wq
*wq
, struct sk_buff
*skb
,
658 unsigned int len_left
, int loopback
)
662 /* Queue additional data fragments */
663 for (frag
= skb_shinfo(skb
)->frags
; len_left
; frag
++) {
664 len_left
-= frag
->size
;
665 enic_queue_wq_desc_cont(wq
, skb
,
666 pci_map_page(enic
->pdev
, frag
->page
,
667 frag
->page_offset
, frag
->size
,
670 (len_left
== 0), /* EOP? */
675 static inline void enic_queue_wq_skb_vlan(struct enic
*enic
,
676 struct vnic_wq
*wq
, struct sk_buff
*skb
,
677 int vlan_tag_insert
, unsigned int vlan_tag
, int loopback
)
679 unsigned int head_len
= skb_headlen(skb
);
680 unsigned int len_left
= skb
->len
- head_len
;
681 int eop
= (len_left
== 0);
683 /* Queue the main skb fragment. The fragments are no larger
684 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
685 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
686 * per fragment is queued.
688 enic_queue_wq_desc(wq
, skb
,
689 pci_map_single(enic
->pdev
, skb
->data
,
690 head_len
, PCI_DMA_TODEVICE
),
692 vlan_tag_insert
, vlan_tag
,
696 enic_queue_wq_skb_cont(enic
, wq
, skb
, len_left
, loopback
);
699 static inline void enic_queue_wq_skb_csum_l4(struct enic
*enic
,
700 struct vnic_wq
*wq
, struct sk_buff
*skb
,
701 int vlan_tag_insert
, unsigned int vlan_tag
, int loopback
)
703 unsigned int head_len
= skb_headlen(skb
);
704 unsigned int len_left
= skb
->len
- head_len
;
705 unsigned int hdr_len
= skb_checksum_start_offset(skb
);
706 unsigned int csum_offset
= hdr_len
+ skb
->csum_offset
;
707 int eop
= (len_left
== 0);
709 /* Queue the main skb fragment. The fragments are no larger
710 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
711 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
712 * per fragment is queued.
714 enic_queue_wq_desc_csum_l4(wq
, skb
,
715 pci_map_single(enic
->pdev
, skb
->data
,
716 head_len
, PCI_DMA_TODEVICE
),
720 vlan_tag_insert
, vlan_tag
,
724 enic_queue_wq_skb_cont(enic
, wq
, skb
, len_left
, loopback
);
727 static inline void enic_queue_wq_skb_tso(struct enic
*enic
,
728 struct vnic_wq
*wq
, struct sk_buff
*skb
, unsigned int mss
,
729 int vlan_tag_insert
, unsigned int vlan_tag
, int loopback
)
731 unsigned int frag_len_left
= skb_headlen(skb
);
732 unsigned int len_left
= skb
->len
- frag_len_left
;
733 unsigned int hdr_len
= skb_transport_offset(skb
) + tcp_hdrlen(skb
);
734 int eop
= (len_left
== 0);
737 unsigned int offset
= 0;
740 /* Preload TCP csum field with IP pseudo hdr calculated
741 * with IP length set to zero. HW will later add in length
742 * to each TCP segment resulting from the TSO.
745 if (skb
->protocol
== cpu_to_be16(ETH_P_IP
)) {
746 ip_hdr(skb
)->check
= 0;
747 tcp_hdr(skb
)->check
= ~csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
748 ip_hdr(skb
)->daddr
, 0, IPPROTO_TCP
, 0);
749 } else if (skb
->protocol
== cpu_to_be16(ETH_P_IPV6
)) {
750 tcp_hdr(skb
)->check
= ~csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
751 &ipv6_hdr(skb
)->daddr
, 0, IPPROTO_TCP
, 0);
754 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
755 * for the main skb fragment
757 while (frag_len_left
) {
758 len
= min(frag_len_left
, (unsigned int)WQ_ENET_MAX_DESC_LEN
);
759 dma_addr
= pci_map_single(enic
->pdev
, skb
->data
+ offset
,
760 len
, PCI_DMA_TODEVICE
);
761 enic_queue_wq_desc_tso(wq
, skb
,
765 vlan_tag_insert
, vlan_tag
,
766 eop
&& (len
== frag_len_left
), loopback
);
767 frag_len_left
-= len
;
774 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
775 * for additional data fragments
777 for (frag
= skb_shinfo(skb
)->frags
; len_left
; frag
++) {
778 len_left
-= frag
->size
;
779 frag_len_left
= frag
->size
;
780 offset
= frag
->page_offset
;
782 while (frag_len_left
) {
783 len
= min(frag_len_left
,
784 (unsigned int)WQ_ENET_MAX_DESC_LEN
);
785 dma_addr
= pci_map_page(enic
->pdev
, frag
->page
,
788 enic_queue_wq_desc_cont(wq
, skb
,
792 (len
== frag_len_left
), /* EOP? */
794 frag_len_left
-= len
;
800 static inline void enic_queue_wq_skb(struct enic
*enic
,
801 struct vnic_wq
*wq
, struct sk_buff
*skb
)
803 unsigned int mss
= skb_shinfo(skb
)->gso_size
;
804 unsigned int vlan_tag
= 0;
805 int vlan_tag_insert
= 0;
808 if (vlan_tx_tag_present(skb
)) {
809 /* VLAN tag from trunking driver */
811 vlan_tag
= vlan_tx_tag_get(skb
);
812 } else if (enic
->loop_enable
) {
813 vlan_tag
= enic
->loop_tag
;
818 enic_queue_wq_skb_tso(enic
, wq
, skb
, mss
,
819 vlan_tag_insert
, vlan_tag
, loopback
);
820 else if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
821 enic_queue_wq_skb_csum_l4(enic
, wq
, skb
,
822 vlan_tag_insert
, vlan_tag
, loopback
);
824 enic_queue_wq_skb_vlan(enic
, wq
, skb
,
825 vlan_tag_insert
, vlan_tag
, loopback
);
828 /* netif_tx_lock held, process context with BHs disabled, or BH */
829 static netdev_tx_t
enic_hard_start_xmit(struct sk_buff
*skb
,
830 struct net_device
*netdev
)
832 struct enic
*enic
= netdev_priv(netdev
);
833 struct vnic_wq
*wq
= &enic
->wq
[0];
841 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
842 * which is very likely. In the off chance it's going to take
843 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
846 if (skb_shinfo(skb
)->gso_size
== 0 &&
847 skb_shinfo(skb
)->nr_frags
+ 1 > ENIC_NON_TSO_MAX_DESC
&&
848 skb_linearize(skb
)) {
853 spin_lock_irqsave(&enic
->wq_lock
[0], flags
);
855 if (vnic_wq_desc_avail(wq
) <
856 skb_shinfo(skb
)->nr_frags
+ ENIC_DESC_MAX_SPLITS
) {
857 netif_stop_queue(netdev
);
858 /* This is a hard error, log it */
859 netdev_err(netdev
, "BUG! Tx ring full when queue awake!\n");
860 spin_unlock_irqrestore(&enic
->wq_lock
[0], flags
);
861 return NETDEV_TX_BUSY
;
864 enic_queue_wq_skb(enic
, wq
, skb
);
866 if (vnic_wq_desc_avail(wq
) < MAX_SKB_FRAGS
+ ENIC_DESC_MAX_SPLITS
)
867 netif_stop_queue(netdev
);
869 spin_unlock_irqrestore(&enic
->wq_lock
[0], flags
);
874 /* dev_base_lock rwlock held, nominally process context */
875 static struct net_device_stats
*enic_get_stats(struct net_device
*netdev
)
877 struct enic
*enic
= netdev_priv(netdev
);
878 struct net_device_stats
*net_stats
= &netdev
->stats
;
879 struct vnic_stats
*stats
;
881 enic_dev_stats_dump(enic
, &stats
);
883 net_stats
->tx_packets
= stats
->tx
.tx_frames_ok
;
884 net_stats
->tx_bytes
= stats
->tx
.tx_bytes_ok
;
885 net_stats
->tx_errors
= stats
->tx
.tx_errors
;
886 net_stats
->tx_dropped
= stats
->tx
.tx_drops
;
888 net_stats
->rx_packets
= stats
->rx
.rx_frames_ok
;
889 net_stats
->rx_bytes
= stats
->rx
.rx_bytes_ok
;
890 net_stats
->rx_errors
= stats
->rx
.rx_errors
;
891 net_stats
->multicast
= stats
->rx
.rx_multicast_frames_ok
;
892 net_stats
->rx_over_errors
= enic
->rq_truncated_pkts
;
893 net_stats
->rx_crc_errors
= enic
->rq_bad_fcs
;
894 net_stats
->rx_dropped
= stats
->rx
.rx_no_bufs
+ stats
->rx
.rx_drop
;
899 static void enic_reset_multicast_list(struct enic
*enic
)
905 static int enic_set_mac_addr(struct net_device
*netdev
, char *addr
)
907 struct enic
*enic
= netdev_priv(netdev
);
909 if (enic_is_dynamic(enic
)) {
910 if (!is_valid_ether_addr(addr
) && !is_zero_ether_addr(addr
))
911 return -EADDRNOTAVAIL
;
913 if (!is_valid_ether_addr(addr
))
914 return -EADDRNOTAVAIL
;
917 memcpy(netdev
->dev_addr
, addr
, netdev
->addr_len
);
922 static int enic_dev_add_station_addr(struct enic
*enic
)
926 if (is_valid_ether_addr(enic
->netdev
->dev_addr
)) {
927 spin_lock(&enic
->devcmd_lock
);
928 err
= vnic_dev_add_addr(enic
->vdev
, enic
->netdev
->dev_addr
);
929 spin_unlock(&enic
->devcmd_lock
);
935 static int enic_dev_del_station_addr(struct enic
*enic
)
939 if (is_valid_ether_addr(enic
->netdev
->dev_addr
)) {
940 spin_lock(&enic
->devcmd_lock
);
941 err
= vnic_dev_del_addr(enic
->vdev
, enic
->netdev
->dev_addr
);
942 spin_unlock(&enic
->devcmd_lock
);
948 static int enic_set_mac_address_dynamic(struct net_device
*netdev
, void *p
)
950 struct enic
*enic
= netdev_priv(netdev
);
951 struct sockaddr
*saddr
= p
;
952 char *addr
= saddr
->sa_data
;
955 if (netif_running(enic
->netdev
)) {
956 err
= enic_dev_del_station_addr(enic
);
961 err
= enic_set_mac_addr(netdev
, addr
);
965 if (netif_running(enic
->netdev
)) {
966 err
= enic_dev_add_station_addr(enic
);
974 static int enic_set_mac_address(struct net_device
*netdev
, void *p
)
976 struct sockaddr
*saddr
= p
;
977 char *addr
= saddr
->sa_data
;
978 struct enic
*enic
= netdev_priv(netdev
);
981 err
= enic_dev_del_station_addr(enic
);
985 err
= enic_set_mac_addr(netdev
, addr
);
989 return enic_dev_add_station_addr(enic
);
992 static int enic_dev_packet_filter(struct enic
*enic
, int directed
,
993 int multicast
, int broadcast
, int promisc
, int allmulti
)
997 spin_lock(&enic
->devcmd_lock
);
998 err
= vnic_dev_packet_filter(enic
->vdev
, directed
,
999 multicast
, broadcast
, promisc
, allmulti
);
1000 spin_unlock(&enic
->devcmd_lock
);
1005 static int enic_dev_add_addr(struct enic
*enic
, u8
*addr
)
1009 spin_lock(&enic
->devcmd_lock
);
1010 err
= vnic_dev_add_addr(enic
->vdev
, addr
);
1011 spin_unlock(&enic
->devcmd_lock
);
1016 static int enic_dev_del_addr(struct enic
*enic
, u8
*addr
)
1020 spin_lock(&enic
->devcmd_lock
);
1021 err
= vnic_dev_del_addr(enic
->vdev
, addr
);
1022 spin_unlock(&enic
->devcmd_lock
);
1027 static void enic_add_multicast_addr_list(struct enic
*enic
)
1029 struct net_device
*netdev
= enic
->netdev
;
1030 struct netdev_hw_addr
*ha
;
1031 unsigned int mc_count
= netdev_mc_count(netdev
);
1032 u8 mc_addr
[ENIC_MULTICAST_PERFECT_FILTERS
][ETH_ALEN
];
1035 if (mc_count
> ENIC_MULTICAST_PERFECT_FILTERS
) {
1036 netdev_warn(netdev
, "Registering only %d out of %d "
1037 "multicast addresses\n",
1038 ENIC_MULTICAST_PERFECT_FILTERS
, mc_count
);
1039 mc_count
= ENIC_MULTICAST_PERFECT_FILTERS
;
1042 /* Is there an easier way? Trying to minimize to
1043 * calls to add/del multicast addrs. We keep the
1044 * addrs from the last call in enic->mc_addr and
1045 * look for changes to add/del.
1049 netdev_for_each_mc_addr(ha
, netdev
) {
1052 memcpy(mc_addr
[i
++], ha
->addr
, ETH_ALEN
);
1055 for (i
= 0; i
< enic
->mc_count
; i
++) {
1056 for (j
= 0; j
< mc_count
; j
++)
1057 if (compare_ether_addr(enic
->mc_addr
[i
],
1061 enic_dev_del_addr(enic
, enic
->mc_addr
[i
]);
1064 for (i
= 0; i
< mc_count
; i
++) {
1065 for (j
= 0; j
< enic
->mc_count
; j
++)
1066 if (compare_ether_addr(mc_addr
[i
],
1067 enic
->mc_addr
[j
]) == 0)
1069 if (j
== enic
->mc_count
)
1070 enic_dev_add_addr(enic
, mc_addr
[i
]);
1073 /* Save the list to compare against next time
1076 for (i
= 0; i
< mc_count
; i
++)
1077 memcpy(enic
->mc_addr
[i
], mc_addr
[i
], ETH_ALEN
);
1079 enic
->mc_count
= mc_count
;
1082 static void enic_add_unicast_addr_list(struct enic
*enic
)
1084 struct net_device
*netdev
= enic
->netdev
;
1085 struct netdev_hw_addr
*ha
;
1086 unsigned int uc_count
= netdev_uc_count(netdev
);
1087 u8 uc_addr
[ENIC_UNICAST_PERFECT_FILTERS
][ETH_ALEN
];
1090 if (uc_count
> ENIC_UNICAST_PERFECT_FILTERS
) {
1091 netdev_warn(netdev
, "Registering only %d out of %d "
1092 "unicast addresses\n",
1093 ENIC_UNICAST_PERFECT_FILTERS
, uc_count
);
1094 uc_count
= ENIC_UNICAST_PERFECT_FILTERS
;
1097 /* Is there an easier way? Trying to minimize to
1098 * calls to add/del unicast addrs. We keep the
1099 * addrs from the last call in enic->uc_addr and
1100 * look for changes to add/del.
1104 netdev_for_each_uc_addr(ha
, netdev
) {
1107 memcpy(uc_addr
[i
++], ha
->addr
, ETH_ALEN
);
1110 for (i
= 0; i
< enic
->uc_count
; i
++) {
1111 for (j
= 0; j
< uc_count
; j
++)
1112 if (compare_ether_addr(enic
->uc_addr
[i
],
1116 enic_dev_del_addr(enic
, enic
->uc_addr
[i
]);
1119 for (i
= 0; i
< uc_count
; i
++) {
1120 for (j
= 0; j
< enic
->uc_count
; j
++)
1121 if (compare_ether_addr(uc_addr
[i
],
1122 enic
->uc_addr
[j
]) == 0)
1124 if (j
== enic
->uc_count
)
1125 enic_dev_add_addr(enic
, uc_addr
[i
]);
1128 /* Save the list to compare against next time
1131 for (i
= 0; i
< uc_count
; i
++)
1132 memcpy(enic
->uc_addr
[i
], uc_addr
[i
], ETH_ALEN
);
1134 enic
->uc_count
= uc_count
;
1137 /* netif_tx_lock held, BHs disabled */
1138 static void enic_set_rx_mode(struct net_device
*netdev
)
1140 struct enic
*enic
= netdev_priv(netdev
);
1142 int multicast
= (netdev
->flags
& IFF_MULTICAST
) ? 1 : 0;
1143 int broadcast
= (netdev
->flags
& IFF_BROADCAST
) ? 1 : 0;
1144 int promisc
= (netdev
->flags
& IFF_PROMISC
) ||
1145 netdev_uc_count(netdev
) > ENIC_UNICAST_PERFECT_FILTERS
;
1146 int allmulti
= (netdev
->flags
& IFF_ALLMULTI
) ||
1147 netdev_mc_count(netdev
) > ENIC_MULTICAST_PERFECT_FILTERS
;
1148 unsigned int flags
= netdev
->flags
|
1149 (allmulti
? IFF_ALLMULTI
: 0) |
1150 (promisc
? IFF_PROMISC
: 0);
1152 if (enic
->flags
!= flags
) {
1153 enic
->flags
= flags
;
1154 enic_dev_packet_filter(enic
, directed
,
1155 multicast
, broadcast
, promisc
, allmulti
);
1159 enic_add_unicast_addr_list(enic
);
1161 enic_add_multicast_addr_list(enic
);
1165 /* rtnl lock is held */
1166 static void enic_vlan_rx_register(struct net_device
*netdev
,
1167 struct vlan_group
*vlan_group
)
1169 struct enic
*enic
= netdev_priv(netdev
);
1170 enic
->vlan_group
= vlan_group
;
1173 /* rtnl lock is held */
1174 static void enic_vlan_rx_add_vid(struct net_device
*netdev
, u16 vid
)
1176 struct enic
*enic
= netdev_priv(netdev
);
1178 spin_lock(&enic
->devcmd_lock
);
1179 enic_add_vlan(enic
, vid
);
1180 spin_unlock(&enic
->devcmd_lock
);
1183 /* rtnl lock is held */
1184 static void enic_vlan_rx_kill_vid(struct net_device
*netdev
, u16 vid
)
1186 struct enic
*enic
= netdev_priv(netdev
);
1188 spin_lock(&enic
->devcmd_lock
);
1189 enic_del_vlan(enic
, vid
);
1190 spin_unlock(&enic
->devcmd_lock
);
1193 /* netif_tx_lock held, BHs disabled */
1194 static void enic_tx_timeout(struct net_device
*netdev
)
1196 struct enic
*enic
= netdev_priv(netdev
);
1197 schedule_work(&enic
->reset
);
1200 static int enic_vnic_dev_deinit(struct enic
*enic
)
1204 spin_lock(&enic
->devcmd_lock
);
1205 err
= vnic_dev_deinit(enic
->vdev
);
1206 spin_unlock(&enic
->devcmd_lock
);
1211 static int enic_dev_init_prov(struct enic
*enic
, struct vic_provinfo
*vp
)
1215 spin_lock(&enic
->devcmd_lock
);
1216 err
= vnic_dev_init_prov(enic
->vdev
,
1217 (u8
*)vp
, vic_provinfo_size(vp
));
1218 spin_unlock(&enic
->devcmd_lock
);
1223 static int enic_dev_init_done(struct enic
*enic
, int *done
, int *error
)
1227 spin_lock(&enic
->devcmd_lock
);
1228 err
= vnic_dev_init_done(enic
->vdev
, done
, error
);
1229 spin_unlock(&enic
->devcmd_lock
);
1234 static int enic_set_vf_mac(struct net_device
*netdev
, int vf
, u8
*mac
)
1236 struct enic
*enic
= netdev_priv(netdev
);
1238 if (vf
!= PORT_SELF_VF
)
1241 /* Ignore the vf argument for now. We can assume the request
1242 * is coming on a vf.
1244 if (is_valid_ether_addr(mac
)) {
1245 memcpy(enic
->pp
.vf_mac
, mac
, ETH_ALEN
);
1251 static int enic_set_port_profile(struct enic
*enic
, u8
*mac
)
1253 struct vic_provinfo
*vp
;
1254 u8 oui
[3] = VIC_PROVINFO_CISCO_OUI
;
1255 u16 os_type
= VIC_GENERIC_PROV_OS_TYPE_LINUX
;
1257 char client_mac_str
[18];
1261 err
= enic_vnic_dev_deinit(enic
);
1265 switch (enic
->pp
.request
) {
1267 case PORT_REQUEST_ASSOCIATE
:
1269 if (!(enic
->pp
.set
& ENIC_SET_NAME
) || !strlen(enic
->pp
.name
))
1272 if (!is_valid_ether_addr(mac
))
1273 return -EADDRNOTAVAIL
;
1275 vp
= vic_provinfo_alloc(GFP_KERNEL
, oui
,
1276 VIC_PROVINFO_GENERIC_TYPE
);
1280 vic_provinfo_add_tlv(vp
,
1281 VIC_GENERIC_PROV_TLV_PORT_PROFILE_NAME_STR
,
1282 strlen(enic
->pp
.name
) + 1, enic
->pp
.name
);
1284 if (!is_zero_ether_addr(enic
->pp
.mac_addr
))
1285 client_mac
= enic
->pp
.mac_addr
;
1289 vic_provinfo_add_tlv(vp
,
1290 VIC_GENERIC_PROV_TLV_CLIENT_MAC_ADDR
,
1291 ETH_ALEN
, client_mac
);
1293 sprintf(client_mac_str
, "%pM", client_mac
);
1294 vic_provinfo_add_tlv(vp
,
1295 VIC_GENERIC_PROV_TLV_CLUSTER_PORT_UUID_STR
,
1296 sizeof(client_mac_str
), client_mac_str
);
1298 if (enic
->pp
.set
& ENIC_SET_INSTANCE
) {
1299 sprintf(uuid_str
, "%pUB", enic
->pp
.instance_uuid
);
1300 vic_provinfo_add_tlv(vp
,
1301 VIC_GENERIC_PROV_TLV_CLIENT_UUID_STR
,
1302 sizeof(uuid_str
), uuid_str
);
1305 if (enic
->pp
.set
& ENIC_SET_HOST
) {
1306 sprintf(uuid_str
, "%pUB", enic
->pp
.host_uuid
);
1307 vic_provinfo_add_tlv(vp
,
1308 VIC_GENERIC_PROV_TLV_HOST_UUID_STR
,
1309 sizeof(uuid_str
), uuid_str
);
1312 os_type
= htons(os_type
);
1313 vic_provinfo_add_tlv(vp
,
1314 VIC_GENERIC_PROV_TLV_OS_TYPE
,
1315 sizeof(os_type
), &os_type
);
1317 err
= enic_dev_init_prov(enic
, vp
);
1318 vic_provinfo_free(vp
);
1322 enic
->pp
.set
|= ENIC_SET_APPLIED
;
1325 case PORT_REQUEST_DISASSOCIATE
:
1326 enic
->pp
.set
&= ~ENIC_SET_APPLIED
;
1336 static int enic_set_vf_port(struct net_device
*netdev
, int vf
,
1337 struct nlattr
*port
[])
1339 struct enic
*enic
= netdev_priv(netdev
);
1340 struct enic_port_profile new_pp
;
1343 memset(&new_pp
, 0, sizeof(new_pp
));
1345 if (port
[IFLA_PORT_REQUEST
]) {
1346 new_pp
.set
|= ENIC_SET_REQUEST
;
1347 new_pp
.request
= nla_get_u8(port
[IFLA_PORT_REQUEST
]);
1350 if (port
[IFLA_PORT_PROFILE
]) {
1351 new_pp
.set
|= ENIC_SET_NAME
;
1352 memcpy(new_pp
.name
, nla_data(port
[IFLA_PORT_PROFILE
]),
1356 if (port
[IFLA_PORT_INSTANCE_UUID
]) {
1357 new_pp
.set
|= ENIC_SET_INSTANCE
;
1358 memcpy(new_pp
.instance_uuid
,
1359 nla_data(port
[IFLA_PORT_INSTANCE_UUID
]), PORT_UUID_MAX
);
1362 if (port
[IFLA_PORT_HOST_UUID
]) {
1363 new_pp
.set
|= ENIC_SET_HOST
;
1364 memcpy(new_pp
.host_uuid
,
1365 nla_data(port
[IFLA_PORT_HOST_UUID
]), PORT_UUID_MAX
);
1368 /* don't support VFs, yet */
1369 if (vf
!= PORT_SELF_VF
)
1372 if (!(new_pp
.set
& ENIC_SET_REQUEST
))
1375 if (new_pp
.request
== PORT_REQUEST_ASSOCIATE
) {
1376 /* Special case handling */
1377 if (!is_zero_ether_addr(enic
->pp
.vf_mac
))
1378 memcpy(new_pp
.mac_addr
, enic
->pp
.vf_mac
, ETH_ALEN
);
1380 if (is_zero_ether_addr(netdev
->dev_addr
))
1381 random_ether_addr(netdev
->dev_addr
);
1382 } else if (new_pp
.request
== PORT_REQUEST_DISASSOCIATE
) {
1383 if (!is_zero_ether_addr(enic
->pp
.mac_addr
))
1384 enic_dev_del_addr(enic
, enic
->pp
.mac_addr
);
1387 memcpy(&enic
->pp
, &new_pp
, sizeof(struct enic_port_profile
));
1389 err
= enic_set_port_profile(enic
, netdev
->dev_addr
);
1391 goto set_port_profile_cleanup
;
1393 if (!is_zero_ether_addr(enic
->pp
.mac_addr
))
1394 enic_dev_add_addr(enic
, enic
->pp
.mac_addr
);
1396 set_port_profile_cleanup
:
1397 memset(enic
->pp
.vf_mac
, 0, ETH_ALEN
);
1399 if (err
|| enic
->pp
.request
== PORT_REQUEST_DISASSOCIATE
) {
1400 memset(netdev
->dev_addr
, 0, ETH_ALEN
);
1401 memset(enic
->pp
.mac_addr
, 0, ETH_ALEN
);
1407 static int enic_get_vf_port(struct net_device
*netdev
, int vf
,
1408 struct sk_buff
*skb
)
1410 struct enic
*enic
= netdev_priv(netdev
);
1411 int err
, error
, done
;
1412 u16 response
= PORT_PROFILE_RESPONSE_SUCCESS
;
1414 if (!(enic
->pp
.set
& ENIC_SET_APPLIED
))
1417 err
= enic_dev_init_done(enic
, &done
, &error
);
1424 response
= PORT_PROFILE_RESPONSE_INPROGRESS
;
1427 response
= PORT_PROFILE_RESPONSE_INVALID
;
1430 response
= PORT_PROFILE_RESPONSE_BADSTATE
;
1433 response
= PORT_PROFILE_RESPONSE_INSUFFICIENT_RESOURCES
;
1436 response
= PORT_PROFILE_RESPONSE_ERROR
;
1440 NLA_PUT_U16(skb
, IFLA_PORT_REQUEST
, enic
->pp
.request
);
1441 NLA_PUT_U16(skb
, IFLA_PORT_RESPONSE
, response
);
1442 if (enic
->pp
.set
& ENIC_SET_NAME
)
1443 NLA_PUT(skb
, IFLA_PORT_PROFILE
, PORT_PROFILE_MAX
,
1445 if (enic
->pp
.set
& ENIC_SET_INSTANCE
)
1446 NLA_PUT(skb
, IFLA_PORT_INSTANCE_UUID
, PORT_UUID_MAX
,
1447 enic
->pp
.instance_uuid
);
1448 if (enic
->pp
.set
& ENIC_SET_HOST
)
1449 NLA_PUT(skb
, IFLA_PORT_HOST_UUID
, PORT_UUID_MAX
,
1450 enic
->pp
.host_uuid
);
1458 static void enic_free_rq_buf(struct vnic_rq
*rq
, struct vnic_rq_buf
*buf
)
1460 struct enic
*enic
= vnic_dev_priv(rq
->vdev
);
1465 pci_unmap_single(enic
->pdev
, buf
->dma_addr
,
1466 buf
->len
, PCI_DMA_FROMDEVICE
);
1467 dev_kfree_skb_any(buf
->os_buf
);
1470 static int enic_rq_alloc_buf(struct vnic_rq
*rq
)
1472 struct enic
*enic
= vnic_dev_priv(rq
->vdev
);
1473 struct net_device
*netdev
= enic
->netdev
;
1474 struct sk_buff
*skb
;
1475 unsigned int len
= netdev
->mtu
+ VLAN_ETH_HLEN
;
1476 unsigned int os_buf_index
= 0;
1477 dma_addr_t dma_addr
;
1479 skb
= netdev_alloc_skb_ip_align(netdev
, len
);
1483 dma_addr
= pci_map_single(enic
->pdev
, skb
->data
,
1484 len
, PCI_DMA_FROMDEVICE
);
1486 enic_queue_rq_desc(rq
, skb
, os_buf_index
,
1492 static int enic_rq_alloc_buf_a1(struct vnic_rq
*rq
)
1494 struct rq_enet_desc
*desc
= vnic_rq_next_desc(rq
);
1496 if (vnic_rq_posting_soon(rq
)) {
1498 /* SW workaround for A0 HW erratum: if we're just about
1499 * to write posted_index, insert a dummy desc
1503 rq_enet_desc_enc(desc
, 0, RQ_ENET_TYPE_RESV2
, 0);
1504 vnic_rq_post(rq
, 0, 0, 0, 0);
1506 return enic_rq_alloc_buf(rq
);
1512 static int enic_dev_hw_version(struct enic
*enic
,
1513 enum vnic_dev_hw_version
*hw_ver
)
1517 spin_lock(&enic
->devcmd_lock
);
1518 err
= vnic_dev_hw_version(enic
->vdev
, hw_ver
);
1519 spin_unlock(&enic
->devcmd_lock
);
1524 static int enic_set_rq_alloc_buf(struct enic
*enic
)
1526 enum vnic_dev_hw_version hw_ver
;
1529 err
= enic_dev_hw_version(enic
, &hw_ver
);
1534 case VNIC_DEV_HW_VER_A1
:
1535 enic
->rq_alloc_buf
= enic_rq_alloc_buf_a1
;
1537 case VNIC_DEV_HW_VER_A2
:
1538 case VNIC_DEV_HW_VER_UNKNOWN
:
1539 enic
->rq_alloc_buf
= enic_rq_alloc_buf
;
1548 static void enic_rq_indicate_buf(struct vnic_rq
*rq
,
1549 struct cq_desc
*cq_desc
, struct vnic_rq_buf
*buf
,
1550 int skipped
, void *opaque
)
1552 struct enic
*enic
= vnic_dev_priv(rq
->vdev
);
1553 struct net_device
*netdev
= enic
->netdev
;
1554 struct sk_buff
*skb
;
1556 u8 type
, color
, eop
, sop
, ingress_port
, vlan_stripped
;
1557 u8 fcoe
, fcoe_sof
, fcoe_fc_crc_ok
, fcoe_enc_error
, fcoe_eof
;
1558 u8 tcp_udp_csum_ok
, udp
, tcp
, ipv4_csum_ok
;
1559 u8 ipv6
, ipv4
, ipv4_fragment
, fcs_ok
, rss_type
, csum_not_calc
;
1561 u16 q_number
, completed_index
, bytes_written
, vlan_tci
, checksum
;
1568 prefetch(skb
->data
- NET_IP_ALIGN
);
1569 pci_unmap_single(enic
->pdev
, buf
->dma_addr
,
1570 buf
->len
, PCI_DMA_FROMDEVICE
);
1572 cq_enet_rq_desc_dec((struct cq_enet_rq_desc
*)cq_desc
,
1573 &type
, &color
, &q_number
, &completed_index
,
1574 &ingress_port
, &fcoe
, &eop
, &sop
, &rss_type
,
1575 &csum_not_calc
, &rss_hash
, &bytes_written
,
1576 &packet_error
, &vlan_stripped
, &vlan_tci
, &checksum
,
1577 &fcoe_sof
, &fcoe_fc_crc_ok
, &fcoe_enc_error
,
1578 &fcoe_eof
, &tcp_udp_csum_ok
, &udp
, &tcp
,
1579 &ipv4_csum_ok
, &ipv6
, &ipv4
, &ipv4_fragment
,
1585 if (bytes_written
> 0)
1587 else if (bytes_written
== 0)
1588 enic
->rq_truncated_pkts
++;
1591 dev_kfree_skb_any(skb
);
1596 if (eop
&& bytes_written
> 0) {
1601 skb_put(skb
, bytes_written
);
1602 skb
->protocol
= eth_type_trans(skb
, netdev
);
1604 if (enic
->csum_rx_enabled
&& !csum_not_calc
) {
1605 skb
->csum
= htons(checksum
);
1606 skb
->ip_summed
= CHECKSUM_COMPLETE
;
1611 if (enic
->vlan_group
&& vlan_stripped
&&
1612 (vlan_tci
& CQ_ENET_RQ_DESC_VLAN_TCI_VLAN_MASK
)) {
1614 if (netdev
->features
& NETIF_F_GRO
)
1615 vlan_gro_receive(&enic
->napi
[q_number
],
1616 enic
->vlan_group
, vlan_tci
, skb
);
1618 vlan_hwaccel_receive_skb(skb
,
1619 enic
->vlan_group
, vlan_tci
);
1623 if (netdev
->features
& NETIF_F_GRO
)
1624 napi_gro_receive(&enic
->napi
[q_number
], skb
);
1626 netif_receive_skb(skb
);
1634 dev_kfree_skb_any(skb
);
1638 static int enic_rq_service(struct vnic_dev
*vdev
, struct cq_desc
*cq_desc
,
1639 u8 type
, u16 q_number
, u16 completed_index
, void *opaque
)
1641 struct enic
*enic
= vnic_dev_priv(vdev
);
1643 vnic_rq_service(&enic
->rq
[q_number
], cq_desc
,
1644 completed_index
, VNIC_RQ_RETURN_DESC
,
1645 enic_rq_indicate_buf
, opaque
);
1650 static int enic_poll(struct napi_struct
*napi
, int budget
)
1652 struct net_device
*netdev
= napi
->dev
;
1653 struct enic
*enic
= netdev_priv(netdev
);
1654 unsigned int cq_rq
= enic_cq_rq(enic
, 0);
1655 unsigned int cq_wq
= enic_cq_wq(enic
, 0);
1656 unsigned int intr
= enic_legacy_io_intr();
1657 unsigned int rq_work_to_do
= budget
;
1658 unsigned int wq_work_to_do
= -1; /* no limit */
1659 unsigned int work_done
, rq_work_done
, wq_work_done
;
1662 /* Service RQ (first) and WQ
1665 rq_work_done
= vnic_cq_service(&enic
->cq
[cq_rq
],
1666 rq_work_to_do
, enic_rq_service
, NULL
);
1668 wq_work_done
= vnic_cq_service(&enic
->cq
[cq_wq
],
1669 wq_work_to_do
, enic_wq_service
, NULL
);
1671 /* Accumulate intr event credits for this polling
1672 * cycle. An intr event is the completion of a
1673 * a WQ or RQ packet.
1676 work_done
= rq_work_done
+ wq_work_done
;
1679 vnic_intr_return_credits(&enic
->intr
[intr
],
1681 0 /* don't unmask intr */,
1682 0 /* don't reset intr timer */);
1684 err
= vnic_rq_fill(&enic
->rq
[0], enic
->rq_alloc_buf
);
1686 /* Buffer allocation failed. Stay in polling
1687 * mode so we can try to fill the ring again.
1691 rq_work_done
= rq_work_to_do
;
1693 if (rq_work_done
< rq_work_to_do
) {
1695 /* Some work done, but not enough to stay in polling,
1699 napi_complete(napi
);
1700 vnic_intr_unmask(&enic
->intr
[intr
]);
1703 return rq_work_done
;
1706 static int enic_poll_msix(struct napi_struct
*napi
, int budget
)
1708 struct net_device
*netdev
= napi
->dev
;
1709 struct enic
*enic
= netdev_priv(netdev
);
1710 unsigned int rq
= (napi
- &enic
->napi
[0]);
1711 unsigned int cq
= enic_cq_rq(enic
, rq
);
1712 unsigned int intr
= enic_msix_rq_intr(enic
, rq
);
1713 unsigned int work_to_do
= budget
;
1714 unsigned int work_done
;
1720 work_done
= vnic_cq_service(&enic
->cq
[cq
],
1721 work_to_do
, enic_rq_service
, NULL
);
1723 /* Return intr event credits for this polling
1724 * cycle. An intr event is the completion of a
1729 vnic_intr_return_credits(&enic
->intr
[intr
],
1731 0 /* don't unmask intr */,
1732 0 /* don't reset intr timer */);
1734 err
= vnic_rq_fill(&enic
->rq
[rq
], enic
->rq_alloc_buf
);
1736 /* Buffer allocation failed. Stay in polling mode
1737 * so we can try to fill the ring again.
1741 work_done
= work_to_do
;
1743 if (work_done
< work_to_do
) {
1745 /* Some work done, but not enough to stay in polling,
1749 napi_complete(napi
);
1750 vnic_intr_unmask(&enic
->intr
[intr
]);
1756 static void enic_notify_timer(unsigned long data
)
1758 struct enic
*enic
= (struct enic
*)data
;
1760 enic_notify_check(enic
);
1762 mod_timer(&enic
->notify_timer
,
1763 round_jiffies(jiffies
+ ENIC_NOTIFY_TIMER_PERIOD
));
1766 static void enic_free_intr(struct enic
*enic
)
1768 struct net_device
*netdev
= enic
->netdev
;
1771 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
1772 case VNIC_DEV_INTR_MODE_INTX
:
1773 free_irq(enic
->pdev
->irq
, netdev
);
1775 case VNIC_DEV_INTR_MODE_MSI
:
1776 free_irq(enic
->pdev
->irq
, enic
);
1778 case VNIC_DEV_INTR_MODE_MSIX
:
1779 for (i
= 0; i
< ARRAY_SIZE(enic
->msix
); i
++)
1780 if (enic
->msix
[i
].requested
)
1781 free_irq(enic
->msix_entry
[i
].vector
,
1782 enic
->msix
[i
].devid
);
1789 static int enic_request_intr(struct enic
*enic
)
1791 struct net_device
*netdev
= enic
->netdev
;
1792 unsigned int i
, intr
;
1795 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
1797 case VNIC_DEV_INTR_MODE_INTX
:
1799 err
= request_irq(enic
->pdev
->irq
, enic_isr_legacy
,
1800 IRQF_SHARED
, netdev
->name
, netdev
);
1803 case VNIC_DEV_INTR_MODE_MSI
:
1805 err
= request_irq(enic
->pdev
->irq
, enic_isr_msi
,
1806 0, netdev
->name
, enic
);
1809 case VNIC_DEV_INTR_MODE_MSIX
:
1811 for (i
= 0; i
< enic
->rq_count
; i
++) {
1812 intr
= enic_msix_rq_intr(enic
, i
);
1813 sprintf(enic
->msix
[intr
].devname
,
1814 "%.11s-rx-%d", netdev
->name
, i
);
1815 enic
->msix
[intr
].isr
= enic_isr_msix_rq
;
1816 enic
->msix
[intr
].devid
= &enic
->napi
[i
];
1819 for (i
= 0; i
< enic
->wq_count
; i
++) {
1820 intr
= enic_msix_wq_intr(enic
, i
);
1821 sprintf(enic
->msix
[intr
].devname
,
1822 "%.11s-tx-%d", netdev
->name
, i
);
1823 enic
->msix
[intr
].isr
= enic_isr_msix_wq
;
1824 enic
->msix
[intr
].devid
= enic
;
1827 intr
= enic_msix_err_intr(enic
);
1828 sprintf(enic
->msix
[intr
].devname
,
1829 "%.11s-err", netdev
->name
);
1830 enic
->msix
[intr
].isr
= enic_isr_msix_err
;
1831 enic
->msix
[intr
].devid
= enic
;
1833 intr
= enic_msix_notify_intr(enic
);
1834 sprintf(enic
->msix
[intr
].devname
,
1835 "%.11s-notify", netdev
->name
);
1836 enic
->msix
[intr
].isr
= enic_isr_msix_notify
;
1837 enic
->msix
[intr
].devid
= enic
;
1839 for (i
= 0; i
< ARRAY_SIZE(enic
->msix
); i
++)
1840 enic
->msix
[i
].requested
= 0;
1842 for (i
= 0; i
< enic
->intr_count
; i
++) {
1843 err
= request_irq(enic
->msix_entry
[i
].vector
,
1844 enic
->msix
[i
].isr
, 0,
1845 enic
->msix
[i
].devname
,
1846 enic
->msix
[i
].devid
);
1848 enic_free_intr(enic
);
1851 enic
->msix
[i
].requested
= 1;
1863 static void enic_synchronize_irqs(struct enic
*enic
)
1867 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
1868 case VNIC_DEV_INTR_MODE_INTX
:
1869 case VNIC_DEV_INTR_MODE_MSI
:
1870 synchronize_irq(enic
->pdev
->irq
);
1872 case VNIC_DEV_INTR_MODE_MSIX
:
1873 for (i
= 0; i
< enic
->intr_count
; i
++)
1874 synchronize_irq(enic
->msix_entry
[i
].vector
);
1881 static int enic_dev_notify_set(struct enic
*enic
)
1885 spin_lock(&enic
->devcmd_lock
);
1886 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
1887 case VNIC_DEV_INTR_MODE_INTX
:
1888 err
= vnic_dev_notify_set(enic
->vdev
,
1889 enic_legacy_notify_intr());
1891 case VNIC_DEV_INTR_MODE_MSIX
:
1892 err
= vnic_dev_notify_set(enic
->vdev
,
1893 enic_msix_notify_intr(enic
));
1896 err
= vnic_dev_notify_set(enic
->vdev
, -1 /* no intr */);
1899 spin_unlock(&enic
->devcmd_lock
);
1904 static int enic_dev_notify_unset(struct enic
*enic
)
1908 spin_lock(&enic
->devcmd_lock
);
1909 err
= vnic_dev_notify_unset(enic
->vdev
);
1910 spin_unlock(&enic
->devcmd_lock
);
1915 static int enic_dev_enable(struct enic
*enic
)
1919 spin_lock(&enic
->devcmd_lock
);
1920 err
= vnic_dev_enable_wait(enic
->vdev
);
1921 spin_unlock(&enic
->devcmd_lock
);
1926 static int enic_dev_disable(struct enic
*enic
)
1930 spin_lock(&enic
->devcmd_lock
);
1931 err
= vnic_dev_disable(enic
->vdev
);
1932 spin_unlock(&enic
->devcmd_lock
);
1937 static void enic_notify_timer_start(struct enic
*enic
)
1939 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
1940 case VNIC_DEV_INTR_MODE_MSI
:
1941 mod_timer(&enic
->notify_timer
, jiffies
);
1944 /* Using intr for notification for INTx/MSI-X */
1949 /* rtnl lock is held, process context */
1950 static int enic_open(struct net_device
*netdev
)
1952 struct enic
*enic
= netdev_priv(netdev
);
1956 err
= enic_request_intr(enic
);
1958 netdev_err(netdev
, "Unable to request irq.\n");
1962 err
= enic_dev_notify_set(enic
);
1965 "Failed to alloc notify buffer, aborting.\n");
1966 goto err_out_free_intr
;
1969 for (i
= 0; i
< enic
->rq_count
; i
++) {
1970 vnic_rq_fill(&enic
->rq
[i
], enic
->rq_alloc_buf
);
1971 /* Need at least one buffer on ring to get going */
1972 if (vnic_rq_desc_used(&enic
->rq
[i
]) == 0) {
1973 netdev_err(netdev
, "Unable to alloc receive buffers\n");
1975 goto err_out_notify_unset
;
1979 for (i
= 0; i
< enic
->wq_count
; i
++)
1980 vnic_wq_enable(&enic
->wq
[i
]);
1981 for (i
= 0; i
< enic
->rq_count
; i
++)
1982 vnic_rq_enable(&enic
->rq
[i
]);
1984 if (enic_is_dynamic(enic
) && !is_zero_ether_addr(enic
->pp
.mac_addr
))
1985 enic_dev_add_addr(enic
, enic
->pp
.mac_addr
);
1987 enic_dev_add_station_addr(enic
);
1988 enic_set_rx_mode(netdev
);
1990 netif_wake_queue(netdev
);
1992 for (i
= 0; i
< enic
->rq_count
; i
++)
1993 napi_enable(&enic
->napi
[i
]);
1995 enic_dev_enable(enic
);
1997 for (i
= 0; i
< enic
->intr_count
; i
++)
1998 vnic_intr_unmask(&enic
->intr
[i
]);
2000 enic_notify_timer_start(enic
);
2004 err_out_notify_unset
:
2005 enic_dev_notify_unset(enic
);
2007 enic_free_intr(enic
);
2012 /* rtnl lock is held, process context */
2013 static int enic_stop(struct net_device
*netdev
)
2015 struct enic
*enic
= netdev_priv(netdev
);
2019 for (i
= 0; i
< enic
->intr_count
; i
++) {
2020 vnic_intr_mask(&enic
->intr
[i
]);
2021 (void)vnic_intr_masked(&enic
->intr
[i
]); /* flush write */
2024 enic_synchronize_irqs(enic
);
2026 del_timer_sync(&enic
->notify_timer
);
2028 enic_dev_disable(enic
);
2030 for (i
= 0; i
< enic
->rq_count
; i
++)
2031 napi_disable(&enic
->napi
[i
]);
2033 netif_carrier_off(netdev
);
2034 netif_tx_disable(netdev
);
2035 if (enic_is_dynamic(enic
) && !is_zero_ether_addr(enic
->pp
.mac_addr
))
2036 enic_dev_del_addr(enic
, enic
->pp
.mac_addr
);
2038 enic_dev_del_station_addr(enic
);
2040 for (i
= 0; i
< enic
->wq_count
; i
++) {
2041 err
= vnic_wq_disable(&enic
->wq
[i
]);
2045 for (i
= 0; i
< enic
->rq_count
; i
++) {
2046 err
= vnic_rq_disable(&enic
->rq
[i
]);
2051 enic_dev_notify_unset(enic
);
2052 enic_free_intr(enic
);
2054 for (i
= 0; i
< enic
->wq_count
; i
++)
2055 vnic_wq_clean(&enic
->wq
[i
], enic_free_wq_buf
);
2056 for (i
= 0; i
< enic
->rq_count
; i
++)
2057 vnic_rq_clean(&enic
->rq
[i
], enic_free_rq_buf
);
2058 for (i
= 0; i
< enic
->cq_count
; i
++)
2059 vnic_cq_clean(&enic
->cq
[i
]);
2060 for (i
= 0; i
< enic
->intr_count
; i
++)
2061 vnic_intr_clean(&enic
->intr
[i
]);
2066 static int enic_change_mtu(struct net_device
*netdev
, int new_mtu
)
2068 struct enic
*enic
= netdev_priv(netdev
);
2069 int running
= netif_running(netdev
);
2071 if (new_mtu
< ENIC_MIN_MTU
|| new_mtu
> ENIC_MAX_MTU
)
2077 netdev
->mtu
= new_mtu
;
2079 if (netdev
->mtu
> enic
->port_mtu
)
2081 "interface MTU (%d) set higher than port MTU (%d)\n",
2082 netdev
->mtu
, enic
->port_mtu
);
2090 #ifdef CONFIG_NET_POLL_CONTROLLER
2091 static void enic_poll_controller(struct net_device
*netdev
)
2093 struct enic
*enic
= netdev_priv(netdev
);
2094 struct vnic_dev
*vdev
= enic
->vdev
;
2095 unsigned int i
, intr
;
2097 switch (vnic_dev_get_intr_mode(vdev
)) {
2098 case VNIC_DEV_INTR_MODE_MSIX
:
2099 for (i
= 0; i
< enic
->rq_count
; i
++) {
2100 intr
= enic_msix_rq_intr(enic
, i
);
2101 enic_isr_msix_rq(enic
->msix_entry
[intr
].vector
,
2104 intr
= enic_msix_wq_intr(enic
, i
);
2105 enic_isr_msix_wq(enic
->msix_entry
[intr
].vector
, enic
);
2107 case VNIC_DEV_INTR_MODE_MSI
:
2108 enic_isr_msi(enic
->pdev
->irq
, enic
);
2110 case VNIC_DEV_INTR_MODE_INTX
:
2111 enic_isr_legacy(enic
->pdev
->irq
, netdev
);
2119 static int enic_dev_wait(struct vnic_dev
*vdev
,
2120 int (*start
)(struct vnic_dev
*, int),
2121 int (*finished
)(struct vnic_dev
*, int *),
2128 BUG_ON(in_interrupt());
2130 err
= start(vdev
, arg
);
2134 /* Wait for func to complete...2 seconds max
2137 time
= jiffies
+ (HZ
* 2);
2140 err
= finished(vdev
, &done
);
2147 schedule_timeout_uninterruptible(HZ
/ 10);
2149 } while (time_after(time
, jiffies
));
2154 static int enic_dev_open(struct enic
*enic
)
2158 err
= enic_dev_wait(enic
->vdev
, vnic_dev_open
,
2159 vnic_dev_open_done
, 0);
2161 dev_err(enic_get_dev(enic
), "vNIC device open failed, err %d\n",
2167 static int enic_dev_hang_reset(struct enic
*enic
)
2171 err
= enic_dev_wait(enic
->vdev
, vnic_dev_hang_reset
,
2172 vnic_dev_hang_reset_done
, 0);
2174 netdev_err(enic
->netdev
, "vNIC hang reset failed, err %d\n",
2180 static int enic_set_rsskey(struct enic
*enic
)
2182 dma_addr_t rss_key_buf_pa
;
2183 union vnic_rss_key
*rss_key_buf_va
= NULL
;
2184 union vnic_rss_key rss_key
= {
2185 .key
[0].b
= {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
2186 .key
[1].b
= {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
2187 .key
[2].b
= {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
2188 .key
[3].b
= {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
2192 rss_key_buf_va
= pci_alloc_consistent(enic
->pdev
,
2193 sizeof(union vnic_rss_key
), &rss_key_buf_pa
);
2194 if (!rss_key_buf_va
)
2197 memcpy(rss_key_buf_va
, &rss_key
, sizeof(union vnic_rss_key
));
2199 spin_lock(&enic
->devcmd_lock
);
2200 err
= enic_set_rss_key(enic
,
2202 sizeof(union vnic_rss_key
));
2203 spin_unlock(&enic
->devcmd_lock
);
2205 pci_free_consistent(enic
->pdev
, sizeof(union vnic_rss_key
),
2206 rss_key_buf_va
, rss_key_buf_pa
);
2211 static int enic_set_rsscpu(struct enic
*enic
, u8 rss_hash_bits
)
2213 dma_addr_t rss_cpu_buf_pa
;
2214 union vnic_rss_cpu
*rss_cpu_buf_va
= NULL
;
2218 rss_cpu_buf_va
= pci_alloc_consistent(enic
->pdev
,
2219 sizeof(union vnic_rss_cpu
), &rss_cpu_buf_pa
);
2220 if (!rss_cpu_buf_va
)
2223 for (i
= 0; i
< (1 << rss_hash_bits
); i
++)
2224 (*rss_cpu_buf_va
).cpu
[i
/4].b
[i
%4] = i
% enic
->rq_count
;
2226 spin_lock(&enic
->devcmd_lock
);
2227 err
= enic_set_rss_cpu(enic
,
2229 sizeof(union vnic_rss_cpu
));
2230 spin_unlock(&enic
->devcmd_lock
);
2232 pci_free_consistent(enic
->pdev
, sizeof(union vnic_rss_cpu
),
2233 rss_cpu_buf_va
, rss_cpu_buf_pa
);
2238 static int enic_set_niccfg(struct enic
*enic
, u8 rss_default_cpu
,
2239 u8 rss_hash_type
, u8 rss_hash_bits
, u8 rss_base_cpu
, u8 rss_enable
)
2241 const u8 tso_ipid_split_en
= 0;
2242 const u8 ig_vlan_strip_en
= 1;
2245 /* Enable VLAN tag stripping.
2248 spin_lock(&enic
->devcmd_lock
);
2249 err
= enic_set_nic_cfg(enic
,
2250 rss_default_cpu
, rss_hash_type
,
2251 rss_hash_bits
, rss_base_cpu
,
2252 rss_enable
, tso_ipid_split_en
,
2254 spin_unlock(&enic
->devcmd_lock
);
2259 static int enic_set_rss_nic_cfg(struct enic
*enic
)
2261 struct device
*dev
= enic_get_dev(enic
);
2262 const u8 rss_default_cpu
= 0;
2263 const u8 rss_hash_type
= NIC_CFG_RSS_HASH_TYPE_IPV4
|
2264 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4
|
2265 NIC_CFG_RSS_HASH_TYPE_IPV6
|
2266 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6
;
2267 const u8 rss_hash_bits
= 7;
2268 const u8 rss_base_cpu
= 0;
2269 u8 rss_enable
= ENIC_SETTING(enic
, RSS
) && (enic
->rq_count
> 1);
2272 if (!enic_set_rsskey(enic
)) {
2273 if (enic_set_rsscpu(enic
, rss_hash_bits
)) {
2275 dev_warn(dev
, "RSS disabled, "
2276 "Failed to set RSS cpu indirection table.");
2280 dev_warn(dev
, "RSS disabled, Failed to set RSS key.\n");
2284 return enic_set_niccfg(enic
, rss_default_cpu
, rss_hash_type
,
2285 rss_hash_bits
, rss_base_cpu
, rss_enable
);
2288 static int enic_dev_hang_notify(struct enic
*enic
)
2292 spin_lock(&enic
->devcmd_lock
);
2293 err
= vnic_dev_hang_notify(enic
->vdev
);
2294 spin_unlock(&enic
->devcmd_lock
);
2299 static int enic_dev_set_ig_vlan_rewrite_mode(struct enic
*enic
)
2303 spin_lock(&enic
->devcmd_lock
);
2304 err
= vnic_dev_set_ig_vlan_rewrite_mode(enic
->vdev
,
2305 IG_VLAN_REWRITE_MODE_PRIORITY_TAG_DEFAULT_VLAN
);
2306 spin_unlock(&enic
->devcmd_lock
);
2311 static void enic_reset(struct work_struct
*work
)
2313 struct enic
*enic
= container_of(work
, struct enic
, reset
);
2315 if (!netif_running(enic
->netdev
))
2320 enic_dev_hang_notify(enic
);
2321 enic_stop(enic
->netdev
);
2322 enic_dev_hang_reset(enic
);
2323 enic_reset_multicast_list(enic
);
2324 enic_init_vnic_resources(enic
);
2325 enic_set_rss_nic_cfg(enic
);
2326 enic_dev_set_ig_vlan_rewrite_mode(enic
);
2327 enic_open(enic
->netdev
);
2332 static int enic_set_intr_mode(struct enic
*enic
)
2334 unsigned int n
= min_t(unsigned int, enic
->rq_count
, ENIC_RQ_MAX
);
2338 /* Set interrupt mode (INTx, MSI, MSI-X) depending
2339 * on system capabilities.
2343 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
2344 * (the second to last INTR is used for WQ/RQ errors)
2345 * (the last INTR is used for notifications)
2348 BUG_ON(ARRAY_SIZE(enic
->msix_entry
) < n
+ m
+ 2);
2349 for (i
= 0; i
< n
+ m
+ 2; i
++)
2350 enic
->msix_entry
[i
].entry
= i
;
2352 /* Use multiple RQs if RSS is enabled
2355 if (ENIC_SETTING(enic
, RSS
) &&
2356 enic
->config
.intr_mode
< 1 &&
2357 enic
->rq_count
>= n
&&
2358 enic
->wq_count
>= m
&&
2359 enic
->cq_count
>= n
+ m
&&
2360 enic
->intr_count
>= n
+ m
+ 2) {
2362 if (!pci_enable_msix(enic
->pdev
, enic
->msix_entry
, n
+ m
+ 2)) {
2366 enic
->cq_count
= n
+ m
;
2367 enic
->intr_count
= n
+ m
+ 2;
2369 vnic_dev_set_intr_mode(enic
->vdev
,
2370 VNIC_DEV_INTR_MODE_MSIX
);
2376 if (enic
->config
.intr_mode
< 1 &&
2377 enic
->rq_count
>= 1 &&
2378 enic
->wq_count
>= m
&&
2379 enic
->cq_count
>= 1 + m
&&
2380 enic
->intr_count
>= 1 + m
+ 2) {
2381 if (!pci_enable_msix(enic
->pdev
, enic
->msix_entry
, 1 + m
+ 2)) {
2385 enic
->cq_count
= 1 + m
;
2386 enic
->intr_count
= 1 + m
+ 2;
2388 vnic_dev_set_intr_mode(enic
->vdev
,
2389 VNIC_DEV_INTR_MODE_MSIX
);
2397 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
2400 if (enic
->config
.intr_mode
< 2 &&
2401 enic
->rq_count
>= 1 &&
2402 enic
->wq_count
>= 1 &&
2403 enic
->cq_count
>= 2 &&
2404 enic
->intr_count
>= 1 &&
2405 !pci_enable_msi(enic
->pdev
)) {
2410 enic
->intr_count
= 1;
2412 vnic_dev_set_intr_mode(enic
->vdev
, VNIC_DEV_INTR_MODE_MSI
);
2419 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2420 * (the first INTR is used for WQ/RQ)
2421 * (the second INTR is used for WQ/RQ errors)
2422 * (the last INTR is used for notifications)
2425 if (enic
->config
.intr_mode
< 3 &&
2426 enic
->rq_count
>= 1 &&
2427 enic
->wq_count
>= 1 &&
2428 enic
->cq_count
>= 2 &&
2429 enic
->intr_count
>= 3) {
2434 enic
->intr_count
= 3;
2436 vnic_dev_set_intr_mode(enic
->vdev
, VNIC_DEV_INTR_MODE_INTX
);
2441 vnic_dev_set_intr_mode(enic
->vdev
, VNIC_DEV_INTR_MODE_UNKNOWN
);
2446 static void enic_clear_intr_mode(struct enic
*enic
)
2448 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
2449 case VNIC_DEV_INTR_MODE_MSIX
:
2450 pci_disable_msix(enic
->pdev
);
2452 case VNIC_DEV_INTR_MODE_MSI
:
2453 pci_disable_msi(enic
->pdev
);
2459 vnic_dev_set_intr_mode(enic
->vdev
, VNIC_DEV_INTR_MODE_UNKNOWN
);
2462 static const struct net_device_ops enic_netdev_dynamic_ops
= {
2463 .ndo_open
= enic_open
,
2464 .ndo_stop
= enic_stop
,
2465 .ndo_start_xmit
= enic_hard_start_xmit
,
2466 .ndo_get_stats
= enic_get_stats
,
2467 .ndo_validate_addr
= eth_validate_addr
,
2468 .ndo_set_rx_mode
= enic_set_rx_mode
,
2469 .ndo_set_multicast_list
= enic_set_rx_mode
,
2470 .ndo_set_mac_address
= enic_set_mac_address_dynamic
,
2471 .ndo_change_mtu
= enic_change_mtu
,
2472 .ndo_vlan_rx_register
= enic_vlan_rx_register
,
2473 .ndo_vlan_rx_add_vid
= enic_vlan_rx_add_vid
,
2474 .ndo_vlan_rx_kill_vid
= enic_vlan_rx_kill_vid
,
2475 .ndo_tx_timeout
= enic_tx_timeout
,
2476 .ndo_set_vf_port
= enic_set_vf_port
,
2477 .ndo_get_vf_port
= enic_get_vf_port
,
2479 .ndo_set_vf_mac
= enic_set_vf_mac
,
2481 #ifdef CONFIG_NET_POLL_CONTROLLER
2482 .ndo_poll_controller
= enic_poll_controller
,
2486 static const struct net_device_ops enic_netdev_ops
= {
2487 .ndo_open
= enic_open
,
2488 .ndo_stop
= enic_stop
,
2489 .ndo_start_xmit
= enic_hard_start_xmit
,
2490 .ndo_get_stats
= enic_get_stats
,
2491 .ndo_validate_addr
= eth_validate_addr
,
2492 .ndo_set_mac_address
= enic_set_mac_address
,
2493 .ndo_set_rx_mode
= enic_set_rx_mode
,
2494 .ndo_set_multicast_list
= enic_set_rx_mode
,
2495 .ndo_change_mtu
= enic_change_mtu
,
2496 .ndo_vlan_rx_register
= enic_vlan_rx_register
,
2497 .ndo_vlan_rx_add_vid
= enic_vlan_rx_add_vid
,
2498 .ndo_vlan_rx_kill_vid
= enic_vlan_rx_kill_vid
,
2499 .ndo_tx_timeout
= enic_tx_timeout
,
2500 #ifdef CONFIG_NET_POLL_CONTROLLER
2501 .ndo_poll_controller
= enic_poll_controller
,
2505 static void enic_dev_deinit(struct enic
*enic
)
2509 for (i
= 0; i
< enic
->rq_count
; i
++)
2510 netif_napi_del(&enic
->napi
[i
]);
2512 enic_free_vnic_resources(enic
);
2513 enic_clear_intr_mode(enic
);
2516 static int enic_dev_init(struct enic
*enic
)
2518 struct device
*dev
= enic_get_dev(enic
);
2519 struct net_device
*netdev
= enic
->netdev
;
2523 /* Get vNIC configuration
2526 err
= enic_get_vnic_config(enic
);
2528 dev_err(dev
, "Get vNIC configuration failed, aborting\n");
2532 /* Get available resource counts
2535 enic_get_res_counts(enic
);
2537 /* Set interrupt mode based on resource counts and system
2541 err
= enic_set_intr_mode(enic
);
2543 dev_err(dev
, "Failed to set intr mode based on resource "
2544 "counts and system capabilities, aborting\n");
2548 /* Allocate and configure vNIC resources
2551 err
= enic_alloc_vnic_resources(enic
);
2553 dev_err(dev
, "Failed to alloc vNIC resources, aborting\n");
2554 goto err_out_free_vnic_resources
;
2557 enic_init_vnic_resources(enic
);
2559 err
= enic_set_rq_alloc_buf(enic
);
2561 dev_err(dev
, "Failed to set RQ buffer allocator, aborting\n");
2562 goto err_out_free_vnic_resources
;
2565 err
= enic_set_rss_nic_cfg(enic
);
2567 dev_err(dev
, "Failed to config nic, aborting\n");
2568 goto err_out_free_vnic_resources
;
2571 err
= enic_dev_set_ig_vlan_rewrite_mode(enic
);
2574 "Failed to set ingress vlan rewrite mode, aborting.\n");
2575 goto err_out_free_vnic_resources
;
2578 switch (vnic_dev_get_intr_mode(enic
->vdev
)) {
2580 netif_napi_add(netdev
, &enic
->napi
[0], enic_poll
, 64);
2582 case VNIC_DEV_INTR_MODE_MSIX
:
2583 for (i
= 0; i
< enic
->rq_count
; i
++)
2584 netif_napi_add(netdev
, &enic
->napi
[i
],
2585 enic_poll_msix
, 64);
2591 err_out_free_vnic_resources
:
2592 enic_clear_intr_mode(enic
);
2593 enic_free_vnic_resources(enic
);
2598 static void enic_iounmap(struct enic
*enic
)
2602 for (i
= 0; i
< ARRAY_SIZE(enic
->bar
); i
++)
2603 if (enic
->bar
[i
].vaddr
)
2604 iounmap(enic
->bar
[i
].vaddr
);
2607 static int __devinit
enic_probe(struct pci_dev
*pdev
,
2608 const struct pci_device_id
*ent
)
2610 struct device
*dev
= &pdev
->dev
;
2611 struct net_device
*netdev
;
2617 /* Allocate net device structure and initialize. Private
2618 * instance data is initialized to zero.
2621 netdev
= alloc_etherdev(sizeof(struct enic
));
2623 pr_err("Etherdev alloc failed, aborting\n");
2627 pci_set_drvdata(pdev
, netdev
);
2629 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
2631 enic
= netdev_priv(netdev
);
2632 enic
->netdev
= netdev
;
2635 /* Setup PCI resources
2638 err
= pci_enable_device_mem(pdev
);
2640 dev_err(dev
, "Cannot enable PCI device, aborting\n");
2641 goto err_out_free_netdev
;
2644 err
= pci_request_regions(pdev
, DRV_NAME
);
2646 dev_err(dev
, "Cannot request PCI regions, aborting\n");
2647 goto err_out_disable_device
;
2650 pci_set_master(pdev
);
2652 /* Query PCI controller on system for DMA addressing
2653 * limitation for the device. Try 40-bit first, and
2657 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(40));
2659 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
2661 dev_err(dev
, "No usable DMA configuration, aborting\n");
2662 goto err_out_release_regions
;
2664 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
2666 dev_err(dev
, "Unable to obtain %u-bit DMA "
2667 "for consistent allocations, aborting\n", 32);
2668 goto err_out_release_regions
;
2671 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(40));
2673 dev_err(dev
, "Unable to obtain %u-bit DMA "
2674 "for consistent allocations, aborting\n", 40);
2675 goto err_out_release_regions
;
2680 /* Map vNIC resources from BAR0-5
2683 for (i
= 0; i
< ARRAY_SIZE(enic
->bar
); i
++) {
2684 if (!(pci_resource_flags(pdev
, i
) & IORESOURCE_MEM
))
2686 enic
->bar
[i
].len
= pci_resource_len(pdev
, i
);
2687 enic
->bar
[i
].vaddr
= pci_iomap(pdev
, i
, enic
->bar
[i
].len
);
2688 if (!enic
->bar
[i
].vaddr
) {
2689 dev_err(dev
, "Cannot memory-map BAR %d, aborting\n", i
);
2691 goto err_out_iounmap
;
2693 enic
->bar
[i
].bus_addr
= pci_resource_start(pdev
, i
);
2696 /* Register vNIC device
2699 enic
->vdev
= vnic_dev_register(NULL
, enic
, pdev
, enic
->bar
,
2700 ARRAY_SIZE(enic
->bar
));
2702 dev_err(dev
, "vNIC registration failed, aborting\n");
2704 goto err_out_iounmap
;
2707 /* Issue device open to get device in known state
2710 err
= enic_dev_open(enic
);
2712 dev_err(dev
, "vNIC dev open failed, aborting\n");
2713 goto err_out_vnic_unregister
;
2716 /* Issue device init to initialize the vnic-to-switch link.
2717 * We'll start with carrier off and wait for link UP
2718 * notification later to turn on carrier. We don't need
2719 * to wait here for the vnic-to-switch link initialization
2720 * to complete; link UP notification is the indication that
2721 * the process is complete.
2724 netif_carrier_off(netdev
);
2726 /* Do not call dev_init for a dynamic vnic.
2727 * For a dynamic vnic, init_prov_info will be
2728 * called later by an upper layer.
2731 if (!enic_is_dynamic(enic
)) {
2732 err
= vnic_dev_init(enic
->vdev
, 0);
2734 dev_err(dev
, "vNIC dev init failed, aborting\n");
2735 goto err_out_dev_close
;
2739 /* Setup devcmd lock
2742 spin_lock_init(&enic
->devcmd_lock
);
2744 err
= enic_dev_init(enic
);
2746 dev_err(dev
, "Device initialization failed, aborting\n");
2747 goto err_out_dev_close
;
2750 /* Setup notification timer, HW reset task, and wq locks
2753 init_timer(&enic
->notify_timer
);
2754 enic
->notify_timer
.function
= enic_notify_timer
;
2755 enic
->notify_timer
.data
= (unsigned long)enic
;
2757 INIT_WORK(&enic
->reset
, enic_reset
);
2759 for (i
= 0; i
< enic
->wq_count
; i
++)
2760 spin_lock_init(&enic
->wq_lock
[i
]);
2762 /* Register net device
2765 enic
->port_mtu
= enic
->config
.mtu
;
2766 (void)enic_change_mtu(netdev
, enic
->port_mtu
);
2768 err
= enic_set_mac_addr(netdev
, enic
->mac_addr
);
2770 dev_err(dev
, "Invalid MAC address, aborting\n");
2771 goto err_out_dev_deinit
;
2774 enic
->tx_coalesce_usecs
= enic
->config
.intr_timer_usec
;
2775 enic
->rx_coalesce_usecs
= enic
->tx_coalesce_usecs
;
2777 if (enic_is_dynamic(enic
))
2778 netdev
->netdev_ops
= &enic_netdev_dynamic_ops
;
2780 netdev
->netdev_ops
= &enic_netdev_ops
;
2782 netdev
->watchdog_timeo
= 2 * HZ
;
2783 netdev
->ethtool_ops
= &enic_ethtool_ops
;
2785 netdev
->features
|= NETIF_F_HW_VLAN_TX
| NETIF_F_HW_VLAN_RX
;
2786 if (ENIC_SETTING(enic
, LOOP
)) {
2787 netdev
->features
&= ~NETIF_F_HW_VLAN_TX
;
2788 enic
->loop_enable
= 1;
2789 enic
->loop_tag
= enic
->config
.loop_tag
;
2790 dev_info(dev
, "loopback tag=0x%04x\n", enic
->loop_tag
);
2792 if (ENIC_SETTING(enic
, TXCSUM
))
2793 netdev
->features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2794 if (ENIC_SETTING(enic
, TSO
))
2795 netdev
->features
|= NETIF_F_TSO
|
2796 NETIF_F_TSO6
| NETIF_F_TSO_ECN
;
2797 if (ENIC_SETTING(enic
, LRO
))
2798 netdev
->features
|= NETIF_F_GRO
;
2800 netdev
->features
|= NETIF_F_HIGHDMA
;
2802 enic
->csum_rx_enabled
= ENIC_SETTING(enic
, RXCSUM
);
2804 err
= register_netdev(netdev
);
2806 dev_err(dev
, "Cannot register net device, aborting\n");
2807 goto err_out_dev_deinit
;
2813 enic_dev_deinit(enic
);
2815 vnic_dev_close(enic
->vdev
);
2816 err_out_vnic_unregister
:
2817 vnic_dev_unregister(enic
->vdev
);
2820 err_out_release_regions
:
2821 pci_release_regions(pdev
);
2822 err_out_disable_device
:
2823 pci_disable_device(pdev
);
2824 err_out_free_netdev
:
2825 pci_set_drvdata(pdev
, NULL
);
2826 free_netdev(netdev
);
2831 static void __devexit
enic_remove(struct pci_dev
*pdev
)
2833 struct net_device
*netdev
= pci_get_drvdata(pdev
);
2836 struct enic
*enic
= netdev_priv(netdev
);
2838 cancel_work_sync(&enic
->reset
);
2839 unregister_netdev(netdev
);
2840 enic_dev_deinit(enic
);
2841 vnic_dev_close(enic
->vdev
);
2842 vnic_dev_unregister(enic
->vdev
);
2844 pci_release_regions(pdev
);
2845 pci_disable_device(pdev
);
2846 pci_set_drvdata(pdev
, NULL
);
2847 free_netdev(netdev
);
2851 static struct pci_driver enic_driver
= {
2853 .id_table
= enic_id_table
,
2854 .probe
= enic_probe
,
2855 .remove
= __devexit_p(enic_remove
),
2858 static int __init
enic_init_module(void)
2860 pr_info("%s, ver %s\n", DRV_DESCRIPTION
, DRV_VERSION
);
2862 return pci_register_driver(&enic_driver
);
2865 static void __exit
enic_cleanup_module(void)
2867 pci_unregister_driver(&enic_driver
);
2870 module_init(enic_init_module
);
2871 module_exit(enic_cleanup_module
);