2 * swconfig.c: Switch configuration API
4 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/types.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/list.h>
22 #include <linux/if_ether.h>
23 #include <linux/capability.h>
24 #include <linux/skbuff.h>
25 #include <linux/switch.h>
27 #include <linux/version.h>
29 #define SWCONFIG_DEVNAME "switch%d"
31 #include "swconfig_leds.c"
33 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
34 MODULE_LICENSE("GPL");
37 static struct list_head swdevs;
38 static DEFINE_SPINLOCK(swdevs_lock);
39 struct swconfig_callback;
41 struct swconfig_callback {
43 struct genlmsghdr *hdr;
44 struct genl_info *info;
47 /* callback for filling in the message data */
48 int (*fill)(struct swconfig_callback *cb, void *arg);
50 /* callback for closing the message before sending it */
51 int (*close)(struct swconfig_callback *cb, void *arg);
53 struct nlattr *nest[4];
60 swconfig_get_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
61 struct switch_val *val)
64 if (val->port_vlan >= dev->vlans)
67 if (!dev->ops->get_vlan_ports)
70 ret = dev->ops->get_vlan_ports(dev, val);
75 swconfig_set_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
76 struct switch_val *val)
78 struct switch_port *ports = val->value.ports;
79 const struct switch_dev_ops *ops = dev->ops;
82 if (val->port_vlan >= dev->vlans)
86 if (val->len > dev->ports)
89 if (!ops->set_vlan_ports)
92 for (i = 0; i < val->len; i++) {
93 if (ports[i].id >= dev->ports)
96 if (ops->set_port_pvid &&
97 !(ports[i].flags & (1 << SWITCH_PORT_FLAG_TAGGED)))
98 ops->set_port_pvid(dev, ports[i].id, val->port_vlan);
101 return ops->set_vlan_ports(dev, val);
105 swconfig_set_pvid(struct switch_dev *dev, const struct switch_attr *attr,
106 struct switch_val *val)
108 if (val->port_vlan >= dev->ports)
111 if (!dev->ops->set_port_pvid)
114 return dev->ops->set_port_pvid(dev, val->port_vlan, val->value.i);
118 swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr,
119 struct switch_val *val)
121 if (val->port_vlan >= dev->ports)
124 if (!dev->ops->get_port_pvid)
127 return dev->ops->get_port_pvid(dev, val->port_vlan, &val->value.i);
131 swconfig_speed_str(enum switch_port_speed speed)
134 case SWITCH_PORT_SPEED_10:
136 case SWITCH_PORT_SPEED_100:
138 case SWITCH_PORT_SPEED_1000:
148 swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr,
149 struct switch_val *val)
151 struct switch_port_link link;
155 if (val->port_vlan >= dev->ports)
158 if (!dev->ops->get_port_link)
161 memset(&link, 0, sizeof(link));
162 ret = dev->ops->get_port_link(dev, val->port_vlan, &link);
166 memset(dev->buf, 0, sizeof(dev->buf));
169 len = snprintf(dev->buf, sizeof(dev->buf),
170 "port:%d link:up speed:%s %s-duplex %s%s%s%s%s",
172 swconfig_speed_str(link.speed),
173 link.duplex ? "full" : "half",
174 link.tx_flow ? "txflow " : "",
175 link.rx_flow ? "rxflow " : "",
176 link.eee & ADVERTISED_100baseT_Full ? "eee100 " : "",
177 link.eee & ADVERTISED_1000baseT_Full ? "eee1000 " : "",
178 link.aneg ? "auto" : "");
180 len = snprintf(dev->buf, sizeof(dev->buf), "port:%d link:down",
183 val->value.s = dev->buf;
190 swconfig_apply_config(struct switch_dev *dev, const struct switch_attr *attr,
191 struct switch_val *val)
193 /* don't complain if not supported by the switch driver */
194 if (!dev->ops->apply_config)
197 return dev->ops->apply_config(dev);
201 swconfig_reset_switch(struct switch_dev *dev, const struct switch_attr *attr,
202 struct switch_val *val)
204 /* don't complain if not supported by the switch driver */
205 if (!dev->ops->reset_switch)
208 return dev->ops->reset_switch(dev);
211 enum global_defaults {
225 static struct switch_attr default_global[] = {
227 .type = SWITCH_TYPE_NOVAL,
229 .description = "Activate changes in the hardware",
230 .set = swconfig_apply_config,
233 .type = SWITCH_TYPE_NOVAL,
235 .description = "Reset the switch",
236 .set = swconfig_reset_switch,
240 static struct switch_attr default_port[] = {
242 .type = SWITCH_TYPE_INT,
244 .description = "Primary VLAN ID",
245 .set = swconfig_set_pvid,
246 .get = swconfig_get_pvid,
249 .type = SWITCH_TYPE_STRING,
251 .description = "Get port link information",
253 .get = swconfig_get_link,
257 static struct switch_attr default_vlan[] = {
259 .type = SWITCH_TYPE_PORTS,
261 .description = "VLAN port mapping",
262 .set = swconfig_set_vlan_ports,
263 .get = swconfig_get_vlan_ports,
267 static const struct switch_attr *
268 swconfig_find_attr_by_name(const struct switch_attrlist *alist,
273 for (i = 0; i < alist->n_attr; i++)
274 if (strcmp(name, alist->attr[i].name) == 0)
275 return &alist->attr[i];
280 static void swconfig_defaults_init(struct switch_dev *dev)
282 const struct switch_dev_ops *ops = dev->ops;
288 if (ops->get_vlan_ports || ops->set_vlan_ports)
289 set_bit(VLAN_PORTS, &dev->def_vlan);
291 if (ops->get_port_pvid || ops->set_port_pvid)
292 set_bit(PORT_PVID, &dev->def_port);
294 if (ops->get_port_link &&
295 !swconfig_find_attr_by_name(&ops->attr_port, "link"))
296 set_bit(PORT_LINK, &dev->def_port);
298 /* always present, can be no-op */
299 set_bit(GLOBAL_APPLY, &dev->def_global);
300 set_bit(GLOBAL_RESET, &dev->def_global);
304 static struct genl_family switch_fam = {
305 .id = GENL_ID_GENERATE,
309 .maxattr = SWITCH_ATTR_MAX,
312 static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
313 [SWITCH_ATTR_ID] = { .type = NLA_U32 },
314 [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 },
315 [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 },
316 [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 },
317 [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 },
318 [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING },
319 [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED },
320 [SWITCH_ATTR_TYPE] = { .type = NLA_U32 },
323 static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
324 [SWITCH_PORT_ID] = { .type = NLA_U32 },
325 [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
331 spin_lock(&swdevs_lock);
335 swconfig_unlock(void)
337 spin_unlock(&swdevs_lock);
340 static struct switch_dev *
341 swconfig_get_dev(struct genl_info *info)
343 struct switch_dev *dev = NULL;
344 struct switch_dev *p;
347 if (!info->attrs[SWITCH_ATTR_ID])
350 id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]);
352 list_for_each_entry(p, &swdevs, dev_list) {
360 mutex_lock(&dev->sw_mutex);
362 pr_debug("device %d not found\n", id);
369 swconfig_put_dev(struct switch_dev *dev)
371 mutex_unlock(&dev->sw_mutex);
375 swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
377 struct switch_attr *op = arg;
378 struct genl_info *info = cb->info;
379 struct sk_buff *msg = cb->msg;
380 int id = cb->args[0];
383 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam,
384 NLM_F_MULTI, SWITCH_CMD_NEW_ATTR);
388 if (nla_put_u32(msg, SWITCH_ATTR_OP_ID, id))
389 goto nla_put_failure;
390 if (nla_put_u32(msg, SWITCH_ATTR_OP_TYPE, op->type))
391 goto nla_put_failure;
392 if (nla_put_string(msg, SWITCH_ATTR_OP_NAME, op->name))
393 goto nla_put_failure;
395 if (nla_put_string(msg, SWITCH_ATTR_OP_DESCRIPTION,
397 goto nla_put_failure;
399 genlmsg_end(msg, hdr);
402 genlmsg_cancel(msg, hdr);
406 /* spread multipart messages across multiple message buffers */
408 swconfig_send_multipart(struct swconfig_callback *cb, void *arg)
410 struct genl_info *info = cb->info;
416 cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
421 if (!(cb->fill(cb, arg) < 0))
424 /* fill failed, check if this was already the second attempt */
428 /* try again in a new message, send the current one */
431 if (cb->close(cb, arg) < 0)
434 err = genlmsg_reply(cb->msg, info);
450 swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
452 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
453 const struct switch_attrlist *alist;
454 struct switch_dev *dev;
455 struct swconfig_callback cb;
460 struct switch_attr *def_list;
461 unsigned long *def_active;
464 dev = swconfig_get_dev(info);
469 case SWITCH_CMD_LIST_GLOBAL:
470 alist = &dev->ops->attr_global;
471 def_list = default_global;
472 def_active = &dev->def_global;
473 n_def = ARRAY_SIZE(default_global);
475 case SWITCH_CMD_LIST_VLAN:
476 alist = &dev->ops->attr_vlan;
477 def_list = default_vlan;
478 def_active = &dev->def_vlan;
479 n_def = ARRAY_SIZE(default_vlan);
481 case SWITCH_CMD_LIST_PORT:
482 alist = &dev->ops->attr_port;
483 def_list = default_port;
484 def_active = &dev->def_port;
485 n_def = ARRAY_SIZE(default_port);
492 memset(&cb, 0, sizeof(cb));
494 cb.fill = swconfig_dump_attr;
495 for (i = 0; i < alist->n_attr; i++) {
496 if (alist->attr[i].disabled)
499 err = swconfig_send_multipart(&cb, (void *) &alist->attr[i]);
505 for (i = 0; i < n_def; i++) {
506 if (!test_bit(i, def_active))
508 cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i;
509 err = swconfig_send_multipart(&cb, (void *) &def_list[i]);
513 swconfig_put_dev(dev);
518 return genlmsg_reply(cb.msg, info);
524 swconfig_put_dev(dev);
528 static const struct switch_attr *
529 swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
530 struct switch_val *val)
532 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
533 const struct switch_attrlist *alist;
534 const struct switch_attr *attr = NULL;
538 struct switch_attr *def_list;
539 unsigned long *def_active;
542 if (!info->attrs[SWITCH_ATTR_OP_ID])
546 case SWITCH_CMD_SET_GLOBAL:
547 case SWITCH_CMD_GET_GLOBAL:
548 alist = &dev->ops->attr_global;
549 def_list = default_global;
550 def_active = &dev->def_global;
551 n_def = ARRAY_SIZE(default_global);
553 case SWITCH_CMD_SET_VLAN:
554 case SWITCH_CMD_GET_VLAN:
555 alist = &dev->ops->attr_vlan;
556 def_list = default_vlan;
557 def_active = &dev->def_vlan;
558 n_def = ARRAY_SIZE(default_vlan);
559 if (!info->attrs[SWITCH_ATTR_OP_VLAN])
561 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]);
562 if (val->port_vlan >= dev->vlans)
565 case SWITCH_CMD_SET_PORT:
566 case SWITCH_CMD_GET_PORT:
567 alist = &dev->ops->attr_port;
568 def_list = default_port;
569 def_active = &dev->def_port;
570 n_def = ARRAY_SIZE(default_port);
571 if (!info->attrs[SWITCH_ATTR_OP_PORT])
573 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
574 if (val->port_vlan >= dev->ports)
585 attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
586 if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
587 attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
588 if (attr_id >= n_def)
590 if (!test_bit(attr_id, def_active))
592 attr = &def_list[attr_id];
594 if (attr_id >= alist->n_attr)
596 attr = &alist->attr[attr_id];
604 pr_debug("attribute lookup failed\n");
610 swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
611 struct switch_val *val, int max)
617 nla_for_each_nested(nla, head, rem) {
618 struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
619 struct switch_port *port = &val->value.ports[val->len];
624 if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
628 if (!tb[SWITCH_PORT_ID])
631 port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
632 if (tb[SWITCH_PORT_FLAG_TAGGED])
633 port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
641 swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
643 const struct switch_attr *attr;
644 struct switch_dev *dev;
645 struct switch_val val;
648 dev = swconfig_get_dev(info);
652 memset(&val, 0, sizeof(val));
653 attr = swconfig_lookup_attr(dev, info, &val);
654 if (!attr || !attr->set)
658 switch (attr->type) {
659 case SWITCH_TYPE_NOVAL:
661 case SWITCH_TYPE_INT:
662 if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
665 nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
667 case SWITCH_TYPE_STRING:
668 if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
671 nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
673 case SWITCH_TYPE_PORTS:
674 val.value.ports = dev->portbuf;
675 memset(dev->portbuf, 0,
676 sizeof(struct switch_port) * dev->ports);
678 /* TODO: implement multipart? */
679 if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
680 err = swconfig_parse_ports(skb,
681 info->attrs[SWITCH_ATTR_OP_VALUE_PORTS],
694 err = attr->set(dev, attr, &val);
696 swconfig_put_dev(dev);
701 swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
704 nla_nest_end(cb->msg, cb->nest[0]);
709 swconfig_send_port(struct swconfig_callback *cb, void *arg)
711 const struct switch_port *port = arg;
712 struct nlattr *p = NULL;
715 cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
720 p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
724 if (nla_put_u32(cb->msg, SWITCH_PORT_ID, port->id))
725 goto nla_put_failure;
726 if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
727 if (nla_put_flag(cb->msg, SWITCH_PORT_FLAG_TAGGED))
728 goto nla_put_failure;
731 nla_nest_end(cb->msg, p);
735 nla_nest_cancel(cb->msg, p);
737 nla_nest_cancel(cb->msg, cb->nest[0]);
742 swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
743 const struct switch_val *val)
745 struct swconfig_callback cb;
749 if (!val->value.ports)
752 memset(&cb, 0, sizeof(cb));
756 cb.fill = swconfig_send_port;
757 cb.close = swconfig_close_portlist;
759 cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
760 for (i = 0; i < val->len; i++) {
761 err = swconfig_send_multipart(&cb, &val->value.ports[i]);
766 swconfig_close_portlist(&cb, NULL);
774 swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
776 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
777 const struct switch_attr *attr;
778 struct switch_dev *dev;
779 struct sk_buff *msg = NULL;
780 struct switch_val val;
784 dev = swconfig_get_dev(info);
788 memset(&val, 0, sizeof(val));
789 attr = swconfig_lookup_attr(dev, info, &val);
790 if (!attr || !attr->get)
793 if (attr->type == SWITCH_TYPE_PORTS) {
794 val.value.ports = dev->portbuf;
795 memset(dev->portbuf, 0,
796 sizeof(struct switch_port) * dev->ports);
799 err = attr->get(dev, attr, &val);
803 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
807 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam,
810 goto nla_put_failure;
812 switch (attr->type) {
813 case SWITCH_TYPE_INT:
814 if (nla_put_u32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i))
815 goto nla_put_failure;
817 case SWITCH_TYPE_STRING:
818 if (nla_put_string(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s))
819 goto nla_put_failure;
821 case SWITCH_TYPE_PORTS:
822 err = swconfig_send_ports(&msg, info,
823 SWITCH_ATTR_OP_VALUE_PORTS, &val);
825 goto nla_put_failure;
828 pr_debug("invalid type in attribute\n");
832 genlmsg_end(msg, hdr);
835 goto nla_put_failure;
837 swconfig_put_dev(dev);
838 return genlmsg_reply(msg, info);
844 swconfig_put_dev(dev);
851 swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
852 const struct switch_dev *dev)
854 struct nlattr *p = NULL, *m = NULL;
858 hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
859 SWITCH_CMD_NEW_ATTR);
863 if (nla_put_u32(msg, SWITCH_ATTR_ID, dev->id))
864 goto nla_put_failure;
865 if (nla_put_string(msg, SWITCH_ATTR_DEV_NAME, dev->devname))
866 goto nla_put_failure;
867 if (nla_put_string(msg, SWITCH_ATTR_ALIAS, dev->alias))
868 goto nla_put_failure;
869 if (nla_put_string(msg, SWITCH_ATTR_NAME, dev->name))
870 goto nla_put_failure;
871 if (nla_put_u32(msg, SWITCH_ATTR_VLANS, dev->vlans))
872 goto nla_put_failure;
873 if (nla_put_u32(msg, SWITCH_ATTR_PORTS, dev->ports))
874 goto nla_put_failure;
875 if (nla_put_u32(msg, SWITCH_ATTR_CPU_PORT, dev->cpu_port))
876 goto nla_put_failure;
878 m = nla_nest_start(msg, SWITCH_ATTR_PORTMAP);
880 goto nla_put_failure;
881 for (i = 0; i < dev->ports; i++) {
882 p = nla_nest_start(msg, SWITCH_ATTR_PORTS);
885 if (dev->portmap[i].s) {
886 if (nla_put_string(msg, SWITCH_PORTMAP_SEGMENT,
888 goto nla_put_failure;
889 if (nla_put_u32(msg, SWITCH_PORTMAP_VIRT,
890 dev->portmap[i].virt))
891 goto nla_put_failure;
893 nla_nest_end(msg, p);
895 nla_nest_end(msg, m);
896 genlmsg_end(msg, hdr);
899 genlmsg_cancel(msg, hdr);
903 static int swconfig_dump_switches(struct sk_buff *skb,
904 struct netlink_callback *cb)
906 struct switch_dev *dev;
907 int start = cb->args[0];
911 list_for_each_entry(dev, &swdevs, dev_list) {
914 if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).portid,
915 cb->nlh->nlmsg_seq, NLM_F_MULTI,
926 swconfig_done(struct netlink_callback *cb)
931 static struct genl_ops swconfig_ops[] = {
933 .cmd = SWITCH_CMD_LIST_GLOBAL,
934 .doit = swconfig_list_attrs,
935 .policy = switch_policy,
938 .cmd = SWITCH_CMD_LIST_VLAN,
939 .doit = swconfig_list_attrs,
940 .policy = switch_policy,
943 .cmd = SWITCH_CMD_LIST_PORT,
944 .doit = swconfig_list_attrs,
945 .policy = switch_policy,
948 .cmd = SWITCH_CMD_GET_GLOBAL,
949 .doit = swconfig_get_attr,
950 .policy = switch_policy,
953 .cmd = SWITCH_CMD_GET_VLAN,
954 .doit = swconfig_get_attr,
955 .policy = switch_policy,
958 .cmd = SWITCH_CMD_GET_PORT,
959 .doit = swconfig_get_attr,
960 .policy = switch_policy,
963 .cmd = SWITCH_CMD_SET_GLOBAL,
964 .doit = swconfig_set_attr,
965 .policy = switch_policy,
968 .cmd = SWITCH_CMD_SET_VLAN,
969 .doit = swconfig_set_attr,
970 .policy = switch_policy,
973 .cmd = SWITCH_CMD_SET_PORT,
974 .doit = swconfig_set_attr,
975 .policy = switch_policy,
978 .cmd = SWITCH_CMD_GET_SWITCH,
979 .dumpit = swconfig_dump_switches,
980 .policy = switch_policy,
981 .done = swconfig_done,
987 of_switch_load_portmap(struct switch_dev *dev)
989 struct device_node *port;
994 for_each_child_of_node(dev->of_node, port) {
999 if (!of_device_is_compatible(port, "swconfig,port"))
1002 if (of_property_read_string(port, "swconfig,segment", &segment))
1005 prop = of_get_property(port, "swconfig,portmap", &size);
1009 if (size != (2 * sizeof(*prop))) {
1010 pr_err("%s: failed to parse port mapping\n",
1015 phys = be32_to_cpup(prop++);
1016 if ((phys < 0) | (phys >= dev->ports)) {
1017 pr_err("%s: physical port index out of range\n",
1022 dev->portmap[phys].s = kstrdup(segment, GFP_KERNEL);
1023 dev->portmap[phys].virt = be32_to_cpup(prop);
1024 pr_debug("Found port: %s, physical: %d, virtual: %d\n",
1025 segment, phys, dev->portmap[phys].virt);
1031 register_switch(struct switch_dev *dev, struct net_device *netdev)
1033 struct switch_dev *sdev;
1034 const int max_switches = 8 * sizeof(unsigned long);
1035 unsigned long in_use = 0;
1039 INIT_LIST_HEAD(&dev->dev_list);
1041 dev->netdev = netdev;
1043 dev->alias = netdev->name;
1045 BUG_ON(!dev->alias);
1047 if (dev->ports > 0) {
1048 dev->portbuf = kzalloc(sizeof(struct switch_port) *
1049 dev->ports, GFP_KERNEL);
1052 dev->portmap = kzalloc(sizeof(struct switch_portmap) *
1053 dev->ports, GFP_KERNEL);
1054 if (!dev->portmap) {
1055 kfree(dev->portbuf);
1059 swconfig_defaults_init(dev);
1060 mutex_init(&dev->sw_mutex);
1062 dev->id = ++swdev_id;
1064 list_for_each_entry(sdev, &swdevs, dev_list) {
1065 if (!sscanf(sdev->devname, SWCONFIG_DEVNAME, &i))
1067 if (i < 0 || i > max_switches)
1070 set_bit(i, &in_use);
1072 i = find_first_zero_bit(&in_use, max_switches);
1074 if (i == max_switches) {
1081 of_switch_load_portmap(dev);
1084 /* fill device name */
1085 snprintf(dev->devname, IFNAMSIZ, SWCONFIG_DEVNAME, i);
1087 list_add_tail(&dev->dev_list, &swdevs);
1090 err = swconfig_create_led_trigger(dev);
1096 EXPORT_SYMBOL_GPL(register_switch);
1099 unregister_switch(struct switch_dev *dev)
1101 swconfig_destroy_led_trigger(dev);
1102 kfree(dev->portbuf);
1103 mutex_lock(&dev->sw_mutex);
1105 list_del(&dev->dev_list);
1107 mutex_unlock(&dev->sw_mutex);
1109 EXPORT_SYMBOL_GPL(unregister_switch);
1116 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0))
1120 INIT_LIST_HEAD(&swdevs);
1122 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0))
1123 err = genl_register_family(&switch_fam);
1127 for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
1128 err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
1135 genl_unregister_family(&switch_fam);
1138 err = genl_register_family_with_ops(&switch_fam, swconfig_ops);
1148 genl_unregister_family(&switch_fam);
1151 module_init(swconfig_init);
1152 module_exit(swconfig_exit);