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