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