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