blob: 68cd34d5a9fbd6c0cde28cf8b68f3e5ad4a10a94 [file] [log] [blame]
Jakub Kicinski2633beb2017-02-09 09:17:28 -08001/*
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>
Jakub Kicinski346cfe82017-05-26 01:03:31 -070044#include <linux/mutex.h>
Jakub Kicinski2633beb2017-02-09 09:17:28 -080045#include <linux/pci.h>
46#include <linux/firmware.h>
47#include <linux/vermagic.h>
Simon Horman1851f93f2017-05-26 01:03:32 -070048#include <net/devlink.h>
Jakub Kicinski2633beb2017-02-09 09:17:28 -080049
Jakub Kicinski63461a02017-02-09 09:17:38 -080050#include "nfpcore/nfp.h"
51#include "nfpcore/nfp_cpp.h"
Jakub Kicinski0bc38272017-02-19 11:58:14 -080052#include "nfpcore/nfp_nffw.h"
Jakub Kicinskice22f5a2017-04-04 16:12:30 -070053#include "nfpcore/nfp_nsp.h"
Jakub Kicinski63461a02017-02-09 09:17:38 -080054
55#include "nfpcore/nfp6000_pcie.h"
56
Jakub Kicinski2633beb2017-02-09 09:17:28 -080057#include "nfp_main.h"
58#include "nfp_net.h"
59
60static const char nfp_driver_name[] = "nfp";
61const char nfp_driver_version[] = VERMAGIC_STRING;
62
Jakub Kicinski63461a02017-02-09 09:17:38 -080063static const struct pci_device_id nfp_pci_device_ids[] = {
Simon Horman3b473522017-02-17 08:57:54 +010064 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
Jakub Kicinski63461a02017-02-09 09:17:38 -080065 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
66 PCI_ANY_ID, 0,
67 },
Simon Horman3b473522017-02-17 08:57:54 +010068 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
Jakub Kicinski63461a02017-02-09 09:17:38 -080069 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
70 PCI_ANY_ID, 0,
71 },
72 { 0, } /* Required last entry. */
73};
74MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
75
Jakub Kicinski651e1f22017-05-28 17:52:54 -070076static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
Jakub Kicinski0bc38272017-02-19 11:58:14 -080077{
Jakub Kicinski0bc38272017-02-19 11:58:14 -080078 int err;
79
80 pf->limit_vfs = nfp_rtsym_read_le(pf->cpp, "nfd_vf_cfg_max_vfs", &err);
81 if (!err)
Jakub Kicinski651e1f22017-05-28 17:52:54 -070082 return pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
Jakub Kicinski0bc38272017-02-19 11:58:14 -080083
84 pf->limit_vfs = ~0;
Jakub Kicinski651e1f22017-05-28 17:52:54 -070085 pci_sriov_set_totalvfs(pf->pdev, 0); /* 0 is unset */
Jakub Kicinski0bc38272017-02-19 11:58:14 -080086 /* Allow any setting for backwards compatibility if symbol not found */
Jakub Kicinski651e1f22017-05-28 17:52:54 -070087 if (err == -ENOENT)
88 return 0;
89
90 nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
91 return err;
Jakub Kicinski0bc38272017-02-19 11:58:14 -080092}
93
Jakub Kicinski63461a02017-02-09 09:17:38 -080094static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
95{
96#ifdef CONFIG_PCI_IOV
97 struct nfp_pf *pf = pci_get_drvdata(pdev);
98 int err;
99
Jakub Kicinski0bc38272017-02-19 11:58:14 -0800100 if (num_vfs > pf->limit_vfs) {
101 nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
102 pf->limit_vfs);
103 return -EINVAL;
104 }
105
Jakub Kicinski63461a02017-02-09 09:17:38 -0800106 err = pci_enable_sriov(pdev, num_vfs);
107 if (err) {
108 dev_warn(&pdev->dev, "Failed to enable PCI sriov: %d\n", err);
109 return err;
110 }
111
112 pf->num_vfs = num_vfs;
113
114 dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
115
116 return num_vfs;
117#endif
118 return 0;
119}
120
121static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
122{
123#ifdef CONFIG_PCI_IOV
124 struct nfp_pf *pf = pci_get_drvdata(pdev);
125
126 /* If the VFs are assigned we cannot shut down SR-IOV without
127 * causing issues, so just leave the hardware available but
128 * disabled
129 */
130 if (pci_vfs_assigned(pdev)) {
131 dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
132 return -EPERM;
133 }
134
135 pf->num_vfs = 0;
136
137 pci_disable_sriov(pdev);
138 dev_dbg(&pdev->dev, "Removed VFs.\n");
139#endif
140 return 0;
141}
142
143static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
144{
145 if (num_vfs == 0)
146 return nfp_pcie_sriov_disable(pdev);
147 else
148 return nfp_pcie_sriov_enable(pdev, num_vfs);
149}
150
151/**
152 * nfp_net_fw_find() - Find the correct firmware image for netdev mode
153 * @pdev: PCI Device structure
154 * @pf: NFP PF Device structure
155 *
156 * Return: firmware if found and requested successfully.
157 */
158static const struct firmware *
159nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
160{
161 const struct firmware *fw = NULL;
162 struct nfp_eth_table_port *port;
163 const char *fw_model;
164 char fw_name[256];
165 int spc, err = 0;
166 int i, j;
167
168 if (!pf->eth_tbl) {
169 dev_err(&pdev->dev, "Error: can't identify media config\n");
170 return NULL;
171 }
172
173 fw_model = nfp_hwinfo_lookup(pf->cpp, "assembly.partno");
174 if (!fw_model) {
175 dev_err(&pdev->dev, "Error: can't read part number\n");
176 return NULL;
177 }
178
179 spc = ARRAY_SIZE(fw_name);
180 spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
181
182 for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
183 port = &pf->eth_tbl->ports[i];
184 j = 1;
185 while (i + j < pf->eth_tbl->count &&
186 port->speed == port[j].speed)
187 j++;
188
189 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
190 "_%dx%d", j, port->speed / 1000);
191 }
192
193 if (spc <= 0)
194 return NULL;
195
196 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
197 if (spc <= 0)
198 return NULL;
199
200 err = request_firmware(&fw, fw_name, &pdev->dev);
201 if (err)
202 return NULL;
203
204 dev_info(&pdev->dev, "Loading FW image: %s\n", fw_name);
205
206 return fw;
207}
208
209/**
210 * nfp_net_fw_load() - Load the firmware image
211 * @pdev: PCI Device structure
212 * @pf: NFP PF Device structure
213 * @nsp: NFP SP handle
214 *
215 * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
216 */
217static int
218nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
219{
220 const struct firmware *fw;
221 u16 interface;
222 int err;
223
224 interface = nfp_cpp_interface(pf->cpp);
225 if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
226 /* Only Unit 0 should reset or load firmware */
227 dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
228 return 0;
229 }
230
231 fw = nfp_net_fw_find(pdev, pf);
232 if (!fw)
233 return 0;
234
235 dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
236 err = nfp_nsp_device_soft_reset(nsp);
237 if (err < 0) {
238 dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
239 err);
240 goto exit_release_fw;
241 }
242
243 err = nfp_nsp_load_fw(nsp, fw);
244
245 if (err < 0) {
246 dev_err(&pdev->dev, "FW loading failed: %d\n", err);
247 goto exit_release_fw;
248 }
249
250 dev_info(&pdev->dev, "Finished loading FW image\n");
251
252exit_release_fw:
253 release_firmware(fw);
254
255 return err < 0 ? err : 1;
256}
257
Jakub Kicinskia9c83f72017-02-19 11:58:08 -0800258static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
259{
260 struct nfp_nsp *nsp;
261 int err;
262
263 nsp = nfp_nsp_open(pf->cpp);
264 if (IS_ERR(nsp)) {
265 err = PTR_ERR(nsp);
266 dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
267 return err;
268 }
269
270 err = nfp_nsp_wait(nsp);
271 if (err < 0)
272 goto exit_close_nsp;
273
274 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
275
David Bruneczeefbde72017-05-28 17:53:00 -0700276 pf->nspi = __nfp_nsp_identify(nsp);
277 if (pf->nspi)
278 dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
David Brunecz010e2f92017-04-22 20:17:54 -0700279
Jakub Kicinskia9c83f72017-02-19 11:58:08 -0800280 err = nfp_fw_load(pdev, pf, nsp);
281 if (err < 0) {
282 kfree(pf->eth_tbl);
283 dev_err(&pdev->dev, "Failed to load FW\n");
284 goto exit_close_nsp;
285 }
286
287 pf->fw_loaded = !!err;
288 err = 0;
289
290exit_close_nsp:
291 nfp_nsp_close(nsp);
292
293 return err;
294}
295
Jakub Kicinski63461a02017-02-09 09:17:38 -0800296static void nfp_fw_unload(struct nfp_pf *pf)
297{
298 struct nfp_nsp *nsp;
299 int err;
300
301 nsp = nfp_nsp_open(pf->cpp);
302 if (IS_ERR(nsp)) {
303 nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
304 return;
305 }
306
307 err = nfp_nsp_device_soft_reset(nsp);
308 if (err < 0)
309 dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
310 else
311 dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
312
313 nfp_nsp_close(nsp);
314}
315
316static int nfp_pci_probe(struct pci_dev *pdev,
317 const struct pci_device_id *pci_id)
318{
Simon Horman1851f93f2017-05-26 01:03:32 -0700319 struct devlink *devlink;
Jakub Kicinski63461a02017-02-09 09:17:38 -0800320 struct nfp_pf *pf;
321 int err;
322
323 err = pci_enable_device(pdev);
324 if (err < 0)
325 return err;
326
327 pci_set_master(pdev);
328
329 err = dma_set_mask_and_coherent(&pdev->dev,
330 DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
331 if (err)
332 goto err_pci_disable;
333
334 err = pci_request_regions(pdev, nfp_driver_name);
335 if (err < 0) {
336 dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
337 goto err_pci_disable;
338 }
339
Simon Horman1851f93f2017-05-26 01:03:32 -0700340 devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf));
341 if (!devlink) {
Jakub Kicinski63461a02017-02-09 09:17:38 -0800342 err = -ENOMEM;
343 goto err_rel_regions;
344 }
Simon Horman1851f93f2017-05-26 01:03:32 -0700345 pf = devlink_priv(devlink);
Jakub Kicinskid4e7f092017-05-22 10:59:24 -0700346 INIT_LIST_HEAD(&pf->vnics);
Jakub Kicinski3eb3b742017-05-22 10:59:31 -0700347 INIT_LIST_HEAD(&pf->ports);
Jakub Kicinski346cfe82017-05-26 01:03:31 -0700348 mutex_init(&pf->lock);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800349 pci_set_drvdata(pdev, pf);
350 pf->pdev = pdev;
351
352 pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
353 if (IS_ERR_OR_NULL(pf->cpp)) {
354 err = PTR_ERR(pf->cpp);
355 if (err >= 0)
356 err = -ENOMEM;
357 goto err_disable_msix;
358 }
359
Jakub Kicinski64db09e2017-02-19 11:58:09 -0800360 dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
361 nfp_hwinfo_lookup(pf->cpp, "assembly.vendor"),
362 nfp_hwinfo_lookup(pf->cpp, "assembly.partno"),
363 nfp_hwinfo_lookup(pf->cpp, "assembly.serial"),
364 nfp_hwinfo_lookup(pf->cpp, "assembly.revision"),
365 nfp_hwinfo_lookup(pf->cpp, "cpld.version"));
366
Simon Horman1851f93f2017-05-26 01:03:32 -0700367 err = devlink_register(devlink, &pdev->dev);
Jakub Kicinskia9c83f72017-02-19 11:58:08 -0800368 if (err)
Jakub Kicinski63461a02017-02-09 09:17:38 -0800369 goto err_cpp_free;
Jakub Kicinski63461a02017-02-09 09:17:38 -0800370
Simon Horman1851f93f2017-05-26 01:03:32 -0700371 err = nfp_nsp_init(pdev, pf);
372 if (err)
373 goto err_devlink_unreg;
374
Jakub Kicinski651e1f22017-05-28 17:52:54 -0700375 err = nfp_pcie_sriov_read_nfd_limit(pf);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800376 if (err)
377 goto err_fw_unload;
378
Jakub Kicinski651e1f22017-05-28 17:52:54 -0700379 err = nfp_net_pci_probe(pf);
380 if (err)
381 goto err_sriov_unlimit;
382
David Bruneczeefbde72017-05-28 17:53:00 -0700383 err = nfp_hwmon_register(pf);
384 if (err) {
385 dev_err(&pdev->dev, "Failed to register hwmon info\n");
386 goto err_net_remove;
387 }
388
Jakub Kicinski63461a02017-02-09 09:17:38 -0800389 return 0;
390
David Bruneczeefbde72017-05-28 17:53:00 -0700391err_net_remove:
392 nfp_net_pci_remove(pf);
Jakub Kicinski651e1f22017-05-28 17:52:54 -0700393err_sriov_unlimit:
394 pci_sriov_set_totalvfs(pf->pdev, 0);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800395err_fw_unload:
396 if (pf->fw_loaded)
397 nfp_fw_unload(pf);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800398 kfree(pf->eth_tbl);
David Bruneczeefbde72017-05-28 17:53:00 -0700399 kfree(pf->nspi);
Simon Horman1851f93f2017-05-26 01:03:32 -0700400err_devlink_unreg:
401 devlink_unregister(devlink);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800402err_cpp_free:
403 nfp_cpp_free(pf->cpp);
404err_disable_msix:
405 pci_set_drvdata(pdev, NULL);
Jakub Kicinski346cfe82017-05-26 01:03:31 -0700406 mutex_destroy(&pf->lock);
Simon Horman1851f93f2017-05-26 01:03:32 -0700407 devlink_free(devlink);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800408err_rel_regions:
409 pci_release_regions(pdev);
410err_pci_disable:
411 pci_disable_device(pdev);
412
413 return err;
414}
415
416static void nfp_pci_remove(struct pci_dev *pdev)
417{
418 struct nfp_pf *pf = pci_get_drvdata(pdev);
Simon Horman1851f93f2017-05-26 01:03:32 -0700419 struct devlink *devlink;
420
David Bruneczeefbde72017-05-28 17:53:00 -0700421 nfp_hwmon_unregister(pf);
422
Simon Horman1851f93f2017-05-26 01:03:32 -0700423 devlink = priv_to_devlink(pf);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800424
Jakub Kicinskid12537d2017-04-04 16:12:24 -0700425 nfp_net_pci_remove(pf);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800426
427 nfp_pcie_sriov_disable(pdev);
Jakub Kicinski651e1f22017-05-28 17:52:54 -0700428 pci_sriov_set_totalvfs(pf->pdev, 0);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800429
Simon Horman1851f93f2017-05-26 01:03:32 -0700430 devlink_unregister(devlink);
431
Jakub Kicinski63461a02017-02-09 09:17:38 -0800432 if (pf->fw_loaded)
433 nfp_fw_unload(pf);
434
435 pci_set_drvdata(pdev, NULL);
436 nfp_cpp_free(pf->cpp);
437
438 kfree(pf->eth_tbl);
David Bruneczeefbde72017-05-28 17:53:00 -0700439 kfree(pf->nspi);
Jakub Kicinski346cfe82017-05-26 01:03:31 -0700440 mutex_destroy(&pf->lock);
Simon Horman1851f93f2017-05-26 01:03:32 -0700441 devlink_free(devlink);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800442 pci_release_regions(pdev);
443 pci_disable_device(pdev);
444}
445
446static struct pci_driver nfp_pci_driver = {
447 .name = nfp_driver_name,
448 .id_table = nfp_pci_device_ids,
449 .probe = nfp_pci_probe,
450 .remove = nfp_pci_remove,
451 .sriov_configure = nfp_pcie_sriov_configure,
452};
453
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800454static int __init nfp_main_init(void)
455{
456 int err;
457
458 pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
459 nfp_driver_name);
460
461 nfp_net_debugfs_create();
462
Jakub Kicinski63461a02017-02-09 09:17:38 -0800463 err = pci_register_driver(&nfp_pci_driver);
464 if (err < 0)
465 goto err_destroy_debugfs;
466
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800467 err = pci_register_driver(&nfp_netvf_pci_driver);
468 if (err)
Jakub Kicinski63461a02017-02-09 09:17:38 -0800469 goto err_unreg_pf;
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800470
471 return err;
472
Jakub Kicinski63461a02017-02-09 09:17:38 -0800473err_unreg_pf:
474 pci_unregister_driver(&nfp_pci_driver);
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800475err_destroy_debugfs:
476 nfp_net_debugfs_destroy();
477 return err;
478}
479
480static void __exit nfp_main_exit(void)
481{
482 pci_unregister_driver(&nfp_netvf_pci_driver);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800483 pci_unregister_driver(&nfp_pci_driver);
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800484 nfp_net_debugfs_destroy();
485}
486
487module_init(nfp_main_init);
488module_exit(nfp_main_exit);
489
Jakub Kicinski63461a02017-02-09 09:17:38 -0800490MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
491MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
492MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
493MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
494MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
495MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
496MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
497MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
498
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800499MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
500MODULE_LICENSE("GPL");
501MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");