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 				     phy_interface_t phy_mode)
487 {
488 	enetc_port_wr(hw, ENETC_PM0_MAXFRM,
489 		      ENETC_SET_MAXFRM(ENETC_RX_MAXFRM_SIZE));
490 
491 	enetc_port_wr(hw, ENETC_PTCMSDUR(0), ENETC_MAC_MAXFRM_SIZE);
492 	enetc_port_wr(hw, ENETC_PTXMBAR, 2 * ENETC_MAC_MAXFRM_SIZE);
493 
494 	enetc_port_wr(hw, ENETC_PM0_CMD_CFG, ENETC_PM0_CMD_PHY_TX_EN |
495 		      ENETC_PM0_CMD_TXP	| ENETC_PM0_PROMISC |
496 		      ENETC_PM0_TX_EN | ENETC_PM0_RX_EN);
497 
498 	enetc_port_wr(hw, ENETC_PM1_CMD_CFG, ENETC_PM0_CMD_PHY_TX_EN |
499 		      ENETC_PM0_CMD_TXP	| ENETC_PM0_PROMISC |
500 		      ENETC_PM0_TX_EN | ENETC_PM0_RX_EN);
501 	/* set auto-speed for RGMII */
502 	if (enetc_port_rd(hw, ENETC_PM0_IF_MODE) & ENETC_PMO_IFM_RG ||
503 	    phy_interface_mode_is_rgmii(phy_mode))
504 		enetc_port_wr(hw, ENETC_PM0_IF_MODE, ENETC_PM0_IFM_RGAUTO);
505 
506 	if (phy_mode == PHY_INTERFACE_MODE_USXGMII)
507 		enetc_port_wr(hw, ENETC_PM0_IF_MODE, ENETC_PM0_IFM_XGMII);
508 }
509 
510 static void enetc_configure_port_pmac(struct enetc_hw *hw)
511 {
512 	u32 temp;
513 
514 	/* Set pMAC step lock */
515 	temp = enetc_port_rd(hw, ENETC_PFPMR);
516 	enetc_port_wr(hw, ENETC_PFPMR,
517 		      temp | ENETC_PFPMR_PMACE | ENETC_PFPMR_MWLM);
518 
519 	temp = enetc_port_rd(hw, ENETC_MMCSR);
520 	enetc_port_wr(hw, ENETC_MMCSR, temp | ENETC_MMCSR_ME);
521 }
522 
523 static void enetc_configure_port(struct enetc_pf *pf)
524 {
525 	u8 hash_key[ENETC_RSSHASH_KEY_SIZE];
526 	struct enetc_hw *hw = &pf->si->hw;
527 
528 	enetc_configure_port_pmac(hw);
529 
530 	enetc_configure_port_mac(hw, pf->if_mode);
531 
532 	enetc_port_si_configure(pf->si);
533 
534 	/* set up hash key */
535 	get_random_bytes(hash_key, ENETC_RSSHASH_KEY_SIZE);
536 	enetc_set_rss_key(hw, hash_key);
537 
538 	/* split up RFS entries */
539 	enetc_port_assign_rfs_entries(pf->si);
540 
541 	/* fix-up primary MAC addresses, if not set already */
542 	enetc_port_setup_primary_mac_address(pf->si);
543 
544 	/* enforce VLAN promisc mode for all SIs */
545 	pf->vlan_promisc_simap = ENETC_VLAN_PROMISC_MAP_ALL;
546 	enetc_set_vlan_promisc(hw, pf->vlan_promisc_simap);
547 
548 	enetc_port_wr(hw, ENETC_PSIPMR, 0);
549 
550 	/* enable port */
551 	enetc_port_wr(hw, ENETC_PMR, ENETC_PMR_EN);
552 }
553 
554 /* Messaging */
555 static u16 enetc_msg_pf_set_vf_primary_mac_addr(struct enetc_pf *pf,
556 						int vf_id)
557 {
558 	struct enetc_vf_state *vf_state = &pf->vf_state[vf_id];
559 	struct enetc_msg_swbd *msg = &pf->rxmsg[vf_id];
560 	struct enetc_msg_cmd_set_primary_mac *cmd;
561 	struct device *dev = &pf->si->pdev->dev;
562 	u16 cmd_id;
563 	char *addr;
564 
565 	cmd = (struct enetc_msg_cmd_set_primary_mac *)msg->vaddr;
566 	cmd_id = cmd->header.id;
567 	if (cmd_id != ENETC_MSG_CMD_MNG_ADD)
568 		return ENETC_MSG_CMD_STATUS_FAIL;
569 
570 	addr = cmd->mac.sa_data;
571 	if (vf_state->flags & ENETC_VF_FLAG_PF_SET_MAC)
572 		dev_warn(dev, "Attempt to override PF set mac addr for VF%d\n",
573 			 vf_id);
574 	else
575 		enetc_pf_set_primary_mac_addr(&pf->si->hw, vf_id + 1, addr);
576 
577 	return ENETC_MSG_CMD_STATUS_OK;
578 }
579 
580 void enetc_msg_handle_rxmsg(struct enetc_pf *pf, int vf_id, u16 *status)
581 {
582 	struct enetc_msg_swbd *msg = &pf->rxmsg[vf_id];
583 	struct device *dev = &pf->si->pdev->dev;
584 	struct enetc_msg_cmd_header *cmd_hdr;
585 	u16 cmd_type;
586 
587 	*status = ENETC_MSG_CMD_STATUS_OK;
588 	cmd_hdr = (struct enetc_msg_cmd_header *)msg->vaddr;
589 	cmd_type = cmd_hdr->type;
590 
591 	switch (cmd_type) {
592 	case ENETC_MSG_CMD_MNG_MAC:
593 		*status = enetc_msg_pf_set_vf_primary_mac_addr(pf, vf_id);
594 		break;
595 	default:
596 		dev_err(dev, "command not supported (cmd_type: 0x%x)\n",
597 			cmd_type);
598 	}
599 }
600 
601 #ifdef CONFIG_PCI_IOV
602 static int enetc_sriov_configure(struct pci_dev *pdev, int num_vfs)
603 {
604 	struct enetc_si *si = pci_get_drvdata(pdev);
605 	struct enetc_pf *pf = enetc_si_priv(si);
606 	int err;
607 
608 	if (!num_vfs) {
609 		enetc_msg_psi_free(pf);
610 		kfree(pf->vf_state);
611 		pf->num_vfs = 0;
612 		pci_disable_sriov(pdev);
613 	} else {
614 		pf->num_vfs = num_vfs;
615 
616 		pf->vf_state = kcalloc(num_vfs, sizeof(struct enetc_vf_state),
617 				       GFP_KERNEL);
618 		if (!pf->vf_state) {
619 			pf->num_vfs = 0;
620 			return -ENOMEM;
621 		}
622 
623 		err = enetc_msg_psi_init(pf);
624 		if (err) {
625 			dev_err(&pdev->dev, "enetc_msg_psi_init (%d)\n", err);
626 			goto err_msg_psi;
627 		}
628 
629 		err = pci_enable_sriov(pdev, num_vfs);
630 		if (err) {
631 			dev_err(&pdev->dev, "pci_enable_sriov err %d\n", err);
632 			goto err_en_sriov;
633 		}
634 	}
635 
636 	return num_vfs;
637 
638 err_en_sriov:
639 	enetc_msg_psi_free(pf);
640 err_msg_psi:
641 	kfree(pf->vf_state);
642 	pf->num_vfs = 0;
643 
644 	return err;
645 }
646 #else
647 #define enetc_sriov_configure(pdev, num_vfs)	(void)0
648 #endif
649 
650 static int enetc_pf_set_features(struct net_device *ndev,
651 				 netdev_features_t features)
652 {
653 	netdev_features_t changed = ndev->features ^ features;
654 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
655 
656 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
657 		struct enetc_pf *pf = enetc_si_priv(priv->si);
658 
659 		if (!!(features & NETIF_F_HW_VLAN_CTAG_FILTER))
660 			enetc_disable_si_vlan_promisc(pf, 0);
661 		else
662 			enetc_enable_si_vlan_promisc(pf, 0);
663 	}
664 
665 	if (changed & NETIF_F_LOOPBACK)
666 		enetc_set_loopback(ndev, !!(features & NETIF_F_LOOPBACK));
667 
668 	return enetc_set_features(ndev, features);
669 }
670 
671 static const struct net_device_ops enetc_ndev_ops = {
672 	.ndo_open		= enetc_open,
673 	.ndo_stop		= enetc_close,
674 	.ndo_start_xmit		= enetc_xmit,
675 	.ndo_get_stats		= enetc_get_stats,
676 	.ndo_set_mac_address	= enetc_pf_set_mac_addr,
677 	.ndo_set_rx_mode	= enetc_pf_set_rx_mode,
678 	.ndo_vlan_rx_add_vid	= enetc_vlan_rx_add_vid,
679 	.ndo_vlan_rx_kill_vid	= enetc_vlan_rx_del_vid,
680 	.ndo_set_vf_mac		= enetc_pf_set_vf_mac,
681 	.ndo_set_vf_vlan	= enetc_pf_set_vf_vlan,
682 	.ndo_set_vf_spoofchk	= enetc_pf_set_vf_spoofchk,
683 	.ndo_set_features	= enetc_pf_set_features,
684 	.ndo_do_ioctl		= enetc_ioctl,
685 	.ndo_setup_tc		= enetc_setup_tc,
686 };
687 
688 static void enetc_pf_netdev_setup(struct enetc_si *si, struct net_device *ndev,
689 				  const struct net_device_ops *ndev_ops)
690 {
691 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
692 
693 	SET_NETDEV_DEV(ndev, &si->pdev->dev);
694 	priv->ndev = ndev;
695 	priv->si = si;
696 	priv->dev = &si->pdev->dev;
697 	si->ndev = ndev;
698 
699 	priv->msg_enable = (NETIF_MSG_WOL << 1) - 1;
700 	ndev->netdev_ops = ndev_ops;
701 	enetc_set_ethtool_ops(ndev);
702 	ndev->watchdog_timeo = 5 * HZ;
703 	ndev->max_mtu = ENETC_MAX_MTU;
704 
705 	ndev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM | NETIF_F_HW_CSUM |
706 			    NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
707 			    NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_LOOPBACK;
708 	ndev->features = NETIF_F_HIGHDMA | NETIF_F_SG |
709 			 NETIF_F_RXCSUM | NETIF_F_HW_CSUM |
710 			 NETIF_F_HW_VLAN_CTAG_TX |
711 			 NETIF_F_HW_VLAN_CTAG_RX;
712 
713 	if (si->num_rss)
714 		ndev->hw_features |= NETIF_F_RXHASH;
715 
716 	if (si->errata & ENETC_ERR_TXCSUM) {
717 		ndev->hw_features &= ~NETIF_F_HW_CSUM;
718 		ndev->features &= ~NETIF_F_HW_CSUM;
719 	}
720 
721 	ndev->priv_flags |= IFF_UNICAST_FLT;
722 
723 	if (si->hw_features & ENETC_SI_F_QBV)
724 		priv->active_offloads |= ENETC_F_QBV;
725 
726 	if (si->hw_features & ENETC_SI_F_PSFP && !enetc_psfp_enable(priv)) {
727 		priv->active_offloads |= ENETC_F_QCI;
728 		ndev->features |= NETIF_F_HW_TC;
729 		ndev->hw_features |= NETIF_F_HW_TC;
730 	}
731 
732 	/* pick up primary MAC address from SI */
733 	enetc_get_primary_mac_addr(&si->hw, ndev->dev_addr);
734 }
735 
736 static int enetc_mdio_probe(struct enetc_pf *pf)
737 {
738 	struct device *dev = &pf->si->pdev->dev;
739 	struct enetc_mdio_priv *mdio_priv;
740 	struct device_node *np;
741 	struct mii_bus *bus;
742 	int err;
743 
744 	bus = devm_mdiobus_alloc_size(dev, sizeof(*mdio_priv));
745 	if (!bus)
746 		return -ENOMEM;
747 
748 	bus->name = "Freescale ENETC MDIO Bus";
749 	bus->read = enetc_mdio_read;
750 	bus->write = enetc_mdio_write;
751 	bus->parent = dev;
752 	mdio_priv = bus->priv;
753 	mdio_priv->hw = &pf->si->hw;
754 	mdio_priv->mdio_base = ENETC_EMDIO_BASE;
755 	snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
756 
757 	np = of_get_child_by_name(dev->of_node, "mdio");
758 	if (!np) {
759 		dev_err(dev, "MDIO node missing\n");
760 		return -EINVAL;
761 	}
762 
763 	err = of_mdiobus_register(bus, np);
764 	if (err) {
765 		of_node_put(np);
766 		dev_err(dev, "cannot register MDIO bus\n");
767 		return err;
768 	}
769 
770 	of_node_put(np);
771 	pf->mdio = bus;
772 
773 	return 0;
774 }
775 
776 static void enetc_mdio_remove(struct enetc_pf *pf)
777 {
778 	if (pf->mdio)
779 		mdiobus_unregister(pf->mdio);
780 }
781 
782 static int enetc_of_get_phy(struct enetc_pf *pf)
783 {
784 	struct device *dev = &pf->si->pdev->dev;
785 	struct device_node *np = dev->of_node;
786 	struct device_node *mdio_np;
787 	int err;
788 
789 	pf->phy_node = of_parse_phandle(np, "phy-handle", 0);
790 	if (!pf->phy_node) {
791 		if (!of_phy_is_fixed_link(np)) {
792 			dev_err(dev, "PHY not specified\n");
793 			return -ENODEV;
794 		}
795 
796 		err = of_phy_register_fixed_link(np);
797 		if (err < 0) {
798 			dev_err(dev, "fixed link registration failed\n");
799 			return err;
800 		}
801 
802 		pf->phy_node = of_node_get(np);
803 	}
804 
805 	mdio_np = of_get_child_by_name(np, "mdio");
806 	if (mdio_np) {
807 		of_node_put(mdio_np);
808 		err = enetc_mdio_probe(pf);
809 		if (err) {
810 			of_node_put(pf->phy_node);
811 			return err;
812 		}
813 	}
814 
815 	err = of_get_phy_mode(np, &pf->if_mode);
816 	if (err) {
817 		dev_err(dev, "missing phy type\n");
818 		of_node_put(pf->phy_node);
819 		if (of_phy_is_fixed_link(np))
820 			of_phy_deregister_fixed_link(np);
821 		else
822 			enetc_mdio_remove(pf);
823 
824 		return -EINVAL;
825 	}
826 
827 	return 0;
828 }
829 
830 static void enetc_of_put_phy(struct enetc_pf *pf)
831 {
832 	struct device_node *np = pf->si->pdev->dev.of_node;
833 
834 	if (np && of_phy_is_fixed_link(np))
835 		of_phy_deregister_fixed_link(np);
836 	if (pf->phy_node)
837 		of_node_put(pf->phy_node);
838 }
839 
840 static int enetc_imdio_init(struct enetc_pf *pf, bool is_c45)
841 {
842 	struct device *dev = &pf->si->pdev->dev;
843 	struct enetc_mdio_priv *mdio_priv;
844 	struct phy_device *pcs;
845 	struct mii_bus *bus;
846 	int err;
847 
848 	bus = mdiobus_alloc_size(sizeof(*mdio_priv));
849 	if (!bus)
850 		return -ENOMEM;
851 
852 	bus->name = "Freescale ENETC internal MDIO Bus";
853 	bus->read = enetc_mdio_read;
854 	bus->write = enetc_mdio_write;
855 	bus->parent = dev;
856 	bus->phy_mask = ~0;
857 	mdio_priv = bus->priv;
858 	mdio_priv->hw = &pf->si->hw;
859 	mdio_priv->mdio_base = ENETC_PM_IMDIO_BASE;
860 	snprintf(bus->id, MII_BUS_ID_SIZE, "%s-imdio", dev_name(dev));
861 
862 	err = mdiobus_register(bus);
863 	if (err) {
864 		dev_err(dev, "cannot register internal MDIO bus (%d)\n", err);
865 		goto free_mdio_bus;
866 	}
867 
868 	pcs = get_phy_device(bus, 0, is_c45);
869 	if (IS_ERR(pcs)) {
870 		err = PTR_ERR(pcs);
871 		dev_err(dev, "cannot get internal PCS PHY (%d)\n", err);
872 		goto unregister_mdiobus;
873 	}
874 
875 	pf->imdio = bus;
876 	pf->pcs = pcs;
877 
878 	return 0;
879 
880 unregister_mdiobus:
881 	mdiobus_unregister(bus);
882 free_mdio_bus:
883 	mdiobus_free(bus);
884 	return err;
885 }
886 
887 static void enetc_imdio_remove(struct enetc_pf *pf)
888 {
889 	if (pf->pcs)
890 		put_device(&pf->pcs->mdio.dev);
891 	if (pf->imdio) {
892 		mdiobus_unregister(pf->imdio);
893 		mdiobus_free(pf->imdio);
894 	}
895 }
896 
897 static void enetc_configure_sgmii(struct phy_device *pcs)
898 {
899 	/* SGMII spec requires tx_config_Reg[15:0] to be exactly 0x4001
900 	 * for the MAC PCS in order to acknowledge the AN.
901 	 */
902 	phy_write(pcs, MII_ADVERTISE, ADVERTISE_SGMII | ADVERTISE_LPACK);
903 
904 	phy_write(pcs, ENETC_PCS_IF_MODE,
905 		  ENETC_PCS_IF_MODE_SGMII_EN |
906 		  ENETC_PCS_IF_MODE_USE_SGMII_AN);
907 
908 	/* Adjust link timer for SGMII */
909 	phy_write(pcs, ENETC_PCS_LINK_TIMER1, ENETC_PCS_LINK_TIMER1_VAL);
910 	phy_write(pcs, ENETC_PCS_LINK_TIMER2, ENETC_PCS_LINK_TIMER2_VAL);
911 
912 	phy_write(pcs, MII_BMCR, BMCR_ANRESTART | BMCR_ANENABLE);
913 }
914 
915 static void enetc_configure_2500basex(struct phy_device *pcs)
916 {
917 	phy_write(pcs, ENETC_PCS_IF_MODE,
918 		  ENETC_PCS_IF_MODE_SGMII_EN |
919 		  ENETC_PCS_IF_MODE_SGMII_SPEED(ENETC_PCS_SPEED_2500));
920 
921 	phy_write(pcs, MII_BMCR, BMCR_SPEED1000 | BMCR_FULLDPLX | BMCR_RESET);
922 }
923 
924 static void enetc_configure_usxgmii(struct phy_device *pcs)
925 {
926 	/* Configure device ability for the USXGMII Replicator */
927 	phy_write_mmd(pcs, MDIO_MMD_VEND2, MII_ADVERTISE,
928 		      ADVERTISE_SGMII | ADVERTISE_LPACK |
929 		      MDIO_USXGMII_FULL_DUPLEX);
930 
931 	/* Restart PCS AN */
932 	phy_write_mmd(pcs, MDIO_MMD_VEND2, MII_BMCR,
933 		      BMCR_RESET | BMCR_ANENABLE | BMCR_ANRESTART);
934 }
935 
936 static int enetc_configure_serdes(struct enetc_ndev_priv *priv)
937 {
938 	bool is_c45 = priv->if_mode == PHY_INTERFACE_MODE_USXGMII;
939 	struct enetc_pf *pf = enetc_si_priv(priv->si);
940 	int err;
941 
942 	if (priv->if_mode != PHY_INTERFACE_MODE_SGMII &&
943 	    priv->if_mode != PHY_INTERFACE_MODE_2500BASEX &&
944 	    priv->if_mode != PHY_INTERFACE_MODE_USXGMII)
945 		return 0;
946 
947 	err = enetc_imdio_init(pf, is_c45);
948 	if (err)
949 		return err;
950 
951 	switch (priv->if_mode) {
952 	case PHY_INTERFACE_MODE_SGMII:
953 		enetc_configure_sgmii(pf->pcs);
954 		break;
955 	case PHY_INTERFACE_MODE_2500BASEX:
956 		enetc_configure_2500basex(pf->pcs);
957 		break;
958 	case PHY_INTERFACE_MODE_USXGMII:
959 		enetc_configure_usxgmii(pf->pcs);
960 		break;
961 	default:
962 		dev_err(&pf->si->pdev->dev, "Unsupported link mode %s\n",
963 			phy_modes(priv->if_mode));
964 	}
965 
966 	return 0;
967 }
968 
969 static void enetc_teardown_serdes(struct enetc_ndev_priv *priv)
970 {
971 	struct enetc_pf *pf = enetc_si_priv(priv->si);
972 
973 	enetc_imdio_remove(pf);
974 }
975 
976 static int enetc_pf_probe(struct pci_dev *pdev,
977 			  const struct pci_device_id *ent)
978 {
979 	struct enetc_ndev_priv *priv;
980 	struct net_device *ndev;
981 	struct enetc_si *si;
982 	struct enetc_pf *pf;
983 	int err;
984 
985 	if (pdev->dev.of_node && !of_device_is_available(pdev->dev.of_node)) {
986 		dev_info(&pdev->dev, "device is disabled, skipping\n");
987 		return -ENODEV;
988 	}
989 
990 	err = enetc_pci_probe(pdev, KBUILD_MODNAME, sizeof(*pf));
991 	if (err) {
992 		dev_err(&pdev->dev, "PCI probing failed\n");
993 		return err;
994 	}
995 
996 	si = pci_get_drvdata(pdev);
997 	if (!si->hw.port || !si->hw.global) {
998 		err = -ENODEV;
999 		dev_err(&pdev->dev, "could not map PF space, probing a VF?\n");
1000 		goto err_map_pf_space;
1001 	}
1002 
1003 	pf = enetc_si_priv(si);
1004 	pf->si = si;
1005 	pf->total_vfs = pci_sriov_get_totalvfs(pdev);
1006 
1007 	err = enetc_of_get_phy(pf);
1008 	if (err)
1009 		dev_warn(&pdev->dev, "Fallback to PHY-less operation\n");
1010 
1011 	enetc_configure_port(pf);
1012 
1013 	enetc_get_si_caps(si);
1014 
1015 	ndev = alloc_etherdev_mq(sizeof(*priv), ENETC_MAX_NUM_TXQS);
1016 	if (!ndev) {
1017 		err = -ENOMEM;
1018 		dev_err(&pdev->dev, "netdev creation failed\n");
1019 		goto err_alloc_netdev;
1020 	}
1021 
1022 	enetc_pf_netdev_setup(si, ndev, &enetc_ndev_ops);
1023 
1024 	priv = netdev_priv(ndev);
1025 	priv->phy_node = pf->phy_node;
1026 	priv->if_mode = pf->if_mode;
1027 
1028 	enetc_init_si_rings_params(priv);
1029 
1030 	err = enetc_alloc_si_resources(priv);
1031 	if (err) {
1032 		dev_err(&pdev->dev, "SI resource alloc failed\n");
1033 		goto err_alloc_si_res;
1034 	}
1035 
1036 	err = enetc_alloc_msix(priv);
1037 	if (err) {
1038 		dev_err(&pdev->dev, "MSIX alloc failed\n");
1039 		goto err_alloc_msix;
1040 	}
1041 
1042 	err = enetc_configure_serdes(priv);
1043 	if (err)
1044 		dev_warn(&pdev->dev, "Attempted SerDes config but failed\n");
1045 
1046 	err = register_netdev(ndev);
1047 	if (err)
1048 		goto err_reg_netdev;
1049 
1050 	netif_carrier_off(ndev);
1051 
1052 	return 0;
1053 
1054 err_reg_netdev:
1055 	enetc_teardown_serdes(priv);
1056 	enetc_mdio_remove(pf);
1057 	enetc_free_msix(priv);
1058 err_alloc_msix:
1059 	enetc_free_si_resources(priv);
1060 err_alloc_si_res:
1061 	si->ndev = NULL;
1062 	free_netdev(ndev);
1063 err_alloc_netdev:
1064 	enetc_of_put_phy(pf);
1065 err_map_pf_space:
1066 	enetc_pci_remove(pdev);
1067 
1068 	return err;
1069 }
1070 
1071 static void enetc_pf_remove(struct pci_dev *pdev)
1072 {
1073 	struct enetc_si *si = pci_get_drvdata(pdev);
1074 	struct enetc_pf *pf = enetc_si_priv(si);
1075 	struct enetc_ndev_priv *priv;
1076 
1077 	if (pf->num_vfs)
1078 		enetc_sriov_configure(pdev, 0);
1079 
1080 	priv = netdev_priv(si->ndev);
1081 	unregister_netdev(si->ndev);
1082 
1083 	enetc_teardown_serdes(priv);
1084 	enetc_mdio_remove(pf);
1085 	enetc_of_put_phy(pf);
1086 
1087 	enetc_free_msix(priv);
1088 
1089 	enetc_free_si_resources(priv);
1090 
1091 	free_netdev(si->ndev);
1092 
1093 	enetc_pci_remove(pdev);
1094 }
1095 
1096 static const struct pci_device_id enetc_pf_id_table[] = {
1097 	{ PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, ENETC_DEV_ID_PF) },
1098 	{ 0, } /* End of table. */
1099 };
1100 MODULE_DEVICE_TABLE(pci, enetc_pf_id_table);
1101 
1102 static struct pci_driver enetc_pf_driver = {
1103 	.name = KBUILD_MODNAME,
1104 	.id_table = enetc_pf_id_table,
1105 	.probe = enetc_pf_probe,
1106 	.remove = enetc_pf_remove,
1107 #ifdef CONFIG_PCI_IOV
1108 	.sriov_configure = enetc_sriov_configure,
1109 #endif
1110 };
1111 module_pci_driver(enetc_pf_driver);
1112 
1113 MODULE_DESCRIPTION(ENETC_DRV_NAME_STR);
1114 MODULE_LICENSE("Dual BSD/GPL");
1115