kernel: swconfig: make compatible with kernel 5.2
[openwrt/staging/mkresin.git] / target / linux / generic / files / drivers / net / phy / swconfig.c
index 34f43bfd9b93ed504fed087af1dec5033761efd3..4b039991dba99b3ffe414c55fb919918d8c86e27 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * swconfig.c: Switch configuration API
  *
- * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 #include <linux/capability.h>
 #include <linux/skbuff.h>
 #include <linux/switch.h>
-
-//#define DEBUG 1
-#ifdef DEBUG
-#define DPRINTF(format, ...) printk("%s: " format, __func__, ##__VA_ARGS__)
-#else
-#define DPRINTF(...) do {} while(0)
-#endif
+#include <linux/of.h>
+#include <linux/version.h>
+#include <uapi/linux/mii.h>
 
 #define SWCONFIG_DEVNAME       "switch%d"
 
 #include "swconfig_leds.c"
 
-MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
+MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
 MODULE_LICENSE("GPL");
 
-static int swdev_id = 0;
+static int swdev_id;
 static struct list_head swdevs;
-static DEFINE_SPINLOCK(swdevs_lock);
+static DEFINE_MUTEX(swdevs_lock);
 struct swconfig_callback;
 
-struct swconfig_callback
-{
+struct swconfig_callback {
        struct sk_buff *msg;
        struct genlmsghdr *hdr;
        struct genl_info *info;
@@ -63,7 +58,8 @@ struct swconfig_callback
 /* defaults */
 
 static int
-swconfig_get_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
+swconfig_get_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
+                       struct switch_val *val)
 {
        int ret;
        if (val->port_vlan >= dev->vlans)
@@ -77,7 +73,8 @@ swconfig_get_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
 }
 
 static int
-swconfig_set_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
+swconfig_set_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
+                       struct switch_val *val)
 {
        struct switch_port *ports = val->value.ports;
        const struct switch_dev_ops *ops = dev->ops;
@@ -106,7 +103,8 @@ swconfig_set_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
 }
 
 static int
-swconfig_set_pvid(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
+swconfig_set_pvid(struct switch_dev *dev, const struct switch_attr *attr,
+                       struct switch_val *val)
 {
        if (val->port_vlan >= dev->ports)
                return -EINVAL;
@@ -118,7 +116,8 @@ swconfig_set_pvid(struct switch_dev *dev, const struct switch_attr *attr, struct
 }
 
 static int
-swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
+swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr,
+                       struct switch_val *val)
 {
        if (val->port_vlan >= dev->ports)
                return -EINVAL;
@@ -129,29 +128,21 @@ swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr, struct
        return dev->ops->get_port_pvid(dev, val->port_vlan, &val->value.i);
 }
 
-static const char *
-swconfig_speed_str(enum switch_port_speed speed)
+static int
+swconfig_set_link(struct switch_dev *dev, const struct switch_attr *attr,
+                       struct switch_val *val)
 {
-       switch (speed) {
-       case SWITCH_PORT_SPEED_10:
-               return "10baseT";
-       case SWITCH_PORT_SPEED_100:
-               return "100baseT";
-       case SWITCH_PORT_SPEED_1000:
-               return "1000baseT";
-       default:
-               break;
-       }
+       if (!dev->ops->set_port_link)
+               return -EOPNOTSUPP;
 
-       return "unknown";
+       return dev->ops->set_port_link(dev, val->port_vlan, val->value.link);
 }
 
 static int
-swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
+swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr,
+                       struct switch_val *val)
 {
-       struct switch_port_link link;
-       int len;
-       int ret;
+       struct switch_port_link *link = val->value.link;
 
        if (val->port_vlan >= dev->ports)
                return -EINVAL;
@@ -159,34 +150,13 @@ swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr, struct
        if (!dev->ops->get_port_link)
                return -EOPNOTSUPP;
 
-       memset(&link, 0, sizeof(link));
-       ret = dev->ops->get_port_link(dev, val->port_vlan, &link);
-       if (ret)
-               return ret;
-
-       memset(dev->buf, 0, sizeof(dev->buf));
-
-       if (link.link)
-               len = snprintf(dev->buf, sizeof(dev->buf),
-                              "port:%d link:up speed:%s %s-duplex %s%s%s",
-                              val->port_vlan,
-                              swconfig_speed_str(link.speed),
-                              link.duplex ? "full" : "half",
-                              link.tx_flow ? "txflow ": "",
-                              link.rx_flow ?   "rxflow " : "",
-                              link.aneg ? "auto" : "");
-       else
-               len = snprintf(dev->buf, sizeof(dev->buf), "port:%d link:down",
-                              val->port_vlan);
-
-       val->value.s = dev->buf;
-       val->len = len;
-
-       return 0;
+       memset(link, 0, sizeof(*link));
+       return dev->ops->get_port_link(dev, val->port_vlan, link);
 }
 
 static int
