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 size_t sja1105pqrs_general_params_entry_packing(void *buf, void *entry_ptr,
150 						enum packing_op op)
151 {
152 	const size_t size = SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY;
153 	struct sja1105_general_params_entry *entry = entry_ptr;
154 
155 	sja1105_packing(buf, &entry->vllupformat, 351, 351, size, op);
156 	sja1105_packing(buf, &entry->mirr_ptacu,  350, 350, size, op);
157 	sja1105_packing(buf, &entry->switchid,    349, 347, size, op);
158 	sja1105_packing(buf, &entry->hostprio,    346, 344, size, op);
159 	sja1105_packing(buf, &entry->mac_fltres1, 343, 296, size, op);
160 	sja1105_packing(buf, &entry->mac_fltres0, 295, 248, size, op);
161 	sja1105_packing(buf, &entry->mac_flt1,    247, 200, size, op);
162 	sja1105_packing(buf, &entry->mac_flt0,    199, 152, size, op);
163 	sja1105_packing(buf, &entry->incl_srcpt1, 151, 151, size, op);
164 	sja1105_packing(buf, &entry->incl_srcpt0, 150, 150, size, op);
165 	sja1105_packing(buf, &entry->send_meta1,  149, 149, size, op);
166 	sja1105_packing(buf, &entry->send_meta0,  148, 148, size, op);
167 	sja1105_packing(buf, &entry->casc_port,   147, 145, size, op);
168 	sja1105_packing(buf, &entry->host_port,   144, 142, size, op);
169 	sja1105_packing(buf, &entry->mirr_port,   141, 139, size, op);
170 	sja1105_packing(buf, &entry->vlmarker,    138, 107, size, op);
171 	sja1105_packing(buf, &entry->vlmask,      106,  75, size, op);
172 	sja1105_packing(buf, &entry->tpid2,        74,  59, size, op);
173 	sja1105_packing(buf, &entry->ignore2stf,   58,  58, size, op);
174 	sja1105_packing(buf, &entry->tpid,         57,  42, size, op);
175 	sja1105_packing(buf, &entry->queue_ts,     41,  41, size, op);
176 	sja1105_packing(buf, &entry->egrmirrvid,   40,  29, size, op);
177 	sja1105_packing(buf, &entry->egrmirrpcp,   28,  26, size, op);
178 	sja1105_packing(buf, &entry->egrmirrdei,   25,  25, size, op);
179 	sja1105_packing(buf, &entry->replay_port,  24,  22, size, op);
180 	return size;
181 }
182 
183 static size_t
184 sja1105_l2_forwarding_params_entry_packing(void *buf, void *entry_ptr,
185 					   enum packing_op op)
186 {
187 	const size_t size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY;
188 	struct sja1105_l2_forwarding_params_entry *entry = entry_ptr;
189 	int offset, i;
190 
191 	sja1105_packing(buf, &entry->max_dynp, 95, 93, size, op);
192 	for (i = 0, offset = 13; i < 8; i++, offset += 10)
193 		sja1105_packing(buf, &entry->part_spc[i],
194 				offset + 9, offset + 0, size, op);
195 	return size;
196 }
197 
198 size_t sja1105_l2_forwarding_entry_packing(void *buf, void *entry_ptr,
199 					   enum packing_op op)
200 {
201 	const size_t size = SJA1105_SIZE_L2_FORWARDING_ENTRY;
202 	struct sja1105_l2_forwarding_entry *entry = entry_ptr;
203 	int offset, i;
204 
205 	sja1105_packing(buf, &entry->bc_domain,  63, 59, size, op);
206 	sja1105_packing(buf, &entry->reach_port, 58, 54, size, op);
207 	sja1105_packing(buf, &entry->fl_domain,  53, 49, size, op);
208 	for (i = 0, offset = 25; i < 8; i++, offset += 3)
209 		sja1105_packing(buf, &entry->vlan_pmap[i],
210 				offset + 2, offset + 0, size, op);
211 	return size;
212 }
213 
214 static size_t
215 sja1105et_l2_lookup_params_entry_packing(void *buf, void *entry_ptr,
216 					 enum packing_op op)
217 {
218 	const size_t size = SJA1105ET_SIZE_L2_LOOKUP_PARAMS_ENTRY;
219 	struct sja1105_l2_lookup_params_entry *entry = entry_ptr;
220 
221 	sja1105_packing(buf, &entry->maxage,         31, 17, size, op);
222 	sja1105_packing(buf, &entry->dyn_tbsz,       16, 14, size, op);
223 	sja1105_packing(buf, &entry->poly,           13,  6, size, op);
224 	sja1105_packing(buf, &entry->shared_learn,    5,  5, size, op);
225 	sja1105_packing(buf, &entry->no_enf_hostprt,  4,  4, size, op);
226 	sja1105_packing(buf, &entry->no_mgmt_learn,   3,  3, size, op);
227 	return size;
228 }
229 
230 size_t sja1105pqrs_l2_lookup_params_entry_packing(void *buf, void *entry_ptr,
231 						  enum packing_op op)
232 {
233 	const size_t size = SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY;
234 	struct sja1105_l2_lookup_params_entry *entry = entry_ptr;
235 	int offset, i;
236 
237 	for (i = 0, offset = 58; i < 5; i++, offset += 11)
238 		sja1105_packing(buf, &entry->maxaddrp[i],
239 				offset + 10, offset + 0, size, op);
240 	sja1105_packing(buf, &entry->maxage,         57,  43, size, op);
241 	sja1105_packing(buf, &entry->start_dynspc,   42,  33, size, op);
242 	sja1105_packing(buf, &entry->drpnolearn,     32,  28, size, op);
243 	sja1105_packing(buf, &entry->shared_learn,   27,  27, size, op);
244 	sja1105_packing(buf, &entry->no_enf_hostprt, 26,  26, size, op);
245 	sja1105_packing(buf, &entry->no_mgmt_learn,  25,  25, size, op);
246 	sja1105_packing(buf, &entry->use_static,     24,  24, size, op);
247 	sja1105_packing(buf, &entry->owr_dyn,        23,  23, size, op);
248 	sja1105_packing(buf, &entry->learn_once,     22,  22, size, op);
249 	return size;
250 }
251 
252 size_t sja1105et_l2_lookup_entry_packing(void *buf, void *entry_ptr,
253 					 enum packing_op op)
254 {
255 	const size_t size = SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
256 	struct sja1105_l2_lookup_entry *entry = entry_ptr;
257 
258 	sja1105_packing(buf, &entry->vlanid,    95, 84, size, op);
259 	sja1105_packing(buf, &entry->macaddr,   83, 36, size, op);
260 	sja1105_packing(buf, &entry->destports, 35, 31, size, op);
261 	sja1105_packing(buf, &entry->enfport,   30, 30, size, op);
262 	sja1105_packing(buf, &entry->index,     29, 20, size, op);
263 	return size;
264 }
265 
266 size_t sja1105pqrs_l2_lookup_entry_packing(void *buf, void *entry_ptr,
267 					   enum packing_op op)
268 {
269 	const size_t size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
270 	struct sja1105_l2_lookup_entry *entry = entry_ptr;
271 
272 	if (entry->lockeds) {
273 		sja1105_packing(buf, &entry->tsreg,    159, 159, size, op);
274 		sja1105_packing(buf, &entry->mirrvlan, 158, 147, size, op);
275 		sja1105_packing(buf, &entry->takets,   146, 146, size, op);
276 		sja1105_packing(buf, &entry->mirr,     145, 145, size, op);
277 		sja1105_packing(buf, &entry->retag,    144, 144, size, op);
278 	} else {
279 		sja1105_packing(buf, &entry->touched,  159, 159, size, op);
280 		sja1105_packing(buf, &entry->age,      158, 144, size, op);
281 	}
282 	sja1105_packing(buf, &entry->mask_iotag,   143, 143, size, op);
283 	sja1105_packing(buf, &entry->mask_vlanid,  142, 131, size, op);
284 	sja1105_packing(buf, &entry->mask_macaddr, 130,  83, size, op);
285 	sja1105_packing(buf, &entry->iotag,         82,  82, size, op);
286 	sja1105_packing(buf, &entry->vlanid,        81,  70, size, op);
287 	sja1105_packing(buf, &entry->macaddr,       69,  22, size, op);
288 	sja1105_packing(buf, &entry->destports,     21,  17, size, op);
289 	sja1105_packing(buf, &entry->enfport,       16,  16, size, op);
290 	sja1105_packing(buf, &entry->index,         15,   6, size, op);
291 	return size;
292 }
293 
294 static size_t sja1105_l2_policing_entry_packing(void *buf, void *entry_ptr,
295 						enum packing_op op)
296 {
297 	const size_t size = SJA1105_SIZE_L2_POLICING_ENTRY;
298 	struct sja1105_l2_policing_entry *entry = entry_ptr;
299 
300 	sja1105_packing(buf, &entry->sharindx,  63, 58, size, op);
301 	sja1105_packing(buf, &entry->smax,      57, 42, size, op);
302 	sja1105_packing(buf, &entry->rate,      41, 26, size, op);
303 	sja1105_packing(buf, &entry->maxlen,    25, 15, size, op);
304 	sja1105_packing(buf, &entry->partition, 14, 12, size, op);
305 	return size;
306 }
307 
308 static size_t sja1105et_mac_config_entry_packing(void *buf, void *entry_ptr,
309 						 enum packing_op op)
310 {
311 	const size_t size = SJA1105ET_SIZE_MAC_CONFIG_ENTRY;
312 	struct sja1105_mac_config_entry *entry = entry_ptr;
313 	int offset, i;
314 
315 	for (i = 0, offset = 72; i < 8; i++, offset += 19) {
316 		sja1105_packing(buf, &entry->enabled[i],
317 				offset +  0, offset +  0, size, op);
318 		sja1105_packing(buf, &entry->base[i],
319 				offset +  9, offset +  1, size, op);
320 		sja1105_packing(buf, &entry->top[i],
321 				offset + 18, offset + 10, size, op);
322 	}
323 	sja1105_packing(buf, &entry->ifg,       71, 67, size, op);
324 	sja1105_packing(buf, &entry->speed,     66, 65, size, op);
325 	sja1105_packing(buf, &entry->tp_delin,  64, 49, size, op);
326 	sja1105_packing(buf, &entry->tp_delout, 48, 33, size, op);
327 	sja1105_packing(buf, &entry->maxage,    32, 25, size, op);
328 	sja1105_packing(buf, &entry->vlanprio,  24, 22, size, op);
329 	sja1105_packing(buf, &entry->vlanid,    21, 10, size, op);
330 	sja1105_packing(buf, &entry->ing_mirr,   9,  9, size, op);
331 	sja1105_packing(buf, &entry->egr_mirr,   8,  8, size, op);
332 	sja1105_packing(buf, &entry->drpnona664, 7,  7, size, op);
333 	sja1105_packing(buf, &entry->drpdtag,    6,  6, size, op);
334 	sja1105_packing(buf, &entry->drpuntag,   5,  5, size, op);
335 	sja1105_packing(buf, &entry->retag,      4,  4, size, op);
336 	sja1105_packing(buf, &entry->dyn_learn,  3,  3, size, op);
337 	sja1105_packing(buf, &entry->egress,     2,  2, size, op);
338 	sja1105_packing(buf, &entry->ingress,    1,  1, size, op);
339 	return size;
340 }
341 
342 size_t sja1105pqrs_mac_config_entry_packing(void *buf, void *entry_ptr,
343 					    enum packing_op op)
344 {
345 	const size_t size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY;
346 	struct sja1105_mac_config_entry *entry = entry_ptr;
347 	int offset, i;
348 
349 	for (i = 0, offset = 104; i < 8; i++, offset += 19) {
350 		sja1105_packing(buf, &entry->enabled[i],
351 				offset +  0, offset +  0, size, op);
352 		sja1105_packing(buf, &entry->base[i],
353 				offset +  9, offset +  1, size, op);
354 		sja1105_packing(buf, &entry->top[i],
355 				offset + 18, offset + 10, size, op);
356 	}
357 	sja1105_packing(buf, &entry->ifg,       103, 99, size, op);
358 	sja1105_packing(buf, &entry->speed,      98, 97, size, op);
359 	sja1105_packing(buf, &entry->tp_delin,   96, 81, size, op);
360 	sja1105_packing(buf, &entry->tp_delout,  80, 65, size, op);
361 	sja1105_packing(buf, &entry->maxage,     64, 57, size, op);
362 	sja1105_packing(buf, &entry->vlanprio,   56, 54, size, op);
363 	sja1105_packing(buf, &entry->vlanid,     53, 42, size, op);
364 	sja1105_packing(buf, &entry->ing_mirr,   41, 41, size, op);
365 	sja1105_packing(buf, &entry->egr_mirr,   40, 40, size, op);
366 	sja1105_packing(buf, &entry->drpnona664, 39, 39, size, op);
367 	sja1105_packing(buf, &entry->drpdtag,    38, 38, size, op);
368 	sja1105_packing(buf, &entry->drpuntag,   35, 35, size, op);
369 	sja1105_packing(buf, &entry->retag,      34, 34, size, op);
370 	sja1105_packing(buf, &entry->dyn_learn,  33, 33, size, op);
371 	sja1105_packing(buf, &entry->egress,     32, 32, size, op);
372 	sja1105_packing(buf, &entry->ingress,    31, 31, size, op);
373 	return size;
374 }
375 
376 static size_t
377 sja1105_schedule_entry_points_params_entry_packing(void *buf, void *entry_ptr,
378 						   enum packing_op op)
379 {
380 	struct sja1105_schedule_entry_points_params_entry *entry = entry_ptr;
381 	const size_t size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_PARAMS_ENTRY;
382 
383 	sja1105_packing(buf, &entry->clksrc,    31, 30, size, op);
384 	sja1105_packing(buf, &entry->actsubsch, 29, 27, size, op);
385 	return size;
386 }
387 
388 static size_t
389 sja1105_schedule_entry_points_entry_packing(void *buf, void *entry_ptr,
390 					    enum packing_op op)
391 {
392 	struct sja1105_schedule_entry_points_entry *entry = entry_ptr;
393 	const size_t size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_ENTRY;
394 
395 	sja1105_packing(buf, &entry->subschindx, 31, 29, size, op);
396 	sja1105_packing(buf, &entry->delta,      28, 11, size, op);
397 	sja1105_packing(buf, &entry->address,    10, 1,  size, op);
398 	return size;
399 }
400 
401 static size_t sja1105_schedule_params_entry_packing(void *buf, void *entry_ptr,
402 						    enum packing_op op)
403 {
404 	const size_t size = SJA1105_SIZE_SCHEDULE_PARAMS_ENTRY;
405 	struct sja1105_schedule_params_entry *entry = entry_ptr;
406 	int offset, i;
407 
408 	for (i = 0, offset = 16; i < 8; i++, offset += 10)
409 		sja1105_packing(buf, &entry->subscheind[i],
410 				offset + 9, offset + 0, size, op);
411 	return size;
412 }
413 
414 static size_t sja1105_schedule_entry_packing(void *buf, void *entry_ptr,
415 					     enum packing_op op)
416 {
417 	const size_t size = SJA1105_SIZE_SCHEDULE_ENTRY;
418 	struct sja1105_schedule_entry *entry = entry_ptr;
419 
420 	sja1105_packing(buf, &entry->winstindex,  63, 54, size, op);
421 	sja1105_packing(buf, &entry->winend,      53, 53, size, op);
422 	sja1105_packing(buf, &entry->winst,       52, 52, size, op);
423 	sja1105_packing(buf, &entry->destports,   51, 47, size, op);
424 	sja1105_packing(buf, &entry->setvalid,    46, 46, size, op);
425 	sja1105_packing(buf, &entry->txen,        45, 45, size, op);
426 	sja1105_packing(buf, &entry->resmedia_en, 44, 44, size, op);
427 	sja1105_packing(buf, &entry->resmedia,    43, 36, size, op);
428 	sja1105_packing(buf, &entry->vlindex,     35, 26, size, op);
429 	sja1105_packing(buf, &entry->delta,       25, 8,  size, op);
430 	return size;
431 }
432 
433 static size_t
434 sja1105_vl_forwarding_params_entry_packing(void *buf, void *entry_ptr,
435 					   enum packing_op op)
436 {
437 	struct sja1105_vl_forwarding_params_entry *entry = entry_ptr;
438 	const size_t size = SJA1105_SIZE_VL_FORWARDING_PARAMS_ENTRY;
439 	int offset, i;
440 
441 	for (i = 0, offset = 16; i < 8; i++, offset += 10)
442 		sja1105_packing(buf, &entry->partspc[i],
443 				offset + 9, offset + 0, size, op);
444 	sja1105_packing(buf, &entry->debugen, 15, 15, size, op);
445 	return size;
446 }
447 
448 static size_t sja1105_vl_forwarding_entry_packing(void *buf, void *entry_ptr,
449 						  enum packing_op op)
450 {
451 	struct sja1105_vl_forwarding_entry *entry = entry_ptr;
452 	const size_t size = SJA1105_SIZE_VL_FORWARDING_ENTRY;
453 
454 	sja1105_packing(buf, &entry->type,      31, 31, size, op);
455 	sja1105_packing(buf, &entry->priority,  30, 28, size, op);
456 	sja1105_packing(buf, &entry->partition, 27, 25, size, op);
457 	sja1105_packing(buf, &entry->destports, 24, 20, size, op);
458 	return size;
459 }
460 
461 size_t sja1105_vl_lookup_entry_packing(void *buf, void *entry_ptr,
462 				       enum packing_op op)
463 {
464 	struct sja1105_vl_lookup_entry *entry = entry_ptr;
465 	const size_t size = SJA1105_SIZE_VL_LOOKUP_ENTRY;
466 
467 	if (entry->format == SJA1105_VL_FORMAT_PSFP) {
468 		/* Interpreting vllupformat as 0 */
469 		sja1105_packing(buf, &entry->destports,
470 				95, 91, size, op);
471 		sja1105_packing(buf, &entry->iscritical,
472 				90, 90, size, op);
473 		sja1105_packing(buf, &entry->macaddr,
474 				89, 42, size, op);
475 		sja1105_packing(buf, &entry->vlanid,
476 				41, 30, size, op);
477 		sja1105_packing(buf, &entry->port,
478 				29, 27, size, op);
479 		sja1105_packing(buf, &entry->vlanprior,
480 				26, 24, size, op);
481 	} else {
482 		/* Interpreting vllupformat as 1 */
483 		sja1105_packing(buf, &entry->egrmirr,
484 				95, 91, size, op);
485 		sja1105_packing(buf, &entry->ingrmirr,
486 				90, 90, size, op);
487 		sja1105_packing(buf, &entry->vlid,
488 				57, 42, size, op);
489 		sja1105_packing(buf, &entry->port,
490 				29, 27, size, op);
491 	}
492 	return size;
493 }
494 
495 static size_t sja1105_vl_policing_entry_packing(void *buf, void *entry_ptr,
496 						enum packing_op op)
497 {
498 	struct sja1105_vl_policing_entry *entry = entry_ptr;
499 	const size_t size = SJA1105_SIZE_VL_POLICING_ENTRY;
500 
501 	sja1105_packing(buf, &entry->type,      63, 63, size, op);
502 	sja1105_packing(buf, &entry->maxlen,    62, 52, size, op);
503 	sja1105_packing(buf, &entry->sharindx,  51, 42, size, op);
504 	if (entry->type == 0) {
505 		sja1105_packing(buf, &entry->bag,    41, 28, size, op);
506 		sja1105_packing(buf, &entry->jitter, 27, 18, size, op);
507 	}
508 	return size;
509 }
510 
511 size_t sja1105_vlan_lookup_entry_packing(void *buf, void *entry_ptr,
512 					 enum packing_op op)
513 {
514 	const size_t size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY;
515 	struct sja1105_vlan_lookup_entry *entry = entry_ptr;
516 
517 	sja1105_packing(buf, &entry->ving_mirr,  63, 59, size, op);
518 	sja1105_packing(buf, &entry->vegr_mirr,  58, 54, size, op);
519 	sja1105_packing(buf, &entry->vmemb_port, 53, 49, size, op);
520 	sja1105_packing(buf, &entry->vlan_bc,    48, 44, size, op);
521 	sja1105_packing(buf, &entry->tag_port,   43, 39, size, op);
522 	sja1105_packing(buf, &entry->vlanid,     38, 27, size, op);
523 	return size;
524 }
525 
526 static size_t sja1105_xmii_params_entry_packing(void *buf, void *entry_ptr,
527 						enum packing_op op)
528 {
529 	const size_t size = SJA1105_SIZE_XMII_PARAMS_ENTRY;
530 	struct sja1105_xmii_params_entry *entry = entry_ptr;
531 	int offset, i;
532 
533 	for (i = 0, offset = 17; i < 5; i++, offset += 3) {
534 		sja1105_packing(buf, &entry->xmii_mode[i],
535 				offset + 1, offset + 0, size, op);
536 		sja1105_packing(buf, &entry->phy_mac[i],
537 				offset + 2, offset + 2, size, op);
538 	}
539 	return size;
540 }
541 
542 size_t sja1105_retagging_entry_packing(void *buf, void *entry_ptr,
543 				       enum packing_op op)
544 {
545 	struct sja1105_retagging_entry *entry = entry_ptr;
546 	const size_t size = SJA1105_SIZE_RETAGGING_ENTRY;
547 
548 	sja1105_packing(buf, &entry->egr_port,       63, 59, size, op);
549 	sja1105_packing(buf, &entry->ing_port,       58, 54, size, op);
550 	sja1105_packing(buf, &entry->vlan_ing,       53, 42, size, op);
551 	sja1105_packing(buf, &entry->vlan_egr,       41, 30, size, op);
552 	sja1105_packing(buf, &entry->do_not_learn,   29, 29, size, op);
553 	sja1105_packing(buf, &entry->use_dest_ports, 28, 28, size, op);
554 	sja1105_packing(buf, &entry->destports,      27, 23, size, op);
555 	return size;
556 }
557 
558 size_t sja1105_table_header_packing(void *buf, void *entry_ptr,
559 				    enum packing_op op)
560 {
561 	const size_t size = SJA1105_SIZE_TABLE_HEADER;
562 	struct sja1105_table_header *entry = entry_ptr;
563 
564 	sja1105_packing(buf, &entry->block_id, 31, 24, size, op);
565 	sja1105_packing(buf, &entry->len,      55, 32, size, op);
566 	sja1105_packing(buf, &entry->crc,      95, 64, size, op);
567 	return size;
568 }
569 
570 /* WARNING: the *hdr pointer is really non-const, because it is
571  * modifying the CRC of the header for a 2-stage packing operation
572  */
573 void
574 sja1105_table_header_pack_with_crc(void *buf, struct sja1105_table_header *hdr)
575 {
576 	/* First pack the table as-is, then calculate the CRC, and
577 	 * finally put the proper CRC into the packed buffer
578 	 */
579 	memset(buf, 0, SJA1105_SIZE_TABLE_HEADER);
580 	sja1105_table_header_packing(buf, hdr, PACK);
581 	hdr->crc = sja1105_crc32(buf, SJA1105_SIZE_TABLE_HEADER - 4);
582 	sja1105_pack(buf + SJA1105_SIZE_TABLE_HEADER - 4, &hdr->crc, 31, 0, 4);
583 }
584 
585 static void sja1105_table_write_crc(u8 *table_start, u8 *crc_ptr)
586 {
587 	u64 computed_crc;
588 	int len_bytes;
589 
590 	len_bytes = (uintptr_t)(crc_ptr - table_start);
591 	computed_crc = sja1105_crc32(table_start, len_bytes);
592 	sja1105_pack(crc_ptr, &computed_crc, 31, 0, 4);
593 }
594 
595 /* The block IDs that the switches support are unfortunately sparse, so keep a
596  * mapping table to "block indices" and translate back and forth so that we
597  * don't waste useless memory in struct sja1105_static_config.
598  * Also, since the block id comes from essentially untrusted input (unpacking
599  * the static config from userspace) it has to be sanitized (range-checked)
600  * before blindly indexing kernel memory with the blk_idx.
601  */
602 static u64 blk_id_map[BLK_IDX_MAX] = {
603 	[BLK_IDX_SCHEDULE] = BLKID_SCHEDULE,
604 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = BLKID_SCHEDULE_ENTRY_POINTS,
605 	[BLK_IDX_VL_LOOKUP] = BLKID_VL_LOOKUP,
606 	[BLK_IDX_VL_POLICING] = BLKID_VL_POLICING,
607 	[BLK_IDX_VL_FORWARDING] = BLKID_VL_FORWARDING,
608 	[BLK_IDX_L2_LOOKUP] = BLKID_L2_LOOKUP,
609 	[BLK_IDX_L2_POLICING] = BLKID_L2_POLICING,
610 	[BLK_IDX_VLAN_LOOKUP] = BLKID_VLAN_LOOKUP,
611 	[BLK_IDX_L2_FORWARDING] = BLKID_L2_FORWARDING,
612 	[BLK_IDX_MAC_CONFIG] = BLKID_MAC_CONFIG,
613 	[BLK_IDX_SCHEDULE_PARAMS] = BLKID_SCHEDULE_PARAMS,
614 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = BLKID_SCHEDULE_ENTRY_POINTS_PARAMS,
615 	[BLK_IDX_VL_FORWARDING_PARAMS] = BLKID_VL_FORWARDING_PARAMS,
616 	[BLK_IDX_L2_LOOKUP_PARAMS] = BLKID_L2_LOOKUP_PARAMS,
617 	[BLK_IDX_L2_FORWARDING_PARAMS] = BLKID_L2_FORWARDING_PARAMS,
618 	[BLK_IDX_AVB_PARAMS] = BLKID_AVB_PARAMS,
619 	[BLK_IDX_GENERAL_PARAMS] = BLKID_GENERAL_PARAMS,
620 	[BLK_IDX_RETAGGING] = BLKID_RETAGGING,
621 	[BLK_IDX_XMII_PARAMS] = BLKID_XMII_PARAMS,
622 };
623 
624 const char *sja1105_static_config_error_msg[] = {
625 	[SJA1105_CONFIG_OK] = "",
626 	[SJA1105_TTETHERNET_NOT_SUPPORTED] =
627 		"schedule-table present, but TTEthernet is "
628 		"only supported on T and Q/S",
629 	[SJA1105_INCORRECT_TTETHERNET_CONFIGURATION] =
630 		"schedule-table present, but one of "
631 		"schedule-entry-points-table, schedule-parameters-table or "
632 		"schedule-entry-points-parameters table is empty",
633 	[SJA1105_INCORRECT_VIRTUAL_LINK_CONFIGURATION] =
634 		"vl-lookup-table present, but one of vl-policing-table, "
635 		"vl-forwarding-table or vl-forwarding-parameters-table is empty",
636 	[SJA1105_MISSING_L2_POLICING_TABLE] =
637 		"l2-policing-table needs to have at least one entry",
638 	[SJA1105_MISSING_L2_FORWARDING_TABLE] =
639 		"l2-forwarding-table is either missing or incomplete",
640 	[SJA1105_MISSING_L2_FORWARDING_PARAMS_TABLE] =
641 		"l2-forwarding-parameters-table is missing",
642 	[SJA1105_MISSING_GENERAL_PARAMS_TABLE] =
643 		"general-parameters-table is missing",
644 	[SJA1105_MISSING_VLAN_TABLE] =
645 		"vlan-lookup-table needs to have at least the default untagged VLAN",
646 	[SJA1105_MISSING_XMII_TABLE] =
647 		"xmii-table is missing",
648 	[SJA1105_MISSING_MAC_TABLE] =
649 		"mac-configuration-table needs to contain an entry for each port",
650 	[SJA1105_OVERCOMMITTED_FRAME_MEMORY] =
651 		"Not allowed to overcommit frame memory. L2 memory partitions "
652 		"and VL memory partitions share the same space. The sum of all "
653 		"16 memory partitions is not allowed to be larger than 929 "
654 		"128-byte blocks (or 910 with retagging). Please adjust "
655 		"l2-forwarding-parameters-table.part_spc and/or "
656 		"vl-forwarding-parameters-table.partspc.",
657 };
658 
659 static sja1105_config_valid_t
660 static_config_check_memory_size(const struct sja1105_table *tables)
661 {
662 	const struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
663 	const struct sja1105_vl_forwarding_params_entry *vl_fwd_params;
664 	int i, max_mem, mem = 0;
665 
666 	l2_fwd_params = tables[BLK_IDX_L2_FORWARDING_PARAMS].entries;
667 
668 	for (i = 0; i < 8; i++)
669 		mem += l2_fwd_params->part_spc[i];
670 
671 	if (tables[BLK_IDX_VL_FORWARDING_PARAMS].entry_count) {
672 		vl_fwd_params = tables[BLK_IDX_VL_FORWARDING_PARAMS].entries;
673 		for (i = 0; i < 8; i++)
674 			mem += vl_fwd_params->partspc[i];
675 	}
676 
677 	if (tables[BLK_IDX_RETAGGING].entry_count)
678 		max_mem = SJA1105_MAX_FRAME_MEMORY_RETAGGING;
679 	else
680 		max_mem = SJA1105_MAX_FRAME_MEMORY;
681 
682 	if (mem > max_mem)
683 		return SJA1105_OVERCOMMITTED_FRAME_MEMORY;
684 
685 	return SJA1105_CONFIG_OK;
686 }
687 
688 sja1105_config_valid_t
689 sja1105_static_config_check_valid(const struct sja1105_static_config *config)
690 {
691 	const struct sja1105_table *tables = config->tables;
692 #define IS_FULL(blk_idx) \
693 	(tables[blk_idx].entry_count == tables[blk_idx].ops->max_entry_count)
694 
695 	if (tables[BLK_IDX_SCHEDULE].entry_count) {
696 		if (config->device_id != SJA1105T_DEVICE_ID &&
697 		    config->device_id != SJA1105QS_DEVICE_ID)
698 			return SJA1105_TTETHERNET_NOT_SUPPORTED;
699 
700 		if (tables[BLK_IDX_SCHEDULE_ENTRY_POINTS].entry_count == 0)
701 			return SJA1105_INCORRECT_TTETHERNET_CONFIGURATION;
702 
703 		if (!IS_FULL(BLK_IDX_SCHEDULE_PARAMS))
704 			return SJA1105_INCORRECT_TTETHERNET_CONFIGURATION;
705 
706 		if (!IS_FULL(BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS))
707 			return SJA1105_INCORRECT_TTETHERNET_CONFIGURATION;
708 	}
709 	if (tables[BLK_IDX_VL_LOOKUP].entry_count) {
710 		struct sja1105_vl_lookup_entry *vl_lookup;
711 		bool has_critical_links = false;
712 		int i;
713 
714 		vl_lookup = tables[BLK_IDX_VL_LOOKUP].entries;
715 
716 		for (i = 0; i < tables[BLK_IDX_VL_LOOKUP].entry_count; i++) {
717 			if (vl_lookup[i].iscritical) {
718 				has_critical_links = true;
719 				break;
720 			}
721 		}
722 
723 		if (tables[BLK_IDX_VL_POLICING].entry_count == 0 &&
724 		    has_critical_links)
725 			return SJA1105_INCORRECT_VIRTUAL_LINK_CONFIGURATION;
726 
727 		if (tables[BLK_IDX_VL_FORWARDING].entry_count == 0 &&
728 		    has_critical_links)
729 			return SJA1105_INCORRECT_VIRTUAL_LINK_CONFIGURATION;
730 
731 		if (tables[BLK_IDX_VL_FORWARDING_PARAMS].entry_count == 0 &&
732 		    has_critical_links)
733 			return SJA1105_INCORRECT_VIRTUAL_LINK_CONFIGURATION;
734 	}
735 
736 	if (tables[BLK_IDX_L2_POLICING].entry_count == 0)
737 		return SJA1105_MISSING_L2_POLICING_TABLE;
738 
739 	if (tables[BLK_IDX_VLAN_LOOKUP].entry_count == 0)
740 		return SJA1105_MISSING_VLAN_TABLE;
741 
742 	if (!IS_FULL(BLK_IDX_L2_FORWARDING))
743 		return SJA1105_MISSING_L2_FORWARDING_TABLE;
744 
745 	if (!IS_FULL(BLK_IDX_MAC_CONFIG))
746 		return SJA1105_MISSING_MAC_TABLE;
747 
748 	if (!IS_FULL(BLK_IDX_L2_FORWARDING_PARAMS))
749 		return SJA1105_MISSING_L2_FORWARDING_PARAMS_TABLE;
750 
751 	if (!IS_FULL(BLK_IDX_GENERAL_PARAMS))
752 		return SJA1105_MISSING_GENERAL_PARAMS_TABLE;
753 
754 	if (!IS_FULL(BLK_IDX_XMII_PARAMS))
755 		return SJA1105_MISSING_XMII_TABLE;
756 
757 	return static_config_check_memory_size(tables);
758 #undef IS_FULL
759 }
760 
761 void
762 sja1105_static_config_pack(void *buf, struct sja1105_static_config *config)
763 {
764 	struct sja1105_table_header header = {0};
765 	enum sja1105_blk_idx i;
766 	char *p = buf;
767 	int j;
768 
769 	sja1105_pack(p, &config->device_id, 31, 0, 4);
770 	p += SJA1105_SIZE_DEVICE_ID;
771 
772 	for (i = 0; i < BLK_IDX_MAX; i++) {
773 		const struct sja1105_table *table;
774 		char *table_start;
775 
776 		table = &config->tables[i];
777 		if (!table->entry_count)
778 			continue;
779 
780 		header.block_id = blk_id_map[i];
781 		header.len = table->entry_count *
782 			     table->ops->packed_entry_size / 4;
783 		sja1105_table_header_pack_with_crc(p, &header);
784 		p += SJA1105_SIZE_TABLE_HEADER;
785 		table_start = p;
786 		for (j = 0; j < table->entry_count; j++) {
787 			u8 *entry_ptr = table->entries;
788 
789 			entry_ptr += j * table->ops->unpacked_entry_size;
790 			memset(p, 0, table->ops->packed_entry_size);
791 			table->ops->packing(p, entry_ptr, PACK);
792 			p += table->ops->packed_entry_size;
793 		}
794 		sja1105_table_write_crc(table_start, p);
795 		p += 4;
796 	}
797 	/* Final header:
798 	 * Block ID does not matter
799 	 * Length of 0 marks that header is final
800 	 * CRC will be replaced on-the-fly on "config upload"
801 	 */
802 	header.block_id = 0;
803 	header.len = 0;
804 	header.crc = 0xDEADBEEF;
805 	memset(p, 0, SJA1105_SIZE_TABLE_HEADER);
806 	sja1105_table_header_packing(p, &header, PACK);
807 }
808 
809 size_t
810 sja1105_static_config_get_length(const struct sja1105_static_config *config)
811 {
812 	unsigned int sum;
813 	unsigned int header_count;
814 	enum sja1105_blk_idx i;
815 
816 	/* Ending header */
817 	header_count = 1;
818 	sum = SJA1105_SIZE_DEVICE_ID;
819 
820 	/* Tables (headers and entries) */
821 	for (i = 0; i < BLK_IDX_MAX; i++) {
822 		const struct sja1105_table *table;
823 
824 		table = &config->tables[i];
825 		if (table->entry_count)
826 			header_count++;
827 
828 		sum += table->ops->packed_entry_size * table->entry_count;
829 	}
830 	/* Headers have an additional CRC at the end */
831 	sum += header_count * (SJA1105_SIZE_TABLE_HEADER + 4);
832 	/* Last header does not have an extra CRC because there is no data */
833 	sum -= 4;
834 
835 	return sum;
836 }
837 
838 /* Compatibility matrices */
839 
840 /* SJA1105E: First generation, no TTEthernet */
841 struct sja1105_table_ops sja1105e_table_ops[BLK_IDX_MAX] = {
842 	[BLK_IDX_SCHEDULE] = {0},
843 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {0},
844 	[BLK_IDX_VL_LOOKUP] = {0},
845 	[BLK_IDX_VL_POLICING] = {0},
846 	[BLK_IDX_VL_FORWARDING] = {0},
847 	[BLK_IDX_L2_LOOKUP] = {
848 		.packing = sja1105et_l2_lookup_entry_packing,
849 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_entry),
850 		.packed_entry_size = SJA1105ET_SIZE_L2_LOOKUP_ENTRY,
851 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
852 	},
853 	[BLK_IDX_L2_POLICING] = {
854 		.packing = sja1105_l2_policing_entry_packing,
855 		.unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
856 		.packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
857 		.max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
858 	},
859 	[BLK_IDX_VLAN_LOOKUP] = {
860 		.packing = sja1105_vlan_lookup_entry_packing,
861 		.unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
862 		.packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
863 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
864 	},
865 	[BLK_IDX_L2_FORWARDING] = {
866 		.packing = sja1105_l2_forwarding_entry_packing,
867 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
868 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
869 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
870 	},
871 	[BLK_IDX_MAC_CONFIG] = {
872 		.packing = sja1105et_mac_config_entry_packing,
873 		.unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
874 		.packed_entry_size = SJA1105ET_SIZE_MAC_CONFIG_ENTRY,
875 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
876 	},
877 	[BLK_IDX_SCHEDULE_PARAMS] = {0},
878 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {0},
879 	[BLK_IDX_VL_FORWARDING_PARAMS] = {0},
880 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
881 		.packing = sja1105et_l2_lookup_params_entry_packing,
882 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_params_entry),
883 		.packed_entry_size = SJA1105ET_SIZE_L2_LOOKUP_PARAMS_ENTRY,
884 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
885 	},
886 	[BLK_IDX_L2_FORWARDING_PARAMS] = {
887 		.packing = sja1105_l2_forwarding_params_entry_packing,
888 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
889 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
890 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
891 	},
892 	[BLK_IDX_AVB_PARAMS] = {
893 		.packing = sja1105et_avb_params_entry_packing,
894 		.unpacked_entry_size = sizeof(struct sja1105_avb_params_entry),
895 		.packed_entry_size = SJA1105ET_SIZE_AVB_PARAMS_ENTRY,
896 		.max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
897 	},
898 	[BLK_IDX_GENERAL_PARAMS] = {
899 		.packing = sja1105et_general_params_entry_packing,
900 		.unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
901 		.packed_entry_size = SJA1105ET_SIZE_GENERAL_PARAMS_ENTRY,
902 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
903 	},
904 	[BLK_IDX_RETAGGING] = {
905 		.packing = sja1105_retagging_entry_packing,
906 		.unpacked_entry_size = sizeof(struct sja1105_retagging_entry),
907 		.packed_entry_size = SJA1105_SIZE_RETAGGING_ENTRY,
908 		.max_entry_count = SJA1105_MAX_RETAGGING_COUNT,
909 	},
910 	[BLK_IDX_XMII_PARAMS] = {
911 		.packing = sja1105_xmii_params_entry_packing,
912 		.unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
913 		.packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
914 		.max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
915 	},
916 };
917 
918 /* SJA1105T: First generation, TTEthernet */
919 struct sja1105_table_ops sja1105t_table_ops[BLK_IDX_MAX] = {
920 	[BLK_IDX_SCHEDULE] = {
921 		.packing = sja1105_schedule_entry_packing,
922 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry),
923 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY,
924 		.max_entry_count = SJA1105_MAX_SCHEDULE_COUNT,
925 	},
926 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {
927 		.packing = sja1105_schedule_entry_points_entry_packing,
928 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry_points_entry),
929 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_ENTRY,
930 		.max_entry_count = SJA1105_MAX_SCHEDULE_ENTRY_POINTS_COUNT,
931 	},
932 	[BLK_IDX_VL_LOOKUP] = {
933 		.packing = sja1105_vl_lookup_entry_packing,
934 		.unpacked_entry_size = sizeof(struct sja1105_vl_lookup_entry),
935 		.packed_entry_size = SJA1105_SIZE_VL_LOOKUP_ENTRY,
936 		.max_entry_count = SJA1105_MAX_VL_LOOKUP_COUNT,
937 	},
938 	[BLK_IDX_VL_POLICING] = {
939 		.packing = sja1105_vl_policing_entry_packing,
940 		.unpacked_entry_size = sizeof(struct sja1105_vl_policing_entry),
941 		.packed_entry_size = SJA1105_SIZE_VL_POLICING_ENTRY,
942 		.max_entry_count = SJA1105_MAX_VL_POLICING_COUNT,
943 	},
944 	[BLK_IDX_VL_FORWARDING] = {
945 		.packing = sja1105_vl_forwarding_entry_packing,
946 		.unpacked_entry_size = sizeof(struct sja1105_vl_forwarding_entry),
947 		.packed_entry_size = SJA1105_SIZE_VL_FORWARDING_ENTRY,
948 		.max_entry_count = SJA1105_MAX_VL_FORWARDING_COUNT,
949 	},
950 	[BLK_IDX_L2_LOOKUP] = {
951 		.packing = sja1105et_l2_lookup_entry_packing,
952 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_entry),
953 		.packed_entry_size = SJA1105ET_SIZE_L2_LOOKUP_ENTRY,
954 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
955 	},
956 	[BLK_IDX_L2_POLICING] = {
957 		.packing = sja1105_l2_policing_entry_packing,
958 		.unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
959 		.packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
960 		.max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
961 	},
962 	[BLK_IDX_VLAN_LOOKUP] = {
963 		.packing = sja1105_vlan_lookup_entry_packing,
964 		.unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
965 		.packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
966 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
967 	},
968 	[BLK_IDX_L2_FORWARDING] = {
969 		.packing = sja1105_l2_forwarding_entry_packing,
970 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
971 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
972 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
973 	},
974 	[BLK_IDX_MAC_CONFIG] = {
975 		.packing = sja1105et_mac_config_entry_packing,
976 		.unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
977 		.packed_entry_size = SJA1105ET_SIZE_MAC_CONFIG_ENTRY,
978 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
979 	},
980 	[BLK_IDX_SCHEDULE_PARAMS] = {
981 		.packing = sja1105_schedule_params_entry_packing,
982 		.unpacked_entry_size = sizeof(struct sja1105_schedule_params_entry),
983 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_PARAMS_ENTRY,
984 		.max_entry_count = SJA1105_MAX_SCHEDULE_PARAMS_COUNT,
985 	},
986 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {
987 		.packing = sja1105_schedule_entry_points_params_entry_packing,
988 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry_points_params_entry),
989 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_PARAMS_ENTRY,
990 		.max_entry_count = SJA1105_MAX_SCHEDULE_ENTRY_POINTS_PARAMS_COUNT,
991 	},
992 	[BLK_IDX_VL_FORWARDING_PARAMS] = {
993 		.packing = sja1105_vl_forwarding_params_entry_packing,
994 		.unpacked_entry_size = sizeof(struct sja1105_vl_forwarding_params_entry),
995 		.packed_entry_size = SJA1105_SIZE_VL_FORWARDING_PARAMS_ENTRY,
996 		.max_entry_count = SJA1105_MAX_VL_FORWARDING_PARAMS_COUNT,
997 	},
998 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
999 		.packing = sja1105et_l2_lookup_params_entry_packing,
1000 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_params_entry),
1001 		.packed_entry_size = SJA1105ET_SIZE_L2_LOOKUP_PARAMS_ENTRY,
1002 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
1003 	},
1004 	[BLK_IDX_L2_FORWARDING_PARAMS] = {
1005 		.packing = sja1105_l2_forwarding_params_entry_packing,
1006 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
1007 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
1008 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
1009 	},
1010 	[BLK_IDX_AVB_PARAMS] = {
1011 		.packing = sja1105et_avb_params_entry_packing,
1012 		.unpacked_entry_size = sizeof(struct sja1105_avb_params_entry),
1013 		.packed_entry_size = SJA1105ET_SIZE_AVB_PARAMS_ENTRY,
1014 		.max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
1015 	},
1016 	[BLK_IDX_GENERAL_PARAMS] = {
1017 		.packing = sja1105et_general_params_entry_packing,
1018 		.unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
1019 		.packed_entry_size = SJA1105ET_SIZE_GENERAL_PARAMS_ENTRY,
1020 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
1021 	},
1022 	[BLK_IDX_RETAGGING] = {
1023 		.packing = sja1105_retagging_entry_packing,
1024 		.unpacked_entry_size = sizeof(struct sja1105_retagging_entry),
1025 		.packed_entry_size = SJA1105_SIZE_RETAGGING_ENTRY,
1026 		.max_entry_count = SJA1105_MAX_RETAGGING_COUNT,
1027 	},
1028 	[BLK_IDX_XMII_PARAMS] = {
1029 		.packing = sja1105_xmii_params_entry_packing,
1030 		.unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
1031 		.packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
1032 		.max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
1033 	},
1034 };
1035 
1036 /* SJA1105P: Second generation, no TTEthernet, no SGMII */
1037 struct sja1105_table_ops sja1105p_table_ops[BLK_IDX_MAX] = {
1038 	[BLK_IDX_SCHEDULE] = {0},
1039 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {0},
1040 	[BLK_IDX_VL_LOOKUP] = {0},
1041 	[BLK_IDX_VL_POLICING] = {0},
1042 	[BLK_IDX_VL_FORWARDING] = {0},
1043 	[BLK_IDX_L2_LOOKUP] = {
1044 		.packing = sja1105pqrs_l2_lookup_entry_packing,
1045 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_entry),
1046 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY,
1047 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
1048 	},
1049 	[BLK_IDX_L2_POLICING] = {
1050 		.packing = sja1105_l2_policing_entry_packing,
1051 		.unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
1052 		.packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
1053 		.max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
1054 	},
1055 	[BLK_IDX_VLAN_LOOKUP] = {
1056 		.packing = sja1105_vlan_lookup_entry_packing,
1057 		.unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
1058 		.packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
1059 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
1060 	},
1061 	[BLK_IDX_L2_FORWARDING] = {
1062 		.packing = sja1105_l2_forwarding_entry_packing,
1063 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
1064 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
1065 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
1066 	},
1067 	[BLK_IDX_MAC_CONFIG] = {
1068 		.packing = sja1105pqrs_mac_config_entry_packing,
1069 		.unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
1070 		.packed_entry_size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY,
1071 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
1072 	},
1073 	[BLK_IDX_SCHEDULE_PARAMS] = {0},
1074 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {0},
1075 	[BLK_IDX_VL_FORWARDING_PARAMS] = {0},
1076 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
1077 		.packing = sja1105pqrs_l2_lookup_params_entry_packing,
1078 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_params_entry),
1079 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY,
1080 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
1081 	},
1082 	[BLK_IDX_L2_FORWARDING_PARAMS] = {
1083 		.packing = sja1105_l2_forwarding_params_entry_packing,
1084 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
1085 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
1086 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
1087 	},
1088 	[BLK_IDX_AVB_PARAMS] = {
1089 		.packing = sja1105pqrs_avb_params_entry_packing,
1090 		.unpacked_entry_size = sizeof(struct sja1105_avb_params_entry),
1091 		.packed_entry_size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY,
1092 		.max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
1093 	},
1094 	[BLK_IDX_GENERAL_PARAMS] = {
1095 		.packing = sja1105pqrs_general_params_entry_packing,
1096 		.unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
1097 		.packed_entry_size = SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY,
1098 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
1099 	},
1100 	[BLK_IDX_RETAGGING] = {
1101 		.packing = sja1105_retagging_entry_packing,
1102 		.unpacked_entry_size = sizeof(struct sja1105_retagging_entry),
1103 		.packed_entry_size = SJA1105_SIZE_RETAGGING_ENTRY,
1104 		.max_entry_count = SJA1105_MAX_RETAGGING_COUNT,
1105 	},
1106 	[BLK_IDX_XMII_PARAMS] = {
1107 		.packing = sja1105_xmii_params_entry_packing,
1108 		.unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
1109 		.packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
1110 		.max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
1111 	},
1112 };
1113 
1114 /* SJA1105Q: Second generation, TTEthernet, no SGMII */
1115 struct sja1105_table_ops sja1105q_table_ops[BLK_IDX_MAX] = {
1116 	[BLK_IDX_SCHEDULE] = {
1117 		.packing = sja1105_schedule_entry_packing,
1118 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry),
1119 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY,
1120 		.max_entry_count = SJA1105_MAX_SCHEDULE_COUNT,
1121 	},
1122 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {
1123 		.packing = sja1105_schedule_entry_points_entry_packing,
1124 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry_points_entry),
1125 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_ENTRY,
1126 		.max_entry_count = SJA1105_MAX_SCHEDULE_ENTRY_POINTS_COUNT,
1127 	},
1128 	[BLK_IDX_VL_LOOKUP] = {
1129 		.packing = sja1105_vl_lookup_entry_packing,
1130 		.unpacked_entry_size = sizeof(struct sja1105_vl_lookup_entry),
1131 		.packed_entry_size = SJA1105_SIZE_VL_LOOKUP_ENTRY,
1132 		.max_entry_count = SJA1105_MAX_VL_LOOKUP_COUNT,
1133 	},
1134 	[BLK_IDX_VL_POLICING] = {
1135 		.packing = sja1105_vl_policing_entry_packing,
1136 		.unpacked_entry_size = sizeof(struct sja1105_vl_policing_entry),
1137 		.packed_entry_size = SJA1105_SIZE_VL_POLICING_ENTRY,
1138 		.max_entry_count = SJA1105_MAX_VL_POLICING_COUNT,
1139 	},
1140 	[BLK_IDX_VL_FORWARDING] = {
1141 		.packing = sja1105_vl_forwarding_entry_packing,
1142 		.unpacked_entry_size = sizeof(struct sja1105_vl_forwarding_entry),
1143 		.packed_entry_size = SJA1105_SIZE_VL_FORWARDING_ENTRY,
1144 		.max_entry_count = SJA1105_MAX_VL_FORWARDING_COUNT,
1145 	},
1146 	[BLK_IDX_L2_LOOKUP] = {
1147 		.packing = sja1105pqrs_l2_lookup_entry_packing,
1148 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_entry),
1149 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY,
1150 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
1151 	},
1152 	[BLK_IDX_L2_POLICING] = {
1153 		.packing = sja1105_l2_policing_entry_packing,
1154 		.unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
1155 		.packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
1156 		.max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
1157 	},
1158 	[BLK_IDX_VLAN_LOOKUP] = {
1159 		.packing = sja1105_vlan_lookup_entry_packing,
1160 		.unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
1161 		.packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
1162 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
1163 	},
1164 	[BLK_IDX_L2_FORWARDING] = {
1165 		.packing = sja1105_l2_forwarding_entry_packing,
1166 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
1167 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
1168 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
1169 	},
1170 	[BLK_IDX_MAC_CONFIG] = {
1171 		.packing = sja1105pqrs_mac_config_entry_packing,
1172 		.unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
1173 		.packed_entry_size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY,
1174 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
1175 	},
1176 	[BLK_IDX_SCHEDULE_PARAMS] = {
1177 		.packing = sja1105_schedule_params_entry_packing,
1178 		.unpacked_entry_size = sizeof(struct sja1105_schedule_params_entry),
1179 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_PARAMS_ENTRY,
1180 		.max_entry_count = SJA1105_MAX_SCHEDULE_PARAMS_COUNT,
1181 	},
1182 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {
1183 		.packing = sja1105_schedule_entry_points_params_entry_packing,
1184 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry_points_params_entry),
1185 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_PARAMS_ENTRY,
1186 		.max_entry_count = SJA1105_MAX_SCHEDULE_ENTRY_POINTS_PARAMS_COUNT,
1187 	},
1188 	[BLK_IDX_VL_FORWARDING_PARAMS] = {
1189 		.packing = sja1105_vl_forwarding_params_entry_packing,
1190 		.unpacked_entry_size = sizeof(struct sja1105_vl_forwarding_params_entry),
1191 		.packed_entry_size = SJA1105_SIZE_VL_FORWARDING_PARAMS_ENTRY,
1192 		.max_entry_count = SJA1105_MAX_VL_FORWARDING_PARAMS_COUNT,
1193 	},
1194 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
1195 		.packing = sja1105pqrs_l2_lookup_params_entry_packing,
1196 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_params_entry),
1197 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY,
1198 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
1199 	},
1200 	[BLK_IDX_L2_FORWARDING_PARAMS] = {
1201 		.packing = sja1105_l2_forwarding_params_entry_packing,
1202 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
1203 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
1204 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
1205 	},
1206 	[BLK_IDX_AVB_PARAMS] = {
1207 		.packing = sja1105pqrs_avb_params_entry_packing,
1208 		.unpacked_entry_size = sizeof(struct sja1105_avb_params_entry),
1209 		.packed_entry_size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY,
1210 		.max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
1211 	},
1212 	[BLK_IDX_GENERAL_PARAMS] = {
1213 		.packing = sja1105pqrs_general_params_entry_packing,
1214 		.unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
1215 		.packed_entry_size = SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY,
1216 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
1217 	},
1218 	[BLK_IDX_RETAGGING] = {
1219 		.packing = sja1105_retagging_entry_packing,
1220 		.unpacked_entry_size = sizeof(struct sja1105_retagging_entry),
1221 		.packed_entry_size = SJA1105_SIZE_RETAGGING_ENTRY,
1222 		.max_entry_count = SJA1105_MAX_RETAGGING_COUNT,
1223 	},
1224 	[BLK_IDX_XMII_PARAMS] = {
1225 		.packing = sja1105_xmii_params_entry_packing,
1226 		.unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
1227 		.packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
1228 		.max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
1229 	},
1230 };
1231 
1232 /* SJA1105R: Second generation, no TTEthernet, SGMII */
1233 struct sja1105_table_ops sja1105r_table_ops[BLK_IDX_MAX] = {
1234 	[BLK_IDX_SCHEDULE] = {0},
1235 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {0},
1236 	[BLK_IDX_VL_LOOKUP] = {0},
1237 	[BLK_IDX_VL_POLICING] = {0},
1238 	[BLK_IDX_VL_FORWARDING] = {0},
1239 	[BLK_IDX_L2_LOOKUP] = {
1240 		.packing = sja1105pqrs_l2_lookup_entry_packing,
1241 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_entry),
1242 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY,
1243 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
1244 	},
1245 	[BLK_IDX_L2_POLICING] = {
1246 		.packing = sja1105_l2_policing_entry_packing,
1247 		.unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
1248 		.packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
1249 		.max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
1250 	},
1251 	[BLK_IDX_VLAN_LOOKUP] = {
1252 		.packing = sja1105_vlan_lookup_entry_packing,
1253 		.unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
1254 		.packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
1255 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
1256 	},
1257 	[BLK_IDX_L2_FORWARDING] = {
1258 		.packing = sja1105_l2_forwarding_entry_packing,
1259 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
1260 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
1261 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
1262 	},
1263 	[BLK_IDX_MAC_CONFIG] = {
1264 		.packing = sja1105pqrs_mac_config_entry_packing,
1265 		.unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
1266 		.packed_entry_size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY,
1267 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
1268 	},
1269 	[BLK_IDX_SCHEDULE_PARAMS] = {0},
1270 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {0},
1271 	[BLK_IDX_VL_FORWARDING_PARAMS] = {0},
1272 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
1273 		.packing = sja1105pqrs_l2_lookup_params_entry_packing,
1274 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_params_entry),
1275 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY,
1276 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
1277 	},
1278 	[BLK_IDX_L2_FORWARDING_PARAMS] = {
1279 		.packing = sja1105_l2_forwarding_params_entry_packing,
1280 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
1281 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
1282 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
1283 	},
1284 	[BLK_IDX_AVB_PARAMS] = {
1285 		.packing = sja1105pqrs_avb_params_entry_packing,
1286 		.unpacked_entry_size = sizeof(struct sja1105_avb_params_entry),
1287 		.packed_entry_size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY,
1288 		.max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
1289 	},
1290 	[BLK_IDX_GENERAL_PARAMS] = {
1291 		.packing = sja1105pqrs_general_params_entry_packing,
1292 		.unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
1293 		.packed_entry_size = SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY,
1294 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
1295 	},
1296 	[BLK_IDX_RETAGGING] = {
1297 		.packing = sja1105_retagging_entry_packing,
1298 		.unpacked_entry_size = sizeof(struct sja1105_retagging_entry),
1299 		.packed_entry_size = SJA1105_SIZE_RETAGGING_ENTRY,
1300 		.max_entry_count = SJA1105_MAX_RETAGGING_COUNT,
1301 	},
1302 	[BLK_IDX_XMII_PARAMS] = {
1303 		.packing = sja1105_xmii_params_entry_packing,
1304 		.unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
1305 		.packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
1306 		.max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
1307 	},
1308 };
1309 
1310 /* SJA1105S: Second generation, TTEthernet, SGMII */
1311 struct sja1105_table_ops sja1105s_table_ops[BLK_IDX_MAX] = {
1312 	[BLK_IDX_SCHEDULE] = {
1313 		.packing = sja1105_schedule_entry_packing,
1314 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry),
1315 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY,
1316 		.max_entry_count = SJA1105_MAX_SCHEDULE_COUNT,
1317 	},
1318 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {
1319 		.packing = sja1105_schedule_entry_points_entry_packing,
1320 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry_points_entry),
1321 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_ENTRY,
1322 		.max_entry_count = SJA1105_MAX_SCHEDULE_ENTRY_POINTS_COUNT,
1323 	},
1324 	[BLK_IDX_VL_LOOKUP] = {
1325 		.packing = sja1105_vl_lookup_entry_packing,
1326 		.unpacked_entry_size = sizeof(struct sja1105_vl_lookup_entry),
1327 		.packed_entry_size = SJA1105_SIZE_VL_LOOKUP_ENTRY,
1328 		.max_entry_count = SJA1105_MAX_VL_LOOKUP_COUNT,
1329 	},
1330 	[BLK_IDX_VL_POLICING] = {
1331 		.packing = sja1105_vl_policing_entry_packing,
1332 		.unpacked_entry_size = sizeof(struct sja1105_vl_policing_entry),
1333 		.packed_entry_size = SJA1105_SIZE_VL_POLICING_ENTRY,
1334 		.max_entry_count = SJA1105_MAX_VL_POLICING_COUNT,
1335 	},
1336 	[BLK_IDX_VL_FORWARDING] = {
1337 		.packing = sja1105_vl_forwarding_entry_packing,
1338 		.unpacked_entry_size = sizeof(struct sja1105_vl_forwarding_entry),
1339 		.packed_entry_size = SJA1105_SIZE_VL_FORWARDING_ENTRY,
1340 		.max_entry_count = SJA1105_MAX_VL_FORWARDING_COUNT,
1341 	},
1342 	[BLK_IDX_L2_LOOKUP] = {
1343 		.packing = sja1105pqrs_l2_lookup_entry_packing,
1344 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_entry),
1345 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY,
1346 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
1347 	},
1348 	[BLK_IDX_L2_POLICING] = {
1349 		.packing = sja1105_l2_policing_entry_packing,
1350 		.unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
1351 		.packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
1352 		.max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
1353 	},
1354 	[BLK_IDX_VLAN_LOOKUP] = {
1355 		.packing = sja1105_vlan_lookup_entry_packing,
1356 		.unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
1357 		.packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
1358 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
1359 	},
1360 	[BLK_IDX_L2_FORWARDING] = {
1361 		.packing = sja1105_l2_forwarding_entry_packing,
1362 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
1363 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
1364 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
1365 	},
1366 	[BLK_IDX_MAC_CONFIG] = {
1367 		.packing = sja1105pqrs_mac_config_entry_packing,
1368 		.unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
1369 		.packed_entry_size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY,
1370 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
1371 	},
1372 	[BLK_IDX_SCHEDULE_PARAMS] = {
1373 		.packing = sja1105_schedule_params_entry_packing,
1374 		.unpacked_entry_size = sizeof(struct sja1105_schedule_params_entry),
1375 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_PARAMS_ENTRY,
1376 		.max_entry_count = SJA1105_MAX_SCHEDULE_PARAMS_COUNT,
1377 	},
1378 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {
1379 		.packing = sja1105_schedule_entry_points_params_entry_packing,
1380 		.unpacked_entry_size = sizeof(struct sja1105_schedule_entry_points_params_entry),
1381 		.packed_entry_size = SJA1105_SIZE_SCHEDULE_ENTRY_POINTS_PARAMS_ENTRY,
1382 		.max_entry_count = SJA1105_MAX_SCHEDULE_ENTRY_POINTS_PARAMS_COUNT,
1383 	},
1384 	[BLK_IDX_VL_FORWARDING_PARAMS] = {
1385 		.packing = sja1105_vl_forwarding_params_entry_packing,
1386 		.unpacked_entry_size = sizeof(struct sja1105_vl_forwarding_params_entry),
1387 		.packed_entry_size = SJA1105_SIZE_VL_FORWARDING_PARAMS_ENTRY,
1388 		.max_entry_count = SJA1105_MAX_VL_FORWARDING_PARAMS_COUNT,
1389 	},
1390 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
1391 		.packing = sja1105pqrs_l2_lookup_params_entry_packing,
1392 		.unpacked_entry_size = sizeof(struct sja1105_l2_lookup_params_entry),
1393 		.packed_entry_size = SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY,
1394 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
1395 	},
1396 	[BLK_IDX_L2_FORWARDING_PARAMS] = {
1397 		.packing = sja1105_l2_forwarding_params_entry_packing,
1398 		.unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
1399 		.packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
1400 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
1401 	},
1402 	[BLK_IDX_AVB_PARAMS] = {
1403 		.packing = sja1105pqrs_avb_params_entry_packing,
1404 		.unpacked_entry_size = sizeof(struct sja1105_avb_params_entry),
1405 		.packed_entry_size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY,
1406 		.max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
1407 	},
1408 	[BLK_IDX_GENERAL_PARAMS] = {
1409 		.packing = sja1105pqrs_general_params_entry_packing,
1410 		.unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
1411 		.packed_entry_size = SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY,
1412 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
1413 	},
1414 	[BLK_IDX_RETAGGING] = {
1415 		.packing = sja1105_retagging_entry_packing,
1416 		.unpacked_entry_size = sizeof(struct sja1105_retagging_entry),
1417 		.packed_entry_size = SJA1105_SIZE_RETAGGING_ENTRY,
1418 		.max_entry_count = SJA1105_MAX_RETAGGING_COUNT,
1419 	},
1420 	[BLK_IDX_XMII_PARAMS] = {
1421 		.packing = sja1105_xmii_params_entry_packing,
1422 		.unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
1423 		.packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
1424 		.max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
1425 	},
1426 };
1427 
1428 int sja1105_static_config_init(struct sja1105_static_config *config,
1429 			       const struct sja1105_table_ops *static_ops,
1430 			       u64 device_id)
1431 {
1432 	enum sja1105_blk_idx i;
1433 
1434 	*config = (struct sja1105_static_config) {0};
1435 
1436 	/* Transfer static_ops array from priv into per-table ops
1437 	 * for handier access
1438 	 */
1439 	for (i = 0; i < BLK_IDX_MAX; i++)
1440 		config->tables[i].ops = &static_ops[i];
1441 
1442 	config->device_id = device_id;
1443 	return 0;
1444 }
1445 
1446 void sja1105_static_config_free(struct sja1105_static_config *config)
1447 {
1448 	enum sja1105_blk_idx i;
1449 
1450 	for (i = 0; i < BLK_IDX_MAX; i++) {
1451 		if (config->tables[i].entry_count) {
1452 			kfree(config->tables[i].entries);
1453 			config->tables[i].entry_count = 0;
1454 		}
1455 	}
1456 }
1457 
1458 int sja1105_table_delete_entry(struct sja1105_table *table, int i)
1459 {
1460 	size_t entry_size = table->ops->unpacked_entry_size;
1461 	u8 *entries = table->entries;
1462 
1463 	if (i > table->entry_count)
1464 		return -ERANGE;
1465 
1466 	memmove(entries + i * entry_size, entries + (i + 1) * entry_size,
1467 		(table->entry_count - i) * entry_size);
1468 
1469 	table->entry_count--;
1470 
1471 	return 0;
1472 }
1473 
1474 /* No pointers to table->entries should be kept when this is called. */
1475 int sja1105_table_resize(struct sja1105_table *table, size_t new_count)
1476 {
1477 	size_t entry_size = table->ops->unpacked_entry_size;
1478 	void *new_entries, *old_entries = table->entries;
1479 
1480 	if (new_count > table->ops->max_entry_count)
1481 		return -ERANGE;
1482 
1483 	new_entries = kcalloc(new_count, entry_size, GFP_KERNEL);
1484 	if (!new_entries)
1485 		return -ENOMEM;
1486 
1487 	memcpy(new_entries, old_entries, min(new_count, table->entry_count) *
1488 		entry_size);
1489 
1490 	table->entries = new_entries;
1491 	table->entry_count = new_count;
1492 	kfree(old_entries);
1493 	return 0;
1494 }
1495