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 		n = snprintf(name, len, "pf%d", port->pf_id);
185 		break;
186 	case NFP_PORT_VF_PORT:
187 		n = snprintf(name, len, "pf%dvf%d", port->pf_id, port->vf_id);
188 		break;
189 	default:
190 		return -EOPNOTSUPP;
191 	}
192 
193 	if (n >= len)
194 		return -EINVAL;
195 
196 	return 0;
197 }
198 
199 /**
200  * nfp_port_configure() - helper to set the interface configured bit
201  * @netdev:	net_device instance
202  * @configed:	Desired state
203  *
204  * Helper to set the ifup/ifdown state on the PHY only if there is a physical
205  * interface associated with the netdev.
206  *
207  * Return:
208  * 0 - configuration successful (or no change);
209  * -ERRNO - configuration failed.
210  */
211 int nfp_port_configure(struct net_device *netdev, bool configed)
212 {
213 	struct nfp_eth_table_port *eth_port;
214 	struct nfp_port *port;
215 	int err;
216 
217 	port = nfp_port_from_netdev(netdev);
218 	eth_port = __nfp_port_get_eth_port(port);
219 	if (!eth_port)
220 		return 0;
221 
222 	err = nfp_eth_set_configured(port->app->cpp, eth_port->index, configed);
223 	return err < 0 && err != -EOPNOTSUPP ? err : 0;
224 }
225 
226 int nfp_port_init_phy_port(struct nfp_pf *pf, struct nfp_app *app,
227 			   struct nfp_port *port, unsigned int id)
228 {
229 	/* Check if vNIC has external port associated and cfg is OK */
230 	if (!pf->eth_tbl || id >= pf->eth_tbl->count) {
231 		nfp_err(app->cpp,
232 			"NSP port entries don't match vNICs (no entry %d)\n",
233 			id);
234 		return -EINVAL;
235 	}
236 	if (pf->eth_tbl->ports[id].override_changed) {
237 		nfp_warn(app->cpp,
238 			 "Config changed for port #%d, reboot required before port will be operational\n",
239 			 pf->eth_tbl->ports[id].index);
240 		port->type = NFP_PORT_INVALID;
241 		return 0;
242 	}
243 
244 	port->eth_port = &pf->eth_tbl->ports[id];
245 	port->eth_id = pf->eth_tbl->ports[id].index;
246 	if (pf->mac_stats_mem)
247 		port->eth_stats =
248 			pf->mac_stats_mem + port->eth_id * NFP_MAC_STATS_SIZE;
249 
250 	return 0;
251 }
252 
253 struct nfp_port *
254 nfp_port_alloc(struct nfp_app *app, enum nfp_port_type type,
255 	       struct net_device *netdev)
256 {
257 	struct nfp_port *port;
258 
259 	port = kzalloc(sizeof(*port), GFP_KERNEL);
260 	if (!port)
261 		return ERR_PTR(-ENOMEM);
262 
263 	port->netdev = netdev;
264 	port->type = type;
265 	port->app = app;
266 
267 	list_add_tail(&port->port_list, &app->pf->ports);
268 
269 	return port;
270 }
271 
272 void nfp_port_free(struct nfp_port *port)
273 {
274 	if (!port)
275 		return;
276 	list_del(&port->port_list);
277 	kfree(port);
278 }
279