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