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
);
333 * sfp_bus_put() - put a reference on the &struct sfp_bus
334 * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
336 * Put a reference on the &struct sfp_bus and free the underlying structure
337 * if this was the last reference.
339 void sfp_bus_put(struct sfp_bus
*bus
)
342 kref_put_mutex(&bus
->kref
, sfp_bus_release
, &sfp_mutex
);
344 EXPORT_SYMBOL_GPL(sfp_bus_put
);
346 static int sfp_register_bus(struct sfp_bus
*bus
)
348 const struct sfp_upstream_ops
*ops
= bus
->upstream_ops
;
353 ops
->link_down(bus
->upstream
);
354 if (ops
->connect_phy
&& bus
->phydev
) {
355 ret
= ops
->connect_phy(bus
->upstream
, bus
->phydev
);
360 bus
->registered
= true;
361 bus
->socket_ops
->attach(bus
->sfp
);
363 bus
->socket_ops
->start(bus
->sfp
);
364 bus
->upstream_ops
->attach(bus
->upstream
, bus
);
368 static void sfp_unregister_bus(struct sfp_bus
*bus
)
370 const struct sfp_upstream_ops
*ops
= bus
->upstream_ops
;
372 if (bus
->registered
) {
373 bus
->upstream_ops
->detach(bus
->upstream
, bus
);
375 bus
->socket_ops
->stop(bus
->sfp
);
376 bus
->socket_ops
->detach(bus
->sfp
);
377 if (bus
->phydev
&& ops
&& ops
->disconnect_phy
)
378 ops
->disconnect_phy(bus
->upstream
);
380 bus
->registered
= false;
384 * sfp_get_module_info() - Get the ethtool_modinfo for a SFP module
385 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
386 * @modinfo: a &struct ethtool_modinfo
388 * Fill in the type and eeprom_len parameters in @modinfo for a module on
389 * the sfp bus specified by @bus.
391 * Returns 0 on success or a negative errno number.
393 int sfp_get_module_info(struct sfp_bus
*bus
, struct ethtool_modinfo
*modinfo
)
395 return bus
->socket_ops
->module_info(bus
->sfp
, modinfo
);
397 EXPORT_SYMBOL_GPL(sfp_get_module_info
);
400 * sfp_get_module_eeprom() - Read the SFP module EEPROM
401 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
402 * @ee: a &struct ethtool_eeprom
403 * @data: buffer to contain the EEPROM data (must be at least @ee->len bytes)
405 * Read the EEPROM as specified by the supplied @ee. See the documentation
406 * for &struct ethtool_eeprom for the region to be read.
408 * Returns 0 on success or a negative errno number.
410 int sfp_get_module_eeprom(struct sfp_bus
*bus
, struct ethtool_eeprom
*ee
,
413 return bus
->socket_ops
->module_eeprom(bus
->sfp
, ee
, data
);
415 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom
);
418 * sfp_upstream_start() - Inform the SFP that the network device is up
419 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
421 * Inform the SFP socket that the network device is now up, so that the
422 * module can be enabled by allowing TX_DISABLE to be deasserted. This
423 * should be called from the network device driver's &struct net_device_ops
426 void sfp_upstream_start(struct sfp_bus
*bus
)
429 bus
->socket_ops
->start(bus
->sfp
);
432 EXPORT_SYMBOL_GPL(sfp_upstream_start
);
435 * sfp_upstream_stop() - Inform the SFP that the network device is down
436 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
438 * Inform the SFP socket that the network device is now up, so that the
439 * module can be disabled by asserting TX_DISABLE, disabling the laser
440 * in optical modules. This should be called from the network device
441 * driver's &struct net_device_ops ndo_stop() method.
443 void sfp_upstream_stop(struct sfp_bus
*bus
)
446 bus
->socket_ops
->stop(bus
->sfp
);
447 bus
->started
= false;
449 EXPORT_SYMBOL_GPL(sfp_upstream_stop
);
451 static void sfp_upstream_clear(struct sfp_bus
*bus
)
453 bus
->upstream_ops
= NULL
;
454 bus
->upstream
= NULL
;
458 * sfp_bus_find_fwnode() - parse and locate the SFP bus from fwnode
459 * @fwnode: firmware node for the parent device (MAC or PHY)
461 * Parse the parent device's firmware node for a SFP bus, and locate
462 * the sfp_bus structure, incrementing its reference count. This must
463 * be put via sfp_bus_put() when done.
465 * Returns: on success, a pointer to the sfp_bus structure,
466 * %NULL if no SFP is specified,
467 * on failure, an error pointer value:
468 * corresponding to the errors detailed for
469 * fwnode_property_get_reference_args().
470 * %-ENOMEM if we failed to allocate the bus.
471 * an error from the upstream's connect_phy() method.
473 struct sfp_bus
*sfp_bus_find_fwnode(struct fwnode_handle
*fwnode
)
475 struct fwnode_reference_args ref
;
479 ret
= fwnode_property_get_reference_args(fwnode
, "sfp", NULL
,
486 bus
= sfp_bus_get(ref
.fwnode
);
487 fwnode_handle_put(ref
.fwnode
);
489 return ERR_PTR(-ENOMEM
);
493 EXPORT_SYMBOL_GPL(sfp_bus_find_fwnode
);
496 * sfp_bus_add_upstream() - parse and register the neighbouring device
497 * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
498 * @upstream: the upstream private data
499 * @ops: the upstream's &struct sfp_upstream_ops
501 * Add upstream driver for the SFP bus, and if the bus is complete, register
502 * the SFP bus using sfp_register_upstream(). This takes a reference on the
503 * bus, so it is safe to put the bus after this call.
505 * Returns: on success, a pointer to the sfp_bus structure,
506 * %NULL if no SFP is specified,
507 * on failure, an error pointer value:
508 * corresponding to the errors detailed for
509 * fwnode_property_get_reference_args().
510 * %-ENOMEM if we failed to allocate the bus.
511 * an error from the upstream's connect_phy() method.
513 int sfp_bus_add_upstream(struct sfp_bus
*bus
, void *upstream
,
514 const struct sfp_upstream_ops
*ops
)
518 /* If no bus, return success */
523 kref_get(&bus
->kref
);
524 bus
->upstream_ops
= ops
;
525 bus
->upstream
= upstream
;
528 ret
= sfp_register_bus(bus
);
530 sfp_upstream_clear(bus
);
541 EXPORT_SYMBOL_GPL(sfp_bus_add_upstream
);
544 * sfp_bus_del_upstream() - Delete a sfp bus
545 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
547 * Delete a previously registered upstream connection for the SFP
548 * module. @bus should have been added by sfp_bus_add_upstream().
550 void sfp_bus_del_upstream(struct sfp_bus
*bus
)
555 sfp_unregister_bus(bus
);
556 sfp_upstream_clear(bus
);
562 EXPORT_SYMBOL_GPL(sfp_bus_del_upstream
);
564 /* Socket driver entry points */
565 int sfp_add_phy(struct sfp_bus
*bus
, struct phy_device
*phydev
)
567 const struct sfp_upstream_ops
*ops
= sfp_get_upstream_ops(bus
);
570 if (ops
&& ops
->connect_phy
)
571 ret
= ops
->connect_phy(bus
->upstream
, phydev
);
574 bus
->phydev
= phydev
;
578 EXPORT_SYMBOL_GPL(sfp_add_phy
);
580 void sfp_remove_phy(struct sfp_bus
*bus
)
582 const struct sfp_upstream_ops
*ops
= sfp_get_upstream_ops(bus
);
584 if (ops
&& ops
->disconnect_phy
)
585 ops
->disconnect_phy(bus
->upstream
);
588 EXPORT_SYMBOL_GPL(sfp_remove_phy
);
590 void sfp_link_up(struct sfp_bus
*bus
)
592 const struct sfp_upstream_ops
*ops
= sfp_get_upstream_ops(bus
);
594 if (ops
&& ops
->link_up
)
595 ops
->link_up(bus
->upstream
);
597 EXPORT_SYMBOL_GPL(sfp_link_up
);
599 void sfp_link_down(struct sfp_bus
*bus
)
601 const struct sfp_upstream_ops
*ops
= sfp_get_upstream_ops(bus
);
603 if (ops
&& ops
->link_down
)
604 ops
->link_down(bus
->upstream
);
606 EXPORT_SYMBOL_GPL(sfp_link_down
);
608 int sfp_module_insert(struct sfp_bus
*bus
, const struct sfp_eeprom_id
*id
)
610 const struct sfp_upstream_ops
*ops
= sfp_get_upstream_ops(bus
);
613 if (ops
&& ops
->module_insert
)
614 ret
= ops
->module_insert(bus
->upstream
, id
);
618 EXPORT_SYMBOL_GPL(sfp_module_insert
);
620 void sfp_module_remove(struct sfp_bus
*bus
)
622 const struct sfp_upstream_ops
*ops
= sfp_get_upstream_ops(bus
);
624 if (ops
&& ops
->module_remove
)
625 ops
->module_remove(bus
->upstream
);
627 EXPORT_SYMBOL_GPL(sfp_module_remove
);
629 static void sfp_socket_clear(struct sfp_bus
*bus
)
633 bus
->socket_ops
= NULL
;
636 struct sfp_bus
*sfp_register_socket(struct device
*dev
, struct sfp
*sfp
,
637 const struct sfp_socket_ops
*ops
)
639 struct sfp_bus
*bus
= sfp_bus_get(dev
->fwnode
);
646 bus
->socket_ops
= ops
;
648 if (bus
->upstream_ops
) {
649 ret
= sfp_register_bus(bus
);
651 sfp_socket_clear(bus
);
663 EXPORT_SYMBOL_GPL(sfp_register_socket
);
665 void sfp_unregister_socket(struct sfp_bus
*bus
)
668 if (bus
->upstream_ops
)
669 sfp_unregister_bus(bus
);
670 sfp_socket_clear(bus
);
675 EXPORT_SYMBOL_GPL(sfp_unregister_socket
);