1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
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.
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
18 * Compared to the struct sja1105_table_ops from sja1105_static_config.c,
19 * the dynamic accessors work with a compound buffer:
25 * +-----------------------------------------+------------------+
26 * | ENTRY BUFFER | COMMAND BUFFER |
27 * +-----------------------------------------+------------------+
29 * <----------------------- packed_size ------------------------>
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).
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
41 * The COMMAND BUFFER is always SJA1105_SIZE_DYN_CMD bytes (one 32-bit word) in
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.
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
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
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
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
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
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.
98 #define SJA1105_SIZE_DYN_CMD 4
100 #define SJA1105ET_SIZE_VL_LOOKUP_DYN_CMD \
103 #define SJA1105PQRS_SIZE_VL_LOOKUP_DYN_CMD \
104 (SJA1105_SIZE_DYN_CMD + SJA1105_SIZE_VL_LOOKUP_ENTRY)
106 #define SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY \
109 #define SJA1105ET_SIZE_L2_LOOKUP_DYN_CMD \
110 (SJA1105_SIZE_DYN_CMD + SJA1105ET_SIZE_L2_LOOKUP_ENTRY)
112 #define SJA1105PQRS_SIZE_L2_LOOKUP_DYN_CMD \
113 (SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY)
115 #define SJA1105_SIZE_VLAN_LOOKUP_DYN_CMD \
116 (SJA1105_SIZE_DYN_CMD + 4 + SJA1105_SIZE_VLAN_LOOKUP_ENTRY)
118 #define SJA1105_SIZE_L2_FORWARDING_DYN_CMD \
119 (SJA1105_SIZE_DYN_CMD + SJA1105_SIZE_L2_FORWARDING_ENTRY)
121 #define SJA1105ET_SIZE_MAC_CONFIG_DYN_CMD \
122 (SJA1105_SIZE_DYN_CMD + SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY)
124 #define SJA1105PQRS_SIZE_MAC_CONFIG_DYN_CMD \
125 (SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY)
127 #define SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD \
130 #define SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_DYN_CMD \
131 (SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY)
133 #define SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD \
136 #define SJA1105PQRS_SIZE_GENERAL_PARAMS_DYN_CMD \
137 (SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY)
139 #define SJA1105PQRS_SIZE_AVB_PARAMS_DYN_CMD \
140 (SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY)
142 #define SJA1105_SIZE_RETAGGING_DYN_CMD \
143 (SJA1105_SIZE_DYN_CMD + SJA1105_SIZE_RETAGGING_ENTRY)
145 #define SJA1105ET_SIZE_CBS_DYN_CMD \
146 (SJA1105_SIZE_DYN_CMD + SJA1105ET_SIZE_CBS_ENTRY)
148 #define SJA1105PQRS_SIZE_CBS_DYN_CMD \
149 (SJA1105_SIZE_DYN_CMD + SJA1105PQRS_SIZE_CBS_ENTRY)
151 #define SJA1105_MAX_DYN_CMD_SIZE \
152 SJA1105PQRS_SIZE_GENERAL_PARAMS_DYN_CMD
154 struct sja1105_dyn_cmd {
163 enum sja1105_hostcmd {
164 SJA1105_HOSTCMD_SEARCH = 1,
165 SJA1105_HOSTCMD_READ = 2,
166 SJA1105_HOSTCMD_WRITE = 3,
167 SJA1105_HOSTCMD_INVALIDATE = 4,
170 /* Command and entry overlap */
172 sja1105et_vl_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
175 const int size = SJA1105_SIZE_DYN_CMD;
177 sja1105_packing(buf, &cmd->valid, 31, 31, size, op);
178 sja1105_packing(buf, &cmd->errors, 30, 30, size, op);
179 sja1105_packing(buf, &cmd->rdwrset, 29, 29, size, op);
180 sja1105_packing(buf, &cmd->index, 9, 0, size, op);
183 /* Command and entry are separate */
185 sja1105pqrs_vl_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
188 u8 *p = buf + SJA1105_SIZE_VL_LOOKUP_ENTRY;
189 const int size = SJA1105_SIZE_DYN_CMD;
191 sja1105_packing(p, &cmd->valid, 31, 31, size, op);
192 sja1105_packing(p, &cmd->errors, 30, 30, size, op);
193 sja1105_packing(p, &cmd->rdwrset, 29, 29, size, op);
194 sja1105_packing(p, &cmd->index, 9, 0, size, op);
197 static size_t sja1105et_vl_lookup_entry_packing(void *buf, void *entry_ptr,
200 struct sja1105_vl_lookup_entry *entry = entry_ptr;
201 const int size = SJA1105ET_SIZE_VL_LOOKUP_DYN_CMD;
203 sja1105_packing(buf, &entry->egrmirr, 21, 17, size, op);
204 sja1105_packing(buf, &entry->ingrmirr, 16, 16, size, op);
209 sja1105pqrs_l2_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
212 u8 *p = buf + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
213 const int size = SJA1105_SIZE_DYN_CMD;
216 sja1105_packing(p, &cmd->valid, 31, 31, size, op);
217 sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
218 sja1105_packing(p, &cmd->errors, 29, 29, size, op);
219 sja1105_packing(p, &cmd->valident, 27, 27, size, op);
221 /* VALIDENT is supposed to indicate "keep or not", but in SJA1105 E/T,
222 * using it to delete a management route was unsupported. UM10944
225 * In case of a write access with the MGMTROUTE flag set,
226 * the flag will be ignored. It will always be found cleared
227 * for read accesses with the MGMTROUTE flag set.
229 * SJA1105 P/Q/R/S keeps the same behavior w.r.t. VALIDENT, but there
230 * is now another flag called HOSTCMD which does more stuff (quoting
233 * A write request is accepted only when HOSTCMD is set to write host
234 * or invalid. A read request is accepted only when HOSTCMD is set to
235 * search host or read host.
237 * So it is possible to translate a RDWRSET/VALIDENT combination into
238 * HOSTCMD so that we keep the dynamic command API in place, and
239 * at the same time achieve compatibility with the management route
242 if (cmd->rdwrset == SPI_READ) {
244 hostcmd = SJA1105_HOSTCMD_SEARCH;
246 hostcmd = SJA1105_HOSTCMD_READ;
250 hostcmd = SJA1105_HOSTCMD_WRITE;
252 hostcmd = SJA1105_HOSTCMD_INVALIDATE;
254 sja1105_packing(p, &hostcmd, 25, 23, size, op);
256 /* Hack - The hardware takes the 'index' field within
257 * struct sja1105_l2_lookup_entry as the index on which this command
258 * will operate. However it will ignore everything else, so 'index'
259 * is logically part of command but physically part of entry.
260 * Populate the 'index' entry field from within the command callback,
261 * such that our API doesn't need to ask for a full-blown entry
262 * structure when e.g. a delete is requested.
264 sja1105_packing(buf, &cmd->index, 15, 6,
265 SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY, op);
268 /* The switch is so retarded that it makes our command/entry abstraction
271 * On P/Q/R/S, the switch tries to say whether a FDB entry
272 * is statically programmed or dynamically learned via a flag called LOCKEDS.
273 * The hardware manual says about this fiels:
275 * On write will specify the format of ENTRY.
276 * On read the flag will be found cleared at times the VALID flag is found
277 * set. The flag will also be found cleared in response to a read having the
278 * MGMTROUTE flag set. In response to a read with the MGMTROUTE flag
279 * cleared, the flag be set if the most recent access operated on an entry
280 * that was either loaded by configuration or through dynamic reconfiguration
281 * (as opposed to automatically learned entries).
283 * The trouble with this flag is that it's part of the *command* to access the
284 * dynamic interface, and not part of the *entry* retrieved from it.
285 * Otherwise said, for a sja1105_dynamic_config_read, LOCKEDS is supposed to be
286 * an output from the switch into the command buffer, and for a
287 * sja1105_dynamic_config_write, the switch treats LOCKEDS as an input
288 * (hence we can write either static, or automatically learned entries, from
290 * But the manual contradicts itself in the last phrase where it says that on
291 * read, LOCKEDS will be set to 1 for all FDB entries written through the
292 * dynamic interface (therefore, the value of LOCKEDS from the
293 * sja1105_dynamic_config_write is not really used for anything, it'll store a
295 * This means you can't really write a FDB entry with LOCKEDS=0 (automatically
296 * learned) into the switch, which kind of makes sense.
297 * As for reading through the dynamic interface, it doesn't make too much sense
298 * to put LOCKEDS into the command, since the switch will inevitably have to
299 * ignore it (otherwise a command would be like "read the FDB entry 123, but
300 * only if it's dynamically learned" <- well how am I supposed to know?) and
301 * just use it as an output buffer for its findings. But guess what... that's
302 * what the entry buffer is for!
303 * Unfortunately, what really breaks this abstraction is the fact that it
304 * wasn't designed having the fact in mind that the switch can output
305 * entry-related data as writeback through the command buffer.
306 * However, whether a FDB entry is statically or dynamically learned *is* part
307 * of the entry and not the command data, no matter what the switch thinks.
308 * In order to do that, we'll need to wrap around the
309 * sja1105pqrs_l2_lookup_entry_packing from sja1105_static_config.c, and take
310 * a peek outside of the caller-supplied @buf (the entry buffer), to reach the
314 sja1105pqrs_dyn_l2_lookup_entry_packing(void *buf, void *entry_ptr,
317 struct sja1105_l2_lookup_entry *entry = entry_ptr;
318 u8 *cmd = buf + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
319 const int size = SJA1105_SIZE_DYN_CMD;
321 sja1105_packing(cmd, &entry->lockeds, 28, 28, size, op);
323 return sja1105pqrs_l2_lookup_entry_packing(buf, entry_ptr, op);
327 sja1105et_l2_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
330 u8 *p = buf + SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
331 const int size = SJA1105_SIZE_DYN_CMD;
333 sja1105_packing(p, &cmd->valid, 31, 31, size, op);
334 sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
335 sja1105_packing(p, &cmd->errors, 29, 29, size, op);
336 sja1105_packing(p, &cmd->valident, 27, 27, size, op);
337 /* Hack - see comments above. */
338 sja1105_packing(buf, &cmd->index, 29, 20,
339 SJA1105ET_SIZE_L2_LOOKUP_ENTRY, op);
342 static size_t sja1105et_dyn_l2_lookup_entry_packing(void *buf, void *entry_ptr,
345 struct sja1105_l2_lookup_entry *entry = entry_ptr;
346 u8 *cmd = buf + SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
347 const int size = SJA1105_SIZE_DYN_CMD;
349 sja1105_packing(cmd, &entry->lockeds, 28, 28, size, op);
351 return sja1105et_l2_lookup_entry_packing(buf, entry_ptr, op);
355 sja1105et_mgmt_route_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
358 u8 *p = buf + SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
361 sja1105et_l2_lookup_cmd_packing(buf, cmd, op);
363 sja1105_pack(p, &mgmtroute, 26, 26, SJA1105_SIZE_DYN_CMD);
366 static size_t sja1105et_mgmt_route_entry_packing(void *buf, void *entry_ptr,
369 struct sja1105_mgmt_entry *entry = entry_ptr;
370 const size_t size = SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
372 /* UM10944: To specify if a PTP egress timestamp shall be captured on
373 * each port upon transmission of the frame, the LSB of VLANID in the
374 * ENTRY field provided by the host must be set.
375 * Bit 1 of VLANID then specifies the register where the timestamp for
376 * this port is stored in.
378 sja1105_packing(buf, &entry->tsreg, 85, 85, size, op);
379 sja1105_packing(buf, &entry->takets, 84, 84, size, op);
380 sja1105_packing(buf, &entry->macaddr, 83, 36, size, op);
381 sja1105_packing(buf, &entry->destports, 35, 31, size, op);
382 sja1105_packing(buf, &entry->enfport, 30, 30, size, op);
387 sja1105pqrs_mgmt_route_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
390 u8 *p = buf + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
393 sja1105pqrs_l2_lookup_cmd_packing(buf, cmd, op);
395 sja1105_pack(p, &mgmtroute, 26, 26, SJA1105_SIZE_DYN_CMD);
398 static size_t sja1105pqrs_mgmt_route_entry_packing(void *buf, void *entry_ptr,
401 const size_t size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
402 struct sja1105_mgmt_entry *entry = entry_ptr;
404 /* In P/Q/R/S, enfport got renamed to mgmtvalid, but its purpose
405 * is the same (driver uses it to confirm that frame was sent).
406 * So just keep the name from E/T.
408 sja1105_packing(buf, &entry->tsreg, 71, 71, size, op);
409 sja1105_packing(buf, &entry->takets, 70, 70, size, op);
410 sja1105_packing(buf, &entry->macaddr, 69, 22, size, op);
411 sja1105_packing(buf, &entry->destports, 21, 17, size, op);
412 sja1105_packing(buf, &entry->enfport, 16, 16, size, op);
416 /* In E/T, entry is at addresses 0x27-0x28. There is a 4 byte gap at 0x29,
417 * and command is at 0x2a. Similarly in P/Q/R/S there is a 1 register gap
418 * between entry (0x2d, 0x2e) and command (0x30).
421 sja1105_vlan_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
424 u8 *p = buf + SJA1105_SIZE_VLAN_LOOKUP_ENTRY + 4;
425 const int size = SJA1105_SIZE_DYN_CMD;
427 sja1105_packing(p, &cmd->valid, 31, 31, size, op);
428 sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
429 sja1105_packing(p, &cmd->valident, 27, 27, size, op);
430 /* Hack - see comments above, applied for 'vlanid' field of
431 * struct sja1105_vlan_lookup_entry.
433 sja1105_packing(buf, &cmd->index, 38, 27,
434 SJA1105_SIZE_VLAN_LOOKUP_ENTRY, op);
438 sja1105_l2_forwarding_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
441 u8 *p = buf + SJA1105_SIZE_L2_FORWARDING_ENTRY;
442 const int size = SJA1105_SIZE_DYN_CMD;
444 sja1105_packing(p, &cmd->valid, 31, 31, size, op);
445 sja1105_packing(p, &cmd->errors, 30, 30, size, op);
446 sja1105_packing(p, &cmd->rdwrset, 29, 29, size, op);
447 sja1105_packing(p, &cmd->index, 4, 0, size, op);
451 sja1105et_mac_config_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
454 const int size = SJA1105_SIZE_DYN_CMD;
455 /* Yup, user manual definitions are reversed */
458 sja1105_packing(reg1, &cmd->valid, 31, 31, size, op);
459 sja1105_packing(reg1, &cmd->index, 26, 24, size, op);
462 static size_t sja1105et_mac_config_entry_packing(void *buf, void *entry_ptr,
465 const int size = SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY;
466 struct sja1105_mac_config_entry *entry = entry_ptr;
467 /* Yup, user manual definitions are reversed */
471 sja1105_packing(reg1, &entry->speed, 30, 29, size, op);
472 sja1105_packing(reg1, &entry->drpdtag, 23, 23, size, op);
473 sja1105_packing(reg1, &entry->drpuntag, 22, 22, size, op);
474 sja1105_packing(reg1, &entry->retag, 21, 21, size, op);
475 sja1105_packing(reg1, &entry->dyn_learn, 20, 20, size, op);
476 sja1105_packing(reg1, &entry->egress, 19, 19, size, op);
477 sja1105_packing(reg1, &entry->ingress, 18, 18, size, op);
478 sja1105_packing(reg1, &entry->ing_mirr, 17, 17, size, op);
479 sja1105_packing(reg1, &entry->egr_mirr, 16, 16, size, op);
480 sja1105_packing(reg1, &entry->vlanprio, 14, 12, size, op);
481 sja1105_packing(reg1, &entry->vlanid, 11, 0, size, op);
482 sja1105_packing(reg2, &entry->tp_delin, 31, 16, size, op);
483 sja1105_packing(reg2, &entry->tp_delout, 15, 0, size, op);
484 /* MAC configuration table entries which can't be reconfigured:
485 * top, base, enabled, ifg, maxage, drpnona664
487 /* Bogus return value, not used anywhere */
492 sja1105pqrs_mac_config_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
495 const int size = SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY;
496 u8 *p = buf + SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY;
498 sja1105_packing(p, &cmd->valid, 31, 31, size, op);
499 sja1105_packing(p, &cmd->errors, 30, 30, size, op);
500 sja1105_packing(p, &cmd->rdwrset, 29, 29, size, op);
501 sja1105_packing(p, &cmd->index, 2, 0, size, op);
505 sja1105et_l2_lookup_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
508 sja1105_packing(buf, &cmd->valid, 31, 31,
509 SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD, op);
513 sja1105et_l2_lookup_params_entry_packing(void *buf, void *entry_ptr,
516 struct sja1105_l2_lookup_params_entry *entry = entry_ptr;
518 sja1105_packing(buf, &entry->poly, 7, 0,
519 SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD, op);
520 /* Bogus return value, not used anywhere */
525 sja1105pqrs_l2_lookup_params_cmd_packing(void *buf,
526 struct sja1105_dyn_cmd *cmd,
529 u8 *p = buf + SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY;
530 const int size = SJA1105_SIZE_DYN_CMD;
532 sja1105_packing(p, &cmd->valid, 31, 31, size, op);
533 sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
537 sja1105et_general_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
540 const int size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD;
542 sja1105_packing(buf, &cmd->valid, 31, 31, size, op);
543 sja1105_packing(buf, &cmd->errors, 30, 30, size, op);
547 sja1105et_general_params_entry_packing(void *buf, void *entry_ptr,
550 struct sja1105_general_params_entry *entry = entry_ptr;
551 const int size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD;
553 sja1105_packing(buf, &entry->mirr_port, 2, 0, size, op);
554 /* Bogus return value, not used anywhere */
559 sja1105pqrs_general_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
562 u8 *p = buf + SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY;
563 const int size = SJA1105_SIZE_DYN_CMD;
565 sja1105_packing(p, &cmd->valid, 31, 31, size, op);
566 sja1105_packing(p, &cmd->errors, 30, 30, size, op);
567 sja1105_packing(p, &cmd->rdwrset, 28, 28, size, op);
571 sja1105pqrs_avb_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
574 u8 *p = buf + SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY;
575 const int size = SJA1105_SIZE_DYN_CMD;
577 sja1105_packing(p, &cmd->valid, 31, 31, size, op);
578 sja1105_packing(p, &cmd->errors, 30, 30, size, op);
579 sja1105_packing(p, &cmd->rdwrset, 29, 29, size, op);
583 sja1105_retagging_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
586 u8 *p = buf + SJA1105_SIZE_RETAGGING_ENTRY;
587 const int size = SJA1105_SIZE_DYN_CMD;
589 sja1105_packing(p, &cmd->valid, 31, 31, size, op);
590 sja1105_packing(p, &cmd->errors, 30, 30, size, op);
591 sja1105_packing(p, &cmd->valident, 29, 29, size, op);
592 sja1105_packing(p, &cmd->rdwrset, 28, 28, size, op);
593 sja1105_packing(p, &cmd->index, 5, 0, size, op);
596 static void sja1105et_cbs_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
599 u8 *p = buf + SJA1105ET_SIZE_CBS_ENTRY;
600 const int size = SJA1105_SIZE_DYN_CMD;
602 sja1105_packing(p, &cmd->valid, 31, 31, size, op);
603 sja1105_packing(p, &cmd->index, 19, 16, size, op);
606 static size_t sja1105et_cbs_entry_packing(void *buf, void *entry_ptr,
609 const size_t size = SJA1105ET_SIZE_CBS_ENTRY;
610 struct sja1105_cbs_entry *entry = entry_ptr;
611 u8 *cmd = buf + size;
614 sja1105_packing(cmd, &entry->port, 5, 3, SJA1105_SIZE_DYN_CMD, op);
615 sja1105_packing(cmd, &entry->prio, 2, 0, SJA1105_SIZE_DYN_CMD, op);
616 sja1105_packing(p + 3, &entry->credit_lo, 31, 0, size, op);
617 sja1105_packing(p + 2, &entry->credit_hi, 31, 0, size, op);
618 sja1105_packing(p + 1, &entry->send_slope, 31, 0, size, op);
619 sja1105_packing(p + 0, &entry->idle_slope, 31, 0, size, op);
623 static void sja1105pqrs_cbs_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
626 u8 *p = buf + SJA1105PQRS_SIZE_CBS_ENTRY;
627 const int size = SJA1105_SIZE_DYN_CMD;
629 sja1105_packing(p, &cmd->valid, 31, 31, size, op);
630 sja1105_packing(p, &cmd->rdwrset, 30, 30, size, op);
631 sja1105_packing(p, &cmd->errors, 29, 29, size, op);
632 sja1105_packing(p, &cmd->index, 3, 0, size, op);
635 static size_t sja1105pqrs_cbs_entry_packing(void *buf, void *entry_ptr,
638 const size_t size = SJA1105PQRS_SIZE_CBS_ENTRY;
639 struct sja1105_cbs_entry *entry = entry_ptr;
641 sja1105_packing(buf, &entry->port, 159, 157, size, op);
642 sja1105_packing(buf, &entry->prio, 156, 154, size, op);
643 sja1105_packing(buf, &entry->credit_lo, 153, 122, size, op);
644 sja1105_packing(buf, &entry->credit_hi, 121, 90, size, op);
645 sja1105_packing(buf, &entry->send_slope, 89, 58, size, op);
646 sja1105_packing(buf, &entry->idle_slope, 57, 26, size, op);
650 #define OP_READ BIT(0)
651 #define OP_WRITE BIT(1)
652 #define OP_DEL BIT(2)
653 #define OP_SEARCH BIT(3)
655 /* SJA1105E/T: First generation */
656 const struct sja1105_dynamic_table_ops sja1105et_dyn_ops[BLK_IDX_MAX_DYN] = {
657 [BLK_IDX_VL_LOOKUP] = {
658 .entry_packing = sja1105et_vl_lookup_entry_packing,
659 .cmd_packing = sja1105et_vl_lookup_cmd_packing,
661 .max_entry_count = SJA1105_MAX_VL_LOOKUP_COUNT,
662 .packed_size = SJA1105ET_SIZE_VL_LOOKUP_DYN_CMD,
665 [BLK_IDX_L2_LOOKUP] = {
666 .entry_packing = sja1105et_dyn_l2_lookup_entry_packing,
667 .cmd_packing = sja1105et_l2_lookup_cmd_packing,
668 .access = (OP_READ | OP_WRITE | OP_DEL),
669 .max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
670 .packed_size = SJA1105ET_SIZE_L2_LOOKUP_DYN_CMD,
673 [BLK_IDX_MGMT_ROUTE] = {
674 .entry_packing = sja1105et_mgmt_route_entry_packing,
675 .cmd_packing = sja1105et_mgmt_route_cmd_packing,
676 .access = (OP_READ | OP_WRITE),
677 .max_entry_count = SJA1105_NUM_PORTS,
678 .packed_size = SJA1105ET_SIZE_L2_LOOKUP_DYN_CMD,
681 [BLK_IDX_VLAN_LOOKUP] = {
682 .entry_packing = sja1105_vlan_lookup_entry_packing,
683 .cmd_packing = sja1105_vlan_lookup_cmd_packing,
684 .access = (OP_WRITE | OP_DEL),
685 .max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
686 .packed_size = SJA1105_SIZE_VLAN_LOOKUP_DYN_CMD,
689 [BLK_IDX_L2_FORWARDING] = {
690 .entry_packing = sja1105_l2_forwarding_entry_packing,
691 .cmd_packing = sja1105_l2_forwarding_cmd_packing,
692 .max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
694 .packed_size = SJA1105_SIZE_L2_FORWARDING_DYN_CMD,
697 [BLK_IDX_MAC_CONFIG] = {
698 .entry_packing = sja1105et_mac_config_entry_packing,
699 .cmd_packing = sja1105et_mac_config_cmd_packing,
700 .max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
702 .packed_size = SJA1105ET_SIZE_MAC_CONFIG_DYN_CMD,
705 [BLK_IDX_L2_LOOKUP_PARAMS] = {
706 .entry_packing = sja1105et_l2_lookup_params_entry_packing,
707 .cmd_packing = sja1105et_l2_lookup_params_cmd_packing,
708 .max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
710 .packed_size = SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD,
713 [BLK_IDX_GENERAL_PARAMS] = {
714 .entry_packing = sja1105et_general_params_entry_packing,
715 .cmd_packing = sja1105et_general_params_cmd_packing,
716 .max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
718 .packed_size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD,
721 [BLK_IDX_RETAGGING] = {
722 .entry_packing = sja1105_retagging_entry_packing,
723 .cmd_packing = sja1105_retagging_cmd_packing,
724 .max_entry_count = SJA1105_MAX_RETAGGING_COUNT,
725 .access = (OP_WRITE | OP_DEL),
726 .packed_size = SJA1105_SIZE_RETAGGING_DYN_CMD,
730 .entry_packing = sja1105et_cbs_entry_packing,
731 .cmd_packing = sja1105et_cbs_cmd_packing,
732 .max_entry_count = SJA1105ET_MAX_CBS_COUNT,
734 .packed_size = SJA1105ET_SIZE_CBS_DYN_CMD,
739 /* SJA1105P/Q/R/S: Second generation */
740 const struct sja1105_dynamic_table_ops sja1105pqrs_dyn_ops[BLK_IDX_MAX_DYN] = {
741 [BLK_IDX_VL_LOOKUP] = {
742 .entry_packing = sja1105_vl_lookup_entry_packing,
743 .cmd_packing = sja1105pqrs_vl_lookup_cmd_packing,
744 .access = (OP_READ | OP_WRITE),
745 .max_entry_count = SJA1105_MAX_VL_LOOKUP_COUNT,
746 .packed_size = SJA1105PQRS_SIZE_VL_LOOKUP_DYN_CMD,
749 [BLK_IDX_L2_LOOKUP] = {
750 .entry_packing = sja1105pqrs_dyn_l2_lookup_entry_packing,
751 .cmd_packing = sja1105pqrs_l2_lookup_cmd_packing,
752 .access = (OP_READ | OP_WRITE | OP_DEL | OP_SEARCH),
753 .max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
754 .packed_size = SJA1105PQRS_SIZE_L2_LOOKUP_DYN_CMD,
757 [BLK_IDX_MGMT_ROUTE] = {
758 .entry_packing = sja1105pqrs_mgmt_route_entry_packing,
759 .cmd_packing = sja1105pqrs_mgmt_route_cmd_packing,
760 .access = (OP_READ | OP_WRITE | OP_DEL | OP_SEARCH),
761 .max_entry_count = SJA1105_NUM_PORTS,
762 .packed_size = SJA1105PQRS_SIZE_L2_LOOKUP_DYN_CMD,
765 [BLK_IDX_VLAN_LOOKUP] = {
766 .entry_packing = sja1105_vlan_lookup_entry_packing,
767 .cmd_packing = sja1105_vlan_lookup_cmd_packing,
768 .access = (OP_READ | OP_WRITE | OP_DEL),
769 .max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
770 .packed_size = SJA1105_SIZE_VLAN_LOOKUP_DYN_CMD,
773 [BLK_IDX_L2_FORWARDING] = {
774 .entry_packing = sja1105_l2_forwarding_entry_packing,
775 .cmd_packing = sja1105_l2_forwarding_cmd_packing,
776 .max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
778 .packed_size = SJA1105_SIZE_L2_FORWARDING_DYN_CMD,
781 [BLK_IDX_MAC_CONFIG] = {
782 .entry_packing = sja1105pqrs_mac_config_entry_packing,
783 .cmd_packing = sja1105pqrs_mac_config_cmd_packing,
784 .max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
785 .access = (OP_READ | OP_WRITE),
786 .packed_size = SJA1105PQRS_SIZE_MAC_CONFIG_DYN_CMD,
789 [BLK_IDX_L2_LOOKUP_PARAMS] = {
790 .entry_packing = sja1105pqrs_l2_lookup_params_entry_packing,
791 .cmd_packing = sja1105pqrs_l2_lookup_params_cmd_packing,
792 .max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
793 .access = (OP_READ | OP_WRITE),
794 .packed_size = SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_DYN_CMD,
797 [BLK_IDX_AVB_PARAMS] = {
798 .entry_packing = sja1105pqrs_avb_params_entry_packing,
799 .cmd_packing = sja1105pqrs_avb_params_cmd_packing,
800 .max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT,
801 .access = (OP_READ | OP_WRITE),
802 .packed_size = SJA1105PQRS_SIZE_AVB_PARAMS_DYN_CMD,
805 [BLK_IDX_GENERAL_PARAMS] = {
806 .entry_packing = sja1105pqrs_general_params_entry_packing,
807 .cmd_packing = sja1105pqrs_general_params_cmd_packing,
808 .max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
809 .access = (OP_READ | OP_WRITE),
810 .packed_size = SJA1105PQRS_SIZE_GENERAL_PARAMS_DYN_CMD,
813 [BLK_IDX_RETAGGING] = {
814 .entry_packing = sja1105_retagging_entry_packing,
815 .cmd_packing = sja1105_retagging_cmd_packing,
816 .max_entry_count = SJA1105_MAX_RETAGGING_COUNT,
817 .access = (OP_READ | OP_WRITE | OP_DEL),
818 .packed_size = SJA1105_SIZE_RETAGGING_DYN_CMD,
822 .entry_packing = sja1105pqrs_cbs_entry_packing,
823 .cmd_packing = sja1105pqrs_cbs_cmd_packing,
824 .max_entry_count = SJA1105PQRS_MAX_CBS_COUNT,
826 .packed_size = SJA1105PQRS_SIZE_CBS_DYN_CMD,
831 /* Provides read access to the settings through the dynamic interface
833 * @blk_idx is used as key to select from the sja1105_dynamic_table_ops.
834 * The selection is limited by the hardware in respect to which
835 * configuration blocks can be read through the dynamic interface.
836 * @index is used to retrieve a particular table entry. If negative,
837 * (and if the @blk_idx supports the searching operation) a search
838 * is performed by the @entry parameter.
839 * @entry Type-casted to an unpacked structure that holds a table entry
840 * of the type specified in @blk_idx.
841 * Usually an output argument. If @index is negative, then this
842 * argument is used as input/output: it should be pre-populated
843 * with the element to search for. Entries which support the
844 * search operation will have an "index" field (not the @index
845 * argument to this function) and that is where the found index
846 * will be returned (or left unmodified - thus negative - if not
849 int sja1105_dynamic_config_read(struct sja1105_private *priv,
850 enum sja1105_blk_idx blk_idx,
851 int index, void *entry)
853 const struct sja1105_dynamic_table_ops *ops;
854 struct sja1105_dyn_cmd cmd = {0};
855 /* SPI payload buffer */
856 u8 packed_buf[SJA1105_MAX_DYN_CMD_SIZE] = {0};
860 if (blk_idx >= BLK_IDX_MAX_DYN)
863 ops = &priv->info->dyn_ops[blk_idx];
865 if (index >= 0 && index >= ops->max_entry_count)
867 if (index < 0 && !(ops->access & OP_SEARCH))
869 if (!(ops->access & OP_READ))
871 if (ops->packed_size > SJA1105_MAX_DYN_CMD_SIZE)
873 if (!ops->cmd_packing)
875 if (!ops->entry_packing)
878 cmd.valid = true; /* Trigger action on table entry */
879 cmd.rdwrset = SPI_READ; /* Action is read */
881 /* Avoid copying a signed negative number to an u64 */
889 ops->cmd_packing(packed_buf, &cmd, PACK);
892 ops->entry_packing(packed_buf, entry, PACK);
894 /* Send SPI write operation: read config table entry */
895 rc = sja1105_xfer_buf(priv, SPI_WRITE, ops->addr, packed_buf,
900 /* Loop until we have confirmation that hardware has finished
901 * processing the command and has cleared the VALID field
904 memset(packed_buf, 0, ops->packed_size);
906 /* Retrieve the read operation's result */
907 rc = sja1105_xfer_buf(priv, SPI_READ, ops->addr, packed_buf,
912 cmd = (struct sja1105_dyn_cmd) {0};
913 ops->cmd_packing(packed_buf, &cmd, UNPACK);
914 /* UM10944: [valident] will always be found cleared
915 * during a read access with MGMTROUTE set.
916 * So don't error out in that case.
918 if (!cmd.valident && blk_idx != BLK_IDX_MGMT_ROUTE)
921 } while (cmd.valid && --retries);
926 /* Don't dereference possibly NULL pointer - maybe caller
927 * only wanted to see whether the entry existed or not.
930 ops->entry_packing(packed_buf, entry, UNPACK);
934 int sja1105_dynamic_config_write(struct sja1105_private *priv,
935 enum sja1105_blk_idx blk_idx,
936 int index, void *entry, bool keep)
938 const struct sja1105_dynamic_table_ops *ops;
939 struct sja1105_dyn_cmd cmd = {0};
940 /* SPI payload buffer */
941 u8 packed_buf[SJA1105_MAX_DYN_CMD_SIZE] = {0};
944 if (blk_idx >= BLK_IDX_MAX_DYN)
947 ops = &priv->info->dyn_ops[blk_idx];
949 if (index >= ops->max_entry_count)
953 if (!(ops->access & OP_WRITE))
955 if (!keep && !(ops->access & OP_DEL))
957 if (ops->packed_size > SJA1105_MAX_DYN_CMD_SIZE)
960 cmd.valident = keep; /* If false, deletes entry */
961 cmd.valid = true; /* Trigger action on table entry */
962 cmd.rdwrset = SPI_WRITE; /* Action is write */
965 if (!ops->cmd_packing)
967 ops->cmd_packing(packed_buf, &cmd, PACK);
969 if (!ops->entry_packing)
971 /* Don't dereference potentially NULL pointer if just
972 * deleting a table entry is what was requested. For cases
973 * where 'index' field is physically part of entry structure,
974 * and needed here, we deal with that in the cmd_packing callback.
977 ops->entry_packing(packed_buf, entry, PACK);
979 /* Send SPI write operation: read config table entry */
980 rc = sja1105_xfer_buf(priv, SPI_WRITE, ops->addr, packed_buf,
985 cmd = (struct sja1105_dyn_cmd) {0};
986 ops->cmd_packing(packed_buf, &cmd, UNPACK);
993 static u8 sja1105_crc8_add(u8 crc, u8 byte, u8 poly)
997 for (i = 0; i < 8; i++) {
998 if ((crc ^ byte) & (1 << 7)) {
1009 /* CRC8 algorithm with non-reversed input, non-reversed output,
1010 * no input xor and no output xor. Code customized for receiving
1011 * the SJA1105 E/T FDB keys (vlanid, macaddr) as input. CRC polynomial
1012 * is also received as argument in the Koopman notation that the switch
1013 * hardware stores it in.
1015 u8 sja1105et_fdb_hash(struct sja1105_private *priv, const u8 *addr, u16 vid)
1017 struct sja1105_l2_lookup_params_entry *l2_lookup_params =
1018 priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS].entries;
1019 u64 poly_koopman = l2_lookup_params->poly;
1020 /* Convert polynomial from Koopman to 'normal' notation */
1021 u8 poly = (u8)(1 + (poly_koopman << 1));
1022 u64 vlanid = l2_lookup_params->shared_learn ? 0 : vid;
1023 u64 input = (vlanid << 48) | ether_addr_to_u64(addr);
1024 u8 crc = 0; /* seed */
1027 /* Mask the eight bytes starting from MSB one at a time */
1028 for (i = 56; i >= 0; i -= 8) {
1029 u8 byte = (input & (0xffull << i)) >> i;
1031 crc = sja1105_crc8_add(crc, byte, poly);