xref: /openbmc/linux/drivers/net/ethernet/marvell/prestera/prestera.h (revision f7af616c632ee2ac3af0876fe33bf9e0232e665a)
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 struct prestera_port_caps {
57 	u64 supp_link_modes;
58 	u8 supp_fec;
59 	u8 type;
60 	u8 transceiver;
61 };
62 
63 struct prestera_lag {
64 	struct net_device *dev;
65 	struct list_head members;
66 	u16 member_count;
67 	u16 lag_id;
68 };
69 
70 struct prestera_port {
71 	struct net_device *dev;
72 	struct prestera_switch *sw;
73 	struct devlink_port dl_port;
74 	struct list_head lag_member;
75 	struct prestera_lag *lag;
76 	u32 id;
77 	u32 hw_id;
78 	u32 dev_id;
79 	u16 fp_id;
80 	u16 pvid;
81 	bool autoneg;
82 	u64 adver_link_modes;
83 	u8 adver_fec;
84 	struct prestera_port_caps caps;
85 	struct list_head list;
86 	struct list_head vlans_list;
87 	struct {
88 		struct prestera_port_stats stats;
89 		struct delayed_work caching_dw;
90 	} cached_hw_stats;
91 };
92 
93 struct prestera_device {
94 	struct device *dev;
95 	u8 __iomem *ctl_regs;
96 	u8 __iomem *pp_regs;
97 	struct prestera_fw_rev fw_rev;
98 	void *priv;
99 
100 	/* called by device driver to handle received packets */
101 	void (*recv_pkt)(struct prestera_device *dev);
102 
103 	/* called by device driver to pass event up to the higher layer */
104 	int (*recv_msg)(struct prestera_device *dev, void *msg, size_t size);
105 
106 	/* called by higher layer to send request to the firmware */
107 	int (*send_req)(struct prestera_device *dev, void *in_msg,
108 			size_t in_size, void *out_msg, size_t out_size,
109 			unsigned int wait);
110 };
111 
112 enum prestera_event_type {
113 	PRESTERA_EVENT_TYPE_UNSPEC,
114 
115 	PRESTERA_EVENT_TYPE_PORT,
116 	PRESTERA_EVENT_TYPE_FDB,
117 	PRESTERA_EVENT_TYPE_RXTX,
118 
119 	PRESTERA_EVENT_TYPE_MAX
120 };
121 
122 enum prestera_rxtx_event_id {
123 	PRESTERA_RXTX_EVENT_UNSPEC,
124 	PRESTERA_RXTX_EVENT_RCV_PKT,
125 };
126 
127 enum prestera_port_event_id {
128 	PRESTERA_PORT_EVENT_UNSPEC,
129 	PRESTERA_PORT_EVENT_STATE_CHANGED,
130 };
131 
132 struct prestera_port_event {
133 	u32 port_id;
134 	union {
135 		u32 oper_state;
136 	} data;
137 };
138 
139 enum prestera_fdb_entry_type {
140 	PRESTERA_FDB_ENTRY_TYPE_REG_PORT,
141 	PRESTERA_FDB_ENTRY_TYPE_LAG,
142 	PRESTERA_FDB_ENTRY_TYPE_MAX
143 };
144 
145 enum prestera_fdb_event_id {
146 	PRESTERA_FDB_EVENT_UNSPEC,
147 	PRESTERA_FDB_EVENT_LEARNED,
148 	PRESTERA_FDB_EVENT_AGED,
149 };
150 
151 struct prestera_fdb_event {
152 	enum prestera_fdb_entry_type type;
153 	union {
154 		u32 port_id;
155 		u16 lag_id;
156 	} dest;
157 	u32 vid;
158 	union {
159 		u8 mac[ETH_ALEN];
160 	} data;
161 };
162 
163 struct prestera_event {
164 	u16 id;
165 	union {
166 		struct prestera_port_event port_evt;
167 		struct prestera_fdb_event fdb_evt;
168 	};
169 };
170 
171 struct prestera_switchdev;
172 struct prestera_rxtx;
173 
174 struct prestera_switch {
175 	struct prestera_device *dev;
176 	struct prestera_switchdev *swdev;
177 	struct prestera_rxtx *rxtx;
178 	struct list_head event_handlers;
179 	struct notifier_block netdev_nb;
180 	char base_mac[ETH_ALEN];
181 	struct list_head port_list;
182 	rwlock_t port_list_lock;
183 	u32 port_count;
184 	u32 mtu_min;
185 	u32 mtu_max;
186 	u8 id;
187 	struct prestera_lag *lags;
188 	u8 lag_member_max;
189 	u8 lag_max;
190 };
191 
192 struct prestera_rxtx_params {
193 	bool use_sdma;
194 	u32 map_addr;
195 };
196 
197 #define prestera_dev(sw)		((sw)->dev->dev)
198 
199 static inline void prestera_write(const struct prestera_switch *sw,
200 				  unsigned int reg, u32 val)
201 {
202 	writel(val, sw->dev->pp_regs + reg);
203 }
204 
205 static inline u32 prestera_read(const struct prestera_switch *sw,
206 				unsigned int reg)
207 {
208 	return readl(sw->dev->pp_regs + reg);
209 }
210 
211 int prestera_device_register(struct prestera_device *dev);
212 void prestera_device_unregister(struct prestera_device *dev);
213 
214 struct prestera_port *prestera_port_find_by_hwid(struct prestera_switch *sw,
215 						 u32 dev_id, u32 hw_id);
216 
217 int prestera_port_autoneg_set(struct prestera_port *port, bool enable,
218 			      u64 adver_link_modes, u8 adver_fec);
219 
220 struct prestera_port *prestera_find_port(struct prestera_switch *sw, u32 id);
221 
222 struct prestera_port *prestera_port_dev_lower_find(struct net_device *dev);
223 
224 int prestera_port_pvid_set(struct prestera_port *port, u16 vid);
225 
226 bool prestera_netdev_check(const struct net_device *dev);
227 
228 bool prestera_port_is_lag_member(const struct prestera_port *port);
229 
230 struct prestera_lag *prestera_lag_by_id(struct prestera_switch *sw, u16 id);
231 
232 u16 prestera_port_lag_id(const struct prestera_port *port);
233 
234 #endif /* _PRESTERA_H_ */
235