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_eth.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 *nsp;
257 	int err;
258 
259 	nsp = nfp_nsp_open(pf->cpp);
260 	if (IS_ERR(nsp)) {
261 		err = PTR_ERR(nsp);
262 		dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
263 		return err;
264 	}
265 
266 	err = nfp_nsp_wait(nsp);
267 	if (err < 0)
268 		goto exit_close_nsp;
269 
270 	pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
271 
272 	err = nfp_fw_load(pdev, pf, nsp);
273 	if (err < 0) {
274 		kfree(pf->eth_tbl);
275 		dev_err(&pdev->dev, "Failed to load FW\n");
276 		goto exit_close_nsp;
277 	}
278 
279 	pf->fw_loaded = !!err;
280 	err = 0;
281 
282 exit_close_nsp:
283 	nfp_nsp_close(nsp);
284 
285 	return err;
286 }
287 
288 static void nfp_fw_unload(struct nfp_pf *pf)
289 {
290 	struct nfp_nsp *nsp;
291 	int err;
292 
293 	nsp = nfp_nsp_open(pf->cpp);
294 	if (IS_ERR(nsp)) {
295 		nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
296 		return;
297 	}
298 
299 	err = nfp_nsp_device_soft_reset(nsp);
300 	if (err < 0)
301 		dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
302 	else
303 		dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
304 
305 	nfp_nsp_close(nsp);
306 }
307 
308 static int nfp_pci_probe(struct pci_dev *pdev,
309 			 const struct pci_device_id *pci_id)
310 {
311 	struct nfp_pf *pf;
312 	int err;
313 
314 	err = pci_enable_device(pdev);
315 	if (err < 0)
316 		return err;
317 
318 	pci_set_master(pdev);
319 
320 	err = dma_set_mask_and_coherent(&pdev->dev,
321 					DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
322 	if (err)
323 		goto err_pci_disable;
324 
325 	err = pci_request_regions(pdev, nfp_driver_name);
326 	if (err < 0) {
327 		dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
328 		goto err_pci_disable;
329 	}
330 
331 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
332 	if (!pf) {
333 		err = -ENOMEM;
334 		goto err_rel_regions;
335 	}
336 	INIT_LIST_HEAD(&pf->ports);
337 	pci_set_drvdata(pdev, pf);
338 	pf->pdev = pdev;
339 
340 	pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
341 	if (IS_ERR_OR_NULL(pf->cpp)) {
342 		err = PTR_ERR(pf->cpp);
343 		if (err >= 0)
344 			err = -ENOMEM;
345 		goto err_disable_msix;
346 	}
347 
348 	dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
349 		 nfp_hwinfo_lookup(pf->cpp, "assembly.vendor"),
350 		 nfp_hwinfo_lookup(pf->cpp, "assembly.partno"),
351 		 nfp_hwinfo_lookup(pf->cpp, "assembly.serial"),
352 		 nfp_hwinfo_lookup(pf->cpp, "assembly.revision"),
353 		 nfp_hwinfo_lookup(pf->cpp, "cpld.version"));
354 
355 	err = nfp_nsp_init(pdev, pf);
356 	if (err)
357 		goto err_cpp_free;
358 
359 	nfp_pcie_sriov_read_nfd_limit(pf);
360 
361 	err = nfp_net_pci_probe(pf);
362 	if (err)
363 		goto err_fw_unload;
364 
365 	return 0;
366 
367 err_fw_unload:
368 	if (pf->fw_loaded)
369 		nfp_fw_unload(pf);
370 	kfree(pf->eth_tbl);
371 err_cpp_free:
372 	nfp_cpp_free(pf->cpp);
373 err_disable_msix:
374 	pci_set_drvdata(pdev, NULL);
375 	kfree(pf);
376 err_rel_regions:
377 	pci_release_regions(pdev);
378 err_pci_disable:
379 	pci_disable_device(pdev);
380 
381 	return err;
382 }
383 
384 static void nfp_pci_remove(struct pci_dev *pdev)
385 {
386 	struct nfp_pf *pf = pci_get_drvdata(pdev);
387 
388 	if (!list_empty(&pf->ports))
389 		nfp_net_pci_remove(pf);
390 
391 	nfp_pcie_sriov_disable(pdev);
392 
393 	if (pf->fw_loaded)
394 		nfp_fw_unload(pf);
395 
396 	pci_set_drvdata(pdev, NULL);
397 	nfp_cpp_free(pf->cpp);
398 
399 	kfree(pf->eth_tbl);
400 	kfree(pf);
401 	pci_release_regions(pdev);
402 	pci_disable_device(pdev);
403 }
404 
405 static struct pci_driver nfp_pci_driver = {
406 	.name			= nfp_driver_name,
407 	.id_table		= nfp_pci_device_ids,
408 	.probe			= nfp_pci_probe,
409 	.remove			= nfp_pci_remove,
410 	.sriov_configure	= nfp_pcie_sriov_configure,
411 };
412 
413 static int __init nfp_main_init(void)
414 {
415 	int err;
416 
417 	pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
418 		nfp_driver_name);
419 
420 	nfp_net_debugfs_create();
421 
422 	err = pci_register_driver(&nfp_pci_driver);
423 	if (err < 0)
424 		goto err_destroy_debugfs;
425 
426 	err = pci_register_driver(&nfp_netvf_pci_driver);
427 	if (err)
428 		goto err_unreg_pf;
429 
430 	return err;
431 
432 err_unreg_pf:
433 	pci_unregister_driver(&nfp_pci_driver);
434 err_destroy_debugfs:
435 	nfp_net_debugfs_destroy();
436 	return err;
437 }
438 
439 static void __exit nfp_main_exit(void)
440 {
441 	pci_unregister_driver(&nfp_netvf_pci_driver);
442 	pci_unregister_driver(&nfp_pci_driver);
443 	nfp_net_debugfs_destroy();
444 }
445 
446 module_init(nfp_main_init);
447 module_exit(nfp_main_exit);
448 
449 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
450 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
451 MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
452 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
453 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
454 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
455 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
456 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
457 
458 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
459 MODULE_LICENSE("GPL");
460 MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");
461