]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/phy/sfp-bus.c
Merge tag 'wireless-drivers-for-davem-2018-01-17' of git://git.kernel.org/pub/scm...
[mirror_ubuntu-bionic-kernel.git] / drivers / net / phy / sfp-bus.c
CommitLineData
ce0aa27f
RK
1#include <linux/export.h>
2#include <linux/kref.h>
3#include <linux/list.h>
4#include <linux/mutex.h>
5#include <linux/phylink.h>
6#include <linux/rtnetlink.h>
7#include <linux/slab.h>
8
9#include "sfp.h"
10
11struct sfp_bus {
12 struct kref kref;
13 struct list_head node;
14 struct device_node *device_node;
15
16 const struct sfp_socket_ops *socket_ops;
17 struct device *sfp_dev;
18 struct sfp *sfp;
19
20 const struct sfp_upstream_ops *upstream_ops;
21 void *upstream;
22 struct net_device *netdev;
23 struct phy_device *phydev;
24
25 bool registered;
26 bool started;
27};
28
ce0aa27f
RK
29int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
30 unsigned long *support)
31{
32 int port;
33
34 /* port is the physical connector, set this from the connector field. */
35 switch (id->base.connector) {
36 case SFP_CONNECTOR_SC:
37 case SFP_CONNECTOR_FIBERJACK:
38 case SFP_CONNECTOR_LC:
39 case SFP_CONNECTOR_MT_RJ:
40 case SFP_CONNECTOR_MU:
41 case SFP_CONNECTOR_OPTICAL_PIGTAIL:
42 if (support)
43 phylink_set(support, FIBRE);
44 port = PORT_FIBRE;
45 break;
46
47 case SFP_CONNECTOR_RJ45:
48 if (support)
49 phylink_set(support, TP);
50 port = PORT_TP;
51 break;
52
53 case SFP_CONNECTOR_UNSPEC:
54 if (id->base.e1000_base_t) {
55 if (support)
56 phylink_set(support, TP);
57 port = PORT_TP;
58 break;
59 }
60 /* fallthrough */
61 case SFP_CONNECTOR_SG: /* guess */
62 case SFP_CONNECTOR_MPO_1X12:
63 case SFP_CONNECTOR_MPO_2X16:
64 case SFP_CONNECTOR_HSSDC_II:
65 case SFP_CONNECTOR_COPPER_PIGTAIL:
66 case SFP_CONNECTOR_NOSEPARATE:
67 case SFP_CONNECTOR_MXC_2X16:
68 port = PORT_OTHER;
69 break;
70 default:
71 dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
72 id->base.connector);
73 port = PORT_OTHER;
74 break;
75 }
76
77 return port;
78}
79EXPORT_SYMBOL_GPL(sfp_parse_port);
80
81phy_interface_t sfp_parse_interface(struct sfp_bus *bus,
82 const struct sfp_eeprom_id *id)
83{
84 phy_interface_t iface;
85
86 /* Setting the serdes link mode is guesswork: there's no field in
87 * the EEPROM which indicates what mode should be used.
88 *
89 * If the module wants 64b66b, then it must be >= 10G.
90 *
91 * If it's a gigabit-only fiber module, it probably does not have
92 * a PHY, so switch to 802.3z negotiation mode. Otherwise, switch
93 * to SGMII mode (which is required to support non-gigabit speeds).
94 */
95 switch (id->base.encoding) {
96 case SFP_ENCODING_8472_64B66B:
97 iface = PHY_INTERFACE_MODE_10GKR;
98 break;
99
100 case SFP_ENCODING_8B10B:
101 if (!id->base.e1000_base_t &&
102 !id->base.e100_base_lx &&
103 !id->base.e100_base_fx)
104 iface = PHY_INTERFACE_MODE_1000BASEX;
105 else
106 iface = PHY_INTERFACE_MODE_SGMII;
107 break;
108
109 default:
110 iface = PHY_INTERFACE_MODE_NA;
111 dev_err(bus->sfp_dev,
112 "SFP module encoding does not support 8b10b nor 64b66b\n");
113 break;
114 }
115
116 return iface;
117}
118EXPORT_SYMBOL_GPL(sfp_parse_interface);
119
120void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
121 unsigned long *support)
122{
123 phylink_set(support, Autoneg);
124 phylink_set(support, Pause);
125 phylink_set(support, Asym_Pause);
126
127 /* Set ethtool support from the compliance fields. */
128 if (id->base.e10g_base_sr)
129 phylink_set(support, 10000baseSR_Full);
130 if (id->base.e10g_base_lr)
131 phylink_set(support, 10000baseLR_Full);
132 if (id->base.e10g_base_lrm)
133 phylink_set(support, 10000baseLRM_Full);
134 if (id->base.e10g_base_er)
135 phylink_set(support, 10000baseER_Full);
136 if (id->base.e1000_base_sx ||
137 id->base.e1000_base_lx ||
138 id->base.e1000_base_cx)
139 phylink_set(support, 1000baseX_Full);
140 if (id->base.e1000_base_t) {
141 phylink_set(support, 1000baseT_Half);
142 phylink_set(support, 1000baseT_Full);
143 }
144
145 switch (id->base.extended_cc) {
146 case 0x00: /* Unspecified */
147 break;
148 case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */
149 phylink_set(support, 100000baseSR4_Full);
150 phylink_set(support, 25000baseSR_Full);
151 break;
152 case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */
153 case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */
154 phylink_set(support, 100000baseLR4_ER4_Full);
155 break;
156 case 0x0b: /* 100Gbase-CR4 or 25Gbase-CR CA-L */
157 case 0x0c: /* 25Gbase-CR CA-S */
158 case 0x0d: /* 25Gbase-CR CA-N */
159 phylink_set(support, 100000baseCR4_Full);
160 phylink_set(support, 25000baseCR_Full);
161 break;
162 default:
163 dev_warn(bus->sfp_dev,
164 "Unknown/unsupported extended compliance code: 0x%02x\n",
165 id->base.extended_cc);
166 break;
167 }
168
169 /* For fibre channel SFP, derive possible BaseX modes */
170 if (id->base.fc_speed_100 ||
171 id->base.fc_speed_200 ||
172 id->base.fc_speed_400) {
173 if (id->base.br_nominal >= 31)
174 phylink_set(support, 2500baseX_Full);
175 if (id->base.br_nominal >= 12)
176 phylink_set(support, 1000baseX_Full);
177 }
178
179 switch (id->base.connector) {
180 case SFP_CONNECTOR_SC:
181 case SFP_CONNECTOR_FIBERJACK:
182 case SFP_CONNECTOR_LC:
183 case SFP_CONNECTOR_MT_RJ:
184 case SFP_CONNECTOR_MU:
185 case SFP_CONNECTOR_OPTICAL_PIGTAIL:
186 break;
187
188 case SFP_CONNECTOR_UNSPEC:
189 if (id->base.e1000_base_t)
190 break;
191
192 case SFP_CONNECTOR_SG: /* guess */
193 case SFP_CONNECTOR_MPO_1X12:
194 case SFP_CONNECTOR_MPO_2X16:
195 case SFP_CONNECTOR_HSSDC_II:
196 case SFP_CONNECTOR_COPPER_PIGTAIL:
197 case SFP_CONNECTOR_NOSEPARATE:
198 case SFP_CONNECTOR_MXC_2X16:
199 default:
200 /* a guess at the supported link modes */
201 dev_warn(bus->sfp_dev,
202 "Guessing link modes, please report...\n");
203 phylink_set(support, 1000baseT_Half);
204 phylink_set(support, 1000baseT_Full);
205 break;
206 }
207}
208EXPORT_SYMBOL_GPL(sfp_parse_support);
209
ce0aa27f
RK
210static LIST_HEAD(sfp_buses);
211static DEFINE_MUTEX(sfp_mutex);
212
213static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
214{
215 return bus->registered ? bus->upstream_ops : NULL;
216}
217
218static struct sfp_bus *sfp_bus_get(struct device_node *np)
219{
220 struct sfp_bus *sfp, *new, *found = NULL;
221
222 new = kzalloc(sizeof(*new), GFP_KERNEL);
223
224 mutex_lock(&sfp_mutex);
225
226 list_for_each_entry(sfp, &sfp_buses, node) {
227 if (sfp->device_node == np) {
228 kref_get(&sfp->kref);
229 found = sfp;
230 break;
231 }
232 }
233
234 if (!found && new) {
235 kref_init(&new->kref);
236 new->device_node = np;
237 list_add(&new->node, &sfp_buses);
238 found = new;
239 new = NULL;
240 }
241
242 mutex_unlock(&sfp_mutex);
243
244 kfree(new);
245
246 return found;
247}
248
249static void sfp_bus_release(struct kref *kref) __releases(sfp_mutex)
250{
251 struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
252
253 list_del(&bus->node);
254 mutex_unlock(&sfp_mutex);
255 kfree(bus);
256}
257
258static void sfp_bus_put(struct sfp_bus *bus)
259{
260 kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
261}
262
263static int sfp_register_bus(struct sfp_bus *bus)
264{
265 const struct sfp_upstream_ops *ops = bus->upstream_ops;
266 int ret;
267
268 if (ops) {
269 if (ops->link_down)
270 ops->link_down(bus->upstream);
271 if (ops->connect_phy && bus->phydev) {
272 ret = ops->connect_phy(bus->upstream, bus->phydev);
273 if (ret)
274 return ret;
275 }
276 }
277 if (bus->started)
278 bus->socket_ops->start(bus->sfp);
279 bus->registered = true;
280 return 0;
281}
282
283static void sfp_unregister_bus(struct sfp_bus *bus)
284{
285 const struct sfp_upstream_ops *ops = bus->upstream_ops;
286
287 if (bus->registered) {
288 if (bus->started)
289 bus->socket_ops->stop(bus->sfp);
290 if (bus->phydev && ops && ops->disconnect_phy)
291 ops->disconnect_phy(bus->upstream);
292 }
293 bus->registered = false;
294}
295
ce0aa27f
RK
296int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
297{
298 if (!bus->registered)
299 return -ENOIOCTLCMD;
300 return bus->socket_ops->module_info(bus->sfp, modinfo);
301}
302EXPORT_SYMBOL_GPL(sfp_get_module_info);
303
304int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
516b29ed 305 u8 *data)
ce0aa27f
RK
306{
307 if (!bus->registered)
308 return -ENOIOCTLCMD;
309 return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
310}
311EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
312
313void sfp_upstream_start(struct sfp_bus *bus)
314{
315 if (bus->registered)
316 bus->socket_ops->start(bus->sfp);
317 bus->started = true;
318}
319EXPORT_SYMBOL_GPL(sfp_upstream_start);
320
321void sfp_upstream_stop(struct sfp_bus *bus)
322{
323 if (bus->registered)
324 bus->socket_ops->stop(bus->sfp);
325 bus->started = false;
326}
327EXPORT_SYMBOL_GPL(sfp_upstream_stop);
328
329struct sfp_bus *sfp_register_upstream(struct device_node *np,
516b29ed
FF
330 struct net_device *ndev, void *upstream,
331 const struct sfp_upstream_ops *ops)
ce0aa27f
RK
332{
333 struct sfp_bus *bus = sfp_bus_get(np);
334 int ret = 0;
335
336 if (bus) {
337 rtnl_lock();
338 bus->upstream_ops = ops;
339 bus->upstream = upstream;
340 bus->netdev = ndev;
341
342 if (bus->sfp)
343 ret = sfp_register_bus(bus);
344 rtnl_unlock();
345 }
346
347 if (ret) {
348 sfp_bus_put(bus);
349 bus = NULL;
350 }
351
352 return bus;
353}
354EXPORT_SYMBOL_GPL(sfp_register_upstream);
355
356void sfp_unregister_upstream(struct sfp_bus *bus)
357{
358 rtnl_lock();
0b2122e4
RK
359 if (bus->sfp)
360 sfp_unregister_bus(bus);
ce0aa27f
RK
361 bus->upstream = NULL;
362 bus->netdev = NULL;
363 rtnl_unlock();
364
365 sfp_bus_put(bus);
366}
367EXPORT_SYMBOL_GPL(sfp_unregister_upstream);
368
ce0aa27f
RK
369/* Socket driver entry points */
370int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
371{
372 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
373 int ret = 0;
374
375 if (ops && ops->connect_phy)
376 ret = ops->connect_phy(bus->upstream, phydev);
377
378 if (ret == 0)
379 bus->phydev = phydev;
380
381 return ret;
382}
383EXPORT_SYMBOL_GPL(sfp_add_phy);
384
385void sfp_remove_phy(struct sfp_bus *bus)
386{
387 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
388
389 if (ops && ops->disconnect_phy)
390 ops->disconnect_phy(bus->upstream);
391 bus->phydev = NULL;
392}
393EXPORT_SYMBOL_GPL(sfp_remove_phy);
394
ce0aa27f
RK
395void sfp_link_up(struct sfp_bus *bus)
396{
397 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
398
399 if (ops && ops->link_up)
400 ops->link_up(bus->upstream);
401}
402EXPORT_SYMBOL_GPL(sfp_link_up);
403
404void sfp_link_down(struct sfp_bus *bus)
405{
406 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
407
408 if (ops && ops->link_down)
409 ops->link_down(bus->upstream);
410}
411EXPORT_SYMBOL_GPL(sfp_link_down);
412
413int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
414{
415 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
416 int ret = 0;
417
418 if (ops && ops->module_insert)
419 ret = ops->module_insert(bus->upstream, id);
420
421 return ret;
422}
423EXPORT_SYMBOL_GPL(sfp_module_insert);
424
425void sfp_module_remove(struct sfp_bus *bus)
426{
427 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
428
429 if (ops && ops->module_remove)
430 ops->module_remove(bus->upstream);
431}
432EXPORT_SYMBOL_GPL(sfp_module_remove);
433
434struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
435 const struct sfp_socket_ops *ops)
436{
437 struct sfp_bus *bus = sfp_bus_get(dev->of_node);
438 int ret = 0;
439
440 if (bus) {
441 rtnl_lock();
442 bus->sfp_dev = dev;
443 bus->sfp = sfp;
444 bus->socket_ops = ops;
445
446 if (bus->netdev)
447 ret = sfp_register_bus(bus);
448 rtnl_unlock();
449 }
450
451 if (ret) {
452 sfp_bus_put(bus);
453 bus = NULL;
454 }
455
456 return bus;
457}
458EXPORT_SYMBOL_GPL(sfp_register_socket);
459
460void sfp_unregister_socket(struct sfp_bus *bus)
461{
462 rtnl_lock();
0b2122e4
RK
463 if (bus->netdev)
464 sfp_unregister_bus(bus);
ce0aa27f
RK
465 bus->sfp_dev = NULL;
466 bus->sfp = NULL;
467 bus->socket_ops = NULL;
468 rtnl_unlock();
469
470 sfp_bus_put(bus);
471}
472EXPORT_SYMBOL_GPL(sfp_unregister_socket);