-swconfig_apply_config(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
+swconfig_apply_config(struct switch_dev *dev, const struct switch_attr *attr,
+                       struct switch_val *val)
 {
        /* don't complain if not supported by the switch driver */
        if (!dev->ops->apply_config)
@@ -196,7 +166,8 @@ swconfig_apply_config(struct switch_dev *dev, const struct switch_attr *attr, st
 }
 
 static int
-swconfig_reset_switch(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
+swconfig_reset_switch(struct switch_dev *dev, const struct switch_attr *attr,
+                       struct switch_val *val)
 {
        /* don't complain if not supported by the switch driver */
        if (!dev->ops->reset_switch)
@@ -243,10 +214,10 @@ static struct switch_attr default_port[] = {
                .get = swconfig_get_pvid,
        },
        [PORT_LINK] = {
-               .type = SWITCH_TYPE_STRING,
+               .type = SWITCH_TYPE_LINK,
                .name = "link",
                .description = "Get port link information",
-               .set = NULL,
+               .set = swconfig_set_link,
                .get = swconfig_get_link,
        }
 };
@@ -262,7 +233,8 @@ static struct switch_attr default_vlan[] = {
 };
 
 static const struct switch_attr *
-swconfig_find_attr_by_name(const struct switch_attrlist *alist, const char *name)
+swconfig_find_attr_by_name(const struct switch_attrlist *alist,
+                               const char *name)
 {
        int i;
 
@@ -297,13 +269,7 @@ static void swconfig_defaults_init(struct switch_dev *dev)
 }
 
 
-static struct genl_family switch_fam = {
-       .id = GENL_ID_GENERATE,
-       .name = "switch",
-       .hdrsize = 0,
-       .version = 1,
-       .maxattr = SWITCH_ATTR_MAX,
-};
+static struct genl_family switch_fam;
 
 static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
        [SWITCH_ATTR_ID] = { .type = NLA_U32 },
@@ -321,16 +287,22 @@ static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
        [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
 };
 
+static struct nla_policy link_policy[SWITCH_LINK_ATTR_MAX] = {
+       [SWITCH_LINK_FLAG_DUPLEX] = { .type = NLA_FLAG },
+       [SWITCH_LINK_FLAG_ANEG] = { .type = NLA_FLAG },
+       [SWITCH_LINK_SPEED] = { .type = NLA_U32 },
+};
+
 static inline void
 swconfig_lock(void)
 {
-       spin_lock(&swdevs_lock);
+       mutex_lock(&swdevs_lock);
 }
 
 static inline void
 swconfig_unlock(void)
 {
-       spin_unlock(&swdevs_lock);
+       mutex_unlock(&swdevs_lock);
 }
 
 static struct switch_dev *
@@ -355,7 +327,7 @@ swconfig_get_dev(struct genl_info *info)
        if (dev)
                mutex_lock(&dev->sw_mutex);
        else
-               DPRINTF("device %d not found\n", id);
+               pr_debug("device %d not found\n", id);
        swconfig_unlock();
 done:
        return dev;
@@ -392,7 +364,8 @@ swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
                        op->description))
                        goto nla_put_failure;
 
-       return genlmsg_end(msg, hdr);
+       genlmsg_end(msg, hdr);
+       return msg->len;
 nla_put_failure:
        genlmsg_cancel(msg, hdr);
        return -EMSGSIZE;
@@ -460,7 +433,7 @@ swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
        if (!dev)
                return -EINVAL;
 
-       switch(hdr->cmd) {
+       switch (hdr->cmd) {
        case SWITCH_CMD_LIST_GLOBAL:
                alist = &dev->ops->attr_global;
                def_list = default_global;
@@ -527,7 +500,7 @@ swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
        struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
        const struct switch_attrlist *alist;
        const struct switch_attr *attr = NULL;
-       int attr_id;
+       unsigned int attr_id;
 
        /* defaults */
        struct switch_attr *def_list;
@@ -537,7 +510,7 @@ swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
        if (!info->attrs[SWITCH_ATTR_OP_ID])
                goto done;
 
-       switch(hdr->cmd) {
+       switch (hdr->cmd) {
        case SWITCH_CMD_SET_GLOBAL:
        case SWITCH_CMD_GET_GLOBAL:
                alist = &dev->ops->attr_global;
@@ -596,7 +569,7 @@ swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
 
 done:
        if (!attr)
-               DPRINTF("attribute lookup failed\n");
+               pr_debug("attribute lookup failed\n");
        val->attr = attr;
        return attr;
 }
@@ -611,13 +584,23 @@ swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
        val->len = 0;
        nla_for_each_nested(nla, head, rem) {
                struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
-               struct switch_port *port = &val->value.ports[val->len];
+               struct switch_port *port;
 
                if (val->len >= max)
                        return -EINVAL;
 
+               port = &val->value.ports[val->len];
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
+               if (nla_parse_nested_deprecated(tb, SWITCH_PORT_ATTR_MAX, nla,
+                               port_policy, NULL))
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4,12,0)
+               if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
+                               port_policy, NULL))
+#else
                if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
                                port_policy))
