blob: 96266796fd094ee0caae5fefac74c1f91269a9d7 [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>
44#include <linux/pci.h>
45#include <linux/firmware.h>
46#include <linux/vermagic.h>
47
Jakub Kicinski63461a02017-02-09 09:17:38 -080048#include "nfpcore/nfp.h"
49#include "nfpcore/nfp_cpp.h"
Jakub Kicinski0bc38272017-02-19 11:58:14 -080050#include "nfpcore/nfp_nffw.h"
Jakub Kicinski63461a02017-02-09 09:17:38 -080051#include "nfpcore/nfp_nsp_eth.h"
52
53#include "nfpcore/nfp6000_pcie.h"
54
Jakub Kicinski2633beb2017-02-09 09:17:28 -080055#include "nfp_main.h"
56#include "nfp_net.h"
57
58static const char nfp_driver_name[] = "nfp";
59const char nfp_driver_version[] = VERMAGIC_STRING;
60
Jakub Kicinski63461a02017-02-09 09:17:38 -080061static const struct pci_device_id nfp_pci_device_ids[] = {
Simon Horman3b473522017-02-17 08:57:54 +010062 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
Jakub Kicinski63461a02017-02-09 09:17:38 -080063 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
64 PCI_ANY_ID, 0,
65 },
Simon Horman3b473522017-02-17 08:57:54 +010066 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
Jakub Kicinski63461a02017-02-09 09:17:38 -080067 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
68 PCI_ANY_ID, 0,
69 },
70 { 0, } /* Required last entry. */
71};
72MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
73
Jakub Kicinski0bc38272017-02-19 11:58:14 -080074static 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
Jakub Kicinski63461a02017-02-09 09:17:38 -080090static 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
Jakub Kicinski0bc38272017-02-19 11:58:14 -080096 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
Jakub Kicinski63461a02017-02-09 09:17:38 -0800102 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
117static 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
139static 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 */
154static const struct firmware *
155nfp_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 */
213static int
214nfp_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
248exit_release_fw:
249 release_firmware(fw);
250
251 return err < 0 ? err : 1;
252}
253
Jakub Kicinskia9c83f72017-02-19 11:58:08 -0800254static 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
282exit_close_nsp:
283 nfp_nsp_close(nsp);
284
285 return err;
286}
287
Jakub Kicinski63461a02017-02-09 09:17:38 -0800288static 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
308static int nfp_pci_probe(struct pci_dev *pdev,
309 const struct pci_device_id *pci_id)
310{
Jakub Kicinski63461a02017-02-09 09:17:38 -0800311 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
Jakub Kicinski64db09e2017-02-19 11:58:09 -0800348 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
Jakub Kicinskia9c83f72017-02-19 11:58:08 -0800355 err = nfp_nsp_init(pdev, pf);
356 if (err)
Jakub Kicinski63461a02017-02-09 09:17:38 -0800357 goto err_cpp_free;
Jakub Kicinski63461a02017-02-09 09:17:38 -0800358
Jakub Kicinski0bc38272017-02-19 11:58:14 -0800359 nfp_pcie_sriov_read_nfd_limit(pf);
360
Jakub Kicinski63461a02017-02-09 09:17:38 -0800361 err = nfp_net_pci_probe(pf);
362 if (err)
363 goto err_fw_unload;
364
365 return 0;
366
367err_fw_unload:
368 if (pf->fw_loaded)
369 nfp_fw_unload(pf);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800370 kfree(pf->eth_tbl);
371err_cpp_free:
372 nfp_cpp_free(pf->cpp);
373err_disable_msix:
374 pci_set_drvdata(pdev, NULL);
375 kfree(pf);
376err_rel_regions:
377 pci_release_regions(pdev);
378err_pci_disable:
379 pci_disable_device(pdev);
380
381 return err;
382}
383
384static void nfp_pci_remove(struct pci_dev *pdev)
385{
386 struct nfp_pf *pf = pci_get_drvdata(pdev);
387
Jakub Kicinskid12537d2017-04-04 16:12:24 -0700388 nfp_net_pci_remove(pf);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800389
390 nfp_pcie_sriov_disable(pdev);
391
392 if (pf->fw_loaded)
393 nfp_fw_unload(pf);
394
395 pci_set_drvdata(pdev, NULL);
396 nfp_cpp_free(pf->cpp);
397
398 kfree(pf->eth_tbl);
399 kfree(pf);
400 pci_release_regions(pdev);
401 pci_disable_device(pdev);
402}
403
404static struct pci_driver nfp_pci_driver = {
405 .name = nfp_driver_name,
406 .id_table = nfp_pci_device_ids,
407 .probe = nfp_pci_probe,
408 .remove = nfp_pci_remove,
409 .sriov_configure = nfp_pcie_sriov_configure,
410};
411
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800412static int __init nfp_main_init(void)
413{
414 int err;
415
416 pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
417 nfp_driver_name);
418
419 nfp_net_debugfs_create();
420
Jakub Kicinski63461a02017-02-09 09:17:38 -0800421 err = pci_register_driver(&nfp_pci_driver);
422 if (err < 0)
423 goto err_destroy_debugfs;
424
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800425 err = pci_register_driver(&nfp_netvf_pci_driver);
426 if (err)
Jakub Kicinski63461a02017-02-09 09:17:38 -0800427 goto err_unreg_pf;
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800428
429 return err;
430
Jakub Kicinski63461a02017-02-09 09:17:38 -0800431err_unreg_pf:
432 pci_unregister_driver(&nfp_pci_driver);
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800433err_destroy_debugfs:
434 nfp_net_debugfs_destroy();
435 return err;
436}
437
438static void __exit nfp_main_exit(void)
439{
440 pci_unregister_driver(&nfp_netvf_pci_driver);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800441 pci_unregister_driver(&nfp_pci_driver);
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800442 nfp_net_debugfs_destroy();
443}
444
445module_init(nfp_main_init);
446module_exit(nfp_main_exit);
447
Jakub Kicinski63461a02017-02-09 09:17:38 -0800448MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
449MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
450MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
451MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
452MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
453MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
454MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
455MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
456
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800457MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
458MODULE_LICENSE("GPL");
459MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");