xref: /linux/drivers/net/ethernet/freescale/enetc/enetc_pf.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2017-2019 NXP */
3 
4 #include <linux/unaligned.h>
5 #include <linux/module.h>
6 #include <linux/of.h>
7 #include <linux/of_platform.h>
8 #include <linux/of_net.h>
9 #include <linux/pcs-lynx.h>
10 #include "enetc_ierb.h"
11 #include "enetc_pf_common.h"
12 
13 #define ENETC_DRV_NAME_STR "ENETC PF driver"
14 
15 static void enetc_pf_get_primary_mac_addr(struct enetc_hw *hw, int si, u8 *addr)
16 {
17 	u32 upper = __raw_readl(hw->port + ENETC_PSIPMAR0(si));
18 	u16 lower = __raw_readw(hw->port + ENETC_PSIPMAR1(si));
19 
20 	put_unaligned_le32(upper, addr);
21 	put_unaligned_le16(lower, addr + 4);
22 }
23 
24 static void enetc_pf_set_primary_mac_addr(struct enetc_hw *hw, int si,
25 					  const u8 *addr)
26 {
27 	u32 upper = get_unaligned_le32(addr);
28 	u16 lower = get_unaligned_le16(addr + 4);
29 
30 	__raw_writel(upper, hw->port + ENETC_PSIPMAR0(si));
31 	__raw_writew(lower, hw->port + ENETC_PSIPMAR1(si));
32 }
33 
34 static struct phylink_pcs *enetc_pf_create_pcs(struct enetc_pf *pf,
35 					       struct mii_bus *bus)
36 {
37 	return lynx_pcs_create_mdiodev(bus, 0);
38 }
39 
40 static void enetc_pf_destroy_pcs(struct phylink_pcs *pcs)
41 {
42 	lynx_pcs_destroy(pcs);
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 				 unsigned long 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),
137 			      lower_32_bits(hash));
138 		enetc_port_wr(&si->hw, ENETC_PSIUMHFR1(si_idx),
139 			      upper_32_bits(hash));
140 	} else { /* MC */
141 		enetc_port_wr(&si->hw, ENETC_PSIMMHFR0(si_idx, err),
142 			      lower_32_bits(hash));
143 		enetc_port_wr(&si->hw, ENETC_PSIMMHFR1(si_idx),
144 			      upper_32_bits(hash));
145 	}
146 }
147 
148 static void enetc_sync_mac_filters(struct enetc_pf *pf)
149 {
150 	struct enetc_mac_filter *f = pf->mac_filter;
151 	struct enetc_si *si = pf->si;
152 	int i, pos;
153 
154 	pos = EMETC_MAC_ADDR_FILT_RES;
155 
156 	for (i = 0; i < MADDR_TYPE; i++, f++) {
157 		bool em = (f->mac_addr_cnt == 1) && (i == UC);
158 		bool clear = !f->mac_addr_cnt;
159 
160 		if (clear) {
161 			if (i == UC)
162 				enetc_clear_mac_flt_entry(si, pos);
163 
164 			enetc_clear_mac_ht_flt(si, 0, i);
165 			continue;
166 		}
167 
168 		/* exact match filter */
169 		if (em) {
170 			int err;
171 
172 			enetc_clear_mac_ht_flt(si, 0, UC);
173 
174 			err = enetc_set_mac_flt_entry(si, pos, f->mac_addr,
175 						      BIT(0));
176 			if (!err)
177 				continue;
178 
179 			/* fallback to HT filtering */
180 			dev_warn(&si->pdev->dev, "fallback to HT filt (%d)\n",
181 				 err);
182 		}
183 
184 		/* hash table filter, clear EM filter for UC entries */
185 		if (i == UC)
186 			enetc_clear_mac_flt_entry(si, pos);
187 
188 		enetc_set_mac_ht_flt(si, 0, i, *f->mac_hash_table);
189 	}
190 }
191 
192 static void enetc_pf_set_rx_mode(struct net_device *ndev)
193 {
194 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
195 	struct enetc_pf *pf = enetc_si_priv(priv->si);
196 	struct enetc_hw *hw = &priv->si->hw;
197 	bool uprom = false, mprom = false;
198 	struct enetc_mac_filter *filter;
199 	struct netdev_hw_addr *ha;
200 	u32 psipmr = 0;
201 	bool em;
202 
203 	if (ndev->flags & IFF_PROMISC) {
204 		/* enable promisc mode for SI0 (PF) */
205 		psipmr = ENETC_PSIPMR_SET_UP(0) | ENETC_PSIPMR_SET_MP(0);
206 		uprom = true;
207 		mprom = true;
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 	/* first 2 filter entries belong to PF */
215 	if (!uprom) {
216 		/* Update unicast filters */
217 		filter = &pf->mac_filter[UC];
218 		enetc_reset_mac_addr_filter(filter);
219 
220 		em = (netdev_uc_count(ndev) == 1);
221 		netdev_for_each_uc_addr(ha, ndev) {
222 			if (em) {
223 				enetc_add_mac_addr_em_filter(filter, ha->addr);
224 				break;
225 			}
226 
227 			enetc_add_mac_addr_ht_filter(filter, ha->addr);
228 		}
229 	}
230 
231 	if (!mprom) {
232 		/* Update multicast filters */
233 		filter = &pf->mac_filter[MC];
234 		enetc_reset_mac_addr_filter(filter);
235 
236 		netdev_for_each_mc_addr(ha, ndev) {
237 			if (!is_multicast_ether_addr(ha->addr))
238 				continue;
239 
240 			enetc_add_mac_addr_ht_filter(filter, ha->addr);
241 		}
242 	}
243 
244 	if (!uprom || !mprom)
245 		/* update PF entries */
246 		enetc_sync_mac_filters(pf);
247 
248 	psipmr |= enetc_port_rd(hw, ENETC_PSIPMR) &
249 		  ~(ENETC_PSIPMR_SET_UP(0) | ENETC_PSIPMR_SET_MP(0));
250 	enetc_port_wr(hw, ENETC_PSIPMR, psipmr);
251 }
252 
253 static void enetc_set_vlan_ht_filter(struct enetc_hw *hw, int si_idx,
254 				     unsigned long hash)
255 {
256 	enetc_port_wr(hw, ENETC_PSIVHFR0(si_idx), lower_32_bits(hash));
257 	enetc_port_wr(hw, ENETC_PSIVHFR1(si_idx), upper_32_bits(hash));
258 }
259 
260 static int enetc_vid_hash_idx(unsigned int vid)
261 {
262 	int res = 0;
263 	int i;
264 
265 	for (i = 0; i < 6; i++)
266 		res |= (hweight8(vid & (BIT(i) | BIT(i + 6))) & 0x1) << i;
267 
268 	return res;
269 }
270 
271 static void enetc_sync_vlan_ht_filter(struct enetc_pf *pf, bool rehash)
272 {
273 	int i;
274 
275 	if (rehash) {
276 		bitmap_zero(pf->vlan_ht_filter, ENETC_VLAN_HT_SIZE);
277 
278 		for_each_set_bit(i, pf->active_vlans, VLAN_N_VID) {
279 			int hidx = enetc_vid_hash_idx(i);
280 
281 			__set_bit(hidx, pf->vlan_ht_filter);
282 		}
283 	}
284 
285 	enetc_set_vlan_ht_filter(&pf->si->hw, 0, *pf->vlan_ht_filter);
286 }
287 
288 static int enetc_vlan_rx_add_vid(struct net_device *ndev, __be16 prot, u16 vid)
289 {
290 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
291 	struct enetc_pf *pf = enetc_si_priv(priv->si);
292 	int idx;
293 
294 	__set_bit(vid, pf->active_vlans);
295 
296 	idx = enetc_vid_hash_idx(vid);
297 	if (!__test_and_set_bit(idx, pf->vlan_ht_filter))
298 		enetc_sync_vlan_ht_filter(pf, false);
299 
300 	return 0;
301 }
302 
303 static int enetc_vlan_rx_del_vid(struct net_device *ndev, __be16 prot, u16 vid)
304 {
305 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
306 	struct enetc_pf *pf = enetc_si_priv(priv->si);
307 
308 	__clear_bit(vid, pf->active_vlans);
309 	enetc_sync_vlan_ht_filter(pf, true);
310 
311 	return 0;
312 }
313 
314 static void enetc_set_loopback(struct net_device *ndev, bool en)
315 {
316 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
317 	struct enetc_si *si = priv->si;
318 	u32 reg;
319 
320 	reg = enetc_port_mac_rd(si, ENETC_PM0_IF_MODE);
321 	if (reg & ENETC_PM0_IFM_RG) {
322 		/* RGMII mode */
323 		reg = (reg & ~ENETC_PM0_IFM_RLP) |
324 		      (en ? ENETC_PM0_IFM_RLP : 0);
325 		enetc_port_mac_wr(si, ENETC_PM0_IF_MODE, reg);
326 	} else {
327 		/* assume SGMII mode */
328 		reg = enetc_port_mac_rd(si, ENETC_PM0_CMD_CFG);
329 		reg = (reg & ~ENETC_PM0_CMD_XGLP) |
330 		      (en ? ENETC_PM0_CMD_XGLP : 0);
331 		reg = (reg & ~ENETC_PM0_CMD_PHY_TX_EN) |
332 		      (en ? ENETC_PM0_CMD_PHY_TX_EN : 0);
333 		enetc_port_mac_wr(si, ENETC_PM0_CMD_CFG, reg);
334 	}
335 }
336 
337 static int enetc_pf_set_vf_mac(struct net_device *ndev, int vf, u8 *mac)
338 {
339 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
340 	struct enetc_pf *pf = enetc_si_priv(priv->si);
341 	struct enetc_vf_state *vf_state;
342 
343 	if (vf >= pf->total_vfs)
344 		return -EINVAL;
345 
346 	if (!is_valid_ether_addr(mac))
347 		return -EADDRNOTAVAIL;
348 
349 	vf_state = &pf->vf_state[vf];
350 	vf_state->flags |= ENETC_VF_FLAG_PF_SET_MAC;
351 	enetc_pf_set_primary_mac_addr(&priv->si->hw, vf + 1, mac);
352 	return 0;
353 }
354 
355 static int enetc_pf_set_vf_vlan(struct net_device *ndev, int vf, u16 vlan,
356 				u8 qos, __be16 proto)
357 {
358 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
359 	struct enetc_pf *pf = enetc_si_priv(priv->si);
360 
361 	if (priv->si->errata & ENETC_ERR_VLAN_ISOL)
362 		return -EOPNOTSUPP;
363 
364 	if (vf >= pf->total_vfs)
365 		return -EINVAL;
366 
367 	if (proto != htons(ETH_P_8021Q))
368 		/* only C-tags supported for now */
369 		return -EPROTONOSUPPORT;
370 
371 	enetc_set_isol_vlan(&priv->si->hw, vf + 1, vlan, qos);
372 	return 0;
373 }
374 
375 static int enetc_pf_set_vf_spoofchk(struct net_device *ndev, int vf, bool en)
376 {
377 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
378 	struct enetc_pf *pf = enetc_si_priv(priv->si);
379 	u32 cfgr;
380 
381 	if (vf >= pf->total_vfs)
382 		return -EINVAL;
383 
384 	cfgr = enetc_port_rd(&priv->si->hw, ENETC_PSICFGR0(vf + 1));
385 	cfgr = (cfgr & ~ENETC_PSICFGR0_ASE) | (en ? ENETC_PSICFGR0_ASE : 0);
386 	enetc_port_wr(&priv->si->hw, ENETC_PSICFGR0(vf + 1), cfgr);
387 
388 	return 0;
389 }
390 
391 static void enetc_port_assign_rfs_entries(struct enetc_si *si)
392 {
393 	struct enetc_pf *pf = enetc_si_priv(si);
394 	struct enetc_hw *hw = &si->hw;
395 	int num_entries, vf_entries, i;
396 	u32 val;
397 
398 	/* split RFS entries between functions */
399 	val = enetc_port_rd(hw, ENETC_PRFSCAPR);
400 	num_entries = ENETC_PRFSCAPR_GET_NUM_RFS(val);
401 	vf_entries = num_entries / (pf->total_vfs + 1);
402 
403 	for (i = 0; i < pf->total_vfs; i++)
404 		enetc_port_wr(hw, ENETC_PSIRFSCFGR(i + 1), vf_entries);
405 	enetc_port_wr(hw, ENETC_PSIRFSCFGR(0),
406 		      num_entries - vf_entries * pf->total_vfs);
407 
408 	/* enable RFS on port */
409 	enetc_port_wr(hw, ENETC_PRFSMR, ENETC_PRFSMR_RFSE);
410 }
411 
412 static void enetc_port_si_configure(struct enetc_si *si)
413 {
414 	struct enetc_pf *pf = enetc_si_priv(si);
415 	struct enetc_hw *hw = &si->hw;
416 	int num_rings, i;
417 	u32 val;
418 
419 	val = enetc_port_rd(hw, ENETC_PCAPR0);
420 	num_rings = min(ENETC_PCAPR0_RXBDR(val), ENETC_PCAPR0_TXBDR(val));
421 
422 	val = ENETC_PSICFGR0_SET_TXBDR(ENETC_PF_NUM_RINGS);
423 	val |= ENETC_PSICFGR0_SET_RXBDR(ENETC_PF_NUM_RINGS);
424 
425 	if (unlikely(num_rings < ENETC_PF_NUM_RINGS)) {
426 		val = ENETC_PSICFGR0_SET_TXBDR(num_rings);
427 		val |= ENETC_PSICFGR0_SET_RXBDR(num_rings);
428 
429 		dev_warn(&si->pdev->dev, "Found %d rings, expected %d!\n",
430 			 num_rings, ENETC_PF_NUM_RINGS);
431 
432 		num_rings = 0;
433 	}
434 
435 	/* Add default one-time settings for SI0 (PF) */
436 	val |= ENETC_PSICFGR0_SIVC(ENETC_VLAN_TYPE_C | ENETC_VLAN_TYPE_S);
437 
438 	enetc_port_wr(hw, ENETC_PSICFGR0(0), val);
439 
440 	if (num_rings)
441 		num_rings -= ENETC_PF_NUM_RINGS;
442 
443 	/* Configure the SIs for each available VF */
444 	val = ENETC_PSICFGR0_SIVC(ENETC_VLAN_TYPE_C | ENETC_VLAN_TYPE_S);
445 	val |= ENETC_PSICFGR0_VTE | ENETC_PSICFGR0_SIVIE;
446 
447 	if (num_rings) {
448 		num_rings /= pf->total_vfs;
449 		val |= ENETC_PSICFGR0_SET_TXBDR(num_rings);
450 		val |= ENETC_PSICFGR0_SET_RXBDR(num_rings);
451 	}
452 
453 	for (i = 0; i < pf->total_vfs; i++)
454 		enetc_port_wr(hw, ENETC_PSICFGR0(i + 1), val);
455 
456 	/* Port level VLAN settings */
457 	val = ENETC_PVCLCTR_OVTPIDL(ENETC_VLAN_TYPE_C | ENETC_VLAN_TYPE_S);
458 	enetc_port_wr(hw, ENETC_PVCLCTR, val);
459 	/* use outer tag for VLAN filtering */
460 	enetc_port_wr(hw, ENETC_PSIVLANFMR, ENETC_PSIVLANFMR_VS);
461 }
462 
463 void enetc_set_ptcmsdur(struct enetc_hw *hw, u32 *max_sdu)
464 {
465 	int tc;
466 
467 	for (tc = 0; tc < 8; tc++) {
468 		u32 val = ENETC_MAC_MAXFRM_SIZE;
469 
470 		if (max_sdu[tc])
471 			val = max_sdu[tc] + VLAN_ETH_HLEN;
472 
473 		enetc_port_wr(hw, ENETC_PTCMSDUR(tc), val);
474 	}
475 }
476 
477 void enetc_reset_ptcmsdur(struct enetc_hw *hw)
478 {
479 	int tc;
480 
481 	for (tc = 0; tc < 8; tc++)
482 		enetc_port_wr(hw, ENETC_PTCMSDUR(tc), ENETC_MAC_MAXFRM_SIZE);
483 }
484 
485 static void enetc_configure_port_mac(struct enetc_si *si)
486 {
487 	struct enetc_hw *hw = &si->hw;
488 
489 	enetc_port_mac_wr(si, ENETC_PM0_MAXFRM,
490 			  ENETC_SET_MAXFRM(ENETC_RX_MAXFRM_SIZE));
491 
492 	enetc_reset_ptcmsdur(hw);
493 
494 	enetc_port_mac_wr(si, ENETC_PM0_CMD_CFG, ENETC_PM0_CMD_PHY_TX_EN |
495 			  ENETC_PM0_CMD_TXP | ENETC_PM0_PROMISC);
496 
497 	/* On LS1028A, the MAC RX FIFO defaults to 2, which is too high
498 	 * and may lead to RX lock-up under traffic. Set it to 1 instead,
499 	 * as recommended by the hardware team.
500 	 */
501 	enetc_port_mac_wr(si, ENETC_PM0_RX_FIFO, ENETC_PM0_RX_FIFO_VAL);
502 }
503 
504 static void enetc_mac_config(struct enetc_si *si, phy_interface_t phy_mode)
505 {
506 	u32 val;
507 
508 	if (phy_interface_mode_is_rgmii(phy_mode)) {
509 		val = enetc_port_mac_rd(si, ENETC_PM0_IF_MODE);
510 		val &= ~(ENETC_PM0_IFM_EN_AUTO | ENETC_PM0_IFM_IFMODE_MASK);
511 		val |= ENETC_PM0_IFM_IFMODE_GMII | ENETC_PM0_IFM_RG;
512 		enetc_port_mac_wr(si, ENETC_PM0_IF_MODE, val);
513 	}
514 
515 	if (phy_mode == PHY_INTERFACE_MODE_USXGMII) {
516 		val = ENETC_PM0_IFM_FULL_DPX | ENETC_PM0_IFM_IFMODE_XGMII;
517 		enetc_port_mac_wr(si, ENETC_PM0_IF_MODE, val);
518 	}
519 }
520 
521 static void enetc_mac_enable(struct enetc_si *si, bool en)
522 {
523 	u32 val = enetc_port_mac_rd(si, ENETC_PM0_CMD_CFG);
524 
525 	val &= ~(ENETC_PM0_TX_EN | ENETC_PM0_RX_EN);
526 	val |= en ? (ENETC_PM0_TX_EN | ENETC_PM0_RX_EN) : 0;
527 
528 	enetc_port_mac_wr(si, ENETC_PM0_CMD_CFG, val);
529 }
530 
531 static void enetc_configure_port(struct enetc_pf *pf)
532 {
533 	u8 hash_key[ENETC_RSSHASH_KEY_SIZE];
534 	struct enetc_hw *hw = &pf->si->hw;
535 
536 	enetc_configure_port_mac(pf->si);
537 
538 	enetc_port_si_configure(pf->si);
539 
540 	/* set up hash key */
541 	get_random_bytes(hash_key, ENETC_RSSHASH_KEY_SIZE);
542 	enetc_set_rss_key(hw, hash_key);
543 
544 	/* split up RFS entries */
545 	enetc_port_assign_rfs_entries(pf->si);
546 
547 	/* enforce VLAN promisc mode for all SIs */
548 	pf->vlan_promisc_simap = ENETC_VLAN_PROMISC_MAP_ALL;
549 	enetc_set_vlan_promisc(hw, pf->vlan_promisc_simap);
550 
551 	enetc_port_wr(hw, ENETC_PSIPMR, 0);
552 
553 	/* enable port */
554 	enetc_port_wr(hw, ENETC_PMR, ENETC_PMR_EN);
555 }
556 
557 /* Messaging */
558 static u16 enetc_msg_pf_set_vf_primary_mac_addr(struct enetc_pf *pf,
559 						int vf_id)
560 {
561 	struct enetc_vf_state *vf_state = &pf->vf_state[vf_id];
562 	struct enetc_msg_swbd *msg = &pf->rxmsg[vf_id];
563 	struct enetc_msg_cmd_set_primary_mac *cmd;
564 	struct device *dev = &pf->si->pdev->dev;
565 	u16 cmd_id;
566 	char *addr;
567 
568 	cmd = (struct enetc_msg_cmd_set_primary_mac *)msg->vaddr;
569 	cmd_id = cmd->header.id;
570 	if (cmd_id != ENETC_MSG_CMD_MNG_ADD)
571 		return ENETC_MSG_CMD_STATUS_FAIL;
572 
573 	addr = cmd->mac.sa_data;
574 	if (vf_state->flags & ENETC_VF_FLAG_PF_SET_MAC)
575 		dev_warn(dev, "Attempt to override PF set mac addr for VF%d\n",
576 			 vf_id);
577 	else
578 		enetc_pf_set_primary_mac_addr(&pf->si->hw, vf_id + 1, addr);
579 
580 	return ENETC_MSG_CMD_STATUS_OK;
581 }
582 
583 void enetc_msg_handle_rxmsg(struct enetc_pf *pf, int vf_id, u16 *status)
584 {
585 	struct enetc_msg_swbd *msg = &pf->rxmsg[vf_id];
586 	struct device *dev = &pf->si->pdev->dev;
587 	struct enetc_msg_cmd_header *cmd_hdr;
588 	u16 cmd_type;
589 
590 	*status = ENETC_MSG_CMD_STATUS_OK;
591 	cmd_hdr = (struct enetc_msg_cmd_header *)msg->vaddr;
592 	cmd_type = cmd_hdr->type;
593 
594 	switch (cmd_type) {
595 	case ENETC_MSG_CMD_MNG_MAC:
596 		*status = enetc_msg_pf_set_vf_primary_mac_addr(pf, vf_id);
597 		break;
598 	default:
599 		dev_err(dev, "command not supported (cmd_type: 0x%x)\n",
600 			cmd_type);
601 	}
602 }
603 
604 #ifdef CONFIG_PCI_IOV
605 static int enetc_sriov_configure(struct pci_dev *pdev, int num_vfs)
606 {
607 	struct enetc_si *si = pci_get_drvdata(pdev);
608 	struct enetc_pf *pf = enetc_si_priv(si);
609 	int err;
610 
611 	if (!num_vfs) {
612 		enetc_msg_psi_free(pf);
613 		pf->num_vfs = 0;
614 		pci_disable_sriov(pdev);
615 	} else {
616 		pf->num_vfs = num_vfs;
617 
618 		err = enetc_msg_psi_init(pf);
619 		if (err) {
620 			dev_err(&pdev->dev, "enetc_msg_psi_init (%d)\n", err);
621 			goto err_msg_psi;
622 		}
623 
624 		err = pci_enable_sriov(pdev, num_vfs);
625 		if (err) {
626 			dev_err(&pdev->dev, "pci_enable_sriov err %d\n", err);
627 			goto err_en_sriov;
628 		}
629 	}
630 
631 	return num_vfs;
632 
633 err_en_sriov:
634 	enetc_msg_psi_free(pf);
635 err_msg_psi:
636 	pf->num_vfs = 0;
637 
638 	return err;
639 }
640 #else
641 #define enetc_sriov_configure(pdev, num_vfs)	(void)0
642 #endif
643 
644 static int enetc_pf_set_features(struct net_device *ndev,
645 				 netdev_features_t features)
646 {
647 	netdev_features_t changed = ndev->features ^ features;
648 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
649 	int err;
650 
651 	if (changed & NETIF_F_HW_TC) {
652 		err = enetc_set_psfp(ndev, !!(features & NETIF_F_HW_TC));
653 		if (err)
654 			return err;
655 	}
656 
657 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
658 		struct enetc_pf *pf = enetc_si_priv(priv->si);
659 
660 		if (!!(features & NETIF_F_HW_VLAN_CTAG_FILTER))
661 			enetc_disable_si_vlan_promisc(pf, 0);
662 		else
663 			enetc_enable_si_vlan_promisc(pf, 0);
664 	}
665 
666 	if (changed & NETIF_F_LOOPBACK)
667 		enetc_set_loopback(ndev, !!(features & NETIF_F_LOOPBACK));
668 
669 	enetc_set_features(ndev, features);
670 
671 	return 0;
672 }
673 
674 static int enetc_pf_setup_tc(struct net_device *ndev, enum tc_setup_type type,
675 			     void *type_data)
676 {
677 	switch (type) {
678 	case TC_QUERY_CAPS:
679 		return enetc_qos_query_caps(ndev, type_data);
680 	case TC_SETUP_QDISC_MQPRIO:
681 		return enetc_setup_tc_mqprio(ndev, type_data);
682 	case TC_SETUP_QDISC_TAPRIO:
683 		return enetc_setup_tc_taprio(ndev, type_data);
684 	case TC_SETUP_QDISC_CBS:
685 		return enetc_setup_tc_cbs(ndev, type_data);
686 	case TC_SETUP_QDISC_ETF:
687 		return enetc_setup_tc_txtime(ndev, type_data);
688 	case TC_SETUP_BLOCK:
689 		return enetc_setup_tc_psfp(ndev, type_data);
690 	default:
691 		return -EOPNOTSUPP;
692 	}
693 }
694 
695 static const struct net_device_ops enetc_ndev_ops = {
696 	.ndo_open		= enetc_open,
697 	.ndo_stop		= enetc_close,
698 	.ndo_start_xmit		= enetc_xmit,
699 	.ndo_get_stats		= enetc_get_stats,
700 	.ndo_set_mac_address	= enetc_pf_set_mac_addr,
701 	.ndo_set_rx_mode	= enetc_pf_set_rx_mode,
702 	.ndo_vlan_rx_add_vid	= enetc_vlan_rx_add_vid,
703 	.ndo_vlan_rx_kill_vid	= enetc_vlan_rx_del_vid,
704 	.ndo_set_vf_mac		= enetc_pf_set_vf_mac,
705 	.ndo_set_vf_vlan	= enetc_pf_set_vf_vlan,
706 	.ndo_set_vf_spoofchk	= enetc_pf_set_vf_spoofchk,
707 	.ndo_set_features	= enetc_pf_set_features,
708 	.ndo_eth_ioctl		= enetc_ioctl,
709 	.ndo_setup_tc		= enetc_pf_setup_tc,
710 	.ndo_bpf		= enetc_setup_bpf,
711 	.ndo_xdp_xmit		= enetc_xdp_xmit,
712 };
713 
714 static struct phylink_pcs *
715 enetc_pl_mac_select_pcs(struct phylink_config *config, phy_interface_t iface)
716 {
717 	struct enetc_pf *pf = phylink_to_enetc_pf(config);
718 
719 	return pf->pcs;
720 }
721 
722 static void enetc_pl_mac_config(struct phylink_config *config,
723 				unsigned int mode,
724 				const struct phylink_link_state *state)
725 {
726 	struct enetc_pf *pf = phylink_to_enetc_pf(config);
727 
728 	enetc_mac_config(pf->si, state->interface);
729 }
730 
731 static void enetc_force_rgmii_mac(struct enetc_si *si, int speed, int duplex)
732 {
733 	u32 old_val, val;
734 
735 	old_val = val = enetc_port_mac_rd(si, ENETC_PM0_IF_MODE);
736 
737 	if (speed == SPEED_1000) {
738 		val &= ~ENETC_PM0_IFM_SSP_MASK;
739 		val |= ENETC_PM0_IFM_SSP_1000;
740 	} else if (speed == SPEED_100) {
741 		val &= ~ENETC_PM0_IFM_SSP_MASK;
742 		val |= ENETC_PM0_IFM_SSP_100;
743 	} else if (speed == SPEED_10) {
744 		val &= ~ENETC_PM0_IFM_SSP_MASK;
745 		val |= ENETC_PM0_IFM_SSP_10;
746 	}
747 
748 	if (duplex == DUPLEX_FULL)
749 		val |= ENETC_PM0_IFM_FULL_DPX;
750 	else
751 		val &= ~ENETC_PM0_IFM_FULL_DPX;
752 
753 	if (val == old_val)
754 		return;
755 
756 	enetc_port_mac_wr(si, ENETC_PM0_IF_MODE, val);
757 }
758 
759 static void enetc_pl_mac_link_up(struct phylink_config *config,
760 				 struct phy_device *phy, unsigned int mode,
761 				 phy_interface_t interface, int speed,
762 				 int duplex, bool tx_pause, bool rx_pause)
763 {
764 	struct enetc_pf *pf = phylink_to_enetc_pf(config);
765 	u32 pause_off_thresh = 0, pause_on_thresh = 0;
766 	u32 init_quanta = 0, refresh_quanta = 0;
767 	struct enetc_hw *hw = &pf->si->hw;
768 	struct enetc_si *si = pf->si;
769 	struct enetc_ndev_priv *priv;
770 	u32 rbmr, cmd_cfg;
771 	int idx;
772 
773 	priv = netdev_priv(pf->si->ndev);
774 
775 	if (pf->si->hw_features & ENETC_SI_F_QBV)
776 		enetc_sched_speed_set(priv, speed);
777 
778 	if (!phylink_autoneg_inband(mode) &&
779 	    phy_interface_mode_is_rgmii(interface))
780 		enetc_force_rgmii_mac(si, speed, duplex);
781 
782 	/* Flow control */
783 	for (idx = 0; idx < priv->num_rx_rings; idx++) {
784 		rbmr = enetc_rxbdr_rd(hw, idx, ENETC_RBMR);
785 
786 		if (tx_pause)
787 			rbmr |= ENETC_RBMR_CM;
788 		else
789 			rbmr &= ~ENETC_RBMR_CM;
790 
791 		enetc_rxbdr_wr(hw, idx, ENETC_RBMR, rbmr);
792 	}
793 
794 	if (tx_pause) {
795 		/* When the port first enters congestion, send a PAUSE request
796 		 * with the maximum number of quanta. When the port exits
797 		 * congestion, it will automatically send a PAUSE frame with
798 		 * zero quanta.
799 		 */
800 		init_quanta = 0xffff;
801 
802 		/* Also, set up the refresh timer to send follow-up PAUSE
803 		 * frames at half the quanta value, in case the congestion
804 		 * condition persists.
805 		 */
806 		refresh_quanta = 0xffff / 2;
807 
808 		/* Start emitting PAUSE frames when 3 large frames (or more
809 		 * smaller frames) have accumulated in the FIFO waiting to be
810 		 * DMAed to the RX ring.
811 		 */
812 		pause_on_thresh = 3 * ENETC_MAC_MAXFRM_SIZE;
813 		pause_off_thresh = 1 * ENETC_MAC_MAXFRM_SIZE;
814 	}
815 
816 	enetc_port_mac_wr(si, ENETC_PM0_PAUSE_QUANTA, init_quanta);
817 	enetc_port_mac_wr(si, ENETC_PM0_PAUSE_THRESH, refresh_quanta);
818 	enetc_port_wr(hw, ENETC_PPAUONTR, pause_on_thresh);
819 	enetc_port_wr(hw, ENETC_PPAUOFFTR, pause_off_thresh);
820 
821 	cmd_cfg = enetc_port_mac_rd(si, ENETC_PM0_CMD_CFG);
822 
823 	if (rx_pause)
824 		cmd_cfg &= ~ENETC_PM0_PAUSE_IGN;
825 	else
826 		cmd_cfg |= ENETC_PM0_PAUSE_IGN;
827 
828 	enetc_port_mac_wr(si, ENETC_PM0_CMD_CFG, cmd_cfg);
829 
830 	enetc_mac_enable(si, true);
831 
832 	if (si->hw_features & ENETC_SI_F_QBU)
833 		enetc_mm_link_state_update(priv, true);
834 }
835 
836 static void enetc_pl_mac_link_down(struct phylink_config *config,
837 				   unsigned int mode,
838 				   phy_interface_t interface)
839 {
840 	struct enetc_pf *pf = phylink_to_enetc_pf(config);
841 	struct enetc_si *si = pf->si;
842 	struct enetc_ndev_priv *priv;
843 
844 	priv = netdev_priv(si->ndev);
845 
846 	if (si->hw_features & ENETC_SI_F_QBU)
847 		enetc_mm_link_state_update(priv, false);
848 
849 	enetc_mac_enable(si, false);
850 }
851 
852 static const struct phylink_mac_ops enetc_mac_phylink_ops = {
853 	.mac_select_pcs = enetc_pl_mac_select_pcs,
854 	.mac_config = enetc_pl_mac_config,
855 	.mac_link_up = enetc_pl_mac_link_up,
856 	.mac_link_down = enetc_pl_mac_link_down,
857 };
858 
859 /* Initialize the entire shared memory for the flow steering entries
860  * of this port (PF + VFs)
861  */
862 static int enetc_init_port_rfs_memory(struct enetc_si *si)
863 {
864 	struct enetc_cmd_rfse rfse = {0};
865 	struct enetc_hw *hw = &si->hw;
866 	int num_rfs, i, err = 0;
867 	u32 val;
868 
869 	val = enetc_port_rd(hw, ENETC_PRFSCAPR);
870 	num_rfs = ENETC_PRFSCAPR_GET_NUM_RFS(val);
871 
872 	for (i = 0; i < num_rfs; i++) {
873 		err = enetc_set_fs_entry(si, &rfse, i);
874 		if (err)
875 			break;
876 	}
877 
878 	return err;
879 }
880 
881 static int enetc_init_port_rss_memory(struct enetc_si *si)
882 {
883 	struct enetc_hw *hw = &si->hw;
884 	int num_rss, err;
885 	int *rss_table;
886 	u32 val;
887 
888 	val = enetc_port_rd(hw, ENETC_PRSSCAPR);
889 	num_rss = ENETC_PRSSCAPR_GET_NUM_RSS(val);
890 	if (!num_rss)
891 		return 0;
892 
893 	rss_table = kcalloc(num_rss, sizeof(*rss_table), GFP_KERNEL);
894 	if (!rss_table)
895 		return -ENOMEM;
896 
897 	err = enetc_set_rss_table(si, rss_table, num_rss);
898 
899 	kfree(rss_table);
900 
901 	return err;
902 }
903 
904 static int enetc_pf_register_with_ierb(struct pci_dev *pdev)
905 {
906 	struct platform_device *ierb_pdev;
907 	struct device_node *ierb_node;
908 
909 	ierb_node = of_find_compatible_node(NULL, NULL,
910 					    "fsl,ls1028a-enetc-ierb");
911 	if (!ierb_node || !of_device_is_available(ierb_node))
912 		return -ENODEV;
913 
914 	ierb_pdev = of_find_device_by_node(ierb_node);
915 	of_node_put(ierb_node);
916 
917 	if (!ierb_pdev)
918 		return -EPROBE_DEFER;
919 
920 	return enetc_ierb_register_pf(ierb_pdev, pdev);
921 }
922 
923 static struct enetc_si *enetc_psi_create(struct pci_dev *pdev)
924 {
925 	struct enetc_si *si;
926 	int err;
927 
928 	err = enetc_pci_probe(pdev, KBUILD_MODNAME, sizeof(struct enetc_pf));
929 	if (err) {
930 		dev_err_probe(&pdev->dev, err, "PCI probing failed\n");
931 		goto out;
932 	}
933 
934 	si = pci_get_drvdata(pdev);
935 	if (!si->hw.port || !si->hw.global) {
936 		err = -ENODEV;
937 		dev_err(&pdev->dev, "could not map PF space, probing a VF?\n");
938 		goto out_pci_remove;
939 	}
940 
941 	si->revision = enetc_get_ip_revision(&si->hw);
942 	err = enetc_get_driver_data(si);
943 	if (err) {
944 		dev_err(&pdev->dev, "Could not get PF driver data\n");
945 		goto out_pci_remove;
946 	}
947 
948 	err = enetc_setup_cbdr(&pdev->dev, &si->hw, ENETC_CBDR_DEFAULT_SIZE,
949 			       &si->cbd_ring);
950 	if (err)
951 		goto out_pci_remove;
952 
953 	err = enetc_init_port_rfs_memory(si);
954 	if (err) {
955 		dev_err(&pdev->dev, "Failed to initialize RFS memory\n");
956 		goto out_teardown_cbdr;
957 	}
958 
959 	err = enetc_init_port_rss_memory(si);
960 	if (err) {
961 		dev_err(&pdev->dev, "Failed to initialize RSS memory\n");
962 		goto out_teardown_cbdr;
963 	}
964 
965 	return si;
966 
967 out_teardown_cbdr:
968 	enetc_teardown_cbdr(&si->cbd_ring);
969 out_pci_remove:
970 	enetc_pci_remove(pdev);
971 out:
972 	return ERR_PTR(err);
973 }
974 
975 static void enetc_psi_destroy(struct pci_dev *pdev)
976 {
977 	struct enetc_si *si = pci_get_drvdata(pdev);
978 
979 	enetc_teardown_cbdr(&si->cbd_ring);
980 	enetc_pci_remove(pdev);
981 }
982 
983 static const struct enetc_pf_ops enetc_pf_ops = {
984 	.set_si_primary_mac = enetc_pf_set_primary_mac_addr,
985 	.get_si_primary_mac = enetc_pf_get_primary_mac_addr,
986 	.create_pcs = enetc_pf_create_pcs,
987 	.destroy_pcs = enetc_pf_destroy_pcs,
988 	.enable_psfp = enetc_psfp_enable,
989 };
990 
991 static int enetc_pf_probe(struct pci_dev *pdev,
992 			  const struct pci_device_id *ent)
993 {
994 	struct device_node *node = pdev->dev.of_node;
995 	struct enetc_ndev_priv *priv;
996 	struct net_device *ndev;
997 	struct enetc_si *si;
998 	struct enetc_pf *pf;
999 	int err;
1000 
1001 	err = enetc_pf_register_with_ierb(pdev);
1002 	if (err == -EPROBE_DEFER)
1003 		return err;
1004 	if (err)
1005 		dev_warn(&pdev->dev,
1006 			 "Could not register with IERB driver: %pe, please update the device tree\n",
1007 			 ERR_PTR(err));
1008 
1009 	si = enetc_psi_create(pdev);
1010 	if (IS_ERR(si)) {
1011 		err = PTR_ERR(si);
1012 		goto err_psi_create;
1013 	}
1014 
1015 	pf = enetc_si_priv(si);
1016 	pf->si = si;
1017 	pf->ops = &enetc_pf_ops;
1018 
1019 	pf->total_vfs = pci_sriov_get_totalvfs(pdev);
1020 	if (pf->total_vfs) {
1021 		pf->vf_state = kcalloc(pf->total_vfs, sizeof(struct enetc_vf_state),
1022 				       GFP_KERNEL);
1023 		if (!pf->vf_state)
1024 			goto err_alloc_vf_state;
1025 	}
1026 
1027 	err = enetc_setup_mac_addresses(node, pf);
1028 	if (err)
1029 		goto err_setup_mac_addresses;
1030 
1031 	enetc_configure_port(pf);
1032 
1033 	enetc_get_si_caps(si);
1034 
1035 	ndev = alloc_etherdev_mq(sizeof(*priv), ENETC_MAX_NUM_TXQS);
1036 	if (!ndev) {
1037 		err = -ENOMEM;
1038 		dev_err(&pdev->dev, "netdev creation failed\n");
1039 		goto err_alloc_netdev;
1040 	}
1041 
1042 	enetc_pf_netdev_setup(si, ndev, &enetc_ndev_ops);
1043 
1044 	priv = netdev_priv(ndev);
1045 
1046 	mutex_init(&priv->mm_lock);
1047 
1048 	enetc_init_si_rings_params(priv);
1049 
1050 	err = enetc_alloc_si_resources(priv);
1051 	if (err) {
1052 		dev_err(&pdev->dev, "SI resource alloc failed\n");
1053 		goto err_alloc_si_res;
1054 	}
1055 
1056 	err = enetc_configure_si(priv);
1057 	if (err) {
1058 		dev_err(&pdev->dev, "Failed to configure SI\n");
1059 		goto err_config_si;
1060 	}
1061 
1062 	err = enetc_alloc_msix(priv);
1063 	if (err) {
1064 		dev_err(&pdev->dev, "MSIX alloc failed\n");
1065 		goto err_alloc_msix;
1066 	}
1067 
1068 	err = of_get_phy_mode(node, &pf->if_mode);
1069 	if (err) {
1070 		dev_err(&pdev->dev, "Failed to read PHY mode\n");
1071 		goto err_phy_mode;
1072 	}
1073 
1074 	err = enetc_mdiobus_create(pf, node);
1075 	if (err)
1076 		goto err_mdiobus_create;
1077 
1078 	err = enetc_phylink_create(priv, node, &enetc_mac_phylink_ops);
1079 	if (err)
1080 		goto err_phylink_create;
1081 
1082 	err = register_netdev(ndev);
1083 	if (err)
1084 		goto err_reg_netdev;
1085 
1086 	return 0;
1087 
1088 err_reg_netdev:
1089 	enetc_phylink_destroy(priv);
1090 err_phylink_create:
1091 	enetc_mdiobus_destroy(pf);
1092 err_mdiobus_create:
1093 err_phy_mode:
1094 	enetc_free_msix(priv);
1095 err_config_si:
1096 err_alloc_msix:
1097 	enetc_free_si_resources(priv);
1098 err_alloc_si_res:
1099 	si->ndev = NULL;
1100 	free_netdev(ndev);
1101 err_alloc_netdev:
1102 err_setup_mac_addresses:
1103 	kfree(pf->vf_state);
1104 err_alloc_vf_state:
1105 	enetc_psi_destroy(pdev);
1106 err_psi_create:
1107 	return err;
1108 }
1109 
1110 static void enetc_pf_remove(struct pci_dev *pdev)
1111 {
1112 	struct enetc_si *si = pci_get_drvdata(pdev);
1113 	struct enetc_pf *pf = enetc_si_priv(si);
1114 	struct enetc_ndev_priv *priv;
1115 
1116 	priv = netdev_priv(si->ndev);
1117 
1118 	if (pf->num_vfs)
1119 		enetc_sriov_configure(pdev, 0);
1120 
1121 	unregister_netdev(si->ndev);
1122 
1123 	enetc_phylink_destroy(priv);
1124 	enetc_mdiobus_destroy(pf);
1125 
1126 	enetc_free_msix(priv);
1127 
1128 	enetc_free_si_resources(priv);
1129 
1130 	free_netdev(si->ndev);
1131 	kfree(pf->vf_state);
1132 
1133 	enetc_psi_destroy(pdev);
1134 }
1135 
1136 static void enetc_fixup_clear_rss_rfs(struct pci_dev *pdev)
1137 {
1138 	struct device_node *node = pdev->dev.of_node;
1139 	struct enetc_si *si;
1140 
1141 	/* Only apply quirk for disabled functions. For the ones
1142 	 * that are enabled, enetc_pf_probe() will apply it.
1143 	 */
1144 	if (node && of_device_is_available(node))
1145 		return;
1146 
1147 	si = enetc_psi_create(pdev);
1148 	if (!IS_ERR(si))
1149 		enetc_psi_destroy(pdev);
1150 }
1151 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_FREESCALE, ENETC_DEV_ID_PF,
1152 			enetc_fixup_clear_rss_rfs);
1153 
1154 static const struct pci_device_id enetc_pf_id_table[] = {
1155 	{ PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, ENETC_DEV_ID_PF) },
1156 	{ 0, } /* End of table. */
1157 };
1158 MODULE_DEVICE_TABLE(pci, enetc_pf_id_table);
1159 
1160 static struct pci_driver enetc_pf_driver = {
1161 	.name = KBUILD_MODNAME,
1162 	.id_table = enetc_pf_id_table,
1163 	.probe = enetc_pf_probe,
1164 	.remove = enetc_pf_remove,
1165 #ifdef CONFIG_PCI_IOV
1166 	.sriov_configure = enetc_sriov_configure,
1167 #endif
1168 };
1169 module_pci_driver(enetc_pf_driver);
1170 
1171 MODULE_DESCRIPTION(ENETC_DRV_NAME_STR);
1172 MODULE_LICENSE("Dual BSD/GPL");
1173