8f5a240c8d2f5d9f2418cb9a2ee850daf2a51a6b
[linux-2.6-block.git] / drivers / net / ethernet / netronome / nfp / nfp_net_main.c
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_net_main.c
36  * Netronome network device driver: Main entry point
37  * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
38  *          Alejandro Lucero <alejandro.lucero@netronome.com>
39  *          Jason McMullan <jason.mcmullan@netronome.com>
40  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
41  */
42
43 #include <linux/etherdevice.h>
44 #include <linux/kernel.h>
45 #include <linux/init.h>
46 #include <linux/pci.h>
47 #include <linux/pci_regs.h>
48 #include <linux/msi.h>
49 #include <linux/random.h>
50 #include <linux/rtnetlink.h>
51
52 #include "nfpcore/nfp.h"
53 #include "nfpcore/nfp_cpp.h"
54 #include "nfpcore/nfp_nffw.h"
55 #include "nfpcore/nfp_nsp.h"
56 #include "nfpcore/nfp6000_pcie.h"
57 #include "nfp_app.h"
58 #include "nfp_net_ctrl.h"
59 #include "nfp_net.h"
60 #include "nfp_main.h"
61 #include "nfp_port.h"
62
63 #define NFP_PF_CSR_SLICE_SIZE   (32 * 1024)
64
65 static int nfp_is_ready(struct nfp_cpp *cpp)
66 {
67         const char *cp;
68         long state;
69         int err;
70
71         cp = nfp_hwinfo_lookup(cpp, "board.state");
72         if (!cp)
73                 return 0;
74
75         err = kstrtol(cp, 0, &state);
76         if (err < 0)
77                 return 0;
78
79         return state == 15;
80 }
81
82 /**
83  * nfp_net_map_area() - Help function to map an area
84  * @cpp:    NFP CPP handler
85  * @name:   Name for the area
86  * @target: CPP target
87  * @addr:   CPP address
88  * @size:   Size of the area
89  * @area:   Area handle (returned).
90  *
91  * This function is primarily to simplify the code in the main probe
92  * function. To undo the effect of this functions call
93  * @nfp_cpp_area_release_free(*area);
94  *
95  * Return: Pointer to memory mapped area or ERR_PTR
96  */
97 static u8 __iomem *nfp_net_map_area(struct nfp_cpp *cpp,
98                                     const char *name, int isl, int target,
99                                     unsigned long long addr, unsigned long size,
100                                     struct nfp_cpp_area **area)
101 {
102         u8 __iomem *res;
103         u32 dest;
104         int err;
105
106         dest = NFP_CPP_ISLAND_ID(target, NFP_CPP_ACTION_RW, 0, isl);
107
108         *area = nfp_cpp_area_alloc_with_name(cpp, dest, name, addr, size);
109         if (!*area) {
110                 err = -EIO;
111                 goto err_area;
112         }
113
114         err = nfp_cpp_area_acquire(*area);
115         if (err < 0)
116                 goto err_acquire;
117
118         res = nfp_cpp_area_iomem(*area);
119         if (!res) {
120                 err = -EIO;
121                 goto err_map;
122         }
123
124         return res;
125
126 err_map:
127         nfp_cpp_area_release(*area);
128 err_acquire:
129         nfp_cpp_area_free(*area);
130 err_area:
131         return (u8 __iomem *)ERR_PTR(err);
132 }
133
134 /**
135  * nfp_net_get_mac_addr() - Get the MAC address.
136  * @nn:       NFP Network structure
137  * @cpp:      NFP CPP handle
138  * @id:       NFP port id
139  *
140  * First try to get the MAC address from NSP ETH table. If that
141  * fails try HWInfo.  As a last resort generate a random address.
142  */
143 static void
144 nfp_net_get_mac_addr(struct nfp_net *nn, struct nfp_cpp *cpp, unsigned int id)
145 {
146         struct nfp_eth_table_port *eth_port;
147         struct nfp_net_dp *dp = &nn->dp;
148         u8 mac_addr[ETH_ALEN];
149         const char *mac_str;
150         char name[32];
151
152         eth_port = __nfp_port_get_eth_port(nn->port);
153         if (eth_port) {
154                 ether_addr_copy(dp->netdev->dev_addr, eth_port->mac_addr);
155                 ether_addr_copy(dp->netdev->perm_addr, eth_port->mac_addr);
156                 return;
157         }
158
159         snprintf(name, sizeof(name), "eth%d.mac", id);
160
161         mac_str = nfp_hwinfo_lookup(cpp, name);
162         if (!mac_str) {
163                 dev_warn(dp->dev, "Can't lookup MAC address. Generate\n");
164                 eth_hw_addr_random(dp->netdev);
165                 return;
166         }
167
168         if (sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
169                    &mac_addr[0], &mac_addr[1], &mac_addr[2],
170                    &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6) {
171                 dev_warn(dp->dev,
172                          "Can't parse MAC address (%s). Generate.\n", mac_str);
173                 eth_hw_addr_random(dp->netdev);
174                 return;
175         }
176
177         ether_addr_copy(dp->netdev->dev_addr, mac_addr);
178         ether_addr_copy(dp->netdev->perm_addr, mac_addr);
179 }
180
181 static struct nfp_eth_table_port *
182 nfp_net_find_port(struct nfp_eth_table *eth_tbl, unsigned int id)
183 {
184         int i;
185
186         for (i = 0; eth_tbl && i < eth_tbl->count; i++)
187                 if (eth_tbl->ports[i].eth_index == id)
188                         return &eth_tbl->ports[i];
189
190         return NULL;
191 }
192
193 static unsigned int nfp_net_pf_get_num_ports(struct nfp_pf *pf)
194 {
195         char name[256];
196         int err = 0;
197         u64 val;
198
199         snprintf(name, sizeof(name), "nfd_cfg_pf%u_num_ports",
200                  nfp_cppcore_pcie_unit(pf->cpp));
201
202         val = nfp_rtsym_read_le(pf->cpp, name, &err);
203         /* Default to one port/vNIC */
204         if (err) {
205                 if (err != -ENOENT)
206                         nfp_err(pf->cpp, "Unable to read adapter vNIC count\n");
207                 val = 1;
208         }
209
210         return val;
211 }
212
213 static unsigned int
214 nfp_net_pf_total_qcs(struct nfp_pf *pf, void __iomem *ctrl_bar,
215                      unsigned int stride, u32 start_off, u32 num_off)
216 {
217         unsigned int i, min_qc, max_qc;
218
219         min_qc = readl(ctrl_bar + start_off);
220         max_qc = min_qc;
221
222         for (i = 0; i < pf->max_data_vnics; i++) {
223                 /* To make our lives simpler only accept configuration where
224                  * queues are allocated to PFs in order (queues of PFn all have
225                  * indexes lower than PFn+1).
226                  */
227                 if (max_qc > readl(ctrl_bar + start_off))
228                         return 0;
229
230                 max_qc = readl(ctrl_bar + start_off);
231                 max_qc += readl(ctrl_bar + num_off) * stride;
232                 ctrl_bar += NFP_PF_CSR_SLICE_SIZE;
233         }
234
235         return max_qc - min_qc;
236 }
237
238 static u8 __iomem *nfp_net_pf_map_ctrl_bar(struct nfp_pf *pf)
239 {
240         const struct nfp_rtsym *ctrl_sym;
241         u8 __iomem *ctrl_bar;
242         char pf_symbol[256];
243
244         snprintf(pf_symbol, sizeof(pf_symbol), "_pf%u_net_bar0",
245                  nfp_cppcore_pcie_unit(pf->cpp));
246
247         ctrl_sym = nfp_rtsym_lookup(pf->cpp, pf_symbol);
248         if (!ctrl_sym) {
249                 dev_err(&pf->pdev->dev,
250                         "Failed to find PF BAR0 symbol %s\n", pf_symbol);
251                 return NULL;
252         }
253
254         if (ctrl_sym->size < pf->max_data_vnics * NFP_PF_CSR_SLICE_SIZE) {
255                 dev_err(&pf->pdev->dev,
256                         "PF BAR0 too small to contain %d vNICs\n",
257                         pf->max_data_vnics);
258                 return NULL;
259         }
260
261         ctrl_bar = nfp_net_map_area(pf->cpp, "net.ctrl",
262                                     ctrl_sym->domain, ctrl_sym->target,
263                                     ctrl_sym->addr, ctrl_sym->size,
264                                     &pf->data_vnic_bar);
265         if (IS_ERR(ctrl_bar)) {
266                 dev_err(&pf->pdev->dev, "Failed to map PF BAR0: %ld\n",
267                         PTR_ERR(ctrl_bar));
268                 return NULL;
269         }
270
271         return ctrl_bar;
272 }
273
274 static void nfp_net_pf_free_vnic(struct nfp_pf *pf, struct nfp_net *nn)
275 {
276         nfp_port_free(nn->port);
277         list_del(&nn->vnic_list);
278         pf->num_vnics--;
279         nfp_net_free(nn);
280 }
281
282 static void nfp_net_pf_free_vnics(struct nfp_pf *pf)
283 {
284         struct nfp_net *nn;
285
286         while (!list_empty(&pf->vnics)) {
287                 nn = list_first_entry(&pf->vnics, struct nfp_net, vnic_list);
288                 nfp_net_pf_free_vnic(pf, nn);
289         }
290 }
291
292 static struct nfp_net *
293 nfp_net_pf_alloc_vnic(struct nfp_pf *pf, void __iomem *ctrl_bar,
294                       void __iomem *tx_bar, void __iomem *rx_bar,
295                       int stride, struct nfp_net_fw_version *fw_ver,
296                       unsigned int eth_id)
297 {
298         struct nfp_eth_table_port *eth_port;
299         u32 n_tx_rings, n_rx_rings;
300         struct nfp_net *nn;
301
302         n_tx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_TXRINGS);
303         n_rx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_RXRINGS);
304
305         /* Allocate and initialise the vNIC */
306         nn = nfp_net_alloc(pf->pdev, n_tx_rings, n_rx_rings);
307         if (IS_ERR(nn))
308                 return nn;
309
310         nn->app = pf->app;
311         nn->fw_ver = *fw_ver;
312         nn->dp.ctrl_bar = ctrl_bar;
313         nn->tx_bar = tx_bar;
314         nn->rx_bar = rx_bar;
315         nn->dp.is_vf = 0;
316         nn->stride_rx = stride;
317         nn->stride_tx = stride;
318
319         eth_port = nfp_net_find_port(pf->eth_tbl, eth_id);
320         if (eth_port) {
321                 nn->port = nfp_port_alloc(pf->app, NFP_PORT_PHYS_PORT,
322                                           nn->dp.netdev);
323                 if (IS_ERR(nn->port)) {
324                         nfp_net_free(nn);
325                         return ERR_CAST(nn->port);
326                 }
327                 nn->port->eth_id = eth_id;
328                 nn->port->eth_port = eth_port;
329         }
330
331         pf->num_vnics++;
332         list_add_tail(&nn->vnic_list, &pf->vnics);
333
334         return nn;
335 }
336
337 static int
338 nfp_net_pf_init_vnic(struct nfp_pf *pf, struct nfp_net *nn, unsigned int id)
339 {
340         int err;
341
342         /* Get MAC address */
343         nfp_net_get_mac_addr(nn, pf->cpp, id);
344
345         /* Get ME clock frequency from ctrl BAR
346          * XXX for now frequency is hardcoded until we figure out how
347          * to get the value from nfp-hwinfo into ctrl bar
348          */
349         nn->me_freq_mhz = 1200;
350
351         err = nfp_net_init(nn);
352         if (err)
353                 return err;
354
355         nfp_net_debugfs_vnic_add(nn, pf->ddir, id);
356
357         nfp_net_info(nn);
358
359         return 0;
360 }
361
362 static int
363 nfp_net_pf_alloc_vnics(struct nfp_pf *pf, void __iomem *ctrl_bar,
364                        void __iomem *tx_bar, void __iomem *rx_bar,
365                        int stride, struct nfp_net_fw_version *fw_ver)
366 {
367         u32 prev_tx_base, prev_rx_base, tgt_tx_base, tgt_rx_base;
368         struct nfp_net *nn;
369         unsigned int i;
370         int err;
371
372         if (pf->eth_tbl && pf->max_data_vnics != pf->eth_tbl->count) {
373                 nfp_err(pf->cpp, "ETH entries don't match vNICs (%d vs %d)\n",
374                         pf->max_data_vnics, pf->eth_tbl->count);
375                 return -EINVAL;
376         }
377
378         prev_tx_base = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
379         prev_rx_base = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
380
381         for (i = 0; i < pf->max_data_vnics; i++) {
382                 tgt_tx_base = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
383                 tgt_rx_base = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
384                 tx_bar += (tgt_tx_base - prev_tx_base) * NFP_QCP_QUEUE_ADDR_SZ;
385                 rx_bar += (tgt_rx_base - prev_rx_base) * NFP_QCP_QUEUE_ADDR_SZ;
386                 prev_tx_base = tgt_tx_base;
387                 prev_rx_base = tgt_rx_base;
388
389                 nn = nfp_net_pf_alloc_vnic(pf, ctrl_bar, tx_bar, rx_bar,
390                                            stride, fw_ver, i);
391                 if (IS_ERR(nn)) {
392                         err = PTR_ERR(nn);
393                         goto err_free_prev;
394                 }
395
396                 ctrl_bar += NFP_PF_CSR_SLICE_SIZE;
397
398                 /* Check if vNIC has external port associated and cfg is OK */
399                 if (pf->eth_tbl && !nn->port) {
400                         nfp_err(pf->cpp, "NSP port entries don't match vNICs (no entry for port #%d)\n", i);
401                         err = -EINVAL;
402                         goto err_free_prev;
403                 }
404                 if (nn->port && nn->port->eth_port->override_changed) {
405                         nfp_warn(pf->cpp, "Config changed for port #%d, reboot required before port will be operational\n", i);
406                         nfp_net_pf_free_vnic(pf, nn);
407                         continue;
408                 }
409         }
410
411         if (list_empty(&pf->vnics))
412                 return -ENODEV;
413
414         return 0;
415
416 err_free_prev:
417         nfp_net_pf_free_vnics(pf);
418         return err;
419 }
420
421 static int
422 nfp_net_pf_spawn_vnics(struct nfp_pf *pf,
423                        void __iomem *ctrl_bar, void __iomem *tx_bar,
424                        void __iomem *rx_bar, int stride,
425                        struct nfp_net_fw_version *fw_ver)
426 {
427         unsigned int id, wanted_irqs, num_irqs, vnics_left, irqs_left;
428         struct nfp_net *nn;
429         int err;
430
431         /* Allocate the vnics and do basic init */
432         err = nfp_net_pf_alloc_vnics(pf, ctrl_bar, tx_bar, rx_bar,
433                                      stride, fw_ver);
434         if (err)
435                 return err;
436
437         /* Get MSI-X vectors */
438         wanted_irqs = 0;
439         list_for_each_entry(nn, &pf->vnics, vnic_list)
440                 wanted_irqs += NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs;
441         pf->irq_entries = kcalloc(wanted_irqs, sizeof(*pf->irq_entries),
442                                   GFP_KERNEL);
443         if (!pf->irq_entries) {
444                 err = -ENOMEM;
445                 goto err_nn_free;
446         }
447
448         num_irqs = nfp_net_irqs_alloc(pf->pdev, pf->irq_entries,
449                                       NFP_NET_MIN_VNIC_IRQS * pf->num_vnics,
450                                       wanted_irqs);
451         if (!num_irqs) {
452                 nn_warn(nn, "Unable to allocate MSI-X Vectors. Exiting\n");
453                 err = -ENOMEM;
454                 goto err_vec_free;
455         }
456
457         /* Distribute IRQs to vNICs */
458         irqs_left = num_irqs;
459         vnics_left = pf->num_vnics;
460         list_for_each_entry(nn, &pf->vnics, vnic_list) {
461                 unsigned int n;
462
463                 n = DIV_ROUND_UP(irqs_left, vnics_left);
464                 nfp_net_irqs_assign(nn, &pf->irq_entries[num_irqs - irqs_left],
465                                     n);
466                 irqs_left -= n;
467                 vnics_left--;
468         }
469
470         /* Finish vNIC init and register */
471         id = 0;
472         list_for_each_entry(nn, &pf->vnics, vnic_list) {
473                 err = nfp_net_pf_init_vnic(pf, nn, id);
474                 if (err)
475                         goto err_prev_deinit;
476
477                 id++;
478         }
479
480         return 0;
481
482 err_prev_deinit:
483         list_for_each_entry_continue_reverse(nn, &pf->vnics, vnic_list) {
484                 nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
485                 nfp_net_clean(nn);
486         }
487         nfp_net_irqs_disable(pf->pdev);
488 err_vec_free:
489         kfree(pf->irq_entries);
490 err_nn_free:
491         nfp_net_pf_free_vnics(pf);
492         return err;
493 }
494
495 static int nfp_net_pf_app_init(struct nfp_pf *pf)
496 {
497         pf->app = nfp_app_alloc(pf);
498
499         return PTR_ERR_OR_ZERO(pf->app);
500 }
501
502 static void nfp_net_pf_app_clean(struct nfp_pf *pf)
503 {
504         nfp_app_free(pf->app);
505         pf->app = NULL;
506 }
507
508 static void nfp_net_pci_remove_finish(struct nfp_pf *pf)
509 {
510         nfp_net_debugfs_dir_clean(&pf->ddir);
511
512         nfp_net_irqs_disable(pf->pdev);
513         kfree(pf->irq_entries);
514
515         nfp_net_pf_app_clean(pf);
516
517         nfp_cpp_area_release_free(pf->rx_area);
518         nfp_cpp_area_release_free(pf->tx_area);
519         nfp_cpp_area_release_free(pf->data_vnic_bar);
520 }
521
522 static int
523 nfp_net_eth_port_update(struct nfp_cpp *cpp, struct nfp_port *port,
524                         struct nfp_eth_table *eth_table)
525 {
526         struct nfp_eth_table_port *eth_port;
527
528         ASSERT_RTNL();
529
530         eth_port = nfp_net_find_port(eth_table, port->eth_id);
531         if (!eth_port) {
532                 set_bit(NFP_PORT_CHANGED, &port->flags);
533                 nfp_warn(cpp, "Warning: port #%d not present after reconfig\n",
534                          port->eth_id);
535                 return -EIO;
536         }
537         if (eth_port->override_changed) {
538                 nfp_warn(cpp, "Port #%d config changed, unregistering. Reboot required before port will be operational again.\n", port->eth_id);
539                 port->type = NFP_PORT_INVALID;
540         }
541
542         memcpy(port->eth_port, eth_port, sizeof(*eth_port));
543
544         return 0;
545 }
546
547 static void nfp_net_refresh_vnics(struct work_struct *work)
548 {
549         struct nfp_pf *pf = container_of(work, struct nfp_pf,
550                                          port_refresh_work);
551         struct nfp_eth_table *eth_table;
552         struct nfp_net *nn, *next;
553         struct nfp_port *port;
554
555         mutex_lock(&pf->lock);
556
557         /* Check for nfp_net_pci_remove() racing against us */
558         if (list_empty(&pf->vnics))
559                 goto out;
560
561         /* Update state of all ports */
562         rtnl_lock();
563         list_for_each_entry(port, &pf->ports, port_list)
564                 clear_bit(NFP_PORT_CHANGED, &port->flags);
565
566         eth_table = nfp_eth_read_ports(pf->cpp);
567         if (!eth_table) {
568                 list_for_each_entry(port, &pf->ports, port_list)
569                         if (__nfp_port_get_eth_port(port))
570                                 set_bit(NFP_PORT_CHANGED, &port->flags);
571                 rtnl_unlock();
572                 nfp_err(pf->cpp, "Error refreshing port config!\n");
573                 goto out;
574         }
575
576         list_for_each_entry(port, &pf->ports, port_list)
577                 if (__nfp_port_get_eth_port(port))
578                         nfp_net_eth_port_update(pf->cpp, port, eth_table);
579         rtnl_unlock();
580
581         kfree(eth_table);
582
583         /* Shoot off the ports which became invalid */
584         list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list) {
585                 if (!nn->port || nn->port->type != NFP_PORT_INVALID)
586                         continue;
587
588                 nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
589                 nfp_net_clean(nn);
590
591                 nfp_net_pf_free_vnic(pf, nn);
592         }
593
594         if (list_empty(&pf->vnics))
595                 nfp_net_pci_remove_finish(pf);
596 out:
597         mutex_unlock(&pf->lock);
598 }
599
600 void nfp_net_refresh_port_table(struct nfp_port *port)
601 {
602         struct nfp_pf *pf = port->app->pf;
603
604         set_bit(NFP_PORT_CHANGED, &port->flags);
605
606         schedule_work(&pf->port_refresh_work);
607 }
608
609 int nfp_net_refresh_eth_port(struct nfp_port *port)
610 {
611         struct nfp_cpp *cpp = port->app->cpp;
612         struct nfp_eth_table *eth_table;
613         int ret;
614
615         clear_bit(NFP_PORT_CHANGED, &port->flags);
616
617         eth_table = nfp_eth_read_ports(cpp);
618         if (!eth_table) {
619                 set_bit(NFP_PORT_CHANGED, &port->flags);
620                 nfp_err(cpp, "Error refreshing port state table!\n");
621                 return -EIO;
622         }
623
624         ret = nfp_net_eth_port_update(cpp, port, eth_table);
625
626         kfree(eth_table);
627
628         return ret;
629 }
630
631 /*
632  * PCI device functions
633  */
634 int nfp_net_pci_probe(struct nfp_pf *pf)
635 {
636         u8 __iomem *ctrl_bar, *tx_bar, *rx_bar;
637         u32 total_tx_qcs, total_rx_qcs;
638         struct nfp_net_fw_version fw_ver;
639         u32 tx_area_sz, rx_area_sz;
640         u32 start_q;
641         int stride;
642         int err;
643
644         INIT_WORK(&pf->port_refresh_work, nfp_net_refresh_vnics);
645
646         /* Verify that the board has completed initialization */
647         if (!nfp_is_ready(pf->cpp)) {
648                 nfp_err(pf->cpp, "NFP is not ready for NIC operation.\n");
649                 return -EINVAL;
650         }
651
652         mutex_lock(&pf->lock);
653         pf->max_data_vnics = nfp_net_pf_get_num_ports(pf);
654
655         ctrl_bar = nfp_net_pf_map_ctrl_bar(pf);
656         if (!ctrl_bar) {
657                 err = pf->fw_loaded ? -EINVAL : -EPROBE_DEFER;
658                 goto err_unlock;
659         }
660
661         nfp_net_get_fw_version(&fw_ver, ctrl_bar);
662         if (fw_ver.resv || fw_ver.class != NFP_NET_CFG_VERSION_CLASS_GENERIC) {
663                 nfp_err(pf->cpp, "Unknown Firmware ABI %d.%d.%d.%d\n",
664                         fw_ver.resv, fw_ver.class, fw_ver.major, fw_ver.minor);
665                 err = -EINVAL;
666                 goto err_ctrl_unmap;
667         }
668
669         /* Determine stride */
670         if (nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0, 1)) {
671                 stride = 2;
672                 nfp_warn(pf->cpp, "OBSOLETE Firmware detected - VF isolation not available\n");
673         } else {
674                 switch (fw_ver.major) {
675                 case 1 ... 4:
676                         stride = 4;
677                         break;
678                 default:
679                         nfp_err(pf->cpp, "Unsupported Firmware ABI %d.%d.%d.%d\n",
680                                 fw_ver.resv, fw_ver.class,
681                                 fw_ver.major, fw_ver.minor);
682                         err = -EINVAL;
683                         goto err_ctrl_unmap;
684                 }
685         }
686
687         /* Find how many QC structs need to be mapped */
688         total_tx_qcs = nfp_net_pf_total_qcs(pf, ctrl_bar, stride,
689                                             NFP_NET_CFG_START_TXQ,
690                                             NFP_NET_CFG_MAX_TXRINGS);
691         total_rx_qcs = nfp_net_pf_total_qcs(pf, ctrl_bar, stride,
692                                             NFP_NET_CFG_START_RXQ,
693                                             NFP_NET_CFG_MAX_RXRINGS);
694         if (!total_tx_qcs || !total_rx_qcs) {
695                 nfp_err(pf->cpp, "Invalid PF QC configuration [%d,%d]\n",
696                         total_tx_qcs, total_rx_qcs);
697                 err = -EINVAL;
698                 goto err_ctrl_unmap;
699         }
700
701         tx_area_sz = NFP_QCP_QUEUE_ADDR_SZ * total_tx_qcs;
702         rx_area_sz = NFP_QCP_QUEUE_ADDR_SZ * total_rx_qcs;
703
704         /* Map TX queues */
705         start_q = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
706         tx_bar = nfp_net_map_area(pf->cpp, "net.tx", 0, 0,
707                                   NFP_PCIE_QUEUE(start_q),
708                                   tx_area_sz, &pf->tx_area);
709         if (IS_ERR(tx_bar)) {
710                 nfp_err(pf->cpp, "Failed to map TX area.\n");
711                 err = PTR_ERR(tx_bar);
712                 goto err_ctrl_unmap;
713         }
714
715         /* Map RX queues */
716         start_q = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
717         rx_bar = nfp_net_map_area(pf->cpp, "net.rx", 0, 0,
718                                   NFP_PCIE_QUEUE(start_q),
719                                   rx_area_sz, &pf->rx_area);
720         if (IS_ERR(rx_bar)) {
721                 nfp_err(pf->cpp, "Failed to map RX area.\n");
722                 err = PTR_ERR(rx_bar);
723                 goto err_unmap_tx;
724         }
725
726         err = nfp_net_pf_app_init(pf);
727         if (err)
728                 goto err_unmap_rx;
729
730         pf->ddir = nfp_net_debugfs_device_add(pf->pdev);
731
732         err = nfp_net_pf_spawn_vnics(pf, ctrl_bar, tx_bar, rx_bar,
733                                      stride, &fw_ver);
734         if (err)
735                 goto err_clean_ddir;
736
737         mutex_unlock(&pf->lock);
738
739         return 0;
740
741 err_clean_ddir:
742         nfp_net_debugfs_dir_clean(&pf->ddir);
743         nfp_net_pf_app_clean(pf);
744 err_unmap_rx:
745         nfp_cpp_area_release_free(pf->rx_area);
746 err_unmap_tx:
747         nfp_cpp_area_release_free(pf->tx_area);
748 err_ctrl_unmap:
749         nfp_cpp_area_release_free(pf->data_vnic_bar);
750 err_unlock:
751         mutex_unlock(&pf->lock);
752         return err;
753 }
754
755 void nfp_net_pci_remove(struct nfp_pf *pf)
756 {
757         struct nfp_net *nn;
758
759         mutex_lock(&pf->lock);
760         if (list_empty(&pf->vnics))
761                 goto out;
762
763         list_for_each_entry(nn, &pf->vnics, vnic_list) {
764                 nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
765
766                 nfp_net_clean(nn);
767         }
768
769         nfp_net_pf_free_vnics(pf);
770
771         nfp_net_pci_remove_finish(pf);
772 out:
773         mutex_unlock(&pf->lock);
774
775         cancel_work_sync(&pf->port_refresh_work);
776 }