net: dsa: sja1105: Add a high-level overview of the dynamic config interface
[linux-2.6-microblaze.git] / drivers / net / dsa / sja1105 / sja1105_dynamic_config.c
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 SJA1105_MAX_DYN_CMD_SIZE                                \
128         SJA1105PQRS_SIZE_MAC_CONFIG_DYN_CMD
129
130 struct sja1105_dyn_cmd {
131         bool search;
132         u64 valid;
133         u64 rdwrset;
134         u64 errors;
135         u64 valident;
136         u64 index;
137 };
138
139 enum sja1105_hostcmd {
140         SJA1105_HOSTCMD_SEARCH = 1,
141         SJA1105_HOSTCMD_READ = 2,
142         SJA1105_HOSTCMD_WRITE = 3,
143         SJA1105_HOSTCMD_INVALIDATE = 4,
144 };
145
146 static void
147 sja1105pqrs_l2_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
148                                   enum packing_op op)
149 {
150         u8 *p = buf + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
151         const int size = SJA1105_SIZE_DYN_CMD;
152         u64 lockeds = 0;
153         u64 hostcmd;
154
155         sja1105_packing(p, &cmd->valid,    31, 31, size, op);
156         sja1105_packing(p, &cmd->rdwrset,  30, 30, size, op);
157         sja1105_packing(p, &cmd->errors,   29, 29, size, op);
158         sja1105_packing(p, &lockeds,       28, 28, size, op);
159         sja1105_packing(p, &cmd->valident, 27, 27, size, op);
160
161         /* VALIDENT is supposed to indicate "keep or not", but in SJA1105 E/T,
162          * using it to delete a management route was unsupported. UM10944
163          * said about it:
164          *
165          *   In case of a write access with the MGMTROUTE flag set,
166          *   the flag will be ignored. It will always be found cleared
167          *   for read accesses with the MGMTROUTE flag set.
168          *
169          * SJA1105 P/Q/R/S keeps the same behavior w.r.t. VALIDENT, but there
170          * is now another flag called HOSTCMD which does more stuff (quoting
171          * from UM11040):
172          *
173          *   A write request is accepted only when HOSTCMD is set to write host
174          *   or invalid. A read request is accepted only when HOSTCMD is set to
175          *   search host or read host.
176          *
177          * So it is possible to translate a RDWRSET/VALIDENT combination into
178          * HOSTCMD so that we keep the dynamic command API in place, and
179          * at the same time achieve compatibility with the management route
180          * command structure.
181          */
182         if (cmd->rdwrset == SPI_READ) {
183                 if (cmd->search)
184                         hostcmd = SJA1105_HOSTCMD_SEARCH;
185                 else
186                         hostcmd = SJA1105_HOSTCMD_READ;
187         } else {
188                 /* SPI_WRITE */
189                 if (cmd->valident)
190                         hostcmd = SJA1105_HOSTCMD_WRITE;
191                 else
192                         hostcmd = SJA1105_HOSTCMD_INVALIDATE;
193         }
194         sja1105_packing(p, &hostcmd, 25, 23, size, op);
195
196         /* Hack - The hardware takes the 'index' field within
197          * struct sja1105_l2_lookup_entry as the index on which this command
198          * will operate. However it will ignore everything else, so 'index'
199          * is logically part of command but physically part of entry.
200          * Populate the 'index' entry field from within the command callback,
201          * such that our API doesn't need to ask for a full-blown entry
202          * structure when e.g. a delete is requested.
203          */
204         sja1105_packing(buf, &cmd->index, 15, 6,
205                         SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY, op);
206 }
207
208 static void
209 sja1105et_l2_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
210                                 enum packing_op op)
211 {
212         u8 *p = buf + SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
213         const int size = SJA1105_SIZE_DYN_CMD;
214
215         sja1105_packing(p, &cmd->valid,    31, 31, size, op);
216         sja1105_packing(p, &cmd->rdwrset,  30, 30, size, op);
217         sja1105_packing(p, &cmd->errors,   29, 29, size, op);
218         sja1105_packing(p, &cmd->valident, 27, 27, size, op);
219         /* Hack - see comments above. */
220         sja1105_packing(buf, &cmd->index, 29, 20,
221                         SJA1105ET_SIZE_L2_LOOKUP_ENTRY, op);
222 }
223
224 static void
225 sja1105et_mgmt_route_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
226                                  enum packing_op op)
227 {
228         u8 *p = buf + SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
229         u64 mgmtroute = 1;
230
231         sja1105et_l2_lookup_cmd_packing(buf, cmd, op);
232         if (op == PACK)
233                 sja1105_pack(p, &mgmtroute, 26, 26, SJA1105_SIZE_DYN_CMD);
234 }
235
236 static size_t sja1105et_mgmt_route_entry_packing(void *buf, void *entry_ptr,
237                                                  enum packing_op op)
238 {
239         struct sja1105_mgmt_entry *entry = entry_ptr;
240         const size_t size = SJA1105ET_SIZE_L2_LOOKUP_ENTRY;
241
242         /* UM10944: To specify if a PTP egress timestamp shall be captured on
243          * each port upon transmission of the frame, the LSB of VLANID in the
244          * ENTRY field provided by the host must be set.
245          * Bit 1 of VLANID then specifies the register where the timestamp for
246          * this port is stored in.
247          */
248         sja1105_packing(buf, &entry->tsreg,     85, 85, size, op);
249         sja1105_packing(buf, &entry->takets,    84, 84, size, op);
250         sja1105_packing(buf, &entry->macaddr,   83, 36, size, op);
251         sja1105_packing(buf, &entry->destports, 35, 31, size, op);
252         sja1105_packing(buf, &entry->enfport,   30, 30, size, op);
253         return size;
254 }
255
256 static void
257 sja1105pqrs_mgmt_route_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
258                                    enum packing_op op)
259 {
260         u8 *p = buf + SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
261         u64 mgmtroute = 1;
262
263         sja1105pqrs_l2_lookup_cmd_packing(buf, cmd, op);
264         if (op == PACK)
265                 sja1105_pack(p, &mgmtroute, 26, 26, SJA1105_SIZE_DYN_CMD);
266 }
267
268 static size_t sja1105pqrs_mgmt_route_entry_packing(void *buf, void *entry_ptr,
269                                                    enum packing_op op)
270 {
271         const size_t size = SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY;
272         struct sja1105_mgmt_entry *entry = entry_ptr;
273
274         /* In P/Q/R/S, enfport got renamed to mgmtvalid, but its purpose
275          * is the same (driver uses it to confirm that frame was sent).
276          * So just keep the name from E/T.
277          */
278         sja1105_packing(buf, &entry->tsreg,     71, 71, size, op);
279         sja1105_packing(buf, &entry->takets,    70, 70, size, op);
280         sja1105_packing(buf, &entry->macaddr,   69, 22, size, op);
281         sja1105_packing(buf, &entry->destports, 21, 17, size, op);
282         sja1105_packing(buf, &entry->enfport,   16, 16, size, op);
283         return size;
284 }
285
286 /* In E/T, entry is at addresses 0x27-0x28. There is a 4 byte gap at 0x29,
287  * and command is at 0x2a. Similarly in P/Q/R/S there is a 1 register gap
288  * between entry (0x2d, 0x2e) and command (0x30).
289  */
290 static void
291 sja1105_vlan_lookup_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
292                                 enum packing_op op)
293 {
294         u8 *p = buf + SJA1105_SIZE_VLAN_LOOKUP_ENTRY + 4;
295         const int size = SJA1105_SIZE_DYN_CMD;
296
297         sja1105_packing(p, &cmd->valid,    31, 31, size, op);
298         sja1105_packing(p, &cmd->rdwrset,  30, 30, size, op);
299         sja1105_packing(p, &cmd->valident, 27, 27, size, op);
300         /* Hack - see comments above, applied for 'vlanid' field of
301          * struct sja1105_vlan_lookup_entry.
302          */
303         sja1105_packing(buf, &cmd->index, 38, 27,
304                         SJA1105_SIZE_VLAN_LOOKUP_ENTRY, op);
305 }
306
307 static void
308 sja1105_l2_forwarding_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
309                                   enum packing_op op)
310 {
311         u8 *p = buf + SJA1105_SIZE_L2_FORWARDING_ENTRY;
312         const int size = SJA1105_SIZE_DYN_CMD;
313
314         sja1105_packing(p, &cmd->valid,   31, 31, size, op);
315         sja1105_packing(p, &cmd->errors,  30, 30, size, op);
316         sja1105_packing(p, &cmd->rdwrset, 29, 29, size, op);
317         sja1105_packing(p, &cmd->index,    4,  0, size, op);
318 }
319
320 static void
321 sja1105et_mac_config_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
322                                  enum packing_op op)
323 {
324         const int size = SJA1105_SIZE_DYN_CMD;
325         /* Yup, user manual definitions are reversed */
326         u8 *reg1 = buf + 4;
327
328         sja1105_packing(reg1, &cmd->valid, 31, 31, size, op);
329         sja1105_packing(reg1, &cmd->index, 26, 24, size, op);
330 }
331
332 static size_t sja1105et_mac_config_entry_packing(void *buf, void *entry_ptr,
333                                                  enum packing_op op)
334 {
335         const int size = SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY;
336         struct sja1105_mac_config_entry *entry = entry_ptr;
337         /* Yup, user manual definitions are reversed */
338         u8 *reg1 = buf + 4;
339         u8 *reg2 = buf;
340
341         sja1105_packing(reg1, &entry->speed,     30, 29, size, op);
342         sja1105_packing(reg1, &entry->drpdtag,   23, 23, size, op);
343         sja1105_packing(reg1, &entry->drpuntag,  22, 22, size, op);
344         sja1105_packing(reg1, &entry->retag,     21, 21, size, op);
345         sja1105_packing(reg1, &entry->dyn_learn, 20, 20, size, op);
346         sja1105_packing(reg1, &entry->egress,    19, 19, size, op);
347         sja1105_packing(reg1, &entry->ingress,   18, 18, size, op);
348         sja1105_packing(reg1, &entry->ing_mirr,  17, 17, size, op);
349         sja1105_packing(reg1, &entry->egr_mirr,  16, 16, size, op);
350         sja1105_packing(reg1, &entry->vlanprio,  14, 12, size, op);
351         sja1105_packing(reg1, &entry->vlanid,    11,  0, size, op);
352         sja1105_packing(reg2, &entry->tp_delin,  31, 16, size, op);
353         sja1105_packing(reg2, &entry->tp_delout, 15,  0, size, op);
354         /* MAC configuration table entries which can't be reconfigured:
355          * top, base, enabled, ifg, maxage, drpnona664
356          */
357         /* Bogus return value, not used anywhere */
358         return 0;
359 }
360
361 static void
362 sja1105pqrs_mac_config_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
363                                    enum packing_op op)
364 {
365         const int size = SJA1105ET_SIZE_MAC_CONFIG_DYN_ENTRY;
366         u8 *p = buf + SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY;
367
368         sja1105_packing(p, &cmd->valid,   31, 31, size, op);
369         sja1105_packing(p, &cmd->errors,  30, 30, size, op);
370         sja1105_packing(p, &cmd->rdwrset, 29, 29, size, op);
371         sja1105_packing(p, &cmd->index,    2,  0, size, op);
372 }
373
374 static void
375 sja1105et_l2_lookup_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
376                                        enum packing_op op)
377 {
378         sja1105_packing(buf, &cmd->valid, 31, 31,
379                         SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD, op);
380 }
381
382 static size_t
383 sja1105et_l2_lookup_params_entry_packing(void *buf, void *entry_ptr,
384                                          enum packing_op op)
385 {
386         struct sja1105_l2_lookup_params_entry *entry = entry_ptr;
387
388         sja1105_packing(buf, &entry->poly, 7, 0,
389                         SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD, op);
390         /* Bogus return value, not used anywhere */
391         return 0;
392 }
393
394 static void
395 sja1105et_general_params_cmd_packing(void *buf, struct sja1105_dyn_cmd *cmd,
396                                      enum packing_op op)
397 {
398         const int size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD;
399
400         sja1105_packing(buf, &cmd->valid,  31, 31, size, op);
401         sja1105_packing(buf, &cmd->errors, 30, 30, size, op);
402 }
403
404 static size_t
405 sja1105et_general_params_entry_packing(void *buf, void *entry_ptr,
406                                        enum packing_op op)
407 {
408         struct sja1105_general_params_entry *entry = entry_ptr;
409         const int size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD;
410
411         sja1105_packing(buf, &entry->mirr_port, 2, 0, size, op);
412         /* Bogus return value, not used anywhere */
413         return 0;
414 }
415
416 #define OP_READ         BIT(0)
417 #define OP_WRITE        BIT(1)
418 #define OP_DEL          BIT(2)
419 #define OP_SEARCH       BIT(3)
420
421 /* SJA1105E/T: First generation */
422 struct sja1105_dynamic_table_ops sja1105et_dyn_ops[BLK_IDX_MAX_DYN] = {
423         [BLK_IDX_L2_LOOKUP] = {
424                 .entry_packing = sja1105et_l2_lookup_entry_packing,
425                 .cmd_packing = sja1105et_l2_lookup_cmd_packing,
426                 .access = (OP_READ | OP_WRITE | OP_DEL),
427                 .max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
428                 .packed_size = SJA1105ET_SIZE_L2_LOOKUP_DYN_CMD,
429                 .addr = 0x20,
430         },
431         [BLK_IDX_MGMT_ROUTE] = {
432                 .entry_packing = sja1105et_mgmt_route_entry_packing,
433                 .cmd_packing = sja1105et_mgmt_route_cmd_packing,
434                 .access = (OP_READ | OP_WRITE),
435                 .max_entry_count = SJA1105_NUM_PORTS,
436                 .packed_size = SJA1105ET_SIZE_L2_LOOKUP_DYN_CMD,
437                 .addr = 0x20,
438         },
439         [BLK_IDX_L2_POLICING] = {0},
440         [BLK_IDX_VLAN_LOOKUP] = {
441                 .entry_packing = sja1105_vlan_lookup_entry_packing,
442                 .cmd_packing = sja1105_vlan_lookup_cmd_packing,
443                 .access = (OP_WRITE | OP_DEL),
444                 .max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
445                 .packed_size = SJA1105_SIZE_VLAN_LOOKUP_DYN_CMD,
446                 .addr = 0x27,
447         },
448         [BLK_IDX_L2_FORWARDING] = {
449                 .entry_packing = sja1105_l2_forwarding_entry_packing,
450                 .cmd_packing = sja1105_l2_forwarding_cmd_packing,
451                 .max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
452                 .access = OP_WRITE,
453                 .packed_size = SJA1105_SIZE_L2_FORWARDING_DYN_CMD,
454                 .addr = 0x24,
455         },
456         [BLK_IDX_MAC_CONFIG] = {
457                 .entry_packing = sja1105et_mac_config_entry_packing,
458                 .cmd_packing = sja1105et_mac_config_cmd_packing,
459                 .max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
460                 .access = OP_WRITE,
461                 .packed_size = SJA1105ET_SIZE_MAC_CONFIG_DYN_CMD,
462                 .addr = 0x36,
463         },
464         [BLK_IDX_L2_LOOKUP_PARAMS] = {
465                 .entry_packing = sja1105et_l2_lookup_params_entry_packing,
466                 .cmd_packing = sja1105et_l2_lookup_params_cmd_packing,
467                 .max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
468                 .access = OP_WRITE,
469                 .packed_size = SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD,
470                 .addr = 0x38,
471         },
472         [BLK_IDX_L2_FORWARDING_PARAMS] = {0},
473         [BLK_IDX_AVB_PARAMS] = {0},
474         [BLK_IDX_GENERAL_PARAMS] = {
475                 .entry_packing = sja1105et_general_params_entry_packing,
476                 .cmd_packing = sja1105et_general_params_cmd_packing,
477                 .max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
478                 .access = OP_WRITE,
479                 .packed_size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD,
480                 .addr = 0x34,
481         },
482         [BLK_IDX_XMII_PARAMS] = {0},
483 };
484
485 /* SJA1105P/Q/R/S: Second generation */
486 struct sja1105_dynamic_table_ops sja1105pqrs_dyn_ops[BLK_IDX_MAX_DYN] = {
487         [BLK_IDX_L2_LOOKUP] = {
488                 .entry_packing = sja1105pqrs_l2_lookup_entry_packing,
489                 .cmd_packing = sja1105pqrs_l2_lookup_cmd_packing,
490                 .access = (OP_READ | OP_WRITE | OP_DEL | OP_SEARCH),
491                 .max_entry_count = SJA1105_MAX_L2_LOOKUP_COUNT,
492                 .packed_size = SJA1105PQRS_SIZE_L2_LOOKUP_DYN_CMD,
493                 .addr = 0x24,
494         },
495         [BLK_IDX_MGMT_ROUTE] = {
496                 .entry_packing = sja1105pqrs_mgmt_route_entry_packing,
497                 .cmd_packing = sja1105pqrs_mgmt_route_cmd_packing,
498                 .access = (OP_READ | OP_WRITE | OP_DEL | OP_SEARCH),
499                 .max_entry_count = SJA1105_NUM_PORTS,
500                 .packed_size = SJA1105PQRS_SIZE_L2_LOOKUP_DYN_CMD,
501                 .addr = 0x24,
502         },
503         [BLK_IDX_L2_POLICING] = {0},
504         [BLK_IDX_VLAN_LOOKUP] = {
505                 .entry_packing = sja1105_vlan_lookup_entry_packing,
506                 .cmd_packing = sja1105_vlan_lookup_cmd_packing,
507                 .access = (OP_READ | OP_WRITE | OP_DEL),
508                 .max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
509                 .packed_size = SJA1105_SIZE_VLAN_LOOKUP_DYN_CMD,
510                 .addr = 0x2D,
511         },
512         [BLK_IDX_L2_FORWARDING] = {
513                 .entry_packing = sja1105_l2_forwarding_entry_packing,
514                 .cmd_packing = sja1105_l2_forwarding_cmd_packing,
515                 .max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
516                 .access = OP_WRITE,
517                 .packed_size = SJA1105_SIZE_L2_FORWARDING_DYN_CMD,
518                 .addr = 0x2A,
519         },
520         [BLK_IDX_MAC_CONFIG] = {
521                 .entry_packing = sja1105pqrs_mac_config_entry_packing,
522                 .cmd_packing = sja1105pqrs_mac_config_cmd_packing,
523                 .max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
524                 .access = (OP_READ | OP_WRITE),
525                 .packed_size = SJA1105PQRS_SIZE_MAC_CONFIG_DYN_CMD,
526                 .addr = 0x4B,
527         },
528         [BLK_IDX_L2_LOOKUP_PARAMS] = {
529                 .entry_packing = sja1105et_l2_lookup_params_entry_packing,
530                 .cmd_packing = sja1105et_l2_lookup_params_cmd_packing,
531                 .max_entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
532                 .access = (OP_READ | OP_WRITE),
533                 .packed_size = SJA1105ET_SIZE_L2_LOOKUP_PARAMS_DYN_CMD,
534                 .addr = 0x38,
535         },
536         [BLK_IDX_L2_FORWARDING_PARAMS] = {0},
537         [BLK_IDX_AVB_PARAMS] = {0},
538         [BLK_IDX_GENERAL_PARAMS] = {
539                 .entry_packing = sja1105et_general_params_entry_packing,
540                 .cmd_packing = sja1105et_general_params_cmd_packing,
541                 .max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
542                 .access = OP_WRITE,
543                 .packed_size = SJA1105ET_SIZE_GENERAL_PARAMS_DYN_CMD,
544                 .addr = 0x34,
545         },
546         [BLK_IDX_XMII_PARAMS] = {0},
547 };
548
549 /* Provides read access to the settings through the dynamic interface
550  * of the switch.
551  * @blk_idx     is used as key to select from the sja1105_dynamic_table_ops.
552  *              The selection is limited by the hardware in respect to which
553  *              configuration blocks can be read through the dynamic interface.
554  * @index       is used to retrieve a particular table entry. If negative,
555  *              (and if the @blk_idx supports the searching operation) a search
556  *              is performed by the @entry parameter.
557  * @entry       Type-casted to an unpacked structure that holds a table entry
558  *              of the type specified in @blk_idx.
559  *              Usually an output argument. If @index is negative, then this
560  *              argument is used as input/output: it should be pre-populated
561  *              with the element to search for. Entries which support the
562  *              search operation will have an "index" field (not the @index
563  *              argument to this function) and that is where the found index
564  *              will be returned (or left unmodified - thus negative - if not
565  *              found).
566  */
567 int sja1105_dynamic_config_read(struct sja1105_private *priv,
568                                 enum sja1105_blk_idx blk_idx,
569                                 int index, void *entry)
570 {
571         const struct sja1105_dynamic_table_ops *ops;
572         struct sja1105_dyn_cmd cmd = {0};
573         /* SPI payload buffer */
574         u8 packed_buf[SJA1105_MAX_DYN_CMD_SIZE] = {0};
575         int retries = 3;
576         int rc;
577
578         if (blk_idx >= BLK_IDX_MAX_DYN)
579                 return -ERANGE;
580
581         ops = &priv->info->dyn_ops[blk_idx];
582
583         if (index >= 0 && index >= ops->max_entry_count)
584                 return -ERANGE;
585         if (index < 0 && !(ops->access & OP_SEARCH))
586                 return -EOPNOTSUPP;
587         if (!(ops->access & OP_READ))
588                 return -EOPNOTSUPP;
589         if (ops->packed_size > SJA1105_MAX_DYN_CMD_SIZE)
590                 return -ERANGE;
591         if (!ops->cmd_packing)
592                 return -EOPNOTSUPP;
593         if (!ops->entry_packing)
594                 return -EOPNOTSUPP;
595
596         cmd.valid = true; /* Trigger action on table entry */
597         cmd.rdwrset = SPI_READ; /* Action is read */
598         if (index < 0) {
599                 /* Avoid copying a signed negative number to an u64 */
600                 cmd.index = 0;
601                 cmd.search = true;
602         } else {
603                 cmd.index = index;
604                 cmd.search = false;
605         }
606         cmd.valident = true;
607         ops->cmd_packing(packed_buf, &cmd, PACK);
608
609         if (cmd.search)
610                 ops->entry_packing(packed_buf, entry, PACK);
611
612         /* Send SPI write operation: read config table entry */
613         rc = sja1105_spi_send_packed_buf(priv, SPI_WRITE, ops->addr,
614                                          packed_buf, ops->packed_size);
615         if (rc < 0)
616                 return rc;
617
618         /* Loop until we have confirmation that hardware has finished
619          * processing the command and has cleared the VALID field
620          */
621         do {
622                 memset(packed_buf, 0, ops->packed_size);
623
624                 /* Retrieve the read operation's result */
625                 rc = sja1105_spi_send_packed_buf(priv, SPI_READ, ops->addr,
626                                                  packed_buf, ops->packed_size);
627                 if (rc < 0)
628                         return rc;
629
630                 cmd = (struct sja1105_dyn_cmd) {0};
631                 ops->cmd_packing(packed_buf, &cmd, UNPACK);
632                 /* UM10944: [valident] will always be found cleared
633                  * during a read access with MGMTROUTE set.
634                  * So don't error out in that case.
635                  */
636                 if (!cmd.valident && blk_idx != BLK_IDX_MGMT_ROUTE)
637                         return -ENOENT;
638                 cpu_relax();
639         } while (cmd.valid && --retries);
640
641         if (cmd.valid)
642                 return -ETIMEDOUT;
643
644         /* Don't dereference possibly NULL pointer - maybe caller
645          * only wanted to see whether the entry existed or not.
646          */
647         if (entry)
648                 ops->entry_packing(packed_buf, entry, UNPACK);
649         return 0;
650 }
651
652 int sja1105_dynamic_config_write(struct sja1105_private *priv,
653                                  enum sja1105_blk_idx blk_idx,
654                                  int index, void *entry, bool keep)
655 {
656         const struct sja1105_dynamic_table_ops *ops;
657         struct sja1105_dyn_cmd cmd = {0};
658         /* SPI payload buffer */
659         u8 packed_buf[SJA1105_MAX_DYN_CMD_SIZE] = {0};
660         int rc;
661
662         if (blk_idx >= BLK_IDX_MAX_DYN)
663                 return -ERANGE;
664
665         ops = &priv->info->dyn_ops[blk_idx];
666
667         if (index >= ops->max_entry_count)
668                 return -ERANGE;
669         if (index < 0)
670                 return -ERANGE;
671         if (!(ops->access & OP_WRITE))
672                 return -EOPNOTSUPP;
673         if (!keep && !(ops->access & OP_DEL))
674                 return -EOPNOTSUPP;
675         if (ops->packed_size > SJA1105_MAX_DYN_CMD_SIZE)
676                 return -ERANGE;
677
678         cmd.valident = keep; /* If false, deletes entry */
679         cmd.valid = true; /* Trigger action on table entry */
680         cmd.rdwrset = SPI_WRITE; /* Action is write */
681         cmd.index = index;
682
683         if (!ops->cmd_packing)
684                 return -EOPNOTSUPP;
685         ops->cmd_packing(packed_buf, &cmd, PACK);
686
687         if (!ops->entry_packing)
688                 return -EOPNOTSUPP;
689         /* Don't dereference potentially NULL pointer if just
690          * deleting a table entry is what was requested. For cases
691          * where 'index' field is physically part of entry structure,
692          * and needed here, we deal with that in the cmd_packing callback.
693          */
694         if (keep)
695                 ops->entry_packing(packed_buf, entry, PACK);
696
697         /* Send SPI write operation: read config table entry */
698         rc = sja1105_spi_send_packed_buf(priv, SPI_WRITE, ops->addr,
699                                          packed_buf, ops->packed_size);
700         if (rc < 0)
701                 return rc;
702
703         cmd = (struct sja1105_dyn_cmd) {0};
704         ops->cmd_packing(packed_buf, &cmd, UNPACK);
705         if (cmd.errors)
706                 return -EINVAL;
707
708         return 0;
709 }
710
711 static u8 sja1105_crc8_add(u8 crc, u8 byte, u8 poly)
712 {
713         int i;
714
715         for (i = 0; i < 8; i++) {
716                 if ((crc ^ byte) & (1 << 7)) {
717                         crc <<= 1;
718                         crc ^= poly;
719                 } else {
720                         crc <<= 1;
721                 }
722                 byte <<= 1;
723         }
724         return crc;
725 }
726
727 /* CRC8 algorithm with non-reversed input, non-reversed output,
728  * no input xor and no output xor. Code customized for receiving
729  * the SJA1105 E/T FDB keys (vlanid, macaddr) as input. CRC polynomial
730  * is also received as argument in the Koopman notation that the switch
731  * hardware stores it in.
732  */
733 u8 sja1105et_fdb_hash(struct sja1105_private *priv, const u8 *addr, u16 vid)
734 {
735         struct sja1105_l2_lookup_params_entry *l2_lookup_params =
736                 priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS].entries;
737         u64 poly_koopman = l2_lookup_params->poly;
738         /* Convert polynomial from Koopman to 'normal' notation */
739         u8 poly = (u8)(1 + (poly_koopman << 1));
740         u64 vlanid = l2_lookup_params->shared_learn ? 0 : vid;
741         u64 input = (vlanid << 48) | ether_addr_to_u64(addr);
742         u8 crc = 0; /* seed */
743         int i;
744
745         /* Mask the eight bytes starting from MSB one at a time */
746         for (i = 56; i >= 0; i -= 8) {
747                 u8 byte = (input & (0xffull << i)) >> i;
748
749                 crc = sja1105_crc8_add(crc, byte, poly);
750         }
751         return crc;
752 }