GNU Linux-libre 5.15.54-gnu
[releases.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 struct sfp_quirk {
14         const char *vendor;
15         const char *part;
16         void (*modes)(const struct sfp_eeprom_id *id, unsigned long *modes);
17 };
18
19 /**
20  * struct sfp_bus - internal representation of a sfp bus
21  */
22 struct sfp_bus {
23         /* private: */
24         struct kref kref;
25         struct list_head node;
26         struct fwnode_handle *fwnode;
27
28         const struct sfp_socket_ops *socket_ops;
29         struct device *sfp_dev;
30         struct sfp *sfp;
31         const struct sfp_quirk *sfp_quirk;
32
33         const struct sfp_upstream_ops *upstream_ops;
34         void *upstream;
35         struct phy_device *phydev;
36
37         bool registered;
38         bool started;
39 };
40
41 static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id,
42                                 unsigned long *modes)
43 {
44         phylink_set(modes, 2500baseX_Full);
45 }
46
47 static void sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id *id,
48                                       unsigned long *modes)
49 {
50         /* Ubiquiti U-Fiber Instant module claims that support all transceiver
51          * types including 10G Ethernet which is not truth. So clear all claimed
52          * modes and set only one mode which module supports: 1000baseX_Full.
53          */
54         phylink_zero(modes);
55         phylink_set(modes, 1000baseX_Full);
56 }
57
58 static const struct sfp_quirk sfp_quirks[] = {
59         {
60                 // Alcatel Lucent G-010S-P can operate at 2500base-X, but
61                 // incorrectly report 2500MBd NRZ in their EEPROM
62                 .vendor = "ALCATELLUCENT",
63                 .part = "G010SP",
64                 .modes = sfp_quirk_2500basex,
65         }, {
66                 // Alcatel Lucent G-010S-A can operate at 2500base-X, but
67                 // report 3.2GBd NRZ in their EEPROM
68                 .vendor = "ALCATELLUCENT",
69                 .part = "3FE46541AA",
70                 .modes = sfp_quirk_2500basex,
71         }, {
72                 // Huawei MA5671A can operate at 2500base-X, but report 1.2GBd
73                 // NRZ in their EEPROM
74                 .vendor = "HUAWEI",
75                 .part = "MA5671A",
76                 .modes = sfp_quirk_2500basex,
77         }, {
78                 // Lantech 8330-262D-E can operate at 2500base-X, but
79                 // incorrectly report 2500MBd NRZ in their EEPROM
80                 .vendor = "Lantech",
81                 .part = "8330-262D-E",
82                 .modes = sfp_quirk_2500basex,
83         }, {
84                 .vendor = "UBNT",
85                 .part = "UF-INSTANT",
86                 .modes = sfp_quirk_ubnt_uf_instant,
87         },
88 };
89
90 static size_t sfp_strlen(const char *str, size_t maxlen)
91 {
92         size_t size, i;
93
94         /* Trailing characters should be filled with space chars */
95         for (i = 0, size = 0; i < maxlen; i++)
96                 if (str[i] != ' ')
97                         size = i + 1;
98
99         return size;
100 }
101
102 static bool sfp_match(const char *qs, const char *str, size_t len)
103 {
104         if (!qs)
105                 return true;
106         if (strlen(qs) != len)
107                 return false;
108         return !strncmp(qs, str, len);
109 }
110
111 static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
112 {
113         const struct sfp_quirk *q;
114         unsigned int i;
115         size_t vs, ps;
116
117         vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name));
118         ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn));
119
120         for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++)
121                 if (sfp_match(q->vendor, id->base.vendor_name, vs) &&
122                     sfp_match(q->part, id->base.vendor_pn, ps))
123                         return q;
124
125         return NULL;
126 }
127
128 /**
129  * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
130  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
131  * @id: a pointer to the module's &struct sfp_eeprom_id
132  * @support: optional pointer to an array of unsigned long for the
133  *   ethtool support mask
134  *
135  * Parse the EEPROM identification given in @id, and return one of
136  * %PORT_TP, %PORT_FIBRE or %PORT_OTHER. If @support is non-%NULL,
137  * also set the ethtool %ETHTOOL_LINK_MODE_xxx_BIT corresponding with
138  * the connector type.
139  *
140  * If the port type is not known, returns %PORT_OTHER.
141  */
142 int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
143                    unsigned long *support)
144 {
145         int port;
146
147         /* port is the physical connector, set this from the connector field. */
148         switch (id->base.connector) {
149         case SFF8024_CONNECTOR_SC:
150         case SFF8024_CONNECTOR_FIBERJACK:
151         case SFF8024_CONNECTOR_LC:
152         case SFF8024_CONNECTOR_MT_RJ:
153         case SFF8024_CONNECTOR_MU:
154         case SFF8024_CONNECTOR_OPTICAL_PIGTAIL:
155         case SFF8024_CONNECTOR_MPO_1X12:
156         case SFF8024_CONNECTOR_MPO_2X16:
157                 port = PORT_FIBRE;
158                 break;
159
160         case SFF8024_CONNECTOR_RJ45:
161                 port = PORT_TP;
162                 break;
163
164         case SFF8024_CONNECTOR_COPPER_PIGTAIL:
165                 port = PORT_DA;
166                 break;
167
168         case SFF8024_CONNECTOR_UNSPEC:
169                 if (id->base.e1000_base_t) {
170                         port = PORT_TP;
171                         break;
172                 }
173                 fallthrough;
174         case SFF8024_CONNECTOR_SG: /* guess */
175         case SFF8024_CONNECTOR_HSSDC_II:
176         case SFF8024_CONNECTOR_NOSEPARATE:
177         case SFF8024_CONNECTOR_MXC_2X16:
178                 port = PORT_OTHER;
179                 break;
180         default:
181                 dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
182                          id->base.connector);
183                 port = PORT_OTHER;
184                 break;
185         }
186
187         if (support) {
188                 switch (port) {
189                 case PORT_FIBRE:
190                         phylink_set(support, FIBRE);
191                         break;
192
193                 case PORT_TP:
194                         phylink_set(support, TP);
195                         break;
196                 }
197         }
198
199         return port;
200 }
201 EXPORT_SYMBOL_GPL(sfp_parse_port);
202
203 /**
204  * sfp_may_have_phy() - indicate whether the module may have a PHY
205  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
206  * @id: a pointer to the module's &struct sfp_eeprom_id
207  *
208  * Parse the EEPROM identification given in @id, and return whether
209  * this module may have a PHY.
210  */
211 bool sfp_may_have_phy(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
212 {
213         if (id->base.e1000_base_t)
214                 return true;
215
216         if (id->base.phys_id != SFF8024_ID_DWDM_SFP) {
217                 switch (id->base.extended_cc) {
218                 case SFF8024_ECC_10GBASE_T_SFI:
219                 case SFF8024_ECC_10GBASE_T_SR:
220                 case SFF8024_ECC_5GBASE_T:
221                 case SFF8024_ECC_2_5GBASE_T:
222                         return true;
223                 }
224         }
225
226         return false;
227 }
228 EXPORT_SYMBOL_GPL(sfp_may_have_phy);
229
230 /**
231  * sfp_parse_support() - Parse the eeprom id for supported link modes
232  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
233  * @id: a pointer to the module's &struct sfp_eeprom_id
234  * @support: pointer to an array of unsigned long for the ethtool support mask
235  *
236  * Parse the EEPROM identification information and derive the supported
237  * ethtool link modes for the module.
238  */
239 void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
240                        unsigned long *support)
241 {
242         unsigned int br_min, br_nom, br_max;
243         __ETHTOOL_DECLARE_LINK_MODE_MASK(modes) = { 0, };
244
245         /* Decode the bitrate information to MBd */
246         br_min = br_nom = br_max = 0;
247         if (id->base.br_nominal) {
248                 if (id->base.br_nominal != 255) {
249                         br_nom = id->base.br_nominal * 100;
250                         br_min = br_nom - id->base.br_nominal * id->ext.br_min;
251                         br_max = br_nom + id->base.br_nominal * id->ext.br_max;
252                 } else if (id->ext.br_max) {
253                         br_nom = 250 * id->ext.br_max;
254                         br_max = br_nom + br_nom * id->ext.br_min / 100;
255                         br_min = br_nom - br_nom * id->ext.br_min / 100;
256                 }
257
258                 /* When using passive cables, in case neither BR,min nor BR,max
259                  * are specified, set br_min to 0 as the nominal value is then
260                  * used as the maximum.
261                  */
262                 if (br_min == br_max && id->base.sfp_ct_passive)
263                         br_min = 0;
264         }
265
266         /* Set ethtool support from the compliance fields. */
267         if (id->base.e10g_base_sr)
268                 phylink_set(modes, 10000baseSR_Full);
269         if (id->base.e10g_base_lr)
270                 phylink_set(modes, 10000baseLR_Full);
271         if (id->base.e10g_base_lrm)
272                 phylink_set(modes, 10000baseLRM_Full);
273         if (id->base.e10g_base_er)
274                 phylink_set(modes, 10000baseER_Full);
275         if (id->base.e1000_base_sx ||
276             id->base.e1000_base_lx ||
277             id->base.e1000_base_cx)
278                 phylink_set(modes, 1000baseX_Full);
279         if (id->base.e1000_base_t) {
280                 phylink_set(modes, 1000baseT_Half);
281                 phylink_set(modes, 1000baseT_Full);
282         }
283
284         /* 1000Base-PX or 1000Base-BX10 */
285         if ((id->base.e_base_px || id->base.e_base_bx10) &&
286             br_min <= 1300 && br_max >= 1200)
287                 phylink_set(modes, 1000baseX_Full);
288
289         /* 100Base-FX, 100Base-LX, 100Base-PX, 100Base-BX10 */
290         if (id->base.e100_base_fx || id->base.e100_base_lx)
291                 phylink_set(modes, 100baseFX_Full);
292         if ((id->base.e_base_px || id->base.e_base_bx10) && br_nom == 100)
293                 phylink_set(modes, 100baseFX_Full);
294
295         /* For active or passive cables, select the link modes
296          * based on the bit rates and the cable compliance bytes.
297          */
298         if ((id->base.sfp_ct_passive || id->base.sfp_ct_active) && br_nom) {
299                 /* This may look odd, but some manufacturers use 12000MBd */
300                 if (br_min <= 12000 && br_max >= 10300)
301                         phylink_set(modes, 10000baseCR_Full);
302                 if (br_min <= 3200 && br_max >= 3100)
303                         phylink_set(modes, 2500baseX_Full);
304                 if (br_min <= 1300 && br_max >= 1200)
305                         phylink_set(modes, 1000baseX_Full);
306         }
307         if (id->base.sfp_ct_passive) {
308                 if (id->base.passive.sff8431_app_e)
309                         phylink_set(modes, 10000baseCR_Full);
310         }
311         if (id->base.sfp_ct_active) {
312                 if (id->base.active.sff8431_app_e ||
313                     id->base.active.sff8431_lim) {
314                         phylink_set(modes, 10000baseCR_Full);
315                 }
316         }
317
318         switch (id->base.extended_cc) {
319         case SFF8024_ECC_UNSPEC:
320                 break;
321         case SFF8024_ECC_100GBASE_SR4_25GBASE_SR:
322                 phylink_set(modes, 100000baseSR4_Full);
323                 phylink_set(modes, 25000baseSR_Full);
324                 break;
325         case SFF8024_ECC_100GBASE_LR4_25GBASE_LR:
326         case SFF8024_ECC_100GBASE_ER4_25GBASE_ER:
327                 phylink_set(modes, 100000baseLR4_ER4_Full);
328                 break;
329         case SFF8024_ECC_100GBASE_CR4:
330                 phylink_set(modes, 100000baseCR4_Full);
331                 fallthrough;
332         case SFF8024_ECC_25GBASE_CR_S:
333         case SFF8024_ECC_25GBASE_CR_N:
334                 phylink_set(modes, 25000baseCR_Full);
335                 break;
336         case SFF8024_ECC_10GBASE_T_SFI:
337         case SFF8024_ECC_10GBASE_T_SR:
338                 phylink_set(modes, 10000baseT_Full);
339                 break;
340         case SFF8024_ECC_5GBASE_T:
341                 phylink_set(modes, 5000baseT_Full);
342                 break;
343         case SFF8024_ECC_2_5GBASE_T:
344                 phylink_set(modes, 2500baseT_Full);
345                 break;
346         default:
347                 dev_warn(bus->sfp_dev,
348                          "Unknown/unsupported extended compliance code: 0x%02x\n",
349                          id->base.extended_cc);
350                 break;
351         }
352
353         /* For fibre channel SFP, derive possible BaseX modes */
354         if (id->base.fc_speed_100 ||
355             id->base.fc_speed_200 ||
356             id->base.fc_speed_400) {
357                 if (id->base.br_nominal >= 31)
358                         phylink_set(modes, 2500baseX_Full);
359                 if (id->base.br_nominal >= 12)
360                         phylink_set(modes, 1000baseX_Full);
361         }
362
363         /* If we haven't discovered any modes that this module supports, try
364          * the bitrate to determine supported modes. Some BiDi modules (eg,
365          * 1310nm/1550nm) are not 1000BASE-BX compliant due to the differing
366          * wavelengths, so do not set any transceiver bits.
367          *
368          * Do the same for modules supporting 2500BASE-X. Note that some
369          * modules use 2500Mbaud rather than 3100 or 3200Mbaud for
370          * 2500BASE-X, so we allow some slack here.
371          */
372         if (bitmap_empty(modes, __ETHTOOL_LINK_MODE_MASK_NBITS) && br_nom) {
373                 if (br_min <= 1300 && br_max >= 1200)
374                         phylink_set(modes, 1000baseX_Full);
375                 if (br_min <= 3200 && br_max >= 2500)
376                         phylink_set(modes, 2500baseX_Full);
377         }
378
379         if (bus->sfp_quirk)
380                 bus->sfp_quirk->modes(id, modes);
381
382         bitmap_or(support, support, modes, __ETHTOOL_LINK_MODE_MASK_NBITS);
383
384         phylink_set(support, Autoneg);
385         phylink_set(support, Pause);
386         phylink_set(support, Asym_Pause);
387 }
388 EXPORT_SYMBOL_GPL(sfp_parse_support);
389
390 /**
391  * sfp_select_interface() - Select appropriate phy_interface_t mode
392  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
393  * @link_modes: ethtool link modes mask
394  *
395  * Derive the phy_interface_t mode for the SFP module from the link
396  * modes mask.
397  */
398 phy_interface_t sfp_select_interface(struct sfp_bus *bus,
399                                      unsigned long *link_modes)
400 {
401         if (phylink_test(link_modes, 25000baseCR_Full) ||
402             phylink_test(link_modes, 25000baseKR_Full) ||
403             phylink_test(link_modes, 25000baseSR_Full))
404                 return PHY_INTERFACE_MODE_25GBASER;
405
406         if (phylink_test(link_modes, 10000baseCR_Full) ||
407             phylink_test(link_modes, 10000baseSR_Full) ||
408             phylink_test(link_modes, 10000baseLR_Full) ||
409             phylink_test(link_modes, 10000baseLRM_Full) ||
410             phylink_test(link_modes, 10000baseER_Full) ||
411             phylink_test(link_modes, 10000baseT_Full))
412                 return PHY_INTERFACE_MODE_10GBASER;
413
414         if (phylink_test(link_modes, 5000baseT_Full))
415                 return PHY_INTERFACE_MODE_5GBASER;
416
417         if (phylink_test(link_modes, 2500baseX_Full))
418                 return PHY_INTERFACE_MODE_2500BASEX;
419
420         if (phylink_test(link_modes, 1000baseT_Half) ||
421             phylink_test(link_modes, 1000baseT_Full))
422                 return PHY_INTERFACE_MODE_SGMII;
423
424         if (phylink_test(link_modes, 1000baseX_Full))
425                 return PHY_INTERFACE_MODE_1000BASEX;
426
427         if (phylink_test(link_modes, 100baseFX_Full))
428                 return PHY_INTERFACE_MODE_100BASEX;
429
430         dev_warn(bus->sfp_dev, "Unable to ascertain link mode\n");
431
432         return PHY_INTERFACE_MODE_NA;
433 }
434 EXPORT_SYMBOL_GPL(sfp_select_interface);
435
436 static LIST_HEAD(sfp_buses);
437 static DEFINE_MUTEX(sfp_mutex);
438
439 static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
440 {
441         return bus->registered ? bus->upstream_ops : NULL;
442 }
443
444 static struct sfp_bus *sfp_bus_get(struct fwnode_handle *fwnode)
445 {
446         struct sfp_bus *sfp, *new, *found = NULL;
447
448         new = kzalloc(sizeof(*new), GFP_KERNEL);
449
450         mutex_lock(&sfp_mutex);
451
452         list_for_each_entry(sfp, &sfp_buses, node) {
453                 if (sfp->fwnode == fwnode) {
454                         kref_get(&sfp->kref);
455                         found = sfp;
456                         break;
457                 }
458         }
459
460         if (!found && new) {
461                 kref_init(&new->kref);
462                 new->fwnode = fwnode;
463                 list_add(&new->node, &sfp_buses);
464                 found = new;
465                 new = NULL;
466         }
467
468         mutex_unlock(&sfp_mutex);
469
470         kfree(new);
471
472         return found;
473 }
474
475 static void sfp_bus_release(struct kref *kref)
476 {
477         struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
478
479         list_del(&bus->node);
480         mutex_unlock(&sfp_mutex);
481         kfree(bus);
482 }
483
484 /**
485  * sfp_bus_put() - put a reference on the &struct sfp_bus
486  * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
487  *
488  * Put a reference on the &struct sfp_bus and free the underlying structure
489  * if this was the last reference.
490  */
491 void sfp_bus_put(struct sfp_bus *bus)
492 {
493         if (bus)
494                 kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
495 }
496 EXPORT_SYMBOL_GPL(sfp_bus_put);
497
498 static int sfp_register_bus(struct sfp_bus *bus)
499 {
500         const struct sfp_upstream_ops *ops = bus->upstream_ops;
501         int ret;
502
503         if (ops) {
504                 if (ops->link_down)
505                         ops->link_down(bus->upstream);
506                 if (ops->connect_phy && bus->phydev) {
507                         ret = ops->connect_phy(bus->upstream, bus->phydev);
508                         if (ret)
509                                 return ret;
510                 }
511         }
512         bus->registered = true;
513         bus->socket_ops->attach(bus->sfp);
514         if (bus->started)
515                 bus->socket_ops->start(bus->sfp);
516         bus->upstream_ops->attach(bus->upstream, bus);
517         return 0;
518 }
519
520 static void sfp_unregister_bus(struct sfp_bus *bus)
521 {
522         const struct sfp_upstream_ops *ops = bus->upstream_ops;
523
524         if (bus->registered) {
525                 bus->upstream_ops->detach(bus->upstream, bus);
526                 if (bus->started)
527                         bus->socket_ops->stop(bus->sfp);
528                 bus->socket_ops->detach(bus->sfp);
529                 if (bus->phydev && ops && ops->disconnect_phy)
530                         ops->disconnect_phy(bus->upstream);
531         }
532         bus->registered = false;
533 }
534
535 /**
536  * sfp_get_module_info() - Get the ethtool_modinfo for a SFP module
537  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
538  * @modinfo: a &struct ethtool_modinfo
539  *
540  * Fill in the type and eeprom_len parameters in @modinfo for a module on
541  * the sfp bus specified by @bus.
542  *
543  * Returns 0 on success or a negative errno number.
544  */
545 int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
546 {
547         return bus->socket_ops->module_info(bus->sfp, modinfo);
548 }
549 EXPORT_SYMBOL_GPL(sfp_get_module_info);
550
551 /**
552  * sfp_get_module_eeprom() - Read the SFP module EEPROM
553  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
554  * @ee: a &struct ethtool_eeprom
555  * @data: buffer to contain the EEPROM data (must be at least @ee->len bytes)
556  *
557  * Read the EEPROM as specified by the supplied @ee. See the documentation
558  * for &struct ethtool_eeprom for the region to be read.
559  *
560  * Returns 0 on success or a negative errno number.
561  */
562 int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
563                           u8 *data)
564 {
565         return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
566 }
567 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
568
569 /**
570  * sfp_get_module_eeprom_by_page() - Read a page from the SFP module EEPROM
571  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
572  * @page: a &struct ethtool_module_eeprom
573  * @extack: extack for reporting problems
574  *
575  * Read an EEPROM page as specified by the supplied @page. See the
576  * documentation for &struct ethtool_module_eeprom for the page to be read.
577  *
578  * Returns 0 on success or a negative errno number. More error
579  * information might be provided via extack
580  */
581 int sfp_get_module_eeprom_by_page(struct sfp_bus *bus,
582                                   const struct ethtool_module_eeprom *page,
583                                   struct netlink_ext_ack *extack)
584 {
585         return bus->socket_ops->module_eeprom_by_page(bus->sfp, page, extack);
586 }
587 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom_by_page);
588
589 /**
590  * sfp_upstream_start() - Inform the SFP that the network device is up
591  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
592  *
593  * Inform the SFP socket that the network device is now up, so that the
594  * module can be enabled by allowing TX_DISABLE to be deasserted. This
595  * should be called from the network device driver's &struct net_device_ops
596  * ndo_open() method.
597  */
598 void sfp_upstream_start(struct sfp_bus *bus)
599 {
600         if (bus->registered)
601                 bus->socket_ops->start(bus->sfp);
602         bus->started = true;
603 }
604 EXPORT_SYMBOL_GPL(sfp_upstream_start);
605
606 /**
607  * sfp_upstream_stop() - Inform the SFP that the network device is down
608  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
609  *
610  * Inform the SFP socket that the network device is now up, so that the
611  * module can be disabled by asserting TX_DISABLE, disabling the laser
612  * in optical modules. This should be called from the network device
613  * driver's &struct net_device_ops ndo_stop() method.
614  */
615 void sfp_upstream_stop(struct sfp_bus *bus)
616 {
617         if (bus->registered)
618                 bus->socket_ops->stop(bus->sfp);
619         bus->started = false;
620 }
621 EXPORT_SYMBOL_GPL(sfp_upstream_stop);
622
623 static void sfp_upstream_clear(struct sfp_bus *bus)
624 {
625         bus->upstream_ops = NULL;
626         bus->upstream = NULL;
627 }
628
629 /**
630  * sfp_bus_find_fwnode() - parse and locate the SFP bus from fwnode
631  * @fwnode: firmware node for the parent device (MAC or PHY)
632  *
633  * Parse the parent device's firmware node for a SFP bus, and locate
634  * the sfp_bus structure, incrementing its reference count.  This must
635  * be put via sfp_bus_put() when done.
636  *
637  * Returns:
638  *      - on success, a pointer to the sfp_bus structure,
639  *      - %NULL if no SFP is specified,
640  *      - on failure, an error pointer value:
641  *
642  *      - corresponding to the errors detailed for
643  *        fwnode_property_get_reference_args().
644  *      - %-ENOMEM if we failed to allocate the bus.
645  *      - an error from the upstream's connect_phy() method.
646  */
647 struct sfp_bus *sfp_bus_find_fwnode(struct fwnode_handle *fwnode)
648 {
649         struct fwnode_reference_args ref;
650         struct sfp_bus *bus;
651         int ret;
652
653         ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
654                                                  0, 0, &ref);
655         if (ret == -ENOENT)
656                 return NULL;
657         else if (ret < 0)
658                 return ERR_PTR(ret);
659
660         if (!fwnode_device_is_available(ref.fwnode)) {
661                 fwnode_handle_put(ref.fwnode);
662                 return NULL;
663         }
664
665         bus = sfp_bus_get(ref.fwnode);
666         fwnode_handle_put(ref.fwnode);
667         if (!bus)
668                 return ERR_PTR(-ENOMEM);
669
670         return bus;
671 }
672 EXPORT_SYMBOL_GPL(sfp_bus_find_fwnode);
673
674 /**
675  * sfp_bus_add_upstream() - parse and register the neighbouring device
676  * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
677  * @upstream: the upstream private data
678  * @ops: the upstream's &struct sfp_upstream_ops
679  *
680  * Add upstream driver for the SFP bus, and if the bus is complete, register
681  * the SFP bus using sfp_register_upstream().  This takes a reference on the
682  * bus, so it is safe to put the bus after this call.
683  *
684  * Returns:
685  *      - on success, a pointer to the sfp_bus structure,
686  *      - %NULL if no SFP is specified,
687  *      - on failure, an error pointer value:
688  *
689  *      - corresponding to the errors detailed for
690  *        fwnode_property_get_reference_args().
691  *      - %-ENOMEM if we failed to allocate the bus.
692  *      - an error from the upstream's connect_phy() method.
693  */
694 int sfp_bus_add_upstream(struct sfp_bus *bus, void *upstream,
695                          const struct sfp_upstream_ops *ops)
696 {
697         int ret;
698
699         /* If no bus, return success */
700         if (!bus)
701                 return 0;
702
703         rtnl_lock();
704         kref_get(&bus->kref);
705         bus->upstream_ops = ops;
706         bus->upstream = upstream;
707
708         if (bus->sfp) {
709                 ret = sfp_register_bus(bus);
710                 if (ret)
711                         sfp_upstream_clear(bus);
712         } else {
713                 ret = 0;
714         }
715         rtnl_unlock();
716
717         if (ret)
718                 sfp_bus_put(bus);
719
720         return ret;
721 }
722 EXPORT_SYMBOL_GPL(sfp_bus_add_upstream);
723
724 /**
725  * sfp_bus_del_upstream() - Delete a sfp bus
726  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
727  *
728  * Delete a previously registered upstream connection for the SFP
729  * module. @bus should have been added by sfp_bus_add_upstream().
730  */
731 void sfp_bus_del_upstream(struct sfp_bus *bus)
732 {
733         if (bus) {
734                 rtnl_lock();
735                 if (bus->sfp)
736                         sfp_unregister_bus(bus);
737                 sfp_upstream_clear(bus);
738                 rtnl_unlock();
739
740                 sfp_bus_put(bus);
741         }
742 }
743 EXPORT_SYMBOL_GPL(sfp_bus_del_upstream);
744
745 /* Socket driver entry points */
746 int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
747 {
748         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
749         int ret = 0;
750
751         if (ops && ops->connect_phy)
752                 ret = ops->connect_phy(bus->upstream, phydev);
753
754         if (ret == 0)
755                 bus->phydev = phydev;
756
757         return ret;
758 }
759 EXPORT_SYMBOL_GPL(sfp_add_phy);
760
761 void sfp_remove_phy(struct sfp_bus *bus)
762 {
763         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
764
765         if (ops && ops->disconnect_phy)
766                 ops->disconnect_phy(bus->upstream);
767         bus->phydev = NULL;
768 }
769 EXPORT_SYMBOL_GPL(sfp_remove_phy);
770
771 void sfp_link_up(struct sfp_bus *bus)
772 {
773         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
774
775         if (ops && ops->link_up)
776                 ops->link_up(bus->upstream);
777 }
778 EXPORT_SYMBOL_GPL(sfp_link_up);
779
780 void sfp_link_down(struct sfp_bus *bus)
781 {
782         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
783
784         if (ops && ops->link_down)
785                 ops->link_down(bus->upstream);
786 }
787 EXPORT_SYMBOL_GPL(sfp_link_down);
788
789 int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
790 {
791         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
792         int ret = 0;
793
794         bus->sfp_quirk = sfp_lookup_quirk(id);
795
796         if (ops && ops->module_insert)
797                 ret = ops->module_insert(bus->upstream, id);
798
799         return ret;
800 }
801 EXPORT_SYMBOL_GPL(sfp_module_insert);
802
803 void sfp_module_remove(struct sfp_bus *bus)
804 {
805         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
806
807         if (ops && ops->module_remove)
808                 ops->module_remove(bus->upstream);
809
810         bus->sfp_quirk = NULL;
811 }
812 EXPORT_SYMBOL_GPL(sfp_module_remove);
813
814 int sfp_module_start(struct sfp_bus *bus)
815 {
816         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
817         int ret = 0;
818
819         if (ops && ops->module_start)
820                 ret = ops->module_start(bus->upstream);
821
822         return ret;
823 }
824 EXPORT_SYMBOL_GPL(sfp_module_start);
825
826 void sfp_module_stop(struct sfp_bus *bus)
827 {
828         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
829
830         if (ops && ops->module_stop)
831                 ops->module_stop(bus->upstream);
832 }
833 EXPORT_SYMBOL_GPL(sfp_module_stop);
834
835 static void sfp_socket_clear(struct sfp_bus *bus)
836 {
837         bus->sfp_dev = NULL;
838         bus->sfp = NULL;
839         bus->socket_ops = NULL;
840 }
841
842 struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
843                                     const struct sfp_socket_ops *ops)
844 {
845         struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
846         int ret = 0;
847
848         if (bus) {
849                 rtnl_lock();
850                 bus->sfp_dev = dev;
851                 bus->sfp = sfp;
852                 bus->socket_ops = ops;
853
854                 if (bus->upstream_ops) {
855                         ret = sfp_register_bus(bus);
856                         if (ret)
857                                 sfp_socket_clear(bus);
858                 }
859                 rtnl_unlock();
860         }
861
862         if (ret) {
863                 sfp_bus_put(bus);
864                 bus = NULL;
865         }
866
867         return bus;
868 }
869 EXPORT_SYMBOL_GPL(sfp_register_socket);
870
871 void sfp_unregister_socket(struct sfp_bus *bus)
872 {
873         rtnl_lock();
874         if (bus->upstream_ops)
875                 sfp_unregister_bus(bus);
876         sfp_socket_clear(bus);
877         rtnl_unlock();
878
879         sfp_bus_put(bus);
880 }
881 EXPORT_SYMBOL_GPL(sfp_unregister_socket);