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