xref: /dpdk/drivers/net/nfp/nfp_ethdev.c (revision 4b53e9802b6b6040ad5622b1414aaa93d9581d0c)
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 	PMD_INIT_LOG(INFO, "VER: %u.%u, Maximum supported MTU: %d",
623 			   NFD_CFG_MAJOR_VERSION_of(hw->ver),
624 			   NFD_CFG_MINOR_VERSION_of(hw->ver), hw->max_mtu);
625 
626 	PMD_INIT_LOG(INFO, "CAP: %#x, %s%s%s%s%s%s%s%s%s%s%s%s%s%s", hw->cap,
627 		     hw->cap & NFP_NET_CFG_CTRL_PROMISC ? "PROMISC " : "",
628 		     hw->cap & NFP_NET_CFG_CTRL_L2BC    ? "L2BCFILT " : "",
629 		     hw->cap & NFP_NET_CFG_CTRL_L2MC    ? "L2MCFILT " : "",
630 		     hw->cap & NFP_NET_CFG_CTRL_RXCSUM  ? "RXCSUM "  : "",
631 		     hw->cap & NFP_NET_CFG_CTRL_TXCSUM  ? "TXCSUM "  : "",
632 		     hw->cap & NFP_NET_CFG_CTRL_RXVLAN  ? "RXVLAN "  : "",
633 		     hw->cap & NFP_NET_CFG_CTRL_TXVLAN  ? "TXVLAN "  : "",
634 		     hw->cap & NFP_NET_CFG_CTRL_SCATTER ? "SCATTER " : "",
635 		     hw->cap & NFP_NET_CFG_CTRL_GATHER  ? "GATHER "  : "",
636 		     hw->cap & NFP_NET_CFG_CTRL_LIVE_ADDR ? "LIVE_ADDR "  : "",
637 		     hw->cap & NFP_NET_CFG_CTRL_LSO     ? "TSO "     : "",
638 		     hw->cap & NFP_NET_CFG_CTRL_LSO2     ? "TSOv2 "     : "",
639 		     hw->cap & NFP_NET_CFG_CTRL_RSS     ? "RSS "     : "",
640 		     hw->cap & NFP_NET_CFG_CTRL_RSS2     ? "RSSv2 "     : "");
641 
642 	hw->ctrl = 0;
643 
644 	hw->stride_rx = stride;
645 	hw->stride_tx = stride;
646 
647 	PMD_INIT_LOG(INFO, "max_rx_queues: %u, max_tx_queues: %u",
648 		     hw->max_rx_queues, hw->max_tx_queues);
649 
650 	/* Initializing spinlock for reconfigs */
651 	rte_spinlock_init(&hw->reconfig_lock);
652 
653 	/* Allocating memory for mac addr */
654 	eth_dev->data->mac_addrs = rte_zmalloc("mac_addr",
655 					       RTE_ETHER_ADDR_LEN, 0);
656 	if (eth_dev->data->mac_addrs == NULL) {
657 		PMD_INIT_LOG(ERR, "Failed to space for MAC address");
658 		return -ENOMEM;
659 	}
660 
661 	nfp_net_pf_read_mac(app_fw_nic, port);
662 	nfp_net_write_mac(hw, (uint8_t *)&hw->mac_addr);
663 
664 	tmp_ether_addr = (struct rte_ether_addr *)&hw->mac_addr;
665 	if (!rte_is_valid_assigned_ether_addr(tmp_ether_addr)) {
666 		PMD_INIT_LOG(INFO, "Using random mac address for port %d", port);
667 		/* Using random mac addresses for VFs */
668 		rte_eth_random_addr(&hw->mac_addr[0]);
669 		nfp_net_write_mac(hw, (uint8_t *)&hw->mac_addr);
670 	}
671 
672 	/* Copying mac address to DPDK eth_dev struct */
673 	rte_ether_addr_copy((struct rte_ether_addr *)hw->mac_addr,
674 			&eth_dev->data->mac_addrs[0]);
675 
676 	if (!(hw->cap & NFP_NET_CFG_CTRL_LIVE_ADDR))
677 		eth_dev->data->dev_flags |= RTE_ETH_DEV_NOLIVE_MAC_ADDR;
678 
679 	eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
680 
681 	PMD_INIT_LOG(INFO, "port %d VendorID=0x%x DeviceID=0x%x "
682 		     "mac=" RTE_ETHER_ADDR_PRT_FMT,
683 		     eth_dev->data->port_id, pci_dev->id.vendor_id,
684 		     pci_dev->id.device_id,
685 		     hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
686 		     hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
687 
688 	/* Registering LSC interrupt handler */
689 	rte_intr_callback_register(pci_dev->intr_handle,
690 			nfp_net_dev_interrupt_handler, (void *)eth_dev);
691 	/* Telling the firmware about the LSC interrupt entry */
692 	nn_cfg_writeb(hw, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
693 	/* Recording current stats counters values */
694 	nfp_net_stats_reset(eth_dev);
695 
696 	return 0;
697 }
698 
699 #define DEFAULT_FW_PATH       "/lib/firmware/netronome"
700 
701 static int
702 nfp_fw_upload(struct rte_pci_device *dev, struct nfp_nsp *nsp, char *card)
703 {
704 	struct nfp_cpp *cpp = nsp->cpp;
705 	void *fw_buf;
706 	char fw_name[125];
707 	char serial[40];
708 	size_t fsize;
709 
710 	/* Looking for firmware file in order of priority */
711 
712 	/* First try to find a firmware image specific for this device */
713 	snprintf(serial, sizeof(serial),
714 			"serial-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x",
715 		cpp->serial[0], cpp->serial[1], cpp->serial[2], cpp->serial[3],
716 		cpp->serial[4], cpp->serial[5], cpp->interface >> 8,
717 		cpp->interface & 0xff);
718 
719 	snprintf(fw_name, sizeof(fw_name), "%s/%s.nffw", DEFAULT_FW_PATH,
720 			serial);
721 
722 	PMD_DRV_LOG(DEBUG, "Trying with fw file: %s", fw_name);
723 	if (rte_firmware_read(fw_name, &fw_buf, &fsize) == 0)
724 		goto load_fw;
725 	/* Then try the PCI name */
726 	snprintf(fw_name, sizeof(fw_name), "%s/pci-%s.nffw", DEFAULT_FW_PATH,
727 			dev->device.name);
728 
729 	PMD_DRV_LOG(DEBUG, "Trying with fw file: %s", fw_name);
730 	if (rte_firmware_read(fw_name, &fw_buf, &fsize) == 0)
731 		goto load_fw;
732 
733 	/* Finally try the card type and media */
734 	snprintf(fw_name, sizeof(fw_name), "%s/%s", DEFAULT_FW_PATH, card);
735 	PMD_DRV_LOG(DEBUG, "Trying with fw file: %s", fw_name);
736 	if (rte_firmware_read(fw_name, &fw_buf, &fsize) < 0) {
737 		PMD_DRV_LOG(INFO, "Firmware file %s not found.", fw_name);
738 		return -ENOENT;
739 	}
740 
741 load_fw:
742 	PMD_DRV_LOG(INFO, "Firmware file found at %s with size: %zu",
743 		fw_name, fsize);
744 	PMD_DRV_LOG(INFO, "Uploading the firmware ...");
745 	nfp_nsp_load_fw(nsp, fw_buf, fsize);
746 	PMD_DRV_LOG(INFO, "Done");
747 
748 	free(fw_buf);
749 
750 	return 0;
751 }
752 
753 static int
754 nfp_fw_setup(struct rte_pci_device *dev,
755 		struct nfp_cpp *cpp,
756 		struct nfp_eth_table *nfp_eth_table,
757 		struct nfp_hwinfo *hwinfo)
758 {
759 	struct nfp_nsp *nsp;
760 	const char *nfp_fw_model;
761 	char card_desc[100];
762 	int err = 0;
763 
764 	nfp_fw_model = nfp_hwinfo_lookup(hwinfo, "nffw.partno");
765 	if (nfp_fw_model == NULL)
766 		nfp_fw_model = nfp_hwinfo_lookup(hwinfo, "assembly.partno");
767 
768 	if (nfp_fw_model) {
769 		PMD_DRV_LOG(INFO, "firmware model found: %s", nfp_fw_model);
770 	} else {
771 		PMD_DRV_LOG(ERR, "firmware model NOT found");
772 		return -EIO;
773 	}
774 
775 	if (nfp_eth_table->count == 0 || nfp_eth_table->count > 8) {
776 		PMD_DRV_LOG(ERR, "NFP ethernet table reports wrong ports: %u",
777 			nfp_eth_table->count);
778 		return -EIO;
779 	}
780 
781 	PMD_DRV_LOG(INFO, "NFP ethernet port table reports %u ports",
782 			nfp_eth_table->count);
783 
784 	PMD_DRV_LOG(INFO, "Port speed: %u", nfp_eth_table->ports[0].speed);
785 
786 	snprintf(card_desc, sizeof(card_desc), "nic_%s_%dx%d.nffw",
787 			nfp_fw_model, nfp_eth_table->count,
788 			nfp_eth_table->ports[0].speed / 1000);
789 
790 	nsp = nfp_nsp_open(cpp);
791 	if (nsp == NULL) {
792 		PMD_DRV_LOG(ERR, "NFP error when obtaining NSP handle");
793 		return -EIO;
794 	}
795 
796 	nfp_nsp_device_soft_reset(nsp);
797 	err = nfp_fw_upload(dev, nsp, card_desc);
798 
799 	nfp_nsp_close(nsp);
800 	return err;
801 }
802 
803 static int
804 nfp_init_app_fw_nic(struct nfp_pf_dev *pf_dev)
805 {
806 	int i;
807 	int ret;
808 	int err = 0;
809 	int total_vnics;
810 	struct nfp_net_hw *hw;
811 	unsigned int numa_node;
812 	struct rte_eth_dev *eth_dev;
813 	struct nfp_app_fw_nic *app_fw_nic;
814 	struct nfp_eth_table *nfp_eth_table;
815 	char port_name[RTE_ETH_NAME_MAX_LEN];
816 
817 	nfp_eth_table = pf_dev->nfp_eth_table;
818 	PMD_INIT_LOG(INFO, "Total physical ports: %d", nfp_eth_table->count);
819 
820 	/* Allocate memory for the CoreNIC app */
821 	app_fw_nic = rte_zmalloc("nfp_app_fw_nic", sizeof(*app_fw_nic), 0);
822 	if (app_fw_nic == NULL)
823 		return -ENOMEM;
824 
825 	/* Point the app_fw_priv pointer in the PF to the coreNIC app */
826 	pf_dev->app_fw_priv = app_fw_nic;
827 
828 	/* Read the number of vNIC's created for the PF */
829 	total_vnics = nfp_rtsym_read_le(pf_dev->sym_tbl, "nfd_cfg_pf0_num_ports", &err);
830 	if (err != 0 || total_vnics <= 0 || total_vnics > 8) {
831 		PMD_INIT_LOG(ERR, "nfd_cfg_pf0_num_ports symbol with wrong value");
832 		ret = -ENODEV;
833 		goto app_cleanup;
834 	}
835 
836 	/*
837 	 * For coreNIC the number of vNICs exposed should be the same as the
838 	 * number of physical ports
839 	 */
840 	if (total_vnics != (int)nfp_eth_table->count) {
841 		PMD_INIT_LOG(ERR, "Total physical ports do not match number of vNICs");
842 		ret = -ENODEV;
843 		goto app_cleanup;
844 	}
845 
846 	/* Populate coreNIC app properties*/
847 	app_fw_nic->total_phyports = total_vnics;
848 	app_fw_nic->pf_dev = pf_dev;
849 	if (total_vnics > 1)
850 		app_fw_nic->multiport = true;
851 
852 	/* Map the symbol table */
853 	pf_dev->ctrl_bar = nfp_rtsym_map(pf_dev->sym_tbl, "_pf0_net_bar0",
854 			app_fw_nic->total_phyports * 32768, &pf_dev->ctrl_area);
855 	if (pf_dev->ctrl_bar == NULL) {
856 		PMD_INIT_LOG(ERR, "nfp_rtsym_map fails for _pf0_net_ctrl_bar");
857 		ret = -EIO;
858 		goto app_cleanup;
859 	}
860 
861 	PMD_INIT_LOG(DEBUG, "ctrl bar: %p", pf_dev->ctrl_bar);
862 
863 	/* Loop through all physical ports on PF */
864 	numa_node = rte_socket_id();
865 	for (i = 0; i < app_fw_nic->total_phyports; i++) {
866 		snprintf(port_name, sizeof(port_name), "%s_port%d",
867 			 pf_dev->pci_dev->device.name, i);
868 
869 		/* Allocate a eth_dev for this phyport */
870 		eth_dev = rte_eth_dev_allocate(port_name);
871 		if (eth_dev == NULL) {
872 			ret = -ENODEV;
873 			goto port_cleanup;
874 		}
875 
876 		/* Allocate memory for this phyport */
877 		eth_dev->data->dev_private =
878 			rte_zmalloc_socket(port_name, sizeof(struct nfp_net_hw),
879 				RTE_CACHE_LINE_SIZE, numa_node);
880 		if (eth_dev->data->dev_private == NULL) {
881 			ret = -ENOMEM;
882 			rte_eth_dev_release_port(eth_dev);
883 			goto port_cleanup;
884 		}
885 
886 		hw = NFP_NET_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
887 
888 		/* Add this device to the PF's array of physical ports */
889 		app_fw_nic->ports[i] = hw;
890 
891 		hw->pf_dev = pf_dev;
892 		hw->cpp = pf_dev->cpp;
893 		hw->eth_dev = eth_dev;
894 		hw->idx = i;
895 		hw->nfp_idx = nfp_eth_table->ports[i].index;
896 
897 		eth_dev->device = &pf_dev->pci_dev->device;
898 
899 		/* ctrl/tx/rx BAR mappings and remaining init happens in
900 		 * nfp_net_init
901 		 */
902 		ret = nfp_net_init(eth_dev);
903 		if (ret) {
904 			ret = -ENODEV;
905 			goto port_cleanup;
906 		}
907 
908 		rte_eth_dev_probing_finish(eth_dev);
909 
910 	} /* End loop, all ports on this PF */
911 
912 	return 0;
913 
914 port_cleanup:
915 	for (i = 0; i < app_fw_nic->total_phyports; i++) {
916 		if (app_fw_nic->ports[i] && app_fw_nic->ports[i]->eth_dev) {
917 			struct rte_eth_dev *tmp_dev;
918 			tmp_dev = app_fw_nic->ports[i]->eth_dev;
919 			rte_eth_dev_release_port(tmp_dev);
920 			app_fw_nic->ports[i] = NULL;
921 		}
922 	}
923 	nfp_cpp_area_free(pf_dev->ctrl_area);
924 app_cleanup:
925 	rte_free(app_fw_nic);
926 
927 	return ret;
928 }
929 
930 static int
931 nfp_pf_init(struct rte_pci_device *pci_dev)
932 {
933 	int ret;
934 	int err = 0;
935 	uint64_t addr;
936 	struct nfp_cpp *cpp;
937 	enum nfp_app_fw_id app_fw_id;
938 	struct nfp_pf_dev *pf_dev;
939 	struct nfp_hwinfo *hwinfo;
940 	char name[RTE_ETH_NAME_MAX_LEN];
941 	struct nfp_rtsym_table *sym_tbl;
942 	struct nfp_eth_table *nfp_eth_table;
943 
944 	if (pci_dev == NULL)
945 		return -ENODEV;
946 
947 	/*
948 	 * When device bound to UIO, the device could be used, by mistake,
949 	 * by two DPDK apps, and the UIO driver does not avoid it. This
950 	 * could lead to a serious problem when configuring the NFP CPP
951 	 * interface. Here we avoid this telling to the CPP init code to
952 	 * use a lock file if UIO is being used.
953 	 */
954 	if (pci_dev->kdrv == RTE_PCI_KDRV_VFIO)
955 		cpp = nfp_cpp_from_device_name(pci_dev, 0);
956 	else
957 		cpp = nfp_cpp_from_device_name(pci_dev, 1);
958 
959 	if (cpp == NULL) {
960 		PMD_INIT_LOG(ERR, "A CPP handle can not be obtained");
961 		return -EIO;
962 	}
963 
964 	hwinfo = nfp_hwinfo_read(cpp);
965 	if (hwinfo == NULL) {
966 		PMD_INIT_LOG(ERR, "Error reading hwinfo table");
967 		ret = -EIO;
968 		goto cpp_cleanup;
969 	}
970 
971 	/* Read the number of physical ports from hardware */
972 	nfp_eth_table = nfp_eth_read_ports(cpp);
973 	if (nfp_eth_table == NULL) {
974 		PMD_INIT_LOG(ERR, "Error reading NFP ethernet table");
975 		ret = -EIO;
976 		goto hwinfo_cleanup;
977 	}
978 
979 	if (nfp_fw_setup(pci_dev, cpp, nfp_eth_table, hwinfo)) {
980 		PMD_INIT_LOG(ERR, "Error when uploading firmware");
981 		ret = -EIO;
982 		goto eth_table_cleanup;
983 	}
984 
985 	/* Now the symbol table should be there */
986 	sym_tbl = nfp_rtsym_table_read(cpp);
987 	if (sym_tbl == NULL) {
988 		PMD_INIT_LOG(ERR, "Something is wrong with the firmware"
989 				" symbol table");
990 		ret = -EIO;
991 		goto eth_table_cleanup;
992 	}
993 
994 	/* Read the app ID of the firmware loaded */
995 	app_fw_id = nfp_rtsym_read_le(sym_tbl, "_pf0_net_app_id", &err);
996 	if (err != 0) {
997 		PMD_INIT_LOG(ERR, "Couldn't read app_fw_id from fw");
998 		ret = -EIO;
999 		goto sym_tbl_cleanup;
1000 	}
1001 
1002 	/* Allocate memory for the PF "device" */
1003 	snprintf(name, sizeof(name), "nfp_pf%d", 0);
1004 	pf_dev = rte_zmalloc(name, sizeof(*pf_dev), 0);
1005 	if (pf_dev == NULL) {
1006 		ret = -ENOMEM;
1007 		goto sym_tbl_cleanup;
1008 	}
1009 
1010 	/* Populate the newly created PF device */
1011 	pf_dev->app_fw_id = app_fw_id;
1012 	pf_dev->cpp = cpp;
1013 	pf_dev->hwinfo = hwinfo;
1014 	pf_dev->sym_tbl = sym_tbl;
1015 	pf_dev->pci_dev = pci_dev;
1016 	pf_dev->nfp_eth_table = nfp_eth_table;
1017 
1018 	/* configure access to tx/rx vNIC BARs */
1019 	switch (pci_dev->id.device_id) {
1020 	case PCI_DEVICE_ID_NFP3800_PF_NIC:
1021 		addr = NFP_PCIE_QUEUE(NFP_PCIE_QCP_NFP3800_OFFSET,
1022 					0, NFP_PCIE_QUEUE_NFP3800_MASK);
1023 		break;
1024 	case PCI_DEVICE_ID_NFP4000_PF_NIC:
1025 	case PCI_DEVICE_ID_NFP6000_PF_NIC:
1026 		addr = NFP_PCIE_QUEUE(NFP_PCIE_QCP_NFP6000_OFFSET,
1027 					0, NFP_PCIE_QUEUE_NFP6000_MASK);
1028 		break;
1029 	default:
1030 		PMD_INIT_LOG(ERR, "nfp_net: no device ID matching");
1031 		ret = -ENODEV;
1032 		goto pf_cleanup;
1033 	}
1034 
1035 	pf_dev->hw_queues = nfp_cpp_map_area(pf_dev->cpp, 0, 0,
1036 			addr, NFP_QCP_QUEUE_AREA_SZ,
1037 			&pf_dev->hwqueues_area);
1038 	if (pf_dev->hw_queues == NULL) {
1039 		PMD_INIT_LOG(ERR, "nfp_rtsym_map fails for net.qc");
1040 		ret = -EIO;
1041 		goto pf_cleanup;
1042 	}
1043 
1044 	PMD_INIT_LOG(DEBUG, "tx/rx bar address: 0x%p", pf_dev->hw_queues);
1045 
1046 	/*
1047 	 * PF initialization has been done at this point. Call app specific
1048 	 * init code now
1049 	 */
1050 	switch (pf_dev->app_fw_id) {
1051 	case NFP_APP_FW_CORE_NIC:
1052 		PMD_INIT_LOG(INFO, "Initializing coreNIC");
1053 		ret = nfp_init_app_fw_nic(pf_dev);
1054 		if (ret != 0) {
1055 			PMD_INIT_LOG(ERR, "Could not initialize coreNIC!");
1056 			goto hwqueues_cleanup;
1057 		}
1058 		break;
1059 	case NFP_APP_FW_FLOWER_NIC:
1060 		PMD_INIT_LOG(INFO, "Initializing Flower");
1061 		ret = nfp_init_app_fw_flower(pf_dev);
1062 		if (ret != 0) {
1063 			PMD_INIT_LOG(ERR, "Could not initialize Flower!");
1064 			goto hwqueues_cleanup;
1065 		}
1066 		break;
1067 	default:
1068 		PMD_INIT_LOG(ERR, "Unsupported Firmware loaded");
1069 		ret = -EINVAL;
1070 		goto hwqueues_cleanup;
1071 	}
1072 
1073 	/* register the CPP bridge service here for primary use */
1074 	ret = nfp_enable_cpp_service(pf_dev);
1075 	if (ret != 0)
1076 		PMD_INIT_LOG(INFO, "Enable cpp service failed.");
1077 
1078 	return 0;
1079 
1080 hwqueues_cleanup:
1081 	nfp_cpp_area_free(pf_dev->hwqueues_area);
1082 pf_cleanup:
1083 	rte_free(pf_dev);
1084 sym_tbl_cleanup:
1085 	free(sym_tbl);
1086 eth_table_cleanup:
1087 	free(nfp_eth_table);
1088 hwinfo_cleanup:
1089 	free(hwinfo);
1090 cpp_cleanup:
1091 	nfp_cpp_free(cpp);
1092 
1093 	return ret;
1094 }
1095 
1096 static int
1097 nfp_secondary_init_app_fw_nic(struct rte_pci_device *pci_dev,
1098 		struct nfp_rtsym_table *sym_tbl,
1099 		struct nfp_cpp *cpp)
1100 {
1101 	int i;
1102 	int err = 0;
1103 	int ret = 0;
1104 	int total_vnics;
1105 	struct nfp_net_hw *hw;
1106 
1107 	/* Read the number of vNIC's created for the PF */
1108 	total_vnics = nfp_rtsym_read_le(sym_tbl, "nfd_cfg_pf0_num_ports", &err);
1109 	if (err != 0 || total_vnics <= 0 || total_vnics > 8) {
1110 		PMD_INIT_LOG(ERR, "nfd_cfg_pf0_num_ports symbol with wrong value");
1111 		return -ENODEV;
1112 	}
1113 
1114 	for (i = 0; i < total_vnics; i++) {
1115 		struct rte_eth_dev *eth_dev;
1116 		char port_name[RTE_ETH_NAME_MAX_LEN];
1117 		snprintf(port_name, sizeof(port_name), "%s_port%d",
1118 				pci_dev->device.name, i);
1119 
1120 		PMD_INIT_LOG(DEBUG, "Secondary attaching to port %s", port_name);
1121 		eth_dev = rte_eth_dev_attach_secondary(port_name);
1122 		if (eth_dev == NULL) {
1123 			PMD_INIT_LOG(ERR, "Secondary process attach to port %s failed", port_name);
1124 			ret = -ENODEV;
1125 			break;
1126 		}
1127 
1128 		eth_dev->process_private = cpp;
1129 		hw = NFP_NET_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
1130 		if (nfp_net_ethdev_ops_mount(hw, eth_dev))
1131 			return -EINVAL;
1132 
1133 		rte_eth_dev_probing_finish(eth_dev);
1134 	}
1135 
1136 	return ret;
1137 }
1138 
1139 /*
1140  * When attaching to the NFP4000/6000 PF on a secondary process there
1141  * is no need to initialise the PF again. Only minimal work is required
1142  * here
1143  */
1144 static int
1145 nfp_pf_secondary_init(struct rte_pci_device *pci_dev)
1146 {
1147 	int err = 0;
1148 	int ret = 0;
1149 	struct nfp_cpp *cpp;
1150 	enum nfp_app_fw_id app_fw_id;
1151 	struct nfp_rtsym_table *sym_tbl;
1152 
1153 	if (pci_dev == NULL)
1154 		return -ENODEV;
1155 
1156 	/*
1157 	 * When device bound to UIO, the device could be used, by mistake,
1158 	 * by two DPDK apps, and the UIO driver does not avoid it. This
1159 	 * could lead to a serious problem when configuring the NFP CPP
1160 	 * interface. Here we avoid this telling to the CPP init code to
1161 	 * use a lock file if UIO is being used.
1162 	 */
1163 	if (pci_dev->kdrv == RTE_PCI_KDRV_VFIO)
1164 		cpp = nfp_cpp_from_device_name(pci_dev, 0);
1165 	else
1166 		cpp = nfp_cpp_from_device_name(pci_dev, 1);
1167 
1168 	if (cpp == NULL) {
1169 		PMD_INIT_LOG(ERR, "A CPP handle can not be obtained");
1170 		return -EIO;
1171 	}
1172 
1173 	/*
1174 	 * We don't have access to the PF created in the primary process
1175 	 * here so we have to read the number of ports from firmware
1176 	 */
1177 	sym_tbl = nfp_rtsym_table_read(cpp);
1178 	if (sym_tbl == NULL) {
1179 		PMD_INIT_LOG(ERR, "Something is wrong with the firmware"
1180 				" symbol table");
1181 		return -EIO;
1182 	}
1183 
1184 	/* Read the app ID of the firmware loaded */
1185 	app_fw_id = nfp_rtsym_read_le(sym_tbl, "_pf0_net_app_id", &err);
1186 	if (err != 0) {
1187 		PMD_INIT_LOG(ERR, "Couldn't read app_fw_id from fw");
1188 		goto sym_tbl_cleanup;
1189 	}
1190 
1191 	switch (app_fw_id) {
1192 	case NFP_APP_FW_CORE_NIC:
1193 		PMD_INIT_LOG(INFO, "Initializing coreNIC");
1194 		ret = nfp_secondary_init_app_fw_nic(pci_dev, sym_tbl, cpp);
1195 		if (ret != 0) {
1196 			PMD_INIT_LOG(ERR, "Could not initialize coreNIC!");
1197 			goto sym_tbl_cleanup;
1198 		}
1199 		break;
1200 	case NFP_APP_FW_FLOWER_NIC:
1201 		PMD_INIT_LOG(INFO, "Initializing Flower");
1202 		ret = nfp_secondary_init_app_fw_flower(cpp);
1203 		if (ret != 0) {
1204 			PMD_INIT_LOG(ERR, "Could not initialize Flower!");
1205 			goto sym_tbl_cleanup;
1206 		}
1207 		break;
1208 	default:
1209 		PMD_INIT_LOG(ERR, "Unsupported Firmware loaded");
1210 		ret = -EINVAL;
1211 		goto sym_tbl_cleanup;
1212 	}
1213 
1214 sym_tbl_cleanup:
1215 	free(sym_tbl);
1216 
1217 	return ret;
1218 }
1219 
1220 static int
1221 nfp_pf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1222 		struct rte_pci_device *dev)
1223 {
1224 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1225 		return nfp_pf_init(dev);
1226 	else
1227 		return nfp_pf_secondary_init(dev);
1228 }
1229 
1230 static const struct rte_pci_id pci_id_nfp_pf_net_map[] = {
1231 	{
1232 		RTE_PCI_DEVICE(PCI_VENDOR_ID_NETRONOME,
1233 			       PCI_DEVICE_ID_NFP3800_PF_NIC)
1234 	},
1235 	{
1236 		RTE_PCI_DEVICE(PCI_VENDOR_ID_NETRONOME,
1237 			       PCI_DEVICE_ID_NFP4000_PF_NIC)
1238 	},
1239 	{
1240 		RTE_PCI_DEVICE(PCI_VENDOR_ID_NETRONOME,
1241 			       PCI_DEVICE_ID_NFP6000_PF_NIC)
1242 	},
1243 	{
1244 		RTE_PCI_DEVICE(PCI_VENDOR_ID_CORIGINE,
1245 			       PCI_DEVICE_ID_NFP3800_PF_NIC)
1246 	},
1247 	{
1248 		RTE_PCI_DEVICE(PCI_VENDOR_ID_CORIGINE,
1249 			       PCI_DEVICE_ID_NFP4000_PF_NIC)
1250 	},
1251 	{
1252 		RTE_PCI_DEVICE(PCI_VENDOR_ID_CORIGINE,
1253 			       PCI_DEVICE_ID_NFP6000_PF_NIC)
1254 	},
1255 	{
1256 		.vendor_id = 0,
1257 	},
1258 };
1259 
1260 static int
1261 nfp_pci_uninit(struct rte_eth_dev *eth_dev)
1262 {
1263 	struct rte_pci_device *pci_dev;
1264 	uint16_t port_id;
1265 
1266 	pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1267 
1268 	/* Free up all physical ports under PF */
1269 	RTE_ETH_FOREACH_DEV_OF(port_id, &pci_dev->device)
1270 		rte_eth_dev_close(port_id);
1271 	/*
1272 	 * Ports can be closed and freed but hotplugging is not
1273 	 * currently supported
1274 	 */
1275 	return -ENOTSUP;
1276 }
1277 
1278 static int
1279 eth_nfp_pci_remove(struct rte_pci_device *pci_dev)
1280 {
1281 	return rte_eth_dev_pci_generic_remove(pci_dev, nfp_pci_uninit);
1282 }
1283 
1284 static struct rte_pci_driver rte_nfp_net_pf_pmd = {
1285 	.id_table = pci_id_nfp_pf_net_map,
1286 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
1287 	.probe = nfp_pf_pci_probe,
1288 	.remove = eth_nfp_pci_remove,
1289 };
1290 
1291 RTE_PMD_REGISTER_PCI(net_nfp_pf, rte_nfp_net_pf_pmd);
1292 RTE_PMD_REGISTER_PCI_TABLE(net_nfp_pf, pci_id_nfp_pf_net_map);
1293 RTE_PMD_REGISTER_KMOD_DEP(net_nfp_pf, "* igb_uio | uio_pci_generic | vfio");
1294 /*
1295  * Local variables:
1296  * c-file-style: "Linux"
1297  * indent-tabs-mode: t
1298  * End:
1299  */
1300