generic: backport support for MT7530 DSA port mirroring
[oweals/openwrt.git] / target / linux / generic / backport-5.4 / 716-v5.4-net-sfp-move-fwnode-parsing-into-sfp-bus-layer.patch
1 From 4054955f0da08c81d42220cb445820d474f1ac92 Mon Sep 17 00:00:00 2001
2 From: Russell King <rmk+kernel@armlinux.org.uk>
3 Date: Sat, 14 Sep 2019 14:21:22 +0100
4 Subject: [PATCH 614/660] net: sfp: move fwnode parsing into sfp-bus layer
5
6 Rather than parsing the sfp firmware node in phylink, parse it in the
7 sfp-bus code, so we can re-use this code for PHYs without having to
8 duplicate the parsing.
9
10 Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
11 ---
12  drivers/net/phy/phylink.c | 21 ++++---------
13  drivers/net/phy/sfp-bus.c | 65 +++++++++++++++++++++++++--------------
14  include/linux/sfp.h       | 10 +++---
15  3 files changed, 53 insertions(+), 43 deletions(-)
16
17 --- a/drivers/net/phy/phylink.c
18 +++ b/drivers/net/phy/phylink.c
19 @@ -565,26 +565,17 @@ static const struct sfp_upstream_ops sfp
20  static int phylink_register_sfp(struct phylink *pl,
21                                 struct fwnode_handle *fwnode)
22  {
23 -       struct fwnode_reference_args ref;
24 +       struct sfp_bus *bus;
25         int ret;
26  
27 -       if (!fwnode)
28 -               return 0;
29 -
30 -       ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
31 -                                                0, 0, &ref);
32 -       if (ret < 0) {
33 -               if (ret == -ENOENT)
34 -                       return 0;
35 -
36 -               phylink_err(pl, "unable to parse \"sfp\" node: %d\n",
37 -                           ret);
38 +       bus = sfp_register_upstream_node(fwnode, pl, &sfp_phylink_ops);
39 +       if (IS_ERR(bus)) {
40 +               ret = PTR_ERR(bus);
41 +               phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
42                 return ret;
43         }
44  
45 -       pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl, &sfp_phylink_ops);
46 -       if (!pl->sfp_bus)
47 -               return -ENOMEM;
48 +       pl->sfp_bus = bus;
49  
50         return 0;
51  }
52 --- a/drivers/net/phy/sfp-bus.c
53 +++ b/drivers/net/phy/sfp-bus.c
54 @@ -4,6 +4,7 @@
55  #include <linux/list.h>
56  #include <linux/mutex.h>
57  #include <linux/phylink.h>
58 +#include <linux/property.h>
59  #include <linux/rtnetlink.h>
60  #include <linux/slab.h>
61  
62 @@ -445,45 +446,63 @@ static void sfp_upstream_clear(struct sf
63  }
64  
65  /**
66 - * sfp_register_upstream() - Register the neighbouring device
67 - * @fwnode: firmware node for the SFP bus
68 + * sfp_register_upstream_node() - parse and register the neighbouring device
69 + * @fwnode: firmware node for the parent device (MAC or PHY)
70   * @upstream: the upstream private data
71   * @ops: the upstream's &struct sfp_upstream_ops
72   *
73 - * Register the upstream device (eg, PHY) with the SFP bus. MAC drivers
74 - * should use phylink, which will call this function for them. Returns
75 - * a pointer to the allocated &struct sfp_bus.
76 + * Parse the parent device's firmware node for a SFP bus, and register the
77 + * SFP bus using sfp_register_upstream().
78   *
79 - * On error, returns %NULL.
80 + * Returns: on success, a pointer to the sfp_bus structure,
81 + *         %NULL if no SFP is specified,
82 + *         on failure, an error pointer value:
83 + *             corresponding to the errors detailed for
84 + *             fwnode_property_get_reference_args().
85 + *             %-ENOMEM if we failed to allocate the bus.
86 + *             an error from the upstream's connect_phy() method.
87   */
88 -struct sfp_bus *sfp_register_upstream(struct fwnode_handle *fwnode,
89 -                                     void *upstream,
90 -                                     const struct sfp_upstream_ops *ops)
91 -{
92 -       struct sfp_bus *bus = sfp_bus_get(fwnode);
93 -       int ret = 0;
94 -
95 -       if (bus) {
96 -               rtnl_lock();
97 -               bus->upstream_ops = ops;
98 -               bus->upstream = upstream;
99 +struct sfp_bus *sfp_register_upstream_node(struct fwnode_handle *fwnode,
100 +                                          void *upstream,
101 +                                          const struct sfp_upstream_ops *ops)
102 +{
103 +       struct fwnode_reference_args ref;
104 +       struct sfp_bus *bus;
105 +       int ret;
106  
107 -               if (bus->sfp) {
108 -                       ret = sfp_register_bus(bus);
109 -                       if (ret)
110 -                               sfp_upstream_clear(bus);
111 -               }
112 -               rtnl_unlock();
113 +       ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
114 +                                                0, 0, &ref);
115 +       if (ret == -ENOENT)
116 +               return NULL;
117 +       else if (ret < 0)
118 +               return ERR_PTR(ret);
119 +
120 +       bus = sfp_bus_get(ref.fwnode);
121 +       fwnode_handle_put(ref.fwnode);
122 +       if (!bus)
123 +               return ERR_PTR(-ENOMEM);
124 +
125 +       rtnl_lock();
126 +       bus->upstream_ops = ops;
127 +       bus->upstream = upstream;
128 +
129 +       if (bus->sfp) {
130 +               ret = sfp_register_bus(bus);
131 +               if (ret)
132 +                       sfp_upstream_clear(bus);
133 +       } else {
134 +               ret = 0;
135         }
136 +       rtnl_unlock();
137  
138         if (ret) {
139                 sfp_bus_put(bus);
140 -               bus = NULL;
141 +               bus = ERR_PTR(ret);
142         }
143  
144         return bus;
145  }
146 -EXPORT_SYMBOL_GPL(sfp_register_upstream);
147 +EXPORT_SYMBOL_GPL(sfp_register_upstream_node);
148  
149  /**
150   * sfp_unregister_upstream() - Unregister sfp bus
151 --- a/include/linux/sfp.h
152 +++ b/include/linux/sfp.h
153 @@ -508,9 +508,9 @@ int sfp_get_module_eeprom(struct sfp_bus
154                           u8 *data);
155  void sfp_upstream_start(struct sfp_bus *bus);
156  void sfp_upstream_stop(struct sfp_bus *bus);
157 -struct sfp_bus *sfp_register_upstream(struct fwnode_handle *fwnode,
158 -                                     void *upstream,
159 -                                     const struct sfp_upstream_ops *ops);
160 +struct sfp_bus *sfp_register_upstream_node(struct fwnode_handle *fwnode,
161 +                                          void *upstream,
162 +                                          const struct sfp_upstream_ops *ops);
163  void sfp_unregister_upstream(struct sfp_bus *bus);
164  #else
165  static inline int sfp_parse_port(struct sfp_bus *bus,
166 @@ -553,11 +553,11 @@ static inline void sfp_upstream_stop(str
167  {
168  }
169  
170 -static inline struct sfp_bus *sfp_register_upstream(
171 +static inline struct sfp_bus *sfp_register_upstream_node(
172         struct fwnode_handle *fwnode, void *upstream,
173         const struct sfp_upstream_ops *ops)
174  {
175 -       return (struct sfp_bus *)-1;
176 +       return NULL;
177  }
178  
179  static inline void sfp_unregister_upstream(struct sfp_bus *bus)