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 #include "sja1105_static_config.h"
6 #include <linux/crc32.h>
7 #include <linux/slab.h>
8 #include <linux/string.h>
9 #include <linux/errno.h>
10 
11 /* Convenience wrappers over the generic packing functions. These take into
12  * account the SJA1105 memory layout quirks and provide some level of
13  * programmer protection against incorrect API use. The errors are not expected
14  * to occur durring runtime, therefore printing and swallowing them here is
15  * appropriate instead of clutterring up higher-level code.
16  */
17 void sja1105_pack(void *buf, const u64 *val, int start, int end, size_t len)
18 {
19 	int rc = packing(buf, (u64 *)val, start, end, len,
20 			 PACK, QUIRK_LSW32_IS_FIRST);
21 
22 	if (likely(!rc))
23 		return;
24 
25 	if (rc == -EINVAL) {
26 		pr_err("Start bit (%d) expected to be larger than end (%d)\n",
27 		       start, end);
28 	} else if (rc == -ERANGE) {
29 		if ((start - end + 1) > 64)
30 			pr_err("Field %d-%d too large for 64 bits!\n",
31 			       start, end);
32 		else
33 			pr_err("Cannot store %llx inside bits %d-%d (would truncate)\n",
34 			       *val, start, end);
35 	}
36 	dump_stack();
37 }
38 
39 void sja1105_unpack(const void *buf, u64 *val, int start, int end, size_t len)
40 {
41 	int rc = packing((void *)buf, val, start, end, len,
42 			 UNPACK, QUIRK_LSW32_IS_FIRST);
43 
44 	if (likely(!rc))
45 		return;
46 
47 	if (rc == -EINVAL)
48 		pr_err("Start bit (%d) expected to be larger than end (%d)\n",
49 		       start, end);
50 	else if (rc == -ERANGE)
51 		pr_err("Field %d-%d too large for 64 bits!\n",
52 		       start, end);
53 	dump_stack();
54 }
55 
56 void sja1105_packing(void *buf, u64 *val, int start, int end,
57 		     size_t len, enum packing_op op)
58 {
59 	int rc = packing(buf, val, start, end, len, op, QUIRK_LSW32_IS_FIRST);
60 
61 	if (likely(!rc))
62 		return;
63 
64 	if (rc == -EINVAL) {
65 		pr_err("Start bit (%d) expected to be larger than end (%d)\n",
66 		       start, end);
67 	} else if (rc == -ERANGE) {
68 		if ((start - end + 1) > 64)
69 			pr_err("Field %d-%d too large for 64 bits!\n",
70 			       start, end);
71 		else
72 			pr_err("Cannot store %llx inside bits %d-%d (would truncate)\n",
73 			       *val, start, end);
74 	}
75 	dump_stack();
76 }
77 
78 /* Little-endian Ethernet CRC32 of data packed as big-endian u32 words */
79 u32 sja1105_crc32(const void *buf, size_t len)
80 {
81 	unsigned int i;
82 	u64 word;
83 	u32 crc;
84 
85 	/* seed */
86 	crc = ~0;
87 	for (i = 0; i < len; i += 4) {
88 		sja1105_unpack((void *)buf + i, &word, 31, 0, 4);
89 		crc = crc32_le(crc, (u8 *)&word, 4);
90 	}
91 	return ~crc;
92 }
93 
94 static size_t sja1105et_avb_params_entry_packing(void *buf, void *entry_ptr,
95 						 enum packing_op op)
96 {
97 	const size_t size = SJA1105ET_SIZE_AVB_PARAMS_ENTRY;
98 	struct sja1105_avb_params_entry *entry = entry_ptr;
99 
100 	sja1105_packing(buf, &entry->destmeta, 95, 48, size, op);
101 	sja1105_packing(buf, &entry->srcmeta,  47,  0, size, op);
102 	return size;
103 }
104 
105 size_t sja1105pqrs_avb_params_entry_packing(void *buf, void *entry_ptr,
106 					    enum packing_op op)
107 {
108 	const size_t size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY;
109 	struct sja1105_avb_params_entry *entry = entry_ptr;
110 
111 	sja1105_packing(buf, &entry->cas_master, 126, 126, size, op);
112 	sja1105_packing(buf, &entry->destmeta,   125,  78, size, op);
113 	sja1105_packing(buf, &entry->srcmeta,     77,  30, size, op);
114 	return size;
115 }
116 
117 static size_t sja1105et_general_params_entry_packing(void *buf, void *entry_ptr,
118 						     enum packing_op op)
119 {
120 	const size_t size = SJA1105ET_SIZE_GENERAL_PARAMS_ENTRY;
121 	struct sja1105_general_params_entry *entry = entry_ptr;
122 
123 	sja1105_packing(buf, &entry->vllupformat, 319, 319, size, op);
124 	sja1105_packing(buf, &entry->mirr_ptacu,  318, 318, size, op);
125 	sja1105_packing(buf, &entry->switchid,    317, 315, size, op);
126 	sja1105_packing(buf, &entry->hostprio,    314, 312, size, op);
127 	sja1105_packing(buf, &entry->mac_fltres1, 311, 264, size, op);
128 	sja1105_packing(buf, &entry->mac_fltres0, 263, 216, size, op);
129 	sja1105_packing(buf, &entry->mac_flt1,    215, 168, size, op);
130 	sja1105_packing(buf, &entry->mac_flt0,    167, 120, size, op);
131 	sja1105_packing(buf, &entry->incl_srcpt1, 119, 119, size, op);
132 	sja1105_packing(buf, &entry->incl_srcpt0, 118, 118, size, op);
133 	sja1105_packing(buf, &entry->send_meta1,  117, 117, size, op);
134 	sja1105_packing(buf, &entry->send_meta0,  116, 116, size, op);
135 	sja1105_packing(buf, &entry->casc_port,   115, 113, size, op);
136 	sja1105_packing(buf, &entry->host_port,   112, 110, size, op);
137 	sja1105_packing(buf, &entry->mirr_port,   109, 107, size, op);
138 	sja1105_packing(buf, &entry->vlmarker,    106,  75, size, op);
139 	sja1105_packing(buf, &entry->vlmask,       74,  43, size, op);
140 	sja1105_packing(buf, &entry->tpid,         42,  27, size, op);
141 	sja1105_packing(buf, &entry->ignore2stf,   26,  26, size, op);
142 	sja1105_packing(buf, &entry->tpid2,        25,  10, size, op);
143 	return size;
144 }
145 
146 /* TPID and TPID2 are intentionally reversed so that semantic
147  * compatibility with E/T is kept.
148  */
149 static size_t
150 sja1105pqrs_general_params_entry_packing(void *buf, void *entry_ptr,
151 					 enum packing_op op)
152 {
153 	const size_t size = SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY;
154 	struct sja1105_general_params_entry *entry = entry_ptr;
155 
156 	sja1105_packing(buf, &entry->vllupformat, 351, 351, size, op);
157 	sja1105_packing(buf, &entry->mirr_ptacu,  350, 350, size, op);
158 	sja1105_packing(buf, &entry->switchid,    349, 347, size, op);
159 	sja1105_packing(buf, &entry->hostprio,    346, 344, size, op);
160 	sja1105_packing(buf, &entry->mac_fltres1, 343, 296, size, op);
161 	sja1105_packing(buf, &entry->mac_fltres0, 295, 248, size, op);
162 	sja1105_packing(buf, &entry->mac_flt1,    247, 200, size, op);
163 	sja1105_packing(buf, &entry->mac_flt0,    199, 152, size, op);
164 	sja1105_packing(buf, &entry->incl_srcpt1, 151, 151, size, op);
165 	sja1105_packing(buf, &entry->incl_srcpt0, 150, 150, size, op);
166 	sja1105_packing(buf, &entry->send_meta1,  149, 149, size, op);
167 	sja1105_packing(buf, &entry->send_meta0,  148, 148, size, op);
168 	sja1105_packing(buf, &entry->casc_port,   147, 145, size, op);
169 	sja1105_packing(buf, &entry->host_port,   144, 142, size, op);
170 	sja1105_packing(buf, &entry->mirr_port,   141, 139, size, op);
171 	sja1105_packing(buf, &entry->vlmarker,    138, 107, size, op);
172 	sja1105_packing(buf, &entry->vlmask,      106,  75, size, op);
173 	sja1105_packing(buf, &entry->tpid2,        74,  59, size, op);
174 	sja1105_packing(buf, &entry->ignore2stf,   58,  58, size, op);
175 	sja1105_packing(buf, &entry->tpid,         57,  42, size, op);
176 	sja1105_packing(buf, &entry->queue_ts,     41,  41, size, op);
177 	sja1105_packing(buf, &entry->egrmirrvid,   40,  29, size, op);
178 	sja1105_packing(buf, &entry->egrmirrpcp,   28,  26, size, op);
179 	sja1105_packing(buf, &entry->egrmirrdei,   25,  25, size, op);
180 	sja1105_packing(buf, &entry->replay_port,  24,  22, size, op);
181 	return size;
182 }
183 
184 static size_t
185 sja1105_l2_forwarding_params_entry_packing(void *buf, void *entry_ptr,
186 					   enum packing_op op)
187 {
188 	const size_t size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY;
189 	struct sja1105_l2_forwarding_params_entry *entry = entry_ptr;
190 	int offset, i;
191 
192 	sja1105_packing(buf, &entry->max_dynp, 95, 93, size, op);
193 	for (i = 0, offset = 13; i < 8; i++, offset += 10)
194 		sja1105_packing(buf, &entry->part_spc[i],
195 				offset + 9, offset + 0, size, op);
196 	return size;
197 }
198 
199 size_t sja1105_l2_forwarding_entry_packing(void *buf, void *entry_ptr,
200 					   enum packing_op op)
201 {
202 	const size_t size = SJA1105_SIZE_L2_FORWARDING_ENTRY;
203 	struct sja1105_l2_forwarding_entry *entry = entry_ptr;
204 	int offset, i;
205 
206 	sja1105_packing(buf, &entry->bc_domain,  63, 59, size, op);
207 	sja1105_packing(buf, &entry->reach_port, 58, 54, size, op);
208 	sja1105_packing(buf, &entry->fl_domain,  53, 49, size, op);
209 	for (i = 0, offset = 25; i < 8; i++, offset += 3)
210 		sja1105_packing(buf, &entry->vlan_pmap[i],
211 				offset + 2, offset + 0, size, op);
212 	return size;
213 }
214 
215 static size_t
216 sja1105et_l2_lookup_params_entry_packing(void *buf, void *entry_ptr,
217 					 enum packing_op op)
218 {
219 	const size_t size = SJA1105ET_SIZE_L2_LOOKUP_PARAMS_ENTRY;
220 	struct sja1105_l2_lookup_params_entry *entry = entry_ptr;
221 
222 	sja1105_packing(buf, &entry->maxage,         31, 17, size, op);
223 	sja1105_packing(buf, &entry->dyn_tbsz,       16, 14, size, op);
224 	sja1105_packing(buf, &entry->poly,           13,  6, size, op);
225 	sja1105_packing(buf, &entry->shared_learn,    5,  5, size, op);
226 	sja1105_packing(buf, &entry->no_enf_hostprt,  4,  4, size, op);
227 	sja1105_packing(buf, &entry->no_mgmt_learn,   3,  3, size, op);
228 	return size;
229 }
230 
231 static size_t
232 sja1105pqrs_l2_lookup_params_entry_packing(void *buf, void *entry_ptr,
233 					   enum packing_op op)
234 {
235 	const size_t size = SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY;
236 	struct sja1105_l2_lookup_params_entry *entry = entry_ptr;
237 	int offset, i;
238 
239 	for (i = 0, offset = 58; i < 5; i++, offset += 11)
240 		sja1105_packing(buf, &entry->maxaddrp[i],
241 				offset + 10, offset + 0, size, op);
242 	sja1105_packing(buf, &entry->maxage,         57,  43, size, op);
243 	sja1105_packing(buf, &entry->start_dynspc,   42,  33, size, op);
244 	sja1105_packing(buf, &entry->drpnolearn,     32,  28, size, op);
245 	sja1105_packing(buf, &entry->shared_learn,   27,  27, size, op);
246 	sja1105_packing(buf, &entry->no_enf_hostprt, 26,  26, size, op);
247 	sja1105_packing(buf, &entry->no_mgmt_learn,  25,  25, size, op);
248 	sja1105_packing(buf, &entry->use_static,     24,  24, size, op);
249 	sja1105_packing(buf, &entry->owr_dyn,        23,  23, size, op);
250 	sja1105_packing(buf, &entry->learn_once,     22,  22, size, op);
251 	return size;
252 }
253 
254 size_t sja1105et_l2_lookup_entry_packing(void *buf, void *entry_ptr,
255 					 enum packing_op op)
256 {
257 	const size_t size = SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
258 	struct sja1105_l2_lookup_entry *entry = entry_ptr;
259 
260 	sja1105_packing(buf, &entry->vlanid,    95, 84, size, op);
261 	sja1105_packing(buf, &entry->macaddr,   83, 36, size, op);
262 	sja1105_packing(buf, &entry->destports, 35, 31, size, op);
263 	sja1105_packing(buf, &entry->enfport,   30, 30, size, op);
264 	sja1105_packing(buf, &entry->index,     29, 20, size, op);
265 	return size;
266 }
267 
268 size_t sja1105pqrs_l2_lookup_entry_packing(void *buf, void *entry_ptr,
269 					   enum packing_op op)
270 {
271 	const size_t size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
272 	struct sja1105_l2_lookup_entry *entry = entry_ptr;
273 
274 	if (entry->lockeds) {
275 		sja1105_packing(buf, &entry->tsreg,    159, 159, size, op);
276 		sja1105_packing(buf, &entry->mirrvlan, 158, 147, size, op);
277 		sja1105_packing(buf, &entry->takets,   146, 146, size, op);
278 		sja1105_packing(buf, &entry->mirr,     145, 145, size, op);
279 		sja1105_packing(buf, &entry->retag,    144, 144, size, op);
280 	} else {
281 		sja1105_packing(buf, &entry->touched,  159, 159, size, op);
282 		sja1105_packing(buf, &entry->age,      158, 144, size, op);
283 	}
284 	sja1105_packing(buf, &entry->mask_iotag,   143, 143, size, op);
285 	sja1105_packing(buf, &entry->mask_vlanid,  142, 131, size, op);
286 	sja1105_packing(buf, &entry->mask_macaddr, 130,  83, size, op);
287 	sja1105_packing(buf, &entry->iotag,         82,  82, size, op);
288 	sja1105_packing(buf, &entry->vlanid,        81,  70, size, op);
289 	sja1105_packing(buf, &entry->macaddr,       69,  22, size, op);
290 	sja1105_packing(buf, &entry->destports,     21,  17, size, op);
291 	sja1105_packing(buf, &entry->enfport,       16,  16, size, op);
292 	sja1105_packing(buf, &entry->index,         15,   6, size, op);
293 	return size;
294 }
295 
296 static size_t sja1105_l2_policing_entry_packing(void *buf, void *entry_ptr,
297 						enum packing_op op)
298 {
299 	const size_t size = SJA1105_SIZE_L2_POLICING_ENTRY;
300 	struct sja1105_l2_policing_entry *entry = entry_ptr;
301 
302 	sja1105_packing(buf, &entry->sharindx,  63, 58, size, op);
303 	sja1105_packing(buf, &entry->smax,      57, 42, size, op);
304 	sja1105_packing(buf, &entry->rate,      41, 26, size, op);
305 	sja1105_packing(buf, &entry->maxlen,    25, 15, size, op);
306 	sja1105_packing(buf, &entry->partition, 14, 12, size, op);
307 	return size;
308 }
309 
310 static size_t sja1105et_mac_config_entry_packing(void *buf, void *entry_ptr,
311 						 enum packing_op op)
312 {
313 	const size_t size = SJA1105ET_SIZE_MAC_CONFIG_ENTRY;
314 	struct sja1105_mac_config_entry *entry = entry_ptr;
315 	int offset, i;
316 
317 	for (i = 0, offset = 72; i < 8; i++, offset += 19) {
318 		sja1105_packing(buf, &entry->enabled[i],
319 				offset +  0, offset +  0, size, op);
320 		sja1105_packing(buf, &entry->base[i],
321 				offset +  9, offset +  1, size, op);
322 		sja1105_packing(buf, &entry->top[i],
323 				offset + 18, offset + 10, size, op);
324 	}
325 	sja1105_packing(buf, &entry->ifg,       71, 67, size, op);
326 	sja1105_packing(buf, &entry->speed,     66, 65, size, op);
327 	sja1105_packing(buf, &entry->tp_delin,  64, 49, size, op);
328 	sja1105_packing(buf, &entry->tp_delout, 48, 33, size, op);
329 	sja1105_packing(buf, &entry->maxage,    32, 25, size, op);
330 	sja1105_packing(buf, &entry->vlanprio,  24, 22, size, op);
331 	sja1105_packing(buf, &entry->vlanid,    21, 10, size, op);
332 	sja1105_packing(buf, &entry->ing_mirr,   9,  9, size, op);
333 	sja1105_packing(buf, &entry->egr_mirr,   8,  8, size, op);
334 	sja1105_packing(buf, &entry->drpnona664, 7,  7, size, op);
335 	sja1105_packing(buf, &entry->drpdtag,    6,  6, size, op);
336 	sja1105_packing(buf, &entry->drpuntag,   5,  5, size, op);
337 	sja1105_packing(buf, &entry->retag,      4,  4, size, op);
338 	sja1105_packing(buf, &entry->dyn_learn,  3,  3, size, op);
339 	sja1105_packing(buf, &entry->egress,     2,  2, size, op);
340 	sja1105_packing(buf, &entry->ingress,    1,  1, size, op);
341 	return size;
342 }
343 
344 size_t sja1105pqrs_mac_config_entry_packing(void *buf, void *entry_ptr,
345 					    enum packing_op op)
346 {
347 	const size_t size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY;
348 	struct sja1105_mac_config_entry *entry = entry_ptr;
349 	int offset, i;
350 
351 	for (i = 0, offset = 104; i < 8; i++, offset += 19) {
352 		sja1105_packing(buf, &entry->enabled[i],
353 				offset +  0, offset +  0, size, op);
354 		sja1105_packing(buf, &entry->base[i],
355 				offset +  9, offset +  1, size, op);
356 		sja1105_packing(buf, &entry->top[i],
357 				offset + 18, offset + 10, size, op);
358 	}
359 	sja1105_packing(buf, &entry->ifg,       103, 99, size, op);
360 	sja1105_packing(buf, &entry->speed,      98, 97, size, op);
361 	sja1105_packing(buf, &entry->tp_delin,   96, 81, size, op);
362 	sja1105_packing(buf, &entry->tp_delout,  80, 65, size, op);
363 	sja1105_packing(buf, &entry->maxage,     64, 57, size, op);
364 	sja1105_packing(buf, &entry->vlanprio,   56, 54, size, op);
365 	sja1105_packing(buf, &entry->vlanid,     53, 42, size, op);
366 	sja1105_packing(buf, &entry->ing_mirr,   41, 41, size, op);
367 	sja1105_packing(buf, &entry->egr_mirr,   40, 40, size, op);
368 	sja1105_packing(buf, &entry->drpnona664, 39, 39, size, op);
369 	sja1105_packing(buf, &entry->drpdtag,    38, 38, size, op);
370 	sja1105_packing(buf, &entry->drpuntag,   35, 35, size, op);
371 	sja1105_packing(buf, &entry->retag,      34, 34, size, op);
372 	sja1105_packing(buf, &entry->dyn_learn,  33, 33, size, op);
373 	sja1105_packing(buf, &entry->egress,     32, 32, size, op);
374 	sja1105_packing(buf, &entry->ingress,    31, 31, size, op);
375 	return size;
376 }
377 
378 static size_t
379 sja1105_schedule_entry_points_params_entry_packing(void *buf, void *entry_ptr,
380 						   enum packing_op op)
381 {
382 	struct sja1105_schedule_entry_points_params_entry *entry = entry_ptr;
383 	const size_t size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_PARAMS_ENTRY;
384 
385 	sja1105_packing(buf, &entry->clksrc,    31, 30, size, op);
386 	sja1105_packing(buf, &entry->actsubsch, 29, 27, size, op);
387 	return size;
388 }
389 
390 static size_t
391 sja1105_schedule_entry_points_entry_packing(void *buf, void *entry_ptr,
392 					    enum packing_op op)
393 {
394 	struct sja1105_schedule_entry_points_entry *entry = entry_ptr;
395 	const size_t size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_ENTRY;
396 
397 	sja1105_packing(buf, &entry->subschindx, 31, 29, size, op);
398 	sja1105_packing(buf, &entry->delta,      28, 11, size, op);
399 	sja1105_packing(buf, &entry->address,    10, 1,  size, op);
400 	return size;
401 }
402 
403 static size_t sja1105_schedule_params_entry_packing(void *buf, void *entry_ptr,
404 						    enum packing_op op)
405 {
406 	const size_t size = SJA1105_SIZE_SCHEDULE_PARAMS_ENTRY;
407 	struct sja1105_schedule_params_entry *entry = entry_ptr;
408 	int offset, i;
409 
410 	for (i = 0, offset = 16; i < 8; i++, offset += 10)
411 		sja1105_packing(buf, &entry->subscheind[i],
412 				offset + 9, offset + 0, size, op);
413 	return size;
414 }
415 
416 static size_t sja1105_schedule_entry_packing(void *buf, void *entry_ptr,
417 					     enum packing_op op)
418 {
419 	const size_t size = SJA1105_SIZE_SCHEDULE_ENTRY;
420 	struct sja1105_schedule_entry *entry = entry_ptr;
421 
422 	sja1105_packing(buf, &entry->winstindex,  63, 54, size, op);
423 	sja1105_packing(buf, &entry->winend,      53, 53, size, op);
424 	sja1105_packing(buf, &entry->winst,       52, 52, size, op);
425 	sja1105_packing(buf, &entry->destports,   51, 47, size, op);
426 	sja1105_packing(buf, &entry->setvalid,    46, 46, size, op);
427 	sja1105_packing(buf, &entry->txen,        45, 45, size, op);
428 	sja1105_packing(buf, &entry->resmedia_en, 44, 44, size, op);
429 	sja1105_packing(buf, &entry->resmedia,    43, 36, size, op);
430 	sja1105_packing(buf, &entry->vlindex,     35, 26, size, op);
431 	sja1105_packing(buf, &entry->delta,       25, 8,  size, op);
432 	return size;
433 }
434 
435 size_t sja1105_vlan_lookup_entry_packing(void *buf, void *entry_ptr,
436 					 enum packing_op op)
437 {
438 	const size_t size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY;
439 	struct sja1105_vlan_lookup_entry *entry = entry_ptr;
440 
441 	sja1105_packing(buf, &entry->ving_mirr,  63, 59, size, op);
442 	sja1105_packing(buf, &entry->vegr_mirr,  58, 54, size, op);
443 	sja1105_packing(buf, &entry->vmemb_port, 53, 49, size, op);
444 	sja1105_packing(buf, &entry->vlan_bc,    48, 44, size, op);
445 	sja1105_packing(buf, &entry->tag_port,   43, 39, size, op);
446 	sja1105_packing(buf, &entry->vlanid,     38, 27, size, op);
447 	return size;
448 }
449 
450 static size_t sja1105_xmii_params_entry_packing(void *buf, void *entry_ptr,
451 						enum packing_op op)
452 {
453 	const size_t size = SJA1105_SIZE_XMII_PARAMS_ENTRY;
454 	struct sja1105_xmii_params_entry *entry = entry_ptr;
455 	int offset, i;
456 
457 	for (i = 0, offset = 17; i < 5; i++, offset += 3) {
458 		sja1105_packing(buf, &entry->xmii_mode[i],
459 				offset + 1, offset + 0, size, op);
460 		sja1105_packing(buf, &entry->phy_mac[i],
461 				offset + 2, offset + 2, size, op);
462 	}
463 	return size;
464 }
465 
466 size_t sja1105_table_header_packing(void *buf, void *entry_ptr,
467 				    enum packing_op op)
468 {
469 	const size_t size = SJA1105_SIZE_TABLE_HEADER;
470 	struct sja1105_table_header *entry = entry_ptr;
471 
472 	sja1105_packing(buf, &entry->block_id, 31, 24, size, op);
473 	sja1105_packing(buf, &entry->len,      55, 32, size, op);
474 	sja1105_packing(buf, &entry->crc,      95, 64, size, op);
475 	return size;
476 }
477 
478 /* WARNING: the *hdr pointer is really non-const, because it is
479  * modifying the CRC of the header for a 2-stage packing operation
480  */
481 void
482 sja1105_table_header_pack_with_crc(void *buf, struct sja1105_table_header *hdr)
483 {
484 	/* First pack the table as-is, then calculate the CRC, and
485 	 * finally put the proper CRC into the packed buffer
486 	 */
487 	memset(buf, 0, SJA1105_SIZE_TABLE_HEADER);
488 	sja1105_table_header_packing(buf, hdr, PACK);
489 	hdr->crc = sja1105_crc32(buf, SJA1105_SIZE_TABLE_HEADER - 4);
490 	sja1105_pack(buf + SJA1105_SIZE_TABLE_HEADER - 4, &hdr->crc, 31, 0, 4);
491 }
492 
493 static void sja1105_table_write_crc(u8 *table_start, u8 *crc_ptr)
494 {
495 	u64 computed_crc;
496 	int len_bytes;
497 
498 	len_bytes = (uintptr_t)(crc_ptr - table_start);
499 	computed_crc = sja1105_crc32(table_start, len_bytes);
500 	sja1105_pack(crc_ptr, &computed_crc, 31, 0, 4);
501 }
502 
503 /* The block IDs that the switches support are unfortunately sparse, so keep a
504  * mapping table to "block indices" and translate back and forth so that we
505  * don't waste useless memory in struct sja1105_static_config.
506  * Also, since the block id comes from essentially untrusted input (unpacking
507  * the static config from userspace) it has to be sanitized (range-checked)
508  * before blindly indexing kernel memory with the blk_idx.
509  */
510 static u64 blk_id_map[BLK_IDX_MAX] = {
511 	[BLK_IDX_SCHEDULE] = BLKID_SCHEDULE,
512 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = BLKID_SCHEDULE_ENTRY_POINTS,
513 	[BLK_IDX_L2_LOOKUP] = BLKID_L2_LOOKUP,
514 	[BLK_IDX_L2_POLICING] = BLKID_L2_POLICING,
515 	[BLK_IDX_VLAN_LOOKUP] = BLKID_VLAN_LOOKUP,
516 	[BLK_IDX_L2_FORWARDING] = BLKID_L2_FORWARDING,
517 	[BLK_IDX_MAC_CONFIG] = BLKID_MAC_CONFIG,
518 	[BLK_IDX_SCHEDULE_PARAMS] = BLKID_SCHEDULE_PARAMS,
519 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = BLKID_SCHEDULE_ENTRY_POINTS_PARAMS,
520 	[BLK_IDX_L2_LOOKUP_PARAMS] = BLKID_L2_LOOKUP_PARAMS,
521 	[BLK_IDX_L2_FORWARDING_PARAMS] = BLKID_L2_FORWARDING_PARAMS,
522 	[BLK_IDX_AVB_PARAMS] = BLKID_AVB_PARAMS,
523 	[BLK_IDX_GENERAL_PARAMS] = BLKID_GENERAL_PARAMS,
524 	[BLK_IDX_XMII_PARAMS] = BLKID_XMII_PARAMS,
525 };
526 
527 const char *sja1105_static_config_error_msg[] = {
528 	[SJA1105_CONFIG_OK] = "",
529 	[SJA1105_TTETHERNET_NOT_SUPPORTED] =
530 		"schedule-table present, but TTEthernet is "
531 		"only supported on T and Q/S",
532 	[SJA1105_INCORRECT_TTETHERNET_CONFIGURATION] =
533 		"schedule-table present, but one of "
534 		"schedule-entry-points-table, schedule-parameters-table or "
535 		"schedule-entry-points-parameters table is empty",
536 	[SJA1105_MISSING_L2_POLICING_TABLE] =
537 		"l2-policing-table needs to have at least one entry",
538 	[SJA1105_MISSING_L2_FORWARDING_TABLE] =
539 		"l2-forwarding-table is either missing or incomplete",
540 	[SJA1105_MISSING_L2_FORWARDING_PARAMS_TABLE] =
541 		"l2-forwarding-parameters-table is missing",
542 	[SJA1105_MISSING_GENERAL_PARAMS_TABLE] =
543 		"general-parameters-table is missing",
544 	[SJA1105_MISSING_VLAN_TABLE] =
545 		"vlan-lookup-table needs to have at least the default untagged VLAN",
546 	[SJA1105_MISSING_XMII_TABLE] =
547 		"xmii-table is missing",
548 	[SJA1105_MISSING_MAC_TABLE] =
549 		"mac-configuration-table needs to contain an entry for each port",
550 	[SJA1105_OVERCOMMITTED_FRAME_MEMORY] =
551 		"Not allowed to overcommit frame memory. L2 memory partitions "
552 		"and VL memory partitions share the same space. The sum of all "
553 		"16 memory partitions is not allowed to be larger than 929 "
554 		"128-byte blocks (or 910 with retagging). Please adjust "
555 		"l2-forwarding-parameters-table.part_spc and/or "
556 		"vl-forwarding-parameters-table.partspc.",
557 };
558 
559 static sja1105_config_valid_t
560 static_config_check_memory_size(const struct sja1105_table *tables)
561 {
562 	const struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
563 	int i, mem = 0;
564 
565 	l2_fwd_params = tables[BLK_IDX_L2_FORWARDING_PARAMS].entries;
566 
567 	for (i = 0; i < 8; i++)
568 		mem += l2_fwd_params->part_spc[i];
569 
570 	if (mem > SJA1105_MAX_FRAME_MEMORY)
571 		return SJA1105_OVERCOMMITTED_FRAME_MEMORY;
572 
573 	return SJA1105_CONFIG_OK;
574 }
575 
576 sja1105_config_valid_t
577 sja1105_static_config_check_valid(const struct sja1105_static_config *config)
578 {
579 	const struct sja1105_table *tables = config->tables;
580 #define IS_FULL(blk_idx) \
581 	(tables[blk_idx].entry_count == tables[blk_idx].ops->max_entry_count)
582 
583 	if (tables[BLK_IDX_SCHEDULE].entry_count) {
584 		if (config->device_id != SJA1105T_DEVICE_ID &&
585 		    config->device_id != SJA1105QS_DEVICE_ID)
586 			return SJA1105_TTETHERNET_NOT_SUPPORTED;
587 
588 		if (tables[BLK_IDX_SCHEDULE_ENTRY_POINTS].entry_count == 0)
589 			return SJA1105_INCORRECT_TTETHERNET_CONFIGURATION;
590 
591 		if (!IS_FULL(BLK_IDX_SCHEDULE_PARAMS))
592 			return SJA1105_INCORRECT_TTETHERNET_CONFIGURATION;
593 
594 		if (!IS_FULL(BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS))
595 			return SJA1105_INCORRECT_TTETHERNET_CONFIGURATION;
596 	}
597 
598 	if (tables[BLK_IDX_L2_POLICING].entry_count == 0)
599 		return SJA1105_MISSING_L2_POLICING_TABLE;
600 
601 	if (tables[BLK_IDX_VLAN_LOOKUP].entry_count == 0)
602 		return SJA1105_MISSING_VLAN_TABLE;
603 
604 	if (!IS_FULL(BLK_IDX_L2_FORWARDING))
605 		return SJA1105_MISSING_L2_FORWARDING_TABLE;
606 
607 	if (!IS_FULL(BLK_IDX_MAC_CONFIG))
608 		return SJA1105_MISSING_MAC_TABLE;
609 
610 	if (!IS_FULL(BLK_IDX_L2_FORWARDING_PARAMS))
611 		return SJA1105_MISSING_L2_FORWARDING_PARAMS_TABLE;
612 
613 	if (!IS_FULL(BLK_IDX_GENERAL_PARAMS))
614 		return SJA1105_MISSING_GENERAL_PARAMS_TABLE;
615 
616 	if (!IS_FULL(BLK_IDX_XMII_PARAMS))
617 		return SJA1105_MISSING_XMII_TABLE;
618 
619 	return static_config_check_memory_size(tables);
620 #undef IS_FULL
621 }
622 
623 void
624 sja1105_static_config_pack(void *buf, struct sja1105_static_config *config)
625 {
626 	struct sja1105_table_header header = {0};
627 	enum sja1105_blk_idx i;
628 	char *p = buf;
629 	int j;
630 
631 	sja1105_pack(p, &config->device_id, 31, 0, 4);
632 	p += SJA1105_SIZE_DEVICE_ID;
633 
634 	for (i = 0; i < BLK_IDX_MAX; i++) {
635 		const struct sja1105_table *table;
636 		char *table_start;
637 
638 		table = &config->tables[i];
639 		if (!table->entry_count)
640 			continue;
641 
642 		header.block_id = blk_id_map[i];
643 		header.len = table->entry_count *
644 			     table->ops->packed_entry_size / 4;
645 		sja1105_table_header_pack_with_crc(p, &header);
646 		p += SJA1105_SIZE_TABLE_HEADER;
647 		table_start = p;
648 		for (j = 0; j < table->entry_count; j++) {
649 			u8 *entry_ptr = table->entries;
650 
651 			entry_ptr += j * table->ops->unpacked_entry_size;
652 			memset(p, 0, table->ops->packed_entry_size);
653 			table->ops->packing(p, entry_ptr, PACK);
654 			p += table->ops->packed_entry_size;
655 		}
656 		sja1105_table_write_crc(table_start, p);
657 		p += 4;
658 	}
659 	/* Final header:
660 	 * Block ID does not matter
661 	 * Length of 0 marks that header is final
662 	 * CRC will be replaced on-the-fly on "config upload"
663 	 */
664 	header.block_id = 0;
665 	header.len = 0;
666 	header.crc = 0xDEADBEEF;
667 	memset(p, 0, SJA1105_SIZE_TABLE_HEADER);
668 	sja1105_table_header_packing(p, &header, PACK);
669 }
670 
671 size_t
672 sja1105_static_config_get_length(const struct sja1105_static_config *config)
673 {
674 	unsigned int sum;
675 	unsigned int header_count;
676 	enum sja1105_blk_idx i;
677 
678 	/* Ending header */
679 	header_count = 1;
680 	sum = SJA1105_SIZE_DEVICE_ID;
681 
682 	/* Tables (headers and entries) */
683 	for (i = 0; i < BLK_IDX_MAX; i++) {
684 		const struct sja1105_table *table;
685 
686 		table = &config->tables[i];
687 		if (table->entry_count)
688 			header_count++;
689 
690 		sum += table->ops->packed_entry_size * table->entry_count;
691 	}
692 	/* Headers have an additional CRC at the end */
693 	sum += header_count * (SJA1105_SIZE_TABLE_HEADER + 4);
694 	/* Last header does not have an extra CRC because there is no data */
695 	sum -= 4;
696 
697 	return sum;
698 }
699 
700 /* Compatibility matrices */
701 
702 /* SJA1105E: First generation, no TTEthernet */
703 struct sja1105_table_ops sja1105e_table_ops[BLK_IDX_MAX] = {
704 	[BLK_IDX_SCHEDULE] = {0},
705 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {0},
706 	[BLK_IDX_L2_LOOKUP] = {
707 		.packing = sja1105et_l2_lookup_entry_packing,
708 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_entry),
709 		.packed_entry_size = SJA1105ET_SIZE_L2_LOOKUP_ENTRY,
710 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
711 	},
712 	[BLK_IDX_L2_POLICING] = {
713 		.packing = sja1105_l2_policing_entry_packing,
714 		.unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
715 		.packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
716 		.max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
717 	},
718 	[BLK_IDX_VLAN_LOOKUP] = {
719 		.packing = sja1105_vlan_lookup_entry_packing,
720 		.unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
721 		.packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
722 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
723 	},
724 	[BLK_IDX_L2_FORWARDING] = {
725 		.packing = sja1105_l2_forwarding_entry_packing,
726 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
727 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
728 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
729 	},
730 	[BLK_IDX_MAC_CONFIG] = {
731 		.packing = sja1105et_mac_config_entry_packing,
732 		.unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
733 		.packed_entry_size = SJA1105ET_SIZE_MAC_CONFIG_ENTRY,
734 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
735 	},
736 	[BLK_IDX_SCHEDULE_PARAMS] = {0},
737 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {0},
738 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
739 		.packing = sja1105et_l2_lookup_params_entry_packing,
740 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_params_entry),
741 		.packed_entry_size = SJA1105ET_SIZE_L2_LOOKUP_PARAMS_ENTRY,
742 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
743 	},
744 	[BLK_IDX_L2_FORWARDING_PARAMS] = {
745 		.packing = sja1105_l2_forwarding_params_entry_packing,
746 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
747 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
748 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
749 	},
750 	[BLK_IDX_AVB_PARAMS] = {
751 		.packing = sja1105et_avb_params_entry_packing,
752 		.unpacked_entry_size = sizeof(struct sja1105_avb_params_entry),
753 		.packed_entry_size = SJA1105ET_SIZE_AVB_PARAMS_ENTRY,
754 		.max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
755 	},
756 	[BLK_IDX_GENERAL_PARAMS] = {
757 		.packing = sja1105et_general_params_entry_packing,
758 		.unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
759 		.packed_entry_size = SJA1105ET_SIZE_GENERAL_PARAMS_ENTRY,
760 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
761 	},
762 	[BLK_IDX_XMII_PARAMS] = {
763 		.packing = sja1105_xmii_params_entry_packing,
764 		.unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
765 		.packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
766 		.max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
767 	},
768 };
769 
770 /* SJA1105T: First generation, TTEthernet */
771 struct sja1105_table_ops sja1105t_table_ops[BLK_IDX_MAX] = {
772 	[BLK_IDX_SCHEDULE] = {
773 		.packing = sja1105_schedule_entry_packing,
774 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry),
775 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY,
776 		.max_entry_count = SJA1105_MAX_SCHEDULE_COUNT,
777 	},
778 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {
779 		.packing = sja1105_schedule_entry_points_entry_packing,
780 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry_points_entry),
781 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_ENTRY,
782 		.max_entry_count = SJA1105_MAX_SCHEDULE_ENTRY_POINTS_COUNT,
783 	},
784 	[BLK_IDX_L2_LOOKUP] = {
785 		.packing = sja1105et_l2_lookup_entry_packing,
786 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_entry),
787 		.packed_entry_size = SJA1105ET_SIZE_L2_LOOKUP_ENTRY,
788 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
789 	},
790 	[BLK_IDX_L2_POLICING] = {
791 		.packing = sja1105_l2_policing_entry_packing,
792 		.unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
793 		.packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
794 		.max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
795 	},
796 	[BLK_IDX_VLAN_LOOKUP] = {
797 		.packing = sja1105_vlan_lookup_entry_packing,
798 		.unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
799 		.packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
800 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
801 	},
802 	[BLK_IDX_L2_FORWARDING] = {
803 		.packing = sja1105_l2_forwarding_entry_packing,
804 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
805 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
806 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
807 	},
808 	[BLK_IDX_MAC_CONFIG] = {
809 		.packing = sja1105et_mac_config_entry_packing,
810 		.unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
811 		.packed_entry_size = SJA1105ET_SIZE_MAC_CONFIG_ENTRY,
812 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
813 	},
814 	[BLK_IDX_SCHEDULE_PARAMS] = {
815 		.packing = sja1105_schedule_params_entry_packing,
816 		.unpacked_entry_size = sizeof(struct sja1105_schedule_params_entry),
817 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_PARAMS_ENTRY,
818 		.max_entry_count = SJA1105_MAX_SCHEDULE_PARAMS_COUNT,
819 	},
820 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {
821 		.packing = sja1105_schedule_entry_points_params_entry_packing,
822 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry_points_params_entry),
823 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_PARAMS_ENTRY,
824 		.max_entry_count = SJA1105_MAX_SCHEDULE_ENTRY_POINTS_PARAMS_COUNT,
825 	},
826 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
827 		.packing = sja1105et_l2_lookup_params_entry_packing,
828 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_params_entry),
829 		.packed_entry_size = SJA1105ET_SIZE_L2_LOOKUP_PARAMS_ENTRY,
830 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
831 	},
832 	[BLK_IDX_L2_FORWARDING_PARAMS] = {
833 		.packing = sja1105_l2_forwarding_params_entry_packing,
834 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
835 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
836 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
837 	},
838 	[BLK_IDX_AVB_PARAMS] = {
839 		.packing = sja1105et_avb_params_entry_packing,
840 		.unpacked_entry_size = sizeof(struct sja1105_avb_params_entry),
841 		.packed_entry_size = SJA1105ET_SIZE_AVB_PARAMS_ENTRY,
842 		.max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
843 	},
844 	[BLK_IDX_GENERAL_PARAMS] = {
845 		.packing = sja1105et_general_params_entry_packing,
846 		.unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
847 		.packed_entry_size = SJA1105ET_SIZE_GENERAL_PARAMS_ENTRY,
848 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
849 	},
850 	[BLK_IDX_XMII_PARAMS] = {
851 		.packing = sja1105_xmii_params_entry_packing,
852 		.unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
853 		.packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
854 		.max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
855 	},
856 };
857 
858 /* SJA1105P: Second generation, no TTEthernet, no SGMII */
859 struct sja1105_table_ops sja1105p_table_ops[BLK_IDX_MAX] = {
860 	[BLK_IDX_SCHEDULE] = {0},
861 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {0},
862 	[BLK_IDX_L2_LOOKUP] = {
863 		.packing = sja1105pqrs_l2_lookup_entry_packing,
864 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_entry),
865 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY,
866 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
867 	},
868 	[BLK_IDX_L2_POLICING] = {
869 		.packing = sja1105_l2_policing_entry_packing,
870 		.unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
871 		.packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
872 		.max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
873 	},
874 	[BLK_IDX_VLAN_LOOKUP] = {
875 		.packing = sja1105_vlan_lookup_entry_packing,
876 		.unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
877 		.packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
878 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
879 	},
880 	[BLK_IDX_L2_FORWARDING] = {
881 		.packing = sja1105_l2_forwarding_entry_packing,
882 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
883 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
884 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
885 	},
886 	[BLK_IDX_MAC_CONFIG] = {
887 		.packing = sja1105pqrs_mac_config_entry_packing,
888 		.unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
889 		.packed_entry_size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY,
890 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
891 	},
892 	[BLK_IDX_SCHEDULE_PARAMS] = {0},
893 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {0},
894 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
895 		.packing = sja1105pqrs_l2_lookup_params_entry_packing,
896 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_params_entry),
897 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY,
898 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
899 	},
900 	[BLK_IDX_L2_FORWARDING_PARAMS] = {
901 		.packing = sja1105_l2_forwarding_params_entry_packing,
902 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
903 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
904 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
905 	},
906 	[BLK_IDX_AVB_PARAMS] = {
907 		.packing = sja1105pqrs_avb_params_entry_packing,
908 		.unpacked_entry_size = sizeof(struct sja1105_avb_params_entry),
909 		.packed_entry_size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY,
910 		.max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
911 	},
912 	[BLK_IDX_GENERAL_PARAMS] = {
913 		.packing = sja1105pqrs_general_params_entry_packing,
914 		.unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
915 		.packed_entry_size = SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY,
916 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
917 	},
918 	[BLK_IDX_XMII_PARAMS] = {
919 		.packing = sja1105_xmii_params_entry_packing,
920 		.unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
921 		.packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
922 		.max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
923 	},
924 };
925 
926 /* SJA1105Q: Second generation, TTEthernet, no SGMII */
927 struct sja1105_table_ops sja1105q_table_ops[BLK_IDX_MAX] = {
928 	[BLK_IDX_SCHEDULE] = {
929 		.packing = sja1105_schedule_entry_packing,
930 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry),
931 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY,
932 		.max_entry_count = SJA1105_MAX_SCHEDULE_COUNT,
933 	},
934 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {
935 		.packing = sja1105_schedule_entry_points_entry_packing,
936 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry_points_entry),
937 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_ENTRY,
938 		.max_entry_count = SJA1105_MAX_SCHEDULE_ENTRY_POINTS_COUNT,
939 	},
940 	[BLK_IDX_L2_LOOKUP] = {
941 		.packing = sja1105pqrs_l2_lookup_entry_packing,
942 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_entry),
943 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY,
944 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
945 	},
946 	[BLK_IDX_L2_POLICING] = {
947 		.packing = sja1105_l2_policing_entry_packing,
948 		.unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
949 		.packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
950 		.max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
951 	},
952 	[BLK_IDX_VLAN_LOOKUP] = {
953 		.packing = sja1105_vlan_lookup_entry_packing,
954 		.unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
955 		.packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
956 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
957 	},
958 	[BLK_IDX_L2_FORWARDING] = {
959 		.packing = sja1105_l2_forwarding_entry_packing,
960 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
961 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
962 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
963 	},
964 	[BLK_IDX_MAC_CONFIG] = {
965 		.packing = sja1105pqrs_mac_config_entry_packing,
966 		.unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
967 		.packed_entry_size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY,
968 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
969 	},
970 	[BLK_IDX_SCHEDULE_PARAMS] = {
971 		.packing = sja1105_schedule_params_entry_packing,
972 		.unpacked_entry_size = sizeof(struct sja1105_schedule_params_entry),
973 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_PARAMS_ENTRY,
974 		.max_entry_count = SJA1105_MAX_SCHEDULE_PARAMS_COUNT,
975 	},
976 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {
977 		.packing = sja1105_schedule_entry_points_params_entry_packing,
978 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry_points_params_entry),
979 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_PARAMS_ENTRY,
980 		.max_entry_count = SJA1105_MAX_SCHEDULE_ENTRY_POINTS_PARAMS_COUNT,
981 	},
982 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
983 		.packing = sja1105pqrs_l2_lookup_params_entry_packing,
984 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_params_entry),
985 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY,
986 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
987 	},
988 	[BLK_IDX_L2_FORWARDING_PARAMS] = {
989 		.packing = sja1105_l2_forwarding_params_entry_packing,
990 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
991 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
992 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
993 	},
994 	[BLK_IDX_AVB_PARAMS] = {
995 		.packing = sja1105pqrs_avb_params_entry_packing,
996 		.unpacked_entry_size = sizeof(struct sja1105_avb_params_entry),
997 		.packed_entry_size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY,
998 		.max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
999 	},
1000 	[BLK_IDX_GENERAL_PARAMS] = {
1001 		.packing = sja1105pqrs_general_params_entry_packing,
1002 		.unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
1003 		.packed_entry_size = SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY,
1004 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
1005 	},
1006 	[BLK_IDX_XMII_PARAMS] = {
1007 		.packing = sja1105_xmii_params_entry_packing,
1008 		.unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
1009 		.packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
1010 		.max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
1011 	},
1012 };
1013 
1014 /* SJA1105R: Second generation, no TTEthernet, SGMII */
1015 struct sja1105_table_ops sja1105r_table_ops[BLK_IDX_MAX] = {
1016 	[BLK_IDX_SCHEDULE] = {0},
1017 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {0},
1018 	[BLK_IDX_L2_LOOKUP] = {
1019 		.packing = sja1105pqrs_l2_lookup_entry_packing,
1020 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_entry),
1021 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY,
1022 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
1023 	},
1024 	[BLK_IDX_L2_POLICING] = {
1025 		.packing = sja1105_l2_policing_entry_packing,
1026 		.unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
1027 		.packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
1028 		.max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
1029 	},
1030 	[BLK_IDX_VLAN_LOOKUP] = {
1031 		.packing = sja1105_vlan_lookup_entry_packing,
1032 		.unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
1033 		.packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
1034 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
1035 	},
1036 	[BLK_IDX_L2_FORWARDING] = {
1037 		.packing = sja1105_l2_forwarding_entry_packing,
1038 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
1039 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
1040 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
1041 	},
1042 	[BLK_IDX_MAC_CONFIG] = {
1043 		.packing = sja1105pqrs_mac_config_entry_packing,
1044 		.unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
1045 		.packed_entry_size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY,
1046 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
1047 	},
1048 	[BLK_IDX_SCHEDULE_PARAMS] = {0},
1049 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {0},
1050 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
1051 		.packing = sja1105pqrs_l2_lookup_params_entry_packing,
1052 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_params_entry),
1053 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY,
1054 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
1055 	},
1056 	[BLK_IDX_L2_FORWARDING_PARAMS] = {
1057 		.packing = sja1105_l2_forwarding_params_entry_packing,
1058 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
1059 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
1060 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
1061 	},
1062 	[BLK_IDX_AVB_PARAMS] = {
1063 		.packing = sja1105pqrs_avb_params_entry_packing,
1064 		.unpacked_entry_size = sizeof(struct sja1105_avb_params_entry),
1065 		.packed_entry_size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY,
1066 		.max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
1067 	},
1068 	[BLK_IDX_GENERAL_PARAMS] = {
1069 		.packing = sja1105pqrs_general_params_entry_packing,
1070 		.unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
1071 		.packed_entry_size = SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY,
1072 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
1073 	},
1074 	[BLK_IDX_XMII_PARAMS] = {
1075 		.packing = sja1105_xmii_params_entry_packing,
1076 		.unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
1077 		.packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
1078 		.max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
1079 	},
1080 };
1081 
1082 /* SJA1105S: Second generation, TTEthernet, SGMII */
1083 struct sja1105_table_ops sja1105s_table_ops[BLK_IDX_MAX] = {
1084 	[BLK_IDX_SCHEDULE] = {
1085 		.packing = sja1105_schedule_entry_packing,
1086 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry),
1087 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY,
1088 		.max_entry_count = SJA1105_MAX_SCHEDULE_COUNT,
1089 	},
1090 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {
1091 		.packing = sja1105_schedule_entry_points_entry_packing,
1092 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry_points_entry),
1093 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_ENTRY,
1094 		.max_entry_count = SJA1105_MAX_SCHEDULE_ENTRY_POINTS_COUNT,
1095 	},
1096 	[BLK_IDX_L2_LOOKUP] = {
1097 		.packing = sja1105pqrs_l2_lookup_entry_packing,
1098 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_entry),
1099 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY,
1100 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
1101 	},
1102 	[BLK_IDX_L2_POLICING] = {
1103 		.packing = sja1105_l2_policing_entry_packing,
1104 		.unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
1105 		.packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
1106 		.max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
1107 	},
1108 	[BLK_IDX_VLAN_LOOKUP] = {
1109 		.packing = sja1105_vlan_lookup_entry_packing,
1110 		.unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
1111 		.packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
1112 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
1113 	},
1114 	[BLK_IDX_L2_FORWARDING] = {
1115 		.packing = sja1105_l2_forwarding_entry_packing,
1116 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
1117 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
1118 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
1119 	},
1120 	[BLK_IDX_MAC_CONFIG] = {
1121 		.packing = sja1105pqrs_mac_config_entry_packing,
1122 		.unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
1123 		.packed_entry_size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY,
1124 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
1125 	},
1126 	[BLK_IDX_SCHEDULE_PARAMS] = {
1127 		.packing = sja1105_schedule_params_entry_packing,
1128 		.unpacked_entry_size = sizeof(struct sja1105_schedule_params_entry),
1129 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_PARAMS_ENTRY,
1130 		.max_entry_count = SJA1105_MAX_SCHEDULE_PARAMS_COUNT,
1131 	},
1132 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {
1133 		.packing = sja1105_schedule_entry_points_params_entry_packing,
1134 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry_points_params_entry),
1135 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_PARAMS_ENTRY,
1136 		.max_entry_count = SJA1105_MAX_SCHEDULE_ENTRY_POINTS_PARAMS_COUNT,
1137 	},
1138 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
1139 		.packing = sja1105pqrs_l2_lookup_params_entry_packing,
1140 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_params_entry),
1141 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY,
1142 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
1143 	},
1144 	[BLK_IDX_L2_FORWARDING_PARAMS] = {
1145 		.packing = sja1105_l2_forwarding_params_entry_packing,
1146 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
1147 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
1148 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
1149 	},
1150 	[BLK_IDX_AVB_PARAMS] = {
1151 		.packing = sja1105pqrs_avb_params_entry_packing,
1152 		.unpacked_entry_size = sizeof(struct sja1105_avb_params_entry),
1153 		.packed_entry_size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY,
1154 		.max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
1155 	},
1156 	[BLK_IDX_GENERAL_PARAMS] = {
1157 		.packing = sja1105pqrs_general_params_entry_packing,
1158 		.unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
1159 		.packed_entry_size = SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY,
1160 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
1161 	},
1162 	[BLK_IDX_XMII_PARAMS] = {
1163 		.packing = sja1105_xmii_params_entry_packing,
1164 		.unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
1165 		.packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
1166 		.max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
1167 	},
1168 };
1169 
1170 int sja1105_static_config_init(struct sja1105_static_config *config,
1171 			       const struct sja1105_table_ops *static_ops,
1172 			       u64 device_id)
1173 {
1174 	enum sja1105_blk_idx i;
1175 
1176 	*config = (struct sja1105_static_config) {0};
1177 
1178 	/* Transfer static_ops array from priv into per-table ops
1179 	 * for handier access
1180 	 */
1181 	for (i = 0; i < BLK_IDX_MAX; i++)
1182 		config->tables[i].ops = &static_ops[i];
1183 
1184 	config->device_id = device_id;
1185 	return 0;
1186 }
1187 
1188 void sja1105_static_config_free(struct sja1105_static_config *config)
1189 {
1190 	enum sja1105_blk_idx i;
1191 
1192 	for (i = 0; i < BLK_IDX_MAX; i++) {
1193 		if (config->tables[i].entry_count) {
1194 			kfree(config->tables[i].entries);
1195 			config->tables[i].entry_count = 0;
1196 		}
1197 	}
1198 }
1199 
1200 int sja1105_table_delete_entry(struct sja1105_table *table, int i)
1201 {
1202 	size_t entry_size = table->ops->unpacked_entry_size;
1203 	u8 *entries = table->entries;
1204 
1205 	if (i > table->entry_count)
1206 		return -ERANGE;
1207 
1208 	memmove(entries + i * entry_size, entries + (i + 1) * entry_size,
1209 		(table->entry_count - i) * entry_size);
1210 
1211 	table->entry_count--;
1212 
1213 	return 0;
1214 }
1215 
1216 /* No pointers to table->entries should be kept when this is called. */
1217 int sja1105_table_resize(struct sja1105_table *table, size_t new_count)
1218 {
1219 	size_t entry_size = table->ops->unpacked_entry_size;
1220 	void *new_entries, *old_entries = table->entries;
1221 
1222 	if (new_count > table->ops->max_entry_count)
1223 		return -ERANGE;
1224 
1225 	new_entries = kcalloc(new_count, entry_size, GFP_KERNEL);
1226 	if (!new_entries)
1227 		return -ENOMEM;
1228 
1229 	memcpy(new_entries, old_entries, min(new_count, table->entry_count) *
1230 		entry_size);
1231 
1232 	table->entries = new_entries;
1233 	table->entry_count = new_count;
1234 	kfree(old_entries);
1235 	return 0;
1236 }
1237