Several small fixes for ar8216 driver (patch by Jonas Gorski)
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / 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
27 //#define DEBUG 1
28 #ifdef DEBUG
29 #define DPRINTF(format, ...) printk("%s: " format, __func__, ##__VA_ARGS__)
30 #else
31 #define DPRINTF(...) do {} while(0)
32 #endif
33
34 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
35 MODULE_LICENSE("GPL");
36
37 static int swdev_id = 0;
38 static struct list_head swdevs;
39 static spinlock_t swdevs_lock = SPIN_LOCK_UNLOCKED;
40 struct swconfig_callback;
41
42 struct swconfig_callback
43 {
44 struct sk_buff *msg;
45 struct genlmsghdr *hdr;
46 struct genl_info *info;
47 int cmd;
48
49 /* callback for filling in the message data */
50 int (*fill)(struct swconfig_callback *cb, void *arg);
51
52 /* callback for closing the message before sending it */
53 int (*close)(struct swconfig_callback *cb, void *arg);
54
55 struct nlattr *nest[4];
56 int args[4];
57 };
58
59 /* defaults */
60
61 static int
62 swconfig_get_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
63 {
64 int ret;
65 if (val->port_vlan >= dev->vlans)
66 return -EINVAL;
67
68 if (!dev->get_vlan_ports)
69 return -EOPNOTSUPP;
70
71 ret = dev->get_vlan_ports(dev, val);
72 return ret;
73 }
74
75 static int
76 swconfig_set_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
77 {
78 struct switch_port *ports = val->value.ports;
79 int i;
80
81 if (val->port_vlan >= dev->vlans)
82 return -EINVAL;
83
84 /* validate ports */
85 if (val->len > dev->ports)
86 return -EINVAL;
87
88 if (!dev->set_vlan_ports)
89 return -EOPNOTSUPP;
90
91 for (i = 0; i < val->len; i++) {
92 if (ports[i].id >= dev->ports)
93 return -EINVAL;
94
95 if (dev->set_port_pvid && !(ports[i].flags & SWITCH_PORT_FLAG_TAGGED))
96 dev->set_port_pvid(dev, ports[i].id, val->port_vlan);
97 }
98
99 return dev->set_vlan_ports(dev, val);
100 }
101
102 static int
103 swconfig_set_pvid(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
104 {
105 if (val->port_vlan >= dev->ports)
106 return -EINVAL;
107
108 if (!dev->set_port_pvid)
109 return -EOPNOTSUPP;
110
111 return dev->set_port_pvid(dev, val->port_vlan, val->value.i);
112 }
113
114 static int
115 swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
116 {
117 if (val->port_vlan >= dev->ports)
118 return -EINVAL;
119
120 if (!dev->get_port_pvid)
121 return -EOPNOTSUPP;
122
123 return dev->get_port_pvid(dev, val->port_vlan, &val->value.i);
124 }
125
126 static int
127 swconfig_apply_config(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
128 {
129 /* don't complain if not supported by the switch driver */
130 if (!dev->apply_config)
131 return 0;
132
133 return dev->apply_config(dev);
134 }
135
136 static int
137 swconfig_reset_switch(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
138 {
139 /* don't complain if not supported by the switch driver */
140 if (!dev->reset_switch)
141 return 0;
142
143 return dev->reset_switch(dev);
144 }
145
146 enum global_defaults {
147 GLOBAL_APPLY,
148 GLOBAL_RESET,
149 };
150
151 enum vlan_defaults {
152 VLAN_PORTS,
153 };
154
155 enum port_defaults {
156 PORT_PVID,
157 };
158
159 static struct switch_attr default_global[] = {
160 [GLOBAL_APPLY] = {
161 .type = SWITCH_TYPE_NOVAL,
162 .name = "apply",
163 .description = "Activate changes in the hardware",
164 .set = swconfig_apply_config,
165 },
166 [GLOBAL_RESET] = {
167 .type = SWITCH_TYPE_NOVAL,
168 .name = "reset",
169 .description = "Reset the switch",
170 .set = swconfig_reset_switch,
171 }
172 };
173
174 static struct switch_attr default_port[] = {
175 [PORT_PVID] = {
176 .type = SWITCH_TYPE_INT,
177 .name = "pvid",
178 .description = "Primary VLAN ID",
179 .set = swconfig_set_pvid,
180 .get = swconfig_get_pvid,
181 }
182 };
183
184 static struct switch_attr default_vlan[] = {
185 [VLAN_PORTS] = {
186 .type = SWITCH_TYPE_PORTS,
187 .name = "ports",
188 .description = "VLAN port mapping",
189 .set = swconfig_set_vlan_ports,
190 .get = swconfig_get_vlan_ports,
191 },
192 };
193
194
195 static void swconfig_defaults_init(struct switch_dev *dev)
196 {
197 dev->def_global = 0;
198 dev->def_vlan = 0;
199 dev->def_port = 0;
200
201 if (dev->get_vlan_ports || dev->set_vlan_ports)
202 set_bit(VLAN_PORTS, &dev->def_vlan);
203
204 if (dev->get_port_pvid || dev->set_port_pvid)
205 set_bit(PORT_PVID, &dev->def_port);
206
207 /* always present, can be no-op */
208 set_bit(GLOBAL_APPLY, &dev->def_global);
209 set_bit(GLOBAL_RESET, &dev->def_global);
210 }
211
212
213 static struct genl_family switch_fam = {
214 .id = GENL_ID_GENERATE,
215 .name = "switch",
216 .hdrsize = 0,
217 .version = 1,
218 .maxattr = SWITCH_ATTR_MAX,
219 };
220
221 static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
222 [SWITCH_ATTR_ID] = { .type = NLA_U32 },
223 [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 },
224 [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 },
225 [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 },
226 [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 },
227 [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING },
228 [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED },
229 [SWITCH_ATTR_TYPE] = { .type = NLA_U32 },
230 };
231
232 static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
233 [SWITCH_PORT_ID] = { .type = NLA_U32 },
234 [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
235 };
236
237 static inline void
238 swconfig_lock(void)
239 {
240 spin_lock(&swdevs_lock);
241 }
242
243 static inline void
244 swconfig_unlock(void)
245 {
246 spin_unlock(&swdevs_lock);
247 }
248
249 static struct switch_dev *
250 swconfig_get_dev(struct genl_info *info)
251 {
252 struct switch_dev *dev = NULL;
253 struct switch_dev *p;
254 int id;
255
256 if (!info->attrs[SWITCH_ATTR_ID])
257 goto done;
258
259 id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]);
260 swconfig_lock();
261 list_for_each_entry(p, &swdevs, dev_list) {
262 if (id != p->id)
263 continue;
264
265 dev = p;
266 break;
267 }
268 if (dev)
269 spin_lock(&dev->lock);
270 else
271 DPRINTF("device %d not found\n", id);
272 swconfig_unlock();
273 done:
274 return dev;
275 }
276
277 static inline void
278 swconfig_put_dev(struct switch_dev *dev)
279 {
280 spin_unlock(&dev->lock);
281 }
282
283 static int
284 swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
285 {
286 struct switch_attr *op = arg;
287 struct genl_info *info = cb->info;
288 struct sk_buff *msg = cb->msg;
289 int id = cb->args[0];
290 void *hdr;
291
292 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
293 NLM_F_MULTI, SWITCH_CMD_NEW_ATTR);
294 if (IS_ERR(hdr))
295 return -1;
296
297 NLA_PUT_U32(msg, SWITCH_ATTR_OP_ID, id);
298 NLA_PUT_U32(msg, SWITCH_ATTR_OP_TYPE, op->type);
299 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_NAME, op->name);
300 if (op->description)
301 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_DESCRIPTION,
302 op->description);
303
304 return genlmsg_end(msg, hdr);
305 nla_put_failure:
306 genlmsg_cancel(msg, hdr);
307 return -EMSGSIZE;
308 }
309
310 /* spread multipart messages across multiple message buffers */
311 static int
312 swconfig_send_multipart(struct swconfig_callback *cb, void *arg)
313 {
314 struct genl_info *info = cb->info;
315 int restart = 0;
316 int err;
317
318 do {
319 if (!cb->msg) {
320 cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
321 if (cb->msg == NULL)
322 goto error;
323 }
324
325 if (!(cb->fill(cb, arg) < 0))
326 break;
327
328 /* fill failed, check if this was already the second attempt */
329 if (restart)
330 goto error;
331
332 /* try again in a new message, send the current one */
333 restart = 1;
334 if (cb->close) {
335 if (cb->close(cb, arg) < 0)
336 goto error;
337 }
338 err = genlmsg_unicast(cb->msg, info->snd_pid);
339 cb->msg = NULL;
340 if (err < 0)
341 goto error;
342
343 } while (restart);
344
345 return 0;
346
347 error:
348 if (cb->msg)
349 nlmsg_free(cb->msg);
350 return -1;
351 }
352
353 static int
354 swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
355 {
356 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
357 const struct switch_attrlist *alist;
358 struct switch_dev *dev;
359 struct swconfig_callback cb;
360 int err = -EINVAL;
361 int i;
362
363 /* defaults */
364 struct switch_attr *def_list;
365 unsigned long *def_active;
366 int n_def;
367
368 dev = swconfig_get_dev(info);
369 if (!dev)
370 return -EINVAL;
371
372 switch(hdr->cmd) {
373 case SWITCH_CMD_LIST_GLOBAL:
374 alist = &dev->attr_global;
375 def_list = default_global;
376 def_active = &dev->def_global;
377 n_def = ARRAY_SIZE(default_global);
378 break;
379 case SWITCH_CMD_LIST_VLAN:
380 alist = &dev->attr_vlan;
381 def_list = default_vlan;
382 def_active = &dev->def_vlan;
383 n_def = ARRAY_SIZE(default_vlan);
384 break;
385 case SWITCH_CMD_LIST_PORT:
386 alist = &dev->attr_port;
387 def_list = default_port;
388 def_active = &dev->def_port;
389 n_def = ARRAY_SIZE(default_port);
390 break;
391 default:
392 WARN_ON(1);
393 goto out;
394 }
395
396 memset(&cb, 0, sizeof(cb));
397 cb.info = info;
398 cb.fill = swconfig_dump_attr;
399 for (i = 0; i < alist->n_attr; i++) {
400 if (alist->attr[i].disabled)
401 continue;
402 cb.args[0] = i;
403 err = swconfig_send_multipart(&cb, (void *) &alist->attr[i]);
404 if (err < 0)
405 goto error;
406 }
407
408 /* defaults */
409 for (i = 0; i < n_def; i++) {
410 if (!test_bit(i, def_active))
411 continue;
412 cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i;
413 err = swconfig_send_multipart(&cb, (void *) &def_list[i]);
414 if (err < 0)
415 goto error;
416 }
417 swconfig_put_dev(dev);
418
419 if (!cb.msg)
420 return 0;
421
422 return genlmsg_unicast(cb.msg, info->snd_pid);
423
424 error:
425 if (cb.msg)
426 nlmsg_free(cb.msg);
427 out:
428 swconfig_put_dev(dev);
429 return err;
430 }
431
432 static const struct switch_attr *
433 swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
434 struct switch_val *val)
435 {
436 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
437 const struct switch_attrlist *alist;
438 const struct switch_attr *attr = NULL;
439 int attr_id;
440
441 /* defaults */
442 struct switch_attr *def_list;
443 unsigned long *def_active;
444 int n_def;
445
446 if (!info->attrs[SWITCH_ATTR_OP_ID])
447 goto done;
448
449 switch(hdr->cmd) {
450 case SWITCH_CMD_SET_GLOBAL:
451 case SWITCH_CMD_GET_GLOBAL:
452 alist = &dev->attr_global;
453 def_list = default_global;
454 def_active = &dev->def_global;
455 n_def = ARRAY_SIZE(default_global);
456 break;
457 case SWITCH_CMD_SET_VLAN:
458 case SWITCH_CMD_GET_VLAN:
459 alist = &dev->attr_vlan;
460 def_list = default_vlan;
461 def_active = &dev->def_vlan;
462 n_def = ARRAY_SIZE(default_vlan);
463 if (!info->attrs[SWITCH_ATTR_OP_VLAN])
464 goto done;
465 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]);
466 break;
467 case SWITCH_CMD_SET_PORT:
468 case SWITCH_CMD_GET_PORT:
469 alist = &dev->attr_port;
470 def_list = default_port;
471 def_active = &dev->def_port;
472 n_def = ARRAY_SIZE(default_port);
473 if (!info->attrs[SWITCH_ATTR_OP_PORT])
474 goto done;
475 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
476 break;
477 default:
478 WARN_ON(1);
479 goto done;
480 }
481
482 if (!alist)
483 goto done;
484
485 attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
486 if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
487 attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
488 if (attr_id >= n_def)
489 goto done;
490 if (!test_bit(attr_id, def_active))
491 goto done;
492 attr = &def_list[attr_id];
493 } else {
494 if (attr_id >= alist->n_attr)
495 goto done;
496 attr = &alist->attr[attr_id];
497 }
498
499 if (attr->disabled)
500 attr = NULL;
501
502 done:
503 if (!attr)
504 DPRINTF("attribute lookup failed\n");
505 val->attr = attr;
506 return attr;
507 }
508
509 static int
510 swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
511 struct switch_val *val, int max)
512 {
513 struct nlattr *nla;
514 int rem;
515
516 val->len = 0;
517 nla_for_each_nested(nla, head, rem) {
518 struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
519 struct switch_port *port = &val->value.ports[val->len];
520
521 if (val->len >= max)
522 return -EINVAL;
523
524 if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
525 port_policy))
526 return -EINVAL;
527
528 if (!tb[SWITCH_PORT_ID])
529 return -EINVAL;
530
531 port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
532 if (tb[SWITCH_PORT_FLAG_TAGGED])
533 port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
534 val->len++;
535 }
536
537 return 0;
538 }
539
540 static int
541 swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
542 {
543 const struct switch_attr *attr;
544 struct switch_dev *dev;
545 struct switch_val val;
546 int err = -EINVAL;
547
548 dev = swconfig_get_dev(info);
549 if (!dev)
550 return -EINVAL;
551
552 memset(&val, 0, sizeof(val));
553 attr = swconfig_lookup_attr(dev, info, &val);
554 if (!attr || !attr->set)
555 goto error;
556
557 val.attr = attr;
558 switch(attr->type) {
559 case SWITCH_TYPE_NOVAL:
560 break;
561 case SWITCH_TYPE_INT:
562 if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
563 goto error;
564 val.value.i =
565 nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
566 break;
567 case SWITCH_TYPE_STRING:
568 if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
569 goto error;
570 val.value.s =
571 nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
572 break;
573 case SWITCH_TYPE_PORTS:
574 val.value.ports = dev->portbuf;
575 memset(dev->portbuf, 0,
576 sizeof(struct switch_port) * dev->ports);
577
578 /* TODO: implement multipart? */
579 if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
580 err = swconfig_parse_ports(skb,
581 info->attrs[SWITCH_ATTR_OP_VALUE_PORTS], &val, dev->ports);
582 if (err < 0)
583 goto error;
584 } else {
585 val.len = 0;
586 err = 0;
587 }
588 break;
589 default:
590 goto error;
591 }
592
593 err = attr->set(dev, attr, &val);
594 error:
595 swconfig_put_dev(dev);
596 return err;
597 }
598
599 static int
600 swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
601 {
602 if (cb->nest[0])
603 nla_nest_end(cb->msg, cb->nest[0]);
604 return 0;
605 }
606
607 static int
608 swconfig_send_port(struct swconfig_callback *cb, void *arg)
609 {
610 const struct switch_port *port = arg;
611 struct nlattr *p = NULL;
612
613 if (!cb->nest[0]) {
614 cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
615 if (!cb->nest[0])
616 return -1;
617 }
618
619 p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
620 if (!p)
621 goto error;
622
623 NLA_PUT_U32(cb->msg, SWITCH_PORT_ID, port->id);
624 if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
625 NLA_PUT_FLAG(cb->msg, SWITCH_PORT_FLAG_TAGGED);
626
627 nla_nest_end(cb->msg, p);
628 return 0;
629
630 nla_put_failure:
631 nla_nest_cancel(cb->msg, p);
632 error:
633 nla_nest_cancel(cb->msg, cb->nest[0]);
634 return -1;
635 }
636
637 static int
638 swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
639 const struct switch_val *val)
640 {
641 struct swconfig_callback cb;
642 int err = 0;
643 int i;
644
645 if (!val->value.ports)
646 return -EINVAL;
647
648 memset(&cb, 0, sizeof(cb));
649 cb.cmd = attr;
650 cb.msg = *msg;
651 cb.info = info;
652 cb.fill = swconfig_send_port;
653 cb.close = swconfig_close_portlist;
654
655 cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
656 for (i = 0; i < val->len; i++) {
657 err = swconfig_send_multipart(&cb, &val->value.ports[i]);
658 if (err)
659 goto done;
660 }
661 err = val->len;
662 swconfig_close_portlist(&cb, NULL);
663 *msg = cb.msg;
664
665 done:
666 return err;
667 }
668
669 static int
670 swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
671 {
672 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
673 const struct switch_attr *attr;
674 struct switch_dev *dev;
675 struct sk_buff *msg = NULL;
676 struct switch_val val;
677 int err = -EINVAL;
678 int cmd = hdr->cmd;
679
680 dev = swconfig_get_dev(info);
681 if (!dev)
682 return -EINVAL;
683
684 memset(&val, 0, sizeof(val));
685 attr = swconfig_lookup_attr(dev, info, &val);
686 if (!attr || !attr->get)
687 goto error_dev;
688
689 if (attr->type == SWITCH_TYPE_PORTS) {
690 val.value.ports = dev->portbuf;
691 memset(dev->portbuf, 0,
692 sizeof(struct switch_port) * dev->ports);
693 }
694
695 err = attr->get(dev, attr, &val);
696 if (err)
697 goto error;
698
699 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
700 if (!msg)
701 goto error;
702
703 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
704 0, cmd);
705 if (IS_ERR(hdr))
706 goto nla_put_failure;
707
708 switch(attr->type) {
709 case SWITCH_TYPE_INT:
710 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i);
711 break;
712 case SWITCH_TYPE_STRING:
713 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s);
714 break;
715 case SWITCH_TYPE_PORTS:
716 err = swconfig_send_ports(&msg, info,
717 SWITCH_ATTR_OP_VALUE_PORTS, &val);
718 if (err < 0)
719 goto nla_put_failure;
720 break;
721 default:
722 DPRINTF("invalid type in attribute\n");
723 err = -EINVAL;
724 goto error;
725 }
726 err = genlmsg_end(msg, hdr);
727 if (err < 0)
728 goto nla_put_failure;
729
730 swconfig_put_dev(dev);
731 return genlmsg_unicast(msg, info->snd_pid);
732
733 nla_put_failure:
734 if (msg)
735 nlmsg_free(msg);
736 error_dev:
737 swconfig_put_dev(dev);
738 error:
739 if (!err)
740 err = -ENOMEM;
741 return err;
742 }
743
744 static int
745 swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
746 const struct switch_dev *dev)
747 {
748 void *hdr;
749
750 hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
751 SWITCH_CMD_NEW_ATTR);
752 if (IS_ERR(hdr))
753 return -1;
754
755 NLA_PUT_U32(msg, SWITCH_ATTR_ID, dev->id);
756 NLA_PUT_STRING(msg, SWITCH_ATTR_NAME, dev->name);
757 NLA_PUT_STRING(msg, SWITCH_ATTR_DEV_NAME, dev->devname);
758 NLA_PUT_U32(msg, SWITCH_ATTR_VLANS, dev->vlans);
759 NLA_PUT_U32(msg, SWITCH_ATTR_PORTS, dev->ports);
760
761 return genlmsg_end(msg, hdr);
762 nla_put_failure:
763 genlmsg_cancel(msg, hdr);
764 return -EMSGSIZE;
765 }
766
767 static int swconfig_dump_switches(struct sk_buff *skb,
768 struct netlink_callback *cb)
769 {
770 struct switch_dev *dev;
771 int start = cb->args[0];
772 int idx = 0;
773
774 swconfig_lock();
775 list_for_each_entry(dev, &swdevs, dev_list) {
776 if (++idx <= start)
777 continue;
778 if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).pid,
779 cb->nlh->nlmsg_seq, NLM_F_MULTI,
780 dev) < 0)
781 break;
782 }
783 swconfig_unlock();
784 cb->args[0] = idx;
785
786 return skb->len;
787 }
788
789 static int
790 swconfig_done(struct netlink_callback *cb)
791 {
792 return 0;
793 }
794
795 static struct genl_ops swconfig_ops[] = {
796 {
797 .cmd = SWITCH_CMD_LIST_GLOBAL,
798 .doit = swconfig_list_attrs,
799 .policy = switch_policy,
800 },
801 {
802 .cmd = SWITCH_CMD_LIST_VLAN,
803 .doit = swconfig_list_attrs,
804 .policy = switch_policy,
805 },
806 {
807 .cmd = SWITCH_CMD_LIST_PORT,
808 .doit = swconfig_list_attrs,
809 .policy = switch_policy,
810 },
811 {
812 .cmd = SWITCH_CMD_GET_GLOBAL,
813 .doit = swconfig_get_attr,
814 .policy = switch_policy,
815 },
816 {
817 .cmd = SWITCH_CMD_GET_VLAN,
818 .doit = swconfig_get_attr,
819 .policy = switch_policy,
820 },
821 {
822 .cmd = SWITCH_CMD_GET_PORT,
823 .doit = swconfig_get_attr,
824 .policy = switch_policy,
825 },
826 {
827 .cmd = SWITCH_CMD_SET_GLOBAL,
828 .doit = swconfig_set_attr,
829 .policy = switch_policy,
830 },
831 {
832 .cmd = SWITCH_CMD_SET_VLAN,
833 .doit = swconfig_set_attr,
834 .policy = switch_policy,
835 },
836 {
837 .cmd = SWITCH_CMD_SET_PORT,
838 .doit = swconfig_set_attr,
839 .policy = switch_policy,
840 },
841 {
842 .cmd = SWITCH_CMD_GET_SWITCH,
843 .dumpit = swconfig_dump_switches,
844 .policy = switch_policy,
845 .done = swconfig_done,
846 }
847 };
848
849 int
850 register_switch(struct switch_dev *dev, struct net_device *netdev)
851 {
852 INIT_LIST_HEAD(&dev->dev_list);
853 if (netdev) {
854 dev->netdev = netdev;
855 if (!dev->devname)
856 dev->devname = netdev->name;
857 }
858 BUG_ON(!dev->devname);
859
860 if (dev->ports > 0) {
861 dev->portbuf = kzalloc(sizeof(struct switch_port) * dev->ports,
862 GFP_KERNEL);
863 if (!dev->portbuf)
864 return -ENOMEM;
865 }
866 dev->id = ++swdev_id;
867 swconfig_defaults_init(dev);
868 spin_lock_init(&dev->lock);
869 swconfig_lock();
870 list_add(&dev->dev_list, &swdevs);
871 swconfig_unlock();
872
873 return 0;
874 }
875 EXPORT_SYMBOL_GPL(register_switch);
876
877 void
878 unregister_switch(struct switch_dev *dev)
879 {
880 kfree(dev->portbuf);
881 spin_lock(&dev->lock);
882 swconfig_lock();
883 list_del(&dev->dev_list);
884 swconfig_unlock();
885 }
886 EXPORT_SYMBOL_GPL(unregister_switch);
887
888
889 static int __init
890 swconfig_init(void)
891 {
892 int i, err;
893
894 INIT_LIST_HEAD(&swdevs);
895 err = genl_register_family(&switch_fam);
896 if (err)
897 return err;
898
899 for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
900 err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
901 if (err)
902 goto unregister;
903 }
904
905 return 0;
906
907 unregister:
908 genl_unregister_family(&switch_fam);
909 return err;
910 }
911
912 static void __exit
913 swconfig_exit(void)
914 {
915 genl_unregister_family(&switch_fam);
916 }
917
918 module_init(swconfig_init);
919 module_exit(swconfig_exit);
920