xref: /dpdk/drivers/net/nfp/nfp_ethdev.c (revision 4aa10e5dc1b0fd6cc5b1b18770ac603e2c33a66c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2014-2021 Netronome Systems, Inc.
3  * All rights reserved.
4  *
5  * Small portions derived from code Copyright(c) 2010-2015 Intel Corporation.
6  */
7 
8 /*
9  * vim:shiftwidth=8:noexpandtab
10  *
11  * @file dpdk/pmd/nfp_ethdev.c
12  *
13  * Netronome vNIC DPDK Poll-Mode Driver: Main entry point
14  */
15 
16 #include <rte_common.h>
17 #include <ethdev_driver.h>
18 #include <ethdev_pci.h>
19 #include <dev_driver.h>
20 #include <rte_ether.h>
21 #include <rte_malloc.h>
22 #include <rte_memzone.h>
23 #include <rte_mempool.h>
24 #include <rte_service_component.h>
25 #include <rte_alarm.h>
26 #include "eal_firmware.h"
27 
28 #include "nfpcore/nfp_cpp.h"
29 #include "nfpcore/nfp_nffw.h"
30 #include "nfpcore/nfp_hwinfo.h"
31 #include "nfpcore/nfp_mip.h"
32 #include "nfpcore/nfp_rtsym.h"
33 #include "nfpcore/nfp_nsp.h"
34 
35 #include "nfp_common.h"
36 #include "nfp_ctrl.h"
37 #include "nfp_rxtx.h"
38 #include "nfp_logs.h"
39 #include "nfp_cpp_bridge.h"
40 
41 #include "flower/nfp_flower.h"
42 
43 static int
44 nfp_net_pf_read_mac(struct nfp_app_fw_nic *app_fw_nic, int port)
45 {
46 	struct nfp_eth_table *nfp_eth_table;
47 	struct nfp_net_hw *hw = NULL;
48 
49 	/* Grab a pointer to the correct physical port */
50 	hw = app_fw_nic->ports[port];
51 
52 	nfp_eth_table = nfp_eth_read_ports(app_fw_nic->pf_dev->cpp);
53 
54 	nfp_eth_copy_mac((uint8_t *)&hw->mac_addr,
55 			 (uint8_t *)&nfp_eth_table->ports[port].mac_addr);
56 
57 	free(nfp_eth_table);
58 	return 0;
59 }
60 
61 static int
62 nfp_net_start(struct rte_eth_dev *dev)
63 {
64 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
65 	struct rte_intr_handle *intr_handle = pci_dev->intr_handle;
66 	uint32_t new_ctrl, update = 0;
67 	struct nfp_net_hw *hw;
68 	struct nfp_pf_dev *pf_dev;
69 	struct nfp_app_fw_nic *app_fw_nic;
70 	struct rte_eth_conf *dev_conf;
71 	struct rte_eth_rxmode *rxmode;
72 	uint32_t intr_vector;
73 	int ret;
74 
75 	hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
76 	pf_dev = NFP_NET_DEV_PRIVATE_TO_PF(dev->data->dev_private);
77 	app_fw_nic = NFP_PRIV_TO_APP_FW_NIC(pf_dev->app_fw_priv);
78 
79 	PMD_INIT_LOG(DEBUG, "Start");
80 
81 	/* Disabling queues just in case... */
82 	nfp_net_disable_queues(dev);
83 
84 	/* Enabling the required queues in the device */
85 	nfp_net_enable_queues(dev);
86 
87 	/* check and configure queue intr-vector mapping */
88 	if (dev->data->dev_conf.intr_conf.rxq != 0) {
89 		if (app_fw_nic->multiport) {
90 			PMD_INIT_LOG(ERR, "PMD rx interrupt is not supported "
91 					  "with NFP multiport PF");
92 				return -EINVAL;
93 		}
94 		if (rte_intr_type_get(intr_handle) ==
95 						RTE_INTR_HANDLE_UIO) {
96 			/*
97 			 * Better not to share LSC with RX interrupts.
98 			 * Unregistering LSC interrupt handler
99 			 */
100 			rte_intr_callback_unregister(pci_dev->intr_handle,
101 				nfp_net_dev_interrupt_handler, (void *)dev);
102 
103 			if (dev->data->nb_rx_queues > 1) {
104 				PMD_INIT_LOG(ERR, "PMD rx interrupt only "
105 					     "supports 1 queue with UIO");
106 				return -EIO;
107 			}
108 		}
109 		intr_vector = dev->data->nb_rx_queues;
110 		if (rte_intr_efd_enable(intr_handle, intr_vector))
111 			return -1;
112 
113 		nfp_configure_rx_interrupt(dev, intr_handle);
114 		update = NFP_NET_CFG_UPDATE_MSIX;
115 	}
116 
117 	/* Checking MTU set */
118 	if (dev->data->mtu > hw->flbufsz) {
119 		PMD_INIT_LOG(ERR, "MTU (%u) can't be larger than the current NFP_FRAME_SIZE (%u)",
120 				dev->data->mtu, hw->flbufsz);
121 		return -ERANGE;
122 	}
123 
124 	rte_intr_enable(intr_handle);
125 
126 	new_ctrl = nfp_check_offloads(dev);
127 
128 	/* Writing configuration parameters in the device */
129 	nfp_net_params_setup(hw);
130 
131 	dev_conf = &dev->data->dev_conf;
132 	rxmode = &dev_conf->rxmode;
133 
134 	if (rxmode->mq_mode & RTE_ETH_MQ_RX_RSS) {
135 		nfp_net_rss_config_default(dev);
136 		update |= NFP_NET_CFG_UPDATE_RSS;
137 		if (hw->cap & NFP_NET_CFG_CTRL_RSS2)
138 			new_ctrl |= NFP_NET_CFG_CTRL_RSS2;
139 		else
140 			new_ctrl |= NFP_NET_CFG_CTRL_RSS;
141 	}
142 
143 	/* Enable device */
144 	new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
145 
146 	update |= NFP_NET_CFG_UPDATE_GEN | NFP_NET_CFG_UPDATE_RING;
147 
148 	/* Enable vxlan */
149 	new_ctrl |= NFP_NET_CFG_CTRL_VXLAN;
150 	update |= NFP_NET_CFG_UPDATE_VXLAN;
151 
152 	if (hw->cap & NFP_NET_CFG_CTRL_RINGCFG)
153 		new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
154 
155 	nn_cfg_writel(hw, NFP_NET_CFG_CTRL, new_ctrl);
156 	if (nfp_net_reconfig(hw, new_ctrl, update) < 0)
157 		return -EIO;
158 
159 	/*
160 	 * Allocating rte mbufs for configured rx queues.
161 	 * This requires queues being enabled before
162 	 */
163 	if (nfp_net_rx_freelist_setup(dev) < 0) {
164 		ret = -ENOMEM;
165 		goto error;
166 	}
167 
168 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
169 		/* Configure the physical port up */
170 		nfp_eth_set_configured(hw->cpp, hw->nfp_idx, 1);
171 	else
172 		nfp_eth_set_configured(dev->process_private,
173 				       hw->nfp_idx, 1);
174 
175 	hw->ctrl = new_ctrl;
176 
177 	return 0;
178 
179 error:
180 	/*
181 	 * An error returned by this function should mean the app
182 	 * exiting and then the system releasing all the memory
183 	 * allocated even memory coming from hugepages.
184 	 *
185 	 * The device could be enabled at this point with some queues
186 	 * ready for getting packets. This is true if the call to
187 	 * nfp_net_rx_freelist_setup() succeeds for some queues but
188 	 * fails for subsequent queues.
189 	 *
190 	 * This should make the app exiting but better if we tell the
191 	 * device first.
192 	 */
193 	nfp_net_disable_queues(dev);
194 
195 	return ret;
196 }
197 
198 /* Stop device: disable rx and tx functions to allow for reconfiguring. */
199 static int
200 nfp_net_stop(struct rte_eth_dev *dev)
201 {
202 	struct nfp_net_hw *hw;
203 
204 	PMD_INIT_LOG(DEBUG, "Stop");
205 
206 	hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
207 
208 	nfp_net_disable_queues(dev);
209 
210 	/* Clear queues */
211 	nfp_net_stop_tx_queue(dev);
212 
213 	nfp_net_stop_rx_queue(dev);
214 
215 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
216 		/* Configure the physical port down */
217 		nfp_eth_set_configured(hw->cpp, hw->nfp_idx, 0);
218 	else
219 		nfp_eth_set_configured(dev->process_private,
220 				       hw->nfp_idx, 0);
221 
222 	return 0;
223 }
224 
225 /* Set the link up. */
226 static int
227 nfp_net_set_link_up(struct rte_eth_dev *dev)
228 {
229 	struct nfp_net_hw *hw;
230 
231 	PMD_DRV_LOG(DEBUG, "Set link up");
232 
233 	hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
234 
235 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
236 		/* Configure the physical port down */
237 		return nfp_eth_set_configured(hw->cpp, hw->nfp_idx, 1);
238 	else
239 		return nfp_eth_set_configured(dev->process_private,
240 					      hw->nfp_idx, 1);
241 }
242 
243 /* Set the link down. */
244 static int
245 nfp_net_set_link_down(struct rte_eth_dev *dev)
246 {
247 	struct nfp_net_hw *hw;
248 
249 	PMD_DRV_LOG(DEBUG, "Set link down");
250 
251 	hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
252 
253 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
254 		/* Configure the physical port down */
255 		return nfp_eth_set_configured(hw->cpp, hw->nfp_idx, 0);
256 	else
257 		return nfp_eth_set_configured(dev->process_private,
258 					      hw->nfp_idx, 0);
259 }
260 
261 /* Reset and stop device. The device can not be restarted. */
262 static int
263 nfp_net_close(struct rte_eth_dev *dev)
264 {
265 	struct nfp_net_hw *hw;
266 	struct rte_pci_device *pci_dev;
267 	struct nfp_pf_dev *pf_dev;
268 	struct nfp_app_fw_nic *app_fw_nic;
269 	int i;
270 
271 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
272 		return 0;
273 
274 	PMD_INIT_LOG(DEBUG, "Close");
275 
276 	pf_dev = NFP_NET_DEV_PRIVATE_TO_PF(dev->data->dev_private);
277 	hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
278 	pci_dev = RTE_ETH_DEV_TO_PCI(dev);
279 	app_fw_nic = NFP_PRIV_TO_APP_FW_NIC(pf_dev->app_fw_priv);
280 
281 	/*
282 	 * We assume that the DPDK application is stopping all the
283 	 * threads/queues before calling the device close function.
284 	 */
285 
286 	nfp_net_disable_queues(dev);
287 
288 	/* Clear queues */
289 	nfp_net_close_tx_queue(dev);
290 
291 	nfp_net_close_rx_queue(dev);
292 
293 	/* Cancel possible impending LSC work here before releasing the port*/
294 	rte_eal_alarm_cancel(nfp_net_dev_interrupt_delayed_handler,
295 			     (void *)dev);
296 
297 	/* Only free PF resources after all physical ports have been closed */
298 	/* Mark this port as unused and free device priv resources*/
299 	nn_cfg_writeb(hw, NFP_NET_CFG_LSC, 0xff);
300 	app_fw_nic->ports[hw->idx] = NULL;
301 	rte_eth_dev_release_port(dev);
302 
303 	for (i = 0; i < app_fw_nic->total_phyports; i++) {
304 		/* Check to see if ports are still in use */
305 		if (app_fw_nic->ports[i])
306 			return 0;
307 	}
308 
309 	/* Now it is safe to free all PF resources */
310 	PMD_INIT_LOG(INFO, "Freeing PF resources");
311 	nfp_cpp_area_free(pf_dev->ctrl_area);
312 	nfp_cpp_area_free(pf_dev->hwqueues_area);
313 	free(pf_dev->hwinfo);
314 	free(pf_dev->sym_tbl);
315 	nfp_cpp_free(pf_dev->cpp);
316 	rte_free(app_fw_nic);
317 	rte_free(pf_dev);
318 
319 	rte_intr_disable(pci_dev->intr_handle);
320 
321 	/* unregister callback func from eal lib */
322 	rte_intr_callback_unregister(pci_dev->intr_handle,
323 			nfp_net_dev_interrupt_handler, (void *)dev);
324 
325 	/*
326 	 * The ixgbe PMD disables the pcie master on the
327 	 * device. The i40e does not...
328 	 */
329 
330 	return 0;
331 }
332 
333 static int
334 nfp_net_find_vxlan_idx(struct nfp_net_hw *hw,
335 		uint16_t port,
336 		uint32_t *idx)
337 {
338 	uint32_t i;
339 	int free_idx = -1;
340 
341 	for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i++) {
342 		if (hw->vxlan_ports[i] == port) {
343 			free_idx = i;
344 			break;
345 		}
346 
347 		if (hw->vxlan_usecnt[i] == 0) {
348 			free_idx = i;
349 			break;
350 		}
351 	}
352 
353 	if (free_idx == -1)
354 		return -EINVAL;
355 
356 	*idx = free_idx;
357 
358 	return 0;
359 }
360 
361 static int
362 nfp_udp_tunnel_port_add(struct rte_eth_dev *dev,
363 		struct rte_eth_udp_tunnel *tunnel_udp)
364 {
365 	int ret;
366 	uint32_t idx;
367 	uint16_t vxlan_port;
368 	struct nfp_net_hw *hw;
369 	enum rte_eth_tunnel_type tnl_type;
370 
371 	hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
372 	vxlan_port = tunnel_udp->udp_port;
373 	tnl_type   = tunnel_udp->prot_type;
374 
375 	if (tnl_type != RTE_ETH_TUNNEL_TYPE_VXLAN) {
376 		PMD_DRV_LOG(ERR, "Not VXLAN tunnel");
377 		return -ENOTSUP;
378 	}
379 
380 	ret = nfp_net_find_vxlan_idx(hw, vxlan_port, &idx);
381 	if (ret != 0) {
382 		PMD_DRV_LOG(ERR, "Failed find valid vxlan idx");
383 		return -EINVAL;
384 	}
385 
386 	if (hw->vxlan_usecnt[idx] == 0) {
387 		ret = nfp_net_set_vxlan_port(hw, idx, vxlan_port);
388 		if (ret != 0) {
389 			PMD_DRV_LOG(ERR, "Failed set vxlan port");
390 			return -EINVAL;
391 		}
392 	}
393 
394 	hw->vxlan_usecnt[idx]++;
395 
396 	return 0;
397 }
398 
399 static int
400 nfp_udp_tunnel_port_del(struct rte_eth_dev *dev,
401 		struct rte_eth_udp_tunnel *tunnel_udp)
402 {
403 	int ret;
404 	uint32_t idx;
405 	uint16_t vxlan_port;
406 	struct nfp_net_hw *hw;
407 	enum rte_eth_tunnel_type tnl_type;
408 
409 	hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
410 	vxlan_port = tunnel_udp->udp_port;
411 	tnl_type   = tunnel_udp->prot_type;
412 
413 	if (tnl_type != RTE_ETH_TUNNEL_TYPE_VXLAN) {
414 		PMD_DRV_LOG(ERR, "Not VXLAN tunnel");
415 		return -ENOTSUP;
416 	}
417 
418 	ret = nfp_net_find_vxlan_idx(hw, vxlan_port, &idx);
419 	if (ret != 0 || hw->vxlan_usecnt[idx] == 0) {
420 		PMD_DRV_LOG(ERR, "Failed find valid vxlan idx");
421 		return -EINVAL;
422 	}
423 
424 	hw->vxlan_usecnt[idx]--;
425 
426 	if (hw->vxlan_usecnt[idx] == 0) {
427 		ret = nfp_net_set_vxlan_port(hw, idx, 0);
428 		if (ret != 0) {
429 			PMD_DRV_LOG(ERR, "Failed set vxlan port");
430 			return -EINVAL;
431 		}
432 	}
433 
434 	return 0;
435 }
436 
437 /* Initialise and register driver with DPDK Application */
438 static const struct eth_dev_ops nfp_net_eth_dev_ops = {
439 	.dev_configure		= nfp_net_configure,
440 	.dev_start		= nfp_net_start,
441 	.dev_stop		= nfp_net_stop,
442 	.dev_set_link_up	= nfp_net_set_link_up,
443 	.dev_set_link_down	= nfp_net_set_link_down,
444 	.dev_close		= nfp_net_close,
445 	.promiscuous_enable	= nfp_net_promisc_enable,
446 	.promiscuous_disable	= nfp_net_promisc_disable,
447 	.link_update		= nfp_net_link_update,
448 	.stats_get		= nfp_net_stats_get,
449 	.stats_reset		= nfp_net_stats_reset,
450 	.dev_infos_get		= nfp_net_infos_get,
451 	.dev_supported_ptypes_get = nfp_net_supported_ptypes_get,
452 	.mtu_set		= nfp_net_dev_mtu_set,
453 	.mac_addr_set		= nfp_net_set_mac_addr,
454 	.vlan_offload_set	= nfp_net_vlan_offload_set,
455 	.reta_update		= nfp_net_reta_update,
456 	.reta_query		= nfp_net_reta_query,
457 	.rss_hash_update	= nfp_net_rss_hash_update,
458 	.rss_hash_conf_get	= nfp_net_rss_hash_conf_get,
459 	.rx_queue_setup		= nfp_net_rx_queue_setup,
460 	.rx_queue_release	= nfp_net_rx_queue_release,
461 	.tx_queue_setup		= nfp_net_tx_queue_setup,
462 	.tx_queue_release	= nfp_net_tx_queue_release,
463 	.rx_queue_intr_enable   = nfp_rx_queue_intr_enable,
464 	.rx_queue_intr_disable  = nfp_rx_queue_intr_disable,
465 	.udp_tunnel_port_add    = nfp_udp_tunnel_port_add,
466 	.udp_tunnel_port_del    = nfp_udp_tunnel_port_del,
467 };
468 
469 static inline int
470 nfp_net_ethdev_ops_mount(struct nfp_net_hw *hw, struct rte_eth_dev *eth_dev)
471 {
472 	switch (NFD_CFG_CLASS_VER_of(hw->ver)) {
473 	case NFP_NET_CFG_VERSION_DP_NFD3:
474 		eth_dev->tx_pkt_burst = &nfp_net_nfd3_xmit_pkts;
475 		break;
476 	case NFP_NET_CFG_VERSION_DP_NFDK:
477 		if (NFD_CFG_MAJOR_VERSION_of(hw->ver) < 5) {
478 			PMD_DRV_LOG(ERR, "NFDK must use ABI 5 or newer, found: %d",
479 				NFD_CFG_MAJOR_VERSION_of(hw->ver));
480 			return -EINVAL;
481 		}
482 		eth_dev->tx_pkt_burst = &nfp_net_nfdk_xmit_pkts;
483 		break;
484 	default:
485 		PMD_DRV_LOG(ERR, "The version of firmware is not correct.");
486 		return -EINVAL;
487 	}
488 
489 	eth_dev->dev_ops = &nfp_net_eth_dev_ops;
490 	eth_dev->rx_queue_count = nfp_net_rx_queue_count;
491 	eth_dev->rx_pkt_burst = &nfp_net_recv_pkts;
492 
493 	return 0;
494 }
495 
496 static int
497 nfp_net_init(struct rte_eth_dev *eth_dev)
498 {
499 	struct rte_pci_device *pci_dev;
500 	struct nfp_pf_dev *pf_dev;
501 	struct nfp_app_fw_nic *app_fw_nic;
502 	struct nfp_net_hw *hw;
503 	struct rte_ether_addr *tmp_ether_addr;
504 	uint64_t rx_bar_off = 0;
505 	uint64_t tx_bar_off = 0;
506 	uint32_t start_q;
507 	int stride = 4;
508 	int port = 0;
509 
510 	PMD_INIT_FUNC_TRACE();
511 
512 	pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
513 
514 	/* Use backpointer here to the PF of this eth_dev */
515 	pf_dev = NFP_NET_DEV_PRIVATE_TO_PF(eth_dev->data->dev_private);
516 
517 	/* Use backpointer to the CoreNIC app struct */
518 	app_fw_nic = NFP_PRIV_TO_APP_FW_NIC(pf_dev->app_fw_priv);
519 
520 	/* NFP can not handle DMA addresses requiring more than 40 bits */
521 	if (rte_mem_check_dma_mask(40)) {
522 		RTE_LOG(ERR, PMD,
523 			"device %s can not be used: restricted dma mask to 40 bits!\n",
524 			pci_dev->device.name);
525 		return -ENODEV;
526 	}
527 
528 	port = ((struct nfp_net_hw *)eth_dev->data->dev_private)->idx;
529 	if (port < 0 || port > 7) {
530 		PMD_DRV_LOG(ERR, "Port value is wrong");
531 		return -ENODEV;
532 	}
533 
534 	/*
535 	 * Use PF array of physical ports to get pointer to
536 	 * this specific port
537 	 */
538 	hw = app_fw_nic->ports[port];
539 
540 	PMD_INIT_LOG(DEBUG, "Working with physical port number: %d, "
541 			"NFP internal port number: %d", port, hw->nfp_idx);
542 
543 	rte_eth_copy_pci_info(eth_dev, pci_dev);
544 
545 	hw->device_id = pci_dev->id.device_id;
546 	hw->vendor_id = pci_dev->id.vendor_id;
547 	hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
548 	hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
549 
550 	PMD_INIT_LOG(DEBUG, "nfp_net: device (%u:%u) %u:%u:%u:%u",
551 		     pci_dev->id.vendor_id, pci_dev->id.device_id,
552 		     pci_dev->addr.domain, pci_dev->addr.bus,
553 		     pci_dev->addr.devid, pci_dev->addr.function);
554 
555 	hw->ctrl_bar = (uint8_t *)pci_dev->mem_resource[0].addr;
556 	if (hw->ctrl_bar == NULL) {
557 		PMD_DRV_LOG(ERR,
558 			"hw->ctrl_bar is NULL. BAR0 not configured");
559 		return -ENODEV;
560 	}
561 
562 	if (port == 0) {
563 		hw->ctrl_bar = pf_dev->ctrl_bar;
564 	} else {
565 		if (pf_dev->ctrl_bar == NULL)
566 			return -ENODEV;
567 		/* Use port offset in pf ctrl_bar for this ports control bar */
568 		hw->ctrl_bar = pf_dev->ctrl_bar + (port * NFP_PF_CSR_SLICE_SIZE);
569 	}
570 
571 	PMD_INIT_LOG(DEBUG, "ctrl bar: %p", hw->ctrl_bar);
572 
573 	hw->ver = nn_cfg_readl(hw, NFP_NET_CFG_VERSION);
574 
575 	if (nfp_net_ethdev_ops_mount(hw, eth_dev))
576 		return -EINVAL;
577 
578 	hw->max_rx_queues = nn_cfg_readl(hw, NFP_NET_CFG_MAX_RXRINGS);
579 	hw->max_tx_queues = nn_cfg_readl(hw, NFP_NET_CFG_MAX_TXRINGS);
580 
581 	/* Work out where in the BAR the queues start. */
582 	switch (pci_dev->id.device_id) {
583 	case PCI_DEVICE_ID_NFP3800_PF_NIC:
584 	case PCI_DEVICE_ID_NFP4000_PF_NIC:
585 	case PCI_DEVICE_ID_NFP6000_PF_NIC:
586 		start_q = nn_cfg_readl(hw, NFP_NET_CFG_START_TXQ);
587 		tx_bar_off = nfp_pci_queue(pci_dev, start_q);
588 		start_q = nn_cfg_readl(hw, NFP_NET_CFG_START_RXQ);
589 		rx_bar_off = nfp_pci_queue(pci_dev, start_q);
590 		break;
591 	default:
592 		PMD_DRV_LOG(ERR, "nfp_net: no device ID matching");
593 		return -ENODEV;
594 	}
595 
596 	PMD_INIT_LOG(DEBUG, "tx_bar_off: 0x%" PRIx64 "", tx_bar_off);
597 	PMD_INIT_LOG(DEBUG, "rx_bar_off: 0x%" PRIx64 "", rx_bar_off);
598 
599 	hw->tx_bar = pf_dev->hw_queues + tx_bar_off;
600 	hw->rx_bar = pf_dev->hw_queues + rx_bar_off;
601 	eth_dev->data->dev_private = hw;
602 
603 	PMD_INIT_LOG(DEBUG, "ctrl_bar: %p, tx_bar: %p, rx_bar: %p",
604 		     hw->ctrl_bar, hw->tx_bar, hw->rx_bar);
605 
606 	nfp_net_cfg_queue_setup(hw);
607 
608 	/* Get some of the read-only fields from the config BAR */
609 	hw->cap = nn_cfg_readl(hw, NFP_NET_CFG_CAP);
610 	hw->max_mtu = nn_cfg_readl(hw, NFP_NET_CFG_MAX_MTU);
611 	hw->mtu = RTE_ETHER_MTU;
612 
613 	/* VLAN insertion is incompatible with LSOv2 */
614 	if (hw->cap & NFP_NET_CFG_CTRL_LSO2)
615 		hw->cap &= ~NFP_NET_CFG_CTRL_TXVLAN;
616 
617 	if (NFD_CFG_MAJOR_VERSION_of(hw->ver) < 2)
618 		hw->rx_offset = NFP_NET_RX_OFFSET;
619 	else
620 		hw->rx_offset = nn_cfg_readl(hw, NFP_NET_CFG_RX_OFFSET_ADDR);
621 
622 	hw->ctrl = 0;
623 
624 	hw->stride_rx = stride;
625 	hw->stride_tx = stride;
626 
627 	nfp_net_log_device_information(hw);
628 
629 	/* Initializing spinlock for reconfigs */
630 	rte_spinlock_init(&hw->reconfig_lock);
631 
632 	/* Allocating memory for mac addr */
633 	eth_dev->data->mac_addrs = rte_zmalloc("mac_addr",
634 					       RTE_ETHER_ADDR_LEN, 0);
635 	if (eth_dev->data->mac_addrs == NULL) {
636 		PMD_INIT_LOG(ERR, "Failed to space for MAC address");
637 		return -ENOMEM;
638 	}
639 
640 	nfp_net_pf_read_mac(app_fw_nic, port);
641 	nfp_net_write_mac(hw, (uint8_t *)&hw->mac_addr);
642 
643 	tmp_ether_addr = (struct rte_ether_addr *)&hw->mac_addr;
644 	if (!rte_is_valid_assigned_ether_addr(tmp_ether_addr)) {
645 		PMD_INIT_LOG(INFO, "Using random mac address for port %d", port);
646 		/* Using random mac addresses for VFs */
647 		rte_eth_random_addr(&hw->mac_addr[0]);
648 		nfp_net_write_mac(hw, (uint8_t *)&hw->mac_addr);
649 	}
650 
651 	/* Copying mac address to DPDK eth_dev struct */
652 	rte_ether_addr_copy((struct rte_ether_addr *)hw->mac_addr,
653 			&eth_dev->data->mac_addrs[0]);
654 
655 	if ((hw->cap & NFP_NET_CFG_CTRL_LIVE_ADDR) == 0)
656 		eth_dev->data->dev_flags |= RTE_ETH_DEV_NOLIVE_MAC_ADDR;
657 
658 	eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
659 
660 	PMD_INIT_LOG(INFO, "port %d VendorID=0x%x DeviceID=0x%x "
661 		     "mac=" RTE_ETHER_ADDR_PRT_FMT,
662 		     eth_dev->data->port_id, pci_dev->id.vendor_id,
663 		     pci_dev->id.device_id,
664 		     hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
665 		     hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
666 
667 	/* Registering LSC interrupt handler */
668 	rte_intr_callback_register(pci_dev->intr_handle,
669 			nfp_net_dev_interrupt_handler, (void *)eth_dev);
670 	/* Telling the firmware about the LSC interrupt entry */
671 	nn_cfg_writeb(hw, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
672 	/* Recording current stats counters values */
673 	nfp_net_stats_reset(eth_dev);
674 
675 	return 0;
676 }
677 
678 #define DEFAULT_FW_PATH       "/lib/firmware/netronome"
679 
680 static int
681 nfp_fw_upload(struct rte_pci_device *dev, struct nfp_nsp *nsp, char *card)
682 {
683 	struct nfp_cpp *cpp = nsp->cpp;
684 	void *fw_buf;
685 	char fw_name[125];
686 	char serial[40];
687 	size_t fsize;
688 
689 	/* Looking for firmware file in order of priority */
690 
691 	/* First try to find a firmware image specific for this device */
692 	snprintf(serial, sizeof(serial),
693 			"serial-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x",
694 		cpp->serial[0], cpp->serial[1], cpp->serial[2], cpp->serial[3],
695 		cpp->serial[4], cpp->serial[5], cpp->interface >> 8,
696 		cpp->interface & 0xff);
697 
698 	snprintf(fw_name, sizeof(fw_name), "%s/%s.nffw", DEFAULT_FW_PATH,
699 			serial);
700 
701 	PMD_DRV_LOG(DEBUG, "Trying with fw file: %s", fw_name);
702 	if (rte_firmware_read(fw_name, &fw_buf, &fsize) == 0)
703 		goto load_fw;
704 	/* Then try the PCI name */
705 	snprintf(fw_name, sizeof(fw_name), "%s/pci-%s.nffw", DEFAULT_FW_PATH,
706 			dev->name);
707 
708 	PMD_DRV_LOG(DEBUG, "Trying with fw file: %s", fw_name);
709 	if (rte_firmware_read(fw_name, &fw_buf, &fsize) == 0)
710 		goto load_fw;
711 
712 	/* Finally try the card type and media */
713 	snprintf(fw_name, sizeof(fw_name), "%s/%s", DEFAULT_FW_PATH, card);
714 	PMD_DRV_LOG(DEBUG, "Trying with fw file: %s", fw_name);
715 	if (rte_firmware_read(fw_name, &fw_buf, &fsize) < 0) {
716 		PMD_DRV_LOG(INFO, "Firmware file %s not found.", fw_name);
717 		return -ENOENT;
718 	}
719 
720 load_fw:
721 	PMD_DRV_LOG(INFO, "Firmware file found at %s with size: %zu",
722 		fw_name, fsize);
723 	PMD_DRV_LOG(INFO, "Uploading the firmware ...");
724 	nfp_nsp_load_fw(nsp, fw_buf, fsize);
725 	PMD_DRV_LOG(INFO, "Done");
726 
727 	free(fw_buf);
728 
729 	return 0;
730 }
731 
732 static int
733 nfp_fw_setup(struct rte_pci_device *dev,
734 		struct nfp_cpp *cpp,
735 		struct nfp_eth_table *nfp_eth_table,
736 		struct nfp_hwinfo *hwinfo)
737 {
738 	struct nfp_nsp *nsp;
739 	const char *nfp_fw_model;
740 	char card_desc[100];
741 	int err = 0;
742 
743 	nfp_fw_model = nfp_hwinfo_lookup(hwinfo, "nffw.partno");
744 	if (nfp_fw_model == NULL)
745 		nfp_fw_model = nfp_hwinfo_lookup(hwinfo, "assembly.partno");
746 
747 	if (nfp_fw_model) {
748 		PMD_DRV_LOG(INFO, "firmware model found: %s", nfp_fw_model);
749 	} else {
750 		PMD_DRV_LOG(ERR, "firmware model NOT found");
751 		return -EIO;
752 	}
753 
754 	if (nfp_eth_table->count == 0 || nfp_eth_table->count > 8) {
755 		PMD_DRV_LOG(ERR, "NFP ethernet table reports wrong ports: %u",
756 			nfp_eth_table->count);
757 		return -EIO;
758 	}
759 
760 	PMD_DRV_LOG(INFO, "NFP ethernet port table reports %u ports",
761 			nfp_eth_table->count);
762 
763 	PMD_DRV_LOG(INFO, "Port speed: %u", nfp_eth_table->ports[0].speed);
764 
765 	snprintf(card_desc, sizeof(card_desc), "nic_%s_%dx%d.nffw",
766 			nfp_fw_model, nfp_eth_table->count,
767 			nfp_eth_table->ports[0].speed / 1000);
768 
769 	nsp = nfp_nsp_open(cpp);
770 	if (nsp == NULL) {
771 		PMD_DRV_LOG(ERR, "NFP error when obtaining NSP handle");
772 		return -EIO;
773 	}
774 
775 	nfp_nsp_device_soft_reset(nsp);
776 	err = nfp_fw_upload(dev, nsp, card_desc);
777 
778 	nfp_nsp_close(nsp);
779 	return err;
780 }
781 
782 static int
783 nfp_init_app_fw_nic(struct nfp_pf_dev *pf_dev)
784 {
785 	int i;
786 	int ret;
787 	int err = 0;
788 	int total_vnics;
789 	struct nfp_net_hw *hw;
790 	unsigned int numa_node;
791 	struct rte_eth_dev *eth_dev;
792 	struct nfp_app_fw_nic *app_fw_nic;
793 	struct nfp_eth_table *nfp_eth_table;
794 	char port_name[RTE_ETH_NAME_MAX_LEN];
795 
796 	nfp_eth_table = pf_dev->nfp_eth_table;
797 	PMD_INIT_LOG(INFO, "Total physical ports: %d", nfp_eth_table->count);
798 
799 	/* Allocate memory for the CoreNIC app */
800 	app_fw_nic = rte_zmalloc("nfp_app_fw_nic", sizeof(*app_fw_nic), 0);
801 	if (app_fw_nic == NULL)
802 		return -ENOMEM;
803 
804 	/* Point the app_fw_priv pointer in the PF to the coreNIC app */
805 	pf_dev->app_fw_priv = app_fw_nic;
806 
807 	/* Read the number of vNIC's created for the PF */
808 	total_vnics = nfp_rtsym_read_le(pf_dev->sym_tbl, "nfd_cfg_pf0_num_ports", &err);
809 	if (err != 0 || total_vnics <= 0 || total_vnics > 8) {
810 		PMD_INIT_LOG(ERR, "nfd_cfg_pf0_num_ports symbol with wrong value");
811 		ret = -ENODEV;
812 		goto app_cleanup;
813 	}
814 
815 	/*
816 	 * For coreNIC the number of vNICs exposed should be the same as the
817 	 * number of physical ports
818 	 */
819 	if (total_vnics != (int)nfp_eth_table->count) {
820 		PMD_INIT_LOG(ERR, "Total physical ports do not match number of vNICs");
821 		ret = -ENODEV;
822 		goto app_cleanup;
823 	}
824 
825 	/* Populate coreNIC app properties*/
826 	app_fw_nic->total_phyports = total_vnics;
827 	app_fw_nic->pf_dev = pf_dev;
828 	if (total_vnics > 1)
829 		app_fw_nic->multiport = true;
830 
831 	/* Map the symbol table */
832 	pf_dev->ctrl_bar = nfp_rtsym_map(pf_dev->sym_tbl, "_pf0_net_bar0",
833 			app_fw_nic->total_phyports * 32768, &pf_dev->ctrl_area);
834 	if (pf_dev->ctrl_bar == NULL) {
835 		PMD_INIT_LOG(ERR, "nfp_rtsym_map fails for _pf0_net_ctrl_bar");
836 		ret = -EIO;
837 		goto app_cleanup;
838 	}
839 
840 	PMD_INIT_LOG(DEBUG, "ctrl bar: %p", pf_dev->ctrl_bar);
841 
842 	/* Loop through all physical ports on PF */
843 	numa_node = rte_socket_id();
844 	for (i = 0; i < app_fw_nic->total_phyports; i++) {
845 		snprintf(port_name, sizeof(port_name), "%s_port%d",
846 			 pf_dev->pci_dev->device.name, i);
847 
848 		/* Allocate a eth_dev for this phyport */
849 		eth_dev = rte_eth_dev_allocate(port_name);
850 		if (eth_dev == NULL) {
851 			ret = -ENODEV;
852 			goto port_cleanup;
853 		}
854 
855 		/* Allocate memory for this phyport */
856 		eth_dev->data->dev_private =
857 			rte_zmalloc_socket(port_name, sizeof(struct nfp_net_hw),
858 				RTE_CACHE_LINE_SIZE, numa_node);
859 		if (eth_dev->data->dev_private == NULL) {
860 			ret = -ENOMEM;
861 			rte_eth_dev_release_port(eth_dev);
862 			goto port_cleanup;
863 		}
864 
865 		hw = NFP_NET_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
866 
867 		/* Add this device to the PF's array of physical ports */
868 		app_fw_nic->ports[i] = hw;
869 
870 		hw->pf_dev = pf_dev;
871 		hw->cpp = pf_dev->cpp;
872 		hw->eth_dev = eth_dev;
873 		hw->idx = i;
874 		hw->nfp_idx = nfp_eth_table->ports[i].index;
875 
876 		eth_dev->device = &pf_dev->pci_dev->device;
877 
878 		/* ctrl/tx/rx BAR mappings and remaining init happens in
879 		 * nfp_net_init
880 		 */
881 		ret = nfp_net_init(eth_dev);
882 		if (ret) {
883 			ret = -ENODEV;
884 			goto port_cleanup;
885 		}
886 
887 		rte_eth_dev_probing_finish(eth_dev);
888 
889 	} /* End loop, all ports on this PF */
890 
891 	return 0;
892 
893 port_cleanup:
894 	for (i = 0; i < app_fw_nic->total_phyports; i++) {
895 		if (app_fw_nic->ports[i] && app_fw_nic->ports[i]->eth_dev) {
896 			struct rte_eth_dev *tmp_dev;
897 			tmp_dev = app_fw_nic->ports[i]->eth_dev;
898 			rte_eth_dev_release_port(tmp_dev);
899 			app_fw_nic->ports[i] = NULL;
900 		}
901 	}
902 	nfp_cpp_area_free(pf_dev->ctrl_area);
903 app_cleanup:
904 	rte_free(app_fw_nic);
905 
906 	return ret;
907 }
908 
909 static int
910 nfp_pf_init(struct rte_pci_device *pci_dev)
911 {
912 	int ret;
913 	int err = 0;
914 	uint64_t addr;
915 	struct nfp_cpp *cpp;
916 	enum nfp_app_fw_id app_fw_id;
917 	struct nfp_pf_dev *pf_dev;
918 	struct nfp_hwinfo *hwinfo;
919 	char name[RTE_ETH_NAME_MAX_LEN];
920 	struct nfp_rtsym_table *sym_tbl;
921 	struct nfp_eth_table *nfp_eth_table;
922 
923 	if (pci_dev == NULL)
924 		return -ENODEV;
925 
926 	/*
927 	 * When device bound to UIO, the device could be used, by mistake,
928 	 * by two DPDK apps, and the UIO driver does not avoid it. This
929 	 * could lead to a serious problem when configuring the NFP CPP
930 	 * interface. Here we avoid this telling to the CPP init code to
931 	 * use a lock file if UIO is being used.
932 	 */
933 	if (pci_dev->kdrv == RTE_PCI_KDRV_VFIO)
934 		cpp = nfp_cpp_from_device_name(pci_dev, 0);
935 	else
936 		cpp = nfp_cpp_from_device_name(pci_dev, 1);
937 
938 	if (cpp == NULL) {
939 		PMD_INIT_LOG(ERR, "A CPP handle can not be obtained");
940 		return -EIO;
941 	}
942 
943 	hwinfo = nfp_hwinfo_read(cpp);
944 	if (hwinfo == NULL) {
945 		PMD_INIT_LOG(ERR, "Error reading hwinfo table");
946 		ret = -EIO;
947 		goto cpp_cleanup;
948 	}
949 
950 	/* Read the number of physical ports from hardware */
951 	nfp_eth_table = nfp_eth_read_ports(cpp);
952 	if (nfp_eth_table == NULL) {
953 		PMD_INIT_LOG(ERR, "Error reading NFP ethernet table");
954 		ret = -EIO;
955 		goto hwinfo_cleanup;
956 	}
957 
958 	if (nfp_fw_setup(pci_dev, cpp, nfp_eth_table, hwinfo)) {
959 		PMD_INIT_LOG(ERR, "Error when uploading firmware");
960 		ret = -EIO;
961 		goto eth_table_cleanup;
962 	}
963 
964 	/* Now the symbol table should be there */
965 	sym_tbl = nfp_rtsym_table_read(cpp);
966 	if (sym_tbl == NULL) {
967 		PMD_INIT_LOG(ERR, "Something is wrong with the firmware"
968 				" symbol table");
969 		ret = -EIO;
970 		goto eth_table_cleanup;
971 	}
972 
973 	/* Read the app ID of the firmware loaded */
974 	app_fw_id = nfp_rtsym_read_le(sym_tbl, "_pf0_net_app_id", &err);
975 	if (err != 0) {
976 		PMD_INIT_LOG(ERR, "Couldn't read app_fw_id from fw");
977 		ret = -EIO;
978 		goto sym_tbl_cleanup;
979 	}
980 
981 	/* Allocate memory for the PF "device" */
982 	snprintf(name, sizeof(name), "nfp_pf%d", 0);
983 	pf_dev = rte_zmalloc(name, sizeof(*pf_dev), 0);
984 	if (pf_dev == NULL) {
985 		ret = -ENOMEM;
986 		goto sym_tbl_cleanup;
987 	}
988 
989 	/* Populate the newly created PF device */
990 	pf_dev->app_fw_id = app_fw_id;
991 	pf_dev->cpp = cpp;
992 	pf_dev->hwinfo = hwinfo;
993 	pf_dev->sym_tbl = sym_tbl;
994 	pf_dev->pci_dev = pci_dev;
995 	pf_dev->nfp_eth_table = nfp_eth_table;
996 
997 	/* configure access to tx/rx vNIC BARs */
998 	switch (pci_dev->id.device_id) {
999 	case PCI_DEVICE_ID_NFP3800_PF_NIC:
1000 		addr = NFP_PCIE_QUEUE(NFP_PCIE_QCP_NFP3800_OFFSET,
1001 					0, NFP_PCIE_QUEUE_NFP3800_MASK);
1002 		break;
1003 	case PCI_DEVICE_ID_NFP4000_PF_NIC:
1004 	case PCI_DEVICE_ID_NFP6000_PF_NIC:
1005 		addr = NFP_PCIE_QUEUE(NFP_PCIE_QCP_NFP6000_OFFSET,
1006 					0, NFP_PCIE_QUEUE_NFP6000_MASK);
1007 		break;
1008 	default:
1009 		PMD_INIT_LOG(ERR, "nfp_net: no device ID matching");
1010 		ret = -ENODEV;
1011 		goto pf_cleanup;
1012 	}
1013 
1014 	pf_dev->hw_queues = nfp_cpp_map_area(pf_dev->cpp, 0, 0,
1015 			addr, NFP_QCP_QUEUE_AREA_SZ,
1016 			&pf_dev->hwqueues_area);
1017 	if (pf_dev->hw_queues == NULL) {
1018 		PMD_INIT_LOG(ERR, "nfp_rtsym_map fails for net.qc");
1019 		ret = -EIO;
1020 		goto pf_cleanup;
1021 	}
1022 
1023 	PMD_INIT_LOG(DEBUG, "tx/rx bar address: 0x%p", pf_dev->hw_queues);
1024 
1025 	/*
1026 	 * PF initialization has been done at this point. Call app specific
1027 	 * init code now
1028 	 */
1029 	switch (pf_dev->app_fw_id) {
1030 	case NFP_APP_FW_CORE_NIC:
1031 		PMD_INIT_LOG(INFO, "Initializing coreNIC");
1032 		ret = nfp_init_app_fw_nic(pf_dev);
1033 		if (ret != 0) {
1034 			PMD_INIT_LOG(ERR, "Could not initialize coreNIC!");
1035 			goto hwqueues_cleanup;
1036 		}
1037 		break;
1038 	case NFP_APP_FW_FLOWER_NIC:
1039 		PMD_INIT_LOG(INFO, "Initializing Flower");
1040 		ret = nfp_init_app_fw_flower(pf_dev);
1041 		if (ret != 0) {
1042 			PMD_INIT_LOG(ERR, "Could not initialize Flower!");
1043 			goto hwqueues_cleanup;
1044 		}
1045 		break;
1046 	default:
1047 		PMD_INIT_LOG(ERR, "Unsupported Firmware loaded");
1048 		ret = -EINVAL;
1049 		goto hwqueues_cleanup;
1050 	}
1051 
1052 	/* register the CPP bridge service here for primary use */
1053 	ret = nfp_enable_cpp_service(pf_dev);
1054 	if (ret != 0)
1055 		PMD_INIT_LOG(INFO, "Enable cpp service failed.");
1056 
1057 	return 0;
1058 
1059 hwqueues_cleanup:
1060 	nfp_cpp_area_free(pf_dev->hwqueues_area);
1061 pf_cleanup:
1062 	rte_free(pf_dev);
1063 sym_tbl_cleanup:
1064 	free(sym_tbl);
1065 eth_table_cleanup:
1066 	free(nfp_eth_table);
1067 hwinfo_cleanup:
1068 	free(hwinfo);
1069 cpp_cleanup:
1070 	nfp_cpp_free(cpp);
1071 
1072 	return ret;
1073 }
1074 
1075 static int
1076 nfp_secondary_init_app_fw_nic(struct rte_pci_device *pci_dev,
1077 		struct nfp_rtsym_table *sym_tbl,
1078 		struct nfp_cpp *cpp)
1079 {
1080 	int i;
1081 	int err = 0;
1082 	int ret = 0;
1083 	int total_vnics;
1084 	struct nfp_net_hw *hw;
1085 
1086 	/* Read the number of vNIC's created for the PF */
1087 	total_vnics = nfp_rtsym_read_le(sym_tbl, "nfd_cfg_pf0_num_ports", &err);
1088 	if (err != 0 || total_vnics <= 0 || total_vnics > 8) {
1089 		PMD_INIT_LOG(ERR, "nfd_cfg_pf0_num_ports symbol with wrong value");
1090 		return -ENODEV;
1091 	}
1092 
1093 	for (i = 0; i < total_vnics; i++) {
1094 		struct rte_eth_dev *eth_dev;
1095 		char port_name[RTE_ETH_NAME_MAX_LEN];
1096 		snprintf(port_name, sizeof(port_name), "%s_port%d",
1097 				pci_dev->device.name, i);
1098 
1099 		PMD_INIT_LOG(DEBUG, "Secondary attaching to port %s", port_name);
1100 		eth_dev = rte_eth_dev_attach_secondary(port_name);
1101 		if (eth_dev == NULL) {
1102 			PMD_INIT_LOG(ERR, "Secondary process attach to port %s failed", port_name);
1103 			ret = -ENODEV;
1104 			break;
1105 		}
1106 
1107 		eth_dev->process_private = cpp;
1108 		hw = NFP_NET_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
1109 		if (nfp_net_ethdev_ops_mount(hw, eth_dev))
1110 			return -EINVAL;
1111 
1112 		rte_eth_dev_probing_finish(eth_dev);
1113 	}
1114 
1115 	return ret;
1116 }
1117 
1118 /*
1119  * When attaching to the NFP4000/6000 PF on a secondary process there
1120  * is no need to initialise the PF again. Only minimal work is required
1121  * here
1122  */
1123 static int
1124 nfp_pf_secondary_init(struct rte_pci_device *pci_dev)
1125 {
1126 	int err = 0;
1127 	int ret = 0;
1128 	struct nfp_cpp *cpp;
1129 	enum nfp_app_fw_id app_fw_id;
1130 	struct nfp_rtsym_table *sym_tbl;
1131 
1132 	if (pci_dev == NULL)
1133 		return -ENODEV;
1134 
1135 	/*
1136 	 * When device bound to UIO, the device could be used, by mistake,
1137 	 * by two DPDK apps, and the UIO driver does not avoid it. This
1138 	 * could lead to a serious problem when configuring the NFP CPP
1139 	 * interface. Here we avoid this telling to the CPP init code to
1140 	 * use a lock file if UIO is being used.
1141 	 */
1142 	if (pci_dev->kdrv == RTE_PCI_KDRV_VFIO)
1143 		cpp = nfp_cpp_from_device_name(pci_dev, 0);
1144 	else
1145 		cpp = nfp_cpp_from_device_name(pci_dev, 1);
1146 
1147 	if (cpp == NULL) {
1148 		PMD_INIT_LOG(ERR, "A CPP handle can not be obtained");
1149 		return -EIO;
1150 	}
1151 
1152 	/*
1153 	 * We don't have access to the PF created in the primary process
1154 	 * here so we have to read the number of ports from firmware
1155 	 */
1156 	sym_tbl = nfp_rtsym_table_read(cpp);
1157 	if (sym_tbl == NULL) {
1158 		PMD_INIT_LOG(ERR, "Something is wrong with the firmware"
1159 				" symbol table");
1160 		return -EIO;
1161 	}
1162 
1163 	/* Read the app ID of the firmware loaded */
1164 	app_fw_id = nfp_rtsym_read_le(sym_tbl, "_pf0_net_app_id", &err);
1165 	if (err != 0) {
1166 		PMD_INIT_LOG(ERR, "Couldn't read app_fw_id from fw");
1167 		goto sym_tbl_cleanup;
1168 	}
1169 
1170 	switch (app_fw_id) {
1171 	case NFP_APP_FW_CORE_NIC:
1172 		PMD_INIT_LOG(INFO, "Initializing coreNIC");
1173 		ret = nfp_secondary_init_app_fw_nic(pci_dev, sym_tbl, cpp);
1174 		if (ret != 0) {
1175 			PMD_INIT_LOG(ERR, "Could not initialize coreNIC!");
1176 			goto sym_tbl_cleanup;
1177 		}
1178 		break;
1179 	case NFP_APP_FW_FLOWER_NIC:
1180 		PMD_INIT_LOG(INFO, "Initializing Flower");
1181 		ret = nfp_secondary_init_app_fw_flower(cpp);
1182 		if (ret != 0) {
1183 			PMD_INIT_LOG(ERR, "Could not initialize Flower!");
1184 			goto sym_tbl_cleanup;
1185 		}
1186 		break;
1187 	default:
1188 		PMD_INIT_LOG(ERR, "Unsupported Firmware loaded");
1189 		ret = -EINVAL;
1190 		goto sym_tbl_cleanup;
1191 	}
1192 
1193 sym_tbl_cleanup:
1194 	free(sym_tbl);
1195 
1196 	return ret;
1197 }
1198 
1199 static int
1200 nfp_pf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1201 		struct rte_pci_device *dev)
1202 {
1203 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1204 		return nfp_pf_init(dev);
1205 	else
1206 		return nfp_pf_secondary_init(dev);
1207 }
1208 
1209 static const struct rte_pci_id pci_id_nfp_pf_net_map[] = {
1210 	{
1211 		RTE_PCI_DEVICE(PCI_VENDOR_ID_NETRONOME,
1212 			       PCI_DEVICE_ID_NFP3800_PF_NIC)
1213 	},
1214 	{
1215 		RTE_PCI_DEVICE(PCI_VENDOR_ID_NETRONOME,
1216 			       PCI_DEVICE_ID_NFP4000_PF_NIC)
1217 	},
1218 	{
1219 		RTE_PCI_DEVICE(PCI_VENDOR_ID_NETRONOME,
1220 			       PCI_DEVICE_ID_NFP6000_PF_NIC)
1221 	},
1222 	{
1223 		RTE_PCI_DEVICE(PCI_VENDOR_ID_CORIGINE,
1224 			       PCI_DEVICE_ID_NFP3800_PF_NIC)
1225 	},
1226 	{
1227 		RTE_PCI_DEVICE(PCI_VENDOR_ID_CORIGINE,
1228 			       PCI_DEVICE_ID_NFP4000_PF_NIC)
1229 	},
1230 	{
1231 		RTE_PCI_DEVICE(PCI_VENDOR_ID_CORIGINE,
1232 			       PCI_DEVICE_ID_NFP6000_PF_NIC)
1233 	},
1234 	{
1235 		.vendor_id = 0,
1236 	},
1237 };
1238 
1239 static int
1240 nfp_pci_uninit(struct rte_eth_dev *eth_dev)
1241 {
1242 	struct rte_pci_device *pci_dev;
1243 	uint16_t port_id;
1244 
1245 	pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1246 
1247 	/* Free up all physical ports under PF */
1248 	RTE_ETH_FOREACH_DEV_OF(port_id, &pci_dev->device)
1249 		rte_eth_dev_close(port_id);
1250 	/*
1251 	 * Ports can be closed and freed but hotplugging is not
1252 	 * currently supported
1253 	 */
1254 	return -ENOTSUP;
1255 }
1256 
1257 static int
1258 eth_nfp_pci_remove(struct rte_pci_device *pci_dev)
1259 {
1260 	return rte_eth_dev_pci_generic_remove(pci_dev, nfp_pci_uninit);
1261 }
1262 
1263 static struct rte_pci_driver rte_nfp_net_pf_pmd = {
1264 	.id_table = pci_id_nfp_pf_net_map,
1265 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
1266 	.probe = nfp_pf_pci_probe,
1267 	.remove = eth_nfp_pci_remove,
1268 };
1269 
1270 RTE_PMD_REGISTER_PCI(net_nfp_pf, rte_nfp_net_pf_pmd);
1271 RTE_PMD_REGISTER_PCI_TABLE(net_nfp_pf, pci_id_nfp_pf_net_map);
1272 RTE_PMD_REGISTER_KMOD_DEP(net_nfp_pf, "* igb_uio | uio_pci_generic | vfio");
1273 /*
1274  * Local variables:
1275  * c-file-style: "Linux"
1276  * indent-tabs-mode: t
1277  * End:
1278  */
1279