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