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 <net/switchdev.h> 36 37 #include "nfpcore/nfp_cpp.h" 38 #include "nfpcore/nfp_nsp.h" 39 #include "nfp_app.h" 40 #include "nfp_main.h" 41 #include "nfp_net.h" 42 #include "nfp_port.h" 43 44 struct nfp_port *nfp_port_from_netdev(struct net_device *netdev) 45 { 46 if (nfp_netdev_is_nfp_net(netdev)) { 47 struct nfp_net *nn = netdev_priv(netdev); 48 49 return nn->port; 50 } 51 52 if (nfp_netdev_is_nfp_repr(netdev)) { 53 struct nfp_repr *repr = netdev_priv(netdev); 54 55 return repr->port; 56 } 57 58 WARN(1, "Unknown netdev type for nfp_port\n"); 59 60 return NULL; 61 } 62 63 static int 64 nfp_port_attr_get(struct net_device *netdev, struct switchdev_attr *attr) 65 { 66 struct nfp_port *port; 67 68 port = nfp_port_from_netdev(netdev); 69 if (!port) 70 return -EOPNOTSUPP; 71 72 switch (attr->id) { 73 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: { 74 const u8 *serial; 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); 78 break; 79 } 80 default: 81 return -EOPNOTSUPP; 82 } 83 84 return 0; 85 } 86 87 const struct switchdev_ops nfp_port_switchdev_ops = { 88 .switchdev_port_attr_get = nfp_port_attr_get, 89 }; 90 91 int nfp_port_setup_tc(struct net_device *netdev, u32 handle, u32 chain_index, 92 __be16 proto, struct tc_to_netdev *tc) 93 { 94 struct nfp_port *port; 95 96 if (chain_index) 97 return -EOPNOTSUPP; 98 99 port = nfp_port_from_netdev(netdev); 100 if (!port) 101 return -EOPNOTSUPP; 102 103 return nfp_app_setup_tc(port->app, netdev, handle, proto, tc); 104 } 105 106 struct nfp_port * 107 nfp_port_from_id(struct nfp_pf *pf, enum nfp_port_type type, unsigned int id) 108 { 109 struct nfp_port *port; 110 111 lockdep_assert_held(&pf->lock); 112 113 if (type != NFP_PORT_PHYS_PORT) 114 return NULL; 115 116 list_for_each_entry(port, &pf->ports, port_list) 117 if (port->eth_id == id) 118 return port; 119 120 return NULL; 121 } 122 123 struct nfp_eth_table_port *__nfp_port_get_eth_port(struct nfp_port *port) 124 { 125 if (!port) 126 return NULL; 127 if (port->type != NFP_PORT_PHYS_PORT) 128 return NULL; 129 130 return port->eth_port; 131 } 132 133 struct nfp_eth_table_port *nfp_port_get_eth_port(struct nfp_port *port) 134 { 135 if (!__nfp_port_get_eth_port(port)) 136 return NULL; 137 138 if (test_bit(NFP_PORT_CHANGED, &port->flags)) 139 if (nfp_net_refresh_eth_port(port)) 140 return NULL; 141 142 return __nfp_port_get_eth_port(port); 143 } 144 145 int 146 nfp_port_get_phys_port_name(struct net_device *netdev, char *name, size_t len) 147 { 148 struct nfp_eth_table_port *eth_port; 149 struct nfp_port *port; 150 int n; 151 152 port = nfp_port_from_netdev(netdev); 153 if (!port) 154 return -EOPNOTSUPP; 155 156 switch (port->type) { 157 case NFP_PORT_PHYS_PORT: 158 eth_port = __nfp_port_get_eth_port(port); 159 if (!eth_port) 160 return -EOPNOTSUPP; 161 162 if (!eth_port->is_split) 163 n = snprintf(name, len, "p%d", eth_port->label_port); 164 else 165 n = snprintf(name, len, "p%ds%d", eth_port->label_port, 166 eth_port->label_subport); 167 break; 168 case NFP_PORT_PF_PORT: 169 n = snprintf(name, len, "pf%d", port->pf_id); 170 break; 171 case NFP_PORT_VF_PORT: 172 n = snprintf(name, len, "pf%dvf%d", port->pf_id, port->vf_id); 173 break; 174 default: 175 return -EOPNOTSUPP; 176 } 177 178 if (n >= len) 179 return -EINVAL; 180 181 return 0; 182 } 183 184 int nfp_port_init_phy_port(struct nfp_pf *pf, struct nfp_app *app, 185 struct nfp_port *port, unsigned int id) 186 { 187 /* Check if vNIC has external port associated and cfg is OK */ 188 if (!pf->eth_tbl || id >= pf->eth_tbl->count) { 189 nfp_err(app->cpp, 190 "NSP port entries don't match vNICs (no entry %d)\n", 191 id); 192 return -EINVAL; 193 } 194 if (pf->eth_tbl->ports[id].override_changed) { 195 nfp_warn(app->cpp, 196 "Config changed for port #%d, reboot required before port will be operational\n", 197 pf->eth_tbl->ports[id].index); 198 port->type = NFP_PORT_INVALID; 199 return 0; 200 } 201 202 port->eth_port = &pf->eth_tbl->ports[id]; 203 port->eth_id = pf->eth_tbl->ports[id].index; 204 205 return 0; 206 } 207 208 struct nfp_port * 209 nfp_port_alloc(struct nfp_app *app, enum nfp_port_type type, 210 struct net_device *netdev) 211 { 212 struct nfp_port *port; 213 214 port = kzalloc(sizeof(*port), GFP_KERNEL); 215 if (!port) 216 return ERR_PTR(-ENOMEM); 217 218 port->netdev = netdev; 219 port->type = type; 220 port->app = app; 221 222 list_add_tail(&port->port_list, &app->pf->ports); 223 224 return port; 225 } 226 227 void nfp_port_free(struct nfp_port *port) 228 { 229 if (!port) 230 return; 231 list_del(&port->port_list); 232 kfree(port); 233 } 234