1 // SPDX-License-Identifier: (GPL-2.0 OR MIT) 2 /* 3 * Microsemi Ocelot Switch driver 4 * 5 * Copyright (c) 2017 Microsemi Corporation 6 */ 7 #include <linux/dsa/ocelot.h> 8 #include <linux/if_bridge.h> 9 #include <linux/ptp_classify.h> 10 #include <soc/mscc/ocelot_vcap.h> 11 #include "ocelot.h" 12 #include "ocelot_vcap.h" 13 14 #define TABLE_UPDATE_SLEEP_US 10 15 #define TABLE_UPDATE_TIMEOUT_US 100000 16 17 struct ocelot_mact_entry { 18 u8 mac[ETH_ALEN]; 19 u16 vid; 20 enum macaccess_entry_type type; 21 }; 22 23 static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot) 24 { 25 return ocelot_read(ocelot, ANA_TABLES_MACACCESS); 26 } 27 28 static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot) 29 { 30 u32 val; 31 32 return readx_poll_timeout(ocelot_mact_read_macaccess, 33 ocelot, val, 34 (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) == 35 MACACCESS_CMD_IDLE, 36 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 37 } 38 39 static void ocelot_mact_select(struct ocelot *ocelot, 40 const unsigned char mac[ETH_ALEN], 41 unsigned int vid) 42 { 43 u32 macl = 0, mach = 0; 44 45 /* Set the MAC address to handle and the vlan associated in a format 46 * understood by the hardware. 47 */ 48 mach |= vid << 16; 49 mach |= mac[0] << 8; 50 mach |= mac[1] << 0; 51 macl |= mac[2] << 24; 52 macl |= mac[3] << 16; 53 macl |= mac[4] << 8; 54 macl |= mac[5] << 0; 55 56 ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA); 57 ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA); 58 59 } 60 61 int ocelot_mact_learn(struct ocelot *ocelot, int port, 62 const unsigned char mac[ETH_ALEN], 63 unsigned int vid, enum macaccess_entry_type type) 64 { 65 u32 cmd = ANA_TABLES_MACACCESS_VALID | 66 ANA_TABLES_MACACCESS_DEST_IDX(port) | 67 ANA_TABLES_MACACCESS_ENTRYTYPE(type) | 68 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN); 69 unsigned int mc_ports; 70 71 /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */ 72 if (type == ENTRYTYPE_MACv4) 73 mc_ports = (mac[1] << 8) | mac[2]; 74 else if (type == ENTRYTYPE_MACv6) 75 mc_ports = (mac[0] << 8) | mac[1]; 76 else 77 mc_ports = 0; 78 79 if (mc_ports & BIT(ocelot->num_phys_ports)) 80 cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY; 81 82 ocelot_mact_select(ocelot, mac, vid); 83 84 /* Issue a write command */ 85 ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS); 86 87 return ocelot_mact_wait_for_completion(ocelot); 88 } 89 EXPORT_SYMBOL(ocelot_mact_learn); 90 91 int ocelot_mact_forget(struct ocelot *ocelot, 92 const unsigned char mac[ETH_ALEN], unsigned int vid) 93 { 94 ocelot_mact_select(ocelot, mac, vid); 95 96 /* Issue a forget command */ 97 ocelot_write(ocelot, 98 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET), 99 ANA_TABLES_MACACCESS); 100 101 return ocelot_mact_wait_for_completion(ocelot); 102 } 103 EXPORT_SYMBOL(ocelot_mact_forget); 104 105 static void ocelot_mact_init(struct ocelot *ocelot) 106 { 107 /* Configure the learning mode entries attributes: 108 * - Do not copy the frame to the CPU extraction queues. 109 * - Use the vlan and mac_cpoy for dmac lookup. 110 */ 111 ocelot_rmw(ocelot, 0, 112 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS 113 | ANA_AGENCTRL_LEARN_FWD_KILL 114 | ANA_AGENCTRL_LEARN_IGNORE_VLAN, 115 ANA_AGENCTRL); 116 117 /* Clear the MAC table */ 118 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS); 119 } 120 121 static void ocelot_vcap_enable(struct ocelot *ocelot, int port) 122 { 123 ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA | 124 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa), 125 ANA_PORT_VCAP_S2_CFG, port); 126 127 ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA, 128 ANA_PORT_VCAP_CFG, port); 129 130 ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN, 131 REW_PORT_CFG_ES0_EN, 132 REW_PORT_CFG, port); 133 } 134 135 static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot) 136 { 137 return ocelot_read(ocelot, ANA_TABLES_VLANACCESS); 138 } 139 140 static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot) 141 { 142 u32 val; 143 144 return readx_poll_timeout(ocelot_vlant_read_vlanaccess, 145 ocelot, 146 val, 147 (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) == 148 ANA_TABLES_VLANACCESS_CMD_IDLE, 149 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 150 } 151 152 static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask) 153 { 154 /* Select the VID to configure */ 155 ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid), 156 ANA_TABLES_VLANTIDX); 157 /* Set the vlan port members mask and issue a write command */ 158 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) | 159 ANA_TABLES_VLANACCESS_CMD_WRITE, 160 ANA_TABLES_VLANACCESS); 161 162 return ocelot_vlant_wait_for_completion(ocelot); 163 } 164 165 static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port, 166 struct ocelot_vlan native_vlan) 167 { 168 struct ocelot_port *ocelot_port = ocelot->ports[port]; 169 u32 val = 0; 170 171 ocelot_port->native_vlan = native_vlan; 172 173 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid), 174 REW_PORT_VLAN_CFG_PORT_VID_M, 175 REW_PORT_VLAN_CFG, port); 176 177 if (ocelot_port->vlan_aware) { 178 if (native_vlan.valid) 179 /* Tag all frames except when VID == DEFAULT_VLAN */ 180 val = REW_TAG_CFG_TAG_CFG(1); 181 else 182 /* Tag all frames */ 183 val = REW_TAG_CFG_TAG_CFG(3); 184 } else { 185 /* Port tagging disabled. */ 186 val = REW_TAG_CFG_TAG_CFG(0); 187 } 188 ocelot_rmw_gix(ocelot, val, 189 REW_TAG_CFG_TAG_CFG_M, 190 REW_TAG_CFG, port); 191 } 192 193 /* Default vlan to clasify for untagged frames (may be zero) */ 194 static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, 195 struct ocelot_vlan pvid_vlan) 196 { 197 struct ocelot_port *ocelot_port = ocelot->ports[port]; 198 u32 val = 0; 199 200 ocelot_port->pvid_vlan = pvid_vlan; 201 202 if (!ocelot_port->vlan_aware) 203 pvid_vlan.vid = 0; 204 205 ocelot_rmw_gix(ocelot, 206 ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid), 207 ANA_PORT_VLAN_CFG_VLAN_VID_M, 208 ANA_PORT_VLAN_CFG, port); 209 210 /* If there's no pvid, we should drop not only untagged traffic (which 211 * happens automatically), but also 802.1p traffic which gets 212 * classified to VLAN 0, but that is always in our RX filter, so it 213 * would get accepted were it not for this setting. 214 */ 215 if (!pvid_vlan.valid && ocelot_port->vlan_aware) 216 val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | 217 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA; 218 219 ocelot_rmw_gix(ocelot, val, 220 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | 221 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA, 222 ANA_PORT_DROP_CFG, port); 223 } 224 225 static int ocelot_vlan_member_set(struct ocelot *ocelot, u32 vlan_mask, u16 vid) 226 { 227 int err; 228 229 err = ocelot_vlant_set_mask(ocelot, vid, vlan_mask); 230 if (err) 231 return err; 232 233 ocelot->vlan_mask[vid] = vlan_mask; 234 235 return 0; 236 } 237 238 static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid) 239 { 240 return ocelot_vlan_member_set(ocelot, 241 ocelot->vlan_mask[vid] | BIT(port), 242 vid); 243 } 244 245 static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid) 246 { 247 return ocelot_vlan_member_set(ocelot, 248 ocelot->vlan_mask[vid] & ~BIT(port), 249 vid); 250 } 251 252 int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port, 253 bool vlan_aware, struct netlink_ext_ack *extack) 254 { 255 struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1]; 256 struct ocelot_port *ocelot_port = ocelot->ports[port]; 257 struct ocelot_vcap_filter *filter; 258 u32 val; 259 260 list_for_each_entry(filter, &block->rules, list) { 261 if (filter->ingress_port_mask & BIT(port) && 262 filter->action.vid_replace_ena) { 263 NL_SET_ERR_MSG_MOD(extack, 264 "Cannot change VLAN state with vlan modify rules active"); 265 return -EBUSY; 266 } 267 } 268 269 ocelot_port->vlan_aware = vlan_aware; 270 271 if (vlan_aware) 272 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 273 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1); 274 else 275 val = 0; 276 ocelot_rmw_gix(ocelot, val, 277 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 278 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M, 279 ANA_PORT_VLAN_CFG, port); 280 281 ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan); 282 ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan); 283 284 return 0; 285 } 286 EXPORT_SYMBOL(ocelot_port_vlan_filtering); 287 288 int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid, 289 bool untagged, struct netlink_ext_ack *extack) 290 { 291 struct ocelot_port *ocelot_port = ocelot->ports[port]; 292 293 /* Deny changing the native VLAN, but always permit deleting it */ 294 if (untagged && ocelot_port->native_vlan.vid != vid && 295 ocelot_port->native_vlan.valid) { 296 NL_SET_ERR_MSG_MOD(extack, 297 "Port already has a native VLAN"); 298 return -EBUSY; 299 } 300 301 return 0; 302 } 303 EXPORT_SYMBOL(ocelot_vlan_prepare); 304 305 int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid, 306 bool untagged) 307 { 308 int err; 309 310 err = ocelot_vlan_member_add(ocelot, port, vid); 311 if (err) 312 return err; 313 314 /* Default ingress vlan classification */ 315 if (pvid) { 316 struct ocelot_vlan pvid_vlan; 317 318 pvid_vlan.vid = vid; 319 pvid_vlan.valid = true; 320 ocelot_port_set_pvid(ocelot, port, pvid_vlan); 321 } 322 323 /* Untagged egress vlan clasification */ 324 if (untagged) { 325 struct ocelot_vlan native_vlan; 326 327 native_vlan.vid = vid; 328 native_vlan.valid = true; 329 ocelot_port_set_native_vlan(ocelot, port, native_vlan); 330 } 331 332 return 0; 333 } 334 EXPORT_SYMBOL(ocelot_vlan_add); 335 336 int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid) 337 { 338 struct ocelot_port *ocelot_port = ocelot->ports[port]; 339 int err; 340 341 err = ocelot_vlan_member_del(ocelot, port, vid); 342 if (err) 343 return err; 344 345 /* Ingress */ 346 if (ocelot_port->pvid_vlan.vid == vid) { 347 struct ocelot_vlan pvid_vlan = {0}; 348 349 ocelot_port_set_pvid(ocelot, port, pvid_vlan); 350 } 351 352 /* Egress */ 353 if (ocelot_port->native_vlan.vid == vid) { 354 struct ocelot_vlan native_vlan = {0}; 355 356 ocelot_port_set_native_vlan(ocelot, port, native_vlan); 357 } 358 359 return 0; 360 } 361 EXPORT_SYMBOL(ocelot_vlan_del); 362 363 static void ocelot_vlan_init(struct ocelot *ocelot) 364 { 365 unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0); 366 u16 port, vid; 367 368 /* Clear VLAN table, by default all ports are members of all VLANs */ 369 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT, 370 ANA_TABLES_VLANACCESS); 371 ocelot_vlant_wait_for_completion(ocelot); 372 373 /* Configure the port VLAN memberships */ 374 for (vid = 1; vid < VLAN_N_VID; vid++) 375 ocelot_vlan_member_set(ocelot, 0, vid); 376 377 /* Because VLAN filtering is enabled, we need VID 0 to get untagged 378 * traffic. It is added automatically if 8021q module is loaded, but 379 * we can't rely on it since module may be not loaded. 380 */ 381 ocelot_vlan_member_set(ocelot, all_ports, 0); 382 383 /* Set vlan ingress filter mask to all ports but the CPU port by 384 * default. 385 */ 386 ocelot_write(ocelot, all_ports, ANA_VLANMASK); 387 388 for (port = 0; port < ocelot->num_phys_ports; port++) { 389 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port); 390 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port); 391 } 392 } 393 394 static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port) 395 { 396 return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port); 397 } 398 399 static int ocelot_port_flush(struct ocelot *ocelot, int port) 400 { 401 unsigned int pause_ena; 402 int err, val; 403 404 /* Disable dequeuing from the egress queues */ 405 ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS, 406 QSYS_PORT_MODE_DEQUEUE_DIS, 407 QSYS_PORT_MODE, port); 408 409 /* Disable flow control */ 410 ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena); 411 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 412 413 /* Disable priority flow control */ 414 ocelot_fields_write(ocelot, port, 415 QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0); 416 417 /* Wait at least the time it takes to receive a frame of maximum length 418 * at the port. 419 * Worst-case delays for 10 kilobyte jumbo frames are: 420 * 8 ms on a 10M port 421 * 800 μs on a 100M port 422 * 80 μs on a 1G port 423 * 32 μs on a 2.5G port 424 */ 425 usleep_range(8000, 10000); 426 427 /* Disable half duplex backpressure. */ 428 ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE, 429 SYS_FRONT_PORT_MODE, port); 430 431 /* Flush the queues associated with the port. */ 432 ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA, 433 REW_PORT_CFG, port); 434 435 /* Enable dequeuing from the egress queues. */ 436 ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE, 437 port); 438 439 /* Wait until flushing is complete. */ 440 err = read_poll_timeout(ocelot_read_eq_avail, val, !val, 441 100, 2000000, false, ocelot, port); 442 443 /* Clear flushing again. */ 444 ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port); 445 446 /* Re-enable flow control */ 447 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena); 448 449 return err; 450 } 451 452 void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port, 453 unsigned int link_an_mode, 454 phy_interface_t interface, 455 unsigned long quirks) 456 { 457 struct ocelot_port *ocelot_port = ocelot->ports[port]; 458 int err; 459 460 ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA, 461 DEV_MAC_ENA_CFG); 462 463 ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0); 464 465 err = ocelot_port_flush(ocelot, port); 466 if (err) 467 dev_err(ocelot->dev, "failed to flush port %d: %d\n", 468 port, err); 469 470 /* Put the port in reset. */ 471 if (interface != PHY_INTERFACE_MODE_QSGMII || 472 !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP)) 473 ocelot_port_rmwl(ocelot_port, 474 DEV_CLOCK_CFG_MAC_TX_RST | 475 DEV_CLOCK_CFG_MAC_TX_RST, 476 DEV_CLOCK_CFG_MAC_TX_RST | 477 DEV_CLOCK_CFG_MAC_TX_RST, 478 DEV_CLOCK_CFG); 479 } 480 EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down); 481 482 void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port, 483 struct phy_device *phydev, 484 unsigned int link_an_mode, 485 phy_interface_t interface, 486 int speed, int duplex, 487 bool tx_pause, bool rx_pause, 488 unsigned long quirks) 489 { 490 struct ocelot_port *ocelot_port = ocelot->ports[port]; 491 int mac_speed, mode = 0; 492 u32 mac_fc_cfg; 493 494 /* The MAC might be integrated in systems where the MAC speed is fixed 495 * and it's the PCS who is performing the rate adaptation, so we have 496 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG 497 * (which is also its default value). 498 */ 499 if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) || 500 speed == SPEED_1000) { 501 mac_speed = OCELOT_SPEED_1000; 502 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 503 } else if (speed == SPEED_2500) { 504 mac_speed = OCELOT_SPEED_2500; 505 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 506 } else if (speed == SPEED_100) { 507 mac_speed = OCELOT_SPEED_100; 508 } else { 509 mac_speed = OCELOT_SPEED_10; 510 } 511 512 if (duplex == DUPLEX_FULL) 513 mode |= DEV_MAC_MODE_CFG_FDX_ENA; 514 515 ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG); 516 517 /* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and 518 * PORT_RST bits in DEV_CLOCK_CFG. 519 */ 520 ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed), 521 DEV_CLOCK_CFG); 522 523 switch (speed) { 524 case SPEED_10: 525 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10); 526 break; 527 case SPEED_100: 528 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100); 529 break; 530 case SPEED_1000: 531 case SPEED_2500: 532 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000); 533 break; 534 default: 535 dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n", 536 port, speed); 537 return; 538 } 539 540 /* Handle RX pause in all cases, with 2500base-X this is used for rate 541 * adaptation. 542 */ 543 mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA; 544 545 if (tx_pause) 546 mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA | 547 SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) | 548 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) | 549 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA; 550 551 /* Flow control. Link speed is only used here to evaluate the time 552 * specification in incoming pause frames. 553 */ 554 ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port); 555 556 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port); 557 558 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, tx_pause); 559 560 /* Undo the effects of ocelot_phylink_mac_link_down: 561 * enable MAC module 562 */ 563 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA | 564 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG); 565 566 /* Core: Enable port for frame transfer */ 567 ocelot_fields_write(ocelot, port, 568 QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 569 } 570 EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up); 571 572 static void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, 573 struct sk_buff *clone) 574 { 575 struct ocelot_port *ocelot_port = ocelot->ports[port]; 576 577 spin_lock(&ocelot_port->ts_id_lock); 578 579 skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS; 580 /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */ 581 OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id; 582 ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4; 583 skb_queue_tail(&ocelot_port->tx_skbs, clone); 584 585 spin_unlock(&ocelot_port->ts_id_lock); 586 } 587 588 u32 ocelot_ptp_rew_op(struct sk_buff *skb) 589 { 590 struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone; 591 u8 ptp_cmd = OCELOT_SKB_CB(skb)->ptp_cmd; 592 u32 rew_op = 0; 593 594 if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP && clone) { 595 rew_op = ptp_cmd; 596 rew_op |= OCELOT_SKB_CB(clone)->ts_id << 3; 597 } else if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) { 598 rew_op = ptp_cmd; 599 } 600 601 return rew_op; 602 } 603 EXPORT_SYMBOL(ocelot_ptp_rew_op); 604 605 static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb) 606 { 607 struct ptp_header *hdr; 608 unsigned int ptp_class; 609 u8 msgtype, twostep; 610 611 ptp_class = ptp_classify_raw(skb); 612 if (ptp_class == PTP_CLASS_NONE) 613 return false; 614 615 hdr = ptp_parse_header(skb, ptp_class); 616 if (!hdr) 617 return false; 618 619 msgtype = ptp_get_msgtype(hdr, ptp_class); 620 twostep = hdr->flag_field[0] & 0x2; 621 622 if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) 623 return true; 624 625 return false; 626 } 627 628 int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port, 629 struct sk_buff *skb, 630 struct sk_buff **clone) 631 { 632 struct ocelot_port *ocelot_port = ocelot->ports[port]; 633 u8 ptp_cmd = ocelot_port->ptp_cmd; 634 635 /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */ 636 if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) { 637 if (ocelot_ptp_is_onestep_sync(skb)) { 638 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd; 639 return 0; 640 } 641 642 /* Fall back to two-step timestamping */ 643 ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; 644 } 645 646 if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) { 647 *clone = skb_clone_sk(skb); 648 if (!(*clone)) 649 return -ENOMEM; 650 651 ocelot_port_add_txtstamp_skb(ocelot, port, *clone); 652 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd; 653 } 654 655 return 0; 656 } 657 EXPORT_SYMBOL(ocelot_port_txtstamp_request); 658 659 static void ocelot_get_hwtimestamp(struct ocelot *ocelot, 660 struct timespec64 *ts) 661 { 662 unsigned long flags; 663 u32 val; 664 665 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags); 666 667 /* Read current PTP time to get seconds */ 668 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN); 669 670 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM); 671 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE); 672 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN); 673 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN); 674 675 /* Read packet HW timestamp from FIFO */ 676 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP); 677 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val); 678 679 /* Sec has incremented since the ts was registered */ 680 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC)) 681 ts->tv_sec--; 682 683 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags); 684 } 685 686 void ocelot_get_txtstamp(struct ocelot *ocelot) 687 { 688 int budget = OCELOT_PTP_QUEUE_SZ; 689 690 while (budget--) { 691 struct sk_buff *skb, *skb_tmp, *skb_match = NULL; 692 struct skb_shared_hwtstamps shhwtstamps; 693 struct ocelot_port *port; 694 struct timespec64 ts; 695 unsigned long flags; 696 u32 val, id, txport; 697 698 val = ocelot_read(ocelot, SYS_PTP_STATUS); 699 700 /* Check if a timestamp can be retrieved */ 701 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD)) 702 break; 703 704 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL); 705 706 /* Retrieve the ts ID and Tx port */ 707 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val); 708 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val); 709 710 /* Retrieve its associated skb */ 711 port = ocelot->ports[txport]; 712 713 spin_lock_irqsave(&port->tx_skbs.lock, flags); 714 715 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) { 716 if (OCELOT_SKB_CB(skb)->ts_id != id) 717 continue; 718 __skb_unlink(skb, &port->tx_skbs); 719 skb_match = skb; 720 break; 721 } 722 723 spin_unlock_irqrestore(&port->tx_skbs.lock, flags); 724 725 /* Get the h/w timestamp */ 726 ocelot_get_hwtimestamp(ocelot, &ts); 727 728 if (unlikely(!skb_match)) 729 continue; 730 731 /* Set the timestamp into the skb */ 732 memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 733 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 734 skb_complete_tx_timestamp(skb_match, &shhwtstamps); 735 736 /* Next ts */ 737 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT); 738 } 739 } 740 EXPORT_SYMBOL(ocelot_get_txtstamp); 741 742 static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh, 743 u32 *rval) 744 { 745 u32 bytes_valid, val; 746 747 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 748 if (val == XTR_NOT_READY) { 749 if (ifh) 750 return -EIO; 751 752 do { 753 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 754 } while (val == XTR_NOT_READY); 755 } 756 757 switch (val) { 758 case XTR_ABORT: 759 return -EIO; 760 case XTR_EOF_0: 761 case XTR_EOF_1: 762 case XTR_EOF_2: 763 case XTR_EOF_3: 764 case XTR_PRUNED: 765 bytes_valid = XTR_VALID_BYTES(val); 766 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 767 if (val == XTR_ESCAPE) 768 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 769 else 770 *rval = val; 771 772 return bytes_valid; 773 case XTR_ESCAPE: 774 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp); 775 776 return 4; 777 default: 778 *rval = val; 779 780 return 4; 781 } 782 } 783 784 static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh) 785 { 786 int i, err = 0; 787 788 for (i = 0; i < OCELOT_TAG_LEN / 4; i++) { 789 err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]); 790 if (err != 4) 791 return (err < 0) ? err : -EIO; 792 } 793 794 return 0; 795 } 796 797 int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb) 798 { 799 struct skb_shared_hwtstamps *shhwtstamps; 800 u64 tod_in_ns, full_ts_in_ns; 801 u64 timestamp, src_port, len; 802 u32 xfh[OCELOT_TAG_LEN / 4]; 803 struct net_device *dev; 804 struct timespec64 ts; 805 struct sk_buff *skb; 806 int sz, buf_len; 807 u32 val, *buf; 808 int err; 809 810 err = ocelot_xtr_poll_xfh(ocelot, grp, xfh); 811 if (err) 812 return err; 813 814 ocelot_xfh_get_src_port(xfh, &src_port); 815 ocelot_xfh_get_len(xfh, &len); 816 ocelot_xfh_get_rew_val(xfh, ×tamp); 817 818 if (WARN_ON(src_port >= ocelot->num_phys_ports)) 819 return -EINVAL; 820 821 dev = ocelot->ops->port_to_netdev(ocelot, src_port); 822 if (!dev) 823 return -EINVAL; 824 825 skb = netdev_alloc_skb(dev, len); 826 if (unlikely(!skb)) { 827 netdev_err(dev, "Unable to allocate sk_buff\n"); 828 return -ENOMEM; 829 } 830 831 buf_len = len - ETH_FCS_LEN; 832 buf = (u32 *)skb_put(skb, buf_len); 833 834 len = 0; 835 do { 836 sz = ocelot_rx_frame_word(ocelot, grp, false, &val); 837 if (sz < 0) { 838 err = sz; 839 goto out_free_skb; 840 } 841 *buf++ = val; 842 len += sz; 843 } while (len < buf_len); 844 845 /* Read the FCS */ 846 sz = ocelot_rx_frame_word(ocelot, grp, false, &val); 847 if (sz < 0) { 848 err = sz; 849 goto out_free_skb; 850 } 851 852 /* Update the statistics if part of the FCS was read before */ 853 len -= ETH_FCS_LEN - sz; 854 855 if (unlikely(dev->features & NETIF_F_RXFCS)) { 856 buf = (u32 *)skb_put(skb, ETH_FCS_LEN); 857 *buf = val; 858 } 859 860 if (ocelot->ptp) { 861 ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 862 863 tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec); 864 if ((tod_in_ns & 0xffffffff) < timestamp) 865 full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) | 866 timestamp; 867 else 868 full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) | 869 timestamp; 870 871 shhwtstamps = skb_hwtstamps(skb); 872 memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 873 shhwtstamps->hwtstamp = full_ts_in_ns; 874 } 875 876 /* Everything we see on an interface that is in the HW bridge 877 * has already been forwarded. 878 */ 879 if (ocelot->ports[src_port]->bridge) 880 skb->offload_fwd_mark = 1; 881 882 skb->protocol = eth_type_trans(skb, dev); 883 884 *nskb = skb; 885 886 return 0; 887 888 out_free_skb: 889 kfree_skb(skb); 890 return err; 891 } 892 EXPORT_SYMBOL(ocelot_xtr_poll_frame); 893 894 bool ocelot_can_inject(struct ocelot *ocelot, int grp) 895 { 896 u32 val = ocelot_read(ocelot, QS_INJ_STATUS); 897 898 if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp)))) 899 return false; 900 if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))) 901 return false; 902 903 return true; 904 } 905 EXPORT_SYMBOL(ocelot_can_inject); 906 907 void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp, 908 u32 rew_op, struct sk_buff *skb) 909 { 910 u32 ifh[OCELOT_TAG_LEN / 4] = {0}; 911 unsigned int i, count, last; 912 913 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | 914 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp); 915 916 ocelot_ifh_set_bypass(ifh, 1); 917 ocelot_ifh_set_dest(ifh, BIT_ULL(port)); 918 ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C); 919 ocelot_ifh_set_vid(ifh, skb_vlan_tag_get(skb)); 920 ocelot_ifh_set_rew_op(ifh, rew_op); 921 922 for (i = 0; i < OCELOT_TAG_LEN / 4; i++) 923 ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp); 924 925 count = DIV_ROUND_UP(skb->len, 4); 926 last = skb->len % 4; 927 for (i = 0; i < count; i++) 928 ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp); 929 930 /* Add padding */ 931 while (i < (OCELOT_BUFFER_CELL_SZ / 4)) { 932 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); 933 i++; 934 } 935 936 /* Indicate EOF and valid bytes in last word */ 937 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | 938 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) | 939 QS_INJ_CTRL_EOF, 940 QS_INJ_CTRL, grp); 941 942 /* Add dummy CRC */ 943 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); 944 skb_tx_timestamp(skb); 945 946 skb->dev->stats.tx_packets++; 947 skb->dev->stats.tx_bytes += skb->len; 948 } 949 EXPORT_SYMBOL(ocelot_port_inject_frame); 950 951 void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp) 952 { 953 while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) 954 ocelot_read_rix(ocelot, QS_XTR_RD, grp); 955 } 956 EXPORT_SYMBOL(ocelot_drain_cpu_queue); 957 958 int ocelot_fdb_add(struct ocelot *ocelot, int port, 959 const unsigned char *addr, u16 vid) 960 { 961 int pgid = port; 962 963 if (port == ocelot->npi) 964 pgid = PGID_CPU; 965 966 return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED); 967 } 968 EXPORT_SYMBOL(ocelot_fdb_add); 969 970 int ocelot_fdb_del(struct ocelot *ocelot, int port, 971 const unsigned char *addr, u16 vid) 972 { 973 return ocelot_mact_forget(ocelot, addr, vid); 974 } 975 EXPORT_SYMBOL(ocelot_fdb_del); 976 977 int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid, 978 bool is_static, void *data) 979 { 980 struct ocelot_dump_ctx *dump = data; 981 u32 portid = NETLINK_CB(dump->cb->skb).portid; 982 u32 seq = dump->cb->nlh->nlmsg_seq; 983 struct nlmsghdr *nlh; 984 struct ndmsg *ndm; 985 986 if (dump->idx < dump->cb->args[2]) 987 goto skip; 988 989 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 990 sizeof(*ndm), NLM_F_MULTI); 991 if (!nlh) 992 return -EMSGSIZE; 993 994 ndm = nlmsg_data(nlh); 995 ndm->ndm_family = AF_BRIDGE; 996 ndm->ndm_pad1 = 0; 997 ndm->ndm_pad2 = 0; 998 ndm->ndm_flags = NTF_SELF; 999 ndm->ndm_type = 0; 1000 ndm->ndm_ifindex = dump->dev->ifindex; 1001 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 1002 1003 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 1004 goto nla_put_failure; 1005 1006 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 1007 goto nla_put_failure; 1008 1009 nlmsg_end(dump->skb, nlh); 1010 1011 skip: 1012 dump->idx++; 1013 return 0; 1014 1015 nla_put_failure: 1016 nlmsg_cancel(dump->skb, nlh); 1017 return -EMSGSIZE; 1018 } 1019 EXPORT_SYMBOL(ocelot_port_fdb_do_dump); 1020 1021 static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col, 1022 struct ocelot_mact_entry *entry) 1023 { 1024 u32 val, dst, macl, mach; 1025 char mac[ETH_ALEN]; 1026 1027 /* Set row and column to read from */ 1028 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row); 1029 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col); 1030 1031 /* Issue a read command */ 1032 ocelot_write(ocelot, 1033 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), 1034 ANA_TABLES_MACACCESS); 1035 1036 if (ocelot_mact_wait_for_completion(ocelot)) 1037 return -ETIMEDOUT; 1038 1039 /* Read the entry flags */ 1040 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); 1041 if (!(val & ANA_TABLES_MACACCESS_VALID)) 1042 return -EINVAL; 1043 1044 /* If the entry read has another port configured as its destination, 1045 * do not report it. 1046 */ 1047 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3; 1048 if (dst != port) 1049 return -EINVAL; 1050 1051 /* Get the entry's MAC address and VLAN id */ 1052 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA); 1053 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA); 1054 1055 mac[0] = (mach >> 8) & 0xff; 1056 mac[1] = (mach >> 0) & 0xff; 1057 mac[2] = (macl >> 24) & 0xff; 1058 mac[3] = (macl >> 16) & 0xff; 1059 mac[4] = (macl >> 8) & 0xff; 1060 mac[5] = (macl >> 0) & 0xff; 1061 1062 entry->vid = (mach >> 16) & 0xfff; 1063 ether_addr_copy(entry->mac, mac); 1064 1065 return 0; 1066 } 1067 1068 int ocelot_fdb_dump(struct ocelot *ocelot, int port, 1069 dsa_fdb_dump_cb_t *cb, void *data) 1070 { 1071 int i, j; 1072 1073 /* Loop through all the mac tables entries. */ 1074 for (i = 0; i < ocelot->num_mact_rows; i++) { 1075 for (j = 0; j < 4; j++) { 1076 struct ocelot_mact_entry entry; 1077 bool is_static; 1078 int ret; 1079 1080 ret = ocelot_mact_read(ocelot, port, i, j, &entry); 1081 /* If the entry is invalid (wrong port, invalid...), 1082 * skip it. 1083 */ 1084 if (ret == -EINVAL) 1085 continue; 1086 else if (ret) 1087 return ret; 1088 1089 is_static = (entry.type == ENTRYTYPE_LOCKED); 1090 1091 ret = cb(entry.mac, entry.vid, is_static, data); 1092 if (ret) 1093 return ret; 1094 } 1095 } 1096 1097 return 0; 1098 } 1099 EXPORT_SYMBOL(ocelot_fdb_dump); 1100 1101 int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr) 1102 { 1103 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config, 1104 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0; 1105 } 1106 EXPORT_SYMBOL(ocelot_hwstamp_get); 1107 1108 int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr) 1109 { 1110 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1111 struct hwtstamp_config cfg; 1112 1113 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 1114 return -EFAULT; 1115 1116 /* reserved for future extensions */ 1117 if (cfg.flags) 1118 return -EINVAL; 1119 1120 /* Tx type sanity check */ 1121 switch (cfg.tx_type) { 1122 case HWTSTAMP_TX_ON: 1123 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; 1124 break; 1125 case HWTSTAMP_TX_ONESTEP_SYNC: 1126 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we 1127 * need to update the origin time. 1128 */ 1129 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP; 1130 break; 1131 case HWTSTAMP_TX_OFF: 1132 ocelot_port->ptp_cmd = 0; 1133 break; 1134 default: 1135 return -ERANGE; 1136 } 1137 1138 mutex_lock(&ocelot->ptp_lock); 1139 1140 switch (cfg.rx_filter) { 1141 case HWTSTAMP_FILTER_NONE: 1142 break; 1143 case HWTSTAMP_FILTER_ALL: 1144 case HWTSTAMP_FILTER_SOME: 1145 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 1146 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 1147 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 1148 case HWTSTAMP_FILTER_NTP_ALL: 1149 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 1150 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 1151 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 1152 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 1153 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 1154 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 1155 case HWTSTAMP_FILTER_PTP_V2_EVENT: 1156 case HWTSTAMP_FILTER_PTP_V2_SYNC: 1157 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 1158 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 1159 break; 1160 default: 1161 mutex_unlock(&ocelot->ptp_lock); 1162 return -ERANGE; 1163 } 1164 1165 /* Commit back the result & save it */ 1166 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg)); 1167 mutex_unlock(&ocelot->ptp_lock); 1168 1169 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1170 } 1171 EXPORT_SYMBOL(ocelot_hwstamp_set); 1172 1173 void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data) 1174 { 1175 int i; 1176 1177 if (sset != ETH_SS_STATS) 1178 return; 1179 1180 for (i = 0; i < ocelot->num_stats; i++) 1181 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name, 1182 ETH_GSTRING_LEN); 1183 } 1184 EXPORT_SYMBOL(ocelot_get_strings); 1185 1186 static void ocelot_update_stats(struct ocelot *ocelot) 1187 { 1188 int i, j; 1189 1190 mutex_lock(&ocelot->stats_lock); 1191 1192 for (i = 0; i < ocelot->num_phys_ports; i++) { 1193 /* Configure the port to read the stats from */ 1194 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG); 1195 1196 for (j = 0; j < ocelot->num_stats; j++) { 1197 u32 val; 1198 unsigned int idx = i * ocelot->num_stats + j; 1199 1200 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS, 1201 ocelot->stats_layout[j].offset); 1202 1203 if (val < (ocelot->stats[idx] & U32_MAX)) 1204 ocelot->stats[idx] += (u64)1 << 32; 1205 1206 ocelot->stats[idx] = (ocelot->stats[idx] & 1207 ~(u64)U32_MAX) + val; 1208 } 1209 } 1210 1211 mutex_unlock(&ocelot->stats_lock); 1212 } 1213 1214 static void ocelot_check_stats_work(struct work_struct *work) 1215 { 1216 struct delayed_work *del_work = to_delayed_work(work); 1217 struct ocelot *ocelot = container_of(del_work, struct ocelot, 1218 stats_work); 1219 1220 ocelot_update_stats(ocelot); 1221 1222 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 1223 OCELOT_STATS_CHECK_DELAY); 1224 } 1225 1226 void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data) 1227 { 1228 int i; 1229 1230 /* check and update now */ 1231 ocelot_update_stats(ocelot); 1232 1233 /* Copy all counters */ 1234 for (i = 0; i < ocelot->num_stats; i++) 1235 *data++ = ocelot->stats[port * ocelot->num_stats + i]; 1236 } 1237 EXPORT_SYMBOL(ocelot_get_ethtool_stats); 1238 1239 int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset) 1240 { 1241 if (sset != ETH_SS_STATS) 1242 return -EOPNOTSUPP; 1243 1244 return ocelot->num_stats; 1245 } 1246 EXPORT_SYMBOL(ocelot_get_sset_count); 1247 1248 int ocelot_get_ts_info(struct ocelot *ocelot, int port, 1249 struct ethtool_ts_info *info) 1250 { 1251 info->phc_index = ocelot->ptp_clock ? 1252 ptp_clock_index(ocelot->ptp_clock) : -1; 1253 if (info->phc_index == -1) { 1254 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 1255 SOF_TIMESTAMPING_RX_SOFTWARE | 1256 SOF_TIMESTAMPING_SOFTWARE; 1257 return 0; 1258 } 1259 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 1260 SOF_TIMESTAMPING_RX_SOFTWARE | 1261 SOF_TIMESTAMPING_SOFTWARE | 1262 SOF_TIMESTAMPING_TX_HARDWARE | 1263 SOF_TIMESTAMPING_RX_HARDWARE | 1264 SOF_TIMESTAMPING_RAW_HARDWARE; 1265 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) | 1266 BIT(HWTSTAMP_TX_ONESTEP_SYNC); 1267 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL); 1268 1269 return 0; 1270 } 1271 EXPORT_SYMBOL(ocelot_get_ts_info); 1272 1273 static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond, 1274 bool only_active_ports) 1275 { 1276 u32 mask = 0; 1277 int port; 1278 1279 for (port = 0; port < ocelot->num_phys_ports; port++) { 1280 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1281 1282 if (!ocelot_port) 1283 continue; 1284 1285 if (ocelot_port->bond == bond) { 1286 if (only_active_ports && !ocelot_port->lag_tx_active) 1287 continue; 1288 1289 mask |= BIT(port); 1290 } 1291 } 1292 1293 return mask; 1294 } 1295 1296 static u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, 1297 struct net_device *bridge) 1298 { 1299 u32 mask = 0; 1300 int port; 1301 1302 for (port = 0; port < ocelot->num_phys_ports; port++) { 1303 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1304 1305 if (!ocelot_port) 1306 continue; 1307 1308 if (ocelot_port->stp_state == BR_STATE_FORWARDING && 1309 ocelot_port->bridge == bridge) 1310 mask |= BIT(port); 1311 } 1312 1313 return mask; 1314 } 1315 1316 static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot) 1317 { 1318 u32 mask = 0; 1319 int port; 1320 1321 for (port = 0; port < ocelot->num_phys_ports; port++) { 1322 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1323 1324 if (!ocelot_port) 1325 continue; 1326 1327 if (ocelot_port->is_dsa_8021q_cpu) 1328 mask |= BIT(port); 1329 } 1330 1331 return mask; 1332 } 1333 1334 void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot) 1335 { 1336 unsigned long cpu_fwd_mask; 1337 int port; 1338 1339 /* If a DSA tag_8021q CPU exists, it needs to be included in the 1340 * regular forwarding path of the front ports regardless of whether 1341 * those are bridged or standalone. 1342 * If DSA tag_8021q is not used, this returns 0, which is fine because 1343 * the hardware-based CPU port module can be a destination for packets 1344 * even if it isn't part of PGID_SRC. 1345 */ 1346 cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot); 1347 1348 /* Apply FWD mask. The loop is needed to add/remove the current port as 1349 * a source for the other ports. 1350 */ 1351 for (port = 0; port < ocelot->num_phys_ports; port++) { 1352 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1353 unsigned long mask; 1354 1355 if (!ocelot_port) { 1356 /* Unused ports can't send anywhere */ 1357 mask = 0; 1358 } else if (ocelot_port->is_dsa_8021q_cpu) { 1359 /* The DSA tag_8021q CPU ports need to be able to 1360 * forward packets to all other ports except for 1361 * themselves 1362 */ 1363 mask = GENMASK(ocelot->num_phys_ports - 1, 0); 1364 mask &= ~cpu_fwd_mask; 1365 } else if (ocelot_port->bridge) { 1366 struct net_device *bridge = ocelot_port->bridge; 1367 struct net_device *bond = ocelot_port->bond; 1368 1369 mask = ocelot_get_bridge_fwd_mask(ocelot, bridge); 1370 mask |= cpu_fwd_mask; 1371 mask &= ~BIT(port); 1372 if (bond) { 1373 mask &= ~ocelot_get_bond_mask(ocelot, bond, 1374 false); 1375 } 1376 } else { 1377 /* Standalone ports forward only to DSA tag_8021q CPU 1378 * ports (if those exist), or to the hardware CPU port 1379 * module otherwise. 1380 */ 1381 mask = cpu_fwd_mask; 1382 } 1383 1384 ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port); 1385 } 1386 } 1387 EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask); 1388 1389 void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state) 1390 { 1391 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1392 u32 learn_ena = 0; 1393 1394 ocelot_port->stp_state = state; 1395 1396 if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) && 1397 ocelot_port->learn_ena) 1398 learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA; 1399 1400 ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA, 1401 ANA_PORT_PORT_CFG, port); 1402 1403 ocelot_apply_bridge_fwd_mask(ocelot); 1404 } 1405 EXPORT_SYMBOL(ocelot_bridge_stp_state_set); 1406 1407 void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs) 1408 { 1409 unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000); 1410 1411 /* Setting AGE_PERIOD to zero effectively disables automatic aging, 1412 * which is clearly not what our intention is. So avoid that. 1413 */ 1414 if (!age_period) 1415 age_period = 1; 1416 1417 ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE); 1418 } 1419 EXPORT_SYMBOL(ocelot_set_ageing_time); 1420 1421 static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot, 1422 const unsigned char *addr, 1423 u16 vid) 1424 { 1425 struct ocelot_multicast *mc; 1426 1427 list_for_each_entry(mc, &ocelot->multicast, list) { 1428 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid) 1429 return mc; 1430 } 1431 1432 return NULL; 1433 } 1434 1435 static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr) 1436 { 1437 if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e) 1438 return ENTRYTYPE_MACv4; 1439 if (addr[0] == 0x33 && addr[1] == 0x33) 1440 return ENTRYTYPE_MACv6; 1441 return ENTRYTYPE_LOCKED; 1442 } 1443 1444 static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index, 1445 unsigned long ports) 1446 { 1447 struct ocelot_pgid *pgid; 1448 1449 pgid = kzalloc(sizeof(*pgid), GFP_KERNEL); 1450 if (!pgid) 1451 return ERR_PTR(-ENOMEM); 1452 1453 pgid->ports = ports; 1454 pgid->index = index; 1455 refcount_set(&pgid->refcount, 1); 1456 list_add_tail(&pgid->list, &ocelot->pgids); 1457 1458 return pgid; 1459 } 1460 1461 static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid) 1462 { 1463 if (!refcount_dec_and_test(&pgid->refcount)) 1464 return; 1465 1466 list_del(&pgid->list); 1467 kfree(pgid); 1468 } 1469 1470 static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot, 1471 const struct ocelot_multicast *mc) 1472 { 1473 struct ocelot_pgid *pgid; 1474 int index; 1475 1476 /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and 1477 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the 1478 * destination mask table (PGID), the destination set is programmed as 1479 * part of the entry MAC address.", and the DEST_IDX is set to 0. 1480 */ 1481 if (mc->entry_type == ENTRYTYPE_MACv4 || 1482 mc->entry_type == ENTRYTYPE_MACv6) 1483 return ocelot_pgid_alloc(ocelot, 0, mc->ports); 1484 1485 list_for_each_entry(pgid, &ocelot->pgids, list) { 1486 /* When searching for a nonreserved multicast PGID, ignore the 1487 * dummy PGID of zero that we have for MACv4/MACv6 entries 1488 */ 1489 if (pgid->index && pgid->ports == mc->ports) { 1490 refcount_inc(&pgid->refcount); 1491 return pgid; 1492 } 1493 } 1494 1495 /* Search for a free index in the nonreserved multicast PGID area */ 1496 for_each_nonreserved_multicast_dest_pgid(ocelot, index) { 1497 bool used = false; 1498 1499 list_for_each_entry(pgid, &ocelot->pgids, list) { 1500 if (pgid->index == index) { 1501 used = true; 1502 break; 1503 } 1504 } 1505 1506 if (!used) 1507 return ocelot_pgid_alloc(ocelot, index, mc->ports); 1508 } 1509 1510 return ERR_PTR(-ENOSPC); 1511 } 1512 1513 static void ocelot_encode_ports_to_mdb(unsigned char *addr, 1514 struct ocelot_multicast *mc) 1515 { 1516 ether_addr_copy(addr, mc->addr); 1517 1518 if (mc->entry_type == ENTRYTYPE_MACv4) { 1519 addr[0] = 0; 1520 addr[1] = mc->ports >> 8; 1521 addr[2] = mc->ports & 0xff; 1522 } else if (mc->entry_type == ENTRYTYPE_MACv6) { 1523 addr[0] = mc->ports >> 8; 1524 addr[1] = mc->ports & 0xff; 1525 } 1526 } 1527 1528 int ocelot_port_mdb_add(struct ocelot *ocelot, int port, 1529 const struct switchdev_obj_port_mdb *mdb) 1530 { 1531 unsigned char addr[ETH_ALEN]; 1532 struct ocelot_multicast *mc; 1533 struct ocelot_pgid *pgid; 1534 u16 vid = mdb->vid; 1535 1536 if (port == ocelot->npi) 1537 port = ocelot->num_phys_ports; 1538 1539 mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 1540 if (!mc) { 1541 /* New entry */ 1542 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL); 1543 if (!mc) 1544 return -ENOMEM; 1545 1546 mc->entry_type = ocelot_classify_mdb(mdb->addr); 1547 ether_addr_copy(mc->addr, mdb->addr); 1548 mc->vid = vid; 1549 1550 list_add_tail(&mc->list, &ocelot->multicast); 1551 } else { 1552 /* Existing entry. Clean up the current port mask from 1553 * hardware now, because we'll be modifying it. 1554 */ 1555 ocelot_pgid_free(ocelot, mc->pgid); 1556 ocelot_encode_ports_to_mdb(addr, mc); 1557 ocelot_mact_forget(ocelot, addr, vid); 1558 } 1559 1560 mc->ports |= BIT(port); 1561 1562 pgid = ocelot_mdb_get_pgid(ocelot, mc); 1563 if (IS_ERR(pgid)) { 1564 dev_err(ocelot->dev, 1565 "Cannot allocate PGID for mdb %pM vid %d\n", 1566 mc->addr, mc->vid); 1567 devm_kfree(ocelot->dev, mc); 1568 return PTR_ERR(pgid); 1569 } 1570 mc->pgid = pgid; 1571 1572 ocelot_encode_ports_to_mdb(addr, mc); 1573 1574 if (mc->entry_type != ENTRYTYPE_MACv4 && 1575 mc->entry_type != ENTRYTYPE_MACv6) 1576 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 1577 pgid->index); 1578 1579 return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 1580 mc->entry_type); 1581 } 1582 EXPORT_SYMBOL(ocelot_port_mdb_add); 1583 1584 int ocelot_port_mdb_del(struct ocelot *ocelot, int port, 1585 const struct switchdev_obj_port_mdb *mdb) 1586 { 1587 unsigned char addr[ETH_ALEN]; 1588 struct ocelot_multicast *mc; 1589 struct ocelot_pgid *pgid; 1590 u16 vid = mdb->vid; 1591 1592 if (port == ocelot->npi) 1593 port = ocelot->num_phys_ports; 1594 1595 mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 1596 if (!mc) 1597 return -ENOENT; 1598 1599 ocelot_encode_ports_to_mdb(addr, mc); 1600 ocelot_mact_forget(ocelot, addr, vid); 1601 1602 ocelot_pgid_free(ocelot, mc->pgid); 1603 mc->ports &= ~BIT(port); 1604 if (!mc->ports) { 1605 list_del(&mc->list); 1606 devm_kfree(ocelot->dev, mc); 1607 return 0; 1608 } 1609 1610 /* We have a PGID with fewer ports now */ 1611 pgid = ocelot_mdb_get_pgid(ocelot, mc); 1612 if (IS_ERR(pgid)) 1613 return PTR_ERR(pgid); 1614 mc->pgid = pgid; 1615 1616 ocelot_encode_ports_to_mdb(addr, mc); 1617 1618 if (mc->entry_type != ENTRYTYPE_MACv4 && 1619 mc->entry_type != ENTRYTYPE_MACv6) 1620 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 1621 pgid->index); 1622 1623 return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 1624 mc->entry_type); 1625 } 1626 EXPORT_SYMBOL(ocelot_port_mdb_del); 1627 1628 void ocelot_port_bridge_join(struct ocelot *ocelot, int port, 1629 struct net_device *bridge) 1630 { 1631 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1632 1633 ocelot_port->bridge = bridge; 1634 1635 ocelot_apply_bridge_fwd_mask(ocelot); 1636 } 1637 EXPORT_SYMBOL(ocelot_port_bridge_join); 1638 1639 void ocelot_port_bridge_leave(struct ocelot *ocelot, int port, 1640 struct net_device *bridge) 1641 { 1642 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1643 struct ocelot_vlan pvid = {0}, native_vlan = {0}; 1644 1645 ocelot_port->bridge = NULL; 1646 1647 ocelot_port_set_pvid(ocelot, port, pvid); 1648 ocelot_port_set_native_vlan(ocelot, port, native_vlan); 1649 ocelot_apply_bridge_fwd_mask(ocelot); 1650 } 1651 EXPORT_SYMBOL(ocelot_port_bridge_leave); 1652 1653 static void ocelot_set_aggr_pgids(struct ocelot *ocelot) 1654 { 1655 unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0); 1656 int i, port, lag; 1657 1658 /* Reset destination and aggregation PGIDS */ 1659 for_each_unicast_dest_pgid(ocelot, port) 1660 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 1661 1662 for_each_aggr_pgid(ocelot, i) 1663 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), 1664 ANA_PGID_PGID, i); 1665 1666 /* The visited ports bitmask holds the list of ports offloading any 1667 * bonding interface. Initially we mark all these ports as unvisited, 1668 * then every time we visit a port in this bitmask, we know that it is 1669 * the lowest numbered port, i.e. the one whose logical ID == physical 1670 * port ID == LAG ID. So we mark as visited all further ports in the 1671 * bitmask that are offloading the same bonding interface. This way, 1672 * we set up the aggregation PGIDs only once per bonding interface. 1673 */ 1674 for (port = 0; port < ocelot->num_phys_ports; port++) { 1675 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1676 1677 if (!ocelot_port || !ocelot_port->bond) 1678 continue; 1679 1680 visited &= ~BIT(port); 1681 } 1682 1683 /* Now, set PGIDs for each active LAG */ 1684 for (lag = 0; lag < ocelot->num_phys_ports; lag++) { 1685 struct net_device *bond = ocelot->ports[lag]->bond; 1686 int num_active_ports = 0; 1687 unsigned long bond_mask; 1688 u8 aggr_idx[16]; 1689 1690 if (!bond || (visited & BIT(lag))) 1691 continue; 1692 1693 bond_mask = ocelot_get_bond_mask(ocelot, bond, true); 1694 1695 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) { 1696 // Destination mask 1697 ocelot_write_rix(ocelot, bond_mask, 1698 ANA_PGID_PGID, port); 1699 aggr_idx[num_active_ports++] = port; 1700 } 1701 1702 for_each_aggr_pgid(ocelot, i) { 1703 u32 ac; 1704 1705 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i); 1706 ac &= ~bond_mask; 1707 /* Don't do division by zero if there was no active 1708 * port. Just make all aggregation codes zero. 1709 */ 1710 if (num_active_ports) 1711 ac |= BIT(aggr_idx[i % num_active_ports]); 1712 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i); 1713 } 1714 1715 /* Mark all ports in the same LAG as visited to avoid applying 1716 * the same config again. 1717 */ 1718 for (port = lag; port < ocelot->num_phys_ports; port++) { 1719 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1720 1721 if (!ocelot_port) 1722 continue; 1723 1724 if (ocelot_port->bond == bond) 1725 visited |= BIT(port); 1726 } 1727 } 1728 } 1729 1730 /* When offloading a bonding interface, the switch ports configured under the 1731 * same bond must have the same logical port ID, equal to the physical port ID 1732 * of the lowest numbered physical port in that bond. Otherwise, in standalone/ 1733 * bridged mode, each port has a logical port ID equal to its physical port ID. 1734 */ 1735 static void ocelot_setup_logical_port_ids(struct ocelot *ocelot) 1736 { 1737 int port; 1738 1739 for (port = 0; port < ocelot->num_phys_ports; port++) { 1740 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1741 struct net_device *bond; 1742 1743 if (!ocelot_port) 1744 continue; 1745 1746 bond = ocelot_port->bond; 1747 if (bond) { 1748 int lag = __ffs(ocelot_get_bond_mask(ocelot, bond, 1749 false)); 1750 1751 ocelot_rmw_gix(ocelot, 1752 ANA_PORT_PORT_CFG_PORTID_VAL(lag), 1753 ANA_PORT_PORT_CFG_PORTID_VAL_M, 1754 ANA_PORT_PORT_CFG, port); 1755 } else { 1756 ocelot_rmw_gix(ocelot, 1757 ANA_PORT_PORT_CFG_PORTID_VAL(port), 1758 ANA_PORT_PORT_CFG_PORTID_VAL_M, 1759 ANA_PORT_PORT_CFG, port); 1760 } 1761 } 1762 } 1763 1764 int ocelot_port_lag_join(struct ocelot *ocelot, int port, 1765 struct net_device *bond, 1766 struct netdev_lag_upper_info *info) 1767 { 1768 if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) 1769 return -EOPNOTSUPP; 1770 1771 ocelot->ports[port]->bond = bond; 1772 1773 ocelot_setup_logical_port_ids(ocelot); 1774 ocelot_apply_bridge_fwd_mask(ocelot); 1775 ocelot_set_aggr_pgids(ocelot); 1776 1777 return 0; 1778 } 1779 EXPORT_SYMBOL(ocelot_port_lag_join); 1780 1781 void ocelot_port_lag_leave(struct ocelot *ocelot, int port, 1782 struct net_device *bond) 1783 { 1784 ocelot->ports[port]->bond = NULL; 1785 1786 ocelot_setup_logical_port_ids(ocelot); 1787 ocelot_apply_bridge_fwd_mask(ocelot); 1788 ocelot_set_aggr_pgids(ocelot); 1789 } 1790 EXPORT_SYMBOL(ocelot_port_lag_leave); 1791 1792 void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active) 1793 { 1794 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1795 1796 ocelot_port->lag_tx_active = lag_tx_active; 1797 1798 /* Rebalance the LAGs */ 1799 ocelot_set_aggr_pgids(ocelot); 1800 } 1801 EXPORT_SYMBOL(ocelot_port_lag_change); 1802 1803 /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu. 1804 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG. 1805 * In the special case that it's the NPI port that we're configuring, the 1806 * length of the tag and optional prefix needs to be accounted for privately, 1807 * in order to be able to sustain communication at the requested @sdu. 1808 */ 1809 void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu) 1810 { 1811 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1812 int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN; 1813 int pause_start, pause_stop; 1814 int atop, atop_tot; 1815 1816 if (port == ocelot->npi) { 1817 maxlen += OCELOT_TAG_LEN; 1818 1819 if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 1820 maxlen += OCELOT_SHORT_PREFIX_LEN; 1821 else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 1822 maxlen += OCELOT_LONG_PREFIX_LEN; 1823 } 1824 1825 ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG); 1826 1827 /* Set Pause watermark hysteresis */ 1828 pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ; 1829 pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ; 1830 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START, 1831 pause_start); 1832 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP, 1833 pause_stop); 1834 1835 /* Tail dropping watermarks */ 1836 atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) / 1837 OCELOT_BUFFER_CELL_SZ; 1838 atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ; 1839 ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port); 1840 ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG); 1841 } 1842 EXPORT_SYMBOL(ocelot_port_set_maxlen); 1843 1844 int ocelot_get_max_mtu(struct ocelot *ocelot, int port) 1845 { 1846 int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN; 1847 1848 if (port == ocelot->npi) { 1849 max_mtu -= OCELOT_TAG_LEN; 1850 1851 if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) 1852 max_mtu -= OCELOT_SHORT_PREFIX_LEN; 1853 else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) 1854 max_mtu -= OCELOT_LONG_PREFIX_LEN; 1855 } 1856 1857 return max_mtu; 1858 } 1859 EXPORT_SYMBOL(ocelot_get_max_mtu); 1860 1861 static void ocelot_port_set_learning(struct ocelot *ocelot, int port, 1862 bool enabled) 1863 { 1864 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1865 u32 val = 0; 1866 1867 if (enabled) 1868 val = ANA_PORT_PORT_CFG_LEARN_ENA; 1869 1870 ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA, 1871 ANA_PORT_PORT_CFG, port); 1872 1873 ocelot_port->learn_ena = enabled; 1874 } 1875 1876 static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port, 1877 bool enabled) 1878 { 1879 u32 val = 0; 1880 1881 if (enabled) 1882 val = BIT(port); 1883 1884 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC); 1885 } 1886 1887 static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port, 1888 bool enabled) 1889 { 1890 u32 val = 0; 1891 1892 if (enabled) 1893 val = BIT(port); 1894 1895 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC); 1896 } 1897 1898 static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port, 1899 bool enabled) 1900 { 1901 u32 val = 0; 1902 1903 if (enabled) 1904 val = BIT(port); 1905 1906 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC); 1907 } 1908 1909 int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port, 1910 struct switchdev_brport_flags flags) 1911 { 1912 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 1913 BR_BCAST_FLOOD)) 1914 return -EINVAL; 1915 1916 return 0; 1917 } 1918 EXPORT_SYMBOL(ocelot_port_pre_bridge_flags); 1919 1920 void ocelot_port_bridge_flags(struct ocelot *ocelot, int port, 1921 struct switchdev_brport_flags flags) 1922 { 1923 if (flags.mask & BR_LEARNING) 1924 ocelot_port_set_learning(ocelot, port, 1925 !!(flags.val & BR_LEARNING)); 1926 1927 if (flags.mask & BR_FLOOD) 1928 ocelot_port_set_ucast_flood(ocelot, port, 1929 !!(flags.val & BR_FLOOD)); 1930 1931 if (flags.mask & BR_MCAST_FLOOD) 1932 ocelot_port_set_mcast_flood(ocelot, port, 1933 !!(flags.val & BR_MCAST_FLOOD)); 1934 1935 if (flags.mask & BR_BCAST_FLOOD) 1936 ocelot_port_set_bcast_flood(ocelot, port, 1937 !!(flags.val & BR_BCAST_FLOOD)); 1938 } 1939 EXPORT_SYMBOL(ocelot_port_bridge_flags); 1940 1941 void ocelot_init_port(struct ocelot *ocelot, int port) 1942 { 1943 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1944 1945 skb_queue_head_init(&ocelot_port->tx_skbs); 1946 spin_lock_init(&ocelot_port->ts_id_lock); 1947 1948 /* Basic L2 initialization */ 1949 1950 /* Set MAC IFG Gaps 1951 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0 1952 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5 1953 */ 1954 ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5), 1955 DEV_MAC_IFG_CFG); 1956 1957 /* Load seed (0) and set MAC HDX late collision */ 1958 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) | 1959 DEV_MAC_HDX_CFG_SEED_LOAD, 1960 DEV_MAC_HDX_CFG); 1961 mdelay(1); 1962 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67), 1963 DEV_MAC_HDX_CFG); 1964 1965 /* Set Max Length and maximum tags allowed */ 1966 ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN); 1967 ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) | 1968 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA | 1969 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA | 1970 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA, 1971 DEV_MAC_TAGS_CFG); 1972 1973 /* Set SMAC of Pause frame (00:00:00:00:00:00) */ 1974 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG); 1975 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG); 1976 1977 /* Enable transmission of pause frames */ 1978 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 1979 1980 /* Drop frames with multicast source address */ 1981 ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 1982 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 1983 ANA_PORT_DROP_CFG, port); 1984 1985 /* Set default VLAN and tag type to 8021Q. */ 1986 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q), 1987 REW_PORT_VLAN_CFG_PORT_TPID_M, 1988 REW_PORT_VLAN_CFG, port); 1989 1990 /* Disable source address learning for standalone mode */ 1991 ocelot_port_set_learning(ocelot, port, false); 1992 1993 /* Set the port's initial logical port ID value, enable receiving 1994 * frames on it, and configure the MAC address learning type to 1995 * automatic. 1996 */ 1997 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | 1998 ANA_PORT_PORT_CFG_RECV_ENA | 1999 ANA_PORT_PORT_CFG_PORTID_VAL(port), 2000 ANA_PORT_PORT_CFG, port); 2001 2002 /* Enable vcap lookups */ 2003 ocelot_vcap_enable(ocelot, port); 2004 } 2005 EXPORT_SYMBOL(ocelot_init_port); 2006 2007 /* Configure and enable the CPU port module, which is a set of queues 2008 * accessible through register MMIO, frame DMA or Ethernet (in case 2009 * NPI mode is used). 2010 */ 2011 static void ocelot_cpu_port_init(struct ocelot *ocelot) 2012 { 2013 int cpu = ocelot->num_phys_ports; 2014 2015 /* The unicast destination PGID for the CPU port module is unused */ 2016 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu); 2017 /* Instead set up a multicast destination PGID for traffic copied to 2018 * the CPU. Whitelisted MAC addresses like the port netdevice MAC 2019 * addresses will be copied to the CPU via this PGID. 2020 */ 2021 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU); 2022 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA | 2023 ANA_PORT_PORT_CFG_PORTID_VAL(cpu), 2024 ANA_PORT_PORT_CFG, cpu); 2025 2026 /* Enable CPU port module */ 2027 ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 2028 /* CPU port Injection/Extraction configuration */ 2029 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR, 2030 OCELOT_TAG_PREFIX_NONE); 2031 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR, 2032 OCELOT_TAG_PREFIX_NONE); 2033 2034 /* Configure the CPU port to be VLAN aware */ 2035 ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) | 2036 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 2037 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1), 2038 ANA_PORT_VLAN_CFG, cpu); 2039 } 2040 2041 static void ocelot_detect_features(struct ocelot *ocelot) 2042 { 2043 int mmgt, eq_ctrl; 2044 2045 /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds 2046 * the number of 240-byte free memory words (aka 4-cell chunks) and not 2047 * 192 bytes as the documentation incorrectly says. 2048 */ 2049 mmgt = ocelot_read(ocelot, SYS_MMGT); 2050 ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt); 2051 2052 eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL); 2053 ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl); 2054 } 2055 2056 int ocelot_init(struct ocelot *ocelot) 2057 { 2058 char queue_name[32]; 2059 int i, ret; 2060 u32 port; 2061 2062 if (ocelot->ops->reset) { 2063 ret = ocelot->ops->reset(ocelot); 2064 if (ret) { 2065 dev_err(ocelot->dev, "Switch reset failed\n"); 2066 return ret; 2067 } 2068 } 2069 2070 ocelot->stats = devm_kcalloc(ocelot->dev, 2071 ocelot->num_phys_ports * ocelot->num_stats, 2072 sizeof(u64), GFP_KERNEL); 2073 if (!ocelot->stats) 2074 return -ENOMEM; 2075 2076 mutex_init(&ocelot->stats_lock); 2077 mutex_init(&ocelot->ptp_lock); 2078 spin_lock_init(&ocelot->ptp_clock_lock); 2079 snprintf(queue_name, sizeof(queue_name), "%s-stats", 2080 dev_name(ocelot->dev)); 2081 ocelot->stats_queue = create_singlethread_workqueue(queue_name); 2082 if (!ocelot->stats_queue) 2083 return -ENOMEM; 2084 2085 ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0); 2086 if (!ocelot->owq) { 2087 destroy_workqueue(ocelot->stats_queue); 2088 return -ENOMEM; 2089 } 2090 2091 INIT_LIST_HEAD(&ocelot->multicast); 2092 INIT_LIST_HEAD(&ocelot->pgids); 2093 ocelot_detect_features(ocelot); 2094 ocelot_mact_init(ocelot); 2095 ocelot_vlan_init(ocelot); 2096 ocelot_vcap_init(ocelot); 2097 ocelot_cpu_port_init(ocelot); 2098 2099 for (port = 0; port < ocelot->num_phys_ports; port++) { 2100 /* Clear all counters (5 groups) */ 2101 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) | 2102 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f), 2103 SYS_STAT_CFG); 2104 } 2105 2106 /* Only use S-Tag */ 2107 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG); 2108 2109 /* Aggregation mode */ 2110 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA | 2111 ANA_AGGR_CFG_AC_DMAC_ENA | 2112 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA | 2113 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA | 2114 ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA | 2115 ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA, 2116 ANA_AGGR_CFG); 2117 2118 /* Set MAC age time to default value. The entry is aged after 2119 * 2*AGE_PERIOD 2120 */ 2121 ocelot_write(ocelot, 2122 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ), 2123 ANA_AUTOAGE); 2124 2125 /* Disable learning for frames discarded by VLAN ingress filtering */ 2126 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1); 2127 2128 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */ 2129 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA | 2130 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING); 2131 2132 /* Setup flooding PGIDs */ 2133 for (i = 0; i < ocelot->num_flooding_pgids; i++) 2134 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | 2135 ANA_FLOODING_FLD_BROADCAST(PGID_BC) | 2136 ANA_FLOODING_FLD_UNICAST(PGID_UC), 2137 ANA_FLOODING, i); 2138 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) | 2139 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) | 2140 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) | 2141 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC), 2142 ANA_FLOODING_IPMC); 2143 2144 for (port = 0; port < ocelot->num_phys_ports; port++) { 2145 /* Transmit the frame to the local port. */ 2146 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 2147 /* Do not forward BPDU frames to the front ports. */ 2148 ocelot_write_gix(ocelot, 2149 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 2150 ANA_PORT_CPU_FWD_BPDU_CFG, 2151 port); 2152 /* Ensure bridging is disabled */ 2153 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port); 2154 } 2155 2156 for_each_nonreserved_multicast_dest_pgid(ocelot, i) { 2157 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0)); 2158 2159 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); 2160 } 2161 2162 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE); 2163 2164 /* Allow broadcast and unknown L2 multicast to the CPU. */ 2165 ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 2166 ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 2167 ANA_PGID_PGID, PGID_MC); 2168 ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 2169 ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), 2170 ANA_PGID_PGID, PGID_BC); 2171 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4); 2172 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6); 2173 2174 /* Allow manual injection via DEVCPU_QS registers, and byte swap these 2175 * registers endianness. 2176 */ 2177 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP | 2178 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0); 2179 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP | 2180 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0); 2181 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) | 2182 ANA_CPUQ_CFG_CPUQ_LRN(2) | 2183 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) | 2184 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) | 2185 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) | 2186 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) | 2187 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) | 2188 ANA_CPUQ_CFG_CPUQ_IGMP(6) | 2189 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG); 2190 for (i = 0; i < 16; i++) 2191 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) | 2192 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6), 2193 ANA_CPUQ_8021_CFG, i); 2194 2195 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work); 2196 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 2197 OCELOT_STATS_CHECK_DELAY); 2198 2199 return 0; 2200 } 2201 EXPORT_SYMBOL(ocelot_init); 2202 2203 void ocelot_deinit(struct ocelot *ocelot) 2204 { 2205 cancel_delayed_work(&ocelot->stats_work); 2206 destroy_workqueue(ocelot->stats_queue); 2207 destroy_workqueue(ocelot->owq); 2208 mutex_destroy(&ocelot->stats_lock); 2209 } 2210 EXPORT_SYMBOL(ocelot_deinit); 2211 2212 void ocelot_deinit_port(struct ocelot *ocelot, int port) 2213 { 2214 struct ocelot_port *ocelot_port = ocelot->ports[port]; 2215 2216 skb_queue_purge(&ocelot_port->tx_skbs); 2217 } 2218 EXPORT_SYMBOL(ocelot_deinit_port); 2219 2220 MODULE_LICENSE("Dual MIT/GPL"); 2221