2 * Copyright (C) 2017 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/lockdep.h>
35 #include <net/switchdev.h>
37 #include "nfpcore/nfp_cpp.h"
38 #include "nfpcore/nfp_nsp.h"
44 struct nfp_port *nfp_port_from_netdev(struct net_device *netdev)
46 if (nfp_netdev_is_nfp_net(netdev)) {
47 struct nfp_net *nn = netdev_priv(netdev);
52 if (nfp_netdev_is_nfp_repr(netdev)) {
53 struct nfp_repr *repr = netdev_priv(netdev);
58 WARN(1, "Unknown netdev type for nfp_port\n");
64 nfp_port_attr_get(struct net_device *netdev, struct switchdev_attr *attr)
66 struct nfp_port *port;
68 port = nfp_port_from_netdev(netdev);
73 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: {
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);
87 const struct switchdev_ops nfp_port_switchdev_ops = {
88 .switchdev_port_attr_get = nfp_port_attr_get,
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)
95 struct nfp_port *port;
100 port = nfp_port_from_netdev(netdev);
104 return nfp_app_setup_tc(port->app, netdev, type, handle, proto, tc);
108 nfp_port_from_id(struct nfp_pf *pf, enum nfp_port_type type, unsigned int id)
110 struct nfp_port *port;
112 lockdep_assert_held(&pf->lock);
114 if (type != NFP_PORT_PHYS_PORT)
117 list_for_each_entry(port, &pf->ports, port_list)
118 if (port->eth_id == id)
124 struct nfp_eth_table_port *__nfp_port_get_eth_port(struct nfp_port *port)
128 if (port->type != NFP_PORT_PHYS_PORT)
131 return port->eth_port;
134 struct nfp_eth_table_port *nfp_port_get_eth_port(struct nfp_port *port)
136 if (!__nfp_port_get_eth_port(port))
139 if (test_bit(NFP_PORT_CHANGED, &port->flags))
140 if (nfp_net_refresh_eth_port(port))
143 return __nfp_port_get_eth_port(port);
147 nfp_port_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
149 struct nfp_eth_table_port *eth_port;
150 struct nfp_port *port;
153 port = nfp_port_from_netdev(netdev);
157 switch (port->type) {
158 case NFP_PORT_PHYS_PORT:
159 eth_port = __nfp_port_get_eth_port(port);
163 if (!eth_port->is_split)
164 n = snprintf(name, len, "p%d", eth_port->label_port);
166 n = snprintf(name, len, "p%ds%d", eth_port->label_port,
167 eth_port->label_subport);
169 case NFP_PORT_PF_PORT:
170 n = snprintf(name, len, "pf%d", port->pf_id);
172 case NFP_PORT_VF_PORT:
173 n = snprintf(name, len, "pf%dvf%d", port->pf_id, port->vf_id);
186 * nfp_port_configure() - helper to set the interface configured bit
187 * @netdev: net_device instance
188 * @configed: Desired state
190 * Helper to set the ifup/ifdown state on the PHY only if there is a physical
191 * interface associated with the netdev.
194 * 0 - configuration successful (or no change);
195 * -ERRNO - configuration failed.
197 int nfp_port_configure(struct net_device *netdev, bool configed)
199 struct nfp_eth_table_port *eth_port;
200 struct nfp_port *port;
203 port = nfp_port_from_netdev(netdev);
204 eth_port = __nfp_port_get_eth_port(port);
208 err = nfp_eth_set_configured(port->app->cpp, eth_port->index, configed);
209 return err < 0 && err != -EOPNOTSUPP ? err : 0;
212 int nfp_port_init_phy_port(struct nfp_pf *pf, struct nfp_app *app,
213 struct nfp_port *port, unsigned int id)
215 /* Check if vNIC has external port associated and cfg is OK */
216 if (!pf->eth_tbl || id >= pf->eth_tbl->count) {
218 "NSP port entries don't match vNICs (no entry %d)\n",
222 if (pf->eth_tbl->ports[id].override_changed) {
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;
230 port->eth_port = &pf->eth_tbl->ports[id];
231 port->eth_id = pf->eth_tbl->ports[id].index;
237 nfp_port_alloc(struct nfp_app *app, enum nfp_port_type type,
238 struct net_device *netdev)
240 struct nfp_port *port;
242 port = kzalloc(sizeof(*port), GFP_KERNEL);
244 return ERR_PTR(-ENOMEM);
246 port->netdev = netdev;
250 list_add_tail(&port->port_list, &app->pf->ports);
255 void nfp_port_free(struct nfp_port *port)
259 list_del(&port->port_list);