blob: 3a131559153abebcd222873dc8d36ee05c3b2b78 [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>
48
Jakub Kicinski63461a02017-02-09 09:17:38 -080049#include "nfpcore/nfp.h"
50#include "nfpcore/nfp_cpp.h"
Jakub Kicinski0bc38272017-02-19 11:58:14 -080051#include "nfpcore/nfp_nffw.h"
Jakub Kicinskice22f5a2017-04-04 16:12:30 -070052#include "nfpcore/nfp_nsp.h"
Jakub Kicinski63461a02017-02-09 09:17:38 -080053
54#include "nfpcore/nfp6000_pcie.h"
55
Jakub Kicinski2633beb2017-02-09 09:17:28 -080056#include "nfp_main.h"
57#include "nfp_net.h"
58
59static const char nfp_driver_name[] = "nfp";
60const char nfp_driver_version[] = VERMAGIC_STRING;
61
Jakub Kicinski63461a02017-02-09 09:17:38 -080062static const struct pci_device_id nfp_pci_device_ids[] = {
Simon Horman3b473522017-02-17 08:57:54 +010063 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
Jakub Kicinski63461a02017-02-09 09:17:38 -080064 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
65 PCI_ANY_ID, 0,
66 },
Simon Horman3b473522017-02-17 08:57:54 +010067 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
Jakub Kicinski63461a02017-02-09 09:17:38 -080068 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
69 PCI_ANY_ID, 0,
70 },
71 { 0, } /* Required last entry. */
72};
73MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
74
Jakub Kicinski0bc38272017-02-19 11:58:14 -080075static void nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
76{
77#ifdef CONFIG_PCI_IOV
78 int err;
79
80 pf->limit_vfs = nfp_rtsym_read_le(pf->cpp, "nfd_vf_cfg_max_vfs", &err);
81 if (!err)
82 return;
83
84 pf->limit_vfs = ~0;
85 /* Allow any setting for backwards compatibility if symbol not found */
86 if (err != -ENOENT)
87 nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
88#endif
89}
90
Jakub Kicinski63461a02017-02-09 09:17:38 -080091static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
92{
93#ifdef CONFIG_PCI_IOV
94 struct nfp_pf *pf = pci_get_drvdata(pdev);
95 int err;
96
Jakub Kicinski0bc38272017-02-19 11:58:14 -080097 if (num_vfs > pf->limit_vfs) {
98 nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
99 pf->limit_vfs);
100 return -EINVAL;
101 }
102
Jakub Kicinski63461a02017-02-09 09:17:38 -0800103 err = pci_enable_sriov(pdev, num_vfs);
104 if (err) {
105 dev_warn(&pdev->dev, "Failed to enable PCI sriov: %d\n", err);
106 return err;
107 }
108
109 pf->num_vfs = num_vfs;
110
111 dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
112
113 return num_vfs;
114#endif
115 return 0;
116}
117
118static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
119{
120#ifdef CONFIG_PCI_IOV
121 struct nfp_pf *pf = pci_get_drvdata(pdev);
122
123 /* If the VFs are assigned we cannot shut down SR-IOV without
124 * causing issues, so just leave the hardware available but
125 * disabled
126 */
127 if (pci_vfs_assigned(pdev)) {
128 dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
129 return -EPERM;
130 }
131
132 pf->num_vfs = 0;
133
134 pci_disable_sriov(pdev);
135 dev_dbg(&pdev->dev, "Removed VFs.\n");
136#endif
137 return 0;
138}
139
140static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
141{
142 if (num_vfs == 0)
143 return nfp_pcie_sriov_disable(pdev);
144 else
145 return nfp_pcie_sriov_enable(pdev, num_vfs);
146}
147
148/**
149 * nfp_net_fw_find() - Find the correct firmware image for netdev mode
150 * @pdev: PCI Device structure
151 * @pf: NFP PF Device structure
152 *
153 * Return: firmware if found and requested successfully.
154 */
155static const struct firmware *
156nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
157{
158 const struct firmware *fw = NULL;
159 struct nfp_eth_table_port *port;
160 const char *fw_model;
161 char fw_name[256];
162 int spc, err = 0;
163 int i, j;
164
165 if (!pf->eth_tbl) {
166 dev_err(&pdev->dev, "Error: can't identify media config\n");
167 return NULL;
168 }
169
170 fw_model = nfp_hwinfo_lookup(pf->cpp, "assembly.partno");
171 if (!fw_model) {
172 dev_err(&pdev->dev, "Error: can't read part number\n");
173 return NULL;
174 }
175
176 spc = ARRAY_SIZE(fw_name);
177 spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
178
179 for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
180 port = &pf->eth_tbl->ports[i];
181 j = 1;
182 while (i + j < pf->eth_tbl->count &&
183 port->speed == port[j].speed)
184 j++;
185
186 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
187 "_%dx%d", j, port->speed / 1000);
188 }
189
190 if (spc <= 0)
191 return NULL;
192
193 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
194 if (spc <= 0)
195 return NULL;
196
197 err = request_firmware(&fw, fw_name, &pdev->dev);
198 if (err)
199 return NULL;
200
201 dev_info(&pdev->dev, "Loading FW image: %s\n", fw_name);
202
203 return fw;
204}
205
206/**
207 * nfp_net_fw_load() - Load the firmware image
208 * @pdev: PCI Device structure
209 * @pf: NFP PF Device structure
210 * @nsp: NFP SP handle
211 *
212 * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
213 */
214static int
215nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
216{
217 const struct firmware *fw;
218 u16 interface;
219 int err;
220
221 interface = nfp_cpp_interface(pf->cpp);
222 if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
223 /* Only Unit 0 should reset or load firmware */
224 dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
225 return 0;
226 }
227
228 fw = nfp_net_fw_find(pdev, pf);
229 if (!fw)
230 return 0;
231
232 dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
233 err = nfp_nsp_device_soft_reset(nsp);
234 if (err < 0) {
235 dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
236 err);
237 goto exit_release_fw;
238 }
239
240 err = nfp_nsp_load_fw(nsp, fw);
241
242 if (err < 0) {
243 dev_err(&pdev->dev, "FW loading failed: %d\n", err);
244 goto exit_release_fw;
245 }
246
247 dev_info(&pdev->dev, "Finished loading FW image\n");
248
249exit_release_fw:
250 release_firmware(fw);
251
252 return err < 0 ? err : 1;
253}
254
Jakub Kicinskia9c83f72017-02-19 11:58:08 -0800255static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
256{
David Brunecz010e2f92017-04-22 20:17:54 -0700257 struct nfp_nsp_identify *nspi;
Jakub Kicinskia9c83f72017-02-19 11:58:08 -0800258 struct nfp_nsp *nsp;
259 int err;
260
261 nsp = nfp_nsp_open(pf->cpp);
262 if (IS_ERR(nsp)) {
263 err = PTR_ERR(nsp);
264 dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
265 return err;
266 }
267
268 err = nfp_nsp_wait(nsp);
269 if (err < 0)
270 goto exit_close_nsp;
271
272 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
273
David Brunecz010e2f92017-04-22 20:17:54 -0700274 nspi = __nfp_nsp_identify(nsp);
275 if (nspi) {
276 dev_info(&pdev->dev, "BSP: %s\n", nspi->version);
277 kfree(nspi);
278 }
279
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{
Jakub Kicinski63461a02017-02-09 09:17:38 -0800319 struct nfp_pf *pf;
320 int err;
321
322 err = pci_enable_device(pdev);
323 if (err < 0)
324 return err;
325
326 pci_set_master(pdev);
327
328 err = dma_set_mask_and_coherent(&pdev->dev,
329 DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
330 if (err)
331 goto err_pci_disable;
332
333 err = pci_request_regions(pdev, nfp_driver_name);
334 if (err < 0) {
335 dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
336 goto err_pci_disable;
337 }
338
339 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
340 if (!pf) {
341 err = -ENOMEM;
342 goto err_rel_regions;
343 }
Jakub Kicinskid4e7f092017-05-22 10:59:24 -0700344 INIT_LIST_HEAD(&pf->vnics);
Jakub Kicinski3eb3b742017-05-22 10:59:31 -0700345 INIT_LIST_HEAD(&pf->ports);
Jakub Kicinski346cfe82017-05-26 01:03:31 -0700346 mutex_init(&pf->lock);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800347 pci_set_drvdata(pdev, pf);
348 pf->pdev = pdev;
349
350 pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
351 if (IS_ERR_OR_NULL(pf->cpp)) {
352 err = PTR_ERR(pf->cpp);
353 if (err >= 0)
354 err = -ENOMEM;
355 goto err_disable_msix;
356 }
357
Jakub Kicinski64db09e2017-02-19 11:58:09 -0800358 dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
359 nfp_hwinfo_lookup(pf->cpp, "assembly.vendor"),
360 nfp_hwinfo_lookup(pf->cpp, "assembly.partno"),
361 nfp_hwinfo_lookup(pf->cpp, "assembly.serial"),
362 nfp_hwinfo_lookup(pf->cpp, "assembly.revision"),
363 nfp_hwinfo_lookup(pf->cpp, "cpld.version"));
364
Jakub Kicinskia9c83f72017-02-19 11:58:08 -0800365 err = nfp_nsp_init(pdev, pf);
366 if (err)
Jakub Kicinski63461a02017-02-09 09:17:38 -0800367 goto err_cpp_free;
Jakub Kicinski63461a02017-02-09 09:17:38 -0800368
Jakub Kicinski0bc38272017-02-19 11:58:14 -0800369 nfp_pcie_sriov_read_nfd_limit(pf);
370
Jakub Kicinski63461a02017-02-09 09:17:38 -0800371 err = nfp_net_pci_probe(pf);
372 if (err)
373 goto err_fw_unload;
374
375 return 0;
376
377err_fw_unload:
378 if (pf->fw_loaded)
379 nfp_fw_unload(pf);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800380 kfree(pf->eth_tbl);
381err_cpp_free:
382 nfp_cpp_free(pf->cpp);
383err_disable_msix:
384 pci_set_drvdata(pdev, NULL);
Jakub Kicinski346cfe82017-05-26 01:03:31 -0700385 mutex_destroy(&pf->lock);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800386 kfree(pf);
387err_rel_regions:
388 pci_release_regions(pdev);
389err_pci_disable:
390 pci_disable_device(pdev);
391
392 return err;
393}
394
395static void nfp_pci_remove(struct pci_dev *pdev)
396{
397 struct nfp_pf *pf = pci_get_drvdata(pdev);
398
Jakub Kicinskid12537d2017-04-04 16:12:24 -0700399 nfp_net_pci_remove(pf);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800400
401 nfp_pcie_sriov_disable(pdev);
402
403 if (pf->fw_loaded)
404 nfp_fw_unload(pf);
405
406 pci_set_drvdata(pdev, NULL);
407 nfp_cpp_free(pf->cpp);
408
409 kfree(pf->eth_tbl);
Jakub Kicinski346cfe82017-05-26 01:03:31 -0700410 mutex_destroy(&pf->lock);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800411 kfree(pf);
412 pci_release_regions(pdev);
413 pci_disable_device(pdev);
414}
415
416static struct pci_driver nfp_pci_driver = {
417 .name = nfp_driver_name,
418 .id_table = nfp_pci_device_ids,
419 .probe = nfp_pci_probe,
420 .remove = nfp_pci_remove,
421 .sriov_configure = nfp_pcie_sriov_configure,
422};
423
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800424static int __init nfp_main_init(void)
425{
426 int err;
427
428 pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
429 nfp_driver_name);
430
431 nfp_net_debugfs_create();
432
Jakub Kicinski63461a02017-02-09 09:17:38 -0800433 err = pci_register_driver(&nfp_pci_driver);
434 if (err < 0)
435 goto err_destroy_debugfs;
436
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800437 err = pci_register_driver(&nfp_netvf_pci_driver);
438 if (err)
Jakub Kicinski63461a02017-02-09 09:17:38 -0800439 goto err_unreg_pf;
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800440
441 return err;
442
Jakub Kicinski63461a02017-02-09 09:17:38 -0800443err_unreg_pf:
444 pci_unregister_driver(&nfp_pci_driver);
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800445err_destroy_debugfs:
446 nfp_net_debugfs_destroy();
447 return err;
448}
449
450static void __exit nfp_main_exit(void)
451{
452 pci_unregister_driver(&nfp_netvf_pci_driver);
Jakub Kicinski63461a02017-02-09 09:17:38 -0800453 pci_unregister_driver(&nfp_pci_driver);
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800454 nfp_net_debugfs_destroy();
455}
456
457module_init(nfp_main_init);
458module_exit(nfp_main_exit);
459
Jakub Kicinski63461a02017-02-09 09:17:38 -0800460MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
461MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
462MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
463MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
464MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
465MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
466MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
467MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
468
Jakub Kicinski2633beb2017-02-09 09:17:28 -0800469MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
470MODULE_LICENSE("GPL");
471MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");