4 * Copyright (C) Cavium networks Ltd. 2016.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Cavium networks nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
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 FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <netinet/in.h>
43 #include <sys/queue.h>
44 #include <sys/timerfd.h>
46 #include <rte_alarm.h>
47 #include <rte_atomic.h>
48 #include <rte_branch_prediction.h>
49 #include <rte_byteorder.h>
50 #include <rte_common.h>
51 #include <rte_cycles.h>
52 #include <rte_debug.h>
55 #include <rte_ether.h>
56 #include <rte_ethdev.h>
57 #include <rte_interrupts.h>
59 #include <rte_memory.h>
60 #include <rte_memzone.h>
61 #include <rte_malloc.h>
62 #include <rte_random.h>
64 #include <rte_tailq.h>
66 #include "base/nicvf_plat.h"
68 #include "nicvf_ethdev.h"
70 #include "nicvf_logs.h"
73 nicvf_interrupt(void *arg)
75 struct nicvf *nic = arg;
77 nicvf_reg_poll_interrupts(nic);
79 rte_eal_alarm_set(NICVF_INTR_POLL_INTERVAL_MS * 1000,
80 nicvf_interrupt, nic);
84 nicvf_periodic_alarm_start(struct nicvf *nic)
86 return rte_eal_alarm_set(NICVF_INTR_POLL_INTERVAL_MS * 1000,
87 nicvf_interrupt, nic);
91 nicvf_periodic_alarm_stop(struct nicvf *nic)
93 return rte_eal_alarm_cancel(nicvf_interrupt, nic);
96 /* Initialize and register driver with DPDK Application */
97 static const struct eth_dev_ops nicvf_eth_dev_ops = {
101 nicvf_eth_dev_init(struct rte_eth_dev *eth_dev)
104 struct rte_pci_device *pci_dev;
105 struct nicvf *nic = nicvf_pmd_priv(eth_dev);
107 PMD_INIT_FUNC_TRACE();
109 eth_dev->dev_ops = &nicvf_eth_dev_ops;
111 pci_dev = eth_dev->pci_dev;
112 rte_eth_copy_pci_info(eth_dev, pci_dev);
114 nic->device_id = pci_dev->id.device_id;
115 nic->vendor_id = pci_dev->id.vendor_id;
116 nic->subsystem_device_id = pci_dev->id.subsystem_device_id;
117 nic->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
118 nic->eth_dev = eth_dev;
120 PMD_INIT_LOG(DEBUG, "nicvf: device (%x:%x) %u:%u:%u:%u",
121 pci_dev->id.vendor_id, pci_dev->id.device_id,
122 pci_dev->addr.domain, pci_dev->addr.bus,
123 pci_dev->addr.devid, pci_dev->addr.function);
125 nic->reg_base = (uintptr_t)pci_dev->mem_resource[0].addr;
126 if (!nic->reg_base) {
127 PMD_INIT_LOG(ERR, "Failed to map BAR0");
132 nicvf_disable_all_interrupts(nic);
134 ret = nicvf_periodic_alarm_start(nic);
136 PMD_INIT_LOG(ERR, "Failed to start period alarm");
140 ret = nicvf_mbox_check_pf_ready(nic);
142 PMD_INIT_LOG(ERR, "Failed to get ready message from PF");
146 "node=%d vf=%d mode=%s sqs=%s loopback_supported=%s",
147 nic->node, nic->vf_id,
148 nic->tns_mode == NIC_TNS_MODE ? "tns" : "tns-bypass",
149 nic->sqs_mode ? "true" : "false",
150 nic->loopback_supported ? "true" : "false"
155 PMD_INIT_LOG(INFO, "Unsupported SQS VF detected, Detaching...");
156 /* Detach port by returning Positive error number */
161 eth_dev->data->mac_addrs = rte_zmalloc("mac_addr", ETHER_ADDR_LEN, 0);
162 if (eth_dev->data->mac_addrs == NULL) {
163 PMD_INIT_LOG(ERR, "Failed to allocate memory for mac addr");
167 if (is_zero_ether_addr((struct ether_addr *)nic->mac_addr))
168 eth_random_addr(&nic->mac_addr[0]);
170 ether_addr_copy((struct ether_addr *)nic->mac_addr,
171 ð_dev->data->mac_addrs[0]);
173 ret = nicvf_mbox_set_mac_addr(nic, nic->mac_addr);
175 PMD_INIT_LOG(ERR, "Failed to set mac addr");
179 ret = nicvf_base_init(nic);
181 PMD_INIT_LOG(ERR, "Failed to execute nicvf_base_init");
185 ret = nicvf_mbox_get_rss_size(nic);
187 PMD_INIT_LOG(ERR, "Failed to get rss table size");
191 PMD_INIT_LOG(INFO, "Port %d (%x:%x) mac=%02x:%02x:%02x:%02x:%02x:%02x",
192 eth_dev->data->port_id, nic->vendor_id, nic->device_id,
193 nic->mac_addr[0], nic->mac_addr[1], nic->mac_addr[2],
194 nic->mac_addr[3], nic->mac_addr[4], nic->mac_addr[5]);
199 rte_free(eth_dev->data->mac_addrs);
201 nicvf_periodic_alarm_stop(nic);
206 static const struct rte_pci_id pci_id_nicvf_map[] = {
208 .class_id = RTE_CLASS_ANY_ID,
209 .vendor_id = PCI_VENDOR_ID_CAVIUM,
210 .device_id = PCI_DEVICE_ID_THUNDERX_PASS1_NICVF,
211 .subsystem_vendor_id = PCI_VENDOR_ID_CAVIUM,
212 .subsystem_device_id = PCI_SUB_DEVICE_ID_THUNDERX_PASS1_NICVF,
215 .class_id = RTE_CLASS_ANY_ID,
216 .vendor_id = PCI_VENDOR_ID_CAVIUM,
217 .device_id = PCI_DEVICE_ID_THUNDERX_PASS2_NICVF,
218 .subsystem_vendor_id = PCI_VENDOR_ID_CAVIUM,
219 .subsystem_device_id = PCI_SUB_DEVICE_ID_THUNDERX_PASS2_NICVF,
226 static struct eth_driver rte_nicvf_pmd = {
228 .name = "rte_nicvf_pmd",
229 .id_table = pci_id_nicvf_map,
230 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
232 .eth_dev_init = nicvf_eth_dev_init,
233 .dev_private_size = sizeof(struct nicvf),
237 rte_nicvf_pmd_init(const char *name __rte_unused, const char *para __rte_unused)
239 PMD_INIT_FUNC_TRACE();
240 PMD_INIT_LOG(INFO, "librte_pmd_thunderx nicvf version %s",
241 THUNDERX_NICVF_PMD_VERSION);
243 rte_eth_driver_register(&rte_nicvf_pmd);
247 static struct rte_driver rte_nicvf_driver = {
248 .name = "nicvf_driver",
250 .init = rte_nicvf_pmd_init,
253 PMD_REGISTER_DRIVER(rte_nicvf_driver);