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>
28 #define SWCONFIG_DEVNAME "switch%d"
30 #include "swconfig_leds.c"
32 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
33 MODULE_LICENSE("GPL");
36 static struct list_head swdevs;
37 static DEFINE_SPINLOCK(swdevs_lock);
38 struct swconfig_callback;
40 struct swconfig_callback {
42 struct genlmsghdr *hdr;
43 struct genl_info *info;
46 /* callback for filling in the message data */
47 int (*fill)(struct swconfig_callback *cb, void *arg);
49 /* callback for closing the message before sending it */
50 int (*close)(struct swconfig_callback *cb, void *arg);
52 struct nlattr *nest[4];
59 swconfig_get_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
60 struct switch_val *val)
63 if (val->port_vlan >= dev->vlans)
66 if (!dev->ops->get_vlan_ports)
69 ret = dev->ops->get_vlan_ports(dev, val);
74 swconfig_set_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
75 struct switch_val *val)
77 struct switch_port *ports = val->value.ports;
78 const struct switch_dev_ops *ops = dev->ops;
81 if (val->port_vlan >= dev->vlans)
85 if (val->len > dev->ports)
88 if (!ops->set_vlan_ports)
91 for (i = 0; i < val->len; i++) {
92 if (ports[i].id >= dev->ports)
95 if (ops->set_port_pvid &&
96 !(ports[i].flags & (1 << SWITCH_PORT_FLAG_TAGGED)))
97 ops->set_port_pvid(dev, ports[i].id, val->port_vlan);
100 return ops->set_vlan_ports(dev, val);
104 swconfig_set_pvid(struct switch_dev *dev, const struct switch_attr *attr,
105 struct switch_val *val)
107 if (val->port_vlan >= dev->ports)
110 if (!dev->ops->set_port_pvid)
113 return dev->ops->set_port_pvid(dev, val->port_vlan, val->value.i);
117 swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr,
118 struct switch_val *val)
120 if (val->port_vlan >= dev->ports)
123 if (!dev->ops->get_port_pvid)
126 return dev->ops->get_port_pvid(dev, val->port_vlan, &val->value.i);
130 swconfig_speed_str(enum switch_port_speed speed)
133 case SWITCH_PORT_SPEED_10:
135 case SWITCH_PORT_SPEED_100:
137 case SWITCH_PORT_SPEED_1000:
147 swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr,
148 struct switch_val *val)
150 struct switch_port_link link;
154 if (val->port_vlan >= dev->ports)
157 if (!dev->ops->get_port_link)
160 memset(&link, 0, sizeof(link));
161 ret = dev->ops->get_port_link(dev, val->port_vlan, &link);
165 memset(dev->buf, 0, sizeof(dev->buf));
168 len = snprintf(dev->buf, sizeof(dev->buf),
169 "port:%d link:up speed:%s %s-duplex %s%s%s",
171 swconfig_speed_str(link.speed),
172 link.duplex ? "full" : "half",
173 link.tx_flow ? "txflow " : "",
174 link.rx_flow ? "rxflow " : "",
175 link.aneg ? "auto" : "");
177 len = snprintf(dev->buf, sizeof(dev->buf), "port:%d link:down",
180 val->value.s = dev->buf;
187 swconfig_apply_config(struct switch_dev *dev, const struct switch_attr *attr,
188 struct switch_val *val)
190 /* don't complain if not supported by the switch driver */
191 if (!dev->ops->apply_config)
194 return dev->ops->apply_config(dev);
198 swconfig_reset_switch(struct switch_dev *dev, const struct switch_attr *attr,
199 struct switch_val *val)
201 /* don't complain if not supported by the switch driver */
202 if (!dev->ops->reset_switch)
205 return dev->ops->reset_switch(dev);
208 enum global_defaults {
222 static struct switch_attr default_global[] = {
224 .type = SWITCH_TYPE_NOVAL,
226 .description = "Activate changes in the hardware",
227 .set = swconfig_apply_config,
230 .type = SWITCH_TYPE_NOVAL,
232 .description = "Reset the switch",
233 .set = swconfig_reset_switch,
237 static struct switch_attr default_port[] = {
239 .type = SWITCH_TYPE_INT,
241 .description = "Primary VLAN ID",
242 .set = swconfig_set_pvid,
243 .get = swconfig_get_pvid,
246 .type = SWITCH_TYPE_STRING,
248 .description = "Get port link information",
250 .get = swconfig_get_link,
254 static struct switch_attr default_vlan[] = {
256 .type = SWITCH_TYPE_PORTS,
258 .description = "VLAN port mapping",
259 .set = swconfig_set_vlan_ports,
260 .get = swconfig_get_vlan_ports,
264 static const struct switch_attr *
265 swconfig_find_attr_by_name(const struct switch_attrlist *alist,
270 for (i = 0; i < alist->n_attr; i++)
271 if (strcmp(name, alist->attr[i].name) == 0)
272 return &alist->attr[i];
277 static void swconfig_defaults_init(struct switch_dev *dev)
279 const struct switch_dev_ops *ops = dev->ops;
285 if (ops->get_vlan_ports || ops->set_vlan_ports)
286 set_bit(VLAN_PORTS, &dev->def_vlan);
288 if (ops->get_port_pvid || ops->set_port_pvid)
289 set_bit(PORT_PVID, &dev->def_port);
291 if (ops->get_port_link &&
292 !swconfig_find_attr_by_name(&ops->attr_port, "link"))
293 set_bit(PORT_LINK, &dev->def_port);
295 /* always present, can be no-op */
296 set_bit(GLOBAL_APPLY, &dev->def_global);
297 set_bit(GLOBAL_RESET, &dev->def_global);
301 static struct genl_family switch_fam = {
302 .id = GENL_ID_GENERATE,
306 .maxattr = SWITCH_ATTR_MAX,
309 static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
310 [SWITCH_ATTR_ID] = { .type = NLA_U32 },
311 [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 },
312 [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 },
313 [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 },
314 [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 },
315 [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING },
316 [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED },
317 [SWITCH_ATTR_TYPE] = { .type = NLA_U32 },
320 static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
321 [SWITCH_PORT_ID] = { .type = NLA_U32 },
322 [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
328 spin_lock(&swdevs_lock);
332 swconfig_unlock(void)
334 spin_unlock(&swdevs_lock);
337 static struct switch_dev *
338 swconfig_get_dev(struct genl_info *info)
340 struct switch_dev *dev = NULL;
341 struct switch_dev *p;
344 if (!info->attrs[SWITCH_ATTR_ID])
347 id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]);
349 list_for_each_entry(p, &swdevs, dev_list) {
357 mutex_lock(&dev->sw_mutex);
359 pr_debug("device %d not found\n", id);
366 swconfig_put_dev(struct switch_dev *dev)
368 mutex_unlock(&dev->sw_mutex);
372 swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
374 struct switch_attr *op = arg;
375 struct genl_info *info = cb->info;
376 struct sk_buff *msg = cb->msg;
377 int id = cb->args[0];
380 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam,
381 NLM_F_MULTI, SWITCH_CMD_NEW_ATTR);
385 if (nla_put_u32(msg, SWITCH_ATTR_OP_ID, id))
386 goto nla_put_failure;
387 if (nla_put_u32(msg, SWITCH_ATTR_OP_TYPE, op->type))
388 goto nla_put_failure;
389 if (nla_put_string(msg, SWITCH_ATTR_OP_NAME, op->name))
390 goto nla_put_failure;
392 if (nla_put_string(msg, SWITCH_ATTR_OP_DESCRIPTION,
394 goto nla_put_failure;
396 return genlmsg_end(msg, hdr);
398 genlmsg_cancel(msg, hdr);
402 /* spread multipart messages across multiple message buffers */
404 swconfig_send_multipart(struct swconfig_callback *cb, void *arg)
406 struct genl_info *info = cb->info;
412 cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
417 if (!(cb->fill(cb, arg) < 0))
420 /* fill failed, check if this was already the second attempt */
424 /* try again in a new message, send the current one */
427 if (cb->close(cb, arg) < 0)
430 err = genlmsg_reply(cb->msg, info);
446 swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
448 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
449 const struct switch_attrlist *alist;
450 struct switch_dev *dev;
451 struct swconfig_callback cb;
456 struct switch_attr *def_list;
457 unsigned long *def_active;
460 dev = swconfig_get_dev(info);
465 case SWITCH_CMD_LIST_GLOBAL:
466 alist = &dev->ops->attr_global;
467 def_list = default_global;
468 def_active = &dev->def_global;
469 n_def = ARRAY_SIZE(default_global);
471 case SWITCH_CMD_LIST_VLAN:
472 alist = &dev->ops->attr_vlan;
473 def_list = default_vlan;
474 def_active = &dev->def_vlan;
475 n_def = ARRAY_SIZE(default_vlan);
477 case SWITCH_CMD_LIST_PORT:
478 alist = &dev->ops->attr_port;
479 def_list = default_port;
480 def_active = &dev->def_port;
481 n_def = ARRAY_SIZE(default_port);
488 memset(&cb, 0, sizeof(cb));
490 cb.fill = swconfig_dump_attr;
491 for (i = 0; i < alist->n_attr; i++) {
492 if (alist->attr[i].disabled)
495 err = swconfig_send_multipart(&cb, (void *) &alist->attr[i]);
501 for (i = 0; i < n_def; i++) {
502 if (!test_bit(i, def_active))
504 cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i;
505 err = swconfig_send_multipart(&cb, (void *) &def_list[i]);
509 swconfig_put_dev(dev);
514 return genlmsg_reply(cb.msg, info);
520 swconfig_put_dev(dev);
524 static const struct switch_attr *
525 swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
526 struct switch_val *val)
528 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
529 const struct switch_attrlist *alist;
530 const struct switch_attr *attr = NULL;
534 struct switch_attr *def_list;
535 unsigned long *def_active;
538 if (!info->attrs[SWITCH_ATTR_OP_ID])
542 case SWITCH_CMD_SET_GLOBAL:
543 case SWITCH_CMD_GET_GLOBAL:
544 alist = &dev->ops->attr_global;
545 def_list = default_global;
546 def_active = &dev->def_global;
547 n_def = ARRAY_SIZE(default_global);
549 case SWITCH_CMD_SET_VLAN:
550 case SWITCH_CMD_GET_VLAN:
551 alist = &dev->ops->attr_vlan;
552 def_list = default_vlan;
553 def_active = &dev->def_vlan;
554 n_def = ARRAY_SIZE(default_vlan);
555 if (!info->attrs[SWITCH_ATTR_OP_VLAN])
557 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]);
558 if (val->port_vlan >= dev->vlans)
561 case SWITCH_CMD_SET_PORT:
562 case SWITCH_CMD_GET_PORT:
563 alist = &dev->ops->attr_port;
564 def_list = default_port;
565 def_active = &dev->def_port;
566 n_def = ARRAY_SIZE(default_port);
567 if (!info->attrs[SWITCH_ATTR_OP_PORT])
569 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
570 if (val->port_vlan >= dev->ports)
581 attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
582 if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
583 attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
584 if (attr_id >= n_def)
586 if (!test_bit(attr_id, def_active))
588 attr = &def_list[attr_id];
590 if (attr_id >= alist->n_attr)
592 attr = &alist->attr[attr_id];
600 pr_debug("attribute lookup failed\n");
606 swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
607 struct switch_val *val, int max)
613 nla_for_each_nested(nla, head, rem) {
614 struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
615 struct switch_port *port = &val->value.ports[val->len];
620 if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
624 if (!tb[SWITCH_PORT_ID])
627 port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
628 if (tb[SWITCH_PORT_FLAG_TAGGED])
629 port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
637 swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
639 const struct switch_attr *attr;
640 struct switch_dev *dev;
641 struct switch_val val;
644 dev = swconfig_get_dev(info);
648 memset(&val, 0, sizeof(val));
649 attr = swconfig_lookup_attr(dev, info, &val);
650 if (!attr || !attr->set)
654 switch (attr->type) {
655 case SWITCH_TYPE_NOVAL:
657 case SWITCH_TYPE_INT:
658 if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
661 nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
663 case SWITCH_TYPE_STRING:
664 if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
667 nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
669 case SWITCH_TYPE_PORTS:
670 val.value.ports = dev->portbuf;
671 memset(dev->portbuf, 0,
672 sizeof(struct switch_port) * dev->ports);
674 /* TODO: implement multipart? */
675 if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
676 err = swconfig_parse_ports(skb,
677 info->attrs[SWITCH_ATTR_OP_VALUE_PORTS],
690 err = attr->set(dev, attr, &val);
692 swconfig_put_dev(dev);
697 swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
700 nla_nest_end(cb->msg, cb->nest[0]);
705 swconfig_send_port(struct swconfig_callback *cb, void *arg)
707 const struct switch_port *port = arg;
708 struct nlattr *p = NULL;
711 cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
716 p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
720 if (nla_put_u32(cb->msg, SWITCH_PORT_ID, port->id))
721 goto nla_put_failure;
722 if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
723 if (nla_put_flag(cb->msg, SWITCH_PORT_FLAG_TAGGED))
724 goto nla_put_failure;
727 nla_nest_end(cb->msg, p);
731 nla_nest_cancel(cb->msg, p);
733 nla_nest_cancel(cb->msg, cb->nest[0]);
738 swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
739 const struct switch_val *val)
741 struct swconfig_callback cb;
745 if (!val->value.ports)
748 memset(&cb, 0, sizeof(cb));
752 cb.fill = swconfig_send_port;
753 cb.close = swconfig_close_portlist;
755 cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
756 for (i = 0; i < val->len; i++) {
757 err = swconfig_send_multipart(&cb, &val->value.ports[i]);
762 swconfig_close_portlist(&cb, NULL);
770 swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
772 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
773 const struct switch_attr *attr;
774 struct switch_dev *dev;
775 struct sk_buff *msg = NULL;
776 struct switch_val val;
780 dev = swconfig_get_dev(info);
784 memset(&val, 0, sizeof(val));
785 attr = swconfig_lookup_attr(dev, info, &val);
786 if (!attr || !attr->get)
789 if (attr->type == SWITCH_TYPE_PORTS) {
790 val.value.ports = dev->portbuf;
791 memset(dev->portbuf, 0,
792 sizeof(struct switch_port) * dev->ports);
795 err = attr->get(dev, attr, &val);
799 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
803 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam,
806 goto nla_put_failure;
808 switch (attr->type) {
809 case SWITCH_TYPE_INT:
810 if (nla_put_u32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i))
811 goto nla_put_failure;
813 case SWITCH_TYPE_STRING:
814 if (nla_put_string(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s))
815 goto nla_put_failure;
817 case SWITCH_TYPE_PORTS:
818 err = swconfig_send_ports(&msg, info,
819 SWITCH_ATTR_OP_VALUE_PORTS, &val);
821 goto nla_put_failure;
824 pr_debug("invalid type in attribute\n");
828 err = genlmsg_end(msg, hdr);
830 goto nla_put_failure;
832 swconfig_put_dev(dev);
833 return genlmsg_reply(msg, info);
839 swconfig_put_dev(dev);
846 swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
847 const struct switch_dev *dev)
849 struct nlattr *p = NULL, *m = NULL;
853 hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
854 SWITCH_CMD_NEW_ATTR);
858 if (nla_put_u32(msg, SWITCH_ATTR_ID, dev->id))
859 goto nla_put_failure;
860 if (nla_put_string(msg, SWITCH_ATTR_DEV_NAME, dev->devname))
861 goto nla_put_failure;
862 if (nla_put_string(msg, SWITCH_ATTR_ALIAS, dev->alias))
863 goto nla_put_failure;
864 if (nla_put_string(msg, SWITCH_ATTR_NAME, dev->name))
865 goto nla_put_failure;
866 if (nla_put_u32(msg, SWITCH_ATTR_VLANS, dev->vlans))
867 goto nla_put_failure;
868 if (nla_put_u32(msg, SWITCH_ATTR_PORTS, dev->ports))
869 goto nla_put_failure;
870 if (nla_put_u32(msg, SWITCH_ATTR_CPU_PORT, dev->cpu_port))
871 goto nla_put_failure;
873 m = nla_nest_start(msg, SWITCH_ATTR_PORTMAP);
875 goto nla_put_failure;
876 for (i = 0; i < dev->ports; i++) {
877 p = nla_nest_start(msg, SWITCH_ATTR_PORTS);
880 if (dev->portmap[i].s) {
881 if (nla_put_string(msg, SWITCH_PORTMAP_SEGMENT,
883 goto nla_put_failure;
884 if (nla_put_u32(msg, SWITCH_PORTMAP_VIRT,
885 dev->portmap[i].virt))
886 goto nla_put_failure;
888 nla_nest_end(msg, p);
890 nla_nest_end(msg, m);
891 return genlmsg_end(msg, hdr);
893 genlmsg_cancel(msg, hdr);
897 static int swconfig_dump_switches(struct sk_buff *skb,
898 struct netlink_callback *cb)
900 struct switch_dev *dev;
901 int start = cb->args[0];
905 list_for_each_entry(dev, &swdevs, dev_list) {
908 if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).portid,
909 cb->nlh->nlmsg_seq, NLM_F_MULTI,
920 swconfig_done(struct netlink_callback *cb)
925 static struct genl_ops swconfig_ops[] = {
927 .cmd = SWITCH_CMD_LIST_GLOBAL,
928 .doit = swconfig_list_attrs,
929 .policy = switch_policy,
932 .cmd = SWITCH_CMD_LIST_VLAN,
933 .doit = swconfig_list_attrs,
934 .policy = switch_policy,
937 .cmd = SWITCH_CMD_LIST_PORT,
938 .doit = swconfig_list_attrs,
939 .policy = switch_policy,
942 .cmd = SWITCH_CMD_GET_GLOBAL,
943 .doit = swconfig_get_attr,
944 .policy = switch_policy,
947 .cmd = SWITCH_CMD_GET_VLAN,
948 .doit = swconfig_get_attr,
949 .policy = switch_policy,
952 .cmd = SWITCH_CMD_GET_PORT,
953 .doit = swconfig_get_attr,
954 .policy = switch_policy,
957 .cmd = SWITCH_CMD_SET_GLOBAL,
958 .doit = swconfig_set_attr,
959 .policy = switch_policy,
962 .cmd = SWITCH_CMD_SET_VLAN,
963 .doit = swconfig_set_attr,
964 .policy = switch_policy,
967 .cmd = SWITCH_CMD_SET_PORT,
968 .doit = swconfig_set_attr,
969 .policy = switch_policy,
972 .cmd = SWITCH_CMD_GET_SWITCH,
973 .dumpit = swconfig_dump_switches,
974 .policy = switch_policy,
975 .done = swconfig_done,
981 of_switch_load_portmap(struct switch_dev *dev)
983 struct device_node *port;
988 for_each_child_of_node(dev->of_node, port) {
993 if (!of_device_is_compatible(port, "swconfig,port"))
996 if (of_property_read_string(port, "swconfig,segment", &segment))
999 prop = of_get_property(port, "swconfig,portmap", &size);
1003 if (size != (2 * sizeof(*prop))) {
1004 pr_err("%s: failed to parse port mapping\n",
1009 phys = be32_to_cpup(prop++);
1010 if ((phys < 0) | (phys >= dev->ports)) {
1011 pr_err("%s: physical port index out of range\n",
1016 dev->portmap[phys].s = kstrdup(segment, GFP_KERNEL);
1017 dev->portmap[phys].virt = be32_to_cpup(prop);
1018 pr_debug("Found port: %s, physical: %d, virtual: %d\n",
1019 segment, phys, dev->portmap[phys].virt);
1025 register_switch(struct switch_dev *dev, struct net_device *netdev)
1027 struct switch_dev *sdev;
1028 const int max_switches = 8 * sizeof(unsigned long);
1029 unsigned long in_use = 0;
1033 INIT_LIST_HEAD(&dev->dev_list);
1035 dev->netdev = netdev;
1037 dev->alias = netdev->name;
1039 BUG_ON(!dev->alias);
1041 if (dev->ports > 0) {
1042 dev->portbuf = kzalloc(sizeof(struct switch_port) *
1043 dev->ports, GFP_KERNEL);
1046 dev->portmap = kzalloc(sizeof(struct switch_portmap) *
1047 dev->ports, GFP_KERNEL);
1048 if (!dev->portmap) {
1049 kfree(dev->portbuf);
1053 swconfig_defaults_init(dev);
1054 mutex_init(&dev->sw_mutex);
1056 dev->id = ++swdev_id;
1058 list_for_each_entry(sdev, &swdevs, dev_list) {
1059 if (!sscanf(sdev->devname, SWCONFIG_DEVNAME, &i))
1061 if (i < 0 || i > max_switches)
1064 set_bit(i, &in_use);
1066 i = find_first_zero_bit(&in_use, max_switches);
1068 if (i == max_switches) {
1075 of_switch_load_portmap(dev);
1078 /* fill device name */
1079 snprintf(dev->devname, IFNAMSIZ, SWCONFIG_DEVNAME, i);
1081 list_add(&dev->dev_list, &swdevs);
1084 err = swconfig_create_led_trigger(dev);
1090 EXPORT_SYMBOL_GPL(register_switch);
1093 unregister_switch(struct switch_dev *dev)
1095 swconfig_destroy_led_trigger(dev);
1096 kfree(dev->portbuf);
1097 mutex_lock(&dev->sw_mutex);
1099 list_del(&dev->dev_list);
1101 mutex_unlock(&dev->sw_mutex);
1103 EXPORT_SYMBOL_GPL(unregister_switch);
1111 INIT_LIST_HEAD(&swdevs);
1112 err = genl_register_family(&switch_fam);
1116 for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
1117 err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
1125 genl_unregister_family(&switch_fam);
1132 genl_unregister_family(&switch_fam);
1135 module_init(swconfig_init);
1136 module_exit(swconfig_exit);