net: sched: make type an argument for ndo_setup_tc
[linux-2.6-block.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 #include <net/switchdev.h>
36
37 #include "nfpcore/nfp_cpp.h"
38 #include "nfpcore/nfp_nsp.h"
39 #include "nfp_app.h"
40 #include "nfp_main.h"
41 #include "nfp_net.h"
42 #include "nfp_port.h"
43
44 struct nfp_port *nfp_port_from_netdev(struct net_device *netdev)
45 {
46         if (nfp_netdev_is_nfp_net(netdev)) {
47                 struct nfp_net *nn = netdev_priv(netdev);
48
49                 return nn->port;
50         }
51
52         if (nfp_netdev_is_nfp_repr(netdev)) {
53                 struct nfp_repr *repr = netdev_priv(netdev);
54
55                 return repr->port;
56         }
57
58         WARN(1, "Unknown netdev type for nfp_port\n");
59
60         return NULL;
61 }
62
63 static int
64 nfp_port_attr_get(struct net_device *netdev, struct switchdev_attr *attr)
65 {
66         struct nfp_port *port;
67
68         port = nfp_port_from_netdev(netdev);
69         if (!port)
70                 return -EOPNOTSUPP;
71
72         switch (attr->id) {
73         case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: {
74                 const u8 *serial;
75                 /* N.B: attr->u.ppid.id is binary data */
76                 attr->u.ppid.id_len = nfp_cpp_serial(port->app->cpp, &serial);
77                 memcpy(&attr->u.ppid.id, serial, attr->u.ppid.id_len);
78                 break;
79         }
80         default:
81                 return -EOPNOTSUPP;
82         }
83
84         return 0;
85 }
86
87 const struct switchdev_ops nfp_port_switchdev_ops = {
88         .switchdev_port_attr_get        = nfp_port_attr_get,
89 };
90
91 int nfp_port_setup_tc(struct net_device *netdev, enum tc_setup_type type,
92                       u32 handle, u32 chain_index, __be16 proto,
93                       struct tc_to_netdev *tc)
94 {
95         struct nfp_port *port;
96
97         if (chain_index)
98                 return -EOPNOTSUPP;
99
100         port = nfp_port_from_netdev(netdev);
101         if (!port)
102                 return -EOPNOTSUPP;
103
104         return nfp_app_setup_tc(port->app, netdev, type, handle, proto, tc);
105 }
106
107 struct nfp_port *
108 nfp_port_from_id(struct nfp_pf *pf, enum nfp_port_type type, unsigned int id)
109 {
110         struct nfp_port *port;
111
112         lockdep_assert_held(&pf->lock);
113
114         if (type != NFP_PORT_PHYS_PORT)
115                 return NULL;
116
117         list_for_each_entry(port, &pf->ports, port_list)
118                 if (port->eth_id == id)
119                         return port;
120
121         return NULL;
122 }
123
124 struct nfp_eth_table_port *__nfp_port_get_eth_port(struct nfp_port *port)
125 {
126         if (!port)
127                 return NULL;
128         if (port->type != NFP_PORT_PHYS_PORT)
129                 return NULL;
130
131         return port->eth_port;
132 }
133
134 struct nfp_eth_table_port *nfp_port_get_eth_port(struct nfp_port *port)
135 {
136         if (!__nfp_port_get_eth_port(port))
137                 return NULL;
138
139         if (test_bit(NFP_PORT_CHANGED, &port->flags))
140                 if (nfp_net_refresh_eth_port(port))
141                         return NULL;
142
143         return __nfp_port_get_eth_port(port);
144 }
145
146 int
147 nfp_port_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
148 {
149         struct nfp_eth_table_port *eth_port;
150         struct nfp_port *port;
151         int n;
152
153         port = nfp_port_from_netdev(netdev);
154         if (!port)
155                 return -EOPNOTSUPP;
156
157         switch (port->type) {
158         case NFP_PORT_PHYS_PORT:
159                 eth_port = __nfp_port_get_eth_port(port);
160                 if (!eth_port)
161                         return -EOPNOTSUPP;
162
163                 if (!eth_port->is_split)
164                         n = snprintf(name, len, "p%d", eth_port->label_port);
165                 else
166                         n = snprintf(name, len, "p%ds%d", eth_port->label_port,
167                                      eth_port->label_subport);
168                 break;
169         case NFP_PORT_PF_PORT:
170                 n = snprintf(name, len, "pf%d", port->pf_id);
171                 break;
172         case NFP_PORT_VF_PORT:
173                 n = snprintf(name, len, "pf%dvf%d", port->pf_id, port->vf_id);
174                 break;
175         default:
176                 return -EOPNOTSUPP;
177         }
178
179         if (n >= len)
180                 return -EINVAL;
181
182         return 0;
183 }
184
185 /**
186  * nfp_port_configure() - helper to set the interface configured bit
187  * @netdev:     net_device instance
188  * @configed:   Desired state
189  *
190  * Helper to set the ifup/ifdown state on the PHY only if there is a physical
191  * interface associated with the netdev.
192  *
193  * Return:
194  * 0 - configuration successful (or no change);
195  * -ERRNO - configuration failed.
196  */
197 int nfp_port_configure(struct net_device *netdev, bool configed)
198 {
199         struct nfp_eth_table_port *eth_port;
200         struct nfp_port *port;
201         int err;
202
203         port = nfp_port_from_netdev(netdev);
204         eth_port = __nfp_port_get_eth_port(port);
205         if (!eth_port)
206                 return 0;
207
208         err = nfp_eth_set_configured(port->app->cpp, eth_port->index, configed);
209         return err < 0 && err != -EOPNOTSUPP ? err : 0;
210 }
211
212 int nfp_port_init_phy_port(struct nfp_pf *pf, struct nfp_app *app,
213                            struct nfp_port *port, unsigned int id)
214 {
215         /* Check if vNIC has external port associated and cfg is OK */
216         if (!pf->eth_tbl || id >= pf->eth_tbl->count) {
217                 nfp_err(app->cpp,
218                         "NSP port entries don't match vNICs (no entry %d)\n",
219                         id);
220                 return -EINVAL;
221         }
222         if (pf->eth_tbl->ports[id].override_changed) {
223                 nfp_warn(app->cpp,
224                          "Config changed for port #%d, reboot required before port will be operational\n",
225                          pf->eth_tbl->ports[id].index);
226                 port->type = NFP_PORT_INVALID;
227                 return 0;
228         }
229
230         port->eth_port = &pf->eth_tbl->ports[id];
231         port->eth_id = pf->eth_tbl->ports[id].index;
232
233         return 0;
234 }
235
236 struct nfp_port *
237 nfp_port_alloc(struct nfp_app *app, enum nfp_port_type type,
238                struct net_device *netdev)
239 {
240         struct nfp_port *port;
241
242         port = kzalloc(sizeof(*port), GFP_KERNEL);
243         if (!port)
244                 return ERR_PTR(-ENOMEM);
245
246         port->netdev = netdev;
247         port->type = type;
248         port->app = app;
249
250         list_add_tail(&port->port_list, &app->pf->ports);
251
252         return port;
253 }
254
255 void nfp_port_free(struct nfp_port *port)
256 {
257         if (!port)
258                 return;
259         list_del(&port->port_list);
260         kfree(port);
261 }