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/if_bridge.h> 8 #include <soc/mscc/ocelot_vcap.h> 9 #include "ocelot.h" 10 #include "ocelot_vcap.h" 11 12 #define TABLE_UPDATE_SLEEP_US 10 13 #define TABLE_UPDATE_TIMEOUT_US 100000 14 15 struct ocelot_mact_entry { 16 u8 mac[ETH_ALEN]; 17 u16 vid; 18 enum macaccess_entry_type type; 19 }; 20 21 static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot) 22 { 23 return ocelot_read(ocelot, ANA_TABLES_MACACCESS); 24 } 25 26 static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot) 27 { 28 u32 val; 29 30 return readx_poll_timeout(ocelot_mact_read_macaccess, 31 ocelot, val, 32 (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) == 33 MACACCESS_CMD_IDLE, 34 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 35 } 36 37 static void ocelot_mact_select(struct ocelot *ocelot, 38 const unsigned char mac[ETH_ALEN], 39 unsigned int vid) 40 { 41 u32 macl = 0, mach = 0; 42 43 /* Set the MAC address to handle and the vlan associated in a format 44 * understood by the hardware. 45 */ 46 mach |= vid << 16; 47 mach |= mac[0] << 8; 48 mach |= mac[1] << 0; 49 macl |= mac[2] << 24; 50 macl |= mac[3] << 16; 51 macl |= mac[4] << 8; 52 macl |= mac[5] << 0; 53 54 ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA); 55 ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA); 56 57 } 58 59 int ocelot_mact_learn(struct ocelot *ocelot, int port, 60 const unsigned char mac[ETH_ALEN], 61 unsigned int vid, enum macaccess_entry_type type) 62 { 63 u32 cmd = ANA_TABLES_MACACCESS_VALID | 64 ANA_TABLES_MACACCESS_DEST_IDX(port) | 65 ANA_TABLES_MACACCESS_ENTRYTYPE(type) | 66 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN); 67 unsigned int mc_ports; 68 69 /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */ 70 if (type == ENTRYTYPE_MACv4) 71 mc_ports = (mac[1] << 8) | mac[2]; 72 else if (type == ENTRYTYPE_MACv6) 73 mc_ports = (mac[0] << 8) | mac[1]; 74 else 75 mc_ports = 0; 76 77 if (mc_ports & BIT(ocelot->num_phys_ports)) 78 cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY; 79 80 ocelot_mact_select(ocelot, mac, vid); 81 82 /* Issue a write command */ 83 ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS); 84 85 return ocelot_mact_wait_for_completion(ocelot); 86 } 87 EXPORT_SYMBOL(ocelot_mact_learn); 88 89 int ocelot_mact_forget(struct ocelot *ocelot, 90 const unsigned char mac[ETH_ALEN], unsigned int vid) 91 { 92 ocelot_mact_select(ocelot, mac, vid); 93 94 /* Issue a forget command */ 95 ocelot_write(ocelot, 96 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET), 97 ANA_TABLES_MACACCESS); 98 99 return ocelot_mact_wait_for_completion(ocelot); 100 } 101 EXPORT_SYMBOL(ocelot_mact_forget); 102 103 static void ocelot_mact_init(struct ocelot *ocelot) 104 { 105 /* Configure the learning mode entries attributes: 106 * - Do not copy the frame to the CPU extraction queues. 107 * - Use the vlan and mac_cpoy for dmac lookup. 108 */ 109 ocelot_rmw(ocelot, 0, 110 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS 111 | ANA_AGENCTRL_LEARN_FWD_KILL 112 | ANA_AGENCTRL_LEARN_IGNORE_VLAN, 113 ANA_AGENCTRL); 114 115 /* Clear the MAC table */ 116 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS); 117 } 118 119 static void ocelot_vcap_enable(struct ocelot *ocelot, int port) 120 { 121 ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA | 122 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa), 123 ANA_PORT_VCAP_S2_CFG, port); 124 125 ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA, 126 ANA_PORT_VCAP_CFG, port); 127 128 ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN, 129 REW_PORT_CFG_ES0_EN, 130 REW_PORT_CFG, port); 131 } 132 133 static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot) 134 { 135 return ocelot_read(ocelot, ANA_TABLES_VLANACCESS); 136 } 137 138 static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot) 139 { 140 u32 val; 141 142 return readx_poll_timeout(ocelot_vlant_read_vlanaccess, 143 ocelot, 144 val, 145 (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) == 146 ANA_TABLES_VLANACCESS_CMD_IDLE, 147 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); 148 } 149 150 static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask) 151 { 152 /* Select the VID to configure */ 153 ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid), 154 ANA_TABLES_VLANTIDX); 155 /* Set the vlan port members mask and issue a write command */ 156 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) | 157 ANA_TABLES_VLANACCESS_CMD_WRITE, 158 ANA_TABLES_VLANACCESS); 159 160 return ocelot_vlant_wait_for_completion(ocelot); 161 } 162 163 static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port, 164 struct ocelot_vlan native_vlan) 165 { 166 struct ocelot_port *ocelot_port = ocelot->ports[port]; 167 u32 val = 0; 168 169 ocelot_port->native_vlan = native_vlan; 170 171 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid), 172 REW_PORT_VLAN_CFG_PORT_VID_M, 173 REW_PORT_VLAN_CFG, port); 174 175 if (ocelot_port->vlan_aware) { 176 if (native_vlan.valid) 177 /* Tag all frames except when VID == DEFAULT_VLAN */ 178 val = REW_TAG_CFG_TAG_CFG(1); 179 else 180 /* Tag all frames */ 181 val = REW_TAG_CFG_TAG_CFG(3); 182 } else { 183 /* Port tagging disabled. */ 184 val = REW_TAG_CFG_TAG_CFG(0); 185 } 186 ocelot_rmw_gix(ocelot, val, 187 REW_TAG_CFG_TAG_CFG_M, 188 REW_TAG_CFG, port); 189 } 190 191 /* Default vlan to clasify for untagged frames (may be zero) */ 192 static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, 193 struct ocelot_vlan pvid_vlan) 194 { 195 struct ocelot_port *ocelot_port = ocelot->ports[port]; 196 u32 val = 0; 197 198 ocelot_port->pvid_vlan = pvid_vlan; 199 200 if (!ocelot_port->vlan_aware) 201 pvid_vlan.vid = 0; 202 203 ocelot_rmw_gix(ocelot, 204 ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid), 205 ANA_PORT_VLAN_CFG_VLAN_VID_M, 206 ANA_PORT_VLAN_CFG, port); 207 208 /* If there's no pvid, we should drop not only untagged traffic (which 209 * happens automatically), but also 802.1p traffic which gets 210 * classified to VLAN 0, but that is always in our RX filter, so it 211 * would get accepted were it not for this setting. 212 */ 213 if (!pvid_vlan.valid && ocelot_port->vlan_aware) 214 val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | 215 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA; 216 217 ocelot_rmw_gix(ocelot, val, 218 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | 219 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA, 220 ANA_PORT_DROP_CFG, port); 221 } 222 223 int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port, 224 bool vlan_aware, struct switchdev_trans *trans) 225 { 226 struct ocelot_port *ocelot_port = ocelot->ports[port]; 227 u32 val; 228 229 if (switchdev_trans_ph_prepare(trans)) { 230 struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1]; 231 struct ocelot_vcap_filter *filter; 232 233 list_for_each_entry(filter, &block->rules, list) { 234 if (filter->ingress_port_mask & BIT(port) && 235 filter->action.vid_replace_ena) { 236 dev_err(ocelot->dev, 237 "Cannot change VLAN state with vlan modify rules active\n"); 238 return -EBUSY; 239 } 240 } 241 242 return 0; 243 } 244 245 ocelot_port->vlan_aware = vlan_aware; 246 247 if (vlan_aware) 248 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 249 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1); 250 else 251 val = 0; 252 ocelot_rmw_gix(ocelot, val, 253 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 254 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M, 255 ANA_PORT_VLAN_CFG, port); 256 257 ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan); 258 ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan); 259 260 return 0; 261 } 262 EXPORT_SYMBOL(ocelot_port_vlan_filtering); 263 264 int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid, 265 bool untagged) 266 { 267 struct ocelot_port *ocelot_port = ocelot->ports[port]; 268 269 /* Deny changing the native VLAN, but always permit deleting it */ 270 if (untagged && ocelot_port->native_vlan.vid != vid && 271 ocelot_port->native_vlan.valid) { 272 dev_err(ocelot->dev, 273 "Port already has a native VLAN: %d\n", 274 ocelot_port->native_vlan.vid); 275 return -EBUSY; 276 } 277 278 return 0; 279 } 280 EXPORT_SYMBOL(ocelot_vlan_prepare); 281 282 int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid, 283 bool untagged) 284 { 285 int ret; 286 287 /* Make the port a member of the VLAN */ 288 ocelot->vlan_mask[vid] |= BIT(port); 289 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); 290 if (ret) 291 return ret; 292 293 /* Default ingress vlan classification */ 294 if (pvid) { 295 struct ocelot_vlan pvid_vlan; 296 297 pvid_vlan.vid = vid; 298 pvid_vlan.valid = true; 299 ocelot_port_set_pvid(ocelot, port, pvid_vlan); 300 } 301 302 /* Untagged egress vlan clasification */ 303 if (untagged) { 304 struct ocelot_vlan native_vlan; 305 306 native_vlan.vid = vid; 307 native_vlan.valid = true; 308 ocelot_port_set_native_vlan(ocelot, port, native_vlan); 309 } 310 311 return 0; 312 } 313 EXPORT_SYMBOL(ocelot_vlan_add); 314 315 int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid) 316 { 317 struct ocelot_port *ocelot_port = ocelot->ports[port]; 318 int ret; 319 320 /* Stop the port from being a member of the vlan */ 321 ocelot->vlan_mask[vid] &= ~BIT(port); 322 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); 323 if (ret) 324 return ret; 325 326 /* Ingress */ 327 if (ocelot_port->pvid_vlan.vid == vid) { 328 struct ocelot_vlan pvid_vlan = {0}; 329 330 ocelot_port_set_pvid(ocelot, port, pvid_vlan); 331 } 332 333 /* Egress */ 334 if (ocelot_port->native_vlan.vid == vid) { 335 struct ocelot_vlan native_vlan = {0}; 336 337 ocelot_port_set_native_vlan(ocelot, port, native_vlan); 338 } 339 340 return 0; 341 } 342 EXPORT_SYMBOL(ocelot_vlan_del); 343 344 static void ocelot_vlan_init(struct ocelot *ocelot) 345 { 346 u16 port, vid; 347 348 /* Clear VLAN table, by default all ports are members of all VLANs */ 349 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT, 350 ANA_TABLES_VLANACCESS); 351 ocelot_vlant_wait_for_completion(ocelot); 352 353 /* Configure the port VLAN memberships */ 354 for (vid = 1; vid < VLAN_N_VID; vid++) { 355 ocelot->vlan_mask[vid] = 0; 356 ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); 357 } 358 359 /* Because VLAN filtering is enabled, we need VID 0 to get untagged 360 * traffic. It is added automatically if 8021q module is loaded, but 361 * we can't rely on it since module may be not loaded. 362 */ 363 ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0); 364 ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]); 365 366 /* Set vlan ingress filter mask to all ports but the CPU port by 367 * default. 368 */ 369 ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), 370 ANA_VLANMASK); 371 372 for (port = 0; port < ocelot->num_phys_ports; port++) { 373 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port); 374 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port); 375 } 376 } 377 378 void ocelot_adjust_link(struct ocelot *ocelot, int port, 379 struct phy_device *phydev) 380 { 381 struct ocelot_port *ocelot_port = ocelot->ports[port]; 382 int speed, mode = 0; 383 384 switch (phydev->speed) { 385 case SPEED_10: 386 speed = OCELOT_SPEED_10; 387 break; 388 case SPEED_100: 389 speed = OCELOT_SPEED_100; 390 break; 391 case SPEED_1000: 392 speed = OCELOT_SPEED_1000; 393 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 394 break; 395 case SPEED_2500: 396 speed = OCELOT_SPEED_2500; 397 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; 398 break; 399 default: 400 dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n", 401 port, phydev->speed); 402 return; 403 } 404 405 phy_print_status(phydev); 406 407 if (!phydev->link) 408 return; 409 410 /* Only full duplex supported for now */ 411 ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA | 412 mode, DEV_MAC_MODE_CFG); 413 414 /* Disable HDX fast control */ 415 ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS, 416 DEV_PORT_MISC); 417 418 /* SGMII only for now */ 419 ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA, 420 PCS1G_MODE_CFG); 421 ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG); 422 423 /* Enable PCS */ 424 ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG); 425 426 /* No aneg on SGMII */ 427 ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG); 428 429 /* No loopback */ 430 ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG); 431 432 /* Enable MAC module */ 433 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA | 434 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG); 435 436 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of 437 * reset */ 438 ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed), 439 DEV_CLOCK_CFG); 440 441 /* No PFC */ 442 ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed), 443 ANA_PFC_PFC_CFG, port); 444 445 /* Core: Enable port for frame transfer */ 446 ocelot_fields_write(ocelot, port, 447 QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 448 449 /* Flow control */ 450 ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) | 451 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA | 452 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA | 453 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) | 454 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed), 455 SYS_MAC_FC_CFG, port); 456 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port); 457 } 458 EXPORT_SYMBOL(ocelot_adjust_link); 459 460 void ocelot_port_enable(struct ocelot *ocelot, int port, 461 struct phy_device *phy) 462 { 463 /* Enable receiving frames on the port, and activate auto-learning of 464 * MAC addresses. 465 */ 466 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | 467 ANA_PORT_PORT_CFG_RECV_ENA | 468 ANA_PORT_PORT_CFG_PORTID_VAL(port), 469 ANA_PORT_PORT_CFG, port); 470 } 471 EXPORT_SYMBOL(ocelot_port_enable); 472 473 void ocelot_port_disable(struct ocelot *ocelot, int port) 474 { 475 struct ocelot_port *ocelot_port = ocelot->ports[port]; 476 477 ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG); 478 ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0); 479 } 480 EXPORT_SYMBOL(ocelot_port_disable); 481 482 void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, 483 struct sk_buff *clone) 484 { 485 struct ocelot_port *ocelot_port = ocelot->ports[port]; 486 487 spin_lock(&ocelot_port->ts_id_lock); 488 489 skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS; 490 /* Store timestamp ID in cb[0] of sk_buff */ 491 clone->cb[0] = ocelot_port->ts_id; 492 ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4; 493 skb_queue_tail(&ocelot_port->tx_skbs, clone); 494 495 spin_unlock(&ocelot_port->ts_id_lock); 496 } 497 EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb); 498 499 static void ocelot_get_hwtimestamp(struct ocelot *ocelot, 500 struct timespec64 *ts) 501 { 502 unsigned long flags; 503 u32 val; 504 505 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags); 506 507 /* Read current PTP time to get seconds */ 508 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN); 509 510 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM); 511 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE); 512 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN); 513 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN); 514 515 /* Read packet HW timestamp from FIFO */ 516 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP); 517 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val); 518 519 /* Sec has incremented since the ts was registered */ 520 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC)) 521 ts->tv_sec--; 522 523 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags); 524 } 525 526 void ocelot_get_txtstamp(struct ocelot *ocelot) 527 { 528 int budget = OCELOT_PTP_QUEUE_SZ; 529 530 while (budget--) { 531 struct sk_buff *skb, *skb_tmp, *skb_match = NULL; 532 struct skb_shared_hwtstamps shhwtstamps; 533 struct ocelot_port *port; 534 struct timespec64 ts; 535 unsigned long flags; 536 u32 val, id, txport; 537 538 val = ocelot_read(ocelot, SYS_PTP_STATUS); 539 540 /* Check if a timestamp can be retrieved */ 541 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD)) 542 break; 543 544 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL); 545 546 /* Retrieve the ts ID and Tx port */ 547 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val); 548 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val); 549 550 /* Retrieve its associated skb */ 551 port = ocelot->ports[txport]; 552 553 spin_lock_irqsave(&port->tx_skbs.lock, flags); 554 555 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) { 556 if (skb->cb[0] != id) 557 continue; 558 __skb_unlink(skb, &port->tx_skbs); 559 skb_match = skb; 560 break; 561 } 562 563 spin_unlock_irqrestore(&port->tx_skbs.lock, flags); 564 565 /* Get the h/w timestamp */ 566 ocelot_get_hwtimestamp(ocelot, &ts); 567 568 if (unlikely(!skb_match)) 569 continue; 570 571 /* Set the timestamp into the skb */ 572 memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 573 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 574 skb_complete_tx_timestamp(skb_match, &shhwtstamps); 575 576 /* Next ts */ 577 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT); 578 } 579 } 580 EXPORT_SYMBOL(ocelot_get_txtstamp); 581 582 int ocelot_fdb_add(struct ocelot *ocelot, int port, 583 const unsigned char *addr, u16 vid) 584 { 585 int pgid = port; 586 587 if (port == ocelot->npi) 588 pgid = PGID_CPU; 589 590 return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED); 591 } 592 EXPORT_SYMBOL(ocelot_fdb_add); 593 594 int ocelot_fdb_del(struct ocelot *ocelot, int port, 595 const unsigned char *addr, u16 vid) 596 { 597 return ocelot_mact_forget(ocelot, addr, vid); 598 } 599 EXPORT_SYMBOL(ocelot_fdb_del); 600 601 int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid, 602 bool is_static, void *data) 603 { 604 struct ocelot_dump_ctx *dump = data; 605 u32 portid = NETLINK_CB(dump->cb->skb).portid; 606 u32 seq = dump->cb->nlh->nlmsg_seq; 607 struct nlmsghdr *nlh; 608 struct ndmsg *ndm; 609 610 if (dump->idx < dump->cb->args[2]) 611 goto skip; 612 613 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 614 sizeof(*ndm), NLM_F_MULTI); 615 if (!nlh) 616 return -EMSGSIZE; 617 618 ndm = nlmsg_data(nlh); 619 ndm->ndm_family = AF_BRIDGE; 620 ndm->ndm_pad1 = 0; 621 ndm->ndm_pad2 = 0; 622 ndm->ndm_flags = NTF_SELF; 623 ndm->ndm_type = 0; 624 ndm->ndm_ifindex = dump->dev->ifindex; 625 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; 626 627 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) 628 goto nla_put_failure; 629 630 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) 631 goto nla_put_failure; 632 633 nlmsg_end(dump->skb, nlh); 634 635 skip: 636 dump->idx++; 637 return 0; 638 639 nla_put_failure: 640 nlmsg_cancel(dump->skb, nlh); 641 return -EMSGSIZE; 642 } 643 EXPORT_SYMBOL(ocelot_port_fdb_do_dump); 644 645 static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col, 646 struct ocelot_mact_entry *entry) 647 { 648 u32 val, dst, macl, mach; 649 char mac[ETH_ALEN]; 650 651 /* Set row and column to read from */ 652 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row); 653 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col); 654 655 /* Issue a read command */ 656 ocelot_write(ocelot, 657 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), 658 ANA_TABLES_MACACCESS); 659 660 if (ocelot_mact_wait_for_completion(ocelot)) 661 return -ETIMEDOUT; 662 663 /* Read the entry flags */ 664 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); 665 if (!(val & ANA_TABLES_MACACCESS_VALID)) 666 return -EINVAL; 667 668 /* If the entry read has another port configured as its destination, 669 * do not report it. 670 */ 671 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3; 672 if (dst != port) 673 return -EINVAL; 674 675 /* Get the entry's MAC address and VLAN id */ 676 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA); 677 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA); 678 679 mac[0] = (mach >> 8) & 0xff; 680 mac[1] = (mach >> 0) & 0xff; 681 mac[2] = (macl >> 24) & 0xff; 682 mac[3] = (macl >> 16) & 0xff; 683 mac[4] = (macl >> 8) & 0xff; 684 mac[5] = (macl >> 0) & 0xff; 685 686 entry->vid = (mach >> 16) & 0xfff; 687 ether_addr_copy(entry->mac, mac); 688 689 return 0; 690 } 691 692 int ocelot_fdb_dump(struct ocelot *ocelot, int port, 693 dsa_fdb_dump_cb_t *cb, void *data) 694 { 695 int i, j; 696 697 /* Loop through all the mac tables entries. */ 698 for (i = 0; i < ocelot->num_mact_rows; i++) { 699 for (j = 0; j < 4; j++) { 700 struct ocelot_mact_entry entry; 701 bool is_static; 702 int ret; 703 704 ret = ocelot_mact_read(ocelot, port, i, j, &entry); 705 /* If the entry is invalid (wrong port, invalid...), 706 * skip it. 707 */ 708 if (ret == -EINVAL) 709 continue; 710 else if (ret) 711 return ret; 712 713 is_static = (entry.type == ENTRYTYPE_LOCKED); 714 715 ret = cb(entry.mac, entry.vid, is_static, data); 716 if (ret) 717 return ret; 718 } 719 } 720 721 return 0; 722 } 723 EXPORT_SYMBOL(ocelot_fdb_dump); 724 725 int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr) 726 { 727 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config, 728 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0; 729 } 730 EXPORT_SYMBOL(ocelot_hwstamp_get); 731 732 int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr) 733 { 734 struct ocelot_port *ocelot_port = ocelot->ports[port]; 735 struct hwtstamp_config cfg; 736 737 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 738 return -EFAULT; 739 740 /* reserved for future extensions */ 741 if (cfg.flags) 742 return -EINVAL; 743 744 /* Tx type sanity check */ 745 switch (cfg.tx_type) { 746 case HWTSTAMP_TX_ON: 747 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; 748 break; 749 case HWTSTAMP_TX_ONESTEP_SYNC: 750 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we 751 * need to update the origin time. 752 */ 753 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP; 754 break; 755 case HWTSTAMP_TX_OFF: 756 ocelot_port->ptp_cmd = 0; 757 break; 758 default: 759 return -ERANGE; 760 } 761 762 mutex_lock(&ocelot->ptp_lock); 763 764 switch (cfg.rx_filter) { 765 case HWTSTAMP_FILTER_NONE: 766 break; 767 case HWTSTAMP_FILTER_ALL: 768 case HWTSTAMP_FILTER_SOME: 769 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 770 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 771 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 772 case HWTSTAMP_FILTER_NTP_ALL: 773 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 774 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 775 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 776 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 777 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 778 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 779 case HWTSTAMP_FILTER_PTP_V2_EVENT: 780 case HWTSTAMP_FILTER_PTP_V2_SYNC: 781 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 782 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 783 break; 784 default: 785 mutex_unlock(&ocelot->ptp_lock); 786 return -ERANGE; 787 } 788 789 /* Commit back the result & save it */ 790 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg)); 791 mutex_unlock(&ocelot->ptp_lock); 792 793 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 794 } 795 EXPORT_SYMBOL(ocelot_hwstamp_set); 796 797 void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data) 798 { 799 int i; 800 801 if (sset != ETH_SS_STATS) 802 return; 803 804 for (i = 0; i < ocelot->num_stats; i++) 805 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name, 806 ETH_GSTRING_LEN); 807 } 808 EXPORT_SYMBOL(ocelot_get_strings); 809 810 static void ocelot_update_stats(struct ocelot *ocelot) 811 { 812 int i, j; 813 814 mutex_lock(&ocelot->stats_lock); 815 816 for (i = 0; i < ocelot->num_phys_ports; i++) { 817 /* Configure the port to read the stats from */ 818 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG); 819 820 for (j = 0; j < ocelot->num_stats; j++) { 821 u32 val; 822 unsigned int idx = i * ocelot->num_stats + j; 823 824 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS, 825 ocelot->stats_layout[j].offset); 826 827 if (val < (ocelot->stats[idx] & U32_MAX)) 828 ocelot->stats[idx] += (u64)1 << 32; 829 830 ocelot->stats[idx] = (ocelot->stats[idx] & 831 ~(u64)U32_MAX) + val; 832 } 833 } 834 835 mutex_unlock(&ocelot->stats_lock); 836 } 837 838 static void ocelot_check_stats_work(struct work_struct *work) 839 { 840 struct delayed_work *del_work = to_delayed_work(work); 841 struct ocelot *ocelot = container_of(del_work, struct ocelot, 842 stats_work); 843 844 ocelot_update_stats(ocelot); 845 846 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 847 OCELOT_STATS_CHECK_DELAY); 848 } 849 850 void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data) 851 { 852 int i; 853 854 /* check and update now */ 855 ocelot_update_stats(ocelot); 856 857 /* Copy all counters */ 858 for (i = 0; i < ocelot->num_stats; i++) 859 *data++ = ocelot->stats[port * ocelot->num_stats + i]; 860 } 861 EXPORT_SYMBOL(ocelot_get_ethtool_stats); 862 863 int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset) 864 { 865 if (sset != ETH_SS_STATS) 866 return -EOPNOTSUPP; 867 868 return ocelot->num_stats; 869 } 870 EXPORT_SYMBOL(ocelot_get_sset_count); 871 872 int ocelot_get_ts_info(struct ocelot *ocelot, int port, 873 struct ethtool_ts_info *info) 874 { 875 info->phc_index = ocelot->ptp_clock ? 876 ptp_clock_index(ocelot->ptp_clock) : -1; 877 if (info->phc_index == -1) { 878 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 879 SOF_TIMESTAMPING_RX_SOFTWARE | 880 SOF_TIMESTAMPING_SOFTWARE; 881 return 0; 882 } 883 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | 884 SOF_TIMESTAMPING_RX_SOFTWARE | 885 SOF_TIMESTAMPING_SOFTWARE | 886 SOF_TIMESTAMPING_TX_HARDWARE | 887 SOF_TIMESTAMPING_RX_HARDWARE | 888 SOF_TIMESTAMPING_RAW_HARDWARE; 889 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) | 890 BIT(HWTSTAMP_TX_ONESTEP_SYNC); 891 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL); 892 893 return 0; 894 } 895 EXPORT_SYMBOL(ocelot_get_ts_info); 896 897 void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state) 898 { 899 u32 port_cfg; 900 int p, i; 901 902 if (!(BIT(port) & ocelot->bridge_mask)) 903 return; 904 905 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port); 906 907 switch (state) { 908 case BR_STATE_FORWARDING: 909 ocelot->bridge_fwd_mask |= BIT(port); 910 fallthrough; 911 case BR_STATE_LEARNING: 912 port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA; 913 break; 914 915 default: 916 port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA; 917 ocelot->bridge_fwd_mask &= ~BIT(port); 918 break; 919 } 920 921 ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port); 922 923 /* Apply FWD mask. The loop is needed to add/remove the current port as 924 * a source for the other ports. 925 */ 926 for (p = 0; p < ocelot->num_phys_ports; p++) { 927 if (ocelot->bridge_fwd_mask & BIT(p)) { 928 unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p); 929 930 for (i = 0; i < ocelot->num_phys_ports; i++) { 931 unsigned long bond_mask = ocelot->lags[i]; 932 933 if (!bond_mask) 934 continue; 935 936 if (bond_mask & BIT(p)) { 937 mask &= ~bond_mask; 938 break; 939 } 940 } 941 942 ocelot_write_rix(ocelot, mask, 943 ANA_PGID_PGID, PGID_SRC + p); 944 } else { 945 ocelot_write_rix(ocelot, 0, 946 ANA_PGID_PGID, PGID_SRC + p); 947 } 948 } 949 } 950 EXPORT_SYMBOL(ocelot_bridge_stp_state_set); 951 952 void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs) 953 { 954 unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000); 955 956 /* Setting AGE_PERIOD to zero effectively disables automatic aging, 957 * which is clearly not what our intention is. So avoid that. 958 */ 959 if (!age_period) 960 age_period = 1; 961 962 ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE); 963 } 964 EXPORT_SYMBOL(ocelot_set_ageing_time); 965 966 static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot, 967 const unsigned char *addr, 968 u16 vid) 969 { 970 struct ocelot_multicast *mc; 971 972 list_for_each_entry(mc, &ocelot->multicast, list) { 973 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid) 974 return mc; 975 } 976 977 return NULL; 978 } 979 980 static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr) 981 { 982 if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e) 983 return ENTRYTYPE_MACv4; 984 if (addr[0] == 0x33 && addr[1] == 0x33) 985 return ENTRYTYPE_MACv6; 986 return ENTRYTYPE_LOCKED; 987 } 988 989 static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index, 990 unsigned long ports) 991 { 992 struct ocelot_pgid *pgid; 993 994 pgid = kzalloc(sizeof(*pgid), GFP_KERNEL); 995 if (!pgid) 996 return ERR_PTR(-ENOMEM); 997 998 pgid->ports = ports; 999 pgid->index = index; 1000 refcount_set(&pgid->refcount, 1); 1001 list_add_tail(&pgid->list, &ocelot->pgids); 1002 1003 return pgid; 1004 } 1005 1006 static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid) 1007 { 1008 if (!refcount_dec_and_test(&pgid->refcount)) 1009 return; 1010 1011 list_del(&pgid->list); 1012 kfree(pgid); 1013 } 1014 1015 static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot, 1016 const struct ocelot_multicast *mc) 1017 { 1018 struct ocelot_pgid *pgid; 1019 int index; 1020 1021 /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and 1022 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the 1023 * destination mask table (PGID), the destination set is programmed as 1024 * part of the entry MAC address.", and the DEST_IDX is set to 0. 1025 */ 1026 if (mc->entry_type == ENTRYTYPE_MACv4 || 1027 mc->entry_type == ENTRYTYPE_MACv6) 1028 return ocelot_pgid_alloc(ocelot, 0, mc->ports); 1029 1030 list_for_each_entry(pgid, &ocelot->pgids, list) { 1031 /* When searching for a nonreserved multicast PGID, ignore the 1032 * dummy PGID of zero that we have for MACv4/MACv6 entries 1033 */ 1034 if (pgid->index && pgid->ports == mc->ports) { 1035 refcount_inc(&pgid->refcount); 1036 return pgid; 1037 } 1038 } 1039 1040 /* Search for a free index in the nonreserved multicast PGID area */ 1041 for_each_nonreserved_multicast_dest_pgid(ocelot, index) { 1042 bool used = false; 1043 1044 list_for_each_entry(pgid, &ocelot->pgids, list) { 1045 if (pgid->index == index) { 1046 used = true; 1047 break; 1048 } 1049 } 1050 1051 if (!used) 1052 return ocelot_pgid_alloc(ocelot, index, mc->ports); 1053 } 1054 1055 return ERR_PTR(-ENOSPC); 1056 } 1057 1058 static void ocelot_encode_ports_to_mdb(unsigned char *addr, 1059 struct ocelot_multicast *mc) 1060 { 1061 ether_addr_copy(addr, mc->addr); 1062 1063 if (mc->entry_type == ENTRYTYPE_MACv4) { 1064 addr[0] = 0; 1065 addr[1] = mc->ports >> 8; 1066 addr[2] = mc->ports & 0xff; 1067 } else if (mc->entry_type == ENTRYTYPE_MACv6) { 1068 addr[0] = mc->ports >> 8; 1069 addr[1] = mc->ports & 0xff; 1070 } 1071 } 1072 1073 int ocelot_port_mdb_add(struct ocelot *ocelot, int port, 1074 const struct switchdev_obj_port_mdb *mdb) 1075 { 1076 unsigned char addr[ETH_ALEN]; 1077 struct ocelot_multicast *mc; 1078 struct ocelot_pgid *pgid; 1079 u16 vid = mdb->vid; 1080 1081 if (port == ocelot->npi) 1082 port = ocelot->num_phys_ports; 1083 1084 mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 1085 if (!mc) { 1086 /* New entry */ 1087 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL); 1088 if (!mc) 1089 return -ENOMEM; 1090 1091 mc->entry_type = ocelot_classify_mdb(mdb->addr); 1092 ether_addr_copy(mc->addr, mdb->addr); 1093 mc->vid = vid; 1094 1095 list_add_tail(&mc->list, &ocelot->multicast); 1096 } else { 1097 /* Existing entry. Clean up the current port mask from 1098 * hardware now, because we'll be modifying it. 1099 */ 1100 ocelot_pgid_free(ocelot, mc->pgid); 1101 ocelot_encode_ports_to_mdb(addr, mc); 1102 ocelot_mact_forget(ocelot, addr, vid); 1103 } 1104 1105 mc->ports |= BIT(port); 1106 1107 pgid = ocelot_mdb_get_pgid(ocelot, mc); 1108 if (IS_ERR(pgid)) { 1109 dev_err(ocelot->dev, 1110 "Cannot allocate PGID for mdb %pM vid %d\n", 1111 mc->addr, mc->vid); 1112 devm_kfree(ocelot->dev, mc); 1113 return PTR_ERR(pgid); 1114 } 1115 mc->pgid = pgid; 1116 1117 ocelot_encode_ports_to_mdb(addr, mc); 1118 1119 if (mc->entry_type != ENTRYTYPE_MACv4 && 1120 mc->entry_type != ENTRYTYPE_MACv6) 1121 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 1122 pgid->index); 1123 1124 return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 1125 mc->entry_type); 1126 } 1127 EXPORT_SYMBOL(ocelot_port_mdb_add); 1128 1129 int ocelot_port_mdb_del(struct ocelot *ocelot, int port, 1130 const struct switchdev_obj_port_mdb *mdb) 1131 { 1132 unsigned char addr[ETH_ALEN]; 1133 struct ocelot_multicast *mc; 1134 struct ocelot_pgid *pgid; 1135 u16 vid = mdb->vid; 1136 1137 if (port == ocelot->npi) 1138 port = ocelot->num_phys_ports; 1139 1140 mc = ocelot_multicast_get(ocelot, mdb->addr, vid); 1141 if (!mc) 1142 return -ENOENT; 1143 1144 ocelot_encode_ports_to_mdb(addr, mc); 1145 ocelot_mact_forget(ocelot, addr, vid); 1146 1147 ocelot_pgid_free(ocelot, mc->pgid); 1148 mc->ports &= ~BIT(port); 1149 if (!mc->ports) { 1150 list_del(&mc->list); 1151 devm_kfree(ocelot->dev, mc); 1152 return 0; 1153 } 1154 1155 /* We have a PGID with fewer ports now */ 1156 pgid = ocelot_mdb_get_pgid(ocelot, mc); 1157 if (IS_ERR(pgid)) 1158 return PTR_ERR(pgid); 1159 mc->pgid = pgid; 1160 1161 ocelot_encode_ports_to_mdb(addr, mc); 1162 1163 if (mc->entry_type != ENTRYTYPE_MACv4 && 1164 mc->entry_type != ENTRYTYPE_MACv6) 1165 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, 1166 pgid->index); 1167 1168 return ocelot_mact_learn(ocelot, pgid->index, addr, vid, 1169 mc->entry_type); 1170 } 1171 EXPORT_SYMBOL(ocelot_port_mdb_del); 1172 1173 int ocelot_port_bridge_join(struct ocelot *ocelot, int port, 1174 struct net_device *bridge) 1175 { 1176 if (!ocelot->bridge_mask) { 1177 ocelot->hw_bridge_dev = bridge; 1178 } else { 1179 if (ocelot->hw_bridge_dev != bridge) 1180 /* This is adding the port to a second bridge, this is 1181 * unsupported */ 1182 return -ENODEV; 1183 } 1184 1185 ocelot->bridge_mask |= BIT(port); 1186 1187 return 0; 1188 } 1189 EXPORT_SYMBOL(ocelot_port_bridge_join); 1190 1191 int ocelot_port_bridge_leave(struct ocelot *ocelot, int port, 1192 struct net_device *bridge) 1193 { 1194 struct ocelot_vlan pvid = {0}, native_vlan = {0}; 1195 struct switchdev_trans trans; 1196 int ret; 1197 1198 ocelot->bridge_mask &= ~BIT(port); 1199 1200 if (!ocelot->bridge_mask) 1201 ocelot->hw_bridge_dev = NULL; 1202 1203 trans.ph_prepare = true; 1204 ret = ocelot_port_vlan_filtering(ocelot, port, false, &trans); 1205 if (ret) 1206 return ret; 1207 1208 trans.ph_prepare = false; 1209 ret = ocelot_port_vlan_filtering(ocelot, port, false, &trans); 1210 if (ret) 1211 return ret; 1212 1213 ocelot_port_set_pvid(ocelot, port, pvid); 1214 ocelot_port_set_native_vlan(ocelot, port, native_vlan); 1215 1216 return 0; 1217 } 1218 EXPORT_SYMBOL(ocelot_port_bridge_leave); 1219 1220 static void ocelot_set_aggr_pgids(struct ocelot *ocelot) 1221 { 1222 int i, port, lag; 1223 1224 /* Reset destination and aggregation PGIDS */ 1225 for_each_unicast_dest_pgid(ocelot, port) 1226 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 1227 1228 for_each_aggr_pgid(ocelot, i) 1229 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), 1230 ANA_PGID_PGID, i); 1231 1232 /* Now, set PGIDs for each LAG */ 1233 for (lag = 0; lag < ocelot->num_phys_ports; lag++) { 1234 unsigned long bond_mask; 1235 int aggr_count = 0; 1236 u8 aggr_idx[16]; 1237 1238 bond_mask = ocelot->lags[lag]; 1239 if (!bond_mask) 1240 continue; 1241 1242 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) { 1243 // Destination mask 1244 ocelot_write_rix(ocelot, bond_mask, 1245 ANA_PGID_PGID, port); 1246 aggr_idx[aggr_count] = port; 1247 aggr_count++; 1248 } 1249 1250 for_each_aggr_pgid(ocelot, i) { 1251 u32 ac; 1252 1253 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i); 1254 ac &= ~bond_mask; 1255 ac |= BIT(aggr_idx[i % aggr_count]); 1256 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i); 1257 } 1258 } 1259 } 1260 1261 static void ocelot_setup_lag(struct ocelot *ocelot, int lag) 1262 { 1263 unsigned long bond_mask = ocelot->lags[lag]; 1264 unsigned int p; 1265 1266 for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) { 1267 u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p); 1268 1269 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M; 1270 1271 /* Use lag port as logical port for port i */ 1272 ocelot_write_gix(ocelot, port_cfg | 1273 ANA_PORT_PORT_CFG_PORTID_VAL(lag), 1274 ANA_PORT_PORT_CFG, p); 1275 } 1276 } 1277 1278 int ocelot_port_lag_join(struct ocelot *ocelot, int port, 1279 struct net_device *bond) 1280 { 1281 struct net_device *ndev; 1282 u32 bond_mask = 0; 1283 int lag, lp; 1284 1285 rcu_read_lock(); 1286 for_each_netdev_in_bond_rcu(bond, ndev) { 1287 struct ocelot_port_private *priv = netdev_priv(ndev); 1288 1289 bond_mask |= BIT(priv->chip_port); 1290 } 1291 rcu_read_unlock(); 1292 1293 lp = __ffs(bond_mask); 1294 1295 /* If the new port is the lowest one, use it as the logical port from 1296 * now on 1297 */ 1298 if (port == lp) { 1299 lag = port; 1300 ocelot->lags[port] = bond_mask; 1301 bond_mask &= ~BIT(port); 1302 if (bond_mask) { 1303 lp = __ffs(bond_mask); 1304 ocelot->lags[lp] = 0; 1305 } 1306 } else { 1307 lag = lp; 1308 ocelot->lags[lp] |= BIT(port); 1309 } 1310 1311 ocelot_setup_lag(ocelot, lag); 1312 ocelot_set_aggr_pgids(ocelot); 1313 1314 return 0; 1315 } 1316 EXPORT_SYMBOL(ocelot_port_lag_join); 1317 1318 void ocelot_port_lag_leave(struct ocelot *ocelot, int port, 1319 struct net_device *bond) 1320 { 1321 u32 port_cfg; 1322 int i; 1323 1324 /* Remove port from any lag */ 1325 for (i = 0; i < ocelot->num_phys_ports; i++) 1326 ocelot->lags[i] &= ~BIT(port); 1327 1328 /* if it was the logical port of the lag, move the lag config to the 1329 * next port 1330 */ 1331 if (ocelot->lags[port]) { 1332 int n = __ffs(ocelot->lags[port]); 1333 1334 ocelot->lags[n] = ocelot->lags[port]; 1335 ocelot->lags[port] = 0; 1336 1337 ocelot_setup_lag(ocelot, n); 1338 } 1339 1340 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port); 1341 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M; 1342 ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port), 1343 ANA_PORT_PORT_CFG, port); 1344 1345 ocelot_set_aggr_pgids(ocelot); 1346 } 1347 EXPORT_SYMBOL(ocelot_port_lag_leave); 1348 1349 /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu. 1350 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG. 1351 * In the special case that it's the NPI port that we're configuring, the 1352 * length of the tag and optional prefix needs to be accounted for privately, 1353 * in order to be able to sustain communication at the requested @sdu. 1354 */ 1355 void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu) 1356 { 1357 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1358 int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN; 1359 int pause_start, pause_stop; 1360 int atop, atop_tot; 1361 1362 if (port == ocelot->npi) { 1363 maxlen += OCELOT_TAG_LEN; 1364 1365 if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT) 1366 maxlen += OCELOT_SHORT_PREFIX_LEN; 1367 else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG) 1368 maxlen += OCELOT_LONG_PREFIX_LEN; 1369 } 1370 1371 ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG); 1372 1373 /* Set Pause watermark hysteresis */ 1374 pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ; 1375 pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ; 1376 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START, 1377 pause_start); 1378 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP, 1379 pause_stop); 1380 1381 /* Tail dropping watermarks */ 1382 atop_tot = (ocelot->shared_queue_sz - 9 * maxlen) / 1383 OCELOT_BUFFER_CELL_SZ; 1384 atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ; 1385 ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port); 1386 ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG); 1387 } 1388 EXPORT_SYMBOL(ocelot_port_set_maxlen); 1389 1390 int ocelot_get_max_mtu(struct ocelot *ocelot, int port) 1391 { 1392 int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN; 1393 1394 if (port == ocelot->npi) { 1395 max_mtu -= OCELOT_TAG_LEN; 1396 1397 if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT) 1398 max_mtu -= OCELOT_SHORT_PREFIX_LEN; 1399 else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG) 1400 max_mtu -= OCELOT_LONG_PREFIX_LEN; 1401 } 1402 1403 return max_mtu; 1404 } 1405 EXPORT_SYMBOL(ocelot_get_max_mtu); 1406 1407 void ocelot_init_port(struct ocelot *ocelot, int port) 1408 { 1409 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1410 1411 skb_queue_head_init(&ocelot_port->tx_skbs); 1412 spin_lock_init(&ocelot_port->ts_id_lock); 1413 1414 /* Basic L2 initialization */ 1415 1416 /* Set MAC IFG Gaps 1417 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0 1418 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5 1419 */ 1420 ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5), 1421 DEV_MAC_IFG_CFG); 1422 1423 /* Load seed (0) and set MAC HDX late collision */ 1424 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) | 1425 DEV_MAC_HDX_CFG_SEED_LOAD, 1426 DEV_MAC_HDX_CFG); 1427 mdelay(1); 1428 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67), 1429 DEV_MAC_HDX_CFG); 1430 1431 /* Set Max Length and maximum tags allowed */ 1432 ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN); 1433 ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) | 1434 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA | 1435 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA | 1436 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA, 1437 DEV_MAC_TAGS_CFG); 1438 1439 /* Set SMAC of Pause frame (00:00:00:00:00:00) */ 1440 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG); 1441 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG); 1442 1443 /* Enable transmission of pause frames */ 1444 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 1445 1446 /* Drop frames with multicast source address */ 1447 ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 1448 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, 1449 ANA_PORT_DROP_CFG, port); 1450 1451 /* Set default VLAN and tag type to 8021Q. */ 1452 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q), 1453 REW_PORT_VLAN_CFG_PORT_TPID_M, 1454 REW_PORT_VLAN_CFG, port); 1455 1456 /* Enable vcap lookups */ 1457 ocelot_vcap_enable(ocelot, port); 1458 } 1459 EXPORT_SYMBOL(ocelot_init_port); 1460 1461 /* Configure and enable the CPU port module, which is a set of queues 1462 * accessible through register MMIO, frame DMA or Ethernet (in case 1463 * NPI mode is used). 1464 */ 1465 static void ocelot_cpu_port_init(struct ocelot *ocelot) 1466 { 1467 int cpu = ocelot->num_phys_ports; 1468 1469 /* The unicast destination PGID for the CPU port module is unused */ 1470 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu); 1471 /* Instead set up a multicast destination PGID for traffic copied to 1472 * the CPU. Whitelisted MAC addresses like the port netdevice MAC 1473 * addresses will be copied to the CPU via this PGID. 1474 */ 1475 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU); 1476 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA | 1477 ANA_PORT_PORT_CFG_PORTID_VAL(cpu), 1478 ANA_PORT_PORT_CFG, cpu); 1479 1480 /* Enable CPU port module */ 1481 ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 1482 /* CPU port Injection/Extraction configuration */ 1483 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR, 1484 ocelot->xtr_prefix); 1485 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR, 1486 ocelot->inj_prefix); 1487 1488 /* Configure the CPU port to be VLAN aware */ 1489 ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) | 1490 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | 1491 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1), 1492 ANA_PORT_VLAN_CFG, cpu); 1493 } 1494 1495 int ocelot_init(struct ocelot *ocelot) 1496 { 1497 char queue_name[32]; 1498 int i, ret; 1499 u32 port; 1500 1501 if (ocelot->ops->reset) { 1502 ret = ocelot->ops->reset(ocelot); 1503 if (ret) { 1504 dev_err(ocelot->dev, "Switch reset failed\n"); 1505 return ret; 1506 } 1507 } 1508 1509 ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports, 1510 sizeof(u32), GFP_KERNEL); 1511 if (!ocelot->lags) 1512 return -ENOMEM; 1513 1514 ocelot->stats = devm_kcalloc(ocelot->dev, 1515 ocelot->num_phys_ports * ocelot->num_stats, 1516 sizeof(u64), GFP_KERNEL); 1517 if (!ocelot->stats) 1518 return -ENOMEM; 1519 1520 mutex_init(&ocelot->stats_lock); 1521 mutex_init(&ocelot->ptp_lock); 1522 spin_lock_init(&ocelot->ptp_clock_lock); 1523 snprintf(queue_name, sizeof(queue_name), "%s-stats", 1524 dev_name(ocelot->dev)); 1525 ocelot->stats_queue = create_singlethread_workqueue(queue_name); 1526 if (!ocelot->stats_queue) 1527 return -ENOMEM; 1528 1529 ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0); 1530 if (!ocelot->owq) { 1531 destroy_workqueue(ocelot->stats_queue); 1532 return -ENOMEM; 1533 } 1534 1535 INIT_LIST_HEAD(&ocelot->multicast); 1536 INIT_LIST_HEAD(&ocelot->pgids); 1537 ocelot_mact_init(ocelot); 1538 ocelot_vlan_init(ocelot); 1539 ocelot_vcap_init(ocelot); 1540 ocelot_cpu_port_init(ocelot); 1541 1542 for (port = 0; port < ocelot->num_phys_ports; port++) { 1543 /* Clear all counters (5 groups) */ 1544 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) | 1545 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f), 1546 SYS_STAT_CFG); 1547 } 1548 1549 /* Only use S-Tag */ 1550 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG); 1551 1552 /* Aggregation mode */ 1553 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA | 1554 ANA_AGGR_CFG_AC_DMAC_ENA | 1555 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA | 1556 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG); 1557 1558 /* Set MAC age time to default value. The entry is aged after 1559 * 2*AGE_PERIOD 1560 */ 1561 ocelot_write(ocelot, 1562 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ), 1563 ANA_AUTOAGE); 1564 1565 /* Disable learning for frames discarded by VLAN ingress filtering */ 1566 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1); 1567 1568 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */ 1569 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA | 1570 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING); 1571 1572 /* Setup flooding PGIDs */ 1573 for (i = 0; i < ocelot->num_flooding_pgids; i++) 1574 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | 1575 ANA_FLOODING_FLD_BROADCAST(PGID_MC) | 1576 ANA_FLOODING_FLD_UNICAST(PGID_UC), 1577 ANA_FLOODING, i); 1578 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) | 1579 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) | 1580 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) | 1581 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC), 1582 ANA_FLOODING_IPMC); 1583 1584 for (port = 0; port < ocelot->num_phys_ports; port++) { 1585 /* Transmit the frame to the local port. */ 1586 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); 1587 /* Do not forward BPDU frames to the front ports. */ 1588 ocelot_write_gix(ocelot, 1589 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 1590 ANA_PORT_CPU_FWD_BPDU_CFG, 1591 port); 1592 /* Ensure bridging is disabled */ 1593 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port); 1594 } 1595 1596 /* Allow broadcast MAC frames. */ 1597 for_each_nonreserved_multicast_dest_pgid(ocelot, i) { 1598 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0)); 1599 1600 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); 1601 } 1602 ocelot_write_rix(ocelot, 1603 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)), 1604 ANA_PGID_PGID, PGID_MC); 1605 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4); 1606 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6); 1607 1608 /* Allow manual injection via DEVCPU_QS registers, and byte swap these 1609 * registers endianness. 1610 */ 1611 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP | 1612 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0); 1613 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP | 1614 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0); 1615 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) | 1616 ANA_CPUQ_CFG_CPUQ_LRN(2) | 1617 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) | 1618 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) | 1619 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) | 1620 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) | 1621 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) | 1622 ANA_CPUQ_CFG_CPUQ_IGMP(6) | 1623 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG); 1624 for (i = 0; i < 16; i++) 1625 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) | 1626 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6), 1627 ANA_CPUQ_8021_CFG, i); 1628 1629 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work); 1630 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, 1631 OCELOT_STATS_CHECK_DELAY); 1632 1633 return 0; 1634 } 1635 EXPORT_SYMBOL(ocelot_init); 1636 1637 void ocelot_deinit(struct ocelot *ocelot) 1638 { 1639 cancel_delayed_work(&ocelot->stats_work); 1640 destroy_workqueue(ocelot->stats_queue); 1641 destroy_workqueue(ocelot->owq); 1642 mutex_destroy(&ocelot->stats_lock); 1643 } 1644 EXPORT_SYMBOL(ocelot_deinit); 1645 1646 void ocelot_deinit_port(struct ocelot *ocelot, int port) 1647 { 1648 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1649 1650 skb_queue_purge(&ocelot_port->tx_skbs); 1651 } 1652 EXPORT_SYMBOL(ocelot_deinit_port); 1653 1654 MODULE_LICENSE("Dual MIT/GPL"); 1655