+#endif
                        return -EINVAL;
 
                if (!tb[SWITCH_PORT_ID])
@@ -632,6 +615,28 @@ swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
        return 0;
 }
 
+static int
+swconfig_parse_link(struct sk_buff *msg, struct nlattr *nla,
+                   struct switch_port_link *link)
+{
+       struct nlattr *tb[SWITCH_LINK_ATTR_MAX + 1];
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
+       if (nla_parse_nested_deprecated(tb, SWITCH_LINK_ATTR_MAX, nla, link_policy, NULL))
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4,12,0)
+       if (nla_parse_nested(tb, SWITCH_LINK_ATTR_MAX, nla, link_policy, NULL))
+#else
+       if (nla_parse_nested(tb, SWITCH_LINK_ATTR_MAX, nla, link_policy))
+#endif
+               return -EINVAL;
+
+       link->duplex = !!tb[SWITCH_LINK_FLAG_DUPLEX];
+       link->aneg = !!tb[SWITCH_LINK_FLAG_ANEG];
+       link->speed = nla_get_u32(tb[SWITCH_LINK_SPEED]);
+
+       return 0;
+}
+
 static int
 swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
 {
@@ -640,6 +645,9 @@ swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
        struct switch_val val;
        int err = -EINVAL;
 
+       if (!capable(CAP_NET_ADMIN))
+               return -EPERM;
+
        dev = swconfig_get_dev(info);
        if (!dev)
                return -EINVAL;
@@ -650,7 +658,7 @@ swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
                goto error;
 
        val.attr = attr;
-       switch(attr->type) {
+       switch (attr->type) {
        case SWITCH_TYPE_NOVAL:
                break;
        case SWITCH_TYPE_INT:
@@ -673,7 +681,23 @@ swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
                /* TODO: implement multipart? */
                if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
                        err = swconfig_parse_ports(skb,
-                               info->attrs[SWITCH_ATTR_OP_VALUE_PORTS], &val, dev->ports);
+                               info->attrs[SWITCH_ATTR_OP_VALUE_PORTS],
+                               &val, dev->ports);
+                       if (err < 0)
+                               goto error;
+               } else {
+                       val.len = 0;
+                       err = 0;
+               }
+               break;
+       case SWITCH_TYPE_LINK:
+               val.value.link = &dev->linkbuf;
+               memset(&dev->linkbuf, 0, sizeof(struct switch_port_link));
+
+               if (info->attrs[SWITCH_ATTR_OP_VALUE_LINK]) {
+                       err = swconfig_parse_link(skb,
+                                                 info->attrs[SWITCH_ATTR_OP_VALUE_LINK],
+                                                 val.value.link);
                        if (err < 0)
                                goto error;
                } else {
@@ -764,6 +788,53 @@ done:
        return err;
 }
 
+static int
+swconfig_send_link(struct sk_buff *msg, struct genl_info *info, int attr,
+                  const struct switch_port_link *link)
+{
+       struct nlattr *p = NULL;
+       int err = 0;
+
+       p = nla_nest_start(msg, attr);
+       if (link->link) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_LINK))
+                       goto nla_put_failure;
+       }
+       if (link->duplex) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_DUPLEX))
+                       goto nla_put_failure;
+       }
+       if (link->aneg) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_ANEG))
+                       goto nla_put_failure;
+       }
+       if (link->tx_flow) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_TX_FLOW))
+                       goto nla_put_failure;
+       }
+       if (link->rx_flow) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_RX_FLOW))
+                       goto nla_put_failure;
+       }
+       if (nla_put_u32(msg, SWITCH_LINK_SPEED, link->speed))
+               goto nla_put_failure;
+       if (link->eee & ADVERTISED_100baseT_Full) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_EEE_100BASET))
+                       goto nla_put_failure;
+       }
+       if (link->eee & ADVERTISED_1000baseT_Full) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_EEE_1000BASET))
+                       goto nla_put_failure;
+       }
+       nla_nest_end(msg, p);
+
+       return err;
+
+nla_put_failure:
+       nla_nest_cancel(msg, p);
+       return -1;
+}
+
 static int
 swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
 {
@@ -788,6 +859,9 @@ swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
                val.value.ports = dev->portbuf;
                memset(dev->portbuf, 0,
                        sizeof(struct switch_port) * dev->ports);
+       } else if (attr->type == SWITCH_TYPE_LINK) {
+               val.value.link = &dev->linkbuf;
+               memset(&dev->linkbuf, 0, sizeof(struct switch_port_link));
        }
 
        err = attr->get(dev, attr, &val);
