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> 10 11 #include "sfp.h" 12 13 /** 14 * struct sfp_bus - internal representation of a sfp bus 15 */ 16 struct sfp_bus { 17 /* private: */ 18 struct kref kref; 19 struct list_head node; 20 struct fwnode_handle *fwnode; 21 22 const struct sfp_socket_ops *socket_ops; 23 struct device *sfp_dev; 24 struct sfp *sfp; 25 const struct sfp_quirk *sfp_quirk; 26 27 const struct sfp_upstream_ops *upstream_ops; 28 void *upstream; 29 struct phy_device *phydev; 30 31 bool registered; 32 bool started; 33 }; 34 35 /** 36 * sfp_parse_port() - Parse the EEPROM base ID, setting the port type 37 * @bus: a pointer to the &struct sfp_bus structure for the sfp module 38 * @id: a pointer to the module's &struct sfp_eeprom_id 39 * @support: optional pointer to an array of unsigned long for the 40 * ethtool support mask 41 * 42 * Parse the EEPROM identification given in @id, and return one of 43 * %PORT_TP, %PORT_FIBRE or %PORT_OTHER. If @support is non-%NULL, 44 * also set the ethtool %ETHTOOL_LINK_MODE_xxx_BIT corresponding with 45 * the connector type. 46 * 47 * If the port type is not known, returns %PORT_OTHER. 48 */ 49 int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id, 50 unsigned long *support) 51 { 52 int port; 53 54 /* port is the physical connector, set this from the connector field. */ 55 switch (id->base.connector) { 56 case SFF8024_CONNECTOR_SC: 57 case SFF8024_CONNECTOR_FIBERJACK: 58 case SFF8024_CONNECTOR_LC: 59 case SFF8024_CONNECTOR_MT_RJ: 60 case SFF8024_CONNECTOR_MU: 61 case SFF8024_CONNECTOR_OPTICAL_PIGTAIL: 62 case SFF8024_CONNECTOR_MPO_1X12: 63 case SFF8024_CONNECTOR_MPO_2X16: 64 port = PORT_FIBRE; 65 break; 66 67 case SFF8024_CONNECTOR_RJ45: 68 port = PORT_TP; 69 break; 70 71 case SFF8024_CONNECTOR_COPPER_PIGTAIL: 72 port = PORT_DA; 73 break; 74 75 case SFF8024_CONNECTOR_UNSPEC: 76 if (id->base.e1000_base_t) { 77 port = PORT_TP; 78 break; 79 } 80 fallthrough; 81 case SFF8024_CONNECTOR_SG: /* guess */ 82 case SFF8024_CONNECTOR_HSSDC_II: 83 case SFF8024_CONNECTOR_NOSEPARATE: 84 case SFF8024_CONNECTOR_MXC_2X16: 85 port = PORT_OTHER; 86 break; 87 default: 88 dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n", 89 id->base.connector); 90 port = PORT_OTHER; 91 break; 92 } 93 94 if (support) { 95 switch (port) { 96 case PORT_FIBRE: 97 phylink_set(support, FIBRE); 98 break; 99 100 case PORT_TP: 101 phylink_set(support, TP); 102 break; 103 } 104 } 105 106 return port; 107 } 108 EXPORT_SYMBOL_GPL(sfp_parse_port); 109 110 /** 111 * sfp_may_have_phy() - indicate whether the module may have a PHY 112 * @bus: a pointer to the &struct sfp_bus structure for the sfp module 113 * @id: a pointer to the module's &struct sfp_eeprom_id 114 * 115 * Parse the EEPROM identification given in @id, and return whether 116 * this module may have a PHY. 117 */ 118 bool sfp_may_have_phy(struct sfp_bus *bus, const struct sfp_eeprom_id *id) 119 { 120 if (id->base.e1000_base_t) 121 return true; 122 123 if (id->base.phys_id != SFF8024_ID_DWDM_SFP) { 124 switch (id->base.extended_cc) { 125 case SFF8024_ECC_10GBASE_T_SFI: 126 case SFF8024_ECC_10GBASE_T_SR: 127 case SFF8024_ECC_5GBASE_T: 128 case SFF8024_ECC_2_5GBASE_T: 129 return true; 130 } 131 } 132 133 return false; 134 } 135 EXPORT_SYMBOL_GPL(sfp_may_have_phy); 136 137 /** 138 * sfp_parse_support() - Parse the eeprom id for supported link modes 139 * @bus: a pointer to the &struct sfp_bus structure for the sfp module 140 * @id: a pointer to the module's &struct sfp_eeprom_id 141 * @support: pointer to an array of unsigned long for the ethtool support mask 142 * 143 * Parse the EEPROM identification information and derive the supported 144 * ethtool link modes for the module. 145 */ 146 void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id, 147 unsigned long *support) 148 { 149 unsigned int br_min, br_nom, br_max; 150 __ETHTOOL_DECLARE_LINK_MODE_MASK(modes) = { 0, }; 151 152 /* Decode the bitrate information to MBd */ 153 br_min = br_nom = br_max = 0; 154 if (id->base.br_nominal) { 155 if (id->base.br_nominal != 255) { 156 br_nom = id->base.br_nominal * 100; 157 br_min = br_nom - id->base.br_nominal * id->ext.br_min; 158 br_max = br_nom + id->base.br_nominal * id->ext.br_max; 159 } else if (id->ext.br_max) { 160 br_nom = 250 * id->ext.br_max; 161 br_max = br_nom + br_nom * id->ext.br_min / 100; 162 br_min = br_nom - br_nom * id->ext.br_min / 100; 163 } 164 165 /* When using passive cables, in case neither BR,min nor BR,max 166 * are specified, set br_min to 0 as the nominal value is then 167 * used as the maximum. 168 */ 169 if (br_min == br_max && id->base.sfp_ct_passive) 170 br_min = 0; 171 } 172 173 /* Set ethtool support from the compliance fields. */ 174 if (id->base.e10g_base_sr) 175 phylink_set(modes, 10000baseSR_Full); 176 if (id->base.e10g_base_lr) 177 phylink_set(modes, 10000baseLR_Full); 178 if (id->base.e10g_base_lrm) 179 phylink_set(modes, 10000baseLRM_Full); 180 if (id->base.e10g_base_er) 181 phylink_set(modes, 10000baseER_Full); 182 if (id->base.e1000_base_sx || 183 id->base.e1000_base_lx || 184 id->base.e1000_base_cx) 185 phylink_set(modes, 1000baseX_Full); 186 if (id->base.e1000_base_t) { 187 phylink_set(modes, 1000baseT_Half); 188 phylink_set(modes, 1000baseT_Full); 189 } 190 191 /* 1000Base-PX or 1000Base-BX10 */ 192 if ((id->base.e_base_px || id->base.e_base_bx10) && 193 br_min <= 1300 && br_max >= 1200) 194 phylink_set(modes, 1000baseX_Full); 195 196 /* 100Base-FX, 100Base-LX, 100Base-PX, 100Base-BX10 */ 197 if (id->base.e100_base_fx || id->base.e100_base_lx) 198 phylink_set(modes, 100baseFX_Full); 199 if ((id->base.e_base_px || id->base.e_base_bx10) && br_nom == 100) 200 phylink_set(modes, 100baseFX_Full); 201 202 /* For active or passive cables, select the link modes 203 * based on the bit rates and the cable compliance bytes. 204 */ 205 if ((id->base.sfp_ct_passive || id->base.sfp_ct_active) && br_nom) { 206 /* This may look odd, but some manufacturers use 12000MBd */ 207 if (br_min <= 12000 && br_max >= 10300) 208 phylink_set(modes, 10000baseCR_Full); 209 if (br_min <= 3200 && br_max >= 3100) 210 phylink_set(modes, 2500baseX_Full); 211 if (br_min <= 1300 && br_max >= 1200) 212 phylink_set(modes, 1000baseX_Full); 213 } 214 if (id->base.sfp_ct_passive) { 215 if (id->base.passive.sff8431_app_e) 216 phylink_set(modes, 10000baseCR_Full); 217 } 218 if (id->base.sfp_ct_active) { 219 if (id->base.active.sff8431_app_e || 220 id->base.active.sff8431_lim) { 221 phylink_set(modes, 10000baseCR_Full); 222 } 223 } 224 225 switch (id->base.extended_cc) { 226 case SFF8024_ECC_UNSPEC: 227 break; 228 case SFF8024_ECC_100GBASE_SR4_25GBASE_SR: 229 phylink_set(modes, 100000baseSR4_Full); 230 phylink_set(modes, 25000baseSR_Full); 231 break; 232 case SFF8024_ECC_100GBASE_LR4_25GBASE_LR: 233 case SFF8024_ECC_100GBASE_ER4_25GBASE_ER: 234 phylink_set(modes, 100000baseLR4_ER4_Full); 235 break; 236 case SFF8024_ECC_100GBASE_CR4: 237 phylink_set(modes, 100000baseCR4_Full); 238 fallthrough; 239 case SFF8024_ECC_25GBASE_CR_S: 240 case SFF8024_ECC_25GBASE_CR_N: 241 phylink_set(modes, 25000baseCR_Full); 242 break; 243 case SFF8024_ECC_10GBASE_T_SFI: 244 case SFF8024_ECC_10GBASE_T_SR: 245 phylink_set(modes, 10000baseT_Full); 246 break; 247 case SFF8024_ECC_5GBASE_T: 248 phylink_set(modes, 5000baseT_Full); 249 break; 250 case SFF8024_ECC_2_5GBASE_T: 251 phylink_set(modes, 2500baseT_Full); 252 break; 253 default: 254 dev_warn(bus->sfp_dev, 255 "Unknown/unsupported extended compliance code: 0x%02x\n", 256 id->base.extended_cc); 257 break; 258 } 259 260 /* For fibre channel SFP, derive possible BaseX modes */ 261 if (id->base.fc_speed_100 || 262 id->base.fc_speed_200 || 263 id->base.fc_speed_400) { 264 if (id->base.br_nominal >= 31) 265 phylink_set(modes, 2500baseX_Full); 266 if (id->base.br_nominal >= 12) 267 phylink_set(modes, 1000baseX_Full); 268 } 269 270 /* If we haven't discovered any modes that this module supports, try 271 * the bitrate to determine supported modes. Some BiDi modules (eg, 272 * 1310nm/1550nm) are not 1000BASE-BX compliant due to the differing 273 * wavelengths, so do not set any transceiver bits. 274 * 275 * Do the same for modules supporting 2500BASE-X. Note that some 276 * modules use 2500Mbaud rather than 3100 or 3200Mbaud for 277 * 2500BASE-X, so we allow some slack here. 278 */ 279 if (bitmap_empty(modes, __ETHTOOL_LINK_MODE_MASK_NBITS) && br_nom) { 280 if (br_min <= 1300 && br_max >= 1200) 281 phylink_set(modes, 1000baseX_Full); 282 if (br_min <= 3200 && br_max >= 2500) 283 phylink_set(modes, 2500baseX_Full); 284 } 285 286 if (bus->sfp_quirk && bus->sfp_quirk->modes) 287 bus->sfp_quirk->modes(id, modes); 288 289 linkmode_or(support, support, modes); 290 291 phylink_set(support, Autoneg); 292 phylink_set(support, Pause); 293 phylink_set(support, Asym_Pause); 294 } 295 EXPORT_SYMBOL_GPL(sfp_parse_support); 296 297 /** 298 * sfp_select_interface() - Select appropriate phy_interface_t mode 299 * @bus: a pointer to the &struct sfp_bus structure for the sfp module 300 * @link_modes: ethtool link modes mask 301 * 302 * Derive the phy_interface_t mode for the SFP module from the link 303 * modes mask. 304 */ 305 phy_interface_t sfp_select_interface(struct sfp_bus *bus, 306 unsigned long *link_modes) 307 { 308 if (phylink_test(link_modes, 25000baseCR_Full) || 309 phylink_test(link_modes, 25000baseKR_Full) || 310 phylink_test(link_modes, 25000baseSR_Full)) 311 return PHY_INTERFACE_MODE_25GBASER; 312 313 if (phylink_test(link_modes, 10000baseCR_Full) || 314 phylink_test(link_modes, 10000baseSR_Full) || 315 phylink_test(link_modes, 10000baseLR_Full) || 316 phylink_test(link_modes, 10000baseLRM_Full) || 317 phylink_test(link_modes, 10000baseER_Full) || 318 phylink_test(link_modes, 10000baseT_Full)) 319 return PHY_INTERFACE_MODE_10GBASER; 320 321 if (phylink_test(link_modes, 5000baseT_Full)) 322 return PHY_INTERFACE_MODE_5GBASER; 323 324 if (phylink_test(link_modes, 2500baseX_Full)) 325 return PHY_INTERFACE_MODE_2500BASEX; 326 327 if (phylink_test(link_modes, 1000baseT_Half) || 328 phylink_test(link_modes, 1000baseT_Full)) 329 return PHY_INTERFACE_MODE_SGMII; 330 331 if (phylink_test(link_modes, 1000baseX_Full)) 332 return PHY_INTERFACE_MODE_1000BASEX; 333 334 if (phylink_test(link_modes, 100baseFX_Full)) 335 return PHY_INTERFACE_MODE_100BASEX; 336 337 dev_warn(bus->sfp_dev, "Unable to ascertain link mode\n"); 338 339 return PHY_INTERFACE_MODE_NA; 340 } 341 EXPORT_SYMBOL_GPL(sfp_select_interface); 342 343 static LIST_HEAD(sfp_buses); 344 static DEFINE_MUTEX(sfp_mutex); 345 346 static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus) 347 { 348 return bus->registered ? bus->upstream_ops : NULL; 349 } 350 351 static struct sfp_bus *sfp_bus_get(struct fwnode_handle *fwnode) 352 { 353 struct sfp_bus *sfp, *new, *found = NULL; 354 355 new = kzalloc(sizeof(*new), GFP_KERNEL); 356 357 mutex_lock(&sfp_mutex); 358 359 list_for_each_entry(sfp, &sfp_buses, node) { 360 if (sfp->fwnode == fwnode) { 361 kref_get(&sfp->kref); 362 found = sfp; 363 break; 364 } 365 } 366 367 if (!found && new) { 368 kref_init(&new->kref); 369 new->fwnode = fwnode; 370 list_add(&new->node, &sfp_buses); 371 found = new; 372 new = NULL; 373 } 374 375 mutex_unlock(&sfp_mutex); 376 377 kfree(new); 378 379 return found; 380 } 381 382 static void sfp_bus_release(struct kref *kref) 383 { 384 struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref); 385 386 list_del(&bus->node); 387 mutex_unlock(&sfp_mutex); 388 kfree(bus); 389 } 390 391 /** 392 * sfp_bus_put() - put a reference on the &struct sfp_bus 393 * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode() 394 * 395 * Put a reference on the &struct sfp_bus and free the underlying structure 396 * if this was the last reference. 397 */ 398 void sfp_bus_put(struct sfp_bus *bus) 399 { 400 if (bus) 401 kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex); 402 } 403 EXPORT_SYMBOL_GPL(sfp_bus_put); 404 405 static int sfp_register_bus(struct sfp_bus *bus) 406 { 407 const struct sfp_upstream_ops *ops = bus->upstream_ops; 408 int ret; 409 410 if (ops) { 411 if (ops->link_down) 412 ops->link_down(bus->upstream); 413 if (ops->connect_phy && bus->phydev) { 414 ret = ops->connect_phy(bus->upstream, bus->phydev); 415 if (ret) 416 return ret; 417 } 418 } 419 bus->registered = true; 420 bus->socket_ops->attach(bus->sfp); 421 if (bus->started) 422 bus->socket_ops->start(bus->sfp); 423 bus->upstream_ops->attach(bus->upstream, bus); 424 return 0; 425 } 426 427 static void sfp_unregister_bus(struct sfp_bus *bus) 428 { 429 const struct sfp_upstream_ops *ops = bus->upstream_ops; 430 431 if (bus->registered) { 432 bus->upstream_ops->detach(bus->upstream, bus); 433 if (bus->started) 434 bus->socket_ops->stop(bus->sfp); 435 bus->socket_ops->detach(bus->sfp); 436 if (bus->phydev && ops && ops->disconnect_phy) 437 ops->disconnect_phy(bus->upstream); 438 } 439 bus->registered = false; 440 } 441 442 /** 443 * sfp_get_module_info() - Get the ethtool_modinfo for a SFP module 444 * @bus: a pointer to the &struct sfp_bus structure for the sfp module 445 * @modinfo: a &struct ethtool_modinfo 446 * 447 * Fill in the type and eeprom_len parameters in @modinfo for a module on 448 * the sfp bus specified by @bus. 449 * 450 * Returns 0 on success or a negative errno number. 451 */ 452 int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo) 453 { 454 return bus->socket_ops->module_info(bus->sfp, modinfo); 455 } 456 EXPORT_SYMBOL_GPL(sfp_get_module_info); 457 458 /** 459 * sfp_get_module_eeprom() - Read the SFP module EEPROM 460 * @bus: a pointer to the &struct sfp_bus structure for the sfp module 461 * @ee: a &struct ethtool_eeprom 462 * @data: buffer to contain the EEPROM data (must be at least @ee->len bytes) 463 * 464 * Read the EEPROM as specified by the supplied @ee. See the documentation 465 * for &struct ethtool_eeprom for the region to be read. 466 * 467 * Returns 0 on success or a negative errno number. 468 */ 469 int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee, 470 u8 *data) 471 { 472 return bus->socket_ops->module_eeprom(bus->sfp, ee, data); 473 } 474 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom); 475 476 /** 477 * sfp_get_module_eeprom_by_page() - Read a page from the SFP module EEPROM 478 * @bus: a pointer to the &struct sfp_bus structure for the sfp module 479 * @page: a &struct ethtool_module_eeprom 480 * @extack: extack for reporting problems 481 * 482 * Read an EEPROM page as specified by the supplied @page. See the 483 * documentation for &struct ethtool_module_eeprom for the page to be read. 484 * 485 * Returns 0 on success or a negative errno number. More error 486 * information might be provided via extack 487 */ 488 int sfp_get_module_eeprom_by_page(struct sfp_bus *bus, 489 const struct ethtool_module_eeprom *page, 490 struct netlink_ext_ack *extack) 491 { 492 return bus->socket_ops->module_eeprom_by_page(bus->sfp, page, extack); 493 } 494 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom_by_page); 495 496 /** 497 * sfp_upstream_start() - Inform the SFP that the network device is up 498 * @bus: a pointer to the &struct sfp_bus structure for the sfp module 499 * 500 * Inform the SFP socket that the network device is now up, so that the 501 * module can be enabled by allowing TX_DISABLE to be deasserted. This 502 * should be called from the network device driver's &struct net_device_ops 503 * ndo_open() method. 504 */ 505 void sfp_upstream_start(struct sfp_bus *bus) 506 { 507 if (bus->registered) 508 bus->socket_ops->start(bus->sfp); 509 bus->started = true; 510 } 511 EXPORT_SYMBOL_GPL(sfp_upstream_start); 512 513 /** 514 * sfp_upstream_stop() - Inform the SFP that the network device is down 515 * @bus: a pointer to the &struct sfp_bus structure for the sfp module 516 * 517 * Inform the SFP socket that the network device is now up, so that the 518 * module can be disabled by asserting TX_DISABLE, disabling the laser 519 * in optical modules. This should be called from the network device 520 * driver's &struct net_device_ops ndo_stop() method. 521 */ 522 void sfp_upstream_stop(struct sfp_bus *bus) 523 { 524 if (bus->registered) 525 bus->socket_ops->stop(bus->sfp); 526 bus->started = false; 527 } 528 EXPORT_SYMBOL_GPL(sfp_upstream_stop); 529 530 static void sfp_upstream_clear(struct sfp_bus *bus) 531 { 532 bus->upstream_ops = NULL; 533 bus->upstream = NULL; 534 } 535 536 /** 537 * sfp_bus_find_fwnode() - parse and locate the SFP bus from fwnode 538 * @fwnode: firmware node for the parent device (MAC or PHY) 539 * 540 * Parse the parent device's firmware node for a SFP bus, and locate 541 * the sfp_bus structure, incrementing its reference count. This must 542 * be put via sfp_bus_put() when done. 543 * 544 * Returns: 545 * - on success, a pointer to the sfp_bus structure, 546 * - %NULL if no SFP is specified, 547 * - on failure, an error pointer value: 548 * 549 * - corresponding to the errors detailed for 550 * fwnode_property_get_reference_args(). 551 * - %-ENOMEM if we failed to allocate the bus. 552 * - an error from the upstream's connect_phy() method. 553 */ 554 struct sfp_bus *sfp_bus_find_fwnode(struct fwnode_handle *fwnode) 555 { 556 struct fwnode_reference_args ref; 557 struct sfp_bus *bus; 558 int ret; 559 560 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL, 561 0, 0, &ref); 562 if (ret == -ENOENT) 563 return NULL; 564 else if (ret < 0) 565 return ERR_PTR(ret); 566 567 if (!fwnode_device_is_available(ref.fwnode)) { 568 fwnode_handle_put(ref.fwnode); 569 return NULL; 570 } 571 572 bus = sfp_bus_get(ref.fwnode); 573 fwnode_handle_put(ref.fwnode); 574 if (!bus) 575 return ERR_PTR(-ENOMEM); 576 577 return bus; 578 } 579 EXPORT_SYMBOL_GPL(sfp_bus_find_fwnode); 580 581 /** 582 * sfp_bus_add_upstream() - parse and register the neighbouring device 583 * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode() 584 * @upstream: the upstream private data 585 * @ops: the upstream's &struct sfp_upstream_ops 586 * 587 * Add upstream driver for the SFP bus, and if the bus is complete, register 588 * the SFP bus using sfp_register_upstream(). This takes a reference on the 589 * bus, so it is safe to put the bus after this call. 590 * 591 * Returns: 592 * - on success, a pointer to the sfp_bus structure, 593 * - %NULL if no SFP is specified, 594 * - on failure, an error pointer value: 595 * 596 * - corresponding to the errors detailed for 597 * fwnode_property_get_reference_args(). 598 * - %-ENOMEM if we failed to allocate the bus. 599 * - an error from the upstream's connect_phy() method. 600 */ 601 int sfp_bus_add_upstream(struct sfp_bus *bus, void *upstream, 602 const struct sfp_upstream_ops *ops) 603 { 604 int ret; 605 606 /* If no bus, return success */ 607 if (!bus) 608 return 0; 609 610 rtnl_lock(); 611 kref_get(&bus->kref); 612 bus->upstream_ops = ops; 613 bus->upstream = upstream; 614 615 if (bus->sfp) { 616 ret = sfp_register_bus(bus); 617 if (ret) 618 sfp_upstream_clear(bus); 619 } else { 620 ret = 0; 621 } 622 rtnl_unlock(); 623 624 if (ret) 625 sfp_bus_put(bus); 626 627 return ret; 628 } 629 EXPORT_SYMBOL_GPL(sfp_bus_add_upstream); 630 631 /** 632 * sfp_bus_del_upstream() - Delete a sfp bus 633 * @bus: a pointer to the &struct sfp_bus structure for the sfp module 634 * 635 * Delete a previously registered upstream connection for the SFP 636 * module. @bus should have been added by sfp_bus_add_upstream(). 637 */ 638 void sfp_bus_del_upstream(struct sfp_bus *bus) 639 { 640 if (bus) { 641 rtnl_lock(); 642 if (bus->sfp) 643 sfp_unregister_bus(bus); 644 sfp_upstream_clear(bus); 645 rtnl_unlock(); 646 647 sfp_bus_put(bus); 648 } 649 } 650 EXPORT_SYMBOL_GPL(sfp_bus_del_upstream); 651 652 /* Socket driver entry points */ 653 int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev) 654 { 655 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 656 int ret = 0; 657 658 if (ops && ops->connect_phy) 659 ret = ops->connect_phy(bus->upstream, phydev); 660 661 if (ret == 0) 662 bus->phydev = phydev; 663 664 return ret; 665 } 666 EXPORT_SYMBOL_GPL(sfp_add_phy); 667 668 void sfp_remove_phy(struct sfp_bus *bus) 669 { 670 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 671 672 if (ops && ops->disconnect_phy) 673 ops->disconnect_phy(bus->upstream); 674 bus->phydev = NULL; 675 } 676 EXPORT_SYMBOL_GPL(sfp_remove_phy); 677 678 void sfp_link_up(struct sfp_bus *bus) 679 { 680 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 681 682 if (ops && ops->link_up) 683 ops->link_up(bus->upstream); 684 } 685 EXPORT_SYMBOL_GPL(sfp_link_up); 686 687 void sfp_link_down(struct sfp_bus *bus) 688 { 689 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 690 691 if (ops && ops->link_down) 692 ops->link_down(bus->upstream); 693 } 694 EXPORT_SYMBOL_GPL(sfp_link_down); 695 696 int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id, 697 const struct sfp_quirk *quirk) 698 { 699 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 700 int ret = 0; 701 702 bus->sfp_quirk = quirk; 703 704 if (ops && ops->module_insert) 705 ret = ops->module_insert(bus->upstream, id); 706 707 return ret; 708 } 709 EXPORT_SYMBOL_GPL(sfp_module_insert); 710 711 void sfp_module_remove(struct sfp_bus *bus) 712 { 713 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 714 715 if (ops && ops->module_remove) 716 ops->module_remove(bus->upstream); 717 718 bus->sfp_quirk = NULL; 719 } 720 EXPORT_SYMBOL_GPL(sfp_module_remove); 721 722 int sfp_module_start(struct sfp_bus *bus) 723 { 724 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 725 int ret = 0; 726 727 if (ops && ops->module_start) 728 ret = ops->module_start(bus->upstream); 729 730 return ret; 731 } 732 EXPORT_SYMBOL_GPL(sfp_module_start); 733 734 void sfp_module_stop(struct sfp_bus *bus) 735 { 736 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 737 738 if (ops && ops->module_stop) 739 ops->module_stop(bus->upstream); 740 } 741 EXPORT_SYMBOL_GPL(sfp_module_stop); 742 743 static void sfp_socket_clear(struct sfp_bus *bus) 744 { 745 bus->sfp_dev = NULL; 746 bus->sfp = NULL; 747 bus->socket_ops = NULL; 748 } 749 750 struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp, 751 const struct sfp_socket_ops *ops) 752 { 753 struct sfp_bus *bus = sfp_bus_get(dev->fwnode); 754 int ret = 0; 755 756 if (bus) { 757 rtnl_lock(); 758 bus->sfp_dev = dev; 759 bus->sfp = sfp; 760 bus->socket_ops = ops; 761 762 if (bus->upstream_ops) { 763 ret = sfp_register_bus(bus); 764 if (ret) 765 sfp_socket_clear(bus); 766 } 767 rtnl_unlock(); 768 } 769 770 if (ret) { 771 sfp_bus_put(bus); 772 bus = NULL; 773 } 774 775 return bus; 776 } 777 EXPORT_SYMBOL_GPL(sfp_register_socket); 778 779 void sfp_unregister_socket(struct sfp_bus *bus) 780 { 781 rtnl_lock(); 782 if (bus->upstream_ops) 783 sfp_unregister_bus(bus); 784 sfp_socket_clear(bus); 785 rtnl_unlock(); 786 787 sfp_bus_put(bus); 788 } 789 EXPORT_SYMBOL_GPL(sfp_unregister_socket); 790