1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2 /* Copyright 2024 NXP */ 3 4 #include <linux/fsl/enetc_mdio.h> 5 #include <linux/of_mdio.h> 6 #include <linux/of_net.h> 7 8 #include "enetc_pf_common.h" 9 10 static void enetc_set_si_hw_addr(struct enetc_pf *pf, int si, 11 const u8 *mac_addr) 12 { 13 struct enetc_hw *hw = &pf->si->hw; 14 15 pf->ops->set_si_primary_mac(hw, si, mac_addr); 16 } 17 18 static void enetc_get_si_hw_addr(struct enetc_pf *pf, int si, u8 *mac_addr) 19 { 20 struct enetc_hw *hw = &pf->si->hw; 21 22 pf->ops->get_si_primary_mac(hw, si, mac_addr); 23 } 24 25 int enetc_pf_set_mac_addr(struct net_device *ndev, void *addr) 26 { 27 struct enetc_ndev_priv *priv = netdev_priv(ndev); 28 struct enetc_pf *pf = enetc_si_priv(priv->si); 29 struct sockaddr *saddr = addr; 30 31 if (!is_valid_ether_addr(saddr->sa_data)) 32 return -EADDRNOTAVAIL; 33 34 eth_hw_addr_set(ndev, saddr->sa_data); 35 enetc_set_si_hw_addr(pf, 0, saddr->sa_data); 36 37 return 0; 38 } 39 EXPORT_SYMBOL_GPL(enetc_pf_set_mac_addr); 40 41 static int enetc_setup_mac_address(struct device_node *np, struct enetc_pf *pf, 42 int si) 43 { 44 struct device *dev = &pf->si->pdev->dev; 45 u8 mac_addr[ETH_ALEN] = { 0 }; 46 int err; 47 48 /* (1) try to get the MAC address from the device tree */ 49 if (np) { 50 err = of_get_mac_address(np, mac_addr); 51 if (err == -EPROBE_DEFER) 52 return err; 53 } 54 55 /* (2) bootloader supplied MAC address */ 56 if (is_zero_ether_addr(mac_addr)) 57 enetc_get_si_hw_addr(pf, si, mac_addr); 58 59 /* (3) choose a random one */ 60 if (is_zero_ether_addr(mac_addr)) { 61 eth_random_addr(mac_addr); 62 dev_info(dev, "no MAC address specified for SI%d, using %pM\n", 63 si, mac_addr); 64 } 65 66 enetc_set_si_hw_addr(pf, si, mac_addr); 67 68 return 0; 69 } 70 71 int enetc_setup_mac_addresses(struct device_node *np, struct enetc_pf *pf) 72 { 73 int err, i; 74 75 /* The PF might take its MAC from the device tree */ 76 err = enetc_setup_mac_address(np, pf, 0); 77 if (err) 78 return err; 79 80 for (i = 0; i < pf->total_vfs; i++) { 81 err = enetc_setup_mac_address(NULL, pf, i + 1); 82 if (err) 83 return err; 84 } 85 86 return 0; 87 } 88 EXPORT_SYMBOL_GPL(enetc_setup_mac_addresses); 89 90 void enetc_pf_netdev_setup(struct enetc_si *si, struct net_device *ndev, 91 const struct net_device_ops *ndev_ops) 92 { 93 struct enetc_ndev_priv *priv = netdev_priv(ndev); 94 struct enetc_pf *pf = enetc_si_priv(si); 95 96 SET_NETDEV_DEV(ndev, &si->pdev->dev); 97 priv->ndev = ndev; 98 priv->si = si; 99 priv->dev = &si->pdev->dev; 100 si->ndev = ndev; 101 102 priv->msg_enable = (NETIF_MSG_WOL << 1) - 1; 103 priv->sysclk_freq = si->drvdata->sysclk_freq; 104 priv->max_frags = si->drvdata->max_frags; 105 ndev->netdev_ops = ndev_ops; 106 enetc_set_ethtool_ops(ndev); 107 ndev->watchdog_timeo = 5 * HZ; 108 ndev->max_mtu = ENETC_MAX_MTU; 109 110 ndev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM | 111 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | 112 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_LOOPBACK | 113 NETIF_F_HW_CSUM | NETIF_F_TSO | NETIF_F_TSO6 | 114 NETIF_F_GSO_UDP_L4; 115 ndev->features = NETIF_F_HIGHDMA | NETIF_F_SG | NETIF_F_RXCSUM | 116 NETIF_F_HW_VLAN_CTAG_TX | 117 NETIF_F_HW_VLAN_CTAG_RX | 118 NETIF_F_HW_CSUM | NETIF_F_TSO | NETIF_F_TSO6 | 119 NETIF_F_GSO_UDP_L4; 120 ndev->vlan_features = NETIF_F_SG | NETIF_F_HW_CSUM | 121 NETIF_F_TSO | NETIF_F_TSO6; 122 123 ndev->priv_flags |= IFF_UNICAST_FLT; 124 125 if (si->drvdata->tx_csum) 126 priv->active_offloads |= ENETC_F_TXCSUM; 127 128 if (si->hw_features & ENETC_SI_F_LSO) 129 priv->active_offloads |= ENETC_F_LSO; 130 131 /* TODO: currently, i.MX95 ENETC driver does not support advanced features */ 132 if (!is_enetc_rev1(si)) { 133 ndev->hw_features &= ~(NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_LOOPBACK); 134 goto end; 135 } 136 137 if (si->num_rss) 138 ndev->hw_features |= NETIF_F_RXHASH; 139 140 ndev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 141 NETDEV_XDP_ACT_NDO_XMIT | NETDEV_XDP_ACT_RX_SG | 142 NETDEV_XDP_ACT_NDO_XMIT_SG; 143 144 if (si->hw_features & ENETC_SI_F_PSFP && pf->ops->enable_psfp && 145 !pf->ops->enable_psfp(priv)) { 146 priv->active_offloads |= ENETC_F_QCI; 147 ndev->features |= NETIF_F_HW_TC; 148 ndev->hw_features |= NETIF_F_HW_TC; 149 } 150 151 end: 152 /* pick up primary MAC address from SI */ 153 enetc_load_primary_mac_addr(&si->hw, ndev); 154 } 155 EXPORT_SYMBOL_GPL(enetc_pf_netdev_setup); 156 157 static int enetc_mdio_probe(struct enetc_pf *pf, struct device_node *np) 158 { 159 struct device *dev = &pf->si->pdev->dev; 160 struct enetc_mdio_priv *mdio_priv; 161 struct mii_bus *bus; 162 int err; 163 164 bus = devm_mdiobus_alloc_size(dev, sizeof(*mdio_priv)); 165 if (!bus) 166 return -ENOMEM; 167 168 bus->name = "Freescale ENETC MDIO Bus"; 169 bus->read = enetc_mdio_read_c22; 170 bus->write = enetc_mdio_write_c22; 171 bus->read_c45 = enetc_mdio_read_c45; 172 bus->write_c45 = enetc_mdio_write_c45; 173 bus->parent = dev; 174 mdio_priv = bus->priv; 175 mdio_priv->hw = &pf->si->hw; 176 mdio_priv->mdio_base = ENETC_EMDIO_BASE; 177 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev)); 178 179 err = of_mdiobus_register(bus, np); 180 if (err) 181 return dev_err_probe(dev, err, "cannot register MDIO bus\n"); 182 183 pf->mdio = bus; 184 185 return 0; 186 } 187 188 static void enetc_mdio_remove(struct enetc_pf *pf) 189 { 190 if (pf->mdio) 191 mdiobus_unregister(pf->mdio); 192 } 193 194 static int enetc_imdio_create(struct enetc_pf *pf) 195 { 196 struct device *dev = &pf->si->pdev->dev; 197 struct enetc_mdio_priv *mdio_priv; 198 struct phylink_pcs *phylink_pcs; 199 struct mii_bus *bus; 200 int err; 201 202 if (!pf->ops->create_pcs) { 203 dev_err(dev, "Creating PCS is not supported\n"); 204 205 return -EOPNOTSUPP; 206 } 207 208 bus = mdiobus_alloc_size(sizeof(*mdio_priv)); 209 if (!bus) 210 return -ENOMEM; 211 212 bus->name = "Freescale ENETC internal MDIO Bus"; 213 bus->read = enetc_mdio_read_c22; 214 bus->write = enetc_mdio_write_c22; 215 bus->read_c45 = enetc_mdio_read_c45; 216 bus->write_c45 = enetc_mdio_write_c45; 217 bus->parent = dev; 218 bus->phy_mask = ~0; 219 mdio_priv = bus->priv; 220 mdio_priv->hw = &pf->si->hw; 221 mdio_priv->mdio_base = ENETC_PM_IMDIO_BASE; 222 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-imdio", dev_name(dev)); 223 224 err = mdiobus_register(bus); 225 if (err) { 226 dev_err(dev, "cannot register internal MDIO bus (%d)\n", err); 227 goto free_mdio_bus; 228 } 229 230 phylink_pcs = pf->ops->create_pcs(pf, bus); 231 if (IS_ERR(phylink_pcs)) { 232 err = PTR_ERR(phylink_pcs); 233 dev_err(dev, "cannot create lynx pcs (%d)\n", err); 234 goto unregister_mdiobus; 235 } 236 237 pf->imdio = bus; 238 pf->pcs = phylink_pcs; 239 240 return 0; 241 242 unregister_mdiobus: 243 mdiobus_unregister(bus); 244 free_mdio_bus: 245 mdiobus_free(bus); 246 return err; 247 } 248 249 static void enetc_imdio_remove(struct enetc_pf *pf) 250 { 251 if (pf->pcs && pf->ops->destroy_pcs) 252 pf->ops->destroy_pcs(pf->pcs); 253 254 if (pf->imdio) { 255 mdiobus_unregister(pf->imdio); 256 mdiobus_free(pf->imdio); 257 } 258 } 259 260 static bool enetc_port_has_pcs(struct enetc_pf *pf) 261 { 262 return (pf->if_mode == PHY_INTERFACE_MODE_SGMII || 263 pf->if_mode == PHY_INTERFACE_MODE_1000BASEX || 264 pf->if_mode == PHY_INTERFACE_MODE_2500BASEX || 265 pf->if_mode == PHY_INTERFACE_MODE_USXGMII); 266 } 267 268 int enetc_mdiobus_create(struct enetc_pf *pf, struct device_node *node) 269 { 270 struct device_node *mdio_np; 271 int err; 272 273 mdio_np = of_get_child_by_name(node, "mdio"); 274 if (mdio_np) { 275 err = enetc_mdio_probe(pf, mdio_np); 276 277 of_node_put(mdio_np); 278 if (err) 279 return err; 280 } 281 282 if (enetc_port_has_pcs(pf)) { 283 err = enetc_imdio_create(pf); 284 if (err) { 285 enetc_mdio_remove(pf); 286 return err; 287 } 288 } 289 290 return 0; 291 } 292 EXPORT_SYMBOL_GPL(enetc_mdiobus_create); 293 294 void enetc_mdiobus_destroy(struct enetc_pf *pf) 295 { 296 enetc_mdio_remove(pf); 297 enetc_imdio_remove(pf); 298 } 299 EXPORT_SYMBOL_GPL(enetc_mdiobus_destroy); 300 301 int enetc_phylink_create(struct enetc_ndev_priv *priv, struct device_node *node, 302 const struct phylink_mac_ops *ops) 303 { 304 struct enetc_pf *pf = enetc_si_priv(priv->si); 305 struct phylink *phylink; 306 int err; 307 308 pf->phylink_config.dev = &priv->ndev->dev; 309 pf->phylink_config.type = PHYLINK_NETDEV; 310 pf->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE | 311 MAC_10 | MAC_100 | MAC_1000 | MAC_2500FD; 312 313 __set_bit(PHY_INTERFACE_MODE_INTERNAL, 314 pf->phylink_config.supported_interfaces); 315 __set_bit(PHY_INTERFACE_MODE_SGMII, 316 pf->phylink_config.supported_interfaces); 317 __set_bit(PHY_INTERFACE_MODE_1000BASEX, 318 pf->phylink_config.supported_interfaces); 319 __set_bit(PHY_INTERFACE_MODE_2500BASEX, 320 pf->phylink_config.supported_interfaces); 321 __set_bit(PHY_INTERFACE_MODE_USXGMII, 322 pf->phylink_config.supported_interfaces); 323 phy_interface_set_rgmii(pf->phylink_config.supported_interfaces); 324 325 phylink = phylink_create(&pf->phylink_config, of_fwnode_handle(node), 326 pf->if_mode, ops); 327 if (IS_ERR(phylink)) { 328 err = PTR_ERR(phylink); 329 return err; 330 } 331 332 priv->phylink = phylink; 333 334 return 0; 335 } 336 EXPORT_SYMBOL_GPL(enetc_phylink_create); 337 338 void enetc_phylink_destroy(struct enetc_ndev_priv *priv) 339 { 340 phylink_destroy(priv->phylink); 341 } 342 EXPORT_SYMBOL_GPL(enetc_phylink_destroy); 343 344 MODULE_DESCRIPTION("NXP ENETC PF common functionality driver"); 345 MODULE_LICENSE("Dual BSD/GPL"); 346