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 <linux/netdevice.h>
36 #include <net/switchdev.h>
38 #include "nfpcore/nfp_cpp.h"
39 #include "nfpcore/nfp_nsp.h"
45 struct nfp_port *nfp_port_from_netdev(struct net_device *netdev)
47 if (nfp_netdev_is_nfp_net(netdev)) {
48 struct nfp_net *nn = netdev_priv(netdev);
53 if (nfp_netdev_is_nfp_repr(netdev)) {
54 struct nfp_repr *repr = netdev_priv(netdev);
59 WARN(1, "Unknown netdev type for nfp_port\n");
65 nfp_port_attr_get(struct net_device *netdev, struct switchdev_attr *attr)
67 struct nfp_port *port;
69 port = nfp_port_from_netdev(netdev);
74 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: {
76 /* N.B: attr->u.ppid.id is binary data */
77 attr->u.ppid.id_len = nfp_cpp_serial(port->app->cpp, &serial);
78 memcpy(&attr->u.ppid.id, serial, attr->u.ppid.id_len);
88 const struct switchdev_ops nfp_port_switchdev_ops = {
89 .switchdev_port_attr_get = nfp_port_attr_get,
92 int nfp_port_setup_tc(struct net_device *netdev, enum tc_setup_type type,
95 struct nfp_port *port;
97 port = nfp_port_from_netdev(netdev);
101 return nfp_app_setup_tc(port->app, netdev, type, type_data);
104 int nfp_port_set_features(struct net_device *netdev, netdev_features_t features)
106 struct nfp_port *port;
108 port = nfp_port_from_netdev(netdev);
112 if ((netdev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
113 port->tc_offload_cnt) {
114 netdev_err(netdev, "Cannot disable HW TC offload while offloads active\n");
122 nfp_port_from_id(struct nfp_pf *pf, enum nfp_port_type type, unsigned int id)
124 struct nfp_port *port;
126 lockdep_assert_held(&pf->lock);
128 if (type != NFP_PORT_PHYS_PORT)
131 list_for_each_entry(port, &pf->ports, port_list)
132 if (port->eth_id == id)
138 struct nfp_eth_table_port *__nfp_port_get_eth_port(struct nfp_port *port)
142 if (port->type != NFP_PORT_PHYS_PORT)
145 return port->eth_port;
148 struct nfp_eth_table_port *nfp_port_get_eth_port(struct nfp_port *port)
150 if (!__nfp_port_get_eth_port(port))
153 if (test_bit(NFP_PORT_CHANGED, &port->flags))
154 if (nfp_net_refresh_eth_port(port))
157 return __nfp_port_get_eth_port(port);
161 nfp_port_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
163 struct nfp_eth_table_port *eth_port;
164 struct nfp_port *port;
167 port = nfp_port_from_netdev(netdev);
171 switch (port->type) {
172 case NFP_PORT_PHYS_PORT:
173 eth_port = __nfp_port_get_eth_port(port);
177 if (!eth_port->is_split)
178 n = snprintf(name, len, "p%d", eth_port->label_port);
180 n = snprintf(name, len, "p%ds%d", eth_port->label_port,
181 eth_port->label_subport);
183 case NFP_PORT_PF_PORT:
185 n = snprintf(name, len, "pf%d", port->pf_id);
187 n = snprintf(name, len, "pf%ds%d", port->pf_id,
190 case NFP_PORT_VF_PORT:
191 n = snprintf(name, len, "pf%dvf%d", port->pf_id, port->vf_id);
204 * nfp_port_configure() - helper to set the interface configured bit
205 * @netdev: net_device instance
206 * @configed: Desired state
208 * Helper to set the ifup/ifdown state on the PHY only if there is a physical
209 * interface associated with the netdev.
212 * 0 - configuration successful (or no change);
213 * -ERRNO - configuration failed.
215 int nfp_port_configure(struct net_device *netdev, bool configed)
217 struct nfp_eth_table_port *eth_port;
218 struct nfp_port *port;
221 port = nfp_port_from_netdev(netdev);
222 eth_port = __nfp_port_get_eth_port(port);
225 if (port->eth_forced)
228 err = nfp_eth_set_configured(port->app->cpp, eth_port->index, configed);
229 return err < 0 && err != -EOPNOTSUPP ? err : 0;
232 int nfp_port_init_phy_port(struct nfp_pf *pf, struct nfp_app *app,
233 struct nfp_port *port, unsigned int id)
235 /* Check if vNIC has external port associated and cfg is OK */
236 if (!pf->eth_tbl || id >= pf->eth_tbl->count) {
238 "NSP port entries don't match vNICs (no entry %d)\n",
242 if (pf->eth_tbl->ports[id].override_changed) {
244 "Config changed for port #%d, reboot required before port will be operational\n",
245 pf->eth_tbl->ports[id].index);
246 port->type = NFP_PORT_INVALID;
250 port->eth_port = &pf->eth_tbl->ports[id];
251 port->eth_id = pf->eth_tbl->ports[id].index;
252 if (pf->mac_stats_mem)
254 pf->mac_stats_mem + port->eth_id * NFP_MAC_STATS_SIZE;
260 nfp_port_alloc(struct nfp_app *app, enum nfp_port_type type,
261 struct net_device *netdev)
263 struct nfp_port *port;
265 port = kzalloc(sizeof(*port), GFP_KERNEL);
267 return ERR_PTR(-ENOMEM);
269 port->netdev = netdev;
273 list_add_tail(&port->port_list, &app->pf->ports);
278 void nfp_port_free(struct nfp_port *port)
282 list_del(&port->port_list);