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>
14 * struct sfp_bus - internal representation of a sfp bus
19 struct list_head node;
20 struct fwnode_handle *fwnode;
22 const struct sfp_socket_ops *socket_ops;
23 struct device *sfp_dev;
26 const struct sfp_upstream_ops *upstream_ops;
28 struct phy_device *phydev;
35 * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
36 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
37 * @id: a pointer to the module's &struct sfp_eeprom_id
38 * @support: optional pointer to an array of unsigned long for the
39 * ethtool support mask
41 * Parse the EEPROM identification given in @id, and return one of
42 * %PORT_TP, %PORT_FIBRE or %PORT_OTHER. If @support is non-%NULL,
43 * also set the ethtool %ETHTOOL_LINK_MODE_xxx_BIT corresponding with
46 * If the port type is not known, returns %PORT_OTHER.
48 int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
49 unsigned long *support)
53 /* port is the physical connector, set this from the connector field. */
54 switch (id->base.connector) {
55 case SFP_CONNECTOR_SC:
56 case SFP_CONNECTOR_FIBERJACK:
57 case SFP_CONNECTOR_LC:
58 case SFP_CONNECTOR_MT_RJ:
59 case SFP_CONNECTOR_MU:
60 case SFP_CONNECTOR_OPTICAL_PIGTAIL:
64 case SFP_CONNECTOR_RJ45:
68 case SFP_CONNECTOR_COPPER_PIGTAIL:
72 case SFP_CONNECTOR_UNSPEC:
73 if (id->base.e1000_base_t) {
78 case SFP_CONNECTOR_SG: /* guess */
79 case SFP_CONNECTOR_MPO_1X12:
80 case SFP_CONNECTOR_MPO_2X16:
81 case SFP_CONNECTOR_HSSDC_II:
82 case SFP_CONNECTOR_NOSEPARATE:
83 case SFP_CONNECTOR_MXC_2X16:
87 dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
96 phylink_set(support, FIBRE);
100 phylink_set(support, TP);
107 EXPORT_SYMBOL_GPL(sfp_parse_port);
110 * sfp_parse_support() - Parse the eeprom id for supported link modes
111 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
112 * @id: a pointer to the module's &struct sfp_eeprom_id
113 * @support: pointer to an array of unsigned long for the ethtool support mask
115 * Parse the EEPROM identification information and derive the supported
116 * ethtool link modes for the module.
118 void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
119 unsigned long *support)
121 unsigned int br_min, br_nom, br_max;
122 __ETHTOOL_DECLARE_LINK_MODE_MASK(modes) = { 0, };
124 /* Decode the bitrate information to MBd */
125 br_min = br_nom = br_max = 0;
126 if (id->base.br_nominal) {
127 if (id->base.br_nominal != 255) {
128 br_nom = id->base.br_nominal * 100;
129 br_min = br_nom - id->base.br_nominal * id->ext.br_min;
130 br_max = br_nom + id->base.br_nominal * id->ext.br_max;
131 } else if (id->ext.br_max) {
132 br_nom = 250 * id->ext.br_max;
133 br_max = br_nom + br_nom * id->ext.br_min / 100;
134 br_min = br_nom - br_nom * id->ext.br_min / 100;
137 /* When using passive cables, in case neither BR,min nor BR,max
138 * are specified, set br_min to 0 as the nominal value is then
139 * used as the maximum.
141 if (br_min == br_max && id->base.sfp_ct_passive)
145 /* Set ethtool support from the compliance fields. */
146 if (id->base.e10g_base_sr)
147 phylink_set(modes, 10000baseSR_Full);
148 if (id->base.e10g_base_lr)
149 phylink_set(modes, 10000baseLR_Full);
150 if (id->base.e10g_base_lrm)
151 phylink_set(modes, 10000baseLRM_Full);
152 if (id->base.e10g_base_er)
153 phylink_set(modes, 10000baseER_Full);
154 if (id->base.e1000_base_sx ||
155 id->base.e1000_base_lx ||
156 id->base.e1000_base_cx)
157 phylink_set(modes, 1000baseX_Full);
158 if (id->base.e1000_base_t) {
159 phylink_set(modes, 1000baseT_Half);
160 phylink_set(modes, 1000baseT_Full);
163 /* 1000Base-PX or 1000Base-BX10 */
164 if ((id->base.e_base_px || id->base.e_base_bx10) &&
165 br_min <= 1300 && br_max >= 1200)
166 phylink_set(modes, 1000baseX_Full);
168 /* For active or passive cables, select the link modes
169 * based on the bit rates and the cable compliance bytes.
171 if ((id->base.sfp_ct_passive || id->base.sfp_ct_active) && br_nom) {
172 /* This may look odd, but some manufacturers use 12000MBd */
173 if (br_min <= 12000 && br_max >= 10300)
174 phylink_set(modes, 10000baseCR_Full);
175 if (br_min <= 3200 && br_max >= 3100)
176 phylink_set(modes, 2500baseX_Full);
177 if (br_min <= 1300 && br_max >= 1200)
178 phylink_set(modes, 1000baseX_Full);
180 if (id->base.sfp_ct_passive) {
181 if (id->base.passive.sff8431_app_e)
182 phylink_set(modes, 10000baseCR_Full);
184 if (id->base.sfp_ct_active) {
185 if (id->base.active.sff8431_app_e ||
186 id->base.active.sff8431_lim) {
187 phylink_set(modes, 10000baseCR_Full);
191 switch (id->base.extended_cc) {
192 case 0x00: /* Unspecified */
194 case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */
195 phylink_set(modes, 100000baseSR4_Full);
196 phylink_set(modes, 25000baseSR_Full);
198 case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */
199 case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */
200 phylink_set(modes, 100000baseLR4_ER4_Full);
202 case 0x0b: /* 100Gbase-CR4 or 25Gbase-CR CA-L */
203 case 0x0c: /* 25Gbase-CR CA-S */
204 case 0x0d: /* 25Gbase-CR CA-N */
205 phylink_set(modes, 100000baseCR4_Full);
206 phylink_set(modes, 25000baseCR_Full);
209 dev_warn(bus->sfp_dev,
210 "Unknown/unsupported extended compliance code: 0x%02x\n",
211 id->base.extended_cc);
215 /* For fibre channel SFP, derive possible BaseX modes */
216 if (id->base.fc_speed_100 ||
217 id->base.fc_speed_200 ||
218 id->base.fc_speed_400) {
219 if (id->base.br_nominal >= 31)
220 phylink_set(modes, 2500baseX_Full);
221 if (id->base.br_nominal >= 12)
222 phylink_set(modes, 1000baseX_Full);
225 /* If we haven't discovered any modes that this module supports, try
226 * the encoding and bitrate to determine supported modes. Some BiDi
227 * modules (eg, 1310nm/1550nm) are not 1000BASE-BX compliant due to
228 * the differing wavelengths, so do not set any transceiver bits.
230 if (bitmap_empty(modes, __ETHTOOL_LINK_MODE_MASK_NBITS)) {
231 /* If the encoding and bit rate allows 1000baseX */
232 if (id->base.encoding == SFP_ENCODING_8B10B && br_nom &&
233 br_min <= 1300 && br_max >= 1200)
234 phylink_set(modes, 1000baseX_Full);
237 bitmap_or(support, support, modes, __ETHTOOL_LINK_MODE_MASK_NBITS);
239 phylink_set(support, Autoneg);
240 phylink_set(support, Pause);
241 phylink_set(support, Asym_Pause);
243 EXPORT_SYMBOL_GPL(sfp_parse_support);
246 * sfp_select_interface() - Select appropriate phy_interface_t mode
247 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
248 * @id: a pointer to the module's &struct sfp_eeprom_id
249 * @link_modes: ethtool link modes mask
251 * Derive the phy_interface_t mode for the information found in the
252 * module's identifying EEPROM and the link modes mask. There is no
253 * standard or defined way to derive this information, so we decide
254 * based upon the link mode mask.
256 phy_interface_t sfp_select_interface(struct sfp_bus *bus,
257 const struct sfp_eeprom_id *id,
258 unsigned long *link_modes)
260 if (phylink_test(link_modes, 10000baseCR_Full) ||
261 phylink_test(link_modes, 10000baseSR_Full) ||
262 phylink_test(link_modes, 10000baseLR_Full) ||
263 phylink_test(link_modes, 10000baseLRM_Full) ||
264 phylink_test(link_modes, 10000baseER_Full))
265 return PHY_INTERFACE_MODE_10GKR;
267 if (phylink_test(link_modes, 2500baseX_Full))
268 return PHY_INTERFACE_MODE_2500BASEX;
270 if (id->base.e1000_base_t ||
271 id->base.e100_base_lx ||
272 id->base.e100_base_fx)
273 return PHY_INTERFACE_MODE_SGMII;
275 if (phylink_test(link_modes, 1000baseX_Full))
276 return PHY_INTERFACE_MODE_1000BASEX;
278 dev_warn(bus->sfp_dev, "Unable to ascertain link mode\n");
280 return PHY_INTERFACE_MODE_NA;
282 EXPORT_SYMBOL_GPL(sfp_select_interface);
284 static LIST_HEAD(sfp_buses);
285 static DEFINE_MUTEX(sfp_mutex);
287 static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
289 return bus->registered ? bus->upstream_ops : NULL;
292 static struct sfp_bus *sfp_bus_get(struct fwnode_handle *fwnode)
294 struct sfp_bus *sfp, *new, *found = NULL;
296 new = kzalloc(sizeof(*new), GFP_KERNEL);
298 mutex_lock(&sfp_mutex);
300 list_for_each_entry(sfp, &sfp_buses, node) {
301 if (sfp->fwnode == fwnode) {
302 kref_get(&sfp->kref);
309 kref_init(&new->kref);
310 new->fwnode = fwnode;
311 list_add(&new->node, &sfp_buses);
316 mutex_unlock(&sfp_mutex);
323 static void sfp_bus_release(struct kref *kref)
325 struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
327 list_del(&bus->node);
328 mutex_unlock(&sfp_mutex);
332 static void sfp_bus_put(struct sfp_bus *bus)
334 kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
337 static int sfp_register_bus(struct sfp_bus *bus)
339 const struct sfp_upstream_ops *ops = bus->upstream_ops;
344 ops->link_down(bus->upstream);
345 if (ops->connect_phy && bus->phydev) {
346 ret = ops->connect_phy(bus->upstream, bus->phydev);
351 bus->socket_ops->attach(bus->sfp);
353 bus->socket_ops->start(bus->sfp);
354 bus->upstream_ops->attach(bus->upstream, bus);
355 bus->registered = true;
359 static void sfp_unregister_bus(struct sfp_bus *bus)
361 const struct sfp_upstream_ops *ops = bus->upstream_ops;
363 if (bus->registered) {
364 bus->upstream_ops->detach(bus->upstream, bus);
366 bus->socket_ops->stop(bus->sfp);
367 bus->socket_ops->detach(bus->sfp);
368 if (bus->phydev && ops && ops->disconnect_phy)
369 ops->disconnect_phy(bus->upstream);
371 bus->registered = false;
375 * sfp_get_module_info() - Get the ethtool_modinfo for a SFP module
376 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
377 * @modinfo: a &struct ethtool_modinfo
379 * Fill in the type and eeprom_len parameters in @modinfo for a module on
380 * the sfp bus specified by @bus.
382 * Returns 0 on success or a negative errno number.
384 int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
386 return bus->socket_ops->module_info(bus->sfp, modinfo);
388 EXPORT_SYMBOL_GPL(sfp_get_module_info);
391 * sfp_get_module_eeprom() - Read the SFP module EEPROM
392 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
393 * @ee: a &struct ethtool_eeprom
394 * @data: buffer to contain the EEPROM data (must be at least @ee->len bytes)
396 * Read the EEPROM as specified by the supplied @ee. See the documentation
397 * for &struct ethtool_eeprom for the region to be read.
399 * Returns 0 on success or a negative errno number.
401 int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
404 return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
406 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
409 * sfp_upstream_start() - Inform the SFP that the network device is up
410 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
412 * Inform the SFP socket that the network device is now up, so that the
413 * module can be enabled by allowing TX_DISABLE to be deasserted. This
414 * should be called from the network device driver's &struct net_device_ops
417 void sfp_upstream_start(struct sfp_bus *bus)
420 bus->socket_ops->start(bus->sfp);
423 EXPORT_SYMBOL_GPL(sfp_upstream_start);
426 * sfp_upstream_stop() - Inform the SFP that the network device is down
427 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
429 * Inform the SFP socket that the network device is now up, so that the
430 * module can be disabled by asserting TX_DISABLE, disabling the laser
431 * in optical modules. This should be called from the network device
432 * driver's &struct net_device_ops ndo_stop() method.
434 void sfp_upstream_stop(struct sfp_bus *bus)
437 bus->socket_ops->stop(bus->sfp);
438 bus->started = false;
440 EXPORT_SYMBOL_GPL(sfp_upstream_stop);
442 static void sfp_upstream_clear(struct sfp_bus *bus)
444 bus->upstream_ops = NULL;
445 bus->upstream = NULL;
449 * sfp_register_upstream_node() - parse and register the neighbouring device
450 * @fwnode: firmware node for the parent device (MAC or PHY)
451 * @upstream: the upstream private data
452 * @ops: the upstream's &struct sfp_upstream_ops
454 * Parse the parent device's firmware node for a SFP bus, and register the
455 * SFP bus using sfp_register_upstream().
457 * Returns: on success, a pointer to the sfp_bus structure,
458 * %NULL if no SFP is specified,
459 * on failure, an error pointer value:
460 * corresponding to the errors detailed for
461 * fwnode_property_get_reference_args().
462 * %-ENOMEM if we failed to allocate the bus.
463 * an error from the upstream's connect_phy() method.
465 struct sfp_bus *sfp_register_upstream_node(struct fwnode_handle *fwnode,
467 const struct sfp_upstream_ops *ops)
469 struct fwnode_reference_args ref;
473 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
480 bus = sfp_bus_get(ref.fwnode);
481 fwnode_handle_put(ref.fwnode);
483 return ERR_PTR(-ENOMEM);
486 bus->upstream_ops = ops;
487 bus->upstream = upstream;
490 ret = sfp_register_bus(bus);
492 sfp_upstream_clear(bus);
505 EXPORT_SYMBOL_GPL(sfp_register_upstream_node);
508 * sfp_unregister_upstream() - Unregister sfp bus
509 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
511 * Unregister a previously registered upstream connection for the SFP
512 * module. @bus is returned from sfp_register_upstream().
514 void sfp_unregister_upstream(struct sfp_bus *bus)
518 sfp_unregister_bus(bus);
519 sfp_upstream_clear(bus);
524 EXPORT_SYMBOL_GPL(sfp_unregister_upstream);
526 /* Socket driver entry points */
527 int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
529 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
532 if (ops && ops->connect_phy)
533 ret = ops->connect_phy(bus->upstream, phydev);
536 bus->phydev = phydev;
540 EXPORT_SYMBOL_GPL(sfp_add_phy);
542 void sfp_remove_phy(struct sfp_bus *bus)
544 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
546 if (ops && ops->disconnect_phy)
547 ops->disconnect_phy(bus->upstream);
550 EXPORT_SYMBOL_GPL(sfp_remove_phy);
552 void sfp_link_up(struct sfp_bus *bus)
554 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
556 if (ops && ops->link_up)
557 ops->link_up(bus->upstream);
559 EXPORT_SYMBOL_GPL(sfp_link_up);
561 void sfp_link_down(struct sfp_bus *bus)
563 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
565 if (ops && ops->link_down)
566 ops->link_down(bus->upstream);
568 EXPORT_SYMBOL_GPL(sfp_link_down);
570 int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
572 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
575 if (ops && ops->module_insert)
576 ret = ops->module_insert(bus->upstream, id);
580 EXPORT_SYMBOL_GPL(sfp_module_insert);
582 void sfp_module_remove(struct sfp_bus *bus)
584 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
586 if (ops && ops->module_remove)
587 ops->module_remove(bus->upstream);
589 EXPORT_SYMBOL_GPL(sfp_module_remove);
591 static void sfp_socket_clear(struct sfp_bus *bus)
595 bus->socket_ops = NULL;
598 struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
599 const struct sfp_socket_ops *ops)
601 struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
608 bus->socket_ops = ops;
610 if (bus->upstream_ops) {
611 ret = sfp_register_bus(bus);
613 sfp_socket_clear(bus);
625 EXPORT_SYMBOL_GPL(sfp_register_socket);
627 void sfp_unregister_socket(struct sfp_bus *bus)
630 if (bus->upstream_ops)
631 sfp_unregister_bus(bus);
632 sfp_socket_clear(bus);
637 EXPORT_SYMBOL_GPL(sfp_unregister_socket);