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