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>
39 #include "nfpcore/nfp_cpp.h"
40 #include "nfpcore/nfp_nsp.h"
43 #include "nfp_net_ctrl.h"
44 #include "nfp_net_repr.h"
48 nfp_repr_inc_tx_stats(struct net_device
*netdev
, unsigned int len
,
51 struct nfp_repr
*repr
= netdev_priv(netdev
);
52 struct nfp_repr_pcpu_stats
*stats
;
54 if (unlikely(tx_status
!= NET_XMIT_SUCCESS
&&
55 tx_status
!= NET_XMIT_CN
)) {
56 this_cpu_inc(repr
->stats
->tx_drops
);
60 stats
= this_cpu_ptr(repr
->stats
);
61 u64_stats_update_begin(&stats
->syncp
);
63 stats
->tx_bytes
+= len
;
64 u64_stats_update_end(&stats
->syncp
);
67 void nfp_repr_inc_rx_stats(struct net_device
*netdev
, unsigned int len
)
69 struct nfp_repr
*repr
= netdev_priv(netdev
);
70 struct nfp_repr_pcpu_stats
*stats
;
72 stats
= this_cpu_ptr(repr
->stats
);
73 u64_stats_update_begin(&stats
->syncp
);
75 stats
->rx_bytes
+= len
;
76 u64_stats_update_end(&stats
->syncp
);
80 nfp_repr_phy_port_get_stats64(const struct nfp_app
*app
, u8 phy_port
,
81 struct rtnl_link_stats64
*stats
)
85 mem
= app
->pf
->mac_stats_mem
+ phy_port
* NFP_MAC_STATS_SIZE
;
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.
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
);
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
);
100 nfp_repr_vf_get_stats64(const struct nfp_app
*app
, u8 vf
,
101 struct rtnl_link_stats64
*stats
)
105 mem
= app
->pf
->vf_cfg_mem
+ vf
* NFP_NET_CFG_BAR_SZ
;
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(mem
+ NFP_NET_CFG_STATS_RX_FRAMES
);
111 stats
->tx_bytes
= readq(mem
+ NFP_NET_CFG_STATS_RX_OCTETS
);
112 stats
->tx_dropped
= readq(mem
+ NFP_NET_CFG_STATS_RX_DISCARDS
);
114 stats
->rx_packets
= readq(mem
+ NFP_NET_CFG_STATS_TX_FRAMES
);
115 stats
->rx_bytes
= readq(mem
+ NFP_NET_CFG_STATS_TX_OCTETS
);
116 stats
->rx_dropped
= readq(mem
+ NFP_NET_CFG_STATS_TX_DISCARDS
);
120 nfp_repr_pf_get_stats64(const struct nfp_app
*app
, u8 pf
,
121 struct rtnl_link_stats64
*stats
)
128 mem
= nfp_cpp_area_iomem(app
->pf
->data_vnic_bar
);
130 stats
->tx_packets
= readq(mem
+ NFP_NET_CFG_STATS_RX_FRAMES
);
131 stats
->tx_bytes
= readq(mem
+ NFP_NET_CFG_STATS_RX_OCTETS
);
132 stats
->tx_dropped
= readq(mem
+ NFP_NET_CFG_STATS_RX_DISCARDS
);
134 stats
->rx_packets
= readq(mem
+ NFP_NET_CFG_STATS_TX_FRAMES
);
135 stats
->rx_bytes
= readq(mem
+ NFP_NET_CFG_STATS_TX_OCTETS
);
136 stats
->rx_dropped
= readq(mem
+ NFP_NET_CFG_STATS_TX_DISCARDS
);
140 nfp_repr_get_stats64(struct net_device
*netdev
, struct rtnl_link_stats64
*stats
)
142 struct nfp_repr
*repr
= netdev_priv(netdev
);
143 struct nfp_eth_table_port
*eth_port
;
144 struct nfp_app
*app
= repr
->app
;
146 if (WARN_ON(!repr
->port
))
149 switch (repr
->port
->type
) {
150 case NFP_PORT_PHYS_PORT
:
151 eth_port
= __nfp_port_get_eth_port(repr
->port
);
154 nfp_repr_phy_port_get_stats64(app
, eth_port
->index
, stats
);
156 case NFP_PORT_PF_PORT
:
157 nfp_repr_pf_get_stats64(app
, repr
->port
->pf_id
, stats
);
159 case NFP_PORT_VF_PORT
:
160 nfp_repr_vf_get_stats64(app
, repr
->port
->vf_id
, stats
);
167 nfp_repr_has_offload_stats(const struct net_device
*dev
, int attr_id
)
170 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
178 nfp_repr_get_host_stats64(const struct net_device
*netdev
,
179 struct rtnl_link_stats64
*stats
)
181 struct nfp_repr
*repr
= netdev_priv(netdev
);
184 for_each_possible_cpu(i
) {
185 u64 tbytes
, tpkts
, tdrops
, rbytes
, rpkts
;
186 struct nfp_repr_pcpu_stats
*repr_stats
;
189 repr_stats
= per_cpu_ptr(repr
->stats
, i
);
191 start
= u64_stats_fetch_begin_irq(&repr_stats
->syncp
);
192 tbytes
= repr_stats
->tx_bytes
;
193 tpkts
= repr_stats
->tx_packets
;
194 tdrops
= repr_stats
->tx_drops
;
195 rbytes
= repr_stats
->rx_bytes
;
196 rpkts
= repr_stats
->rx_packets
;
197 } while (u64_stats_fetch_retry_irq(&repr_stats
->syncp
, start
));
199 stats
->tx_bytes
+= tbytes
;
200 stats
->tx_packets
+= tpkts
;
201 stats
->tx_dropped
+= tdrops
;
202 stats
->rx_bytes
+= rbytes
;
203 stats
->rx_packets
+= rpkts
;
210 nfp_repr_get_offload_stats(int attr_id
, const struct net_device
*dev
,
214 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
215 return nfp_repr_get_host_stats64(dev
, stats
);
221 static netdev_tx_t
nfp_repr_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
223 struct nfp_repr
*repr
= netdev_priv(netdev
);
224 unsigned int len
= skb
->len
;
228 dst_hold((struct dst_entry
*)repr
->dst
);
229 skb_dst_set(skb
, (struct dst_entry
*)repr
->dst
);
230 skb
->dev
= repr
->dst
->u
.port_info
.lower_dev
;
232 ret
= dev_queue_xmit(skb
);
233 nfp_repr_inc_tx_stats(netdev
, len
, ret
);
238 static int nfp_repr_stop(struct net_device
*netdev
)
240 struct nfp_repr
*repr
= netdev_priv(netdev
);
242 return nfp_app_repr_stop(repr
->app
, repr
);
245 static int nfp_repr_open(struct net_device
*netdev
)
247 struct nfp_repr
*repr
= netdev_priv(netdev
);
249 return nfp_app_repr_open(repr
->app
, repr
);
252 const struct net_device_ops nfp_repr_netdev_ops
= {
253 .ndo_open
= nfp_repr_open
,
254 .ndo_stop
= nfp_repr_stop
,
255 .ndo_start_xmit
= nfp_repr_xmit
,
256 .ndo_get_stats64
= nfp_repr_get_stats64
,
257 .ndo_has_offload_stats
= nfp_repr_has_offload_stats
,
258 .ndo_get_offload_stats
= nfp_repr_get_offload_stats
,
259 .ndo_get_phys_port_name
= nfp_port_get_phys_port_name
,
262 static void nfp_repr_clean(struct nfp_repr
*repr
)
264 unregister_netdev(repr
->netdev
);
265 dst_release((struct dst_entry
*)repr
->dst
);
266 nfp_port_free(repr
->port
);
269 static struct lock_class_key nfp_repr_netdev_xmit_lock_key
;
270 static struct lock_class_key nfp_repr_netdev_addr_lock_key
;
272 static void nfp_repr_set_lockdep_class_one(struct net_device
*dev
,
273 struct netdev_queue
*txq
,
276 lockdep_set_class(&txq
->_xmit_lock
, &nfp_repr_netdev_xmit_lock_key
);
279 static void nfp_repr_set_lockdep_class(struct net_device
*dev
)
281 lockdep_set_class(&dev
->addr_list_lock
, &nfp_repr_netdev_addr_lock_key
);
282 netdev_for_each_tx_queue(dev
, nfp_repr_set_lockdep_class_one
, NULL
);
285 int nfp_repr_init(struct nfp_app
*app
, struct net_device
*netdev
,
286 u32 cmsg_port_id
, struct nfp_port
*port
,
287 struct net_device
*pf_netdev
)
289 struct nfp_repr
*repr
= netdev_priv(netdev
);
292 nfp_repr_set_lockdep_class(netdev
);
295 repr
->dst
= metadata_dst_alloc(0, METADATA_HW_PORT_MUX
, GFP_KERNEL
);
298 repr
->dst
->u
.port_info
.port_id
= cmsg_port_id
;
299 repr
->dst
->u
.port_info
.lower_dev
= pf_netdev
;
301 netdev
->netdev_ops
= &nfp_repr_netdev_ops
;
303 err
= register_netdev(netdev
);
310 dst_release((struct dst_entry
*)repr
->dst
);
314 static void nfp_repr_free(struct nfp_repr
*repr
)
316 free_percpu(repr
->stats
);
317 free_netdev(repr
->netdev
);
320 struct net_device
*nfp_repr_alloc(struct nfp_app
*app
)
322 struct net_device
*netdev
;
323 struct nfp_repr
*repr
;
325 netdev
= alloc_etherdev(sizeof(*repr
));
329 repr
= netdev_priv(netdev
);
330 repr
->netdev
= netdev
;
333 repr
->stats
= netdev_alloc_pcpu_stats(struct nfp_repr_pcpu_stats
);
335 goto err_free_netdev
;
344 static void nfp_repr_clean_and_free(struct nfp_repr
*repr
)
346 nfp_info(repr
->app
->cpp
, "Destroying Representor(%s)\n",
348 nfp_repr_clean(repr
);
352 void nfp_reprs_clean_and_free(struct nfp_reprs
*reprs
)
356 for (i
= 0; i
< reprs
->num_reprs
; i
++)
358 nfp_repr_clean_and_free(netdev_priv(reprs
->reprs
[i
]));
364 nfp_reprs_clean_and_free_by_type(struct nfp_app
*app
,
365 enum nfp_repr_type type
)
367 struct nfp_reprs
*reprs
;
369 reprs
= nfp_app_reprs_set(app
, type
, NULL
);
374 nfp_reprs_clean_and_free(reprs
);
377 struct nfp_reprs
*nfp_reprs_alloc(unsigned int num_reprs
)
379 struct nfp_reprs
*reprs
;
381 reprs
= kzalloc(sizeof(*reprs
) +
382 num_reprs
* sizeof(struct net_device
*), GFP_KERNEL
);
385 reprs
->num_reprs
= num_reprs
;