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