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