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