]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/ethernet/netronome/nfp/nfp_port.c
nfp: support port splitting via devlink
[mirror_ubuntu-artful-kernel.git] / drivers / net / ethernet / netronome / nfp / nfp_port.c
1 /*
2 * Copyright (C) 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 #include <linux/lockdep.h>
35
36 #include "nfpcore/nfp_nsp.h"
37 #include "nfp_app.h"
38 #include "nfp_main.h"
39 #include "nfp_net.h"
40 #include "nfp_port.h"
41
42 struct nfp_port *nfp_port_from_netdev(struct net_device *netdev)
43 {
44 struct nfp_net *nn;
45
46 if (WARN_ON(!nfp_netdev_is_nfp_net(netdev)))
47 return NULL;
48 nn = netdev_priv(netdev);
49
50 return nn->port;
51 }
52
53 struct nfp_port *
54 nfp_port_from_id(struct nfp_pf *pf, enum nfp_port_type type, unsigned int id)
55 {
56 struct nfp_port *port;
57
58 lockdep_assert_held(&pf->lock);
59
60 if (type != NFP_PORT_PHYS_PORT)
61 return NULL;
62
63 list_for_each_entry(port, &pf->ports, port_list)
64 if (port->eth_id == id)
65 return port;
66
67 return NULL;
68 }
69
70 struct nfp_eth_table_port *__nfp_port_get_eth_port(struct nfp_port *port)
71 {
72 if (!port)
73 return NULL;
74 if (port->type != NFP_PORT_PHYS_PORT)
75 return NULL;
76
77 return port->eth_port;
78 }
79
80 struct nfp_eth_table_port *nfp_port_get_eth_port(struct nfp_port *port)
81 {
82 if (!__nfp_port_get_eth_port(port))
83 return NULL;
84
85 if (test_bit(NFP_PORT_CHANGED, &port->flags))
86 if (nfp_net_refresh_eth_port(port))
87 return NULL;
88
89 return __nfp_port_get_eth_port(port);
90 }
91
92 int
93 nfp_port_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
94 {
95 struct nfp_eth_table_port *eth_port;
96 struct nfp_port *port;
97 int n;
98
99 port = nfp_port_from_netdev(netdev);
100 eth_port = __nfp_port_get_eth_port(port);
101 if (!eth_port)
102 return -EOPNOTSUPP;
103
104 if (!eth_port->is_split)
105 n = snprintf(name, len, "p%d", eth_port->label_port);
106 else
107 n = snprintf(name, len, "p%ds%d", eth_port->label_port,
108 eth_port->label_subport);
109 if (n >= len)
110 return -EINVAL;
111
112 return 0;
113 }
114
115 struct nfp_port *
116 nfp_port_alloc(struct nfp_app *app, enum nfp_port_type type,
117 struct net_device *netdev)
118 {
119 struct nfp_port *port;
120
121 port = kzalloc(sizeof(*port), GFP_KERNEL);
122 if (!port)
123 return ERR_PTR(-ENOMEM);
124
125 port->netdev = netdev;
126 port->type = type;
127 port->app = app;
128
129 list_add_tail(&port->port_list, &app->pf->ports);
130
131 return port;
132 }
133
134 void nfp_port_free(struct nfp_port *port)
135 {
136 if (!port)
137 return;
138 list_del(&port->port_list);
139 kfree(port);
140 }