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 #ifndef _NFP_PORT_H_
35 #define _NFP_PORT_H_
36 
37 #include <net/devlink.h>
38 
39 struct net_device;
40 struct nfp_app;
41 struct nfp_pf;
42 struct nfp_port;
43 
44 /**
45  * enum nfp_port_type - type of port NFP can switch traffic to
46  * @NFP_PORT_INVALID:	port is invalid, %NFP_PORT_PHYS_PORT transitions to this
47  *			state when port disappears because of FW fault or config
48  *			change
49  * @NFP_PORT_PHYS_PORT:	external NIC port
50  * @NFP_PORT_PF_PORT:	logical port of PCI PF
51  * @NFP_PORT_VF_PORT:	logical port of PCI VF
52  */
53 enum nfp_port_type {
54 	NFP_PORT_INVALID,
55 	NFP_PORT_PHYS_PORT,
56 	NFP_PORT_PF_PORT,
57 	NFP_PORT_VF_PORT,
58 };
59 
60 /**
61  * enum nfp_port_flags - port flags (can be type-specific)
62  * @NFP_PORT_CHANGED:	port state has changed since last eth table refresh;
63  *			for NFP_PORT_PHYS_PORT, never set otherwise; must hold
64  *			rtnl_lock to clear
65  */
66 enum nfp_port_flags {
67 	NFP_PORT_CHANGED = 0,
68 };
69 
70 /**
71  * struct nfp_port - structure representing NFP port
72  * @netdev:	backpointer to associated netdev
73  * @type:	what port type does the entity represent
74  * @flags:	port flags
75  * @tc_offload_cnt:	number of active TC offloads, how offloads are counted
76  *			is not defined, use as a boolean
77  * @app:	backpointer to the app structure
78  * @dl_port:	devlink port structure
79  * @eth_id:	for %NFP_PORT_PHYS_PORT port ID in NFP enumeration scheme
80  * @eth_port:	for %NFP_PORT_PHYS_PORT translated ETH Table port entry
81  * @eth_stats:	for %NFP_PORT_PHYS_PORT MAC stats if available
82  * @pf_id:	for %NFP_PORT_PF_PORT, %NFP_PORT_VF_PORT ID of the PCI PF (0-3)
83  * @vf_id:	for %NFP_PORT_VF_PORT ID of the PCI VF within @pf_id
84  * @vnic:	for %NFP_PORT_PF_PORT, %NFP_PORT_VF_PORT vNIC ctrl memory
85  * @port_list:	entry on pf's list of ports
86  */
87 struct nfp_port {
88 	struct net_device *netdev;
89 	enum nfp_port_type type;
90 
91 	unsigned long flags;
92 	unsigned long tc_offload_cnt;
93 
94 	struct nfp_app *app;
95 
96 	struct devlink_port dl_port;
97 
98 	union {
99 		/* NFP_PORT_PHYS_PORT */
100 		struct {
101 			unsigned int eth_id;
102 			struct nfp_eth_table_port *eth_port;
103 			u8 __iomem *eth_stats;
104 		};
105 		/* NFP_PORT_PF_PORT, NFP_PORT_VF_PORT */
106 		struct {
107 			unsigned int pf_id;
108 			unsigned int vf_id;
109 			u8 __iomem *vnic;
110 		};
111 	};
112 
113 	struct list_head port_list;
114 };
115 
116 extern const struct ethtool_ops nfp_port_ethtool_ops;
117 extern const struct switchdev_ops nfp_port_switchdev_ops;
118 
119 int nfp_port_setup_tc(struct net_device *netdev, enum tc_setup_type type,
120 		      void *type_data);
121 
122 static inline bool nfp_port_is_vnic(const struct nfp_port *port)
123 {
124 	return port->type == NFP_PORT_PF_PORT || port->type == NFP_PORT_VF_PORT;
125 }
126 
127 int
128 nfp_port_set_features(struct net_device *netdev, netdev_features_t features);
129 
130 struct nfp_port *nfp_port_from_netdev(struct net_device *netdev);
131 struct nfp_port *
132 nfp_port_from_id(struct nfp_pf *pf, enum nfp_port_type type, unsigned int id);
133 struct nfp_eth_table_port *__nfp_port_get_eth_port(struct nfp_port *port);
134 struct nfp_eth_table_port *nfp_port_get_eth_port(struct nfp_port *port);
135 
136 int
137 nfp_port_get_phys_port_name(struct net_device *netdev, char *name, size_t len);
138 int nfp_port_configure(struct net_device *netdev, bool configed);
139 
140 struct nfp_port *
141 nfp_port_alloc(struct nfp_app *app, enum nfp_port_type type,
142 	       struct net_device *netdev);
143 void nfp_port_free(struct nfp_port *port);
144 
145 int nfp_port_init_phy_port(struct nfp_pf *pf, struct nfp_app *app,
146 			   struct nfp_port *port, unsigned int id);
147 
148 int nfp_net_refresh_eth_port(struct nfp_port *port);
149 void nfp_net_refresh_port_table(struct nfp_port *port);
150 int nfp_net_refresh_port_table_sync(struct nfp_pf *pf);
151 
152 int nfp_devlink_port_register(struct nfp_app *app, struct nfp_port *port);
153 void nfp_devlink_port_unregister(struct nfp_port *port);
154 
155 /**
156  * Mac stats (0x0000 - 0x0200)
157  * all counters are 64bit.
158  */
159 #define NFP_MAC_STATS_BASE                0x0000
160 #define NFP_MAC_STATS_SIZE                0x0200
161 
162 #define NFP_MAC_STATS_RX_IN_OCTETS			(NFP_MAC_STATS_BASE + 0x000)
163 							/* unused 0x008 */
164 #define NFP_MAC_STATS_RX_FRAME_TOO_LONG_ERRORS		(NFP_MAC_STATS_BASE + 0x010)
165 #define NFP_MAC_STATS_RX_RANGE_LENGTH_ERRORS		(NFP_MAC_STATS_BASE + 0x018)
166 #define NFP_MAC_STATS_RX_VLAN_RECEIVED_OK		(NFP_MAC_STATS_BASE + 0x020)
167 #define NFP_MAC_STATS_RX_IN_ERRORS			(NFP_MAC_STATS_BASE + 0x028)
168 #define NFP_MAC_STATS_RX_IN_BROADCAST_PKTS		(NFP_MAC_STATS_BASE + 0x030)
169 #define NFP_MAC_STATS_RX_DROP_EVENTS			(NFP_MAC_STATS_BASE + 0x038)
170 #define NFP_MAC_STATS_RX_ALIGNMENT_ERRORS		(NFP_MAC_STATS_BASE + 0x040)
171 #define NFP_MAC_STATS_RX_PAUSE_MAC_CTRL_FRAMES		(NFP_MAC_STATS_BASE + 0x048)
172 #define NFP_MAC_STATS_RX_FRAMES_RECEIVED_OK		(NFP_MAC_STATS_BASE + 0x050)
173 #define NFP_MAC_STATS_RX_FRAME_CHECK_SEQUENCE_ERRORS	(NFP_MAC_STATS_BASE + 0x058)
174 #define NFP_MAC_STATS_RX_UNICAST_PKTS			(NFP_MAC_STATS_BASE + 0x060)
175 #define NFP_MAC_STATS_RX_MULTICAST_PKTS			(NFP_MAC_STATS_BASE + 0x068)
176 #define NFP_MAC_STATS_RX_PKTS				(NFP_MAC_STATS_BASE + 0x070)
177 #define NFP_MAC_STATS_RX_UNDERSIZE_PKTS			(NFP_MAC_STATS_BASE + 0x078)
178 #define NFP_MAC_STATS_RX_PKTS_64_OCTETS			(NFP_MAC_STATS_BASE + 0x080)
179 #define NFP_MAC_STATS_RX_PKTS_65_TO_127_OCTETS		(NFP_MAC_STATS_BASE + 0x088)
180 #define NFP_MAC_STATS_RX_PKTS_512_TO_1023_OCTETS	(NFP_MAC_STATS_BASE + 0x090)
181 #define NFP_MAC_STATS_RX_PKTS_1024_TO_1518_OCTETS	(NFP_MAC_STATS_BASE + 0x098)
182 #define NFP_MAC_STATS_RX_JABBERS			(NFP_MAC_STATS_BASE + 0x0a0)
183 #define NFP_MAC_STATS_RX_FRAGMENTS			(NFP_MAC_STATS_BASE + 0x0a8)
184 #define NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS2		(NFP_MAC_STATS_BASE + 0x0b0)
185 #define NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS3		(NFP_MAC_STATS_BASE + 0x0b8)
186 #define NFP_MAC_STATS_RX_PKTS_128_TO_255_OCTETS		(NFP_MAC_STATS_BASE + 0x0c0)
187 #define NFP_MAC_STATS_RX_PKTS_256_TO_511_OCTETS		(NFP_MAC_STATS_BASE + 0x0c8)
188 #define NFP_MAC_STATS_RX_PKTS_1519_TO_MAX_OCTETS	(NFP_MAC_STATS_BASE + 0x0d0)
189 #define NFP_MAC_STATS_RX_OVERSIZE_PKTS			(NFP_MAC_STATS_BASE + 0x0d8)
190 #define NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS0		(NFP_MAC_STATS_BASE + 0x0e0)
191 #define NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS1		(NFP_MAC_STATS_BASE + 0x0e8)
192 #define NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS4		(NFP_MAC_STATS_BASE + 0x0f0)
193 #define NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS5		(NFP_MAC_STATS_BASE + 0x0f8)
194 #define NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS6		(NFP_MAC_STATS_BASE + 0x100)
195 #define NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS7		(NFP_MAC_STATS_BASE + 0x108)
196 #define NFP_MAC_STATS_RX_MAC_CTRL_FRAMES_RECEIVED	(NFP_MAC_STATS_BASE + 0x110)
197 #define NFP_MAC_STATS_RX_MAC_HEAD_DROP			(NFP_MAC_STATS_BASE + 0x118)
198 							/* unused 0x120 */
199 							/* unused 0x128 */
200 							/* unused 0x130 */
201 #define NFP_MAC_STATS_TX_QUEUE_DROP			(NFP_MAC_STATS_BASE + 0x138)
202 #define NFP_MAC_STATS_TX_OUT_OCTETS			(NFP_MAC_STATS_BASE + 0x140)
203 							/* unused 0x148 */
204 #define NFP_MAC_STATS_TX_VLAN_TRANSMITTED_OK		(NFP_MAC_STATS_BASE + 0x150)
205 #define NFP_MAC_STATS_TX_OUT_ERRORS			(NFP_MAC_STATS_BASE + 0x158)
206 #define NFP_MAC_STATS_TX_BROADCAST_PKTS			(NFP_MAC_STATS_BASE + 0x160)
207 #define NFP_MAC_STATS_TX_PKTS_64_OCTETS			(NFP_MAC_STATS_BASE + 0x168)
208 #define NFP_MAC_STATS_TX_PKTS_256_TO_511_OCTETS		(NFP_MAC_STATS_BASE + 0x170)
209 #define NFP_MAC_STATS_TX_PKTS_512_TO_1023_OCTETS	(NFP_MAC_STATS_BASE + 0x178)
210 #define NFP_MAC_STATS_TX_PAUSE_MAC_CTRL_FRAMES		(NFP_MAC_STATS_BASE + 0x180)
211 #define NFP_MAC_STATS_TX_FRAMES_TRANSMITTED_OK		(NFP_MAC_STATS_BASE + 0x188)
212 #define NFP_MAC_STATS_TX_UNICAST_PKTS			(NFP_MAC_STATS_BASE + 0x190)
213 #define NFP_MAC_STATS_TX_MULTICAST_PKTS			(NFP_MAC_STATS_BASE + 0x198)
214 #define NFP_MAC_STATS_TX_PKTS_65_TO_127_OCTETS		(NFP_MAC_STATS_BASE + 0x1a0)
215 #define NFP_MAC_STATS_TX_PKTS_128_TO_255_OCTETS		(NFP_MAC_STATS_BASE + 0x1a8)
216 #define NFP_MAC_STATS_TX_PKTS_1024_TO_1518_OCTETS	(NFP_MAC_STATS_BASE + 0x1b0)
217 #define NFP_MAC_STATS_TX_PKTS_1519_TO_MAX_OCTETS	(NFP_MAC_STATS_BASE + 0x1b8)
218 #define NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS0		(NFP_MAC_STATS_BASE + 0x1c0)
219 #define NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS1		(NFP_MAC_STATS_BASE + 0x1c8)
220 #define NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS4		(NFP_MAC_STATS_BASE + 0x1d0)
221 #define NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS5		(NFP_MAC_STATS_BASE + 0x1d8)
222 #define NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS2		(NFP_MAC_STATS_BASE + 0x1e0)
223 #define NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS3		(NFP_MAC_STATS_BASE + 0x1e8)
224 #define NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS6		(NFP_MAC_STATS_BASE + 0x1f0)
225 #define NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS7		(NFP_MAC_STATS_BASE + 0x1f8)
226 
227 #endif
228