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,
94 struct nfp_port *port;
96 port = nfp_port_from_netdev(netdev);
100 return nfp_app_setup_tc(port->app, netdev, type, type_data);
104 nfp_port_from_id(struct nfp_pf *pf, enum nfp_port_type type, unsigned int id)
106 struct nfp_port *port;
108 lockdep_assert_held(&pf->lock);
110 if (type != NFP_PORT_PHYS_PORT)
113 list_for_each_entry(port, &pf->ports, port_list)
114 if (port->eth_id == id)
120 struct nfp_eth_table_port *__nfp_port_get_eth_port(struct nfp_port *port)
124 if (port->type != NFP_PORT_PHYS_PORT)
127 return port->eth_port;
130 struct nfp_eth_table_port *nfp_port_get_eth_port(struct nfp_port *port)
132 if (!__nfp_port_get_eth_port(port))
135 if (test_bit(NFP_PORT_CHANGED, &port->flags))
136 if (nfp_net_refresh_eth_port(port))
139 return __nfp_port_get_eth_port(port);
143 nfp_port_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
145 struct nfp_eth_table_port *eth_port;
146 struct nfp_port *port;
149 port = nfp_port_from_netdev(netdev);
153 switch (port->type) {
154 case NFP_PORT_PHYS_PORT:
155 eth_port = __nfp_port_get_eth_port(port);
159 if (!eth_port->is_split)
160 n = snprintf(name, len, "p%d", eth_port->label_port);
162 n = snprintf(name, len, "p%ds%d", eth_port->label_port,
163 eth_port->label_subport);
165 case NFP_PORT_PF_PORT:
166 n = snprintf(name, len, "pf%d", port->pf_id);
168 case NFP_PORT_VF_PORT:
169 n = snprintf(name, len, "pf%dvf%d", port->pf_id, port->vf_id);
182 * nfp_port_configure() - helper to set the interface configured bit
183 * @netdev: net_device instance
184 * @configed: Desired state
186 * Helper to set the ifup/ifdown state on the PHY only if there is a physical
187 * interface associated with the netdev.
190 * 0 - configuration successful (or no change);
191 * -ERRNO - configuration failed.
193 int nfp_port_configure(struct net_device *netdev, bool configed)
195 struct nfp_eth_table_port *eth_port;
196 struct nfp_port *port;
199 port = nfp_port_from_netdev(netdev);
200 eth_port = __nfp_port_get_eth_port(port);
204 err = nfp_eth_set_configured(port->app->cpp, eth_port->index, configed);
205 return err < 0 && err != -EOPNOTSUPP ? err : 0;
208 int nfp_port_init_phy_port(struct nfp_pf *pf, struct nfp_app *app,
209 struct nfp_port *port, unsigned int id)
211 /* Check if vNIC has external port associated and cfg is OK */
212 if (!pf->eth_tbl || id >= pf->eth_tbl->count) {
214 "NSP port entries don't match vNICs (no entry %d)\n",
218 if (pf->eth_tbl->ports[id].override_changed) {
220 "Config changed for port #%d, reboot required before port will be operational\n",
221 pf->eth_tbl->ports[id].index);
222 port->type = NFP_PORT_INVALID;
226 port->eth_port = &pf->eth_tbl->ports[id];
227 port->eth_id = pf->eth_tbl->ports[id].index;
228 if (pf->mac_stats_mem)
230 pf->mac_stats_mem + port->eth_id * NFP_MAC_STATS_SIZE;
236 nfp_port_alloc(struct nfp_app *app, enum nfp_port_type type,
237 struct net_device *netdev)
239 struct nfp_port *port;
241 port = kzalloc(sizeof(*port), GFP_KERNEL);
243 return ERR_PTR(-ENOMEM);
245 port->netdev = netdev;
249 list_add_tail(&port->port_list, &app->pf->ports);
254 void nfp_port_free(struct nfp_port *port)
258 list_del(&port->port_list);