2 * swconfig.c: Switch configuration API
4 * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
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>
28 #include <uapi/linux/mii.h>
30 #define SWCONFIG_DEVNAME "switch%d"
32 #include "swconfig_leds.c"
34 MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
35 MODULE_LICENSE("GPL");
38 static struct list_head swdevs
;
39 static DEFINE_SPINLOCK(swdevs_lock
);
40 struct swconfig_callback
;
42 struct swconfig_callback
{
44 struct genlmsghdr
*hdr
;
45 struct genl_info
*info
;
48 /* callback for filling in the message data */
49 int (*fill
)(struct swconfig_callback
*cb
, void *arg
);
51 /* callback for closing the message before sending it */
52 int (*close
)(struct swconfig_callback
*cb
, void *arg
);
54 struct nlattr
*nest
[4];
61 swconfig_get_vlan_ports(struct switch_dev
*dev
, const struct switch_attr
*attr
,
62 struct switch_val
*val
)
65 if (val
->port_vlan
>= dev
->vlans
)
68 if (!dev
->ops
->get_vlan_ports
)
71 ret
= dev
->ops
->get_vlan_ports(dev
, val
);
76 swconfig_set_vlan_ports(struct switch_dev
*dev
, const struct switch_attr
*attr
,
77 struct switch_val
*val
)
79 struct switch_port
*ports
= val
->value
.ports
;
80 const struct switch_dev_ops
*ops
= dev
->ops
;
83 if (val
->port_vlan
>= dev
->vlans
)
87 if (val
->len
> dev
->ports
)
90 if (!ops
->set_vlan_ports
)
93 for (i
= 0; i
< val
->len
; i
++) {
94 if (ports
[i
].id
>= dev
->ports
)
97 if (ops
->set_port_pvid
&&
98 !(ports
[i
].flags
& (1 << SWITCH_PORT_FLAG_TAGGED
)))
99 ops
->set_port_pvid(dev
, ports
[i
].id
, val
->port_vlan
);
102 return ops
->set_vlan_ports(dev
, val
);
106 swconfig_set_pvid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
107 struct switch_val
*val
)
109 if (val
->port_vlan
>= dev
->ports
)
112 if (!dev
->ops
->set_port_pvid
)
115 return dev
->ops
->set_port_pvid(dev
, val
->port_vlan
, val
->value
.i
);
119 swconfig_get_pvid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
120 struct switch_val
*val
)
122 if (val
->port_vlan
>= dev
->ports
)
125 if (!dev
->ops
->get_port_pvid
)
128 return dev
->ops
->get_port_pvid(dev
, val
->port_vlan
, &val
->value
.i
);
132 swconfig_set_link(struct switch_dev
*dev
, const struct switch_attr
*attr
,
133 struct switch_val
*val
)
135 if (!dev
->ops
->set_port_link
)
138 return dev
->ops
->set_port_link(dev
, val
->port_vlan
, val
->value
.link
);
142 swconfig_get_link(struct switch_dev
*dev
, const struct switch_attr
*attr
,
143 struct switch_val
*val
)
145 struct switch_port_link
*link
= val
->value
.link
;
147 if (val
->port_vlan
>= dev
->ports
)
150 if (!dev
->ops
->get_port_link
)
153 memset(link
, 0, sizeof(*link
));
154 return dev
->ops
->get_port_link(dev
, val
->port_vlan
, link
);
158 swconfig_apply_config(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
->apply_config
)
165 return dev
->ops
->apply_config(dev
);
169 swconfig_reset_switch(struct switch_dev
*dev
, const struct switch_attr
*attr
,
170 struct switch_val
*val
)
172 /* don't complain if not supported by the switch driver */
173 if (!dev
->ops
->reset_switch
)
176 return dev
->ops
->reset_switch(dev
);
179 enum global_defaults
{
193 static struct switch_attr default_global
[] = {
195 .type
= SWITCH_TYPE_NOVAL
,
197 .description
= "Activate changes in the hardware",
198 .set
= swconfig_apply_config
,
201 .type
= SWITCH_TYPE_NOVAL
,
203 .description
= "Reset the switch",
204 .set
= swconfig_reset_switch
,
208 static struct switch_attr default_port
[] = {
210 .type
= SWITCH_TYPE_INT
,
212 .description
= "Primary VLAN ID",
213 .set
= swconfig_set_pvid
,
214 .get
= swconfig_get_pvid
,
217 .type
= SWITCH_TYPE_LINK
,
219 .description
= "Get port link information",
220 .set
= swconfig_set_link
,
221 .get
= swconfig_get_link
,
225 static struct switch_attr default_vlan
[] = {
227 .type
= SWITCH_TYPE_PORTS
,
229 .description
= "VLAN port mapping",
230 .set
= swconfig_set_vlan_ports
,
231 .get
= swconfig_get_vlan_ports
,
235 static const struct switch_attr
*
236 swconfig_find_attr_by_name(const struct switch_attrlist
*alist
,
241 for (i
= 0; i
< alist
->n_attr
; i
++)
242 if (strcmp(name
, alist
->attr
[i
].name
) == 0)
243 return &alist
->attr
[i
];
248 static void swconfig_defaults_init(struct switch_dev
*dev
)
250 const struct switch_dev_ops
*ops
= dev
->ops
;
256 if (ops
->get_vlan_ports
|| ops
->set_vlan_ports
)
257 set_bit(VLAN_PORTS
, &dev
->def_vlan
);
259 if (ops
->get_port_pvid
|| ops
->set_port_pvid
)
260 set_bit(PORT_PVID
, &dev
->def_port
);
262 if (ops
->get_port_link
&&
263 !swconfig_find_attr_by_name(&ops
->attr_port
, "link"))
264 set_bit(PORT_LINK
, &dev
->def_port
);
266 /* always present, can be no-op */
267 set_bit(GLOBAL_APPLY
, &dev
->def_global
);
268 set_bit(GLOBAL_RESET
, &dev
->def_global
);
272 static struct genl_family switch_fam
= {
273 .id
= GENL_ID_GENERATE
,
277 .maxattr
= SWITCH_ATTR_MAX
,
280 static const struct nla_policy switch_policy
[SWITCH_ATTR_MAX
+1] = {
281 [SWITCH_ATTR_ID
] = { .type
= NLA_U32
},
282 [SWITCH_ATTR_OP_ID
] = { .type
= NLA_U32
},
283 [SWITCH_ATTR_OP_PORT
] = { .type
= NLA_U32
},
284 [SWITCH_ATTR_OP_VLAN
] = { .type
= NLA_U32
},
285 [SWITCH_ATTR_OP_VALUE_INT
] = { .type
= NLA_U32
},
286 [SWITCH_ATTR_OP_VALUE_STR
] = { .type
= NLA_NUL_STRING
},
287 [SWITCH_ATTR_OP_VALUE_PORTS
] = { .type
= NLA_NESTED
},
288 [SWITCH_ATTR_TYPE
] = { .type
= NLA_U32
},
291 static const struct nla_policy port_policy
[SWITCH_PORT_ATTR_MAX
+1] = {
292 [SWITCH_PORT_ID
] = { .type
= NLA_U32
},
293 [SWITCH_PORT_FLAG_TAGGED
] = { .type
= NLA_FLAG
},
296 static struct nla_policy link_policy
[SWITCH_LINK_ATTR_MAX
] = {
297 [SWITCH_LINK_FLAG_DUPLEX
] = { .type
= NLA_FLAG
},
298 [SWITCH_LINK_FLAG_ANEG
] = { .type
= NLA_FLAG
},
299 [SWITCH_LINK_SPEED
] = { .type
= NLA_U32
},
305 spin_lock(&swdevs_lock
);
309 swconfig_unlock(void)
311 spin_unlock(&swdevs_lock
);
314 static struct switch_dev
*
315 swconfig_get_dev(struct genl_info
*info
)
317 struct switch_dev
*dev
= NULL
;
318 struct switch_dev
*p
;
321 if (!info
->attrs
[SWITCH_ATTR_ID
])
324 id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_ID
]);
326 list_for_each_entry(p
, &swdevs
, dev_list
) {
334 mutex_lock(&dev
->sw_mutex
);
336 pr_debug("device %d not found\n", id
);
343 swconfig_put_dev(struct switch_dev
*dev
)
345 mutex_unlock(&dev
->sw_mutex
);
349 swconfig_dump_attr(struct swconfig_callback
*cb
, void *arg
)
351 struct switch_attr
*op
= arg
;
352 struct genl_info
*info
= cb
->info
;
353 struct sk_buff
*msg
= cb
->msg
;
354 int id
= cb
->args
[0];
357 hdr
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
, &switch_fam
,
358 NLM_F_MULTI
, SWITCH_CMD_NEW_ATTR
);
362 if (nla_put_u32(msg
, SWITCH_ATTR_OP_ID
, id
))
363 goto nla_put_failure
;
364 if (nla_put_u32(msg
, SWITCH_ATTR_OP_TYPE
, op
->type
))
365 goto nla_put_failure
;
366 if (nla_put_string(msg
, SWITCH_ATTR_OP_NAME
, op
->name
))
367 goto nla_put_failure
;
369 if (nla_put_string(msg
, SWITCH_ATTR_OP_DESCRIPTION
,
371 goto nla_put_failure
;
373 genlmsg_end(msg
, hdr
);
376 genlmsg_cancel(msg
, hdr
);
380 /* spread multipart messages across multiple message buffers */
382 swconfig_send_multipart(struct swconfig_callback
*cb
, void *arg
)
384 struct genl_info
*info
= cb
->info
;
390 cb
->msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
395 if (!(cb
->fill(cb
, arg
) < 0))
398 /* fill failed, check if this was already the second attempt */
402 /* try again in a new message, send the current one */
405 if (cb
->close(cb
, arg
) < 0)
408 err
= genlmsg_reply(cb
->msg
, info
);
424 swconfig_list_attrs(struct sk_buff
*skb
, struct genl_info
*info
)
426 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
427 const struct switch_attrlist
*alist
;
428 struct switch_dev
*dev
;
429 struct swconfig_callback cb
;
434 struct switch_attr
*def_list
;
435 unsigned long *def_active
;
438 dev
= swconfig_get_dev(info
);
443 case SWITCH_CMD_LIST_GLOBAL
:
444 alist
= &dev
->ops
->attr_global
;
445 def_list
= default_global
;
446 def_active
= &dev
->def_global
;
447 n_def
= ARRAY_SIZE(default_global
);
449 case SWITCH_CMD_LIST_VLAN
:
450 alist
= &dev
->ops
->attr_vlan
;
451 def_list
= default_vlan
;
452 def_active
= &dev
->def_vlan
;
453 n_def
= ARRAY_SIZE(default_vlan
);
455 case SWITCH_CMD_LIST_PORT
:
456 alist
= &dev
->ops
->attr_port
;
457 def_list
= default_port
;
458 def_active
= &dev
->def_port
;
459 n_def
= ARRAY_SIZE(default_port
);
466 memset(&cb
, 0, sizeof(cb
));
468 cb
.fill
= swconfig_dump_attr
;
469 for (i
= 0; i
< alist
->n_attr
; i
++) {
470 if (alist
->attr
[i
].disabled
)
473 err
= swconfig_send_multipart(&cb
, (void *) &alist
->attr
[i
]);
479 for (i
= 0; i
< n_def
; i
++) {
480 if (!test_bit(i
, def_active
))
482 cb
.args
[0] = SWITCH_ATTR_DEFAULTS_OFFSET
+ i
;
483 err
= swconfig_send_multipart(&cb
, (void *) &def_list
[i
]);
487 swconfig_put_dev(dev
);
492 return genlmsg_reply(cb
.msg
, info
);
498 swconfig_put_dev(dev
);
502 static const struct switch_attr
*
503 swconfig_lookup_attr(struct switch_dev
*dev
, struct genl_info
*info
,
504 struct switch_val
*val
)
506 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
507 const struct switch_attrlist
*alist
;
508 const struct switch_attr
*attr
= NULL
;
512 struct switch_attr
*def_list
;
513 unsigned long *def_active
;
516 if (!info
->attrs
[SWITCH_ATTR_OP_ID
])
520 case SWITCH_CMD_SET_GLOBAL
:
521 case SWITCH_CMD_GET_GLOBAL
:
522 alist
= &dev
->ops
->attr_global
;
523 def_list
= default_global
;
524 def_active
= &dev
->def_global
;
525 n_def
= ARRAY_SIZE(default_global
);
527 case SWITCH_CMD_SET_VLAN
:
528 case SWITCH_CMD_GET_VLAN
:
529 alist
= &dev
->ops
->attr_vlan
;
530 def_list
= default_vlan
;
531 def_active
= &dev
->def_vlan
;
532 n_def
= ARRAY_SIZE(default_vlan
);
533 if (!info
->attrs
[SWITCH_ATTR_OP_VLAN
])
535 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VLAN
]);
536 if (val
->port_vlan
>= dev
->vlans
)
539 case SWITCH_CMD_SET_PORT
:
540 case SWITCH_CMD_GET_PORT
:
541 alist
= &dev
->ops
->attr_port
;
542 def_list
= default_port
;
543 def_active
= &dev
->def_port
;
544 n_def
= ARRAY_SIZE(default_port
);
545 if (!info
->attrs
[SWITCH_ATTR_OP_PORT
])
547 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_PORT
]);
548 if (val
->port_vlan
>= dev
->ports
)
559 attr_id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_ID
]);
560 if (attr_id
>= SWITCH_ATTR_DEFAULTS_OFFSET
) {
561 attr_id
-= SWITCH_ATTR_DEFAULTS_OFFSET
;
562 if (attr_id
>= n_def
)
564 if (!test_bit(attr_id
, def_active
))
566 attr
= &def_list
[attr_id
];
568 if (attr_id
>= alist
->n_attr
)
570 attr
= &alist
->attr
[attr_id
];
578 pr_debug("attribute lookup failed\n");
584 swconfig_parse_ports(struct sk_buff
*msg
, struct nlattr
*head
,
585 struct switch_val
*val
, int max
)
591 nla_for_each_nested(nla
, head
, rem
) {
592 struct nlattr
*tb
[SWITCH_PORT_ATTR_MAX
+1];
593 struct switch_port
*port
= &val
->value
.ports
[val
->len
];
598 if (nla_parse_nested(tb
, SWITCH_PORT_ATTR_MAX
, nla
,
602 if (!tb
[SWITCH_PORT_ID
])
605 port
->id
= nla_get_u32(tb
[SWITCH_PORT_ID
]);
606 if (tb
[SWITCH_PORT_FLAG_TAGGED
])
607 port
->flags
|= (1 << SWITCH_PORT_FLAG_TAGGED
);
615 swconfig_parse_link(struct sk_buff
*msg
, struct nlattr
*nla
,
616 struct switch_port_link
*link
)
618 struct nlattr
*tb
[SWITCH_LINK_ATTR_MAX
+ 1];
620 if (nla_parse_nested(tb
, SWITCH_LINK_ATTR_MAX
, nla
, link_policy
))
623 link
->duplex
= !!tb
[SWITCH_LINK_FLAG_DUPLEX
];
624 link
->aneg
= !!tb
[SWITCH_LINK_FLAG_ANEG
];
625 link
->speed
= nla_get_u32(tb
[SWITCH_LINK_SPEED
]);
631 swconfig_set_attr(struct sk_buff
*skb
, struct genl_info
*info
)
633 const struct switch_attr
*attr
;
634 struct switch_dev
*dev
;
635 struct switch_val val
;
638 dev
= swconfig_get_dev(info
);
642 memset(&val
, 0, sizeof(val
));
643 attr
= swconfig_lookup_attr(dev
, info
, &val
);
644 if (!attr
|| !attr
->set
)
648 switch (attr
->type
) {
649 case SWITCH_TYPE_NOVAL
:
651 case SWITCH_TYPE_INT
:
652 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
])
655 nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
]);
657 case SWITCH_TYPE_STRING
:
658 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
])
661 nla_data(info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
]);
663 case SWITCH_TYPE_PORTS
:
664 val
.value
.ports
= dev
->portbuf
;
665 memset(dev
->portbuf
, 0,
666 sizeof(struct switch_port
) * dev
->ports
);
668 /* TODO: implement multipart? */
669 if (info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
]) {
670 err
= swconfig_parse_ports(skb
,
671 info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
],
680 case SWITCH_TYPE_LINK
:
681 val
.value
.link
= &dev
->linkbuf
;
682 memset(&dev
->linkbuf
, 0, sizeof(struct switch_port_link
));
684 if (info
->attrs
[SWITCH_ATTR_OP_VALUE_LINK
]) {
685 err
= swconfig_parse_link(skb
,
686 info
->attrs
[SWITCH_ATTR_OP_VALUE_LINK
],
699 err
= attr
->set(dev
, attr
, &val
);
701 swconfig_put_dev(dev
);
706 swconfig_close_portlist(struct swconfig_callback
*cb
, void *arg
)
709 nla_nest_end(cb
->msg
, cb
->nest
[0]);
714 swconfig_send_port(struct swconfig_callback
*cb
, void *arg
)
716 const struct switch_port
*port
= arg
;
717 struct nlattr
*p
= NULL
;
720 cb
->nest
[0] = nla_nest_start(cb
->msg
, cb
->cmd
);
725 p
= nla_nest_start(cb
->msg
, SWITCH_ATTR_PORT
);
729 if (nla_put_u32(cb
->msg
, SWITCH_PORT_ID
, port
->id
))
730 goto nla_put_failure
;
731 if (port
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
)) {
732 if (nla_put_flag(cb
->msg
, SWITCH_PORT_FLAG_TAGGED
))
733 goto nla_put_failure
;
736 nla_nest_end(cb
->msg
, p
);
740 nla_nest_cancel(cb
->msg
, p
);
742 nla_nest_cancel(cb
->msg
, cb
->nest
[0]);
747 swconfig_send_ports(struct sk_buff
**msg
, struct genl_info
*info
, int attr
,
748 const struct switch_val
*val
)
750 struct swconfig_callback cb
;
754 if (!val
->value
.ports
)
757 memset(&cb
, 0, sizeof(cb
));
761 cb
.fill
= swconfig_send_port
;
762 cb
.close
= swconfig_close_portlist
;
764 cb
.nest
[0] = nla_nest_start(cb
.msg
, cb
.cmd
);
765 for (i
= 0; i
< val
->len
; i
++) {
766 err
= swconfig_send_multipart(&cb
, &val
->value
.ports
[i
]);
771 swconfig_close_portlist(&cb
, NULL
);
779 swconfig_send_link(struct sk_buff
*msg
, struct genl_info
*info
, int attr
,
780 const struct switch_port_link
*link
)
782 struct nlattr
*p
= NULL
;
785 p
= nla_nest_start(msg
, attr
);
787 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_LINK
))
788 goto nla_put_failure
;
791 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_DUPLEX
))
792 goto nla_put_failure
;
795 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_ANEG
))
796 goto nla_put_failure
;
799 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_TX_FLOW
))
800 goto nla_put_failure
;
803 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_RX_FLOW
))
804 goto nla_put_failure
;
806 if (nla_put_u32(msg
, SWITCH_LINK_SPEED
, link
->speed
))
807 goto nla_put_failure
;
808 if (link
->eee
& ADVERTISED_100baseT_Full
) {
809 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_EEE_100BASET
))
810 goto nla_put_failure
;
812 if (link
->eee
& ADVERTISED_1000baseT_Full
) {
813 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_EEE_1000BASET
))
814 goto nla_put_failure
;
816 nla_nest_end(msg
, p
);
821 nla_nest_cancel(msg
, p
);
826 swconfig_get_attr(struct sk_buff
*skb
, struct genl_info
*info
)
828 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
829 const struct switch_attr
*attr
;
830 struct switch_dev
*dev
;
831 struct sk_buff
*msg
= NULL
;
832 struct switch_val val
;
836 dev
= swconfig_get_dev(info
);
840 memset(&val
, 0, sizeof(val
));
841 attr
= swconfig_lookup_attr(dev
, info
, &val
);
842 if (!attr
|| !attr
->get
)
845 if (attr
->type
== SWITCH_TYPE_PORTS
) {
846 val
.value
.ports
= dev
->portbuf
;
847 memset(dev
->portbuf
, 0,
848 sizeof(struct switch_port
) * dev
->ports
);
849 } else if (attr
->type
== SWITCH_TYPE_LINK
) {
850 val
.value
.link
= &dev
->linkbuf
;
851 memset(&dev
->linkbuf
, 0, sizeof(struct switch_port_link
));
854 err
= attr
->get(dev
, attr
, &val
);
858 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
862 hdr
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
, &switch_fam
,
865 goto nla_put_failure
;
867 switch (attr
->type
) {
868 case SWITCH_TYPE_INT
:
869 if (nla_put_u32(msg
, SWITCH_ATTR_OP_VALUE_INT
, val
.value
.i
))
870 goto nla_put_failure
;
872 case SWITCH_TYPE_STRING
:
873 if (nla_put_string(msg
, SWITCH_ATTR_OP_VALUE_STR
, val
.value
.s
))
874 goto nla_put_failure
;
876 case SWITCH_TYPE_PORTS
:
877 err
= swconfig_send_ports(&msg
, info
,
878 SWITCH_ATTR_OP_VALUE_PORTS
, &val
);
880 goto nla_put_failure
;
882 case SWITCH_TYPE_LINK
:
883 err
= swconfig_send_link(msg
, info
,
884 SWITCH_ATTR_OP_VALUE_LINK
, val
.value
.link
);
886 goto nla_put_failure
;
889 pr_debug("invalid type in attribute\n");
893 genlmsg_end(msg
, hdr
);
896 goto nla_put_failure
;
898 swconfig_put_dev(dev
);
899 return genlmsg_reply(msg
, info
);
905 swconfig_put_dev(dev
);
912 swconfig_send_switch(struct sk_buff
*msg
, u32 pid
, u32 seq
, int flags
,
913 const struct switch_dev
*dev
)
915 struct nlattr
*p
= NULL
, *m
= NULL
;
919 hdr
= genlmsg_put(msg
, pid
, seq
, &switch_fam
, flags
,
920 SWITCH_CMD_NEW_ATTR
);
924 if (nla_put_u32(msg
, SWITCH_ATTR_ID
, dev
->id
))
925 goto nla_put_failure
;
926 if (nla_put_string(msg
, SWITCH_ATTR_DEV_NAME
, dev
->devname
))
927 goto nla_put_failure
;
928 if (nla_put_string(msg
, SWITCH_ATTR_ALIAS
, dev
->alias
))
929 goto nla_put_failure
;
930 if (nla_put_string(msg
, SWITCH_ATTR_NAME
, dev
->name
))
931 goto nla_put_failure
;
932 if (nla_put_u32(msg
, SWITCH_ATTR_VLANS
, dev
->vlans
))
933 goto nla_put_failure
;
934 if (nla_put_u32(msg
, SWITCH_ATTR_PORTS
, dev
->ports
))
935 goto nla_put_failure
;
936 if (nla_put_u32(msg
, SWITCH_ATTR_CPU_PORT
, dev
->cpu_port
))
937 goto nla_put_failure
;
939 m
= nla_nest_start(msg
, SWITCH_ATTR_PORTMAP
);
941 goto nla_put_failure
;
942 for (i
= 0; i
< dev
->ports
; i
++) {
943 p
= nla_nest_start(msg
, SWITCH_ATTR_PORTS
);
946 if (dev
->portmap
[i
].s
) {
947 if (nla_put_string(msg
, SWITCH_PORTMAP_SEGMENT
,
949 goto nla_put_failure
;
950 if (nla_put_u32(msg
, SWITCH_PORTMAP_VIRT
,
951 dev
->portmap
[i
].virt
))
952 goto nla_put_failure
;
954 nla_nest_end(msg
, p
);
956 nla_nest_end(msg
, m
);
957 genlmsg_end(msg
, hdr
);
960 genlmsg_cancel(msg
, hdr
);
964 static int swconfig_dump_switches(struct sk_buff
*skb
,
965 struct netlink_callback
*cb
)
967 struct switch_dev
*dev
;
968 int start
= cb
->args
[0];
972 list_for_each_entry(dev
, &swdevs
, dev_list
) {
975 if (swconfig_send_switch(skb
, NETLINK_CB(cb
->skb
).portid
,
976 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
987 swconfig_done(struct netlink_callback
*cb
)
992 static struct genl_ops swconfig_ops
[] = {
994 .cmd
= SWITCH_CMD_LIST_GLOBAL
,
995 .doit
= swconfig_list_attrs
,
996 .policy
= switch_policy
,
999 .cmd
= SWITCH_CMD_LIST_VLAN
,
1000 .doit
= swconfig_list_attrs
,
1001 .policy
= switch_policy
,
1004 .cmd
= SWITCH_CMD_LIST_PORT
,
1005 .doit
= swconfig_list_attrs
,
1006 .policy
= switch_policy
,
1009 .cmd
= SWITCH_CMD_GET_GLOBAL
,
1010 .doit
= swconfig_get_attr
,
1011 .policy
= switch_policy
,
1014 .cmd
= SWITCH_CMD_GET_VLAN
,
1015 .doit
= swconfig_get_attr
,
1016 .policy
= switch_policy
,
1019 .cmd
= SWITCH_CMD_GET_PORT
,
1020 .doit
= swconfig_get_attr
,
1021 .policy
= switch_policy
,
1024 .cmd
= SWITCH_CMD_SET_GLOBAL
,
1025 .doit
= swconfig_set_attr
,
1026 .policy
= switch_policy
,
1029 .cmd
= SWITCH_CMD_SET_VLAN
,
1030 .doit
= swconfig_set_attr
,
1031 .policy
= switch_policy
,
1034 .cmd
= SWITCH_CMD_SET_PORT
,
1035 .doit
= swconfig_set_attr
,
1036 .policy
= switch_policy
,
1039 .cmd
= SWITCH_CMD_GET_SWITCH
,
1040 .dumpit
= swconfig_dump_switches
,
1041 .policy
= switch_policy
,
1042 .done
= swconfig_done
,
1048 of_switch_load_portmap(struct switch_dev
*dev
)
1050 struct device_node
*port
;
1055 for_each_child_of_node(dev
->of_node
, port
) {
1057 const char *segment
;
1060 if (!of_device_is_compatible(port
, "swconfig,port"))
1063 if (of_property_read_string(port
, "swconfig,segment", &segment
))
1066 prop
= of_get_property(port
, "swconfig,portmap", &size
);
1070 if (size
!= (2 * sizeof(*prop
))) {
1071 pr_err("%s: failed to parse port mapping\n",
1076 phys
= be32_to_cpup(prop
++);
1077 if ((phys
< 0) | (phys
>= dev
->ports
)) {
1078 pr_err("%s: physical port index out of range\n",
1083 dev
->portmap
[phys
].s
= kstrdup(segment
, GFP_KERNEL
);
1084 dev
->portmap
[phys
].virt
= be32_to_cpup(prop
);
1085 pr_debug("Found port: %s, physical: %d, virtual: %d\n",
1086 segment
, phys
, dev
->portmap
[phys
].virt
);
1092 register_switch(struct switch_dev
*dev
, struct net_device
*netdev
)
1094 struct switch_dev
*sdev
;
1095 const int max_switches
= 8 * sizeof(unsigned long);
1096 unsigned long in_use
= 0;
1100 INIT_LIST_HEAD(&dev
->dev_list
);
1102 dev
->netdev
= netdev
;
1104 dev
->alias
= netdev
->name
;
1106 BUG_ON(!dev
->alias
);
1108 if (dev
->ports
> 0) {
1109 dev
->portbuf
= kzalloc(sizeof(struct switch_port
) *
1110 dev
->ports
, GFP_KERNEL
);
1113 dev
->portmap
= kzalloc(sizeof(struct switch_portmap
) *
1114 dev
->ports
, GFP_KERNEL
);
1115 if (!dev
->portmap
) {
1116 kfree(dev
->portbuf
);
1120 swconfig_defaults_init(dev
);
1121 mutex_init(&dev
->sw_mutex
);
1123 dev
->id
= ++swdev_id
;
1125 list_for_each_entry(sdev
, &swdevs
, dev_list
) {
1126 if (!sscanf(sdev
->devname
, SWCONFIG_DEVNAME
, &i
))
1128 if (i
< 0 || i
> max_switches
)
1131 set_bit(i
, &in_use
);
1133 i
= find_first_zero_bit(&in_use
, max_switches
);
1135 if (i
== max_switches
) {
1142 of_switch_load_portmap(dev
);
1145 /* fill device name */
1146 snprintf(dev
->devname
, IFNAMSIZ
, SWCONFIG_DEVNAME
, i
);
1148 list_add_tail(&dev
->dev_list
, &swdevs
);
1151 err
= swconfig_create_led_trigger(dev
);
1157 EXPORT_SYMBOL_GPL(register_switch
);
1160 unregister_switch(struct switch_dev
*dev
)
1162 swconfig_destroy_led_trigger(dev
);
1163 kfree(dev
->portbuf
);
1164 mutex_lock(&dev
->sw_mutex
);
1166 list_del(&dev
->dev_list
);
1168 mutex_unlock(&dev
->sw_mutex
);
1170 EXPORT_SYMBOL_GPL(unregister_switch
);
1173 switch_generic_set_link(struct switch_dev
*dev
, int port
,
1174 struct switch_port_link
*link
)
1176 if (WARN_ON(!dev
->ops
->phy_write16
))
1179 /* Generic implementation */
1181 dev
->ops
->phy_write16(dev
, port
, MII_BMCR
, 0x0000);
1182 dev
->ops
->phy_write16(dev
, port
, MII_BMCR
, BMCR_ANENABLE
| BMCR_ANRESTART
);
1187 bmcr
|= BMCR_FULLDPLX
;
1189 switch (link
->speed
) {
1190 case SWITCH_PORT_SPEED_10
:
1192 case SWITCH_PORT_SPEED_100
:
1193 bmcr
|= BMCR_SPEED100
;
1195 case SWITCH_PORT_SPEED_1000
:
1196 bmcr
|= BMCR_SPEED1000
;
1202 dev
->ops
->phy_write16(dev
, port
, MII_BMCR
, bmcr
);
1211 INIT_LIST_HEAD(&swdevs
);
1213 return genl_register_family_with_ops(&switch_fam
, swconfig_ops
);
1219 genl_unregister_family(&switch_fam
);
1222 module_init(swconfig_init
);
1223 module_exit(swconfig_exit
);