net: dsa: get port name at parse time
[linux-2.6-block.git] / net / dsa / dsa2.c
1 /*
2  * net/dsa/dsa2.c - Hardware switch handling, binding version 2
3  * Copyright (c) 2008-2009 Marvell Semiconductor
4  * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
5  * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/list.h>
16 #include <linux/netdevice.h>
17 #include <linux/slab.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/of.h>
20 #include <linux/of_net.h>
21
22 #include "dsa_priv.h"
23
24 static LIST_HEAD(dsa_switch_trees);
25 static DEFINE_MUTEX(dsa2_mutex);
26
27 static const struct devlink_ops dsa_devlink_ops = {
28 };
29
30 static struct dsa_switch_tree *dsa_get_dst(u32 tree)
31 {
32         struct dsa_switch_tree *dst;
33
34         list_for_each_entry(dst, &dsa_switch_trees, list)
35                 if (dst->tree == tree) {
36                         kref_get(&dst->refcount);
37                         return dst;
38                 }
39         return NULL;
40 }
41
42 static void dsa_free_dst(struct kref *ref)
43 {
44         struct dsa_switch_tree *dst = container_of(ref, struct dsa_switch_tree,
45                                                    refcount);
46
47         list_del(&dst->list);
48         kfree(dst);
49 }
50
51 static void dsa_put_dst(struct dsa_switch_tree *dst)
52 {
53         kref_put(&dst->refcount, dsa_free_dst);
54 }
55
56 static struct dsa_switch_tree *dsa_add_dst(u32 tree)
57 {
58         struct dsa_switch_tree *dst;
59
60         dst = kzalloc(sizeof(*dst), GFP_KERNEL);
61         if (!dst)
62                 return NULL;
63         dst->tree = tree;
64         INIT_LIST_HEAD(&dst->list);
65         list_add_tail(&dsa_switch_trees, &dst->list);
66         kref_init(&dst->refcount);
67
68         return dst;
69 }
70
71 static void dsa_dst_add_ds(struct dsa_switch_tree *dst,
72                            struct dsa_switch *ds, u32 index)
73 {
74         kref_get(&dst->refcount);
75         dst->ds[index] = ds;
76 }
77
78 static void dsa_dst_del_ds(struct dsa_switch_tree *dst,
79                            struct dsa_switch *ds, u32 index)
80 {
81         dst->ds[index] = NULL;
82         kref_put(&dst->refcount, dsa_free_dst);
83 }
84
85 /* For platform data configurations, we need to have a valid name argument to
86  * differentiate a disabled port from an enabled one
87  */
88 static bool dsa_port_is_valid(struct dsa_port *port)
89 {
90         return port->type != DSA_PORT_TYPE_UNUSED;
91 }
92
93 static bool dsa_port_is_dsa(struct dsa_port *port)
94 {
95         return port->type == DSA_PORT_TYPE_DSA;
96 }
97
98 static bool dsa_port_is_cpu(struct dsa_port *port)
99 {
100         return port->type == DSA_PORT_TYPE_CPU;
101 }
102
103 static bool dsa_ds_find_port_dn(struct dsa_switch *ds,
104                                 struct device_node *port)
105 {
106         u32 index;
107
108         for (index = 0; index < ds->num_ports; index++)
109                 if (ds->ports[index].dn == port)
110                         return true;
111         return false;
112 }
113
114 static struct dsa_switch *dsa_dst_find_port_dn(struct dsa_switch_tree *dst,
115                                                struct device_node *port)
116 {
117         struct dsa_switch *ds;
118         u32 index;
119
120         for (index = 0; index < DSA_MAX_SWITCHES; index++) {
121                 ds = dst->ds[index];
122                 if (!ds)
123                         continue;
124
125                 if (dsa_ds_find_port_dn(ds, port))
126                         return ds;
127         }
128
129         return NULL;
130 }
131
132 static int dsa_port_complete(struct dsa_switch_tree *dst,
133                              struct dsa_switch *src_ds,
134                              struct dsa_port *port,
135                              u32 src_port)
136 {
137         struct device_node *link;
138         int index;
139         struct dsa_switch *dst_ds;
140
141         for (index = 0;; index++) {
142                 link = of_parse_phandle(port->dn, "link", index);
143                 if (!link)
144                         break;
145
146                 dst_ds = dsa_dst_find_port_dn(dst, link);
147                 of_node_put(link);
148
149                 if (!dst_ds)
150                         return 1;
151
152                 src_ds->rtable[dst_ds->index] = src_port;
153         }
154
155         return 0;
156 }
157
158 /* A switch is complete if all the DSA ports phandles point to ports
159  * known in the tree. A return value of 1 means the tree is not
160  * complete. This is not an error condition. A value of 0 is
161  * success.
162  */
163 static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
164 {
165         struct dsa_port *port;
166         u32 index;
167         int err;
168
169         for (index = 0; index < ds->num_ports; index++) {
170                 port = &ds->ports[index];
171                 if (!dsa_port_is_valid(port))
172                         continue;
173
174                 if (!dsa_port_is_dsa(port))
175                         continue;
176
177                 err = dsa_port_complete(dst, ds, port, index);
178                 if (err != 0)
179                         return err;
180         }
181
182         return 0;
183 }
184
185 /* A tree is complete if all the DSA ports phandles point to ports
186  * known in the tree. A return value of 1 means the tree is not
187  * complete. This is not an error condition. A value of 0 is
188  * success.
189  */
190 static int dsa_dst_complete(struct dsa_switch_tree *dst)
191 {
192         struct dsa_switch *ds;
193         u32 index;
194         int err;
195
196         for (index = 0; index < DSA_MAX_SWITCHES; index++) {
197                 ds = dst->ds[index];
198                 if (!ds)
199                         continue;
200
201                 err = dsa_ds_complete(dst, ds);
202                 if (err != 0)
203                         return err;
204         }
205
206         return 0;
207 }
208
209 static int dsa_dsa_port_apply(struct dsa_port *port)
210 {
211         struct dsa_switch *ds = port->ds;
212         int err;
213
214         err = dsa_port_fixed_link_register_of(port);
215         if (err) {
216                 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
217                          port->index, err);
218                 return err;
219         }
220
221         memset(&port->devlink_port, 0, sizeof(port->devlink_port));
222
223         return devlink_port_register(ds->devlink, &port->devlink_port,
224                                      port->index);
225 }
226
227 static void dsa_dsa_port_unapply(struct dsa_port *port)
228 {
229         devlink_port_unregister(&port->devlink_port);
230         dsa_port_fixed_link_unregister_of(port);
231 }
232
233 static int dsa_cpu_port_apply(struct dsa_port *port)
234 {
235         struct dsa_switch *ds = port->ds;
236         int err;
237
238         err = dsa_port_fixed_link_register_of(port);
239         if (err) {
240                 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
241                          port->index, err);
242                 return err;
243         }
244
245         memset(&port->devlink_port, 0, sizeof(port->devlink_port));
246         err = devlink_port_register(ds->devlink, &port->devlink_port,
247                                     port->index);
248         return err;
249 }
250
251 static void dsa_cpu_port_unapply(struct dsa_port *port)
252 {
253         devlink_port_unregister(&port->devlink_port);
254         dsa_port_fixed_link_unregister_of(port);
255 }
256
257 static int dsa_user_port_apply(struct dsa_port *port)
258 {
259         struct dsa_switch *ds = port->ds;
260         const char *name = port->name;
261         int err;
262
263         err = dsa_slave_create(port, name);
264         if (err) {
265                 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
266                          port->index, err);
267                 port->slave = NULL;
268                 return err;
269         }
270
271         memset(&port->devlink_port, 0, sizeof(port->devlink_port));
272         err = devlink_port_register(ds->devlink, &port->devlink_port,
273                                     port->index);
274         if (err)
275                 return err;
276
277         devlink_port_type_eth_set(&port->devlink_port, port->slave);
278
279         return 0;
280 }
281
282 static void dsa_user_port_unapply(struct dsa_port *port)
283 {
284         devlink_port_unregister(&port->devlink_port);
285         if (port->slave) {
286                 dsa_slave_destroy(port->slave);
287                 port->slave = NULL;
288         }
289 }
290
291 static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
292 {
293         struct dsa_port *port;
294         u32 index;
295         int err;
296
297         /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
298          * driver and before ops->setup() has run, since the switch drivers and
299          * the slave MDIO bus driver rely on these values for probing PHY
300          * devices or not
301          */
302         ds->phys_mii_mask |= dsa_user_ports(ds);
303
304         /* Add the switch to devlink before calling setup, so that setup can
305          * add dpipe tables
306          */
307         ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
308         if (!ds->devlink)
309                 return -ENOMEM;
310
311         err = devlink_register(ds->devlink, ds->dev);
312         if (err)
313                 return err;
314
315         err = ds->ops->setup(ds);
316         if (err < 0)
317                 return err;
318
319         err = dsa_switch_register_notifier(ds);
320         if (err)
321                 return err;
322
323         if (!ds->slave_mii_bus && ds->ops->phy_read) {
324                 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
325                 if (!ds->slave_mii_bus)
326                         return -ENOMEM;
327
328                 dsa_slave_mii_bus_init(ds);
329
330                 err = mdiobus_register(ds->slave_mii_bus);
331                 if (err < 0)
332                         return err;
333         }
334
335         for (index = 0; index < ds->num_ports; index++) {
336                 port = &ds->ports[index];
337                 if (!dsa_port_is_valid(port))
338                         continue;
339
340                 if (dsa_port_is_dsa(port)) {
341                         err = dsa_dsa_port_apply(port);
342                         if (err)
343                                 return err;
344                         continue;
345                 }
346
347                 if (dsa_port_is_cpu(port)) {
348                         err = dsa_cpu_port_apply(port);
349                         if (err)
350                                 return err;
351                         continue;
352                 }
353
354                 err = dsa_user_port_apply(port);
355                 if (err)
356                         continue;
357         }
358
359         return 0;
360 }
361
362 static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
363 {
364         struct dsa_port *port;
365         u32 index;
366
367         for (index = 0; index < ds->num_ports; index++) {
368                 port = &ds->ports[index];
369                 if (!dsa_port_is_valid(port))
370                         continue;
371
372                 if (dsa_port_is_dsa(port)) {
373                         dsa_dsa_port_unapply(port);
374                         continue;
375                 }
376
377                 if (dsa_port_is_cpu(port)) {
378                         dsa_cpu_port_unapply(port);
379                         continue;
380                 }
381
382                 dsa_user_port_unapply(port);
383         }
384
385         if (ds->slave_mii_bus && ds->ops->phy_read)
386                 mdiobus_unregister(ds->slave_mii_bus);
387
388         dsa_switch_unregister_notifier(ds);
389
390         if (ds->devlink) {
391                 devlink_unregister(ds->devlink);
392                 devlink_free(ds->devlink);
393                 ds->devlink = NULL;
394         }
395
396 }
397
398 static int dsa_dst_apply(struct dsa_switch_tree *dst)
399 {
400         struct dsa_switch *ds;
401         u32 index;
402         int err;
403
404         for (index = 0; index < DSA_MAX_SWITCHES; index++) {
405                 ds = dst->ds[index];
406                 if (!ds)
407                         continue;
408
409                 err = dsa_ds_apply(dst, ds);
410                 if (err)
411                         return err;
412         }
413
414         /* If we use a tagging format that doesn't have an ethertype
415          * field, make sure that all packets from this point on get
416          * sent to the tag format's receive function.
417          */
418         wmb();
419         dst->cpu_dp->master->dsa_ptr = dst->cpu_dp;
420
421         err = dsa_master_ethtool_setup(dst->cpu_dp->master);
422         if (err)
423                 return err;
424
425         dst->applied = true;
426
427         return 0;
428 }
429
430 static void dsa_dst_unapply(struct dsa_switch_tree *dst)
431 {
432         struct dsa_switch *ds;
433         u32 index;
434
435         if (!dst->applied)
436                 return;
437
438         dsa_master_ethtool_restore(dst->cpu_dp->master);
439
440         dst->cpu_dp->master->dsa_ptr = NULL;
441
442         /* If we used a tagging format that doesn't have an ethertype
443          * field, make sure that all packets from this point get sent
444          * without the tag and go through the regular receive path.
445          */
446         wmb();
447
448         for (index = 0; index < DSA_MAX_SWITCHES; index++) {
449                 ds = dst->ds[index];
450                 if (!ds)
451                         continue;
452
453                 dsa_ds_unapply(dst, ds);
454         }
455
456         dst->cpu_dp = NULL;
457
458         pr_info("DSA: tree %d unapplied\n", dst->tree);
459         dst->applied = false;
460 }
461
462 static int dsa_cpu_parse(struct dsa_port *port, u32 index,
463                          struct dsa_switch_tree *dst,
464                          struct dsa_switch *ds)
465 {
466         const struct dsa_device_ops *tag_ops;
467         enum dsa_tag_protocol tag_protocol;
468
469         if (!dst->cpu_dp)
470                 dst->cpu_dp = port;
471
472         tag_protocol = ds->ops->get_tag_protocol(ds);
473         tag_ops = dsa_resolve_tag_protocol(tag_protocol);
474         if (IS_ERR(tag_ops)) {
475                 dev_warn(ds->dev, "No tagger for this switch\n");
476                 return PTR_ERR(tag_ops);
477         }
478
479         dst->cpu_dp->tag_ops = tag_ops;
480
481         /* Make a few copies for faster access in master receive hot path */
482         dst->cpu_dp->rcv = dst->cpu_dp->tag_ops->rcv;
483         dst->cpu_dp->dst = dst;
484
485         return 0;
486 }
487
488 static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
489 {
490         struct dsa_port *port;
491         u32 index;
492         int err;
493
494         for (index = 0; index < ds->num_ports; index++) {
495                 port = &ds->ports[index];
496                 if (!dsa_port_is_valid(port) ||
497                     dsa_port_is_dsa(port))
498                         continue;
499
500                 if (dsa_port_is_cpu(port)) {
501                         err = dsa_cpu_parse(port, index, dst, ds);
502                         if (err)
503                                 return err;
504                 }
505
506         }
507
508         pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
509
510         return 0;
511 }
512
513 static int dsa_dst_parse(struct dsa_switch_tree *dst)
514 {
515         struct dsa_switch *ds;
516         struct dsa_port *dp;
517         u32 index;
518         int port;
519         int err;
520
521         for (index = 0; index < DSA_MAX_SWITCHES; index++) {
522                 ds = dst->ds[index];
523                 if (!ds)
524                         continue;
525
526                 err = dsa_ds_parse(dst, ds);
527                 if (err)
528                         return err;
529         }
530
531         if (!dst->cpu_dp) {
532                 pr_warn("Tree has no master device\n");
533                 return -EINVAL;
534         }
535
536         /* Assign the default CPU port to all ports of the fabric */
537         for (index = 0; index < DSA_MAX_SWITCHES; index++) {
538                 ds = dst->ds[index];
539                 if (!ds)
540                         continue;
541
542                 for (port = 0; port < ds->num_ports; port++) {
543                         dp = &ds->ports[port];
544                         if (!dsa_port_is_valid(dp) ||
545                             dsa_port_is_dsa(dp) ||
546                             dsa_port_is_cpu(dp))
547                                 continue;
548
549                         dp->cpu_dp = dst->cpu_dp;
550                 }
551         }
552
553         pr_info("DSA: tree %d parsed\n", dst->tree);
554
555         return 0;
556 }
557
558 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
559 {
560         struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
561         struct device_node *link = of_parse_phandle(dn, "link", 0);
562         const char *name = of_get_property(dn, "label", NULL);
563
564         if (ethernet) {
565                 struct net_device *master;
566
567                 master = of_find_net_device_by_node(ethernet);
568                 if (!master)
569                         return -EPROBE_DEFER;
570
571                 dp->type = DSA_PORT_TYPE_CPU;
572                 dp->master = master;
573         } else if (link) {
574                 dp->type = DSA_PORT_TYPE_DSA;
575         } else {
576                 if (!name)
577                         name = "eth%d";
578
579                 dp->type = DSA_PORT_TYPE_USER;
580                 dp->name = name;
581         }
582
583         dp->dn = dn;
584
585         return 0;
586 }
587
588 static int dsa_parse_ports_of(struct device_node *dn, struct dsa_switch *ds)
589 {
590         struct device_node *ports, *port;
591         struct dsa_port *dp;
592         u32 reg;
593         int err;
594
595         ports = of_get_child_by_name(dn, "ports");
596         if (!ports) {
597                 dev_err(ds->dev, "no ports child node found\n");
598                 return -EINVAL;
599         }
600
601         for_each_available_child_of_node(ports, port) {
602                 err = of_property_read_u32(port, "reg", &reg);
603                 if (err)
604                         return err;
605
606                 if (reg >= ds->num_ports)
607                         return -EINVAL;
608
609                 dp = &ds->ports[reg];
610
611                 err = dsa_port_parse_of(dp, port);
612                 if (err)
613                         return err;
614         }
615
616         return 0;
617 }
618
619 static int dsa_port_parse(struct dsa_port *dp, const char *name,
620                           struct device *dev)
621 {
622         if (!strcmp(name, "cpu")) {
623                 struct net_device *master;
624
625                 master = dsa_dev_to_net_device(dev);
626                 if (!master)
627                         return -EPROBE_DEFER;
628
629                 dev_put(master);
630
631                 dp->type = DSA_PORT_TYPE_CPU;
632                 dp->master = master;
633         } else if (!strcmp(name, "dsa")) {
634                 dp->type = DSA_PORT_TYPE_DSA;
635         } else {
636                 dp->type = DSA_PORT_TYPE_USER;
637         }
638
639         dp->name = name;
640
641         return 0;
642 }
643
644 static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds)
645 {
646         bool valid_name_found = false;
647         struct dsa_port *dp;
648         struct device *dev;
649         const char *name;
650         unsigned int i;
651         int err;
652
653         for (i = 0; i < DSA_MAX_PORTS; i++) {
654                 name = cd->port_names[i];
655                 dev = cd->netdev[i];
656                 dp = &ds->ports[i];
657
658                 if (!name)
659                         continue;
660
661                 err = dsa_port_parse(dp, name, dev);
662                 if (err)
663                         return err;
664
665                 valid_name_found = true;
666         }
667
668         if (!valid_name_found && i == DSA_MAX_PORTS)
669                 return -EINVAL;
670
671         return 0;
672 }
673
674 static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
675 {
676         int err;
677
678         *tree = *index = 0;
679
680         err = of_property_read_u32_index(np, "dsa,member", 0, tree);
681         if (err) {
682                 /* Does not exist, but it is optional */
683                 if (err == -EINVAL)
684                         return 0;
685                 return err;
686         }
687
688         err = of_property_read_u32_index(np, "dsa,member", 1, index);
689         if (err)
690                 return err;
691
692         if (*index >= DSA_MAX_SWITCHES)
693                 return -EINVAL;
694
695         return 0;
696 }
697
698 static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index)
699 {
700         if (!pd)
701                 return -ENODEV;
702
703         /* We do not support complex trees with dsa_chip_data */
704         *tree = 0;
705         *index = 0;
706
707         return 0;
708 }
709
710 static int _dsa_register_switch(struct dsa_switch *ds)
711 {
712         struct dsa_chip_data *pdata = ds->dev->platform_data;
713         struct device_node *np = ds->dev->of_node;
714         struct dsa_switch_tree *dst;
715         u32 tree, index;
716         int i, err;
717
718         if (np) {
719                 err = dsa_parse_member_dn(np, &tree, &index);
720                 if (err)
721                         return err;
722
723                 err = dsa_parse_ports_of(np, ds);
724                 if (err)
725                         return err;
726         } else {
727                 err = dsa_parse_member(pdata, &tree, &index);
728                 if (err)
729                         return err;
730
731                 err = dsa_parse_ports(pdata, ds);
732                 if (err)
733                         return err;
734         }
735
736         dst = dsa_get_dst(tree);
737         if (!dst) {
738                 dst = dsa_add_dst(tree);
739                 if (!dst)
740                         return -ENOMEM;
741         }
742
743         if (dst->ds[index]) {
744                 err = -EBUSY;
745                 goto out;
746         }
747
748         ds->dst = dst;
749         ds->index = index;
750         ds->cd = pdata;
751
752         /* Initialize the routing table */
753         for (i = 0; i < DSA_MAX_SWITCHES; ++i)
754                 ds->rtable[i] = DSA_RTABLE_NONE;
755
756         dsa_dst_add_ds(dst, ds, index);
757
758         err = dsa_dst_complete(dst);
759         if (err < 0)
760                 goto out_del_dst;
761
762         if (err == 1) {
763                 /* Not all switches registered yet */
764                 err = 0;
765                 goto out;
766         }
767
768         if (dst->applied) {
769                 pr_info("DSA: Disjoint trees?\n");
770                 return -EINVAL;
771         }
772
773         err = dsa_dst_parse(dst);
774         if (err)
775                 goto out_del_dst;
776
777         err = dsa_dst_apply(dst);
778         if (err) {
779                 dsa_dst_unapply(dst);
780                 goto out_del_dst;
781         }
782
783         dsa_put_dst(dst);
784         return 0;
785
786 out_del_dst:
787         dsa_dst_del_ds(dst, ds, ds->index);
788 out:
789         dsa_put_dst(dst);
790
791         return err;
792 }
793
794 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
795 {
796         size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
797         struct dsa_switch *ds;
798         int i;
799
800         ds = devm_kzalloc(dev, size, GFP_KERNEL);
801         if (!ds)
802                 return NULL;
803
804         ds->dev = dev;
805         ds->num_ports = n;
806
807         for (i = 0; i < ds->num_ports; ++i) {
808                 ds->ports[i].index = i;
809                 ds->ports[i].ds = ds;
810         }
811
812         return ds;
813 }
814 EXPORT_SYMBOL_GPL(dsa_switch_alloc);
815
816 int dsa_register_switch(struct dsa_switch *ds)
817 {
818         int err;
819
820         mutex_lock(&dsa2_mutex);
821         err = _dsa_register_switch(ds);
822         mutex_unlock(&dsa2_mutex);
823
824         return err;
825 }
826 EXPORT_SYMBOL_GPL(dsa_register_switch);
827
828 static void _dsa_unregister_switch(struct dsa_switch *ds)
829 {
830         struct dsa_switch_tree *dst = ds->dst;
831
832         dsa_dst_unapply(dst);
833
834         dsa_dst_del_ds(dst, ds, ds->index);
835 }
836
837 void dsa_unregister_switch(struct dsa_switch *ds)
838 {
839         mutex_lock(&dsa2_mutex);
840         _dsa_unregister_switch(ds);
841         mutex_unlock(&dsa2_mutex);
842 }
843 EXPORT_SYMBOL_GPL(dsa_unregister_switch);