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