2 * Copyright 2008-2014 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
5 * Copyright (c) 2014, Cisco Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
40 #include <rte_bus_pci.h>
41 #include <rte_ethdev.h>
42 #include <rte_ethdev_pci.h>
43 #include <rte_string_fns.h>
45 #include "vnic_intr.h"
49 #include "vnic_enet.h"
52 int enicpmd_logtype_init;
53 int enicpmd_logtype_flow;
55 #define PMD_INIT_LOG(level, fmt, args...) \
56 rte_log(RTE_LOG_ ## level, enicpmd_logtype_init, \
57 "%s" fmt "\n", __func__, ##args)
59 #define ENICPMD_FUNC_TRACE() PMD_INIT_LOG(DEBUG, " >>")
62 * The set of PCI devices this driver supports
64 #define CISCO_PCI_VENDOR_ID 0x1137
65 static const struct rte_pci_id pci_id_enic_map[] = {
66 { RTE_PCI_DEVICE(CISCO_PCI_VENDOR_ID, PCI_DEVICE_ID_CISCO_VIC_ENET) },
67 { RTE_PCI_DEVICE(CISCO_PCI_VENDOR_ID, PCI_DEVICE_ID_CISCO_VIC_ENET_VF) },
68 {.vendor_id = 0, /* sentinel */},
71 RTE_INIT(enicpmd_init_log);
73 enicpmd_init_log(void)
75 enicpmd_logtype_init = rte_log_register("pmd.enic.init");
76 if (enicpmd_logtype_init >= 0)
77 rte_log_set_level(enicpmd_logtype_init, RTE_LOG_NOTICE);
78 enicpmd_logtype_flow = rte_log_register("pmd.enic.flow");
79 if (enicpmd_logtype_flow >= 0)
80 rte_log_set_level(enicpmd_logtype_flow, RTE_LOG_NOTICE);
84 enicpmd_fdir_ctrl_func(struct rte_eth_dev *eth_dev,
85 enum rte_filter_op filter_op, void *arg)
87 struct enic *enic = pmd_priv(eth_dev);
91 if (filter_op == RTE_ETH_FILTER_NOP)
94 if (arg == NULL && filter_op != RTE_ETH_FILTER_FLUSH)
98 case RTE_ETH_FILTER_ADD:
99 case RTE_ETH_FILTER_UPDATE:
100 ret = enic_fdir_add_fltr(enic,
101 (struct rte_eth_fdir_filter *)arg);
104 case RTE_ETH_FILTER_DELETE:
105 ret = enic_fdir_del_fltr(enic,
106 (struct rte_eth_fdir_filter *)arg);
109 case RTE_ETH_FILTER_STATS:
110 enic_fdir_stats_get(enic, (struct rte_eth_fdir_stats *)arg);
113 case RTE_ETH_FILTER_FLUSH:
114 dev_warning(enic, "unsupported operation %u", filter_op);
117 case RTE_ETH_FILTER_INFO:
118 enic_fdir_info_get(enic, (struct rte_eth_fdir_info *)arg);
121 dev_err(enic, "unknown operation %u", filter_op);
129 enicpmd_dev_filter_ctrl(struct rte_eth_dev *dev,
130 enum rte_filter_type filter_type,
131 enum rte_filter_op filter_op,
136 ENICPMD_FUNC_TRACE();
138 switch (filter_type) {
139 case RTE_ETH_FILTER_GENERIC:
140 if (filter_op != RTE_ETH_FILTER_GET)
142 *(const void **)arg = &enic_flow_ops;
144 case RTE_ETH_FILTER_FDIR:
145 ret = enicpmd_fdir_ctrl_func(dev, filter_op, arg);
148 dev_warning(enic, "Filter type (%d) not supported",
157 static void enicpmd_dev_tx_queue_release(void *txq)
159 ENICPMD_FUNC_TRACE();
161 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
167 static int enicpmd_dev_setup_intr(struct enic *enic)
172 ENICPMD_FUNC_TRACE();
174 /* Are we done with the init of all the queues? */
175 for (index = 0; index < enic->cq_count; index++) {
176 if (!enic->cq[index].ctrl)
179 if (enic->cq_count != index)
181 for (index = 0; index < enic->wq_count; index++) {
182 if (!enic->wq[index].ctrl)
185 if (enic->wq_count != index)
187 /* check start of packet (SOP) RQs only in case scatter is disabled. */
188 for (index = 0; index < enic->rq_count; index++) {
189 if (!enic->rq[enic_rte_rq_idx_to_sop_idx(index)].ctrl)
192 if (enic->rq_count != index)
195 ret = enic_alloc_intr_resources(enic);
197 dev_err(enic, "alloc intr failed\n");
200 enic_init_vnic_resources(enic);
202 ret = enic_setup_finish(enic);
204 dev_err(enic, "setup could not be finished\n");
209 static int enicpmd_dev_tx_queue_setup(struct rte_eth_dev *eth_dev,
212 unsigned int socket_id,
213 __rte_unused const struct rte_eth_txconf *tx_conf)
216 struct enic *enic = pmd_priv(eth_dev);
218 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
219 return -E_RTE_SECONDARY;
221 ENICPMD_FUNC_TRACE();
222 if (queue_idx >= ENIC_WQ_MAX) {
224 "Max number of TX queues exceeded. Max is %d\n",
229 eth_dev->data->tx_queues[queue_idx] = (void *)&enic->wq[queue_idx];
231 ret = enic_alloc_wq(enic, queue_idx, socket_id, nb_desc);
233 dev_err(enic, "error in allocating wq\n");
237 return enicpmd_dev_setup_intr(enic);
240 static int enicpmd_dev_tx_queue_start(struct rte_eth_dev *eth_dev,
243 struct enic *enic = pmd_priv(eth_dev);
245 ENICPMD_FUNC_TRACE();
247 enic_start_wq(enic, queue_idx);
252 static int enicpmd_dev_tx_queue_stop(struct rte_eth_dev *eth_dev,
256 struct enic *enic = pmd_priv(eth_dev);
258 ENICPMD_FUNC_TRACE();
260 ret = enic_stop_wq(enic, queue_idx);
262 dev_err(enic, "error in stopping wq %d\n", queue_idx);
267 static int enicpmd_dev_rx_queue_start(struct rte_eth_dev *eth_dev,
270 struct enic *enic = pmd_priv(eth_dev);
272 ENICPMD_FUNC_TRACE();
274 enic_start_rq(enic, queue_idx);
279 static int enicpmd_dev_rx_queue_stop(struct rte_eth_dev *eth_dev,
283 struct enic *enic = pmd_priv(eth_dev);
285 ENICPMD_FUNC_TRACE();
287 ret = enic_stop_rq(enic, queue_idx);
289 dev_err(enic, "error in stopping rq %d\n", queue_idx);
294 static void enicpmd_dev_rx_queue_release(void *rxq)
296 ENICPMD_FUNC_TRACE();
298 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
304 static uint32_t enicpmd_dev_rx_queue_count(struct rte_eth_dev *dev,
305 uint16_t rx_queue_id)
307 struct enic *enic = pmd_priv(dev);
308 uint32_t queue_count = 0;
314 rq_num = enic_rte_rq_idx_to_sop_idx(rx_queue_id);
315 cq = &enic->cq[enic_cq_rq(enic, rq_num)];
316 cq_idx = cq->to_clean;
318 cq_tail = ioread32(&cq->ctrl->cq_tail);
320 if (cq_tail < cq_idx)
321 cq_tail += cq->ring.desc_count;
323 queue_count = cq_tail - cq_idx;
328 static int enicpmd_dev_rx_queue_setup(struct rte_eth_dev *eth_dev,
331 unsigned int socket_id,
332 const struct rte_eth_rxconf *rx_conf,
333 struct rte_mempool *mp)
336 struct enic *enic = pmd_priv(eth_dev);
338 ENICPMD_FUNC_TRACE();
340 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
341 return -E_RTE_SECONDARY;
343 /* With Rx scatter support, two RQs are now used on VIC per RQ used
344 * by the application.
346 if (queue_idx * 2 >= ENIC_RQ_MAX) {
348 "Max number of RX queues exceeded. Max is %d. This PMD uses 2 RQs on VIC per RQ used by DPDK.\n",
353 eth_dev->data->rx_queues[queue_idx] =
354 (void *)&enic->rq[enic_rte_rq_idx_to_sop_idx(queue_idx)];
356 ret = enic_alloc_rq(enic, queue_idx, socket_id, mp, nb_desc,
357 rx_conf->rx_free_thresh);
359 dev_err(enic, "error in allocating rq\n");
363 return enicpmd_dev_setup_intr(enic);
366 static int enicpmd_vlan_filter_set(struct rte_eth_dev *eth_dev,
367 uint16_t vlan_id, int on)
369 struct enic *enic = pmd_priv(eth_dev);
372 ENICPMD_FUNC_TRACE();
374 err = enic_add_vlan(enic, vlan_id);
376 err = enic_del_vlan(enic, vlan_id);
380 static int enicpmd_vlan_offload_set(struct rte_eth_dev *eth_dev, int mask)
382 struct enic *enic = pmd_priv(eth_dev);
384 ENICPMD_FUNC_TRACE();
386 if (mask & ETH_VLAN_STRIP_MASK) {
387 if (eth_dev->data->dev_conf.rxmode.offloads &
388 DEV_RX_OFFLOAD_VLAN_STRIP)
389 enic->ig_vlan_strip_en = 1;
391 enic->ig_vlan_strip_en = 0;
393 enic_set_rss_nic_cfg(enic);
396 if (mask & ETH_VLAN_FILTER_MASK) {
398 "Configuration of VLAN filter is not supported\n");
401 if (mask & ETH_VLAN_EXTEND_MASK) {
403 "Configuration of extended VLAN is not supported\n");
409 static int enicpmd_dev_configure(struct rte_eth_dev *eth_dev)
412 struct enic *enic = pmd_priv(eth_dev);
414 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
415 return -E_RTE_SECONDARY;
417 ENICPMD_FUNC_TRACE();
418 ret = enic_set_vnic_res(enic);
420 dev_err(enic, "Set vNIC resource num failed, aborting\n");
424 enic->hw_ip_checksum = !!(eth_dev->data->dev_conf.rxmode.offloads &
425 DEV_RX_OFFLOAD_CHECKSUM);
426 ret = enicpmd_vlan_offload_set(eth_dev, ETH_VLAN_STRIP_MASK);
432 * It returns 0 on success.
434 static int enicpmd_dev_start(struct rte_eth_dev *eth_dev)
436 struct enic *enic = pmd_priv(eth_dev);
438 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
439 return -E_RTE_SECONDARY;
441 ENICPMD_FUNC_TRACE();
442 return enic_enable(enic);
446 * Stop device: disable rx and tx functions to allow for reconfiguring.
448 static void enicpmd_dev_stop(struct rte_eth_dev *eth_dev)
450 struct rte_eth_link link;
451 struct enic *enic = pmd_priv(eth_dev);
453 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
456 ENICPMD_FUNC_TRACE();
458 memset(&link, 0, sizeof(link));
459 rte_atomic64_cmpset((uint64_t *)ð_dev->data->dev_link,
460 *(uint64_t *)ð_dev->data->dev_link,
467 static void enicpmd_dev_close(struct rte_eth_dev *eth_dev)
469 struct enic *enic = pmd_priv(eth_dev);
471 ENICPMD_FUNC_TRACE();
475 static int enicpmd_dev_link_update(struct rte_eth_dev *eth_dev,
476 __rte_unused int wait_to_complete)
478 struct enic *enic = pmd_priv(eth_dev);
480 ENICPMD_FUNC_TRACE();
481 return enic_link_update(enic);
484 static int enicpmd_dev_stats_get(struct rte_eth_dev *eth_dev,
485 struct rte_eth_stats *stats)
487 struct enic *enic = pmd_priv(eth_dev);
489 ENICPMD_FUNC_TRACE();
490 return enic_dev_stats_get(enic, stats);
493 static void enicpmd_dev_stats_reset(struct rte_eth_dev *eth_dev)
495 struct enic *enic = pmd_priv(eth_dev);
497 ENICPMD_FUNC_TRACE();
498 enic_dev_stats_clear(enic);
501 static void enicpmd_dev_info_get(struct rte_eth_dev *eth_dev,
502 struct rte_eth_dev_info *device_info)
504 struct enic *enic = pmd_priv(eth_dev);
506 ENICPMD_FUNC_TRACE();
507 device_info->pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
508 /* Scattered Rx uses two receive queues per rx queue exposed to dpdk */
509 device_info->max_rx_queues = enic->conf_rq_count / 2;
510 device_info->max_tx_queues = enic->conf_wq_count;
511 device_info->min_rx_bufsize = ENIC_MIN_MTU;
512 device_info->max_rx_pktlen = enic->max_mtu + ETHER_HDR_LEN + 4;
513 device_info->max_mac_addrs = ENIC_MAX_MAC_ADDR;
514 device_info->rx_offload_capa =
515 DEV_RX_OFFLOAD_VLAN_STRIP |
516 DEV_RX_OFFLOAD_IPV4_CKSUM |
517 DEV_RX_OFFLOAD_UDP_CKSUM |
518 DEV_RX_OFFLOAD_TCP_CKSUM;
519 device_info->tx_offload_capa =
520 DEV_TX_OFFLOAD_VLAN_INSERT |
521 DEV_TX_OFFLOAD_IPV4_CKSUM |
522 DEV_TX_OFFLOAD_UDP_CKSUM |
523 DEV_TX_OFFLOAD_TCP_CKSUM |
524 DEV_TX_OFFLOAD_TCP_TSO;
525 device_info->default_rxconf = (struct rte_eth_rxconf) {
526 .rx_free_thresh = ENIC_DEFAULT_RX_FREE_THRESH
530 static const uint32_t *enicpmd_dev_supported_ptypes_get(struct rte_eth_dev *dev)
532 static const uint32_t ptypes[] = {
534 RTE_PTYPE_L2_ETHER_VLAN,
535 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
536 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
540 RTE_PTYPE_L4_NONFRAG,
544 if (dev->rx_pkt_burst == enic_recv_pkts)
549 static void enicpmd_dev_promiscuous_enable(struct rte_eth_dev *eth_dev)
551 struct enic *enic = pmd_priv(eth_dev);
553 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
556 ENICPMD_FUNC_TRACE();
559 enic_add_packet_filter(enic);
562 static void enicpmd_dev_promiscuous_disable(struct rte_eth_dev *eth_dev)
564 struct enic *enic = pmd_priv(eth_dev);
566 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
569 ENICPMD_FUNC_TRACE();
571 enic_add_packet_filter(enic);
574 static void enicpmd_dev_allmulticast_enable(struct rte_eth_dev *eth_dev)
576 struct enic *enic = pmd_priv(eth_dev);
578 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
581 ENICPMD_FUNC_TRACE();
583 enic_add_packet_filter(enic);
586 static void enicpmd_dev_allmulticast_disable(struct rte_eth_dev *eth_dev)
588 struct enic *enic = pmd_priv(eth_dev);
590 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
593 ENICPMD_FUNC_TRACE();
595 enic_add_packet_filter(enic);
598 static int enicpmd_add_mac_addr(struct rte_eth_dev *eth_dev,
599 struct ether_addr *mac_addr,
600 __rte_unused uint32_t index, __rte_unused uint32_t pool)
602 struct enic *enic = pmd_priv(eth_dev);
604 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
605 return -E_RTE_SECONDARY;
607 ENICPMD_FUNC_TRACE();
608 return enic_set_mac_address(enic, mac_addr->addr_bytes);
611 static void enicpmd_remove_mac_addr(struct rte_eth_dev *eth_dev, uint32_t index)
613 struct enic *enic = pmd_priv(eth_dev);
615 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
618 ENICPMD_FUNC_TRACE();
619 enic_del_mac_address(enic, index);
622 static int enicpmd_mtu_set(struct rte_eth_dev *eth_dev, uint16_t mtu)
624 struct enic *enic = pmd_priv(eth_dev);
626 ENICPMD_FUNC_TRACE();
627 return enic_set_mtu(enic, mtu);
630 static const struct eth_dev_ops enicpmd_eth_dev_ops = {
631 .dev_configure = enicpmd_dev_configure,
632 .dev_start = enicpmd_dev_start,
633 .dev_stop = enicpmd_dev_stop,
634 .dev_set_link_up = NULL,
635 .dev_set_link_down = NULL,
636 .dev_close = enicpmd_dev_close,
637 .promiscuous_enable = enicpmd_dev_promiscuous_enable,
638 .promiscuous_disable = enicpmd_dev_promiscuous_disable,
639 .allmulticast_enable = enicpmd_dev_allmulticast_enable,
640 .allmulticast_disable = enicpmd_dev_allmulticast_disable,
641 .link_update = enicpmd_dev_link_update,
642 .stats_get = enicpmd_dev_stats_get,
643 .stats_reset = enicpmd_dev_stats_reset,
644 .queue_stats_mapping_set = NULL,
645 .dev_infos_get = enicpmd_dev_info_get,
646 .dev_supported_ptypes_get = enicpmd_dev_supported_ptypes_get,
647 .mtu_set = enicpmd_mtu_set,
648 .vlan_filter_set = enicpmd_vlan_filter_set,
649 .vlan_tpid_set = NULL,
650 .vlan_offload_set = enicpmd_vlan_offload_set,
651 .vlan_strip_queue_set = NULL,
652 .rx_queue_start = enicpmd_dev_rx_queue_start,
653 .rx_queue_stop = enicpmd_dev_rx_queue_stop,
654 .tx_queue_start = enicpmd_dev_tx_queue_start,
655 .tx_queue_stop = enicpmd_dev_tx_queue_stop,
656 .rx_queue_setup = enicpmd_dev_rx_queue_setup,
657 .rx_queue_release = enicpmd_dev_rx_queue_release,
658 .rx_queue_count = enicpmd_dev_rx_queue_count,
659 .rx_descriptor_done = NULL,
660 .tx_queue_setup = enicpmd_dev_tx_queue_setup,
661 .tx_queue_release = enicpmd_dev_tx_queue_release,
664 .flow_ctrl_get = NULL,
665 .flow_ctrl_set = NULL,
666 .priority_flow_ctrl_set = NULL,
667 .mac_addr_add = enicpmd_add_mac_addr,
668 .mac_addr_remove = enicpmd_remove_mac_addr,
669 .filter_ctrl = enicpmd_dev_filter_ctrl,
672 struct enic *enicpmd_list_head = NULL;
673 /* Initialize the driver
674 * It returns 0 on success.
676 static int eth_enicpmd_dev_init(struct rte_eth_dev *eth_dev)
678 struct rte_pci_device *pdev;
679 struct rte_pci_addr *addr;
680 struct enic *enic = pmd_priv(eth_dev);
682 ENICPMD_FUNC_TRACE();
684 enic->port_id = eth_dev->data->port_id;
685 enic->rte_dev = eth_dev;
686 eth_dev->dev_ops = &enicpmd_eth_dev_ops;
687 eth_dev->rx_pkt_burst = &enic_recv_pkts;
688 eth_dev->tx_pkt_burst = &enic_xmit_pkts;
690 pdev = RTE_ETH_DEV_TO_PCI(eth_dev);
691 rte_eth_copy_pci_info(eth_dev, pdev);
695 snprintf(enic->bdf_name, ENICPMD_BDF_LENGTH, "%04x:%02x:%02x.%x",
696 addr->domain, addr->bus, addr->devid, addr->function);
698 return enic_probe(enic);
701 static int eth_enic_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
702 struct rte_pci_device *pci_dev)
704 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct enic),
705 eth_enicpmd_dev_init);
708 static int eth_enic_pci_remove(struct rte_pci_device *pci_dev)
710 return rte_eth_dev_pci_generic_remove(pci_dev, NULL);
713 static struct rte_pci_driver rte_enic_pmd = {
714 .id_table = pci_id_enic_map,
715 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
716 .probe = eth_enic_pci_probe,
717 .remove = eth_enic_pci_remove,
720 RTE_PMD_REGISTER_PCI(net_enic, rte_enic_pmd);
721 RTE_PMD_REGISTER_PCI_TABLE(net_enic, pci_id_enic_map);
722 RTE_PMD_REGISTER_KMOD_DEP(net_enic, "* igb_uio | uio_pci_generic | vfio-pci");