1 /* 2 * Copyright (C) 2015 Cavium, Inc. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of version 2 of the GNU General Public License 6 * as published by the Free Software Foundation. 7 */ 8 9 #include <linux/acpi.h> 10 #include <linux/module.h> 11 #include <linux/interrupt.h> 12 #include <linux/pci.h> 13 #include <linux/netdevice.h> 14 #include <linux/etherdevice.h> 15 #include <linux/phy.h> 16 #include <linux/of.h> 17 #include <linux/of_mdio.h> 18 #include <linux/of_net.h> 19 20 #include "nic_reg.h" 21 #include "nic.h" 22 #include "thunder_bgx.h" 23 24 #define DRV_NAME "thunder_bgx" 25 #define DRV_VERSION "1.0" 26 27 struct lmac { 28 struct bgx *bgx; 29 int dmac; 30 u8 mac[ETH_ALEN]; 31 u8 lmac_type; 32 u8 lane_to_sds; 33 bool use_training; 34 bool autoneg; 35 bool link_up; 36 int lmacid; /* ID within BGX */ 37 int lmacid_bd; /* ID on board */ 38 struct net_device netdev; 39 struct phy_device *phydev; 40 unsigned int last_duplex; 41 unsigned int last_link; 42 unsigned int last_speed; 43 bool is_sgmii; 44 struct delayed_work dwork; 45 struct workqueue_struct *check_link; 46 }; 47 48 struct bgx { 49 u8 bgx_id; 50 struct lmac lmac[MAX_LMAC_PER_BGX]; 51 u8 lmac_count; 52 u8 max_lmac; 53 u8 acpi_lmac_idx; 54 void __iomem *reg_base; 55 struct pci_dev *pdev; 56 bool is_dlm; 57 bool is_rgx; 58 }; 59 60 static struct bgx *bgx_vnic[MAX_BGX_THUNDER]; 61 static int lmac_count; /* Total no of LMACs in system */ 62 63 static int bgx_xaui_check_link(struct lmac *lmac); 64 65 /* Supported devices */ 66 static const struct pci_device_id bgx_id_table[] = { 67 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_BGX) }, 68 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_RGX) }, 69 { 0, } /* end of table */ 70 }; 71 72 MODULE_AUTHOR("Cavium Inc"); 73 MODULE_DESCRIPTION("Cavium Thunder BGX/MAC Driver"); 74 MODULE_LICENSE("GPL v2"); 75 MODULE_VERSION(DRV_VERSION); 76 MODULE_DEVICE_TABLE(pci, bgx_id_table); 77 78 /* The Cavium ThunderX network controller can *only* be found in SoCs 79 * containing the ThunderX ARM64 CPU implementation. All accesses to the device 80 * registers on this platform are implicitly strongly ordered with respect 81 * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use 82 * with no memory barriers in this driver. The readq()/writeq() functions add 83 * explicit ordering operation which in this case are redundant, and only 84 * add overhead. 85 */ 86 87 /* Register read/write APIs */ 88 static u64 bgx_reg_read(struct bgx *bgx, u8 lmac, u64 offset) 89 { 90 void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset; 91 92 return readq_relaxed(addr); 93 } 94 95 static void bgx_reg_write(struct bgx *bgx, u8 lmac, u64 offset, u64 val) 96 { 97 void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset; 98 99 writeq_relaxed(val, addr); 100 } 101 102 static void bgx_reg_modify(struct bgx *bgx, u8 lmac, u64 offset, u64 val) 103 { 104 void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset; 105 106 writeq_relaxed(val | readq_relaxed(addr), addr); 107 } 108 109 static int bgx_poll_reg(struct bgx *bgx, u8 lmac, u64 reg, u64 mask, bool zero) 110 { 111 int timeout = 100; 112 u64 reg_val; 113 114 while (timeout) { 115 reg_val = bgx_reg_read(bgx, lmac, reg); 116 if (zero && !(reg_val & mask)) 117 return 0; 118 if (!zero && (reg_val & mask)) 119 return 0; 120 usleep_range(1000, 2000); 121 timeout--; 122 } 123 return 1; 124 } 125 126 static int max_bgx_per_node; 127 static void set_max_bgx_per_node(struct pci_dev *pdev) 128 { 129 u16 sdevid; 130 131 if (max_bgx_per_node) 132 return; 133 134 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &sdevid); 135 switch (sdevid) { 136 case PCI_SUBSYS_DEVID_81XX_BGX: 137 case PCI_SUBSYS_DEVID_81XX_RGX: 138 max_bgx_per_node = MAX_BGX_PER_CN81XX; 139 break; 140 case PCI_SUBSYS_DEVID_83XX_BGX: 141 max_bgx_per_node = MAX_BGX_PER_CN83XX; 142 break; 143 case PCI_SUBSYS_DEVID_88XX_BGX: 144 default: 145 max_bgx_per_node = MAX_BGX_PER_CN88XX; 146 break; 147 } 148 } 149 150 static struct bgx *get_bgx(int node, int bgx_idx) 151 { 152 int idx = (node * max_bgx_per_node) + bgx_idx; 153 154 return bgx_vnic[idx]; 155 } 156 157 /* Return number of BGX present in HW */ 158 unsigned bgx_get_map(int node) 159 { 160 int i; 161 unsigned map = 0; 162 163 for (i = 0; i < max_bgx_per_node; i++) { 164 if (bgx_vnic[(node * max_bgx_per_node) + i]) 165 map |= (1 << i); 166 } 167 168 return map; 169 } 170 EXPORT_SYMBOL(bgx_get_map); 171 172 /* Return number of LMAC configured for this BGX */ 173 int bgx_get_lmac_count(int node, int bgx_idx) 174 { 175 struct bgx *bgx; 176 177 bgx = get_bgx(node, bgx_idx); 178 if (bgx) 179 return bgx->lmac_count; 180 181 return 0; 182 } 183 EXPORT_SYMBOL(bgx_get_lmac_count); 184 185 /* Returns the current link status of LMAC */ 186 void bgx_get_lmac_link_state(int node, int bgx_idx, int lmacid, void *status) 187 { 188 struct bgx_link_status *link = (struct bgx_link_status *)status; 189 struct bgx *bgx; 190 struct lmac *lmac; 191 192 bgx = get_bgx(node, bgx_idx); 193 if (!bgx) 194 return; 195 196 lmac = &bgx->lmac[lmacid]; 197 link->mac_type = lmac->lmac_type; 198 link->link_up = lmac->link_up; 199 link->duplex = lmac->last_duplex; 200 link->speed = lmac->last_speed; 201 } 202 EXPORT_SYMBOL(bgx_get_lmac_link_state); 203 204 const u8 *bgx_get_lmac_mac(int node, int bgx_idx, int lmacid) 205 { 206 struct bgx *bgx = get_bgx(node, bgx_idx); 207 208 if (bgx) 209 return bgx->lmac[lmacid].mac; 210 211 return NULL; 212 } 213 EXPORT_SYMBOL(bgx_get_lmac_mac); 214 215 void bgx_set_lmac_mac(int node, int bgx_idx, int lmacid, const u8 *mac) 216 { 217 struct bgx *bgx = get_bgx(node, bgx_idx); 218 219 if (!bgx) 220 return; 221 222 ether_addr_copy(bgx->lmac[lmacid].mac, mac); 223 } 224 EXPORT_SYMBOL(bgx_set_lmac_mac); 225 226 void bgx_lmac_rx_tx_enable(int node, int bgx_idx, int lmacid, bool enable) 227 { 228 struct bgx *bgx = get_bgx(node, bgx_idx); 229 struct lmac *lmac; 230 u64 cfg; 231 232 if (!bgx) 233 return; 234 lmac = &bgx->lmac[lmacid]; 235 236 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG); 237 if (enable) 238 cfg |= CMR_PKT_RX_EN | CMR_PKT_TX_EN; 239 else 240 cfg &= ~(CMR_PKT_RX_EN | CMR_PKT_TX_EN); 241 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg); 242 243 if (bgx->is_rgx) 244 xcv_setup_link(enable ? lmac->link_up : 0, lmac->last_speed); 245 } 246 EXPORT_SYMBOL(bgx_lmac_rx_tx_enable); 247 248 /* Enables or disables timestamp insertion by BGX for Rx packets */ 249 void bgx_config_timestamping(int node, int bgx_idx, int lmacid, bool enable) 250 { 251 struct bgx *bgx = get_bgx(node, bgx_idx); 252 struct lmac *lmac; 253 u64 csr_offset, cfg; 254 255 if (!bgx) 256 return; 257 258 lmac = &bgx->lmac[lmacid]; 259 260 if (lmac->lmac_type == BGX_MODE_SGMII || 261 lmac->lmac_type == BGX_MODE_QSGMII || 262 lmac->lmac_type == BGX_MODE_RGMII) 263 csr_offset = BGX_GMP_GMI_RXX_FRM_CTL; 264 else 265 csr_offset = BGX_SMUX_RX_FRM_CTL; 266 267 cfg = bgx_reg_read(bgx, lmacid, csr_offset); 268 269 if (enable) 270 cfg |= BGX_PKT_RX_PTP_EN; 271 else 272 cfg &= ~BGX_PKT_RX_PTP_EN; 273 bgx_reg_write(bgx, lmacid, csr_offset, cfg); 274 } 275 EXPORT_SYMBOL(bgx_config_timestamping); 276 277 void bgx_lmac_get_pfc(int node, int bgx_idx, int lmacid, void *pause) 278 { 279 struct pfc *pfc = (struct pfc *)pause; 280 struct bgx *bgx = get_bgx(node, bgx_idx); 281 struct lmac *lmac; 282 u64 cfg; 283 284 if (!bgx) 285 return; 286 lmac = &bgx->lmac[lmacid]; 287 if (lmac->is_sgmii) 288 return; 289 290 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_CBFC_CTL); 291 pfc->fc_rx = cfg & RX_EN; 292 pfc->fc_tx = cfg & TX_EN; 293 pfc->autoneg = 0; 294 } 295 EXPORT_SYMBOL(bgx_lmac_get_pfc); 296 297 void bgx_lmac_set_pfc(int node, int bgx_idx, int lmacid, void *pause) 298 { 299 struct pfc *pfc = (struct pfc *)pause; 300 struct bgx *bgx = get_bgx(node, bgx_idx); 301 struct lmac *lmac; 302 u64 cfg; 303 304 if (!bgx) 305 return; 306 lmac = &bgx->lmac[lmacid]; 307 if (lmac->is_sgmii) 308 return; 309 310 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_CBFC_CTL); 311 cfg &= ~(RX_EN | TX_EN); 312 cfg |= (pfc->fc_rx ? RX_EN : 0x00); 313 cfg |= (pfc->fc_tx ? TX_EN : 0x00); 314 bgx_reg_write(bgx, lmacid, BGX_SMUX_CBFC_CTL, cfg); 315 } 316 EXPORT_SYMBOL(bgx_lmac_set_pfc); 317 318 static void bgx_sgmii_change_link_state(struct lmac *lmac) 319 { 320 struct bgx *bgx = lmac->bgx; 321 u64 cmr_cfg; 322 u64 port_cfg = 0; 323 u64 misc_ctl = 0; 324 bool tx_en, rx_en; 325 326 cmr_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_CMRX_CFG); 327 tx_en = cmr_cfg & CMR_PKT_TX_EN; 328 rx_en = cmr_cfg & CMR_PKT_RX_EN; 329 cmr_cfg &= ~(CMR_PKT_RX_EN | CMR_PKT_TX_EN); 330 bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg); 331 332 /* Wait for BGX RX to be idle */ 333 if (bgx_poll_reg(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG, 334 GMI_PORT_CFG_RX_IDLE, false)) { 335 dev_err(&bgx->pdev->dev, "BGX%d LMAC%d GMI RX not idle\n", 336 bgx->bgx_id, lmac->lmacid); 337 return; 338 } 339 340 /* Wait for BGX TX to be idle */ 341 if (bgx_poll_reg(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG, 342 GMI_PORT_CFG_TX_IDLE, false)) { 343 dev_err(&bgx->pdev->dev, "BGX%d LMAC%d GMI TX not idle\n", 344 bgx->bgx_id, lmac->lmacid); 345 return; 346 } 347 348 port_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG); 349 misc_ctl = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL); 350 351 if (lmac->link_up) { 352 misc_ctl &= ~PCS_MISC_CTL_GMX_ENO; 353 port_cfg &= ~GMI_PORT_CFG_DUPLEX; 354 port_cfg |= (lmac->last_duplex << 2); 355 } else { 356 misc_ctl |= PCS_MISC_CTL_GMX_ENO; 357 } 358 359 switch (lmac->last_speed) { 360 case 10: 361 port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */ 362 port_cfg |= GMI_PORT_CFG_SPEED_MSB; /* speed_msb 1 */ 363 port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */ 364 misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK; 365 misc_ctl |= 50; /* samp_pt */ 366 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64); 367 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0); 368 break; 369 case 100: 370 port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */ 371 port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */ 372 port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */ 373 misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK; 374 misc_ctl |= 5; /* samp_pt */ 375 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64); 376 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0); 377 break; 378 case 1000: 379 port_cfg |= GMI_PORT_CFG_SPEED; /* speed 1 */ 380 port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */ 381 port_cfg |= GMI_PORT_CFG_SLOT_TIME; /* slottime 1 */ 382 misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK; 383 misc_ctl |= 1; /* samp_pt */ 384 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 512); 385 if (lmac->last_duplex) 386 bgx_reg_write(bgx, lmac->lmacid, 387 BGX_GMP_GMI_TXX_BURST, 0); 388 else 389 bgx_reg_write(bgx, lmac->lmacid, 390 BGX_GMP_GMI_TXX_BURST, 8192); 391 break; 392 default: 393 break; 394 } 395 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL, misc_ctl); 396 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG, port_cfg); 397 398 /* Restore CMR config settings */ 399 cmr_cfg |= (rx_en ? CMR_PKT_RX_EN : 0) | (tx_en ? CMR_PKT_TX_EN : 0); 400 bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg); 401 402 if (bgx->is_rgx && (cmr_cfg & (CMR_PKT_RX_EN | CMR_PKT_TX_EN))) 403 xcv_setup_link(lmac->link_up, lmac->last_speed); 404 } 405 406 static void bgx_lmac_handler(struct net_device *netdev) 407 { 408 struct lmac *lmac = container_of(netdev, struct lmac, netdev); 409 struct phy_device *phydev; 410 int link_changed = 0; 411 412 if (!lmac) 413 return; 414 415 phydev = lmac->phydev; 416 417 if (!phydev->link && lmac->last_link) 418 link_changed = -1; 419 420 if (phydev->link && 421 (lmac->last_duplex != phydev->duplex || 422 lmac->last_link != phydev->link || 423 lmac->last_speed != phydev->speed)) { 424 link_changed = 1; 425 } 426 427 lmac->last_link = phydev->link; 428 lmac->last_speed = phydev->speed; 429 lmac->last_duplex = phydev->duplex; 430 431 if (!link_changed) 432 return; 433 434 if (link_changed > 0) 435 lmac->link_up = true; 436 else 437 lmac->link_up = false; 438 439 if (lmac->is_sgmii) 440 bgx_sgmii_change_link_state(lmac); 441 else 442 bgx_xaui_check_link(lmac); 443 } 444 445 u64 bgx_get_rx_stats(int node, int bgx_idx, int lmac, int idx) 446 { 447 struct bgx *bgx; 448 449 bgx = get_bgx(node, bgx_idx); 450 if (!bgx) 451 return 0; 452 453 if (idx > 8) 454 lmac = 0; 455 return bgx_reg_read(bgx, lmac, BGX_CMRX_RX_STAT0 + (idx * 8)); 456 } 457 EXPORT_SYMBOL(bgx_get_rx_stats); 458 459 u64 bgx_get_tx_stats(int node, int bgx_idx, int lmac, int idx) 460 { 461 struct bgx *bgx; 462 463 bgx = get_bgx(node, bgx_idx); 464 if (!bgx) 465 return 0; 466 467 return bgx_reg_read(bgx, lmac, BGX_CMRX_TX_STAT0 + (idx * 8)); 468 } 469 EXPORT_SYMBOL(bgx_get_tx_stats); 470 471 static void bgx_flush_dmac_addrs(struct bgx *bgx, int lmac) 472 { 473 u64 offset; 474 475 while (bgx->lmac[lmac].dmac > 0) { 476 offset = ((bgx->lmac[lmac].dmac - 1) * sizeof(u64)) + 477 (lmac * MAX_DMAC_PER_LMAC * sizeof(u64)); 478 bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM + offset, 0); 479 bgx->lmac[lmac].dmac--; 480 } 481 } 482 483 /* Configure BGX LMAC in internal loopback mode */ 484 void bgx_lmac_internal_loopback(int node, int bgx_idx, 485 int lmac_idx, bool enable) 486 { 487 struct bgx *bgx; 488 struct lmac *lmac; 489 u64 cfg; 490 491 bgx = get_bgx(node, bgx_idx); 492 if (!bgx) 493 return; 494 495 lmac = &bgx->lmac[lmac_idx]; 496 if (lmac->is_sgmii) { 497 cfg = bgx_reg_read(bgx, lmac_idx, BGX_GMP_PCS_MRX_CTL); 498 if (enable) 499 cfg |= PCS_MRX_CTL_LOOPBACK1; 500 else 501 cfg &= ~PCS_MRX_CTL_LOOPBACK1; 502 bgx_reg_write(bgx, lmac_idx, BGX_GMP_PCS_MRX_CTL, cfg); 503 } else { 504 cfg = bgx_reg_read(bgx, lmac_idx, BGX_SPUX_CONTROL1); 505 if (enable) 506 cfg |= SPU_CTL_LOOPBACK; 507 else 508 cfg &= ~SPU_CTL_LOOPBACK; 509 bgx_reg_write(bgx, lmac_idx, BGX_SPUX_CONTROL1, cfg); 510 } 511 } 512 EXPORT_SYMBOL(bgx_lmac_internal_loopback); 513 514 static int bgx_lmac_sgmii_init(struct bgx *bgx, struct lmac *lmac) 515 { 516 int lmacid = lmac->lmacid; 517 u64 cfg; 518 519 bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_THRESH, 0x30); 520 /* max packet size */ 521 bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_RXX_JABBER, MAX_FRAME_SIZE); 522 523 /* Disable frame alignment if using preamble */ 524 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND); 525 if (cfg & 1) 526 bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_SGMII_CTL, 0); 527 528 /* Enable lmac */ 529 bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN); 530 531 /* PCS reset */ 532 bgx_reg_modify(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, PCS_MRX_CTL_RESET); 533 if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, 534 PCS_MRX_CTL_RESET, true)) { 535 dev_err(&bgx->pdev->dev, "BGX PCS reset not completed\n"); 536 return -1; 537 } 538 539 /* power down, reset autoneg, autoneg enable */ 540 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_PCS_MRX_CTL); 541 cfg &= ~PCS_MRX_CTL_PWR_DN; 542 cfg |= PCS_MRX_CTL_RST_AN; 543 if (lmac->phydev) { 544 cfg |= PCS_MRX_CTL_AN_EN; 545 } else { 546 /* In scenarios where PHY driver is not present or it's a 547 * non-standard PHY, FW sets AN_EN to inform Linux driver 548 * to do auto-neg and link polling or not. 549 */ 550 if (cfg & PCS_MRX_CTL_AN_EN) 551 lmac->autoneg = true; 552 } 553 bgx_reg_write(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, cfg); 554 555 if (lmac->lmac_type == BGX_MODE_QSGMII) { 556 /* Disable disparity check for QSGMII */ 557 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_PCS_MISCX_CTL); 558 cfg &= ~PCS_MISC_CTL_DISP_EN; 559 bgx_reg_write(bgx, lmacid, BGX_GMP_PCS_MISCX_CTL, cfg); 560 return 0; 561 } 562 563 if ((lmac->lmac_type == BGX_MODE_SGMII) && lmac->phydev) { 564 if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_STATUS, 565 PCS_MRX_STATUS_AN_CPT, false)) { 566 dev_err(&bgx->pdev->dev, "BGX AN_CPT not completed\n"); 567 return -1; 568 } 569 } 570 571 return 0; 572 } 573 574 static int bgx_lmac_xaui_init(struct bgx *bgx, struct lmac *lmac) 575 { 576 u64 cfg; 577 int lmacid = lmac->lmacid; 578 579 /* Reset SPU */ 580 bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET); 581 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) { 582 dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n"); 583 return -1; 584 } 585 586 /* Disable LMAC */ 587 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG); 588 cfg &= ~CMR_EN; 589 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg); 590 591 bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_LOW_POWER); 592 /* Set interleaved running disparity for RXAUI */ 593 if (lmac->lmac_type == BGX_MODE_RXAUI) 594 bgx_reg_modify(bgx, lmacid, BGX_SPUX_MISC_CONTROL, 595 SPU_MISC_CTL_INTLV_RDISP); 596 597 /* Clear receive packet disable */ 598 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_MISC_CONTROL); 599 cfg &= ~SPU_MISC_CTL_RX_DIS; 600 bgx_reg_write(bgx, lmacid, BGX_SPUX_MISC_CONTROL, cfg); 601 602 /* clear all interrupts */ 603 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_RX_INT); 604 bgx_reg_write(bgx, lmacid, BGX_SMUX_RX_INT, cfg); 605 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_INT); 606 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_INT, cfg); 607 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT); 608 bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg); 609 610 if (lmac->use_training) { 611 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LP_CUP, 0x00); 612 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_CUP, 0x00); 613 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_REP, 0x00); 614 /* training enable */ 615 bgx_reg_modify(bgx, lmacid, 616 BGX_SPUX_BR_PMD_CRTL, SPU_PMD_CRTL_TRAIN_EN); 617 } 618 619 /* Append FCS to each packet */ 620 bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, SMU_TX_APPEND_FCS_D); 621 622 /* Disable forward error correction */ 623 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_FEC_CONTROL); 624 cfg &= ~SPU_FEC_CTL_FEC_EN; 625 bgx_reg_write(bgx, lmacid, BGX_SPUX_FEC_CONTROL, cfg); 626 627 /* Disable autoneg */ 628 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_CONTROL); 629 cfg = cfg & ~(SPU_AN_CTL_AN_EN | SPU_AN_CTL_XNP_EN); 630 bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_CONTROL, cfg); 631 632 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_ADV); 633 if (lmac->lmac_type == BGX_MODE_10G_KR) 634 cfg |= (1 << 23); 635 else if (lmac->lmac_type == BGX_MODE_40G_KR) 636 cfg |= (1 << 24); 637 else 638 cfg &= ~((1 << 23) | (1 << 24)); 639 cfg = cfg & (~((1ULL << 25) | (1ULL << 22) | (1ULL << 12))); 640 bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_ADV, cfg); 641 642 cfg = bgx_reg_read(bgx, 0, BGX_SPU_DBG_CONTROL); 643 cfg &= ~SPU_DBG_CTL_AN_ARB_LINK_CHK_EN; 644 bgx_reg_write(bgx, 0, BGX_SPU_DBG_CONTROL, cfg); 645 646 /* Enable lmac */ 647 bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN); 648 649 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_CONTROL1); 650 cfg &= ~SPU_CTL_LOW_POWER; 651 bgx_reg_write(bgx, lmacid, BGX_SPUX_CONTROL1, cfg); 652 653 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_CTL); 654 cfg &= ~SMU_TX_CTL_UNI_EN; 655 cfg |= SMU_TX_CTL_DIC_EN; 656 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_CTL, cfg); 657 658 /* Enable receive and transmission of pause frames */ 659 bgx_reg_write(bgx, lmacid, BGX_SMUX_CBFC_CTL, ((0xffffULL << 32) | 660 BCK_EN | DRP_EN | TX_EN | RX_EN)); 661 /* Configure pause time and interval */ 662 bgx_reg_write(bgx, lmacid, 663 BGX_SMUX_TX_PAUSE_PKT_TIME, DEFAULT_PAUSE_TIME); 664 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_PAUSE_PKT_INTERVAL); 665 cfg &= ~0xFFFFull; 666 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_PAUSE_PKT_INTERVAL, 667 cfg | (DEFAULT_PAUSE_TIME - 0x1000)); 668 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_PAUSE_ZERO, 0x01); 669 670 /* take lmac_count into account */ 671 bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_THRESH, (0x100 - 1)); 672 /* max packet size */ 673 bgx_reg_modify(bgx, lmacid, BGX_SMUX_RX_JABBER, MAX_FRAME_SIZE); 674 675 return 0; 676 } 677 678 static int bgx_xaui_check_link(struct lmac *lmac) 679 { 680 struct bgx *bgx = lmac->bgx; 681 int lmacid = lmac->lmacid; 682 int lmac_type = lmac->lmac_type; 683 u64 cfg; 684 685 if (lmac->use_training) { 686 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT); 687 if (!(cfg & (1ull << 13))) { 688 cfg = (1ull << 13) | (1ull << 14); 689 bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg); 690 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL); 691 cfg |= (1ull << 0); 692 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL, cfg); 693 return -1; 694 } 695 } 696 697 /* wait for PCS to come out of reset */ 698 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) { 699 dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n"); 700 return -1; 701 } 702 703 if ((lmac_type == BGX_MODE_10G_KR) || (lmac_type == BGX_MODE_XFI) || 704 (lmac_type == BGX_MODE_40G_KR) || (lmac_type == BGX_MODE_XLAUI)) { 705 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BR_STATUS1, 706 SPU_BR_STATUS_BLK_LOCK, false)) { 707 dev_err(&bgx->pdev->dev, 708 "SPU_BR_STATUS_BLK_LOCK not completed\n"); 709 return -1; 710 } 711 } else { 712 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BX_STATUS, 713 SPU_BX_STATUS_RX_ALIGN, false)) { 714 dev_err(&bgx->pdev->dev, 715 "SPU_BX_STATUS_RX_ALIGN not completed\n"); 716 return -1; 717 } 718 } 719 720 /* Clear rcvflt bit (latching high) and read it back */ 721 if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT) 722 bgx_reg_modify(bgx, lmacid, 723 BGX_SPUX_STATUS2, SPU_STATUS2_RCVFLT); 724 if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT) { 725 dev_err(&bgx->pdev->dev, "Receive fault, retry training\n"); 726 if (lmac->use_training) { 727 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT); 728 if (!(cfg & (1ull << 13))) { 729 cfg = (1ull << 13) | (1ull << 14); 730 bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg); 731 cfg = bgx_reg_read(bgx, lmacid, 732 BGX_SPUX_BR_PMD_CRTL); 733 cfg |= (1ull << 0); 734 bgx_reg_write(bgx, lmacid, 735 BGX_SPUX_BR_PMD_CRTL, cfg); 736 return -1; 737 } 738 } 739 return -1; 740 } 741 742 /* Wait for BGX RX to be idle */ 743 if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_RX_IDLE, false)) { 744 dev_err(&bgx->pdev->dev, "SMU RX not idle\n"); 745 return -1; 746 } 747 748 /* Wait for BGX TX to be idle */ 749 if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_TX_IDLE, false)) { 750 dev_err(&bgx->pdev->dev, "SMU TX not idle\n"); 751 return -1; 752 } 753 754 /* Check for MAC RX faults */ 755 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_RX_CTL); 756 /* 0 - Link is okay, 1 - Local fault, 2 - Remote fault */ 757 cfg &= SMU_RX_CTL_STATUS; 758 if (!cfg) 759 return 0; 760 761 /* Rx local/remote fault seen. 762 * Do lmac reinit to see if condition recovers 763 */ 764 bgx_lmac_xaui_init(bgx, lmac); 765 766 return -1; 767 } 768 769 static void bgx_poll_for_sgmii_link(struct lmac *lmac) 770 { 771 u64 pcs_link, an_result; 772 u8 speed; 773 774 pcs_link = bgx_reg_read(lmac->bgx, lmac->lmacid, 775 BGX_GMP_PCS_MRX_STATUS); 776 777 /*Link state bit is sticky, read it again*/ 778 if (!(pcs_link & PCS_MRX_STATUS_LINK)) 779 pcs_link = bgx_reg_read(lmac->bgx, lmac->lmacid, 780 BGX_GMP_PCS_MRX_STATUS); 781 782 if (bgx_poll_reg(lmac->bgx, lmac->lmacid, BGX_GMP_PCS_MRX_STATUS, 783 PCS_MRX_STATUS_AN_CPT, false)) { 784 lmac->link_up = false; 785 lmac->last_speed = SPEED_UNKNOWN; 786 lmac->last_duplex = DUPLEX_UNKNOWN; 787 goto next_poll; 788 } 789 790 lmac->link_up = ((pcs_link & PCS_MRX_STATUS_LINK) != 0) ? true : false; 791 an_result = bgx_reg_read(lmac->bgx, lmac->lmacid, 792 BGX_GMP_PCS_ANX_AN_RESULTS); 793 794 speed = (an_result >> 3) & 0x3; 795 lmac->last_duplex = (an_result >> 1) & 0x1; 796 switch (speed) { 797 case 0: 798 lmac->last_speed = 10; 799 break; 800 case 1: 801 lmac->last_speed = 100; 802 break; 803 case 2: 804 lmac->last_speed = 1000; 805 break; 806 default: 807 lmac->link_up = false; 808 lmac->last_speed = SPEED_UNKNOWN; 809 lmac->last_duplex = DUPLEX_UNKNOWN; 810 break; 811 } 812 813 next_poll: 814 815 if (lmac->last_link != lmac->link_up) { 816 if (lmac->link_up) 817 bgx_sgmii_change_link_state(lmac); 818 lmac->last_link = lmac->link_up; 819 } 820 821 queue_delayed_work(lmac->check_link, &lmac->dwork, HZ * 3); 822 } 823 824 static void bgx_poll_for_link(struct work_struct *work) 825 { 826 struct lmac *lmac; 827 u64 spu_link, smu_link; 828 829 lmac = container_of(work, struct lmac, dwork.work); 830 if (lmac->is_sgmii) { 831 bgx_poll_for_sgmii_link(lmac); 832 return; 833 } 834 835 /* Receive link is latching low. Force it high and verify it */ 836 bgx_reg_modify(lmac->bgx, lmac->lmacid, 837 BGX_SPUX_STATUS1, SPU_STATUS1_RCV_LNK); 838 bgx_poll_reg(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1, 839 SPU_STATUS1_RCV_LNK, false); 840 841 spu_link = bgx_reg_read(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1); 842 smu_link = bgx_reg_read(lmac->bgx, lmac->lmacid, BGX_SMUX_RX_CTL); 843 844 if ((spu_link & SPU_STATUS1_RCV_LNK) && 845 !(smu_link & SMU_RX_CTL_STATUS)) { 846 lmac->link_up = 1; 847 if (lmac->lmac_type == BGX_MODE_XLAUI) 848 lmac->last_speed = 40000; 849 else 850 lmac->last_speed = 10000; 851 lmac->last_duplex = 1; 852 } else { 853 lmac->link_up = 0; 854 lmac->last_speed = SPEED_UNKNOWN; 855 lmac->last_duplex = DUPLEX_UNKNOWN; 856 } 857 858 if (lmac->last_link != lmac->link_up) { 859 if (lmac->link_up) { 860 if (bgx_xaui_check_link(lmac)) { 861 /* Errors, clear link_up state */ 862 lmac->link_up = 0; 863 lmac->last_speed = SPEED_UNKNOWN; 864 lmac->last_duplex = DUPLEX_UNKNOWN; 865 } 866 } 867 lmac->last_link = lmac->link_up; 868 } 869 870 queue_delayed_work(lmac->check_link, &lmac->dwork, HZ * 2); 871 } 872 873 static int phy_interface_mode(u8 lmac_type) 874 { 875 if (lmac_type == BGX_MODE_QSGMII) 876 return PHY_INTERFACE_MODE_QSGMII; 877 if (lmac_type == BGX_MODE_RGMII) 878 return PHY_INTERFACE_MODE_RGMII; 879 880 return PHY_INTERFACE_MODE_SGMII; 881 } 882 883 static int bgx_lmac_enable(struct bgx *bgx, u8 lmacid) 884 { 885 struct lmac *lmac; 886 u64 cfg; 887 888 lmac = &bgx->lmac[lmacid]; 889 lmac->bgx = bgx; 890 891 if ((lmac->lmac_type == BGX_MODE_SGMII) || 892 (lmac->lmac_type == BGX_MODE_QSGMII) || 893 (lmac->lmac_type == BGX_MODE_RGMII)) { 894 lmac->is_sgmii = 1; 895 if (bgx_lmac_sgmii_init(bgx, lmac)) 896 return -1; 897 } else { 898 lmac->is_sgmii = 0; 899 if (bgx_lmac_xaui_init(bgx, lmac)) 900 return -1; 901 } 902 903 if (lmac->is_sgmii) { 904 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND); 905 cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */ 906 bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND, cfg); 907 bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_MIN_PKT, 60 - 1); 908 } else { 909 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_APPEND); 910 cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */ 911 bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, cfg); 912 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_MIN_PKT, 60 + 4); 913 } 914 915 /* Enable lmac */ 916 bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN); 917 918 /* Restore default cfg, incase low level firmware changed it */ 919 bgx_reg_write(bgx, lmacid, BGX_CMRX_RX_DMAC_CTL, 0x03); 920 921 if ((lmac->lmac_type != BGX_MODE_XFI) && 922 (lmac->lmac_type != BGX_MODE_XLAUI) && 923 (lmac->lmac_type != BGX_MODE_40G_KR) && 924 (lmac->lmac_type != BGX_MODE_10G_KR)) { 925 if (!lmac->phydev) { 926 if (lmac->autoneg) { 927 bgx_reg_write(bgx, lmacid, 928 BGX_GMP_PCS_LINKX_TIMER, 929 PCS_LINKX_TIMER_COUNT); 930 goto poll; 931 } else { 932 /* Default to below link speed and duplex */ 933 lmac->link_up = true; 934 lmac->last_speed = 1000; 935 lmac->last_duplex = 1; 936 bgx_sgmii_change_link_state(lmac); 937 return 0; 938 } 939 } 940 lmac->phydev->dev_flags = 0; 941 942 if (phy_connect_direct(&lmac->netdev, lmac->phydev, 943 bgx_lmac_handler, 944 phy_interface_mode(lmac->lmac_type))) 945 return -ENODEV; 946 947 phy_start_aneg(lmac->phydev); 948 return 0; 949 } 950 951 poll: 952 lmac->check_link = alloc_workqueue("check_link", WQ_UNBOUND | 953 WQ_MEM_RECLAIM, 1); 954 if (!lmac->check_link) 955 return -ENOMEM; 956 INIT_DELAYED_WORK(&lmac->dwork, bgx_poll_for_link); 957 queue_delayed_work(lmac->check_link, &lmac->dwork, 0); 958 959 return 0; 960 } 961 962 static void bgx_lmac_disable(struct bgx *bgx, u8 lmacid) 963 { 964 struct lmac *lmac; 965 u64 cfg; 966 967 lmac = &bgx->lmac[lmacid]; 968 if (lmac->check_link) { 969 /* Destroy work queue */ 970 cancel_delayed_work_sync(&lmac->dwork); 971 destroy_workqueue(lmac->check_link); 972 } 973 974 /* Disable packet reception */ 975 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG); 976 cfg &= ~CMR_PKT_RX_EN; 977 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg); 978 979 /* Give chance for Rx/Tx FIFO to get drained */ 980 bgx_poll_reg(bgx, lmacid, BGX_CMRX_RX_FIFO_LEN, (u64)0x1FFF, true); 981 bgx_poll_reg(bgx, lmacid, BGX_CMRX_TX_FIFO_LEN, (u64)0x3FFF, true); 982 983 /* Disable packet transmission */ 984 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG); 985 cfg &= ~CMR_PKT_TX_EN; 986 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg); 987 988 /* Disable serdes lanes */ 989 if (!lmac->is_sgmii) 990 bgx_reg_modify(bgx, lmacid, 991 BGX_SPUX_CONTROL1, SPU_CTL_LOW_POWER); 992 else 993 bgx_reg_modify(bgx, lmacid, 994 BGX_GMP_PCS_MRX_CTL, PCS_MRX_CTL_PWR_DN); 995 996 /* Disable LMAC */ 997 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG); 998 cfg &= ~CMR_EN; 999 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg); 1000 1001 bgx_flush_dmac_addrs(bgx, lmacid); 1002 1003 if ((lmac->lmac_type != BGX_MODE_XFI) && 1004 (lmac->lmac_type != BGX_MODE_XLAUI) && 1005 (lmac->lmac_type != BGX_MODE_40G_KR) && 1006 (lmac->lmac_type != BGX_MODE_10G_KR) && lmac->phydev) 1007 phy_disconnect(lmac->phydev); 1008 1009 lmac->phydev = NULL; 1010 } 1011 1012 static void bgx_init_hw(struct bgx *bgx) 1013 { 1014 int i; 1015 struct lmac *lmac; 1016 1017 bgx_reg_modify(bgx, 0, BGX_CMR_GLOBAL_CFG, CMR_GLOBAL_CFG_FCS_STRIP); 1018 if (bgx_reg_read(bgx, 0, BGX_CMR_BIST_STATUS)) 1019 dev_err(&bgx->pdev->dev, "BGX%d BIST failed\n", bgx->bgx_id); 1020 1021 /* Set lmac type and lane2serdes mapping */ 1022 for (i = 0; i < bgx->lmac_count; i++) { 1023 lmac = &bgx->lmac[i]; 1024 bgx_reg_write(bgx, i, BGX_CMRX_CFG, 1025 (lmac->lmac_type << 8) | lmac->lane_to_sds); 1026 bgx->lmac[i].lmacid_bd = lmac_count; 1027 lmac_count++; 1028 } 1029 1030 bgx_reg_write(bgx, 0, BGX_CMR_TX_LMACS, bgx->lmac_count); 1031 bgx_reg_write(bgx, 0, BGX_CMR_RX_LMACS, bgx->lmac_count); 1032 1033 /* Set the backpressure AND mask */ 1034 for (i = 0; i < bgx->lmac_count; i++) 1035 bgx_reg_modify(bgx, 0, BGX_CMR_CHAN_MSK_AND, 1036 ((1ULL << MAX_BGX_CHANS_PER_LMAC) - 1) << 1037 (i * MAX_BGX_CHANS_PER_LMAC)); 1038 1039 /* Disable all MAC filtering */ 1040 for (i = 0; i < RX_DMAC_COUNT; i++) 1041 bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM + (i * 8), 0x00); 1042 1043 /* Disable MAC steering (NCSI traffic) */ 1044 for (i = 0; i < RX_TRAFFIC_STEER_RULE_COUNT; i++) 1045 bgx_reg_write(bgx, 0, BGX_CMR_RX_STREERING + (i * 8), 0x00); 1046 } 1047 1048 static u8 bgx_get_lane2sds_cfg(struct bgx *bgx, struct lmac *lmac) 1049 { 1050 return (u8)(bgx_reg_read(bgx, lmac->lmacid, BGX_CMRX_CFG) & 0xFF); 1051 } 1052 1053 static void bgx_print_qlm_mode(struct bgx *bgx, u8 lmacid) 1054 { 1055 struct device *dev = &bgx->pdev->dev; 1056 struct lmac *lmac; 1057 char str[27]; 1058 1059 if (!bgx->is_dlm && lmacid) 1060 return; 1061 1062 lmac = &bgx->lmac[lmacid]; 1063 if (!bgx->is_dlm) 1064 sprintf(str, "BGX%d QLM mode", bgx->bgx_id); 1065 else 1066 sprintf(str, "BGX%d LMAC%d mode", bgx->bgx_id, lmacid); 1067 1068 switch (lmac->lmac_type) { 1069 case BGX_MODE_SGMII: 1070 dev_info(dev, "%s: SGMII\n", (char *)str); 1071 break; 1072 case BGX_MODE_XAUI: 1073 dev_info(dev, "%s: XAUI\n", (char *)str); 1074 break; 1075 case BGX_MODE_RXAUI: 1076 dev_info(dev, "%s: RXAUI\n", (char *)str); 1077 break; 1078 case BGX_MODE_XFI: 1079 if (!lmac->use_training) 1080 dev_info(dev, "%s: XFI\n", (char *)str); 1081 else 1082 dev_info(dev, "%s: 10G_KR\n", (char *)str); 1083 break; 1084 case BGX_MODE_XLAUI: 1085 if (!lmac->use_training) 1086 dev_info(dev, "%s: XLAUI\n", (char *)str); 1087 else 1088 dev_info(dev, "%s: 40G_KR4\n", (char *)str); 1089 break; 1090 case BGX_MODE_QSGMII: 1091 dev_info(dev, "%s: QSGMII\n", (char *)str); 1092 break; 1093 case BGX_MODE_RGMII: 1094 dev_info(dev, "%s: RGMII\n", (char *)str); 1095 break; 1096 case BGX_MODE_INVALID: 1097 /* Nothing to do */ 1098 break; 1099 } 1100 } 1101 1102 static void lmac_set_lane2sds(struct bgx *bgx, struct lmac *lmac) 1103 { 1104 switch (lmac->lmac_type) { 1105 case BGX_MODE_SGMII: 1106 case BGX_MODE_XFI: 1107 lmac->lane_to_sds = lmac->lmacid; 1108 break; 1109 case BGX_MODE_XAUI: 1110 case BGX_MODE_XLAUI: 1111 case BGX_MODE_RGMII: 1112 lmac->lane_to_sds = 0xE4; 1113 break; 1114 case BGX_MODE_RXAUI: 1115 lmac->lane_to_sds = (lmac->lmacid) ? 0xE : 0x4; 1116 break; 1117 case BGX_MODE_QSGMII: 1118 /* There is no way to determine if DLM0/2 is QSGMII or 1119 * DLM1/3 is configured to QSGMII as bootloader will 1120 * configure all LMACs, so take whatever is configured 1121 * by low level firmware. 1122 */ 1123 lmac->lane_to_sds = bgx_get_lane2sds_cfg(bgx, lmac); 1124 break; 1125 default: 1126 lmac->lane_to_sds = 0; 1127 break; 1128 } 1129 } 1130 1131 static void lmac_set_training(struct bgx *bgx, struct lmac *lmac, int lmacid) 1132 { 1133 if ((lmac->lmac_type != BGX_MODE_10G_KR) && 1134 (lmac->lmac_type != BGX_MODE_40G_KR)) { 1135 lmac->use_training = 0; 1136 return; 1137 } 1138 1139 lmac->use_training = bgx_reg_read(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL) & 1140 SPU_PMD_CRTL_TRAIN_EN; 1141 } 1142 1143 static void bgx_set_lmac_config(struct bgx *bgx, u8 idx) 1144 { 1145 struct lmac *lmac; 1146 u64 cmr_cfg; 1147 u8 lmac_type; 1148 u8 lane_to_sds; 1149 1150 lmac = &bgx->lmac[idx]; 1151 1152 if (!bgx->is_dlm || bgx->is_rgx) { 1153 /* Read LMAC0 type to figure out QLM mode 1154 * This is configured by low level firmware 1155 */ 1156 cmr_cfg = bgx_reg_read(bgx, 0, BGX_CMRX_CFG); 1157 lmac->lmac_type = (cmr_cfg >> 8) & 0x07; 1158 if (bgx->is_rgx) 1159 lmac->lmac_type = BGX_MODE_RGMII; 1160 lmac_set_training(bgx, lmac, 0); 1161 lmac_set_lane2sds(bgx, lmac); 1162 return; 1163 } 1164 1165 /* For DLMs or SLMs on 80/81/83xx so many lane configurations 1166 * are possible and vary across boards. Also Kernel doesn't have 1167 * any way to identify board type/info and since firmware does, 1168 * just take lmac type and serdes lane config as is. 1169 */ 1170 cmr_cfg = bgx_reg_read(bgx, idx, BGX_CMRX_CFG); 1171 lmac_type = (u8)((cmr_cfg >> 8) & 0x07); 1172 lane_to_sds = (u8)(cmr_cfg & 0xFF); 1173 /* Check if config is reset value */ 1174 if ((lmac_type == 0) && (lane_to_sds == 0xE4)) 1175 lmac->lmac_type = BGX_MODE_INVALID; 1176 else 1177 lmac->lmac_type = lmac_type; 1178 lmac->lane_to_sds = lane_to_sds; 1179 lmac_set_training(bgx, lmac, lmac->lmacid); 1180 } 1181 1182 static void bgx_get_qlm_mode(struct bgx *bgx) 1183 { 1184 struct lmac *lmac; 1185 u8 idx; 1186 1187 /* Init all LMAC's type to invalid */ 1188 for (idx = 0; idx < bgx->max_lmac; idx++) { 1189 lmac = &bgx->lmac[idx]; 1190 lmac->lmacid = idx; 1191 lmac->lmac_type = BGX_MODE_INVALID; 1192 lmac->use_training = false; 1193 } 1194 1195 /* It is assumed that low level firmware sets this value */ 1196 bgx->lmac_count = bgx_reg_read(bgx, 0, BGX_CMR_RX_LMACS) & 0x7; 1197 if (bgx->lmac_count > bgx->max_lmac) 1198 bgx->lmac_count = bgx->max_lmac; 1199 1200 for (idx = 0; idx < bgx->lmac_count; idx++) { 1201 bgx_set_lmac_config(bgx, idx); 1202 bgx_print_qlm_mode(bgx, idx); 1203 } 1204 } 1205 1206 #ifdef CONFIG_ACPI 1207 1208 static int acpi_get_mac_address(struct device *dev, struct acpi_device *adev, 1209 u8 *dst) 1210 { 1211 u8 mac[ETH_ALEN]; 1212 int ret; 1213 1214 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev), 1215 "mac-address", mac, ETH_ALEN); 1216 if (ret) 1217 goto out; 1218 1219 if (!is_valid_ether_addr(mac)) { 1220 dev_err(dev, "MAC address invalid: %pM\n", mac); 1221 ret = -EINVAL; 1222 goto out; 1223 } 1224 1225 dev_info(dev, "MAC address set to: %pM\n", mac); 1226 1227 memcpy(dst, mac, ETH_ALEN); 1228 out: 1229 return ret; 1230 } 1231 1232 /* Currently only sets the MAC address. */ 1233 static acpi_status bgx_acpi_register_phy(acpi_handle handle, 1234 u32 lvl, void *context, void **rv) 1235 { 1236 struct bgx *bgx = context; 1237 struct device *dev = &bgx->pdev->dev; 1238 struct acpi_device *adev; 1239 1240 if (acpi_bus_get_device(handle, &adev)) 1241 goto out; 1242 1243 acpi_get_mac_address(dev, adev, bgx->lmac[bgx->acpi_lmac_idx].mac); 1244 1245 SET_NETDEV_DEV(&bgx->lmac[bgx->acpi_lmac_idx].netdev, dev); 1246 1247 bgx->lmac[bgx->acpi_lmac_idx].lmacid = bgx->acpi_lmac_idx; 1248 bgx->acpi_lmac_idx++; /* move to next LMAC */ 1249 out: 1250 return AE_OK; 1251 } 1252 1253 static acpi_status bgx_acpi_match_id(acpi_handle handle, u32 lvl, 1254 void *context, void **ret_val) 1255 { 1256 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL }; 1257 struct bgx *bgx = context; 1258 char bgx_sel[5]; 1259 1260 snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id); 1261 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &string))) { 1262 pr_warn("Invalid link device\n"); 1263 return AE_OK; 1264 } 1265 1266 if (strncmp(string.pointer, bgx_sel, 4)) 1267 return AE_OK; 1268 1269 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, 1270 bgx_acpi_register_phy, NULL, bgx, NULL); 1271 1272 kfree(string.pointer); 1273 return AE_CTRL_TERMINATE; 1274 } 1275 1276 static int bgx_init_acpi_phy(struct bgx *bgx) 1277 { 1278 acpi_get_devices(NULL, bgx_acpi_match_id, bgx, (void **)NULL); 1279 return 0; 1280 } 1281 1282 #else 1283 1284 static int bgx_init_acpi_phy(struct bgx *bgx) 1285 { 1286 return -ENODEV; 1287 } 1288 1289 #endif /* CONFIG_ACPI */ 1290 1291 #if IS_ENABLED(CONFIG_OF_MDIO) 1292 1293 static int bgx_init_of_phy(struct bgx *bgx) 1294 { 1295 struct fwnode_handle *fwn; 1296 struct device_node *node = NULL; 1297 u8 lmac = 0; 1298 1299 device_for_each_child_node(&bgx->pdev->dev, fwn) { 1300 struct phy_device *pd; 1301 struct device_node *phy_np; 1302 const char *mac; 1303 1304 /* Should always be an OF node. But if it is not, we 1305 * cannot handle it, so exit the loop. 1306 */ 1307 node = to_of_node(fwn); 1308 if (!node) 1309 break; 1310 1311 mac = of_get_mac_address(node); 1312 if (mac) 1313 ether_addr_copy(bgx->lmac[lmac].mac, mac); 1314 1315 SET_NETDEV_DEV(&bgx->lmac[lmac].netdev, &bgx->pdev->dev); 1316 bgx->lmac[lmac].lmacid = lmac; 1317 1318 phy_np = of_parse_phandle(node, "phy-handle", 0); 1319 /* If there is no phy or defective firmware presents 1320 * this cortina phy, for which there is no driver 1321 * support, ignore it. 1322 */ 1323 if (phy_np && 1324 !of_device_is_compatible(phy_np, "cortina,cs4223-slice")) { 1325 /* Wait until the phy drivers are available */ 1326 pd = of_phy_find_device(phy_np); 1327 if (!pd) 1328 goto defer; 1329 bgx->lmac[lmac].phydev = pd; 1330 } 1331 1332 lmac++; 1333 if (lmac == bgx->max_lmac) { 1334 of_node_put(node); 1335 break; 1336 } 1337 } 1338 return 0; 1339 1340 defer: 1341 /* We are bailing out, try not to leak device reference counts 1342 * for phy devices we may have already found. 1343 */ 1344 while (lmac) { 1345 if (bgx->lmac[lmac].phydev) { 1346 put_device(&bgx->lmac[lmac].phydev->mdio.dev); 1347 bgx->lmac[lmac].phydev = NULL; 1348 } 1349 lmac--; 1350 } 1351 of_node_put(node); 1352 return -EPROBE_DEFER; 1353 } 1354 1355 #else 1356 1357 static int bgx_init_of_phy(struct bgx *bgx) 1358 { 1359 return -ENODEV; 1360 } 1361 1362 #endif /* CONFIG_OF_MDIO */ 1363 1364 static int bgx_init_phy(struct bgx *bgx) 1365 { 1366 if (!acpi_disabled) 1367 return bgx_init_acpi_phy(bgx); 1368 1369 return bgx_init_of_phy(bgx); 1370 } 1371 1372 static int bgx_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 1373 { 1374 int err; 1375 struct device *dev = &pdev->dev; 1376 struct bgx *bgx = NULL; 1377 u8 lmac; 1378 u16 sdevid; 1379 1380 bgx = devm_kzalloc(dev, sizeof(*bgx), GFP_KERNEL); 1381 if (!bgx) 1382 return -ENOMEM; 1383 bgx->pdev = pdev; 1384 1385 pci_set_drvdata(pdev, bgx); 1386 1387 err = pci_enable_device(pdev); 1388 if (err) { 1389 dev_err(dev, "Failed to enable PCI device\n"); 1390 pci_set_drvdata(pdev, NULL); 1391 return err; 1392 } 1393 1394 err = pci_request_regions(pdev, DRV_NAME); 1395 if (err) { 1396 dev_err(dev, "PCI request regions failed 0x%x\n", err); 1397 goto err_disable_device; 1398 } 1399 1400 /* MAP configuration registers */ 1401 bgx->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0); 1402 if (!bgx->reg_base) { 1403 dev_err(dev, "BGX: Cannot map CSR memory space, aborting\n"); 1404 err = -ENOMEM; 1405 goto err_release_regions; 1406 } 1407 1408 set_max_bgx_per_node(pdev); 1409 1410 pci_read_config_word(pdev, PCI_DEVICE_ID, &sdevid); 1411 if (sdevid != PCI_DEVICE_ID_THUNDER_RGX) { 1412 bgx->bgx_id = (pci_resource_start(pdev, 1413 PCI_CFG_REG_BAR_NUM) >> 24) & BGX_ID_MASK; 1414 bgx->bgx_id += nic_get_node_id(pdev) * max_bgx_per_node; 1415 bgx->max_lmac = MAX_LMAC_PER_BGX; 1416 bgx_vnic[bgx->bgx_id] = bgx; 1417 } else { 1418 bgx->is_rgx = true; 1419 bgx->max_lmac = 1; 1420 bgx->bgx_id = MAX_BGX_PER_CN81XX - 1; 1421 bgx_vnic[bgx->bgx_id] = bgx; 1422 xcv_init_hw(); 1423 } 1424 1425 /* On 81xx all are DLMs and on 83xx there are 3 BGX QLMs and one 1426 * BGX i.e BGX2 can be split across 2 DLMs. 1427 */ 1428 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &sdevid); 1429 if ((sdevid == PCI_SUBSYS_DEVID_81XX_BGX) || 1430 ((sdevid == PCI_SUBSYS_DEVID_83XX_BGX) && (bgx->bgx_id == 2))) 1431 bgx->is_dlm = true; 1432 1433 bgx_get_qlm_mode(bgx); 1434 1435 err = bgx_init_phy(bgx); 1436 if (err) 1437 goto err_enable; 1438 1439 bgx_init_hw(bgx); 1440 1441 /* Enable all LMACs */ 1442 for (lmac = 0; lmac < bgx->lmac_count; lmac++) { 1443 err = bgx_lmac_enable(bgx, lmac); 1444 if (err) { 1445 dev_err(dev, "BGX%d failed to enable lmac%d\n", 1446 bgx->bgx_id, lmac); 1447 while (lmac) 1448 bgx_lmac_disable(bgx, --lmac); 1449 goto err_enable; 1450 } 1451 } 1452 1453 return 0; 1454 1455 err_enable: 1456 bgx_vnic[bgx->bgx_id] = NULL; 1457 err_release_regions: 1458 pci_release_regions(pdev); 1459 err_disable_device: 1460 pci_disable_device(pdev); 1461 pci_set_drvdata(pdev, NULL); 1462 return err; 1463 } 1464 1465 static void bgx_remove(struct pci_dev *pdev) 1466 { 1467 struct bgx *bgx = pci_get_drvdata(pdev); 1468 u8 lmac; 1469 1470 /* Disable all LMACs */ 1471 for (lmac = 0; lmac < bgx->lmac_count; lmac++) 1472 bgx_lmac_disable(bgx, lmac); 1473 1474 bgx_vnic[bgx->bgx_id] = NULL; 1475 pci_release_regions(pdev); 1476 pci_disable_device(pdev); 1477 pci_set_drvdata(pdev, NULL); 1478 } 1479 1480 static struct pci_driver bgx_driver = { 1481 .name = DRV_NAME, 1482 .id_table = bgx_id_table, 1483 .probe = bgx_probe, 1484 .remove = bgx_remove, 1485 }; 1486 1487 static int __init bgx_init_module(void) 1488 { 1489 pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION); 1490 1491 return pci_register_driver(&bgx_driver); 1492 } 1493 1494 static void __exit bgx_cleanup_module(void) 1495 { 1496 pci_unregister_driver(&bgx_driver); 1497 } 1498 1499 module_init(bgx_init_module); 1500 module_exit(bgx_cleanup_module); 1501