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