Linux Audio

Check our new training course

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