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 <net/devlink.h>
49 
50 #include "nfpcore/nfp.h"
51 #include "nfpcore/nfp_cpp.h"
52 #include "nfpcore/nfp_nffw.h"
53 #include "nfpcore/nfp_nsp.h"
54 
55 #include "nfpcore/nfp6000_pcie.h"
56 
57 #include "nfp_app.h"
58 #include "nfp_main.h"
59 #include "nfp_net.h"
60 
61 static const char nfp_driver_name[] = "nfp";
62 const char nfp_driver_version[] = VERMAGIC_STRING;
63 
64 static const struct pci_device_id nfp_pci_device_ids[] = {
65 	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
66 	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
67 	  PCI_ANY_ID, 0,
68 	},
69 	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
70 	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
71 	  PCI_ANY_ID, 0,
72 	},
73 	{ 0, } /* Required last entry. */
74 };
75 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
76 
77 static bool nfp_board_ready(struct nfp_pf *pf)
78 {
79 	const char *cp;
80 	long state;
81 	int err;
82 
83 	cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
84 	if (!cp)
85 		return false;
86 
87 	err = kstrtol(cp, 0, &state);
88 	if (err < 0)
89 		return false;
90 
91 	return state == 15;
92 }
93 
94 static int nfp_pf_board_state_wait(struct nfp_pf *pf)
95 {
96 	const unsigned long wait_until = jiffies + 10 * HZ;
97 
98 	while (!nfp_board_ready(pf)) {
99 		if (time_is_before_eq_jiffies(wait_until)) {
100 			nfp_err(pf->cpp, "NFP board initialization timeout\n");
101 			return -EINVAL;
102 		}
103 
104 		nfp_info(pf->cpp, "waiting for board initialization\n");
105 		if (msleep_interruptible(500))
106 			return -ERESTARTSYS;
107 
108 		/* Refresh cached information */
109 		kfree(pf->hwinfo);
110 		pf->hwinfo = nfp_hwinfo_read(pf->cpp);
111 	}
112 
113 	return 0;
114 }
115 
116 static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
117 {
118 	int err;
119 
120 	pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
121 	if (!err)
122 		return pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
123 
124 	pf->limit_vfs = ~0;
125 	pci_sriov_set_totalvfs(pf->pdev, 0); /* 0 is unset */
126 	/* Allow any setting for backwards compatibility if symbol not found */
127 	if (err == -ENOENT)
128 		return 0;
129 
130 	nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
131 	return err;
132 }
133 
134 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
135 {
136 #ifdef CONFIG_PCI_IOV
137 	struct nfp_pf *pf = pci_get_drvdata(pdev);
138 	int err;
139 
140 	if (num_vfs > pf->limit_vfs) {
141 		nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
142 			 pf->limit_vfs);
143 		return -EINVAL;
144 	}
145 
146 	err = pci_enable_sriov(pdev, num_vfs);
147 	if (err) {
148 		dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
149 		return err;
150 	}
151 
152 	mutex_lock(&pf->lock);
153 
154 	err = nfp_app_sriov_enable(pf->app, num_vfs);
155 	if (err) {
156 		dev_warn(&pdev->dev,
157 			 "App specific PCI SR-IOV configuration failed: %d\n",
158 			 err);
159 		goto err_sriov_disable;
160 	}
161 
162 	pf->num_vfs = num_vfs;
163 
164 	dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
165 
166 	mutex_unlock(&pf->lock);
167 	return num_vfs;
168 
169 err_sriov_disable:
170 	mutex_unlock(&pf->lock);
171 	pci_disable_sriov(pdev);
172 	return err;
173 #endif
174 	return 0;
175 }
176 
177 static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
178 {
179 #ifdef CONFIG_PCI_IOV
180 	struct nfp_pf *pf = pci_get_drvdata(pdev);
181 
182 	mutex_lock(&pf->lock);
183 
184 	/* If the VFs are assigned we cannot shut down SR-IOV without
185 	 * causing issues, so just leave the hardware available but
186 	 * disabled
187 	 */
188 	if (pci_vfs_assigned(pdev)) {
189 		dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
190 		mutex_unlock(&pf->lock);
191 		return -EPERM;
192 	}
193 
194 	nfp_app_sriov_disable(pf->app);
195 
196 	pf->num_vfs = 0;
197 
198 	mutex_unlock(&pf->lock);
199 
200 	pci_disable_sriov(pdev);
201 	dev_dbg(&pdev->dev, "Removed VFs.\n");
202 #endif
203 	return 0;
204 }
205 
206 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
207 {
208 	if (num_vfs == 0)
209 		return nfp_pcie_sriov_disable(pdev);
210 	else
211 		return nfp_pcie_sriov_enable(pdev, num_vfs);
212 }
213 
214 static const struct firmware *
215 nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name)
216 {
217 	const struct firmware *fw = NULL;
218 	int err;
219 
220 	err = request_firmware_direct(&fw, name, &pdev->dev);
221 	nfp_info(pf->cpp, "  %s: %s\n",
222 		 name, err ? "not found" : "found, loading...");
223 	if (err)
224 		return NULL;
225 
226 	return fw;
227 }
228 
229 /**
230  * nfp_net_fw_find() - Find the correct firmware image for netdev mode
231  * @pdev:	PCI Device structure
232  * @pf:		NFP PF Device structure
233  *
234  * Return: firmware if found and requested successfully.
235  */
236 static const struct firmware *
237 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
238 {
239 	struct nfp_eth_table_port *port;
240 	const struct firmware *fw;
241 	const char *fw_model;
242 	char fw_name[256];
243 	const u8 *serial;
244 	u16 interface;
245 	int spc, i, j;
246 
247 	nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n");
248 
249 	/* First try to find a firmware image specific for this device */
250 	interface = nfp_cpp_interface(pf->cpp);
251 	nfp_cpp_serial(pf->cpp, &serial);
252 	sprintf(fw_name, "netronome/serial-%pMF-%02hhx-%02hhx.nffw",
253 		serial, interface >> 8, interface & 0xff);
254 	fw = nfp_net_fw_request(pdev, pf, fw_name);
255 	if (fw)
256 		return fw;
257 
258 	/* Then try the PCI name */
259 	sprintf(fw_name, "netronome/pci-%s.nffw", pci_name(pdev));
260 	fw = nfp_net_fw_request(pdev, pf, fw_name);
261 	if (fw)
262 		return fw;
263 
264 	/* Finally try the card type and media */
265 	if (!pf->eth_tbl) {
266 		dev_err(&pdev->dev, "Error: can't identify media config\n");
267 		return NULL;
268 	}
269 
270 	fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
271 	if (!fw_model) {
272 		dev_err(&pdev->dev, "Error: can't read part number\n");
273 		return NULL;
274 	}
275 
276 	spc = ARRAY_SIZE(fw_name);
277 	spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
278 
279 	for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
280 		port = &pf->eth_tbl->ports[i];
281 		j = 1;
282 		while (i + j < pf->eth_tbl->count &&
283 		       port->speed == port[j].speed)
284 			j++;
285 
286 		spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
287 				"_%dx%d", j, port->speed / 1000);
288 	}
289 
290 	if (spc <= 0)
291 		return NULL;
292 
293 	spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
294 	if (spc <= 0)
295 		return NULL;
296 
297 	return nfp_net_fw_request(pdev, pf, fw_name);
298 }
299 
300 /**
301  * nfp_net_fw_load() - Load the firmware image
302  * @pdev:       PCI Device structure
303  * @pf:		NFP PF Device structure
304  * @nsp:	NFP SP handle
305  *
306  * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
307  */
308 static int
309 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
310 {
311 	const struct firmware *fw;
312 	u16 interface;
313 	int err;
314 
315 	interface = nfp_cpp_interface(pf->cpp);
316 	if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
317 		/* Only Unit 0 should reset or load firmware */
318 		dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
319 		return 0;
320 	}
321 
322 	fw = nfp_net_fw_find(pdev, pf);
323 	if (!fw)
324 		return 0;
325 
326 	dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
327 	err = nfp_nsp_device_soft_reset(nsp);
328 	if (err < 0) {
329 		dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
330 			err);
331 		goto exit_release_fw;
332 	}
333 
334 	err = nfp_nsp_load_fw(nsp, fw);
335 
336 	if (err < 0) {
337 		dev_err(&pdev->dev, "FW loading failed: %d\n", err);
338 		goto exit_release_fw;
339 	}
340 
341 	dev_info(&pdev->dev, "Finished loading FW image\n");
342 
343 exit_release_fw:
344 	release_firmware(fw);
345 
346 	return err < 0 ? err : 1;
347 }
348 
349 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
350 {
351 	struct nfp_nsp *nsp;
352 	int err;
353 
354 	err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
355 	if (err)
356 		return err;
357 
358 	nsp = nfp_nsp_open(pf->cpp);
359 	if (IS_ERR(nsp)) {
360 		err = PTR_ERR(nsp);
361 		dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
362 		return err;
363 	}
364 
365 	err = nfp_nsp_wait(nsp);
366 	if (err < 0)
367 		goto exit_close_nsp;
368 
369 	pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
370 
371 	pf->nspi = __nfp_nsp_identify(nsp);
372 	if (pf->nspi)
373 		dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
374 
375 	err = nfp_fw_load(pdev, pf, nsp);
376 	if (err < 0) {
377 		kfree(pf->nspi);
378 		kfree(pf->eth_tbl);
379 		dev_err(&pdev->dev, "Failed to load FW\n");
380 		goto exit_close_nsp;
381 	}
382 
383 	pf->fw_loaded = !!err;
384 	err = 0;
385 
386 exit_close_nsp:
387 	nfp_nsp_close(nsp);
388 
389 	return err;
390 }
391 
392 static void nfp_fw_unload(struct nfp_pf *pf)
393 {
394 	struct nfp_nsp *nsp;
395 	int err;
396 
397 	nsp = nfp_nsp_open(pf->cpp);
398 	if (IS_ERR(nsp)) {
399 		nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
400 		return;
401 	}
402 
403 	err = nfp_nsp_device_soft_reset(nsp);
404 	if (err < 0)
405 		dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
406 	else
407 		dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
408 
409 	nfp_nsp_close(nsp);
410 }
411 
412 static int nfp_pci_probe(struct pci_dev *pdev,
413 			 const struct pci_device_id *pci_id)
414 {
415 	struct devlink *devlink;
416 	struct nfp_pf *pf;
417 	int err;
418 
419 	err = pci_enable_device(pdev);
420 	if (err < 0)
421 		return err;
422 
423 	pci_set_master(pdev);
424 
425 	err = dma_set_mask_and_coherent(&pdev->dev,
426 					DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
427 	if (err)
428 		goto err_pci_disable;
429 
430 	err = pci_request_regions(pdev, nfp_driver_name);
431 	if (err < 0) {
432 		dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
433 		goto err_pci_disable;
434 	}
435 
436 	devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf));
437 	if (!devlink) {
438 		err = -ENOMEM;
439 		goto err_rel_regions;
440 	}
441 	pf = devlink_priv(devlink);
442 	INIT_LIST_HEAD(&pf->vnics);
443 	INIT_LIST_HEAD(&pf->ports);
444 	mutex_init(&pf->lock);
445 	pci_set_drvdata(pdev, pf);
446 	pf->pdev = pdev;
447 
448 	pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
449 	if (!pf->wq) {
450 		err = -ENOMEM;
451 		goto err_pci_priv_unset;
452 	}
453 
454 	pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
455 	if (IS_ERR_OR_NULL(pf->cpp)) {
456 		err = PTR_ERR(pf->cpp);
457 		if (err >= 0)
458 			err = -ENOMEM;
459 		goto err_disable_msix;
460 	}
461 
462 	pf->hwinfo = nfp_hwinfo_read(pf->cpp);
463 
464 	dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
465 		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"),
466 		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"),
467 		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"),
468 		 nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"),
469 		 nfp_hwinfo_lookup(pf->hwinfo, "cpld.version"));
470 
471 	err = nfp_pf_board_state_wait(pf);
472 	if (err)
473 		goto err_hwinfo_free;
474 
475 	err = devlink_register(devlink, &pdev->dev);
476 	if (err)
477 		goto err_hwinfo_free;
478 
479 	err = nfp_nsp_init(pdev, pf);
480 	if (err)
481 		goto err_devlink_unreg;
482 
483 	pf->mip = nfp_mip_open(pf->cpp);
484 	pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
485 
486 	err = nfp_pcie_sriov_read_nfd_limit(pf);
487 	if (err)
488 		goto err_fw_unload;
489 
490 	pf->num_vfs = pci_num_vf(pdev);
491 	if (pf->num_vfs > pf->limit_vfs) {
492 		dev_err(&pdev->dev,
493 			"Error: %d VFs already enabled, but loaded FW can only support %d\n",
494 			pf->num_vfs, pf->limit_vfs);
495 		goto err_fw_unload;
496 	}
497 
498 	err = nfp_net_pci_probe(pf);
499 	if (err)
500 		goto err_sriov_unlimit;
501 
502 	err = nfp_hwmon_register(pf);
503 	if (err) {
504 		dev_err(&pdev->dev, "Failed to register hwmon info\n");
505 		goto err_net_remove;
506 	}
507 
508 	return 0;
509 
510 err_net_remove:
511 	nfp_net_pci_remove(pf);
512 err_sriov_unlimit:
513 	pci_sriov_set_totalvfs(pf->pdev, 0);
514 err_fw_unload:
515 	kfree(pf->rtbl);
516 	nfp_mip_close(pf->mip);
517 	if (pf->fw_loaded)
518 		nfp_fw_unload(pf);
519 	kfree(pf->eth_tbl);
520 	kfree(pf->nspi);
521 err_devlink_unreg:
522 	devlink_unregister(devlink);
523 err_hwinfo_free:
524 	kfree(pf->hwinfo);
525 	nfp_cpp_free(pf->cpp);
526 err_disable_msix:
527 	destroy_workqueue(pf->wq);
528 err_pci_priv_unset:
529 	pci_set_drvdata(pdev, NULL);
530 	mutex_destroy(&pf->lock);
531 	devlink_free(devlink);
532 err_rel_regions:
533 	pci_release_regions(pdev);
534 err_pci_disable:
535 	pci_disable_device(pdev);
536 
537 	return err;
538 }
539 
540 static void nfp_pci_remove(struct pci_dev *pdev)
541 {
542 	struct nfp_pf *pf = pci_get_drvdata(pdev);
543 	struct devlink *devlink;
544 
545 	nfp_hwmon_unregister(pf);
546 
547 	devlink = priv_to_devlink(pf);
548 
549 	nfp_net_pci_remove(pf);
550 
551 	nfp_pcie_sriov_disable(pdev);
552 	pci_sriov_set_totalvfs(pf->pdev, 0);
553 
554 	devlink_unregister(devlink);
555 
556 	kfree(pf->rtbl);
557 	nfp_mip_close(pf->mip);
558 	if (pf->fw_loaded)
559 		nfp_fw_unload(pf);
560 
561 	destroy_workqueue(pf->wq);
562 	pci_set_drvdata(pdev, NULL);
563 	kfree(pf->hwinfo);
564 	nfp_cpp_free(pf->cpp);
565 
566 	kfree(pf->eth_tbl);
567 	kfree(pf->nspi);
568 	mutex_destroy(&pf->lock);
569 	devlink_free(devlink);
570 	pci_release_regions(pdev);
571 	pci_disable_device(pdev);
572 }
573 
574 static struct pci_driver nfp_pci_driver = {
575 	.name			= nfp_driver_name,
576 	.id_table		= nfp_pci_device_ids,
577 	.probe			= nfp_pci_probe,
578 	.remove			= nfp_pci_remove,
579 	.sriov_configure	= nfp_pcie_sriov_configure,
580 };
581 
582 static int __init nfp_main_init(void)
583 {
584 	int err;
585 
586 	pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
587 		nfp_driver_name);
588 
589 	nfp_net_debugfs_create();
590 
591 	err = pci_register_driver(&nfp_pci_driver);
592 	if (err < 0)
593 		goto err_destroy_debugfs;
594 
595 	err = pci_register_driver(&nfp_netvf_pci_driver);
596 	if (err)
597 		goto err_unreg_pf;
598 
599 	return err;
600 
601 err_unreg_pf:
602 	pci_unregister_driver(&nfp_pci_driver);
603 err_destroy_debugfs:
604 	nfp_net_debugfs_destroy();
605 	return err;
606 }
607 
608 static void __exit nfp_main_exit(void)
609 {
610 	pci_unregister_driver(&nfp_netvf_pci_driver);
611 	pci_unregister_driver(&nfp_pci_driver);
612 	nfp_net_debugfs_destroy();
613 }
614 
615 module_init(nfp_main_init);
616 module_exit(nfp_main_exit);
617 
618 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
619 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
620 MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
621 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
622 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
623 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
624 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
625 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
626 
627 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
628 MODULE_LICENSE("GPL");
629 MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");
630