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