xref: /linux/drivers/net/ethernet/netronome/nfp/nfp_main.c (revision b9b77222d4ff6b5bb8f5d87fca20de0910618bb9)
1 /*
2  * Copyright (C) 2015-2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 /*
35  * nfp_main.c
36  * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
37  *          Alejandro Lucero <alejandro.lucero@netronome.com>
38  *          Jason McMullan <jason.mcmullan@netronome.com>
39  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
40  */
41 
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/mutex.h>
45 #include <linux/pci.h>
46 #include <linux/firmware.h>
47 #include <linux/vermagic.h>
48 #include <linux/vmalloc.h>
49 #include <net/devlink.h>
50 
51 #include "nfpcore/nfp.h"
52 #include "nfpcore/nfp_cpp.h"
53 #include "nfpcore/nfp_nffw.h"
54 #include "nfpcore/nfp_nsp.h"
55 
56 #include "nfpcore/nfp6000_pcie.h"
57 
58 #include "nfp_abi.h"
59 #include "nfp_app.h"
60 #include "nfp_main.h"
61 #include "nfp_net.h"
62 
63 static const char nfp_driver_name[] = "nfp";
64 const char nfp_driver_version[] = VERMAGIC_STRING;
65 
66 static const struct pci_device_id nfp_pci_device_ids[] = {
67 	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
68 	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
69 	  PCI_ANY_ID, 0,
70 	},
71 	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
72 	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
73 	  PCI_ANY_ID, 0,
74 	},
75 	{ 0, } /* Required last entry. */
76 };
77 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
78 
79 int nfp_pf_rtsym_read_optional(struct nfp_pf *pf, const char *format,
80 			       unsigned int default_val)
81 {
82 	char name[256];
83 	int err = 0;
84 	u64 val;
85 
86 	snprintf(name, sizeof(name), format, nfp_cppcore_pcie_unit(pf->cpp));
87 
88 	val = nfp_rtsym_read_le(pf->rtbl, name, &err);
89 	if (err) {
90 		if (err == -ENOENT)
91 			return default_val;
92 		nfp_err(pf->cpp, "Unable to read symbol %s\n", name);
93 		return err;
94 	}
95 
96 	return val;
97 }
98 
99 u8 __iomem *
100 nfp_pf_map_rtsym(struct nfp_pf *pf, const char *name, const char *sym_fmt,
101 		 unsigned int min_size, struct nfp_cpp_area **area)
102 {
103 	char pf_symbol[256];
104 
105 	snprintf(pf_symbol, sizeof(pf_symbol), sym_fmt,
106 		 nfp_cppcore_pcie_unit(pf->cpp));
107 
108 	return nfp_rtsym_map(pf->rtbl, pf_symbol, name, min_size, area);
109 }
110 
111 /* Callers should hold the devlink instance lock */
112 int nfp_mbox_cmd(struct nfp_pf *pf, u32 cmd, void *in_data, u64 in_length,
113 		 void *out_data, u64 out_length)
114 {
115 	unsigned long long addr;
116 	unsigned long err_at;
117 	u64 max_data_sz;
118 	u32 val = 0;
119 	u32 cpp_id;
120 	int n, err;
121 
122 	if (!pf->mbox)
123 		return -EOPNOTSUPP;
124 
125 	cpp_id = NFP_CPP_ISLAND_ID(pf->mbox->target, NFP_CPP_ACTION_RW, 0,
126 				   pf->mbox->domain);
127 	addr = pf->mbox->addr;
128 	max_data_sz = pf->mbox->size - NFP_MBOX_SYM_MIN_SIZE;
129 
130 	/* Check if cmd field is clear */
131 	err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_CMD, &val);
132 	if (err || val) {
133 		nfp_warn(pf->cpp, "failed to issue command (%u): %u, err: %d\n",
134 			 cmd, val, err);
135 		return err ?: -EBUSY;
136 	}
137 
138 	in_length = min(in_length, max_data_sz);
139 	n = nfp_cpp_write(pf->cpp, cpp_id, addr + NFP_MBOX_DATA,
140 			  in_data, in_length);
141 	if (n != in_length)
142 		return -EIO;
143 	/* Write data_len and wipe reserved */
144 	err = nfp_cpp_writeq(pf->cpp, cpp_id, addr + NFP_MBOX_DATA_LEN,
145 			     in_length);
146 	if (err)
147 		return err;
148 
149 	/* Read back for ordering */
150 	err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_DATA_LEN, &val);
151 	if (err)
152 		return err;
153 
154 	/* Write cmd and wipe return value */
155 	err = nfp_cpp_writeq(pf->cpp, cpp_id, addr + NFP_MBOX_CMD, cmd);
156 	if (err)
157 		return err;
158 
159 	err_at = jiffies + 5 * HZ;
160 	while (true) {
161 		/* Wait for command to go to 0 (NFP_MBOX_NO_CMD) */
162 		err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_CMD, &val);
163 		if (err)
164 			return err;
165 		if (!val)
166 			break;
167 
168 		if (time_is_before_eq_jiffies(err_at))
169 			return -ETIMEDOUT;
170 
171 		msleep(5);
172 	}
173 
174 	/* Copy output if any (could be error info, do it before reading ret) */
175 	err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_DATA_LEN, &val);
176 	if (err)
177 		return err;
178 
179 	out_length = min_t(u32, val, min(out_length, max_data_sz));
180 	n = nfp_cpp_read(pf->cpp, cpp_id, addr + NFP_MBOX_DATA,
181 			 out_data, out_length);
182 	if (n != out_length)
183 		return -EIO;
184 
185 	/* Check if there is an error */
186 	err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_RET, &val);
187 	if (err)
188 		return err;
189 	if (val)
190 		return -val;
191 
192 	return out_length;
193 }
194 
195 static bool nfp_board_ready(struct nfp_pf *pf)
196 {
197 	const char *cp;
198 	long state;
199 	int err;
200 
201 	cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
202 	if (!cp)
203 		return false;
204 
205 	err = kstrtol(cp, 0, &state);
206 	if (err < 0)
207 		return false;
208 
209 	return state == 15;
210 }
211 
212 static int nfp_pf_board_state_wait(struct nfp_pf *pf)
213 {
214 	const unsigned long wait_until = jiffies + 10 * HZ;
215 
216 	while (!nfp_board_ready(pf)) {
217 		if (time_is_before_eq_jiffies(wait_until)) {
218 			nfp_err(pf->cpp, "NFP board initialization timeout\n");
219 			return -EINVAL;
220 		}
221 
222 		nfp_info(pf->cpp, "waiting for board initialization\n");
223 		if (msleep_interruptible(500))
224 			return -ERESTARTSYS;
225 
226 		/* Refresh cached information */
227 		kfree(pf->hwinfo);
228 		pf->hwinfo = nfp_hwinfo_read(pf->cpp);
229 	}
230 
231 	return 0;
232 }
233 
234 static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
235 {
236 	int err;
237 
238 	pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
239 	if (!err)
240 		return pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
241 
242 	pf->limit_vfs = ~0;
243 	/* Allow any setting for backwards compatibility if symbol not found */
244 	if (err == -ENOENT)
245 		return 0;
246 
247 	nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
248 	return err;
249 }
250 
251 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
252 {
253 #ifdef CONFIG_PCI_IOV
254 	struct nfp_pf *pf = pci_get_drvdata(pdev);
255 	int err;
256 
257 	if (num_vfs > pf->limit_vfs) {
258 		nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
259 			 pf->limit_vfs);
260 		return -EINVAL;
261 	}
262 
263 	err = pci_enable_sriov(pdev, num_vfs);
264 	if (err) {
265 		dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
266 		return err;
267 	}
268 
269 	mutex_lock(&pf->lock);
270 
271 	err = nfp_app_sriov_enable(pf->app, num_vfs);
272 	if (err) {
273 		dev_warn(&pdev->dev,
274 			 "App specific PCI SR-IOV configuration failed: %d\n",
275 			 err);
276 		goto err_sriov_disable;
277 	}
278 
279 	pf->num_vfs = num_vfs;
280 
281 	dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
282 
283 	mutex_unlock(&pf->lock);
284 	return num_vfs;
285 
286 err_sriov_disable:
287 	mutex_unlock(&pf->lock);
288 	pci_disable_sriov(pdev);
289 	return err;
290 #endif
291 	return 0;
292 }
293 
294 static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
295 {
296 #ifdef CONFIG_PCI_IOV
297 	struct nfp_pf *pf = pci_get_drvdata(pdev);
298 
299 	mutex_lock(&pf->lock);
300 
301 	/* If the VFs are assigned we cannot shut down SR-IOV without
302 	 * causing issues, so just leave the hardware available but
303 	 * disabled
304 	 */
305 	if (pci_vfs_assigned(pdev)) {
306 		dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
307 		mutex_unlock(&pf->lock);
308 		return -EPERM;
309 	}
310 
311 	nfp_app_sriov_disable(pf->app);
312 
313 	pf->num_vfs = 0;
314 
315 	mutex_unlock(&pf->lock);
316 
317 	pci_disable_sriov(pdev);
318 	dev_dbg(&pdev->dev, "Removed VFs.\n");
319 #endif
320 	return 0;
321 }
322 
323 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
324 {
325 	if (num_vfs == 0)
326 		return nfp_pcie_sriov_disable(pdev);
327 	else
328 		return nfp_pcie_sriov_enable(pdev, num_vfs);
329 }
330 
331 static const struct firmware *
332 nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name)
333 {
334 	const struct firmware *fw = NULL;
335 	int err;
336 
337 	err = request_firmware_direct(&fw, name, &pdev->dev);
338 	nfp_info(pf->cpp, "  %s: %s\n",
339 		 name, err ? "not found" : "found, loading...");
340 	if (err)
341 		return NULL;
342 
343 	return fw;
344 }
345 
346 /**
347  * nfp_net_fw_find() - Find the correct firmware image for netdev mode
348  * @pdev:	PCI Device structure
349  * @pf:		NFP PF Device structure
350  *
351  * Return: firmware if found and requested successfully.
352  */
353 static const struct firmware *
354 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
355 {
356 	struct nfp_eth_table_port *port;
357 	const struct firmware *fw;
358 	const char *fw_model;
359 	char fw_name[256];
360 	const u8 *serial;
361 	u16 interface;
362 	int spc, i, j;
363 
364 	nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n");
365 
366 	/* First try to find a firmware image specific for this device */
367 	interface = nfp_cpp_interface(pf->cpp);
368 	nfp_cpp_serial(pf->cpp, &serial);
369 	sprintf(fw_name, "netronome/serial-%pMF-%02hhx-%02hhx.nffw",
370 		serial, interface >> 8, interface & 0xff);
371 	fw = nfp_net_fw_request(pdev, pf, fw_name);
372 	if (fw)
373 		return fw;
374 
375 	/* Then try the PCI name */
376 	sprintf(fw_name, "netronome/pci-%s.nffw", pci_name(pdev));
377 	fw = nfp_net_fw_request(pdev, pf, fw_name);
378 	if (fw)
379 		return fw;
380 
381 	/* Finally try the card type and media */
382 	if (!pf->eth_tbl) {
383 		dev_err(&pdev->dev, "Error: can't identify media config\n");
384 		return NULL;
385 	}
386 
387 	fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
388 	if (!fw_model) {
389 		dev_err(&pdev->dev, "Error: can't read part number\n");
390 		return NULL;
391 	}
392 
393 	spc = ARRAY_SIZE(fw_name);
394 	spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
395 
396 	for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
397 		port = &pf->eth_tbl->ports[i];
398 		j = 1;
399 		while (i + j < pf->eth_tbl->count &&
400 		       port->speed == port[j].speed)
401 			j++;
402 
403 		spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
404 				"_%dx%d", j, port->speed / 1000);
405 	}
406 
407 	if (spc <= 0)
408 		return NULL;
409 
410 	spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
411 	if (spc <= 0)
412 		return NULL;
413 
414 	return nfp_net_fw_request(pdev, pf, fw_name);
415 }
416 
417 /**
418  * nfp_net_fw_load() - Load the firmware image
419  * @pdev:       PCI Device structure
420  * @pf:		NFP PF Device structure
421  * @nsp:	NFP SP handle
422  *
423  * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
424  */
425 static int
426 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
427 {
428 	const struct firmware *fw;
429 	u16 interface;
430 	int err;
431 
432 	interface = nfp_cpp_interface(pf->cpp);
433 	if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
434 		/* Only Unit 0 should reset or load firmware */
435 		dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
436 		return 0;
437 	}
438 
439 	fw = nfp_net_fw_find(pdev, pf);
440 	if (!fw)
441 		return 0;
442 
443 	dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
444 	err = nfp_nsp_device_soft_reset(nsp);
445 	if (err < 0) {
446 		dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
447 			err);
448 		goto exit_release_fw;
449 	}
450 
451 	err = nfp_nsp_load_fw(nsp, fw);
452 
453 	if (err < 0) {
454 		dev_err(&pdev->dev, "FW loading failed: %d\n", err);
455 		goto exit_release_fw;
456 	}
457 
458 	dev_info(&pdev->dev, "Finished loading FW image\n");
459 
460 exit_release_fw:
461 	release_firmware(fw);
462 
463 	return err < 0 ? err : 1;
464 }
465 
466 static void
467 nfp_nsp_init_ports(struct pci_dev *pdev, struct nfp_pf *pf,
468 		   struct nfp_nsp *nsp)
469 {
470 	bool needs_reinit = false;
471 	int i;
472 
473 	pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
474 	if (!pf->eth_tbl)
475 		return;
476 
477 	if (!nfp_nsp_has_mac_reinit(nsp))
478 		return;
479 
480 	for (i = 0; i < pf->eth_tbl->count; i++)
481 		needs_reinit |= pf->eth_tbl->ports[i].override_changed;
482 	if (!needs_reinit)
483 		return;
484 
485 	kfree(pf->eth_tbl);
486 	if (nfp_nsp_mac_reinit(nsp))
487 		dev_warn(&pdev->dev, "MAC reinit failed\n");
488 
489 	pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
490 }
491 
492 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
493 {
494 	struct nfp_nsp *nsp;
495 	int err;
496 
497 	err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
498 	if (err)
499 		return err;
500 
501 	nsp = nfp_nsp_open(pf->cpp);
502 	if (IS_ERR(nsp)) {
503 		err = PTR_ERR(nsp);
504 		dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
505 		return err;
506 	}
507 
508 	err = nfp_nsp_wait(nsp);
509 	if (err < 0)
510 		goto exit_close_nsp;
511 
512 	nfp_nsp_init_ports(pdev, pf, nsp);
513 
514 	pf->nspi = __nfp_nsp_identify(nsp);
515 	if (pf->nspi)
516 		dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
517 
518 	err = nfp_fw_load(pdev, pf, nsp);
519 	if (err < 0) {
520 		kfree(pf->nspi);
521 		kfree(pf->eth_tbl);
522 		dev_err(&pdev->dev, "Failed to load FW\n");
523 		goto exit_close_nsp;
524 	}
525 
526 	pf->fw_loaded = !!err;
527 	err = 0;
528 
529 exit_close_nsp:
530 	nfp_nsp_close(nsp);
531 
532 	return err;
533 }
534 
535 static void nfp_fw_unload(struct nfp_pf *pf)
536 {
537 	struct nfp_nsp *nsp;
538 	int err;
539 
540 	nsp = nfp_nsp_open(pf->cpp);
541 	if (IS_ERR(nsp)) {
542 		nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
543 		return;
544 	}
545 
546 	err = nfp_nsp_device_soft_reset(nsp);
547 	if (err < 0)
548 		dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
549 	else
550 		dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
551 
552 	nfp_nsp_close(nsp);
553 }
554 
555 static int nfp_pf_find_rtsyms(struct nfp_pf *pf)
556 {
557 	char pf_symbol[256];
558 	unsigned int pf_id;
559 
560 	pf_id = nfp_cppcore_pcie_unit(pf->cpp);
561 
562 	/* Optional per-PCI PF mailbox */
563 	snprintf(pf_symbol, sizeof(pf_symbol), NFP_MBOX_SYM_NAME, pf_id);
564 	pf->mbox = nfp_rtsym_lookup(pf->rtbl, pf_symbol);
565 	if (pf->mbox && pf->mbox->size < NFP_MBOX_SYM_MIN_SIZE) {
566 		nfp_err(pf->cpp, "PF mailbox symbol too small: %llu < %d\n",
567 			pf->mbox->size, NFP_MBOX_SYM_MIN_SIZE);
568 		return -EINVAL;
569 	}
570 
571 	return 0;
572 }
573 
574 static int nfp_pci_probe(struct pci_dev *pdev,
575 			 const struct pci_device_id *pci_id)
576 {
577 	struct devlink *devlink;
578 	struct nfp_pf *pf;
579 	int err;
580 
581 	err = pci_enable_device(pdev);
582 	if (err < 0)
583 		return err;
584 
585 	pci_set_master(pdev);
586 
587 	err = dma_set_mask_and_coherent(&pdev->dev,
588 					DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
589 	if (err)
590 		goto err_pci_disable;
591 
592 	err = pci_request_regions(pdev, nfp_driver_name);
593 	if (err < 0) {
594 		dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
595 		goto err_pci_disable;
596 	}
597 
598 	devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf));
599 	if (!devlink) {
600 		err = -ENOMEM;
601 		goto err_rel_regions;
602 	}
603 	pf = devlink_priv(devlink);
604 	INIT_LIST_HEAD(&pf->vnics);
605 	INIT_LIST_HEAD(&pf->ports);
606 	mutex_init(&pf->lock);
607 	pci_set_drvdata(pdev, pf);
608 	pf->pdev = pdev;
609 
610 	pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
611 	if (!pf->wq) {
612 		err = -ENOMEM;
613 		goto err_pci_priv_unset;
614 	}
615 
616 	pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
617 	if (IS_ERR_OR_NULL(pf->cpp)) {
618 		err = PTR_ERR(pf->cpp);
619 		if (err >= 0)
620 			err = -ENOMEM;
621 		goto err_disable_msix;
622 	}
623 
624 	err = nfp_resource_table_init(pf->cpp);
625 	if (err)
626 		goto err_cpp_free;
627 
628 	pf->hwinfo = nfp_hwinfo_read(pf->cpp);
629 
630 	dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
631 		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"),
632 		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"),
633 		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"),
634 		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"),
635 		 nfp_hwinfo_lookup(pf->hwinfo, "cpld.version"));
636 
637 	err = nfp_pf_board_state_wait(pf);
638 	if (err)
639 		goto err_hwinfo_free;
640 
641 	err = nfp_nsp_init(pdev, pf);
642 	if (err)
643 		goto err_hwinfo_free;
644 
645 	pf->mip = nfp_mip_open(pf->cpp);
646 	pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
647 
648 	err = nfp_pf_find_rtsyms(pf);
649 	if (err)
650 		goto err_fw_unload;
651 
652 	pf->dump_flag = NFP_DUMP_NSP_DIAG;
653 	pf->dumpspec = nfp_net_dump_load_dumpspec(pf->cpp, pf->rtbl);
654 
655 	err = nfp_pcie_sriov_read_nfd_limit(pf);
656 	if (err)
657 		goto err_fw_unload;
658 
659 	pf->num_vfs = pci_num_vf(pdev);
660 	if (pf->num_vfs > pf->limit_vfs) {
661 		dev_err(&pdev->dev,
662 			"Error: %d VFs already enabled, but loaded FW can only support %d\n",
663 			pf->num_vfs, pf->limit_vfs);
664 		err = -EINVAL;
665 		goto err_fw_unload;
666 	}
667 
668 	err = nfp_net_pci_probe(pf);
669 	if (err)
670 		goto err_fw_unload;
671 
672 	err = nfp_hwmon_register(pf);
673 	if (err) {
674 		dev_err(&pdev->dev, "Failed to register hwmon info\n");
675 		goto err_net_remove;
676 	}
677 
678 	return 0;
679 
680 err_net_remove:
681 	nfp_net_pci_remove(pf);
682 err_fw_unload:
683 	kfree(pf->rtbl);
684 	nfp_mip_close(pf->mip);
685 	if (pf->fw_loaded)
686 		nfp_fw_unload(pf);
687 	kfree(pf->eth_tbl);
688 	kfree(pf->nspi);
689 	vfree(pf->dumpspec);
690 err_hwinfo_free:
691 	kfree(pf->hwinfo);
692 err_cpp_free:
693 	nfp_cpp_free(pf->cpp);
694 err_disable_msix:
695 	destroy_workqueue(pf->wq);
696 err_pci_priv_unset:
697 	pci_set_drvdata(pdev, NULL);
698 	mutex_destroy(&pf->lock);
699 	devlink_free(devlink);
700 err_rel_regions:
701 	pci_release_regions(pdev);
702 err_pci_disable:
703 	pci_disable_device(pdev);
704 
705 	return err;
706 }
707 
708 static void nfp_pci_remove(struct pci_dev *pdev)
709 {
710 	struct nfp_pf *pf = pci_get_drvdata(pdev);
711 
712 	nfp_hwmon_unregister(pf);
713 
714 	nfp_pcie_sriov_disable(pdev);
715 
716 	nfp_net_pci_remove(pf);
717 
718 	vfree(pf->dumpspec);
719 	kfree(pf->rtbl);
720 	nfp_mip_close(pf->mip);
721 	if (pf->fw_loaded)
722 		nfp_fw_unload(pf);
723 
724 	destroy_workqueue(pf->wq);
725 	pci_set_drvdata(pdev, NULL);
726 	kfree(pf->hwinfo);
727 	nfp_cpp_free(pf->cpp);
728 
729 	kfree(pf->eth_tbl);
730 	kfree(pf->nspi);
731 	mutex_destroy(&pf->lock);
732 	devlink_free(priv_to_devlink(pf));
733 	pci_release_regions(pdev);
734 	pci_disable_device(pdev);
735 }
736 
737 static struct pci_driver nfp_pci_driver = {
738 	.name			= nfp_driver_name,
739 	.id_table		= nfp_pci_device_ids,
740 	.probe			= nfp_pci_probe,
741 	.remove			= nfp_pci_remove,
742 	.sriov_configure	= nfp_pcie_sriov_configure,
743 };
744 
745 static int __init nfp_main_init(void)
746 {
747 	int err;
748 
749 	pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
750 		nfp_driver_name);
751 
752 	nfp_net_debugfs_create();
753 
754 	err = pci_register_driver(&nfp_pci_driver);
755 	if (err < 0)
756 		goto err_destroy_debugfs;
757 
758 	err = pci_register_driver(&nfp_netvf_pci_driver);
759 	if (err)
760 		goto err_unreg_pf;
761 
762 	return err;
763 
764 err_unreg_pf:
765 	pci_unregister_driver(&nfp_pci_driver);
766 err_destroy_debugfs:
767 	nfp_net_debugfs_destroy();
768 	return err;
769 }
770 
771 static void __exit nfp_main_exit(void)
772 {
773 	pci_unregister_driver(&nfp_netvf_pci_driver);
774 	pci_unregister_driver(&nfp_pci_driver);
775 	nfp_net_debugfs_destroy();
776 }
777 
778 module_init(nfp_main_init);
779 module_exit(nfp_main_exit);
780 
781 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
782 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
783 MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
784 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
785 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
786 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
787 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
788 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
789 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_1x10_1x25.nffw");
790 
791 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
792 MODULE_LICENSE("GPL");
793 MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");
794 MODULE_VERSION(UTS_RELEASE);
795