1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2016-2018, NXP Semiconductors
3  * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
4  */
5 #ifndef _SJA1105_STATIC_CONFIG_H
6 #define _SJA1105_STATIC_CONFIG_H
7 
8 #include <linux/packing.h>
9 #include <linux/types.h>
10 #include <asm/types.h>
11 
12 #define SJA1105_SIZE_DEVICE_ID				4
13 #define SJA1105_SIZE_TABLE_HEADER			12
14 #define SJA1105_SIZE_L2_POLICING_ENTRY			8
15 #define SJA1105_SIZE_VLAN_LOOKUP_ENTRY			8
16 #define SJA1105_SIZE_L2_FORWARDING_ENTRY		8
17 #define SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY		12
18 #define SJA1105_SIZE_XMII_PARAMS_ENTRY			4
19 #define SJA1105ET_SIZE_L2_LOOKUP_ENTRY			12
20 #define SJA1105ET_SIZE_MAC_CONFIG_ENTRY			28
21 #define SJA1105ET_SIZE_L2_LOOKUP_PARAMS_ENTRY		4
22 #define SJA1105ET_SIZE_GENERAL_PARAMS_ENTRY		40
23 #define SJA1105ET_SIZE_AVB_PARAMS_ENTRY			12
24 #define SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY		20
25 #define SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY		32
26 #define SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY		16
27 #define SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY		44
28 #define SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY		16
29 
30 /* UM10944.pdf Page 11, Table 2. Configuration Blocks */
31 enum {
32 	BLKID_L2_LOOKUP					= 0x05,
33 	BLKID_L2_POLICING				= 0x06,
34 	BLKID_VLAN_LOOKUP				= 0x07,
35 	BLKID_L2_FORWARDING				= 0x08,
36 	BLKID_MAC_CONFIG				= 0x09,
37 	BLKID_L2_LOOKUP_PARAMS				= 0x0D,
38 	BLKID_L2_FORWARDING_PARAMS			= 0x0E,
39 	BLKID_AVB_PARAMS				= 0x10,
40 	BLKID_GENERAL_PARAMS				= 0x11,
41 	BLKID_XMII_PARAMS				= 0x4E,
42 };
43 
44 enum sja1105_blk_idx {
45 	BLK_IDX_L2_LOOKUP = 0,
46 	BLK_IDX_L2_POLICING,
47 	BLK_IDX_VLAN_LOOKUP,
48 	BLK_IDX_L2_FORWARDING,
49 	BLK_IDX_MAC_CONFIG,
50 	BLK_IDX_L2_LOOKUP_PARAMS,
51 	BLK_IDX_L2_FORWARDING_PARAMS,
52 	BLK_IDX_AVB_PARAMS,
53 	BLK_IDX_GENERAL_PARAMS,
54 	BLK_IDX_XMII_PARAMS,
55 	BLK_IDX_MAX,
56 	/* Fake block indices that are only valid for dynamic access */
57 	BLK_IDX_MGMT_ROUTE,
58 	BLK_IDX_MAX_DYN,
59 	BLK_IDX_INVAL = -1,
60 };
61 
62 #define SJA1105_MAX_L2_LOOKUP_COUNT			1024
63 #define SJA1105_MAX_L2_POLICING_COUNT			45
64 #define SJA1105_MAX_VLAN_LOOKUP_COUNT			4096
65 #define SJA1105_MAX_L2_FORWARDING_COUNT			13
66 #define SJA1105_MAX_MAC_CONFIG_COUNT			5
67 #define SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT		1
68 #define SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT		1
69 #define SJA1105_MAX_GENERAL_PARAMS_COUNT		1
70 #define SJA1105_MAX_XMII_PARAMS_COUNT			1
71 #define SJA1105_MAX_AVB_PARAMS_COUNT			1
72 
73 #define SJA1105_MAX_FRAME_MEMORY			929
74 
75 #define SJA1105E_DEVICE_ID				0x9C00000Cull
76 #define SJA1105T_DEVICE_ID				0x9E00030Eull
77 #define SJA1105PR_DEVICE_ID				0xAF00030Eull
78 #define SJA1105QS_DEVICE_ID				0xAE00030Eull
79 
80 #define SJA1105ET_PART_NO				0x9A83
81 #define SJA1105P_PART_NO				0x9A84
82 #define SJA1105Q_PART_NO				0x9A85
83 #define SJA1105R_PART_NO				0x9A86
84 #define SJA1105S_PART_NO				0x9A87
85 
86 struct sja1105_general_params_entry {
87 	u64 vllupformat;
88 	u64 mirr_ptacu;
89 	u64 switchid;
90 	u64 hostprio;
91 	u64 mac_fltres1;
92 	u64 mac_fltres0;
93 	u64 mac_flt1;
94 	u64 mac_flt0;
95 	u64 incl_srcpt1;
96 	u64 incl_srcpt0;
97 	u64 send_meta1;
98 	u64 send_meta0;
99 	u64 casc_port;
100 	u64 host_port;
101 	u64 mirr_port;
102 	u64 vlmarker;
103 	u64 vlmask;
104 	u64 tpid;
105 	u64 ignore2stf;
106 	u64 tpid2;
107 	/* P/Q/R/S only */
108 	u64 queue_ts;
109 	u64 egrmirrvid;
110 	u64 egrmirrpcp;
111 	u64 egrmirrdei;
112 	u64 replay_port;
113 };
114 
115 struct sja1105_vlan_lookup_entry {
116 	u64 ving_mirr;
117 	u64 vegr_mirr;
118 	u64 vmemb_port;
119 	u64 vlan_bc;
120 	u64 tag_port;
121 	u64 vlanid;
122 };
123 
124 struct sja1105_l2_lookup_entry {
125 	u64 vlanid;
126 	u64 macaddr;
127 	u64 destports;
128 	u64 enfport;
129 	u64 index;
130 	/* P/Q/R/S only */
131 	u64 mask_iotag;
132 	u64 mask_vlanid;
133 	u64 mask_macaddr;
134 	u64 iotag;
135 	u64 lockeds;
136 	union {
137 		/* LOCKEDS=1: Static FDB entries */
138 		struct {
139 			u64 tsreg;
140 			u64 mirrvlan;
141 			u64 takets;
142 			u64 mirr;
143 			u64 retag;
144 		};
145 		/* LOCKEDS=0: Dynamically learned FDB entries */
146 		struct {
147 			u64 touched;
148 			u64 age;
149 		};
150 	};
151 };
152 
153 struct sja1105_l2_lookup_params_entry {
154 	u64 maxaddrp[5];     /* P/Q/R/S only */
155 	u64 start_dynspc;    /* P/Q/R/S only */
156 	u64 drpnolearn;      /* P/Q/R/S only */
157 	u64 use_static;      /* P/Q/R/S only */
158 	u64 owr_dyn;         /* P/Q/R/S only */
159 	u64 learn_once;      /* P/Q/R/S only */
160 	u64 maxage;          /* Shared */
161 	u64 dyn_tbsz;        /* E/T only */
162 	u64 poly;            /* E/T only */
163 	u64 shared_learn;    /* Shared */
164 	u64 no_enf_hostprt;  /* Shared */
165 	u64 no_mgmt_learn;   /* Shared */
166 };
167 
168 struct sja1105_l2_forwarding_entry {
169 	u64 bc_domain;
170 	u64 reach_port;
171 	u64 fl_domain;
172 	u64 vlan_pmap[8];
173 };
174 
175 struct sja1105_l2_forwarding_params_entry {
176 	u64 max_dynp;
177 	u64 part_spc[8];
178 };
179 
180 struct sja1105_l2_policing_entry {
181 	u64 sharindx;
182 	u64 smax;
183 	u64 rate;
184 	u64 maxlen;
185 	u64 partition;
186 };
187 
188 struct sja1105_avb_params_entry {
189 	u64 destmeta;
190 	u64 srcmeta;
191 };
192 
193 struct sja1105_mac_config_entry {
194 	u64 top[8];
195 	u64 base[8];
196 	u64 enabled[8];
197 	u64 ifg;
198 	u64 speed;
199 	u64 tp_delin;
200 	u64 tp_delout;
201 	u64 maxage;
202 	u64 vlanprio;
203 	u64 vlanid;
204 	u64 ing_mirr;
205 	u64 egr_mirr;
206 	u64 drpnona664;
207 	u64 drpdtag;
208 	u64 drpuntag;
209 	u64 retag;
210 	u64 dyn_learn;
211 	u64 egress;
212 	u64 ingress;
213 };
214 
215 struct sja1105_xmii_params_entry {
216 	u64 phy_mac[5];
217 	u64 xmii_mode[5];
218 };
219 
220 struct sja1105_table_header {
221 	u64 block_id;
222 	u64 len;
223 	u64 crc;
224 };
225 
226 struct sja1105_table_ops {
227 	size_t (*packing)(void *buf, void *entry_ptr, enum packing_op op);
228 	size_t unpacked_entry_size;
229 	size_t packed_entry_size;
230 	size_t max_entry_count;
231 };
232 
233 struct sja1105_table {
234 	const struct sja1105_table_ops *ops;
235 	size_t entry_count;
236 	void *entries;
237 };
238 
239 struct sja1105_static_config {
240 	u64 device_id;
241 	struct sja1105_table tables[BLK_IDX_MAX];
242 };
243 
244 extern struct sja1105_table_ops sja1105e_table_ops[BLK_IDX_MAX];
245 extern struct sja1105_table_ops sja1105t_table_ops[BLK_IDX_MAX];
246 extern struct sja1105_table_ops sja1105p_table_ops[BLK_IDX_MAX];
247 extern struct sja1105_table_ops sja1105q_table_ops[BLK_IDX_MAX];
248 extern struct sja1105_table_ops sja1105r_table_ops[BLK_IDX_MAX];
249 extern struct sja1105_table_ops sja1105s_table_ops[BLK_IDX_MAX];
250 
251 size_t sja1105_table_header_packing(void *buf, void *hdr, enum packing_op op);
252 void
253 sja1105_table_header_pack_with_crc(void *buf, struct sja1105_table_header *hdr);
254 size_t
255 sja1105_static_config_get_length(const struct sja1105_static_config *config);
256 
257 typedef enum {
258 	SJA1105_CONFIG_OK = 0,
259 	SJA1105_MISSING_L2_POLICING_TABLE,
260 	SJA1105_MISSING_L2_FORWARDING_TABLE,
261 	SJA1105_MISSING_L2_FORWARDING_PARAMS_TABLE,
262 	SJA1105_MISSING_GENERAL_PARAMS_TABLE,
263 	SJA1105_MISSING_VLAN_TABLE,
264 	SJA1105_MISSING_XMII_TABLE,
265 	SJA1105_MISSING_MAC_TABLE,
266 	SJA1105_OVERCOMMITTED_FRAME_MEMORY,
267 } sja1105_config_valid_t;
268 
269 extern const char *sja1105_static_config_error_msg[];
270 
271 sja1105_config_valid_t
272 sja1105_static_config_check_valid(const struct sja1105_static_config *config);
273 void
274 sja1105_static_config_pack(void *buf, struct sja1105_static_config *config);
275 int sja1105_static_config_init(struct sja1105_static_config *config,
276 			       const struct sja1105_table_ops *static_ops,
277 			       u64 device_id);
278 void sja1105_static_config_free(struct sja1105_static_config *config);
279 
280 int sja1105_table_delete_entry(struct sja1105_table *table, int i);
281 int sja1105_table_resize(struct sja1105_table *table, size_t new_count);
282 
283 u32 sja1105_crc32(const void *buf, size_t len);
284 
285 void sja1105_pack(void *buf, const u64 *val, int start, int end, size_t len);
286 void sja1105_unpack(const void *buf, u64 *val, int start, int end, size_t len);
287 void sja1105_packing(void *buf, u64 *val, int start, int end,
288 		     size_t len, enum packing_op op);
289 
290 #endif
291