Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'Decoupling-PHYLINK-from-struct-net_device'

Ioana Ciornei says:

====================
Decoupling PHYLINK from struct net_device

Following two separate discussion threads in:
https://www.spinics.net/lists/netdev/msg569087.html
and:
https://www.spinics.net/lists/netdev/msg570450.html

Previous RFC patch set: https://www.spinics.net/lists/netdev/msg571995.html

PHYLINK was reworked in order to accept multiple operation types,
PHYLINK_NETDEV and PHYLINK_DEV, passed through a phylink_config
structure alongside the corresponding struct device.

One of the main concerns expressed in the RFC was that using notifiers
to signal the corresponding phylink_mac_ops would break PHYLINK's API
unity and that it would become harder to grep for its users.
Using the current approach, we maintain a common API for all users.
Also, printing useful information in PHYLINK, when decoupled from a
net_device, is achieved using dev_err&co on the struct device received
(in DSA's case is the device corresponding to the dsa_switch).

PHYLIB (which PHYLINK uses) was reworked to the extent that it does not
crash when connecting to a PHY and the net_device pointer is NULL.

Lastly, DSA has been reworked in its way that it handles PHYs for ports
that lack a net_device (CPU and DSA ports). For these, it was
previously using PHYLIB and is now using the PHYLINK_DEV operation type.
Previously, a driver that wanted to support PHY operations on CPU/DSA
ports has to implement .adjust_link(). This patch set not only gives
drivers the options to use PHYLINK uniformly but also urges them to
convert to it. For compatibility, the old code is kept but it will be
removed once all drivers switch over.

The patchset was tested on the NXP LS1021A-TSN board having the
following Ethernet layout:
https://lkml.org/lkml/2019/5/5/279
The CPU port was moved from the internal RGMII fixed-link (enet2 ->
switch port 4) to an external loopback Cat5 cable between the enet1 port
and the front-facing swp2 SJA1105 port. In this mode, both the master
and the CPU port have an attached PHY which detects link change events:

[ 49.105426] fsl-gianfar soc:ethernet@2d50000 eth1: Link is Down
[ 50.305486] sja1105 spi0.1: Link is Down
[ 53.265596] fsl-gianfar soc:ethernet@2d50000 eth1: Link is Up - 1Gbps/Full - flow control off
[ 54.466304] sja1105 spi0.1: Link is Up - 1Gbps/Full - flow control off

Changes in v2:
- fixed sparse warnings
- updated 'Documentation/ABI/testing/sysfs-class-net-phydev'
====================

Signed-off-by: David S. Miller <davem@davemloft.net>

+497 -255
+8
Documentation/ABI/testing/sysfs-class-net-phydev
··· 34 34 xgmii, moca, qsgmii, trgmii, 1000base-x, 2500base-x, rxaui, 35 35 xaui, 10gbase-kr, unknown 36 36 37 + What: /sys/class/mdio_bus/<bus>/<device>/phy_standalone 38 + Date: May 2019 39 + KernelVersion: 5.3 40 + Contact: netdev@vger.kernel.org 41 + Description: 42 + Boolean value indicating whether the PHY device is used in 43 + standalone mode, without a net_device associated, by PHYLINK. 44 + Attribute created only when this is the case.
+4 -1
Documentation/networking/sfp-phylink.rst
··· 98 98 4. Add:: 99 99 100 100 struct phylink *phylink; 101 + struct phylink_config phylink_config; 101 102 102 103 to the driver's private data structure. We shall refer to the 103 104 driver's private data pointer as ``priv`` below, and the driver's ··· 224 223 .. code-block:: c 225 224 226 225 struct phylink *phylink; 226 + priv->phylink_config.dev = &dev.dev; 227 + priv->phylink_config.type = PHYLINK_NETDEV; 227 228 228 - phylink = phylink_create(dev, node, phy_mode, &phylink_ops); 229 + phylink = phylink_create(&priv->phylink_config, node, phy_mode, &phylink_ops); 229 230 if (IS_ERR(phylink)) { 230 231 err = PTR_ERR(phylink); 231 232 fail probe;
+6 -5
drivers/net/dsa/sja1105/sja1105_main.c
··· 734 734 return sja1105_clocking_setup_port(priv, port); 735 735 } 736 736 737 - static void sja1105_adjust_link(struct dsa_switch *ds, int port, 738 - struct phy_device *phydev) 737 + static void sja1105_mac_config(struct dsa_switch *ds, int port, 738 + unsigned int link_an_mode, 739 + const struct phylink_link_state *state) 739 740 { 740 741 struct sja1105_private *priv = ds->priv; 741 742 742 - if (!phydev->link) 743 + if (!state->link) 743 744 sja1105_adjust_port_config(priv, port, 0, false); 744 745 else 745 - sja1105_adjust_port_config(priv, port, phydev->speed, true); 746 + sja1105_adjust_port_config(priv, port, state->speed, true); 746 747 } 747 748 748 749 static void sja1105_phylink_validate(struct dsa_switch *ds, int port, ··· 1516 1515 static const struct dsa_switch_ops sja1105_switch_ops = { 1517 1516 .get_tag_protocol = sja1105_get_tag_protocol, 1518 1517 .setup = sja1105_setup, 1519 - .adjust_link = sja1105_adjust_link, 1520 1518 .set_ageing_time = sja1105_set_ageing_time, 1521 1519 .phylink_validate = sja1105_phylink_validate, 1520 + .phylink_mac_config = sja1105_mac_config, 1522 1521 .get_strings = sja1105_get_strings, 1523 1522 .get_ethtool_stats = sja1105_get_ethtool_stats, 1524 1523 .get_sset_count = sja1105_get_sset_count,
+24 -12
drivers/net/ethernet/marvell/mvneta.c
··· 437 437 struct device_node *dn; 438 438 unsigned int tx_csum_limit; 439 439 struct phylink *phylink; 440 + struct phylink_config phylink_config; 440 441 struct phy *comphy; 441 442 442 443 struct mvneta_bm *bm_priv; ··· 3357 3356 return 0; 3358 3357 } 3359 3358 3360 - static void mvneta_validate(struct net_device *ndev, unsigned long *supported, 3359 + static void mvneta_validate(struct phylink_config *config, 3360 + unsigned long *supported, 3361 3361 struct phylink_link_state *state) 3362 3362 { 3363 + struct net_device *ndev = to_net_dev(config->dev); 3363 3364 struct mvneta_port *pp = netdev_priv(ndev); 3364 3365 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 3365 3366 ··· 3411 3408 phylink_helper_basex_speed(state); 3412 3409 } 3413 3410 3414 - static int mvneta_mac_link_state(struct net_device *ndev, 3411 + static int mvneta_mac_link_state(struct phylink_config *config, 3415 3412 struct phylink_link_state *state) 3416 3413 { 3414 + struct net_device *ndev = to_net_dev(config->dev); 3417 3415 struct mvneta_port *pp = netdev_priv(ndev); 3418 3416 u32 gmac_stat; 3419 3417 ··· 3442 3438 return 1; 3443 3439 } 3444 3440 3445 - static void mvneta_mac_an_restart(struct net_device *ndev) 3441 + static void mvneta_mac_an_restart(struct phylink_config *config) 3446 3442 { 3443 + struct net_device *ndev = to_net_dev(config->dev); 3447 3444 struct mvneta_port *pp = netdev_priv(ndev); 3448 3445 u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG); 3449 3446 ··· 3454 3449 gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN); 3455 3450 } 3456 3451 3457 - static void mvneta_mac_config(struct net_device *ndev, unsigned int mode, 3458 - const struct phylink_link_state *state) 3452 + static void mvneta_mac_config(struct phylink_config *config, unsigned int mode, 3453 + const struct phylink_link_state *state) 3459 3454 { 3455 + struct net_device *ndev = to_net_dev(config->dev); 3460 3456 struct mvneta_port *pp = netdev_priv(ndev); 3461 3457 u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0); 3462 3458 u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2); ··· 3587 3581 mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi_ctl1); 3588 3582 } 3589 3583 3590 - static void mvneta_mac_link_down(struct net_device *ndev, unsigned int mode, 3591 - phy_interface_t interface) 3584 + static void mvneta_mac_link_down(struct phylink_config *config, 3585 + unsigned int mode, phy_interface_t interface) 3592 3586 { 3587 + struct net_device *ndev = to_net_dev(config->dev); 3593 3588 struct mvneta_port *pp = netdev_priv(ndev); 3594 3589 u32 val; 3595 3590 ··· 3607 3600 mvneta_set_eee(pp, false); 3608 3601 } 3609 3602 3610 - static void mvneta_mac_link_up(struct net_device *ndev, unsigned int mode, 3603 + static void mvneta_mac_link_up(struct phylink_config *config, unsigned int mode, 3611 3604 phy_interface_t interface, 3612 3605 struct phy_device *phy) 3613 3606 { 3607 + struct net_device *ndev = to_net_dev(config->dev); 3614 3608 struct mvneta_port *pp = netdev_priv(ndev); 3615 3609 u32 val; 3616 3610 ··· 4508 4500 comphy = NULL; 4509 4501 } 4510 4502 4511 - phylink = phylink_create(dev, pdev->dev.fwnode, phy_mode, 4512 - &mvneta_phylink_ops); 4503 + pp = netdev_priv(dev); 4504 + spin_lock_init(&pp->lock); 4505 + 4506 + pp->phylink_config.dev = &dev->dev; 4507 + pp->phylink_config.type = PHYLINK_NETDEV; 4508 + 4509 + phylink = phylink_create(&pp->phylink_config, pdev->dev.fwnode, 4510 + phy_mode, &mvneta_phylink_ops); 4513 4511 if (IS_ERR(phylink)) { 4514 4512 err = PTR_ERR(phylink); 4515 4513 goto err_free_irq; ··· 4527 4513 4528 4514 dev->ethtool_ops = &mvneta_eth_tool_ops; 4529 4515 4530 - pp = netdev_priv(dev); 4531 - spin_lock_init(&pp->lock); 4532 4516 pp->phylink = phylink; 4533 4517 pp->comphy = comphy; 4534 4518 pp->phy_interface = phy_mode;
+1
drivers/net/ethernet/marvell/mvpp2/mvpp2.h
··· 915 915 916 916 phy_interface_t phy_interface; 917 917 struct phylink *phylink; 918 + struct phylink_config phylink_config; 918 919 struct phy *comphy; 919 920 920 921 struct mvpp2_bm_pool *pool_long;
+26 -17
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c
··· 56 56 /* The prototype is added here to be used in start_dev when using ACPI. This 57 57 * will be removed once phylink is used for all modes (dt+ACPI). 58 58 */ 59 - static void mvpp2_mac_config(struct net_device *dev, unsigned int mode, 59 + static void mvpp2_mac_config(struct phylink_config *config, unsigned int mode, 60 60 const struct phylink_link_state *state); 61 - static void mvpp2_mac_link_up(struct net_device *dev, unsigned int mode, 61 + static void mvpp2_mac_link_up(struct phylink_config *config, unsigned int mode, 62 62 phy_interface_t interface, struct phy_device *phy); 63 63 64 64 /* Queue modes */ ··· 3239 3239 struct phylink_link_state state = { 3240 3240 .interface = port->phy_interface, 3241 3241 }; 3242 - mvpp2_mac_config(port->dev, MLO_AN_INBAND, &state); 3243 - mvpp2_mac_link_up(port->dev, MLO_AN_INBAND, port->phy_interface, 3244 - NULL); 3242 + mvpp2_mac_config(&port->phylink_config, MLO_AN_INBAND, &state); 3243 + mvpp2_mac_link_up(&port->phylink_config, MLO_AN_INBAND, 3244 + port->phy_interface, NULL); 3245 3245 } 3246 3246 3247 3247 netif_tx_start_all_queues(port->dev); ··· 4463 4463 eth_hw_addr_random(dev); 4464 4464 } 4465 4465 4466 - static void mvpp2_phylink_validate(struct net_device *dev, 4466 + static void mvpp2_phylink_validate(struct phylink_config *config, 4467 4467 unsigned long *supported, 4468 4468 struct phylink_link_state *state) 4469 4469 { 4470 - struct mvpp2_port *port = netdev_priv(dev); 4470 + struct mvpp2_port *port = container_of(config, struct mvpp2_port, 4471 + phylink_config); 4471 4472 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 4472 4473 4473 4474 /* Invalid combinations */ ··· 4592 4591 state->pause |= MLO_PAUSE_TX; 4593 4592 } 4594 4593 4595 - static int mvpp2_phylink_mac_link_state(struct net_device *dev, 4594 + static int mvpp2_phylink_mac_link_state(struct phylink_config *config, 4596 4595 struct phylink_link_state *state) 4597 4596 { 4598 - struct mvpp2_port *port = netdev_priv(dev); 4597 + struct mvpp2_port *port = container_of(config, struct mvpp2_port, 4598 + phylink_config); 4599 4599 4600 4600 if (port->priv->hw_version == MVPP22 && port->gop_id == 0) { 4601 4601 u32 mode = readl(port->base + MVPP22_XLG_CTRL3_REG); ··· 4612 4610 return 1; 4613 4611 } 4614 4612 4615 - static void mvpp2_mac_an_restart(struct net_device *dev) 4613 + static void mvpp2_mac_an_restart(struct phylink_config *config) 4616 4614 { 4617 - struct mvpp2_port *port = netdev_priv(dev); 4615 + struct mvpp2_port *port = container_of(config, struct mvpp2_port, 4616 + phylink_config); 4618 4617 u32 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG); 4619 4618 4620 4619 writel(val | MVPP2_GMAC_IN_BAND_RESTART_AN, ··· 4800 4797 } 4801 4798 } 4802 4799 4803 - static void mvpp2_mac_config(struct net_device *dev, unsigned int mode, 4800 + static void mvpp2_mac_config(struct phylink_config *config, unsigned int mode, 4804 4801 const struct phylink_link_state *state) 4805 4802 { 4803 + struct net_device *dev = to_net_dev(config->dev); 4806 4804 struct mvpp2_port *port = netdev_priv(dev); 4807 4805 bool change_interface = port->phy_interface != state->interface; 4808 4806 ··· 4843 4839 mvpp2_port_enable(port); 4844 4840 } 4845 4841 4846 - static void mvpp2_mac_link_up(struct net_device *dev, unsigned int mode, 4842 + static void mvpp2_mac_link_up(struct phylink_config *config, unsigned int mode, 4847 4843 phy_interface_t interface, struct phy_device *phy) 4848 4844 { 4845 + struct net_device *dev = to_net_dev(config->dev); 4849 4846 struct mvpp2_port *port = netdev_priv(dev); 4850 4847 u32 val; 4851 4848 ··· 4871 4866 netif_tx_wake_all_queues(dev); 4872 4867 } 4873 4868 4874 - static void mvpp2_mac_link_down(struct net_device *dev, unsigned int mode, 4875 - phy_interface_t interface) 4869 + static void mvpp2_mac_link_down(struct phylink_config *config, 4870 + unsigned int mode, phy_interface_t interface) 4876 4871 { 4872 + struct net_device *dev = to_net_dev(config->dev); 4877 4873 struct mvpp2_port *port = netdev_priv(dev); 4878 4874 u32 val; 4879 4875 ··· 5131 5125 5132 5126 /* Phylink isn't used w/ ACPI as of now */ 5133 5127 if (port_node) { 5134 - phylink = phylink_create(dev, port_fwnode, phy_mode, 5135 - &mvpp2_phylink_ops); 5128 + port->phylink_config.dev = &dev->dev; 5129 + port->phylink_config.type = PHYLINK_NETDEV; 5130 + 5131 + phylink = phylink_create(&port->phylink_config, port_fwnode, 5132 + phy_mode, &mvpp2_phylink_ops); 5136 5133 if (IS_ERR(phylink)) { 5137 5134 err = PTR_ERR(phylink); 5138 5135 goto err_free_port_pcpu;
+75 -25
drivers/net/phy/phy_device.c
··· 948 948 { 949 949 int rc; 950 950 951 + if (!dev) 952 + return -EINVAL; 953 + 951 954 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); 952 955 if (rc) 953 956 return rc; ··· 1136 1133 } 1137 1134 EXPORT_SYMBOL(phy_attached_print); 1138 1135 1136 + static void phy_sysfs_create_links(struct phy_device *phydev) 1137 + { 1138 + struct net_device *dev = phydev->attached_dev; 1139 + int err; 1140 + 1141 + if (!dev) 1142 + return; 1143 + 1144 + err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj, 1145 + "attached_dev"); 1146 + if (err) 1147 + return; 1148 + 1149 + err = sysfs_create_link_nowarn(&dev->dev.kobj, 1150 + &phydev->mdio.dev.kobj, 1151 + "phydev"); 1152 + if (err) { 1153 + dev_err(&dev->dev, "could not add device link to %s err %d\n", 1154 + kobject_name(&phydev->mdio.dev.kobj), 1155 + err); 1156 + /* non-fatal - some net drivers can use one netdevice 1157 + * with more then one phy 1158 + */ 1159 + } 1160 + 1161 + phydev->sysfs_links = true; 1162 + } 1163 + 1164 + static ssize_t 1165 + phy_standalone_show(struct device *dev, struct device_attribute *attr, 1166 + char *buf) 1167 + { 1168 + struct phy_device *phydev = to_phy_device(dev); 1169 + 1170 + return sprintf(buf, "%d\n", !phydev->attached_dev); 1171 + } 1172 + static DEVICE_ATTR_RO(phy_standalone); 1173 + 1139 1174 /** 1140 1175 * phy_attach_direct - attach a network device to a given PHY device pointer 1141 1176 * @dev: network device to attach ··· 1192 1151 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, 1193 1152 u32 flags, phy_interface_t interface) 1194 1153 { 1195 - struct module *ndev_owner = dev->dev.parent->driver->owner; 1196 1154 struct mii_bus *bus = phydev->mdio.bus; 1197 1155 struct device *d = &phydev->mdio.dev; 1156 + struct module *ndev_owner = NULL; 1198 1157 bool using_genphy = false; 1199 1158 int err; 1200 1159 ··· 1203 1162 * our own module->refcnt here, otherwise we would not be able to 1204 1163 * unload later on. 1205 1164 */ 1165 + if (dev) 1166 + ndev_owner = dev->dev.parent->driver->owner; 1206 1167 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) { 1207 - dev_err(&dev->dev, "failed to get the bus module\n"); 1168 + phydev_err(phydev, "failed to get the bus module\n"); 1208 1169 return -EIO; 1209 1170 } 1210 1171 ··· 1225 1182 } 1226 1183 1227 1184 if (!try_module_get(d->driver->owner)) { 1228 - dev_err(&dev->dev, "failed to get the device driver module\n"); 1185 + phydev_err(phydev, "failed to get the device driver module\n"); 1229 1186 err = -EIO; 1230 1187 goto error_put_device; 1231 1188 } ··· 1246 1203 } 1247 1204 1248 1205 phydev->phy_link_change = phy_link_change; 1249 - phydev->attached_dev = dev; 1250 - dev->phydev = phydev; 1206 + if (dev) { 1207 + phydev->attached_dev = dev; 1208 + dev->phydev = phydev; 1209 + } 1251 1210 1252 1211 /* Some Ethernet drivers try to connect to a PHY device before 1253 1212 * calling register_netdevice() -> netdev_register_kobject() and ··· 1261 1216 */ 1262 1217 phydev->sysfs_links = false; 1263 1218 1264 - err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj, 1265 - "attached_dev"); 1266 - if (!err) { 1267 - err = sysfs_create_link_nowarn(&dev->dev.kobj, 1268 - &phydev->mdio.dev.kobj, 1269 - "phydev"); 1270 - if (err) { 1271 - dev_err(&dev->dev, "could not add device link to %s err %d\n", 1272 - kobject_name(&phydev->mdio.dev.kobj), 1273 - err); 1274 - /* non-fatal - some net drivers can use one netdevice 1275 - * with more then one phy 1276 - */ 1277 - } 1219 + phy_sysfs_create_links(phydev); 1278 1220 1279 - phydev->sysfs_links = true; 1221 + if (!phydev->attached_dev) { 1222 + err = sysfs_create_file(&phydev->mdio.dev.kobj, 1223 + &dev_attr_phy_standalone.attr); 1224 + if (err) 1225 + phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n"); 1280 1226 } 1281 1227 1282 1228 phydev->dev_flags = flags; ··· 1279 1243 /* Initial carrier state is off as the phy is about to be 1280 1244 * (re)initialized. 1281 1245 */ 1282 - netif_carrier_off(phydev->attached_dev); 1246 + if (dev) 1247 + netif_carrier_off(phydev->attached_dev); 1283 1248 1284 1249 /* Do initial configuration here, now that 1285 1250 * we have certain key parameters ··· 1326 1289 struct phy_device *phydev; 1327 1290 struct device *d; 1328 1291 int rc; 1292 + 1293 + if (!dev) 1294 + return ERR_PTR(-EINVAL); 1329 1295 1330 1296 /* Search the list of PHY devices on the mdio bus for the 1331 1297 * PHY with the requested name ··· 1389 1349 void phy_detach(struct phy_device *phydev) 1390 1350 { 1391 1351 struct net_device *dev = phydev->attached_dev; 1392 - struct module *ndev_owner = dev->dev.parent->driver->owner; 1352 + struct module *ndev_owner = NULL; 1393 1353 struct mii_bus *bus; 1394 1354 1395 1355 if (phydev->sysfs_links) { 1396 - sysfs_remove_link(&dev->dev.kobj, "phydev"); 1356 + if (dev) 1357 + sysfs_remove_link(&dev->dev.kobj, "phydev"); 1397 1358 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev"); 1398 1359 } 1360 + 1361 + if (!phydev->attached_dev) 1362 + sysfs_remove_file(&phydev->mdio.dev.kobj, 1363 + &dev_attr_phy_standalone.attr); 1364 + 1399 1365 phy_suspend(phydev); 1400 - phydev->attached_dev->phydev = NULL; 1401 - phydev->attached_dev = NULL; 1366 + if (dev) { 1367 + phydev->attached_dev->phydev = NULL; 1368 + phydev->attached_dev = NULL; 1369 + } 1402 1370 phydev->phylink = NULL; 1403 1371 1404 1372 phy_led_triggers_unregister(phydev); ··· 1429 1381 bus = phydev->mdio.bus; 1430 1382 1431 1383 put_device(&phydev->mdio.dev); 1384 + if (dev) 1385 + ndev_owner = dev->dev.parent->driver->owner; 1432 1386 if (ndev_owner != bus->owner) 1433 1387 module_put(bus->owner); 1434 1388
+135 -81
drivers/net/phy/phylink.c
··· 41 41 /* private: */ 42 42 struct net_device *netdev; 43 43 const struct phylink_mac_ops *ops; 44 + struct phylink_config *config; 45 + struct device *dev; 46 + unsigned int old_link_state:1; 44 47 45 48 unsigned long phylink_disable_state; /* bitmask of disables */ 46 49 struct phy_device *phydev; ··· 67 64 68 65 struct sfp_bus *sfp_bus; 69 66 }; 67 + 68 + #define phylink_printk(level, pl, fmt, ...) \ 69 + do { \ 70 + if ((pl)->config->type == PHYLINK_NETDEV) \ 71 + netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \ 72 + else if ((pl)->config->type == PHYLINK_DEV) \ 73 + dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \ 74 + } while (0) 75 + 76 + #define phylink_err(pl, fmt, ...) \ 77 + phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__) 78 + #define phylink_warn(pl, fmt, ...) \ 79 + phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__) 80 + #define phylink_info(pl, fmt, ...) \ 81 + phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__) 82 + #define phylink_dbg(pl, fmt, ...) \ 83 + phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__) 70 84 71 85 /** 72 86 * phylink_set_port_modes() - set the port type modes in the ethtool mask ··· 131 111 static int phylink_validate(struct phylink *pl, unsigned long *supported, 132 112 struct phylink_link_state *state) 133 113 { 134 - pl->ops->validate(pl->netdev, supported, state); 114 + pl->ops->validate(pl->config, supported, state); 135 115 136 116 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0; 137 117 } ··· 181 161 ret = fwnode_property_read_u32_array(fwnode, "fixed-link", 182 162 NULL, 0); 183 163 if (ret != ARRAY_SIZE(prop)) { 184 - netdev_err(pl->netdev, "broken fixed-link?\n"); 164 + phylink_err(pl, "broken fixed-link?\n"); 185 165 return -EINVAL; 186 166 } 187 167 ··· 200 180 201 181 if (pl->link_config.speed > SPEED_1000 && 202 182 pl->link_config.duplex != DUPLEX_FULL) 203 - netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n", 204 - pl->link_config.speed); 183 + phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n", 184 + pl->link_config.speed); 205 185 206 186 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 207 187 linkmode_copy(pl->link_config.advertising, pl->supported); ··· 214 194 if (s) { 215 195 __set_bit(s->bit, pl->supported); 216 196 } else { 217 - netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n", 218 - pl->link_config.duplex == DUPLEX_FULL ? "full" : "half", 219 - pl->link_config.speed); 197 + phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n", 198 + pl->link_config.duplex == DUPLEX_FULL ? "full" : "half", 199 + pl->link_config.speed); 220 200 } 221 201 222 202 linkmode_and(pl->link_config.advertising, pl->link_config.advertising, ··· 241 221 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 && 242 222 strcmp(managed, "in-band-status") == 0) { 243 223 if (pl->link_an_mode == MLO_AN_FIXED) { 244 - netdev_err(pl->netdev, 245 - "can't use both fixed-link and in-band-status\n"); 224 + phylink_err(pl, 225 + "can't use both fixed-link and in-band-status\n"); 246 226 return -EINVAL; 247 227 } 248 228 ··· 289 269 break; 290 270 291 271 default: 292 - netdev_err(pl->netdev, 293 - "incorrect link mode %s for in-band status\n", 294 - phy_modes(pl->link_config.interface)); 272 + phylink_err(pl, 273 + "incorrect link mode %s for in-band status\n", 274 + phy_modes(pl->link_config.interface)); 295 275 return -EINVAL; 296 276 } 297 277 298 278 linkmode_copy(pl->link_config.advertising, pl->supported); 299 279 300 280 if (phylink_validate(pl, pl->supported, &pl->link_config)) { 301 - netdev_err(pl->netdev, 302 - "failed to validate link configuration for in-band status\n"); 281 + phylink_err(pl, 282 + "failed to validate link configuration for in-band status\n"); 303 283 return -EINVAL; 304 284 } 305 285 } ··· 310 290 static void phylink_mac_config(struct phylink *pl, 311 291 const struct phylink_link_state *state) 312 292 { 313 - netdev_dbg(pl->netdev, 314 - "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n", 315 - __func__, phylink_an_mode_str(pl->link_an_mode), 316 - phy_modes(state->interface), 317 - phy_speed_to_str(state->speed), 318 - phy_duplex_to_str(state->duplex), 319 - __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising, 320 - state->pause, state->link, state->an_enabled); 293 + phylink_dbg(pl, 294 + "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n", 295 + __func__, phylink_an_mode_str(pl->link_an_mode), 296 + phy_modes(state->interface), 297 + phy_speed_to_str(state->speed), 298 + phy_duplex_to_str(state->duplex), 299 + __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising, 300 + state->pause, state->link, state->an_enabled); 321 301 322 - pl->ops->mac_config(pl->netdev, pl->link_an_mode, state); 302 + pl->ops->mac_config(pl->config, pl->link_an_mode, state); 323 303 } 324 304 325 305 static void phylink_mac_config_up(struct phylink *pl, ··· 333 313 { 334 314 if (pl->link_config.an_enabled && 335 315 phy_interface_mode_is_8023z(pl->link_config.interface)) 336 - pl->ops->mac_an_restart(pl->netdev); 316 + pl->ops->mac_an_restart(pl->config); 337 317 } 338 318 339 319 static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state) 340 320 { 341 - struct net_device *ndev = pl->netdev; 342 321 343 322 linkmode_copy(state->advertising, pl->link_config.advertising); 344 323 linkmode_zero(state->lp_advertising); ··· 349 330 state->an_complete = 0; 350 331 state->link = 1; 351 332 352 - return pl->ops->mac_link_state(ndev, state); 333 + return pl->ops->mac_link_state(pl->config, state); 353 334 } 354 335 355 336 /* The fixed state is... fixed except for the link state, ··· 414 395 } 415 396 } 416 397 398 + static void phylink_mac_link_up(struct phylink *pl, 399 + struct phylink_link_state link_state) 400 + { 401 + struct net_device *ndev = pl->netdev; 402 + 403 + pl->ops->mac_link_up(pl->config, pl->link_an_mode, 404 + pl->phy_state.interface, 405 + pl->phydev); 406 + 407 + if (ndev) 408 + netif_carrier_on(ndev); 409 + 410 + phylink_info(pl, 411 + "Link is Up - %s/%s - flow control %s\n", 412 + phy_speed_to_str(link_state.speed), 413 + phy_duplex_to_str(link_state.duplex), 414 + phylink_pause_to_str(link_state.pause)); 415 + } 416 + 417 + static void phylink_mac_link_down(struct phylink *pl) 418 + { 419 + struct net_device *ndev = pl->netdev; 420 + 421 + if (ndev) 422 + netif_carrier_off(ndev); 423 + pl->ops->mac_link_down(pl->config, pl->link_an_mode, 424 + pl->phy_state.interface); 425 + phylink_info(pl, "Link is Down\n"); 426 + } 427 + 417 428 static void phylink_resolve(struct work_struct *w) 418 429 { 419 430 struct phylink *pl = container_of(w, struct phylink, resolve); 420 431 struct phylink_link_state link_state; 421 432 struct net_device *ndev = pl->netdev; 433 + int link_changed; 422 434 423 435 mutex_lock(&pl->state_mutex); 424 436 if (pl->phylink_disable_state) { ··· 492 442 } 493 443 } 494 444 495 - if (link_state.link != netif_carrier_ok(ndev)) { 496 - if (!link_state.link) { 497 - netif_carrier_off(ndev); 498 - pl->ops->mac_link_down(ndev, pl->link_an_mode, 499 - pl->phy_state.interface); 500 - netdev_info(ndev, "Link is Down\n"); 501 - } else { 502 - pl->ops->mac_link_up(ndev, pl->link_an_mode, 503 - pl->phy_state.interface, 504 - pl->phydev); 445 + if (pl->netdev) 446 + link_changed = (link_state.link != netif_carrier_ok(ndev)); 447 + else 448 + link_changed = (link_state.link != pl->old_link_state); 505 449 506 - netif_carrier_on(ndev); 507 - 508 - netdev_info(ndev, 509 - "Link is Up - %s/%s - flow control %s\n", 510 - phy_speed_to_str(link_state.speed), 511 - phy_duplex_to_str(link_state.duplex), 512 - phylink_pause_to_str(link_state.pause)); 513 - } 450 + if (link_changed) { 451 + pl->old_link_state = link_state.link; 452 + if (!link_state.link) 453 + phylink_mac_link_down(pl); 454 + else 455 + phylink_mac_link_up(pl, link_state); 514 456 } 515 457 if (!link_state.link && pl->mac_link_dropped) { 516 458 pl->mac_link_dropped = false; ··· 554 512 if (ret == -ENOENT) 555 513 return 0; 556 514 557 - netdev_err(pl->netdev, "unable to parse \"sfp\" node: %d\n", 558 - ret); 515 + phylink_err(pl, "unable to parse \"sfp\" node: %d\n", 516 + ret); 559 517 return ret; 560 518 } 561 519 ··· 581 539 * Returns a pointer to a &struct phylink, or an error-pointer value. Users 582 540 * must use IS_ERR() to check for errors from this function. 583 541 */ 584 - struct phylink *phylink_create(struct net_device *ndev, 542 + struct phylink *phylink_create(struct phylink_config *config, 585 543 struct fwnode_handle *fwnode, 586 544 phy_interface_t iface, 587 545 const struct phylink_mac_ops *ops) ··· 595 553 596 554 mutex_init(&pl->state_mutex); 597 555 INIT_WORK(&pl->resolve, phylink_resolve); 598 - pl->netdev = ndev; 556 + 557 + pl->config = config; 558 + if (config->type == PHYLINK_NETDEV) { 559 + pl->netdev = to_net_dev(config->dev); 560 + } else if (config->type == PHYLINK_DEV) { 561 + pl->dev = config->dev; 562 + } else { 563 + kfree(pl); 564 + return ERR_PTR(-EINVAL); 565 + } 566 + 599 567 pl->phy_state.interface = iface; 600 568 pl->link_interface = iface; 601 569 if (iface == PHY_INTERFACE_MODE_MOCA) ··· 687 635 688 636 phylink_run_resolve(pl); 689 637 690 - netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down", 691 - phy_modes(phydev->interface), 692 - phy_speed_to_str(phydev->speed), 693 - phy_duplex_to_str(phydev->duplex)); 638 + phylink_dbg(pl, "phy link %s %s/%s/%s\n", up ? "up" : "down", 639 + phy_modes(phydev->interface), 640 + phy_speed_to_str(phydev->speed), 641 + phy_duplex_to_str(phydev->duplex)); 694 642 } 695 643 696 644 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy) ··· 723 671 phy->phylink = pl; 724 672 phy->phy_link_change = phylink_phy_change; 725 673 726 - netdev_info(pl->netdev, 727 - "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev), 728 - phy->drv->name); 674 + phylink_info(pl, 675 + "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev), 676 + phy->drv->name); 729 677 730 678 mutex_lock(&phy->lock); 731 679 mutex_lock(&pl->state_mutex); ··· 738 686 mutex_unlock(&pl->state_mutex); 739 687 mutex_unlock(&phy->lock); 740 688 741 - netdev_dbg(pl->netdev, 742 - "phy: setting supported %*pb advertising %*pb\n", 743 - __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported, 744 - __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising); 689 + phylink_dbg(pl, 690 + "phy: setting supported %*pb advertising %*pb\n", 691 + __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported, 692 + __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising); 745 693 746 694 if (phy_interrupt_is_valid(phy)) 747 695 phy_request_interrupt(phy); ··· 919 867 if (!up) 920 868 pl->mac_link_dropped = true; 921 869 phylink_run_resolve(pl); 922 - netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down"); 870 + phylink_dbg(pl, "mac link %s\n", up ? "up" : "down"); 923 871 } 924 872 EXPORT_SYMBOL_GPL(phylink_mac_change); 925 873 ··· 935 883 { 936 884 ASSERT_RTNL(); 937 885 938 - netdev_info(pl->netdev, "configuring for %s/%s link mode\n", 939 - phylink_an_mode_str(pl->link_an_mode), 940 - phy_modes(pl->link_config.interface)); 886 + phylink_info(pl, "configuring for %s/%s link mode\n", 887 + phylink_an_mode_str(pl->link_an_mode), 888 + phy_modes(pl->link_config.interface)); 941 889 942 890 /* Always set the carrier off */ 943 - netif_carrier_off(pl->netdev); 891 + if (pl->netdev) 892 + netif_carrier_off(pl->netdev); 944 893 945 894 /* Apply the link configuration to the MAC when starting. This allows 946 895 * a fixed-link to start with the correct parameters, and also ··· 1286 1233 switch (pl->link_an_mode) { 1287 1234 case MLO_AN_PHY: 1288 1235 /* Silently mark the carrier down, and then trigger a resolve */ 1289 - netif_carrier_off(pl->netdev); 1236 + if (pl->netdev) 1237 + netif_carrier_off(pl->netdev); 1290 1238 phylink_run_resolve(pl); 1291 1239 break; 1292 1240 ··· 1648 1594 /* Ignore errors if we're expecting a PHY to attach later */ 1649 1595 ret = phylink_validate(pl, support, &config); 1650 1596 if (ret) { 1651 - netdev_err(pl->netdev, "validation with support %*pb failed: %d\n", 1652 - __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret); 1597 + phylink_err(pl, "validation with support %*pb failed: %d\n", 1598 + __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret); 1653 1599 return ret; 1654 1600 } 1655 1601 1656 1602 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising); 1657 1603 if (iface == PHY_INTERFACE_MODE_NA) { 1658 - netdev_err(pl->netdev, 1659 - "selection of interface failed, advertisement %*pb\n", 1660 - __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising); 1604 + phylink_err(pl, 1605 + "selection of interface failed, advertisement %*pb\n", 1606 + __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising); 1661 1607 return -EINVAL; 1662 1608 } 1663 1609 1664 1610 config.interface = iface; 1665 1611 ret = phylink_validate(pl, support, &config); 1666 1612 if (ret) { 1667 - netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n", 1668 - phylink_an_mode_str(MLO_AN_INBAND), 1669 - phy_modes(config.interface), 1670 - __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret); 1613 + phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n", 1614 + phylink_an_mode_str(MLO_AN_INBAND), 1615 + phy_modes(config.interface), 1616 + __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret); 1671 1617 return ret; 1672 1618 } 1673 1619 1674 - netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n", 1675 - phylink_an_mode_str(MLO_AN_INBAND), 1676 - phy_modes(config.interface), 1677 - __ETHTOOL_LINK_MODE_MASK_NBITS, support); 1620 + phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n", 1621 + phylink_an_mode_str(MLO_AN_INBAND), 1622 + phy_modes(config.interface), 1623 + __ETHTOOL_LINK_MODE_MASK_NBITS, support); 1678 1624 1679 1625 if (phy_interface_mode_is_8023z(iface) && pl->phydev) 1680 1626 return -EINVAL; ··· 1693 1639 1694 1640 changed = true; 1695 1641 1696 - netdev_info(pl->netdev, "switched to %s/%s link mode\n", 1697 - phylink_an_mode_str(MLO_AN_INBAND), 1698 - phy_modes(config.interface)); 1642 + phylink_info(pl, "switched to %s/%s link mode\n", 1643 + phylink_an_mode_str(MLO_AN_INBAND), 1644 + phy_modes(config.interface)); 1699 1645 } 1700 1646 1701 1647 pl->link_port = port;
+37 -20
include/linux/phylink.h
··· 54 54 unsigned int an_complete:1; 55 55 }; 56 56 57 + enum phylink_op_type { 58 + PHYLINK_NETDEV = 0, 59 + PHYLINK_DEV, 60 + }; 61 + 62 + /** 63 + * struct phylink_config - PHYLINK configuration structure 64 + * @dev: a pointer to a struct device associated with the MAC 65 + * @type: operation type of PHYLINK instance 66 + */ 67 + struct phylink_config { 68 + struct device *dev; 69 + enum phylink_op_type type; 70 + }; 71 + 57 72 /** 58 73 * struct phylink_mac_ops - MAC operations structure. 59 74 * @validate: Validate and update the link configuration. ··· 81 66 * The individual methods are described more fully below. 82 67 */ 83 68 struct phylink_mac_ops { 84 - void (*validate)(struct net_device *ndev, unsigned long *supported, 69 + void (*validate)(struct phylink_config *config, 70 + unsigned long *supported, 85 71 struct phylink_link_state *state); 86 - int (*mac_link_state)(struct net_device *ndev, 72 + int (*mac_link_state)(struct phylink_config *config, 87 73 struct phylink_link_state *state); 88 - void (*mac_config)(struct net_device *ndev, unsigned int mode, 74 + void (*mac_config)(struct phylink_config *config, unsigned int mode, 89 75 const struct phylink_link_state *state); 90 - void (*mac_an_restart)(struct net_device *ndev); 91 - void (*mac_link_down)(struct net_device *ndev, unsigned int mode, 76 + void (*mac_an_restart)(struct phylink_config *config); 77 + void (*mac_link_down)(struct phylink_config *config, unsigned int mode, 92 78 phy_interface_t interface); 93 - void (*mac_link_up)(struct net_device *ndev, unsigned int mode, 79 + void (*mac_link_up)(struct phylink_config *config, unsigned int mode, 94 80 phy_interface_t interface, 95 81 struct phy_device *phy); 96 82 }; ··· 99 83 #if 0 /* For kernel-doc purposes only. */ 100 84 /** 101 85 * validate - Validate and update the link configuration 102 - * @ndev: a pointer to a &struct net_device for the MAC. 86 + * @config: a pointer to a &struct phylink_config. 103 87 * @supported: ethtool bitmask for supported link modes. 104 88 * @state: a pointer to a &struct phylink_link_state. 105 89 * ··· 116 100 * based on @state->advertising and/or @state->speed and update 117 101 * @state->interface accordingly. 118 102 */ 119 - void validate(struct net_device *ndev, unsigned long *supported, 103 + void validate(struct phylink_config *config, unsigned long *supported, 120 104 struct phylink_link_state *state); 121 105 122 106 /** 123 107 * mac_link_state() - Read the current link state from the hardware 124 - * @ndev: a pointer to a &struct net_device for the MAC. 108 + * @config: a pointer to a &struct phylink_config. 125 109 * @state: a pointer to a &struct phylink_link_state. 126 110 * 127 111 * Read the current link state from the MAC, reporting the current ··· 130 114 * negotiation completion state in @state->an_complete, and link 131 115 * up state in @state->link. 132 116 */ 133 - int mac_link_state(struct net_device *ndev, 117 + int mac_link_state(struct phylink_config *config, 134 118 struct phylink_link_state *state); 135 119 136 120 /** 137 121 * mac_config() - configure the MAC for the selected mode and state 138 - * @ndev: a pointer to a &struct net_device for the MAC. 122 + * @config: a pointer to a &struct phylink_config. 139 123 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND. 140 124 * @state: a pointer to a &struct phylink_link_state. 141 125 * ··· 173 157 * down. This "update" behaviour is critical to avoid bouncing the 174 158 * link up status. 175 159 */ 176 - void mac_config(struct net_device *ndev, unsigned int mode, 160 + void mac_config(struct phylink_config *config, unsigned int mode, 177 161 const struct phylink_link_state *state); 178 162 179 163 /** 180 164 * mac_an_restart() - restart 802.3z BaseX autonegotiation 181 - * @ndev: a pointer to a &struct net_device for the MAC. 165 + * @config: a pointer to a &struct phylink_config. 182 166 */ 183 - void mac_an_restart(struct net_device *ndev); 167 + void mac_an_restart(struct phylink_config *config); 184 168 185 169 /** 186 170 * mac_link_down() - take the link down 187 - * @ndev: a pointer to a &struct net_device for the MAC. 171 + * @config: a pointer to a &struct phylink_config. 188 172 * @mode: link autonegotiation mode 189 173 * @interface: link &typedef phy_interface_t mode 190 174 * ··· 193 177 * Energy Efficient Ethernet MAC configuration. Interface type 194 178 * selection must be done in mac_config(). 195 179 */ 196 - void mac_link_down(struct net_device *ndev, unsigned int mode, 180 + void mac_link_down(struct phylink_config *config, unsigned int mode, 197 181 phy_interface_t interface); 198 182 199 183 /** 200 184 * mac_link_up() - allow the link to come up 201 - * @ndev: a pointer to a &struct net_device for the MAC. 185 + * @config: a pointer to a &struct phylink_config. 202 186 * @mode: link autonegotiation mode 203 187 * @interface: link &typedef phy_interface_t mode 204 188 * @phy: any attached phy ··· 209 193 * phy_init_eee() and perform appropriate MAC configuration for EEE. 210 194 * Interface type selection must be done in mac_config(). 211 195 */ 212 - void mac_link_up(struct net_device *ndev, unsigned int mode, 196 + void mac_link_up(struct phylink_config *config, unsigned int mode, 213 197 phy_interface_t interface, 214 198 struct phy_device *phy); 215 199 #endif 216 200 217 - struct phylink *phylink_create(struct net_device *, struct fwnode_handle *, 218 - phy_interface_t iface, const struct phylink_mac_ops *ops); 201 + struct phylink *phylink_create(struct phylink_config *, struct fwnode_handle *, 202 + phy_interface_t iface, 203 + const struct phylink_mac_ops *ops); 219 204 void phylink_destroy(struct phylink *); 220 205 221 206 int phylink_connect_phy(struct phylink *, struct phy_device *);
+2
include/net/dsa.h
··· 22 22 #include <linux/net_tstamp.h> 23 23 #include <linux/phy.h> 24 24 #include <linux/platform_data/dsa.h> 25 + #include <linux/phylink.h> 25 26 #include <net/devlink.h> 26 27 #include <net/switchdev.h> 27 28 ··· 194 193 struct net_device *bridge_dev; 195 194 struct devlink_port devlink_port; 196 195 struct phylink *pl; 196 + struct phylink_config pl_config; 197 197 198 198 struct work_struct xmit_work; 199 199 struct sk_buff_head xmit_queue;
+17
net/dsa/dsa_priv.h
··· 163 163 int dsa_port_vid_del(struct dsa_port *dp, u16 vid); 164 164 int dsa_port_link_register_of(struct dsa_port *dp); 165 165 void dsa_port_link_unregister_of(struct dsa_port *dp); 166 + void dsa_port_phylink_validate(struct phylink_config *config, 167 + unsigned long *supported, 168 + struct phylink_link_state *state); 169 + int dsa_port_phylink_mac_link_state(struct phylink_config *config, 170 + struct phylink_link_state *state); 171 + void dsa_port_phylink_mac_config(struct phylink_config *config, 172 + unsigned int mode, 173 + const struct phylink_link_state *state); 174 + void dsa_port_phylink_mac_an_restart(struct phylink_config *config); 175 + void dsa_port_phylink_mac_link_down(struct phylink_config *config, 176 + unsigned int mode, 177 + phy_interface_t interface); 178 + void dsa_port_phylink_mac_link_up(struct phylink_config *config, 179 + unsigned int mode, 180 + phy_interface_t interface, 181 + struct phy_device *phydev); 182 + extern const struct phylink_mac_ops dsa_port_phylink_mac_ops; 166 183 167 184 /* slave.c */ 168 185 extern const struct dsa_device_ops notag_netdev_ops;
+157
net/dsa/port.c
··· 422 422 return phydev; 423 423 } 424 424 425 + void dsa_port_phylink_validate(struct phylink_config *config, 426 + unsigned long *supported, 427 + struct phylink_link_state *state) 428 + { 429 + struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 430 + struct dsa_switch *ds = dp->ds; 431 + 432 + if (!ds->ops->phylink_validate) 433 + return; 434 + 435 + ds->ops->phylink_validate(ds, dp->index, supported, state); 436 + } 437 + EXPORT_SYMBOL_GPL(dsa_port_phylink_validate); 438 + 439 + int dsa_port_phylink_mac_link_state(struct phylink_config *config, 440 + struct phylink_link_state *state) 441 + { 442 + struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 443 + struct dsa_switch *ds = dp->ds; 444 + 445 + /* Only called for SGMII and 802.3z */ 446 + if (!ds->ops->phylink_mac_link_state) 447 + return -EOPNOTSUPP; 448 + 449 + return ds->ops->phylink_mac_link_state(ds, dp->index, state); 450 + } 451 + EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_link_state); 452 + 453 + void dsa_port_phylink_mac_config(struct phylink_config *config, 454 + unsigned int mode, 455 + const struct phylink_link_state *state) 456 + { 457 + struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 458 + struct dsa_switch *ds = dp->ds; 459 + 460 + if (!ds->ops->phylink_mac_config) 461 + return; 462 + 463 + ds->ops->phylink_mac_config(ds, dp->index, mode, state); 464 + } 465 + EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_config); 466 + 467 + void dsa_port_phylink_mac_an_restart(struct phylink_config *config) 468 + { 469 + struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 470 + struct dsa_switch *ds = dp->ds; 471 + 472 + if (!ds->ops->phylink_mac_an_restart) 473 + return; 474 + 475 + ds->ops->phylink_mac_an_restart(ds, dp->index); 476 + } 477 + EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_an_restart); 478 + 479 + void dsa_port_phylink_mac_link_down(struct phylink_config *config, 480 + unsigned int mode, 481 + phy_interface_t interface) 482 + { 483 + struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 484 + struct phy_device *phydev = NULL; 485 + struct dsa_switch *ds = dp->ds; 486 + 487 + if (dsa_is_user_port(ds, dp->index)) 488 + phydev = dp->slave->phydev; 489 + 490 + if (!ds->ops->phylink_mac_link_down) { 491 + if (ds->ops->adjust_link && phydev) 492 + ds->ops->adjust_link(ds, dp->index, phydev); 493 + return; 494 + } 495 + 496 + ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface); 497 + } 498 + EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_link_down); 499 + 500 + void dsa_port_phylink_mac_link_up(struct phylink_config *config, 501 + unsigned int mode, 502 + phy_interface_t interface, 503 + struct phy_device *phydev) 504 + { 505 + struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 506 + struct dsa_switch *ds = dp->ds; 507 + 508 + if (!ds->ops->phylink_mac_link_up) { 509 + if (ds->ops->adjust_link && phydev) 510 + ds->ops->adjust_link(ds, dp->index, phydev); 511 + return; 512 + } 513 + 514 + ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev); 515 + } 516 + EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_link_up); 517 + 518 + const struct phylink_mac_ops dsa_port_phylink_mac_ops = { 519 + .validate = dsa_port_phylink_validate, 520 + .mac_link_state = dsa_port_phylink_mac_link_state, 521 + .mac_config = dsa_port_phylink_mac_config, 522 + .mac_an_restart = dsa_port_phylink_mac_an_restart, 523 + .mac_link_down = dsa_port_phylink_mac_link_down, 524 + .mac_link_up = dsa_port_phylink_mac_link_up, 525 + }; 526 + 425 527 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable) 426 528 { 427 529 struct dsa_switch *ds = dp->ds; ··· 601 499 return 0; 602 500 } 603 501 502 + static int dsa_port_phylink_register(struct dsa_port *dp) 503 + { 504 + struct dsa_switch *ds = dp->ds; 505 + struct device_node *port_dn = dp->dn; 506 + int mode, err; 507 + 508 + mode = of_get_phy_mode(port_dn); 509 + if (mode < 0) 510 + mode = PHY_INTERFACE_MODE_NA; 511 + 512 + dp->pl_config.dev = ds->dev; 513 + dp->pl_config.type = PHYLINK_DEV; 514 + 515 + dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), 516 + mode, &dsa_port_phylink_mac_ops); 517 + if (IS_ERR(dp->pl)) { 518 + pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); 519 + return PTR_ERR(dp->pl); 520 + } 521 + 522 + err = phylink_of_phy_connect(dp->pl, port_dn, 0); 523 + if (err) { 524 + pr_err("could not attach to PHY: %d\n", err); 525 + goto err_phy_connect; 526 + } 527 + 528 + rtnl_lock(); 529 + phylink_start(dp->pl); 530 + rtnl_unlock(); 531 + 532 + return 0; 533 + 534 + err_phy_connect: 535 + phylink_destroy(dp->pl); 536 + return err; 537 + } 538 + 604 539 int dsa_port_link_register_of(struct dsa_port *dp) 605 540 { 541 + struct dsa_switch *ds = dp->ds; 542 + 543 + if (!ds->ops->adjust_link) 544 + return dsa_port_phylink_register(dp); 545 + 546 + dev_warn(ds->dev, 547 + "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n"); 548 + 606 549 if (of_phy_is_fixed_link(dp->dn)) 607 550 return dsa_port_fixed_link_register_of(dp); 608 551 else ··· 656 509 657 510 void dsa_port_link_unregister_of(struct dsa_port *dp) 658 511 { 512 + struct dsa_switch *ds = dp->ds; 513 + 514 + if (!ds->ops->adjust_link) { 515 + rtnl_lock(); 516 + phylink_disconnect_phy(dp->pl); 517 + rtnl_unlock(); 518 + phylink_destroy(dp->pl); 519 + return; 520 + } 521 + 659 522 if (of_phy_is_fixed_link(dp->dn)) 660 523 of_phy_deregister_fixed_link(dp->dn); 661 524 else
+5 -94
net/dsa/slave.c
··· 1164 1164 .name = "dsa", 1165 1165 }; 1166 1166 1167 - static void dsa_slave_phylink_validate(struct net_device *dev, 1168 - unsigned long *supported, 1169 - struct phylink_link_state *state) 1170 - { 1171 - struct dsa_port *dp = dsa_slave_to_port(dev); 1172 - struct dsa_switch *ds = dp->ds; 1173 - 1174 - if (!ds->ops->phylink_validate) 1175 - return; 1176 - 1177 - ds->ops->phylink_validate(ds, dp->index, supported, state); 1178 - } 1179 - 1180 - static int dsa_slave_phylink_mac_link_state(struct net_device *dev, 1181 - struct phylink_link_state *state) 1182 - { 1183 - struct dsa_port *dp = dsa_slave_to_port(dev); 1184 - struct dsa_switch *ds = dp->ds; 1185 - 1186 - /* Only called for SGMII and 802.3z */ 1187 - if (!ds->ops->phylink_mac_link_state) 1188 - return -EOPNOTSUPP; 1189 - 1190 - return ds->ops->phylink_mac_link_state(ds, dp->index, state); 1191 - } 1192 - 1193 - static void dsa_slave_phylink_mac_config(struct net_device *dev, 1194 - unsigned int mode, 1195 - const struct phylink_link_state *state) 1196 - { 1197 - struct dsa_port *dp = dsa_slave_to_port(dev); 1198 - struct dsa_switch *ds = dp->ds; 1199 - 1200 - if (!ds->ops->phylink_mac_config) 1201 - return; 1202 - 1203 - ds->ops->phylink_mac_config(ds, dp->index, mode, state); 1204 - } 1205 - 1206 - static void dsa_slave_phylink_mac_an_restart(struct net_device *dev) 1207 - { 1208 - struct dsa_port *dp = dsa_slave_to_port(dev); 1209 - struct dsa_switch *ds = dp->ds; 1210 - 1211 - if (!ds->ops->phylink_mac_an_restart) 1212 - return; 1213 - 1214 - ds->ops->phylink_mac_an_restart(ds, dp->index); 1215 - } 1216 - 1217 - static void dsa_slave_phylink_mac_link_down(struct net_device *dev, 1218 - unsigned int mode, 1219 - phy_interface_t interface) 1220 - { 1221 - struct dsa_port *dp = dsa_slave_to_port(dev); 1222 - struct dsa_switch *ds = dp->ds; 1223 - 1224 - if (!ds->ops->phylink_mac_link_down) { 1225 - if (ds->ops->adjust_link && dev->phydev) 1226 - ds->ops->adjust_link(ds, dp->index, dev->phydev); 1227 - return; 1228 - } 1229 - 1230 - ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface); 1231 - } 1232 - 1233 - static void dsa_slave_phylink_mac_link_up(struct net_device *dev, 1234 - unsigned int mode, 1235 - phy_interface_t interface, 1236 - struct phy_device *phydev) 1237 - { 1238 - struct dsa_port *dp = dsa_slave_to_port(dev); 1239 - struct dsa_switch *ds = dp->ds; 1240 - 1241 - if (!ds->ops->phylink_mac_link_up) { 1242 - if (ds->ops->adjust_link && dev->phydev) 1243 - ds->ops->adjust_link(ds, dp->index, dev->phydev); 1244 - return; 1245 - } 1246 - 1247 - ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev); 1248 - } 1249 - 1250 - static const struct phylink_mac_ops dsa_slave_phylink_mac_ops = { 1251 - .validate = dsa_slave_phylink_validate, 1252 - .mac_link_state = dsa_slave_phylink_mac_link_state, 1253 - .mac_config = dsa_slave_phylink_mac_config, 1254 - .mac_an_restart = dsa_slave_phylink_mac_an_restart, 1255 - .mac_link_down = dsa_slave_phylink_mac_link_down, 1256 - .mac_link_up = dsa_slave_phylink_mac_link_up, 1257 - }; 1258 - 1259 1167 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up) 1260 1168 { 1261 1169 const struct dsa_port *dp = dsa_to_port(ds, port); ··· 1211 1303 if (mode < 0) 1212 1304 mode = PHY_INTERFACE_MODE_NA; 1213 1305 1214 - dp->pl = phylink_create(slave_dev, of_fwnode_handle(port_dn), mode, 1215 - &dsa_slave_phylink_mac_ops); 1306 + dp->pl_config.dev = &slave_dev->dev; 1307 + dp->pl_config.type = PHYLINK_NETDEV; 1308 + 1309 + dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode, 1310 + &dsa_port_phylink_mac_ops); 1216 1311 if (IS_ERR(dp->pl)) { 1217 1312 netdev_err(slave_dev, 1218 1313 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));