+static int rtl8366_global_reset_mibs(struct switch_dev *dev,
+ const struct switch_attr *attr,
+ struct switch_val *val)
+{
+ u32 data = 0;
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+
+ if (val->value.i == 1) {
+ rtl8366_smi_read_reg(smi, RTL8366S_MIB_CTRL_REG, &data);
+ data |= (1 << 2);
+ rtl8366_smi_write_reg(smi, RTL8366S_MIB_CTRL_REG, data);
+ }
+
+ return 0;
+}
+
+static int rtl8366_get_vlan(struct switch_dev *dev,
+ const struct switch_attr *attr,
+ struct switch_val *val)
+{
+ u32 data;
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+
+ if (attr->ofs == 1) {
+ rtl8366_smi_read_reg(smi, RTL8366_CHIP_GLOBAL_CTRL_REG, &data);
+
+ if (data & RTL8366_CHIP_CTRL_VLAN)
+ val->value.i = 1;
+ else
+ val->value.i = 0;
+ } else if (attr->ofs == 2) {
+ rtl8366_smi_read_reg(smi, RTL8366S_VLAN_TB_CTRL_REG, &data);
+
+ if (data & 0x0001)
+ val->value.i = 1;
+ else
+ val->value.i = 0;
+ }
+
+ return 0;
+}
+
+static int rtl8366_global_get_blinkrate(struct switch_dev *dev,
+ const struct switch_attr *attr,
+ struct switch_val *val)
+{
+ u32 data;
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+ rtl8366_smi_read_reg(smi, RTL8366_LED_BLINKRATE_REG, &data);
+
+ val->value.i = (data & (RTL8366_LED_BLINKRATE_MASK));
+
+ return 0;
+}
+
+static int rtl8366_global_set_blinkrate(struct switch_dev *dev,
+ const struct switch_attr *attr,
+ struct switch_val *val)
+{
+ u32 data;
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+
+ if (val->value.i >= 6)
+ return -EINVAL;
+
+ rtl8366_smi_read_reg(smi, RTL8366_LED_BLINKRATE_REG, &data);
+
+ data &= ~(data & RTL8366_LED_BLINKRATE_MASK);
+ data |= val->value.i;
+
+ rtl8366_smi_write_reg(smi, RTL8366_LED_BLINKRATE_REG, data);
+
+ return 0;
+}
+
+static int rtl8366_set_vlan(struct switch_dev *dev,
+ const struct switch_attr *attr,
+ struct switch_val *val)
+{
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+
+ if (attr->ofs == 1)
+ return rtl8366_vlan_set_vlan(smi, val->value.i);
+ else
+ return rtl8366_vlan_set_4ktable(smi, val->value.i);
+}
+
+static int rtl8366_attr_get_port_link(struct switch_dev *dev,
+ const struct switch_attr *attr,
+ struct switch_val *val)
+{
+ u32 len = 0, data = 0;
+ int speed, duplex, link, txpause, rxpause, nway;
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+ char *buf = smi->buf;
+
+ if (val->port_vlan >= RTL8366_NUM_PORTS)
+ return -EINVAL;
+
+ memset(buf, '\0', sizeof(smi->buf));
+ rtl8366_smi_read_reg(smi, RTL8366S_PORT_LINK_STATUS_BASE +
+ (val->port_vlan >> 1),
+ &data);
+
+ if (val->port_vlan & 0x1)
+ data = data >> 8;
+
+ speed = (data & RTL8366S_PORT_STATUS_SPEED_MASK);
+ duplex = (data & RTL8366S_PORT_STATUS_DUPLEX_MASK) >> 2;
+ link = (data & RTL8366S_PORT_STATUS_LINK_MASK) >> 4;
+ txpause = (data & RTL8366S_PORT_STATUS_TXPAUSE_MASK) >> 5;
+ rxpause = (data & RTL8366S_PORT_STATUS_RXPAUSE_MASK) >> 6;
+ nway = (data & RTL8366S_PORT_STATUS_AN_MASK) >> 7;
+
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "Port %d: ",
+ val->port_vlan);
+
+ if (link)
+ len += snprintf(buf + len, sizeof(smi->buf) - len,
+ "Link UP, Speed: ");
+ else
+ len += snprintf(buf + len, sizeof(smi->buf) - len,
+ "Link DOWN, Speed: ");
+
+ if (speed == 0)
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "10Base-TX ");
+ else if (speed == 1)
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "100Base-TX ");
+ else if (speed == 2)
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "1000Base-TX ");
+
+ if (duplex)
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "Full-Duplex, ");
+ else
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "Half-Duplex, ");
+
+ if (txpause)
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "TX-Pause ");
+ if (rxpause)
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "RX-Pause ");
+ if (nway)
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "nway ");
+
+ val->value.s = buf;
+ val->len = len;
+
+ return 0;
+}
+
+static int rtl8366_attr_get_vlan_info(struct switch_dev *dev,
+ const struct switch_attr *attr,
+ struct switch_val *val)
+{
+ int i;
+ u32 len = 0;
+ struct rtl8366s_vlanconfig vlanmc;
+ struct rtl8366s_vlan4kentry vlan4k;
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+ char *buf = smi->buf;
+
+ if (val->port_vlan >= RTL8366_NUM_PORTS)
+ return -EINVAL;
+
+ memset(buf, '\0', sizeof(smi->buf));
+
+ rtl8366s_get_vlan_member_config(smi, val->port_vlan, &vlanmc);
+ rtl8366s_get_vlan_4k_entry(smi, vlanmc.vid, &vlan4k);
+
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "VLAN %d: Ports: ",
+ val->port_vlan);
+
+ for (i = 0; i < RTL8366_NUM_PORTS; ++i) {
+ int index = 0;
+ if (!rtl8366_get_port_vlan_index(smi, i, &index) &&
+ index == val->port_vlan)
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "%d", i);
+ }
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "\n");
+
+ len += snprintf(buf + len, sizeof(smi->buf) - len,
+ "\t\t vid \t prio \t member \t untag \t fid\n");
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "\tMC:\t");
+ len += snprintf(buf + len, sizeof(smi->buf) - len,
+ "%d \t %d \t 0x%04x \t 0x%04x \t %d\n",
+ vlanmc.vid, vlanmc.priority, vlanmc.member,
+ vlanmc.untag, vlanmc.fid);
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "\t4K:\t");
+ len += snprintf(buf + len, sizeof(smi->buf) - len,
+ "%d \t \t 0x%04x \t 0x%04x \t %d",
+ vlan4k.vid, vlan4k.member, vlan4k.untag, vlan4k.fid);
+
+ val->value.s = buf;
+ val->len = len;
+
+ return 0;
+}
+
+static int rtl8366_set_port_led(struct switch_dev *dev,
+ const struct switch_attr *attr,
+ struct switch_val *val)
+{
+ u32 data = 0;
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+ if (val->port_vlan >= RTL8366_NUM_PORTS ||
+ (1 << val->port_vlan) == RTL8366_PORT_UNKNOWN)
+ return -EINVAL;
+
+ if (val->port_vlan == RTL8366_PORT_NUM_CPU) {
+ rtl8366_smi_read_reg(smi, RTL8366_LED_BLINKRATE_REG, &data);
+ data = (data & (~(0xF << 4))) | (val->value.i << 4);
+ rtl8366_smi_write_reg(smi, RTL8366_LED_BLINKRATE_REG, data);
+ } else {
+ rtl8366_smi_read_reg(smi, RTL8366_LED_CTRL_REG, &data);
+ data = (data & (~(0xF << (val->port_vlan * 4)))) |
+ (val->value.i << (val->port_vlan * 4));
+ rtl8366_smi_write_reg(smi, RTL8366_LED_CTRL_REG, data);
+ }
+
+ return 0;
+}
+
+static int rtl8366_get_port_led(struct switch_dev *dev,
+ const struct switch_attr *attr,
+ struct switch_val *val)
+{
+ u32 data = 0;
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+ if (val->port_vlan >= RTL8366_NUM_LEDGROUPS)
+ return -EINVAL;
+
+ rtl8366_smi_read_reg(smi, RTL8366_LED_CTRL_REG, &data);
+ val->value.i = (data >> (val->port_vlan * 4)) & 0x000F;
+
+ return 0;
+}
+
+static int rtl8366_reset_port_mibs(struct switch_dev *dev,
+ const struct switch_attr *attr,
+ struct switch_val *val)
+{
+ u32 data = 0;
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+ if (val->port_vlan >= RTL8366_NUM_PORTS)
+ return -EINVAL;
+
+ rtl8366_smi_read_reg(smi, RTL8366S_MIB_CTRL_REG, &data);
+ data |= (1 << (val->port_vlan + 3));
+ rtl8366_smi_write_reg(smi, RTL8366S_MIB_CTRL_REG, data);
+
+ return 0;
+}
+
+static int rtl8366_get_port_mib(struct switch_dev *dev,
+ const struct switch_attr *attr,
+ struct switch_val *val)
+{
+ int i, len = 0;
+ unsigned long long counter = 0;
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+ char *buf = smi->buf;
+
+ if (val->port_vlan >= RTL8366_NUM_PORTS)
+ return -EINVAL;
+
+ len += snprintf(buf + len, sizeof(smi->buf) - len, "Port %d MIB counters\n",
+ val->port_vlan);
+ for (i = 0; i < RTL8366S_MIB_COUNT; ++i) {
+
+ len += snprintf(buf + len, sizeof(smi->buf) - len,
+ "%d:%s\t", i, MIBCOUNTERS[i]);
+ if (!rtl8366_get_mib_counter(smi, i, val->port_vlan, &counter))
+ len += snprintf(buf + len, sizeof(smi->buf) - len,
+ "[%llu]\n", counter);
+ else
+ len += snprintf(buf + len, sizeof(smi->buf) - len,
+ "[error]\n");
+ }
+
+ val->value.s = buf;
+ val->len = len;
+ return 0;
+}
+
+static int rtl8366_get_ports(struct switch_dev *dev,
+ struct switch_val *val)
+{
+ struct rtl8366s_vlanconfig vlanmc;
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+ struct switch_port *port;
+ int i;
+
+ if (val->port_vlan >= RTL8366_NUM_VLANS)
+ return -EINVAL;
+
+ rtl8366s_get_vlan_member_config(smi, val->port_vlan, &vlanmc);
+
+ port = &val->value.ports[0];
+ val->len = 0;
+ for (i = 0; i < RTL8366_NUM_PORTS; i++) {
+ if (!(vlanmc.member & BIT(i)))
+ continue;
+
+ port->id = i;
+ port->flags = (vlanmc.untag & BIT(i)) ?
+ 0 : BIT(SWITCH_PORT_FLAG_TAGGED);
+ val->len++;
+ port++;
+ }
+ return 0;
+}
+
+static int rtl8366_set_ports(struct switch_dev *dev,
+ struct switch_val *val)
+{
+ struct rtl8366s_vlanconfig vlanmc;
+ struct rtl8366s_vlan4kentry vlan4k;
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+ struct switch_port *port;
+ int i;
+
+ if (val->port_vlan >= RTL8366_NUM_VLANS)
+ return -EINVAL;
+
+ rtl8366s_get_vlan_member_config(smi, val->port_vlan, &vlanmc);
+ rtl8366s_get_vlan_4k_entry(smi, vlanmc.vid, &vlan4k);
+
+ vlanmc.untag = 0;
+ vlanmc.member = 0;
+
+ port = &val->value.ports[0];
+ for (i = 0; i < val->len; i++, port++) {
+ vlanmc.member |= BIT(port->id);
+
+ if (!(port->flags & BIT(SWITCH_PORT_FLAG_TAGGED)))
+ vlanmc.untag |= BIT(port->id);
+ }
+
+ vlan4k.member = vlanmc.member;
+ vlan4k.untag = vlanmc.untag;
+
+ rtl8366s_set_vlan_member_config(smi, val->port_vlan, &vlanmc);
+ rtl8366s_set_vlan_4k_entry(smi, &vlan4k);
+ return 0;
+}
+
+static int rtl8366_get_port_pvid(struct switch_dev *dev, int port, int *val)
+{
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+ return rtl8366_get_vlan_port_pvid(smi, port, val);
+}
+
+static int rtl8366_set_port_pvid(struct switch_dev *dev, int port, int val)
+{
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+ return rtl8366_set_vlan_port_pvid(smi, port, val);
+}
+
+static int rtl8366_reset_switch(struct switch_dev *dev)
+{
+ struct rtl8366_smi *smi = sw_to_rtl8366(dev);
+ int timeout = 10;
+ u32 data;
+
+ rtl8366_smi_write_reg(smi, RTL8366_RESET_CTRL_REG,
+ RTL8366_CHIP_CTRL_RESET_HW);
+ do {
+ msleep(1);
+ if (rtl8366_smi_read_reg(smi, RTL8366_RESET_CTRL_REG, &data))
+ return -EIO;
+
+ if (!(data & RTL8366_CHIP_CTRL_RESET_HW))
+ break;
+ } while (--timeout);
+
+ if (!timeout) {
+ printk("Timeout waiting for the switch to reset\n");
+ return -EIO;
+ }
+
+ return rtl8366s_reset_vlan(smi);
+}
+
+static struct switch_attr rtl8366_globals[] = {
+ {
+ .type = SWITCH_TYPE_INT,
+ .name = "enable_vlan",
+ .description = "Enable VLAN mode",
+ .set = rtl8366_set_vlan,
+ .get = rtl8366_get_vlan,
+ .max = 1,
+ .ofs = 1
+ },
+ {
+ .type = SWITCH_TYPE_INT,
+ .name = "enable_vlan4k",
+ .description = "Enable VLAN 4K mode",
+ .set = rtl8366_set_vlan,
+ .get = rtl8366_get_vlan,
+ .max = 1,
+ .ofs = 2
+ },
+ {
+ .type = SWITCH_TYPE_INT,
+ .name = "reset_mibs",
+ .description = "Reset all MIB counters",
+ .set = rtl8366_global_reset_mibs,
+ .get = NULL,
+ .max = 1
+ },
+ {
+ .type = SWITCH_TYPE_INT,
+ .name = "blinkrate",
+ .description = "Get/Set LED blinking rate (0 = 43ms, 1 = 84ms,"
+ " 2 = 120ms, 3 = 170ms, 4 = 340ms, 5 = 670ms)",
+ .set = rtl8366_global_set_blinkrate,
+ .get = rtl8366_global_get_blinkrate,
+ .max = 5
+ },
+};
+
+static struct switch_attr rtl8366_port[] = {
+ {
+ .type = SWITCH_TYPE_STRING,
+ .name = "link",
+ .description = "Get port link information",
+ .max = 1,
+ .set = NULL,
+ .get = rtl8366_attr_get_port_link
+ },
+ {
+ .type = SWITCH_TYPE_INT,
+ .name = "reset_mib",
+ .description = "Reset single port MIB counters",
+ .max = 1,
+ .set = rtl8366_reset_port_mibs,
+ .get = NULL
+ },
+ {
+ .type = SWITCH_TYPE_STRING,
+ .name = "mib",
+ .description = "Get MIB counters for port",
+ .max = 33,
+ .set = NULL,
+ .get = rtl8366_get_port_mib
+ },
+ {
+ .type = SWITCH_TYPE_INT,
+ .name = "led",
+ .description = "Get/Set port group (0 - 3) led mode (0 - 15)",
+ .max = 15,
+ .set = rtl8366_set_port_led,
+ .get = rtl8366_get_port_led
+ },
+};
+
+static struct switch_attr rtl8366_vlan[] = {
+ {
+ .type = SWITCH_TYPE_STRING,
+ .name = "info",
+ .description = "Get vlan information",
+ .max = 1,
+ .set = NULL,
+ .get = rtl8366_attr_get_vlan_info
+ },
+};
+
+
+/* template */
+static struct switch_dev rtldev = {
+ .name = "RTL8366S",
+ .cpu_port = RTL8366_PORT_NUM_CPU,
+ .ports = RTL8366_NUM_PORTS,
+ .vlans = RTL8366_NUM_VLANS,
+ .attr_global = {
+ .attr = rtl8366_globals,
+ .n_attr = ARRAY_SIZE(rtl8366_globals),
+ },
+ .attr_port = {
+ .attr = rtl8366_port,
+ .n_attr = ARRAY_SIZE(rtl8366_port),
+ },
+ .attr_vlan = {
+ .attr = rtl8366_vlan,
+ .n_attr = ARRAY_SIZE(rtl8366_vlan),
+ },
+
+ .get_vlan_ports = rtl8366_get_ports,
+ .set_vlan_ports = rtl8366_set_ports,
+ .get_port_pvid = rtl8366_get_port_pvid,
+ .set_port_pvid = rtl8366_set_port_pvid,
+ .reset_switch = rtl8366_reset_switch,
+};
+
+static int rtl8366_smi_switch_init(struct rtl8366_smi *smi)
+{
+ struct switch_dev *dev = &smi->dev;
+ int err;
+
+ memcpy(dev, &rtldev, sizeof(struct switch_dev));
+ dev->priv = smi;
+ dev->devname = dev_name(&smi->pdev->dev);
+
+ err = register_switch(dev, NULL);
+ if (err)
+ dev_err(&smi->pdev->dev, "switch registration failed\n");
+
+ return err;
+}
+
+static void rtl8366_smi_switch_cleanup(struct rtl8366_smi *smi)
+{
+ unregister_switch(&smi->dev);
+}
+