@@ -803,7 +877,7 @@ swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
        if (IS_ERR(hdr))
                goto nla_put_failure;
 
-       switch(attr->type) {
+       switch (attr->type) {
        case SWITCH_TYPE_INT:
                if (nla_put_u32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i))
                        goto nla_put_failure;
@@ -818,12 +892,19 @@ swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
                if (err < 0)
                        goto nla_put_failure;
                break;
+       case SWITCH_TYPE_LINK:
+               err = swconfig_send_link(msg, info,
+                                        SWITCH_ATTR_OP_VALUE_LINK, val.value.link);
+               if (err < 0)
+                       goto nla_put_failure;
+               break;
        default:
-               DPRINTF("invalid type in attribute\n");
+               pr_debug("invalid type in attribute\n");
                err = -EINVAL;
-               goto error;
+               goto nla_put_failure;
        }
-       err = genlmsg_end(msg, hdr);
+       genlmsg_end(msg, hdr);
+       err = msg->len;
        if (err < 0)
                goto nla_put_failure;
 
@@ -844,7 +925,9 @@ static int
 swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
                const struct switch_dev *dev)
 {
+       struct nlattr *p = NULL, *m = NULL;
        void *hdr;
+       int i;
 
        hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
                        SWITCH_CMD_NEW_ATTR);
@@ -866,7 +949,26 @@ swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
        if (nla_put_u32(msg, SWITCH_ATTR_CPU_PORT, dev->cpu_port))
                goto nla_put_failure;
 
