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