swconfig: add API for setting port link speed
[openwrt/openwrt.git] / target / linux / generic / files / drivers / net / phy / swconfig.c
1 /*
2 * swconfig.c: Switch configuration API
3 *
4 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
5 *
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.
10 *
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.
15 */
16
17 #include <linux/types.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/list.h>
21 #include <linux/if.h>
22 #include <linux/if_ether.h>
23 #include <linux/capability.h>
24 #include <linux/skbuff.h>
25 #include <linux/switch.h>
26 #include <linux/of.h>
27 #include <linux/version.h>
28
29 #define SWCONFIG_DEVNAME "switch%d"
30
31 #include "swconfig_leds.c"
32
33 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
34 MODULE_LICENSE("GPL");
35
36 static int swdev_id;
37 static struct list_head swdevs;
38 static DEFINE_SPINLOCK(swdevs_lock);
39 struct swconfig_callback;
40
41 struct swconfig_callback {
42 struct sk_buff *msg;
43 struct genlmsghdr *hdr;
44 struct genl_info *info;
45 int cmd;
46
47 /* callback for filling in the message data */
48 int (*fill)(struct swconfig_callback *cb, void *arg);
49
50 /* callback for closing the message before sending it */
51 int (*close)(struct swconfig_callback *cb, void *arg);
52
53 struct nlattr *nest[4];
54 int args[4];
55 };
56
57 /* defaults */
58
59 static int
60 swconfig_get_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
61 struct switch_val *val)
62 {
63 int ret;
64 if (val->port_vlan >= dev->vlans)
65 return -EINVAL;
66
67 if (!dev->ops->get_vlan_ports)
68 return -EOPNOTSUPP;
69
70 ret = dev->ops->get_vlan_ports(dev, val);
71 return ret;
72 }
73
74 static int
75 swconfig_set_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
76 struct switch_val *val)
77 {
78 struct switch_port *ports = val->value.ports;
79 const struct switch_dev_ops *ops = dev->ops;
80 int i;
81
82 if (val->port_vlan >= dev->vlans)
83 return -EINVAL;
84
85 /* validate ports */
86 if (val->len > dev->ports)
87 return -EINVAL;
88
89 if (!ops->set_vlan_ports)
90 return -EOPNOTSUPP;
91
92 for (i = 0; i < val->len; i++) {
93 if (ports[i].id >= dev->ports)
94 return -EINVAL;
95
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);
99 }
100
101 return ops->set_vlan_ports(dev, val);
102 }
103
104 static int
105 swconfig_set_pvid(struct switch_dev *dev, const struct switch_attr *attr,
106 struct switch_val *val)
107 {
108 if (val->port_vlan >= dev->ports)
109 return -EINVAL;
110
111 if (!dev->ops->set_port_pvid)
112 return -EOPNOTSUPP;
113
114 return dev->ops->set_port_pvid(dev, val->port_vlan, val->value.i);
115 }
116
117 static int
118 swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr,
119 struct switch_val *val)
120 {
121 if (val->port_vlan >= dev->ports)
122 return -EINVAL;
123
124 if (!dev->ops->get_port_pvid)
125 return -EOPNOTSUPP;
126
127 return dev->ops->get_port_pvid(dev, val->port_vlan, &val->value.i);
128 }
129
130 static int
131 swconfig_set_link(struct switch_dev *dev, const struct switch_attr *attr,
132 struct switch_val *val)
133 {
134 if (!dev->ops->set_port_link)
135 return -EOPNOTSUPP;
136
137 return dev->ops->set_port_link(dev, val->port_vlan, val->value.link);
138 }
139
140 static int
141 swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr,
142 struct switch_val *val)
143 {
144 struct switch_port_link *link = val->value.link;
145
146 if (val->port_vlan >= dev->ports)
147 return -EINVAL;
148
149 if (!dev->ops->get_port_link)
150 return -EOPNOTSUPP;
151
152 memset(link, 0, sizeof(*link));
153 return dev->ops->get_port_link(dev, val->port_vlan, link);
154 }
155
156 static int
157 swconfig_apply_config(struct switch_dev *dev, const struct switch_attr *attr,
158 struct switch_val *val)
159 {
160 /* don't complain if not supported by the switch driver */
161 if (!dev->ops->apply_config)
162 return 0;
163
164 return dev->ops->apply_config(dev);
165 }
166
167 static int
168 swconfig_reset_switch(struct switch_dev *dev, const struct switch_attr *attr,
169 struct switch_val *val)
170 {
171 /* don't complain if not supported by the switch driver */
172 if (!dev->ops->reset_switch)
173 return 0;
174
175 return dev->ops->reset_switch(dev);
176 }
177
178 enum global_defaults {
179 GLOBAL_APPLY,
180 GLOBAL_RESET,
181 };
182
183 enum vlan_defaults {
184 VLAN_PORTS,
185 };
186
187 enum port_defaults {
188 PORT_PVID,
189 PORT_LINK,
190 };
191
192 static struct switch_attr default_global[] = {
193 [GLOBAL_APPLY] = {
194 .type = SWITCH_TYPE_NOVAL,
195 .name = "apply",
196 .description = "Activate changes in the hardware",
197 .set = swconfig_apply_config,
198 },
199 [GLOBAL_RESET] = {
200 .type = SWITCH_TYPE_NOVAL,
201 .name = "reset",
202 .description = "Reset the switch",
203 .set = swconfig_reset_switch,
204 }
205 };
206
207 static struct switch_attr default_port[] = {
208 [PORT_PVID] = {
209 .type = SWITCH_TYPE_INT,
210 .name = "pvid",
211 .description = "Primary VLAN ID",
212 .set = swconfig_set_pvid,
213 .get = swconfig_get_pvid,
214 },
215 [PORT_LINK] = {
216 .type = SWITCH_TYPE_LINK,
217 .name = "link",
218 .description = "Get port link information",
219 .set = swconfig_set_link,
220 .get = swconfig_get_link,
221 }
222 };
223
224 static struct switch_attr default_vlan[] = {
225 [VLAN_PORTS] = {
226 .type = SWITCH_TYPE_PORTS,
227 .name = "ports",
228 .description = "VLAN port mapping",
229 .set = swconfig_set_vlan_ports,
230 .get = swconfig_get_vlan_ports,
231 },
232 };
233
234 static const struct switch_attr *
235 swconfig_find_attr_by_name(const struct switch_attrlist *alist,
236 const char *name)
237 {
238 int i;
239
240 for (i = 0; i < alist->n_attr; i++)
241 if (strcmp(name, alist->attr[i].name) == 0)
242 return &alist->attr[i];
243
244 return NULL;
245 }
246
247 static void swconfig_defaults_init(struct switch_dev *dev)
248 {
249 const struct switch_dev_ops *ops = dev->ops;
250
251 dev->def_global = 0;
252 dev->def_vlan = 0;
253 dev->def_port = 0;
254
255 if (ops->get_vlan_ports || ops->set_vlan_ports)
256 set_bit(VLAN_PORTS, &dev->def_vlan);
257
258 if (ops->get_port_pvid || ops->set_port_pvid)
259 set_bit(PORT_PVID, &dev->def_port);
260
261 if (ops->get_port_link &&
262 !swconfig_find_attr_by_name(&ops->attr_port, "link"))
263 set_bit(PORT_LINK, &dev->def_port);
264
265 /* always present, can be no-op */
266 set_bit(GLOBAL_APPLY, &dev->def_global);
267 set_bit(GLOBAL_RESET, &dev->def_global);
268 }
269
270
271 static struct genl_family switch_fam = {
272 .id = GENL_ID_GENERATE,
273 .name = "switch",
274 .hdrsize = 0,
275 .version = 1,
276 .maxattr = SWITCH_ATTR_MAX,
277 };
278
279 static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
280 [SWITCH_ATTR_ID] = { .type = NLA_U32 },
281 [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 },
282 [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 },
283 [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 },
284 [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 },
285 [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING },
286 [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED },
287 [SWITCH_ATTR_TYPE] = { .type = NLA_U32 },
288 };
289
290 static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
291 [SWITCH_PORT_ID] = { .type = NLA_U32 },
292 [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
293 };
294
295 static struct nla_policy link_policy[SWITCH_LINK_ATTR_MAX] = {
296 [SWITCH_LINK_FLAG_DUPLEX] = { .type = NLA_FLAG },
297 [SWITCH_LINK_FLAG_ANEG] = { .type = NLA_FLAG },
298 [SWITCH_LINK_SPEED] = { .type = NLA_U32 },
299 };
300
301 static inline void
302 swconfig_lock(void)
303 {
304 spin_lock(&swdevs_lock);
305 }
306
307 static inline void
308 swconfig_unlock(void)
309 {
310 spin_unlock(&swdevs_lock);
311 }
312
313 static struct switch_dev *
314 swconfig_get_dev(struct genl_info *info)
315 {
316 struct switch_dev *dev = NULL;
317 struct switch_dev *p;
318 int id;
319
320 if (!info->attrs[SWITCH_ATTR_ID])
321 goto done;
322
323 id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]);
324 swconfig_lock();
325 list_for_each_entry(p, &swdevs, dev_list) {
326 if (id != p->id)
327 continue;
328
329 dev = p;
330 break;
331 }
332 if (dev)
333 mutex_lock(&dev->sw_mutex);
334 else
335 pr_debug("device %d not found\n", id);
336 swconfig_unlock();
337 done:
338 return dev;
339 }
340
341 static inline void
342 swconfig_put_dev(struct switch_dev *dev)
343 {
344 mutex_unlock(&dev->sw_mutex);
345 }
346
347 static int
348 swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
349 {
350 struct switch_attr *op = arg;
351 struct genl_info *info = cb->info;
352 struct sk_buff *msg = cb->msg;
353 int id = cb->args[0];
354 void *hdr;
355
356 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam,
357 NLM_F_MULTI, SWITCH_CMD_NEW_ATTR);
358 if (IS_ERR(hdr))
359 return -1;
360
361 if (nla_put_u32(msg, SWITCH_ATTR_OP_ID, id))
362 goto nla_put_failure;
363 if (nla_put_u32(msg, SWITCH_ATTR_OP_TYPE, op->type))
364 goto nla_put_failure;
365 if (nla_put_string(msg, SWITCH_ATTR_OP_NAME, op->name))
366 goto nla_put_failure;
367 if (op->description)
368 if (nla_put_string(msg, SWITCH_ATTR_OP_DESCRIPTION,
369 op->description))
370 goto nla_put_failure;
371
372 genlmsg_end(msg, hdr);
373 return msg->len;
374 nla_put_failure:
375 genlmsg_cancel(msg, hdr);
376 return -EMSGSIZE;
377 }
378
379 /* spread multipart messages across multiple message buffers */
380 static int
381 swconfig_send_multipart(struct swconfig_callback *cb, void *arg)
382 {
383 struct genl_info *info = cb->info;
384 int restart = 0;
385 int err;
386
387 do {
388 if (!cb->msg) {
389 cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
390 if (cb->msg == NULL)
391 goto error;
392 }
393
394 if (!(cb->fill(cb, arg) < 0))
395 break;
396
397 /* fill failed, check if this was already the second attempt */
398 if (restart)
399 goto error;
400
401 /* try again in a new message, send the current one */
402 restart = 1;
403 if (cb->close) {
404 if (cb->close(cb, arg) < 0)
405 goto error;
406 }
407 err = genlmsg_reply(cb->msg, info);
408 cb->msg = NULL;
409 if (err < 0)
410 goto error;
411
412 } while (restart);
413
414 return 0;
415
416 error:
417 if (cb->msg)
418 nlmsg_free(cb->msg);
419 return -1;
420 }
421
422 static int
423 swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
424 {
425 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
426 const struct switch_attrlist *alist;
427 struct switch_dev *dev;
428 struct swconfig_callback cb;
429 int err = -EINVAL;
430 int i;
431
432 /* defaults */
433 struct switch_attr *def_list;
434 unsigned long *def_active;
435 int n_def;
436
437 dev = swconfig_get_dev(info);
438 if (!dev)
439 return -EINVAL;
440
441 switch (hdr->cmd) {
442 case SWITCH_CMD_LIST_GLOBAL:
443 alist = &dev->ops->attr_global;
444 def_list = default_global;
445 def_active = &dev->def_global;
446 n_def = ARRAY_SIZE(default_global);
447 break;
448 case SWITCH_CMD_LIST_VLAN:
449 alist = &dev->ops->attr_vlan;
450 def_list = default_vlan;
451 def_active = &dev->def_vlan;
452 n_def = ARRAY_SIZE(default_vlan);
453 break;
454 case SWITCH_CMD_LIST_PORT:
455 alist = &dev->ops->attr_port;
456 def_list = default_port;
457 def_active = &dev->def_port;
458 n_def = ARRAY_SIZE(default_port);
459 break;
460 default:
461 WARN_ON(1);
462 goto out;
463 }
464
465 memset(&cb, 0, sizeof(cb));
466 cb.info = info;
467 cb.fill = swconfig_dump_attr;
468 for (i = 0; i < alist->n_attr; i++) {
469 if (alist->attr[i].disabled)
470 continue;
471 cb.args[0] = i;
472 err = swconfig_send_multipart(&cb, (void *) &alist->attr[i]);
473 if (err < 0)
474 goto error;
475 }
476
477 /* defaults */
478 for (i = 0; i < n_def; i++) {
479 if (!test_bit(i, def_active))
480 continue;
481 cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i;
482 err = swconfig_send_multipart(&cb, (void *) &def_list[i]);
483 if (err < 0)
484 goto error;
485 }
486 swconfig_put_dev(dev);
487
488 if (!cb.msg)
489 return 0;
490
491 return genlmsg_reply(cb.msg, info);
492
493 error:
494 if (cb.msg)
495 nlmsg_free(cb.msg);
496 out:
497 swconfig_put_dev(dev);
498 return err;
499 }
500
501 static const struct switch_attr *
502 swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
503 struct switch_val *val)
504 {
505 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
506 const struct switch_attrlist *alist;
507 const struct switch_attr *attr = NULL;
508 int attr_id;
509
510 /* defaults */
511 struct switch_attr *def_list;
512 unsigned long *def_active;
513 int n_def;
514
515 if (!info->attrs[SWITCH_ATTR_OP_ID])
516 goto done;
517
518 switch (hdr->cmd) {
519 case SWITCH_CMD_SET_GLOBAL:
520 case SWITCH_CMD_GET_GLOBAL:
521 alist = &dev->ops->attr_global;
522 def_list = default_global;
523 def_active = &dev->def_global;
524 n_def = ARRAY_SIZE(default_global);
525 break;
526 case SWITCH_CMD_SET_VLAN:
527 case SWITCH_CMD_GET_VLAN:
528 alist = &dev->ops->attr_vlan;
529 def_list = default_vlan;
530 def_active = &dev->def_vlan;
531 n_def = ARRAY_SIZE(default_vlan);
532 if (!info->attrs[SWITCH_ATTR_OP_VLAN])
533 goto done;
534 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]);
535 if (val->port_vlan >= dev->vlans)
536 goto done;
537 break;
538 case SWITCH_CMD_SET_PORT:
539 case SWITCH_CMD_GET_PORT:
540 alist = &dev->ops->attr_port;
541 def_list = default_port;
542 def_active = &dev->def_port;
543 n_def = ARRAY_SIZE(default_port);
544 if (!info->attrs[SWITCH_ATTR_OP_PORT])
545 goto done;
546 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
547 if (val->port_vlan >= dev->ports)
548 goto done;
549 break;
550 default:
551 WARN_ON(1);
552 goto done;
553 }
554
555 if (!alist)
556 goto done;
557
558 attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
559 if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
560 attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
561 if (attr_id >= n_def)
562 goto done;
563 if (!test_bit(attr_id, def_active))
564 goto done;
565 attr = &def_list[attr_id];
566 } else {
567 if (attr_id >= alist->n_attr)
568 goto done;
569 attr = &alist->attr[attr_id];
570 }
571
572 if (attr->disabled)
573 attr = NULL;
574
575 done:
576 if (!attr)
577 pr_debug("attribute lookup failed\n");
578 val->attr = attr;
579 return attr;
580 }
581
582 static int
583 swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
584 struct switch_val *val, int max)
585 {
586 struct nlattr *nla;
587 int rem;
588
589 val->len = 0;
590 nla_for_each_nested(nla, head, rem) {
591 struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
592 struct switch_port *port = &val->value.ports[val->len];
593
594 if (val->len >= max)
595 return -EINVAL;
596
597 if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
598 port_policy))
599 return -EINVAL;
600
601 if (!tb[SWITCH_PORT_ID])
602 return -EINVAL;
603
604 port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
605 if (tb[SWITCH_PORT_FLAG_TAGGED])
606 port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
607 val->len++;
608 }
609
610 return 0;
611 }
612
613 static int
614 swconfig_parse_link(struct sk_buff *msg, struct nlattr *nla,
615 struct switch_port_link *link)
616 {
617 struct nlattr *tb[SWITCH_LINK_ATTR_MAX + 1];
618
619 if (nla_parse_nested(tb, SWITCH_LINK_ATTR_MAX, nla, link_policy))
620 return -EINVAL;
621
622 link->duplex = !!tb[SWITCH_LINK_FLAG_DUPLEX];
623 link->aneg = !!tb[SWITCH_LINK_FLAG_ANEG];
624 link->speed = nla_get_u32(tb[SWITCH_LINK_SPEED]);
625
626 return 0;
627 }
628
629 static int
630 swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
631 {
632 const struct switch_attr *attr;
633 struct switch_dev *dev;
634 struct switch_val val;
635 int err = -EINVAL;
636
637 dev = swconfig_get_dev(info);
638 if (!dev)
639 return -EINVAL;
640
641 memset(&val, 0, sizeof(val));
642 attr = swconfig_lookup_attr(dev, info, &val);
643 if (!attr || !attr->set)
644 goto error;
645
646 val.attr = attr;
647 switch (attr->type) {
648 case SWITCH_TYPE_NOVAL:
649 break;
650 case SWITCH_TYPE_INT:
651 if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
652 goto error;
653 val.value.i =
654 nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
655 break;
656 case SWITCH_TYPE_STRING:
657 if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
658 goto error;
659 val.value.s =
660 nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
661 break;
662 case SWITCH_TYPE_PORTS:
663 val.value.ports = dev->portbuf;
664 memset(dev->portbuf, 0,
665 sizeof(struct switch_port) * dev->ports);
666
667 /* TODO: implement multipart? */
668 if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
669 err = swconfig_parse_ports(skb,
670 info->attrs[SWITCH_ATTR_OP_VALUE_PORTS],
671 &val, dev->ports);
672 if (err < 0)
673 goto error;
674 } else {
675 val.len = 0;
676 err = 0;
677 }
678 break;
679 case SWITCH_TYPE_LINK:
680 val.value.link = &dev->linkbuf;
681 memset(&dev->linkbuf, 0, sizeof(struct switch_port_link));
682
683 if (info->attrs[SWITCH_ATTR_OP_VALUE_LINK]) {
684 err = swconfig_parse_link(skb,
685 info->attrs[SWITCH_ATTR_OP_VALUE_LINK],
686 val.value.link);
687 if (err < 0)
688 goto error;
689 } else {
690 val.len = 0;
691 err = 0;
692 }
693 break;
694 default:
695 goto error;
696 }
697
698 err = attr->set(dev, attr, &val);
699 error:
700 swconfig_put_dev(dev);
701 return err;
702 }
703
704 static int
705 swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
706 {
707 if (cb->nest[0])
708 nla_nest_end(cb->msg, cb->nest[0]);
709 return 0;
710 }
711
712 static int
713 swconfig_send_port(struct swconfig_callback *cb, void *arg)
714 {
715 const struct switch_port *port = arg;
716 struct nlattr *p = NULL;
717
718 if (!cb->nest[0]) {
719 cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
720 if (!cb->nest[0])
721 return -1;
722 }
723
724 p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
725 if (!p)
726 goto error;
727
728 if (nla_put_u32(cb->msg, SWITCH_PORT_ID, port->id))
729 goto nla_put_failure;
730 if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
731 if (nla_put_flag(cb->msg, SWITCH_PORT_FLAG_TAGGED))
732 goto nla_put_failure;
733 }
734
735 nla_nest_end(cb->msg, p);
736 return 0;
737
738 nla_put_failure:
739 nla_nest_cancel(cb->msg, p);
740 error:
741 nla_nest_cancel(cb->msg, cb->nest[0]);
742 return -1;
743 }
744
745 static int
746 swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
747 const struct switch_val *val)
748 {
749 struct swconfig_callback cb;
750 int err = 0;
751 int i;
752
753 if (!val->value.ports)
754 return -EINVAL;
755
756 memset(&cb, 0, sizeof(cb));
757 cb.cmd = attr;
758 cb.msg = *msg;
759 cb.info = info;
760 cb.fill = swconfig_send_port;
761 cb.close = swconfig_close_portlist;
762
763 cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
764 for (i = 0; i < val->len; i++) {
765 err = swconfig_send_multipart(&cb, &val->value.ports[i]);
766 if (err)
767 goto done;
768 }
769 err = val->len;
770 swconfig_close_portlist(&cb, NULL);
771 *msg = cb.msg;
772
773 done:
774 return err;
775 }
776
777 static int
778 swconfig_send_link(struct sk_buff *msg, struct genl_info *info, int attr,
779 const struct switch_port_link *link)
780 {
781 struct nlattr *p = NULL;
782 int err = 0;
783
784 p = nla_nest_start(msg, attr);
785 if (link->link) {
786 if (nla_put_flag(msg, SWITCH_LINK_FLAG_LINK))
787 goto nla_put_failure;
788 }
789 if (link->duplex) {
790 if (nla_put_flag(msg, SWITCH_LINK_FLAG_DUPLEX))
791 goto nla_put_failure;
792 }
793 if (link->aneg) {
794 if (nla_put_flag(msg, SWITCH_LINK_FLAG_ANEG))
795 goto nla_put_failure;
796 }
797 if (link->tx_flow) {
798 if (nla_put_flag(msg, SWITCH_LINK_FLAG_TX_FLOW))
799 goto nla_put_failure;
800 }
801 if (link->rx_flow) {
802 if (nla_put_flag(msg, SWITCH_LINK_FLAG_RX_FLOW))
803 goto nla_put_failure;
804 }
805 if (nla_put_u32(msg, SWITCH_LINK_SPEED, link->speed))
806 goto nla_put_failure;
807 if (link->eee & ADVERTISED_100baseT_Full) {
808 if (nla_put_flag(msg, SWITCH_LINK_FLAG_EEE_100BASET))
809 goto nla_put_failure;
810 }
811 if (link->eee & ADVERTISED_1000baseT_Full) {
812 if (nla_put_flag(msg, SWITCH_LINK_FLAG_EEE_1000BASET))
813 goto nla_put_failure;
814 }
815 nla_nest_end(msg, p);
816
817 return err;
818
819 nla_put_failure:
820 nla_nest_cancel(msg, p);
821 return -1;
822 }
823
824 static int
825 swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
826 {
827 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
828 const struct switch_attr *attr;
829 struct switch_dev *dev;
830 struct sk_buff *msg = NULL;
831 struct switch_val val;
832 int err = -EINVAL;
833 int cmd = hdr->cmd;
834
835 dev = swconfig_get_dev(info);
836 if (!dev)
837 return -EINVAL;
838
839 memset(&val, 0, sizeof(val));
840 attr = swconfig_lookup_attr(dev, info, &val);
841 if (!attr || !attr->get)
842 goto error;
843
844 if (attr->type == SWITCH_TYPE_PORTS) {
845 val.value.ports = dev->portbuf;
846 memset(dev->portbuf, 0,
847 sizeof(struct switch_port) * dev->ports);
848 } else if (attr->type == SWITCH_TYPE_LINK) {
849 val.value.link = &dev->linkbuf;
850 memset(&dev->linkbuf, 0, sizeof(struct switch_port_link));
851 }
852
853 err = attr->get(dev, attr, &val);
854 if (err)
855 goto error;
856
857 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
858 if (!msg)
859 goto error;
860
861 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam,
862 0, cmd);
863 if (IS_ERR(hdr))
864 goto nla_put_failure;
865
866 switch (attr->type) {
867 case SWITCH_TYPE_INT:
868 if (nla_put_u32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i))
869 goto nla_put_failure;
870 break;
871 case SWITCH_TYPE_STRING:
872 if (nla_put_string(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s))
873 goto nla_put_failure;
874 break;
875 case SWITCH_TYPE_PORTS:
876 err = swconfig_send_ports(&msg, info,
877 SWITCH_ATTR_OP_VALUE_PORTS, &val);
878 if (err < 0)
879 goto nla_put_failure;
880 break;
881 case SWITCH_TYPE_LINK:
882 err = swconfig_send_link(msg, info,
883 SWITCH_ATTR_OP_VALUE_LINK, val.value.link);
884 if (err < 0)
885 goto nla_put_failure;
886 break;
887 default:
888 pr_debug("invalid type in attribute\n");
889 err = -EINVAL;
890 goto error;
891 }
892 genlmsg_end(msg, hdr);
893 err = msg->len;
894 if (err < 0)
895 goto nla_put_failure;
896
897 swconfig_put_dev(dev);
898 return genlmsg_reply(msg, info);
899
900 nla_put_failure:
901 if (msg)
902 nlmsg_free(msg);
903 error:
904 swconfig_put_dev(dev);
905 if (!err)
906 err = -ENOMEM;
907 return err;
908 }
909
910 static int
911 swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
912 const struct switch_dev *dev)
913 {
914 struct nlattr *p = NULL, *m = NULL;
915 void *hdr;
916 int i;
917
918 hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
919 SWITCH_CMD_NEW_ATTR);
920 if (IS_ERR(hdr))
921 return -1;
922
923 if (nla_put_u32(msg, SWITCH_ATTR_ID, dev->id))
924 goto nla_put_failure;
925 if (nla_put_string(msg, SWITCH_ATTR_DEV_NAME, dev->devname))
926 goto nla_put_failure;
927 if (nla_put_string(msg, SWITCH_ATTR_ALIAS, dev->alias))
928 goto nla_put_failure;
929 if (nla_put_string(msg, SWITCH_ATTR_NAME, dev->name))
930 goto nla_put_failure;
931 if (nla_put_u32(msg, SWITCH_ATTR_VLANS, dev->vlans))
932 goto nla_put_failure;
933 if (nla_put_u32(msg, SWITCH_ATTR_PORTS, dev->ports))
934 goto nla_put_failure;
935 if (nla_put_u32(msg, SWITCH_ATTR_CPU_PORT, dev->cpu_port))
936 goto nla_put_failure;
937
938 m = nla_nest_start(msg, SWITCH_ATTR_PORTMAP);
939 if (!m)
940 goto nla_put_failure;
941 for (i = 0; i < dev->ports; i++) {
942 p = nla_nest_start(msg, SWITCH_ATTR_PORTS);
943 if (!p)
944 continue;
945 if (dev->portmap[i].s) {
946 if (nla_put_string(msg, SWITCH_PORTMAP_SEGMENT,
947 dev->portmap[i].s))
948 goto nla_put_failure;
949 if (nla_put_u32(msg, SWITCH_PORTMAP_VIRT,
950 dev->portmap[i].virt))
951 goto nla_put_failure;
952 }
953 nla_nest_end(msg, p);
954 }
955 nla_nest_end(msg, m);
956 genlmsg_end(msg, hdr);
957 return msg->len;
958 nla_put_failure:
959 genlmsg_cancel(msg, hdr);
960 return -EMSGSIZE;
961 }
962
963 static int swconfig_dump_switches(struct sk_buff *skb,
964 struct netlink_callback *cb)
965 {
966 struct switch_dev *dev;
967 int start = cb->args[0];
968 int idx = 0;
969
970 swconfig_lock();
971 list_for_each_entry(dev, &swdevs, dev_list) {
972 if (++idx <= start)
973 continue;
974 if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).portid,
975 cb->nlh->nlmsg_seq, NLM_F_MULTI,
976 dev) < 0)
977 break;
978 }
979 swconfig_unlock();
980 cb->args[0] = idx;
981
982 return skb->len;
983 }
984
985 static int
986 swconfig_done(struct netlink_callback *cb)
987 {
988 return 0;
989 }
990
991 static struct genl_ops swconfig_ops[] = {
992 {
993 .cmd = SWITCH_CMD_LIST_GLOBAL,
994 .doit = swconfig_list_attrs,
995 .policy = switch_policy,
996 },
997 {
998 .cmd = SWITCH_CMD_LIST_VLAN,
999 .doit = swconfig_list_attrs,
1000 .policy = switch_policy,
1001 },
1002 {
1003 .cmd = SWITCH_CMD_LIST_PORT,
1004 .doit = swconfig_list_attrs,
1005 .policy = switch_policy,
1006 },
1007 {
1008 .cmd = SWITCH_CMD_GET_GLOBAL,
1009 .doit = swconfig_get_attr,
1010 .policy = switch_policy,
1011 },
1012 {
1013 .cmd = SWITCH_CMD_GET_VLAN,
1014 .doit = swconfig_get_attr,
1015 .policy = switch_policy,
1016 },
1017 {
1018 .cmd = SWITCH_CMD_GET_PORT,
1019 .doit = swconfig_get_attr,
1020 .policy = switch_policy,
1021 },
1022 {
1023 .cmd = SWITCH_CMD_SET_GLOBAL,
1024 .doit = swconfig_set_attr,
1025 .policy = switch_policy,
1026 },
1027 {
1028 .cmd = SWITCH_CMD_SET_VLAN,
1029 .doit = swconfig_set_attr,
1030 .policy = switch_policy,
1031 },
1032 {
1033 .cmd = SWITCH_CMD_SET_PORT,
1034 .doit = swconfig_set_attr,
1035 .policy = switch_policy,
1036 },
1037 {
1038 .cmd = SWITCH_CMD_GET_SWITCH,
1039 .dumpit = swconfig_dump_switches,
1040 .policy = switch_policy,
1041 .done = swconfig_done,
1042 }
1043 };
1044
1045 #ifdef CONFIG_OF
1046 void
1047 of_switch_load_portmap(struct switch_dev *dev)
1048 {
1049 struct device_node *port;
1050
1051 if (!dev->of_node)
1052 return;
1053
1054 for_each_child_of_node(dev->of_node, port) {
1055 const __be32 *prop;
1056 const char *segment;
1057 int size, phys;
1058
1059 if (!of_device_is_compatible(port, "swconfig,port"))
1060 continue;
1061
1062 if (of_property_read_string(port, "swconfig,segment", &segment))
1063 continue;
1064
1065 prop = of_get_property(port, "swconfig,portmap", &size);
1066 if (!prop)
1067 continue;
1068
1069 if (size != (2 * sizeof(*prop))) {
1070 pr_err("%s: failed to parse port mapping\n",
1071 port->name);
1072 continue;
1073 }
1074
1075 phys = be32_to_cpup(prop++);
1076 if ((phys < 0) | (phys >= dev->ports)) {
1077 pr_err("%s: physical port index out of range\n",
1078 port->name);
1079 continue;
1080 }
1081
1082 dev->portmap[phys].s = kstrdup(segment, GFP_KERNEL);
1083 dev->portmap[phys].virt = be32_to_cpup(prop);
1084 pr_debug("Found port: %s, physical: %d, virtual: %d\n",
1085 segment, phys, dev->portmap[phys].virt);
1086 }
1087 }
1088 #endif
1089
1090 int
1091 register_switch(struct switch_dev *dev, struct net_device *netdev)
1092 {
1093 struct switch_dev *sdev;
1094 const int max_switches = 8 * sizeof(unsigned long);
1095 unsigned long in_use = 0;
1096 int err;
1097 int i;
1098
1099 INIT_LIST_HEAD(&dev->dev_list);
1100 if (netdev) {
1101 dev->netdev = netdev;
1102 if (!dev->alias)
1103 dev->alias = netdev->name;
1104 }
1105 BUG_ON(!dev->alias);
1106
1107 if (dev->ports > 0) {
1108 dev->portbuf = kzalloc(sizeof(struct switch_port) *
1109 dev->ports, GFP_KERNEL);
1110 if (!dev->portbuf)
1111 return -ENOMEM;
1112 dev->portmap = kzalloc(sizeof(struct switch_portmap) *
1113 dev->ports, GFP_KERNEL);
1114 if (!dev->portmap) {
1115 kfree(dev->portbuf);
1116 return -ENOMEM;
1117 }
1118 }
1119 swconfig_defaults_init(dev);
1120 mutex_init(&dev->sw_mutex);
1121 swconfig_lock();
1122 dev->id = ++swdev_id;
1123
1124 list_for_each_entry(sdev, &swdevs, dev_list) {
1125 if (!sscanf(sdev->devname, SWCONFIG_DEVNAME, &i))
1126 continue;
1127 if (i < 0 || i > max_switches)
1128 continue;
1129
1130 set_bit(i, &in_use);
1131 }
1132 i = find_first_zero_bit(&in_use, max_switches);
1133
1134 if (i == max_switches) {
1135 swconfig_unlock();
1136 return -ENFILE;
1137 }
1138
1139 #ifdef CONFIG_OF
1140 if (dev->ports)
1141 of_switch_load_portmap(dev);
1142 #endif
1143
1144 /* fill device name */
1145 snprintf(dev->devname, IFNAMSIZ, SWCONFIG_DEVNAME, i);
1146
1147 list_add_tail(&dev->dev_list, &swdevs);
1148 swconfig_unlock();
1149
1150 err = swconfig_create_led_trigger(dev);
1151 if (err)
1152 return err;
1153
1154 return 0;
1155 }
1156 EXPORT_SYMBOL_GPL(register_switch);
1157
1158 void
1159 unregister_switch(struct switch_dev *dev)
1160 {
1161 swconfig_destroy_led_trigger(dev);
1162 kfree(dev->portbuf);
1163 mutex_lock(&dev->sw_mutex);
1164 swconfig_lock();
1165 list_del(&dev->dev_list);
1166 swconfig_unlock();
1167 mutex_unlock(&dev->sw_mutex);
1168 }
1169 EXPORT_SYMBOL_GPL(unregister_switch);
1170
1171
1172 static int __init
1173 swconfig_init(void)
1174 {
1175 int err;
1176 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0))
1177 int i;
1178 #endif
1179
1180 INIT_LIST_HEAD(&swdevs);
1181
1182 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0))
1183 err = genl_register_family(&switch_fam);
1184 if (err)
1185 return err;
1186
1187 for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
1188 err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
1189 if (err)
1190 goto unregister;
1191 }
1192 return 0;
1193
1194 unregister:
1195 genl_unregister_family(&switch_fam);
1196 return err;
1197 #else
1198 err = genl_register_family_with_ops(&switch_fam, swconfig_ops);
1199 if (err)
1200 return err;
1201 return 0;
1202 #endif
1203 }
1204
1205 static void __exit
1206 swconfig_exit(void)
1207 {
1208 genl_unregister_family(&switch_fam);
1209 }
1210
1211 module_init(swconfig_init);
1212 module_exit(swconfig_exit);
1213