1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
3  */
4 #include "sja1105.h"
5 
6 /* In the dynamic configuration interface, the switch exposes a register-like
7  * view of some of the static configuration tables.
8  * Many times the field organization of the dynamic tables is abbreviated (not
9  * all fields are dynamically reconfigurable) and different from the static
10  * ones, but the key reason for having it is that we can spare a switch reset
11  * for settings that can be changed dynamically.
12  *
13  * This file creates a per-switch-family abstraction called
14  * struct sja1105_dynamic_table_ops and two operations that work with it:
15  * - sja1105_dynamic_config_write
16  * - sja1105_dynamic_config_read
17  *
18  * Compared to the struct sja1105_table_ops from sja1105_static_config.c,
19  * the dynamic accessors work with a compound buffer:
20  *
21  * packed_buf
22  *
23  * |
24  * V
25  * +-----------------------------------------+------------------+
26  * |              ENTRY BUFFER               |  COMMAND BUFFER  |
27  * +-----------------------------------------+------------------+
28  *
29  * <----------------------- packed_size ------------------------>
30  *
31  * The ENTRY BUFFER may or may not have the same layout, or size, as its static
32  * configuration table entry counterpart. When it does, the same packing
33  * function is reused (bar exceptional cases - see
34  * sja1105pqrs_dyn_l2_lookup_entry_packing).
35  *
36  * The reason for the COMMAND BUFFER being at the end is to be able to send
37  * a dynamic write command through a single SPI burst. By the time the switch
38  * reacts to the command, the ENTRY BUFFER is already populated with the data
39  * sent by the core.
40  *
41  * The COMMAND BUFFER is always SJA1105_SIZE_DYN_CMD bytes (one 32-bit word) in
42  * size.
43  *
44  * Sometimes the ENTRY BUFFER does not really exist (when the number of fields
45  * that can be reconfigured is small), then the switch repurposes some of the
46  * unused 32 bits of the COMMAND BUFFER to hold ENTRY data.
47  *
48  * The key members of struct sja1105_dynamic_table_ops are:
49  * - .entry_packing: A function that deals with packing an ENTRY structure
50  *		     into an SPI buffer, or retrieving an ENTRY structure
51  *		     from one.
52  *		     The @packed_buf pointer it's given does always point to
53  *		     the ENTRY portion of the buffer.
54  * - .cmd_packing: A function that deals with packing/unpacking the COMMAND
55  *		   structure to/from the SPI buffer.
56  *		   It is given the same @packed_buf pointer as .entry_packing,
57  *		   so most of the time, the @packed_buf points *behind* the
58  *		   COMMAND offset inside the buffer.
59  *		   To access the COMMAND portion of the buffer, the function
60  *		   knows its correct offset.
61  *		   Giving both functions the same pointer is handy because in
62  *		   extreme cases (see sja1105pqrs_dyn_l2_lookup_entry_packing)
63  *		   the .entry_packing is able to jump to the COMMAND portion,
64  *		   or vice-versa (sja1105pqrs_l2_lookup_cmd_packing).
65  * - .access: A bitmap of:
66  *	OP_READ: Set if the hardware manual marks the ENTRY portion of the
67  *		 dynamic configuration table buffer as R (readable) after
68  *		 an SPI read command (the switch will populate the buffer).
69  *	OP_WRITE: Set if the manual marks the ENTRY portion of the dynamic
70  *		  table buffer as W (writable) after an SPI write command
71  *		  (the switch will read the fields provided in the buffer).
72  *	OP_DEL: Set if the manual says the VALIDENT bit is supported in the
73  *		COMMAND portion of this dynamic config buffer (i.e. the
74  *		specified entry can be invalidated through a SPI write
75  *		command).
76  *	OP_SEARCH: Set if the manual says that the index of an entry can
77  *		   be retrieved in the COMMAND portion of the buffer based
78  *		   on its ENTRY portion, as a result of a SPI write command.
79  *		   Only the TCAM-based FDB table on SJA1105 P/Q/R/S supports
80  *		   this.
81  * - .max_entry_count: The number of entries, counting from zero, that can be
82  *		       reconfigured through the dynamic interface. If a static
83  *		       table can be reconfigured at all dynamically, this
84  *		       number always matches the maximum number of supported
85  *		       static entries.
86  * - .packed_size: The length in bytes of the compound ENTRY + COMMAND BUFFER.
87  *		   Note that sometimes the compound buffer may contain holes in
88  *		   it (see sja1105_vlan_lookup_cmd_packing). The @packed_buf is
89  *		   contiguous however, so @packed_size includes any unused
90  *		   bytes.
91  * - .addr: The base SPI address at which the buffer must be written to the
92  *	    switch's memory. When looking at the hardware manual, this must
93  *	    always match the lowest documented address for the ENTRY, and not
94  *	    that of the COMMAND, since the other 32-bit words will follow along
95  *	    at the correct addresses.
96  */
97 
98 #define SJA1105_SIZE_DYN_CMD					4
99 
100 #define SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY			\
101 	SJA1105_SIZE_DYN_CMD
102 
103 #define SJA1105ET_SIZE_L2_LOOKUP_DYN_CMD			\
104 	(SJA1105_SIZE_DYN_CMD + SJA1105ET_SIZE_L2_LOOKUP_ENTRY)
105 
106 #define SJA1105PQRS_SIZE_L2_LOOKUP_DYN_CMD			\
107 	(SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY)
108 
109 #define SJA1105_SIZE_VLAN_LOOKUP_DYN_CMD			\
110 	(SJA1105_SIZE_DYN_CMD + 4 + SJA1105_SIZE_VLAN_LOOKUP_ENTRY)
111 
112 #define SJA1105_SIZE_L2_FORWARDING_DYN_CMD			\
113 	(SJA1105_SIZE_DYN_CMD + SJA1105_SIZE_L2_FORWARDING_ENTRY)
114 
115 #define SJA1105ET_SIZE_MAC_CONFIG_DYN_CMD			\
116 	(SJA1105_SIZE_DYN_CMD + SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY)
117 
118 #define SJA1105PQRS_SIZE_MAC_CONFIG_DYN_CMD			\
119 	(SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY)
120 
121 #define SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD			\
122 	SJA1105_SIZE_DYN_CMD
123 
124 #define SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD			\
125 	SJA1105_SIZE_DYN_CMD
126 
127 #define SJA1105PQRS_SIZE_AVB_PARAMS_DYN_CMD			\
128 	(SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY)
129 
130 #define SJA1105_MAX_DYN_CMD_SIZE				\
131 	SJA1105PQRS_SIZE_MAC_CONFIG_DYN_CMD
132 
133 struct sja1105_dyn_cmd {
134 	bool search;
135 	u64 valid;
136 	u64 rdwrset;
137 	u64 errors;
138 	u64 valident;
139 	u64 index;
140 };
141 
142 enum sja1105_hostcmd {
143 	SJA1105_HOSTCMD_SEARCH = 1,
144 	SJA1105_HOSTCMD_READ = 2,
145 	SJA1105_HOSTCMD_WRITE = 3,
146 	SJA1105_HOSTCMD_INVALIDATE = 4,
147 };
148 
149 static void
150 sja1105pqrs_l2_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
151 				  enum packing_op op)
152 {
153 	u8 *p = buf + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
154 	const int size = SJA1105_SIZE_DYN_CMD;
155 	u64 hostcmd;
156 
157 	sja1105_packing(p, &cmd->valid,    31, 31, size, op);
158 	sja1105_packing(p, &cmd->rdwrset,  30, 30, size, op);
159 	sja1105_packing(p, &cmd->errors,   29, 29, size, op);
160 	sja1105_packing(p, &cmd->valident, 27, 27, size, op);
161 
162 	/* VALIDENT is supposed to indicate "keep or not", but in SJA1105 E/T,
163 	 * using it to delete a management route was unsupported. UM10944
164 	 * said about it:
165 	 *
166 	 *   In case of a write access with the MGMTROUTE flag set,
167 	 *   the flag will be ignored. It will always be found cleared
168 	 *   for read accesses with the MGMTROUTE flag set.
169 	 *
170 	 * SJA1105 P/Q/R/S keeps the same behavior w.r.t. VALIDENT, but there
171 	 * is now another flag called HOSTCMD which does more stuff (quoting
172 	 * from UM11040):
173 	 *
174 	 *   A write request is accepted only when HOSTCMD is set to write host
175 	 *   or invalid. A read request is accepted only when HOSTCMD is set to
176 	 *   search host or read host.
177 	 *
178 	 * So it is possible to translate a RDWRSET/VALIDENT combination into
179 	 * HOSTCMD so that we keep the dynamic command API in place, and
180 	 * at the same time achieve compatibility with the management route
181 	 * command structure.
182 	 */
183 	if (cmd->rdwrset == SPI_READ) {
184 		if (cmd->search)
185 			hostcmd = SJA1105_HOSTCMD_SEARCH;
186 		else
187 			hostcmd = SJA1105_HOSTCMD_READ;
188 	} else {
189 		/* SPI_WRITE */
190 		if (cmd->valident)
191 			hostcmd = SJA1105_HOSTCMD_WRITE;
192 		else
193 			hostcmd = SJA1105_HOSTCMD_INVALIDATE;
194 	}
195 	sja1105_packing(p, &hostcmd, 25, 23, size, op);
196 
197 	/* Hack - The hardware takes the 'index' field within
198 	 * struct sja1105_l2_lookup_entry as the index on which this command
199 	 * will operate. However it will ignore everything else, so 'index'
200 	 * is logically part of command but physically part of entry.
201 	 * Populate the 'index' entry field from within the command callback,
202 	 * such that our API doesn't need to ask for a full-blown entry
203 	 * structure when e.g. a delete is requested.
204 	 */
205 	sja1105_packing(buf, &cmd->index, 15, 6,
206 			SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY, op);
207 }
208 
209 /* The switch is so retarded that it makes our command/entry abstraction
210  * crumble apart.
211  *
212  * On P/Q/R/S, the switch tries to say whether a FDB entry
213  * is statically programmed or dynamically learned via a flag called LOCKEDS.
214  * The hardware manual says about this fiels:
215  *
216  *   On write will specify the format of ENTRY.
217  *   On read the flag will be found cleared at times the VALID flag is found
218  *   set.  The flag will also be found cleared in response to a read having the
219  *   MGMTROUTE flag set.  In response to a read with the MGMTROUTE flag
220  *   cleared, the flag be set if the most recent access operated on an entry
221  *   that was either loaded by configuration or through dynamic reconfiguration
222  *   (as opposed to automatically learned entries).
223  *
224  * The trouble with this flag is that it's part of the *command* to access the
225  * dynamic interface, and not part of the *entry* retrieved from it.
226  * Otherwise said, for a sja1105_dynamic_config_read, LOCKEDS is supposed to be
227  * an output from the switch into the command buffer, and for a
228  * sja1105_dynamic_config_write, the switch treats LOCKEDS as an input
229  * (hence we can write either static, or automatically learned entries, from
230  * the core).
231  * But the manual contradicts itself in the last phrase where it says that on
232  * read, LOCKEDS will be set to 1 for all FDB entries written through the
233  * dynamic interface (therefore, the value of LOCKEDS from the
234  * sja1105_dynamic_config_write is not really used for anything, it'll store a
235  * 1 anyway).
236  * This means you can't really write a FDB entry with LOCKEDS=0 (automatically
237  * learned) into the switch, which kind of makes sense.
238  * As for reading through the dynamic interface, it doesn't make too much sense
239  * to put LOCKEDS into the command, since the switch will inevitably have to
240  * ignore it (otherwise a command would be like "read the FDB entry 123, but
241  * only if it's dynamically learned" <- well how am I supposed to know?) and
242  * just use it as an output buffer for its findings. But guess what... that's
243  * what the entry buffer is for!
244  * Unfortunately, what really breaks this abstraction is the fact that it
245  * wasn't designed having the fact in mind that the switch can output
246  * entry-related data as writeback through the command buffer.
247  * However, whether a FDB entry is statically or dynamically learned *is* part
248  * of the entry and not the command data, no matter what the switch thinks.
249  * In order to do that, we'll need to wrap around the
250  * sja1105pqrs_l2_lookup_entry_packing from sja1105_static_config.c, and take
251  * a peek outside of the caller-supplied @buf (the entry buffer), to reach the
252  * command buffer.
253  */
254 static size_t
255 sja1105pqrs_dyn_l2_lookup_entry_packing(void *buf, void *entry_ptr,
256 					enum packing_op op)
257 {
258 	struct sja1105_l2_lookup_entry *entry = entry_ptr;
259 	u8 *cmd = buf + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
260 	const int size = SJA1105_SIZE_DYN_CMD;
261 
262 	sja1105_packing(cmd, &entry->lockeds, 28, 28, size, op);
263 
264 	return sja1105pqrs_l2_lookup_entry_packing(buf, entry_ptr, op);
265 }
266 
267 static void
268 sja1105et_l2_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
269 				enum packing_op op)
270 {
271 	u8 *p = buf + SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
272 	const int size = SJA1105_SIZE_DYN_CMD;
273 
274 	sja1105_packing(p, &cmd->valid,    31, 31, size, op);
275 	sja1105_packing(p, &cmd->rdwrset,  30, 30, size, op);
276 	sja1105_packing(p, &cmd->errors,   29, 29, size, op);
277 	sja1105_packing(p, &cmd->valident, 27, 27, size, op);
278 	/* Hack - see comments above. */
279 	sja1105_packing(buf, &cmd->index, 29, 20,
280 			SJA1105ET_SIZE_L2_LOOKUP_ENTRY, op);
281 }
282 
283 static size_t sja1105et_dyn_l2_lookup_entry_packing(void *buf, void *entry_ptr,
284 						    enum packing_op op)
285 {
286 	struct sja1105_l2_lookup_entry *entry = entry_ptr;
287 	u8 *cmd = buf + SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
288 	const int size = SJA1105_SIZE_DYN_CMD;
289 
290 	sja1105_packing(cmd, &entry->lockeds, 28, 28, size, op);
291 
292 	return sja1105et_l2_lookup_entry_packing(buf, entry_ptr, op);
293 }
294 
295 static void
296 sja1105et_mgmt_route_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
297 				 enum packing_op op)
298 {
299 	u8 *p = buf + SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
300 	u64 mgmtroute = 1;
301 
302 	sja1105et_l2_lookup_cmd_packing(buf, cmd, op);
303 	if (op == PACK)
304 		sja1105_pack(p, &mgmtroute, 26, 26, SJA1105_SIZE_DYN_CMD);
305 }
306 
307 static size_t sja1105et_mgmt_route_entry_packing(void *buf, void *entry_ptr,
308 						 enum packing_op op)
309 {
310 	struct sja1105_mgmt_entry *entry = entry_ptr;
311 	const size_t size = SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
312 
313 	/* UM10944: To specify if a PTP egress timestamp shall be captured on
314 	 * each port upon transmission of the frame, the LSB of VLANID in the
315 	 * ENTRY field provided by the host must be set.
316 	 * Bit 1 of VLANID then specifies the register where the timestamp for
317 	 * this port is stored in.
318 	 */
319 	sja1105_packing(buf, &entry->tsreg,     85, 85, size, op);
320 	sja1105_packing(buf, &entry->takets,    84, 84, size, op);
321 	sja1105_packing(buf, &entry->macaddr,   83, 36, size, op);
322 	sja1105_packing(buf, &entry->destports, 35, 31, size, op);
323 	sja1105_packing(buf, &entry->enfport,   30, 30, size, op);
324 	return size;
325 }
326 
327 static void
328 sja1105pqrs_mgmt_route_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
329 				   enum packing_op op)
330 {
331 	u8 *p = buf + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
332 	u64 mgmtroute = 1;
333 
334 	sja1105pqrs_l2_lookup_cmd_packing(buf, cmd, op);
335 	if (op == PACK)
336 		sja1105_pack(p, &mgmtroute, 26, 26, SJA1105_SIZE_DYN_CMD);
337 }
338 
339 static size_t sja1105pqrs_mgmt_route_entry_packing(void *buf, void *entry_ptr,
340 						   enum packing_op op)
341 {
342 	const size_t size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
343 	struct sja1105_mgmt_entry *entry = entry_ptr;
344 
345 	/* In P/Q/R/S, enfport got renamed to mgmtvalid, but its purpose
346 	 * is the same (driver uses it to confirm that frame was sent).
347 	 * So just keep the name from E/T.
348 	 */
349 	sja1105_packing(buf, &entry->tsreg,     71, 71, size, op);
350 	sja1105_packing(buf, &entry->takets,    70, 70, size, op);
351 	sja1105_packing(buf, &entry->macaddr,   69, 22, size, op);
352 	sja1105_packing(buf, &entry->destports, 21, 17, size, op);
353 	sja1105_packing(buf, &entry->enfport,   16, 16, size, op);
354 	return size;
355 }
356 
357 /* In E/T, entry is at addresses 0x27-0x28. There is a 4 byte gap at 0x29,
358  * and command is at 0x2a. Similarly in P/Q/R/S there is a 1 register gap
359  * between entry (0x2d, 0x2e) and command (0x30).
360  */
361 static void
362 sja1105_vlan_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
363 				enum packing_op op)
364 {
365 	u8 *p = buf + SJA1105_SIZE_VLAN_LOOKUP_ENTRY + 4;
366 	const int size = SJA1105_SIZE_DYN_CMD;
367 
368 	sja1105_packing(p, &cmd->valid,    31, 31, size, op);
369 	sja1105_packing(p, &cmd->rdwrset,  30, 30, size, op);
370 	sja1105_packing(p, &cmd->valident, 27, 27, size, op);
371 	/* Hack - see comments above, applied for 'vlanid' field of
372 	 * struct sja1105_vlan_lookup_entry.
373 	 */
374 	sja1105_packing(buf, &cmd->index, 38, 27,
375 			SJA1105_SIZE_VLAN_LOOKUP_ENTRY, op);
376 }
377 
378 static void
379 sja1105_l2_forwarding_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
380 				  enum packing_op op)
381 {
382 	u8 *p = buf + SJA1105_SIZE_L2_FORWARDING_ENTRY;
383 	const int size = SJA1105_SIZE_DYN_CMD;
384 
385 	sja1105_packing(p, &cmd->valid,   31, 31, size, op);
386 	sja1105_packing(p, &cmd->errors,  30, 30, size, op);
387 	sja1105_packing(p, &cmd->rdwrset, 29, 29, size, op);
388 	sja1105_packing(p, &cmd->index,    4,  0, size, op);
389 }
390 
391 static void
392 sja1105et_mac_config_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
393 				 enum packing_op op)
394 {
395 	const int size = SJA1105_SIZE_DYN_CMD;
396 	/* Yup, user manual definitions are reversed */
397 	u8 *reg1 = buf + 4;
398 
399 	sja1105_packing(reg1, &cmd->valid, 31, 31, size, op);
400 	sja1105_packing(reg1, &cmd->index, 26, 24, size, op);
401 }
402 
403 static size_t sja1105et_mac_config_entry_packing(void *buf, void *entry_ptr,
404 						 enum packing_op op)
405 {
406 	const int size = SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY;
407 	struct sja1105_mac_config_entry *entry = entry_ptr;
408 	/* Yup, user manual definitions are reversed */
409 	u8 *reg1 = buf + 4;
410 	u8 *reg2 = buf;
411 
412 	sja1105_packing(reg1, &entry->speed,     30, 29, size, op);
413 	sja1105_packing(reg1, &entry->drpdtag,   23, 23, size, op);
414 	sja1105_packing(reg1, &entry->drpuntag,  22, 22, size, op);
415 	sja1105_packing(reg1, &entry->retag,     21, 21, size, op);
416 	sja1105_packing(reg1, &entry->dyn_learn, 20, 20, size, op);
417 	sja1105_packing(reg1, &entry->egress,    19, 19, size, op);
418 	sja1105_packing(reg1, &entry->ingress,   18, 18, size, op);
419 	sja1105_packing(reg1, &entry->ing_mirr,  17, 17, size, op);
420 	sja1105_packing(reg1, &entry->egr_mirr,  16, 16, size, op);
421 	sja1105_packing(reg1, &entry->vlanprio,  14, 12, size, op);
422 	sja1105_packing(reg1, &entry->vlanid,    11,  0, size, op);
423 	sja1105_packing(reg2, &entry->tp_delin,  31, 16, size, op);
424 	sja1105_packing(reg2, &entry->tp_delout, 15,  0, size, op);
425 	/* MAC configuration table entries which can't be reconfigured:
426 	 * top, base, enabled, ifg, maxage, drpnona664
427 	 */
428 	/* Bogus return value, not used anywhere */
429 	return 0;
430 }
431 
432 static void
433 sja1105pqrs_mac_config_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
434 				   enum packing_op op)
435 {
436 	const int size = SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY;
437 	u8 *p = buf + SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY;
438 
439 	sja1105_packing(p, &cmd->valid,   31, 31, size, op);
440 	sja1105_packing(p, &cmd->errors,  30, 30, size, op);
441 	sja1105_packing(p, &cmd->rdwrset, 29, 29, size, op);
442 	sja1105_packing(p, &cmd->index,    2,  0, size, op);
443 }
444 
445 static void
446 sja1105et_l2_lookup_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
447 				       enum packing_op op)
448 {
449 	sja1105_packing(buf, &cmd->valid, 31, 31,
450 			SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD, op);
451 }
452 
453 static size_t
454 sja1105et_l2_lookup_params_entry_packing(void *buf, void *entry_ptr,
455 					 enum packing_op op)
456 {
457 	struct sja1105_l2_lookup_params_entry *entry = entry_ptr;
458 
459 	sja1105_packing(buf, &entry->poly, 7, 0,
460 			SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD, op);
461 	/* Bogus return value, not used anywhere */
462 	return 0;
463 }
464 
465 static void
466 sja1105et_general_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
467 				     enum packing_op op)
468 {
469 	const int size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD;
470 
471 	sja1105_packing(buf, &cmd->valid,  31, 31, size, op);
472 	sja1105_packing(buf, &cmd->errors, 30, 30, size, op);
473 }
474 
475 static size_t
476 sja1105et_general_params_entry_packing(void *buf, void *entry_ptr,
477 				       enum packing_op op)
478 {
479 	struct sja1105_general_params_entry *entry = entry_ptr;
480 	const int size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD;
481 
482 	sja1105_packing(buf, &entry->mirr_port, 2, 0, size, op);
483 	/* Bogus return value, not used anywhere */
484 	return 0;
485 }
486 
487 static void
488 sja1105pqrs_avb_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
489 				   enum packing_op op)
490 {
491 	u8 *p = buf + SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY;
492 	const int size = SJA1105_SIZE_DYN_CMD;
493 
494 	sja1105_packing(p, &cmd->valid,   31, 31, size, op);
495 	sja1105_packing(p, &cmd->errors,  30, 30, size, op);
496 	sja1105_packing(p, &cmd->rdwrset, 29, 29, size, op);
497 }
498 
499 #define OP_READ		BIT(0)
500 #define OP_WRITE	BIT(1)
501 #define OP_DEL		BIT(2)
502 #define OP_SEARCH	BIT(3)
503 
504 /* SJA1105E/T: First generation */
505 struct sja1105_dynamic_table_ops sja1105et_dyn_ops[BLK_IDX_MAX_DYN] = {
506 	[BLK_IDX_SCHEDULE] = {0},
507 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {0},
508 	[BLK_IDX_L2_LOOKUP] = {
509 		.entry_packing = sja1105et_dyn_l2_lookup_entry_packing,
510 		.cmd_packing = sja1105et_l2_lookup_cmd_packing,
511 		.access = (OP_READ | OP_WRITE | OP_DEL),
512 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
513 		.packed_size = SJA1105ET_SIZE_L2_LOOKUP_DYN_CMD,
514 		.addr = 0x20,
515 	},
516 	[BLK_IDX_MGMT_ROUTE] = {
517 		.entry_packing = sja1105et_mgmt_route_entry_packing,
518 		.cmd_packing = sja1105et_mgmt_route_cmd_packing,
519 		.access = (OP_READ | OP_WRITE),
520 		.max_entry_count = SJA1105_NUM_PORTS,
521 		.packed_size = SJA1105ET_SIZE_L2_LOOKUP_DYN_CMD,
522 		.addr = 0x20,
523 	},
524 	[BLK_IDX_L2_POLICING] = {0},
525 	[BLK_IDX_VLAN_LOOKUP] = {
526 		.entry_packing = sja1105_vlan_lookup_entry_packing,
527 		.cmd_packing = sja1105_vlan_lookup_cmd_packing,
528 		.access = (OP_WRITE | OP_DEL),
529 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
530 		.packed_size = SJA1105_SIZE_VLAN_LOOKUP_DYN_CMD,
531 		.addr = 0x27,
532 	},
533 	[BLK_IDX_L2_FORWARDING] = {
534 		.entry_packing = sja1105_l2_forwarding_entry_packing,
535 		.cmd_packing = sja1105_l2_forwarding_cmd_packing,
536 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
537 		.access = OP_WRITE,
538 		.packed_size = SJA1105_SIZE_L2_FORWARDING_DYN_CMD,
539 		.addr = 0x24,
540 	},
541 	[BLK_IDX_MAC_CONFIG] = {
542 		.entry_packing = sja1105et_mac_config_entry_packing,
543 		.cmd_packing = sja1105et_mac_config_cmd_packing,
544 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
545 		.access = OP_WRITE,
546 		.packed_size = SJA1105ET_SIZE_MAC_CONFIG_DYN_CMD,
547 		.addr = 0x36,
548 	},
549 	[BLK_IDX_SCHEDULE_PARAMS] = {0},
550 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {0},
551 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
552 		.entry_packing = sja1105et_l2_lookup_params_entry_packing,
553 		.cmd_packing = sja1105et_l2_lookup_params_cmd_packing,
554 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
555 		.access = OP_WRITE,
556 		.packed_size = SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD,
557 		.addr = 0x38,
558 	},
559 	[BLK_IDX_L2_FORWARDING_PARAMS] = {0},
560 	[BLK_IDX_AVB_PARAMS] = {0},
561 	[BLK_IDX_GENERAL_PARAMS] = {
562 		.entry_packing = sja1105et_general_params_entry_packing,
563 		.cmd_packing = sja1105et_general_params_cmd_packing,
564 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
565 		.access = OP_WRITE,
566 		.packed_size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD,
567 		.addr = 0x34,
568 	},
569 	[BLK_IDX_XMII_PARAMS] = {0},
570 };
571 
572 /* SJA1105P/Q/R/S: Second generation */
573 struct sja1105_dynamic_table_ops sja1105pqrs_dyn_ops[BLK_IDX_MAX_DYN] = {
574 	[BLK_IDX_SCHEDULE] = {0},
575 	[BLK_IDX_SCHEDULE_ENTRY_POINTS] = {0},
576 	[BLK_IDX_L2_LOOKUP] = {
577 		.entry_packing = sja1105pqrs_dyn_l2_lookup_entry_packing,
578 		.cmd_packing = sja1105pqrs_l2_lookup_cmd_packing,
579 		.access = (OP_READ | OP_WRITE | OP_DEL | OP_SEARCH),
580 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
581 		.packed_size = SJA1105PQRS_SIZE_L2_LOOKUP_DYN_CMD,
582 		.addr = 0x24,
583 	},
584 	[BLK_IDX_MGMT_ROUTE] = {
585 		.entry_packing = sja1105pqrs_mgmt_route_entry_packing,
586 		.cmd_packing = sja1105pqrs_mgmt_route_cmd_packing,
587 		.access = (OP_READ | OP_WRITE | OP_DEL | OP_SEARCH),
588 		.max_entry_count = SJA1105_NUM_PORTS,
589 		.packed_size = SJA1105PQRS_SIZE_L2_LOOKUP_DYN_CMD,
590 		.addr = 0x24,
591 	},
592 	[BLK_IDX_L2_POLICING] = {0},
593 	[BLK_IDX_VLAN_LOOKUP] = {
594 		.entry_packing = sja1105_vlan_lookup_entry_packing,
595 		.cmd_packing = sja1105_vlan_lookup_cmd_packing,
596 		.access = (OP_READ | OP_WRITE | OP_DEL),
597 		.max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
598 		.packed_size = SJA1105_SIZE_VLAN_LOOKUP_DYN_CMD,
599 		.addr = 0x2D,
600 	},
601 	[BLK_IDX_L2_FORWARDING] = {
602 		.entry_packing = sja1105_l2_forwarding_entry_packing,
603 		.cmd_packing = sja1105_l2_forwarding_cmd_packing,
604 		.max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
605 		.access = OP_WRITE,
606 		.packed_size = SJA1105_SIZE_L2_FORWARDING_DYN_CMD,
607 		.addr = 0x2A,
608 	},
609 	[BLK_IDX_MAC_CONFIG] = {
610 		.entry_packing = sja1105pqrs_mac_config_entry_packing,
611 		.cmd_packing = sja1105pqrs_mac_config_cmd_packing,
612 		.max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
613 		.access = (OP_READ | OP_WRITE),
614 		.packed_size = SJA1105PQRS_SIZE_MAC_CONFIG_DYN_CMD,
615 		.addr = 0x4B,
616 	},
617 	[BLK_IDX_SCHEDULE_PARAMS] = {0},
618 	[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS] = {0},
619 	[BLK_IDX_L2_LOOKUP_PARAMS] = {
620 		.entry_packing = sja1105et_l2_lookup_params_entry_packing,
621 		.cmd_packing = sja1105et_l2_lookup_params_cmd_packing,
622 		.max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
623 		.access = (OP_READ | OP_WRITE),
624 		.packed_size = SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD,
625 		.addr = 0x38,
626 	},
627 	[BLK_IDX_L2_FORWARDING_PARAMS] = {0},
628 	[BLK_IDX_AVB_PARAMS] = {
629 		.entry_packing = sja1105pqrs_avb_params_entry_packing,
630 		.cmd_packing = sja1105pqrs_avb_params_cmd_packing,
631 		.max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
632 		.access = (OP_READ | OP_WRITE),
633 		.packed_size = SJA1105PQRS_SIZE_AVB_PARAMS_DYN_CMD,
634 		.addr = 0x8003,
635 	},
636 	[BLK_IDX_GENERAL_PARAMS] = {
637 		.entry_packing = sja1105et_general_params_entry_packing,
638 		.cmd_packing = sja1105et_general_params_cmd_packing,
639 		.max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
640 		.access = OP_WRITE,
641 		.packed_size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD,
642 		.addr = 0x34,
643 	},
644 	[BLK_IDX_XMII_PARAMS] = {0},
645 };
646 
647 /* Provides read access to the settings through the dynamic interface
648  * of the switch.
649  * @blk_idx	is used as key to select from the sja1105_dynamic_table_ops.
650  *		The selection is limited by the hardware in respect to which
651  *		configuration blocks can be read through the dynamic interface.
652  * @index	is used to retrieve a particular table entry. If negative,
653  *		(and if the @blk_idx supports the searching operation) a search
654  *		is performed by the @entry parameter.
655  * @entry	Type-casted to an unpacked structure that holds a table entry
656  *		of the type specified in @blk_idx.
657  *		Usually an output argument. If @index is negative, then this
658  *		argument is used as input/output: it should be pre-populated
659  *		with the element to search for. Entries which support the
660  *		search operation will have an "index" field (not the @index
661  *		argument to this function) and that is where the found index
662  *		will be returned (or left unmodified - thus negative - if not
663  *		found).
664  */
665 int sja1105_dynamic_config_read(struct sja1105_private *priv,
666 				enum sja1105_blk_idx blk_idx,
667 				int index, void *entry)
668 {
669 	const struct sja1105_dynamic_table_ops *ops;
670 	struct sja1105_dyn_cmd cmd = {0};
671 	/* SPI payload buffer */
672 	u8 packed_buf[SJA1105_MAX_DYN_CMD_SIZE] = {0};
673 	int retries = 3;
674 	int rc;
675 
676 	if (blk_idx >= BLK_IDX_MAX_DYN)
677 		return -ERANGE;
678 
679 	ops = &priv->info->dyn_ops[blk_idx];
680 
681 	if (index >= 0 && index >= ops->max_entry_count)
682 		return -ERANGE;
683 	if (index < 0 && !(ops->access & OP_SEARCH))
684 		return -EOPNOTSUPP;
685 	if (!(ops->access & OP_READ))
686 		return -EOPNOTSUPP;
687 	if (ops->packed_size > SJA1105_MAX_DYN_CMD_SIZE)
688 		return -ERANGE;
689 	if (!ops->cmd_packing)
690 		return -EOPNOTSUPP;
691 	if (!ops->entry_packing)
692 		return -EOPNOTSUPP;
693 
694 	cmd.valid = true; /* Trigger action on table entry */
695 	cmd.rdwrset = SPI_READ; /* Action is read */
696 	if (index < 0) {
697 		/* Avoid copying a signed negative number to an u64 */
698 		cmd.index = 0;
699 		cmd.search = true;
700 	} else {
701 		cmd.index = index;
702 		cmd.search = false;
703 	}
704 	cmd.valident = true;
705 	ops->cmd_packing(packed_buf, &cmd, PACK);
706 
707 	if (cmd.search)
708 		ops->entry_packing(packed_buf, entry, PACK);
709 
710 	/* Send SPI write operation: read config table entry */
711 	rc = sja1105_xfer_buf(priv, SPI_WRITE, ops->addr, packed_buf,
712 			      ops->packed_size);
713 	if (rc < 0)
714 		return rc;
715 
716 	/* Loop until we have confirmation that hardware has finished
717 	 * processing the command and has cleared the VALID field
718 	 */
719 	do {
720 		memset(packed_buf, 0, ops->packed_size);
721 
722 		/* Retrieve the read operation's result */
723 		rc = sja1105_xfer_buf(priv, SPI_READ, ops->addr, packed_buf,
724 				      ops->packed_size);
725 		if (rc < 0)
726 			return rc;
727 
728 		cmd = (struct sja1105_dyn_cmd) {0};
729 		ops->cmd_packing(packed_buf, &cmd, UNPACK);
730 		/* UM10944: [valident] will always be found cleared
731 		 * during a read access with MGMTROUTE set.
732 		 * So don't error out in that case.
733 		 */
734 		if (!cmd.valident && blk_idx != BLK_IDX_MGMT_ROUTE)
735 			return -ENOENT;
736 		cpu_relax();
737 	} while (cmd.valid && --retries);
738 
739 	if (cmd.valid)
740 		return -ETIMEDOUT;
741 
742 	/* Don't dereference possibly NULL pointer - maybe caller
743 	 * only wanted to see whether the entry existed or not.
744 	 */
745 	if (entry)
746 		ops->entry_packing(packed_buf, entry, UNPACK);
747 	return 0;
748 }
749 
750 int sja1105_dynamic_config_write(struct sja1105_private *priv,
751 				 enum sja1105_blk_idx blk_idx,
752 				 int index, void *entry, bool keep)
753 {
754 	const struct sja1105_dynamic_table_ops *ops;
755 	struct sja1105_dyn_cmd cmd = {0};
756 	/* SPI payload buffer */
757 	u8 packed_buf[SJA1105_MAX_DYN_CMD_SIZE] = {0};
758 	int rc;
759 
760 	if (blk_idx >= BLK_IDX_MAX_DYN)
761 		return -ERANGE;
762 
763 	ops = &priv->info->dyn_ops[blk_idx];
764 
765 	if (index >= ops->max_entry_count)
766 		return -ERANGE;
767 	if (index < 0)
768 		return -ERANGE;
769 	if (!(ops->access & OP_WRITE))
770 		return -EOPNOTSUPP;
771 	if (!keep && !(ops->access & OP_DEL))
772 		return -EOPNOTSUPP;
773 	if (ops->packed_size > SJA1105_MAX_DYN_CMD_SIZE)
774 		return -ERANGE;
775 
776 	cmd.valident = keep; /* If false, deletes entry */
777 	cmd.valid = true; /* Trigger action on table entry */
778 	cmd.rdwrset = SPI_WRITE; /* Action is write */
779 	cmd.index = index;
780 
781 	if (!ops->cmd_packing)
782 		return -EOPNOTSUPP;
783 	ops->cmd_packing(packed_buf, &cmd, PACK);
784 
785 	if (!ops->entry_packing)
786 		return -EOPNOTSUPP;
787 	/* Don't dereference potentially NULL pointer if just
788 	 * deleting a table entry is what was requested. For cases
789 	 * where 'index' field is physically part of entry structure,
790 	 * and needed here, we deal with that in the cmd_packing callback.
791 	 */
792 	if (keep)
793 		ops->entry_packing(packed_buf, entry, PACK);
794 
795 	/* Send SPI write operation: read config table entry */
796 	rc = sja1105_xfer_buf(priv, SPI_WRITE, ops->addr, packed_buf,
797 			      ops->packed_size);
798 	if (rc < 0)
799 		return rc;
800 
801 	cmd = (struct sja1105_dyn_cmd) {0};
802 	ops->cmd_packing(packed_buf, &cmd, UNPACK);
803 	if (cmd.errors)
804 		return -EINVAL;
805 
806 	return 0;
807 }
808 
809 static u8 sja1105_crc8_add(u8 crc, u8 byte, u8 poly)
810 {
811 	int i;
812 
813 	for (i = 0; i < 8; i++) {
814 		if ((crc ^ byte) & (1 << 7)) {
815 			crc <<= 1;
816 			crc ^= poly;
817 		} else {
818 			crc <<= 1;
819 		}
820 		byte <<= 1;
821 	}
822 	return crc;
823 }
824 
825 /* CRC8 algorithm with non-reversed input, non-reversed output,
826  * no input xor and no output xor. Code customized for receiving
827  * the SJA1105 E/T FDB keys (vlanid, macaddr) as input. CRC polynomial
828  * is also received as argument in the Koopman notation that the switch
829  * hardware stores it in.
830  */
831 u8 sja1105et_fdb_hash(struct sja1105_private *priv, const u8 *addr, u16 vid)
832 {
833 	struct sja1105_l2_lookup_params_entry *l2_lookup_params =
834 		priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS].entries;
835 	u64 poly_koopman = l2_lookup_params->poly;
836 	/* Convert polynomial from Koopman to 'normal' notation */
837 	u8 poly = (u8)(1 + (poly_koopman << 1));
838 	u64 vlanid = l2_lookup_params->shared_learn ? 0 : vid;
839 	u64 input = (vlanid << 48) | ether_addr_to_u64(addr);
840 	u8 crc = 0; /* seed */
841 	int i;
842 
843 	/* Mask the eight bytes starting from MSB one at a time */
844 	for (i = 56; i >= 0; i -= 8) {
845 		u8 byte = (input & (0xffull << i)) >> i;
846 
847 		crc = sja1105_crc8_add(crc, byte, poly);
848 	}
849 	return crc;
850 }
851