modules: swconfig.c: update the error path on 3.13, and behave nicely with return
[openwrt/openwrt.git] / target / linux / generic / files / drivers / net / phy / swconfig.c
index 34f43bfd9b93ed504fed087af1dec5033761efd3..c043ee46494eb566e945f7b37fb6900626cdb90f 100644 (file)
 #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>
 
 #define SWCONFIG_DEVNAME       "switch%d"
 
 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
 MODULE_LICENSE("GPL");
 
-static int swdev_id = 0;
+static int swdev_id;
 static struct list_head swdevs;
 static DEFINE_SPINLOCK(swdevs_lock);
 struct swconfig_callback;
 
-struct swconfig_callback
-{
+struct swconfig_callback {
        struct sk_buff *msg;
        struct genlmsghdr *hdr;
        struct genl_info *info;
@@ -63,7 +57,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 +72,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 +102,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 +115,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;
@@ -147,7 +145,8 @@ swconfig_speed_str(enum switch_port_speed speed)
 }
 
 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;
@@ -172,7 +171,7 @@ swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr, struct
                               val->port_vlan,
                               swconfig_speed_str(link.speed),
                               link.duplex ? "full" : "half",
-                              link.tx_flow ? "txflow ": "",
+                              link.tx_flow ? "txflow " : "",
                               link.rx_flow ?   "rxflow " : "",
                               link.aneg ? "auto" : "");
        else
@@ -186,7 +185,8 @@ swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr, struct
 }
 
 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 +196,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)
@@ -262,7 +263,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;
 
@@ -355,7 +357,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;
@@ -460,7 +462,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;
@@ -537,7 +539,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 +598,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;
 }
@@ -650,7 +652,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 +675,8 @@ 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 {
@@ -803,7 +806,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;
@@ -819,7 +822,7 @@ swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
                        goto nla_put_failure;
                break;
        default:
-               DPRINTF("invalid type in attribute\n");
+               pr_debug("invalid type in attribute\n");
                err = -EINVAL;
                goto error;
        }
@@ -844,7 +847,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,6 +871,24 @@ 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;
 
+       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);
        return genlmsg_end(msg, hdr);
 nla_put_failure:
        genlmsg_cancel(msg, hdr);
@@ -954,6 +977,51 @@ static struct genl_ops 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)
 {
@@ -972,10 +1040,16 @@ register_switch(struct switch_dev *dev, struct net_device *netdev)
        BUG_ON(!dev->alias);
 
        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,6 +1071,11 @@ 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);
 
@@ -1031,6 +1110,8 @@ swconfig_init(void)
        int i, err;
 
        INIT_LIST_HEAD(&swdevs);
+       
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0))
        err = genl_register_family(&switch_fam);
        if (err)
                return err;
@@ -1040,7 +1121,11 @@ swconfig_init(void)
                if (err)
                        goto unregister;
        }
-
+#else
+       err = genl_register_family_with_ops(&switch_fam, swconfig_ops);
+       if (err)
+               return err;
+#endif
        return 0;
 
 unregister: