xref: /linux/drivers/net/ethernet/netronome/nfp/nfp_main.c (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
3 
4 /*
5  * nfp_main.c
6  * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
7  *          Alejandro Lucero <alejandro.lucero@netronome.com>
8  *          Jason McMullan <jason.mcmullan@netronome.com>
9  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/pci.h>
16 #include <linux/firmware.h>
17 #include <linux/vmalloc.h>
18 #include <net/devlink.h>
19 
20 #include "nfpcore/nfp.h"
21 #include "nfpcore/nfp_cpp.h"
22 #include "nfpcore/nfp_dev.h"
23 #include "nfpcore/nfp_nffw.h"
24 #include "nfpcore/nfp_nsp.h"
25 
26 #include "nfpcore/nfp6000_pcie.h"
27 
28 #include "nfp_abi.h"
29 #include "nfp_app.h"
30 #include "nfp_main.h"
31 #include "nfp_net.h"
32 
33 static const char nfp_driver_name[] = "nfp";
34 
35 static const struct pci_device_id nfp_pci_device_ids[] = {
36 	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NFP3800,
37 	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
38 	  PCI_ANY_ID, 0, NFP_DEV_NFP3800,
39 	},
40 	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NFP4000,
41 	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
42 	  PCI_ANY_ID, 0, NFP_DEV_NFP6000,
43 	},
44 	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NFP5000,
45 	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
46 	  PCI_ANY_ID, 0, NFP_DEV_NFP6000,
47 	},
48 	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NFP6000,
49 	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
50 	  PCI_ANY_ID, 0, NFP_DEV_NFP6000,
51 	},
52 	{ PCI_VENDOR_ID_CORIGINE, PCI_DEVICE_ID_NFP3800,
53 	  PCI_VENDOR_ID_CORIGINE, PCI_ANY_ID,
54 	  PCI_ANY_ID, 0, NFP_DEV_NFP3800,
55 	},
56 	{ PCI_VENDOR_ID_CORIGINE, PCI_DEVICE_ID_NFP4000,
57 	  PCI_VENDOR_ID_CORIGINE, PCI_ANY_ID,
58 	  PCI_ANY_ID, 0, NFP_DEV_NFP6000,
59 	},
60 	{ PCI_VENDOR_ID_CORIGINE, PCI_DEVICE_ID_NFP5000,
61 	  PCI_VENDOR_ID_CORIGINE, PCI_ANY_ID,
62 	  PCI_ANY_ID, 0, NFP_DEV_NFP6000,
63 	},
64 	{ PCI_VENDOR_ID_CORIGINE, PCI_DEVICE_ID_NFP6000,
65 	  PCI_VENDOR_ID_CORIGINE, PCI_ANY_ID,
66 	  PCI_ANY_ID, 0, NFP_DEV_NFP6000,
67 	},
68 	{ 0, } /* Required last entry. */
69 };
70 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
71 
72 int nfp_pf_rtsym_read_optional(struct nfp_pf *pf, const char *format,
73 			       unsigned int default_val)
74 {
75 	char name[256];
76 	int err = 0;
77 	u64 val;
78 
79 	snprintf(name, sizeof(name), format, nfp_cppcore_pcie_unit(pf->cpp));
80 
81 	val = nfp_rtsym_read_le(pf->rtbl, name, &err);
82 	if (err) {
83 		if (err == -ENOENT)
84 			return default_val;
85 		nfp_err(pf->cpp, "Unable to read symbol %s\n", name);
86 		return err;
87 	}
88 
89 	return val;
90 }
91 
92 u8 __iomem *
93 nfp_pf_map_rtsym(struct nfp_pf *pf, const char *name, const char *sym_fmt,
94 		 unsigned int min_size, struct nfp_cpp_area **area)
95 {
96 	char pf_symbol[256];
97 
98 	snprintf(pf_symbol, sizeof(pf_symbol), sym_fmt,
99 		 nfp_cppcore_pcie_unit(pf->cpp));
100 
101 	return nfp_rtsym_map(pf->rtbl, pf_symbol, name, min_size, area);
102 }
103 
104 /* Callers should hold the devlink instance lock */
105 int nfp_mbox_cmd(struct nfp_pf *pf, u32 cmd, void *in_data, u64 in_length,
106 		 void *out_data, u64 out_length)
107 {
108 	unsigned long err_at;
109 	u64 max_data_sz;
110 	u32 val = 0;
111 	int n, err;
112 
113 	if (!pf->mbox)
114 		return -EOPNOTSUPP;
115 
116 	max_data_sz = nfp_rtsym_size(pf->mbox) - NFP_MBOX_SYM_MIN_SIZE;
117 
118 	/* Check if cmd field is clear */
119 	err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
120 	if (err || val) {
121 		nfp_warn(pf->cpp, "failed to issue command (%u): %u, err: %d\n",
122 			 cmd, val, err);
123 		return err ?: -EBUSY;
124 	}
125 
126 	in_length = min(in_length, max_data_sz);
127 	n = nfp_rtsym_write(pf->cpp, pf->mbox, NFP_MBOX_DATA, in_data,
128 			    in_length);
129 	if (n != in_length)
130 		return -EIO;
131 	/* Write data_len and wipe reserved */
132 	err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, in_length);
133 	if (err)
134 		return err;
135 
136 	/* Read back for ordering */
137 	err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
138 	if (err)
139 		return err;
140 
141 	/* Write cmd and wipe return value */
142 	err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_CMD, cmd);
143 	if (err)
144 		return err;
145 
146 	err_at = jiffies + 5 * HZ;
147 	while (true) {
148 		/* Wait for command to go to 0 (NFP_MBOX_NO_CMD) */
149 		err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
150 		if (err)
151 			return err;
152 		if (!val)
153 			break;
154 
155 		if (time_is_before_eq_jiffies(err_at))
156 			return -ETIMEDOUT;
157 
158 		msleep(5);
159 	}
160 
161 	/* Copy output if any (could be error info, do it before reading ret) */
162 	err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
163 	if (err)
164 		return err;
165 
166 	out_length = min_t(u32, val, min(out_length, max_data_sz));
167 	n = nfp_rtsym_read(pf->cpp, pf->mbox, NFP_MBOX_DATA,
168 			   out_data, out_length);
169 	if (n != out_length)
170 		return -EIO;
171 
172 	/* Check if there is an error */
173 	err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_RET, &val);
174 	if (err)
175 		return err;
176 	if (val)
177 		return -val;
178 
179 	return out_length;
180 }
181 
182 static bool nfp_board_ready(struct nfp_pf *pf)
183 {
184 	const char *cp;
185 	long state;
186 	int err;
187 
188 	cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
189 	if (!cp)
190 		return false;
191 
192 	err = kstrtol(cp, 0, &state);
193 	if (err < 0)
194 		return false;
195 
196 	return state == 15;
197 }
198 
199 static int nfp_pf_board_state_wait(struct nfp_pf *pf)
200 {
201 	const unsigned long wait_until = jiffies + 10 * HZ;
202 
203 	while (!nfp_board_ready(pf)) {
204 		if (time_is_before_eq_jiffies(wait_until)) {
205 			nfp_err(pf->cpp, "NFP board initialization timeout\n");
206 			return -EINVAL;
207 		}
208 
209 		nfp_info(pf->cpp, "waiting for board initialization\n");
210 		if (msleep_interruptible(500))
211 			return -ERESTARTSYS;
212 
213 		/* Refresh cached information */
214 		kfree(pf->hwinfo);
215 		pf->hwinfo = nfp_hwinfo_read(pf->cpp);
216 	}
217 
218 	return 0;
219 }
220 
221 static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
222 {
223 	int err;
224 
225 	pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
226 	if (err) {
227 		/* For backwards compatibility if symbol not found allow all */
228 		pf->limit_vfs = ~0;
229 		if (err == -ENOENT)
230 			return 0;
231 
232 		nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
233 		return err;
234 	}
235 
236 	err = pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
237 	if (err)
238 		nfp_warn(pf->cpp, "Failed to set VF count in sysfs: %d\n", err);
239 	return 0;
240 }
241 
242 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
243 {
244 #ifdef CONFIG_PCI_IOV
245 	struct nfp_pf *pf = pci_get_drvdata(pdev);
246 	struct devlink *devlink;
247 	int err;
248 
249 	if (num_vfs > pf->limit_vfs) {
250 		nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
251 			 pf->limit_vfs);
252 		return -EINVAL;
253 	}
254 
255 	err = pci_enable_sriov(pdev, num_vfs);
256 	if (err) {
257 		dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
258 		return err;
259 	}
260 
261 	devlink = priv_to_devlink(pf);
262 	devl_lock(devlink);
263 
264 	err = nfp_app_sriov_enable(pf->app, num_vfs);
265 	if (err) {
266 		dev_warn(&pdev->dev,
267 			 "App specific PCI SR-IOV configuration failed: %d\n",
268 			 err);
269 		goto err_sriov_disable;
270 	}
271 
272 	pf->num_vfs = num_vfs;
273 
274 	dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
275 
276 	devl_unlock(devlink);
277 	return num_vfs;
278 
279 err_sriov_disable:
280 	devl_unlock(devlink);
281 	pci_disable_sriov(pdev);
282 	return err;
283 #endif
284 	return 0;
285 }
286 
287 static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
288 {
289 #ifdef CONFIG_PCI_IOV
290 	struct nfp_pf *pf = pci_get_drvdata(pdev);
291 	struct devlink *devlink;
292 
293 	devlink = priv_to_devlink(pf);
294 	devl_lock(devlink);
295 
296 	/* If the VFs are assigned we cannot shut down SR-IOV without
297 	 * causing issues, so just leave the hardware available but
298 	 * disabled
299 	 */
300 	if (pci_vfs_assigned(pdev)) {
301 		dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
302 		devl_unlock(devlink);
303 		return -EPERM;
304 	}
305 
306 	nfp_app_sriov_disable(pf->app);
307 
308 	pf->num_vfs = 0;
309 
310 	devl_unlock(devlink);
311 
312 	pci_disable_sriov(pdev);
313 	dev_dbg(&pdev->dev, "Removed VFs.\n");
314 #endif
315 	return 0;
316 }
317 
318 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
319 {
320 	if (!pci_get_drvdata(pdev))
321 		return -ENOENT;
322 
323 	if (num_vfs == 0)
324 		return nfp_pcie_sriov_disable(pdev);
325 	else
326 		return nfp_pcie_sriov_enable(pdev, num_vfs);
327 }
328 
329 int nfp_flash_update_common(struct nfp_pf *pf, const struct firmware *fw,
330 			    struct netlink_ext_ack *extack)
331 {
332 	struct device *dev = &pf->pdev->dev;
333 	struct nfp_nsp *nsp;
334 	int err;
335 
336 	nsp = nfp_nsp_open(pf->cpp);
337 	if (IS_ERR(nsp)) {
338 		err = PTR_ERR(nsp);
339 		if (extack)
340 			NL_SET_ERR_MSG_MOD(extack, "can't access NSP");
341 		else
342 			dev_err(dev, "Failed to access the NSP: %d\n", err);
343 		return err;
344 	}
345 
346 	err = nfp_nsp_write_flash(nsp, fw);
347 	if (err < 0)
348 		goto exit_close_nsp;
349 	dev_info(dev, "Finished writing flash image\n");
350 	err = 0;
351 
352 exit_close_nsp:
353 	nfp_nsp_close(nsp);
354 	return err;
355 }
356 
357 static const struct firmware *
358 nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name)
359 {
360 	const struct firmware *fw = NULL;
361 	int err;
362 
363 	err = request_firmware_direct(&fw, name, &pdev->dev);
364 	nfp_info(pf->cpp, "  %s: %s\n",
365 		 name, err ? "not found" : "found");
366 	if (err)
367 		return NULL;
368 
369 	return fw;
370 }
371 
372 /**
373  * nfp_net_fw_find() - Find the correct firmware image for netdev mode
374  * @pdev:	PCI Device structure
375  * @pf:		NFP PF Device structure
376  *
377  * Return: firmware if found and requested successfully.
378  */
379 static const struct firmware *
380 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
381 {
382 	struct nfp_eth_table_port *port;
383 	const struct firmware *fw;
384 	const char *fw_model;
385 	char fw_name[256];
386 	const u8 *serial;
387 	u16 interface;
388 	int spc, i, j;
389 
390 	nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n");
391 
392 	/* First try to find a firmware image specific for this device */
393 	interface = nfp_cpp_interface(pf->cpp);
394 	nfp_cpp_serial(pf->cpp, &serial);
395 	sprintf(fw_name, "netronome/serial-%pMF-%02hhx-%02hhx.nffw",
396 		serial, interface >> 8, interface & 0xff);
397 	fw = nfp_net_fw_request(pdev, pf, fw_name);
398 	if (fw)
399 		return fw;
400 
401 	/* Then try the PCI name */
402 	sprintf(fw_name, "netronome/pci-%s.nffw", pci_name(pdev));
403 	fw = nfp_net_fw_request(pdev, pf, fw_name);
404 	if (fw)
405 		return fw;
406 
407 	/* Finally try the card type and media */
408 	if (!pf->eth_tbl) {
409 		dev_err(&pdev->dev, "Error: can't identify media config\n");
410 		return NULL;
411 	}
412 
413 	fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
414 	if (!fw_model) {
415 		dev_err(&pdev->dev, "Error: can't read part number\n");
416 		return NULL;
417 	}
418 
419 	spc = ARRAY_SIZE(fw_name);
420 	spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
421 
422 	for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
423 		port = &pf->eth_tbl->ports[i];
424 		j = 1;
425 		while (i + j < pf->eth_tbl->count &&
426 		       port->speed == port[j].speed)
427 			j++;
428 
429 		spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
430 				"_%dx%d", j, port->speed / 1000);
431 	}
432 
433 	if (spc <= 0)
434 		return NULL;
435 
436 	spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
437 	if (spc <= 0)
438 		return NULL;
439 
440 	return nfp_net_fw_request(pdev, pf, fw_name);
441 }
442 
443 static int
444 nfp_get_fw_policy_value(struct pci_dev *pdev, struct nfp_nsp *nsp,
445 			const char *key, const char *default_val, int max_val,
446 			int *value)
447 {
448 	char hwinfo[64];
449 	long hi_val;
450 	int err;
451 
452 	snprintf(hwinfo, sizeof(hwinfo), key);
453 	err = nfp_nsp_hwinfo_lookup_optional(nsp, hwinfo, sizeof(hwinfo),
454 					     default_val);
455 	if (err)
456 		return err;
457 
458 	err = kstrtol(hwinfo, 0, &hi_val);
459 	if (err || hi_val < 0 || hi_val > max_val) {
460 		dev_warn(&pdev->dev,
461 			 "Invalid value '%s' from '%s', ignoring\n",
462 			 hwinfo, key);
463 		err = kstrtol(default_val, 0, &hi_val);
464 	}
465 
466 	*value = hi_val;
467 	return err;
468 }
469 
470 /**
471  * nfp_fw_load() - Load the firmware image
472  * @pdev:       PCI Device structure
473  * @pf:		NFP PF Device structure
474  * @nsp:	NFP SP handle
475  *
476  * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
477  */
478 static int
479 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
480 {
481 	bool do_reset, fw_loaded = false;
482 	const struct firmware *fw = NULL;
483 	int err, reset, policy, ifcs = 0;
484 	char *token, *ptr;
485 	char hwinfo[64];
486 	u16 interface;
487 
488 	snprintf(hwinfo, sizeof(hwinfo), "abi_drv_load_ifc");
489 	err = nfp_nsp_hwinfo_lookup_optional(nsp, hwinfo, sizeof(hwinfo),
490 					     NFP_NSP_DRV_LOAD_IFC_DEFAULT);
491 	if (err)
492 		return err;
493 
494 	interface = nfp_cpp_interface(pf->cpp);
495 	ptr = hwinfo;
496 	while ((token = strsep(&ptr, ","))) {
497 		unsigned long interface_hi;
498 
499 		err = kstrtoul(token, 0, &interface_hi);
500 		if (err) {
501 			dev_err(&pdev->dev,
502 				"Failed to parse interface '%s': %d\n",
503 				token, err);
504 			return err;
505 		}
506 
507 		ifcs++;
508 		if (interface == interface_hi)
509 			break;
510 	}
511 
512 	if (!token) {
513 		dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
514 		return 0;
515 	}
516 
517 	err = nfp_get_fw_policy_value(pdev, nsp, "abi_drv_reset",
518 				      NFP_NSP_DRV_RESET_DEFAULT,
519 				      NFP_NSP_DRV_RESET_NEVER, &reset);
520 	if (err)
521 		return err;
522 
523 	err = nfp_get_fw_policy_value(pdev, nsp, "app_fw_from_flash",
524 				      NFP_NSP_APP_FW_LOAD_DEFAULT,
525 				      NFP_NSP_APP_FW_LOAD_PREF, &policy);
526 	if (err)
527 		return err;
528 
529 	fw = nfp_net_fw_find(pdev, pf);
530 	do_reset = reset == NFP_NSP_DRV_RESET_ALWAYS ||
531 		   (fw && reset == NFP_NSP_DRV_RESET_DISK);
532 
533 	if (do_reset) {
534 		dev_info(&pdev->dev, "Soft-resetting the NFP\n");
535 		err = nfp_nsp_device_soft_reset(nsp);
536 		if (err < 0) {
537 			dev_err(&pdev->dev,
538 				"Failed to soft reset the NFP: %d\n", err);
539 			goto exit_release_fw;
540 		}
541 	}
542 
543 	if (fw && policy != NFP_NSP_APP_FW_LOAD_FLASH) {
544 		if (nfp_nsp_has_fw_loaded(nsp) && nfp_nsp_fw_loaded(nsp))
545 			goto exit_release_fw;
546 
547 		err = nfp_nsp_load_fw(nsp, fw);
548 		if (err < 0) {
549 			dev_err(&pdev->dev, "FW loading failed: %d\n",
550 				err);
551 			goto exit_release_fw;
552 		}
553 		dev_info(&pdev->dev, "Finished loading FW image\n");
554 		fw_loaded = true;
555 	} else if (policy != NFP_NSP_APP_FW_LOAD_DISK &&
556 		   nfp_nsp_has_stored_fw_load(nsp)) {
557 
558 		/* Don't propagate this error to stick with legacy driver
559 		 * behavior, failure will be detected later during init.
560 		 */
561 		if (!nfp_nsp_load_stored_fw(nsp))
562 			dev_info(&pdev->dev, "Finished loading stored FW image\n");
563 
564 		/* Don't flag the fw_loaded in this case since other devices
565 		 * may reuse the firmware when configured this way
566 		 */
567 	} else {
568 		dev_warn(&pdev->dev, "Didn't load firmware, please update flash or reconfigure card\n");
569 	}
570 
571 exit_release_fw:
572 	release_firmware(fw);
573 
574 	/* We don't want to unload firmware when other devices may still be
575 	 * dependent on it, which could be the case if there are multiple
576 	 * devices that could load firmware.
577 	 */
578 	if (fw_loaded && ifcs == 1)
579 		pf->unload_fw_on_remove = true;
580 
581 	return err < 0 ? err : fw_loaded;
582 }
583 
584 static void
585 nfp_nsp_init_ports(struct pci_dev *pdev, struct nfp_pf *pf,
586 		   struct nfp_nsp *nsp)
587 {
588 	bool needs_reinit = false;
589 	int i;
590 
591 	pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
592 	if (!pf->eth_tbl)
593 		return;
594 
595 	if (!nfp_nsp_has_mac_reinit(nsp))
596 		return;
597 
598 	for (i = 0; i < pf->eth_tbl->count; i++)
599 		needs_reinit |= pf->eth_tbl->ports[i].override_changed;
600 	if (!needs_reinit)
601 		return;
602 
603 	kfree(pf->eth_tbl);
604 	if (nfp_nsp_mac_reinit(nsp))
605 		dev_warn(&pdev->dev, "MAC reinit failed\n");
606 
607 	pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
608 }
609 
610 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
611 {
612 	struct nfp_nsp *nsp;
613 	int err;
614 
615 	err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
616 	if (err)
617 		return err;
618 
619 	nsp = nfp_nsp_open(pf->cpp);
620 	if (IS_ERR(nsp)) {
621 		err = PTR_ERR(nsp);
622 		dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
623 		return err;
624 	}
625 
626 	err = nfp_nsp_wait(nsp);
627 	if (err < 0)
628 		goto exit_close_nsp;
629 
630 	nfp_nsp_init_ports(pdev, pf, nsp);
631 
632 	pf->nspi = __nfp_nsp_identify(nsp);
633 	if (pf->nspi)
634 		dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
635 
636 	err = nfp_fw_load(pdev, pf, nsp);
637 	if (err < 0) {
638 		kfree(pf->nspi);
639 		kfree(pf->eth_tbl);
640 		dev_err(&pdev->dev, "Failed to load FW\n");
641 		goto exit_close_nsp;
642 	}
643 
644 	pf->fw_loaded = !!err;
645 	err = 0;
646 
647 exit_close_nsp:
648 	nfp_nsp_close(nsp);
649 
650 	return err;
651 }
652 
653 static void nfp_fw_unload(struct nfp_pf *pf)
654 {
655 	struct nfp_nsp *nsp;
656 	int err;
657 
658 	nsp = nfp_nsp_open(pf->cpp);
659 	if (IS_ERR(nsp)) {
660 		nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
661 		return;
662 	}
663 
664 	err = nfp_nsp_device_soft_reset(nsp);
665 	if (err < 0)
666 		dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
667 	else
668 		dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
669 
670 	nfp_nsp_close(nsp);
671 }
672 
673 static int nfp_pf_find_rtsyms(struct nfp_pf *pf)
674 {
675 	char pf_symbol[256];
676 	unsigned int pf_id;
677 
678 	pf_id = nfp_cppcore_pcie_unit(pf->cpp);
679 
680 	/* Optional per-PCI PF mailbox */
681 	snprintf(pf_symbol, sizeof(pf_symbol), NFP_MBOX_SYM_NAME, pf_id);
682 	pf->mbox = nfp_rtsym_lookup(pf->rtbl, pf_symbol);
683 	if (pf->mbox && nfp_rtsym_size(pf->mbox) < NFP_MBOX_SYM_MIN_SIZE) {
684 		nfp_err(pf->cpp, "PF mailbox symbol too small: %llu < %d\n",
685 			nfp_rtsym_size(pf->mbox), NFP_MBOX_SYM_MIN_SIZE);
686 		return -EINVAL;
687 	}
688 
689 	return 0;
690 }
691 
692 static int nfp_pci_probe(struct pci_dev *pdev,
693 			 const struct pci_device_id *pci_id)
694 {
695 	const struct nfp_dev_info *dev_info;
696 	struct devlink *devlink;
697 	struct nfp_pf *pf;
698 	int err;
699 
700 	if ((pdev->vendor == PCI_VENDOR_ID_NETRONOME ||
701 	     pdev->vendor == PCI_VENDOR_ID_CORIGINE) &&
702 	    (pdev->device == PCI_DEVICE_ID_NFP3800_VF ||
703 	     pdev->device == PCI_DEVICE_ID_NFP6000_VF))
704 		dev_warn(&pdev->dev, "Binding NFP VF device to the NFP PF driver, the VF driver is called 'nfp_netvf'\n");
705 
706 	dev_info = &nfp_dev_info[pci_id->driver_data];
707 
708 	err = pci_enable_device(pdev);
709 	if (err < 0)
710 		return err;
711 
712 	pci_set_master(pdev);
713 
714 	err = dma_set_mask_and_coherent(&pdev->dev, dev_info->dma_mask);
715 	if (err)
716 		goto err_pci_disable;
717 
718 	err = pci_request_regions(pdev, nfp_driver_name);
719 	if (err < 0) {
720 		dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
721 		goto err_pci_disable;
722 	}
723 
724 	devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf), &pdev->dev);
725 	if (!devlink) {
726 		err = -ENOMEM;
727 		goto err_rel_regions;
728 	}
729 	pf = devlink_priv(devlink);
730 	INIT_LIST_HEAD(&pf->vnics);
731 	INIT_LIST_HEAD(&pf->ports);
732 	pci_set_drvdata(pdev, pf);
733 	pf->pdev = pdev;
734 	pf->dev_info = dev_info;
735 
736 	pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
737 	if (!pf->wq) {
738 		err = -ENOMEM;
739 		goto err_pci_priv_unset;
740 	}
741 
742 	pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev, dev_info);
743 	if (IS_ERR(pf->cpp)) {
744 		err = PTR_ERR(pf->cpp);
745 		goto err_disable_msix;
746 	}
747 
748 	err = nfp_resource_table_init(pf->cpp);
749 	if (err)
750 		goto err_cpp_free;
751 
752 	pf->hwinfo = nfp_hwinfo_read(pf->cpp);
753 
754 	dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
755 		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"),
756 		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"),
757 		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"),
758 		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"),
759 		 nfp_hwinfo_lookup(pf->hwinfo, "cpld.version"));
760 
761 	err = nfp_pf_board_state_wait(pf);
762 	if (err)
763 		goto err_hwinfo_free;
764 
765 	err = nfp_nsp_init(pdev, pf);
766 	if (err)
767 		goto err_hwinfo_free;
768 
769 	pf->mip = nfp_mip_open(pf->cpp);
770 	pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
771 
772 	err = nfp_pf_find_rtsyms(pf);
773 	if (err)
774 		goto err_fw_unload;
775 
776 	pf->dump_flag = NFP_DUMP_NSP_DIAG;
777 	pf->dumpspec = nfp_net_dump_load_dumpspec(pf->cpp, pf->rtbl);
778 
779 	err = nfp_pcie_sriov_read_nfd_limit(pf);
780 	if (err)
781 		goto err_fw_unload;
782 
783 	pf->num_vfs = pci_num_vf(pdev);
784 	if (pf->num_vfs > pf->limit_vfs) {
785 		dev_err(&pdev->dev,
786 			"Error: %d VFs already enabled, but loaded FW can only support %d\n",
787 			pf->num_vfs, pf->limit_vfs);
788 		err = -EINVAL;
789 		goto err_fw_unload;
790 	}
791 
792 	err = nfp_net_pci_probe(pf);
793 	if (err)
794 		goto err_fw_unload;
795 
796 	err = nfp_hwmon_register(pf);
797 	if (err) {
798 		dev_err(&pdev->dev, "Failed to register hwmon info\n");
799 		goto err_net_remove;
800 	}
801 
802 	return 0;
803 
804 err_net_remove:
805 	nfp_net_pci_remove(pf);
806 err_fw_unload:
807 	kfree(pf->rtbl);
808 	nfp_mip_close(pf->mip);
809 	if (pf->unload_fw_on_remove)
810 		nfp_fw_unload(pf);
811 	kfree(pf->eth_tbl);
812 	kfree(pf->nspi);
813 	vfree(pf->dumpspec);
814 err_hwinfo_free:
815 	kfree(pf->hwinfo);
816 err_cpp_free:
817 	nfp_cpp_free(pf->cpp);
818 err_disable_msix:
819 	destroy_workqueue(pf->wq);
820 err_pci_priv_unset:
821 	pci_set_drvdata(pdev, NULL);
822 	devlink_free(devlink);
823 err_rel_regions:
824 	pci_release_regions(pdev);
825 err_pci_disable:
826 	pci_disable_device(pdev);
827 
828 	return err;
829 }
830 
831 static void __nfp_pci_shutdown(struct pci_dev *pdev, bool unload_fw)
832 {
833 	struct nfp_pf *pf;
834 
835 	pf = pci_get_drvdata(pdev);
836 	if (!pf)
837 		return;
838 
839 	nfp_hwmon_unregister(pf);
840 
841 	nfp_pcie_sriov_disable(pdev);
842 
843 	nfp_net_pci_remove(pf);
844 
845 	vfree(pf->dumpspec);
846 	kfree(pf->rtbl);
847 	nfp_mip_close(pf->mip);
848 	if (unload_fw && pf->unload_fw_on_remove)
849 		nfp_fw_unload(pf);
850 
851 	destroy_workqueue(pf->wq);
852 	pci_set_drvdata(pdev, NULL);
853 	kfree(pf->hwinfo);
854 	nfp_cpp_free(pf->cpp);
855 
856 	kfree(pf->eth_tbl);
857 	kfree(pf->nspi);
858 	devlink_free(priv_to_devlink(pf));
859 	pci_release_regions(pdev);
860 	pci_disable_device(pdev);
861 }
862 
863 static void nfp_pci_remove(struct pci_dev *pdev)
864 {
865 	__nfp_pci_shutdown(pdev, true);
866 }
867 
868 static void nfp_pci_shutdown(struct pci_dev *pdev)
869 {
870 	__nfp_pci_shutdown(pdev, false);
871 }
872 
873 static struct pci_driver nfp_pci_driver = {
874 	.name			= nfp_driver_name,
875 	.id_table		= nfp_pci_device_ids,
876 	.probe			= nfp_pci_probe,
877 	.remove			= nfp_pci_remove,
878 	.shutdown		= nfp_pci_shutdown,
879 	.sriov_configure	= nfp_pcie_sriov_configure,
880 };
881 
882 static int __init nfp_main_init(void)
883 {
884 	int err;
885 
886 	pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2020 Netronome Systems\n",
887 		nfp_driver_name);
888 	pr_info("%s: NFP PCIe Driver, Copyright (C) 2021-2022 Corigine Inc.\n",
889 		nfp_driver_name);
890 
891 	nfp_net_debugfs_create();
892 
893 	err = pci_register_driver(&nfp_pci_driver);
894 	if (err < 0)
895 		goto err_destroy_debugfs;
896 
897 	err = pci_register_driver(&nfp_netvf_pci_driver);
898 	if (err)
899 		goto err_unreg_pf;
900 
901 	return err;
902 
903 err_unreg_pf:
904 	pci_unregister_driver(&nfp_pci_driver);
905 err_destroy_debugfs:
906 	nfp_net_debugfs_destroy();
907 	return err;
908 }
909 
910 static void __exit nfp_main_exit(void)
911 {
912 	pci_unregister_driver(&nfp_netvf_pci_driver);
913 	pci_unregister_driver(&nfp_pci_driver);
914 	nfp_net_debugfs_destroy();
915 }
916 
917 module_init(nfp_main_init);
918 module_exit(nfp_main_exit);
919 
920 MODULE_FIRMWARE("netronome/nic_AMDA0058-0011_2x40.nffw");
921 MODULE_FIRMWARE("netronome/nic_AMDA0058-0012_2x40.nffw");
922 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
923 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
924 MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
925 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
926 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
927 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
928 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
929 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
930 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_1x10_1x25.nffw");
931 
932 MODULE_AUTHOR("Corigine, Inc. <oss-drivers@corigine.com>");
933 MODULE_LICENSE("GPL");
934 MODULE_DESCRIPTION("The Network Flow Processor (NFP) driver.");
935