Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Handling of a master device, switching frames via its switch fabric CPU port
0004  *
0005  * Copyright (c) 2017 Savoir-faire Linux Inc.
0006  *  Vivien Didelot <vivien.didelot@savoirfairelinux.com>
0007  */
0008 
0009 #include "dsa_priv.h"
0010 
0011 static int dsa_master_get_regs_len(struct net_device *dev)
0012 {
0013     struct dsa_port *cpu_dp = dev->dsa_ptr;
0014     const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
0015     struct dsa_switch *ds = cpu_dp->ds;
0016     int port = cpu_dp->index;
0017     int ret = 0;
0018     int len;
0019 
0020     if (ops->get_regs_len) {
0021         len = ops->get_regs_len(dev);
0022         if (len < 0)
0023             return len;
0024         ret += len;
0025     }
0026 
0027     ret += sizeof(struct ethtool_drvinfo);
0028     ret += sizeof(struct ethtool_regs);
0029 
0030     if (ds->ops->get_regs_len) {
0031         len = ds->ops->get_regs_len(ds, port);
0032         if (len < 0)
0033             return len;
0034         ret += len;
0035     }
0036 
0037     return ret;
0038 }
0039 
0040 static void dsa_master_get_regs(struct net_device *dev,
0041                 struct ethtool_regs *regs, void *data)
0042 {
0043     struct dsa_port *cpu_dp = dev->dsa_ptr;
0044     const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
0045     struct dsa_switch *ds = cpu_dp->ds;
0046     struct ethtool_drvinfo *cpu_info;
0047     struct ethtool_regs *cpu_regs;
0048     int port = cpu_dp->index;
0049     int len;
0050 
0051     if (ops->get_regs_len && ops->get_regs) {
0052         len = ops->get_regs_len(dev);
0053         if (len < 0)
0054             return;
0055         regs->len = len;
0056         ops->get_regs(dev, regs, data);
0057         data += regs->len;
0058     }
0059 
0060     cpu_info = (struct ethtool_drvinfo *)data;
0061     strlcpy(cpu_info->driver, "dsa", sizeof(cpu_info->driver));
0062     data += sizeof(*cpu_info);
0063     cpu_regs = (struct ethtool_regs *)data;
0064     data += sizeof(*cpu_regs);
0065 
0066     if (ds->ops->get_regs_len && ds->ops->get_regs) {
0067         len = ds->ops->get_regs_len(ds, port);
0068         if (len < 0)
0069             return;
0070         cpu_regs->len = len;
0071         ds->ops->get_regs(ds, port, cpu_regs, data);
0072     }
0073 }
0074 
0075 static void dsa_master_get_ethtool_stats(struct net_device *dev,
0076                      struct ethtool_stats *stats,
0077                      uint64_t *data)
0078 {
0079     struct dsa_port *cpu_dp = dev->dsa_ptr;
0080     const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
0081     struct dsa_switch *ds = cpu_dp->ds;
0082     int port = cpu_dp->index;
0083     int count = 0;
0084 
0085     if (ops->get_sset_count && ops->get_ethtool_stats) {
0086         count = ops->get_sset_count(dev, ETH_SS_STATS);
0087         ops->get_ethtool_stats(dev, stats, data);
0088     }
0089 
0090     if (ds->ops->get_ethtool_stats)
0091         ds->ops->get_ethtool_stats(ds, port, data + count);
0092 }
0093 
0094 static void dsa_master_get_ethtool_phy_stats(struct net_device *dev,
0095                          struct ethtool_stats *stats,
0096                          uint64_t *data)
0097 {
0098     struct dsa_port *cpu_dp = dev->dsa_ptr;
0099     const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
0100     struct dsa_switch *ds = cpu_dp->ds;
0101     int port = cpu_dp->index;
0102     int count = 0;
0103 
0104     if (dev->phydev && !ops->get_ethtool_phy_stats) {
0105         count = phy_ethtool_get_sset_count(dev->phydev);
0106         if (count >= 0)
0107             phy_ethtool_get_stats(dev->phydev, stats, data);
0108     } else if (ops->get_sset_count && ops->get_ethtool_phy_stats) {
0109         count = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
0110         ops->get_ethtool_phy_stats(dev, stats, data);
0111     }
0112 
0113     if (count < 0)
0114         count = 0;
0115 
0116     if (ds->ops->get_ethtool_phy_stats)
0117         ds->ops->get_ethtool_phy_stats(ds, port, data + count);
0118 }
0119 
0120 static int dsa_master_get_sset_count(struct net_device *dev, int sset)
0121 {
0122     struct dsa_port *cpu_dp = dev->dsa_ptr;
0123     const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
0124     struct dsa_switch *ds = cpu_dp->ds;
0125     int count = 0;
0126 
0127     if (sset == ETH_SS_PHY_STATS && dev->phydev &&
0128         !ops->get_ethtool_phy_stats)
0129         count = phy_ethtool_get_sset_count(dev->phydev);
0130     else if (ops->get_sset_count)
0131         count = ops->get_sset_count(dev, sset);
0132 
0133     if (count < 0)
0134         count = 0;
0135 
0136     if (ds->ops->get_sset_count)
0137         count += ds->ops->get_sset_count(ds, cpu_dp->index, sset);
0138 
0139     return count;
0140 }
0141 
0142 static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset,
0143                    uint8_t *data)
0144 {
0145     struct dsa_port *cpu_dp = dev->dsa_ptr;
0146     const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
0147     struct dsa_switch *ds = cpu_dp->ds;
0148     int port = cpu_dp->index;
0149     int len = ETH_GSTRING_LEN;
0150     int mcount = 0, count, i;
0151     uint8_t pfx[4];
0152     uint8_t *ndata;
0153 
0154     snprintf(pfx, sizeof(pfx), "p%.2d", port);
0155     /* We do not want to be NULL-terminated, since this is a prefix */
0156     pfx[sizeof(pfx) - 1] = '_';
0157 
0158     if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
0159         !ops->get_ethtool_phy_stats) {
0160         mcount = phy_ethtool_get_sset_count(dev->phydev);
0161         if (mcount < 0)
0162             mcount = 0;
0163         else
0164             phy_ethtool_get_strings(dev->phydev, data);
0165     } else if (ops->get_sset_count && ops->get_strings) {
0166         mcount = ops->get_sset_count(dev, stringset);
0167         if (mcount < 0)
0168             mcount = 0;
0169         ops->get_strings(dev, stringset, data);
0170     }
0171 
0172     if (ds->ops->get_strings) {
0173         ndata = data + mcount * len;
0174         /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
0175          * the output after to prepend our CPU port prefix we
0176          * constructed earlier
0177          */
0178         ds->ops->get_strings(ds, port, stringset, ndata);
0179         count = ds->ops->get_sset_count(ds, port, stringset);
0180         if (count < 0)
0181             return;
0182         for (i = 0; i < count; i++) {
0183             memmove(ndata + (i * len + sizeof(pfx)),
0184                 ndata + i * len, len - sizeof(pfx));
0185             memcpy(ndata + i * len, pfx, sizeof(pfx));
0186         }
0187     }
0188 }
0189 
0190 static int dsa_master_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
0191 {
0192     struct dsa_port *cpu_dp = dev->dsa_ptr;
0193     struct dsa_switch *ds = cpu_dp->ds;
0194     struct dsa_switch_tree *dst;
0195     int err = -EOPNOTSUPP;
0196     struct dsa_port *dp;
0197 
0198     dst = ds->dst;
0199 
0200     switch (cmd) {
0201     case SIOCGHWTSTAMP:
0202     case SIOCSHWTSTAMP:
0203         /* Deny PTP operations on master if there is at least one
0204          * switch in the tree that is PTP capable.
0205          */
0206         list_for_each_entry(dp, &dst->ports, list)
0207             if (dp->ds->ops->port_hwtstamp_get ||
0208                 dp->ds->ops->port_hwtstamp_set)
0209                 return -EBUSY;
0210         break;
0211     }
0212 
0213     if (dev->netdev_ops->ndo_eth_ioctl)
0214         err = dev->netdev_ops->ndo_eth_ioctl(dev, ifr, cmd);
0215 
0216     return err;
0217 }
0218 
0219 static const struct dsa_netdevice_ops dsa_netdev_ops = {
0220     .ndo_eth_ioctl = dsa_master_ioctl,
0221 };
0222 
0223 static int dsa_master_ethtool_setup(struct net_device *dev)
0224 {
0225     struct dsa_port *cpu_dp = dev->dsa_ptr;
0226     struct dsa_switch *ds = cpu_dp->ds;
0227     struct ethtool_ops *ops;
0228 
0229     ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
0230     if (!ops)
0231         return -ENOMEM;
0232 
0233     cpu_dp->orig_ethtool_ops = dev->ethtool_ops;
0234     if (cpu_dp->orig_ethtool_ops)
0235         memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops));
0236 
0237     ops->get_regs_len = dsa_master_get_regs_len;
0238     ops->get_regs = dsa_master_get_regs;
0239     ops->get_sset_count = dsa_master_get_sset_count;
0240     ops->get_ethtool_stats = dsa_master_get_ethtool_stats;
0241     ops->get_strings = dsa_master_get_strings;
0242     ops->get_ethtool_phy_stats = dsa_master_get_ethtool_phy_stats;
0243 
0244     dev->ethtool_ops = ops;
0245 
0246     return 0;
0247 }
0248 
0249 static void dsa_master_ethtool_teardown(struct net_device *dev)
0250 {
0251     struct dsa_port *cpu_dp = dev->dsa_ptr;
0252 
0253     dev->ethtool_ops = cpu_dp->orig_ethtool_ops;
0254     cpu_dp->orig_ethtool_ops = NULL;
0255 }
0256 
0257 static void dsa_netdev_ops_set(struct net_device *dev,
0258                    const struct dsa_netdevice_ops *ops)
0259 {
0260     dev->dsa_ptr->netdev_ops = ops;
0261 }
0262 
0263 /* Keep the master always promiscuous if the tagging protocol requires that
0264  * (garbles MAC DA) or if it doesn't support unicast filtering, case in which
0265  * it would revert to promiscuous mode as soon as we call dev_uc_add() on it
0266  * anyway.
0267  */
0268 static void dsa_master_set_promiscuity(struct net_device *dev, int inc)
0269 {
0270     const struct dsa_device_ops *ops = dev->dsa_ptr->tag_ops;
0271 
0272     if ((dev->priv_flags & IFF_UNICAST_FLT) && !ops->promisc_on_master)
0273         return;
0274 
0275     ASSERT_RTNL();
0276 
0277     dev_set_promiscuity(dev, inc);
0278 }
0279 
0280 static ssize_t tagging_show(struct device *d, struct device_attribute *attr,
0281                 char *buf)
0282 {
0283     struct net_device *dev = to_net_dev(d);
0284     struct dsa_port *cpu_dp = dev->dsa_ptr;
0285 
0286     return sprintf(buf, "%s\n",
0287                dsa_tag_protocol_to_str(cpu_dp->tag_ops));
0288 }
0289 
0290 static ssize_t tagging_store(struct device *d, struct device_attribute *attr,
0291                  const char *buf, size_t count)
0292 {
0293     const struct dsa_device_ops *new_tag_ops, *old_tag_ops;
0294     struct net_device *dev = to_net_dev(d);
0295     struct dsa_port *cpu_dp = dev->dsa_ptr;
0296     int err;
0297 
0298     old_tag_ops = cpu_dp->tag_ops;
0299     new_tag_ops = dsa_find_tagger_by_name(buf);
0300     /* Bad tagger name, or module is not loaded? */
0301     if (IS_ERR(new_tag_ops))
0302         return PTR_ERR(new_tag_ops);
0303 
0304     if (new_tag_ops == old_tag_ops)
0305         /* Drop the temporarily held duplicate reference, since
0306          * the DSA switch tree uses this tagger.
0307          */
0308         goto out;
0309 
0310     err = dsa_tree_change_tag_proto(cpu_dp->ds->dst, dev, new_tag_ops,
0311                     old_tag_ops);
0312     if (err) {
0313         /* On failure the old tagger is restored, so we don't need the
0314          * driver for the new one.
0315          */
0316         dsa_tag_driver_put(new_tag_ops);
0317         return err;
0318     }
0319 
0320     /* On success we no longer need the module for the old tagging protocol
0321      */
0322 out:
0323     dsa_tag_driver_put(old_tag_ops);
0324     return count;
0325 }
0326 static DEVICE_ATTR_RW(tagging);
0327 
0328 static struct attribute *dsa_slave_attrs[] = {
0329     &dev_attr_tagging.attr,
0330     NULL
0331 };
0332 
0333 static const struct attribute_group dsa_group = {
0334     .name   = "dsa",
0335     .attrs  = dsa_slave_attrs,
0336 };
0337 
0338 static void dsa_master_reset_mtu(struct net_device *dev)
0339 {
0340     int err;
0341 
0342     err = dev_set_mtu(dev, ETH_DATA_LEN);
0343     if (err)
0344         netdev_dbg(dev,
0345                "Unable to reset MTU to exclude DSA overheads\n");
0346 }
0347 
0348 int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp)
0349 {
0350     const struct dsa_device_ops *tag_ops = cpu_dp->tag_ops;
0351     struct dsa_switch *ds = cpu_dp->ds;
0352     struct device_link *consumer_link;
0353     int mtu, ret;
0354 
0355     mtu = ETH_DATA_LEN + dsa_tag_protocol_overhead(tag_ops);
0356 
0357     /* The DSA master must use SET_NETDEV_DEV for this to work. */
0358     consumer_link = device_link_add(ds->dev, dev->dev.parent,
0359                     DL_FLAG_AUTOREMOVE_CONSUMER);
0360     if (!consumer_link)
0361         netdev_err(dev,
0362                "Failed to create a device link to DSA switch %s\n",
0363                dev_name(ds->dev));
0364 
0365     /* The switch driver may not implement ->port_change_mtu(), case in
0366      * which dsa_slave_change_mtu() will not update the master MTU either,
0367      * so we need to do that here.
0368      */
0369     ret = dev_set_mtu(dev, mtu);
0370     if (ret)
0371         netdev_warn(dev, "error %d setting MTU to %d to include DSA overhead\n",
0372                 ret, mtu);
0373 
0374     /* If we use a tagging format that doesn't have an ethertype
0375      * field, make sure that all packets from this point on get
0376      * sent to the tag format's receive function.
0377      */
0378     wmb();
0379 
0380     dev->dsa_ptr = cpu_dp;
0381 
0382     dsa_master_set_promiscuity(dev, 1);
0383 
0384     ret = dsa_master_ethtool_setup(dev);
0385     if (ret)
0386         goto out_err_reset_promisc;
0387 
0388     dsa_netdev_ops_set(dev, &dsa_netdev_ops);
0389 
0390     ret = sysfs_create_group(&dev->dev.kobj, &dsa_group);
0391     if (ret)
0392         goto out_err_ndo_teardown;
0393 
0394     return ret;
0395 
0396 out_err_ndo_teardown:
0397     dsa_netdev_ops_set(dev, NULL);
0398     dsa_master_ethtool_teardown(dev);
0399 out_err_reset_promisc:
0400     dsa_master_set_promiscuity(dev, -1);
0401     return ret;
0402 }
0403 
0404 void dsa_master_teardown(struct net_device *dev)
0405 {
0406     sysfs_remove_group(&dev->dev.kobj, &dsa_group);
0407     dsa_netdev_ops_set(dev, NULL);
0408     dsa_master_ethtool_teardown(dev);
0409     dsa_master_reset_mtu(dev);
0410     dsa_master_set_promiscuity(dev, -1);
0411 
0412     dev->dsa_ptr = NULL;
0413 
0414     /* If we used a tagging format that doesn't have an ethertype
0415      * field, make sure that all packets from this point get sent
0416      * without the tag and go through the regular receive path.
0417      */
0418     wmb();
0419 }