• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/lockdep.h>
47 #include <linux/pci.h>
48 #include <linux/pci_regs.h>
49 #include <linux/msi.h>
50 #include <linux/random.h>
51 #include <linux/rtnetlink.h>
52 
53 #include "nfpcore/nfp.h"
54 #include "nfpcore/nfp_cpp.h"
55 #include "nfpcore/nfp_nffw.h"
56 #include "nfpcore/nfp_nsp.h"
57 #include "nfpcore/nfp6000_pcie.h"
58 #include "nfp_app.h"
59 #include "nfp_net_ctrl.h"
60 #include "nfp_net_sriov.h"
61 #include "nfp_net.h"
62 #include "nfp_main.h"
63 #include "nfp_port.h"
64 
65 #define NFP_PF_CSR_SLICE_SIZE	(32 * 1024)
66 
67 /**
68  * nfp_net_get_mac_addr() - Get the MAC address.
69  * @pf:       NFP PF handle
70  * @port:     NFP port structure
71  *
72  * First try to get the MAC address from NSP ETH table. If that
73  * fails generate a random address.
74  */
nfp_net_get_mac_addr(struct nfp_pf * pf,struct nfp_port * port)75 void nfp_net_get_mac_addr(struct nfp_pf *pf, struct nfp_port *port)
76 {
77 	struct nfp_eth_table_port *eth_port;
78 
79 	eth_port = __nfp_port_get_eth_port(port);
80 	if (!eth_port) {
81 		eth_hw_addr_random(port->netdev);
82 		return;
83 	}
84 
85 	ether_addr_copy(port->netdev->dev_addr, eth_port->mac_addr);
86 	ether_addr_copy(port->netdev->perm_addr, eth_port->mac_addr);
87 }
88 
89 static struct nfp_eth_table_port *
nfp_net_find_port(struct nfp_eth_table * eth_tbl,unsigned int index)90 nfp_net_find_port(struct nfp_eth_table *eth_tbl, unsigned int index)
91 {
92 	int i;
93 
94 	for (i = 0; eth_tbl && i < eth_tbl->count; i++)
95 		if (eth_tbl->ports[i].index == index)
96 			return &eth_tbl->ports[i];
97 
98 	return NULL;
99 }
100 
101 static int
nfp_net_pf_rtsym_read_optional(struct nfp_pf * pf,const char * format,unsigned int default_val)102 nfp_net_pf_rtsym_read_optional(struct nfp_pf *pf, const char *format,
103 			       unsigned int default_val)
104 {
105 	char name[256];
106 	int err = 0;
107 	u64 val;
108 
109 	snprintf(name, sizeof(name), format, nfp_cppcore_pcie_unit(pf->cpp));
110 
111 	val = nfp_rtsym_read_le(pf->rtbl, name, &err);
112 	if (err) {
113 		if (err == -ENOENT)
114 			return default_val;
115 		nfp_err(pf->cpp, "Unable to read symbol %s\n", name);
116 		return err;
117 	}
118 
119 	return val;
120 }
121 
nfp_net_pf_get_num_ports(struct nfp_pf * pf)122 static int nfp_net_pf_get_num_ports(struct nfp_pf *pf)
123 {
124 	return nfp_net_pf_rtsym_read_optional(pf, "nfd_cfg_pf%u_num_ports", 1);
125 }
126 
nfp_net_pf_get_app_id(struct nfp_pf * pf)127 static int nfp_net_pf_get_app_id(struct nfp_pf *pf)
128 {
129 	return nfp_net_pf_rtsym_read_optional(pf, "_pf%u_net_app_id",
130 					      NFP_APP_CORE_NIC);
131 }
132 
133 static u8 __iomem *
nfp_net_pf_map_rtsym(struct nfp_pf * pf,const char * name,const char * sym_fmt,unsigned int min_size,struct nfp_cpp_area ** area)134 nfp_net_pf_map_rtsym(struct nfp_pf *pf, const char *name, const char *sym_fmt,
135 		     unsigned int min_size, struct nfp_cpp_area **area)
136 {
137 	char pf_symbol[256];
138 
139 	snprintf(pf_symbol, sizeof(pf_symbol), sym_fmt,
140 		 nfp_cppcore_pcie_unit(pf->cpp));
141 
142 	return nfp_rtsym_map(pf->rtbl, pf_symbol, name, min_size, area);
143 }
144 
nfp_net_pf_free_vnic(struct nfp_pf * pf,struct nfp_net * nn)145 static void nfp_net_pf_free_vnic(struct nfp_pf *pf, struct nfp_net *nn)
146 {
147 	if (nfp_net_is_data_vnic(nn))
148 		nfp_app_vnic_free(pf->app, nn);
149 	nfp_port_free(nn->port);
150 	list_del(&nn->vnic_list);
151 	pf->num_vnics--;
152 	nfp_net_free(nn);
153 }
154 
nfp_net_pf_free_vnics(struct nfp_pf * pf)155 static void nfp_net_pf_free_vnics(struct nfp_pf *pf)
156 {
157 	struct nfp_net *nn, *next;
158 
159 	list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list)
160 		if (nfp_net_is_data_vnic(nn))
161 			nfp_net_pf_free_vnic(pf, nn);
162 }
163 
164 static struct nfp_net *
nfp_net_pf_alloc_vnic(struct nfp_pf * pf,bool needs_netdev,void __iomem * ctrl_bar,void __iomem * qc_bar,int stride,unsigned int id)165 nfp_net_pf_alloc_vnic(struct nfp_pf *pf, bool needs_netdev,
166 		      void __iomem *ctrl_bar, void __iomem *qc_bar,
167 		      int stride, unsigned int id)
168 {
169 	u32 tx_base, rx_base, n_tx_rings, n_rx_rings;
170 	struct nfp_net *nn;
171 	int err;
172 
173 	tx_base = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
174 	rx_base = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
175 	n_tx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_TXRINGS);
176 	n_rx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_RXRINGS);
177 
178 	/* Allocate and initialise the vNIC */
179 	nn = nfp_net_alloc(pf->pdev, needs_netdev, n_tx_rings, n_rx_rings);
180 	if (IS_ERR(nn))
181 		return nn;
182 
183 	nn->app = pf->app;
184 	nfp_net_get_fw_version(&nn->fw_ver, ctrl_bar);
185 	nn->dp.ctrl_bar = ctrl_bar;
186 	nn->tx_bar = qc_bar + tx_base * NFP_QCP_QUEUE_ADDR_SZ;
187 	nn->rx_bar = qc_bar + rx_base * NFP_QCP_QUEUE_ADDR_SZ;
188 	nn->dp.is_vf = 0;
189 	nn->stride_rx = stride;
190 	nn->stride_tx = stride;
191 
192 	if (needs_netdev) {
193 		err = nfp_app_vnic_alloc(pf->app, nn, id);
194 		if (err) {
195 			nfp_net_free(nn);
196 			return ERR_PTR(err);
197 		}
198 	}
199 
200 	pf->num_vnics++;
201 	list_add_tail(&nn->vnic_list, &pf->vnics);
202 
203 	return nn;
204 }
205 
206 static int
nfp_net_pf_init_vnic(struct nfp_pf * pf,struct nfp_net * nn,unsigned int id)207 nfp_net_pf_init_vnic(struct nfp_pf *pf, struct nfp_net *nn, unsigned int id)
208 {
209 	int err;
210 
211 	/* Get ME clock frequency from ctrl BAR
212 	 * XXX for now frequency is hardcoded until we figure out how
213 	 * to get the value from nfp-hwinfo into ctrl bar
214 	 */
215 	nn->me_freq_mhz = 1200;
216 
217 	err = nfp_net_init(nn);
218 	if (err)
219 		return err;
220 
221 	nfp_net_debugfs_vnic_add(nn, pf->ddir, id);
222 
223 	if (nn->port) {
224 		err = nfp_devlink_port_register(pf->app, nn->port);
225 		if (err)
226 			goto err_dfs_clean;
227 	}
228 
229 	nfp_net_info(nn);
230 
231 	if (nfp_net_is_data_vnic(nn)) {
232 		err = nfp_app_vnic_init(pf->app, nn);
233 		if (err)
234 			goto err_devlink_port_clean;
235 	}
236 
237 	return 0;
238 
239 err_devlink_port_clean:
240 	if (nn->port)
241 		nfp_devlink_port_unregister(nn->port);
242 err_dfs_clean:
243 	nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
244 	nfp_net_clean(nn);
245 	return err;
246 }
247 
248 static int
nfp_net_pf_alloc_vnics(struct nfp_pf * pf,void __iomem * ctrl_bar,void __iomem * qc_bar,int stride)249 nfp_net_pf_alloc_vnics(struct nfp_pf *pf, void __iomem *ctrl_bar,
250 		       void __iomem *qc_bar, int stride)
251 {
252 	struct nfp_net *nn;
253 	unsigned int i;
254 	int err;
255 
256 	for (i = 0; i < pf->max_data_vnics; i++) {
257 		nn = nfp_net_pf_alloc_vnic(pf, true, ctrl_bar, qc_bar,
258 					   stride, i);
259 		if (IS_ERR(nn)) {
260 			err = PTR_ERR(nn);
261 			goto err_free_prev;
262 		}
263 
264 		ctrl_bar += NFP_PF_CSR_SLICE_SIZE;
265 
266 		/* Kill the vNIC if app init marked it as invalid */
267 		if (nn->port && nn->port->type == NFP_PORT_INVALID) {
268 			nfp_net_pf_free_vnic(pf, nn);
269 			continue;
270 		}
271 	}
272 
273 	if (list_empty(&pf->vnics))
274 		return -ENODEV;
275 
276 	return 0;
277 
278 err_free_prev:
279 	nfp_net_pf_free_vnics(pf);
280 	return err;
281 }
282 
nfp_net_pf_clean_vnic(struct nfp_pf * pf,struct nfp_net * nn)283 static void nfp_net_pf_clean_vnic(struct nfp_pf *pf, struct nfp_net *nn)
284 {
285 	if (nfp_net_is_data_vnic(nn))
286 		nfp_app_vnic_clean(pf->app, nn);
287 	if (nn->port)
288 		nfp_devlink_port_unregister(nn->port);
289 	nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
290 	nfp_net_clean(nn);
291 }
292 
nfp_net_pf_alloc_irqs(struct nfp_pf * pf)293 static int nfp_net_pf_alloc_irqs(struct nfp_pf *pf)
294 {
295 	unsigned int wanted_irqs, num_irqs, vnics_left, irqs_left;
296 	struct nfp_net *nn;
297 
298 	/* Get MSI-X vectors */
299 	wanted_irqs = 0;
300 	list_for_each_entry(nn, &pf->vnics, vnic_list)
301 		wanted_irqs += NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs;
302 	pf->irq_entries = kcalloc(wanted_irqs, sizeof(*pf->irq_entries),
303 				  GFP_KERNEL);
304 	if (!pf->irq_entries)
305 		return -ENOMEM;
306 
307 	num_irqs = nfp_net_irqs_alloc(pf->pdev, pf->irq_entries,
308 				      NFP_NET_MIN_VNIC_IRQS * pf->num_vnics,
309 				      wanted_irqs);
310 	if (!num_irqs) {
311 		nfp_warn(pf->cpp, "Unable to allocate MSI-X vectors\n");
312 		kfree(pf->irq_entries);
313 		return -ENOMEM;
314 	}
315 
316 	/* Distribute IRQs to vNICs */
317 	irqs_left = num_irqs;
318 	vnics_left = pf->num_vnics;
319 	list_for_each_entry(nn, &pf->vnics, vnic_list) {
320 		unsigned int n;
321 
322 		n = min(NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs,
323 			DIV_ROUND_UP(irqs_left, vnics_left));
324 		nfp_net_irqs_assign(nn, &pf->irq_entries[num_irqs - irqs_left],
325 				    n);
326 		irqs_left -= n;
327 		vnics_left--;
328 	}
329 
330 	return 0;
331 }
332 
nfp_net_pf_free_irqs(struct nfp_pf * pf)333 static void nfp_net_pf_free_irqs(struct nfp_pf *pf)
334 {
335 	nfp_net_irqs_disable(pf->pdev);
336 	kfree(pf->irq_entries);
337 }
338 
nfp_net_pf_init_vnics(struct nfp_pf * pf)339 static int nfp_net_pf_init_vnics(struct nfp_pf *pf)
340 {
341 	struct nfp_net *nn;
342 	unsigned int id;
343 	int err;
344 
345 	/* Finish vNIC init and register */
346 	id = 0;
347 	list_for_each_entry(nn, &pf->vnics, vnic_list) {
348 		if (!nfp_net_is_data_vnic(nn))
349 			continue;
350 		err = nfp_net_pf_init_vnic(pf, nn, id);
351 		if (err)
352 			goto err_prev_deinit;
353 
354 		id++;
355 	}
356 
357 	return 0;
358 
359 err_prev_deinit:
360 	list_for_each_entry_continue_reverse(nn, &pf->vnics, vnic_list)
361 		if (nfp_net_is_data_vnic(nn))
362 			nfp_net_pf_clean_vnic(pf, nn);
363 	return err;
364 }
365 
366 static int
nfp_net_pf_app_init(struct nfp_pf * pf,u8 __iomem * qc_bar,unsigned int stride)367 nfp_net_pf_app_init(struct nfp_pf *pf, u8 __iomem *qc_bar, unsigned int stride)
368 {
369 	u8 __iomem *ctrl_bar;
370 	int err;
371 
372 	pf->app = nfp_app_alloc(pf, nfp_net_pf_get_app_id(pf));
373 	if (IS_ERR(pf->app))
374 		return PTR_ERR(pf->app);
375 
376 	err = nfp_app_init(pf->app);
377 	if (err)
378 		goto err_free;
379 
380 	if (!nfp_app_needs_ctrl_vnic(pf->app))
381 		return 0;
382 
383 	ctrl_bar = nfp_net_pf_map_rtsym(pf, "net.ctrl", "_pf%u_net_ctrl_bar",
384 					NFP_PF_CSR_SLICE_SIZE,
385 					&pf->ctrl_vnic_bar);
386 	if (IS_ERR(ctrl_bar)) {
387 		nfp_err(pf->cpp, "Failed to find ctrl vNIC memory symbol\n");
388 		err = PTR_ERR(ctrl_bar);
389 		goto err_app_clean;
390 	}
391 
392 	pf->ctrl_vnic =	nfp_net_pf_alloc_vnic(pf, false, ctrl_bar, qc_bar,
393 					      stride, 0);
394 	if (IS_ERR(pf->ctrl_vnic)) {
395 		err = PTR_ERR(pf->ctrl_vnic);
396 		goto err_unmap;
397 	}
398 
399 	return 0;
400 
401 err_unmap:
402 	nfp_cpp_area_release_free(pf->ctrl_vnic_bar);
403 err_app_clean:
404 	nfp_app_clean(pf->app);
405 err_free:
406 	nfp_app_free(pf->app);
407 	pf->app = NULL;
408 	return err;
409 }
410 
nfp_net_pf_app_clean(struct nfp_pf * pf)411 static void nfp_net_pf_app_clean(struct nfp_pf *pf)
412 {
413 	if (pf->ctrl_vnic) {
414 		nfp_net_pf_free_vnic(pf, pf->ctrl_vnic);
415 		nfp_cpp_area_release_free(pf->ctrl_vnic_bar);
416 	}
417 	nfp_app_clean(pf->app);
418 	nfp_app_free(pf->app);
419 	pf->app = NULL;
420 }
421 
nfp_net_pf_app_start_ctrl(struct nfp_pf * pf)422 static int nfp_net_pf_app_start_ctrl(struct nfp_pf *pf)
423 {
424 	int err;
425 
426 	if (!pf->ctrl_vnic)
427 		return 0;
428 	err = nfp_net_pf_init_vnic(pf, pf->ctrl_vnic, 0);
429 	if (err)
430 		return err;
431 
432 	err = nfp_ctrl_open(pf->ctrl_vnic);
433 	if (err)
434 		goto err_clean_ctrl;
435 
436 	return 0;
437 
438 err_clean_ctrl:
439 	nfp_net_pf_clean_vnic(pf, pf->ctrl_vnic);
440 	return err;
441 }
442 
nfp_net_pf_app_stop_ctrl(struct nfp_pf * pf)443 static void nfp_net_pf_app_stop_ctrl(struct nfp_pf *pf)
444 {
445 	if (!pf->ctrl_vnic)
446 		return;
447 	nfp_ctrl_close(pf->ctrl_vnic);
448 	nfp_net_pf_clean_vnic(pf, pf->ctrl_vnic);
449 }
450 
nfp_net_pf_app_start(struct nfp_pf * pf)451 static int nfp_net_pf_app_start(struct nfp_pf *pf)
452 {
453 	int err;
454 
455 	err = nfp_net_pf_app_start_ctrl(pf);
456 	if (err)
457 		return err;
458 
459 	err = nfp_app_start(pf->app, pf->ctrl_vnic);
460 	if (err)
461 		goto err_ctrl_stop;
462 
463 	if (pf->num_vfs) {
464 		err = nfp_app_sriov_enable(pf->app, pf->num_vfs);
465 		if (err)
466 			goto err_app_stop;
467 	}
468 
469 	return 0;
470 
471 err_app_stop:
472 	nfp_app_stop(pf->app);
473 err_ctrl_stop:
474 	nfp_net_pf_app_stop_ctrl(pf);
475 	return err;
476 }
477 
nfp_net_pf_app_stop(struct nfp_pf * pf)478 static void nfp_net_pf_app_stop(struct nfp_pf *pf)
479 {
480 	if (pf->num_vfs)
481 		nfp_app_sriov_disable(pf->app);
482 	nfp_app_stop(pf->app);
483 	nfp_net_pf_app_stop_ctrl(pf);
484 }
485 
nfp_net_pci_unmap_mem(struct nfp_pf * pf)486 static void nfp_net_pci_unmap_mem(struct nfp_pf *pf)
487 {
488 	if (pf->vfcfg_tbl2_area)
489 		nfp_cpp_area_release_free(pf->vfcfg_tbl2_area);
490 	if (pf->vf_cfg_bar)
491 		nfp_cpp_area_release_free(pf->vf_cfg_bar);
492 	if (pf->mac_stats_bar)
493 		nfp_cpp_area_release_free(pf->mac_stats_bar);
494 	nfp_cpp_area_release_free(pf->qc_area);
495 	nfp_cpp_area_release_free(pf->data_vnic_bar);
496 }
497 
nfp_net_pci_map_mem(struct nfp_pf * pf)498 static int nfp_net_pci_map_mem(struct nfp_pf *pf)
499 {
500 	u8 __iomem *mem;
501 	u32 min_size;
502 	int err;
503 
504 	min_size = pf->max_data_vnics * NFP_PF_CSR_SLICE_SIZE;
505 	mem = nfp_net_pf_map_rtsym(pf, "net.bar0", "_pf%d_net_bar0",
506 				   min_size, &pf->data_vnic_bar);
507 	if (IS_ERR(mem)) {
508 		nfp_err(pf->cpp, "Failed to find data vNIC memory symbol\n");
509 		return PTR_ERR(mem);
510 	}
511 
512 	min_size =  NFP_MAC_STATS_SIZE * (pf->eth_tbl->max_index + 1);
513 	pf->mac_stats_mem = nfp_rtsym_map(pf->rtbl, "_mac_stats",
514 					  "net.macstats", min_size,
515 					  &pf->mac_stats_bar);
516 	if (IS_ERR(pf->mac_stats_mem)) {
517 		if (PTR_ERR(pf->mac_stats_mem) != -ENOENT) {
518 			err = PTR_ERR(pf->mac_stats_mem);
519 			goto err_unmap_ctrl;
520 		}
521 		pf->mac_stats_mem = NULL;
522 	}
523 
524 	pf->vf_cfg_mem = nfp_net_pf_map_rtsym(pf, "net.vfcfg",
525 					      "_pf%d_net_vf_bar",
526 					      NFP_NET_CFG_BAR_SZ *
527 					      pf->limit_vfs, &pf->vf_cfg_bar);
528 	if (IS_ERR(pf->vf_cfg_mem)) {
529 		if (PTR_ERR(pf->vf_cfg_mem) != -ENOENT) {
530 			err = PTR_ERR(pf->vf_cfg_mem);
531 			goto err_unmap_mac_stats;
532 		}
533 		pf->vf_cfg_mem = NULL;
534 	}
535 
536 	min_size = NFP_NET_VF_CFG_SZ * pf->limit_vfs + NFP_NET_VF_CFG_MB_SZ;
537 	pf->vfcfg_tbl2 = nfp_net_pf_map_rtsym(pf, "net.vfcfg_tbl2",
538 					      "_pf%d_net_vf_cfg2",
539 					      min_size, &pf->vfcfg_tbl2_area);
540 	if (IS_ERR(pf->vfcfg_tbl2)) {
541 		if (PTR_ERR(pf->vfcfg_tbl2) != -ENOENT) {
542 			err = PTR_ERR(pf->vfcfg_tbl2);
543 			goto err_unmap_vf_cfg;
544 		}
545 		pf->vfcfg_tbl2 = NULL;
546 	}
547 
548 	mem = nfp_cpp_map_area(pf->cpp, "net.qc", 0, 0,
549 			       NFP_PCIE_QUEUE(0), NFP_QCP_QUEUE_AREA_SZ,
550 			       &pf->qc_area);
551 	if (IS_ERR(mem)) {
552 		nfp_err(pf->cpp, "Failed to map Queue Controller area.\n");
553 		err = PTR_ERR(mem);
554 		goto err_unmap_vfcfg_tbl2;
555 	}
556 
557 	return 0;
558 
559 err_unmap_vfcfg_tbl2:
560 	if (pf->vfcfg_tbl2_area)
561 		nfp_cpp_area_release_free(pf->vfcfg_tbl2_area);
562 err_unmap_vf_cfg:
563 	if (pf->vf_cfg_bar)
564 		nfp_cpp_area_release_free(pf->vf_cfg_bar);
565 err_unmap_mac_stats:
566 	if (pf->mac_stats_bar)
567 		nfp_cpp_area_release_free(pf->mac_stats_bar);
568 err_unmap_ctrl:
569 	nfp_cpp_area_release_free(pf->data_vnic_bar);
570 	return err;
571 }
572 
nfp_net_pci_remove_finish(struct nfp_pf * pf)573 static void nfp_net_pci_remove_finish(struct nfp_pf *pf)
574 {
575 	nfp_net_pf_app_stop(pf);
576 	/* stop app first, to avoid double free of ctrl vNIC's ddir */
577 	nfp_net_debugfs_dir_clean(&pf->ddir);
578 
579 	nfp_net_pf_free_irqs(pf);
580 	nfp_net_pf_app_clean(pf);
581 	nfp_net_pci_unmap_mem(pf);
582 }
583 
584 static int
nfp_net_eth_port_update(struct nfp_cpp * cpp,struct nfp_port * port,struct nfp_eth_table * eth_table)585 nfp_net_eth_port_update(struct nfp_cpp *cpp, struct nfp_port *port,
586 			struct nfp_eth_table *eth_table)
587 {
588 	struct nfp_eth_table_port *eth_port;
589 
590 	ASSERT_RTNL();
591 
592 	eth_port = nfp_net_find_port(eth_table, port->eth_id);
593 	if (!eth_port) {
594 		set_bit(NFP_PORT_CHANGED, &port->flags);
595 		nfp_warn(cpp, "Warning: port #%d not present after reconfig\n",
596 			 port->eth_id);
597 		return -EIO;
598 	}
599 	if (eth_port->override_changed) {
600 		nfp_warn(cpp, "Port #%d config changed, unregistering. Reboot required before port will be operational again.\n", port->eth_id);
601 		port->type = NFP_PORT_INVALID;
602 	}
603 
604 	memcpy(port->eth_port, eth_port, sizeof(*eth_port));
605 
606 	return 0;
607 }
608 
nfp_net_refresh_port_table_sync(struct nfp_pf * pf)609 int nfp_net_refresh_port_table_sync(struct nfp_pf *pf)
610 {
611 	struct nfp_eth_table *eth_table;
612 	struct nfp_net *nn, *next;
613 	struct nfp_port *port;
614 
615 	lockdep_assert_held(&pf->lock);
616 
617 	/* Check for nfp_net_pci_remove() racing against us */
618 	if (list_empty(&pf->vnics))
619 		return 0;
620 
621 	/* Update state of all ports */
622 	rtnl_lock();
623 	list_for_each_entry(port, &pf->ports, port_list)
624 		clear_bit(NFP_PORT_CHANGED, &port->flags);
625 
626 	eth_table = nfp_eth_read_ports(pf->cpp);
627 	if (!eth_table) {
628 		list_for_each_entry(port, &pf->ports, port_list)
629 			if (__nfp_port_get_eth_port(port))
630 				set_bit(NFP_PORT_CHANGED, &port->flags);
631 		rtnl_unlock();
632 		nfp_err(pf->cpp, "Error refreshing port config!\n");
633 		return -EIO;
634 	}
635 
636 	list_for_each_entry(port, &pf->ports, port_list)
637 		if (__nfp_port_get_eth_port(port))
638 			nfp_net_eth_port_update(pf->cpp, port, eth_table);
639 	rtnl_unlock();
640 
641 	kfree(eth_table);
642 
643 	/* Shoot off the ports which became invalid */
644 	list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list) {
645 		if (!nn->port || nn->port->type != NFP_PORT_INVALID)
646 			continue;
647 
648 		nfp_net_pf_clean_vnic(pf, nn);
649 		nfp_net_pf_free_vnic(pf, nn);
650 	}
651 
652 	if (list_empty(&pf->vnics))
653 		nfp_net_pci_remove_finish(pf);
654 
655 	return 0;
656 }
657 
nfp_net_refresh_vnics(struct work_struct * work)658 static void nfp_net_refresh_vnics(struct work_struct *work)
659 {
660 	struct nfp_pf *pf = container_of(work, struct nfp_pf,
661 					 port_refresh_work);
662 
663 	mutex_lock(&pf->lock);
664 	nfp_net_refresh_port_table_sync(pf);
665 	mutex_unlock(&pf->lock);
666 }
667 
nfp_net_refresh_port_table(struct nfp_port * port)668 void nfp_net_refresh_port_table(struct nfp_port *port)
669 {
670 	struct nfp_pf *pf = port->app->pf;
671 
672 	set_bit(NFP_PORT_CHANGED, &port->flags);
673 
674 	queue_work(pf->wq, &pf->port_refresh_work);
675 }
676 
nfp_net_refresh_eth_port(struct nfp_port * port)677 int nfp_net_refresh_eth_port(struct nfp_port *port)
678 {
679 	struct nfp_cpp *cpp = port->app->cpp;
680 	struct nfp_eth_table *eth_table;
681 	int ret;
682 
683 	clear_bit(NFP_PORT_CHANGED, &port->flags);
684 
685 	eth_table = nfp_eth_read_ports(cpp);
686 	if (!eth_table) {
687 		set_bit(NFP_PORT_CHANGED, &port->flags);
688 		nfp_err(cpp, "Error refreshing port state table!\n");
689 		return -EIO;
690 	}
691 
692 	ret = nfp_net_eth_port_update(cpp, port, eth_table);
693 
694 	kfree(eth_table);
695 
696 	return ret;
697 }
698 
699 /*
700  * PCI device functions
701  */
nfp_net_pci_probe(struct nfp_pf * pf)702 int nfp_net_pci_probe(struct nfp_pf *pf)
703 {
704 	struct nfp_net_fw_version fw_ver;
705 	u8 __iomem *ctrl_bar, *qc_bar;
706 	int stride;
707 	int err;
708 
709 	INIT_WORK(&pf->port_refresh_work, nfp_net_refresh_vnics);
710 
711 	if (!pf->rtbl) {
712 		nfp_err(pf->cpp, "No %s, giving up.\n",
713 			pf->fw_loaded ? "symbol table" : "firmware found");
714 		return -EINVAL;
715 	}
716 
717 	mutex_lock(&pf->lock);
718 	pf->max_data_vnics = nfp_net_pf_get_num_ports(pf);
719 	if ((int)pf->max_data_vnics < 0) {
720 		err = pf->max_data_vnics;
721 		goto err_unlock;
722 	}
723 
724 	err = nfp_net_pci_map_mem(pf);
725 	if (err)
726 		goto err_unlock;
727 
728 	ctrl_bar = nfp_cpp_area_iomem(pf->data_vnic_bar);
729 	qc_bar = nfp_cpp_area_iomem(pf->qc_area);
730 	if (!ctrl_bar || !qc_bar) {
731 		err = -EIO;
732 		goto err_unmap;
733 	}
734 
735 	nfp_net_get_fw_version(&fw_ver, ctrl_bar);
736 	if (fw_ver.resv || fw_ver.class != NFP_NET_CFG_VERSION_CLASS_GENERIC) {
737 		nfp_err(pf->cpp, "Unknown Firmware ABI %d.%d.%d.%d\n",
738 			fw_ver.resv, fw_ver.class, fw_ver.major, fw_ver.minor);
739 		err = -EINVAL;
740 		goto err_unmap;
741 	}
742 
743 	/* Determine stride */
744 	if (nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0, 1)) {
745 		stride = 2;
746 		nfp_warn(pf->cpp, "OBSOLETE Firmware detected - VF isolation not available\n");
747 	} else {
748 		switch (fw_ver.major) {
749 		case 1 ... 5:
750 			stride = 4;
751 			break;
752 		default:
753 			nfp_err(pf->cpp, "Unsupported Firmware ABI %d.%d.%d.%d\n",
754 				fw_ver.resv, fw_ver.class,
755 				fw_ver.major, fw_ver.minor);
756 			err = -EINVAL;
757 			goto err_unmap;
758 		}
759 	}
760 
761 	err = nfp_net_pf_app_init(pf, qc_bar, stride);
762 	if (err)
763 		goto err_unmap;
764 
765 	pf->ddir = nfp_net_debugfs_device_add(pf->pdev);
766 
767 	/* Allocate the vnics and do basic init */
768 	err = nfp_net_pf_alloc_vnics(pf, ctrl_bar, qc_bar, stride);
769 	if (err)
770 		goto err_clean_ddir;
771 
772 	err = nfp_net_pf_alloc_irqs(pf);
773 	if (err)
774 		goto err_free_vnics;
775 
776 	err = nfp_net_pf_app_start(pf);
777 	if (err)
778 		goto err_free_irqs;
779 
780 	err = nfp_net_pf_init_vnics(pf);
781 	if (err)
782 		goto err_stop_app;
783 
784 	mutex_unlock(&pf->lock);
785 
786 	return 0;
787 
788 err_stop_app:
789 	nfp_net_pf_app_stop(pf);
790 err_free_irqs:
791 	nfp_net_pf_free_irqs(pf);
792 err_free_vnics:
793 	nfp_net_pf_free_vnics(pf);
794 err_clean_ddir:
795 	nfp_net_debugfs_dir_clean(&pf->ddir);
796 	nfp_net_pf_app_clean(pf);
797 err_unmap:
798 	nfp_net_pci_unmap_mem(pf);
799 err_unlock:
800 	mutex_unlock(&pf->lock);
801 	cancel_work_sync(&pf->port_refresh_work);
802 	return err;
803 }
804 
nfp_net_pci_remove(struct nfp_pf * pf)805 void nfp_net_pci_remove(struct nfp_pf *pf)
806 {
807 	struct nfp_net *nn;
808 
809 	mutex_lock(&pf->lock);
810 	if (list_empty(&pf->vnics))
811 		goto out;
812 
813 	list_for_each_entry(nn, &pf->vnics, vnic_list)
814 		if (nfp_net_is_data_vnic(nn))
815 			nfp_net_pf_clean_vnic(pf, nn);
816 
817 	nfp_net_pf_free_vnics(pf);
818 
819 	nfp_net_pci_remove_finish(pf);
820 out:
821 	mutex_unlock(&pf->lock);
822 
823 	cancel_work_sync(&pf->port_refresh_work);
824 }
825