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/etherdevice.h>
35 #include <linux/io-64-nonatomic-hi-lo.h>
36 #include <linux/lockdep.h>
37 #include <net/dst_metadata.h>
38 #include <net/switchdev.h>
39 
40 #include "nfpcore/nfp_cpp.h"
41 #include "nfpcore/nfp_nsp.h"
42 #include "nfp_app.h"
43 #include "nfp_main.h"
44 #include "nfp_net_ctrl.h"
45 #include "nfp_net_repr.h"
46 #include "nfp_net_sriov.h"
47 #include "nfp_port.h"
48 
49 struct net_device *
50 nfp_repr_get_locked(struct nfp_app *app, struct nfp_reprs *set, unsigned int id)
51 {
52 	return rcu_dereference_protected(set->reprs[id],
53 					 lockdep_is_held(&app->pf->lock));
54 }
55 
56 static void
57 nfp_repr_inc_tx_stats(struct net_device *netdev, unsigned int len,
58 		      int tx_status)
59 {
60 	struct nfp_repr *repr = netdev_priv(netdev);
61 	struct nfp_repr_pcpu_stats *stats;
62 
63 	if (unlikely(tx_status != NET_XMIT_SUCCESS &&
64 		     tx_status != NET_XMIT_CN)) {
65 		this_cpu_inc(repr->stats->tx_drops);
66 		return;
67 	}
68 
69 	stats = this_cpu_ptr(repr->stats);
70 	u64_stats_update_begin(&stats->syncp);
71 	stats->tx_packets++;
72 	stats->tx_bytes += len;
73 	u64_stats_update_end(&stats->syncp);
74 }
75 
76 void nfp_repr_inc_rx_stats(struct net_device *netdev, unsigned int len)
77 {
78 	struct nfp_repr *repr = netdev_priv(netdev);
79 	struct nfp_repr_pcpu_stats *stats;
80 
81 	stats = this_cpu_ptr(repr->stats);
82 	u64_stats_update_begin(&stats->syncp);
83 	stats->rx_packets++;
84 	stats->rx_bytes += len;
85 	u64_stats_update_end(&stats->syncp);
86 }
87 
88 static void
89 nfp_repr_phy_port_get_stats64(struct nfp_port *port,
90 			      struct rtnl_link_stats64 *stats)
91 {
92 	u8 __iomem *mem = port->eth_stats;
93 
94 	stats->tx_packets = readq(mem + NFP_MAC_STATS_TX_FRAMES_TRANSMITTED_OK);
95 	stats->tx_bytes = readq(mem + NFP_MAC_STATS_TX_OUT_OCTETS);
96 	stats->tx_dropped = readq(mem + NFP_MAC_STATS_TX_OUT_ERRORS);
97 
98 	stats->rx_packets = readq(mem + NFP_MAC_STATS_RX_FRAMES_RECEIVED_OK);
99 	stats->rx_bytes = readq(mem + NFP_MAC_STATS_RX_IN_OCTETS);
100 	stats->rx_dropped = readq(mem + NFP_MAC_STATS_RX_IN_ERRORS);
101 }
102 
103 static void
104 nfp_repr_vnic_get_stats64(struct nfp_port *port,
105 			  struct rtnl_link_stats64 *stats)
106 {
107 	/* TX and RX stats are flipped as we are returning the stats as seen
108 	 * at the switch port corresponding to the VF.
109 	 */
110 	stats->tx_packets = readq(port->vnic + NFP_NET_CFG_STATS_RX_FRAMES);
111 	stats->tx_bytes = readq(port->vnic + NFP_NET_CFG_STATS_RX_OCTETS);
112 	stats->tx_dropped = readq(port->vnic + NFP_NET_CFG_STATS_RX_DISCARDS);
113 
114 	stats->rx_packets = readq(port->vnic + NFP_NET_CFG_STATS_TX_FRAMES);
115 	stats->rx_bytes = readq(port->vnic + NFP_NET_CFG_STATS_TX_OCTETS);
116 	stats->rx_dropped = readq(port->vnic + NFP_NET_CFG_STATS_TX_DISCARDS);
117 }
118 
119 static void
120 nfp_repr_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
121 {
122 	struct nfp_repr *repr = netdev_priv(netdev);
123 
124 	if (WARN_ON(!repr->port))
125 		return;
126 
127 	switch (repr->port->type) {
128 	case NFP_PORT_PHYS_PORT:
129 		if (!__nfp_port_get_eth_port(repr->port))
130 			break;
131 		nfp_repr_phy_port_get_stats64(repr->port, stats);
132 		break;
133 	case NFP_PORT_PF_PORT:
134 	case NFP_PORT_VF_PORT:
135 		nfp_repr_vnic_get_stats64(repr->port, stats);
136 	default:
137 		break;
138 	}
139 }
140 
141 static bool
142 nfp_repr_has_offload_stats(const struct net_device *dev, int attr_id)
143 {
144 	switch (attr_id) {
145 	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
146 		return true;
147 	}
148 
149 	return false;
150 }
151 
152 static int
153 nfp_repr_get_host_stats64(const struct net_device *netdev,
154 			  struct rtnl_link_stats64 *stats)
155 {
156 	struct nfp_repr *repr = netdev_priv(netdev);
157 	int i;
158 
159 	for_each_possible_cpu(i) {
160 		u64 tbytes, tpkts, tdrops, rbytes, rpkts;
161 		struct nfp_repr_pcpu_stats *repr_stats;
162 		unsigned int start;
163 
164 		repr_stats = per_cpu_ptr(repr->stats, i);
165 		do {
166 			start = u64_stats_fetch_begin_irq(&repr_stats->syncp);
167 			tbytes = repr_stats->tx_bytes;
168 			tpkts = repr_stats->tx_packets;
169 			tdrops = repr_stats->tx_drops;
170 			rbytes = repr_stats->rx_bytes;
171 			rpkts = repr_stats->rx_packets;
172 		} while (u64_stats_fetch_retry_irq(&repr_stats->syncp, start));
173 
174 		stats->tx_bytes += tbytes;
175 		stats->tx_packets += tpkts;
176 		stats->tx_dropped += tdrops;
177 		stats->rx_bytes += rbytes;
178 		stats->rx_packets += rpkts;
179 	}
180 
181 	return 0;
182 }
183 
184 static int
185 nfp_repr_get_offload_stats(int attr_id, const struct net_device *dev,
186 			   void *stats)
187 {
188 	switch (attr_id) {
189 	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
190 		return nfp_repr_get_host_stats64(dev, stats);
191 	}
192 
193 	return -EINVAL;
194 }
195 
196 static int nfp_repr_change_mtu(struct net_device *netdev, int new_mtu)
197 {
198 	struct nfp_repr *repr = netdev_priv(netdev);
199 	int err;
200 
201 	err = nfp_app_check_mtu(repr->app, netdev, new_mtu);
202 	if (err)
203 		return err;
204 
205 	err = nfp_app_repr_change_mtu(repr->app, netdev, new_mtu);
206 	if (err)
207 		return err;
208 
209 	netdev->mtu = new_mtu;
210 
211 	return 0;
212 }
213 
214 static netdev_tx_t nfp_repr_xmit(struct sk_buff *skb, struct net_device *netdev)
215 {
216 	struct nfp_repr *repr = netdev_priv(netdev);
217 	unsigned int len = skb->len;
218 	int ret;
219 
220 	skb_dst_drop(skb);
221 	dst_hold((struct dst_entry *)repr->dst);
222 	skb_dst_set(skb, (struct dst_entry *)repr->dst);
223 	skb->dev = repr->dst->u.port_info.lower_dev;
224 
225 	ret = dev_queue_xmit(skb);
226 	nfp_repr_inc_tx_stats(netdev, len, ret);
227 
228 	return ret;
229 }
230 
231 static int nfp_repr_stop(struct net_device *netdev)
232 {
233 	struct nfp_repr *repr = netdev_priv(netdev);
234 	int err;
235 
236 	err = nfp_app_repr_stop(repr->app, repr);
237 	if (err)
238 		return err;
239 
240 	nfp_port_configure(netdev, false);
241 	return 0;
242 }
243 
244 static int nfp_repr_open(struct net_device *netdev)
245 {
246 	struct nfp_repr *repr = netdev_priv(netdev);
247 	int err;
248 
249 	err = nfp_port_configure(netdev, true);
250 	if (err)
251 		return err;
252 
253 	err = nfp_app_repr_open(repr->app, repr);
254 	if (err)
255 		goto err_port_disable;
256 
257 	return 0;
258 
259 err_port_disable:
260 	nfp_port_configure(netdev, false);
261 	return err;
262 }
263 
264 const struct net_device_ops nfp_repr_netdev_ops = {
265 	.ndo_open		= nfp_repr_open,
266 	.ndo_stop		= nfp_repr_stop,
267 	.ndo_start_xmit		= nfp_repr_xmit,
268 	.ndo_change_mtu		= nfp_repr_change_mtu,
269 	.ndo_get_stats64	= nfp_repr_get_stats64,
270 	.ndo_has_offload_stats	= nfp_repr_has_offload_stats,
271 	.ndo_get_offload_stats	= nfp_repr_get_offload_stats,
272 	.ndo_get_phys_port_name	= nfp_port_get_phys_port_name,
273 	.ndo_setup_tc		= nfp_port_setup_tc,
274 	.ndo_set_vf_mac		= nfp_app_set_vf_mac,
275 	.ndo_set_vf_vlan	= nfp_app_set_vf_vlan,
276 	.ndo_set_vf_spoofchk	= nfp_app_set_vf_spoofchk,
277 	.ndo_get_vf_config	= nfp_app_get_vf_config,
278 	.ndo_set_vf_link_state	= nfp_app_set_vf_link_state,
279 	.ndo_set_features	= nfp_port_set_features,
280 	.ndo_set_mac_address    = eth_mac_addr,
281 };
282 
283 static void nfp_repr_clean(struct nfp_repr *repr)
284 {
285 	unregister_netdev(repr->netdev);
286 	nfp_app_repr_clean(repr->app, repr->netdev);
287 	dst_release((struct dst_entry *)repr->dst);
288 	nfp_port_free(repr->port);
289 }
290 
291 static struct lock_class_key nfp_repr_netdev_xmit_lock_key;
292 static struct lock_class_key nfp_repr_netdev_addr_lock_key;
293 
294 static void nfp_repr_set_lockdep_class_one(struct net_device *dev,
295 					   struct netdev_queue *txq,
296 					   void *_unused)
297 {
298 	lockdep_set_class(&txq->_xmit_lock, &nfp_repr_netdev_xmit_lock_key);
299 }
300 
301 static void nfp_repr_set_lockdep_class(struct net_device *dev)
302 {
303 	lockdep_set_class(&dev->addr_list_lock, &nfp_repr_netdev_addr_lock_key);
304 	netdev_for_each_tx_queue(dev, nfp_repr_set_lockdep_class_one, NULL);
305 }
306 
307 int nfp_repr_init(struct nfp_app *app, struct net_device *netdev,
308 		  u32 cmsg_port_id, struct nfp_port *port,
309 		  struct net_device *pf_netdev)
310 {
311 	struct nfp_repr *repr = netdev_priv(netdev);
312 	int err;
313 
314 	nfp_repr_set_lockdep_class(netdev);
315 
316 	repr->port = port;
317 	repr->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX, GFP_KERNEL);
318 	if (!repr->dst)
319 		return -ENOMEM;
320 	repr->dst->u.port_info.port_id = cmsg_port_id;
321 	repr->dst->u.port_info.lower_dev = pf_netdev;
322 
323 	netdev->netdev_ops = &nfp_repr_netdev_ops;
324 	netdev->ethtool_ops = &nfp_port_ethtool_ops;
325 
326 	netdev->max_mtu = pf_netdev->max_mtu;
327 
328 	SWITCHDEV_SET_OPS(netdev, &nfp_port_switchdev_ops);
329 
330 	if (nfp_app_has_tc(app)) {
331 		netdev->features |= NETIF_F_HW_TC;
332 		netdev->hw_features |= NETIF_F_HW_TC;
333 	}
334 
335 	err = nfp_app_repr_init(app, netdev);
336 	if (err)
337 		goto err_clean;
338 
339 	err = register_netdev(netdev);
340 	if (err)
341 		goto err_repr_clean;
342 
343 	return 0;
344 
345 err_repr_clean:
346 	nfp_app_repr_clean(app, netdev);
347 err_clean:
348 	dst_release((struct dst_entry *)repr->dst);
349 	return err;
350 }
351 
352 static void __nfp_repr_free(struct nfp_repr *repr)
353 {
354 	free_percpu(repr->stats);
355 	free_netdev(repr->netdev);
356 }
357 
358 void nfp_repr_free(struct net_device *netdev)
359 {
360 	__nfp_repr_free(netdev_priv(netdev));
361 }
362 
363 struct net_device *
364 nfp_repr_alloc_mqs(struct nfp_app *app, unsigned int txqs, unsigned int rxqs)
365 {
366 	struct net_device *netdev;
367 	struct nfp_repr *repr;
368 
369 	netdev = alloc_etherdev_mqs(sizeof(*repr), txqs, rxqs);
370 	if (!netdev)
371 		return NULL;
372 
373 	netif_carrier_off(netdev);
374 
375 	repr = netdev_priv(netdev);
376 	repr->netdev = netdev;
377 	repr->app = app;
378 
379 	repr->stats = netdev_alloc_pcpu_stats(struct nfp_repr_pcpu_stats);
380 	if (!repr->stats)
381 		goto err_free_netdev;
382 
383 	return netdev;
384 
385 err_free_netdev:
386 	free_netdev(netdev);
387 	return NULL;
388 }
389 
390 void nfp_repr_clean_and_free(struct nfp_repr *repr)
391 {
392 	nfp_info(repr->app->cpp, "Destroying Representor(%s)\n",
393 		 repr->netdev->name);
394 	nfp_repr_clean(repr);
395 	__nfp_repr_free(repr);
396 }
397 
398 void nfp_reprs_clean_and_free(struct nfp_app *app, struct nfp_reprs *reprs)
399 {
400 	struct net_device *netdev;
401 	unsigned int i;
402 
403 	for (i = 0; i < reprs->num_reprs; i++) {
404 		netdev = nfp_repr_get_locked(app, reprs, i);
405 		if (netdev)
406 			nfp_repr_clean_and_free(netdev_priv(netdev));
407 	}
408 
409 	kfree(reprs);
410 }
411 
412 void
413 nfp_reprs_clean_and_free_by_type(struct nfp_app *app, enum nfp_repr_type type)
414 {
415 	struct net_device *netdev;
416 	struct nfp_reprs *reprs;
417 	int i;
418 
419 	reprs = rcu_dereference_protected(app->reprs[type],
420 					  lockdep_is_held(&app->pf->lock));
421 	if (!reprs)
422 		return;
423 
424 	/* Preclean must happen before we remove the reprs reference from the
425 	 * app below.
426 	 */
427 	for (i = 0; i < reprs->num_reprs; i++) {
428 		netdev = nfp_repr_get_locked(app, reprs, i);
429 		if (netdev)
430 			nfp_app_repr_preclean(app, netdev);
431 	}
432 
433 	reprs = nfp_app_reprs_set(app, type, NULL);
434 
435 	synchronize_rcu();
436 	nfp_reprs_clean_and_free(app, reprs);
437 }
438 
439 struct nfp_reprs *nfp_reprs_alloc(unsigned int num_reprs)
440 {
441 	struct nfp_reprs *reprs;
442 
443 	reprs = kzalloc(sizeof(*reprs) +
444 			num_reprs * sizeof(struct net_device *), GFP_KERNEL);
445 	if (!reprs)
446 		return NULL;
447 	reprs->num_reprs = num_reprs;
448 
449 	return reprs;
450 }
451 
452 int nfp_reprs_resync_phys_ports(struct nfp_app *app)
453 {
454 	struct net_device *netdev;
455 	struct nfp_reprs *reprs;
456 	struct nfp_repr *repr;
457 	int i;
458 
459 	reprs = nfp_reprs_get_locked(app, NFP_REPR_TYPE_PHYS_PORT);
460 	if (!reprs)
461 		return 0;
462 
463 	for (i = 0; i < reprs->num_reprs; i++) {
464 		netdev = nfp_repr_get_locked(app, reprs, i);
465 		if (!netdev)
466 			continue;
467 
468 		repr = netdev_priv(netdev);
469 		if (repr->port->type != NFP_PORT_INVALID)
470 			continue;
471 
472 		nfp_app_repr_preclean(app, netdev);
473 		rcu_assign_pointer(reprs->reprs[i], NULL);
474 		synchronize_rcu();
475 		nfp_repr_clean(repr);
476 	}
477 
478 	return 0;
479 }
480