1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
4 #include <linux/lockdep.h>
5 #include <linux/netdevice.h>
6 #include <net/switchdev.h>
8 #include "nfpcore/nfp_cpp.h"
9 #include "nfpcore/nfp_nsp.h"
15 struct nfp_port *nfp_port_from_netdev(struct net_device *netdev)
17 if (nfp_netdev_is_nfp_net(netdev)) {
18 struct nfp_net *nn = netdev_priv(netdev);
23 if (nfp_netdev_is_nfp_repr(netdev)) {
24 struct nfp_repr *repr = netdev_priv(netdev);
29 WARN(1, "Unknown netdev type for nfp_port\n");
35 nfp_port_attr_get(struct net_device *netdev, struct switchdev_attr *attr)
37 struct nfp_port *port;
39 port = nfp_port_from_netdev(netdev);
44 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: {
46 /* N.B: attr->u.ppid.id is binary data */
47 attr->u.ppid.id_len = nfp_cpp_serial(port->app->cpp, &serial);
48 memcpy(&attr->u.ppid.id, serial, attr->u.ppid.id_len);
58 const struct switchdev_ops nfp_port_switchdev_ops = {
59 .switchdev_port_attr_get = nfp_port_attr_get,
62 int nfp_port_setup_tc(struct net_device *netdev, enum tc_setup_type type,
65 struct nfp_port *port;
67 port = nfp_port_from_netdev(netdev);
71 return nfp_app_setup_tc(port->app, netdev, type, type_data);
74 int nfp_port_set_features(struct net_device *netdev, netdev_features_t features)
76 struct nfp_port *port;
78 port = nfp_port_from_netdev(netdev);
82 if ((netdev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
83 port->tc_offload_cnt) {
84 netdev_err(netdev, "Cannot disable HW TC offload while offloads active\n");
92 nfp_port_from_id(struct nfp_pf *pf, enum nfp_port_type type, unsigned int id)
94 struct nfp_port *port;
96 lockdep_assert_held(&pf->lock);
98 if (type != NFP_PORT_PHYS_PORT)
101 list_for_each_entry(port, &pf->ports, port_list)
102 if (port->eth_id == id)
108 struct nfp_eth_table_port *__nfp_port_get_eth_port(struct nfp_port *port)
112 if (port->type != NFP_PORT_PHYS_PORT)
115 return port->eth_port;
118 struct nfp_eth_table_port *nfp_port_get_eth_port(struct nfp_port *port)
120 if (!__nfp_port_get_eth_port(port))
123 if (test_bit(NFP_PORT_CHANGED, &port->flags))
124 if (nfp_net_refresh_eth_port(port))
127 return __nfp_port_get_eth_port(port);
131 nfp_port_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
133 struct nfp_eth_table_port *eth_port;
134 struct nfp_port *port;
137 port = nfp_port_from_netdev(netdev);
141 switch (port->type) {
142 case NFP_PORT_PHYS_PORT:
143 eth_port = __nfp_port_get_eth_port(port);
147 if (!eth_port->is_split)
148 n = snprintf(name, len, "p%d", eth_port->label_port);
150 n = snprintf(name, len, "p%ds%d", eth_port->label_port,
151 eth_port->label_subport);
153 case NFP_PORT_PF_PORT:
155 n = snprintf(name, len, "pf%d", port->pf_id);
157 n = snprintf(name, len, "pf%ds%d", port->pf_id,
160 case NFP_PORT_VF_PORT:
161 n = snprintf(name, len, "pf%dvf%d", port->pf_id, port->vf_id);
174 * nfp_port_configure() - helper to set the interface configured bit
175 * @netdev: net_device instance
176 * @configed: Desired state
178 * Helper to set the ifup/ifdown state on the PHY only if there is a physical
179 * interface associated with the netdev.
182 * 0 - configuration successful (or no change);
183 * -ERRNO - configuration failed.
185 int nfp_port_configure(struct net_device *netdev, bool configed)
187 struct nfp_eth_table_port *eth_port;
188 struct nfp_port *port;
191 port = nfp_port_from_netdev(netdev);
192 eth_port = __nfp_port_get_eth_port(port);
195 if (port->eth_forced)
198 err = nfp_eth_set_configured(port->app->cpp, eth_port->index, configed);
199 return err < 0 && err != -EOPNOTSUPP ? err : 0;
202 int nfp_port_init_phy_port(struct nfp_pf *pf, struct nfp_app *app,
203 struct nfp_port *port, unsigned int id)
205 /* Check if vNIC has external port associated and cfg is OK */
206 if (!pf->eth_tbl || id >= pf->eth_tbl->count) {
208 "NSP port entries don't match vNICs (no entry %d)\n",
212 if (pf->eth_tbl->ports[id].override_changed) {
214 "Config changed for port #%d, reboot required before port will be operational\n",
215 pf->eth_tbl->ports[id].index);
216 port->type = NFP_PORT_INVALID;
220 port->eth_port = &pf->eth_tbl->ports[id];
221 port->eth_id = pf->eth_tbl->ports[id].index;
222 if (pf->mac_stats_mem)
224 pf->mac_stats_mem + port->eth_id * NFP_MAC_STATS_SIZE;
230 nfp_port_alloc(struct nfp_app *app, enum nfp_port_type type,
231 struct net_device *netdev)
233 struct nfp_port *port;
235 port = kzalloc(sizeof(*port), GFP_KERNEL);
237 return ERR_PTR(-ENOMEM);
239 port->netdev = netdev;
243 list_add_tail(&port->port_list, &app->pf->ports);
248 void nfp_port_free(struct nfp_port *port)
252 list_del(&port->port_list);