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