-       return genlmsg_end(msg, hdr);
+       m = nla_nest_start(msg, SWITCH_ATTR_PORTMAP);
+       if (!m)
+               goto nla_put_failure;
+       for (i = 0; i < dev->ports; i++) {
+               p = nla_nest_start(msg, SWITCH_ATTR_PORTS);
+               if (!p)
+                       continue;
+               if (dev->portmap[i].s) {
+                       if (nla_put_string(msg, SWITCH_PORTMAP_SEGMENT,
+                                               dev->portmap[i].s))
+                               goto nla_put_failure;
+                       if (nla_put_u32(msg, SWITCH_PORTMAP_VIRT,
+                                               dev->portmap[i].virt))
+                               goto nla_put_failure;
+               }
+               nla_nest_end(msg, p);
+       }
+       nla_nest_end(msg, m);
+       genlmsg_end(msg, hdr);
+       return msg->len;
 nla_put_failure:
        genlmsg_cancel(msg, hdr);
        return -EMSGSIZE;
@@ -903,57 +1005,171 @@ swconfig_done(struct netlink_callback *cb)
 static struct genl_ops swconfig_ops[] = {
        {
                .cmd = SWITCH_CMD_LIST_GLOBAL,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+#endif
                .doit = swconfig_list_attrs,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
                .policy = switch_policy,
+#endif
        },
        {
                .cmd = SWITCH_CMD_LIST_VLAN,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+#endif
                .doit = swconfig_list_attrs,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
                .policy = switch_policy,
+#endif
        },
        {
                .cmd = SWITCH_CMD_LIST_PORT,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+#endif
                .doit = swconfig_list_attrs,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
                .policy = switch_policy,
+#endif
        },
        {
                .cmd = SWITCH_CMD_GET_GLOBAL,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+#endif
                .doit = swconfig_get_attr,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
                .policy = switch_policy,
+#endif
        },
        {
                .cmd = SWITCH_CMD_GET_VLAN,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+#endif
                .doit = swconfig_get_attr,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
                .policy = switch_policy,
+#endif
        },
        {
                .cmd = SWITCH_CMD_GET_PORT,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+#endif
                .doit = swconfig_get_attr,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
                .policy = switch_policy,
+#endif
        },
        {
                .cmd = SWITCH_CMD_SET_GLOBAL,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+#endif
+               .flags = GENL_ADMIN_PERM,
                .doit = swconfig_set_attr,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
                .policy = switch_policy,
+#endif
        },
        {
                .cmd = SWITCH_CMD_SET_VLAN,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+#endif
+               .flags = GENL_ADMIN_PERM,
                .doit = swconfig_set_attr,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
                .policy = switch_policy,
+#endif
        },
        {
                .cmd = SWITCH_CMD_SET_PORT,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+#endif
+               .flags = GENL_ADMIN_PERM,
                .doit = swconfig_set_attr,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
                .policy = switch_policy,
+#endif
        },
        {
                .cmd = SWITCH_CMD_GET_SWITCH,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+#endif
                .dumpit = swconfig_dump_switches,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
                .policy = switch_policy,
+#endif
                .done = swconfig_done,
        }
 };
 
+static struct genl_family switch_fam = {
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4,10,0)
+       .id = GENL_ID_GENERATE,
+#endif
+       .name = "switch",
+       .hdrsize = 0,
+       .version = 1,
+       .maxattr = SWITCH_ATTR_MAX,
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
+       .policy = switch_policy,
+#endif
+       .module = THIS_MODULE,
+       .ops = swconfig_ops,
+       .n_ops = ARRAY_SIZE(swconfig_ops),
+};
+
+#ifdef CONFIG_OF
+void
+of_switch_load_portmap(struct switch_dev *dev)
+{
+       struct device_node *port;
+
+       if (!dev->of_node)
+               return;
+
+       for_each_child_of_node(dev->of_node, port) {
+               const __be32 *prop;
+               const char *segment;
+               int size, phys;
+
+               if (!of_device_is_compatible(port, "swconfig,port"))
+                       continue;
+
+               if (of_property_read_string(port, "swconfig,segment", &segment))
+                       continue;
+
+               prop = of_get_property(port, "swconfig,portmap", &size);
+               if (!prop)
+                       continue;
+
+               if (size != (2 * sizeof(*prop))) {
+                       pr_err("%s: failed to parse port mapping\n",
+                                       port->name);
+                       continue;
+               }
+
+               phys = be32_to_cpup(prop++);
+               if ((phys < 0) | (phys >= dev->ports)) {
+                       pr_err("%s: physical port index out of range\n",
+                                       port->name);
+                       continue;
+               }
+
+               dev->portmap[phys].s = kstrdup(segment, GFP_KERNEL);
+               dev->portmap[phys].virt = be32_to_cpup(prop);
+               pr_debug("Found port: %s, physical: %d, virtual: %d\n",
+                       segment, phys, dev->portmap[phys].virt);
+       }
+}
+#endif
+
 int
 register_switch(struct switch_dev *dev, struct net_device *netdev)
 {
@@ -971,11 +1187,22 @@ register_switch(struct switch_dev *dev, struct net_device *netdev)
        }
        BUG_ON(!dev->alias);
 
