d037aab6a71d54d84c7d527f77a342e96e1c62f4
[linux-2.6-microblaze.git] / drivers / net / phy / sfp-bus.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/export.h>
3 #include <linux/kref.h>
4 #include <linux/list.h>
5 #include <linux/mutex.h>
6 #include <linux/phylink.h>
7 #include <linux/property.h>
8 #include <linux/rtnetlink.h>
9 #include <linux/slab.h>
10
11 #include "sfp.h"
12
13 /**
14  * struct sfp_bus - internal representation of a sfp bus
15  */
16 struct sfp_bus {
17         /* private: */
18         struct kref kref;
19         struct list_head node;
20         struct fwnode_handle *fwnode;
21
22         const struct sfp_socket_ops *socket_ops;
23         struct device *sfp_dev;
24         struct sfp *sfp;
25
26         const struct sfp_upstream_ops *upstream_ops;
27         void *upstream;
28         struct phy_device *phydev;
29
30         bool registered;
31         bool started;
32 };
33
34 /**
35  * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
36  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
37  * @id: a pointer to the module's &struct sfp_eeprom_id
38  * @support: optional pointer to an array of unsigned long for the
39  *   ethtool support mask
40  *
41  * Parse the EEPROM identification given in @id, and return one of
42  * %PORT_TP, %PORT_FIBRE or %PORT_OTHER. If @support is non-%NULL,
43  * also set the ethtool %ETHTOOL_LINK_MODE_xxx_BIT corresponding with
44  * the connector type.
45  *
46  * If the port type is not known, returns %PORT_OTHER.
47  */
48 int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
49                    unsigned long *support)
50 {
51         int port;
52
53         /* port is the physical connector, set this from the connector field. */
54         switch (id->base.connector) {
55         case SFP_CONNECTOR_SC:
56         case SFP_CONNECTOR_FIBERJACK:
57         case SFP_CONNECTOR_LC:
58         case SFP_CONNECTOR_MT_RJ:
59         case SFP_CONNECTOR_MU:
60         case SFP_CONNECTOR_OPTICAL_PIGTAIL:
61                 port = PORT_FIBRE;
62                 break;
63
64         case SFP_CONNECTOR_RJ45:
65                 port = PORT_TP;
66                 break;
67
68         case SFP_CONNECTOR_COPPER_PIGTAIL:
69                 port = PORT_DA;
70                 break;
71
72         case SFP_CONNECTOR_UNSPEC:
73                 if (id->base.e1000_base_t) {
74                         port = PORT_TP;
75                         break;
76                 }
77                 /* fallthrough */
78         case SFP_CONNECTOR_SG: /* guess */
79         case SFP_CONNECTOR_MPO_1X12:
80         case SFP_CONNECTOR_MPO_2X16:
81         case SFP_CONNECTOR_HSSDC_II:
82         case SFP_CONNECTOR_NOSEPARATE:
83         case SFP_CONNECTOR_MXC_2X16:
84                 port = PORT_OTHER;
85                 break;
86         default:
87                 dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
88                          id->base.connector);
89                 port = PORT_OTHER;
90                 break;
91         }
92
93         if (support) {
94                 switch (port) {
95                 case PORT_FIBRE:
96                         phylink_set(support, FIBRE);
97                         break;
98
99                 case PORT_TP:
100                         phylink_set(support, TP);
101                         break;
102                 }
103         }
104
105         return port;
106 }
107 EXPORT_SYMBOL_GPL(sfp_parse_port);
108
109 /**
110  * sfp_parse_support() - Parse the eeprom id for supported link modes
111  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
112  * @id: a pointer to the module's &struct sfp_eeprom_id
113  * @support: pointer to an array of unsigned long for the ethtool support mask
114  *
115  * Parse the EEPROM identification information and derive the supported
116  * ethtool link modes for the module.
117  */
118 void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
119                        unsigned long *support)
120 {
121         unsigned int br_min, br_nom, br_max;
122         __ETHTOOL_DECLARE_LINK_MODE_MASK(modes) = { 0, };
123
124         /* Decode the bitrate information to MBd */
125         br_min = br_nom = br_max = 0;
126         if (id->base.br_nominal) {
127                 if (id->base.br_nominal != 255) {
128                         br_nom = id->base.br_nominal * 100;
129                         br_min = br_nom - id->base.br_nominal * id->ext.br_min;
130                         br_max = br_nom + id->base.br_nominal * id->ext.br_max;
131                 } else if (id->ext.br_max) {
132                         br_nom = 250 * id->ext.br_max;
133                         br_max = br_nom + br_nom * id->ext.br_min / 100;
134                         br_min = br_nom - br_nom * id->ext.br_min / 100;
135                 }
136
137                 /* When using passive cables, in case neither BR,min nor BR,max
138                  * are specified, set br_min to 0 as the nominal value is then
139                  * used as the maximum.
140                  */
141                 if (br_min == br_max && id->base.sfp_ct_passive)
142                         br_min = 0;
143         }
144
145         /* Set ethtool support from the compliance fields. */
146         if (id->base.e10g_base_sr)
147                 phylink_set(modes, 10000baseSR_Full);
148         if (id->base.e10g_base_lr)
149                 phylink_set(modes, 10000baseLR_Full);
150         if (id->base.e10g_base_lrm)
151                 phylink_set(modes, 10000baseLRM_Full);
152         if (id->base.e10g_base_er)
153                 phylink_set(modes, 10000baseER_Full);
154         if (id->base.e1000_base_sx ||
155             id->base.e1000_base_lx ||
156             id->base.e1000_base_cx)
157                 phylink_set(modes, 1000baseX_Full);
158         if (id->base.e1000_base_t) {
159                 phylink_set(modes, 1000baseT_Half);
160                 phylink_set(modes, 1000baseT_Full);
161         }
162
163         /* 1000Base-PX or 1000Base-BX10 */
164         if ((id->base.e_base_px || id->base.e_base_bx10) &&
165             br_min <= 1300 && br_max >= 1200)
166                 phylink_set(modes, 1000baseX_Full);
167
168         /* For active or passive cables, select the link modes
169          * based on the bit rates and the cable compliance bytes.
170          */
171         if ((id->base.sfp_ct_passive || id->base.sfp_ct_active) && br_nom) {
172                 /* This may look odd, but some manufacturers use 12000MBd */
173                 if (br_min <= 12000 && br_max >= 10300)
174                         phylink_set(modes, 10000baseCR_Full);
175                 if (br_min <= 3200 && br_max >= 3100)
176                         phylink_set(modes, 2500baseX_Full);
177                 if (br_min <= 1300 && br_max >= 1200)
178                         phylink_set(modes, 1000baseX_Full);
179         }
180         if (id->base.sfp_ct_passive) {
181                 if (id->base.passive.sff8431_app_e)
182                         phylink_set(modes, 10000baseCR_Full);
183         }
184         if (id->base.sfp_ct_active) {
185                 if (id->base.active.sff8431_app_e ||
186                     id->base.active.sff8431_lim) {
187                         phylink_set(modes, 10000baseCR_Full);
188                 }
189         }
190
191         switch (id->base.extended_cc) {
192         case 0x00: /* Unspecified */
193                 break;
194         case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */
195                 phylink_set(modes, 100000baseSR4_Full);
196                 phylink_set(modes, 25000baseSR_Full);
197                 break;
198         case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */
199         case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */
200                 phylink_set(modes, 100000baseLR4_ER4_Full);
201                 break;
202         case 0x0b: /* 100Gbase-CR4 or 25Gbase-CR CA-L */
203         case 0x0c: /* 25Gbase-CR CA-S */
204         case 0x0d: /* 25Gbase-CR CA-N */
205                 phylink_set(modes, 100000baseCR4_Full);
206                 phylink_set(modes, 25000baseCR_Full);
207                 break;
208         default:
209                 dev_warn(bus->sfp_dev,
210                          "Unknown/unsupported extended compliance code: 0x%02x\n",
211                          id->base.extended_cc);
212                 break;
213         }
214
215         /* For fibre channel SFP, derive possible BaseX modes */
216         if (id->base.fc_speed_100 ||
217             id->base.fc_speed_200 ||
218             id->base.fc_speed_400) {
219                 if (id->base.br_nominal >= 31)
220                         phylink_set(modes, 2500baseX_Full);
221                 if (id->base.br_nominal >= 12)
222                         phylink_set(modes, 1000baseX_Full);
223         }
224
225         /* If we haven't discovered any modes that this module supports, try
226          * the encoding and bitrate to determine supported modes. Some BiDi
227          * modules (eg, 1310nm/1550nm) are not 1000BASE-BX compliant due to
228          * the differing wavelengths, so do not set any transceiver bits.
229          */
230         if (bitmap_empty(modes, __ETHTOOL_LINK_MODE_MASK_NBITS)) {
231                 /* If the encoding and bit rate allows 1000baseX */
232                 if (id->base.encoding == SFP_ENCODING_8B10B && br_nom &&
233                     br_min <= 1300 && br_max >= 1200)
234                         phylink_set(modes, 1000baseX_Full);
235         }
236
237         bitmap_or(support, support, modes, __ETHTOOL_LINK_MODE_MASK_NBITS);
238
239         phylink_set(support, Autoneg);
240         phylink_set(support, Pause);
241         phylink_set(support, Asym_Pause);
242 }
243 EXPORT_SYMBOL_GPL(sfp_parse_support);
244
245 /**
246  * sfp_select_interface() - Select appropriate phy_interface_t mode
247  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
248  * @id: a pointer to the module's &struct sfp_eeprom_id
249  * @link_modes: ethtool link modes mask
250  *
251  * Derive the phy_interface_t mode for the information found in the
252  * module's identifying EEPROM and the link modes mask. There is no
253  * standard or defined way to derive this information, so we decide
254  * based upon the link mode mask.
255  */
256 phy_interface_t sfp_select_interface(struct sfp_bus *bus,
257                                      const struct sfp_eeprom_id *id,
258                                      unsigned long *link_modes)
259 {
260         if (phylink_test(link_modes, 10000baseCR_Full) ||
261             phylink_test(link_modes, 10000baseSR_Full) ||
262             phylink_test(link_modes, 10000baseLR_Full) ||
263             phylink_test(link_modes, 10000baseLRM_Full) ||
264             phylink_test(link_modes, 10000baseER_Full))
265                 return PHY_INTERFACE_MODE_10GKR;
266
267         if (phylink_test(link_modes, 2500baseX_Full))
268                 return PHY_INTERFACE_MODE_2500BASEX;
269
270         if (id->base.e1000_base_t ||
271             id->base.e100_base_lx ||
272             id->base.e100_base_fx)
273                 return PHY_INTERFACE_MODE_SGMII;
274
275         if (phylink_test(link_modes, 1000baseX_Full))
276                 return PHY_INTERFACE_MODE_1000BASEX;
277
278         dev_warn(bus->sfp_dev, "Unable to ascertain link mode\n");
279
280         return PHY_INTERFACE_MODE_NA;
281 }
282 EXPORT_SYMBOL_GPL(sfp_select_interface);
283
284 static LIST_HEAD(sfp_buses);
285 static DEFINE_MUTEX(sfp_mutex);
286
287 static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
288 {
289         return bus->registered ? bus->upstream_ops : NULL;
290 }
291
292 static struct sfp_bus *sfp_bus_get(struct fwnode_handle *fwnode)
293 {
294         struct sfp_bus *sfp, *new, *found = NULL;
295
296         new = kzalloc(sizeof(*new), GFP_KERNEL);
297
298         mutex_lock(&sfp_mutex);
299
300         list_for_each_entry(sfp, &sfp_buses, node) {
301                 if (sfp->fwnode == fwnode) {
302                         kref_get(&sfp->kref);
303                         found = sfp;
304                         break;
305                 }
306         }
307
308         if (!found && new) {
309                 kref_init(&new->kref);
310                 new->fwnode = fwnode;
311                 list_add(&new->node, &sfp_buses);
312                 found = new;
313                 new = NULL;
314         }
315
316         mutex_unlock(&sfp_mutex);
317
318         kfree(new);
319
320         return found;
321 }
322
323 static void sfp_bus_release(struct kref *kref)
324 {
325         struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
326
327         list_del(&bus->node);
328         mutex_unlock(&sfp_mutex);
329         kfree(bus);
330 }
331
332 static void sfp_bus_put(struct sfp_bus *bus)
333 {
334         kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
335 }
336
337 static int sfp_register_bus(struct sfp_bus *bus)
338 {
339         const struct sfp_upstream_ops *ops = bus->upstream_ops;
340         int ret;
341
342         if (ops) {
343                 if (ops->link_down)
344                         ops->link_down(bus->upstream);
345                 if (ops->connect_phy && bus->phydev) {
346                         ret = ops->connect_phy(bus->upstream, bus->phydev);
347                         if (ret)
348                                 return ret;
349                 }
350         }
351         bus->socket_ops->attach(bus->sfp);
352         if (bus->started)
353                 bus->socket_ops->start(bus->sfp);
354         bus->upstream_ops->attach(bus->upstream, bus);
355         bus->registered = true;
356         return 0;
357 }
358
359 static void sfp_unregister_bus(struct sfp_bus *bus)
360 {
361         const struct sfp_upstream_ops *ops = bus->upstream_ops;
362
363         if (bus->registered) {
364                 bus->upstream_ops->detach(bus->upstream, bus);
365                 if (bus->started)
366                         bus->socket_ops->stop(bus->sfp);
367                 bus->socket_ops->detach(bus->sfp);
368                 if (bus->phydev && ops && ops->disconnect_phy)
369                         ops->disconnect_phy(bus->upstream);
370         }
371         bus->registered = false;
372 }
373
374 /**
375  * sfp_get_module_info() - Get the ethtool_modinfo for a SFP module
376  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
377  * @modinfo: a &struct ethtool_modinfo
378  *
379  * Fill in the type and eeprom_len parameters in @modinfo for a module on
380  * the sfp bus specified by @bus.
381  *
382  * Returns 0 on success or a negative errno number.
383  */
384 int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
385 {
386         return bus->socket_ops->module_info(bus->sfp, modinfo);
387 }
388 EXPORT_SYMBOL_GPL(sfp_get_module_info);
389
390 /**
391  * sfp_get_module_eeprom() - Read the SFP module EEPROM
392  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
393  * @ee: a &struct ethtool_eeprom
394  * @data: buffer to contain the EEPROM data (must be at least @ee->len bytes)
395  *
396  * Read the EEPROM as specified by the supplied @ee. See the documentation
397  * for &struct ethtool_eeprom for the region to be read.
398  *
399  * Returns 0 on success or a negative errno number.
400  */
401 int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
402                           u8 *data)
403 {
404         return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
405 }
406 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
407
408 /**
409  * sfp_upstream_start() - Inform the SFP that the network device is up
410  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
411  *
412  * Inform the SFP socket that the network device is now up, so that the
413  * module can be enabled by allowing TX_DISABLE to be deasserted. This
414  * should be called from the network device driver's &struct net_device_ops
415  * ndo_open() method.
416  */
417 void sfp_upstream_start(struct sfp_bus *bus)
418 {
419         if (bus->registered)
420                 bus->socket_ops->start(bus->sfp);
421         bus->started = true;
422 }
423 EXPORT_SYMBOL_GPL(sfp_upstream_start);
424
425 /**
426  * sfp_upstream_stop() - Inform the SFP that the network device is down
427  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
428  *
429  * Inform the SFP socket that the network device is now up, so that the
430  * module can be disabled by asserting TX_DISABLE, disabling the laser
431  * in optical modules. This should be called from the network device
432  * driver's &struct net_device_ops ndo_stop() method.
433  */
434 void sfp_upstream_stop(struct sfp_bus *bus)
435 {
436         if (bus->registered)
437                 bus->socket_ops->stop(bus->sfp);
438         bus->started = false;
439 }
440 EXPORT_SYMBOL_GPL(sfp_upstream_stop);
441
442 static void sfp_upstream_clear(struct sfp_bus *bus)
443 {
444         bus->upstream_ops = NULL;
445         bus->upstream = NULL;
446 }
447
448 /**
449  * sfp_register_upstream_node() - parse and register the neighbouring device
450  * @fwnode: firmware node for the parent device (MAC or PHY)
451  * @upstream: the upstream private data
452  * @ops: the upstream's &struct sfp_upstream_ops
453  *
454  * Parse the parent device's firmware node for a SFP bus, and register the
455  * SFP bus using sfp_register_upstream().
456  *
457  * Returns: on success, a pointer to the sfp_bus structure,
458  *          %NULL if no SFP is specified,
459  *          on failure, an error pointer value:
460  *              corresponding to the errors detailed for
461  *              fwnode_property_get_reference_args().
462  *              %-ENOMEM if we failed to allocate the bus.
463  *              an error from the upstream's connect_phy() method.
464  */
465 struct sfp_bus *sfp_register_upstream_node(struct fwnode_handle *fwnode,
466                                            void *upstream,
467                                            const struct sfp_upstream_ops *ops)
468 {
469         struct fwnode_reference_args ref;
470         struct sfp_bus *bus;
471         int ret;
472
473         ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
474                                                  0, 0, &ref);
475         if (ret == -ENOENT)
476                 return NULL;
477         else if (ret < 0)
478                 return ERR_PTR(ret);
479
480         bus = sfp_bus_get(ref.fwnode);
481         fwnode_handle_put(ref.fwnode);
482         if (!bus)
483                 return ERR_PTR(-ENOMEM);
484
485         rtnl_lock();
486         bus->upstream_ops = ops;
487         bus->upstream = upstream;
488
489         if (bus->sfp) {
490                 ret = sfp_register_bus(bus);
491                 if (ret)
492                         sfp_upstream_clear(bus);
493         } else {
494                 ret = 0;
495         }
496         rtnl_unlock();
497
498         if (ret) {
499                 sfp_bus_put(bus);
500                 bus = ERR_PTR(ret);
501         }
502
503         return bus;
504 }
505 EXPORT_SYMBOL_GPL(sfp_register_upstream_node);
506
507 /**
508  * sfp_unregister_upstream() - Unregister sfp bus
509  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
510  *
511  * Unregister a previously registered upstream connection for the SFP
512  * module. @bus is returned from sfp_register_upstream().
513  */
514 void sfp_unregister_upstream(struct sfp_bus *bus)
515 {
516         rtnl_lock();
517         if (bus->sfp)
518                 sfp_unregister_bus(bus);
519         sfp_upstream_clear(bus);
520         rtnl_unlock();
521
522         sfp_bus_put(bus);
523 }
524 EXPORT_SYMBOL_GPL(sfp_unregister_upstream);
525
526 /* Socket driver entry points */
527 int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
528 {
529         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
530         int ret = 0;
531
532         if (ops && ops->connect_phy)
533                 ret = ops->connect_phy(bus->upstream, phydev);
534
535         if (ret == 0)
536                 bus->phydev = phydev;
537
538         return ret;
539 }
540 EXPORT_SYMBOL_GPL(sfp_add_phy);
541
542 void sfp_remove_phy(struct sfp_bus *bus)
543 {
544         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
545
546         if (ops && ops->disconnect_phy)
547                 ops->disconnect_phy(bus->upstream);
548         bus->phydev = NULL;
549 }
550 EXPORT_SYMBOL_GPL(sfp_remove_phy);
551
552 void sfp_link_up(struct sfp_bus *bus)
553 {
554         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
555
556         if (ops && ops->link_up)
557                 ops->link_up(bus->upstream);
558 }
559 EXPORT_SYMBOL_GPL(sfp_link_up);
560
561 void sfp_link_down(struct sfp_bus *bus)
562 {
563         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
564
565         if (ops && ops->link_down)
566                 ops->link_down(bus->upstream);
567 }
568 EXPORT_SYMBOL_GPL(sfp_link_down);
569
570 int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
571 {
572         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
573         int ret = 0;
574
575         if (ops && ops->module_insert)
576                 ret = ops->module_insert(bus->upstream, id);
577
578         return ret;
579 }
580 EXPORT_SYMBOL_GPL(sfp_module_insert);
581
582 void sfp_module_remove(struct sfp_bus *bus)
583 {
584         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
585
586         if (ops && ops->module_remove)
587                 ops->module_remove(bus->upstream);
588 }
589 EXPORT_SYMBOL_GPL(sfp_module_remove);
590
591 static void sfp_socket_clear(struct sfp_bus *bus)
592 {
593         bus->sfp_dev = NULL;
594         bus->sfp = NULL;
595         bus->socket_ops = NULL;
596 }
597
598 struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
599                                     const struct sfp_socket_ops *ops)
600 {
601         struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
602         int ret = 0;
603
604         if (bus) {
605                 rtnl_lock();
606                 bus->sfp_dev = dev;
607                 bus->sfp = sfp;
608                 bus->socket_ops = ops;
609
610                 if (bus->upstream_ops) {
611                         ret = sfp_register_bus(bus);
612                         if (ret)
613                                 sfp_socket_clear(bus);
614                 }
615                 rtnl_unlock();
616         }
617
618         if (ret) {
619                 sfp_bus_put(bus);
620                 bus = NULL;
621         }
622
623         return bus;
624 }
625 EXPORT_SYMBOL_GPL(sfp_register_socket);
626
627 void sfp_unregister_socket(struct sfp_bus *bus)
628 {
629         rtnl_lock();
630         if (bus->upstream_ops)
631                 sfp_unregister_bus(bus);
632         sfp_socket_clear(bus);
633         rtnl_unlock();
634
635         sfp_bus_put(bus);
636 }
637 EXPORT_SYMBOL_GPL(sfp_unregister_socket);