1 // SPDX-License-Identifier: (GPL-2.0 OR MIT) 2 /* 3 * DSA driver for: 4 * Hirschmann Hellcreek TSN switch. 5 * 6 * Copyright (C) 2019-2021 Linutronix GmbH 7 * Author Kurt Kanzenbach <kurt@linutronix.de> 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/device.h> 13 #include <linux/of.h> 14 #include <linux/of_mdio.h> 15 #include <linux/platform_device.h> 16 #include <linux/bitops.h> 17 #include <linux/if_bridge.h> 18 #include <linux/if_vlan.h> 19 #include <linux/etherdevice.h> 20 #include <linux/random.h> 21 #include <linux/iopoll.h> 22 #include <linux/mutex.h> 23 #include <linux/delay.h> 24 #include <net/dsa.h> 25 26 #include "hellcreek.h" 27 #include "hellcreek_ptp.h" 28 #include "hellcreek_hwtstamp.h" 29 30 static const struct hellcreek_counter hellcreek_counter[] = { 31 { 0x00, "RxFiltered", }, 32 { 0x01, "RxOctets1k", }, 33 { 0x02, "RxVTAG", }, 34 { 0x03, "RxL2BAD", }, 35 { 0x04, "RxOverloadDrop", }, 36 { 0x05, "RxUC", }, 37 { 0x06, "RxMC", }, 38 { 0x07, "RxBC", }, 39 { 0x08, "RxRS<64", }, 40 { 0x09, "RxRS64", }, 41 { 0x0a, "RxRS65_127", }, 42 { 0x0b, "RxRS128_255", }, 43 { 0x0c, "RxRS256_511", }, 44 { 0x0d, "RxRS512_1023", }, 45 { 0x0e, "RxRS1024_1518", }, 46 { 0x0f, "RxRS>1518", }, 47 { 0x10, "TxTailDropQueue0", }, 48 { 0x11, "TxTailDropQueue1", }, 49 { 0x12, "TxTailDropQueue2", }, 50 { 0x13, "TxTailDropQueue3", }, 51 { 0x14, "TxTailDropQueue4", }, 52 { 0x15, "TxTailDropQueue5", }, 53 { 0x16, "TxTailDropQueue6", }, 54 { 0x17, "TxTailDropQueue7", }, 55 { 0x18, "RxTrafficClass0", }, 56 { 0x19, "RxTrafficClass1", }, 57 { 0x1a, "RxTrafficClass2", }, 58 { 0x1b, "RxTrafficClass3", }, 59 { 0x1c, "RxTrafficClass4", }, 60 { 0x1d, "RxTrafficClass5", }, 61 { 0x1e, "RxTrafficClass6", }, 62 { 0x1f, "RxTrafficClass7", }, 63 { 0x21, "TxOctets1k", }, 64 { 0x22, "TxVTAG", }, 65 { 0x23, "TxL2BAD", }, 66 { 0x25, "TxUC", }, 67 { 0x26, "TxMC", }, 68 { 0x27, "TxBC", }, 69 { 0x28, "TxTS<64", }, 70 { 0x29, "TxTS64", }, 71 { 0x2a, "TxTS65_127", }, 72 { 0x2b, "TxTS128_255", }, 73 { 0x2c, "TxTS256_511", }, 74 { 0x2d, "TxTS512_1023", }, 75 { 0x2e, "TxTS1024_1518", }, 76 { 0x2f, "TxTS>1518", }, 77 { 0x30, "TxTrafficClassOverrun0", }, 78 { 0x31, "TxTrafficClassOverrun1", }, 79 { 0x32, "TxTrafficClassOverrun2", }, 80 { 0x33, "TxTrafficClassOverrun3", }, 81 { 0x34, "TxTrafficClassOverrun4", }, 82 { 0x35, "TxTrafficClassOverrun5", }, 83 { 0x36, "TxTrafficClassOverrun6", }, 84 { 0x37, "TxTrafficClassOverrun7", }, 85 { 0x38, "TxTrafficClass0", }, 86 { 0x39, "TxTrafficClass1", }, 87 { 0x3a, "TxTrafficClass2", }, 88 { 0x3b, "TxTrafficClass3", }, 89 { 0x3c, "TxTrafficClass4", }, 90 { 0x3d, "TxTrafficClass5", }, 91 { 0x3e, "TxTrafficClass6", }, 92 { 0x3f, "TxTrafficClass7", }, 93 }; 94 95 static u16 hellcreek_read(struct hellcreek *hellcreek, unsigned int offset) 96 { 97 return readw(hellcreek->base + offset); 98 } 99 100 static u16 hellcreek_read_ctrl(struct hellcreek *hellcreek) 101 { 102 return readw(hellcreek->base + HR_CTRL_C); 103 } 104 105 static u16 hellcreek_read_stat(struct hellcreek *hellcreek) 106 { 107 return readw(hellcreek->base + HR_SWSTAT); 108 } 109 110 static void hellcreek_write(struct hellcreek *hellcreek, u16 data, 111 unsigned int offset) 112 { 113 writew(data, hellcreek->base + offset); 114 } 115 116 static void hellcreek_select_port(struct hellcreek *hellcreek, int port) 117 { 118 u16 val = port << HR_PSEL_PTWSEL_SHIFT; 119 120 hellcreek_write(hellcreek, val, HR_PSEL); 121 } 122 123 static void hellcreek_select_prio(struct hellcreek *hellcreek, int prio) 124 { 125 u16 val = prio << HR_PSEL_PRTCWSEL_SHIFT; 126 127 hellcreek_write(hellcreek, val, HR_PSEL); 128 } 129 130 static void hellcreek_select_port_prio(struct hellcreek *hellcreek, int port, 131 int prio) 132 { 133 u16 val = port << HR_PSEL_PTWSEL_SHIFT; 134 135 val |= prio << HR_PSEL_PRTCWSEL_SHIFT; 136 137 hellcreek_write(hellcreek, val, HR_PSEL); 138 } 139 140 static void hellcreek_select_counter(struct hellcreek *hellcreek, int counter) 141 { 142 u16 val = counter << HR_CSEL_SHIFT; 143 144 hellcreek_write(hellcreek, val, HR_CSEL); 145 146 /* Data sheet states to wait at least 20 internal clock cycles */ 147 ndelay(200); 148 } 149 150 static void hellcreek_select_vlan(struct hellcreek *hellcreek, int vid, 151 bool pvid) 152 { 153 u16 val = 0; 154 155 /* Set pvid bit first */ 156 if (pvid) 157 val |= HR_VIDCFG_PVID; 158 hellcreek_write(hellcreek, val, HR_VIDCFG); 159 160 /* Set vlan */ 161 val |= vid << HR_VIDCFG_VID_SHIFT; 162 hellcreek_write(hellcreek, val, HR_VIDCFG); 163 } 164 165 static void hellcreek_select_tgd(struct hellcreek *hellcreek, int port) 166 { 167 u16 val = port << TR_TGDSEL_TDGSEL_SHIFT; 168 169 hellcreek_write(hellcreek, val, TR_TGDSEL); 170 } 171 172 static int hellcreek_wait_until_ready(struct hellcreek *hellcreek) 173 { 174 u16 val; 175 176 /* Wait up to 1ms, although 3 us should be enough */ 177 return readx_poll_timeout(hellcreek_read_ctrl, hellcreek, 178 val, val & HR_CTRL_C_READY, 179 3, 1000); 180 } 181 182 static int hellcreek_wait_until_transitioned(struct hellcreek *hellcreek) 183 { 184 u16 val; 185 186 return readx_poll_timeout_atomic(hellcreek_read_ctrl, hellcreek, 187 val, !(val & HR_CTRL_C_TRANSITION), 188 1, 1000); 189 } 190 191 static int hellcreek_wait_fdb_ready(struct hellcreek *hellcreek) 192 { 193 u16 val; 194 195 return readx_poll_timeout_atomic(hellcreek_read_stat, hellcreek, 196 val, !(val & HR_SWSTAT_BUSY), 197 1, 1000); 198 } 199 200 static int hellcreek_detect(struct hellcreek *hellcreek) 201 { 202 u16 id, rel_low, rel_high, date_low, date_high, tgd_ver; 203 u8 tgd_maj, tgd_min; 204 u32 rel, date; 205 206 id = hellcreek_read(hellcreek, HR_MODID_C); 207 rel_low = hellcreek_read(hellcreek, HR_REL_L_C); 208 rel_high = hellcreek_read(hellcreek, HR_REL_H_C); 209 date_low = hellcreek_read(hellcreek, HR_BLD_L_C); 210 date_high = hellcreek_read(hellcreek, HR_BLD_H_C); 211 tgd_ver = hellcreek_read(hellcreek, TR_TGDVER); 212 213 if (id != hellcreek->pdata->module_id) 214 return -ENODEV; 215 216 rel = rel_low | (rel_high << 16); 217 date = date_low | (date_high << 16); 218 tgd_maj = (tgd_ver & TR_TGDVER_REV_MAJ_MASK) >> TR_TGDVER_REV_MAJ_SHIFT; 219 tgd_min = (tgd_ver & TR_TGDVER_REV_MIN_MASK) >> TR_TGDVER_REV_MIN_SHIFT; 220 221 dev_info(hellcreek->dev, "Module ID=%02x Release=%04x Date=%04x TGD Version=%02x.%02x\n", 222 id, rel, date, tgd_maj, tgd_min); 223 224 return 0; 225 } 226 227 static void hellcreek_feature_detect(struct hellcreek *hellcreek) 228 { 229 u16 features; 230 231 features = hellcreek_read(hellcreek, HR_FEABITS0); 232 233 /* Only detect the size of the FDB table. The size and current 234 * utilization can be queried via devlink. 235 */ 236 hellcreek->fdb_entries = ((features & HR_FEABITS0_FDBBINS_MASK) >> 237 HR_FEABITS0_FDBBINS_SHIFT) * 32; 238 } 239 240 static enum dsa_tag_protocol hellcreek_get_tag_protocol(struct dsa_switch *ds, 241 int port, 242 enum dsa_tag_protocol mp) 243 { 244 return DSA_TAG_PROTO_HELLCREEK; 245 } 246 247 static int hellcreek_port_enable(struct dsa_switch *ds, int port, 248 struct phy_device *phy) 249 { 250 struct hellcreek *hellcreek = ds->priv; 251 struct hellcreek_port *hellcreek_port; 252 u16 val; 253 254 hellcreek_port = &hellcreek->ports[port]; 255 256 dev_dbg(hellcreek->dev, "Enable port %d\n", port); 257 258 mutex_lock(&hellcreek->reg_lock); 259 260 hellcreek_select_port(hellcreek, port); 261 val = hellcreek_port->ptcfg; 262 val |= HR_PTCFG_ADMIN_EN; 263 hellcreek_write(hellcreek, val, HR_PTCFG); 264 hellcreek_port->ptcfg = val; 265 266 mutex_unlock(&hellcreek->reg_lock); 267 268 return 0; 269 } 270 271 static void hellcreek_port_disable(struct dsa_switch *ds, int port) 272 { 273 struct hellcreek *hellcreek = ds->priv; 274 struct hellcreek_port *hellcreek_port; 275 u16 val; 276 277 hellcreek_port = &hellcreek->ports[port]; 278 279 dev_dbg(hellcreek->dev, "Disable port %d\n", port); 280 281 mutex_lock(&hellcreek->reg_lock); 282 283 hellcreek_select_port(hellcreek, port); 284 val = hellcreek_port->ptcfg; 285 val &= ~HR_PTCFG_ADMIN_EN; 286 hellcreek_write(hellcreek, val, HR_PTCFG); 287 hellcreek_port->ptcfg = val; 288 289 mutex_unlock(&hellcreek->reg_lock); 290 } 291 292 static void hellcreek_get_strings(struct dsa_switch *ds, int port, 293 u32 stringset, uint8_t *data) 294 { 295 int i; 296 297 for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) 298 ethtool_puts(&data, hellcreek_counter[i].name); 299 } 300 301 static int hellcreek_get_sset_count(struct dsa_switch *ds, int port, int sset) 302 { 303 if (sset != ETH_SS_STATS) 304 return 0; 305 306 return ARRAY_SIZE(hellcreek_counter); 307 } 308 309 static void hellcreek_get_ethtool_stats(struct dsa_switch *ds, int port, 310 uint64_t *data) 311 { 312 struct hellcreek *hellcreek = ds->priv; 313 struct hellcreek_port *hellcreek_port; 314 int i; 315 316 hellcreek_port = &hellcreek->ports[port]; 317 318 for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) { 319 const struct hellcreek_counter *counter = &hellcreek_counter[i]; 320 u8 offset = counter->offset + port * 64; 321 u16 high, low; 322 u64 value; 323 324 mutex_lock(&hellcreek->reg_lock); 325 326 hellcreek_select_counter(hellcreek, offset); 327 328 /* The registers are locked internally by selecting the 329 * counter. So low and high can be read without reading high 330 * again. 331 */ 332 high = hellcreek_read(hellcreek, HR_CRDH); 333 low = hellcreek_read(hellcreek, HR_CRDL); 334 value = ((u64)high << 16) | low; 335 336 hellcreek_port->counter_values[i] += value; 337 data[i] = hellcreek_port->counter_values[i]; 338 339 mutex_unlock(&hellcreek->reg_lock); 340 } 341 } 342 343 static u16 hellcreek_private_vid(int port) 344 { 345 return VLAN_N_VID - port + 1; 346 } 347 348 static int hellcreek_vlan_prepare(struct dsa_switch *ds, int port, 349 const struct switchdev_obj_port_vlan *vlan, 350 struct netlink_ext_ack *extack) 351 { 352 struct hellcreek *hellcreek = ds->priv; 353 int i; 354 355 dev_dbg(hellcreek->dev, "VLAN prepare for port %d\n", port); 356 357 /* Restriction: Make sure that nobody uses the "private" VLANs. These 358 * VLANs are internally used by the driver to ensure port 359 * separation. Thus, they cannot be used by someone else. 360 */ 361 for (i = 0; i < hellcreek->pdata->num_ports; ++i) { 362 const u16 restricted_vid = hellcreek_private_vid(i); 363 364 if (!dsa_is_user_port(ds, i)) 365 continue; 366 367 if (vlan->vid == restricted_vid) { 368 NL_SET_ERR_MSG_MOD(extack, "VID restricted by driver"); 369 return -EBUSY; 370 } 371 } 372 373 return 0; 374 } 375 376 static void hellcreek_select_vlan_params(struct hellcreek *hellcreek, int port, 377 int *shift, int *mask) 378 { 379 switch (port) { 380 case 0: 381 *shift = HR_VIDMBRCFG_P0MBR_SHIFT; 382 *mask = HR_VIDMBRCFG_P0MBR_MASK; 383 break; 384 case 1: 385 *shift = HR_VIDMBRCFG_P1MBR_SHIFT; 386 *mask = HR_VIDMBRCFG_P1MBR_MASK; 387 break; 388 case 2: 389 *shift = HR_VIDMBRCFG_P2MBR_SHIFT; 390 *mask = HR_VIDMBRCFG_P2MBR_MASK; 391 break; 392 case 3: 393 *shift = HR_VIDMBRCFG_P3MBR_SHIFT; 394 *mask = HR_VIDMBRCFG_P3MBR_MASK; 395 break; 396 default: 397 *shift = *mask = 0; 398 dev_err(hellcreek->dev, "Unknown port %d selected!\n", port); 399 } 400 } 401 402 static void hellcreek_apply_vlan(struct hellcreek *hellcreek, int port, u16 vid, 403 bool pvid, bool untagged) 404 { 405 int shift, mask; 406 u16 val; 407 408 dev_dbg(hellcreek->dev, "Apply VLAN: port=%d vid=%u pvid=%d untagged=%d", 409 port, vid, pvid, untagged); 410 411 mutex_lock(&hellcreek->reg_lock); 412 413 hellcreek_select_port(hellcreek, port); 414 hellcreek_select_vlan(hellcreek, vid, pvid); 415 416 /* Setup port vlan membership */ 417 hellcreek_select_vlan_params(hellcreek, port, &shift, &mask); 418 val = hellcreek->vidmbrcfg[vid]; 419 val &= ~mask; 420 if (untagged) 421 val |= HELLCREEK_VLAN_UNTAGGED_MEMBER << shift; 422 else 423 val |= HELLCREEK_VLAN_TAGGED_MEMBER << shift; 424 425 hellcreek_write(hellcreek, val, HR_VIDMBRCFG); 426 hellcreek->vidmbrcfg[vid] = val; 427 428 mutex_unlock(&hellcreek->reg_lock); 429 } 430 431 static void hellcreek_unapply_vlan(struct hellcreek *hellcreek, int port, 432 u16 vid) 433 { 434 int shift, mask; 435 u16 val; 436 437 dev_dbg(hellcreek->dev, "Unapply VLAN: port=%d vid=%u\n", port, vid); 438 439 mutex_lock(&hellcreek->reg_lock); 440 441 hellcreek_select_vlan(hellcreek, vid, false); 442 443 /* Setup port vlan membership */ 444 hellcreek_select_vlan_params(hellcreek, port, &shift, &mask); 445 val = hellcreek->vidmbrcfg[vid]; 446 val &= ~mask; 447 val |= HELLCREEK_VLAN_NO_MEMBER << shift; 448 449 hellcreek_write(hellcreek, val, HR_VIDMBRCFG); 450 hellcreek->vidmbrcfg[vid] = val; 451 452 mutex_unlock(&hellcreek->reg_lock); 453 } 454 455 static int hellcreek_vlan_add(struct dsa_switch *ds, int port, 456 const struct switchdev_obj_port_vlan *vlan, 457 struct netlink_ext_ack *extack) 458 { 459 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 460 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; 461 struct hellcreek *hellcreek = ds->priv; 462 int err; 463 464 err = hellcreek_vlan_prepare(ds, port, vlan, extack); 465 if (err) 466 return err; 467 468 dev_dbg(hellcreek->dev, "Add VLAN %d on port %d, %s, %s\n", 469 vlan->vid, port, untagged ? "untagged" : "tagged", 470 pvid ? "PVID" : "no PVID"); 471 472 hellcreek_apply_vlan(hellcreek, port, vlan->vid, pvid, untagged); 473 474 return 0; 475 } 476 477 static int hellcreek_vlan_del(struct dsa_switch *ds, int port, 478 const struct switchdev_obj_port_vlan *vlan) 479 { 480 struct hellcreek *hellcreek = ds->priv; 481 482 dev_dbg(hellcreek->dev, "Remove VLAN %d on port %d\n", vlan->vid, port); 483 484 hellcreek_unapply_vlan(hellcreek, port, vlan->vid); 485 486 return 0; 487 } 488 489 static void hellcreek_port_stp_state_set(struct dsa_switch *ds, int port, 490 u8 state) 491 { 492 struct hellcreek *hellcreek = ds->priv; 493 struct hellcreek_port *hellcreek_port; 494 const char *new_state; 495 u16 val; 496 497 mutex_lock(&hellcreek->reg_lock); 498 499 hellcreek_port = &hellcreek->ports[port]; 500 val = hellcreek_port->ptcfg; 501 502 switch (state) { 503 case BR_STATE_DISABLED: 504 new_state = "DISABLED"; 505 val |= HR_PTCFG_BLOCKED; 506 val &= ~HR_PTCFG_LEARNING_EN; 507 break; 508 case BR_STATE_BLOCKING: 509 new_state = "BLOCKING"; 510 val |= HR_PTCFG_BLOCKED; 511 val &= ~HR_PTCFG_LEARNING_EN; 512 break; 513 case BR_STATE_LISTENING: 514 new_state = "LISTENING"; 515 val |= HR_PTCFG_BLOCKED; 516 val &= ~HR_PTCFG_LEARNING_EN; 517 break; 518 case BR_STATE_LEARNING: 519 new_state = "LEARNING"; 520 val |= HR_PTCFG_BLOCKED; 521 val |= HR_PTCFG_LEARNING_EN; 522 break; 523 case BR_STATE_FORWARDING: 524 new_state = "FORWARDING"; 525 val &= ~HR_PTCFG_BLOCKED; 526 val |= HR_PTCFG_LEARNING_EN; 527 break; 528 default: 529 new_state = "UNKNOWN"; 530 } 531 532 hellcreek_select_port(hellcreek, port); 533 hellcreek_write(hellcreek, val, HR_PTCFG); 534 hellcreek_port->ptcfg = val; 535 536 mutex_unlock(&hellcreek->reg_lock); 537 538 dev_dbg(hellcreek->dev, "Configured STP state for port %d: %s\n", 539 port, new_state); 540 } 541 542 static void hellcreek_setup_ingressflt(struct hellcreek *hellcreek, int port, 543 bool enable) 544 { 545 struct hellcreek_port *hellcreek_port = &hellcreek->ports[port]; 546 u16 ptcfg; 547 548 mutex_lock(&hellcreek->reg_lock); 549 550 ptcfg = hellcreek_port->ptcfg; 551 552 if (enable) 553 ptcfg |= HR_PTCFG_INGRESSFLT; 554 else 555 ptcfg &= ~HR_PTCFG_INGRESSFLT; 556 557 hellcreek_select_port(hellcreek, port); 558 hellcreek_write(hellcreek, ptcfg, HR_PTCFG); 559 hellcreek_port->ptcfg = ptcfg; 560 561 mutex_unlock(&hellcreek->reg_lock); 562 } 563 564 static void hellcreek_setup_vlan_awareness(struct hellcreek *hellcreek, 565 bool enable) 566 { 567 u16 swcfg; 568 569 mutex_lock(&hellcreek->reg_lock); 570 571 swcfg = hellcreek->swcfg; 572 573 if (enable) 574 swcfg |= HR_SWCFG_VLAN_UNAWARE; 575 else 576 swcfg &= ~HR_SWCFG_VLAN_UNAWARE; 577 578 hellcreek_write(hellcreek, swcfg, HR_SWCFG); 579 580 mutex_unlock(&hellcreek->reg_lock); 581 } 582 583 /* Default setup for DSA: VLAN <X>: CPU and Port <X> egress untagged. */ 584 static void hellcreek_setup_vlan_membership(struct dsa_switch *ds, int port, 585 bool enabled) 586 { 587 const u16 vid = hellcreek_private_vid(port); 588 int upstream = dsa_upstream_port(ds, port); 589 struct hellcreek *hellcreek = ds->priv; 590 591 /* Apply vid to port as egress untagged and port vlan id */ 592 if (enabled) 593 hellcreek_apply_vlan(hellcreek, port, vid, true, true); 594 else 595 hellcreek_unapply_vlan(hellcreek, port, vid); 596 597 /* Apply vid to cpu port as well */ 598 if (enabled) 599 hellcreek_apply_vlan(hellcreek, upstream, vid, false, true); 600 else 601 hellcreek_unapply_vlan(hellcreek, upstream, vid); 602 } 603 604 static void hellcreek_port_set_ucast_flood(struct hellcreek *hellcreek, 605 int port, bool enable) 606 { 607 struct hellcreek_port *hellcreek_port; 608 u16 val; 609 610 hellcreek_port = &hellcreek->ports[port]; 611 612 dev_dbg(hellcreek->dev, "%s unicast flooding on port %d\n", 613 enable ? "Enable" : "Disable", port); 614 615 mutex_lock(&hellcreek->reg_lock); 616 617 hellcreek_select_port(hellcreek, port); 618 val = hellcreek_port->ptcfg; 619 if (enable) 620 val &= ~HR_PTCFG_UUC_FLT; 621 else 622 val |= HR_PTCFG_UUC_FLT; 623 hellcreek_write(hellcreek, val, HR_PTCFG); 624 hellcreek_port->ptcfg = val; 625 626 mutex_unlock(&hellcreek->reg_lock); 627 } 628 629 static void hellcreek_port_set_mcast_flood(struct hellcreek *hellcreek, 630 int port, bool enable) 631 { 632 struct hellcreek_port *hellcreek_port; 633 u16 val; 634 635 hellcreek_port = &hellcreek->ports[port]; 636 637 dev_dbg(hellcreek->dev, "%s multicast flooding on port %d\n", 638 enable ? "Enable" : "Disable", port); 639 640 mutex_lock(&hellcreek->reg_lock); 641 642 hellcreek_select_port(hellcreek, port); 643 val = hellcreek_port->ptcfg; 644 if (enable) 645 val &= ~HR_PTCFG_UMC_FLT; 646 else 647 val |= HR_PTCFG_UMC_FLT; 648 hellcreek_write(hellcreek, val, HR_PTCFG); 649 hellcreek_port->ptcfg = val; 650 651 mutex_unlock(&hellcreek->reg_lock); 652 } 653 654 static int hellcreek_pre_bridge_flags(struct dsa_switch *ds, int port, 655 struct switchdev_brport_flags flags, 656 struct netlink_ext_ack *extack) 657 { 658 if (flags.mask & ~(BR_FLOOD | BR_MCAST_FLOOD)) 659 return -EINVAL; 660 661 return 0; 662 } 663 664 static int hellcreek_bridge_flags(struct dsa_switch *ds, int port, 665 struct switchdev_brport_flags flags, 666 struct netlink_ext_ack *extack) 667 { 668 struct hellcreek *hellcreek = ds->priv; 669 670 if (flags.mask & BR_FLOOD) 671 hellcreek_port_set_ucast_flood(hellcreek, port, 672 !!(flags.val & BR_FLOOD)); 673 674 if (flags.mask & BR_MCAST_FLOOD) 675 hellcreek_port_set_mcast_flood(hellcreek, port, 676 !!(flags.val & BR_MCAST_FLOOD)); 677 678 return 0; 679 } 680 681 static int hellcreek_port_bridge_join(struct dsa_switch *ds, int port, 682 struct dsa_bridge bridge, 683 bool *tx_fwd_offload, 684 struct netlink_ext_ack *extack) 685 { 686 struct hellcreek *hellcreek = ds->priv; 687 688 dev_dbg(hellcreek->dev, "Port %d joins a bridge\n", port); 689 690 /* When joining a vlan_filtering bridge, keep the switch VLAN aware */ 691 if (!ds->vlan_filtering) 692 hellcreek_setup_vlan_awareness(hellcreek, false); 693 694 /* Drop private vlans */ 695 hellcreek_setup_vlan_membership(ds, port, false); 696 697 return 0; 698 } 699 700 static void hellcreek_port_bridge_leave(struct dsa_switch *ds, int port, 701 struct dsa_bridge bridge) 702 { 703 struct hellcreek *hellcreek = ds->priv; 704 705 dev_dbg(hellcreek->dev, "Port %d leaves a bridge\n", port); 706 707 /* Enable VLAN awareness */ 708 hellcreek_setup_vlan_awareness(hellcreek, true); 709 710 /* Enable private vlans */ 711 hellcreek_setup_vlan_membership(ds, port, true); 712 } 713 714 static int __hellcreek_fdb_add(struct hellcreek *hellcreek, 715 const struct hellcreek_fdb_entry *entry) 716 { 717 u16 meta = 0; 718 719 dev_dbg(hellcreek->dev, "Add static FDB entry: MAC=%pM, MASK=0x%02x, " 720 "OBT=%d, PASS_BLOCKED=%d, REPRIO_EN=%d, PRIO=%d\n", entry->mac, 721 entry->portmask, entry->is_obt, entry->pass_blocked, 722 entry->reprio_en, entry->reprio_tc); 723 724 /* Add mac address */ 725 hellcreek_write(hellcreek, entry->mac[1] | (entry->mac[0] << 8), HR_FDBWDH); 726 hellcreek_write(hellcreek, entry->mac[3] | (entry->mac[2] << 8), HR_FDBWDM); 727 hellcreek_write(hellcreek, entry->mac[5] | (entry->mac[4] << 8), HR_FDBWDL); 728 729 /* Meta data */ 730 meta |= entry->portmask << HR_FDBWRM0_PORTMASK_SHIFT; 731 if (entry->is_obt) 732 meta |= HR_FDBWRM0_OBT; 733 if (entry->pass_blocked) 734 meta |= HR_FDBWRM0_PASS_BLOCKED; 735 if (entry->reprio_en) { 736 meta |= HR_FDBWRM0_REPRIO_EN; 737 meta |= entry->reprio_tc << HR_FDBWRM0_REPRIO_TC_SHIFT; 738 } 739 hellcreek_write(hellcreek, meta, HR_FDBWRM0); 740 741 /* Commit */ 742 hellcreek_write(hellcreek, 0x00, HR_FDBWRCMD); 743 744 /* Wait until done */ 745 return hellcreek_wait_fdb_ready(hellcreek); 746 } 747 748 static int __hellcreek_fdb_del(struct hellcreek *hellcreek, 749 const struct hellcreek_fdb_entry *entry) 750 { 751 dev_dbg(hellcreek->dev, "Delete FDB entry: MAC=%pM!\n", entry->mac); 752 753 /* Delete by matching idx */ 754 hellcreek_write(hellcreek, entry->idx | HR_FDBWRCMD_FDBDEL, HR_FDBWRCMD); 755 756 /* Wait until done */ 757 return hellcreek_wait_fdb_ready(hellcreek); 758 } 759 760 static void hellcreek_populate_fdb_entry(struct hellcreek *hellcreek, 761 struct hellcreek_fdb_entry *entry, 762 size_t idx) 763 { 764 unsigned char addr[ETH_ALEN]; 765 u16 meta, mac; 766 767 /* Read values */ 768 meta = hellcreek_read(hellcreek, HR_FDBMDRD); 769 mac = hellcreek_read(hellcreek, HR_FDBRDL); 770 addr[5] = mac & 0xff; 771 addr[4] = (mac & 0xff00) >> 8; 772 mac = hellcreek_read(hellcreek, HR_FDBRDM); 773 addr[3] = mac & 0xff; 774 addr[2] = (mac & 0xff00) >> 8; 775 mac = hellcreek_read(hellcreek, HR_FDBRDH); 776 addr[1] = mac & 0xff; 777 addr[0] = (mac & 0xff00) >> 8; 778 779 /* Populate @entry */ 780 memcpy(entry->mac, addr, sizeof(addr)); 781 entry->idx = idx; 782 entry->portmask = (meta & HR_FDBMDRD_PORTMASK_MASK) >> 783 HR_FDBMDRD_PORTMASK_SHIFT; 784 entry->age = (meta & HR_FDBMDRD_AGE_MASK) >> 785 HR_FDBMDRD_AGE_SHIFT; 786 entry->is_obt = !!(meta & HR_FDBMDRD_OBT); 787 entry->pass_blocked = !!(meta & HR_FDBMDRD_PASS_BLOCKED); 788 entry->is_static = !!(meta & HR_FDBMDRD_STATIC); 789 entry->reprio_tc = (meta & HR_FDBMDRD_REPRIO_TC_MASK) >> 790 HR_FDBMDRD_REPRIO_TC_SHIFT; 791 entry->reprio_en = !!(meta & HR_FDBMDRD_REPRIO_EN); 792 } 793 794 /* Retrieve the index of a FDB entry by mac address. Currently we search through 795 * the complete table in hardware. If that's too slow, we might have to cache 796 * the complete FDB table in software. 797 */ 798 static int hellcreek_fdb_get(struct hellcreek *hellcreek, 799 const unsigned char *dest, 800 struct hellcreek_fdb_entry *entry) 801 { 802 size_t i; 803 804 /* Set read pointer to zero: The read of HR_FDBMAX (read-only register) 805 * should reset the internal pointer. But, that doesn't work. The vendor 806 * suggested a subsequent write as workaround. Same for HR_FDBRDH below. 807 */ 808 hellcreek_read(hellcreek, HR_FDBMAX); 809 hellcreek_write(hellcreek, 0x00, HR_FDBMAX); 810 811 /* We have to read the complete table, because the switch/driver might 812 * enter new entries anywhere. 813 */ 814 for (i = 0; i < hellcreek->fdb_entries; ++i) { 815 struct hellcreek_fdb_entry tmp = { 0 }; 816 817 /* Read entry */ 818 hellcreek_populate_fdb_entry(hellcreek, &tmp, i); 819 820 /* Force next entry */ 821 hellcreek_write(hellcreek, 0x00, HR_FDBRDH); 822 823 if (memcmp(tmp.mac, dest, ETH_ALEN)) 824 continue; 825 826 /* Match found */ 827 memcpy(entry, &tmp, sizeof(*entry)); 828 829 return 0; 830 } 831 832 return -ENOENT; 833 } 834 835 static int hellcreek_fdb_add(struct dsa_switch *ds, int port, 836 const unsigned char *addr, u16 vid, 837 struct dsa_db db) 838 { 839 struct hellcreek_fdb_entry entry = { 0 }; 840 struct hellcreek *hellcreek = ds->priv; 841 int ret; 842 843 dev_dbg(hellcreek->dev, "Add FDB entry for MAC=%pM\n", addr); 844 845 mutex_lock(&hellcreek->reg_lock); 846 847 ret = hellcreek_fdb_get(hellcreek, addr, &entry); 848 if (ret) { 849 /* Not found */ 850 memcpy(entry.mac, addr, sizeof(entry.mac)); 851 entry.portmask = BIT(port); 852 853 ret = __hellcreek_fdb_add(hellcreek, &entry); 854 if (ret) { 855 dev_err(hellcreek->dev, "Failed to add FDB entry!\n"); 856 goto out; 857 } 858 } else { 859 /* Found */ 860 ret = __hellcreek_fdb_del(hellcreek, &entry); 861 if (ret) { 862 dev_err(hellcreek->dev, "Failed to delete FDB entry!\n"); 863 goto out; 864 } 865 866 entry.portmask |= BIT(port); 867 868 ret = __hellcreek_fdb_add(hellcreek, &entry); 869 if (ret) { 870 dev_err(hellcreek->dev, "Failed to add FDB entry!\n"); 871 goto out; 872 } 873 } 874 875 out: 876 mutex_unlock(&hellcreek->reg_lock); 877 878 return ret; 879 } 880 881 static int hellcreek_fdb_del(struct dsa_switch *ds, int port, 882 const unsigned char *addr, u16 vid, 883 struct dsa_db db) 884 { 885 struct hellcreek_fdb_entry entry = { 0 }; 886 struct hellcreek *hellcreek = ds->priv; 887 int ret; 888 889 dev_dbg(hellcreek->dev, "Delete FDB entry for MAC=%pM\n", addr); 890 891 mutex_lock(&hellcreek->reg_lock); 892 893 ret = hellcreek_fdb_get(hellcreek, addr, &entry); 894 if (ret) { 895 /* Not found */ 896 dev_err(hellcreek->dev, "FDB entry for deletion not found!\n"); 897 } else { 898 /* Found */ 899 ret = __hellcreek_fdb_del(hellcreek, &entry); 900 if (ret) { 901 dev_err(hellcreek->dev, "Failed to delete FDB entry!\n"); 902 goto out; 903 } 904 905 entry.portmask &= ~BIT(port); 906 907 if (entry.portmask != 0x00) { 908 ret = __hellcreek_fdb_add(hellcreek, &entry); 909 if (ret) { 910 dev_err(hellcreek->dev, "Failed to add FDB entry!\n"); 911 goto out; 912 } 913 } 914 } 915 916 out: 917 mutex_unlock(&hellcreek->reg_lock); 918 919 return ret; 920 } 921 922 static int hellcreek_fdb_dump(struct dsa_switch *ds, int port, 923 dsa_fdb_dump_cb_t *cb, void *data) 924 { 925 struct hellcreek *hellcreek = ds->priv; 926 u16 entries; 927 int ret = 0; 928 size_t i; 929 930 mutex_lock(&hellcreek->reg_lock); 931 932 /* Set read pointer to zero: The read of HR_FDBMAX (read-only register) 933 * should reset the internal pointer. But, that doesn't work. The vendor 934 * suggested a subsequent write as workaround. Same for HR_FDBRDH below. 935 */ 936 entries = hellcreek_read(hellcreek, HR_FDBMAX); 937 hellcreek_write(hellcreek, 0x00, HR_FDBMAX); 938 939 dev_dbg(hellcreek->dev, "FDB dump for port %d, entries=%d!\n", port, entries); 940 941 /* Read table */ 942 for (i = 0; i < hellcreek->fdb_entries; ++i) { 943 struct hellcreek_fdb_entry entry = { 0 }; 944 945 /* Read entry */ 946 hellcreek_populate_fdb_entry(hellcreek, &entry, i); 947 948 /* Force next entry */ 949 hellcreek_write(hellcreek, 0x00, HR_FDBRDH); 950 951 /* Check valid */ 952 if (is_zero_ether_addr(entry.mac)) 953 continue; 954 955 /* Check port mask */ 956 if (!(entry.portmask & BIT(port))) 957 continue; 958 959 ret = cb(entry.mac, 0, entry.is_static, data); 960 if (ret) 961 break; 962 } 963 964 mutex_unlock(&hellcreek->reg_lock); 965 966 return ret; 967 } 968 969 static int hellcreek_vlan_filtering(struct dsa_switch *ds, int port, 970 bool vlan_filtering, 971 struct netlink_ext_ack *extack) 972 { 973 struct hellcreek *hellcreek = ds->priv; 974 975 dev_dbg(hellcreek->dev, "%s VLAN filtering on port %d\n", 976 vlan_filtering ? "Enable" : "Disable", port); 977 978 /* Configure port to drop packages with not known vids */ 979 hellcreek_setup_ingressflt(hellcreek, port, vlan_filtering); 980 981 /* Enable VLAN awareness on the switch. This save due to 982 * ds->vlan_filtering_is_global. 983 */ 984 hellcreek_setup_vlan_awareness(hellcreek, vlan_filtering); 985 986 return 0; 987 } 988 989 static int hellcreek_enable_ip_core(struct hellcreek *hellcreek) 990 { 991 int ret; 992 u16 val; 993 994 mutex_lock(&hellcreek->reg_lock); 995 996 val = hellcreek_read(hellcreek, HR_CTRL_C); 997 val |= HR_CTRL_C_ENABLE; 998 hellcreek_write(hellcreek, val, HR_CTRL_C); 999 ret = hellcreek_wait_until_transitioned(hellcreek); 1000 1001 mutex_unlock(&hellcreek->reg_lock); 1002 1003 return ret; 1004 } 1005 1006 static void hellcreek_setup_cpu_and_tunnel_port(struct hellcreek *hellcreek) 1007 { 1008 struct hellcreek_port *tunnel_port = &hellcreek->ports[TUNNEL_PORT]; 1009 struct hellcreek_port *cpu_port = &hellcreek->ports[CPU_PORT]; 1010 u16 ptcfg = 0; 1011 1012 ptcfg |= HR_PTCFG_LEARNING_EN | HR_PTCFG_ADMIN_EN; 1013 1014 mutex_lock(&hellcreek->reg_lock); 1015 1016 hellcreek_select_port(hellcreek, CPU_PORT); 1017 hellcreek_write(hellcreek, ptcfg, HR_PTCFG); 1018 1019 hellcreek_select_port(hellcreek, TUNNEL_PORT); 1020 hellcreek_write(hellcreek, ptcfg, HR_PTCFG); 1021 1022 cpu_port->ptcfg = ptcfg; 1023 tunnel_port->ptcfg = ptcfg; 1024 1025 mutex_unlock(&hellcreek->reg_lock); 1026 } 1027 1028 static void hellcreek_setup_tc_identity_mapping(struct hellcreek *hellcreek) 1029 { 1030 int i; 1031 1032 /* The switch has multiple egress queues per port. The queue is selected 1033 * via the PCP field in the VLAN header. The switch internally deals 1034 * with traffic classes instead of PCP values and this mapping is 1035 * configurable. 1036 * 1037 * The default mapping is (PCP - TC): 1038 * 7 - 7 1039 * 6 - 6 1040 * 5 - 5 1041 * 4 - 4 1042 * 3 - 3 1043 * 2 - 1 1044 * 1 - 0 1045 * 0 - 2 1046 * 1047 * The default should be an identity mapping. 1048 */ 1049 1050 for (i = 0; i < 8; ++i) { 1051 mutex_lock(&hellcreek->reg_lock); 1052 1053 hellcreek_select_prio(hellcreek, i); 1054 hellcreek_write(hellcreek, 1055 i << HR_PRTCCFG_PCP_TC_MAP_SHIFT, 1056 HR_PRTCCFG); 1057 1058 mutex_unlock(&hellcreek->reg_lock); 1059 } 1060 } 1061 1062 static int hellcreek_setup_fdb(struct hellcreek *hellcreek) 1063 { 1064 static struct hellcreek_fdb_entry l2_ptp = { 1065 /* MAC: 01-1B-19-00-00-00 */ 1066 .mac = { 0x01, 0x1b, 0x19, 0x00, 0x00, 0x00 }, 1067 .portmask = 0x03, /* Management ports */ 1068 .age = 0, 1069 .is_obt = 0, 1070 .pass_blocked = 0, 1071 .is_static = 1, 1072 .reprio_tc = 6, /* TC: 6 as per IEEE 802.1AS */ 1073 .reprio_en = 1, 1074 }; 1075 static struct hellcreek_fdb_entry udp4_ptp = { 1076 /* MAC: 01-00-5E-00-01-81 */ 1077 .mac = { 0x01, 0x00, 0x5e, 0x00, 0x01, 0x81 }, 1078 .portmask = 0x03, /* Management ports */ 1079 .age = 0, 1080 .is_obt = 0, 1081 .pass_blocked = 0, 1082 .is_static = 1, 1083 .reprio_tc = 6, 1084 .reprio_en = 1, 1085 }; 1086 static struct hellcreek_fdb_entry udp6_ptp = { 1087 /* MAC: 33-33-00-00-01-81 */ 1088 .mac = { 0x33, 0x33, 0x00, 0x00, 0x01, 0x81 }, 1089 .portmask = 0x03, /* Management ports */ 1090 .age = 0, 1091 .is_obt = 0, 1092 .pass_blocked = 0, 1093 .is_static = 1, 1094 .reprio_tc = 6, 1095 .reprio_en = 1, 1096 }; 1097 static struct hellcreek_fdb_entry l2_p2p = { 1098 /* MAC: 01-80-C2-00-00-0E */ 1099 .mac = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e }, 1100 .portmask = 0x03, /* Management ports */ 1101 .age = 0, 1102 .is_obt = 0, 1103 .pass_blocked = 1, 1104 .is_static = 1, 1105 .reprio_tc = 6, /* TC: 6 as per IEEE 802.1AS */ 1106 .reprio_en = 1, 1107 }; 1108 static struct hellcreek_fdb_entry udp4_p2p = { 1109 /* MAC: 01-00-5E-00-00-6B */ 1110 .mac = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x6b }, 1111 .portmask = 0x03, /* Management ports */ 1112 .age = 0, 1113 .is_obt = 0, 1114 .pass_blocked = 1, 1115 .is_static = 1, 1116 .reprio_tc = 6, 1117 .reprio_en = 1, 1118 }; 1119 static struct hellcreek_fdb_entry udp6_p2p = { 1120 /* MAC: 33-33-00-00-00-6B */ 1121 .mac = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x6b }, 1122 .portmask = 0x03, /* Management ports */ 1123 .age = 0, 1124 .is_obt = 0, 1125 .pass_blocked = 1, 1126 .is_static = 1, 1127 .reprio_tc = 6, 1128 .reprio_en = 1, 1129 }; 1130 static struct hellcreek_fdb_entry stp = { 1131 /* MAC: 01-80-C2-00-00-00 */ 1132 .mac = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 }, 1133 .portmask = 0x03, /* Management ports */ 1134 .age = 0, 1135 .is_obt = 0, 1136 .pass_blocked = 1, 1137 .is_static = 1, 1138 .reprio_tc = 6, 1139 .reprio_en = 1, 1140 }; 1141 int ret; 1142 1143 mutex_lock(&hellcreek->reg_lock); 1144 ret = __hellcreek_fdb_add(hellcreek, &l2_ptp); 1145 if (ret) 1146 goto out; 1147 ret = __hellcreek_fdb_add(hellcreek, &udp4_ptp); 1148 if (ret) 1149 goto out; 1150 ret = __hellcreek_fdb_add(hellcreek, &udp6_ptp); 1151 if (ret) 1152 goto out; 1153 ret = __hellcreek_fdb_add(hellcreek, &l2_p2p); 1154 if (ret) 1155 goto out; 1156 ret = __hellcreek_fdb_add(hellcreek, &udp4_p2p); 1157 if (ret) 1158 goto out; 1159 ret = __hellcreek_fdb_add(hellcreek, &udp6_p2p); 1160 if (ret) 1161 goto out; 1162 ret = __hellcreek_fdb_add(hellcreek, &stp); 1163 out: 1164 mutex_unlock(&hellcreek->reg_lock); 1165 1166 return ret; 1167 } 1168 1169 static int hellcreek_devlink_info_get(struct dsa_switch *ds, 1170 struct devlink_info_req *req, 1171 struct netlink_ext_ack *extack) 1172 { 1173 struct hellcreek *hellcreek = ds->priv; 1174 1175 return devlink_info_version_fixed_put(req, 1176 DEVLINK_INFO_VERSION_GENERIC_ASIC_ID, 1177 hellcreek->pdata->name); 1178 } 1179 1180 static u64 hellcreek_devlink_vlan_table_get(void *priv) 1181 { 1182 struct hellcreek *hellcreek = priv; 1183 u64 count = 0; 1184 int i; 1185 1186 mutex_lock(&hellcreek->reg_lock); 1187 for (i = 0; i < VLAN_N_VID; ++i) 1188 if (hellcreek->vidmbrcfg[i]) 1189 count++; 1190 mutex_unlock(&hellcreek->reg_lock); 1191 1192 return count; 1193 } 1194 1195 static u64 hellcreek_devlink_fdb_table_get(void *priv) 1196 { 1197 struct hellcreek *hellcreek = priv; 1198 u64 count = 0; 1199 1200 /* Reading this register has side effects. Synchronize against the other 1201 * FDB operations. 1202 */ 1203 mutex_lock(&hellcreek->reg_lock); 1204 count = hellcreek_read(hellcreek, HR_FDBMAX); 1205 mutex_unlock(&hellcreek->reg_lock); 1206 1207 return count; 1208 } 1209 1210 static int hellcreek_setup_devlink_resources(struct dsa_switch *ds) 1211 { 1212 struct devlink_resource_size_params size_vlan_params; 1213 struct devlink_resource_size_params size_fdb_params; 1214 struct hellcreek *hellcreek = ds->priv; 1215 int err; 1216 1217 devlink_resource_size_params_init(&size_vlan_params, VLAN_N_VID, 1218 VLAN_N_VID, 1219 1, DEVLINK_RESOURCE_UNIT_ENTRY); 1220 1221 devlink_resource_size_params_init(&size_fdb_params, 1222 hellcreek->fdb_entries, 1223 hellcreek->fdb_entries, 1224 1, DEVLINK_RESOURCE_UNIT_ENTRY); 1225 1226 err = dsa_devlink_resource_register(ds, "VLAN", VLAN_N_VID, 1227 HELLCREEK_DEVLINK_PARAM_ID_VLAN_TABLE, 1228 DEVLINK_RESOURCE_ID_PARENT_TOP, 1229 &size_vlan_params); 1230 if (err) 1231 goto out; 1232 1233 err = dsa_devlink_resource_register(ds, "FDB", hellcreek->fdb_entries, 1234 HELLCREEK_DEVLINK_PARAM_ID_FDB_TABLE, 1235 DEVLINK_RESOURCE_ID_PARENT_TOP, 1236 &size_fdb_params); 1237 if (err) 1238 goto out; 1239 1240 dsa_devlink_resource_occ_get_register(ds, 1241 HELLCREEK_DEVLINK_PARAM_ID_VLAN_TABLE, 1242 hellcreek_devlink_vlan_table_get, 1243 hellcreek); 1244 1245 dsa_devlink_resource_occ_get_register(ds, 1246 HELLCREEK_DEVLINK_PARAM_ID_FDB_TABLE, 1247 hellcreek_devlink_fdb_table_get, 1248 hellcreek); 1249 1250 return 0; 1251 1252 out: 1253 dsa_devlink_resources_unregister(ds); 1254 1255 return err; 1256 } 1257 1258 static int hellcreek_devlink_region_vlan_snapshot(struct devlink *dl, 1259 const struct devlink_region_ops *ops, 1260 struct netlink_ext_ack *extack, 1261 u8 **data) 1262 { 1263 struct hellcreek_devlink_vlan_entry *table, *entry; 1264 struct dsa_switch *ds = dsa_devlink_to_ds(dl); 1265 struct hellcreek *hellcreek = ds->priv; 1266 int i; 1267 1268 table = kcalloc(VLAN_N_VID, sizeof(*entry), GFP_KERNEL); 1269 if (!table) 1270 return -ENOMEM; 1271 1272 entry = table; 1273 1274 mutex_lock(&hellcreek->reg_lock); 1275 for (i = 0; i < VLAN_N_VID; ++i, ++entry) { 1276 entry->member = hellcreek->vidmbrcfg[i]; 1277 entry->vid = i; 1278 } 1279 mutex_unlock(&hellcreek->reg_lock); 1280 1281 *data = (u8 *)table; 1282 1283 return 0; 1284 } 1285 1286 static int hellcreek_devlink_region_fdb_snapshot(struct devlink *dl, 1287 const struct devlink_region_ops *ops, 1288 struct netlink_ext_ack *extack, 1289 u8 **data) 1290 { 1291 struct dsa_switch *ds = dsa_devlink_to_ds(dl); 1292 struct hellcreek_fdb_entry *table, *entry; 1293 struct hellcreek *hellcreek = ds->priv; 1294 size_t i; 1295 1296 table = kcalloc(hellcreek->fdb_entries, sizeof(*entry), GFP_KERNEL); 1297 if (!table) 1298 return -ENOMEM; 1299 1300 entry = table; 1301 1302 mutex_lock(&hellcreek->reg_lock); 1303 1304 /* Start table read */ 1305 hellcreek_read(hellcreek, HR_FDBMAX); 1306 hellcreek_write(hellcreek, 0x00, HR_FDBMAX); 1307 1308 for (i = 0; i < hellcreek->fdb_entries; ++i, ++entry) { 1309 /* Read current entry */ 1310 hellcreek_populate_fdb_entry(hellcreek, entry, i); 1311 1312 /* Advance read pointer */ 1313 hellcreek_write(hellcreek, 0x00, HR_FDBRDH); 1314 } 1315 1316 mutex_unlock(&hellcreek->reg_lock); 1317 1318 *data = (u8 *)table; 1319 1320 return 0; 1321 } 1322 1323 static struct devlink_region_ops hellcreek_region_vlan_ops = { 1324 .name = "vlan", 1325 .snapshot = hellcreek_devlink_region_vlan_snapshot, 1326 .destructor = kfree, 1327 }; 1328 1329 static struct devlink_region_ops hellcreek_region_fdb_ops = { 1330 .name = "fdb", 1331 .snapshot = hellcreek_devlink_region_fdb_snapshot, 1332 .destructor = kfree, 1333 }; 1334 1335 static int hellcreek_setup_devlink_regions(struct dsa_switch *ds) 1336 { 1337 struct hellcreek *hellcreek = ds->priv; 1338 struct devlink_region_ops *ops; 1339 struct devlink_region *region; 1340 u64 size; 1341 int ret; 1342 1343 /* VLAN table */ 1344 size = VLAN_N_VID * sizeof(struct hellcreek_devlink_vlan_entry); 1345 ops = &hellcreek_region_vlan_ops; 1346 1347 region = dsa_devlink_region_create(ds, ops, 1, size); 1348 if (IS_ERR(region)) 1349 return PTR_ERR(region); 1350 1351 hellcreek->vlan_region = region; 1352 1353 /* FDB table */ 1354 size = hellcreek->fdb_entries * sizeof(struct hellcreek_fdb_entry); 1355 ops = &hellcreek_region_fdb_ops; 1356 1357 region = dsa_devlink_region_create(ds, ops, 1, size); 1358 if (IS_ERR(region)) { 1359 ret = PTR_ERR(region); 1360 goto err_fdb; 1361 } 1362 1363 hellcreek->fdb_region = region; 1364 1365 return 0; 1366 1367 err_fdb: 1368 dsa_devlink_region_destroy(hellcreek->vlan_region); 1369 1370 return ret; 1371 } 1372 1373 static void hellcreek_teardown_devlink_regions(struct dsa_switch *ds) 1374 { 1375 struct hellcreek *hellcreek = ds->priv; 1376 1377 dsa_devlink_region_destroy(hellcreek->fdb_region); 1378 dsa_devlink_region_destroy(hellcreek->vlan_region); 1379 } 1380 1381 static int hellcreek_setup(struct dsa_switch *ds) 1382 { 1383 struct hellcreek *hellcreek = ds->priv; 1384 u16 swcfg = 0; 1385 int ret, i; 1386 1387 dev_dbg(hellcreek->dev, "Set up the switch\n"); 1388 1389 /* Let's go */ 1390 ret = hellcreek_enable_ip_core(hellcreek); 1391 if (ret) { 1392 dev_err(hellcreek->dev, "Failed to enable IP core!\n"); 1393 return ret; 1394 } 1395 1396 /* Enable CPU/Tunnel ports */ 1397 hellcreek_setup_cpu_and_tunnel_port(hellcreek); 1398 1399 /* Switch config: Keep defaults, enable FDB aging and learning and tag 1400 * each frame from/to cpu port for DSA tagging. Also enable the length 1401 * aware shaping mode. This eliminates the need for Qbv guard bands. 1402 */ 1403 swcfg |= HR_SWCFG_FDBAGE_EN | 1404 HR_SWCFG_FDBLRN_EN | 1405 HR_SWCFG_ALWAYS_OBT | 1406 (HR_SWCFG_LAS_ON << HR_SWCFG_LAS_MODE_SHIFT); 1407 hellcreek->swcfg = swcfg; 1408 hellcreek_write(hellcreek, swcfg, HR_SWCFG); 1409 1410 /* Initial vlan membership to reflect port separation */ 1411 for (i = 0; i < ds->num_ports; ++i) { 1412 if (!dsa_is_user_port(ds, i)) 1413 continue; 1414 1415 hellcreek_setup_vlan_membership(ds, i, true); 1416 } 1417 1418 /* Configure PCP <-> TC mapping */ 1419 hellcreek_setup_tc_identity_mapping(hellcreek); 1420 1421 /* The VLAN awareness is a global switch setting. Therefore, mixed vlan 1422 * filtering setups are not supported. 1423 */ 1424 ds->vlan_filtering_is_global = true; 1425 ds->needs_standalone_vlan_filtering = true; 1426 1427 /* Intercept _all_ PTP multicast traffic */ 1428 ret = hellcreek_setup_fdb(hellcreek); 1429 if (ret) { 1430 dev_err(hellcreek->dev, 1431 "Failed to insert static PTP FDB entries\n"); 1432 return ret; 1433 } 1434 1435 /* Register devlink resources with DSA */ 1436 ret = hellcreek_setup_devlink_resources(ds); 1437 if (ret) { 1438 dev_err(hellcreek->dev, 1439 "Failed to setup devlink resources!\n"); 1440 return ret; 1441 } 1442 1443 ret = hellcreek_setup_devlink_regions(ds); 1444 if (ret) { 1445 dev_err(hellcreek->dev, 1446 "Failed to setup devlink regions!\n"); 1447 goto err_regions; 1448 } 1449 1450 return 0; 1451 1452 err_regions: 1453 dsa_devlink_resources_unregister(ds); 1454 1455 return ret; 1456 } 1457 1458 static void hellcreek_teardown(struct dsa_switch *ds) 1459 { 1460 hellcreek_teardown_devlink_regions(ds); 1461 dsa_devlink_resources_unregister(ds); 1462 } 1463 1464 static void hellcreek_phylink_get_caps(struct dsa_switch *ds, int port, 1465 struct phylink_config *config) 1466 { 1467 struct hellcreek *hellcreek = ds->priv; 1468 1469 __set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces); 1470 __set_bit(PHY_INTERFACE_MODE_RGMII, config->supported_interfaces); 1471 1472 /* Include GMII - the hardware does not support this interface 1473 * mode, but it's the default interface mode for phylib, so we 1474 * need it for compatibility with existing DT. 1475 */ 1476 __set_bit(PHY_INTERFACE_MODE_GMII, config->supported_interfaces); 1477 1478 /* The MAC settings are a hardware configuration option and cannot be 1479 * changed at run time or by strapping. Therefore the attached PHYs 1480 * should be programmed to only advertise settings which are supported 1481 * by the hardware. 1482 */ 1483 if (hellcreek->pdata->is_100_mbits) 1484 config->mac_capabilities = MAC_100FD; 1485 else 1486 config->mac_capabilities = MAC_1000FD; 1487 } 1488 1489 static int 1490 hellcreek_port_prechangeupper(struct dsa_switch *ds, int port, 1491 struct netdev_notifier_changeupper_info *info) 1492 { 1493 struct hellcreek *hellcreek = ds->priv; 1494 bool used = true; 1495 int ret = -EBUSY; 1496 u16 vid; 1497 int i; 1498 1499 dev_dbg(hellcreek->dev, "Pre change upper for port %d\n", port); 1500 1501 /* 1502 * Deny VLAN devices on top of lan ports with the same VLAN ids, because 1503 * it breaks the port separation due to the private VLANs. Example: 1504 * 1505 * lan0.100 *and* lan1.100 cannot be used in parallel. However, lan0.99 1506 * and lan1.100 works. 1507 */ 1508 1509 if (!is_vlan_dev(info->upper_dev)) 1510 return 0; 1511 1512 vid = vlan_dev_vlan_id(info->upper_dev); 1513 1514 /* For all ports, check bitmaps */ 1515 mutex_lock(&hellcreek->vlan_lock); 1516 for (i = 0; i < hellcreek->pdata->num_ports; ++i) { 1517 if (!dsa_is_user_port(ds, i)) 1518 continue; 1519 1520 if (port == i) 1521 continue; 1522 1523 used = used && test_bit(vid, hellcreek->ports[i].vlan_dev_bitmap); 1524 } 1525 1526 if (used) 1527 goto out; 1528 1529 /* Update bitmap */ 1530 set_bit(vid, hellcreek->ports[port].vlan_dev_bitmap); 1531 1532 ret = 0; 1533 1534 out: 1535 mutex_unlock(&hellcreek->vlan_lock); 1536 1537 return ret; 1538 } 1539 1540 static void hellcreek_setup_maxsdu(struct hellcreek *hellcreek, int port, 1541 const struct tc_taprio_qopt_offload *schedule) 1542 { 1543 int tc; 1544 1545 for (tc = 0; tc < 8; ++tc) { 1546 u32 max_sdu = schedule->max_sdu[tc] + VLAN_ETH_HLEN - ETH_FCS_LEN; 1547 u16 val; 1548 1549 if (!schedule->max_sdu[tc]) 1550 continue; 1551 1552 dev_dbg(hellcreek->dev, "Configure max-sdu %u for tc %d on port %d\n", 1553 max_sdu, tc, port); 1554 1555 hellcreek_select_port_prio(hellcreek, port, tc); 1556 1557 val = (max_sdu & HR_PTPRTCCFG_MAXSDU_MASK) << HR_PTPRTCCFG_MAXSDU_SHIFT; 1558 1559 hellcreek_write(hellcreek, val, HR_PTPRTCCFG); 1560 } 1561 } 1562 1563 static void hellcreek_reset_maxsdu(struct hellcreek *hellcreek, int port) 1564 { 1565 int tc; 1566 1567 for (tc = 0; tc < 8; ++tc) { 1568 u16 val; 1569 1570 hellcreek_select_port_prio(hellcreek, port, tc); 1571 1572 val = (HELLCREEK_DEFAULT_MAX_SDU & HR_PTPRTCCFG_MAXSDU_MASK) 1573 << HR_PTPRTCCFG_MAXSDU_SHIFT; 1574 1575 hellcreek_write(hellcreek, val, HR_PTPRTCCFG); 1576 } 1577 } 1578 1579 static void hellcreek_setup_gcl(struct hellcreek *hellcreek, int port, 1580 const struct tc_taprio_qopt_offload *schedule) 1581 { 1582 const struct tc_taprio_sched_entry *cur, *initial, *next; 1583 size_t i; 1584 1585 cur = initial = &schedule->entries[0]; 1586 next = cur + 1; 1587 1588 for (i = 1; i <= schedule->num_entries; ++i) { 1589 u16 data; 1590 u8 gates; 1591 1592 if (i == schedule->num_entries) 1593 gates = initial->gate_mask ^ 1594 cur->gate_mask; 1595 else 1596 gates = next->gate_mask ^ 1597 cur->gate_mask; 1598 1599 data = gates; 1600 1601 if (i == schedule->num_entries) 1602 data |= TR_GCLDAT_GCLWRLAST; 1603 1604 /* Gates states */ 1605 hellcreek_write(hellcreek, data, TR_GCLDAT); 1606 1607 /* Time interval */ 1608 hellcreek_write(hellcreek, 1609 cur->interval & 0x0000ffff, 1610 TR_GCLTIL); 1611 hellcreek_write(hellcreek, 1612 (cur->interval & 0xffff0000) >> 16, 1613 TR_GCLTIH); 1614 1615 /* Commit entry */ 1616 data = ((i - 1) << TR_GCLCMD_GCLWRADR_SHIFT) | 1617 (initial->gate_mask << 1618 TR_GCLCMD_INIT_GATE_STATES_SHIFT); 1619 hellcreek_write(hellcreek, data, TR_GCLCMD); 1620 1621 cur++; 1622 next++; 1623 } 1624 } 1625 1626 static void hellcreek_set_cycle_time(struct hellcreek *hellcreek, 1627 const struct tc_taprio_qopt_offload *schedule) 1628 { 1629 u32 cycle_time = schedule->cycle_time; 1630 1631 hellcreek_write(hellcreek, cycle_time & 0x0000ffff, TR_CTWRL); 1632 hellcreek_write(hellcreek, (cycle_time & 0xffff0000) >> 16, TR_CTWRH); 1633 } 1634 1635 static void hellcreek_switch_schedule(struct hellcreek *hellcreek, 1636 ktime_t start_time) 1637 { 1638 struct timespec64 ts = ktime_to_timespec64(start_time); 1639 1640 /* Start schedule at this point of time */ 1641 hellcreek_write(hellcreek, ts.tv_nsec & 0x0000ffff, TR_ESTWRL); 1642 hellcreek_write(hellcreek, (ts.tv_nsec & 0xffff0000) >> 16, TR_ESTWRH); 1643 1644 /* Arm timer, set seconds and switch schedule */ 1645 hellcreek_write(hellcreek, TR_ESTCMD_ESTARM | TR_ESTCMD_ESTSWCFG | 1646 ((ts.tv_sec & TR_ESTCMD_ESTSEC_MASK) << 1647 TR_ESTCMD_ESTSEC_SHIFT), TR_ESTCMD); 1648 } 1649 1650 static bool hellcreek_schedule_startable(struct hellcreek *hellcreek, int port) 1651 { 1652 struct hellcreek_port *hellcreek_port = &hellcreek->ports[port]; 1653 s64 base_time_ns, current_ns; 1654 1655 /* The switch allows a schedule to be started only eight seconds within 1656 * the future. Therefore, check the current PTP time if the schedule is 1657 * startable or not. 1658 */ 1659 1660 /* Use the "cached" time. That should be alright, as it's updated quite 1661 * frequently in the PTP code. 1662 */ 1663 mutex_lock(&hellcreek->ptp_lock); 1664 current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts; 1665 mutex_unlock(&hellcreek->ptp_lock); 1666 1667 /* Calculate difference to admin base time */ 1668 base_time_ns = ktime_to_ns(hellcreek_port->current_schedule->base_time); 1669 1670 return base_time_ns - current_ns < (s64)4 * NSEC_PER_SEC; 1671 } 1672 1673 static void hellcreek_start_schedule(struct hellcreek *hellcreek, int port) 1674 { 1675 struct hellcreek_port *hellcreek_port = &hellcreek->ports[port]; 1676 ktime_t base_time, current_time; 1677 s64 current_ns; 1678 u32 cycle_time; 1679 1680 /* First select port */ 1681 hellcreek_select_tgd(hellcreek, port); 1682 1683 /* Forward base time into the future if needed */ 1684 mutex_lock(&hellcreek->ptp_lock); 1685 current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts; 1686 mutex_unlock(&hellcreek->ptp_lock); 1687 1688 current_time = ns_to_ktime(current_ns); 1689 base_time = hellcreek_port->current_schedule->base_time; 1690 cycle_time = hellcreek_port->current_schedule->cycle_time; 1691 1692 if (ktime_compare(current_time, base_time) > 0) { 1693 s64 n; 1694 1695 n = div64_s64(ktime_sub_ns(current_time, base_time), 1696 cycle_time); 1697 base_time = ktime_add_ns(base_time, (n + 1) * cycle_time); 1698 } 1699 1700 /* Set admin base time and switch schedule */ 1701 hellcreek_switch_schedule(hellcreek, base_time); 1702 1703 taprio_offload_free(hellcreek_port->current_schedule); 1704 hellcreek_port->current_schedule = NULL; 1705 1706 dev_dbg(hellcreek->dev, "Armed EST timer for port %d\n", 1707 hellcreek_port->port); 1708 } 1709 1710 static void hellcreek_check_schedule(struct work_struct *work) 1711 { 1712 struct delayed_work *dw = to_delayed_work(work); 1713 struct hellcreek_port *hellcreek_port; 1714 struct hellcreek *hellcreek; 1715 bool startable; 1716 1717 hellcreek_port = dw_to_hellcreek_port(dw); 1718 hellcreek = hellcreek_port->hellcreek; 1719 1720 mutex_lock(&hellcreek->reg_lock); 1721 1722 /* Check starting time */ 1723 startable = hellcreek_schedule_startable(hellcreek, 1724 hellcreek_port->port); 1725 if (startable) { 1726 hellcreek_start_schedule(hellcreek, hellcreek_port->port); 1727 mutex_unlock(&hellcreek->reg_lock); 1728 return; 1729 } 1730 1731 mutex_unlock(&hellcreek->reg_lock); 1732 1733 /* Reschedule */ 1734 schedule_delayed_work(&hellcreek_port->schedule_work, 1735 HELLCREEK_SCHEDULE_PERIOD); 1736 } 1737 1738 static int hellcreek_port_set_schedule(struct dsa_switch *ds, int port, 1739 struct tc_taprio_qopt_offload *taprio) 1740 { 1741 struct hellcreek *hellcreek = ds->priv; 1742 struct hellcreek_port *hellcreek_port; 1743 bool startable; 1744 u16 ctrl; 1745 1746 hellcreek_port = &hellcreek->ports[port]; 1747 1748 dev_dbg(hellcreek->dev, "Configure traffic schedule on port %d\n", 1749 port); 1750 1751 /* First cancel delayed work */ 1752 cancel_delayed_work_sync(&hellcreek_port->schedule_work); 1753 1754 mutex_lock(&hellcreek->reg_lock); 1755 1756 if (hellcreek_port->current_schedule) { 1757 taprio_offload_free(hellcreek_port->current_schedule); 1758 hellcreek_port->current_schedule = NULL; 1759 } 1760 hellcreek_port->current_schedule = taprio_offload_get(taprio); 1761 1762 /* Configure max sdu */ 1763 hellcreek_setup_maxsdu(hellcreek, port, hellcreek_port->current_schedule); 1764 1765 /* Select tdg */ 1766 hellcreek_select_tgd(hellcreek, port); 1767 1768 /* Enable gating and keep defaults */ 1769 ctrl = (0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT) | TR_TGDCTRL_GATE_EN; 1770 hellcreek_write(hellcreek, ctrl, TR_TGDCTRL); 1771 1772 /* Cancel pending schedule */ 1773 hellcreek_write(hellcreek, 0x00, TR_ESTCMD); 1774 1775 /* Setup a new schedule */ 1776 hellcreek_setup_gcl(hellcreek, port, hellcreek_port->current_schedule); 1777 1778 /* Configure cycle time */ 1779 hellcreek_set_cycle_time(hellcreek, hellcreek_port->current_schedule); 1780 1781 /* Check starting time */ 1782 startable = hellcreek_schedule_startable(hellcreek, port); 1783 if (startable) { 1784 hellcreek_start_schedule(hellcreek, port); 1785 mutex_unlock(&hellcreek->reg_lock); 1786 return 0; 1787 } 1788 1789 mutex_unlock(&hellcreek->reg_lock); 1790 1791 /* Schedule periodic schedule check */ 1792 schedule_delayed_work(&hellcreek_port->schedule_work, 1793 HELLCREEK_SCHEDULE_PERIOD); 1794 1795 return 0; 1796 } 1797 1798 static int hellcreek_port_del_schedule(struct dsa_switch *ds, int port) 1799 { 1800 struct hellcreek *hellcreek = ds->priv; 1801 struct hellcreek_port *hellcreek_port; 1802 1803 hellcreek_port = &hellcreek->ports[port]; 1804 1805 dev_dbg(hellcreek->dev, "Remove traffic schedule on port %d\n", port); 1806 1807 /* First cancel delayed work */ 1808 cancel_delayed_work_sync(&hellcreek_port->schedule_work); 1809 1810 mutex_lock(&hellcreek->reg_lock); 1811 1812 if (hellcreek_port->current_schedule) { 1813 taprio_offload_free(hellcreek_port->current_schedule); 1814 hellcreek_port->current_schedule = NULL; 1815 } 1816 1817 /* Reset max sdu */ 1818 hellcreek_reset_maxsdu(hellcreek, port); 1819 1820 /* Select tgd */ 1821 hellcreek_select_tgd(hellcreek, port); 1822 1823 /* Disable gating and return to regular switching flow */ 1824 hellcreek_write(hellcreek, 0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT, 1825 TR_TGDCTRL); 1826 1827 mutex_unlock(&hellcreek->reg_lock); 1828 1829 return 0; 1830 } 1831 1832 static bool hellcreek_validate_schedule(struct hellcreek *hellcreek, 1833 struct tc_taprio_qopt_offload *schedule) 1834 { 1835 size_t i; 1836 1837 /* Does this hellcreek version support Qbv in hardware? */ 1838 if (!hellcreek->pdata->qbv_support) 1839 return false; 1840 1841 /* cycle time can only be 32bit */ 1842 if (schedule->cycle_time > (u32)-1) 1843 return false; 1844 1845 /* cycle time extension is not supported */ 1846 if (schedule->cycle_time_extension) 1847 return false; 1848 1849 /* Only set command is supported */ 1850 for (i = 0; i < schedule->num_entries; ++i) 1851 if (schedule->entries[i].command != TC_TAPRIO_CMD_SET_GATES) 1852 return false; 1853 1854 return true; 1855 } 1856 1857 static int hellcreek_tc_query_caps(struct tc_query_caps_base *base) 1858 { 1859 switch (base->type) { 1860 case TC_SETUP_QDISC_TAPRIO: { 1861 struct tc_taprio_caps *caps = base->caps; 1862 1863 caps->supports_queue_max_sdu = true; 1864 1865 return 0; 1866 } 1867 default: 1868 return -EOPNOTSUPP; 1869 } 1870 } 1871 1872 static int hellcreek_port_setup_tc(struct dsa_switch *ds, int port, 1873 enum tc_setup_type type, void *type_data) 1874 { 1875 struct hellcreek *hellcreek = ds->priv; 1876 1877 switch (type) { 1878 case TC_QUERY_CAPS: 1879 return hellcreek_tc_query_caps(type_data); 1880 case TC_SETUP_QDISC_TAPRIO: { 1881 struct tc_taprio_qopt_offload *taprio = type_data; 1882 1883 switch (taprio->cmd) { 1884 case TAPRIO_CMD_REPLACE: 1885 if (!hellcreek_validate_schedule(hellcreek, taprio)) 1886 return -EOPNOTSUPP; 1887 1888 return hellcreek_port_set_schedule(ds, port, taprio); 1889 case TAPRIO_CMD_DESTROY: 1890 return hellcreek_port_del_schedule(ds, port); 1891 default: 1892 return -EOPNOTSUPP; 1893 } 1894 } 1895 default: 1896 return -EOPNOTSUPP; 1897 } 1898 } 1899 1900 static const struct dsa_switch_ops hellcreek_ds_ops = { 1901 .devlink_info_get = hellcreek_devlink_info_get, 1902 .get_ethtool_stats = hellcreek_get_ethtool_stats, 1903 .get_sset_count = hellcreek_get_sset_count, 1904 .get_strings = hellcreek_get_strings, 1905 .get_tag_protocol = hellcreek_get_tag_protocol, 1906 .get_ts_info = hellcreek_get_ts_info, 1907 .phylink_get_caps = hellcreek_phylink_get_caps, 1908 .port_bridge_flags = hellcreek_bridge_flags, 1909 .port_bridge_join = hellcreek_port_bridge_join, 1910 .port_bridge_leave = hellcreek_port_bridge_leave, 1911 .port_disable = hellcreek_port_disable, 1912 .port_enable = hellcreek_port_enable, 1913 .port_fdb_add = hellcreek_fdb_add, 1914 .port_fdb_del = hellcreek_fdb_del, 1915 .port_fdb_dump = hellcreek_fdb_dump, 1916 .port_hwtstamp_set = hellcreek_port_hwtstamp_set, 1917 .port_hwtstamp_get = hellcreek_port_hwtstamp_get, 1918 .port_pre_bridge_flags = hellcreek_pre_bridge_flags, 1919 .port_prechangeupper = hellcreek_port_prechangeupper, 1920 .port_rxtstamp = hellcreek_port_rxtstamp, 1921 .port_setup_tc = hellcreek_port_setup_tc, 1922 .port_stp_state_set = hellcreek_port_stp_state_set, 1923 .port_txtstamp = hellcreek_port_txtstamp, 1924 .port_vlan_add = hellcreek_vlan_add, 1925 .port_vlan_del = hellcreek_vlan_del, 1926 .port_vlan_filtering = hellcreek_vlan_filtering, 1927 .setup = hellcreek_setup, 1928 .teardown = hellcreek_teardown, 1929 }; 1930 1931 static int hellcreek_probe(struct platform_device *pdev) 1932 { 1933 struct device *dev = &pdev->dev; 1934 struct hellcreek *hellcreek; 1935 struct resource *res; 1936 int ret, i; 1937 1938 hellcreek = devm_kzalloc(dev, sizeof(*hellcreek), GFP_KERNEL); 1939 if (!hellcreek) 1940 return -ENOMEM; 1941 1942 hellcreek->vidmbrcfg = devm_kcalloc(dev, VLAN_N_VID, 1943 sizeof(*hellcreek->vidmbrcfg), 1944 GFP_KERNEL); 1945 if (!hellcreek->vidmbrcfg) 1946 return -ENOMEM; 1947 1948 hellcreek->pdata = of_device_get_match_data(dev); 1949 1950 hellcreek->ports = devm_kcalloc(dev, hellcreek->pdata->num_ports, 1951 sizeof(*hellcreek->ports), 1952 GFP_KERNEL); 1953 if (!hellcreek->ports) 1954 return -ENOMEM; 1955 1956 for (i = 0; i < hellcreek->pdata->num_ports; ++i) { 1957 struct hellcreek_port *port = &hellcreek->ports[i]; 1958 1959 port->counter_values = 1960 devm_kcalloc(dev, 1961 ARRAY_SIZE(hellcreek_counter), 1962 sizeof(*port->counter_values), 1963 GFP_KERNEL); 1964 if (!port->counter_values) 1965 return -ENOMEM; 1966 1967 port->vlan_dev_bitmap = devm_bitmap_zalloc(dev, VLAN_N_VID, 1968 GFP_KERNEL); 1969 if (!port->vlan_dev_bitmap) 1970 return -ENOMEM; 1971 1972 port->hellcreek = hellcreek; 1973 port->port = i; 1974 1975 INIT_DELAYED_WORK(&port->schedule_work, 1976 hellcreek_check_schedule); 1977 } 1978 1979 mutex_init(&hellcreek->reg_lock); 1980 mutex_init(&hellcreek->vlan_lock); 1981 mutex_init(&hellcreek->ptp_lock); 1982 1983 hellcreek->dev = dev; 1984 1985 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tsn"); 1986 if (!res) { 1987 dev_err(dev, "No memory region provided!\n"); 1988 return -ENODEV; 1989 } 1990 1991 hellcreek->base = devm_ioremap_resource(dev, res); 1992 if (IS_ERR(hellcreek->base)) 1993 return PTR_ERR(hellcreek->base); 1994 1995 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ptp"); 1996 if (!res) { 1997 dev_err(dev, "No PTP memory region provided!\n"); 1998 return -ENODEV; 1999 } 2000 2001 hellcreek->ptp_base = devm_ioremap_resource(dev, res); 2002 if (IS_ERR(hellcreek->ptp_base)) 2003 return PTR_ERR(hellcreek->ptp_base); 2004 2005 ret = hellcreek_detect(hellcreek); 2006 if (ret) { 2007 dev_err(dev, "No (known) chip found!\n"); 2008 return ret; 2009 } 2010 2011 ret = hellcreek_wait_until_ready(hellcreek); 2012 if (ret) { 2013 dev_err(dev, "Switch didn't become ready!\n"); 2014 return ret; 2015 } 2016 2017 hellcreek_feature_detect(hellcreek); 2018 2019 hellcreek->ds = devm_kzalloc(dev, sizeof(*hellcreek->ds), GFP_KERNEL); 2020 if (!hellcreek->ds) 2021 return -ENOMEM; 2022 2023 hellcreek->ds->dev = dev; 2024 hellcreek->ds->priv = hellcreek; 2025 hellcreek->ds->ops = &hellcreek_ds_ops; 2026 hellcreek->ds->num_ports = hellcreek->pdata->num_ports; 2027 hellcreek->ds->num_tx_queues = HELLCREEK_NUM_EGRESS_QUEUES; 2028 2029 ret = dsa_register_switch(hellcreek->ds); 2030 if (ret) { 2031 dev_err_probe(dev, ret, "Unable to register switch\n"); 2032 return ret; 2033 } 2034 2035 ret = hellcreek_ptp_setup(hellcreek); 2036 if (ret) { 2037 dev_err(dev, "Failed to setup PTP!\n"); 2038 goto err_ptp_setup; 2039 } 2040 2041 ret = hellcreek_hwtstamp_setup(hellcreek); 2042 if (ret) { 2043 dev_err(dev, "Failed to setup hardware timestamping!\n"); 2044 goto err_tstamp_setup; 2045 } 2046 2047 platform_set_drvdata(pdev, hellcreek); 2048 2049 return 0; 2050 2051 err_tstamp_setup: 2052 hellcreek_ptp_free(hellcreek); 2053 err_ptp_setup: 2054 dsa_unregister_switch(hellcreek->ds); 2055 2056 return ret; 2057 } 2058 2059 static void hellcreek_remove(struct platform_device *pdev) 2060 { 2061 struct hellcreek *hellcreek = platform_get_drvdata(pdev); 2062 2063 if (!hellcreek) 2064 return; 2065 2066 hellcreek_hwtstamp_free(hellcreek); 2067 hellcreek_ptp_free(hellcreek); 2068 dsa_unregister_switch(hellcreek->ds); 2069 } 2070 2071 static void hellcreek_shutdown(struct platform_device *pdev) 2072 { 2073 struct hellcreek *hellcreek = platform_get_drvdata(pdev); 2074 2075 if (!hellcreek) 2076 return; 2077 2078 dsa_switch_shutdown(hellcreek->ds); 2079 2080 platform_set_drvdata(pdev, NULL); 2081 } 2082 2083 static const struct hellcreek_platform_data de1soc_r1_pdata = { 2084 .name = "r4c30", 2085 .num_ports = 4, 2086 .is_100_mbits = 1, 2087 .qbv_support = 1, 2088 .qbv_on_cpu_port = 1, 2089 .qbu_support = 0, 2090 .module_id = 0x4c30, 2091 }; 2092 2093 static const struct of_device_id hellcreek_of_match[] = { 2094 { 2095 .compatible = "hirschmann,hellcreek-de1soc-r1", 2096 .data = &de1soc_r1_pdata, 2097 }, 2098 { /* sentinel */ }, 2099 }; 2100 MODULE_DEVICE_TABLE(of, hellcreek_of_match); 2101 2102 static struct platform_driver hellcreek_driver = { 2103 .probe = hellcreek_probe, 2104 .remove = hellcreek_remove, 2105 .shutdown = hellcreek_shutdown, 2106 .driver = { 2107 .name = "hellcreek", 2108 .of_match_table = hellcreek_of_match, 2109 }, 2110 }; 2111 module_platform_driver(hellcreek_driver); 2112 2113 MODULE_AUTHOR("Kurt Kanzenbach <kurt@linutronix.de>"); 2114 MODULE_DESCRIPTION("Hirschmann Hellcreek driver"); 2115 MODULE_LICENSE("Dual MIT/GPL"); 2116