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>
16 void (*modes)(const struct sfp_eeprom_id *id, unsigned long *modes);
20 * struct sfp_bus - internal representation of a sfp bus
25 struct list_head node;
26 struct fwnode_handle *fwnode;
28 const struct sfp_socket_ops *socket_ops;
29 struct device *sfp_dev;
31 const struct sfp_quirk *sfp_quirk;
33 const struct sfp_upstream_ops *upstream_ops;
35 struct phy_device *phydev;
41 static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id,
44 phylink_set(modes, 2500baseX_Full);
47 static const struct sfp_quirk sfp_quirks[] = {
49 // Alcatel Lucent G-010S-P can operate at 2500base-X, but
50 // incorrectly report 2500MBd NRZ in their EEPROM
51 .vendor = "ALCATELLUCENT",
53 .modes = sfp_quirk_2500basex,
55 // Alcatel Lucent G-010S-A can operate at 2500base-X, but
56 // report 3.2GBd NRZ in their EEPROM
57 .vendor = "ALCATELLUCENT",
59 .modes = sfp_quirk_2500basex,
61 // Huawei MA5671A can operate at 2500base-X, but report 1.2GBd
62 // NRZ in their EEPROM
65 .modes = sfp_quirk_2500basex,
69 static size_t sfp_strlen(const char *str, size_t maxlen)
73 /* Trailing characters should be filled with space chars */
74 for (i = 0, size = 0; i < maxlen; i++)
81 static bool sfp_match(const char *qs, const char *str, size_t len)
85 if (strlen(qs) != len)
87 return !strncmp(qs, str, len);
90 static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
92 const struct sfp_quirk *q;
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));
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))
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
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.
118 * If the port type is not known, returns %PORT_OTHER.
120 int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
121 unsigned long *support)
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:
136 case SFP_CONNECTOR_RJ45:
140 case SFP_CONNECTOR_COPPER_PIGTAIL:
144 case SFP_CONNECTOR_UNSPEC:
145 if (id->base.e1000_base_t) {
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:
159 dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
168 phylink_set(support, FIBRE);
172 phylink_set(support, TP);
179 EXPORT_SYMBOL_GPL(sfp_parse_port);
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
187 * Parse the EEPROM identification information and derive the supported
188 * ethtool link modes for the module.
190 void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
191 unsigned long *support)
193 unsigned int br_min, br_nom, br_max;
194 __ETHTOOL_DECLARE_LINK_MODE_MASK(modes) = { 0, };
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;
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.
213 if (br_min == br_max && id->base.sfp_ct_passive)
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);
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);
240 /* For active or passive cables, select the link modes
241 * based on the bit rates and the cable compliance bytes.
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);
252 if (id->base.sfp_ct_passive) {
253 if (id->base.passive.sff8431_app_e)
254 phylink_set(modes, 10000baseCR_Full);
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);
263 switch (id->base.extended_cc) {
264 case 0x00: /* Unspecified */
266 case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */
267 phylink_set(modes, 100000baseSR4_Full);
268 phylink_set(modes, 25000baseSR_Full);
270 case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */
271 case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */
272 phylink_set(modes, 100000baseLR4_ER4_Full);
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);
281 dev_warn(bus->sfp_dev,
282 "Unknown/unsupported extended compliance code: 0x%02x\n",
283 id->base.extended_cc);
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);
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.
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);
310 bus->sfp_quirk->modes(id, modes);
312 bitmap_or(support, support, modes, __ETHTOOL_LINK_MODE_MASK_NBITS);
314 phylink_set(support, Autoneg);
315 phylink_set(support, Pause);
316 phylink_set(support, Asym_Pause);
318 EXPORT_SYMBOL_GPL(sfp_parse_support);
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
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.
331 phy_interface_t sfp_select_interface(struct sfp_bus *bus,
332 const struct sfp_eeprom_id *id,
333 unsigned long *link_modes)
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;
342 if (phylink_test(link_modes, 2500baseX_Full))
343 return PHY_INTERFACE_MODE_2500BASEX;
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;
350 if (phylink_test(link_modes, 1000baseX_Full))
351 return PHY_INTERFACE_MODE_1000BASEX;
353 dev_warn(bus->sfp_dev, "Unable to ascertain link mode\n");
355 return PHY_INTERFACE_MODE_NA;
357 EXPORT_SYMBOL_GPL(sfp_select_interface);
359 static LIST_HEAD(sfp_buses);
360 static DEFINE_MUTEX(sfp_mutex);
362 static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
364 return bus->registered ? bus->upstream_ops : NULL;
367 static struct sfp_bus *sfp_bus_get(struct fwnode_handle *fwnode)
369 struct sfp_bus *sfp, *new, *found = NULL;
371 new = kzalloc(sizeof(*new), GFP_KERNEL);
373 mutex_lock(&sfp_mutex);
375 list_for_each_entry(sfp, &sfp_buses, node) {
376 if (sfp->fwnode == fwnode) {
377 kref_get(&sfp->kref);
384 kref_init(&new->kref);
385 new->fwnode = fwnode;
386 list_add(&new->node, &sfp_buses);
391 mutex_unlock(&sfp_mutex);
398 static void sfp_bus_release(struct kref *kref)
400 struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
402 list_del(&bus->node);
403 mutex_unlock(&sfp_mutex);
408 * sfp_bus_put() - put a reference on the &struct sfp_bus
409 * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
411 * Put a reference on the &struct sfp_bus and free the underlying structure
412 * if this was the last reference.
414 void sfp_bus_put(struct sfp_bus *bus)
417 kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
419 EXPORT_SYMBOL_GPL(sfp_bus_put);
421 static int sfp_register_bus(struct sfp_bus *bus)
423 const struct sfp_upstream_ops *ops = bus->upstream_ops;
428 ops->link_down(bus->upstream);
429 if (ops->connect_phy && bus->phydev) {
430 ret = ops->connect_phy(bus->upstream, bus->phydev);
435 bus->registered = true;
436 bus->socket_ops->attach(bus->sfp);
438 bus->socket_ops->start(bus->sfp);
439 bus->upstream_ops->attach(bus->upstream, bus);
443 static void sfp_unregister_bus(struct sfp_bus *bus)
445 const struct sfp_upstream_ops *ops = bus->upstream_ops;
447 if (bus->registered) {
448 bus->upstream_ops->detach(bus->upstream, bus);
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);
455 bus->registered = false;
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
463 * Fill in the type and eeprom_len parameters in @modinfo for a module on
464 * the sfp bus specified by @bus.
466 * Returns 0 on success or a negative errno number.
468 int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
470 return bus->socket_ops->module_info(bus->sfp, modinfo);
472 EXPORT_SYMBOL_GPL(sfp_get_module_info);
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)
480 * Read the EEPROM as specified by the supplied @ee. See the documentation
481 * for &struct ethtool_eeprom for the region to be read.
483 * Returns 0 on success or a negative errno number.
485 int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
488 return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
490 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
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
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
501 void sfp_upstream_start(struct sfp_bus *bus)
504 bus->socket_ops->start(bus->sfp);
507 EXPORT_SYMBOL_GPL(sfp_upstream_start);
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
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.
518 void sfp_upstream_stop(struct sfp_bus *bus)
521 bus->socket_ops->stop(bus->sfp);
522 bus->started = false;
524 EXPORT_SYMBOL_GPL(sfp_upstream_stop);
526 static void sfp_upstream_clear(struct sfp_bus *bus)
528 bus->upstream_ops = NULL;
529 bus->upstream = NULL;
533 * sfp_bus_find_fwnode() - parse and locate the SFP bus from fwnode
534 * @fwnode: firmware node for the parent device (MAC or PHY)
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.
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.
548 struct sfp_bus *sfp_bus_find_fwnode(struct fwnode_handle *fwnode)
550 struct fwnode_reference_args ref;
554 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
561 bus = sfp_bus_get(ref.fwnode);
562 fwnode_handle_put(ref.fwnode);
564 return ERR_PTR(-ENOMEM);
568 EXPORT_SYMBOL_GPL(sfp_bus_find_fwnode);
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
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.
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.
588 int sfp_bus_add_upstream(struct sfp_bus *bus, void *upstream,
589 const struct sfp_upstream_ops *ops)
593 /* If no bus, return success */
598 kref_get(&bus->kref);
599 bus->upstream_ops = ops;
600 bus->upstream = upstream;
603 ret = sfp_register_bus(bus);
605 sfp_upstream_clear(bus);
616 EXPORT_SYMBOL_GPL(sfp_bus_add_upstream);
619 * sfp_bus_del_upstream() - Delete a sfp bus
620 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
622 * Delete a previously registered upstream connection for the SFP
623 * module. @bus should have been added by sfp_bus_add_upstream().
625 void sfp_bus_del_upstream(struct sfp_bus *bus)
630 sfp_unregister_bus(bus);
631 sfp_upstream_clear(bus);
637 EXPORT_SYMBOL_GPL(sfp_bus_del_upstream);
639 /* Socket driver entry points */
640 int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
642 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
645 if (ops && ops->connect_phy)
646 ret = ops->connect_phy(bus->upstream, phydev);
649 bus->phydev = phydev;
653 EXPORT_SYMBOL_GPL(sfp_add_phy);
655 void sfp_remove_phy(struct sfp_bus *bus)
657 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
659 if (ops && ops->disconnect_phy)
660 ops->disconnect_phy(bus->upstream);
663 EXPORT_SYMBOL_GPL(sfp_remove_phy);
665 void sfp_link_up(struct sfp_bus *bus)
667 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
669 if (ops && ops->link_up)
670 ops->link_up(bus->upstream);
672 EXPORT_SYMBOL_GPL(sfp_link_up);
674 void sfp_link_down(struct sfp_bus *bus)
676 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
678 if (ops && ops->link_down)
679 ops->link_down(bus->upstream);
681 EXPORT_SYMBOL_GPL(sfp_link_down);
683 int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
685 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
688 bus->sfp_quirk = sfp_lookup_quirk(id);
690 if (ops && ops->module_insert)
691 ret = ops->module_insert(bus->upstream, id);
695 EXPORT_SYMBOL_GPL(sfp_module_insert);
697 void sfp_module_remove(struct sfp_bus *bus)
699 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
701 if (ops && ops->module_remove)
702 ops->module_remove(bus->upstream);
704 bus->sfp_quirk = NULL;
706 EXPORT_SYMBOL_GPL(sfp_module_remove);
708 static void sfp_socket_clear(struct sfp_bus *bus)
712 bus->socket_ops = NULL;
715 struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
716 const struct sfp_socket_ops *ops)
718 struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
725 bus->socket_ops = ops;
727 if (bus->upstream_ops) {
728 ret = sfp_register_bus(bus);
730 sfp_socket_clear(bus);
742 EXPORT_SYMBOL_GPL(sfp_register_socket);
744 void sfp_unregister_socket(struct sfp_bus *bus)
747 if (bus->upstream_ops)
748 sfp_unregister_bus(bus);
749 sfp_socket_clear(bus);
754 EXPORT_SYMBOL_GPL(sfp_unregister_socket);