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