blob: 816e59fe68f579119854e16497e9e11e49b175f4 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002#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/rtnetlink.h>
8#include <linux/slab.h>
9
10#include "sfp.h"
11
Olivier Deprez0e641232021-09-23 10:07:05 +020012struct sfp_quirk {
13 const char *vendor;
14 const char *part;
15 void (*modes)(const struct sfp_eeprom_id *id, unsigned long *modes);
16};
17
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018/**
19 * struct sfp_bus - internal representation of a sfp bus
20 */
21struct sfp_bus {
22 /* private: */
23 struct kref kref;
24 struct list_head node;
25 struct fwnode_handle *fwnode;
26
27 const struct sfp_socket_ops *socket_ops;
28 struct device *sfp_dev;
29 struct sfp *sfp;
Olivier Deprez0e641232021-09-23 10:07:05 +020030 const struct sfp_quirk *sfp_quirk;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031
32 const struct sfp_upstream_ops *upstream_ops;
33 void *upstream;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034 struct phy_device *phydev;
35
36 bool registered;
37 bool started;
38};
39
Olivier Deprez0e641232021-09-23 10:07:05 +020040static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id,
41 unsigned long *modes)
42{
43 phylink_set(modes, 2500baseX_Full);
44}
45
46static 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
68static 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
80static 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
89static 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}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105/**
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 */
119int 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}
178EXPORT_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 */
189void 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
Olivier Deprez0e641232021-09-23 10:07:05 +0200308 if (bus->sfp_quirk)
309 bus->sfp_quirk->modes(id, modes);
310
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000311 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}
317EXPORT_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 */
330phy_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}
356EXPORT_SYMBOL_GPL(sfp_select_interface);
357
358static LIST_HEAD(sfp_buses);
359static DEFINE_MUTEX(sfp_mutex);
360
361static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
362{
363 return bus->registered ? bus->upstream_ops : NULL;
364}
365
366static 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
397static 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
406static void sfp_bus_put(struct sfp_bus *bus)
407{
408 kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
409}
410
411static 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 }
David Brazdil0f672f62019-12-10 10:32:29 +0000425 bus->socket_ops->attach(bus->sfp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000426 if (bus->started)
427 bus->socket_ops->start(bus->sfp);
David Brazdil0f672f62019-12-10 10:32:29 +0000428 bus->upstream_ops->attach(bus->upstream, bus);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000429 bus->registered = true;
430 return 0;
431}
432
433static void sfp_unregister_bus(struct sfp_bus *bus)
434{
435 const struct sfp_upstream_ops *ops = bus->upstream_ops;
436
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000437 if (bus->registered) {
David Brazdil0f672f62019-12-10 10:32:29 +0000438 bus->upstream_ops->detach(bus->upstream, bus);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000439 if (bus->started)
440 bus->socket_ops->stop(bus->sfp);
David Brazdil0f672f62019-12-10 10:32:29 +0000441 bus->socket_ops->detach(bus->sfp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000442 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 */
458int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
459{
460 return bus->socket_ops->module_info(bus->sfp, modinfo);
461}
462EXPORT_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 */
475int 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}
480EXPORT_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 */
491void sfp_upstream_start(struct sfp_bus *bus)
492{
493 if (bus->registered)
494 bus->socket_ops->start(bus->sfp);
495 bus->started = true;
496}
497EXPORT_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 */
508void sfp_upstream_stop(struct sfp_bus *bus)
509{
510 if (bus->registered)
511 bus->socket_ops->stop(bus->sfp);
512 bus->started = false;
513}
514EXPORT_SYMBOL_GPL(sfp_upstream_stop);
515
516static void sfp_upstream_clear(struct sfp_bus *bus)
517{
518 bus->upstream_ops = NULL;
519 bus->upstream = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000520}
521
522/**
523 * sfp_register_upstream() - Register the neighbouring device
524 * @fwnode: firmware node for the SFP bus
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000525 * @upstream: the upstream private data
526 * @ops: the upstream's &struct sfp_upstream_ops
527 *
528 * Register the upstream device (eg, PHY) with the SFP bus. MAC drivers
529 * should use phylink, which will call this function for them. Returns
530 * a pointer to the allocated &struct sfp_bus.
531 *
532 * On error, returns %NULL.
533 */
534struct sfp_bus *sfp_register_upstream(struct fwnode_handle *fwnode,
David Brazdil0f672f62019-12-10 10:32:29 +0000535 void *upstream,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000536 const struct sfp_upstream_ops *ops)
537{
538 struct sfp_bus *bus = sfp_bus_get(fwnode);
539 int ret = 0;
540
541 if (bus) {
542 rtnl_lock();
543 bus->upstream_ops = ops;
544 bus->upstream = upstream;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000545
546 if (bus->sfp) {
547 ret = sfp_register_bus(bus);
548 if (ret)
549 sfp_upstream_clear(bus);
550 }
551 rtnl_unlock();
552 }
553
554 if (ret) {
555 sfp_bus_put(bus);
556 bus = NULL;
557 }
558
559 return bus;
560}
561EXPORT_SYMBOL_GPL(sfp_register_upstream);
562
563/**
564 * sfp_unregister_upstream() - Unregister sfp bus
565 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
566 *
567 * Unregister a previously registered upstream connection for the SFP
568 * module. @bus is returned from sfp_register_upstream().
569 */
570void sfp_unregister_upstream(struct sfp_bus *bus)
571{
572 rtnl_lock();
573 if (bus->sfp)
574 sfp_unregister_bus(bus);
575 sfp_upstream_clear(bus);
576 rtnl_unlock();
577
578 sfp_bus_put(bus);
579}
580EXPORT_SYMBOL_GPL(sfp_unregister_upstream);
581
582/* Socket driver entry points */
583int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
584{
585 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
586 int ret = 0;
587
588 if (ops && ops->connect_phy)
589 ret = ops->connect_phy(bus->upstream, phydev);
590
591 if (ret == 0)
592 bus->phydev = phydev;
593
594 return ret;
595}
596EXPORT_SYMBOL_GPL(sfp_add_phy);
597
598void sfp_remove_phy(struct sfp_bus *bus)
599{
600 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
601
602 if (ops && ops->disconnect_phy)
603 ops->disconnect_phy(bus->upstream);
604 bus->phydev = NULL;
605}
606EXPORT_SYMBOL_GPL(sfp_remove_phy);
607
608void sfp_link_up(struct sfp_bus *bus)
609{
610 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
611
612 if (ops && ops->link_up)
613 ops->link_up(bus->upstream);
614}
615EXPORT_SYMBOL_GPL(sfp_link_up);
616
617void sfp_link_down(struct sfp_bus *bus)
618{
619 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
620
621 if (ops && ops->link_down)
622 ops->link_down(bus->upstream);
623}
624EXPORT_SYMBOL_GPL(sfp_link_down);
625
626int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
627{
628 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
629 int ret = 0;
630
Olivier Deprez0e641232021-09-23 10:07:05 +0200631 bus->sfp_quirk = sfp_lookup_quirk(id);
632
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000633 if (ops && ops->module_insert)
634 ret = ops->module_insert(bus->upstream, id);
635
636 return ret;
637}
638EXPORT_SYMBOL_GPL(sfp_module_insert);
639
640void sfp_module_remove(struct sfp_bus *bus)
641{
642 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
643
644 if (ops && ops->module_remove)
645 ops->module_remove(bus->upstream);
Olivier Deprez0e641232021-09-23 10:07:05 +0200646
647 bus->sfp_quirk = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000648}
649EXPORT_SYMBOL_GPL(sfp_module_remove);
650
651static void sfp_socket_clear(struct sfp_bus *bus)
652{
653 bus->sfp_dev = NULL;
654 bus->sfp = NULL;
655 bus->socket_ops = NULL;
656}
657
658struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
659 const struct sfp_socket_ops *ops)
660{
661 struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
662 int ret = 0;
663
664 if (bus) {
665 rtnl_lock();
666 bus->sfp_dev = dev;
667 bus->sfp = sfp;
668 bus->socket_ops = ops;
669
David Brazdil0f672f62019-12-10 10:32:29 +0000670 if (bus->upstream_ops) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000671 ret = sfp_register_bus(bus);
672 if (ret)
673 sfp_socket_clear(bus);
674 }
675 rtnl_unlock();
676 }
677
678 if (ret) {
679 sfp_bus_put(bus);
680 bus = NULL;
681 }
682
683 return bus;
684}
685EXPORT_SYMBOL_GPL(sfp_register_socket);
686
687void sfp_unregister_socket(struct sfp_bus *bus)
688{
689 rtnl_lock();
David Brazdil0f672f62019-12-10 10:32:29 +0000690 if (bus->upstream_ops)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000691 sfp_unregister_bus(bus);
692 sfp_socket_clear(bus);
693 rtnl_unlock();
694
695 sfp_bus_put(bus);
696}
697EXPORT_SYMBOL_GPL(sfp_unregister_socket);