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