xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105.h (revision 88cac0fa)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH
3  * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
4  */
5 #ifndef _SJA1105_H
6 #define _SJA1105_H
7 
8 #include <linux/ptp_clock_kernel.h>
9 #include <linux/timecounter.h>
10 #include <linux/dsa/sja1105.h>
11 #include <linux/dsa/8021q.h>
12 #include <net/dsa.h>
13 #include <linux/mutex.h>
14 #include "sja1105_static_config.h"
15 
16 #define SJA1105_NUM_PORTS		5
17 #define SJA1105_NUM_TC			8
18 #define SJA1105ET_FDB_BIN_SIZE		4
19 /* The hardware value is in multiples of 10 ms.
20  * The passed parameter is in multiples of 1 ms.
21  */
22 #define SJA1105_AGEING_TIME_MS(ms)	((ms) / 10)
23 #define SJA1105_NUM_L2_POLICERS		45
24 
25 typedef enum {
26 	SPI_READ = 0,
27 	SPI_WRITE = 1,
28 } sja1105_spi_rw_mode_t;
29 
30 #include "sja1105_tas.h"
31 #include "sja1105_ptp.h"
32 
33 /* Keeps the different addresses between E/T and P/Q/R/S */
34 struct sja1105_regs {
35 	u64 device_id;
36 	u64 prod_id;
37 	u64 status;
38 	u64 port_control;
39 	u64 rgu;
40 	u64 vl_status;
41 	u64 config;
42 	u64 sgmii;
43 	u64 rmii_pll1;
44 	u64 ptppinst;
45 	u64 ptppindur;
46 	u64 ptp_control;
47 	u64 ptpclkval;
48 	u64 ptpclkrate;
49 	u64 ptpclkcorp;
50 	u64 ptpsyncts;
51 	u64 ptpschtm;
52 	u64 ptpegr_ts[SJA1105_NUM_PORTS];
53 	u64 pad_mii_tx[SJA1105_NUM_PORTS];
54 	u64 pad_mii_rx[SJA1105_NUM_PORTS];
55 	u64 pad_mii_id[SJA1105_NUM_PORTS];
56 	u64 cgu_idiv[SJA1105_NUM_PORTS];
57 	u64 mii_tx_clk[SJA1105_NUM_PORTS];
58 	u64 mii_rx_clk[SJA1105_NUM_PORTS];
59 	u64 mii_ext_tx_clk[SJA1105_NUM_PORTS];
60 	u64 mii_ext_rx_clk[SJA1105_NUM_PORTS];
61 	u64 rgmii_tx_clk[SJA1105_NUM_PORTS];
62 	u64 rmii_ref_clk[SJA1105_NUM_PORTS];
63 	u64 rmii_ext_tx_clk[SJA1105_NUM_PORTS];
64 	u64 mac[SJA1105_NUM_PORTS];
65 	u64 mac_hl1[SJA1105_NUM_PORTS];
66 	u64 mac_hl2[SJA1105_NUM_PORTS];
67 	u64 ether_stats[SJA1105_NUM_PORTS];
68 	u64 qlevel[SJA1105_NUM_PORTS];
69 };
70 
71 struct sja1105_info {
72 	u64 device_id;
73 	/* Needed for distinction between P and R, and between Q and S
74 	 * (since the parts with/without SGMII share the same
75 	 * switch core and device_id)
76 	 */
77 	u64 part_no;
78 	/* E/T and P/Q/R/S have partial timestamps of different sizes.
79 	 * They must be reconstructed on both families anyway to get the full
80 	 * 64-bit values back.
81 	 */
82 	int ptp_ts_bits;
83 	/* Also SPI commands are of different sizes to retrieve
84 	 * the egress timestamps.
85 	 */
86 	int ptpegr_ts_bytes;
87 	const struct sja1105_dynamic_table_ops *dyn_ops;
88 	const struct sja1105_table_ops *static_ops;
89 	const struct sja1105_regs *regs;
90 	/* Both E/T and P/Q/R/S have quirks when it comes to popping the S-Tag
91 	 * from double-tagged frames. E/T will pop it only when it's equal to
92 	 * TPID from the General Parameters Table, while P/Q/R/S will only
93 	 * pop it when it's equal to TPID2.
94 	 */
95 	u16 qinq_tpid;
96 	int (*reset_cmd)(struct dsa_switch *ds);
97 	int (*setup_rgmii_delay)(const void *ctx, int port);
98 	/* Prototypes from include/net/dsa.h */
99 	int (*fdb_add_cmd)(struct dsa_switch *ds, int port,
100 			   const unsigned char *addr, u16 vid);
101 	int (*fdb_del_cmd)(struct dsa_switch *ds, int port,
102 			   const unsigned char *addr, u16 vid);
103 	void (*ptp_cmd_packing)(u8 *buf, struct sja1105_ptp_cmd *cmd,
104 				enum packing_op op);
105 	const char *name;
106 };
107 
108 enum sja1105_key_type {
109 	SJA1105_KEY_BCAST,
110 	SJA1105_KEY_TC,
111 	SJA1105_KEY_VLAN_UNAWARE_VL,
112 	SJA1105_KEY_VLAN_AWARE_VL,
113 };
114 
115 struct sja1105_key {
116 	enum sja1105_key_type type;
117 
118 	union {
119 		/* SJA1105_KEY_TC */
120 		struct {
121 			int pcp;
122 		} tc;
123 
124 		/* SJA1105_KEY_VLAN_UNAWARE_VL */
125 		/* SJA1105_KEY_VLAN_AWARE_VL */
126 		struct {
127 			u64 dmac;
128 			u16 vid;
129 			u16 pcp;
130 		} vl;
131 	};
132 };
133 
134 enum sja1105_rule_type {
135 	SJA1105_RULE_BCAST_POLICER,
136 	SJA1105_RULE_TC_POLICER,
137 	SJA1105_RULE_VL,
138 };
139 
140 enum sja1105_vl_type {
141 	SJA1105_VL_NONCRITICAL,
142 	SJA1105_VL_RATE_CONSTRAINED,
143 	SJA1105_VL_TIME_TRIGGERED,
144 };
145 
146 struct sja1105_rule {
147 	struct list_head list;
148 	unsigned long cookie;
149 	unsigned long port_mask;
150 	struct sja1105_key key;
151 	enum sja1105_rule_type type;
152 
153 	/* Action */
154 	union {
155 		/* SJA1105_RULE_BCAST_POLICER */
156 		struct {
157 			int sharindx;
158 		} bcast_pol;
159 
160 		/* SJA1105_RULE_TC_POLICER */
161 		struct {
162 			int sharindx;
163 		} tc_pol;
164 
165 		/* SJA1105_RULE_VL */
166 		struct {
167 			enum sja1105_vl_type type;
168 			unsigned long destports;
169 			int sharindx;
170 			int maxlen;
171 			int ipv;
172 			u64 base_time;
173 			u64 cycle_time;
174 			int num_entries;
175 			struct action_gate_entry *entries;
176 			struct flow_stats stats;
177 		} vl;
178 	};
179 };
180 
181 struct sja1105_flow_block {
182 	struct list_head rules;
183 	bool l2_policer_used[SJA1105_NUM_L2_POLICERS];
184 	int num_virtual_links;
185 };
186 
187 struct sja1105_bridge_vlan {
188 	struct list_head list;
189 	int port;
190 	u16 vid;
191 	bool pvid;
192 	bool untagged;
193 };
194 
195 enum sja1105_vlan_state {
196 	SJA1105_VLAN_UNAWARE,
197 	SJA1105_VLAN_BEST_EFFORT,
198 	SJA1105_VLAN_FILTERING_FULL,
199 };
200 
201 struct sja1105_private {
202 	struct sja1105_static_config static_config;
203 	bool rgmii_rx_delay[SJA1105_NUM_PORTS];
204 	bool rgmii_tx_delay[SJA1105_NUM_PORTS];
205 	bool best_effort_vlan_filtering;
206 	const struct sja1105_info *info;
207 	struct gpio_desc *reset_gpio;
208 	struct spi_device *spidev;
209 	struct dsa_switch *ds;
210 	struct list_head dsa_8021q_vlans;
211 	struct list_head bridge_vlans;
212 	struct list_head crosschip_links;
213 	struct sja1105_flow_block flow_block;
214 	struct sja1105_port ports[SJA1105_NUM_PORTS];
215 	/* Serializes transmission of management frames so that
216 	 * the switch doesn't confuse them with one another.
217 	 */
218 	struct mutex mgmt_lock;
219 	bool expect_dsa_8021q;
220 	enum sja1105_vlan_state vlan_state;
221 	struct sja1105_tagger_data tagger_data;
222 	struct sja1105_ptp_data ptp_data;
223 	struct sja1105_tas_data tas_data;
224 };
225 
226 #include "sja1105_dynamic_config.h"
227 
228 struct sja1105_spi_message {
229 	u64 access;
230 	u64 read_count;
231 	u64 address;
232 };
233 
234 /* From sja1105_main.c */
235 enum sja1105_reset_reason {
236 	SJA1105_VLAN_FILTERING = 0,
237 	SJA1105_RX_HWTSTAMPING,
238 	SJA1105_AGEING_TIME,
239 	SJA1105_SCHEDULING,
240 	SJA1105_BEST_EFFORT_POLICING,
241 	SJA1105_VIRTUAL_LINKS,
242 };
243 
244 int sja1105_static_config_reload(struct sja1105_private *priv,
245 				 enum sja1105_reset_reason reason);
246 
247 /* From sja1105_spi.c */
248 int sja1105_xfer_buf(const struct sja1105_private *priv,
249 		     sja1105_spi_rw_mode_t rw, u64 reg_addr,
250 		     u8 *buf, size_t len);
251 int sja1105_xfer_u32(const struct sja1105_private *priv,
252 		     sja1105_spi_rw_mode_t rw, u64 reg_addr, u32 *value,
253 		     struct ptp_system_timestamp *ptp_sts);
254 int sja1105_xfer_u64(const struct sja1105_private *priv,
255 		     sja1105_spi_rw_mode_t rw, u64 reg_addr, u64 *value,
256 		     struct ptp_system_timestamp *ptp_sts);
257 int sja1105_static_config_upload(struct sja1105_private *priv);
258 int sja1105_inhibit_tx(const struct sja1105_private *priv,
259 		       unsigned long port_bitmap, bool tx_inhibited);
260 
261 extern struct sja1105_info sja1105e_info;
262 extern struct sja1105_info sja1105t_info;
263 extern struct sja1105_info sja1105p_info;
264 extern struct sja1105_info sja1105q_info;
265 extern struct sja1105_info sja1105r_info;
266 extern struct sja1105_info sja1105s_info;
267 
268 /* From sja1105_clocking.c */
269 
270 typedef enum {
271 	XMII_MAC = 0,
272 	XMII_PHY = 1,
273 } sja1105_mii_role_t;
274 
275 typedef enum {
276 	XMII_MODE_MII		= 0,
277 	XMII_MODE_RMII		= 1,
278 	XMII_MODE_RGMII		= 2,
279 	XMII_MODE_SGMII		= 3,
280 } sja1105_phy_interface_t;
281 
282 typedef enum {
283 	SJA1105_SPEED_10MBPS	= 3,
284 	SJA1105_SPEED_100MBPS	= 2,
285 	SJA1105_SPEED_1000MBPS	= 1,
286 	SJA1105_SPEED_AUTO	= 0,
287 } sja1105_speed_t;
288 
289 int sja1105pqrs_setup_rgmii_delay(const void *ctx, int port);
290 int sja1105_clocking_setup_port(struct sja1105_private *priv, int port);
291 int sja1105_clocking_setup(struct sja1105_private *priv);
292 
293 /* From sja1105_ethtool.c */
294 void sja1105_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data);
295 void sja1105_get_strings(struct dsa_switch *ds, int port,
296 			 u32 stringset, u8 *data);
297 int sja1105_get_sset_count(struct dsa_switch *ds, int port, int sset);
298 
299 /* From sja1105_dynamic_config.c */
300 int sja1105_dynamic_config_read(struct sja1105_private *priv,
301 				enum sja1105_blk_idx blk_idx,
302 				int index, void *entry);
303 int sja1105_dynamic_config_write(struct sja1105_private *priv,
304 				 enum sja1105_blk_idx blk_idx,
305 				 int index, void *entry, bool keep);
306 
307 enum sja1105_iotag {
308 	SJA1105_C_TAG = 0, /* Inner VLAN header */
309 	SJA1105_S_TAG = 1, /* Outer VLAN header */
310 };
311 
312 u8 sja1105et_fdb_hash(struct sja1105_private *priv, const u8 *addr, u16 vid);
313 int sja1105et_fdb_add(struct dsa_switch *ds, int port,
314 		      const unsigned char *addr, u16 vid);
315 int sja1105et_fdb_del(struct dsa_switch *ds, int port,
316 		      const unsigned char *addr, u16 vid);
317 int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
318 			const unsigned char *addr, u16 vid);
319 int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
320 			const unsigned char *addr, u16 vid);
321 
322 /* Common implementations for the static and dynamic configs */
323 size_t sja1105_l2_forwarding_entry_packing(void *buf, void *entry_ptr,
324 					   enum packing_op op);
325 size_t sja1105pqrs_l2_lookup_entry_packing(void *buf, void *entry_ptr,
326 					   enum packing_op op);
327 size_t sja1105et_l2_lookup_entry_packing(void *buf, void *entry_ptr,
328 					 enum packing_op op);
329 size_t sja1105_vlan_lookup_entry_packing(void *buf, void *entry_ptr,
330 					 enum packing_op op);
331 size_t sja1105_retagging_entry_packing(void *buf, void *entry_ptr,
332 				       enum packing_op op);
333 size_t sja1105pqrs_mac_config_entry_packing(void *buf, void *entry_ptr,
334 					    enum packing_op op);
335 size_t sja1105pqrs_avb_params_entry_packing(void *buf, void *entry_ptr,
336 					    enum packing_op op);
337 size_t sja1105_vl_lookup_entry_packing(void *buf, void *entry_ptr,
338 				       enum packing_op op);
339 
340 /* From sja1105_flower.c */
341 int sja1105_cls_flower_del(struct dsa_switch *ds, int port,
342 			   struct flow_cls_offload *cls, bool ingress);
343 int sja1105_cls_flower_add(struct dsa_switch *ds, int port,
344 			   struct flow_cls_offload *cls, bool ingress);
345 int sja1105_cls_flower_stats(struct dsa_switch *ds, int port,
346 			     struct flow_cls_offload *cls, bool ingress);
347 void sja1105_flower_setup(struct dsa_switch *ds);
348 void sja1105_flower_teardown(struct dsa_switch *ds);
349 struct sja1105_rule *sja1105_rule_find(struct sja1105_private *priv,
350 				       unsigned long cookie);
351 
352 #endif
353