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