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