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_get_link(struct switch_dev
*dev
, const struct switch_attr
*attr
,
132 struct switch_val
*val
)
134 struct switch_port_link
*link
= val
->value
.link
;
136 if (val
->port_vlan
>= dev
->ports
)
139 if (!dev
->ops
->get_port_link
)
142 memset(link
, 0, sizeof(*link
));
143 return dev
->ops
->get_port_link(dev
, val
->port_vlan
, link
);
147 swconfig_apply_config(struct switch_dev
*dev
, const struct switch_attr
*attr
,
148 struct switch_val
*val
)
150 /* don't complain if not supported by the switch driver */
151 if (!dev
->ops
->apply_config
)
154 return dev
->ops
->apply_config(dev
);
158 swconfig_reset_switch(struct switch_dev
*dev
, const struct switch_attr
*attr
,
159 struct switch_val
*val
)
161 /* don't complain if not supported by the switch driver */
162 if (!dev
->ops
->reset_switch
)
165 return dev
->ops
->reset_switch(dev
);
168 enum global_defaults
{
182 static struct switch_attr default_global
[] = {
184 .type
= SWITCH_TYPE_NOVAL
,
186 .description
= "Activate changes in the hardware",
187 .set
= swconfig_apply_config
,
190 .type
= SWITCH_TYPE_NOVAL
,
192 .description
= "Reset the switch",
193 .set
= swconfig_reset_switch
,
197 static struct switch_attr default_port
[] = {
199 .type
= SWITCH_TYPE_INT
,
201 .description
= "Primary VLAN ID",
202 .set
= swconfig_set_pvid
,
203 .get
= swconfig_get_pvid
,
206 .type
= SWITCH_TYPE_LINK
,
208 .description
= "Get port link information",
210 .get
= swconfig_get_link
,
214 static struct switch_attr default_vlan
[] = {
216 .type
= SWITCH_TYPE_PORTS
,
218 .description
= "VLAN port mapping",
219 .set
= swconfig_set_vlan_ports
,
220 .get
= swconfig_get_vlan_ports
,
224 static const struct switch_attr
*
225 swconfig_find_attr_by_name(const struct switch_attrlist
*alist
,
230 for (i
= 0; i
< alist
->n_attr
; i
++)
231 if (strcmp(name
, alist
->attr
[i
].name
) == 0)
232 return &alist
->attr
[i
];
237 static void swconfig_defaults_init(struct switch_dev
*dev
)
239 const struct switch_dev_ops
*ops
= dev
->ops
;
245 if (ops
->get_vlan_ports
|| ops
->set_vlan_ports
)
246 set_bit(VLAN_PORTS
, &dev
->def_vlan
);
248 if (ops
->get_port_pvid
|| ops
->set_port_pvid
)
249 set_bit(PORT_PVID
, &dev
->def_port
);
251 if (ops
->get_port_link
&&
252 !swconfig_find_attr_by_name(&ops
->attr_port
, "link"))
253 set_bit(PORT_LINK
, &dev
->def_port
);
255 /* always present, can be no-op */
256 set_bit(GLOBAL_APPLY
, &dev
->def_global
);
257 set_bit(GLOBAL_RESET
, &dev
->def_global
);
261 static struct genl_family switch_fam
= {
262 .id
= GENL_ID_GENERATE
,
266 .maxattr
= SWITCH_ATTR_MAX
,
269 static const struct nla_policy switch_policy
[SWITCH_ATTR_MAX
+1] = {
270 [SWITCH_ATTR_ID
] = { .type
= NLA_U32
},
271 [SWITCH_ATTR_OP_ID
] = { .type
= NLA_U32
},
272 [SWITCH_ATTR_OP_PORT
] = { .type
= NLA_U32
},
273 [SWITCH_ATTR_OP_VLAN
] = { .type
= NLA_U32
},
274 [SWITCH_ATTR_OP_VALUE_INT
] = { .type
= NLA_U32
},
275 [SWITCH_ATTR_OP_VALUE_STR
] = { .type
= NLA_NUL_STRING
},
276 [SWITCH_ATTR_OP_VALUE_PORTS
] = { .type
= NLA_NESTED
},
277 [SWITCH_ATTR_TYPE
] = { .type
= NLA_U32
},
280 static const struct nla_policy port_policy
[SWITCH_PORT_ATTR_MAX
+1] = {
281 [SWITCH_PORT_ID
] = { .type
= NLA_U32
},
282 [SWITCH_PORT_FLAG_TAGGED
] = { .type
= NLA_FLAG
},
288 spin_lock(&swdevs_lock
);
292 swconfig_unlock(void)
294 spin_unlock(&swdevs_lock
);
297 static struct switch_dev
*
298 swconfig_get_dev(struct genl_info
*info
)
300 struct switch_dev
*dev
= NULL
;
301 struct switch_dev
*p
;
304 if (!info
->attrs
[SWITCH_ATTR_ID
])
307 id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_ID
]);
309 list_for_each_entry(p
, &swdevs
, dev_list
) {
317 mutex_lock(&dev
->sw_mutex
);
319 pr_debug("device %d not found\n", id
);
326 swconfig_put_dev(struct switch_dev
*dev
)
328 mutex_unlock(&dev
->sw_mutex
);
332 swconfig_dump_attr(struct swconfig_callback
*cb
, void *arg
)
334 struct switch_attr
*op
= arg
;
335 struct genl_info
*info
= cb
->info
;
336 struct sk_buff
*msg
= cb
->msg
;
337 int id
= cb
->args
[0];
340 hdr
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
, &switch_fam
,
341 NLM_F_MULTI
, SWITCH_CMD_NEW_ATTR
);
345 if (nla_put_u32(msg
, SWITCH_ATTR_OP_ID
, id
))
346 goto nla_put_failure
;
347 if (nla_put_u32(msg
, SWITCH_ATTR_OP_TYPE
, op
->type
))
348 goto nla_put_failure
;
349 if (nla_put_string(msg
, SWITCH_ATTR_OP_NAME
, op
->name
))
350 goto nla_put_failure
;
352 if (nla_put_string(msg
, SWITCH_ATTR_OP_DESCRIPTION
,
354 goto nla_put_failure
;
356 genlmsg_end(msg
, hdr
);
359 genlmsg_cancel(msg
, hdr
);
363 /* spread multipart messages across multiple message buffers */
365 swconfig_send_multipart(struct swconfig_callback
*cb
, void *arg
)
367 struct genl_info
*info
= cb
->info
;
373 cb
->msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
378 if (!(cb
->fill(cb
, arg
) < 0))
381 /* fill failed, check if this was already the second attempt */
385 /* try again in a new message, send the current one */
388 if (cb
->close(cb
, arg
) < 0)
391 err
= genlmsg_reply(cb
->msg
, info
);
407 swconfig_list_attrs(struct sk_buff
*skb
, struct genl_info
*info
)
409 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
410 const struct switch_attrlist
*alist
;
411 struct switch_dev
*dev
;
412 struct swconfig_callback cb
;
417 struct switch_attr
*def_list
;
418 unsigned long *def_active
;
421 dev
= swconfig_get_dev(info
);
426 case SWITCH_CMD_LIST_GLOBAL
:
427 alist
= &dev
->ops
->attr_global
;
428 def_list
= default_global
;
429 def_active
= &dev
->def_global
;
430 n_def
= ARRAY_SIZE(default_global
);
432 case SWITCH_CMD_LIST_VLAN
:
433 alist
= &dev
->ops
->attr_vlan
;
434 def_list
= default_vlan
;
435 def_active
= &dev
->def_vlan
;
436 n_def
= ARRAY_SIZE(default_vlan
);
438 case SWITCH_CMD_LIST_PORT
:
439 alist
= &dev
->ops
->attr_port
;
440 def_list
= default_port
;
441 def_active
= &dev
->def_port
;
442 n_def
= ARRAY_SIZE(default_port
);
449 memset(&cb
, 0, sizeof(cb
));
451 cb
.fill
= swconfig_dump_attr
;
452 for (i
= 0; i
< alist
->n_attr
; i
++) {
453 if (alist
->attr
[i
].disabled
)
456 err
= swconfig_send_multipart(&cb
, (void *) &alist
->attr
[i
]);
462 for (i
= 0; i
< n_def
; i
++) {
463 if (!test_bit(i
, def_active
))
465 cb
.args
[0] = SWITCH_ATTR_DEFAULTS_OFFSET
+ i
;
466 err
= swconfig_send_multipart(&cb
, (void *) &def_list
[i
]);
470 swconfig_put_dev(dev
);
475 return genlmsg_reply(cb
.msg
, info
);
481 swconfig_put_dev(dev
);
485 static const struct switch_attr
*
486 swconfig_lookup_attr(struct switch_dev
*dev
, struct genl_info
*info
,
487 struct switch_val
*val
)
489 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
490 const struct switch_attrlist
*alist
;
491 const struct switch_attr
*attr
= NULL
;
495 struct switch_attr
*def_list
;
496 unsigned long *def_active
;
499 if (!info
->attrs
[SWITCH_ATTR_OP_ID
])
503 case SWITCH_CMD_SET_GLOBAL
:
504 case SWITCH_CMD_GET_GLOBAL
:
505 alist
= &dev
->ops
->attr_global
;
506 def_list
= default_global
;
507 def_active
= &dev
->def_global
;
508 n_def
= ARRAY_SIZE(default_global
);
510 case SWITCH_CMD_SET_VLAN
:
511 case SWITCH_CMD_GET_VLAN
:
512 alist
= &dev
->ops
->attr_vlan
;
513 def_list
= default_vlan
;
514 def_active
= &dev
->def_vlan
;
515 n_def
= ARRAY_SIZE(default_vlan
);
516 if (!info
->attrs
[SWITCH_ATTR_OP_VLAN
])
518 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VLAN
]);
519 if (val
->port_vlan
>= dev
->vlans
)
522 case SWITCH_CMD_SET_PORT
:
523 case SWITCH_CMD_GET_PORT
:
524 alist
= &dev
->ops
->attr_port
;
525 def_list
= default_port
;
526 def_active
= &dev
->def_port
;
527 n_def
= ARRAY_SIZE(default_port
);
528 if (!info
->attrs
[SWITCH_ATTR_OP_PORT
])
530 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_PORT
]);
531 if (val
->port_vlan
>= dev
->ports
)
542 attr_id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_ID
]);
543 if (attr_id
>= SWITCH_ATTR_DEFAULTS_OFFSET
) {
544 attr_id
-= SWITCH_ATTR_DEFAULTS_OFFSET
;
545 if (attr_id
>= n_def
)
547 if (!test_bit(attr_id
, def_active
))
549 attr
= &def_list
[attr_id
];
551 if (attr_id
>= alist
->n_attr
)
553 attr
= &alist
->attr
[attr_id
];
561 pr_debug("attribute lookup failed\n");
567 swconfig_parse_ports(struct sk_buff
*msg
, struct nlattr
*head
,
568 struct switch_val
*val
, int max
)
574 nla_for_each_nested(nla
, head
, rem
) {
575 struct nlattr
*tb
[SWITCH_PORT_ATTR_MAX
+1];
576 struct switch_port
*port
= &val
->value
.ports
[val
->len
];
581 if (nla_parse_nested(tb
, SWITCH_PORT_ATTR_MAX
, nla
,
585 if (!tb
[SWITCH_PORT_ID
])
588 port
->id
= nla_get_u32(tb
[SWITCH_PORT_ID
]);
589 if (tb
[SWITCH_PORT_FLAG_TAGGED
])
590 port
->flags
|= (1 << SWITCH_PORT_FLAG_TAGGED
);
598 swconfig_set_attr(struct sk_buff
*skb
, struct genl_info
*info
)
600 const struct switch_attr
*attr
;
601 struct switch_dev
*dev
;
602 struct switch_val val
;
605 dev
= swconfig_get_dev(info
);
609 memset(&val
, 0, sizeof(val
));
610 attr
= swconfig_lookup_attr(dev
, info
, &val
);
611 if (!attr
|| !attr
->set
)
615 switch (attr
->type
) {
616 case SWITCH_TYPE_NOVAL
:
618 case SWITCH_TYPE_INT
:
619 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
])
622 nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
]);
624 case SWITCH_TYPE_STRING
:
625 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
])
628 nla_data(info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
]);
630 case SWITCH_TYPE_PORTS
:
631 val
.value
.ports
= dev
->portbuf
;
632 memset(dev
->portbuf
, 0,
633 sizeof(struct switch_port
) * dev
->ports
);
635 /* TODO: implement multipart? */
636 if (info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
]) {
637 err
= swconfig_parse_ports(skb
,
638 info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
],
651 err
= attr
->set(dev
, attr
, &val
);
653 swconfig_put_dev(dev
);
658 swconfig_close_portlist(struct swconfig_callback
*cb
, void *arg
)
661 nla_nest_end(cb
->msg
, cb
->nest
[0]);
666 swconfig_send_port(struct swconfig_callback
*cb
, void *arg
)
668 const struct switch_port
*port
= arg
;
669 struct nlattr
*p
= NULL
;
672 cb
->nest
[0] = nla_nest_start(cb
->msg
, cb
->cmd
);
677 p
= nla_nest_start(cb
->msg
, SWITCH_ATTR_PORT
);
681 if (nla_put_u32(cb
->msg
, SWITCH_PORT_ID
, port
->id
))
682 goto nla_put_failure
;
683 if (port
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
)) {
684 if (nla_put_flag(cb
->msg
, SWITCH_PORT_FLAG_TAGGED
))
685 goto nla_put_failure
;
688 nla_nest_end(cb
->msg
, p
);
692 nla_nest_cancel(cb
->msg
, p
);
694 nla_nest_cancel(cb
->msg
, cb
->nest
[0]);
699 swconfig_send_ports(struct sk_buff
**msg
, struct genl_info
*info
, int attr
,
700 const struct switch_val
*val
)
702 struct swconfig_callback cb
;
706 if (!val
->value
.ports
)
709 memset(&cb
, 0, sizeof(cb
));
713 cb
.fill
= swconfig_send_port
;
714 cb
.close
= swconfig_close_portlist
;
716 cb
.nest
[0] = nla_nest_start(cb
.msg
, cb
.cmd
);
717 for (i
= 0; i
< val
->len
; i
++) {
718 err
= swconfig_send_multipart(&cb
, &val
->value
.ports
[i
]);
723 swconfig_close_portlist(&cb
, NULL
);
731 swconfig_send_link(struct sk_buff
*msg
, struct genl_info
*info
, int attr
,
732 const struct switch_port_link
*link
)
734 struct nlattr
*p
= NULL
;
737 p
= nla_nest_start(msg
, attr
);
739 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_LINK
))
740 goto nla_put_failure
;
743 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_DUPLEX
))
744 goto nla_put_failure
;
747 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_ANEG
))
748 goto nla_put_failure
;
751 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_TX_FLOW
))
752 goto nla_put_failure
;
755 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_RX_FLOW
))
756 goto nla_put_failure
;
758 if (nla_put_u32(msg
, SWITCH_LINK_SPEED
, link
->speed
))
759 goto nla_put_failure
;
760 if (link
->eee
& ADVERTISED_100baseT_Full
) {
761 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_EEE_100BASET
))
762 goto nla_put_failure
;
764 if (link
->eee
& ADVERTISED_1000baseT_Full
) {
765 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_EEE_1000BASET
))
766 goto nla_put_failure
;
768 nla_nest_end(msg
, p
);
773 nla_nest_cancel(msg
, p
);
778 swconfig_get_attr(struct sk_buff
*skb
, struct genl_info
*info
)
780 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
781 const struct switch_attr
*attr
;
782 struct switch_dev
*dev
;
783 struct sk_buff
*msg
= NULL
;
784 struct switch_val val
;
788 dev
= swconfig_get_dev(info
);
792 memset(&val
, 0, sizeof(val
));
793 attr
= swconfig_lookup_attr(dev
, info
, &val
);
794 if (!attr
|| !attr
->get
)
797 if (attr
->type
== SWITCH_TYPE_PORTS
) {
798 val
.value
.ports
= dev
->portbuf
;
799 memset(dev
->portbuf
, 0,
800 sizeof(struct switch_port
) * dev
->ports
);
801 } else if (attr
->type
== SWITCH_TYPE_LINK
) {
802 val
.value
.link
= &dev
->linkbuf
;
803 memset(&dev
->linkbuf
, 0, sizeof(struct switch_port_link
));
806 err
= attr
->get(dev
, attr
, &val
);
810 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
814 hdr
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
, &switch_fam
,
817 goto nla_put_failure
;
819 switch (attr
->type
) {
820 case SWITCH_TYPE_INT
:
821 if (nla_put_u32(msg
, SWITCH_ATTR_OP_VALUE_INT
, val
.value
.i
))
822 goto nla_put_failure
;
824 case SWITCH_TYPE_STRING
:
825 if (nla_put_string(msg
, SWITCH_ATTR_OP_VALUE_STR
, val
.value
.s
))
826 goto nla_put_failure
;
828 case SWITCH_TYPE_PORTS
:
829 err
= swconfig_send_ports(&msg
, info
,
830 SWITCH_ATTR_OP_VALUE_PORTS
, &val
);
832 goto nla_put_failure
;
834 case SWITCH_TYPE_LINK
:
835 err
= swconfig_send_link(msg
, info
,
836 SWITCH_ATTR_OP_VALUE_LINK
, val
.value
.link
);
838 goto nla_put_failure
;
841 pr_debug("invalid type in attribute\n");
845 genlmsg_end(msg
, hdr
);
848 goto nla_put_failure
;
850 swconfig_put_dev(dev
);
851 return genlmsg_reply(msg
, info
);
857 swconfig_put_dev(dev
);
864 swconfig_send_switch(struct sk_buff
*msg
, u32 pid
, u32 seq
, int flags
,
865 const struct switch_dev
*dev
)
867 struct nlattr
*p
= NULL
, *m
= NULL
;
871 hdr
= genlmsg_put(msg
, pid
, seq
, &switch_fam
, flags
,
872 SWITCH_CMD_NEW_ATTR
);
876 if (nla_put_u32(msg
, SWITCH_ATTR_ID
, dev
->id
))
877 goto nla_put_failure
;
878 if (nla_put_string(msg
, SWITCH_ATTR_DEV_NAME
, dev
->devname
))
879 goto nla_put_failure
;
880 if (nla_put_string(msg
, SWITCH_ATTR_ALIAS
, dev
->alias
))
881 goto nla_put_failure
;
882 if (nla_put_string(msg
, SWITCH_ATTR_NAME
, dev
->name
))
883 goto nla_put_failure
;
884 if (nla_put_u32(msg
, SWITCH_ATTR_VLANS
, dev
->vlans
))
885 goto nla_put_failure
;
886 if (nla_put_u32(msg
, SWITCH_ATTR_PORTS
, dev
->ports
))
887 goto nla_put_failure
;
888 if (nla_put_u32(msg
, SWITCH_ATTR_CPU_PORT
, dev
->cpu_port
))
889 goto nla_put_failure
;
891 m
= nla_nest_start(msg
, SWITCH_ATTR_PORTMAP
);
893 goto nla_put_failure
;
894 for (i
= 0; i
< dev
->ports
; i
++) {
895 p
= nla_nest_start(msg
, SWITCH_ATTR_PORTS
);
898 if (dev
->portmap
[i
].s
) {
899 if (nla_put_string(msg
, SWITCH_PORTMAP_SEGMENT
,
901 goto nla_put_failure
;
902 if (nla_put_u32(msg
, SWITCH_PORTMAP_VIRT
,
903 dev
->portmap
[i
].virt
))
904 goto nla_put_failure
;
906 nla_nest_end(msg
, p
);
908 nla_nest_end(msg
, m
);
909 genlmsg_end(msg
, hdr
);
912 genlmsg_cancel(msg
, hdr
);
916 static int swconfig_dump_switches(struct sk_buff
*skb
,
917 struct netlink_callback
*cb
)
919 struct switch_dev
*dev
;
920 int start
= cb
->args
[0];
924 list_for_each_entry(dev
, &swdevs
, dev_list
) {
927 if (swconfig_send_switch(skb
, NETLINK_CB(cb
->skb
).portid
,
928 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
939 swconfig_done(struct netlink_callback
*cb
)
944 static struct genl_ops swconfig_ops
[] = {
946 .cmd
= SWITCH_CMD_LIST_GLOBAL
,
947 .doit
= swconfig_list_attrs
,
948 .policy
= switch_policy
,
951 .cmd
= SWITCH_CMD_LIST_VLAN
,
952 .doit
= swconfig_list_attrs
,
953 .policy
= switch_policy
,
956 .cmd
= SWITCH_CMD_LIST_PORT
,
957 .doit
= swconfig_list_attrs
,
958 .policy
= switch_policy
,
961 .cmd
= SWITCH_CMD_GET_GLOBAL
,
962 .doit
= swconfig_get_attr
,
963 .policy
= switch_policy
,
966 .cmd
= SWITCH_CMD_GET_VLAN
,
967 .doit
= swconfig_get_attr
,
968 .policy
= switch_policy
,
971 .cmd
= SWITCH_CMD_GET_PORT
,
972 .doit
= swconfig_get_attr
,
973 .policy
= switch_policy
,
976 .cmd
= SWITCH_CMD_SET_GLOBAL
,
977 .doit
= swconfig_set_attr
,
978 .policy
= switch_policy
,
981 .cmd
= SWITCH_CMD_SET_VLAN
,
982 .doit
= swconfig_set_attr
,
983 .policy
= switch_policy
,
986 .cmd
= SWITCH_CMD_SET_PORT
,
987 .doit
= swconfig_set_attr
,
988 .policy
= switch_policy
,
991 .cmd
= SWITCH_CMD_GET_SWITCH
,
992 .dumpit
= swconfig_dump_switches
,
993 .policy
= switch_policy
,
994 .done
= swconfig_done
,
1000 of_switch_load_portmap(struct switch_dev
*dev
)
1002 struct device_node
*port
;
1007 for_each_child_of_node(dev
->of_node
, port
) {
1009 const char *segment
;
1012 if (!of_device_is_compatible(port
, "swconfig,port"))
1015 if (of_property_read_string(port
, "swconfig,segment", &segment
))
1018 prop
= of_get_property(port
, "swconfig,portmap", &size
);
1022 if (size
!= (2 * sizeof(*prop
))) {
1023 pr_err("%s: failed to parse port mapping\n",
1028 phys
= be32_to_cpup(prop
++);
1029 if ((phys
< 0) | (phys
>= dev
->ports
)) {
1030 pr_err("%s: physical port index out of range\n",
1035 dev
->portmap
[phys
].s
= kstrdup(segment
, GFP_KERNEL
);
1036 dev
->portmap
[phys
].virt
= be32_to_cpup(prop
);
1037 pr_debug("Found port: %s, physical: %d, virtual: %d\n",
1038 segment
, phys
, dev
->portmap
[phys
].virt
);
1044 register_switch(struct switch_dev
*dev
, struct net_device
*netdev
)
1046 struct switch_dev
*sdev
;
1047 const int max_switches
= 8 * sizeof(unsigned long);
1048 unsigned long in_use
= 0;
1052 INIT_LIST_HEAD(&dev
->dev_list
);
1054 dev
->netdev
= netdev
;
1056 dev
->alias
= netdev
->name
;
1058 BUG_ON(!dev
->alias
);
1060 if (dev
->ports
> 0) {
1061 dev
->portbuf
= kzalloc(sizeof(struct switch_port
) *
1062 dev
->ports
, GFP_KERNEL
);
1065 dev
->portmap
= kzalloc(sizeof(struct switch_portmap
) *
1066 dev
->ports
, GFP_KERNEL
);
1067 if (!dev
->portmap
) {
1068 kfree(dev
->portbuf
);
1072 swconfig_defaults_init(dev
);
1073 mutex_init(&dev
->sw_mutex
);
1075 dev
->id
= ++swdev_id
;
1077 list_for_each_entry(sdev
, &swdevs
, dev_list
) {
1078 if (!sscanf(sdev
->devname
, SWCONFIG_DEVNAME
, &i
))
1080 if (i
< 0 || i
> max_switches
)
1083 set_bit(i
, &in_use
);
1085 i
= find_first_zero_bit(&in_use
, max_switches
);
1087 if (i
== max_switches
) {
1094 of_switch_load_portmap(dev
);
1097 /* fill device name */
1098 snprintf(dev
->devname
, IFNAMSIZ
, SWCONFIG_DEVNAME
, i
);
1100 list_add_tail(&dev
->dev_list
, &swdevs
);
1103 err
= swconfig_create_led_trigger(dev
);
1109 EXPORT_SYMBOL_GPL(register_switch
);
1112 unregister_switch(struct switch_dev
*dev
)
1114 swconfig_destroy_led_trigger(dev
);
1115 kfree(dev
->portbuf
);
1116 mutex_lock(&dev
->sw_mutex
);
1118 list_del(&dev
->dev_list
);
1120 mutex_unlock(&dev
->sw_mutex
);
1122 EXPORT_SYMBOL_GPL(unregister_switch
);
1129 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0))
1133 INIT_LIST_HEAD(&swdevs
);
1135 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0))
1136 err
= genl_register_family(&switch_fam
);
1140 for (i
= 0; i
< ARRAY_SIZE(swconfig_ops
); i
++) {
1141 err
= genl_register_ops(&switch_fam
, &swconfig_ops
[i
]);
1148 genl_unregister_family(&switch_fam
);
1151 err
= genl_register_family_with_ops(&switch_fam
, swconfig_ops
);
1161 genl_unregister_family(&switch_fam
);
1164 module_init(swconfig_init
);
1165 module_exit(swconfig_exit
);