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/pci.h>
45 #include <linux/firmware.h>
46 #include <linux/vermagic.h>
47 
48 #include "nfpcore/nfp.h"
49 #include "nfpcore/nfp_cpp.h"
50 #include "nfpcore/nfp_nffw.h"
51 #include "nfpcore/nfp_nsp.h"
52 
53 #include "nfpcore/nfp6000_pcie.h"
54 
55 #include "nfp_main.h"
56 #include "nfp_net.h"
57 
58 static const char nfp_driver_name[] = "nfp";
59 const char nfp_driver_version[] = VERMAGIC_STRING;
60 
61 static const struct pci_device_id nfp_pci_device_ids[] = {
62 	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
63 	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
64 	  PCI_ANY_ID, 0,
65 	},
66 	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
67 	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
68 	  PCI_ANY_ID, 0,
69 	},
70 	{ 0, } /* Required last entry. */
71 };
72 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
73 
74 static void nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
75 {
76 #ifdef CONFIG_PCI_IOV
77 	int err;
78 
79 	pf->limit_vfs = nfp_rtsym_read_le(pf->cpp, "nfd_vf_cfg_max_vfs", &err);
80 	if (!err)
81 		return;
82 
83 	pf->limit_vfs = ~0;
84 	/* Allow any setting for backwards compatibility if symbol not found */
85 	if (err != -ENOENT)
86 		nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
87 #endif
88 }
89 
90 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
91 {
92 #ifdef CONFIG_PCI_IOV
93 	struct nfp_pf *pf = pci_get_drvdata(pdev);
94 	int err;
95 
96 	if (num_vfs > pf->limit_vfs) {
97 		nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
98 			 pf->limit_vfs);
99 		return -EINVAL;
100 	}
101 
102 	err = pci_enable_sriov(pdev, num_vfs);
103 	if (err) {
104 		dev_warn(&pdev->dev, "Failed to enable PCI sriov: %d\n", err);
105 		return err;
106 	}
107 
108 	pf->num_vfs = num_vfs;
109 
110 	dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
111 
112 	return num_vfs;
113 #endif
114 	return 0;
115 }
116 
117 static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
118 {
119 #ifdef CONFIG_PCI_IOV
120 	struct nfp_pf *pf = pci_get_drvdata(pdev);
121 
122 	/* If the VFs are assigned we cannot shut down SR-IOV without
123 	 * causing issues, so just leave the hardware available but
124 	 * disabled
125 	 */
126 	if (pci_vfs_assigned(pdev)) {
127 		dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
128 		return -EPERM;
129 	}
130 
131 	pf->num_vfs = 0;
132 
133 	pci_disable_sriov(pdev);
134 	dev_dbg(&pdev->dev, "Removed VFs.\n");
135 #endif
136 	return 0;
137 }
138 
139 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
140 {
141 	if (num_vfs == 0)
142 		return nfp_pcie_sriov_disable(pdev);
143 	else
144 		return nfp_pcie_sriov_enable(pdev, num_vfs);
145 }
146 
147 /**
148  * nfp_net_fw_find() - Find the correct firmware image for netdev mode
149  * @pdev:	PCI Device structure
150  * @pf:		NFP PF Device structure
151  *
152  * Return: firmware if found and requested successfully.
153  */
154 static const struct firmware *
155 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
156 {
157 	const struct firmware *fw = NULL;
158 	struct nfp_eth_table_port *port;
159 	const char *fw_model;
160 	char fw_name[256];
161 	int spc, err = 0;
162 	int i, j;
163 
164 	if (!pf->eth_tbl) {
165 		dev_err(&pdev->dev, "Error: can't identify media config\n");
166 		return NULL;
167 	}
168 
169 	fw_model = nfp_hwinfo_lookup(pf->cpp, "assembly.partno");
170 	if (!fw_model) {
171 		dev_err(&pdev->dev, "Error: can't read part number\n");
172 		return NULL;
173 	}
174 
175 	spc = ARRAY_SIZE(fw_name);
176 	spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
177 
178 	for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
179 		port = &pf->eth_tbl->ports[i];
180 		j = 1;
181 		while (i + j < pf->eth_tbl->count &&
182 		       port->speed == port[j].speed)
183 			j++;
184 
185 		spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
186 				"_%dx%d", j, port->speed / 1000);
187 	}
188 
189 	if (spc <= 0)
190 		return NULL;
191 
192 	spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
193 	if (spc <= 0)
194 		return NULL;
195 
196 	err = request_firmware(&fw, fw_name, &pdev->dev);
197 	if (err)
198 		return NULL;
199 
200 	dev_info(&pdev->dev, "Loading FW image: %s\n", fw_name);
201 
202 	return fw;
203 }
204 
205 /**
206  * nfp_net_fw_load() - Load the firmware image
207  * @pdev:       PCI Device structure
208  * @pf:		NFP PF Device structure
209  * @nsp:	NFP SP handle
210  *
211  * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
212  */
213 static int
214 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
215 {
216 	const struct firmware *fw;
217 	u16 interface;
218 	int err;
219 
220 	interface = nfp_cpp_interface(pf->cpp);
221 	if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
222 		/* Only Unit 0 should reset or load firmware */
223 		dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
224 		return 0;
225 	}
226 
227 	fw = nfp_net_fw_find(pdev, pf);
228 	if (!fw)
229 		return 0;
230 
231 	dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
232 	err = nfp_nsp_device_soft_reset(nsp);
233 	if (err < 0) {
234 		dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
235 			err);
236 		goto exit_release_fw;
237 	}
238 
239 	err = nfp_nsp_load_fw(nsp, fw);
240 
241 	if (err < 0) {
242 		dev_err(&pdev->dev, "FW loading failed: %d\n", err);
243 		goto exit_release_fw;
244 	}
245 
246 	dev_info(&pdev->dev, "Finished loading FW image\n");
247 
248 exit_release_fw:
249 	release_firmware(fw);
250 
251 	return err < 0 ? err : 1;
252 }
253 
254 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
255 {
256 	struct nfp_nsp_identify *nspi;
257 	struct nfp_nsp *nsp;
258 	int err;
259 
260 	nsp = nfp_nsp_open(pf->cpp);
261 	if (IS_ERR(nsp)) {
262 		err = PTR_ERR(nsp);
263 		dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
264 		return err;
265 	}
266 
267 	err = nfp_nsp_wait(nsp);
268 	if (err < 0)
269 		goto exit_close_nsp;
270 
271 	pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
272 
273 	nspi = __nfp_nsp_identify(nsp);
274 	if (nspi) {
275 		dev_info(&pdev->dev, "BSP: %s\n", nspi->version);
276 		kfree(nspi);
277 	}
278 
279 	err = nfp_fw_load(pdev, pf, nsp);
280 	if (err < 0) {
281 		kfree(pf->eth_tbl);
282 		dev_err(&pdev->dev, "Failed to load FW\n");
283 		goto exit_close_nsp;
284 	}
285 
286 	pf->fw_loaded = !!err;
287 	err = 0;
288 
289 exit_close_nsp:
290 	nfp_nsp_close(nsp);
291 
292 	return err;
293 }
294 
295 static void nfp_fw_unload(struct nfp_pf *pf)
296 {
297 	struct nfp_nsp *nsp;
298 	int err;
299 
300 	nsp = nfp_nsp_open(pf->cpp);
301 	if (IS_ERR(nsp)) {
302 		nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
303 		return;
304 	}
305 
306 	err = nfp_nsp_device_soft_reset(nsp);
307 	if (err < 0)
308 		dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
309 	else
310 		dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
311 
312 	nfp_nsp_close(nsp);
313 }
314 
315 static int nfp_pci_probe(struct pci_dev *pdev,
316 			 const struct pci_device_id *pci_id)
317 {
318 	struct nfp_pf *pf;
319 	int err;
320 
321 	err = pci_enable_device(pdev);
322 	if (err < 0)
323 		return err;
324 
325 	pci_set_master(pdev);
326 
327 	err = dma_set_mask_and_coherent(&pdev->dev,
328 					DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
329 	if (err)
330 		goto err_pci_disable;
331 
332 	err = pci_request_regions(pdev, nfp_driver_name);
333 	if (err < 0) {
334 		dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
335 		goto err_pci_disable;
336 	}
337 
338 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
339 	if (!pf) {
340 		err = -ENOMEM;
341 		goto err_rel_regions;
342 	}
343 	INIT_LIST_HEAD(&pf->ports);
344 	pci_set_drvdata(pdev, pf);
345 	pf->pdev = pdev;
346 
347 	pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
348 	if (IS_ERR_OR_NULL(pf->cpp)) {
349 		err = PTR_ERR(pf->cpp);
350 		if (err >= 0)
351 			err = -ENOMEM;
352 		goto err_disable_msix;
353 	}
354 
355 	dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
356 		 nfp_hwinfo_lookup(pf->cpp, "assembly.vendor"),
357 		 nfp_hwinfo_lookup(pf->cpp, "assembly.partno"),
358 		 nfp_hwinfo_lookup(pf->cpp, "assembly.serial"),
359 		 nfp_hwinfo_lookup(pf->cpp, "assembly.revision"),
360 		 nfp_hwinfo_lookup(pf->cpp, "cpld.version"));
361 
362 	err = nfp_nsp_init(pdev, pf);
363 	if (err)
364 		goto err_cpp_free;
365 
366 	nfp_pcie_sriov_read_nfd_limit(pf);
367 
368 	err = nfp_net_pci_probe(pf);
369 	if (err)
370 		goto err_fw_unload;
371 
372 	return 0;
373 
374 err_fw_unload:
375 	if (pf->fw_loaded)
376 		nfp_fw_unload(pf);
377 	kfree(pf->eth_tbl);
378 err_cpp_free:
379 	nfp_cpp_free(pf->cpp);
380 err_disable_msix:
381 	pci_set_drvdata(pdev, NULL);
382 	kfree(pf);
383 err_rel_regions:
384 	pci_release_regions(pdev);
385 err_pci_disable:
386 	pci_disable_device(pdev);
387 
388 	return err;
389 }
390 
391 static void nfp_pci_remove(struct pci_dev *pdev)
392 {
393 	struct nfp_pf *pf = pci_get_drvdata(pdev);
394 
395 	nfp_net_pci_remove(pf);
396 
397 	nfp_pcie_sriov_disable(pdev);
398 
399 	if (pf->fw_loaded)
400 		nfp_fw_unload(pf);
401 
402 	pci_set_drvdata(pdev, NULL);
403 	nfp_cpp_free(pf->cpp);
404 
405 	kfree(pf->eth_tbl);
406 	kfree(pf);
407 	pci_release_regions(pdev);
408 	pci_disable_device(pdev);
409 }
410 
411 static struct pci_driver nfp_pci_driver = {
412 	.name			= nfp_driver_name,
413 	.id_table		= nfp_pci_device_ids,
414 	.probe			= nfp_pci_probe,
415 	.remove			= nfp_pci_remove,
416 	.sriov_configure	= nfp_pcie_sriov_configure,
417 };
418 
419 static int __init nfp_main_init(void)
420 {
421 	int err;
422 
423 	pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
424 		nfp_driver_name);
425 
426 	nfp_net_debugfs_create();
427 
428 	err = pci_register_driver(&nfp_pci_driver);
429 	if (err < 0)
430 		goto err_destroy_debugfs;
431 
432 	err = pci_register_driver(&nfp_netvf_pci_driver);
433 	if (err)
434 		goto err_unreg_pf;
435 
436 	return err;
437 
438 err_unreg_pf:
439 	pci_unregister_driver(&nfp_pci_driver);
440 err_destroy_debugfs:
441 	nfp_net_debugfs_destroy();
442 	return err;
443 }
444 
445 static void __exit nfp_main_exit(void)
446 {
447 	pci_unregister_driver(&nfp_netvf_pci_driver);
448 	pci_unregister_driver(&nfp_pci_driver);
449 	nfp_net_debugfs_destroy();
450 }
451 
452 module_init(nfp_main_init);
453 module_exit(nfp_main_exit);
454 
455 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
456 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
457 MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
458 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
459 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
460 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
461 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
462 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
463 
464 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
465 MODULE_LICENSE("GPL");
466 MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");
467