Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/export.h>
0003 #include <linux/kref.h>
0004 #include <linux/list.h>
0005 #include <linux/mutex.h>
0006 #include <linux/phylink.h>
0007 #include <linux/property.h>
0008 #include <linux/rtnetlink.h>
0009 #include <linux/slab.h>
0010 
0011 #include "sfp.h"
0012 
0013 struct sfp_quirk {
0014     const char *vendor;
0015     const char *part;
0016     void (*modes)(const struct sfp_eeprom_id *id, unsigned long *modes);
0017 };
0018 
0019 /**
0020  * struct sfp_bus - internal representation of a sfp bus
0021  */
0022 struct sfp_bus {
0023     /* private: */
0024     struct kref kref;
0025     struct list_head node;
0026     struct fwnode_handle *fwnode;
0027 
0028     const struct sfp_socket_ops *socket_ops;
0029     struct device *sfp_dev;
0030     struct sfp *sfp;
0031     const struct sfp_quirk *sfp_quirk;
0032 
0033     const struct sfp_upstream_ops *upstream_ops;
0034     void *upstream;
0035     struct phy_device *phydev;
0036 
0037     bool registered;
0038     bool started;
0039 };
0040 
0041 static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id,
0042                 unsigned long *modes)
0043 {
0044     phylink_set(modes, 2500baseX_Full);
0045 }
0046 
0047 static void sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id *id,
0048                       unsigned long *modes)
0049 {
0050     /* Ubiquiti U-Fiber Instant module claims that support all transceiver
0051      * types including 10G Ethernet which is not truth. So clear all claimed
0052      * modes and set only one mode which module supports: 1000baseX_Full.
0053      */
0054     phylink_zero(modes);
0055     phylink_set(modes, 1000baseX_Full);
0056 }
0057 
0058 static const struct sfp_quirk sfp_quirks[] = {
0059     {
0060         // Alcatel Lucent G-010S-P can operate at 2500base-X, but
0061         // incorrectly report 2500MBd NRZ in their EEPROM
0062         .vendor = "ALCATELLUCENT",
0063         .part = "G010SP",
0064         .modes = sfp_quirk_2500basex,
0065     }, {
0066         // Alcatel Lucent G-010S-A can operate at 2500base-X, but
0067         // report 3.2GBd NRZ in their EEPROM
0068         .vendor = "ALCATELLUCENT",
0069         .part = "3FE46541AA",
0070         .modes = sfp_quirk_2500basex,
0071     }, {
0072         // Huawei MA5671A can operate at 2500base-X, but report 1.2GBd
0073         // NRZ in their EEPROM
0074         .vendor = "HUAWEI",
0075         .part = "MA5671A",
0076         .modes = sfp_quirk_2500basex,
0077     }, {
0078         // Lantech 8330-262D-E can operate at 2500base-X, but
0079         // incorrectly report 2500MBd NRZ in their EEPROM
0080         .vendor = "Lantech",
0081         .part = "8330-262D-E",
0082         .modes = sfp_quirk_2500basex,
0083     }, {
0084         .vendor = "UBNT",
0085         .part = "UF-INSTANT",
0086         .modes = sfp_quirk_ubnt_uf_instant,
0087     },
0088 };
0089 
0090 static size_t sfp_strlen(const char *str, size_t maxlen)
0091 {
0092     size_t size, i;
0093 
0094     /* Trailing characters should be filled with space chars */
0095     for (i = 0, size = 0; i < maxlen; i++)
0096         if (str[i] != ' ')
0097             size = i + 1;
0098 
0099     return size;
0100 }
0101 
0102 static bool sfp_match(const char *qs, const char *str, size_t len)
0103 {
0104     if (!qs)
0105         return true;
0106     if (strlen(qs) != len)
0107         return false;
0108     return !strncmp(qs, str, len);
0109 }
0110 
0111 static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
0112 {
0113     const struct sfp_quirk *q;
0114     unsigned int i;
0115     size_t vs, ps;
0116 
0117     vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name));
0118     ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn));
0119 
0120     for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++)
0121         if (sfp_match(q->vendor, id->base.vendor_name, vs) &&
0122             sfp_match(q->part, id->base.vendor_pn, ps))
0123             return q;
0124 
0125     return NULL;
0126 }
0127 
0128 /**
0129  * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
0130  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
0131  * @id: a pointer to the module's &struct sfp_eeprom_id
0132  * @support: optional pointer to an array of unsigned long for the
0133  *   ethtool support mask
0134  *
0135  * Parse the EEPROM identification given in @id, and return one of
0136  * %PORT_TP, %PORT_FIBRE or %PORT_OTHER. If @support is non-%NULL,
0137  * also set the ethtool %ETHTOOL_LINK_MODE_xxx_BIT corresponding with
0138  * the connector type.
0139  *
0140  * If the port type is not known, returns %PORT_OTHER.
0141  */
0142 int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
0143            unsigned long *support)
0144 {
0145     int port;
0146 
0147     /* port is the physical connector, set this from the connector field. */
0148     switch (id->base.connector) {
0149     case SFF8024_CONNECTOR_SC:
0150     case SFF8024_CONNECTOR_FIBERJACK:
0151     case SFF8024_CONNECTOR_LC:
0152     case SFF8024_CONNECTOR_MT_RJ:
0153     case SFF8024_CONNECTOR_MU:
0154     case SFF8024_CONNECTOR_OPTICAL_PIGTAIL:
0155     case SFF8024_CONNECTOR_MPO_1X12:
0156     case SFF8024_CONNECTOR_MPO_2X16:
0157         port = PORT_FIBRE;
0158         break;
0159 
0160     case SFF8024_CONNECTOR_RJ45:
0161         port = PORT_TP;
0162         break;
0163 
0164     case SFF8024_CONNECTOR_COPPER_PIGTAIL:
0165         port = PORT_DA;
0166         break;
0167 
0168     case SFF8024_CONNECTOR_UNSPEC:
0169         if (id->base.e1000_base_t) {
0170             port = PORT_TP;
0171             break;
0172         }
0173         fallthrough;
0174     case SFF8024_CONNECTOR_SG: /* guess */
0175     case SFF8024_CONNECTOR_HSSDC_II:
0176     case SFF8024_CONNECTOR_NOSEPARATE:
0177     case SFF8024_CONNECTOR_MXC_2X16:
0178         port = PORT_OTHER;
0179         break;
0180     default:
0181         dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
0182              id->base.connector);
0183         port = PORT_OTHER;
0184         break;
0185     }
0186 
0187     if (support) {
0188         switch (port) {
0189         case PORT_FIBRE:
0190             phylink_set(support, FIBRE);
0191             break;
0192 
0193         case PORT_TP:
0194             phylink_set(support, TP);
0195             break;
0196         }
0197     }
0198 
0199     return port;
0200 }
0201 EXPORT_SYMBOL_GPL(sfp_parse_port);
0202 
0203 /**
0204  * sfp_may_have_phy() - indicate whether the module may have a PHY
0205  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
0206  * @id: a pointer to the module's &struct sfp_eeprom_id
0207  *
0208  * Parse the EEPROM identification given in @id, and return whether
0209  * this module may have a PHY.
0210  */
0211 bool sfp_may_have_phy(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
0212 {
0213     if (id->base.e1000_base_t)
0214         return true;
0215 
0216     if (id->base.phys_id != SFF8024_ID_DWDM_SFP) {
0217         switch (id->base.extended_cc) {
0218         case SFF8024_ECC_10GBASE_T_SFI:
0219         case SFF8024_ECC_10GBASE_T_SR:
0220         case SFF8024_ECC_5GBASE_T:
0221         case SFF8024_ECC_2_5GBASE_T:
0222             return true;
0223         }
0224     }
0225 
0226     return false;
0227 }
0228 EXPORT_SYMBOL_GPL(sfp_may_have_phy);
0229 
0230 /**
0231  * sfp_parse_support() - Parse the eeprom id for supported link modes
0232  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
0233  * @id: a pointer to the module's &struct sfp_eeprom_id
0234  * @support: pointer to an array of unsigned long for the ethtool support mask
0235  *
0236  * Parse the EEPROM identification information and derive the supported
0237  * ethtool link modes for the module.
0238  */
0239 void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
0240                unsigned long *support)
0241 {
0242     unsigned int br_min, br_nom, br_max;
0243     __ETHTOOL_DECLARE_LINK_MODE_MASK(modes) = { 0, };
0244 
0245     /* Decode the bitrate information to MBd */
0246     br_min = br_nom = br_max = 0;
0247     if (id->base.br_nominal) {
0248         if (id->base.br_nominal != 255) {
0249             br_nom = id->base.br_nominal * 100;
0250             br_min = br_nom - id->base.br_nominal * id->ext.br_min;
0251             br_max = br_nom + id->base.br_nominal * id->ext.br_max;
0252         } else if (id->ext.br_max) {
0253             br_nom = 250 * id->ext.br_max;
0254             br_max = br_nom + br_nom * id->ext.br_min / 100;
0255             br_min = br_nom - br_nom * id->ext.br_min / 100;
0256         }
0257 
0258         /* When using passive cables, in case neither BR,min nor BR,max
0259          * are specified, set br_min to 0 as the nominal value is then
0260          * used as the maximum.
0261          */
0262         if (br_min == br_max && id->base.sfp_ct_passive)
0263             br_min = 0;
0264     }
0265 
0266     /* Set ethtool support from the compliance fields. */
0267     if (id->base.e10g_base_sr)
0268         phylink_set(modes, 10000baseSR_Full);
0269     if (id->base.e10g_base_lr)
0270         phylink_set(modes, 10000baseLR_Full);
0271     if (id->base.e10g_base_lrm)
0272         phylink_set(modes, 10000baseLRM_Full);
0273     if (id->base.e10g_base_er)
0274         phylink_set(modes, 10000baseER_Full);
0275     if (id->base.e1000_base_sx ||
0276         id->base.e1000_base_lx ||
0277         id->base.e1000_base_cx)
0278         phylink_set(modes, 1000baseX_Full);
0279     if (id->base.e1000_base_t) {
0280         phylink_set(modes, 1000baseT_Half);
0281         phylink_set(modes, 1000baseT_Full);
0282     }
0283 
0284     /* 1000Base-PX or 1000Base-BX10 */
0285     if ((id->base.e_base_px || id->base.e_base_bx10) &&
0286         br_min <= 1300 && br_max >= 1200)
0287         phylink_set(modes, 1000baseX_Full);
0288 
0289     /* 100Base-FX, 100Base-LX, 100Base-PX, 100Base-BX10 */
0290     if (id->base.e100_base_fx || id->base.e100_base_lx)
0291         phylink_set(modes, 100baseFX_Full);
0292     if ((id->base.e_base_px || id->base.e_base_bx10) && br_nom == 100)
0293         phylink_set(modes, 100baseFX_Full);
0294 
0295     /* For active or passive cables, select the link modes
0296      * based on the bit rates and the cable compliance bytes.
0297      */
0298     if ((id->base.sfp_ct_passive || id->base.sfp_ct_active) && br_nom) {
0299         /* This may look odd, but some manufacturers use 12000MBd */
0300         if (br_min <= 12000 && br_max >= 10300)
0301             phylink_set(modes, 10000baseCR_Full);
0302         if (br_min <= 3200 && br_max >= 3100)
0303             phylink_set(modes, 2500baseX_Full);
0304         if (br_min <= 1300 && br_max >= 1200)
0305             phylink_set(modes, 1000baseX_Full);
0306     }
0307     if (id->base.sfp_ct_passive) {
0308         if (id->base.passive.sff8431_app_e)
0309             phylink_set(modes, 10000baseCR_Full);
0310     }
0311     if (id->base.sfp_ct_active) {
0312         if (id->base.active.sff8431_app_e ||
0313             id->base.active.sff8431_lim) {
0314             phylink_set(modes, 10000baseCR_Full);
0315         }
0316     }
0317 
0318     switch (id->base.extended_cc) {
0319     case SFF8024_ECC_UNSPEC:
0320         break;
0321     case SFF8024_ECC_100GBASE_SR4_25GBASE_SR:
0322         phylink_set(modes, 100000baseSR4_Full);
0323         phylink_set(modes, 25000baseSR_Full);
0324         break;
0325     case SFF8024_ECC_100GBASE_LR4_25GBASE_LR:
0326     case SFF8024_ECC_100GBASE_ER4_25GBASE_ER:
0327         phylink_set(modes, 100000baseLR4_ER4_Full);
0328         break;
0329     case SFF8024_ECC_100GBASE_CR4:
0330         phylink_set(modes, 100000baseCR4_Full);
0331         fallthrough;
0332     case SFF8024_ECC_25GBASE_CR_S:
0333     case SFF8024_ECC_25GBASE_CR_N:
0334         phylink_set(modes, 25000baseCR_Full);
0335         break;
0336     case SFF8024_ECC_10GBASE_T_SFI:
0337     case SFF8024_ECC_10GBASE_T_SR:
0338         phylink_set(modes, 10000baseT_Full);
0339         break;
0340     case SFF8024_ECC_5GBASE_T:
0341         phylink_set(modes, 5000baseT_Full);
0342         break;
0343     case SFF8024_ECC_2_5GBASE_T:
0344         phylink_set(modes, 2500baseT_Full);
0345         break;
0346     default:
0347         dev_warn(bus->sfp_dev,
0348              "Unknown/unsupported extended compliance code: 0x%02x\n",
0349              id->base.extended_cc);
0350         break;
0351     }
0352 
0353     /* For fibre channel SFP, derive possible BaseX modes */
0354     if (id->base.fc_speed_100 ||
0355         id->base.fc_speed_200 ||
0356         id->base.fc_speed_400) {
0357         if (id->base.br_nominal >= 31)
0358             phylink_set(modes, 2500baseX_Full);
0359         if (id->base.br_nominal >= 12)
0360             phylink_set(modes, 1000baseX_Full);
0361     }
0362 
0363     /* If we haven't discovered any modes that this module supports, try
0364      * the bitrate to determine supported modes. Some BiDi modules (eg,
0365      * 1310nm/1550nm) are not 1000BASE-BX compliant due to the differing
0366      * wavelengths, so do not set any transceiver bits.
0367      *
0368      * Do the same for modules supporting 2500BASE-X. Note that some
0369      * modules use 2500Mbaud rather than 3100 or 3200Mbaud for
0370      * 2500BASE-X, so we allow some slack here.
0371      */
0372     if (bitmap_empty(modes, __ETHTOOL_LINK_MODE_MASK_NBITS) && br_nom) {
0373         if (br_min <= 1300 && br_max >= 1200)
0374             phylink_set(modes, 1000baseX_Full);
0375         if (br_min <= 3200 && br_max >= 2500)
0376             phylink_set(modes, 2500baseX_Full);
0377     }
0378 
0379     if (bus->sfp_quirk)
0380         bus->sfp_quirk->modes(id, modes);
0381 
0382     linkmode_or(support, support, modes);
0383 
0384     phylink_set(support, Autoneg);
0385     phylink_set(support, Pause);
0386     phylink_set(support, Asym_Pause);
0387 }
0388 EXPORT_SYMBOL_GPL(sfp_parse_support);
0389 
0390 /**
0391  * sfp_select_interface() - Select appropriate phy_interface_t mode
0392  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
0393  * @link_modes: ethtool link modes mask
0394  *
0395  * Derive the phy_interface_t mode for the SFP module from the link
0396  * modes mask.
0397  */
0398 phy_interface_t sfp_select_interface(struct sfp_bus *bus,
0399                      unsigned long *link_modes)
0400 {
0401     if (phylink_test(link_modes, 25000baseCR_Full) ||
0402         phylink_test(link_modes, 25000baseKR_Full) ||
0403         phylink_test(link_modes, 25000baseSR_Full))
0404         return PHY_INTERFACE_MODE_25GBASER;
0405 
0406     if (phylink_test(link_modes, 10000baseCR_Full) ||
0407         phylink_test(link_modes, 10000baseSR_Full) ||
0408         phylink_test(link_modes, 10000baseLR_Full) ||
0409         phylink_test(link_modes, 10000baseLRM_Full) ||
0410         phylink_test(link_modes, 10000baseER_Full) ||
0411         phylink_test(link_modes, 10000baseT_Full))
0412         return PHY_INTERFACE_MODE_10GBASER;
0413 
0414     if (phylink_test(link_modes, 5000baseT_Full))
0415         return PHY_INTERFACE_MODE_5GBASER;
0416 
0417     if (phylink_test(link_modes, 2500baseX_Full))
0418         return PHY_INTERFACE_MODE_2500BASEX;
0419 
0420     if (phylink_test(link_modes, 1000baseT_Half) ||
0421         phylink_test(link_modes, 1000baseT_Full))
0422         return PHY_INTERFACE_MODE_SGMII;
0423 
0424     if (phylink_test(link_modes, 1000baseX_Full))
0425         return PHY_INTERFACE_MODE_1000BASEX;
0426 
0427     if (phylink_test(link_modes, 100baseFX_Full))
0428         return PHY_INTERFACE_MODE_100BASEX;
0429 
0430     dev_warn(bus->sfp_dev, "Unable to ascertain link mode\n");
0431 
0432     return PHY_INTERFACE_MODE_NA;
0433 }
0434 EXPORT_SYMBOL_GPL(sfp_select_interface);
0435 
0436 static LIST_HEAD(sfp_buses);
0437 static DEFINE_MUTEX(sfp_mutex);
0438 
0439 static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
0440 {
0441     return bus->registered ? bus->upstream_ops : NULL;
0442 }
0443 
0444 static struct sfp_bus *sfp_bus_get(struct fwnode_handle *fwnode)
0445 {
0446     struct sfp_bus *sfp, *new, *found = NULL;
0447 
0448     new = kzalloc(sizeof(*new), GFP_KERNEL);
0449 
0450     mutex_lock(&sfp_mutex);
0451 
0452     list_for_each_entry(sfp, &sfp_buses, node) {
0453         if (sfp->fwnode == fwnode) {
0454             kref_get(&sfp->kref);
0455             found = sfp;
0456             break;
0457         }
0458     }
0459 
0460     if (!found && new) {
0461         kref_init(&new->kref);
0462         new->fwnode = fwnode;
0463         list_add(&new->node, &sfp_buses);
0464         found = new;
0465         new = NULL;
0466     }
0467 
0468     mutex_unlock(&sfp_mutex);
0469 
0470     kfree(new);
0471 
0472     return found;
0473 }
0474 
0475 static void sfp_bus_release(struct kref *kref)
0476 {
0477     struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
0478 
0479     list_del(&bus->node);
0480     mutex_unlock(&sfp_mutex);
0481     kfree(bus);
0482 }
0483 
0484 /**
0485  * sfp_bus_put() - put a reference on the &struct sfp_bus
0486  * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
0487  *
0488  * Put a reference on the &struct sfp_bus and free the underlying structure
0489  * if this was the last reference.
0490  */
0491 void sfp_bus_put(struct sfp_bus *bus)
0492 {
0493     if (bus)
0494         kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
0495 }
0496 EXPORT_SYMBOL_GPL(sfp_bus_put);
0497 
0498 static int sfp_register_bus(struct sfp_bus *bus)
0499 {
0500     const struct sfp_upstream_ops *ops = bus->upstream_ops;
0501     int ret;
0502 
0503     if (ops) {
0504         if (ops->link_down)
0505             ops->link_down(bus->upstream);
0506         if (ops->connect_phy && bus->phydev) {
0507             ret = ops->connect_phy(bus->upstream, bus->phydev);
0508             if (ret)
0509                 return ret;
0510         }
0511     }
0512     bus->registered = true;
0513     bus->socket_ops->attach(bus->sfp);
0514     if (bus->started)
0515         bus->socket_ops->start(bus->sfp);
0516     bus->upstream_ops->attach(bus->upstream, bus);
0517     return 0;
0518 }
0519 
0520 static void sfp_unregister_bus(struct sfp_bus *bus)
0521 {
0522     const struct sfp_upstream_ops *ops = bus->upstream_ops;
0523 
0524     if (bus->registered) {
0525         bus->upstream_ops->detach(bus->upstream, bus);
0526         if (bus->started)
0527             bus->socket_ops->stop(bus->sfp);
0528         bus->socket_ops->detach(bus->sfp);
0529         if (bus->phydev && ops && ops->disconnect_phy)
0530             ops->disconnect_phy(bus->upstream);
0531     }
0532     bus->registered = false;
0533 }
0534 
0535 /**
0536  * sfp_get_module_info() - Get the ethtool_modinfo for a SFP module
0537  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
0538  * @modinfo: a &struct ethtool_modinfo
0539  *
0540  * Fill in the type and eeprom_len parameters in @modinfo for a module on
0541  * the sfp bus specified by @bus.
0542  *
0543  * Returns 0 on success or a negative errno number.
0544  */
0545 int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
0546 {
0547     return bus->socket_ops->module_info(bus->sfp, modinfo);
0548 }
0549 EXPORT_SYMBOL_GPL(sfp_get_module_info);
0550 
0551 /**
0552  * sfp_get_module_eeprom() - Read the SFP module EEPROM
0553  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
0554  * @ee: a &struct ethtool_eeprom
0555  * @data: buffer to contain the EEPROM data (must be at least @ee->len bytes)
0556  *
0557  * Read the EEPROM as specified by the supplied @ee. See the documentation
0558  * for &struct ethtool_eeprom for the region to be read.
0559  *
0560  * Returns 0 on success or a negative errno number.
0561  */
0562 int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
0563               u8 *data)
0564 {
0565     return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
0566 }
0567 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
0568 
0569 /**
0570  * sfp_get_module_eeprom_by_page() - Read a page from the SFP module EEPROM
0571  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
0572  * @page: a &struct ethtool_module_eeprom
0573  * @extack: extack for reporting problems
0574  *
0575  * Read an EEPROM page as specified by the supplied @page. See the
0576  * documentation for &struct ethtool_module_eeprom for the page to be read.
0577  *
0578  * Returns 0 on success or a negative errno number. More error
0579  * information might be provided via extack
0580  */
0581 int sfp_get_module_eeprom_by_page(struct sfp_bus *bus,
0582                   const struct ethtool_module_eeprom *page,
0583                   struct netlink_ext_ack *extack)
0584 {
0585     return bus->socket_ops->module_eeprom_by_page(bus->sfp, page, extack);
0586 }
0587 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom_by_page);
0588 
0589 /**
0590  * sfp_upstream_start() - Inform the SFP that the network device is up
0591  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
0592  *
0593  * Inform the SFP socket that the network device is now up, so that the
0594  * module can be enabled by allowing TX_DISABLE to be deasserted. This
0595  * should be called from the network device driver's &struct net_device_ops
0596  * ndo_open() method.
0597  */
0598 void sfp_upstream_start(struct sfp_bus *bus)
0599 {
0600     if (bus->registered)
0601         bus->socket_ops->start(bus->sfp);
0602     bus->started = true;
0603 }
0604 EXPORT_SYMBOL_GPL(sfp_upstream_start);
0605 
0606 /**
0607  * sfp_upstream_stop() - Inform the SFP that the network device is down
0608  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
0609  *
0610  * Inform the SFP socket that the network device is now up, so that the
0611  * module can be disabled by asserting TX_DISABLE, disabling the laser
0612  * in optical modules. This should be called from the network device
0613  * driver's &struct net_device_ops ndo_stop() method.
0614  */
0615 void sfp_upstream_stop(struct sfp_bus *bus)
0616 {
0617     if (bus->registered)
0618         bus->socket_ops->stop(bus->sfp);
0619     bus->started = false;
0620 }
0621 EXPORT_SYMBOL_GPL(sfp_upstream_stop);
0622 
0623 static void sfp_upstream_clear(struct sfp_bus *bus)
0624 {
0625     bus->upstream_ops = NULL;
0626     bus->upstream = NULL;
0627 }
0628 
0629 /**
0630  * sfp_bus_find_fwnode() - parse and locate the SFP bus from fwnode
0631  * @fwnode: firmware node for the parent device (MAC or PHY)
0632  *
0633  * Parse the parent device's firmware node for a SFP bus, and locate
0634  * the sfp_bus structure, incrementing its reference count.  This must
0635  * be put via sfp_bus_put() when done.
0636  *
0637  * Returns:
0638  *  - on success, a pointer to the sfp_bus structure,
0639  *  - %NULL if no SFP is specified,
0640  *  - on failure, an error pointer value:
0641  *
0642  *  - corresponding to the errors detailed for
0643  *    fwnode_property_get_reference_args().
0644  *  - %-ENOMEM if we failed to allocate the bus.
0645  *  - an error from the upstream's connect_phy() method.
0646  */
0647 struct sfp_bus *sfp_bus_find_fwnode(struct fwnode_handle *fwnode)
0648 {
0649     struct fwnode_reference_args ref;
0650     struct sfp_bus *bus;
0651     int ret;
0652 
0653     ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
0654                          0, 0, &ref);
0655     if (ret == -ENOENT)
0656         return NULL;
0657     else if (ret < 0)
0658         return ERR_PTR(ret);
0659 
0660     if (!fwnode_device_is_available(ref.fwnode)) {
0661         fwnode_handle_put(ref.fwnode);
0662         return NULL;
0663     }
0664 
0665     bus = sfp_bus_get(ref.fwnode);
0666     fwnode_handle_put(ref.fwnode);
0667     if (!bus)
0668         return ERR_PTR(-ENOMEM);
0669 
0670     return bus;
0671 }
0672 EXPORT_SYMBOL_GPL(sfp_bus_find_fwnode);
0673 
0674 /**
0675  * sfp_bus_add_upstream() - parse and register the neighbouring device
0676  * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
0677  * @upstream: the upstream private data
0678  * @ops: the upstream's &struct sfp_upstream_ops
0679  *
0680  * Add upstream driver for the SFP bus, and if the bus is complete, register
0681  * the SFP bus using sfp_register_upstream().  This takes a reference on the
0682  * bus, so it is safe to put the bus after this call.
0683  *
0684  * Returns:
0685  *  - on success, a pointer to the sfp_bus structure,
0686  *  - %NULL if no SFP is specified,
0687  *  - on failure, an error pointer value:
0688  *
0689  *  - corresponding to the errors detailed for
0690  *    fwnode_property_get_reference_args().
0691  *  - %-ENOMEM if we failed to allocate the bus.
0692  *  - an error from the upstream's connect_phy() method.
0693  */
0694 int sfp_bus_add_upstream(struct sfp_bus *bus, void *upstream,
0695              const struct sfp_upstream_ops *ops)
0696 {
0697     int ret;
0698 
0699     /* If no bus, return success */
0700     if (!bus)
0701         return 0;
0702 
0703     rtnl_lock();
0704     kref_get(&bus->kref);
0705     bus->upstream_ops = ops;
0706     bus->upstream = upstream;
0707 
0708     if (bus->sfp) {
0709         ret = sfp_register_bus(bus);
0710         if (ret)
0711             sfp_upstream_clear(bus);
0712     } else {
0713         ret = 0;
0714     }
0715     rtnl_unlock();
0716 
0717     if (ret)
0718         sfp_bus_put(bus);
0719 
0720     return ret;
0721 }
0722 EXPORT_SYMBOL_GPL(sfp_bus_add_upstream);
0723 
0724 /**
0725  * sfp_bus_del_upstream() - Delete a sfp bus
0726  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
0727  *
0728  * Delete a previously registered upstream connection for the SFP
0729  * module. @bus should have been added by sfp_bus_add_upstream().
0730  */
0731 void sfp_bus_del_upstream(struct sfp_bus *bus)
0732 {
0733     if (bus) {
0734         rtnl_lock();
0735         if (bus->sfp)
0736             sfp_unregister_bus(bus);
0737         sfp_upstream_clear(bus);
0738         rtnl_unlock();
0739 
0740         sfp_bus_put(bus);
0741     }
0742 }
0743 EXPORT_SYMBOL_GPL(sfp_bus_del_upstream);
0744 
0745 /* Socket driver entry points */
0746 int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
0747 {
0748     const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
0749     int ret = 0;
0750 
0751     if (ops && ops->connect_phy)
0752         ret = ops->connect_phy(bus->upstream, phydev);
0753 
0754     if (ret == 0)
0755         bus->phydev = phydev;
0756 
0757     return ret;
0758 }
0759 EXPORT_SYMBOL_GPL(sfp_add_phy);
0760 
0761 void sfp_remove_phy(struct sfp_bus *bus)
0762 {
0763     const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
0764 
0765     if (ops && ops->disconnect_phy)
0766         ops->disconnect_phy(bus->upstream);
0767     bus->phydev = NULL;
0768 }
0769 EXPORT_SYMBOL_GPL(sfp_remove_phy);
0770 
0771 void sfp_link_up(struct sfp_bus *bus)
0772 {
0773     const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
0774 
0775     if (ops && ops->link_up)
0776         ops->link_up(bus->upstream);
0777 }
0778 EXPORT_SYMBOL_GPL(sfp_link_up);
0779 
0780 void sfp_link_down(struct sfp_bus *bus)
0781 {
0782     const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
0783 
0784     if (ops && ops->link_down)
0785         ops->link_down(bus->upstream);
0786 }
0787 EXPORT_SYMBOL_GPL(sfp_link_down);
0788 
0789 int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
0790 {
0791     const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
0792     int ret = 0;
0793 
0794     bus->sfp_quirk = sfp_lookup_quirk(id);
0795 
0796     if (ops && ops->module_insert)
0797         ret = ops->module_insert(bus->upstream, id);
0798 
0799     return ret;
0800 }
0801 EXPORT_SYMBOL_GPL(sfp_module_insert);
0802 
0803 void sfp_module_remove(struct sfp_bus *bus)
0804 {
0805     const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
0806 
0807     if (ops && ops->module_remove)
0808         ops->module_remove(bus->upstream);
0809 
0810     bus->sfp_quirk = NULL;
0811 }
0812 EXPORT_SYMBOL_GPL(sfp_module_remove);
0813 
0814 int sfp_module_start(struct sfp_bus *bus)
0815 {
0816     const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
0817     int ret = 0;
0818 
0819     if (ops && ops->module_start)
0820         ret = ops->module_start(bus->upstream);
0821 
0822     return ret;
0823 }
0824 EXPORT_SYMBOL_GPL(sfp_module_start);
0825 
0826 void sfp_module_stop(struct sfp_bus *bus)
0827 {
0828     const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
0829 
0830     if (ops && ops->module_stop)
0831         ops->module_stop(bus->upstream);
0832 }
0833 EXPORT_SYMBOL_GPL(sfp_module_stop);
0834 
0835 static void sfp_socket_clear(struct sfp_bus *bus)
0836 {
0837     bus->sfp_dev = NULL;
0838     bus->sfp = NULL;
0839     bus->socket_ops = NULL;
0840 }
0841 
0842 struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
0843                     const struct sfp_socket_ops *ops)
0844 {
0845     struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
0846     int ret = 0;
0847 
0848     if (bus) {
0849         rtnl_lock();
0850         bus->sfp_dev = dev;
0851         bus->sfp = sfp;
0852         bus->socket_ops = ops;
0853 
0854         if (bus->upstream_ops) {
0855             ret = sfp_register_bus(bus);
0856             if (ret)
0857                 sfp_socket_clear(bus);
0858         }
0859         rtnl_unlock();
0860     }
0861 
0862     if (ret) {
0863         sfp_bus_put(bus);
0864         bus = NULL;
0865     }
0866 
0867     return bus;
0868 }
0869 EXPORT_SYMBOL_GPL(sfp_register_socket);
0870 
0871 void sfp_unregister_socket(struct sfp_bus *bus)
0872 {
0873     rtnl_lock();
0874     if (bus->upstream_ops)
0875         sfp_unregister_bus(bus);
0876     sfp_socket_clear(bus);
0877     rtnl_unlock();
0878 
0879     sfp_bus_put(bus);
0880 }
0881 EXPORT_SYMBOL_GPL(sfp_unregister_socket);