1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
3
4 /*
5 * nfp_main.c
6 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
7 * Alejandro Lucero <alejandro.lucero@netronome.com>
8 * Jason McMullan <jason.mcmullan@netronome.com>
9 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/pci.h>
16 #include <linux/firmware.h>
17 #include <linux/vmalloc.h>
18 #include <net/devlink.h>
19
20 #include "nfpcore/nfp.h"
21 #include "nfpcore/nfp_cpp.h"
22 #include "nfpcore/nfp_nffw.h"
23 #include "nfpcore/nfp_nsp.h"
24
25 #include "nfpcore/nfp6000_pcie.h"
26
27 #include "nfp_abi.h"
28 #include "nfp_app.h"
29 #include "nfp_main.h"
30 #include "nfp_net.h"
31
32 static const char nfp_driver_name[] = "nfp";
33
34 static const struct pci_device_id nfp_pci_device_ids[] = {
35 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
36 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
37 PCI_ANY_ID, 0,
38 },
39 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP5000,
40 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
41 PCI_ANY_ID, 0,
42 },
43 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
44 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
45 PCI_ANY_ID, 0,
46 },
47 { 0, } /* Required last entry. */
48 };
49 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
50
nfp_pf_rtsym_read_optional(struct nfp_pf * pf,const char * format,unsigned int default_val)51 int nfp_pf_rtsym_read_optional(struct nfp_pf *pf, const char *format,
52 unsigned int default_val)
53 {
54 char name[256];
55 int err = 0;
56 u64 val;
57
58 snprintf(name, sizeof(name), format, nfp_cppcore_pcie_unit(pf->cpp));
59
60 val = nfp_rtsym_read_le(pf->rtbl, name, &err);
61 if (err) {
62 if (err == -ENOENT)
63 return default_val;
64 nfp_err(pf->cpp, "Unable to read symbol %s\n", name);
65 return err;
66 }
67
68 return val;
69 }
70
71 u8 __iomem *
nfp_pf_map_rtsym(struct nfp_pf * pf,const char * name,const char * sym_fmt,unsigned int min_size,struct nfp_cpp_area ** area)72 nfp_pf_map_rtsym(struct nfp_pf *pf, const char *name, const char *sym_fmt,
73 unsigned int min_size, struct nfp_cpp_area **area)
74 {
75 char pf_symbol[256];
76
77 snprintf(pf_symbol, sizeof(pf_symbol), sym_fmt,
78 nfp_cppcore_pcie_unit(pf->cpp));
79
80 return nfp_rtsym_map(pf->rtbl, pf_symbol, name, min_size, area);
81 }
82
83 /* Callers should hold the devlink instance lock */
nfp_mbox_cmd(struct nfp_pf * pf,u32 cmd,void * in_data,u64 in_length,void * out_data,u64 out_length)84 int nfp_mbox_cmd(struct nfp_pf *pf, u32 cmd, void *in_data, u64 in_length,
85 void *out_data, u64 out_length)
86 {
87 unsigned long err_at;
88 u64 max_data_sz;
89 u32 val = 0;
90 int n, err;
91
92 if (!pf->mbox)
93 return -EOPNOTSUPP;
94
95 max_data_sz = nfp_rtsym_size(pf->mbox) - NFP_MBOX_SYM_MIN_SIZE;
96
97 /* Check if cmd field is clear */
98 err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
99 if (err || val) {
100 nfp_warn(pf->cpp, "failed to issue command (%u): %u, err: %d\n",
101 cmd, val, err);
102 return err ?: -EBUSY;
103 }
104
105 in_length = min(in_length, max_data_sz);
106 n = nfp_rtsym_write(pf->cpp, pf->mbox, NFP_MBOX_DATA, in_data,
107 in_length);
108 if (n != in_length)
109 return -EIO;
110 /* Write data_len and wipe reserved */
111 err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, in_length);
112 if (err)
113 return err;
114
115 /* Read back for ordering */
116 err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
117 if (err)
118 return err;
119
120 /* Write cmd and wipe return value */
121 err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_CMD, cmd);
122 if (err)
123 return err;
124
125 err_at = jiffies + 5 * HZ;
126 while (true) {
127 /* Wait for command to go to 0 (NFP_MBOX_NO_CMD) */
128 err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
129 if (err)
130 return err;
131 if (!val)
132 break;
133
134 if (time_is_before_eq_jiffies(err_at))
135 return -ETIMEDOUT;
136
137 msleep(5);
138 }
139
140 /* Copy output if any (could be error info, do it before reading ret) */
141 err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
142 if (err)
143 return err;
144
145 out_length = min_t(u32, val, min(out_length, max_data_sz));
146 n = nfp_rtsym_read(pf->cpp, pf->mbox, NFP_MBOX_DATA,
147 out_data, out_length);
148 if (n != out_length)
149 return -EIO;
150
151 /* Check if there is an error */
152 err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_RET, &val);
153 if (err)
154 return err;
155 if (val)
156 return -val;
157
158 return out_length;
159 }
160
nfp_board_ready(struct nfp_pf * pf)161 static bool nfp_board_ready(struct nfp_pf *pf)
162 {
163 const char *cp;
164 long state;
165 int err;
166
167 cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
168 if (!cp)
169 return false;
170
171 err = kstrtol(cp, 0, &state);
172 if (err < 0)
173 return false;
174
175 return state == 15;
176 }
177
nfp_pf_board_state_wait(struct nfp_pf * pf)178 static int nfp_pf_board_state_wait(struct nfp_pf *pf)
179 {
180 const unsigned long wait_until = jiffies + 10 * HZ;
181
182 while (!nfp_board_ready(pf)) {
183 if (time_is_before_eq_jiffies(wait_until)) {
184 nfp_err(pf->cpp, "NFP board initialization timeout\n");
185 return -EINVAL;
186 }
187
188 nfp_info(pf->cpp, "waiting for board initialization\n");
189 if (msleep_interruptible(500))
190 return -ERESTARTSYS;
191
192 /* Refresh cached information */
193 kfree(pf->hwinfo);
194 pf->hwinfo = nfp_hwinfo_read(pf->cpp);
195 }
196
197 return 0;
198 }
199
nfp_pcie_sriov_read_nfd_limit(struct nfp_pf * pf)200 static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
201 {
202 int err;
203
204 pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
205 if (err) {
206 /* For backwards compatibility if symbol not found allow all */
207 pf->limit_vfs = ~0;
208 if (err == -ENOENT)
209 return 0;
210
211 nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
212 return err;
213 }
214
215 err = pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
216 if (err)
217 nfp_warn(pf->cpp, "Failed to set VF count in sysfs: %d\n", err);
218 return 0;
219 }
220
nfp_pcie_sriov_enable(struct pci_dev * pdev,int num_vfs)221 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
222 {
223 #ifdef CONFIG_PCI_IOV
224 struct nfp_pf *pf = pci_get_drvdata(pdev);
225 int err;
226
227 if (num_vfs > pf->limit_vfs) {
228 nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
229 pf->limit_vfs);
230 return -EINVAL;
231 }
232
233 err = pci_enable_sriov(pdev, num_vfs);
234 if (err) {
235 dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
236 return err;
237 }
238
239 mutex_lock(&pf->lock);
240
241 err = nfp_app_sriov_enable(pf->app, num_vfs);
242 if (err) {
243 dev_warn(&pdev->dev,
244 "App specific PCI SR-IOV configuration failed: %d\n",
245 err);
246 goto err_sriov_disable;
247 }
248
249 pf->num_vfs = num_vfs;
250
251 dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
252
253 mutex_unlock(&pf->lock);
254 return num_vfs;
255
256 err_sriov_disable:
257 mutex_unlock(&pf->lock);
258 pci_disable_sriov(pdev);
259 return err;
260 #endif
261 return 0;
262 }
263
nfp_pcie_sriov_disable(struct pci_dev * pdev)264 static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
265 {
266 #ifdef CONFIG_PCI_IOV
267 struct nfp_pf *pf = pci_get_drvdata(pdev);
268
269 mutex_lock(&pf->lock);
270
271 /* If the VFs are assigned we cannot shut down SR-IOV without
272 * causing issues, so just leave the hardware available but
273 * disabled
274 */
275 if (pci_vfs_assigned(pdev)) {
276 dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
277 mutex_unlock(&pf->lock);
278 return -EPERM;
279 }
280
281 nfp_app_sriov_disable(pf->app);
282
283 pf->num_vfs = 0;
284
285 mutex_unlock(&pf->lock);
286
287 pci_disable_sriov(pdev);
288 dev_dbg(&pdev->dev, "Removed VFs.\n");
289 #endif
290 return 0;
291 }
292
nfp_pcie_sriov_configure(struct pci_dev * pdev,int num_vfs)293 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
294 {
295 if (!pci_get_drvdata(pdev))
296 return -ENOENT;
297
298 if (num_vfs == 0)
299 return nfp_pcie_sriov_disable(pdev);
300 else
301 return nfp_pcie_sriov_enable(pdev, num_vfs);
302 }
303
nfp_flash_update_common(struct nfp_pf * pf,const char * path,struct netlink_ext_ack * extack)304 int nfp_flash_update_common(struct nfp_pf *pf, const char *path,
305 struct netlink_ext_ack *extack)
306 {
307 struct device *dev = &pf->pdev->dev;
308 const struct firmware *fw;
309 struct nfp_nsp *nsp;
310 int err;
311
312 nsp = nfp_nsp_open(pf->cpp);
313 if (IS_ERR(nsp)) {
314 err = PTR_ERR(nsp);
315 if (extack)
316 NL_SET_ERR_MSG_MOD(extack, "can't access NSP");
317 else
318 dev_err(dev, "Failed to access the NSP: %d\n", err);
319 return err;
320 }
321
322 err = request_firmware_direct(&fw, path, dev);
323 if (err) {
324 NL_SET_ERR_MSG_MOD(extack,
325 "unable to read flash file from disk");
326 goto exit_close_nsp;
327 }
328
329 dev_info(dev, "Please be patient while writing flash image: %s\n",
330 path);
331
332 err = nfp_nsp_write_flash(nsp, fw);
333 if (err < 0)
334 goto exit_release_fw;
335 dev_info(dev, "Finished writing flash image\n");
336 err = 0;
337
338 exit_release_fw:
339 release_firmware(fw);
340 exit_close_nsp:
341 nfp_nsp_close(nsp);
342 return err;
343 }
344
345 static const struct firmware *
nfp_net_fw_request(struct pci_dev * pdev,struct nfp_pf * pf,const char * name)346 nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name)
347 {
348 const struct firmware *fw = NULL;
349 int err;
350
351 err = request_firmware_direct(&fw, name, &pdev->dev);
352 nfp_info(pf->cpp, " %s: %s\n",
353 name, err ? "not found" : "found");
354 if (err)
355 return NULL;
356
357 return fw;
358 }
359
360 /**
361 * nfp_net_fw_find() - Find the correct firmware image for netdev mode
362 * @pdev: PCI Device structure
363 * @pf: NFP PF Device structure
364 *
365 * Return: firmware if found and requested successfully.
366 */
367 static const struct firmware *
nfp_net_fw_find(struct pci_dev * pdev,struct nfp_pf * pf)368 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
369 {
370 struct nfp_eth_table_port *port;
371 const struct firmware *fw;
372 const char *fw_model;
373 char fw_name[256];
374 const u8 *serial;
375 u16 interface;
376 int spc, i, j;
377
378 nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n");
379
380 /* First try to find a firmware image specific for this device */
381 interface = nfp_cpp_interface(pf->cpp);
382 nfp_cpp_serial(pf->cpp, &serial);
383 sprintf(fw_name, "netronome/serial-%pMF-%02hhx-%02hhx.nffw",
384 serial, interface >> 8, interface & 0xff);
385 fw = nfp_net_fw_request(pdev, pf, fw_name);
386 if (fw)
387 return fw;
388
389 /* Then try the PCI name */
390 sprintf(fw_name, "netronome/pci-%s.nffw", pci_name(pdev));
391 fw = nfp_net_fw_request(pdev, pf, fw_name);
392 if (fw)
393 return fw;
394
395 /* Finally try the card type and media */
396 if (!pf->eth_tbl) {
397 dev_err(&pdev->dev, "Error: can't identify media config\n");
398 return NULL;
399 }
400
401 fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
402 if (!fw_model) {
403 dev_err(&pdev->dev, "Error: can't read part number\n");
404 return NULL;
405 }
406
407 spc = ARRAY_SIZE(fw_name);
408 spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
409
410 for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
411 port = &pf->eth_tbl->ports[i];
412 j = 1;
413 while (i + j < pf->eth_tbl->count &&
414 port->speed == port[j].speed)
415 j++;
416
417 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
418 "_%dx%d", j, port->speed / 1000);
419 }
420
421 if (spc <= 0)
422 return NULL;
423
424 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
425 if (spc <= 0)
426 return NULL;
427
428 return nfp_net_fw_request(pdev, pf, fw_name);
429 }
430
431 static int
nfp_get_fw_policy_value(struct pci_dev * pdev,struct nfp_nsp * nsp,const char * key,const char * default_val,int max_val,int * value)432 nfp_get_fw_policy_value(struct pci_dev *pdev, struct nfp_nsp *nsp,
433 const char *key, const char *default_val, int max_val,
434 int *value)
435 {
436 char hwinfo[64];
437 long hi_val;
438 int err;
439
440 snprintf(hwinfo, sizeof(hwinfo), key);
441 err = nfp_nsp_hwinfo_lookup_optional(nsp, hwinfo, sizeof(hwinfo),
442 default_val);
443 if (err)
444 return err;
445
446 err = kstrtol(hwinfo, 0, &hi_val);
447 if (err || hi_val < 0 || hi_val > max_val) {
448 dev_warn(&pdev->dev,
449 "Invalid value '%s' from '%s', ignoring\n",
450 hwinfo, key);
451 err = kstrtol(default_val, 0, &hi_val);
452 }
453
454 *value = hi_val;
455 return err;
456 }
457
458 /**
459 * nfp_fw_load() - Load the firmware image
460 * @pdev: PCI Device structure
461 * @pf: NFP PF Device structure
462 * @nsp: NFP SP handle
463 *
464 * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
465 */
466 static int
nfp_fw_load(struct pci_dev * pdev,struct nfp_pf * pf,struct nfp_nsp * nsp)467 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
468 {
469 bool do_reset, fw_loaded = false;
470 const struct firmware *fw = NULL;
471 int err, reset, policy, ifcs = 0;
472 char *token, *ptr;
473 char hwinfo[64];
474 u16 interface;
475
476 snprintf(hwinfo, sizeof(hwinfo), "abi_drv_load_ifc");
477 err = nfp_nsp_hwinfo_lookup_optional(nsp, hwinfo, sizeof(hwinfo),
478 NFP_NSP_DRV_LOAD_IFC_DEFAULT);
479 if (err)
480 return err;
481
482 interface = nfp_cpp_interface(pf->cpp);
483 ptr = hwinfo;
484 while ((token = strsep(&ptr, ","))) {
485 unsigned long interface_hi;
486
487 err = kstrtoul(token, 0, &interface_hi);
488 if (err) {
489 dev_err(&pdev->dev,
490 "Failed to parse interface '%s': %d\n",
491 token, err);
492 return err;
493 }
494
495 ifcs++;
496 if (interface == interface_hi)
497 break;
498 }
499
500 if (!token) {
501 dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
502 return 0;
503 }
504
505 err = nfp_get_fw_policy_value(pdev, nsp, "abi_drv_reset",
506 NFP_NSP_DRV_RESET_DEFAULT,
507 NFP_NSP_DRV_RESET_NEVER, &reset);
508 if (err)
509 return err;
510
511 err = nfp_get_fw_policy_value(pdev, nsp, "app_fw_from_flash",
512 NFP_NSP_APP_FW_LOAD_DEFAULT,
513 NFP_NSP_APP_FW_LOAD_PREF, &policy);
514 if (err)
515 return err;
516
517 fw = nfp_net_fw_find(pdev, pf);
518 do_reset = reset == NFP_NSP_DRV_RESET_ALWAYS ||
519 (fw && reset == NFP_NSP_DRV_RESET_DISK);
520
521 if (do_reset) {
522 dev_info(&pdev->dev, "Soft-resetting the NFP\n");
523 err = nfp_nsp_device_soft_reset(nsp);
524 if (err < 0) {
525 dev_err(&pdev->dev,
526 "Failed to soft reset the NFP: %d\n", err);
527 goto exit_release_fw;
528 }
529 }
530
531 if (fw && policy != NFP_NSP_APP_FW_LOAD_FLASH) {
532 if (nfp_nsp_has_fw_loaded(nsp) && nfp_nsp_fw_loaded(nsp))
533 goto exit_release_fw;
534
535 err = nfp_nsp_load_fw(nsp, fw);
536 if (err < 0) {
537 dev_err(&pdev->dev, "FW loading failed: %d\n",
538 err);
539 goto exit_release_fw;
540 }
541 dev_info(&pdev->dev, "Finished loading FW image\n");
542 fw_loaded = true;
543 } else if (policy != NFP_NSP_APP_FW_LOAD_DISK &&
544 nfp_nsp_has_stored_fw_load(nsp)) {
545
546 /* Don't propagate this error to stick with legacy driver
547 * behavior, failure will be detected later during init.
548 */
549 if (!nfp_nsp_load_stored_fw(nsp))
550 dev_info(&pdev->dev, "Finished loading stored FW image\n");
551
552 /* Don't flag the fw_loaded in this case since other devices
553 * may reuse the firmware when configured this way
554 */
555 } else {
556 dev_warn(&pdev->dev, "Didn't load firmware, please update flash or reconfigure card\n");
557 }
558
559 exit_release_fw:
560 release_firmware(fw);
561
562 /* We don't want to unload firmware when other devices may still be
563 * dependent on it, which could be the case if there are multiple
564 * devices that could load firmware.
565 */
566 if (fw_loaded && ifcs == 1)
567 pf->unload_fw_on_remove = true;
568
569 return err < 0 ? err : fw_loaded;
570 }
571
572 static void
nfp_nsp_init_ports(struct pci_dev * pdev,struct nfp_pf * pf,struct nfp_nsp * nsp)573 nfp_nsp_init_ports(struct pci_dev *pdev, struct nfp_pf *pf,
574 struct nfp_nsp *nsp)
575 {
576 bool needs_reinit = false;
577 int i;
578
579 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
580 if (!pf->eth_tbl)
581 return;
582
583 if (!nfp_nsp_has_mac_reinit(nsp))
584 return;
585
586 for (i = 0; i < pf->eth_tbl->count; i++)
587 needs_reinit |= pf->eth_tbl->ports[i].override_changed;
588 if (!needs_reinit)
589 return;
590
591 kfree(pf->eth_tbl);
592 if (nfp_nsp_mac_reinit(nsp))
593 dev_warn(&pdev->dev, "MAC reinit failed\n");
594
595 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
596 }
597
nfp_nsp_init(struct pci_dev * pdev,struct nfp_pf * pf)598 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
599 {
600 struct nfp_nsp *nsp;
601 int err;
602
603 err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
604 if (err)
605 return err;
606
607 nsp = nfp_nsp_open(pf->cpp);
608 if (IS_ERR(nsp)) {
609 err = PTR_ERR(nsp);
610 dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
611 return err;
612 }
613
614 err = nfp_nsp_wait(nsp);
615 if (err < 0)
616 goto exit_close_nsp;
617
618 nfp_nsp_init_ports(pdev, pf, nsp);
619
620 pf->nspi = __nfp_nsp_identify(nsp);
621 if (pf->nspi)
622 dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
623
624 err = nfp_fw_load(pdev, pf, nsp);
625 if (err < 0) {
626 kfree(pf->nspi);
627 kfree(pf->eth_tbl);
628 dev_err(&pdev->dev, "Failed to load FW\n");
629 goto exit_close_nsp;
630 }
631
632 pf->fw_loaded = !!err;
633 err = 0;
634
635 exit_close_nsp:
636 nfp_nsp_close(nsp);
637
638 return err;
639 }
640
nfp_fw_unload(struct nfp_pf * pf)641 static void nfp_fw_unload(struct nfp_pf *pf)
642 {
643 struct nfp_nsp *nsp;
644 int err;
645
646 nsp = nfp_nsp_open(pf->cpp);
647 if (IS_ERR(nsp)) {
648 nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
649 return;
650 }
651
652 err = nfp_nsp_device_soft_reset(nsp);
653 if (err < 0)
654 dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
655 else
656 dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
657
658 nfp_nsp_close(nsp);
659 }
660
nfp_pf_find_rtsyms(struct nfp_pf * pf)661 static int nfp_pf_find_rtsyms(struct nfp_pf *pf)
662 {
663 char pf_symbol[256];
664 unsigned int pf_id;
665
666 pf_id = nfp_cppcore_pcie_unit(pf->cpp);
667
668 /* Optional per-PCI PF mailbox */
669 snprintf(pf_symbol, sizeof(pf_symbol), NFP_MBOX_SYM_NAME, pf_id);
670 pf->mbox = nfp_rtsym_lookup(pf->rtbl, pf_symbol);
671 if (pf->mbox && nfp_rtsym_size(pf->mbox) < NFP_MBOX_SYM_MIN_SIZE) {
672 nfp_err(pf->cpp, "PF mailbox symbol too small: %llu < %d\n",
673 nfp_rtsym_size(pf->mbox), NFP_MBOX_SYM_MIN_SIZE);
674 return -EINVAL;
675 }
676
677 return 0;
678 }
679
nfp_pci_probe(struct pci_dev * pdev,const struct pci_device_id * pci_id)680 static int nfp_pci_probe(struct pci_dev *pdev,
681 const struct pci_device_id *pci_id)
682 {
683 struct devlink *devlink;
684 struct nfp_pf *pf;
685 int err;
686
687 if (pdev->vendor == PCI_VENDOR_ID_NETRONOME &&
688 pdev->device == PCI_DEVICE_ID_NETRONOME_NFP6000_VF)
689 dev_warn(&pdev->dev, "Binding NFP VF device to the NFP PF driver, the VF driver is called 'nfp_netvf'\n");
690
691 err = pci_enable_device(pdev);
692 if (err < 0)
693 return err;
694
695 pci_set_master(pdev);
696
697 err = dma_set_mask_and_coherent(&pdev->dev,
698 DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
699 if (err)
700 goto err_pci_disable;
701
702 err = pci_request_regions(pdev, nfp_driver_name);
703 if (err < 0) {
704 dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
705 goto err_pci_disable;
706 }
707
708 devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf));
709 if (!devlink) {
710 err = -ENOMEM;
711 goto err_rel_regions;
712 }
713 pf = devlink_priv(devlink);
714 INIT_LIST_HEAD(&pf->vnics);
715 INIT_LIST_HEAD(&pf->ports);
716 mutex_init(&pf->lock);
717 pci_set_drvdata(pdev, pf);
718 pf->pdev = pdev;
719
720 pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
721 if (!pf->wq) {
722 err = -ENOMEM;
723 goto err_pci_priv_unset;
724 }
725
726 pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
727 if (IS_ERR_OR_NULL(pf->cpp)) {
728 err = PTR_ERR(pf->cpp);
729 if (err >= 0)
730 err = -ENOMEM;
731 goto err_disable_msix;
732 }
733
734 err = nfp_resource_table_init(pf->cpp);
735 if (err)
736 goto err_cpp_free;
737
738 pf->hwinfo = nfp_hwinfo_read(pf->cpp);
739
740 dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
741 nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"),
742 nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"),
743 nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"),
744 nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"),
745 nfp_hwinfo_lookup(pf->hwinfo, "cpld.version"));
746
747 err = nfp_pf_board_state_wait(pf);
748 if (err)
749 goto err_hwinfo_free;
750
751 err = nfp_nsp_init(pdev, pf);
752 if (err)
753 goto err_hwinfo_free;
754
755 pf->mip = nfp_mip_open(pf->cpp);
756 pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
757
758 err = nfp_pf_find_rtsyms(pf);
759 if (err)
760 goto err_fw_unload;
761
762 pf->dump_flag = NFP_DUMP_NSP_DIAG;
763 pf->dumpspec = nfp_net_dump_load_dumpspec(pf->cpp, pf->rtbl);
764
765 err = nfp_pcie_sriov_read_nfd_limit(pf);
766 if (err)
767 goto err_fw_unload;
768
769 pf->num_vfs = pci_num_vf(pdev);
770 if (pf->num_vfs > pf->limit_vfs) {
771 dev_err(&pdev->dev,
772 "Error: %d VFs already enabled, but loaded FW can only support %d\n",
773 pf->num_vfs, pf->limit_vfs);
774 err = -EINVAL;
775 goto err_fw_unload;
776 }
777
778 err = nfp_net_pci_probe(pf);
779 if (err)
780 goto err_fw_unload;
781
782 err = nfp_hwmon_register(pf);
783 if (err) {
784 dev_err(&pdev->dev, "Failed to register hwmon info\n");
785 goto err_net_remove;
786 }
787
788 return 0;
789
790 err_net_remove:
791 nfp_net_pci_remove(pf);
792 err_fw_unload:
793 kfree(pf->rtbl);
794 nfp_mip_close(pf->mip);
795 if (pf->unload_fw_on_remove)
796 nfp_fw_unload(pf);
797 kfree(pf->eth_tbl);
798 kfree(pf->nspi);
799 vfree(pf->dumpspec);
800 err_hwinfo_free:
801 kfree(pf->hwinfo);
802 err_cpp_free:
803 nfp_cpp_free(pf->cpp);
804 err_disable_msix:
805 destroy_workqueue(pf->wq);
806 err_pci_priv_unset:
807 pci_set_drvdata(pdev, NULL);
808 mutex_destroy(&pf->lock);
809 devlink_free(devlink);
810 err_rel_regions:
811 pci_release_regions(pdev);
812 err_pci_disable:
813 pci_disable_device(pdev);
814
815 return err;
816 }
817
__nfp_pci_shutdown(struct pci_dev * pdev,bool unload_fw)818 static void __nfp_pci_shutdown(struct pci_dev *pdev, bool unload_fw)
819 {
820 struct nfp_pf *pf;
821
822 pf = pci_get_drvdata(pdev);
823 if (!pf)
824 return;
825
826 nfp_hwmon_unregister(pf);
827
828 nfp_pcie_sriov_disable(pdev);
829
830 nfp_net_pci_remove(pf);
831
832 vfree(pf->dumpspec);
833 kfree(pf->rtbl);
834 nfp_mip_close(pf->mip);
835 if (unload_fw && pf->unload_fw_on_remove)
836 nfp_fw_unload(pf);
837
838 destroy_workqueue(pf->wq);
839 pci_set_drvdata(pdev, NULL);
840 kfree(pf->hwinfo);
841 nfp_cpp_free(pf->cpp);
842
843 kfree(pf->eth_tbl);
844 kfree(pf->nspi);
845 mutex_destroy(&pf->lock);
846 devlink_free(priv_to_devlink(pf));
847 pci_release_regions(pdev);
848 pci_disable_device(pdev);
849 }
850
nfp_pci_remove(struct pci_dev * pdev)851 static void nfp_pci_remove(struct pci_dev *pdev)
852 {
853 __nfp_pci_shutdown(pdev, true);
854 }
855
nfp_pci_shutdown(struct pci_dev * pdev)856 static void nfp_pci_shutdown(struct pci_dev *pdev)
857 {
858 __nfp_pci_shutdown(pdev, false);
859 }
860
861 static struct pci_driver nfp_pci_driver = {
862 .name = nfp_driver_name,
863 .id_table = nfp_pci_device_ids,
864 .probe = nfp_pci_probe,
865 .remove = nfp_pci_remove,
866 .shutdown = nfp_pci_shutdown,
867 .sriov_configure = nfp_pcie_sriov_configure,
868 };
869
nfp_main_init(void)870 static int __init nfp_main_init(void)
871 {
872 int err;
873
874 pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
875 nfp_driver_name);
876
877 nfp_net_debugfs_create();
878
879 err = pci_register_driver(&nfp_pci_driver);
880 if (err < 0)
881 goto err_destroy_debugfs;
882
883 err = pci_register_driver(&nfp_netvf_pci_driver);
884 if (err)
885 goto err_unreg_pf;
886
887 return err;
888
889 err_unreg_pf:
890 pci_unregister_driver(&nfp_pci_driver);
891 err_destroy_debugfs:
892 nfp_net_debugfs_destroy();
893 return err;
894 }
895
nfp_main_exit(void)896 static void __exit nfp_main_exit(void)
897 {
898 pci_unregister_driver(&nfp_netvf_pci_driver);
899 pci_unregister_driver(&nfp_pci_driver);
900 nfp_net_debugfs_destroy();
901 }
902
903 module_init(nfp_main_init);
904 module_exit(nfp_main_exit);
905
906 MODULE_FIRMWARE("netronome/nic_AMDA0058-0011_2x40.nffw");
907 MODULE_FIRMWARE("netronome/nic_AMDA0058-0012_2x40.nffw");
908 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
909 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
910 MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
911 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
912 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
913 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
914 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
915 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
916 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_1x10_1x25.nffw");
917
918 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
919 MODULE_LICENSE("GPL");
920 MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");
921