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