1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2 /* Copyright 2019 NXP */ 3 4 #include <linux/acpi.h> 5 #include <linux/pcs-lynx.h> 6 #include <linux/phy/phy.h> 7 #include <linux/property.h> 8 9 #include "dpaa2-eth.h" 10 #include "dpaa2-mac.h" 11 12 #define phylink_to_dpaa2_mac(config) \ 13 container_of((config), struct dpaa2_mac, phylink_config) 14 15 #define DPMAC_PROTOCOL_CHANGE_VER_MAJOR 4 16 #define DPMAC_PROTOCOL_CHANGE_VER_MINOR 8 17 18 #define DPAA2_MAC_FEATURE_PROTOCOL_CHANGE BIT(0) 19 20 static int dpaa2_mac_cmp_ver(struct dpaa2_mac *mac, 21 u16 ver_major, u16 ver_minor) 22 { 23 if (mac->ver_major == ver_major) 24 return mac->ver_minor - ver_minor; 25 return mac->ver_major - ver_major; 26 } 27 28 static void dpaa2_mac_detect_features(struct dpaa2_mac *mac) 29 { 30 mac->features = 0; 31 32 if (dpaa2_mac_cmp_ver(mac, DPMAC_PROTOCOL_CHANGE_VER_MAJOR, 33 DPMAC_PROTOCOL_CHANGE_VER_MINOR) >= 0) 34 mac->features |= DPAA2_MAC_FEATURE_PROTOCOL_CHANGE; 35 } 36 37 static int phy_mode(enum dpmac_eth_if eth_if, phy_interface_t *if_mode) 38 { 39 *if_mode = PHY_INTERFACE_MODE_NA; 40 41 switch (eth_if) { 42 case DPMAC_ETH_IF_RGMII: 43 *if_mode = PHY_INTERFACE_MODE_RGMII; 44 break; 45 case DPMAC_ETH_IF_USXGMII: 46 *if_mode = PHY_INTERFACE_MODE_USXGMII; 47 break; 48 case DPMAC_ETH_IF_QSGMII: 49 *if_mode = PHY_INTERFACE_MODE_QSGMII; 50 break; 51 case DPMAC_ETH_IF_SGMII: 52 *if_mode = PHY_INTERFACE_MODE_SGMII; 53 break; 54 case DPMAC_ETH_IF_XFI: 55 *if_mode = PHY_INTERFACE_MODE_10GBASER; 56 break; 57 case DPMAC_ETH_IF_CAUI: 58 *if_mode = PHY_INTERFACE_MODE_25GBASER; 59 break; 60 default: 61 return -EINVAL; 62 } 63 64 return 0; 65 } 66 67 static enum dpmac_eth_if dpmac_eth_if_mode(phy_interface_t if_mode) 68 { 69 switch (if_mode) { 70 case PHY_INTERFACE_MODE_RGMII: 71 case PHY_INTERFACE_MODE_RGMII_ID: 72 case PHY_INTERFACE_MODE_RGMII_RXID: 73 case PHY_INTERFACE_MODE_RGMII_TXID: 74 return DPMAC_ETH_IF_RGMII; 75 case PHY_INTERFACE_MODE_USXGMII: 76 return DPMAC_ETH_IF_USXGMII; 77 case PHY_INTERFACE_MODE_QSGMII: 78 return DPMAC_ETH_IF_QSGMII; 79 case PHY_INTERFACE_MODE_SGMII: 80 return DPMAC_ETH_IF_SGMII; 81 case PHY_INTERFACE_MODE_10GBASER: 82 return DPMAC_ETH_IF_XFI; 83 case PHY_INTERFACE_MODE_1000BASEX: 84 return DPMAC_ETH_IF_1000BASEX; 85 case PHY_INTERFACE_MODE_25GBASER: 86 return DPMAC_ETH_IF_CAUI; 87 default: 88 return DPMAC_ETH_IF_MII; 89 } 90 } 91 92 static struct fwnode_handle *dpaa2_mac_get_node(struct device *dev, 93 u16 dpmac_id) 94 { 95 struct fwnode_handle *fwnode, *parent = NULL, *child = NULL; 96 struct device_node *dpmacs = NULL; 97 int err; 98 u32 id; 99 100 fwnode = dev_fwnode(dev->parent); 101 if (is_of_node(fwnode)) { 102 dpmacs = of_find_node_by_name(NULL, "dpmacs"); 103 if (!dpmacs) 104 return NULL; 105 parent = of_fwnode_handle(dpmacs); 106 } else if (is_acpi_node(fwnode)) { 107 parent = fwnode; 108 } else { 109 /* The root dprc device didn't yet get to finalize it's probe, 110 * thus the fwnode field is not yet set. Defer probe if we are 111 * facing this situation. 112 */ 113 dev_dbg(dev, "dprc not finished probing\n"); 114 return ERR_PTR(-EPROBE_DEFER); 115 } 116 117 fwnode_for_each_child_node(parent, child) { 118 err = -EINVAL; 119 if (is_acpi_device_node(child)) 120 err = acpi_get_local_address(ACPI_HANDLE_FWNODE(child), &id); 121 else if (is_of_node(child)) 122 err = of_property_read_u32(to_of_node(child), "reg", &id); 123 if (err) 124 continue; 125 126 if (id == dpmac_id) { 127 of_node_put(dpmacs); 128 return child; 129 } 130 } 131 of_node_put(dpmacs); 132 return NULL; 133 } 134 135 static int dpaa2_mac_get_if_mode(struct fwnode_handle *dpmac_node, 136 struct dpmac_attr attr) 137 { 138 phy_interface_t if_mode; 139 int err; 140 141 err = fwnode_get_phy_mode(dpmac_node); 142 if (err > 0) 143 return err; 144 145 err = phy_mode(attr.eth_if, &if_mode); 146 if (!err) 147 return if_mode; 148 149 return err; 150 } 151 152 static struct phylink_pcs *dpaa2_mac_select_pcs(struct phylink_config *config, 153 phy_interface_t interface) 154 { 155 struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config); 156 157 return mac->pcs; 158 } 159 160 static void dpaa2_mac_config(struct phylink_config *config, unsigned int mode, 161 const struct phylink_link_state *state) 162 { 163 struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config); 164 struct dpmac_link_state *dpmac_state = &mac->state; 165 int err; 166 167 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 168 state->advertising)) 169 dpmac_state->options |= DPMAC_LINK_OPT_AUTONEG; 170 else 171 dpmac_state->options &= ~DPMAC_LINK_OPT_AUTONEG; 172 173 err = dpmac_set_link_state(mac->mc_io, 0, 174 mac->mc_dev->mc_handle, dpmac_state); 175 if (err) 176 netdev_err(mac->net_dev, "%s: dpmac_set_link_state() = %d\n", 177 __func__, err); 178 179 if (!mac->serdes_phy) 180 return; 181 182 /* This happens only if we support changing of protocol at runtime */ 183 err = dpmac_set_protocol(mac->mc_io, 0, mac->mc_dev->mc_handle, 184 dpmac_eth_if_mode(state->interface)); 185 if (err) 186 netdev_err(mac->net_dev, "dpmac_set_protocol() = %d\n", err); 187 188 err = phy_set_mode_ext(mac->serdes_phy, PHY_MODE_ETHERNET, state->interface); 189 if (err) 190 netdev_err(mac->net_dev, "phy_set_mode_ext() = %d\n", err); 191 } 192 193 static void dpaa2_mac_link_up(struct phylink_config *config, 194 struct phy_device *phy, 195 unsigned int mode, phy_interface_t interface, 196 int speed, int duplex, 197 bool tx_pause, bool rx_pause) 198 { 199 struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config); 200 struct dpmac_link_state *dpmac_state = &mac->state; 201 int err; 202 203 dpmac_state->up = 1; 204 205 dpmac_state->rate = speed; 206 207 if (duplex == DUPLEX_HALF) 208 dpmac_state->options |= DPMAC_LINK_OPT_HALF_DUPLEX; 209 else if (duplex == DUPLEX_FULL) 210 dpmac_state->options &= ~DPMAC_LINK_OPT_HALF_DUPLEX; 211 212 if (rx_pause) 213 dpmac_state->options |= DPMAC_LINK_OPT_PAUSE; 214 else 215 dpmac_state->options &= ~DPMAC_LINK_OPT_PAUSE; 216 217 if (rx_pause ^ tx_pause) 218 dpmac_state->options |= DPMAC_LINK_OPT_ASYM_PAUSE; 219 else 220 dpmac_state->options &= ~DPMAC_LINK_OPT_ASYM_PAUSE; 221 222 err = dpmac_set_link_state(mac->mc_io, 0, 223 mac->mc_dev->mc_handle, dpmac_state); 224 if (err) 225 netdev_err(mac->net_dev, "%s: dpmac_set_link_state() = %d\n", 226 __func__, err); 227 } 228 229 static void dpaa2_mac_link_down(struct phylink_config *config, 230 unsigned int mode, 231 phy_interface_t interface) 232 { 233 struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config); 234 struct dpmac_link_state *dpmac_state = &mac->state; 235 int err; 236 237 dpmac_state->up = 0; 238 err = dpmac_set_link_state(mac->mc_io, 0, 239 mac->mc_dev->mc_handle, dpmac_state); 240 if (err) 241 netdev_err(mac->net_dev, "dpmac_set_link_state() = %d\n", err); 242 } 243 244 static const struct phylink_mac_ops dpaa2_mac_phylink_ops = { 245 .mac_select_pcs = dpaa2_mac_select_pcs, 246 .mac_config = dpaa2_mac_config, 247 .mac_link_up = dpaa2_mac_link_up, 248 .mac_link_down = dpaa2_mac_link_down, 249 }; 250 251 static int dpaa2_pcs_create(struct dpaa2_mac *mac, 252 struct fwnode_handle *dpmac_node, 253 int id) 254 { 255 struct mdio_device *mdiodev; 256 struct fwnode_handle *node; 257 258 node = fwnode_find_reference(dpmac_node, "pcs-handle", 0); 259 if (IS_ERR(node)) { 260 /* do not error out on old DTS files */ 261 netdev_warn(mac->net_dev, "pcs-handle node not found\n"); 262 return 0; 263 } 264 265 if (!fwnode_device_is_available(node)) { 266 netdev_err(mac->net_dev, "pcs-handle node not available\n"); 267 fwnode_handle_put(node); 268 return -ENODEV; 269 } 270 271 mdiodev = fwnode_mdio_find_device(node); 272 fwnode_handle_put(node); 273 if (!mdiodev) { 274 netdev_dbg(mac->net_dev, "missing PCS device\n"); 275 return -EPROBE_DEFER; 276 } 277 278 mac->pcs = lynx_pcs_create(mdiodev); 279 if (!mac->pcs) { 280 netdev_err(mac->net_dev, "lynx_pcs_create() failed\n"); 281 put_device(&mdiodev->dev); 282 return -ENOMEM; 283 } 284 285 return 0; 286 } 287 288 static void dpaa2_pcs_destroy(struct dpaa2_mac *mac) 289 { 290 struct phylink_pcs *phylink_pcs = mac->pcs; 291 292 if (phylink_pcs) { 293 struct mdio_device *mdio = lynx_get_mdio_device(phylink_pcs); 294 struct device *dev = &mdio->dev; 295 296 lynx_pcs_destroy(phylink_pcs); 297 put_device(dev); 298 mac->pcs = NULL; 299 } 300 } 301 302 static void dpaa2_mac_set_supported_interfaces(struct dpaa2_mac *mac) 303 { 304 int intf, err; 305 306 /* We support the current interface mode, and if we have a PCS 307 * similar interface modes that do not require the SerDes lane to be 308 * reconfigured. 309 */ 310 __set_bit(mac->if_mode, mac->phylink_config.supported_interfaces); 311 if (mac->pcs) { 312 switch (mac->if_mode) { 313 case PHY_INTERFACE_MODE_1000BASEX: 314 case PHY_INTERFACE_MODE_SGMII: 315 __set_bit(PHY_INTERFACE_MODE_1000BASEX, 316 mac->phylink_config.supported_interfaces); 317 __set_bit(PHY_INTERFACE_MODE_SGMII, 318 mac->phylink_config.supported_interfaces); 319 break; 320 321 default: 322 break; 323 } 324 } 325 326 if (!mac->serdes_phy) 327 return; 328 329 /* In case we have access to the SerDes phy/lane, then ask the SerDes 330 * driver what interfaces are supported based on the current PLL 331 * configuration. 332 */ 333 for (intf = 0; intf < PHY_INTERFACE_MODE_MAX; intf++) { 334 if (intf == PHY_INTERFACE_MODE_NA) 335 continue; 336 337 err = phy_validate(mac->serdes_phy, PHY_MODE_ETHERNET, intf, NULL); 338 if (err) 339 continue; 340 341 __set_bit(intf, mac->phylink_config.supported_interfaces); 342 } 343 } 344 345 void dpaa2_mac_start(struct dpaa2_mac *mac) 346 { 347 ASSERT_RTNL(); 348 349 if (mac->serdes_phy) 350 phy_power_on(mac->serdes_phy); 351 352 phylink_start(mac->phylink); 353 } 354 355 void dpaa2_mac_stop(struct dpaa2_mac *mac) 356 { 357 ASSERT_RTNL(); 358 359 phylink_stop(mac->phylink); 360 361 if (mac->serdes_phy) 362 phy_power_off(mac->serdes_phy); 363 } 364 365 int dpaa2_mac_connect(struct dpaa2_mac *mac) 366 { 367 struct net_device *net_dev = mac->net_dev; 368 struct fwnode_handle *dpmac_node; 369 struct phy *serdes_phy = NULL; 370 struct phylink *phylink; 371 int err; 372 373 mac->if_link_type = mac->attr.link_type; 374 375 dpmac_node = mac->fw_node; 376 if (!dpmac_node) { 377 netdev_err(net_dev, "No dpmac@%d node found.\n", mac->attr.id); 378 return -ENODEV; 379 } 380 381 err = dpaa2_mac_get_if_mode(dpmac_node, mac->attr); 382 if (err < 0) 383 return -EINVAL; 384 mac->if_mode = err; 385 386 if (mac->features & DPAA2_MAC_FEATURE_PROTOCOL_CHANGE && 387 !phy_interface_mode_is_rgmii(mac->if_mode) && 388 is_of_node(dpmac_node)) { 389 serdes_phy = of_phy_get(to_of_node(dpmac_node), NULL); 390 391 if (serdes_phy == ERR_PTR(-ENODEV)) 392 serdes_phy = NULL; 393 else if (IS_ERR(serdes_phy)) 394 return PTR_ERR(serdes_phy); 395 else 396 phy_init(serdes_phy); 397 } 398 mac->serdes_phy = serdes_phy; 399 400 /* The MAC does not have the capability to add RGMII delays so 401 * error out if the interface mode requests them and there is no PHY 402 * to act upon them 403 */ 404 if (of_phy_is_fixed_link(to_of_node(dpmac_node)) && 405 (mac->if_mode == PHY_INTERFACE_MODE_RGMII_ID || 406 mac->if_mode == PHY_INTERFACE_MODE_RGMII_RXID || 407 mac->if_mode == PHY_INTERFACE_MODE_RGMII_TXID)) { 408 netdev_err(net_dev, "RGMII delay not supported\n"); 409 return -EINVAL; 410 } 411 412 if ((mac->attr.link_type == DPMAC_LINK_TYPE_PHY && 413 mac->attr.eth_if != DPMAC_ETH_IF_RGMII) || 414 mac->attr.link_type == DPMAC_LINK_TYPE_BACKPLANE) { 415 err = dpaa2_pcs_create(mac, dpmac_node, mac->attr.id); 416 if (err) 417 return err; 418 } 419 420 memset(&mac->phylink_config, 0, sizeof(mac->phylink_config)); 421 mac->phylink_config.dev = &net_dev->dev; 422 mac->phylink_config.type = PHYLINK_NETDEV; 423 424 mac->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE | 425 MAC_10FD | MAC_100FD | MAC_1000FD | MAC_2500FD | MAC_5000FD | 426 MAC_10000FD | MAC_25000FD; 427 428 dpaa2_mac_set_supported_interfaces(mac); 429 430 phylink = phylink_create(&mac->phylink_config, 431 dpmac_node, mac->if_mode, 432 &dpaa2_mac_phylink_ops); 433 if (IS_ERR(phylink)) { 434 err = PTR_ERR(phylink); 435 goto err_pcs_destroy; 436 } 437 mac->phylink = phylink; 438 439 rtnl_lock(); 440 err = phylink_fwnode_phy_connect(mac->phylink, dpmac_node, 0); 441 rtnl_unlock(); 442 if (err) { 443 netdev_err(net_dev, "phylink_fwnode_phy_connect() = %d\n", err); 444 goto err_phylink_destroy; 445 } 446 447 return 0; 448 449 err_phylink_destroy: 450 phylink_destroy(mac->phylink); 451 err_pcs_destroy: 452 dpaa2_pcs_destroy(mac); 453 454 return err; 455 } 456 457 void dpaa2_mac_disconnect(struct dpaa2_mac *mac) 458 { 459 rtnl_lock(); 460 phylink_disconnect_phy(mac->phylink); 461 rtnl_unlock(); 462 463 phylink_destroy(mac->phylink); 464 dpaa2_pcs_destroy(mac); 465 of_phy_put(mac->serdes_phy); 466 mac->serdes_phy = NULL; 467 } 468 469 int dpaa2_mac_open(struct dpaa2_mac *mac) 470 { 471 struct fsl_mc_device *dpmac_dev = mac->mc_dev; 472 struct net_device *net_dev = mac->net_dev; 473 struct fwnode_handle *fw_node; 474 int err; 475 476 err = dpmac_open(mac->mc_io, 0, dpmac_dev->obj_desc.id, 477 &dpmac_dev->mc_handle); 478 if (err || !dpmac_dev->mc_handle) { 479 netdev_err(net_dev, "dpmac_open() = %d\n", err); 480 return -ENODEV; 481 } 482 483 err = dpmac_get_attributes(mac->mc_io, 0, dpmac_dev->mc_handle, 484 &mac->attr); 485 if (err) { 486 netdev_err(net_dev, "dpmac_get_attributes() = %d\n", err); 487 goto err_close_dpmac; 488 } 489 490 err = dpmac_get_api_version(mac->mc_io, 0, &mac->ver_major, &mac->ver_minor); 491 if (err) { 492 netdev_err(net_dev, "dpmac_get_api_version() = %d\n", err); 493 goto err_close_dpmac; 494 } 495 496 dpaa2_mac_detect_features(mac); 497 498 /* Find the device node representing the MAC device and link the device 499 * behind the associated netdev to it. 500 */ 501 fw_node = dpaa2_mac_get_node(&mac->mc_dev->dev, mac->attr.id); 502 if (IS_ERR(fw_node)) { 503 err = PTR_ERR(fw_node); 504 goto err_close_dpmac; 505 } 506 507 mac->fw_node = fw_node; 508 net_dev->dev.of_node = to_of_node(mac->fw_node); 509 510 return 0; 511 512 err_close_dpmac: 513 dpmac_close(mac->mc_io, 0, dpmac_dev->mc_handle); 514 return err; 515 } 516 517 void dpaa2_mac_close(struct dpaa2_mac *mac) 518 { 519 struct fsl_mc_device *dpmac_dev = mac->mc_dev; 520 521 dpmac_close(mac->mc_io, 0, dpmac_dev->mc_handle); 522 if (mac->fw_node) 523 fwnode_handle_put(mac->fw_node); 524 } 525 526 static char dpaa2_mac_ethtool_stats[][ETH_GSTRING_LEN] = { 527 [DPMAC_CNT_ING_ALL_FRAME] = "[mac] rx all frames", 528 [DPMAC_CNT_ING_GOOD_FRAME] = "[mac] rx frames ok", 529 [DPMAC_CNT_ING_ERR_FRAME] = "[mac] rx frame errors", 530 [DPMAC_CNT_ING_FRAME_DISCARD] = "[mac] rx frame discards", 531 [DPMAC_CNT_ING_UCAST_FRAME] = "[mac] rx u-cast", 532 [DPMAC_CNT_ING_BCAST_FRAME] = "[mac] rx b-cast", 533 [DPMAC_CNT_ING_MCAST_FRAME] = "[mac] rx m-cast", 534 [DPMAC_CNT_ING_FRAME_64] = "[mac] rx 64 bytes", 535 [DPMAC_CNT_ING_FRAME_127] = "[mac] rx 65-127 bytes", 536 [DPMAC_CNT_ING_FRAME_255] = "[mac] rx 128-255 bytes", 537 [DPMAC_CNT_ING_FRAME_511] = "[mac] rx 256-511 bytes", 538 [DPMAC_CNT_ING_FRAME_1023] = "[mac] rx 512-1023 bytes", 539 [DPMAC_CNT_ING_FRAME_1518] = "[mac] rx 1024-1518 bytes", 540 [DPMAC_CNT_ING_FRAME_1519_MAX] = "[mac] rx 1519-max bytes", 541 [DPMAC_CNT_ING_FRAG] = "[mac] rx frags", 542 [DPMAC_CNT_ING_JABBER] = "[mac] rx jabber", 543 [DPMAC_CNT_ING_ALIGN_ERR] = "[mac] rx align errors", 544 [DPMAC_CNT_ING_OVERSIZED] = "[mac] rx oversized", 545 [DPMAC_CNT_ING_VALID_PAUSE_FRAME] = "[mac] rx pause", 546 [DPMAC_CNT_ING_BYTE] = "[mac] rx bytes", 547 [DPMAC_CNT_EGR_GOOD_FRAME] = "[mac] tx frames ok", 548 [DPMAC_CNT_EGR_UCAST_FRAME] = "[mac] tx u-cast", 549 [DPMAC_CNT_EGR_MCAST_FRAME] = "[mac] tx m-cast", 550 [DPMAC_CNT_EGR_BCAST_FRAME] = "[mac] tx b-cast", 551 [DPMAC_CNT_EGR_ERR_FRAME] = "[mac] tx frame errors", 552 [DPMAC_CNT_EGR_UNDERSIZED] = "[mac] tx undersized", 553 [DPMAC_CNT_EGR_VALID_PAUSE_FRAME] = "[mac] tx b-pause", 554 [DPMAC_CNT_EGR_BYTE] = "[mac] tx bytes", 555 }; 556 557 #define DPAA2_MAC_NUM_STATS ARRAY_SIZE(dpaa2_mac_ethtool_stats) 558 559 int dpaa2_mac_get_sset_count(void) 560 { 561 return DPAA2_MAC_NUM_STATS; 562 } 563 564 void dpaa2_mac_get_strings(u8 *data) 565 { 566 u8 *p = data; 567 int i; 568 569 for (i = 0; i < DPAA2_MAC_NUM_STATS; i++) { 570 strscpy(p, dpaa2_mac_ethtool_stats[i], ETH_GSTRING_LEN); 571 p += ETH_GSTRING_LEN; 572 } 573 } 574 575 void dpaa2_mac_get_ethtool_stats(struct dpaa2_mac *mac, u64 *data) 576 { 577 struct fsl_mc_device *dpmac_dev = mac->mc_dev; 578 int i, err; 579 u64 value; 580 581 for (i = 0; i < DPAA2_MAC_NUM_STATS; i++) { 582 err = dpmac_get_counter(mac->mc_io, 0, dpmac_dev->mc_handle, 583 i, &value); 584 if (err) { 585 netdev_err_once(mac->net_dev, 586 "dpmac_get_counter error %d\n", err); 587 *(data + i) = U64_MAX; 588 continue; 589 } 590 *(data + i) = value; 591 } 592 } 593