generic: rtl8366: add cpu_port, num_ports and num_vlan_mc to struct rtl8366_smi
[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
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 & (1 << 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 if (val->port_vlan >= dev->vlans)
467 goto done;
468 break;
469 case SWITCH_CMD_SET_PORT:
470 case SWITCH_CMD_GET_PORT:
471 alist = &dev->attr_port;
472 def_list = default_port;
473 def_active = &dev->def_port;
474 n_def = ARRAY_SIZE(default_port);
475 if (!info->attrs[SWITCH_ATTR_OP_PORT])
476 goto done;
477 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
478 if (val->port_vlan >= dev->ports)
479 goto done;
480 break;
481 default:
482 WARN_ON(1);
483 goto done;
484 }
485
486 if (!alist)
487 goto done;
488
489 attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
490 if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
491 attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
492 if (attr_id >= n_def)
493 goto done;
494 if (!test_bit(attr_id, def_active))
495 goto done;
496 attr = &def_list[attr_id];
497 } else {
498 if (attr_id >= alist->n_attr)
499 goto done;
500 attr = &alist->attr[attr_id];
501 }
502
503 if (attr->disabled)
504 attr = NULL;
505
506 done:
507 if (!attr)
508 DPRINTF("attribute lookup failed\n");
509 val->attr = attr;
510 return attr;
511 }
512
513 static int
514 swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
515 struct switch_val *val, int max)
516 {
517 struct nlattr *nla;
518 int rem;
519
520 val->len = 0;
521 nla_for_each_nested(nla, head, rem) {
522 struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
523 struct switch_port *port = &val->value.ports[val->len];
524
525 if (val->len >= max)
526 return -EINVAL;
527
528 if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
529 port_policy))
530 return -EINVAL;
531
532 if (!tb[SWITCH_PORT_ID])
533 return -EINVAL;
534
535 port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
536 if (tb[SWITCH_PORT_FLAG_TAGGED])
537 port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
538 val->len++;
539 }
540
541 return 0;
542 }
543
544 static int
545 swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
546 {
547 const struct switch_attr *attr;
548 struct switch_dev *dev;
549 struct switch_val val;
550 int err = -EINVAL;
551
552 dev = swconfig_get_dev(info);
553 if (!dev)
554 return -EINVAL;
555
556 memset(&val, 0, sizeof(val));
557 attr = swconfig_lookup_attr(dev, info, &val);
558 if (!attr || !attr->set)
559 goto error;
560
561 val.attr = attr;
562 switch(attr->type) {
563 case SWITCH_TYPE_NOVAL:
564 break;
565 case SWITCH_TYPE_INT:
566 if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
567 goto error;
568 val.value.i =
569 nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
570 break;
571 case SWITCH_TYPE_STRING:
572 if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
573 goto error;
574 val.value.s =
575 nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
576 break;
577 case SWITCH_TYPE_PORTS:
578 val.value.ports = dev->portbuf;
579 memset(dev->portbuf, 0,
580 sizeof(struct switch_port) * dev->ports);
581
582 /* TODO: implement multipart? */
583 if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
584 err = swconfig_parse_ports(skb,
585 info->attrs[SWITCH_ATTR_OP_VALUE_PORTS], &val, dev->ports);
586 if (err < 0)
587 goto error;
588 } else {
589 val.len = 0;
590 err = 0;
591 }
592 break;
593 default:
594 goto error;
595 }
596
597 err = attr->set(dev, attr, &val);
598 error:
599 swconfig_put_dev(dev);
600 return err;
601 }
602
603 static int
604 swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
605 {
606 if (cb->nest[0])
607 nla_nest_end(cb->msg, cb->nest[0]);
608 return 0;
609 }
610
611 static int
612 swconfig_send_port(struct swconfig_callback *cb, void *arg)
613 {
614 const struct switch_port *port = arg;
615 struct nlattr *p = NULL;
616
617 if (!cb->nest[0]) {
618 cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
619 if (!cb->nest[0])
620 return -1;
621 }
622
623 p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
624 if (!p)
625 goto error;
626
627 NLA_PUT_U32(cb->msg, SWITCH_PORT_ID, port->id);
628 if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
629 NLA_PUT_FLAG(cb->msg, SWITCH_PORT_FLAG_TAGGED);
630
631 nla_nest_end(cb->msg, p);
632 return 0;
633
634 nla_put_failure:
635 nla_nest_cancel(cb->msg, p);
636 error:
637 nla_nest_cancel(cb->msg, cb->nest[0]);
638 return -1;
639 }
640
641 static int
642 swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
643 const struct switch_val *val)
644 {
645 struct swconfig_callback cb;
646 int err = 0;
647 int i;
648
649 if (!val->value.ports)
650 return -EINVAL;
651
652 memset(&cb, 0, sizeof(cb));
653 cb.cmd = attr;
654 cb.msg = *msg;
655 cb.info = info;
656 cb.fill = swconfig_send_port;
657 cb.close = swconfig_close_portlist;
658
659 cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
660 for (i = 0; i < val->len; i++) {
661 err = swconfig_send_multipart(&cb, &val->value.ports[i]);
662 if (err)
663 goto done;
664 }
665 err = val->len;
666 swconfig_close_portlist(&cb, NULL);
667 *msg = cb.msg;
668
669 done:
670 return err;
671 }
672
673 static int
674 swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
675 {
676 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
677 const struct switch_attr *attr;
678 struct switch_dev *dev;
679 struct sk_buff *msg = NULL;
680 struct switch_val val;
681 int err = -EINVAL;
682 int cmd = hdr->cmd;
683
684 dev = swconfig_get_dev(info);
685 if (!dev)
686 return -EINVAL;
687
688 memset(&val, 0, sizeof(val));
689 attr = swconfig_lookup_attr(dev, info, &val);
690 if (!attr || !attr->get)
691 goto error;
692
693 if (attr->type == SWITCH_TYPE_PORTS) {
694 val.value.ports = dev->portbuf;
695 memset(dev->portbuf, 0,
696 sizeof(struct switch_port) * dev->ports);
697 }
698
699 err = attr->get(dev, attr, &val);
700 if (err)
701 goto error;
702
703 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
704 if (!msg)
705 goto error;
706
707 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
708 0, cmd);
709 if (IS_ERR(hdr))
710 goto nla_put_failure;
711
712 switch(attr->type) {
713 case SWITCH_TYPE_INT:
714 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i);
715 break;
716 case SWITCH_TYPE_STRING:
717 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s);
718 break;
719 case SWITCH_TYPE_PORTS:
720 err = swconfig_send_ports(&msg, info,
721 SWITCH_ATTR_OP_VALUE_PORTS, &val);
722 if (err < 0)
723 goto nla_put_failure;
724 break;
725 default:
726 DPRINTF("invalid type in attribute\n");
727 err = -EINVAL;
728 goto error;
729 }
730 err = genlmsg_end(msg, hdr);
731 if (err < 0)
732 goto nla_put_failure;
733
734 swconfig_put_dev(dev);
735 return genlmsg_unicast(msg, info->snd_pid);
736
737 nla_put_failure:
738 if (msg)
739 nlmsg_free(msg);
740 error:
741 swconfig_put_dev(dev);
742 if (!err)
743 err = -ENOMEM;
744 return err;
745 }
746
747 static int
748 swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
749 const struct switch_dev *dev)
750 {
751 void *hdr;
752
753 hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
754 SWITCH_CMD_NEW_ATTR);
755 if (IS_ERR(hdr))
756 return -1;
757
758 NLA_PUT_U32(msg, SWITCH_ATTR_ID, dev->id);
759 NLA_PUT_STRING(msg, SWITCH_ATTR_NAME, dev->name);
760 NLA_PUT_STRING(msg, SWITCH_ATTR_DEV_NAME, dev->devname);
761 NLA_PUT_U32(msg, SWITCH_ATTR_VLANS, dev->vlans);
762 NLA_PUT_U32(msg, SWITCH_ATTR_PORTS, dev->ports);
763 NLA_PUT_U32(msg, SWITCH_ATTR_CPU_PORT, dev->cpu_port);
764
765 return genlmsg_end(msg, hdr);
766 nla_put_failure:
767 genlmsg_cancel(msg, hdr);
768 return -EMSGSIZE;
769 }
770
771 static int swconfig_dump_switches(struct sk_buff *skb,
772 struct netlink_callback *cb)
773 {
774 struct switch_dev *dev;
775 int start = cb->args[0];
776 int idx = 0;
777
778 swconfig_lock();
779 list_for_each_entry(dev, &swdevs, dev_list) {
780 if (++idx <= start)
781 continue;
782 if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).pid,
783 cb->nlh->nlmsg_seq, NLM_F_MULTI,
784 dev) < 0)
785 break;
786 }
787 swconfig_unlock();
788 cb->args[0] = idx;
789
790 return skb->len;
791 }
792
793 static int
794 swconfig_done(struct netlink_callback *cb)
795 {
796 return 0;
797 }
798
799 static struct genl_ops swconfig_ops[] = {
800 {
801 .cmd = SWITCH_CMD_LIST_GLOBAL,
802 .doit = swconfig_list_attrs,
803 .policy = switch_policy,
804 },
805 {
806 .cmd = SWITCH_CMD_LIST_VLAN,
807 .doit = swconfig_list_attrs,
808 .policy = switch_policy,
809 },
810 {
811 .cmd = SWITCH_CMD_LIST_PORT,
812 .doit = swconfig_list_attrs,
813 .policy = switch_policy,
814 },
815 {
816 .cmd = SWITCH_CMD_GET_GLOBAL,
817 .doit = swconfig_get_attr,
818 .policy = switch_policy,
819 },
820 {
821 .cmd = SWITCH_CMD_GET_VLAN,
822 .doit = swconfig_get_attr,
823 .policy = switch_policy,
824 },
825 {
826 .cmd = SWITCH_CMD_GET_PORT,
827 .doit = swconfig_get_attr,
828 .policy = switch_policy,
829 },
830 {
831 .cmd = SWITCH_CMD_SET_GLOBAL,
832 .doit = swconfig_set_attr,
833 .policy = switch_policy,
834 },
835 {
836 .cmd = SWITCH_CMD_SET_VLAN,
837 .doit = swconfig_set_attr,
838 .policy = switch_policy,
839 },
840 {
841 .cmd = SWITCH_CMD_SET_PORT,
842 .doit = swconfig_set_attr,
843 .policy = switch_policy,
844 },
845 {
846 .cmd = SWITCH_CMD_GET_SWITCH,
847 .dumpit = swconfig_dump_switches,
848 .policy = switch_policy,
849 .done = swconfig_done,
850 }
851 };
852
853 int
854 register_switch(struct switch_dev *dev, struct net_device *netdev)
855 {
856 INIT_LIST_HEAD(&dev->dev_list);
857 if (netdev) {
858 dev->netdev = netdev;
859 if (!dev->devname)
860 dev->devname = netdev->name;
861 }
862 BUG_ON(!dev->devname);
863
864 if (dev->ports > 0) {
865 dev->portbuf = kzalloc(sizeof(struct switch_port) * dev->ports,
866 GFP_KERNEL);
867 if (!dev->portbuf)
868 return -ENOMEM;
869 }
870 dev->id = ++swdev_id;
871 swconfig_defaults_init(dev);
872 spin_lock_init(&dev->lock);
873 swconfig_lock();
874 list_add(&dev->dev_list, &swdevs);
875 swconfig_unlock();
876
877 return 0;
878 }
879 EXPORT_SYMBOL_GPL(register_switch);
880
881 void
882 unregister_switch(struct switch_dev *dev)
883 {
884 kfree(dev->portbuf);
885 spin_lock(&dev->lock);
886 swconfig_lock();
887 list_del(&dev->dev_list);
888 swconfig_unlock();
889 spin_unlock(&dev->lock);
890 }
891 EXPORT_SYMBOL_GPL(unregister_switch);
892
893
894 static int __init
895 swconfig_init(void)
896 {
897 int i, err;
898
899 INIT_LIST_HEAD(&swdevs);
900 err = genl_register_family(&switch_fam);
901 if (err)
902 return err;
903
904 for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
905 err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
906 if (err)
907 goto unregister;
908 }
909
910 return 0;
911
912 unregister:
913 genl_unregister_family(&switch_fam);
914 return err;
915 }
916
917 static void __exit
918 swconfig_exit(void)
919 {
920 genl_unregister_family(&switch_fam);
921 }
922
923 module_init(swconfig_init);
924 module_exit(swconfig_exit);
925
This page took 0.109305 seconds and 5 git commands to generate.