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