xref: /openbmc/linux/drivers/net/ethernet/netronome/nfp/nfp_net_main.c (revision f79e4d5f92a129a1159c973735007d4ddc8541f3)
1 /*
2  * Copyright (C) 2015-2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 /*
35  * nfp_net_main.c
36  * Netronome network device driver: Main entry point
37  * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
38  *          Alejandro Lucero <alejandro.lucero@netronome.com>
39  *          Jason McMullan <jason.mcmullan@netronome.com>
40  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
41  */
42 
43 #include <linux/etherdevice.h>
44 #include <linux/kernel.h>
45 #include <linux/init.h>
46 #include <linux/lockdep.h>
47 #include <linux/pci.h>
48 #include <linux/pci_regs.h>
49 #include <linux/msi.h>
50 #include <linux/random.h>
51 #include <linux/rtnetlink.h>
52 
53 #include "nfpcore/nfp.h"
54 #include "nfpcore/nfp_cpp.h"
55 #include "nfpcore/nfp_nffw.h"
56 #include "nfpcore/nfp_nsp.h"
57 #include "nfpcore/nfp6000_pcie.h"
58 #include "nfp_app.h"
59 #include "nfp_net_ctrl.h"
60 #include "nfp_net_sriov.h"
61 #include "nfp_net.h"
62 #include "nfp_main.h"
63 #include "nfp_port.h"
64 
65 #define NFP_PF_CSR_SLICE_SIZE	(32 * 1024)
66 
67 /**
68  * nfp_net_get_mac_addr() - Get the MAC address.
69  * @pf:       NFP PF handle
70  * @netdev:   net_device to set MAC address on
71  * @port:     NFP port structure
72  *
73  * First try to get the MAC address from NSP ETH table. If that
74  * fails generate a random address.
75  */
76 void
77 nfp_net_get_mac_addr(struct nfp_pf *pf, struct net_device *netdev,
78 		     struct nfp_port *port)
79 {
80 	struct nfp_eth_table_port *eth_port;
81 
82 	eth_port = __nfp_port_get_eth_port(port);
83 	if (!eth_port) {
84 		eth_hw_addr_random(netdev);
85 		return;
86 	}
87 
88 	ether_addr_copy(netdev->dev_addr, eth_port->mac_addr);
89 	ether_addr_copy(netdev->perm_addr, eth_port->mac_addr);
90 }
91 
92 static struct nfp_eth_table_port *
93 nfp_net_find_port(struct nfp_eth_table *eth_tbl, unsigned int index)
94 {
95 	int i;
96 
97 	for (i = 0; eth_tbl && i < eth_tbl->count; i++)
98 		if (eth_tbl->ports[i].index == index)
99 			return &eth_tbl->ports[i];
100 
101 	return NULL;
102 }
103 
104 static int nfp_net_pf_get_num_ports(struct nfp_pf *pf)
105 {
106 	return nfp_pf_rtsym_read_optional(pf, "nfd_cfg_pf%u_num_ports", 1);
107 }
108 
109 static int nfp_net_pf_get_app_id(struct nfp_pf *pf)
110 {
111 	return nfp_pf_rtsym_read_optional(pf, "_pf%u_net_app_id",
112 					  NFP_APP_CORE_NIC);
113 }
114 
115 static void nfp_net_pf_free_vnic(struct nfp_pf *pf, struct nfp_net *nn)
116 {
117 	if (nfp_net_is_data_vnic(nn))
118 		nfp_app_vnic_free(pf->app, nn);
119 	nfp_port_free(nn->port);
120 	list_del(&nn->vnic_list);
121 	pf->num_vnics--;
122 	nfp_net_free(nn);
123 }
124 
125 static void nfp_net_pf_free_vnics(struct nfp_pf *pf)
126 {
127 	struct nfp_net *nn, *next;
128 
129 	list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list)
130 		if (nfp_net_is_data_vnic(nn))
131 			nfp_net_pf_free_vnic(pf, nn);
132 }
133 
134 static struct nfp_net *
135 nfp_net_pf_alloc_vnic(struct nfp_pf *pf, bool needs_netdev,
136 		      void __iomem *ctrl_bar, void __iomem *qc_bar,
137 		      int stride, unsigned int id)
138 {
139 	u32 tx_base, rx_base, n_tx_rings, n_rx_rings;
140 	struct nfp_net *nn;
141 	int err;
142 
143 	tx_base = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
144 	rx_base = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
145 	n_tx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_TXRINGS);
146 	n_rx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_RXRINGS);
147 
148 	/* Allocate and initialise the vNIC */
149 	nn = nfp_net_alloc(pf->pdev, needs_netdev, n_tx_rings, n_rx_rings);
150 	if (IS_ERR(nn))
151 		return nn;
152 
153 	nn->app = pf->app;
154 	nfp_net_get_fw_version(&nn->fw_ver, ctrl_bar);
155 	nn->dp.ctrl_bar = ctrl_bar;
156 	nn->tx_bar = qc_bar + tx_base * NFP_QCP_QUEUE_ADDR_SZ;
157 	nn->rx_bar = qc_bar + rx_base * NFP_QCP_QUEUE_ADDR_SZ;
158 	nn->dp.is_vf = 0;
159 	nn->stride_rx = stride;
160 	nn->stride_tx = stride;
161 
162 	if (needs_netdev) {
163 		err = nfp_app_vnic_alloc(pf->app, nn, id);
164 		if (err) {
165 			nfp_net_free(nn);
166 			return ERR_PTR(err);
167 		}
168 	}
169 
170 	pf->num_vnics++;
171 	list_add_tail(&nn->vnic_list, &pf->vnics);
172 
173 	return nn;
174 }
175 
176 static int
177 nfp_net_pf_init_vnic(struct nfp_pf *pf, struct nfp_net *nn, unsigned int id)
178 {
179 	int err;
180 
181 	nn->id = id;
182 
183 	err = nfp_net_init(nn);
184 	if (err)
185 		return err;
186 
187 	nfp_net_debugfs_vnic_add(nn, pf->ddir);
188 
189 	if (nn->port) {
190 		err = nfp_devlink_port_register(pf->app, nn->port);
191 		if (err)
192 			goto err_dfs_clean;
193 	}
194 
195 	nfp_net_info(nn);
196 
197 	if (nfp_net_is_data_vnic(nn)) {
198 		err = nfp_app_vnic_init(pf->app, nn);
199 		if (err)
200 			goto err_devlink_port_clean;
201 	}
202 
203 	return 0;
204 
205 err_devlink_port_clean:
206 	if (nn->port)
207 		nfp_devlink_port_unregister(nn->port);
208 err_dfs_clean:
209 	nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
210 	nfp_net_clean(nn);
211 	return err;
212 }
213 
214 static int
215 nfp_net_pf_alloc_vnics(struct nfp_pf *pf, void __iomem *ctrl_bar,
216 		       void __iomem *qc_bar, int stride)
217 {
218 	struct nfp_net *nn;
219 	unsigned int i;
220 	int err;
221 
222 	for (i = 0; i < pf->max_data_vnics; i++) {
223 		nn = nfp_net_pf_alloc_vnic(pf, true, ctrl_bar, qc_bar,
224 					   stride, i);
225 		if (IS_ERR(nn)) {
226 			err = PTR_ERR(nn);
227 			goto err_free_prev;
228 		}
229 
230 		ctrl_bar += NFP_PF_CSR_SLICE_SIZE;
231 
232 		/* Kill the vNIC if app init marked it as invalid */
233 		if (nn->port && nn->port->type == NFP_PORT_INVALID) {
234 			nfp_net_pf_free_vnic(pf, nn);
235 			continue;
236 		}
237 	}
238 
239 	if (list_empty(&pf->vnics))
240 		return -ENODEV;
241 
242 	return 0;
243 
244 err_free_prev:
245 	nfp_net_pf_free_vnics(pf);
246 	return err;
247 }
248 
249 static void nfp_net_pf_clean_vnic(struct nfp_pf *pf, struct nfp_net *nn)
250 {
251 	if (nfp_net_is_data_vnic(nn))
252 		nfp_app_vnic_clean(pf->app, nn);
253 	if (nn->port)
254 		nfp_devlink_port_unregister(nn->port);
255 	nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
256 	nfp_net_clean(nn);
257 }
258 
259 static int nfp_net_pf_alloc_irqs(struct nfp_pf *pf)
260 {
261 	unsigned int wanted_irqs, num_irqs, vnics_left, irqs_left;
262 	struct nfp_net *nn;
263 
264 	/* Get MSI-X vectors */
265 	wanted_irqs = 0;
266 	list_for_each_entry(nn, &pf->vnics, vnic_list)
267 		wanted_irqs += NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs;
268 	pf->irq_entries = kcalloc(wanted_irqs, sizeof(*pf->irq_entries),
269 				  GFP_KERNEL);
270 	if (!pf->irq_entries)
271 		return -ENOMEM;
272 
273 	num_irqs = nfp_net_irqs_alloc(pf->pdev, pf->irq_entries,
274 				      NFP_NET_MIN_VNIC_IRQS * pf->num_vnics,
275 				      wanted_irqs);
276 	if (!num_irqs) {
277 		nfp_warn(pf->cpp, "Unable to allocate MSI-X vectors\n");
278 		kfree(pf->irq_entries);
279 		return -ENOMEM;
280 	}
281 
282 	/* Distribute IRQs to vNICs */
283 	irqs_left = num_irqs;
284 	vnics_left = pf->num_vnics;
285 	list_for_each_entry(nn, &pf->vnics, vnic_list) {
286 		unsigned int n;
287 
288 		n = min(NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs,
289 			DIV_ROUND_UP(irqs_left, vnics_left));
290 		nfp_net_irqs_assign(nn, &pf->irq_entries[num_irqs - irqs_left],
291 				    n);
292 		irqs_left -= n;
293 		vnics_left--;
294 	}
295 
296 	return 0;
297 }
298 
299 static void nfp_net_pf_free_irqs(struct nfp_pf *pf)
300 {
301 	nfp_net_irqs_disable(pf->pdev);
302 	kfree(pf->irq_entries);
303 }
304 
305 static int nfp_net_pf_init_vnics(struct nfp_pf *pf)
306 {
307 	struct nfp_net *nn;
308 	unsigned int id;
309 	int err;
310 
311 	/* Finish vNIC init and register */
312 	id = 0;
313 	list_for_each_entry(nn, &pf->vnics, vnic_list) {
314 		if (!nfp_net_is_data_vnic(nn))
315 			continue;
316 		err = nfp_net_pf_init_vnic(pf, nn, id);
317 		if (err)
318 			goto err_prev_deinit;
319 
320 		id++;
321 	}
322 
323 	return 0;
324 
325 err_prev_deinit:
326 	list_for_each_entry_continue_reverse(nn, &pf->vnics, vnic_list)
327 		if (nfp_net_is_data_vnic(nn))
328 			nfp_net_pf_clean_vnic(pf, nn);
329 	return err;
330 }
331 
332 static int
333 nfp_net_pf_app_init(struct nfp_pf *pf, u8 __iomem *qc_bar, unsigned int stride)
334 {
335 	u8 __iomem *ctrl_bar;
336 	int err;
337 
338 	pf->app = nfp_app_alloc(pf, nfp_net_pf_get_app_id(pf));
339 	if (IS_ERR(pf->app))
340 		return PTR_ERR(pf->app);
341 
342 	mutex_lock(&pf->lock);
343 	err = nfp_app_init(pf->app);
344 	mutex_unlock(&pf->lock);
345 	if (err)
346 		goto err_free;
347 
348 	if (!nfp_app_needs_ctrl_vnic(pf->app))
349 		return 0;
350 
351 	ctrl_bar = nfp_pf_map_rtsym(pf, "net.ctrl", "_pf%u_net_ctrl_bar",
352 				    NFP_PF_CSR_SLICE_SIZE, &pf->ctrl_vnic_bar);
353 	if (IS_ERR(ctrl_bar)) {
354 		nfp_err(pf->cpp, "Failed to find ctrl vNIC memory symbol\n");
355 		err = PTR_ERR(ctrl_bar);
356 		goto err_app_clean;
357 	}
358 
359 	pf->ctrl_vnic =	nfp_net_pf_alloc_vnic(pf, false, ctrl_bar, qc_bar,
360 					      stride, 0);
361 	if (IS_ERR(pf->ctrl_vnic)) {
362 		err = PTR_ERR(pf->ctrl_vnic);
363 		goto err_unmap;
364 	}
365 
366 	return 0;
367 
368 err_unmap:
369 	nfp_cpp_area_release_free(pf->ctrl_vnic_bar);
370 err_app_clean:
371 	mutex_lock(&pf->lock);
372 	nfp_app_clean(pf->app);
373 	mutex_unlock(&pf->lock);
374 err_free:
375 	nfp_app_free(pf->app);
376 	pf->app = NULL;
377 	return err;
378 }
379 
380 static void nfp_net_pf_app_clean(struct nfp_pf *pf)
381 {
382 	if (pf->ctrl_vnic) {
383 		nfp_net_pf_free_vnic(pf, pf->ctrl_vnic);
384 		nfp_cpp_area_release_free(pf->ctrl_vnic_bar);
385 	}
386 
387 	mutex_lock(&pf->lock);
388 	nfp_app_clean(pf->app);
389 	mutex_unlock(&pf->lock);
390 
391 	nfp_app_free(pf->app);
392 	pf->app = NULL;
393 }
394 
395 static int nfp_net_pf_app_start_ctrl(struct nfp_pf *pf)
396 {
397 	int err;
398 
399 	if (!pf->ctrl_vnic)
400 		return 0;
401 	err = nfp_net_pf_init_vnic(pf, pf->ctrl_vnic, 0);
402 	if (err)
403 		return err;
404 
405 	err = nfp_ctrl_open(pf->ctrl_vnic);
406 	if (err)
407 		goto err_clean_ctrl;
408 
409 	return 0;
410 
411 err_clean_ctrl:
412 	nfp_net_pf_clean_vnic(pf, pf->ctrl_vnic);
413 	return err;
414 }
415 
416 static void nfp_net_pf_app_stop_ctrl(struct nfp_pf *pf)
417 {
418 	if (!pf->ctrl_vnic)
419 		return;
420 	nfp_ctrl_close(pf->ctrl_vnic);
421 	nfp_net_pf_clean_vnic(pf, pf->ctrl_vnic);
422 }
423 
424 static int nfp_net_pf_app_start(struct nfp_pf *pf)
425 {
426 	int err;
427 
428 	err = nfp_net_pf_app_start_ctrl(pf);
429 	if (err)
430 		return err;
431 
432 	err = nfp_app_start(pf->app, pf->ctrl_vnic);
433 	if (err)
434 		goto err_ctrl_stop;
435 
436 	if (pf->num_vfs) {
437 		err = nfp_app_sriov_enable(pf->app, pf->num_vfs);
438 		if (err)
439 			goto err_app_stop;
440 	}
441 
442 	return 0;
443 
444 err_app_stop:
445 	nfp_app_stop(pf->app);
446 err_ctrl_stop:
447 	nfp_net_pf_app_stop_ctrl(pf);
448 	return err;
449 }
450 
451 static void nfp_net_pf_app_stop(struct nfp_pf *pf)
452 {
453 	if (pf->num_vfs)
454 		nfp_app_sriov_disable(pf->app);
455 	nfp_app_stop(pf->app);
456 	nfp_net_pf_app_stop_ctrl(pf);
457 }
458 
459 static void nfp_net_pci_unmap_mem(struct nfp_pf *pf)
460 {
461 	if (pf->vfcfg_tbl2_area)
462 		nfp_cpp_area_release_free(pf->vfcfg_tbl2_area);
463 	if (pf->vf_cfg_bar)
464 		nfp_cpp_area_release_free(pf->vf_cfg_bar);
465 	if (pf->mac_stats_bar)
466 		nfp_cpp_area_release_free(pf->mac_stats_bar);
467 	nfp_cpp_area_release_free(pf->qc_area);
468 	nfp_cpp_area_release_free(pf->data_vnic_bar);
469 }
470 
471 static int nfp_net_pci_map_mem(struct nfp_pf *pf)
472 {
473 	u8 __iomem *mem;
474 	u32 min_size;
475 	int err;
476 
477 	min_size = pf->max_data_vnics * NFP_PF_CSR_SLICE_SIZE;
478 	mem = nfp_pf_map_rtsym(pf, "net.bar0", "_pf%d_net_bar0",
479 			       min_size, &pf->data_vnic_bar);
480 	if (IS_ERR(mem)) {
481 		nfp_err(pf->cpp, "Failed to find data vNIC memory symbol\n");
482 		return PTR_ERR(mem);
483 	}
484 
485 	if (pf->eth_tbl) {
486 		min_size =  NFP_MAC_STATS_SIZE * (pf->eth_tbl->max_index + 1);
487 		pf->mac_stats_mem = nfp_rtsym_map(pf->rtbl, "_mac_stats",
488 						  "net.macstats", min_size,
489 						  &pf->mac_stats_bar);
490 		if (IS_ERR(pf->mac_stats_mem)) {
491 			if (PTR_ERR(pf->mac_stats_mem) != -ENOENT) {
492 				err = PTR_ERR(pf->mac_stats_mem);
493 				goto err_unmap_ctrl;
494 			}
495 			pf->mac_stats_mem = NULL;
496 		}
497 	}
498 
499 	pf->vf_cfg_mem = nfp_pf_map_rtsym(pf, "net.vfcfg", "_pf%d_net_vf_bar",
500 					  NFP_NET_CFG_BAR_SZ * pf->limit_vfs,
501 					  &pf->vf_cfg_bar);
502 	if (IS_ERR(pf->vf_cfg_mem)) {
503 		if (PTR_ERR(pf->vf_cfg_mem) != -ENOENT) {
504 			err = PTR_ERR(pf->vf_cfg_mem);
505 			goto err_unmap_mac_stats;
506 		}
507 		pf->vf_cfg_mem = NULL;
508 	}
509 
510 	min_size = NFP_NET_VF_CFG_SZ * pf->limit_vfs + NFP_NET_VF_CFG_MB_SZ;
511 	pf->vfcfg_tbl2 = nfp_pf_map_rtsym(pf, "net.vfcfg_tbl2",
512 					  "_pf%d_net_vf_cfg2",
513 					  min_size, &pf->vfcfg_tbl2_area);
514 	if (IS_ERR(pf->vfcfg_tbl2)) {
515 		if (PTR_ERR(pf->vfcfg_tbl2) != -ENOENT) {
516 			err = PTR_ERR(pf->vfcfg_tbl2);
517 			goto err_unmap_vf_cfg;
518 		}
519 		pf->vfcfg_tbl2 = NULL;
520 	}
521 
522 	mem = nfp_cpp_map_area(pf->cpp, "net.qc", 0, 0,
523 			       NFP_PCIE_QUEUE(0), NFP_QCP_QUEUE_AREA_SZ,
524 			       &pf->qc_area);
525 	if (IS_ERR(mem)) {
526 		nfp_err(pf->cpp, "Failed to map Queue Controller area.\n");
527 		err = PTR_ERR(mem);
528 		goto err_unmap_vfcfg_tbl2;
529 	}
530 
531 	return 0;
532 
533 err_unmap_vfcfg_tbl2:
534 	if (pf->vfcfg_tbl2_area)
535 		nfp_cpp_area_release_free(pf->vfcfg_tbl2_area);
536 err_unmap_vf_cfg:
537 	if (pf->vf_cfg_bar)
538 		nfp_cpp_area_release_free(pf->vf_cfg_bar);
539 err_unmap_mac_stats:
540 	if (pf->mac_stats_bar)
541 		nfp_cpp_area_release_free(pf->mac_stats_bar);
542 err_unmap_ctrl:
543 	nfp_cpp_area_release_free(pf->data_vnic_bar);
544 	return err;
545 }
546 
547 static int
548 nfp_net_eth_port_update(struct nfp_cpp *cpp, struct nfp_port *port,
549 			struct nfp_eth_table *eth_table)
550 {
551 	struct nfp_eth_table_port *eth_port;
552 
553 	ASSERT_RTNL();
554 
555 	eth_port = nfp_net_find_port(eth_table, port->eth_id);
556 	if (!eth_port) {
557 		set_bit(NFP_PORT_CHANGED, &port->flags);
558 		nfp_warn(cpp, "Warning: port #%d not present after reconfig\n",
559 			 port->eth_id);
560 		return -EIO;
561 	}
562 	if (eth_port->override_changed) {
563 		nfp_warn(cpp, "Port #%d config changed, unregistering. Driver reload required before port will be operational again.\n", port->eth_id);
564 		port->type = NFP_PORT_INVALID;
565 	}
566 
567 	memcpy(port->eth_port, eth_port, sizeof(*eth_port));
568 
569 	return 0;
570 }
571 
572 int nfp_net_refresh_port_table_sync(struct nfp_pf *pf)
573 {
574 	struct nfp_eth_table *eth_table;
575 	struct nfp_net *nn, *next;
576 	struct nfp_port *port;
577 	int err;
578 
579 	lockdep_assert_held(&pf->lock);
580 
581 	/* Check for nfp_net_pci_remove() racing against us */
582 	if (list_empty(&pf->vnics))
583 		return 0;
584 
585 	/* Update state of all ports */
586 	rtnl_lock();
587 	list_for_each_entry(port, &pf->ports, port_list)
588 		clear_bit(NFP_PORT_CHANGED, &port->flags);
589 
590 	eth_table = nfp_eth_read_ports(pf->cpp);
591 	if (!eth_table) {
592 		list_for_each_entry(port, &pf->ports, port_list)
593 			if (__nfp_port_get_eth_port(port))
594 				set_bit(NFP_PORT_CHANGED, &port->flags);
595 		rtnl_unlock();
596 		nfp_err(pf->cpp, "Error refreshing port config!\n");
597 		return -EIO;
598 	}
599 
600 	list_for_each_entry(port, &pf->ports, port_list)
601 		if (__nfp_port_get_eth_port(port))
602 			nfp_net_eth_port_update(pf->cpp, port, eth_table);
603 	rtnl_unlock();
604 
605 	kfree(eth_table);
606 
607 	/* Resync repr state. This may cause reprs to be removed. */
608 	err = nfp_reprs_resync_phys_ports(pf->app);
609 	if (err)
610 		return err;
611 
612 	/* Shoot off the ports which became invalid */
613 	list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list) {
614 		if (!nn->port || nn->port->type != NFP_PORT_INVALID)
615 			continue;
616 
617 		nfp_net_pf_clean_vnic(pf, nn);
618 		nfp_net_pf_free_vnic(pf, nn);
619 	}
620 
621 	return 0;
622 }
623 
624 static void nfp_net_refresh_vnics(struct work_struct *work)
625 {
626 	struct nfp_pf *pf = container_of(work, struct nfp_pf,
627 					 port_refresh_work);
628 
629 	mutex_lock(&pf->lock);
630 	nfp_net_refresh_port_table_sync(pf);
631 	mutex_unlock(&pf->lock);
632 }
633 
634 void nfp_net_refresh_port_table(struct nfp_port *port)
635 {
636 	struct nfp_pf *pf = port->app->pf;
637 
638 	set_bit(NFP_PORT_CHANGED, &port->flags);
639 
640 	queue_work(pf->wq, &pf->port_refresh_work);
641 }
642 
643 int nfp_net_refresh_eth_port(struct nfp_port *port)
644 {
645 	struct nfp_cpp *cpp = port->app->cpp;
646 	struct nfp_eth_table *eth_table;
647 	int ret;
648 
649 	clear_bit(NFP_PORT_CHANGED, &port->flags);
650 
651 	eth_table = nfp_eth_read_ports(cpp);
652 	if (!eth_table) {
653 		set_bit(NFP_PORT_CHANGED, &port->flags);
654 		nfp_err(cpp, "Error refreshing port state table!\n");
655 		return -EIO;
656 	}
657 
658 	ret = nfp_net_eth_port_update(cpp, port, eth_table);
659 
660 	kfree(eth_table);
661 
662 	return ret;
663 }
664 
665 /*
666  * PCI device functions
667  */
668 int nfp_net_pci_probe(struct nfp_pf *pf)
669 {
670 	struct devlink *devlink = priv_to_devlink(pf);
671 	struct nfp_net_fw_version fw_ver;
672 	u8 __iomem *ctrl_bar, *qc_bar;
673 	int stride;
674 	int err;
675 
676 	INIT_WORK(&pf->port_refresh_work, nfp_net_refresh_vnics);
677 
678 	if (!pf->rtbl) {
679 		nfp_err(pf->cpp, "No %s, giving up.\n",
680 			pf->fw_loaded ? "symbol table" : "firmware found");
681 		return -EINVAL;
682 	}
683 
684 	pf->max_data_vnics = nfp_net_pf_get_num_ports(pf);
685 	if ((int)pf->max_data_vnics < 0)
686 		return pf->max_data_vnics;
687 
688 	err = nfp_net_pci_map_mem(pf);
689 	if (err)
690 		return err;
691 
692 	ctrl_bar = nfp_cpp_area_iomem(pf->data_vnic_bar);
693 	qc_bar = nfp_cpp_area_iomem(pf->qc_area);
694 	if (!ctrl_bar || !qc_bar) {
695 		err = -EIO;
696 		goto err_unmap;
697 	}
698 
699 	nfp_net_get_fw_version(&fw_ver, ctrl_bar);
700 	if (fw_ver.resv || fw_ver.class != NFP_NET_CFG_VERSION_CLASS_GENERIC) {
701 		nfp_err(pf->cpp, "Unknown Firmware ABI %d.%d.%d.%d\n",
702 			fw_ver.resv, fw_ver.class, fw_ver.major, fw_ver.minor);
703 		err = -EINVAL;
704 		goto err_unmap;
705 	}
706 
707 	/* Determine stride */
708 	if (nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0, 1)) {
709 		stride = 2;
710 		nfp_warn(pf->cpp, "OBSOLETE Firmware detected - VF isolation not available\n");
711 	} else {
712 		switch (fw_ver.major) {
713 		case 1 ... 5:
714 			stride = 4;
715 			break;
716 		default:
717 			nfp_err(pf->cpp, "Unsupported Firmware ABI %d.%d.%d.%d\n",
718 				fw_ver.resv, fw_ver.class,
719 				fw_ver.major, fw_ver.minor);
720 			err = -EINVAL;
721 			goto err_unmap;
722 		}
723 	}
724 
725 	err = nfp_net_pf_app_init(pf, qc_bar, stride);
726 	if (err)
727 		goto err_unmap;
728 
729 	err = devlink_register(devlink, &pf->pdev->dev);
730 	if (err)
731 		goto err_app_clean;
732 
733 	err = nfp_shared_buf_register(pf);
734 	if (err)
735 		goto err_devlink_unreg;
736 
737 	mutex_lock(&pf->lock);
738 	pf->ddir = nfp_net_debugfs_device_add(pf->pdev);
739 
740 	/* Allocate the vnics and do basic init */
741 	err = nfp_net_pf_alloc_vnics(pf, ctrl_bar, qc_bar, stride);
742 	if (err)
743 		goto err_clean_ddir;
744 
745 	err = nfp_net_pf_alloc_irqs(pf);
746 	if (err)
747 		goto err_free_vnics;
748 
749 	err = nfp_net_pf_app_start(pf);
750 	if (err)
751 		goto err_free_irqs;
752 
753 	err = nfp_net_pf_init_vnics(pf);
754 	if (err)
755 		goto err_stop_app;
756 
757 	mutex_unlock(&pf->lock);
758 
759 	return 0;
760 
761 err_stop_app:
762 	nfp_net_pf_app_stop(pf);
763 err_free_irqs:
764 	nfp_net_pf_free_irqs(pf);
765 err_free_vnics:
766 	nfp_net_pf_free_vnics(pf);
767 err_clean_ddir:
768 	nfp_net_debugfs_dir_clean(&pf->ddir);
769 	mutex_unlock(&pf->lock);
770 	nfp_shared_buf_unregister(pf);
771 err_devlink_unreg:
772 	cancel_work_sync(&pf->port_refresh_work);
773 	devlink_unregister(devlink);
774 err_app_clean:
775 	nfp_net_pf_app_clean(pf);
776 err_unmap:
777 	nfp_net_pci_unmap_mem(pf);
778 	return err;
779 }
780 
781 void nfp_net_pci_remove(struct nfp_pf *pf)
782 {
783 	struct nfp_net *nn, *next;
784 
785 	mutex_lock(&pf->lock);
786 	list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list) {
787 		if (!nfp_net_is_data_vnic(nn))
788 			continue;
789 		nfp_net_pf_clean_vnic(pf, nn);
790 		nfp_net_pf_free_vnic(pf, nn);
791 	}
792 
793 	nfp_net_pf_app_stop(pf);
794 	/* stop app first, to avoid double free of ctrl vNIC's ddir */
795 	nfp_net_debugfs_dir_clean(&pf->ddir);
796 
797 	mutex_unlock(&pf->lock);
798 
799 	nfp_shared_buf_unregister(pf);
800 	devlink_unregister(priv_to_devlink(pf));
801 
802 	nfp_net_pf_free_irqs(pf);
803 	nfp_net_pf_app_clean(pf);
804 	nfp_net_pci_unmap_mem(pf);
805 
806 	cancel_work_sync(&pf->port_refresh_work);
807 }
808