1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2 /* Copyright 2017-2019 NXP */ 3 4 #include <linux/mdio.h> 5 #include <linux/module.h> 6 #include <linux/fsl/enetc_mdio.h> 7 #include <linux/of_mdio.h> 8 #include <linux/of_net.h> 9 #include "enetc_pf.h" 10 11 #define ENETC_DRV_NAME_STR "ENETC PF driver" 12 13 static void enetc_pf_get_primary_mac_addr(struct enetc_hw *hw, int si, u8 *addr) 14 { 15 u32 upper = __raw_readl(hw->port + ENETC_PSIPMAR0(si)); 16 u16 lower = __raw_readw(hw->port + ENETC_PSIPMAR1(si)); 17 18 *(u32 *)addr = upper; 19 *(u16 *)(addr + 4) = lower; 20 } 21 22 static void enetc_pf_set_primary_mac_addr(struct enetc_hw *hw, int si, 23 const u8 *addr) 24 { 25 u32 upper = *(const u32 *)addr; 26 u16 lower = *(const u16 *)(addr + 4); 27 28 __raw_writel(upper, hw->port + ENETC_PSIPMAR0(si)); 29 __raw_writew(lower, hw->port + ENETC_PSIPMAR1(si)); 30 } 31 32 static int enetc_pf_set_mac_addr(struct net_device *ndev, void *addr) 33 { 34 struct enetc_ndev_priv *priv = netdev_priv(ndev); 35 struct sockaddr *saddr = addr; 36 37 if (!is_valid_ether_addr(saddr->sa_data)) 38 return -EADDRNOTAVAIL; 39 40 memcpy(ndev->dev_addr, saddr->sa_data, ndev->addr_len); 41 enetc_pf_set_primary_mac_addr(&priv->si->hw, 0, saddr->sa_data); 42 43 return 0; 44 } 45 46 static void enetc_set_vlan_promisc(struct enetc_hw *hw, char si_map) 47 { 48 u32 val = enetc_port_rd(hw, ENETC_PSIPVMR); 49 50 val &= ~ENETC_PSIPVMR_SET_VP(ENETC_VLAN_PROMISC_MAP_ALL); 51 enetc_port_wr(hw, ENETC_PSIPVMR, ENETC_PSIPVMR_SET_VP(si_map) | val); 52 } 53 54 static void enetc_enable_si_vlan_promisc(struct enetc_pf *pf, int si_idx) 55 { 56 pf->vlan_promisc_simap |= BIT(si_idx); 57 enetc_set_vlan_promisc(&pf->si->hw, pf->vlan_promisc_simap); 58 } 59 60 static void enetc_disable_si_vlan_promisc(struct enetc_pf *pf, int si_idx) 61 { 62 pf->vlan_promisc_simap &= ~BIT(si_idx); 63 enetc_set_vlan_promisc(&pf->si->hw, pf->vlan_promisc_simap); 64 } 65 66 static void enetc_set_isol_vlan(struct enetc_hw *hw, int si, u16 vlan, u8 qos) 67 { 68 u32 val = 0; 69 70 if (vlan) 71 val = ENETC_PSIVLAN_EN | ENETC_PSIVLAN_SET_QOS(qos) | vlan; 72 73 enetc_port_wr(hw, ENETC_PSIVLANR(si), val); 74 } 75 76 static int enetc_mac_addr_hash_idx(const u8 *addr) 77 { 78 u64 fold = __swab64(ether_addr_to_u64(addr)) >> 16; 79 u64 mask = 0; 80 int res = 0; 81 int i; 82 83 for (i = 0; i < 8; i++) 84 mask |= BIT_ULL(i * 6); 85 86 for (i = 0; i < 6; i++) 87 res |= (hweight64(fold & (mask << i)) & 0x1) << i; 88 89 return res; 90 } 91 92 static void enetc_reset_mac_addr_filter(struct enetc_mac_filter *filter) 93 { 94 filter->mac_addr_cnt = 0; 95 96 bitmap_zero(filter->mac_hash_table, 97 ENETC_MADDR_HASH_TBL_SZ); 98 } 99 100 static void enetc_add_mac_addr_em_filter(struct enetc_mac_filter *filter, 101 const unsigned char *addr) 102 { 103 /* add exact match addr */ 104 ether_addr_copy(filter->mac_addr, addr); 105 filter->mac_addr_cnt++; 106 } 107 108 static void enetc_add_mac_addr_ht_filter(struct enetc_mac_filter *filter, 109 const unsigned char *addr) 110 { 111 int idx = enetc_mac_addr_hash_idx(addr); 112 113 /* add hash table entry */ 114 __set_bit(idx, filter->mac_hash_table); 115 filter->mac_addr_cnt++; 116 } 117 118 static void enetc_clear_mac_ht_flt(struct enetc_si *si, int si_idx, int type) 119 { 120 bool err = si->errata & ENETC_ERR_UCMCSWP; 121 122 if (type == UC) { 123 enetc_port_wr(&si->hw, ENETC_PSIUMHFR0(si_idx, err), 0); 124 enetc_port_wr(&si->hw, ENETC_PSIUMHFR1(si_idx), 0); 125 } else { /* MC */ 126 enetc_port_wr(&si->hw, ENETC_PSIMMHFR0(si_idx, err), 0); 127 enetc_port_wr(&si->hw, ENETC_PSIMMHFR1(si_idx), 0); 128 } 129 } 130 131 static void enetc_set_mac_ht_flt(struct enetc_si *si, int si_idx, int type, 132 u32 *hash) 133 { 134 bool err = si->errata & ENETC_ERR_UCMCSWP; 135 136 if (type == UC) { 137 enetc_port_wr(&si->hw, ENETC_PSIUMHFR0(si_idx, err), *hash); 138 enetc_port_wr(&si->hw, ENETC_PSIUMHFR1(si_idx), *(hash + 1)); 139 } else { /* MC */ 140 enetc_port_wr(&si->hw, ENETC_PSIMMHFR0(si_idx, err), *hash); 141 enetc_port_wr(&si->hw, ENETC_PSIMMHFR1(si_idx), *(hash + 1)); 142 } 143 } 144 145 static void enetc_sync_mac_filters(struct enetc_pf *pf) 146 { 147 struct enetc_mac_filter *f = pf->mac_filter; 148 struct enetc_si *si = pf->si; 149 int i, pos; 150 151 pos = EMETC_MAC_ADDR_FILT_RES; 152 153 for (i = 0; i < MADDR_TYPE; i++, f++) { 154 bool em = (f->mac_addr_cnt == 1) && (i == UC); 155 bool clear = !f->mac_addr_cnt; 156 157 if (clear) { 158 if (i == UC) 159 enetc_clear_mac_flt_entry(si, pos); 160 161 enetc_clear_mac_ht_flt(si, 0, i); 162 continue; 163 } 164 165 /* exact match filter */ 166 if (em) { 167 int err; 168 169 enetc_clear_mac_ht_flt(si, 0, UC); 170 171 err = enetc_set_mac_flt_entry(si, pos, f->mac_addr, 172 BIT(0)); 173 if (!err) 174 continue; 175 176 /* fallback to HT filtering */ 177 dev_warn(&si->pdev->dev, "fallback to HT filt (%d)\n", 178 err); 179 } 180 181 /* hash table filter, clear EM filter for UC entries */ 182 if (i == UC) 183 enetc_clear_mac_flt_entry(si, pos); 184 185 enetc_set_mac_ht_flt(si, 0, i, (u32 *)f->mac_hash_table); 186 } 187 } 188 189 static void enetc_pf_set_rx_mode(struct net_device *ndev) 190 { 191 struct enetc_ndev_priv *priv = netdev_priv(ndev); 192 struct enetc_pf *pf = enetc_si_priv(priv->si); 193 char vlan_promisc_simap = pf->vlan_promisc_simap; 194 struct enetc_hw *hw = &priv->si->hw; 195 bool uprom = false, mprom = false; 196 struct enetc_mac_filter *filter; 197 struct netdev_hw_addr *ha; 198 u32 psipmr = 0; 199 bool em; 200 201 if (ndev->flags & IFF_PROMISC) { 202 /* enable promisc mode for SI0 (PF) */ 203 psipmr = ENETC_PSIPMR_SET_UP(0) | ENETC_PSIPMR_SET_MP(0); 204 uprom = true; 205 mprom = true; 206 /* Enable VLAN promiscuous mode for SI0 (PF) */ 207 vlan_promisc_simap |= BIT(0); 208 } else if (ndev->flags & IFF_ALLMULTI) { 209 /* enable multi cast promisc mode for SI0 (PF) */ 210 psipmr = ENETC_PSIPMR_SET_MP(0); 211 mprom = true; 212 } 213 214 enetc_set_vlan_promisc(&pf->si->hw, vlan_promisc_simap); 215 216 /* first 2 filter entries belong to PF */ 217 if (!uprom) { 218 /* Update unicast filters */ 219 filter = &pf->mac_filter[UC]; 220 enetc_reset_mac_addr_filter(filter); 221 222 em = (netdev_uc_count(ndev) == 1); 223 netdev_for_each_uc_addr(ha, ndev) { 224 if (em) { 225 enetc_add_mac_addr_em_filter(filter, ha->addr); 226 break; 227 } 228 229 enetc_add_mac_addr_ht_filter(filter, ha->addr); 230 } 231 } 232 233 if (!mprom) { 234 /* Update multicast filters */ 235 filter = &pf->mac_filter[MC]; 236 enetc_reset_mac_addr_filter(filter); 237 238 netdev_for_each_mc_addr(ha, ndev) { 239 if (!is_multicast_ether_addr(ha->addr)) 240 continue; 241 242 enetc_add_mac_addr_ht_filter(filter, ha->addr); 243 } 244 } 245 246 if (!uprom || !mprom) 247 /* update PF entries */ 248 enetc_sync_mac_filters(pf); 249 250 psipmr |= enetc_port_rd(hw, ENETC_PSIPMR) & 251 ~(ENETC_PSIPMR_SET_UP(0) | ENETC_PSIPMR_SET_MP(0)); 252 enetc_port_wr(hw, ENETC_PSIPMR, psipmr); 253 } 254 255 static void enetc_set_vlan_ht_filter(struct enetc_hw *hw, int si_idx, 256 u32 *hash) 257 { 258 enetc_port_wr(hw, ENETC_PSIVHFR0(si_idx), *hash); 259 enetc_port_wr(hw, ENETC_PSIVHFR1(si_idx), *(hash + 1)); 260 } 261 262 static int enetc_vid_hash_idx(unsigned int vid) 263 { 264 int res = 0; 265 int i; 266 267 for (i = 0; i < 6; i++) 268 res |= (hweight8(vid & (BIT(i) | BIT(i + 6))) & 0x1) << i; 269 270 return res; 271 } 272 273 static void enetc_sync_vlan_ht_filter(struct enetc_pf *pf, bool rehash) 274 { 275 int i; 276 277 if (rehash) { 278 bitmap_zero(pf->vlan_ht_filter, ENETC_VLAN_HT_SIZE); 279 280 for_each_set_bit(i, pf->active_vlans, VLAN_N_VID) { 281 int hidx = enetc_vid_hash_idx(i); 282 283 __set_bit(hidx, pf->vlan_ht_filter); 284 } 285 } 286 287 enetc_set_vlan_ht_filter(&pf->si->hw, 0, (u32 *)pf->vlan_ht_filter); 288 } 289 290 static int enetc_vlan_rx_add_vid(struct net_device *ndev, __be16 prot, u16 vid) 291 { 292 struct enetc_ndev_priv *priv = netdev_priv(ndev); 293 struct enetc_pf *pf = enetc_si_priv(priv->si); 294 int idx; 295 296 __set_bit(vid, pf->active_vlans); 297 298 idx = enetc_vid_hash_idx(vid); 299 if (!__test_and_set_bit(idx, pf->vlan_ht_filter)) 300 enetc_sync_vlan_ht_filter(pf, false); 301 302 return 0; 303 } 304 305 static int enetc_vlan_rx_del_vid(struct net_device *ndev, __be16 prot, u16 vid) 306 { 307 struct enetc_ndev_priv *priv = netdev_priv(ndev); 308 struct enetc_pf *pf = enetc_si_priv(priv->si); 309 310 __clear_bit(vid, pf->active_vlans); 311 enetc_sync_vlan_ht_filter(pf, true); 312 313 return 0; 314 } 315 316 static void enetc_set_loopback(struct net_device *ndev, bool en) 317 { 318 struct enetc_ndev_priv *priv = netdev_priv(ndev); 319 struct enetc_hw *hw = &priv->si->hw; 320 u32 reg; 321 322 reg = enetc_port_rd(hw, ENETC_PM0_IF_MODE); 323 if (reg & ENETC_PMO_IFM_RG) { 324 /* RGMII mode */ 325 reg = (reg & ~ENETC_PM0_IFM_RLP) | 326 (en ? ENETC_PM0_IFM_RLP : 0); 327 enetc_port_wr(hw, ENETC_PM0_IF_MODE, reg); 328 } else { 329 /* assume SGMII mode */ 330 reg = enetc_port_rd(hw, ENETC_PM0_CMD_CFG); 331 reg = (reg & ~ENETC_PM0_CMD_XGLP) | 332 (en ? ENETC_PM0_CMD_XGLP : 0); 333 reg = (reg & ~ENETC_PM0_CMD_PHY_TX_EN) | 334 (en ? ENETC_PM0_CMD_PHY_TX_EN : 0); 335 enetc_port_wr(hw, ENETC_PM0_CMD_CFG, reg); 336 enetc_port_wr(hw, ENETC_PM1_CMD_CFG, reg); 337 } 338 } 339 340 static int enetc_pf_set_vf_mac(struct net_device *ndev, int vf, u8 *mac) 341 { 342 struct enetc_ndev_priv *priv = netdev_priv(ndev); 343 struct enetc_pf *pf = enetc_si_priv(priv->si); 344 struct enetc_vf_state *vf_state; 345 346 if (vf >= pf->total_vfs) 347 return -EINVAL; 348 349 if (!is_valid_ether_addr(mac)) 350 return -EADDRNOTAVAIL; 351 352 vf_state = &pf->vf_state[vf]; 353 vf_state->flags |= ENETC_VF_FLAG_PF_SET_MAC; 354 enetc_pf_set_primary_mac_addr(&priv->si->hw, vf + 1, mac); 355 return 0; 356 } 357 358 static int enetc_pf_set_vf_vlan(struct net_device *ndev, int vf, u16 vlan, 359 u8 qos, __be16 proto) 360 { 361 struct enetc_ndev_priv *priv = netdev_priv(ndev); 362 struct enetc_pf *pf = enetc_si_priv(priv->si); 363 364 if (priv->si->errata & ENETC_ERR_VLAN_ISOL) 365 return -EOPNOTSUPP; 366 367 if (vf >= pf->total_vfs) 368 return -EINVAL; 369 370 if (proto != htons(ETH_P_8021Q)) 371 /* only C-tags supported for now */ 372 return -EPROTONOSUPPORT; 373 374 enetc_set_isol_vlan(&priv->si->hw, vf + 1, vlan, qos); 375 return 0; 376 } 377 378 static int enetc_pf_set_vf_spoofchk(struct net_device *ndev, int vf, bool en) 379 { 380 struct enetc_ndev_priv *priv = netdev_priv(ndev); 381 struct enetc_pf *pf = enetc_si_priv(priv->si); 382 u32 cfgr; 383 384 if (vf >= pf->total_vfs) 385 return -EINVAL; 386 387 cfgr = enetc_port_rd(&priv->si->hw, ENETC_PSICFGR0(vf + 1)); 388 cfgr = (cfgr & ~ENETC_PSICFGR0_ASE) | (en ? ENETC_PSICFGR0_ASE : 0); 389 enetc_port_wr(&priv->si->hw, ENETC_PSICFGR0(vf + 1), cfgr); 390 391 return 0; 392 } 393 394 static void enetc_port_setup_primary_mac_address(struct enetc_si *si) 395 { 396 unsigned char mac_addr[MAX_ADDR_LEN]; 397 struct enetc_pf *pf = enetc_si_priv(si); 398 struct enetc_hw *hw = &si->hw; 399 int i; 400 401 /* check MAC addresses for PF and all VFs, if any is 0 set it ro rand */ 402 for (i = 0; i < pf->total_vfs + 1; i++) { 403 enetc_pf_get_primary_mac_addr(hw, i, mac_addr); 404 if (!is_zero_ether_addr(mac_addr)) 405 continue; 406 eth_random_addr(mac_addr); 407 dev_info(&si->pdev->dev, "no MAC address specified for SI%d, using %pM\n", 408 i, mac_addr); 409 enetc_pf_set_primary_mac_addr(hw, i, mac_addr); 410 } 411 } 412 413 static void enetc_port_assign_rfs_entries(struct enetc_si *si) 414 { 415 struct enetc_pf *pf = enetc_si_priv(si); 416 struct enetc_hw *hw = &si->hw; 417 int num_entries, vf_entries, i; 418 u32 val; 419 420 /* split RFS entries between functions */ 421 val = enetc_port_rd(hw, ENETC_PRFSCAPR); 422 num_entries = ENETC_PRFSCAPR_GET_NUM_RFS(val); 423 vf_entries = num_entries / (pf->total_vfs + 1); 424 425 for (i = 0; i < pf->total_vfs; i++) 426 enetc_port_wr(hw, ENETC_PSIRFSCFGR(i + 1), vf_entries); 427 enetc_port_wr(hw, ENETC_PSIRFSCFGR(0), 428 num_entries - vf_entries * pf->total_vfs); 429 430 /* enable RFS on port */ 431 enetc_port_wr(hw, ENETC_PRFSMR, ENETC_PRFSMR_RFSE); 432 } 433 434 static void enetc_port_si_configure(struct enetc_si *si) 435 { 436 struct enetc_pf *pf = enetc_si_priv(si); 437 struct enetc_hw *hw = &si->hw; 438 int num_rings, i; 439 u32 val; 440 441 val = enetc_port_rd(hw, ENETC_PCAPR0); 442 num_rings = min(ENETC_PCAPR0_RXBDR(val), ENETC_PCAPR0_TXBDR(val)); 443 444 val = ENETC_PSICFGR0_SET_TXBDR(ENETC_PF_NUM_RINGS); 445 val |= ENETC_PSICFGR0_SET_RXBDR(ENETC_PF_NUM_RINGS); 446 447 if (unlikely(num_rings < ENETC_PF_NUM_RINGS)) { 448 val = ENETC_PSICFGR0_SET_TXBDR(num_rings); 449 val |= ENETC_PSICFGR0_SET_RXBDR(num_rings); 450 451 dev_warn(&si->pdev->dev, "Found %d rings, expected %d!\n", 452 num_rings, ENETC_PF_NUM_RINGS); 453 454 num_rings = 0; 455 } 456 457 /* Add default one-time settings for SI0 (PF) */ 458 val |= ENETC_PSICFGR0_SIVC(ENETC_VLAN_TYPE_C | ENETC_VLAN_TYPE_S); 459 460 enetc_port_wr(hw, ENETC_PSICFGR0(0), val); 461 462 if (num_rings) 463 num_rings -= ENETC_PF_NUM_RINGS; 464 465 /* Configure the SIs for each available VF */ 466 val = ENETC_PSICFGR0_SIVC(ENETC_VLAN_TYPE_C | ENETC_VLAN_TYPE_S); 467 val |= ENETC_PSICFGR0_VTE | ENETC_PSICFGR0_SIVIE; 468 469 if (num_rings) { 470 num_rings /= pf->total_vfs; 471 val |= ENETC_PSICFGR0_SET_TXBDR(num_rings); 472 val |= ENETC_PSICFGR0_SET_RXBDR(num_rings); 473 } 474 475 for (i = 0; i < pf->total_vfs; i++) 476 enetc_port_wr(hw, ENETC_PSICFGR0(i + 1), val); 477 478 /* Port level VLAN settings */ 479 val = ENETC_PVCLCTR_OVTPIDL(ENETC_VLAN_TYPE_C | ENETC_VLAN_TYPE_S); 480 enetc_port_wr(hw, ENETC_PVCLCTR, val); 481 /* use outer tag for VLAN filtering */ 482 enetc_port_wr(hw, ENETC_PSIVLANFMR, ENETC_PSIVLANFMR_VS); 483 } 484 485 static void enetc_configure_port_mac(struct enetc_hw *hw) 486 { 487 enetc_port_wr(hw, ENETC_PM0_MAXFRM, 488 ENETC_SET_MAXFRM(ENETC_RX_MAXFRM_SIZE)); 489 490 enetc_port_wr(hw, ENETC_PTCMSDUR(0), ENETC_MAC_MAXFRM_SIZE); 491 enetc_port_wr(hw, ENETC_PTXMBAR, 2 * ENETC_MAC_MAXFRM_SIZE); 492 493 enetc_port_wr(hw, ENETC_PM0_CMD_CFG, ENETC_PM0_CMD_PHY_TX_EN | 494 ENETC_PM0_CMD_TXP | ENETC_PM0_PROMISC); 495 496 enetc_port_wr(hw, ENETC_PM1_CMD_CFG, ENETC_PM0_CMD_PHY_TX_EN | 497 ENETC_PM0_CMD_TXP | ENETC_PM0_PROMISC); 498 } 499 500 static void enetc_mac_config(struct enetc_hw *hw, phy_interface_t phy_mode) 501 { 502 /* set auto-speed for RGMII */ 503 if (enetc_port_rd(hw, ENETC_PM0_IF_MODE) & ENETC_PMO_IFM_RG || 504 phy_interface_mode_is_rgmii(phy_mode)) 505 enetc_port_wr(hw, ENETC_PM0_IF_MODE, ENETC_PM0_IFM_RGAUTO); 506 507 if (phy_mode == PHY_INTERFACE_MODE_USXGMII) 508 enetc_port_wr(hw, ENETC_PM0_IF_MODE, ENETC_PM0_IFM_XGMII); 509 } 510 511 static void enetc_mac_enable(struct enetc_hw *hw, bool en) 512 { 513 u32 val = enetc_port_rd(hw, ENETC_PM0_CMD_CFG); 514 515 val &= ~(ENETC_PM0_TX_EN | ENETC_PM0_RX_EN); 516 val |= en ? (ENETC_PM0_TX_EN | ENETC_PM0_RX_EN) : 0; 517 518 enetc_port_wr(hw, ENETC_PM0_CMD_CFG, val); 519 enetc_port_wr(hw, ENETC_PM1_CMD_CFG, val); 520 } 521 522 static void enetc_configure_port_pmac(struct enetc_hw *hw) 523 { 524 u32 temp; 525 526 /* Set pMAC step lock */ 527 temp = enetc_port_rd(hw, ENETC_PFPMR); 528 enetc_port_wr(hw, ENETC_PFPMR, 529 temp | ENETC_PFPMR_PMACE | ENETC_PFPMR_MWLM); 530 531 temp = enetc_port_rd(hw, ENETC_MMCSR); 532 enetc_port_wr(hw, ENETC_MMCSR, temp | ENETC_MMCSR_ME); 533 } 534 535 static void enetc_configure_port(struct enetc_pf *pf) 536 { 537 u8 hash_key[ENETC_RSSHASH_KEY_SIZE]; 538 struct enetc_hw *hw = &pf->si->hw; 539 540 enetc_configure_port_pmac(hw); 541 542 enetc_configure_port_mac(hw); 543 544 enetc_port_si_configure(pf->si); 545 546 /* set up hash key */ 547 get_random_bytes(hash_key, ENETC_RSSHASH_KEY_SIZE); 548 enetc_set_rss_key(hw, hash_key); 549 550 /* split up RFS entries */ 551 enetc_port_assign_rfs_entries(pf->si); 552 553 /* fix-up primary MAC addresses, if not set already */ 554 enetc_port_setup_primary_mac_address(pf->si); 555 556 /* enforce VLAN promisc mode for all SIs */ 557 pf->vlan_promisc_simap = ENETC_VLAN_PROMISC_MAP_ALL; 558 enetc_set_vlan_promisc(hw, pf->vlan_promisc_simap); 559 560 enetc_port_wr(hw, ENETC_PSIPMR, 0); 561 562 /* enable port */ 563 enetc_port_wr(hw, ENETC_PMR, ENETC_PMR_EN); 564 } 565 566 /* Messaging */ 567 static u16 enetc_msg_pf_set_vf_primary_mac_addr(struct enetc_pf *pf, 568 int vf_id) 569 { 570 struct enetc_vf_state *vf_state = &pf->vf_state[vf_id]; 571 struct enetc_msg_swbd *msg = &pf->rxmsg[vf_id]; 572 struct enetc_msg_cmd_set_primary_mac *cmd; 573 struct device *dev = &pf->si->pdev->dev; 574 u16 cmd_id; 575 char *addr; 576 577 cmd = (struct enetc_msg_cmd_set_primary_mac *)msg->vaddr; 578 cmd_id = cmd->header.id; 579 if (cmd_id != ENETC_MSG_CMD_MNG_ADD) 580 return ENETC_MSG_CMD_STATUS_FAIL; 581 582 addr = cmd->mac.sa_data; 583 if (vf_state->flags & ENETC_VF_FLAG_PF_SET_MAC) 584 dev_warn(dev, "Attempt to override PF set mac addr for VF%d\n", 585 vf_id); 586 else 587 enetc_pf_set_primary_mac_addr(&pf->si->hw, vf_id + 1, addr); 588 589 return ENETC_MSG_CMD_STATUS_OK; 590 } 591 592 void enetc_msg_handle_rxmsg(struct enetc_pf *pf, int vf_id, u16 *status) 593 { 594 struct enetc_msg_swbd *msg = &pf->rxmsg[vf_id]; 595 struct device *dev = &pf->si->pdev->dev; 596 struct enetc_msg_cmd_header *cmd_hdr; 597 u16 cmd_type; 598 599 *status = ENETC_MSG_CMD_STATUS_OK; 600 cmd_hdr = (struct enetc_msg_cmd_header *)msg->vaddr; 601 cmd_type = cmd_hdr->type; 602 603 switch (cmd_type) { 604 case ENETC_MSG_CMD_MNG_MAC: 605 *status = enetc_msg_pf_set_vf_primary_mac_addr(pf, vf_id); 606 break; 607 default: 608 dev_err(dev, "command not supported (cmd_type: 0x%x)\n", 609 cmd_type); 610 } 611 } 612 613 #ifdef CONFIG_PCI_IOV 614 static int enetc_sriov_configure(struct pci_dev *pdev, int num_vfs) 615 { 616 struct enetc_si *si = pci_get_drvdata(pdev); 617 struct enetc_pf *pf = enetc_si_priv(si); 618 int err; 619 620 if (!num_vfs) { 621 enetc_msg_psi_free(pf); 622 kfree(pf->vf_state); 623 pf->num_vfs = 0; 624 pci_disable_sriov(pdev); 625 } else { 626 pf->num_vfs = num_vfs; 627 628 pf->vf_state = kcalloc(num_vfs, sizeof(struct enetc_vf_state), 629 GFP_KERNEL); 630 if (!pf->vf_state) { 631 pf->num_vfs = 0; 632 return -ENOMEM; 633 } 634 635 err = enetc_msg_psi_init(pf); 636 if (err) { 637 dev_err(&pdev->dev, "enetc_msg_psi_init (%d)\n", err); 638 goto err_msg_psi; 639 } 640 641 err = pci_enable_sriov(pdev, num_vfs); 642 if (err) { 643 dev_err(&pdev->dev, "pci_enable_sriov err %d\n", err); 644 goto err_en_sriov; 645 } 646 } 647 648 return num_vfs; 649 650 err_en_sriov: 651 enetc_msg_psi_free(pf); 652 err_msg_psi: 653 kfree(pf->vf_state); 654 pf->num_vfs = 0; 655 656 return err; 657 } 658 #else 659 #define enetc_sriov_configure(pdev, num_vfs) (void)0 660 #endif 661 662 static int enetc_pf_set_features(struct net_device *ndev, 663 netdev_features_t features) 664 { 665 netdev_features_t changed = ndev->features ^ features; 666 struct enetc_ndev_priv *priv = netdev_priv(ndev); 667 668 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) { 669 struct enetc_pf *pf = enetc_si_priv(priv->si); 670 671 if (!!(features & NETIF_F_HW_VLAN_CTAG_FILTER)) 672 enetc_disable_si_vlan_promisc(pf, 0); 673 else 674 enetc_enable_si_vlan_promisc(pf, 0); 675 } 676 677 if (changed & NETIF_F_LOOPBACK) 678 enetc_set_loopback(ndev, !!(features & NETIF_F_LOOPBACK)); 679 680 return enetc_set_features(ndev, features); 681 } 682 683 static const struct net_device_ops enetc_ndev_ops = { 684 .ndo_open = enetc_open, 685 .ndo_stop = enetc_close, 686 .ndo_start_xmit = enetc_xmit, 687 .ndo_get_stats = enetc_get_stats, 688 .ndo_set_mac_address = enetc_pf_set_mac_addr, 689 .ndo_set_rx_mode = enetc_pf_set_rx_mode, 690 .ndo_vlan_rx_add_vid = enetc_vlan_rx_add_vid, 691 .ndo_vlan_rx_kill_vid = enetc_vlan_rx_del_vid, 692 .ndo_set_vf_mac = enetc_pf_set_vf_mac, 693 .ndo_set_vf_vlan = enetc_pf_set_vf_vlan, 694 .ndo_set_vf_spoofchk = enetc_pf_set_vf_spoofchk, 695 .ndo_set_features = enetc_pf_set_features, 696 .ndo_do_ioctl = enetc_ioctl, 697 .ndo_setup_tc = enetc_setup_tc, 698 }; 699 700 static void enetc_pf_netdev_setup(struct enetc_si *si, struct net_device *ndev, 701 const struct net_device_ops *ndev_ops) 702 { 703 struct enetc_ndev_priv *priv = netdev_priv(ndev); 704 705 SET_NETDEV_DEV(ndev, &si->pdev->dev); 706 priv->ndev = ndev; 707 priv->si = si; 708 priv->dev = &si->pdev->dev; 709 si->ndev = ndev; 710 711 priv->msg_enable = (NETIF_MSG_WOL << 1) - 1; 712 ndev->netdev_ops = ndev_ops; 713 enetc_set_ethtool_ops(ndev); 714 ndev->watchdog_timeo = 5 * HZ; 715 ndev->max_mtu = ENETC_MAX_MTU; 716 717 ndev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM | NETIF_F_HW_CSUM | 718 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | 719 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_LOOPBACK; 720 ndev->features = NETIF_F_HIGHDMA | NETIF_F_SG | 721 NETIF_F_RXCSUM | NETIF_F_HW_CSUM | 722 NETIF_F_HW_VLAN_CTAG_TX | 723 NETIF_F_HW_VLAN_CTAG_RX; 724 725 if (si->num_rss) 726 ndev->hw_features |= NETIF_F_RXHASH; 727 728 if (si->errata & ENETC_ERR_TXCSUM) { 729 ndev->hw_features &= ~NETIF_F_HW_CSUM; 730 ndev->features &= ~NETIF_F_HW_CSUM; 731 } 732 733 ndev->priv_flags |= IFF_UNICAST_FLT; 734 735 if (si->hw_features & ENETC_SI_F_QBV) 736 priv->active_offloads |= ENETC_F_QBV; 737 738 if (si->hw_features & ENETC_SI_F_PSFP && !enetc_psfp_enable(priv)) { 739 priv->active_offloads |= ENETC_F_QCI; 740 ndev->features |= NETIF_F_HW_TC; 741 ndev->hw_features |= NETIF_F_HW_TC; 742 } 743 744 /* pick up primary MAC address from SI */ 745 enetc_get_primary_mac_addr(&si->hw, ndev->dev_addr); 746 } 747 748 static int enetc_mdio_probe(struct enetc_pf *pf, struct device_node *np) 749 { 750 struct device *dev = &pf->si->pdev->dev; 751 struct enetc_mdio_priv *mdio_priv; 752 struct mii_bus *bus; 753 int err; 754 755 bus = devm_mdiobus_alloc_size(dev, sizeof(*mdio_priv)); 756 if (!bus) 757 return -ENOMEM; 758 759 bus->name = "Freescale ENETC MDIO Bus"; 760 bus->read = enetc_mdio_read; 761 bus->write = enetc_mdio_write; 762 bus->parent = dev; 763 mdio_priv = bus->priv; 764 mdio_priv->hw = &pf->si->hw; 765 mdio_priv->mdio_base = ENETC_EMDIO_BASE; 766 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev)); 767 768 err = of_mdiobus_register(bus, np); 769 if (err) { 770 dev_err(dev, "cannot register MDIO bus\n"); 771 return err; 772 } 773 774 pf->mdio = bus; 775 776 return 0; 777 } 778 779 static void enetc_mdio_remove(struct enetc_pf *pf) 780 { 781 if (pf->mdio) 782 mdiobus_unregister(pf->mdio); 783 } 784 785 static int enetc_imdio_create(struct enetc_pf *pf) 786 { 787 struct device *dev = &pf->si->pdev->dev; 788 struct enetc_mdio_priv *mdio_priv; 789 struct lynx_pcs *pcs_lynx; 790 struct mdio_device *pcs; 791 struct mii_bus *bus; 792 int err; 793 794 bus = mdiobus_alloc_size(sizeof(*mdio_priv)); 795 if (!bus) 796 return -ENOMEM; 797 798 bus->name = "Freescale ENETC internal MDIO Bus"; 799 bus->read = enetc_mdio_read; 800 bus->write = enetc_mdio_write; 801 bus->parent = dev; 802 bus->phy_mask = ~0; 803 mdio_priv = bus->priv; 804 mdio_priv->hw = &pf->si->hw; 805 mdio_priv->mdio_base = ENETC_PM_IMDIO_BASE; 806 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-imdio", dev_name(dev)); 807 808 err = mdiobus_register(bus); 809 if (err) { 810 dev_err(dev, "cannot register internal MDIO bus (%d)\n", err); 811 goto free_mdio_bus; 812 } 813 814 pcs = mdio_device_create(bus, 0); 815 if (IS_ERR(pcs)) { 816 err = PTR_ERR(pcs); 817 dev_err(dev, "cannot create pcs (%d)\n", err); 818 goto unregister_mdiobus; 819 } 820 821 pcs_lynx = lynx_pcs_create(pcs); 822 if (!pcs_lynx) { 823 mdio_device_free(pcs); 824 err = -ENOMEM; 825 dev_err(dev, "cannot create lynx pcs (%d)\n", err); 826 goto unregister_mdiobus; 827 } 828 829 pf->imdio = bus; 830 pf->pcs = pcs_lynx; 831 832 return 0; 833 834 unregister_mdiobus: 835 mdiobus_unregister(bus); 836 free_mdio_bus: 837 mdiobus_free(bus); 838 return err; 839 } 840 841 static void enetc_imdio_remove(struct enetc_pf *pf) 842 { 843 if (pf->pcs) { 844 mdio_device_free(pf->pcs->mdio); 845 lynx_pcs_destroy(pf->pcs); 846 } 847 if (pf->imdio) { 848 mdiobus_unregister(pf->imdio); 849 mdiobus_free(pf->imdio); 850 } 851 } 852 853 static bool enetc_port_has_pcs(struct enetc_pf *pf) 854 { 855 return (pf->if_mode == PHY_INTERFACE_MODE_SGMII || 856 pf->if_mode == PHY_INTERFACE_MODE_2500BASEX || 857 pf->if_mode == PHY_INTERFACE_MODE_USXGMII); 858 } 859 860 static int enetc_mdiobus_create(struct enetc_pf *pf) 861 { 862 struct device *dev = &pf->si->pdev->dev; 863 struct device_node *mdio_np; 864 int err; 865 866 mdio_np = of_get_child_by_name(dev->of_node, "mdio"); 867 if (mdio_np) { 868 err = enetc_mdio_probe(pf, mdio_np); 869 870 of_node_put(mdio_np); 871 if (err) 872 return err; 873 } 874 875 if (enetc_port_has_pcs(pf)) { 876 err = enetc_imdio_create(pf); 877 if (err) { 878 enetc_mdio_remove(pf); 879 return err; 880 } 881 } 882 883 return 0; 884 } 885 886 static void enetc_mdiobus_destroy(struct enetc_pf *pf) 887 { 888 enetc_mdio_remove(pf); 889 enetc_imdio_remove(pf); 890 } 891 892 static void enetc_pl_mac_validate(struct phylink_config *config, 893 unsigned long *supported, 894 struct phylink_link_state *state) 895 { 896 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 897 898 if (state->interface != PHY_INTERFACE_MODE_NA && 899 state->interface != PHY_INTERFACE_MODE_INTERNAL && 900 state->interface != PHY_INTERFACE_MODE_SGMII && 901 state->interface != PHY_INTERFACE_MODE_2500BASEX && 902 state->interface != PHY_INTERFACE_MODE_USXGMII && 903 !phy_interface_mode_is_rgmii(state->interface)) { 904 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 905 return; 906 } 907 908 phylink_set_port_modes(mask); 909 phylink_set(mask, Autoneg); 910 phylink_set(mask, Pause); 911 phylink_set(mask, Asym_Pause); 912 phylink_set(mask, 10baseT_Half); 913 phylink_set(mask, 10baseT_Full); 914 phylink_set(mask, 100baseT_Half); 915 phylink_set(mask, 100baseT_Full); 916 phylink_set(mask, 100baseT_Half); 917 phylink_set(mask, 1000baseT_Half); 918 phylink_set(mask, 1000baseT_Full); 919 920 if (state->interface == PHY_INTERFACE_MODE_INTERNAL || 921 state->interface == PHY_INTERFACE_MODE_2500BASEX || 922 state->interface == PHY_INTERFACE_MODE_USXGMII) { 923 phylink_set(mask, 2500baseT_Full); 924 phylink_set(mask, 2500baseX_Full); 925 } 926 927 bitmap_and(supported, supported, mask, 928 __ETHTOOL_LINK_MODE_MASK_NBITS); 929 bitmap_and(state->advertising, state->advertising, mask, 930 __ETHTOOL_LINK_MODE_MASK_NBITS); 931 } 932 933 static void enetc_pl_mac_config(struct phylink_config *config, 934 unsigned int mode, 935 const struct phylink_link_state *state) 936 { 937 struct enetc_pf *pf = phylink_to_enetc_pf(config); 938 struct enetc_ndev_priv *priv; 939 940 enetc_mac_config(&pf->si->hw, state->interface); 941 942 priv = netdev_priv(pf->si->ndev); 943 if (pf->pcs) 944 phylink_set_pcs(priv->phylink, &pf->pcs->pcs); 945 } 946 947 static void enetc_pl_mac_link_up(struct phylink_config *config, 948 struct phy_device *phy, unsigned int mode, 949 phy_interface_t interface, int speed, 950 int duplex, bool tx_pause, bool rx_pause) 951 { 952 struct enetc_pf *pf = phylink_to_enetc_pf(config); 953 struct enetc_ndev_priv *priv; 954 955 priv = netdev_priv(pf->si->ndev); 956 if (priv->active_offloads & ENETC_F_QBV) 957 enetc_sched_speed_set(priv, speed); 958 959 enetc_mac_enable(&pf->si->hw, true); 960 } 961 962 static void enetc_pl_mac_link_down(struct phylink_config *config, 963 unsigned int mode, 964 phy_interface_t interface) 965 { 966 struct enetc_pf *pf = phylink_to_enetc_pf(config); 967 968 enetc_mac_enable(&pf->si->hw, false); 969 } 970 971 static const struct phylink_mac_ops enetc_mac_phylink_ops = { 972 .validate = enetc_pl_mac_validate, 973 .mac_config = enetc_pl_mac_config, 974 .mac_link_up = enetc_pl_mac_link_up, 975 .mac_link_down = enetc_pl_mac_link_down, 976 }; 977 978 static int enetc_phylink_create(struct enetc_ndev_priv *priv) 979 { 980 struct enetc_pf *pf = enetc_si_priv(priv->si); 981 struct device *dev = &pf->si->pdev->dev; 982 struct phylink *phylink; 983 int err; 984 985 pf->phylink_config.dev = &priv->ndev->dev; 986 pf->phylink_config.type = PHYLINK_NETDEV; 987 988 phylink = phylink_create(&pf->phylink_config, 989 of_fwnode_handle(dev->of_node), 990 pf->if_mode, &enetc_mac_phylink_ops); 991 if (IS_ERR(phylink)) { 992 err = PTR_ERR(phylink); 993 return err; 994 } 995 996 priv->phylink = phylink; 997 998 return 0; 999 } 1000 1001 static void enetc_phylink_destroy(struct enetc_ndev_priv *priv) 1002 { 1003 if (priv->phylink) 1004 phylink_destroy(priv->phylink); 1005 } 1006 1007 static int enetc_pf_probe(struct pci_dev *pdev, 1008 const struct pci_device_id *ent) 1009 { 1010 struct enetc_ndev_priv *priv; 1011 struct net_device *ndev; 1012 struct enetc_si *si; 1013 struct enetc_pf *pf; 1014 int err; 1015 1016 if (pdev->dev.of_node && !of_device_is_available(pdev->dev.of_node)) { 1017 dev_info(&pdev->dev, "device is disabled, skipping\n"); 1018 return -ENODEV; 1019 } 1020 1021 err = enetc_pci_probe(pdev, KBUILD_MODNAME, sizeof(*pf)); 1022 if (err) { 1023 dev_err(&pdev->dev, "PCI probing failed\n"); 1024 return err; 1025 } 1026 1027 si = pci_get_drvdata(pdev); 1028 if (!si->hw.port || !si->hw.global) { 1029 err = -ENODEV; 1030 dev_err(&pdev->dev, "could not map PF space, probing a VF?\n"); 1031 goto err_map_pf_space; 1032 } 1033 1034 pf = enetc_si_priv(si); 1035 pf->si = si; 1036 pf->total_vfs = pci_sriov_get_totalvfs(pdev); 1037 1038 enetc_configure_port(pf); 1039 1040 enetc_get_si_caps(si); 1041 1042 ndev = alloc_etherdev_mq(sizeof(*priv), ENETC_MAX_NUM_TXQS); 1043 if (!ndev) { 1044 err = -ENOMEM; 1045 dev_err(&pdev->dev, "netdev creation failed\n"); 1046 goto err_alloc_netdev; 1047 } 1048 1049 enetc_pf_netdev_setup(si, ndev, &enetc_ndev_ops); 1050 1051 priv = netdev_priv(ndev); 1052 1053 enetc_init_si_rings_params(priv); 1054 1055 err = enetc_alloc_si_resources(priv); 1056 if (err) { 1057 dev_err(&pdev->dev, "SI resource alloc failed\n"); 1058 goto err_alloc_si_res; 1059 } 1060 1061 err = enetc_alloc_msix(priv); 1062 if (err) { 1063 dev_err(&pdev->dev, "MSIX alloc failed\n"); 1064 goto err_alloc_msix; 1065 } 1066 1067 if (!of_get_phy_mode(pdev->dev.of_node, &pf->if_mode)) { 1068 err = enetc_mdiobus_create(pf); 1069 if (err) 1070 goto err_mdiobus_create; 1071 1072 err = enetc_phylink_create(priv); 1073 if (err) 1074 goto err_phylink_create; 1075 } 1076 1077 err = register_netdev(ndev); 1078 if (err) 1079 goto err_reg_netdev; 1080 1081 return 0; 1082 1083 err_reg_netdev: 1084 enetc_phylink_destroy(priv); 1085 err_phylink_create: 1086 enetc_mdiobus_destroy(pf); 1087 err_mdiobus_create: 1088 enetc_free_msix(priv); 1089 err_alloc_msix: 1090 enetc_free_si_resources(priv); 1091 err_alloc_si_res: 1092 si->ndev = NULL; 1093 free_netdev(ndev); 1094 err_alloc_netdev: 1095 err_map_pf_space: 1096 enetc_pci_remove(pdev); 1097 1098 return err; 1099 } 1100 1101 static void enetc_pf_remove(struct pci_dev *pdev) 1102 { 1103 struct enetc_si *si = pci_get_drvdata(pdev); 1104 struct enetc_pf *pf = enetc_si_priv(si); 1105 struct enetc_ndev_priv *priv; 1106 1107 priv = netdev_priv(si->ndev); 1108 enetc_phylink_destroy(priv); 1109 enetc_mdiobus_destroy(pf); 1110 1111 if (pf->num_vfs) 1112 enetc_sriov_configure(pdev, 0); 1113 1114 unregister_netdev(si->ndev); 1115 1116 enetc_free_msix(priv); 1117 1118 enetc_free_si_resources(priv); 1119 1120 free_netdev(si->ndev); 1121 1122 enetc_pci_remove(pdev); 1123 } 1124 1125 static const struct pci_device_id enetc_pf_id_table[] = { 1126 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, ENETC_DEV_ID_PF) }, 1127 { 0, } /* End of table. */ 1128 }; 1129 MODULE_DEVICE_TABLE(pci, enetc_pf_id_table); 1130 1131 static struct pci_driver enetc_pf_driver = { 1132 .name = KBUILD_MODNAME, 1133 .id_table = enetc_pf_id_table, 1134 .probe = enetc_pf_probe, 1135 .remove = enetc_pf_remove, 1136 #ifdef CONFIG_PCI_IOV 1137 .sriov_configure = enetc_sriov_configure, 1138 #endif 1139 }; 1140 module_pci_driver(enetc_pf_driver); 1141 1142 MODULE_DESCRIPTION(ENETC_DRV_NAME_STR); 1143 MODULE_LICENSE("Dual BSD/GPL"); 1144