Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: (GPL-2.0 or MIT)
0002 /*
0003  * DSA driver for:
0004  * Hirschmann Hellcreek TSN switch.
0005  *
0006  * Copyright (C) 2019-2021 Linutronix GmbH
0007  * Author Kurt Kanzenbach <kurt@linutronix.de>
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/device.h>
0013 #include <linux/of.h>
0014 #include <linux/of_device.h>
0015 #include <linux/of_mdio.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/bitops.h>
0018 #include <linux/if_bridge.h>
0019 #include <linux/if_vlan.h>
0020 #include <linux/etherdevice.h>
0021 #include <linux/random.h>
0022 #include <linux/iopoll.h>
0023 #include <linux/mutex.h>
0024 #include <linux/delay.h>
0025 #include <net/dsa.h>
0026 
0027 #include "hellcreek.h"
0028 #include "hellcreek_ptp.h"
0029 #include "hellcreek_hwtstamp.h"
0030 
0031 static const struct hellcreek_counter hellcreek_counter[] = {
0032     { 0x00, "RxFiltered", },
0033     { 0x01, "RxOctets1k", },
0034     { 0x02, "RxVTAG", },
0035     { 0x03, "RxL2BAD", },
0036     { 0x04, "RxOverloadDrop", },
0037     { 0x05, "RxUC", },
0038     { 0x06, "RxMC", },
0039     { 0x07, "RxBC", },
0040     { 0x08, "RxRS<64", },
0041     { 0x09, "RxRS64", },
0042     { 0x0a, "RxRS65_127", },
0043     { 0x0b, "RxRS128_255", },
0044     { 0x0c, "RxRS256_511", },
0045     { 0x0d, "RxRS512_1023", },
0046     { 0x0e, "RxRS1024_1518", },
0047     { 0x0f, "RxRS>1518", },
0048     { 0x10, "TxTailDropQueue0", },
0049     { 0x11, "TxTailDropQueue1", },
0050     { 0x12, "TxTailDropQueue2", },
0051     { 0x13, "TxTailDropQueue3", },
0052     { 0x14, "TxTailDropQueue4", },
0053     { 0x15, "TxTailDropQueue5", },
0054     { 0x16, "TxTailDropQueue6", },
0055     { 0x17, "TxTailDropQueue7", },
0056     { 0x18, "RxTrafficClass0", },
0057     { 0x19, "RxTrafficClass1", },
0058     { 0x1a, "RxTrafficClass2", },
0059     { 0x1b, "RxTrafficClass3", },
0060     { 0x1c, "RxTrafficClass4", },
0061     { 0x1d, "RxTrafficClass5", },
0062     { 0x1e, "RxTrafficClass6", },
0063     { 0x1f, "RxTrafficClass7", },
0064     { 0x21, "TxOctets1k", },
0065     { 0x22, "TxVTAG", },
0066     { 0x23, "TxL2BAD", },
0067     { 0x25, "TxUC", },
0068     { 0x26, "TxMC", },
0069     { 0x27, "TxBC", },
0070     { 0x28, "TxTS<64", },
0071     { 0x29, "TxTS64", },
0072     { 0x2a, "TxTS65_127", },
0073     { 0x2b, "TxTS128_255", },
0074     { 0x2c, "TxTS256_511", },
0075     { 0x2d, "TxTS512_1023", },
0076     { 0x2e, "TxTS1024_1518", },
0077     { 0x2f, "TxTS>1518", },
0078     { 0x30, "TxTrafficClassOverrun0", },
0079     { 0x31, "TxTrafficClassOverrun1", },
0080     { 0x32, "TxTrafficClassOverrun2", },
0081     { 0x33, "TxTrafficClassOverrun3", },
0082     { 0x34, "TxTrafficClassOverrun4", },
0083     { 0x35, "TxTrafficClassOverrun5", },
0084     { 0x36, "TxTrafficClassOverrun6", },
0085     { 0x37, "TxTrafficClassOverrun7", },
0086     { 0x38, "TxTrafficClass0", },
0087     { 0x39, "TxTrafficClass1", },
0088     { 0x3a, "TxTrafficClass2", },
0089     { 0x3b, "TxTrafficClass3", },
0090     { 0x3c, "TxTrafficClass4", },
0091     { 0x3d, "TxTrafficClass5", },
0092     { 0x3e, "TxTrafficClass6", },
0093     { 0x3f, "TxTrafficClass7", },
0094 };
0095 
0096 static u16 hellcreek_read(struct hellcreek *hellcreek, unsigned int offset)
0097 {
0098     return readw(hellcreek->base + offset);
0099 }
0100 
0101 static u16 hellcreek_read_ctrl(struct hellcreek *hellcreek)
0102 {
0103     return readw(hellcreek->base + HR_CTRL_C);
0104 }
0105 
0106 static u16 hellcreek_read_stat(struct hellcreek *hellcreek)
0107 {
0108     return readw(hellcreek->base + HR_SWSTAT);
0109 }
0110 
0111 static void hellcreek_write(struct hellcreek *hellcreek, u16 data,
0112                 unsigned int offset)
0113 {
0114     writew(data, hellcreek->base + offset);
0115 }
0116 
0117 static void hellcreek_select_port(struct hellcreek *hellcreek, int port)
0118 {
0119     u16 val = port << HR_PSEL_PTWSEL_SHIFT;
0120 
0121     hellcreek_write(hellcreek, val, HR_PSEL);
0122 }
0123 
0124 static void hellcreek_select_prio(struct hellcreek *hellcreek, int prio)
0125 {
0126     u16 val = prio << HR_PSEL_PRTCWSEL_SHIFT;
0127 
0128     hellcreek_write(hellcreek, val, HR_PSEL);
0129 }
0130 
0131 static void hellcreek_select_counter(struct hellcreek *hellcreek, int counter)
0132 {
0133     u16 val = counter << HR_CSEL_SHIFT;
0134 
0135     hellcreek_write(hellcreek, val, HR_CSEL);
0136 
0137     /* Data sheet states to wait at least 20 internal clock cycles */
0138     ndelay(200);
0139 }
0140 
0141 static void hellcreek_select_vlan(struct hellcreek *hellcreek, int vid,
0142                   bool pvid)
0143 {
0144     u16 val = 0;
0145 
0146     /* Set pvid bit first */
0147     if (pvid)
0148         val |= HR_VIDCFG_PVID;
0149     hellcreek_write(hellcreek, val, HR_VIDCFG);
0150 
0151     /* Set vlan */
0152     val |= vid << HR_VIDCFG_VID_SHIFT;
0153     hellcreek_write(hellcreek, val, HR_VIDCFG);
0154 }
0155 
0156 static void hellcreek_select_tgd(struct hellcreek *hellcreek, int port)
0157 {
0158     u16 val = port << TR_TGDSEL_TDGSEL_SHIFT;
0159 
0160     hellcreek_write(hellcreek, val, TR_TGDSEL);
0161 }
0162 
0163 static int hellcreek_wait_until_ready(struct hellcreek *hellcreek)
0164 {
0165     u16 val;
0166 
0167     /* Wait up to 1ms, although 3 us should be enough */
0168     return readx_poll_timeout(hellcreek_read_ctrl, hellcreek,
0169                   val, val & HR_CTRL_C_READY,
0170                   3, 1000);
0171 }
0172 
0173 static int hellcreek_wait_until_transitioned(struct hellcreek *hellcreek)
0174 {
0175     u16 val;
0176 
0177     return readx_poll_timeout_atomic(hellcreek_read_ctrl, hellcreek,
0178                      val, !(val & HR_CTRL_C_TRANSITION),
0179                      1, 1000);
0180 }
0181 
0182 static int hellcreek_wait_fdb_ready(struct hellcreek *hellcreek)
0183 {
0184     u16 val;
0185 
0186     return readx_poll_timeout_atomic(hellcreek_read_stat, hellcreek,
0187                      val, !(val & HR_SWSTAT_BUSY),
0188                      1, 1000);
0189 }
0190 
0191 static int hellcreek_detect(struct hellcreek *hellcreek)
0192 {
0193     u16 id, rel_low, rel_high, date_low, date_high, tgd_ver;
0194     u8 tgd_maj, tgd_min;
0195     u32 rel, date;
0196 
0197     id    = hellcreek_read(hellcreek, HR_MODID_C);
0198     rel_low   = hellcreek_read(hellcreek, HR_REL_L_C);
0199     rel_high  = hellcreek_read(hellcreek, HR_REL_H_C);
0200     date_low  = hellcreek_read(hellcreek, HR_BLD_L_C);
0201     date_high = hellcreek_read(hellcreek, HR_BLD_H_C);
0202     tgd_ver   = hellcreek_read(hellcreek, TR_TGDVER);
0203 
0204     if (id != hellcreek->pdata->module_id)
0205         return -ENODEV;
0206 
0207     rel = rel_low | (rel_high << 16);
0208     date    = date_low | (date_high << 16);
0209     tgd_maj = (tgd_ver & TR_TGDVER_REV_MAJ_MASK) >> TR_TGDVER_REV_MAJ_SHIFT;
0210     tgd_min = (tgd_ver & TR_TGDVER_REV_MIN_MASK) >> TR_TGDVER_REV_MIN_SHIFT;
0211 
0212     dev_info(hellcreek->dev, "Module ID=%02x Release=%04x Date=%04x TGD Version=%02x.%02x\n",
0213          id, rel, date, tgd_maj, tgd_min);
0214 
0215     return 0;
0216 }
0217 
0218 static void hellcreek_feature_detect(struct hellcreek *hellcreek)
0219 {
0220     u16 features;
0221 
0222     features = hellcreek_read(hellcreek, HR_FEABITS0);
0223 
0224     /* Only detect the size of the FDB table. The size and current
0225      * utilization can be queried via devlink.
0226      */
0227     hellcreek->fdb_entries = ((features & HR_FEABITS0_FDBBINS_MASK) >>
0228                    HR_FEABITS0_FDBBINS_SHIFT) * 32;
0229 }
0230 
0231 static enum dsa_tag_protocol hellcreek_get_tag_protocol(struct dsa_switch *ds,
0232                             int port,
0233                             enum dsa_tag_protocol mp)
0234 {
0235     return DSA_TAG_PROTO_HELLCREEK;
0236 }
0237 
0238 static int hellcreek_port_enable(struct dsa_switch *ds, int port,
0239                  struct phy_device *phy)
0240 {
0241     struct hellcreek *hellcreek = ds->priv;
0242     struct hellcreek_port *hellcreek_port;
0243     u16 val;
0244 
0245     hellcreek_port = &hellcreek->ports[port];
0246 
0247     dev_dbg(hellcreek->dev, "Enable port %d\n", port);
0248 
0249     mutex_lock(&hellcreek->reg_lock);
0250 
0251     hellcreek_select_port(hellcreek, port);
0252     val = hellcreek_port->ptcfg;
0253     val |= HR_PTCFG_ADMIN_EN;
0254     hellcreek_write(hellcreek, val, HR_PTCFG);
0255     hellcreek_port->ptcfg = val;
0256 
0257     mutex_unlock(&hellcreek->reg_lock);
0258 
0259     return 0;
0260 }
0261 
0262 static void hellcreek_port_disable(struct dsa_switch *ds, int port)
0263 {
0264     struct hellcreek *hellcreek = ds->priv;
0265     struct hellcreek_port *hellcreek_port;
0266     u16 val;
0267 
0268     hellcreek_port = &hellcreek->ports[port];
0269 
0270     dev_dbg(hellcreek->dev, "Disable port %d\n", port);
0271 
0272     mutex_lock(&hellcreek->reg_lock);
0273 
0274     hellcreek_select_port(hellcreek, port);
0275     val = hellcreek_port->ptcfg;
0276     val &= ~HR_PTCFG_ADMIN_EN;
0277     hellcreek_write(hellcreek, val, HR_PTCFG);
0278     hellcreek_port->ptcfg = val;
0279 
0280     mutex_unlock(&hellcreek->reg_lock);
0281 }
0282 
0283 static void hellcreek_get_strings(struct dsa_switch *ds, int port,
0284                   u32 stringset, uint8_t *data)
0285 {
0286     int i;
0287 
0288     for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
0289         const struct hellcreek_counter *counter = &hellcreek_counter[i];
0290 
0291         strlcpy(data + i * ETH_GSTRING_LEN,
0292             counter->name, ETH_GSTRING_LEN);
0293     }
0294 }
0295 
0296 static int hellcreek_get_sset_count(struct dsa_switch *ds, int port, int sset)
0297 {
0298     if (sset != ETH_SS_STATS)
0299         return 0;
0300 
0301     return ARRAY_SIZE(hellcreek_counter);
0302 }
0303 
0304 static void hellcreek_get_ethtool_stats(struct dsa_switch *ds, int port,
0305                     uint64_t *data)
0306 {
0307     struct hellcreek *hellcreek = ds->priv;
0308     struct hellcreek_port *hellcreek_port;
0309     int i;
0310 
0311     hellcreek_port = &hellcreek->ports[port];
0312 
0313     for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
0314         const struct hellcreek_counter *counter = &hellcreek_counter[i];
0315         u8 offset = counter->offset + port * 64;
0316         u16 high, low;
0317         u64 value;
0318 
0319         mutex_lock(&hellcreek->reg_lock);
0320 
0321         hellcreek_select_counter(hellcreek, offset);
0322 
0323         /* The registers are locked internally by selecting the
0324          * counter. So low and high can be read without reading high
0325          * again.
0326          */
0327         high  = hellcreek_read(hellcreek, HR_CRDH);
0328         low   = hellcreek_read(hellcreek, HR_CRDL);
0329         value = ((u64)high << 16) | low;
0330 
0331         hellcreek_port->counter_values[i] += value;
0332         data[i] = hellcreek_port->counter_values[i];
0333 
0334         mutex_unlock(&hellcreek->reg_lock);
0335     }
0336 }
0337 
0338 static u16 hellcreek_private_vid(int port)
0339 {
0340     return VLAN_N_VID - port + 1;
0341 }
0342 
0343 static int hellcreek_vlan_prepare(struct dsa_switch *ds, int port,
0344                   const struct switchdev_obj_port_vlan *vlan,
0345                   struct netlink_ext_ack *extack)
0346 {
0347     struct hellcreek *hellcreek = ds->priv;
0348     int i;
0349 
0350     dev_dbg(hellcreek->dev, "VLAN prepare for port %d\n", port);
0351 
0352     /* Restriction: Make sure that nobody uses the "private" VLANs. These
0353      * VLANs are internally used by the driver to ensure port
0354      * separation. Thus, they cannot be used by someone else.
0355      */
0356     for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
0357         const u16 restricted_vid = hellcreek_private_vid(i);
0358 
0359         if (!dsa_is_user_port(ds, i))
0360             continue;
0361 
0362         if (vlan->vid == restricted_vid) {
0363             NL_SET_ERR_MSG_MOD(extack, "VID restricted by driver");
0364             return -EBUSY;
0365         }
0366     }
0367 
0368     return 0;
0369 }
0370 
0371 static void hellcreek_select_vlan_params(struct hellcreek *hellcreek, int port,
0372                      int *shift, int *mask)
0373 {
0374     switch (port) {
0375     case 0:
0376         *shift = HR_VIDMBRCFG_P0MBR_SHIFT;
0377         *mask  = HR_VIDMBRCFG_P0MBR_MASK;
0378         break;
0379     case 1:
0380         *shift = HR_VIDMBRCFG_P1MBR_SHIFT;
0381         *mask  = HR_VIDMBRCFG_P1MBR_MASK;
0382         break;
0383     case 2:
0384         *shift = HR_VIDMBRCFG_P2MBR_SHIFT;
0385         *mask  = HR_VIDMBRCFG_P2MBR_MASK;
0386         break;
0387     case 3:
0388         *shift = HR_VIDMBRCFG_P3MBR_SHIFT;
0389         *mask  = HR_VIDMBRCFG_P3MBR_MASK;
0390         break;
0391     default:
0392         *shift = *mask = 0;
0393         dev_err(hellcreek->dev, "Unknown port %d selected!\n", port);
0394     }
0395 }
0396 
0397 static void hellcreek_apply_vlan(struct hellcreek *hellcreek, int port, u16 vid,
0398                  bool pvid, bool untagged)
0399 {
0400     int shift, mask;
0401     u16 val;
0402 
0403     dev_dbg(hellcreek->dev, "Apply VLAN: port=%d vid=%u pvid=%d untagged=%d",
0404         port, vid, pvid, untagged);
0405 
0406     mutex_lock(&hellcreek->reg_lock);
0407 
0408     hellcreek_select_port(hellcreek, port);
0409     hellcreek_select_vlan(hellcreek, vid, pvid);
0410 
0411     /* Setup port vlan membership */
0412     hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
0413     val = hellcreek->vidmbrcfg[vid];
0414     val &= ~mask;
0415     if (untagged)
0416         val |= HELLCREEK_VLAN_UNTAGGED_MEMBER << shift;
0417     else
0418         val |= HELLCREEK_VLAN_TAGGED_MEMBER << shift;
0419 
0420     hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
0421     hellcreek->vidmbrcfg[vid] = val;
0422 
0423     mutex_unlock(&hellcreek->reg_lock);
0424 }
0425 
0426 static void hellcreek_unapply_vlan(struct hellcreek *hellcreek, int port,
0427                    u16 vid)
0428 {
0429     int shift, mask;
0430     u16 val;
0431 
0432     dev_dbg(hellcreek->dev, "Unapply VLAN: port=%d vid=%u\n", port, vid);
0433 
0434     mutex_lock(&hellcreek->reg_lock);
0435 
0436     hellcreek_select_vlan(hellcreek, vid, false);
0437 
0438     /* Setup port vlan membership */
0439     hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
0440     val = hellcreek->vidmbrcfg[vid];
0441     val &= ~mask;
0442     val |= HELLCREEK_VLAN_NO_MEMBER << shift;
0443 
0444     hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
0445     hellcreek->vidmbrcfg[vid] = val;
0446 
0447     mutex_unlock(&hellcreek->reg_lock);
0448 }
0449 
0450 static int hellcreek_vlan_add(struct dsa_switch *ds, int port,
0451                   const struct switchdev_obj_port_vlan *vlan,
0452                   struct netlink_ext_ack *extack)
0453 {
0454     bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
0455     bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
0456     struct hellcreek *hellcreek = ds->priv;
0457     int err;
0458 
0459     err = hellcreek_vlan_prepare(ds, port, vlan, extack);
0460     if (err)
0461         return err;
0462 
0463     dev_dbg(hellcreek->dev, "Add VLAN %d on port %d, %s, %s\n",
0464         vlan->vid, port, untagged ? "untagged" : "tagged",
0465         pvid ? "PVID" : "no PVID");
0466 
0467     hellcreek_apply_vlan(hellcreek, port, vlan->vid, pvid, untagged);
0468 
0469     return 0;
0470 }
0471 
0472 static int hellcreek_vlan_del(struct dsa_switch *ds, int port,
0473                   const struct switchdev_obj_port_vlan *vlan)
0474 {
0475     struct hellcreek *hellcreek = ds->priv;
0476 
0477     dev_dbg(hellcreek->dev, "Remove VLAN %d on port %d\n", vlan->vid, port);
0478 
0479     hellcreek_unapply_vlan(hellcreek, port, vlan->vid);
0480 
0481     return 0;
0482 }
0483 
0484 static void hellcreek_port_stp_state_set(struct dsa_switch *ds, int port,
0485                      u8 state)
0486 {
0487     struct hellcreek *hellcreek = ds->priv;
0488     struct hellcreek_port *hellcreek_port;
0489     const char *new_state;
0490     u16 val;
0491 
0492     mutex_lock(&hellcreek->reg_lock);
0493 
0494     hellcreek_port = &hellcreek->ports[port];
0495     val = hellcreek_port->ptcfg;
0496 
0497     switch (state) {
0498     case BR_STATE_DISABLED:
0499         new_state = "DISABLED";
0500         val |= HR_PTCFG_BLOCKED;
0501         val &= ~HR_PTCFG_LEARNING_EN;
0502         break;
0503     case BR_STATE_BLOCKING:
0504         new_state = "BLOCKING";
0505         val |= HR_PTCFG_BLOCKED;
0506         val &= ~HR_PTCFG_LEARNING_EN;
0507         break;
0508     case BR_STATE_LISTENING:
0509         new_state = "LISTENING";
0510         val |= HR_PTCFG_BLOCKED;
0511         val &= ~HR_PTCFG_LEARNING_EN;
0512         break;
0513     case BR_STATE_LEARNING:
0514         new_state = "LEARNING";
0515         val |= HR_PTCFG_BLOCKED;
0516         val |= HR_PTCFG_LEARNING_EN;
0517         break;
0518     case BR_STATE_FORWARDING:
0519         new_state = "FORWARDING";
0520         val &= ~HR_PTCFG_BLOCKED;
0521         val |= HR_PTCFG_LEARNING_EN;
0522         break;
0523     default:
0524         new_state = "UNKNOWN";
0525     }
0526 
0527     hellcreek_select_port(hellcreek, port);
0528     hellcreek_write(hellcreek, val, HR_PTCFG);
0529     hellcreek_port->ptcfg = val;
0530 
0531     mutex_unlock(&hellcreek->reg_lock);
0532 
0533     dev_dbg(hellcreek->dev, "Configured STP state for port %d: %s\n",
0534         port, new_state);
0535 }
0536 
0537 static void hellcreek_setup_ingressflt(struct hellcreek *hellcreek, int port,
0538                        bool enable)
0539 {
0540     struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
0541     u16 ptcfg;
0542 
0543     mutex_lock(&hellcreek->reg_lock);
0544 
0545     ptcfg = hellcreek_port->ptcfg;
0546 
0547     if (enable)
0548         ptcfg |= HR_PTCFG_INGRESSFLT;
0549     else
0550         ptcfg &= ~HR_PTCFG_INGRESSFLT;
0551 
0552     hellcreek_select_port(hellcreek, port);
0553     hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
0554     hellcreek_port->ptcfg = ptcfg;
0555 
0556     mutex_unlock(&hellcreek->reg_lock);
0557 }
0558 
0559 static void hellcreek_setup_vlan_awareness(struct hellcreek *hellcreek,
0560                        bool enable)
0561 {
0562     u16 swcfg;
0563 
0564     mutex_lock(&hellcreek->reg_lock);
0565 
0566     swcfg = hellcreek->swcfg;
0567 
0568     if (enable)
0569         swcfg |= HR_SWCFG_VLAN_UNAWARE;
0570     else
0571         swcfg &= ~HR_SWCFG_VLAN_UNAWARE;
0572 
0573     hellcreek_write(hellcreek, swcfg, HR_SWCFG);
0574 
0575     mutex_unlock(&hellcreek->reg_lock);
0576 }
0577 
0578 /* Default setup for DSA: VLAN <X>: CPU and Port <X> egress untagged. */
0579 static void hellcreek_setup_vlan_membership(struct dsa_switch *ds, int port,
0580                         bool enabled)
0581 {
0582     const u16 vid = hellcreek_private_vid(port);
0583     int upstream = dsa_upstream_port(ds, port);
0584     struct hellcreek *hellcreek = ds->priv;
0585 
0586     /* Apply vid to port as egress untagged and port vlan id */
0587     if (enabled)
0588         hellcreek_apply_vlan(hellcreek, port, vid, true, true);
0589     else
0590         hellcreek_unapply_vlan(hellcreek, port, vid);
0591 
0592     /* Apply vid to cpu port as well */
0593     if (enabled)
0594         hellcreek_apply_vlan(hellcreek, upstream, vid, false, true);
0595     else
0596         hellcreek_unapply_vlan(hellcreek, upstream, vid);
0597 }
0598 
0599 static void hellcreek_port_set_ucast_flood(struct hellcreek *hellcreek,
0600                        int port, bool enable)
0601 {
0602     struct hellcreek_port *hellcreek_port;
0603     u16 val;
0604 
0605     hellcreek_port = &hellcreek->ports[port];
0606 
0607     dev_dbg(hellcreek->dev, "%s unicast flooding on port %d\n",
0608         enable ? "Enable" : "Disable", port);
0609 
0610     mutex_lock(&hellcreek->reg_lock);
0611 
0612     hellcreek_select_port(hellcreek, port);
0613     val = hellcreek_port->ptcfg;
0614     if (enable)
0615         val &= ~HR_PTCFG_UUC_FLT;
0616     else
0617         val |= HR_PTCFG_UUC_FLT;
0618     hellcreek_write(hellcreek, val, HR_PTCFG);
0619     hellcreek_port->ptcfg = val;
0620 
0621     mutex_unlock(&hellcreek->reg_lock);
0622 }
0623 
0624 static void hellcreek_port_set_mcast_flood(struct hellcreek *hellcreek,
0625                        int port, bool enable)
0626 {
0627     struct hellcreek_port *hellcreek_port;
0628     u16 val;
0629 
0630     hellcreek_port = &hellcreek->ports[port];
0631 
0632     dev_dbg(hellcreek->dev, "%s multicast flooding on port %d\n",
0633         enable ? "Enable" : "Disable", port);
0634 
0635     mutex_lock(&hellcreek->reg_lock);
0636 
0637     hellcreek_select_port(hellcreek, port);
0638     val = hellcreek_port->ptcfg;
0639     if (enable)
0640         val &= ~HR_PTCFG_UMC_FLT;
0641     else
0642         val |= HR_PTCFG_UMC_FLT;
0643     hellcreek_write(hellcreek, val, HR_PTCFG);
0644     hellcreek_port->ptcfg = val;
0645 
0646     mutex_unlock(&hellcreek->reg_lock);
0647 }
0648 
0649 static int hellcreek_pre_bridge_flags(struct dsa_switch *ds, int port,
0650                       struct switchdev_brport_flags flags,
0651                       struct netlink_ext_ack *extack)
0652 {
0653     if (flags.mask & ~(BR_FLOOD | BR_MCAST_FLOOD))
0654         return -EINVAL;
0655 
0656     return 0;
0657 }
0658 
0659 static int hellcreek_bridge_flags(struct dsa_switch *ds, int port,
0660                   struct switchdev_brport_flags flags,
0661                   struct netlink_ext_ack *extack)
0662 {
0663     struct hellcreek *hellcreek = ds->priv;
0664 
0665     if (flags.mask & BR_FLOOD)
0666         hellcreek_port_set_ucast_flood(hellcreek, port,
0667                            !!(flags.val & BR_FLOOD));
0668 
0669     if (flags.mask & BR_MCAST_FLOOD)
0670         hellcreek_port_set_mcast_flood(hellcreek, port,
0671                            !!(flags.val & BR_MCAST_FLOOD));
0672 
0673     return 0;
0674 }
0675 
0676 static int hellcreek_port_bridge_join(struct dsa_switch *ds, int port,
0677                       struct dsa_bridge bridge,
0678                       bool *tx_fwd_offload,
0679                       struct netlink_ext_ack *extack)
0680 {
0681     struct hellcreek *hellcreek = ds->priv;
0682 
0683     dev_dbg(hellcreek->dev, "Port %d joins a bridge\n", port);
0684 
0685     /* When joining a vlan_filtering bridge, keep the switch VLAN aware */
0686     if (!ds->vlan_filtering)
0687         hellcreek_setup_vlan_awareness(hellcreek, false);
0688 
0689     /* Drop private vlans */
0690     hellcreek_setup_vlan_membership(ds, port, false);
0691 
0692     return 0;
0693 }
0694 
0695 static void hellcreek_port_bridge_leave(struct dsa_switch *ds, int port,
0696                     struct dsa_bridge bridge)
0697 {
0698     struct hellcreek *hellcreek = ds->priv;
0699 
0700     dev_dbg(hellcreek->dev, "Port %d leaves a bridge\n", port);
0701 
0702     /* Enable VLAN awareness */
0703     hellcreek_setup_vlan_awareness(hellcreek, true);
0704 
0705     /* Enable private vlans */
0706     hellcreek_setup_vlan_membership(ds, port, true);
0707 }
0708 
0709 static int __hellcreek_fdb_add(struct hellcreek *hellcreek,
0710                    const struct hellcreek_fdb_entry *entry)
0711 {
0712     u16 meta = 0;
0713 
0714     dev_dbg(hellcreek->dev, "Add static FDB entry: MAC=%pM, MASK=0x%02x, "
0715         "OBT=%d, PASS_BLOCKED=%d, REPRIO_EN=%d, PRIO=%d\n", entry->mac,
0716         entry->portmask, entry->is_obt, entry->pass_blocked,
0717         entry->reprio_en, entry->reprio_tc);
0718 
0719     /* Add mac address */
0720     hellcreek_write(hellcreek, entry->mac[1] | (entry->mac[0] << 8), HR_FDBWDH);
0721     hellcreek_write(hellcreek, entry->mac[3] | (entry->mac[2] << 8), HR_FDBWDM);
0722     hellcreek_write(hellcreek, entry->mac[5] | (entry->mac[4] << 8), HR_FDBWDL);
0723 
0724     /* Meta data */
0725     meta |= entry->portmask << HR_FDBWRM0_PORTMASK_SHIFT;
0726     if (entry->is_obt)
0727         meta |= HR_FDBWRM0_OBT;
0728     if (entry->pass_blocked)
0729         meta |= HR_FDBWRM0_PASS_BLOCKED;
0730     if (entry->reprio_en) {
0731         meta |= HR_FDBWRM0_REPRIO_EN;
0732         meta |= entry->reprio_tc << HR_FDBWRM0_REPRIO_TC_SHIFT;
0733     }
0734     hellcreek_write(hellcreek, meta, HR_FDBWRM0);
0735 
0736     /* Commit */
0737     hellcreek_write(hellcreek, 0x00, HR_FDBWRCMD);
0738 
0739     /* Wait until done */
0740     return hellcreek_wait_fdb_ready(hellcreek);
0741 }
0742 
0743 static int __hellcreek_fdb_del(struct hellcreek *hellcreek,
0744                    const struct hellcreek_fdb_entry *entry)
0745 {
0746     dev_dbg(hellcreek->dev, "Delete FDB entry: MAC=%pM!\n", entry->mac);
0747 
0748     /* Delete by matching idx */
0749     hellcreek_write(hellcreek, entry->idx | HR_FDBWRCMD_FDBDEL, HR_FDBWRCMD);
0750 
0751     /* Wait until done */
0752     return hellcreek_wait_fdb_ready(hellcreek);
0753 }
0754 
0755 static void hellcreek_populate_fdb_entry(struct hellcreek *hellcreek,
0756                      struct hellcreek_fdb_entry *entry,
0757                      size_t idx)
0758 {
0759     unsigned char addr[ETH_ALEN];
0760     u16 meta, mac;
0761 
0762     /* Read values */
0763     meta    = hellcreek_read(hellcreek, HR_FDBMDRD);
0764     mac = hellcreek_read(hellcreek, HR_FDBRDL);
0765     addr[5] = mac & 0xff;
0766     addr[4] = (mac & 0xff00) >> 8;
0767     mac = hellcreek_read(hellcreek, HR_FDBRDM);
0768     addr[3] = mac & 0xff;
0769     addr[2] = (mac & 0xff00) >> 8;
0770     mac = hellcreek_read(hellcreek, HR_FDBRDH);
0771     addr[1] = mac & 0xff;
0772     addr[0] = (mac & 0xff00) >> 8;
0773 
0774     /* Populate @entry */
0775     memcpy(entry->mac, addr, sizeof(addr));
0776     entry->idx      = idx;
0777     entry->portmask     = (meta & HR_FDBMDRD_PORTMASK_MASK) >>
0778         HR_FDBMDRD_PORTMASK_SHIFT;
0779     entry->age      = (meta & HR_FDBMDRD_AGE_MASK) >>
0780         HR_FDBMDRD_AGE_SHIFT;
0781     entry->is_obt       = !!(meta & HR_FDBMDRD_OBT);
0782     entry->pass_blocked = !!(meta & HR_FDBMDRD_PASS_BLOCKED);
0783     entry->is_static    = !!(meta & HR_FDBMDRD_STATIC);
0784     entry->reprio_tc    = (meta & HR_FDBMDRD_REPRIO_TC_MASK) >>
0785         HR_FDBMDRD_REPRIO_TC_SHIFT;
0786     entry->reprio_en    = !!(meta & HR_FDBMDRD_REPRIO_EN);
0787 }
0788 
0789 /* Retrieve the index of a FDB entry by mac address. Currently we search through
0790  * the complete table in hardware. If that's too slow, we might have to cache
0791  * the complete FDB table in software.
0792  */
0793 static int hellcreek_fdb_get(struct hellcreek *hellcreek,
0794                  const unsigned char *dest,
0795                  struct hellcreek_fdb_entry *entry)
0796 {
0797     size_t i;
0798 
0799     /* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
0800      * should reset the internal pointer. But, that doesn't work. The vendor
0801      * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
0802      */
0803     hellcreek_read(hellcreek, HR_FDBMAX);
0804     hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
0805 
0806     /* We have to read the complete table, because the switch/driver might
0807      * enter new entries anywhere.
0808      */
0809     for (i = 0; i < hellcreek->fdb_entries; ++i) {
0810         struct hellcreek_fdb_entry tmp = { 0 };
0811 
0812         /* Read entry */
0813         hellcreek_populate_fdb_entry(hellcreek, &tmp, i);
0814 
0815         /* Force next entry */
0816         hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
0817 
0818         if (memcmp(tmp.mac, dest, ETH_ALEN))
0819             continue;
0820 
0821         /* Match found */
0822         memcpy(entry, &tmp, sizeof(*entry));
0823 
0824         return 0;
0825     }
0826 
0827     return -ENOENT;
0828 }
0829 
0830 static int hellcreek_fdb_add(struct dsa_switch *ds, int port,
0831                  const unsigned char *addr, u16 vid,
0832                  struct dsa_db db)
0833 {
0834     struct hellcreek_fdb_entry entry = { 0 };
0835     struct hellcreek *hellcreek = ds->priv;
0836     int ret;
0837 
0838     dev_dbg(hellcreek->dev, "Add FDB entry for MAC=%pM\n", addr);
0839 
0840     mutex_lock(&hellcreek->reg_lock);
0841 
0842     ret = hellcreek_fdb_get(hellcreek, addr, &entry);
0843     if (ret) {
0844         /* Not found */
0845         memcpy(entry.mac, addr, sizeof(entry.mac));
0846         entry.portmask = BIT(port);
0847 
0848         ret = __hellcreek_fdb_add(hellcreek, &entry);
0849         if (ret) {
0850             dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
0851             goto out;
0852         }
0853     } else {
0854         /* Found */
0855         ret = __hellcreek_fdb_del(hellcreek, &entry);
0856         if (ret) {
0857             dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
0858             goto out;
0859         }
0860 
0861         entry.portmask |= BIT(port);
0862 
0863         ret = __hellcreek_fdb_add(hellcreek, &entry);
0864         if (ret) {
0865             dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
0866             goto out;
0867         }
0868     }
0869 
0870 out:
0871     mutex_unlock(&hellcreek->reg_lock);
0872 
0873     return ret;
0874 }
0875 
0876 static int hellcreek_fdb_del(struct dsa_switch *ds, int port,
0877                  const unsigned char *addr, u16 vid,
0878                  struct dsa_db db)
0879 {
0880     struct hellcreek_fdb_entry entry = { 0 };
0881     struct hellcreek *hellcreek = ds->priv;
0882     int ret;
0883 
0884     dev_dbg(hellcreek->dev, "Delete FDB entry for MAC=%pM\n", addr);
0885 
0886     mutex_lock(&hellcreek->reg_lock);
0887 
0888     ret = hellcreek_fdb_get(hellcreek, addr, &entry);
0889     if (ret) {
0890         /* Not found */
0891         dev_err(hellcreek->dev, "FDB entry for deletion not found!\n");
0892     } else {
0893         /* Found */
0894         ret = __hellcreek_fdb_del(hellcreek, &entry);
0895         if (ret) {
0896             dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
0897             goto out;
0898         }
0899 
0900         entry.portmask &= ~BIT(port);
0901 
0902         if (entry.portmask != 0x00) {
0903             ret = __hellcreek_fdb_add(hellcreek, &entry);
0904             if (ret) {
0905                 dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
0906                 goto out;
0907             }
0908         }
0909     }
0910 
0911 out:
0912     mutex_unlock(&hellcreek->reg_lock);
0913 
0914     return ret;
0915 }
0916 
0917 static int hellcreek_fdb_dump(struct dsa_switch *ds, int port,
0918                   dsa_fdb_dump_cb_t *cb, void *data)
0919 {
0920     struct hellcreek *hellcreek = ds->priv;
0921     u16 entries;
0922     int ret = 0;
0923     size_t i;
0924 
0925     mutex_lock(&hellcreek->reg_lock);
0926 
0927     /* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
0928      * should reset the internal pointer. But, that doesn't work. The vendor
0929      * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
0930      */
0931     entries = hellcreek_read(hellcreek, HR_FDBMAX);
0932     hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
0933 
0934     dev_dbg(hellcreek->dev, "FDB dump for port %d, entries=%d!\n", port, entries);
0935 
0936     /* Read table */
0937     for (i = 0; i < hellcreek->fdb_entries; ++i) {
0938         struct hellcreek_fdb_entry entry = { 0 };
0939 
0940         /* Read entry */
0941         hellcreek_populate_fdb_entry(hellcreek, &entry, i);
0942 
0943         /* Force next entry */
0944         hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
0945 
0946         /* Check valid */
0947         if (is_zero_ether_addr(entry.mac))
0948             continue;
0949 
0950         /* Check port mask */
0951         if (!(entry.portmask & BIT(port)))
0952             continue;
0953 
0954         ret = cb(entry.mac, 0, entry.is_static, data);
0955         if (ret)
0956             break;
0957     }
0958 
0959     mutex_unlock(&hellcreek->reg_lock);
0960 
0961     return ret;
0962 }
0963 
0964 static int hellcreek_vlan_filtering(struct dsa_switch *ds, int port,
0965                     bool vlan_filtering,
0966                     struct netlink_ext_ack *extack)
0967 {
0968     struct hellcreek *hellcreek = ds->priv;
0969 
0970     dev_dbg(hellcreek->dev, "%s VLAN filtering on port %d\n",
0971         vlan_filtering ? "Enable" : "Disable", port);
0972 
0973     /* Configure port to drop packages with not known vids */
0974     hellcreek_setup_ingressflt(hellcreek, port, vlan_filtering);
0975 
0976     /* Enable VLAN awareness on the switch. This save due to
0977      * ds->vlan_filtering_is_global.
0978      */
0979     hellcreek_setup_vlan_awareness(hellcreek, vlan_filtering);
0980 
0981     return 0;
0982 }
0983 
0984 static int hellcreek_enable_ip_core(struct hellcreek *hellcreek)
0985 {
0986     int ret;
0987     u16 val;
0988 
0989     mutex_lock(&hellcreek->reg_lock);
0990 
0991     val = hellcreek_read(hellcreek, HR_CTRL_C);
0992     val |= HR_CTRL_C_ENABLE;
0993     hellcreek_write(hellcreek, val, HR_CTRL_C);
0994     ret = hellcreek_wait_until_transitioned(hellcreek);
0995 
0996     mutex_unlock(&hellcreek->reg_lock);
0997 
0998     return ret;
0999 }
1000 
1001 static void hellcreek_setup_cpu_and_tunnel_port(struct hellcreek *hellcreek)
1002 {
1003     struct hellcreek_port *tunnel_port = &hellcreek->ports[TUNNEL_PORT];
1004     struct hellcreek_port *cpu_port = &hellcreek->ports[CPU_PORT];
1005     u16 ptcfg = 0;
1006 
1007     ptcfg |= HR_PTCFG_LEARNING_EN | HR_PTCFG_ADMIN_EN;
1008 
1009     mutex_lock(&hellcreek->reg_lock);
1010 
1011     hellcreek_select_port(hellcreek, CPU_PORT);
1012     hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
1013 
1014     hellcreek_select_port(hellcreek, TUNNEL_PORT);
1015     hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
1016 
1017     cpu_port->ptcfg    = ptcfg;
1018     tunnel_port->ptcfg = ptcfg;
1019 
1020     mutex_unlock(&hellcreek->reg_lock);
1021 }
1022 
1023 static void hellcreek_setup_tc_identity_mapping(struct hellcreek *hellcreek)
1024 {
1025     int i;
1026 
1027     /* The switch has multiple egress queues per port. The queue is selected
1028      * via the PCP field in the VLAN header. The switch internally deals
1029      * with traffic classes instead of PCP values and this mapping is
1030      * configurable.
1031      *
1032      * The default mapping is (PCP - TC):
1033      *  7 - 7
1034      *  6 - 6
1035      *  5 - 5
1036      *  4 - 4
1037      *  3 - 3
1038      *  2 - 1
1039      *  1 - 0
1040      *  0 - 2
1041      *
1042      * The default should be an identity mapping.
1043      */
1044 
1045     for (i = 0; i < 8; ++i) {
1046         mutex_lock(&hellcreek->reg_lock);
1047 
1048         hellcreek_select_prio(hellcreek, i);
1049         hellcreek_write(hellcreek,
1050                 i << HR_PRTCCFG_PCP_TC_MAP_SHIFT,
1051                 HR_PRTCCFG);
1052 
1053         mutex_unlock(&hellcreek->reg_lock);
1054     }
1055 }
1056 
1057 static int hellcreek_setup_fdb(struct hellcreek *hellcreek)
1058 {
1059     static struct hellcreek_fdb_entry l2_ptp = {
1060         /* MAC: 01-1B-19-00-00-00 */
1061         .mac          = { 0x01, 0x1b, 0x19, 0x00, 0x00, 0x00 },
1062         .portmask     = 0x03,   /* Management ports */
1063         .age          = 0,
1064         .is_obt       = 0,
1065         .pass_blocked = 0,
1066         .is_static    = 1,
1067         .reprio_tc    = 6,  /* TC: 6 as per IEEE 802.1AS */
1068         .reprio_en    = 1,
1069     };
1070     static struct hellcreek_fdb_entry udp4_ptp = {
1071         /* MAC: 01-00-5E-00-01-81 */
1072         .mac          = { 0x01, 0x00, 0x5e, 0x00, 0x01, 0x81 },
1073         .portmask     = 0x03,   /* Management ports */
1074         .age          = 0,
1075         .is_obt       = 0,
1076         .pass_blocked = 0,
1077         .is_static    = 1,
1078         .reprio_tc    = 6,
1079         .reprio_en    = 1,
1080     };
1081     static struct hellcreek_fdb_entry udp6_ptp = {
1082         /* MAC: 33-33-00-00-01-81 */
1083         .mac          = { 0x33, 0x33, 0x00, 0x00, 0x01, 0x81 },
1084         .portmask     = 0x03,   /* Management ports */
1085         .age          = 0,
1086         .is_obt       = 0,
1087         .pass_blocked = 0,
1088         .is_static    = 1,
1089         .reprio_tc    = 6,
1090         .reprio_en    = 1,
1091     };
1092     static struct hellcreek_fdb_entry l2_p2p = {
1093         /* MAC: 01-80-C2-00-00-0E */
1094         .mac          = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e },
1095         .portmask     = 0x03,   /* Management ports */
1096         .age          = 0,
1097         .is_obt       = 0,
1098         .pass_blocked = 1,
1099         .is_static    = 1,
1100         .reprio_tc    = 6,  /* TC: 6 as per IEEE 802.1AS */
1101         .reprio_en    = 1,
1102     };
1103     static struct hellcreek_fdb_entry udp4_p2p = {
1104         /* MAC: 01-00-5E-00-00-6B */
1105         .mac          = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x6b },
1106         .portmask     = 0x03,   /* Management ports */
1107         .age          = 0,
1108         .is_obt       = 0,
1109         .pass_blocked = 1,
1110         .is_static    = 1,
1111         .reprio_tc    = 6,
1112         .reprio_en    = 1,
1113     };
1114     static struct hellcreek_fdb_entry udp6_p2p = {
1115         /* MAC: 33-33-00-00-00-6B */
1116         .mac          = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x6b },
1117         .portmask     = 0x03,   /* Management ports */
1118         .age          = 0,
1119         .is_obt       = 0,
1120         .pass_blocked = 1,
1121         .is_static    = 1,
1122         .reprio_tc    = 6,
1123         .reprio_en    = 1,
1124     };
1125     static struct hellcreek_fdb_entry stp = {
1126         /* MAC: 01-80-C2-00-00-00 */
1127         .mac          = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 },
1128         .portmask     = 0x03,   /* Management ports */
1129         .age          = 0,
1130         .is_obt       = 0,
1131         .pass_blocked = 1,
1132         .is_static    = 1,
1133         .reprio_tc    = 6,
1134         .reprio_en    = 1,
1135     };
1136     int ret;
1137 
1138     mutex_lock(&hellcreek->reg_lock);
1139     ret = __hellcreek_fdb_add(hellcreek, &l2_ptp);
1140     if (ret)
1141         goto out;
1142     ret = __hellcreek_fdb_add(hellcreek, &udp4_ptp);
1143     if (ret)
1144         goto out;
1145     ret = __hellcreek_fdb_add(hellcreek, &udp6_ptp);
1146     if (ret)
1147         goto out;
1148     ret = __hellcreek_fdb_add(hellcreek, &l2_p2p);
1149     if (ret)
1150         goto out;
1151     ret = __hellcreek_fdb_add(hellcreek, &udp4_p2p);
1152     if (ret)
1153         goto out;
1154     ret = __hellcreek_fdb_add(hellcreek, &udp6_p2p);
1155     if (ret)
1156         goto out;
1157     ret = __hellcreek_fdb_add(hellcreek, &stp);
1158 out:
1159     mutex_unlock(&hellcreek->reg_lock);
1160 
1161     return ret;
1162 }
1163 
1164 static int hellcreek_devlink_info_get(struct dsa_switch *ds,
1165                       struct devlink_info_req *req,
1166                       struct netlink_ext_ack *extack)
1167 {
1168     struct hellcreek *hellcreek = ds->priv;
1169     int ret;
1170 
1171     ret = devlink_info_driver_name_put(req, "hellcreek");
1172     if (ret)
1173         return ret;
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_gcl(struct hellcreek *hellcreek, int port,
1541                 const struct tc_taprio_qopt_offload *schedule)
1542 {
1543     const struct tc_taprio_sched_entry *cur, *initial, *next;
1544     size_t i;
1545 
1546     cur = initial = &schedule->entries[0];
1547     next = cur + 1;
1548 
1549     for (i = 1; i <= schedule->num_entries; ++i) {
1550         u16 data;
1551         u8 gates;
1552 
1553         if (i == schedule->num_entries)
1554             gates = initial->gate_mask ^
1555                 cur->gate_mask;
1556         else
1557             gates = next->gate_mask ^
1558                 cur->gate_mask;
1559 
1560         data = gates;
1561 
1562         if (i == schedule->num_entries)
1563             data |= TR_GCLDAT_GCLWRLAST;
1564 
1565         /* Gates states */
1566         hellcreek_write(hellcreek, data, TR_GCLDAT);
1567 
1568         /* Time interval */
1569         hellcreek_write(hellcreek,
1570                 cur->interval & 0x0000ffff,
1571                 TR_GCLTIL);
1572         hellcreek_write(hellcreek,
1573                 (cur->interval & 0xffff0000) >> 16,
1574                 TR_GCLTIH);
1575 
1576         /* Commit entry */
1577         data = ((i - 1) << TR_GCLCMD_GCLWRADR_SHIFT) |
1578             (initial->gate_mask <<
1579              TR_GCLCMD_INIT_GATE_STATES_SHIFT);
1580         hellcreek_write(hellcreek, data, TR_GCLCMD);
1581 
1582         cur++;
1583         next++;
1584     }
1585 }
1586 
1587 static void hellcreek_set_cycle_time(struct hellcreek *hellcreek,
1588                      const struct tc_taprio_qopt_offload *schedule)
1589 {
1590     u32 cycle_time = schedule->cycle_time;
1591 
1592     hellcreek_write(hellcreek, cycle_time & 0x0000ffff, TR_CTWRL);
1593     hellcreek_write(hellcreek, (cycle_time & 0xffff0000) >> 16, TR_CTWRH);
1594 }
1595 
1596 static void hellcreek_switch_schedule(struct hellcreek *hellcreek,
1597                       ktime_t start_time)
1598 {
1599     struct timespec64 ts = ktime_to_timespec64(start_time);
1600 
1601     /* Start schedule at this point of time */
1602     hellcreek_write(hellcreek, ts.tv_nsec & 0x0000ffff, TR_ESTWRL);
1603     hellcreek_write(hellcreek, (ts.tv_nsec & 0xffff0000) >> 16, TR_ESTWRH);
1604 
1605     /* Arm timer, set seconds and switch schedule */
1606     hellcreek_write(hellcreek, TR_ESTCMD_ESTARM | TR_ESTCMD_ESTSWCFG |
1607             ((ts.tv_sec & TR_ESTCMD_ESTSEC_MASK) <<
1608              TR_ESTCMD_ESTSEC_SHIFT), TR_ESTCMD);
1609 }
1610 
1611 static bool hellcreek_schedule_startable(struct hellcreek *hellcreek, int port)
1612 {
1613     struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
1614     s64 base_time_ns, current_ns;
1615 
1616     /* The switch allows a schedule to be started only eight seconds within
1617      * the future. Therefore, check the current PTP time if the schedule is
1618      * startable or not.
1619      */
1620 
1621     /* Use the "cached" time. That should be alright, as it's updated quite
1622      * frequently in the PTP code.
1623      */
1624     mutex_lock(&hellcreek->ptp_lock);
1625     current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts;
1626     mutex_unlock(&hellcreek->ptp_lock);
1627 
1628     /* Calculate difference to admin base time */
1629     base_time_ns = ktime_to_ns(hellcreek_port->current_schedule->base_time);
1630 
1631     return base_time_ns - current_ns < (s64)4 * NSEC_PER_SEC;
1632 }
1633 
1634 static void hellcreek_start_schedule(struct hellcreek *hellcreek, int port)
1635 {
1636     struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
1637     ktime_t base_time, current_time;
1638     s64 current_ns;
1639     u32 cycle_time;
1640 
1641     /* First select port */
1642     hellcreek_select_tgd(hellcreek, port);
1643 
1644     /* Forward base time into the future if needed */
1645     mutex_lock(&hellcreek->ptp_lock);
1646     current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts;
1647     mutex_unlock(&hellcreek->ptp_lock);
1648 
1649     current_time = ns_to_ktime(current_ns);
1650     base_time    = hellcreek_port->current_schedule->base_time;
1651     cycle_time   = hellcreek_port->current_schedule->cycle_time;
1652 
1653     if (ktime_compare(current_time, base_time) > 0) {
1654         s64 n;
1655 
1656         n = div64_s64(ktime_sub_ns(current_time, base_time),
1657                   cycle_time);
1658         base_time = ktime_add_ns(base_time, (n + 1) * cycle_time);
1659     }
1660 
1661     /* Set admin base time and switch schedule */
1662     hellcreek_switch_schedule(hellcreek, base_time);
1663 
1664     taprio_offload_free(hellcreek_port->current_schedule);
1665     hellcreek_port->current_schedule = NULL;
1666 
1667     dev_dbg(hellcreek->dev, "Armed EST timer for port %d\n",
1668         hellcreek_port->port);
1669 }
1670 
1671 static void hellcreek_check_schedule(struct work_struct *work)
1672 {
1673     struct delayed_work *dw = to_delayed_work(work);
1674     struct hellcreek_port *hellcreek_port;
1675     struct hellcreek *hellcreek;
1676     bool startable;
1677 
1678     hellcreek_port = dw_to_hellcreek_port(dw);
1679     hellcreek = hellcreek_port->hellcreek;
1680 
1681     mutex_lock(&hellcreek->reg_lock);
1682 
1683     /* Check starting time */
1684     startable = hellcreek_schedule_startable(hellcreek,
1685                          hellcreek_port->port);
1686     if (startable) {
1687         hellcreek_start_schedule(hellcreek, hellcreek_port->port);
1688         mutex_unlock(&hellcreek->reg_lock);
1689         return;
1690     }
1691 
1692     mutex_unlock(&hellcreek->reg_lock);
1693 
1694     /* Reschedule */
1695     schedule_delayed_work(&hellcreek_port->schedule_work,
1696                   HELLCREEK_SCHEDULE_PERIOD);
1697 }
1698 
1699 static int hellcreek_port_set_schedule(struct dsa_switch *ds, int port,
1700                        struct tc_taprio_qopt_offload *taprio)
1701 {
1702     struct hellcreek *hellcreek = ds->priv;
1703     struct hellcreek_port *hellcreek_port;
1704     bool startable;
1705     u16 ctrl;
1706 
1707     hellcreek_port = &hellcreek->ports[port];
1708 
1709     dev_dbg(hellcreek->dev, "Configure traffic schedule on port %d\n",
1710         port);
1711 
1712     /* First cancel delayed work */
1713     cancel_delayed_work_sync(&hellcreek_port->schedule_work);
1714 
1715     mutex_lock(&hellcreek->reg_lock);
1716 
1717     if (hellcreek_port->current_schedule) {
1718         taprio_offload_free(hellcreek_port->current_schedule);
1719         hellcreek_port->current_schedule = NULL;
1720     }
1721     hellcreek_port->current_schedule = taprio_offload_get(taprio);
1722 
1723     /* Then select port */
1724     hellcreek_select_tgd(hellcreek, port);
1725 
1726     /* Enable gating and keep defaults */
1727     ctrl = (0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT) | TR_TGDCTRL_GATE_EN;
1728     hellcreek_write(hellcreek, ctrl, TR_TGDCTRL);
1729 
1730     /* Cancel pending schedule */
1731     hellcreek_write(hellcreek, 0x00, TR_ESTCMD);
1732 
1733     /* Setup a new schedule */
1734     hellcreek_setup_gcl(hellcreek, port, hellcreek_port->current_schedule);
1735 
1736     /* Configure cycle time */
1737     hellcreek_set_cycle_time(hellcreek, hellcreek_port->current_schedule);
1738 
1739     /* Check starting time */
1740     startable = hellcreek_schedule_startable(hellcreek, port);
1741     if (startable) {
1742         hellcreek_start_schedule(hellcreek, port);
1743         mutex_unlock(&hellcreek->reg_lock);
1744         return 0;
1745     }
1746 
1747     mutex_unlock(&hellcreek->reg_lock);
1748 
1749     /* Schedule periodic schedule check */
1750     schedule_delayed_work(&hellcreek_port->schedule_work,
1751                   HELLCREEK_SCHEDULE_PERIOD);
1752 
1753     return 0;
1754 }
1755 
1756 static int hellcreek_port_del_schedule(struct dsa_switch *ds, int port)
1757 {
1758     struct hellcreek *hellcreek = ds->priv;
1759     struct hellcreek_port *hellcreek_port;
1760 
1761     hellcreek_port = &hellcreek->ports[port];
1762 
1763     dev_dbg(hellcreek->dev, "Remove traffic schedule on port %d\n", port);
1764 
1765     /* First cancel delayed work */
1766     cancel_delayed_work_sync(&hellcreek_port->schedule_work);
1767 
1768     mutex_lock(&hellcreek->reg_lock);
1769 
1770     if (hellcreek_port->current_schedule) {
1771         taprio_offload_free(hellcreek_port->current_schedule);
1772         hellcreek_port->current_schedule = NULL;
1773     }
1774 
1775     /* Then select port */
1776     hellcreek_select_tgd(hellcreek, port);
1777 
1778     /* Disable gating and return to regular switching flow */
1779     hellcreek_write(hellcreek, 0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT,
1780             TR_TGDCTRL);
1781 
1782     mutex_unlock(&hellcreek->reg_lock);
1783 
1784     return 0;
1785 }
1786 
1787 static bool hellcreek_validate_schedule(struct hellcreek *hellcreek,
1788                     struct tc_taprio_qopt_offload *schedule)
1789 {
1790     size_t i;
1791 
1792     /* Does this hellcreek version support Qbv in hardware? */
1793     if (!hellcreek->pdata->qbv_support)
1794         return false;
1795 
1796     /* cycle time can only be 32bit */
1797     if (schedule->cycle_time > (u32)-1)
1798         return false;
1799 
1800     /* cycle time extension is not supported */
1801     if (schedule->cycle_time_extension)
1802         return false;
1803 
1804     /* Only set command is supported */
1805     for (i = 0; i < schedule->num_entries; ++i)
1806         if (schedule->entries[i].command != TC_TAPRIO_CMD_SET_GATES)
1807             return false;
1808 
1809     return true;
1810 }
1811 
1812 static int hellcreek_port_setup_tc(struct dsa_switch *ds, int port,
1813                    enum tc_setup_type type, void *type_data)
1814 {
1815     struct tc_taprio_qopt_offload *taprio = type_data;
1816     struct hellcreek *hellcreek = ds->priv;
1817 
1818     if (type != TC_SETUP_QDISC_TAPRIO)
1819         return -EOPNOTSUPP;
1820 
1821     if (!hellcreek_validate_schedule(hellcreek, taprio))
1822         return -EOPNOTSUPP;
1823 
1824     if (taprio->enable)
1825         return hellcreek_port_set_schedule(ds, port, taprio);
1826 
1827     return hellcreek_port_del_schedule(ds, port);
1828 }
1829 
1830 static const struct dsa_switch_ops hellcreek_ds_ops = {
1831     .devlink_info_get      = hellcreek_devlink_info_get,
1832     .get_ethtool_stats     = hellcreek_get_ethtool_stats,
1833     .get_sset_count        = hellcreek_get_sset_count,
1834     .get_strings           = hellcreek_get_strings,
1835     .get_tag_protocol      = hellcreek_get_tag_protocol,
1836     .get_ts_info           = hellcreek_get_ts_info,
1837     .phylink_get_caps      = hellcreek_phylink_get_caps,
1838     .port_bridge_flags     = hellcreek_bridge_flags,
1839     .port_bridge_join      = hellcreek_port_bridge_join,
1840     .port_bridge_leave     = hellcreek_port_bridge_leave,
1841     .port_disable          = hellcreek_port_disable,
1842     .port_enable           = hellcreek_port_enable,
1843     .port_fdb_add          = hellcreek_fdb_add,
1844     .port_fdb_del          = hellcreek_fdb_del,
1845     .port_fdb_dump         = hellcreek_fdb_dump,
1846     .port_hwtstamp_set     = hellcreek_port_hwtstamp_set,
1847     .port_hwtstamp_get     = hellcreek_port_hwtstamp_get,
1848     .port_pre_bridge_flags = hellcreek_pre_bridge_flags,
1849     .port_prechangeupper   = hellcreek_port_prechangeupper,
1850     .port_rxtstamp         = hellcreek_port_rxtstamp,
1851     .port_setup_tc         = hellcreek_port_setup_tc,
1852     .port_stp_state_set    = hellcreek_port_stp_state_set,
1853     .port_txtstamp         = hellcreek_port_txtstamp,
1854     .port_vlan_add         = hellcreek_vlan_add,
1855     .port_vlan_del         = hellcreek_vlan_del,
1856     .port_vlan_filtering   = hellcreek_vlan_filtering,
1857     .setup             = hellcreek_setup,
1858     .teardown          = hellcreek_teardown,
1859 };
1860 
1861 static int hellcreek_probe(struct platform_device *pdev)
1862 {
1863     struct device *dev = &pdev->dev;
1864     struct hellcreek *hellcreek;
1865     struct resource *res;
1866     int ret, i;
1867 
1868     hellcreek = devm_kzalloc(dev, sizeof(*hellcreek), GFP_KERNEL);
1869     if (!hellcreek)
1870         return -ENOMEM;
1871 
1872     hellcreek->vidmbrcfg = devm_kcalloc(dev, VLAN_N_VID,
1873                         sizeof(*hellcreek->vidmbrcfg),
1874                         GFP_KERNEL);
1875     if (!hellcreek->vidmbrcfg)
1876         return -ENOMEM;
1877 
1878     hellcreek->pdata = of_device_get_match_data(dev);
1879 
1880     hellcreek->ports = devm_kcalloc(dev, hellcreek->pdata->num_ports,
1881                     sizeof(*hellcreek->ports),
1882                     GFP_KERNEL);
1883     if (!hellcreek->ports)
1884         return -ENOMEM;
1885 
1886     for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
1887         struct hellcreek_port *port = &hellcreek->ports[i];
1888 
1889         port->counter_values =
1890             devm_kcalloc(dev,
1891                      ARRAY_SIZE(hellcreek_counter),
1892                      sizeof(*port->counter_values),
1893                      GFP_KERNEL);
1894         if (!port->counter_values)
1895             return -ENOMEM;
1896 
1897         port->vlan_dev_bitmap = devm_bitmap_zalloc(dev, VLAN_N_VID,
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");