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>
14 void (*modes)(const struct sfp_eeprom_id *id, unsigned long *modes);
18 * struct sfp_bus - internal representation of a sfp bus
23 struct list_head node;
24 struct fwnode_handle *fwnode;
26 const struct sfp_socket_ops *socket_ops;
27 struct device *sfp_dev;
29 const struct sfp_quirk *sfp_quirk;
31 const struct sfp_upstream_ops *upstream_ops;
33 struct net_device *netdev;
34 struct phy_device *phydev;
40 static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id,
43 phylink_set(modes, 2500baseX_Full);
46 static const struct sfp_quirk sfp_quirks[] = {
48 // Alcatel Lucent G-010S-P can operate at 2500base-X, but
49 // incorrectly report 2500MBd NRZ in their EEPROM
50 .vendor = "ALCATELLUCENT",
52 .modes = sfp_quirk_2500basex,
54 // Alcatel Lucent G-010S-A can operate at 2500base-X, but
55 // report 3.2GBd NRZ in their EEPROM
56 .vendor = "ALCATELLUCENT",
58 .modes = sfp_quirk_2500basex,
60 // Huawei MA5671A can operate at 2500base-X, but report 1.2GBd
61 // NRZ in their EEPROM
64 .modes = sfp_quirk_2500basex,
68 static size_t sfp_strlen(const char *str, size_t maxlen)
72 /* Trailing characters should be filled with space chars */
73 for (i = 0, size = 0; i < maxlen; i++)
80 static bool sfp_match(const char *qs, const char *str, size_t len)
84 if (strlen(qs) != len)
86 return !strncmp(qs, str, len);
89 static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
91 const struct sfp_quirk *q;
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));
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))
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
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.
117 * If the port type is not known, returns %PORT_OTHER.
119 int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
120 unsigned long *support)
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:
135 case SFP_CONNECTOR_RJ45:
139 case SFP_CONNECTOR_COPPER_PIGTAIL:
143 case SFP_CONNECTOR_UNSPEC:
144 if (id->base.e1000_base_t) {
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:
158 dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
167 phylink_set(support, FIBRE);
171 phylink_set(support, TP);
178 EXPORT_SYMBOL_GPL(sfp_parse_port);
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
186 * Parse the EEPROM identification information and derive the supported
187 * ethtool link modes for the module.
189 void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
190 unsigned long *support)
192 unsigned int br_min, br_nom, br_max;
193 __ETHTOOL_DECLARE_LINK_MODE_MASK(modes) = { 0, };
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;
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.
212 if (br_min == br_max && id->base.sfp_ct_passive)
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);
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);
239 /* For active or passive cables, select the link modes
240 * based on the bit rates and the cable compliance bytes.
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);
251 if (id->base.sfp_ct_passive) {
252 if (id->base.passive.sff8431_app_e)
253 phylink_set(modes, 10000baseCR_Full);
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);
262 switch (id->base.extended_cc) {
263 case 0x00: /* Unspecified */
265 case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */
266 phylink_set(modes, 100000baseSR4_Full);
267 phylink_set(modes, 25000baseSR_Full);
269 case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */
270 case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */
271 phylink_set(modes, 100000baseLR4_ER4_Full);
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);
280 dev_warn(bus->sfp_dev,
281 "Unknown/unsupported extended compliance code: 0x%02x\n",
282 id->base.extended_cc);
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);
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.
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);
309 bus->sfp_quirk->modes(id, modes);
311 bitmap_or(support, support, modes, __ETHTOOL_LINK_MODE_MASK_NBITS);
313 phylink_set(support, Autoneg);
314 phylink_set(support, Pause);
315 phylink_set(support, Asym_Pause);
317 EXPORT_SYMBOL_GPL(sfp_parse_support);
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
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.
330 phy_interface_t sfp_select_interface(struct sfp_bus *bus,
331 const struct sfp_eeprom_id *id,
332 unsigned long *link_modes)
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;
341 if (phylink_test(link_modes, 2500baseX_Full))
342 return PHY_INTERFACE_MODE_2500BASEX;
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;
349 if (phylink_test(link_modes, 1000baseX_Full))
350 return PHY_INTERFACE_MODE_1000BASEX;
352 dev_warn(bus->sfp_dev, "Unable to ascertain link mode\n");
354 return PHY_INTERFACE_MODE_NA;
356 EXPORT_SYMBOL_GPL(sfp_select_interface);
358 static LIST_HEAD(sfp_buses);
359 static DEFINE_MUTEX(sfp_mutex);
361 static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
363 return bus->registered ? bus->upstream_ops : NULL;
366 static struct sfp_bus *sfp_bus_get(struct fwnode_handle *fwnode)
368 struct sfp_bus *sfp, *new, *found = NULL;
370 new = kzalloc(sizeof(*new), GFP_KERNEL);
372 mutex_lock(&sfp_mutex);
374 list_for_each_entry(sfp, &sfp_buses, node) {
375 if (sfp->fwnode == fwnode) {
376 kref_get(&sfp->kref);
383 kref_init(&new->kref);
384 new->fwnode = fwnode;
385 list_add(&new->node, &sfp_buses);
390 mutex_unlock(&sfp_mutex);
397 static void sfp_bus_release(struct kref *kref)
399 struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
401 list_del(&bus->node);
402 mutex_unlock(&sfp_mutex);
406 static void sfp_bus_put(struct sfp_bus *bus)
408 kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
411 static int sfp_register_bus(struct sfp_bus *bus)
413 const struct sfp_upstream_ops *ops = bus->upstream_ops;
418 ops->link_down(bus->upstream);
419 if (ops->connect_phy && bus->phydev) {
420 ret = ops->connect_phy(bus->upstream, bus->phydev);
425 bus->socket_ops->attach(bus->sfp);
427 bus->socket_ops->start(bus->sfp);
428 bus->netdev->sfp_bus = bus;
429 bus->registered = true;
433 static void sfp_unregister_bus(struct sfp_bus *bus)
435 const struct sfp_upstream_ops *ops = bus->upstream_ops;
437 bus->netdev->sfp_bus = NULL;
438 if (bus->registered) {
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);
445 bus->registered = false;
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
453 * Fill in the type and eeprom_len parameters in @modinfo for a module on
454 * the sfp bus specified by @bus.
456 * Returns 0 on success or a negative errno number.
458 int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
460 return bus->socket_ops->module_info(bus->sfp, modinfo);
462 EXPORT_SYMBOL_GPL(sfp_get_module_info);
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)
470 * Read the EEPROM as specified by the supplied @ee. See the documentation
471 * for &struct ethtool_eeprom for the region to be read.
473 * Returns 0 on success or a negative errno number.
475 int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
478 return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
480 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
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
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
491 void sfp_upstream_start(struct sfp_bus *bus)
494 bus->socket_ops->start(bus->sfp);
497 EXPORT_SYMBOL_GPL(sfp_upstream_start);
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
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.
508 void sfp_upstream_stop(struct sfp_bus *bus)
511 bus->socket_ops->stop(bus->sfp);
512 bus->started = false;
514 EXPORT_SYMBOL_GPL(sfp_upstream_stop);
516 static void sfp_upstream_clear(struct sfp_bus *bus)
518 bus->upstream_ops = NULL;
519 bus->upstream = NULL;
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
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.
534 * On error, returns %NULL.
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)
540 struct sfp_bus *bus = sfp_bus_get(fwnode);
545 bus->upstream_ops = ops;
546 bus->upstream = upstream;
550 ret = sfp_register_bus(bus);
552 sfp_upstream_clear(bus);
564 EXPORT_SYMBOL_GPL(sfp_register_upstream);
567 * sfp_unregister_upstream() - Unregister sfp bus
568 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
570 * Unregister a previously registered upstream connection for the SFP
571 * module. @bus is returned from sfp_register_upstream().
573 void sfp_unregister_upstream(struct sfp_bus *bus)
577 sfp_unregister_bus(bus);
578 sfp_upstream_clear(bus);
583 EXPORT_SYMBOL_GPL(sfp_unregister_upstream);
585 /* Socket driver entry points */
586 int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
588 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
591 if (ops && ops->connect_phy)
592 ret = ops->connect_phy(bus->upstream, phydev);
595 bus->phydev = phydev;
599 EXPORT_SYMBOL_GPL(sfp_add_phy);
601 void sfp_remove_phy(struct sfp_bus *bus)
603 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
605 if (ops && ops->disconnect_phy)
606 ops->disconnect_phy(bus->upstream);
609 EXPORT_SYMBOL_GPL(sfp_remove_phy);
611 void sfp_link_up(struct sfp_bus *bus)
613 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
615 if (ops && ops->link_up)
616 ops->link_up(bus->upstream);
618 EXPORT_SYMBOL_GPL(sfp_link_up);
620 void sfp_link_down(struct sfp_bus *bus)
622 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
624 if (ops && ops->link_down)
625 ops->link_down(bus->upstream);
627 EXPORT_SYMBOL_GPL(sfp_link_down);
629 int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
631 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
634 bus->sfp_quirk = sfp_lookup_quirk(id);
636 if (ops && ops->module_insert)
637 ret = ops->module_insert(bus->upstream, id);
641 EXPORT_SYMBOL_GPL(sfp_module_insert);
643 void sfp_module_remove(struct sfp_bus *bus)
645 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
647 if (ops && ops->module_remove)
648 ops->module_remove(bus->upstream);
650 bus->sfp_quirk = NULL;
652 EXPORT_SYMBOL_GPL(sfp_module_remove);
654 static void sfp_socket_clear(struct sfp_bus *bus)
658 bus->socket_ops = NULL;
661 struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
662 const struct sfp_socket_ops *ops)
664 struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
671 bus->socket_ops = ops;
674 ret = sfp_register_bus(bus);
676 sfp_socket_clear(bus);
688 EXPORT_SYMBOL_GPL(sfp_register_socket);
690 void sfp_unregister_socket(struct sfp_bus *bus)
694 sfp_unregister_bus(bus);
695 sfp_socket_clear(bus);
700 EXPORT_SYMBOL_GPL(sfp_unregister_socket);