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