1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */
2 /* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved. */
3 
4 #ifndef _PRESTERA_H_
5 #define _PRESTERA_H_
6 
7 #include <linux/notifier.h>
8 #include <linux/skbuff.h>
9 #include <linux/workqueue.h>
10 #include <net/devlink.h>
11 #include <uapi/linux/if_ether.h>
12 
13 #define PRESTERA_DRV_NAME	"prestera"
14 
15 #define PRESTERA_DEFAULT_VID    1
16 
17 struct prestera_fw_rev {
18 	u16 maj;
19 	u16 min;
20 	u16 sub;
21 };
22 
23 struct prestera_port_stats {
24 	u64 good_octets_received;
25 	u64 bad_octets_received;
26 	u64 mac_trans_error;
27 	u64 broadcast_frames_received;
28 	u64 multicast_frames_received;
29 	u64 frames_64_octets;
30 	u64 frames_65_to_127_octets;
31 	u64 frames_128_to_255_octets;
32 	u64 frames_256_to_511_octets;
33 	u64 frames_512_to_1023_octets;
34 	u64 frames_1024_to_max_octets;
35 	u64 excessive_collision;
36 	u64 multicast_frames_sent;
37 	u64 broadcast_frames_sent;
38 	u64 fc_sent;
39 	u64 fc_received;
40 	u64 buffer_overrun;
41 	u64 undersize;
42 	u64 fragments;
43 	u64 oversize;
44 	u64 jabber;
45 	u64 rx_error_frame_received;
46 	u64 bad_crc;
47 	u64 collisions;
48 	u64 late_collision;
49 	u64 unicast_frames_received;
50 	u64 unicast_frames_sent;
51 	u64 sent_multiple;
52 	u64 sent_deferred;
53 	u64 good_octets_sent;
54 };
55 
56 #define PRESTERA_AP_PORT_MAX   (10)
57 
58 struct prestera_port_caps {
59 	u64 supp_link_modes;
60 	u8 supp_fec;
61 	u8 type;
62 	u8 transceiver;
63 };
64 
65 struct prestera_lag {
66 	struct net_device *dev;
67 	struct list_head members;
68 	u16 member_count;
69 	u16 lag_id;
70 };
71 
72 struct prestera_flow_block;
73 
74 struct prestera_port_mac_state {
75 	u32 mode;
76 	u32 speed;
77 	bool oper;
78 	u8 duplex;
79 	u8 fc;
80 	u8 fec;
81 };
82 
83 struct prestera_port_phy_state {
84 	u64 lmode_bmap;
85 	struct {
86 		bool pause;
87 		bool asym_pause;
88 	} remote_fc;
89 	u8 mdix;
90 };
91 
92 struct prestera_port_mac_config {
93 	u32 mode;
94 	u32 speed;
95 	bool admin;
96 	u8 inband;
97 	u8 duplex;
98 	u8 fec;
99 };
100 
101 struct prestera_port_phy_config {
102 	u32 mode;
103 	bool admin;
104 	u8 mdix;
105 };
106 
107 struct prestera_port {
108 	struct net_device *dev;
109 	struct prestera_switch *sw;
110 	struct prestera_flow_block *flow_block;
111 	struct devlink_port dl_port;
112 	struct list_head lag_member;
113 	struct prestera_lag *lag;
114 	u32 id;
115 	u32 hw_id;
116 	u32 dev_id;
117 	u16 fp_id;
118 	u16 pvid;
119 	bool autoneg;
120 	u64 adver_link_modes;
121 	u8 adver_fec;
122 	struct prestera_port_caps caps;
123 	struct list_head list;
124 	struct list_head vlans_list;
125 	struct {
126 		struct prestera_port_stats stats;
127 		struct delayed_work caching_dw;
128 	} cached_hw_stats;
129 	struct prestera_port_mac_config cfg_mac;
130 	struct prestera_port_phy_config cfg_phy;
131 	struct prestera_port_mac_state state_mac;
132 	struct prestera_port_phy_state state_phy;
133 };
134 
135 struct prestera_device {
136 	struct device *dev;
137 	u8 __iomem *ctl_regs;
138 	u8 __iomem *pp_regs;
139 	struct prestera_fw_rev fw_rev;
140 	void *priv;
141 
142 	/* called by device driver to handle received packets */
143 	void (*recv_pkt)(struct prestera_device *dev);
144 
145 	/* called by device driver to pass event up to the higher layer */
146 	int (*recv_msg)(struct prestera_device *dev, void *msg, size_t size);
147 
148 	/* called by higher layer to send request to the firmware */
149 	int (*send_req)(struct prestera_device *dev, int qid, void *in_msg,
150 			size_t in_size, void *out_msg, size_t out_size,
151 			unsigned int wait);
152 };
153 
154 enum prestera_event_type {
155 	PRESTERA_EVENT_TYPE_UNSPEC,
156 
157 	PRESTERA_EVENT_TYPE_PORT,
158 	PRESTERA_EVENT_TYPE_FDB,
159 	PRESTERA_EVENT_TYPE_RXTX,
160 
161 	PRESTERA_EVENT_TYPE_MAX
162 };
163 
164 enum prestera_rxtx_event_id {
165 	PRESTERA_RXTX_EVENT_UNSPEC,
166 	PRESTERA_RXTX_EVENT_RCV_PKT,
167 };
168 
169 enum prestera_port_event_id {
170 	PRESTERA_PORT_EVENT_UNSPEC,
171 	PRESTERA_PORT_EVENT_MAC_STATE_CHANGED,
172 };
173 
174 struct prestera_port_event {
175 	u32 port_id;
176 	union {
177 		struct {
178 			u32 mode;
179 			u32 speed;
180 			u8 oper;
181 			u8 duplex;
182 			u8 fc;
183 			u8 fec;
184 		} mac;
185 		struct {
186 			u64 lmode_bmap;
187 			struct {
188 				bool pause;
189 				bool asym_pause;
190 			} remote_fc;
191 			u8 mdix;
192 		} phy;
193 	} data;
194 };
195 
196 enum prestera_fdb_entry_type {
197 	PRESTERA_FDB_ENTRY_TYPE_REG_PORT,
198 	PRESTERA_FDB_ENTRY_TYPE_LAG,
199 	PRESTERA_FDB_ENTRY_TYPE_MAX
200 };
201 
202 enum prestera_fdb_event_id {
203 	PRESTERA_FDB_EVENT_UNSPEC,
204 	PRESTERA_FDB_EVENT_LEARNED,
205 	PRESTERA_FDB_EVENT_AGED,
206 };
207 
208 struct prestera_fdb_event {
209 	enum prestera_fdb_entry_type type;
210 	union {
211 		u32 port_id;
212 		u16 lag_id;
213 	} dest;
214 	u32 vid;
215 	union {
216 		u8 mac[ETH_ALEN];
217 	} data;
218 };
219 
220 struct prestera_event {
221 	u16 id;
222 	union {
223 		struct prestera_port_event port_evt;
224 		struct prestera_fdb_event fdb_evt;
225 	};
226 };
227 
228 enum prestera_if_type {
229 	/* the interface is of port type (dev,port) */
230 	PRESTERA_IF_PORT_E = 0,
231 
232 	/* the interface is of lag type (lag-id) */
233 	PRESTERA_IF_LAG_E = 1,
234 
235 	/* the interface is of Vid type (vlan-id) */
236 	PRESTERA_IF_VID_E = 3,
237 };
238 
239 struct prestera_iface {
240 	enum prestera_if_type type;
241 	struct {
242 		u32 hw_dev_num;
243 		u32 port_num;
244 	} dev_port;
245 	u32 hw_dev_num;
246 	u16 vr_id;
247 	u16 lag_id;
248 	u16 vlan_id;
249 };
250 
251 struct prestera_switchdev;
252 struct prestera_span;
253 struct prestera_rxtx;
254 struct prestera_trap_data;
255 struct prestera_acl;
256 
257 struct prestera_switch {
258 	struct prestera_device *dev;
259 	struct prestera_switchdev *swdev;
260 	struct prestera_rxtx *rxtx;
261 	struct prestera_acl *acl;
262 	struct prestera_span *span;
263 	struct list_head event_handlers;
264 	struct notifier_block netdev_nb;
265 	struct prestera_trap_data *trap_data;
266 	char base_mac[ETH_ALEN];
267 	struct list_head port_list;
268 	rwlock_t port_list_lock;
269 	u32 port_count;
270 	u32 mtu_min;
271 	u32 mtu_max;
272 	u8 id;
273 	struct prestera_router *router;
274 	struct prestera_lag *lags;
275 	struct prestera_counter *counter;
276 	u8 lag_member_max;
277 	u8 lag_max;
278 };
279 
280 struct prestera_router {
281 	struct prestera_switch *sw;
282 	struct list_head vr_list;
283 	struct list_head rif_entry_list;
284 	struct rhashtable fib_ht;
285 	struct rhashtable kern_fib_cache_ht;
286 	struct notifier_block inetaddr_nb;
287 	struct notifier_block inetaddr_valid_nb;
288 	struct notifier_block fib_nb;
289 };
290 
291 struct prestera_rxtx_params {
292 	bool use_sdma;
293 	u32 map_addr;
294 };
295 
296 #define prestera_dev(sw)		((sw)->dev->dev)
297 
298 static inline void prestera_write(const struct prestera_switch *sw,
299 				  unsigned int reg, u32 val)
300 {
301 	writel(val, sw->dev->pp_regs + reg);
302 }
303 
304 static inline u32 prestera_read(const struct prestera_switch *sw,
305 				unsigned int reg)
306 {
307 	return readl(sw->dev->pp_regs + reg);
308 }
309 
310 int prestera_device_register(struct prestera_device *dev);
311 void prestera_device_unregister(struct prestera_device *dev);
312 
313 struct prestera_port *prestera_port_find_by_hwid(struct prestera_switch *sw,
314 						 u32 dev_id, u32 hw_id);
315 
316 int prestera_port_autoneg_set(struct prestera_port *port, u64 link_modes);
317 
318 int prestera_router_init(struct prestera_switch *sw);
319 void prestera_router_fini(struct prestera_switch *sw);
320 
321 struct prestera_port *prestera_find_port(struct prestera_switch *sw, u32 id);
322 
323 int prestera_port_cfg_mac_read(struct prestera_port *port,
324 			       struct prestera_port_mac_config *cfg);
325 
326 int prestera_port_cfg_mac_write(struct prestera_port *port,
327 				struct prestera_port_mac_config *cfg);
328 
329 struct prestera_port *prestera_port_dev_lower_find(struct net_device *dev);
330 
331 void prestera_queue_work(struct work_struct *work);
332 
333 int prestera_port_pvid_set(struct prestera_port *port, u16 vid);
334 
335 bool prestera_netdev_check(const struct net_device *dev);
336 
337 int prestera_is_valid_mac_addr(struct prestera_port *port, const u8 *addr);
338 
339 bool prestera_port_is_lag_member(const struct prestera_port *port);
340 
341 struct prestera_lag *prestera_lag_by_id(struct prestera_switch *sw, u16 id);
342 
343 u16 prestera_port_lag_id(const struct prestera_port *port);
344 
345 #endif /* _PRESTERA_H_ */
346