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