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