| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | #include <linux/export.h> |
|---|
| 2 | 3 | #include <linux/kref.h> |
|---|
| 3 | 4 | #include <linux/list.h> |
|---|
| 4 | 5 | #include <linux/mutex.h> |
|---|
| 5 | 6 | #include <linux/phylink.h> |
|---|
| 7 | +#include <linux/property.h> |
|---|
| 6 | 8 | #include <linux/rtnetlink.h> |
|---|
| 7 | 9 | #include <linux/slab.h> |
|---|
| 8 | 10 | |
|---|
| .. | .. |
|---|
| 30 | 32 | |
|---|
| 31 | 33 | const struct sfp_upstream_ops *upstream_ops; |
|---|
| 32 | 34 | void *upstream; |
|---|
| 33 | | - struct net_device *netdev; |
|---|
| 34 | 35 | struct phy_device *phydev; |
|---|
| 35 | 36 | |
|---|
| 36 | 37 | bool registered; |
|---|
| .. | .. |
|---|
| 41 | 42 | unsigned long *modes) |
|---|
| 42 | 43 | { |
|---|
| 43 | 44 | phylink_set(modes, 2500baseX_Full); |
|---|
| 45 | +} |
|---|
| 46 | + |
|---|
| 47 | +static void sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id *id, |
|---|
| 48 | + unsigned long *modes) |
|---|
| 49 | +{ |
|---|
| 50 | + /* Ubiquiti U-Fiber Instant module claims that support all transceiver |
|---|
| 51 | + * types including 10G Ethernet which is not truth. So clear all claimed |
|---|
| 52 | + * modes and set only one mode which module supports: 1000baseX_Full. |
|---|
| 53 | + */ |
|---|
| 54 | + phylink_zero(modes); |
|---|
| 55 | + phylink_set(modes, 1000baseX_Full); |
|---|
| 44 | 56 | } |
|---|
| 45 | 57 | |
|---|
| 46 | 58 | static const struct sfp_quirk sfp_quirks[] = { |
|---|
| .. | .. |
|---|
| 62 | 74 | .vendor = "HUAWEI", |
|---|
| 63 | 75 | .part = "MA5671A", |
|---|
| 64 | 76 | .modes = sfp_quirk_2500basex, |
|---|
| 77 | + }, { |
|---|
| 78 | + // Lantech 8330-262D-E can operate at 2500base-X, but |
|---|
| 79 | + // incorrectly report 2500MBd NRZ in their EEPROM |
|---|
| 80 | + .vendor = "Lantech", |
|---|
| 81 | + .part = "8330-262D-E", |
|---|
| 82 | + .modes = sfp_quirk_2500basex, |
|---|
| 83 | + }, { |
|---|
| 84 | + .vendor = "UBNT", |
|---|
| 85 | + .part = "UF-INSTANT", |
|---|
| 86 | + .modes = sfp_quirk_ubnt_uf_instant, |
|---|
| 65 | 87 | }, |
|---|
| 66 | 88 | }; |
|---|
| 67 | 89 | |
|---|
| .. | .. |
|---|
| 102 | 124 | |
|---|
| 103 | 125 | return NULL; |
|---|
| 104 | 126 | } |
|---|
| 127 | + |
|---|
| 105 | 128 | /** |
|---|
| 106 | 129 | * sfp_parse_port() - Parse the EEPROM base ID, setting the port type |
|---|
| 107 | 130 | * @bus: a pointer to the &struct sfp_bus structure for the sfp module |
|---|
| .. | .. |
|---|
| 123 | 146 | |
|---|
| 124 | 147 | /* port is the physical connector, set this from the connector field. */ |
|---|
| 125 | 148 | 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: |
|---|
| 149 | + case SFF8024_CONNECTOR_SC: |
|---|
| 150 | + case SFF8024_CONNECTOR_FIBERJACK: |
|---|
| 151 | + case SFF8024_CONNECTOR_LC: |
|---|
| 152 | + case SFF8024_CONNECTOR_MT_RJ: |
|---|
| 153 | + case SFF8024_CONNECTOR_MU: |
|---|
| 154 | + case SFF8024_CONNECTOR_OPTICAL_PIGTAIL: |
|---|
| 155 | + case SFF8024_CONNECTOR_MPO_1X12: |
|---|
| 156 | + case SFF8024_CONNECTOR_MPO_2X16: |
|---|
| 132 | 157 | port = PORT_FIBRE; |
|---|
| 133 | 158 | break; |
|---|
| 134 | 159 | |
|---|
| 135 | | - case SFP_CONNECTOR_RJ45: |
|---|
| 160 | + case SFF8024_CONNECTOR_RJ45: |
|---|
| 136 | 161 | port = PORT_TP; |
|---|
| 137 | 162 | break; |
|---|
| 138 | 163 | |
|---|
| 139 | | - case SFP_CONNECTOR_COPPER_PIGTAIL: |
|---|
| 164 | + case SFF8024_CONNECTOR_COPPER_PIGTAIL: |
|---|
| 140 | 165 | port = PORT_DA; |
|---|
| 141 | 166 | break; |
|---|
| 142 | 167 | |
|---|
| 143 | | - case SFP_CONNECTOR_UNSPEC: |
|---|
| 168 | + case SFF8024_CONNECTOR_UNSPEC: |
|---|
| 144 | 169 | if (id->base.e1000_base_t) { |
|---|
| 145 | 170 | port = PORT_TP; |
|---|
| 146 | 171 | break; |
|---|
| 147 | 172 | } |
|---|
| 148 | | - /* fallthrough */ |
|---|
| 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: |
|---|
| 173 | + fallthrough; |
|---|
| 174 | + case SFF8024_CONNECTOR_SG: /* guess */ |
|---|
| 175 | + case SFF8024_CONNECTOR_HSSDC_II: |
|---|
| 176 | + case SFF8024_CONNECTOR_NOSEPARATE: |
|---|
| 177 | + case SFF8024_CONNECTOR_MXC_2X16: |
|---|
| 155 | 178 | port = PORT_OTHER; |
|---|
| 156 | 179 | break; |
|---|
| 157 | 180 | default: |
|---|
| .. | .. |
|---|
| 176 | 199 | return port; |
|---|
| 177 | 200 | } |
|---|
| 178 | 201 | EXPORT_SYMBOL_GPL(sfp_parse_port); |
|---|
| 202 | + |
|---|
| 203 | +/** |
|---|
| 204 | + * sfp_may_have_phy() - indicate whether the module may have a PHY |
|---|
| 205 | + * @bus: a pointer to the &struct sfp_bus structure for the sfp module |
|---|
| 206 | + * @id: a pointer to the module's &struct sfp_eeprom_id |
|---|
| 207 | + * |
|---|
| 208 | + * Parse the EEPROM identification given in @id, and return whether |
|---|
| 209 | + * this module may have a PHY. |
|---|
| 210 | + */ |
|---|
| 211 | +bool sfp_may_have_phy(struct sfp_bus *bus, const struct sfp_eeprom_id *id) |
|---|
| 212 | +{ |
|---|
| 213 | + if (id->base.e1000_base_t) |
|---|
| 214 | + return true; |
|---|
| 215 | + |
|---|
| 216 | + if (id->base.phys_id != SFF8024_ID_DWDM_SFP) { |
|---|
| 217 | + switch (id->base.extended_cc) { |
|---|
| 218 | + case SFF8024_ECC_10GBASE_T_SFI: |
|---|
| 219 | + case SFF8024_ECC_10GBASE_T_SR: |
|---|
| 220 | + case SFF8024_ECC_5GBASE_T: |
|---|
| 221 | + case SFF8024_ECC_2_5GBASE_T: |
|---|
| 222 | + return true; |
|---|
| 223 | + } |
|---|
| 224 | + } |
|---|
| 225 | + |
|---|
| 226 | + return false; |
|---|
| 227 | +} |
|---|
| 228 | +EXPORT_SYMBOL_GPL(sfp_may_have_phy); |
|---|
| 179 | 229 | |
|---|
| 180 | 230 | /** |
|---|
| 181 | 231 | * sfp_parse_support() - Parse the eeprom id for supported link modes |
|---|
| .. | .. |
|---|
| 260 | 310 | } |
|---|
| 261 | 311 | |
|---|
| 262 | 312 | switch (id->base.extended_cc) { |
|---|
| 263 | | - case 0x00: /* Unspecified */ |
|---|
| 313 | + case SFF8024_ECC_UNSPEC: |
|---|
| 264 | 314 | break; |
|---|
| 265 | | - case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */ |
|---|
| 315 | + case SFF8024_ECC_100GBASE_SR4_25GBASE_SR: |
|---|
| 266 | 316 | phylink_set(modes, 100000baseSR4_Full); |
|---|
| 267 | 317 | phylink_set(modes, 25000baseSR_Full); |
|---|
| 268 | 318 | break; |
|---|
| 269 | | - case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */ |
|---|
| 270 | | - case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */ |
|---|
| 319 | + case SFF8024_ECC_100GBASE_LR4_25GBASE_LR: |
|---|
| 320 | + case SFF8024_ECC_100GBASE_ER4_25GBASE_ER: |
|---|
| 271 | 321 | phylink_set(modes, 100000baseLR4_ER4_Full); |
|---|
| 272 | 322 | break; |
|---|
| 273 | | - case 0x0b: /* 100Gbase-CR4 or 25Gbase-CR CA-L */ |
|---|
| 274 | | - case 0x0c: /* 25Gbase-CR CA-S */ |
|---|
| 275 | | - case 0x0d: /* 25Gbase-CR CA-N */ |
|---|
| 323 | + case SFF8024_ECC_100GBASE_CR4: |
|---|
| 276 | 324 | phylink_set(modes, 100000baseCR4_Full); |
|---|
| 325 | + fallthrough; |
|---|
| 326 | + case SFF8024_ECC_25GBASE_CR_S: |
|---|
| 327 | + case SFF8024_ECC_25GBASE_CR_N: |
|---|
| 277 | 328 | phylink_set(modes, 25000baseCR_Full); |
|---|
| 329 | + break; |
|---|
| 330 | + case SFF8024_ECC_10GBASE_T_SFI: |
|---|
| 331 | + case SFF8024_ECC_10GBASE_T_SR: |
|---|
| 332 | + phylink_set(modes, 10000baseT_Full); |
|---|
| 333 | + break; |
|---|
| 334 | + case SFF8024_ECC_5GBASE_T: |
|---|
| 335 | + phylink_set(modes, 5000baseT_Full); |
|---|
| 336 | + break; |
|---|
| 337 | + case SFF8024_ECC_2_5GBASE_T: |
|---|
| 338 | + phylink_set(modes, 2500baseT_Full); |
|---|
| 278 | 339 | break; |
|---|
| 279 | 340 | default: |
|---|
| 280 | 341 | dev_warn(bus->sfp_dev, |
|---|
| .. | .. |
|---|
| 294 | 355 | } |
|---|
| 295 | 356 | |
|---|
| 296 | 357 | /* 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. |
|---|
| 358 | + * the bitrate to determine supported modes. Some BiDi modules (eg, |
|---|
| 359 | + * 1310nm/1550nm) are not 1000BASE-BX compliant due to the differing |
|---|
| 360 | + * wavelengths, so do not set any transceiver bits. |
|---|
| 300 | 361 | */ |
|---|
| 301 | 362 | 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) |
|---|
| 363 | + /* If the bit rate allows 1000baseX */ |
|---|
| 364 | + if (br_nom && br_min <= 1300 && br_max >= 1200) |
|---|
| 305 | 365 | phylink_set(modes, 1000baseX_Full); |
|---|
| 306 | 366 | } |
|---|
| 307 | 367 | |
|---|
| .. | .. |
|---|
| 319 | 379 | /** |
|---|
| 320 | 380 | * sfp_select_interface() - Select appropriate phy_interface_t mode |
|---|
| 321 | 381 | * @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 | 382 | * @link_modes: ethtool link modes mask |
|---|
| 324 | 383 | * |
|---|
| 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. |
|---|
| 384 | + * Derive the phy_interface_t mode for the SFP module from the link |
|---|
| 385 | + * modes mask. |
|---|
| 329 | 386 | */ |
|---|
| 330 | 387 | phy_interface_t sfp_select_interface(struct sfp_bus *bus, |
|---|
| 331 | | - const struct sfp_eeprom_id *id, |
|---|
| 332 | 388 | unsigned long *link_modes) |
|---|
| 333 | 389 | { |
|---|
| 334 | 390 | if (phylink_test(link_modes, 10000baseCR_Full) || |
|---|
| 335 | 391 | phylink_test(link_modes, 10000baseSR_Full) || |
|---|
| 336 | 392 | phylink_test(link_modes, 10000baseLR_Full) || |
|---|
| 337 | 393 | phylink_test(link_modes, 10000baseLRM_Full) || |
|---|
| 338 | | - phylink_test(link_modes, 10000baseER_Full)) |
|---|
| 339 | | - return PHY_INTERFACE_MODE_10GKR; |
|---|
| 394 | + phylink_test(link_modes, 10000baseER_Full) || |
|---|
| 395 | + phylink_test(link_modes, 10000baseT_Full)) |
|---|
| 396 | + return PHY_INTERFACE_MODE_10GBASER; |
|---|
| 340 | 397 | |
|---|
| 341 | 398 | if (phylink_test(link_modes, 2500baseX_Full)) |
|---|
| 342 | 399 | return PHY_INTERFACE_MODE_2500BASEX; |
|---|
| 343 | 400 | |
|---|
| 344 | | - if (id->base.e1000_base_t || |
|---|
| 345 | | - id->base.e100_base_lx || |
|---|
| 346 | | - id->base.e100_base_fx) |
|---|
| 401 | + if (phylink_test(link_modes, 1000baseT_Half) || |
|---|
| 402 | + phylink_test(link_modes, 1000baseT_Full)) |
|---|
| 347 | 403 | return PHY_INTERFACE_MODE_SGMII; |
|---|
| 348 | 404 | |
|---|
| 349 | 405 | if (phylink_test(link_modes, 1000baseX_Full)) |
|---|
| .. | .. |
|---|
| 403 | 459 | kfree(bus); |
|---|
| 404 | 460 | } |
|---|
| 405 | 461 | |
|---|
| 406 | | -static void sfp_bus_put(struct sfp_bus *bus) |
|---|
| 462 | +/** |
|---|
| 463 | + * sfp_bus_put() - put a reference on the &struct sfp_bus |
|---|
| 464 | + * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode() |
|---|
| 465 | + * |
|---|
| 466 | + * Put a reference on the &struct sfp_bus and free the underlying structure |
|---|
| 467 | + * if this was the last reference. |
|---|
| 468 | + */ |
|---|
| 469 | +void sfp_bus_put(struct sfp_bus *bus) |
|---|
| 407 | 470 | { |
|---|
| 408 | | - kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex); |
|---|
| 471 | + if (bus) |
|---|
| 472 | + kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex); |
|---|
| 409 | 473 | } |
|---|
| 474 | +EXPORT_SYMBOL_GPL(sfp_bus_put); |
|---|
| 410 | 475 | |
|---|
| 411 | 476 | static int sfp_register_bus(struct sfp_bus *bus) |
|---|
| 412 | 477 | { |
|---|
| .. | .. |
|---|
| 422 | 487 | return ret; |
|---|
| 423 | 488 | } |
|---|
| 424 | 489 | } |
|---|
| 490 | + bus->registered = true; |
|---|
| 425 | 491 | bus->socket_ops->attach(bus->sfp); |
|---|
| 426 | 492 | if (bus->started) |
|---|
| 427 | 493 | bus->socket_ops->start(bus->sfp); |
|---|
| 428 | | - bus->netdev->sfp_bus = bus; |
|---|
| 429 | | - bus->registered = true; |
|---|
| 494 | + bus->upstream_ops->attach(bus->upstream, bus); |
|---|
| 430 | 495 | return 0; |
|---|
| 431 | 496 | } |
|---|
| 432 | 497 | |
|---|
| .. | .. |
|---|
| 434 | 499 | { |
|---|
| 435 | 500 | const struct sfp_upstream_ops *ops = bus->upstream_ops; |
|---|
| 436 | 501 | |
|---|
| 437 | | - bus->netdev->sfp_bus = NULL; |
|---|
| 438 | 502 | if (bus->registered) { |
|---|
| 503 | + bus->upstream_ops->detach(bus->upstream, bus); |
|---|
| 439 | 504 | if (bus->started) |
|---|
| 440 | 505 | bus->socket_ops->stop(bus->sfp); |
|---|
| 441 | 506 | bus->socket_ops->detach(bus->sfp); |
|---|
| .. | .. |
|---|
| 517 | 582 | { |
|---|
| 518 | 583 | bus->upstream_ops = NULL; |
|---|
| 519 | 584 | bus->upstream = NULL; |
|---|
| 520 | | - bus->netdev = NULL; |
|---|
| 521 | 585 | } |
|---|
| 522 | 586 | |
|---|
| 523 | 587 | /** |
|---|
| 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 |
|---|
| 588 | + * sfp_bus_find_fwnode() - parse and locate the SFP bus from fwnode |
|---|
| 589 | + * @fwnode: firmware node for the parent device (MAC or PHY) |
|---|
| 529 | 590 | * |
|---|
| 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. |
|---|
| 591 | + * Parse the parent device's firmware node for a SFP bus, and locate |
|---|
| 592 | + * the sfp_bus structure, incrementing its reference count. This must |
|---|
| 593 | + * be put via sfp_bus_put() when done. |
|---|
| 533 | 594 | * |
|---|
| 534 | | - * On error, returns %NULL. |
|---|
| 595 | + * Returns: |
|---|
| 596 | + * - on success, a pointer to the sfp_bus structure, |
|---|
| 597 | + * - %NULL if no SFP is specified, |
|---|
| 598 | + * - on failure, an error pointer value: |
|---|
| 599 | + * |
|---|
| 600 | + * - corresponding to the errors detailed for |
|---|
| 601 | + * fwnode_property_get_reference_args(). |
|---|
| 602 | + * - %-ENOMEM if we failed to allocate the bus. |
|---|
| 603 | + * - an error from the upstream's connect_phy() method. |
|---|
| 535 | 604 | */ |
|---|
| 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) |
|---|
| 605 | +struct sfp_bus *sfp_bus_find_fwnode(struct fwnode_handle *fwnode) |
|---|
| 539 | 606 | { |
|---|
| 540 | | - struct sfp_bus *bus = sfp_bus_get(fwnode); |
|---|
| 541 | | - int ret = 0; |
|---|
| 607 | + struct fwnode_reference_args ref; |
|---|
| 608 | + struct sfp_bus *bus; |
|---|
| 609 | + int ret; |
|---|
| 542 | 610 | |
|---|
| 543 | | - if (bus) { |
|---|
| 544 | | - rtnl_lock(); |
|---|
| 545 | | - bus->upstream_ops = ops; |
|---|
| 546 | | - bus->upstream = upstream; |
|---|
| 547 | | - bus->netdev = ndev; |
|---|
| 611 | + ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL, |
|---|
| 612 | + 0, 0, &ref); |
|---|
| 613 | + if (ret == -ENOENT) |
|---|
| 614 | + return NULL; |
|---|
| 615 | + else if (ret < 0) |
|---|
| 616 | + return ERR_PTR(ret); |
|---|
| 548 | 617 | |
|---|
| 549 | | - if (bus->sfp) { |
|---|
| 550 | | - ret = sfp_register_bus(bus); |
|---|
| 551 | | - if (ret) |
|---|
| 552 | | - sfp_upstream_clear(bus); |
|---|
| 553 | | - } |
|---|
| 554 | | - rtnl_unlock(); |
|---|
| 618 | + if (!fwnode_device_is_available(ref.fwnode)) { |
|---|
| 619 | + fwnode_handle_put(ref.fwnode); |
|---|
| 620 | + return NULL; |
|---|
| 555 | 621 | } |
|---|
| 556 | 622 | |
|---|
| 557 | | - if (ret) { |
|---|
| 558 | | - sfp_bus_put(bus); |
|---|
| 559 | | - bus = NULL; |
|---|
| 560 | | - } |
|---|
| 623 | + bus = sfp_bus_get(ref.fwnode); |
|---|
| 624 | + fwnode_handle_put(ref.fwnode); |
|---|
| 625 | + if (!bus) |
|---|
| 626 | + return ERR_PTR(-ENOMEM); |
|---|
| 561 | 627 | |
|---|
| 562 | 628 | return bus; |
|---|
| 563 | 629 | } |
|---|
| 564 | | -EXPORT_SYMBOL_GPL(sfp_register_upstream); |
|---|
| 630 | +EXPORT_SYMBOL_GPL(sfp_bus_find_fwnode); |
|---|
| 565 | 631 | |
|---|
| 566 | 632 | /** |
|---|
| 567 | | - * sfp_unregister_upstream() - Unregister sfp bus |
|---|
| 568 | | - * @bus: a pointer to the &struct sfp_bus structure for the sfp module |
|---|
| 633 | + * sfp_bus_add_upstream() - parse and register the neighbouring device |
|---|
| 634 | + * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode() |
|---|
| 635 | + * @upstream: the upstream private data |
|---|
| 636 | + * @ops: the upstream's &struct sfp_upstream_ops |
|---|
| 569 | 637 | * |
|---|
| 570 | | - * Unregister a previously registered upstream connection for the SFP |
|---|
| 571 | | - * module. @bus is returned from sfp_register_upstream(). |
|---|
| 638 | + * Add upstream driver for the SFP bus, and if the bus is complete, register |
|---|
| 639 | + * the SFP bus using sfp_register_upstream(). This takes a reference on the |
|---|
| 640 | + * bus, so it is safe to put the bus after this call. |
|---|
| 641 | + * |
|---|
| 642 | + * Returns: |
|---|
| 643 | + * - on success, a pointer to the sfp_bus structure, |
|---|
| 644 | + * - %NULL if no SFP is specified, |
|---|
| 645 | + * - on failure, an error pointer value: |
|---|
| 646 | + * |
|---|
| 647 | + * - corresponding to the errors detailed for |
|---|
| 648 | + * fwnode_property_get_reference_args(). |
|---|
| 649 | + * - %-ENOMEM if we failed to allocate the bus. |
|---|
| 650 | + * - an error from the upstream's connect_phy() method. |
|---|
| 572 | 651 | */ |
|---|
| 573 | | -void sfp_unregister_upstream(struct sfp_bus *bus) |
|---|
| 652 | +int sfp_bus_add_upstream(struct sfp_bus *bus, void *upstream, |
|---|
| 653 | + const struct sfp_upstream_ops *ops) |
|---|
| 574 | 654 | { |
|---|
| 655 | + int ret; |
|---|
| 656 | + |
|---|
| 657 | + /* If no bus, return success */ |
|---|
| 658 | + if (!bus) |
|---|
| 659 | + return 0; |
|---|
| 660 | + |
|---|
| 575 | 661 | rtnl_lock(); |
|---|
| 576 | | - if (bus->sfp) |
|---|
| 577 | | - sfp_unregister_bus(bus); |
|---|
| 578 | | - sfp_upstream_clear(bus); |
|---|
| 662 | + kref_get(&bus->kref); |
|---|
| 663 | + bus->upstream_ops = ops; |
|---|
| 664 | + bus->upstream = upstream; |
|---|
| 665 | + |
|---|
| 666 | + if (bus->sfp) { |
|---|
| 667 | + ret = sfp_register_bus(bus); |
|---|
| 668 | + if (ret) |
|---|
| 669 | + sfp_upstream_clear(bus); |
|---|
| 670 | + } else { |
|---|
| 671 | + ret = 0; |
|---|
| 672 | + } |
|---|
| 579 | 673 | rtnl_unlock(); |
|---|
| 580 | 674 | |
|---|
| 581 | | - sfp_bus_put(bus); |
|---|
| 675 | + if (ret) |
|---|
| 676 | + sfp_bus_put(bus); |
|---|
| 677 | + |
|---|
| 678 | + return ret; |
|---|
| 582 | 679 | } |
|---|
| 583 | | -EXPORT_SYMBOL_GPL(sfp_unregister_upstream); |
|---|
| 680 | +EXPORT_SYMBOL_GPL(sfp_bus_add_upstream); |
|---|
| 681 | + |
|---|
| 682 | +/** |
|---|
| 683 | + * sfp_bus_del_upstream() - Delete a sfp bus |
|---|
| 684 | + * @bus: a pointer to the &struct sfp_bus structure for the sfp module |
|---|
| 685 | + * |
|---|
| 686 | + * Delete a previously registered upstream connection for the SFP |
|---|
| 687 | + * module. @bus should have been added by sfp_bus_add_upstream(). |
|---|
| 688 | + */ |
|---|
| 689 | +void sfp_bus_del_upstream(struct sfp_bus *bus) |
|---|
| 690 | +{ |
|---|
| 691 | + if (bus) { |
|---|
| 692 | + rtnl_lock(); |
|---|
| 693 | + if (bus->sfp) |
|---|
| 694 | + sfp_unregister_bus(bus); |
|---|
| 695 | + sfp_upstream_clear(bus); |
|---|
| 696 | + rtnl_unlock(); |
|---|
| 697 | + |
|---|
| 698 | + sfp_bus_put(bus); |
|---|
| 699 | + } |
|---|
| 700 | +} |
|---|
| 701 | +EXPORT_SYMBOL_GPL(sfp_bus_del_upstream); |
|---|
| 584 | 702 | |
|---|
| 585 | 703 | /* Socket driver entry points */ |
|---|
| 586 | 704 | int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev) |
|---|
| .. | .. |
|---|
| 651 | 769 | } |
|---|
| 652 | 770 | EXPORT_SYMBOL_GPL(sfp_module_remove); |
|---|
| 653 | 771 | |
|---|
| 772 | +int sfp_module_start(struct sfp_bus *bus) |
|---|
| 773 | +{ |
|---|
| 774 | + const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); |
|---|
| 775 | + int ret = 0; |
|---|
| 776 | + |
|---|
| 777 | + if (ops && ops->module_start) |
|---|
| 778 | + ret = ops->module_start(bus->upstream); |
|---|
| 779 | + |
|---|
| 780 | + return ret; |
|---|
| 781 | +} |
|---|
| 782 | +EXPORT_SYMBOL_GPL(sfp_module_start); |
|---|
| 783 | + |
|---|
| 784 | +void sfp_module_stop(struct sfp_bus *bus) |
|---|
| 785 | +{ |
|---|
| 786 | + const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); |
|---|
| 787 | + |
|---|
| 788 | + if (ops && ops->module_stop) |
|---|
| 789 | + ops->module_stop(bus->upstream); |
|---|
| 790 | +} |
|---|
| 791 | +EXPORT_SYMBOL_GPL(sfp_module_stop); |
|---|
| 792 | + |
|---|
| 654 | 793 | static void sfp_socket_clear(struct sfp_bus *bus) |
|---|
| 655 | 794 | { |
|---|
| 656 | 795 | bus->sfp_dev = NULL; |
|---|
| .. | .. |
|---|
| 670 | 809 | bus->sfp = sfp; |
|---|
| 671 | 810 | bus->socket_ops = ops; |
|---|
| 672 | 811 | |
|---|
| 673 | | - if (bus->netdev) { |
|---|
| 812 | + if (bus->upstream_ops) { |
|---|
| 674 | 813 | ret = sfp_register_bus(bus); |
|---|
| 675 | 814 | if (ret) |
|---|
| 676 | 815 | sfp_socket_clear(bus); |
|---|
| .. | .. |
|---|
| 690 | 829 | void sfp_unregister_socket(struct sfp_bus *bus) |
|---|
| 691 | 830 | { |
|---|
| 692 | 831 | rtnl_lock(); |
|---|
| 693 | | - if (bus->netdev) |
|---|
| 832 | + if (bus->upstream_ops) |
|---|
| 694 | 833 | sfp_unregister_bus(bus); |
|---|
| 695 | 834 | sfp_socket_clear(bus); |
|---|
| 696 | 835 | rtnl_unlock(); |
|---|