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