2 * Copyright (C) 2017 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
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>
40 #include "nfpcore/nfp_cpp.h"
41 #include "nfpcore/nfp_nsp.h"
44 #include "nfp_net_ctrl.h"
45 #include "nfp_net_repr.h"
46 #include "nfp_net_sriov.h"
50 nfp_repr_get_locked(struct nfp_app
*app
, struct nfp_reprs
*set
, unsigned int id
)
52 return rcu_dereference_protected(set
->reprs
[id
],
53 lockdep_is_held(&app
->pf
->lock
));
57 nfp_repr_inc_tx_stats(struct net_device
*netdev
, unsigned int len
,
60 struct nfp_repr
*repr
= netdev_priv(netdev
);
61 struct nfp_repr_pcpu_stats
*stats
;
63 if (unlikely(tx_status
!= NET_XMIT_SUCCESS
&&
64 tx_status
!= NET_XMIT_CN
)) {
65 this_cpu_inc(repr
->stats
->tx_drops
);
69 stats
= this_cpu_ptr(repr
->stats
);
70 u64_stats_update_begin(&stats
->syncp
);
72 stats
->tx_bytes
+= len
;
73 u64_stats_update_end(&stats
->syncp
);
76 void nfp_repr_inc_rx_stats(struct net_device
*netdev
, unsigned int len
)
78 struct nfp_repr
*repr
= netdev_priv(netdev
);
79 struct nfp_repr_pcpu_stats
*stats
;
81 stats
= this_cpu_ptr(repr
->stats
);
82 u64_stats_update_begin(&stats
->syncp
);
84 stats
->rx_bytes
+= len
;
85 u64_stats_update_end(&stats
->syncp
);
89 nfp_repr_phy_port_get_stats64(struct nfp_port
*port
,
90 struct rtnl_link_stats64
*stats
)
92 u8 __iomem
*mem
= port
->eth_stats
;
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
);
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
);
104 nfp_repr_vnic_get_stats64(struct nfp_port
*port
,
105 struct rtnl_link_stats64
*stats
)
107 /* TX and RX stats are flipped as we are returning the stats as seen
108 * at the switch port corresponding to the VF.
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
);
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
);
120 nfp_repr_get_stats64(struct net_device
*netdev
, struct rtnl_link_stats64
*stats
)
122 struct nfp_repr
*repr
= netdev_priv(netdev
);
124 if (WARN_ON(!repr
->port
))
127 switch (repr
->port
->type
) {
128 case NFP_PORT_PHYS_PORT
:
129 if (!__nfp_port_get_eth_port(repr
->port
))
131 nfp_repr_phy_port_get_stats64(repr
->port
, stats
);
133 case NFP_PORT_PF_PORT
:
134 case NFP_PORT_VF_PORT
:
135 nfp_repr_vnic_get_stats64(repr
->port
, stats
);
142 nfp_repr_has_offload_stats(const struct net_device
*dev
, int attr_id
)
145 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
153 nfp_repr_get_host_stats64(const struct net_device
*netdev
,
154 struct rtnl_link_stats64
*stats
)
156 struct nfp_repr
*repr
= netdev_priv(netdev
);
159 for_each_possible_cpu(i
) {
160 u64 tbytes
, tpkts
, tdrops
, rbytes
, rpkts
;
161 struct nfp_repr_pcpu_stats
*repr_stats
;
164 repr_stats
= per_cpu_ptr(repr
->stats
, i
);
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
));
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
;
185 nfp_repr_get_offload_stats(int attr_id
, const struct net_device
*dev
,
189 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
190 return nfp_repr_get_host_stats64(dev
, stats
);
196 static int nfp_repr_change_mtu(struct net_device
*netdev
, int new_mtu
)
198 struct nfp_repr
*repr
= netdev_priv(netdev
);
200 return nfp_app_change_mtu(repr
->app
, netdev
, new_mtu
);
203 static netdev_tx_t
nfp_repr_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
205 struct nfp_repr
*repr
= netdev_priv(netdev
);
206 unsigned int len
= skb
->len
;
210 dst_hold((struct dst_entry
*)repr
->dst
);
211 skb_dst_set(skb
, (struct dst_entry
*)repr
->dst
);
212 skb
->dev
= repr
->dst
->u
.port_info
.lower_dev
;
214 ret
= dev_queue_xmit(skb
);
215 nfp_repr_inc_tx_stats(netdev
, len
, ret
);
220 static int nfp_repr_stop(struct net_device
*netdev
)
222 struct nfp_repr
*repr
= netdev_priv(netdev
);
225 err
= nfp_app_repr_stop(repr
->app
, repr
);
229 nfp_port_configure(netdev
, false);
233 static int nfp_repr_open(struct net_device
*netdev
)
235 struct nfp_repr
*repr
= netdev_priv(netdev
);
238 err
= nfp_port_configure(netdev
, true);
242 err
= nfp_app_repr_open(repr
->app
, repr
);
244 goto err_port_disable
;
249 nfp_port_configure(netdev
, false);
253 const struct net_device_ops nfp_repr_netdev_ops
= {
254 .ndo_open
= nfp_repr_open
,
255 .ndo_stop
= nfp_repr_stop
,
256 .ndo_start_xmit
= nfp_repr_xmit
,
257 .ndo_change_mtu
= nfp_repr_change_mtu
,
258 .ndo_get_stats64
= nfp_repr_get_stats64
,
259 .ndo_has_offload_stats
= nfp_repr_has_offload_stats
,
260 .ndo_get_offload_stats
= nfp_repr_get_offload_stats
,
261 .ndo_get_phys_port_name
= nfp_port_get_phys_port_name
,
262 .ndo_setup_tc
= nfp_port_setup_tc
,
263 .ndo_set_vf_mac
= nfp_app_set_vf_mac
,
264 .ndo_set_vf_vlan
= nfp_app_set_vf_vlan
,
265 .ndo_set_vf_spoofchk
= nfp_app_set_vf_spoofchk
,
266 .ndo_get_vf_config
= nfp_app_get_vf_config
,
267 .ndo_set_vf_link_state
= nfp_app_set_vf_link_state
,
268 .ndo_set_features
= nfp_port_set_features
,
271 static void nfp_repr_clean(struct nfp_repr
*repr
)
273 unregister_netdev(repr
->netdev
);
274 nfp_app_repr_clean(repr
->app
, repr
->netdev
);
275 dst_release((struct dst_entry
*)repr
->dst
);
276 nfp_port_free(repr
->port
);
279 static struct lock_class_key nfp_repr_netdev_xmit_lock_key
;
280 static struct lock_class_key nfp_repr_netdev_addr_lock_key
;
282 static void nfp_repr_set_lockdep_class_one(struct net_device
*dev
,
283 struct netdev_queue
*txq
,
286 lockdep_set_class(&txq
->_xmit_lock
, &nfp_repr_netdev_xmit_lock_key
);
289 static void nfp_repr_set_lockdep_class(struct net_device
*dev
)
291 lockdep_set_class(&dev
->addr_list_lock
, &nfp_repr_netdev_addr_lock_key
);
292 netdev_for_each_tx_queue(dev
, nfp_repr_set_lockdep_class_one
, NULL
);
295 int nfp_repr_init(struct nfp_app
*app
, struct net_device
*netdev
,
296 u32 cmsg_port_id
, struct nfp_port
*port
,
297 struct net_device
*pf_netdev
)
299 struct nfp_repr
*repr
= netdev_priv(netdev
);
302 nfp_repr_set_lockdep_class(netdev
);
305 repr
->dst
= metadata_dst_alloc(0, METADATA_HW_PORT_MUX
, GFP_KERNEL
);
308 repr
->dst
->u
.port_info
.port_id
= cmsg_port_id
;
309 repr
->dst
->u
.port_info
.lower_dev
= pf_netdev
;
311 netdev
->netdev_ops
= &nfp_repr_netdev_ops
;
312 netdev
->ethtool_ops
= &nfp_port_ethtool_ops
;
314 netdev
->max_mtu
= pf_netdev
->max_mtu
;
316 SWITCHDEV_SET_OPS(netdev
, &nfp_port_switchdev_ops
);
318 if (nfp_app_has_tc(app
)) {
319 netdev
->features
|= NETIF_F_HW_TC
;
320 netdev
->hw_features
|= NETIF_F_HW_TC
;
323 err
= nfp_app_repr_init(app
, netdev
);
327 err
= register_netdev(netdev
);
334 nfp_app_repr_clean(app
, netdev
);
336 dst_release((struct dst_entry
*)repr
->dst
);
340 static void nfp_repr_free(struct nfp_repr
*repr
)
342 free_percpu(repr
->stats
);
343 free_netdev(repr
->netdev
);
346 struct net_device
*nfp_repr_alloc(struct nfp_app
*app
)
348 struct net_device
*netdev
;
349 struct nfp_repr
*repr
;
351 netdev
= alloc_etherdev(sizeof(*repr
));
355 netif_carrier_off(netdev
);
357 repr
= netdev_priv(netdev
);
358 repr
->netdev
= netdev
;
361 repr
->stats
= netdev_alloc_pcpu_stats(struct nfp_repr_pcpu_stats
);
363 goto err_free_netdev
;
372 static void nfp_repr_clean_and_free(struct nfp_repr
*repr
)
374 nfp_info(repr
->app
->cpp
, "Destroying Representor(%s)\n",
376 nfp_repr_clean(repr
);
380 void nfp_reprs_clean_and_free(struct nfp_app
*app
, struct nfp_reprs
*reprs
)
382 struct net_device
*netdev
;
385 for (i
= 0; i
< reprs
->num_reprs
; i
++) {
386 netdev
= nfp_repr_get_locked(app
, reprs
, i
);
388 nfp_repr_clean_and_free(netdev_priv(netdev
));
395 nfp_reprs_clean_and_free_by_type(struct nfp_app
*app
, enum nfp_repr_type type
)
397 struct net_device
*netdev
;
398 struct nfp_reprs
*reprs
;
401 reprs
= rcu_dereference_protected(app
->reprs
[type
],
402 lockdep_is_held(&app
->pf
->lock
));
406 /* Preclean must happen before we remove the reprs reference from the
409 for (i
= 0; i
< reprs
->num_reprs
; i
++) {
410 netdev
= nfp_repr_get_locked(app
, reprs
, i
);
412 nfp_app_repr_preclean(app
, netdev
);
415 reprs
= nfp_app_reprs_set(app
, type
, NULL
);
418 nfp_reprs_clean_and_free(app
, reprs
);
421 struct nfp_reprs
*nfp_reprs_alloc(unsigned int num_reprs
)
423 struct nfp_reprs
*reprs
;
425 reprs
= kzalloc(sizeof(*reprs
) +
426 num_reprs
* sizeof(struct net_device
*), GFP_KERNEL
);
429 reprs
->num_reprs
= num_reprs
;
434 int nfp_reprs_resync_phys_ports(struct nfp_app
*app
)
436 struct net_device
*netdev
;
437 struct nfp_reprs
*reprs
;
438 struct nfp_repr
*repr
;
441 reprs
= nfp_reprs_get_locked(app
, NFP_REPR_TYPE_PHYS_PORT
);
445 for (i
= 0; i
< reprs
->num_reprs
; i
++) {
446 netdev
= nfp_repr_get_locked(app
, reprs
, i
);
450 repr
= netdev_priv(netdev
);
451 if (repr
->port
->type
!= NFP_PORT_INVALID
)
454 nfp_app_repr_preclean(app
, netdev
);
455 rcu_assign_pointer(reprs
->reprs
[i
], NULL
);
457 nfp_repr_clean(repr
);