generic: provide get_port_stats() on rtl836x switches
[openwrt/staging/kaloz.git] / target / linux / generic / files / drivers / net / phy / rtl8366_smi.c
index 804e827dbdb48ddf2fe53e3bd19e8b9fc964beac..ae045970dbb3eaf851aad1a8890c97dfb76cdfd0 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/of_platform.h>
 #include <linux/of_gpio.h>
 #include <linux/rtl8366.h>
+#include <linux/version.h>
 
 #ifdef CONFIG_RTL8366_SMI_DEBUG_FS
 #include <linux/debugfs.h>
@@ -743,7 +744,7 @@ static ssize_t rtl8366_write_debugfs_reg(struct file *file,
                buf[len - 1] = '\0';
 
 
-       if (strict_strtoul(buf, 16, &data)) {
+       if (kstrtoul(buf, 16, &data)) {
                dev_err(smi->parent, "Invalid reg value %s\n", buf);
        } else {
                err = rtl8366_smi_write_reg(smi, reg, data);
@@ -914,7 +915,6 @@ static inline void rtl8366_debugfs_remove(struct rtl8366_smi *smi) {}
 static int rtl8366_smi_mii_init(struct rtl8366_smi *smi)
 {
        int ret;
-       int i;
 
        smi->mii_bus = mdiobus_alloc();
        if (smi->mii_bus == NULL) {
@@ -930,9 +930,14 @@ static int rtl8366_smi_mii_init(struct rtl8366_smi *smi)
                 dev_name(smi->parent));
        smi->mii_bus->parent = smi->parent;
        smi->mii_bus->phy_mask = ~(0x1f);
-       smi->mii_bus->irq = smi->mii_irq;
-       for (i = 0; i < PHY_MAX_ADDR; i++)
-               smi->mii_irq[i] = PHY_POLL;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
+       {
+               int i;
+               smi->mii_bus->irq = smi->mii_irq;
+               for (i = 0; i < PHY_MAX_ADDR; i++)
+                       smi->mii_irq[i] = PHY_POLL;
+       }
+#endif
 
        ret = mdiobus_register(smi->mii_bus);
        if (ret)
@@ -1025,6 +1030,33 @@ int rtl8366_sw_get_port_mib(struct switch_dev *dev,
 }
 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib);
 
+int rtl8366_sw_get_port_stats(struct switch_dev *dev, int port,
+                               struct switch_port_stats *stats,
+                               int txb_id, int rxb_id)
+{
+       struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
+       unsigned long long counter = 0;
+       int ret;
+
+       if (port >= smi->num_ports)
+               return -EINVAL;
+
+       ret = smi->ops->get_mib_counter(smi, txb_id, port, &counter);
+       if (ret)
+               return ret;
+
+       stats->tx_bytes = counter;
+
+       ret = smi->ops->get_mib_counter(smi, rxb_id, port, &counter);
+       if (ret)
+               return ret;
+
+       stats->rx_bytes = counter;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_stats);
+
 int rtl8366_sw_get_vlan_info(struct switch_dev *dev,
                             const struct switch_attr *attr,
                             struct switch_val *val)
@@ -1109,6 +1141,7 @@ int rtl8366_sw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
 
        port = &val->value.ports[0];
        for (i = 0; i < val->len; i++, port++) {
+               int pvid = 0;
                member |= BIT(port->id);
 
                if (!(port->flags & BIT(SWITCH_PORT_FLAG_TAGGED)))
@@ -1118,9 +1151,14 @@ int rtl8366_sw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
                 * To ensure that we have a valid MC entry for this VLAN,
                 * initialize the port VLAN ID here.
                 */
-               err = rtl8366_set_pvid(smi, port->id, val->port_vlan);
+               err = rtl8366_get_pvid(smi, port->id, &pvid);
                if (err < 0)
                        return err;
+               if (pvid == 0) {
+                       err = rtl8366_set_pvid(smi, port->id, val->port_vlan);
+                       if (err < 0)
+                               return err;
+               }
        }
 
        return rtl8366_set_vlan(smi, val->port_vlan, member, untag, 0);