+       /* Make sure swdev_id doesn't overflow */
+       if (swdev_id == INT_MAX) {
+               return -ENOMEM;
+       }
+
        if (dev->ports > 0) {
-               dev->portbuf = kzalloc(sizeof(struct switch_port) * dev->ports,
-                               GFP_KERNEL);
+               dev->portbuf = kzalloc(sizeof(struct switch_port) *
+                               dev->ports, GFP_KERNEL);
                if (!dev->portbuf)
                        return -ENOMEM;
+               dev->portmap = kzalloc(sizeof(struct switch_portmap) *
+                               dev->ports, GFP_KERNEL);
+               if (!dev->portmap) {
+                       kfree(dev->portbuf);
+                       return -ENOMEM;
+               }
        }
        swconfig_defaults_init(dev);
        mutex_init(&dev->sw_mutex);
@@ -997,10 +1224,15 @@ register_switch(struct switch_dev *dev, struct net_device *netdev)
                return -ENFILE;
        }
 
+#ifdef CONFIG_OF
+       if (dev->ports)
+               of_switch_load_portmap(dev);
+#endif
+
        /* fill device name */
        snprintf(dev->devname, IFNAMSIZ, SWCONFIG_DEVNAME, i);
 
-       list_add(&dev->dev_list, &swdevs);
+       list_add_tail(&dev->dev_list, &swdevs);
        swconfig_unlock();
 
        err = swconfig_create_led_trigger(dev);
@@ -1024,28 +1256,52 @@ unregister_switch(struct switch_dev *dev)
 }
 EXPORT_SYMBOL_GPL(unregister_switch);
 
-
-static int __init
-swconfig_init(void)
+int
+switch_generic_set_link(struct switch_dev *dev, int port,
+                       struct switch_port_link *link)
 {
-       int i, err;
+       if (WARN_ON(!dev->ops->phy_write16))
+               return -ENOTSUPP;
 
-       INIT_LIST_HEAD(&swdevs);
-       err = genl_register_family(&switch_fam);
-       if (err)
-               return err;
+       /* Generic implementation */
+       if (link->aneg) {
+               dev->ops->phy_write16(dev, port, MII_BMCR, 0x0000);
+               dev->ops->phy_write16(dev, port, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
+       } else {
+               u16 bmcr = 0;
 
-       for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
-               err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
-               if (err)
-                       goto unregister;
+               if (link->duplex)
+                       bmcr |= BMCR_FULLDPLX;
+
+               switch (link->speed) {
+               case SWITCH_PORT_SPEED_10:
+                       break;
+               case SWITCH_PORT_SPEED_100:
+                       bmcr |= BMCR_SPEED100;
+                       break;
+               case SWITCH_PORT_SPEED_1000:
+                       bmcr |= BMCR_SPEED1000;
+                       break;
+               default:
+                       return -ENOTSUPP;
+               }
+
+               dev->ops->phy_write16(dev, port, MII_BMCR, bmcr);
        }
 
        return 0;
+}
 
-unregister:
-       genl_unregister_family(&switch_fam);
-       return err;
+static int __init
+swconfig_init(void)
+{
+       INIT_LIST_HEAD(&swdevs);
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4,10,0)
+       return genl_register_family_with_ops(&switch_fam, swconfig_ops);
+#else
+       return genl_register_family(&switch_fam);
+#endif
 }
 
 static void __exit
@@ -1056,4 +1312,3 @@ swconfig_exit(void)
 
 module_init(swconfig_init);
 module_exit(swconfig_exit);
-