xref: /dpdk/drivers/net/thunderx/nicvf_ethdev.c (revision e438796617dcd64725fc789092172e40bc7be67e)
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium networks Ltd. 2016.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
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
15  *       distribution.
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.
19  *
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.
31  */
32 
33 #include <assert.h>
34 #include <stdio.h>
35 #include <stdbool.h>
36 #include <errno.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stdarg.h>
41 #include <inttypes.h>
42 #include <netinet/in.h>
43 #include <sys/queue.h>
44 #include <sys/timerfd.h>
45 
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>
53 #include <rte_dev.h>
54 #include <rte_eal.h>
55 #include <rte_ether.h>
56 #include <rte_ethdev.h>
57 #include <rte_interrupts.h>
58 #include <rte_log.h>
59 #include <rte_memory.h>
60 #include <rte_memzone.h>
61 #include <rte_malloc.h>
62 #include <rte_random.h>
63 #include <rte_pci.h>
64 #include <rte_tailq.h>
65 
66 #include "base/nicvf_plat.h"
67 
68 #include "nicvf_ethdev.h"
69 
70 #include "nicvf_logs.h"
71 
72 static void
73 nicvf_interrupt(void *arg)
74 {
75 	struct nicvf *nic = arg;
76 
77 	nicvf_reg_poll_interrupts(nic);
78 
79 	rte_eal_alarm_set(NICVF_INTR_POLL_INTERVAL_MS * 1000,
80 				nicvf_interrupt, nic);
81 }
82 
83 static int
84 nicvf_periodic_alarm_start(struct nicvf *nic)
85 {
86 	return rte_eal_alarm_set(NICVF_INTR_POLL_INTERVAL_MS * 1000,
87 					nicvf_interrupt, nic);
88 }
89 
90 static int
91 nicvf_periodic_alarm_stop(struct nicvf *nic)
92 {
93 	return rte_eal_alarm_cancel(nicvf_interrupt, nic);
94 }
95 
96 /* Initialize and register driver with DPDK Application */
97 static const struct eth_dev_ops nicvf_eth_dev_ops = {
98 };
99 
100 static int
101 nicvf_eth_dev_init(struct rte_eth_dev *eth_dev)
102 {
103 	int ret;
104 	struct rte_pci_device *pci_dev;
105 	struct nicvf *nic = nicvf_pmd_priv(eth_dev);
106 
107 	PMD_INIT_FUNC_TRACE();
108 
109 	eth_dev->dev_ops = &nicvf_eth_dev_ops;
110 
111 	pci_dev = eth_dev->pci_dev;
112 	rte_eth_copy_pci_info(eth_dev, pci_dev);
113 
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;
119 
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);
124 
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");
128 		ret = -ENODEV;
129 		goto fail;
130 	}
131 
132 	nicvf_disable_all_interrupts(nic);
133 
134 	ret = nicvf_periodic_alarm_start(nic);
135 	if (ret) {
136 		PMD_INIT_LOG(ERR, "Failed to start period alarm");
137 		goto fail;
138 	}
139 
140 	ret = nicvf_mbox_check_pf_ready(nic);
141 	if (ret) {
142 		PMD_INIT_LOG(ERR, "Failed to get ready message from PF");
143 		goto alarm_fail;
144 	} else {
145 		PMD_INIT_LOG(INFO,
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"
151 			);
152 	}
153 
154 	if (nic->sqs_mode) {
155 		PMD_INIT_LOG(INFO, "Unsupported SQS VF detected, Detaching...");
156 		/* Detach port by returning Positive error number */
157 		ret = ENOTSUP;
158 		goto alarm_fail;
159 	}
160 
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");
164 		ret = -ENOMEM;
165 		goto alarm_fail;
166 	}
167 	if (is_zero_ether_addr((struct ether_addr *)nic->mac_addr))
168 		eth_random_addr(&nic->mac_addr[0]);
169 
170 	ether_addr_copy((struct ether_addr *)nic->mac_addr,
171 			&eth_dev->data->mac_addrs[0]);
172 
173 	ret = nicvf_mbox_set_mac_addr(nic, nic->mac_addr);
174 	if (ret) {
175 		PMD_INIT_LOG(ERR, "Failed to set mac addr");
176 		goto malloc_fail;
177 	}
178 
179 	ret = nicvf_base_init(nic);
180 	if (ret) {
181 		PMD_INIT_LOG(ERR, "Failed to execute nicvf_base_init");
182 		goto malloc_fail;
183 	}
184 
185 	ret = nicvf_mbox_get_rss_size(nic);
186 	if (ret) {
187 		PMD_INIT_LOG(ERR, "Failed to get rss table size");
188 		goto malloc_fail;
189 	}
190 
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]);
195 
196 	return 0;
197 
198 malloc_fail:
199 	rte_free(eth_dev->data->mac_addrs);
200 alarm_fail:
201 	nicvf_periodic_alarm_stop(nic);
202 fail:
203 	return ret;
204 }
205 
206 static const struct rte_pci_id pci_id_nicvf_map[] = {
207 	{
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,
213 	},
214 	{
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,
220 	},
221 	{
222 		.vendor_id = 0,
223 	},
224 };
225 
226 static struct eth_driver rte_nicvf_pmd = {
227 	.pci_drv = {
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,
231 	},
232 	.eth_dev_init = nicvf_eth_dev_init,
233 	.dev_private_size = sizeof(struct nicvf),
234 };
235 
236 static int
237 rte_nicvf_pmd_init(const char *name __rte_unused, const char *para __rte_unused)
238 {
239 	PMD_INIT_FUNC_TRACE();
240 	PMD_INIT_LOG(INFO, "librte_pmd_thunderx nicvf version %s",
241 			THUNDERX_NICVF_PMD_VERSION);
242 
243 	rte_eth_driver_register(&rte_nicvf_pmd);
244 	return 0;
245 }
246 
247 static struct rte_driver rte_nicvf_driver = {
248 	.name = "nicvf_driver",
249 	.type = PMD_PDEV,
250 	.init = rte_nicvf_pmd_init,
251 };
252 
253 PMD_REGISTER_DRIVER(rte_nicvf_driver);
254