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>
30 #define DPRINTF(format, ...) printk("%s: " format, __func__, ##__VA_ARGS__)
32 #define DPRINTF(...) do {} while(0)
35 #define SWCONFIG_DEVNAME "switch%d"
37 #include "swconfig_leds.c"
39 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
40 MODULE_LICENSE("GPL");
42 static int swdev_id
= 0;
43 static struct list_head swdevs
;
44 static DEFINE_SPINLOCK(swdevs_lock
);
45 struct swconfig_callback
;
47 struct swconfig_callback
50 struct genlmsghdr
*hdr
;
51 struct genl_info
*info
;
54 /* callback for filling in the message data */
55 int (*fill
)(struct swconfig_callback
*cb
, void *arg
);
57 /* callback for closing the message before sending it */
58 int (*close
)(struct swconfig_callback
*cb
, void *arg
);
60 struct nlattr
*nest
[4];
67 swconfig_get_vlan_ports(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
70 if (val
->port_vlan
>= dev
->vlans
)
73 if (!dev
->ops
->get_vlan_ports
)
76 ret
= dev
->ops
->get_vlan_ports(dev
, val
);
81 swconfig_set_vlan_ports(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
83 struct switch_port
*ports
= val
->value
.ports
;
84 const struct switch_dev_ops
*ops
= dev
->ops
;
87 if (val
->port_vlan
>= dev
->vlans
)
91 if (val
->len
> dev
->ports
)
94 if (!ops
->set_vlan_ports
)
97 for (i
= 0; i
< val
->len
; i
++) {
98 if (ports
[i
].id
>= dev
->ports
)
101 if (ops
->set_port_pvid
&&
102 !(ports
[i
].flags
& (1 << SWITCH_PORT_FLAG_TAGGED
)))
103 ops
->set_port_pvid(dev
, ports
[i
].id
, val
->port_vlan
);
106 return ops
->set_vlan_ports(dev
, val
);
110 swconfig_set_pvid(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
112 if (val
->port_vlan
>= dev
->ports
)
115 if (!dev
->ops
->set_port_pvid
)
118 return dev
->ops
->set_port_pvid(dev
, val
->port_vlan
, val
->value
.i
);
122 swconfig_get_pvid(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
124 if (val
->port_vlan
>= dev
->ports
)
127 if (!dev
->ops
->get_port_pvid
)
130 return dev
->ops
->get_port_pvid(dev
, val
->port_vlan
, &val
->value
.i
);
134 swconfig_speed_str(enum switch_port_speed speed
)
137 case SWITCH_PORT_SPEED_10
:
139 case SWITCH_PORT_SPEED_100
:
141 case SWITCH_PORT_SPEED_1000
:
151 swconfig_get_link(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
153 struct switch_port_link link
;
157 if (val
->port_vlan
>= dev
->ports
)
160 if (!dev
->ops
->get_port_link
)
163 memset(&link
, 0, sizeof(link
));
164 ret
= dev
->ops
->get_port_link(dev
, val
->port_vlan
, &link
);
168 memset(dev
->buf
, 0, sizeof(dev
->buf
));
171 len
= snprintf(dev
->buf
, sizeof(dev
->buf
),
172 "port:%d link:up speed:%s %s-duplex %s%s%s",
174 swconfig_speed_str(link
.speed
),
175 link
.duplex
? "full" : "half",
176 link
.tx_flow
? "txflow ": "",
177 link
.rx_flow
? "rxflow " : "",
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
, struct switch_val
*val
)
192 /* don't complain if not supported by the switch driver */
193 if (!dev
->ops
->apply_config
)
196 return dev
->ops
->apply_config(dev
);
200 swconfig_reset_switch(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
202 /* don't complain if not supported by the switch driver */
203 if (!dev
->ops
->reset_switch
)
206 return dev
->ops
->reset_switch(dev
);
209 enum global_defaults
{
223 static struct switch_attr default_global
[] = {
225 .type
= SWITCH_TYPE_NOVAL
,
227 .description
= "Activate changes in the hardware",
228 .set
= swconfig_apply_config
,
231 .type
= SWITCH_TYPE_NOVAL
,
233 .description
= "Reset the switch",
234 .set
= swconfig_reset_switch
,
238 static struct switch_attr default_port
[] = {
240 .type
= SWITCH_TYPE_INT
,
242 .description
= "Primary VLAN ID",
243 .set
= swconfig_set_pvid
,
244 .get
= swconfig_get_pvid
,
247 .type
= SWITCH_TYPE_STRING
,
249 .description
= "Get port link information",
251 .get
= swconfig_get_link
,
255 static struct switch_attr default_vlan
[] = {
257 .type
= SWITCH_TYPE_PORTS
,
259 .description
= "VLAN port mapping",
260 .set
= swconfig_set_vlan_ports
,
261 .get
= swconfig_get_vlan_ports
,
265 static const struct switch_attr
*
266 swconfig_find_attr_by_name(const struct switch_attrlist
*alist
, const char *name
)
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 DPRINTF("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 DPRINTF("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
)
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
], &val
, dev
->ports
);
689 err
= attr
->set(dev
, attr
, &val
);
691 swconfig_put_dev(dev
);
696 swconfig_close_portlist(struct swconfig_callback
*cb
, void *arg
)
699 nla_nest_end(cb
->msg
, cb
->nest
[0]);
704 swconfig_send_port(struct swconfig_callback
*cb
, void *arg
)
706 const struct switch_port
*port
= arg
;
707 struct nlattr
*p
= NULL
;
710 cb
->nest
[0] = nla_nest_start(cb
->msg
, cb
->cmd
);
715 p
= nla_nest_start(cb
->msg
, SWITCH_ATTR_PORT
);
719 if (nla_put_u32(cb
->msg
, SWITCH_PORT_ID
, port
->id
))
720 goto nla_put_failure
;
721 if (port
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
)) {
722 if (nla_put_flag(cb
->msg
, SWITCH_PORT_FLAG_TAGGED
))
723 goto nla_put_failure
;
726 nla_nest_end(cb
->msg
, p
);
730 nla_nest_cancel(cb
->msg
, p
);
732 nla_nest_cancel(cb
->msg
, cb
->nest
[0]);
737 swconfig_send_ports(struct sk_buff
**msg
, struct genl_info
*info
, int attr
,
738 const struct switch_val
*val
)
740 struct swconfig_callback cb
;
744 if (!val
->value
.ports
)
747 memset(&cb
, 0, sizeof(cb
));
751 cb
.fill
= swconfig_send_port
;
752 cb
.close
= swconfig_close_portlist
;
754 cb
.nest
[0] = nla_nest_start(cb
.msg
, cb
.cmd
);
755 for (i
= 0; i
< val
->len
; i
++) {
756 err
= swconfig_send_multipart(&cb
, &val
->value
.ports
[i
]);
761 swconfig_close_portlist(&cb
, NULL
);
769 swconfig_get_attr(struct sk_buff
*skb
, struct genl_info
*info
)
771 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
772 const struct switch_attr
*attr
;
773 struct switch_dev
*dev
;
774 struct sk_buff
*msg
= NULL
;
775 struct switch_val val
;
779 dev
= swconfig_get_dev(info
);
783 memset(&val
, 0, sizeof(val
));
784 attr
= swconfig_lookup_attr(dev
, info
, &val
);
785 if (!attr
|| !attr
->get
)
788 if (attr
->type
== SWITCH_TYPE_PORTS
) {
789 val
.value
.ports
= dev
->portbuf
;
790 memset(dev
->portbuf
, 0,
791 sizeof(struct switch_port
) * dev
->ports
);
794 err
= attr
->get(dev
, attr
, &val
);
798 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
802 hdr
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
, &switch_fam
,
805 goto nla_put_failure
;
808 case SWITCH_TYPE_INT
:
809 if (nla_put_u32(msg
, SWITCH_ATTR_OP_VALUE_INT
, val
.value
.i
))
810 goto nla_put_failure
;
812 case SWITCH_TYPE_STRING
:
813 if (nla_put_string(msg
, SWITCH_ATTR_OP_VALUE_STR
, val
.value
.s
))
814 goto nla_put_failure
;
816 case SWITCH_TYPE_PORTS
:
817 err
= swconfig_send_ports(&msg
, info
,
818 SWITCH_ATTR_OP_VALUE_PORTS
, &val
);
820 goto nla_put_failure
;
823 DPRINTF("invalid type in attribute\n");
827 err
= genlmsg_end(msg
, hdr
);
829 goto nla_put_failure
;
831 swconfig_put_dev(dev
);
832 return genlmsg_reply(msg
, info
);
838 swconfig_put_dev(dev
);
845 swconfig_send_switch(struct sk_buff
*msg
, u32 pid
, u32 seq
, int flags
,
846 const struct switch_dev
*dev
)
848 struct nlattr
*p
= NULL
, *m
= NULL
;
852 hdr
= genlmsg_put(msg
, pid
, seq
, &switch_fam
, flags
,
853 SWITCH_CMD_NEW_ATTR
);
857 if (nla_put_u32(msg
, SWITCH_ATTR_ID
, dev
->id
))
858 goto nla_put_failure
;
859 if (nla_put_string(msg
, SWITCH_ATTR_DEV_NAME
, dev
->devname
))
860 goto nla_put_failure
;
861 if (nla_put_string(msg
, SWITCH_ATTR_ALIAS
, dev
->alias
))
862 goto nla_put_failure
;
863 if (nla_put_string(msg
, SWITCH_ATTR_NAME
, dev
->name
))
864 goto nla_put_failure
;
865 if (nla_put_u32(msg
, SWITCH_ATTR_VLANS
, dev
->vlans
))
866 goto nla_put_failure
;
867 if (nla_put_u32(msg
, SWITCH_ATTR_PORTS
, dev
->ports
))
868 goto nla_put_failure
;
869 if (nla_put_u32(msg
, SWITCH_ATTR_CPU_PORT
, dev
->cpu_port
))
870 goto nla_put_failure
;
872 m
= nla_nest_start(msg
, SWITCH_ATTR_PORTMAP
);
874 goto nla_put_failure
;
875 for (i
= 0; i
< dev
->ports
; i
++) {
876 p
= nla_nest_start(msg
, SWITCH_ATTR_PORTS
);
879 if (dev
->portmap
[i
].s
) {
880 if (nla_put_string(msg
, SWITCH_PORTMAP_SEGMENT
, dev
->portmap
[i
].s
))
881 goto nla_put_failure
;
882 if (nla_put_u32(msg
, SWITCH_PORTMAP_VIRT
, dev
->portmap
[i
].virt
))
883 goto nla_put_failure
;
885 nla_nest_end(msg
, p
);
887 nla_nest_end(msg
, m
);
888 return genlmsg_end(msg
, hdr
);
890 genlmsg_cancel(msg
, hdr
);
894 static int swconfig_dump_switches(struct sk_buff
*skb
,
895 struct netlink_callback
*cb
)
897 struct switch_dev
*dev
;
898 int start
= cb
->args
[0];
902 list_for_each_entry(dev
, &swdevs
, dev_list
) {
905 if (swconfig_send_switch(skb
, NETLINK_CB(cb
->skb
).portid
,
906 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
917 swconfig_done(struct netlink_callback
*cb
)
922 static struct genl_ops swconfig_ops
[] = {
924 .cmd
= SWITCH_CMD_LIST_GLOBAL
,
925 .doit
= swconfig_list_attrs
,
926 .policy
= switch_policy
,
929 .cmd
= SWITCH_CMD_LIST_VLAN
,
930 .doit
= swconfig_list_attrs
,
931 .policy
= switch_policy
,
934 .cmd
= SWITCH_CMD_LIST_PORT
,
935 .doit
= swconfig_list_attrs
,
936 .policy
= switch_policy
,
939 .cmd
= SWITCH_CMD_GET_GLOBAL
,
940 .doit
= swconfig_get_attr
,
941 .policy
= switch_policy
,
944 .cmd
= SWITCH_CMD_GET_VLAN
,
945 .doit
= swconfig_get_attr
,
946 .policy
= switch_policy
,
949 .cmd
= SWITCH_CMD_GET_PORT
,
950 .doit
= swconfig_get_attr
,
951 .policy
= switch_policy
,
954 .cmd
= SWITCH_CMD_SET_GLOBAL
,
955 .doit
= swconfig_set_attr
,
956 .policy
= switch_policy
,
959 .cmd
= SWITCH_CMD_SET_VLAN
,
960 .doit
= swconfig_set_attr
,
961 .policy
= switch_policy
,
964 .cmd
= SWITCH_CMD_SET_PORT
,
965 .doit
= swconfig_set_attr
,
966 .policy
= switch_policy
,
969 .cmd
= SWITCH_CMD_GET_SWITCH
,
970 .dumpit
= swconfig_dump_switches
,
971 .policy
= switch_policy
,
972 .done
= swconfig_done
,
978 of_switch_load_portmap(struct switch_dev
*dev
)
980 struct device_node
*port
;
985 for_each_child_of_node(dev
->of_node
, port
) {
990 if (of_device_is_compatible(port
, "swconfig,port"))
993 if (of_property_read_string(port
, "swconfig,segment", &segment
))
996 prop
= of_get_property(port
, "swconfig,portmap", &size
);
1000 if (size
!= (2 * sizeof(*prop
))) {
1001 pr_err("%s: failed to parse port mapping\n", port
->name
);
1005 phys
= be32_to_cpup(prop
++);
1006 if ((phys
< 0) | (phys
>= dev
->ports
)) {
1007 pr_err("%s: physical port index out of range\n", port
->name
);
1011 dev
->portmap
[phys
].s
= kstrdup(segment
, GFP_KERNEL
);
1012 dev
->portmap
[phys
].virt
= be32_to_cpup(prop
);
1013 pr_debug("Found port: %s, physical: %d, virtual: %d\n",
1014 segment
, phys
, dev
->portmap
[phys
].virt
);
1020 register_switch(struct switch_dev
*dev
, struct net_device
*netdev
)
1022 struct switch_dev
*sdev
;
1023 const int max_switches
= 8 * sizeof(unsigned long);
1024 unsigned long in_use
= 0;
1028 INIT_LIST_HEAD(&dev
->dev_list
);
1030 dev
->netdev
= netdev
;
1032 dev
->alias
= netdev
->name
;
1034 BUG_ON(!dev
->alias
);
1036 if (dev
->ports
> 0) {
1037 dev
->portbuf
= kzalloc(sizeof(struct switch_port
) * dev
->ports
,
1041 dev
->portmap
= kzalloc(sizeof(struct switch_portmap
) * dev
->ports
,
1043 if (!dev
->portmap
) {
1044 kfree(dev
->portbuf
);
1048 swconfig_defaults_init(dev
);
1049 mutex_init(&dev
->sw_mutex
);
1051 dev
->id
= ++swdev_id
;
1053 list_for_each_entry(sdev
, &swdevs
, dev_list
) {
1054 if (!sscanf(sdev
->devname
, SWCONFIG_DEVNAME
, &i
))
1056 if (i
< 0 || i
> max_switches
)
1059 set_bit(i
, &in_use
);
1061 i
= find_first_zero_bit(&in_use
, max_switches
);
1063 if (i
== max_switches
) {
1070 of_switch_load_portmap(dev
);
1073 /* fill device name */
1074 snprintf(dev
->devname
, IFNAMSIZ
, SWCONFIG_DEVNAME
, i
);
1076 list_add(&dev
->dev_list
, &swdevs
);
1079 err
= swconfig_create_led_trigger(dev
);
1085 EXPORT_SYMBOL_GPL(register_switch
);
1088 unregister_switch(struct switch_dev
*dev
)
1090 swconfig_destroy_led_trigger(dev
);
1091 kfree(dev
->portbuf
);
1092 mutex_lock(&dev
->sw_mutex
);
1094 list_del(&dev
->dev_list
);
1096 mutex_unlock(&dev
->sw_mutex
);
1098 EXPORT_SYMBOL_GPL(unregister_switch
);
1106 INIT_LIST_HEAD(&swdevs
);
1107 err
= genl_register_family(&switch_fam
);
1111 for (i
= 0; i
< ARRAY_SIZE(swconfig_ops
); i
++) {
1112 err
= genl_register_ops(&switch_fam
, &swconfig_ops
[i
]);
1120 genl_unregister_family(&switch_fam
);
1127 genl_unregister_family(&switch_fam
);
1130 module_init(swconfig_init
);
1131 module_exit(swconfig_exit
);