1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2024 NXP */
3
4 #include <linux/clk.h>
5 #include <linux/module.h>
6 #include <linux/of_net.h>
7 #include <linux/of_platform.h>
8 #include <linux/unaligned.h>
9
10 #include "enetc_pf_common.h"
11 #include "enetc4_debugfs.h"
12
13 #define ENETC_SI_MAX_RING_NUM 8
14
15 #define ENETC_MAC_FILTER_TYPE_UC BIT(0)
16 #define ENETC_MAC_FILTER_TYPE_MC BIT(1)
17 #define ENETC_MAC_FILTER_TYPE_ALL (ENETC_MAC_FILTER_TYPE_UC | \
18 ENETC_MAC_FILTER_TYPE_MC)
19
enetc4_get_port_caps(struct enetc_pf * pf)20 static void enetc4_get_port_caps(struct enetc_pf *pf)
21 {
22 struct enetc_hw *hw = &pf->si->hw;
23 u32 val;
24
25 val = enetc_port_rd(hw, ENETC4_ECAPR1);
26 pf->caps.num_msix = ((val & ECAPR1_NUM_MSIX) >> 12) + 1;
27
28 val = enetc_port_rd(hw, ENETC4_ECAPR2);
29 pf->caps.num_rx_bdr = (val & ECAPR2_NUM_RX_BDR) >> 16;
30 pf->caps.num_tx_bdr = val & ECAPR2_NUM_TX_BDR;
31 }
32
enetc4_get_psi_hw_features(struct enetc_si * si)33 static void enetc4_get_psi_hw_features(struct enetc_si *si)
34 {
35 struct enetc_hw *hw = &si->hw;
36 u32 val;
37
38 val = enetc_port_rd(hw, ENETC4_PCAPR);
39 if (val & PCAPR_LINK_TYPE)
40 si->hw_features |= ENETC_SI_F_PPM;
41 }
42
enetc4_pf_set_si_primary_mac(struct enetc_hw * hw,int si,const u8 * addr)43 static void enetc4_pf_set_si_primary_mac(struct enetc_hw *hw, int si,
44 const u8 *addr)
45 {
46 u16 lower = get_unaligned_le16(addr + 4);
47 u32 upper = get_unaligned_le32(addr);
48
49 if (si != 0) {
50 __raw_writel(upper, hw->port + ENETC4_PSIPMAR0(si));
51 __raw_writel(lower, hw->port + ENETC4_PSIPMAR1(si));
52 } else {
53 __raw_writel(upper, hw->port + ENETC4_PMAR0);
54 __raw_writel(lower, hw->port + ENETC4_PMAR1);
55 }
56 }
57
enetc4_pf_get_si_primary_mac(struct enetc_hw * hw,int si,u8 * addr)58 static void enetc4_pf_get_si_primary_mac(struct enetc_hw *hw, int si,
59 u8 *addr)
60 {
61 u32 upper;
62 u16 lower;
63
64 upper = __raw_readl(hw->port + ENETC4_PSIPMAR0(si));
65 lower = __raw_readl(hw->port + ENETC4_PSIPMAR1(si));
66
67 put_unaligned_le32(upper, addr);
68 put_unaligned_le16(lower, addr + 4);
69 }
70
enetc4_pf_set_loopback(struct net_device * ndev,bool en)71 static void enetc4_pf_set_loopback(struct net_device *ndev, bool en)
72 {
73 struct enetc_ndev_priv *priv = netdev_priv(ndev);
74 struct enetc_si *si = priv->si;
75 u32 val;
76
77 val = enetc_port_mac_rd(si, ENETC4_PM_CMD_CFG(0));
78 val = u32_replace_bits(val, en ? 1 : 0, PM_CMD_CFG_LOOP_EN);
79 /* Default to select MAC level loopback mode if loopback is enabled. */
80 val = u32_replace_bits(val, en ? LPBCK_MODE_MAC_LEVEL : 0,
81 PM_CMD_CFG_LPBK_MODE);
82
83 enetc_port_mac_wr(si, ENETC4_PM_CMD_CFG(0), val);
84 }
85
enetc4_pf_clear_maft_entries(struct enetc_pf * pf)86 static void enetc4_pf_clear_maft_entries(struct enetc_pf *pf)
87 {
88 struct ntmp_user *user = &pf->si->ntmp_user;
89 u32 entry_id;
90
91 for_each_set_bit(entry_id, user->maft_eid_bitmap,
92 user->maft_num_entries) {
93 if (!ntmp_maft_delete_entry(user, entry_id))
94 ntmp_clear_eid_bitmap(user->maft_eid_bitmap, entry_id);
95 }
96 }
97
enetc4_pf_add_maft_entries(struct enetc_pf * pf,struct netdev_hw_addr_list * uc)98 static int enetc4_pf_add_maft_entries(struct enetc_pf *pf,
99 struct netdev_hw_addr_list *uc)
100 {
101 struct ntmp_user *user = &pf->si->ntmp_user;
102 int mac_cnt = netdev_hw_addr_list_count(uc);
103 struct maft_entry_data maft = {};
104 struct netdev_hw_addr *ha;
105 u32 available_entries;
106 u16 si_bit = BIT(0);
107 u32 entry_id;
108 int err;
109
110 available_entries = user->maft_num_entries -
111 bitmap_weight(user->maft_eid_bitmap,
112 user->maft_num_entries);
113
114 if (mac_cnt > available_entries)
115 return -ENOSPC;
116
117 maft.cfge.si_bitmap = cpu_to_le16(si_bit);
118 netdev_hw_addr_list_for_each(ha, uc) {
119 entry_id = ntmp_lookup_free_eid(user->maft_eid_bitmap,
120 user->maft_num_entries);
121 ether_addr_copy(maft.keye.mac_addr, ha->addr);
122 err = ntmp_maft_add_entry(user, entry_id, &maft);
123 if (unlikely(err)) {
124 ntmp_clear_eid_bitmap(user->maft_eid_bitmap, entry_id);
125 goto clear_maft_entries;
126 }
127 }
128
129 return 0;
130
131 clear_maft_entries:
132 enetc4_pf_clear_maft_entries(pf);
133
134 return err;
135 }
136
enetc4_pf_set_uc_hash_filter(struct enetc_pf * pf,struct netdev_hw_addr_list * uc)137 static void enetc4_pf_set_uc_hash_filter(struct enetc_pf *pf,
138 struct netdev_hw_addr_list *uc)
139 {
140 struct enetc_mac_filter *mac_filter = &pf->mac_filter[UC];
141 struct netdev_hw_addr *ha;
142 u64 hash;
143
144 enetc_reset_mac_addr_filter(mac_filter);
145 netdev_hw_addr_list_for_each(ha, uc)
146 enetc_add_mac_addr_ht_filter(mac_filter, ha->addr);
147
148 bitmap_to_arr64(&hash, mac_filter->mac_hash_table,
149 ENETC_MADDR_HASH_TBL_SZ);
150 enetc_set_si_uc_hash_filter(pf->si, 0, hash);
151 }
152
enetc4_pf_set_uc_exact_filter(struct enetc_pf * pf,struct netdev_hw_addr_list * uc)153 static int enetc4_pf_set_uc_exact_filter(struct enetc_pf *pf,
154 struct netdev_hw_addr_list *uc)
155 {
156 struct enetc_si *si = pf->si;
157 int err;
158
159 if (netdev_hw_addr_list_empty(uc)) {
160 /* clear both MAC hash and exact filters */
161 enetc_set_si_uc_hash_filter(si, 0, 0);
162 enetc4_pf_clear_maft_entries(pf);
163
164 return 0;
165 }
166
167 /* Set temporary unicast hash filter in case of Rx loss when
168 * updating MAC address filter table
169 */
170 enetc4_pf_set_uc_hash_filter(pf, uc);
171 enetc4_pf_clear_maft_entries(pf);
172
173 err = enetc4_pf_add_maft_entries(pf, uc);
174 if (!err) {
175 enetc_reset_mac_addr_filter(&pf->mac_filter[UC]);
176 enetc_set_si_uc_hash_filter(si, 0, 0);
177 }
178
179 return err;
180 }
181
enetc4_pf_set_mc_hash_filter(struct enetc_pf * pf,struct netdev_hw_addr_list * mc)182 static void enetc4_pf_set_mc_hash_filter(struct enetc_pf *pf,
183 struct netdev_hw_addr_list *mc)
184 {
185 struct enetc_mac_filter *mac_filter = &pf->mac_filter[MC];
186 struct netdev_hw_addr *ha;
187 u64 hash;
188
189 enetc_reset_mac_addr_filter(mac_filter);
190 netdev_hw_addr_list_for_each(ha, mc)
191 enetc_add_mac_addr_ht_filter(mac_filter, ha->addr);
192
193 bitmap_to_arr64(&hash, mac_filter->mac_hash_table,
194 ENETC_MADDR_HASH_TBL_SZ);
195 enetc_set_si_mc_hash_filter(pf->si, 0, hash);
196 }
197
enetc4_pf_set_mac_filter(struct enetc_pf * pf,int type,struct netdev_hw_addr_list * uc,struct netdev_hw_addr_list * mc)198 static void enetc4_pf_set_mac_filter(struct enetc_pf *pf, int type,
199 struct netdev_hw_addr_list *uc,
200 struct netdev_hw_addr_list *mc)
201 {
202 /* Currently, the MAC address filter table (MAFT) only has 4 entries,
203 * and multiple multicast addresses for filtering will be configured
204 * in the default network configuration, so MAFT is only suitable for
205 * unicast filtering. If the number of unicast addresses exceeds the
206 * table capacity, the MAC hash filter will be used.
207 */
208 if (type & ENETC_MAC_FILTER_TYPE_UC &&
209 enetc4_pf_set_uc_exact_filter(pf, uc)) {
210 /* Fall back to the MAC hash filter */
211 enetc4_pf_set_uc_hash_filter(pf, uc);
212 /* Clear the old MAC exact filter */
213 enetc4_pf_clear_maft_entries(pf);
214 }
215
216 if (type & ENETC_MAC_FILTER_TYPE_MC)
217 enetc4_pf_set_mc_hash_filter(pf, mc);
218 }
219
220 static const struct enetc_pf_ops enetc4_pf_ops = {
221 .set_si_primary_mac = enetc4_pf_set_si_primary_mac,
222 .get_si_primary_mac = enetc4_pf_get_si_primary_mac,
223 };
224
enetc4_pf_struct_init(struct enetc_si * si)225 static int enetc4_pf_struct_init(struct enetc_si *si)
226 {
227 struct enetc_pf *pf = enetc_si_priv(si);
228
229 pf->si = si;
230 pf->total_vfs = pci_sriov_get_totalvfs(si->pdev);
231 pf->ops = &enetc4_pf_ops;
232
233 enetc4_get_port_caps(pf);
234 enetc4_get_psi_hw_features(si);
235
236 return 0;
237 }
238
enetc4_psicfgr0_val_construct(bool is_vf,u32 num_tx_bdr,u32 num_rx_bdr)239 static u32 enetc4_psicfgr0_val_construct(bool is_vf, u32 num_tx_bdr, u32 num_rx_bdr)
240 {
241 u32 val;
242
243 val = ENETC_PSICFGR0_SET_TXBDR(num_tx_bdr);
244 val |= ENETC_PSICFGR0_SET_RXBDR(num_rx_bdr);
245 val |= ENETC_PSICFGR0_SIVC(ENETC_VLAN_TYPE_C | ENETC_VLAN_TYPE_S);
246
247 if (is_vf)
248 val |= ENETC_PSICFGR0_VTE | ENETC_PSICFGR0_SIVIE;
249
250 return val;
251 }
252
enetc4_default_rings_allocation(struct enetc_pf * pf)253 static void enetc4_default_rings_allocation(struct enetc_pf *pf)
254 {
255 struct enetc_hw *hw = &pf->si->hw;
256 u32 num_rx_bdr, num_tx_bdr, val;
257 int num_vfs = pf->total_vfs;
258 u32 vf_tx_bdr, vf_rx_bdr;
259 int i, rx_rem, tx_rem;
260
261 if (pf->caps.num_rx_bdr < ENETC_SI_MAX_RING_NUM + num_vfs)
262 num_rx_bdr = pf->caps.num_rx_bdr - num_vfs;
263 else
264 num_rx_bdr = ENETC_SI_MAX_RING_NUM;
265
266 if (pf->caps.num_tx_bdr < ENETC_SI_MAX_RING_NUM + num_vfs)
267 num_tx_bdr = pf->caps.num_tx_bdr - num_vfs;
268 else
269 num_tx_bdr = ENETC_SI_MAX_RING_NUM;
270
271 val = enetc4_psicfgr0_val_construct(false, num_tx_bdr, num_rx_bdr);
272 enetc_port_wr(hw, ENETC4_PSICFGR0(0), val);
273
274 if (!num_vfs)
275 return;
276
277 num_rx_bdr = pf->caps.num_rx_bdr - num_rx_bdr;
278 rx_rem = num_rx_bdr % num_vfs;
279 num_rx_bdr = num_rx_bdr / num_vfs;
280
281 num_tx_bdr = pf->caps.num_tx_bdr - num_tx_bdr;
282 tx_rem = num_tx_bdr % num_vfs;
283 num_tx_bdr = num_tx_bdr / num_vfs;
284
285 for (i = 0; i < num_vfs; i++) {
286 vf_tx_bdr = (i < tx_rem) ? num_tx_bdr + 1 : num_tx_bdr;
287 vf_rx_bdr = (i < rx_rem) ? num_rx_bdr + 1 : num_rx_bdr;
288 val = enetc4_psicfgr0_val_construct(true, vf_tx_bdr, vf_rx_bdr);
289 enetc_port_wr(hw, ENETC4_PSICFGR0(i + 1), val);
290 }
291 }
292
enetc4_allocate_si_rings(struct enetc_pf * pf)293 static void enetc4_allocate_si_rings(struct enetc_pf *pf)
294 {
295 enetc4_default_rings_allocation(pf);
296 }
297
298 /* Allocate the number of MSI-X vectors for per SI. */
enetc4_set_si_msix_num(struct enetc_pf * pf)299 static void enetc4_set_si_msix_num(struct enetc_pf *pf)
300 {
301 int valid_num_si = pf->total_vfs + 1;
302 struct enetc_hw *hw = &pf->si->hw;
303 int i, num_msix, num_vsi;
304 u32 val;
305
306 val = enetc_port_rd(hw, ENETC4_ECAPR1);
307 num_vsi = FIELD_GET(ECAPR1_NUM_VSI, val);
308
309 /* The PSIaCFGR2[NUM_MSIX] indicates the number of MSI-X allocated to
310 * the SI is NUM_MSIX + 1, so the minimum number of MSI-X allocated to
311 * each SI is 1. The total number of MSI-X allocated to PSI and VSIs
312 * cannot exceed the total number of MSI-X owned by this ENETC, which
313 * is ECAPR1[NUM_MSIX]. Otherwise, when multiple ENETC instances exist,
314 * it will affect other ENETCs whose MSI-X interrupts cannot be
315 * generated. This is similar to out-of-bounds array access: the array
316 * itself is not affected, but adjacent arrays will be corrupted.
317 *
318 * pf->total_vfs is 0 if CONFIG_PCI_IOV is disabled. If the hardware
319 * itself supports SR-IOV, then when allocating the number of MSIXs to
320 * the SI, it must be taken into account that the VSI has at least 1
321 * MSIX, and the total number of MSIXs of all SIs cannot exceed
322 * ECAPR1[NUM_MSIX].
323 */
324 if (!pf->total_vfs && num_vsi) {
325 /* Because each SI has at least one MSIX, and from the hardware
326 * perspective, pf->caps.num_msix will always be greater than
327 * num_vsi. So num_msix is always greater than or equal to 0.
328 */
329 num_msix = pf->caps.num_msix - num_vsi - 1;
330 if (num_msix > PSICFGR2_NUM_MSIX)
331 num_msix = PSICFGR2_NUM_MSIX;
332 enetc_port_wr(hw, ENETC4_PSICFGR2(0), num_msix);
333
334 for (i = 0; i < num_vsi; i++)
335 enetc_port_wr(hw, ENETC4_PSICFGR2(i + 1), 0);
336
337 return;
338 }
339
340 /* Likewise, from the hardware perspective pf->caps.num_msix is always
341 * greater than valid_num_si. So num_msix is always greater than or
342 * equal to 0.
343 */
344 num_msix = pf->caps.num_msix / valid_num_si +
345 pf->caps.num_msix % valid_num_si - 1;
346 if (num_msix > PSICFGR2_NUM_MSIX)
347 num_msix = PSICFGR2_NUM_MSIX;
348 enetc_port_wr(hw, ENETC4_PSICFGR2(0), num_msix);
349
350 num_msix = pf->caps.num_msix / valid_num_si - 1;
351 if (num_msix > PSICFGR2_NUM_MSIX)
352 num_msix = PSICFGR2_NUM_MSIX;
353
354 for (i = 0; i < pf->total_vfs; i++)
355 enetc_port_wr(hw, ENETC4_PSICFGR2(i + 1), num_msix);
356 }
357
enetc4_enable_all_si(struct enetc_pf * pf)358 static void enetc4_enable_all_si(struct enetc_pf *pf)
359 {
360 struct enetc_hw *hw = &pf->si->hw;
361 int num_si = pf->total_vfs + 1;
362 u32 si_bitmap = 0;
363 int i;
364
365 /* Master enable for all SIs */
366 for (i = 0; i < num_si; i++)
367 si_bitmap |= PMR_SI_EN(i);
368
369 enetc_port_wr(hw, ENETC4_PMR, si_bitmap);
370 }
371
enetc4_configure_port_si(struct enetc_pf * pf)372 static void enetc4_configure_port_si(struct enetc_pf *pf)
373 {
374 struct enetc_hw *hw = &pf->si->hw;
375
376 enetc4_allocate_si_rings(pf);
377
378 /* Outer VLAN tag will be used for VLAN filtering */
379 enetc_port_wr(hw, ENETC4_PSIVLANFMR, PSIVLANFMR_VS);
380
381 /* Enforce VLAN promiscuous mode for all SIs */
382 for (int i = 0; i < pf->total_vfs + 1; i++)
383 enetc_set_si_vlan_promisc(pf->si, i, true);
384
385 /* Disable SI MAC multicast & unicast promiscuous */
386 enetc_port_wr(hw, ENETC4_PSIPMMR, 0);
387
388 enetc4_set_si_msix_num(pf);
389
390 enetc4_enable_all_si(pf);
391 }
392
enetc4_pf_reset_tc_msdu(struct enetc_hw * hw)393 static void enetc4_pf_reset_tc_msdu(struct enetc_hw *hw)
394 {
395 u32 val = ENETC_MAC_MAXFRM_SIZE;
396 int tc;
397
398 val = u32_replace_bits(val, SDU_TYPE_MPDU, PTCTMSDUR_SDU_TYPE);
399
400 for (tc = 0; tc < ENETC_NUM_TC; tc++)
401 enetc_port_wr(hw, ENETC4_PTCTMSDUR(tc), val);
402 }
403
enetc4_set_trx_frame_size(struct enetc_pf * pf)404 static void enetc4_set_trx_frame_size(struct enetc_pf *pf)
405 {
406 struct enetc_si *si = pf->si;
407
408 enetc_port_mac_wr(si, ENETC4_PM_MAXFRM(0),
409 ENETC_SET_MAXFRM(ENETC_MAC_MAXFRM_SIZE));
410
411 enetc4_pf_reset_tc_msdu(&si->hw);
412 }
413
enetc4_configure_port(struct enetc_pf * pf)414 static void enetc4_configure_port(struct enetc_pf *pf)
415 {
416 enetc4_configure_port_si(pf);
417 enetc4_set_trx_frame_size(pf);
418 enetc_set_default_rss_key(pf);
419 }
420
enetc4_get_ntmp_caps(struct enetc_si * si)421 static void enetc4_get_ntmp_caps(struct enetc_si *si)
422 {
423 struct ntmp_user *user = &si->ntmp_user;
424 struct enetc_hw *hw = &si->hw;
425 u32 val;
426
427 val = enetc_port_rd(hw, ENETC4_PSIMAFCAPR);
428 user->maft_num_entries = FIELD_GET(PSIMAFCAPR_NUM_MAC_AFTE, val);
429 }
430
enetc4_ntmp_bitmap_init(struct ntmp_user * user)431 static int enetc4_ntmp_bitmap_init(struct ntmp_user *user)
432 {
433 user->maft_eid_bitmap = bitmap_zalloc(user->maft_num_entries,
434 GFP_KERNEL);
435 if (!user->maft_eid_bitmap)
436 return -ENOMEM;
437
438 return 0;
439 }
440
enetc4_ntmp_bitmap_free(struct ntmp_user * user)441 static void enetc4_ntmp_bitmap_free(struct ntmp_user *user)
442 {
443 bitmap_free(user->maft_eid_bitmap);
444 user->maft_eid_bitmap = NULL;
445 }
446
enetc4_init_ntmp_user(struct enetc_si * si)447 static int enetc4_init_ntmp_user(struct enetc_si *si)
448 {
449 struct ntmp_user *user = &si->ntmp_user;
450 int err;
451
452 /* For ENETC 4.1, all table versions are 0 */
453 memset(&user->tbl, 0, sizeof(user->tbl));
454
455 err = enetc4_setup_cbdr(si);
456 if (err)
457 return err;
458
459 enetc4_get_ntmp_caps(si);
460 err = enetc4_ntmp_bitmap_init(user);
461 if (err)
462 goto teardown_cbdr;
463
464 return 0;
465
466 teardown_cbdr:
467 enetc4_teardown_cbdr(si);
468
469 return err;
470 }
471
enetc4_free_ntmp_user(struct enetc_si * si)472 static void enetc4_free_ntmp_user(struct enetc_si *si)
473 {
474 enetc4_ntmp_bitmap_free(&si->ntmp_user);
475 enetc4_teardown_cbdr(si);
476 }
477
enetc4_pf_init(struct enetc_pf * pf)478 static int enetc4_pf_init(struct enetc_pf *pf)
479 {
480 struct device *dev = &pf->si->pdev->dev;
481 int err;
482
483 /* Initialize the MAC address for PF and VFs */
484 err = enetc_setup_mac_addresses(dev->of_node, pf);
485 if (err) {
486 dev_err(dev, "Failed to set MAC addresses\n");
487 return err;
488 }
489
490 err = enetc4_init_ntmp_user(pf->si);
491 if (err) {
492 dev_err(dev, "Failed to init NTMP user\n");
493 return err;
494 }
495
496 enetc4_configure_port(pf);
497
498 return 0;
499 }
500
enetc4_pf_free(struct enetc_pf * pf)501 static void enetc4_pf_free(struct enetc_pf *pf)
502 {
503 enetc4_free_ntmp_user(pf->si);
504 }
505
enetc4_pf_set_rx_mode(struct net_device * ndev,struct netdev_hw_addr_list * uc,struct netdev_hw_addr_list * mc)506 static int enetc4_pf_set_rx_mode(struct net_device *ndev,
507 struct netdev_hw_addr_list *uc,
508 struct netdev_hw_addr_list *mc)
509 {
510 struct enetc_ndev_priv *priv = netdev_priv(ndev);
511 struct enetc_pf *pf = enetc_si_priv(priv->si);
512 struct enetc_si *si = priv->si;
513 bool uc_promisc = false;
514 bool mc_promisc = false;
515 int type = 0;
516
517 if (ndev->flags & IFF_PROMISC) {
518 uc_promisc = true;
519 mc_promisc = true;
520 } else if (ndev->flags & IFF_ALLMULTI) {
521 mc_promisc = true;
522 type = ENETC_MAC_FILTER_TYPE_UC;
523 } else {
524 type = ENETC_MAC_FILTER_TYPE_ALL;
525 }
526
527 enetc_set_si_uc_promisc(si, 0, uc_promisc);
528 enetc_set_si_mc_promisc(si, 0, mc_promisc);
529
530 if (uc_promisc) {
531 enetc_set_si_uc_hash_filter(si, 0, 0);
532 enetc4_pf_clear_maft_entries(pf);
533 }
534
535 if (mc_promisc)
536 enetc_set_si_mc_hash_filter(si, 0, 0);
537
538 /* Set new MAC filter */
539 enetc4_pf_set_mac_filter(pf, type, uc, mc);
540
541 return 0;
542 }
543
enetc4_pf_set_features(struct net_device * ndev,netdev_features_t features)544 static int enetc4_pf_set_features(struct net_device *ndev,
545 netdev_features_t features)
546 {
547 netdev_features_t changed = ndev->features ^ features;
548 struct enetc_ndev_priv *priv = netdev_priv(ndev);
549
550 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
551 bool promisc_en = !(features & NETIF_F_HW_VLAN_CTAG_FILTER);
552
553 enetc_set_si_vlan_promisc(priv->si, 0, promisc_en);
554 }
555
556 if (changed & NETIF_F_LOOPBACK)
557 enetc4_pf_set_loopback(ndev, !!(features & NETIF_F_LOOPBACK));
558
559 enetc_set_features(ndev, features);
560
561 return 0;
562 }
563
564 static const struct net_device_ops enetc4_ndev_ops = {
565 .ndo_open = enetc_open,
566 .ndo_stop = enetc_close,
567 .ndo_start_xmit = enetc_xmit,
568 .ndo_get_stats = enetc_get_stats,
569 .ndo_set_mac_address = enetc_pf_set_mac_addr,
570 .ndo_set_rx_mode_async = enetc4_pf_set_rx_mode,
571 .ndo_set_features = enetc4_pf_set_features,
572 .ndo_vlan_rx_add_vid = enetc_vlan_rx_add_vid,
573 .ndo_vlan_rx_kill_vid = enetc_vlan_rx_del_vid,
574 .ndo_eth_ioctl = enetc_ioctl,
575 .ndo_hwtstamp_get = enetc_hwtstamp_get,
576 .ndo_hwtstamp_set = enetc_hwtstamp_set,
577 };
578
579 static struct phylink_pcs *
enetc4_pl_mac_select_pcs(struct phylink_config * config,phy_interface_t iface)580 enetc4_pl_mac_select_pcs(struct phylink_config *config, phy_interface_t iface)
581 {
582 struct enetc_pf *pf = phylink_to_enetc_pf(config);
583
584 return pf->pcs;
585 }
586
enetc4_mac_config(struct enetc_pf * pf,unsigned int mode,phy_interface_t phy_mode)587 static void enetc4_mac_config(struct enetc_pf *pf, unsigned int mode,
588 phy_interface_t phy_mode)
589 {
590 struct enetc_ndev_priv *priv = netdev_priv(pf->si->ndev);
591 struct enetc_si *si = pf->si;
592 u32 val;
593
594 if (enetc_is_pseudo_mac(si))
595 return;
596
597 val = enetc_port_mac_rd(si, ENETC4_PM_IF_MODE(0));
598 val &= ~(PM_IF_MODE_IFMODE | PM_IF_MODE_ENA);
599
600 switch (phy_mode) {
601 case PHY_INTERFACE_MODE_RGMII:
602 case PHY_INTERFACE_MODE_RGMII_ID:
603 case PHY_INTERFACE_MODE_RGMII_RXID:
604 case PHY_INTERFACE_MODE_RGMII_TXID:
605 val |= IFMODE_RGMII;
606 break;
607 case PHY_INTERFACE_MODE_RMII:
608 val |= IFMODE_RMII;
609 break;
610 case PHY_INTERFACE_MODE_SGMII:
611 case PHY_INTERFACE_MODE_2500BASEX:
612 val |= IFMODE_SGMII;
613 break;
614 case PHY_INTERFACE_MODE_10GBASER:
615 case PHY_INTERFACE_MODE_USXGMII:
616 val |= IFMODE_XGMII;
617 break;
618 default:
619 dev_err(priv->dev,
620 "Unsupported PHY mode:%d\n", phy_mode);
621 return;
622 }
623
624 enetc_port_mac_wr(si, ENETC4_PM_IF_MODE(0), val);
625 }
626
enetc4_pl_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)627 static void enetc4_pl_mac_config(struct phylink_config *config, unsigned int mode,
628 const struct phylink_link_state *state)
629 {
630 struct enetc_pf *pf = phylink_to_enetc_pf(config);
631
632 enetc4_mac_config(pf, mode, state->interface);
633 }
634
enetc4_set_port_speed(struct enetc_ndev_priv * priv,int speed)635 static void enetc4_set_port_speed(struct enetc_ndev_priv *priv, int speed)
636 {
637 u32 old_speed = priv->speed;
638 u32 val;
639
640 /* If the speed is unknown, use the minimum value */
641 if (speed == SPEED_UNKNOWN) {
642 speed = SPEED_10;
643 dev_warn(priv->dev, "Speed unknown, default is 10Mbps\n");
644 }
645
646 if (speed == old_speed)
647 return;
648
649 val = enetc_port_rd(&priv->si->hw, ENETC4_PCR) & (~PCR_PSPEED);
650 val |= PCR_PSPEED_VAL(speed);
651 enetc_port_wr(&priv->si->hw, ENETC4_PCR, val);
652 priv->speed = speed;
653 }
654
enetc4_set_rgmii_mac(struct enetc_pf * pf,int speed,int duplex)655 static void enetc4_set_rgmii_mac(struct enetc_pf *pf, int speed, int duplex)
656 {
657 struct enetc_si *si = pf->si;
658 u32 old_val, val;
659
660 old_val = enetc_port_mac_rd(si, ENETC4_PM_IF_MODE(0));
661 val = old_val & ~(PM_IF_MODE_ENA | PM_IF_MODE_M10 | PM_IF_MODE_REVMII);
662
663 switch (speed) {
664 case SPEED_1000:
665 val = u32_replace_bits(val, SSP_1G, PM_IF_MODE_SSP);
666 break;
667 case SPEED_100:
668 val = u32_replace_bits(val, SSP_100M, PM_IF_MODE_SSP);
669 break;
670 case SPEED_10:
671 val = u32_replace_bits(val, SSP_10M, PM_IF_MODE_SSP);
672 }
673
674 val = u32_replace_bits(val, duplex == DUPLEX_FULL ? 0 : 1,
675 PM_IF_MODE_HD);
676
677 if (val == old_val)
678 return;
679
680 enetc_port_mac_wr(si, ENETC4_PM_IF_MODE(0), val);
681 }
682
enetc4_set_rmii_mac(struct enetc_pf * pf,int speed,int duplex)683 static void enetc4_set_rmii_mac(struct enetc_pf *pf, int speed, int duplex)
684 {
685 struct enetc_si *si = pf->si;
686 u32 old_val, val;
687
688 old_val = enetc_port_mac_rd(si, ENETC4_PM_IF_MODE(0));
689 val = old_val & ~(PM_IF_MODE_ENA | PM_IF_MODE_SSP);
690
691 switch (speed) {
692 case SPEED_100:
693 val &= ~PM_IF_MODE_M10;
694 break;
695 case SPEED_10:
696 val |= PM_IF_MODE_M10;
697 }
698
699 val = u32_replace_bits(val, duplex == DUPLEX_FULL ? 0 : 1,
700 PM_IF_MODE_HD);
701
702 if (val == old_val)
703 return;
704
705 enetc_port_mac_wr(si, ENETC4_PM_IF_MODE(0), val);
706 }
707
enetc4_set_rx_pause(struct enetc_pf * pf,bool rx_pause)708 static void enetc4_set_rx_pause(struct enetc_pf *pf, bool rx_pause)
709 {
710 struct enetc_si *si = pf->si;
711 u32 old_val, val;
712
713 old_val = enetc_port_mac_rd(si, ENETC4_PM_CMD_CFG(0));
714 val = u32_replace_bits(old_val, rx_pause ? 0 : 1, PM_CMD_CFG_PAUSE_IGN);
715 if (val == old_val)
716 return;
717
718 enetc_port_mac_wr(si, ENETC4_PM_CMD_CFG(0), val);
719 }
720
enetc4_set_tx_pause(struct enetc_pf * pf,bool tx_pause)721 static void enetc4_set_tx_pause(struct enetc_pf *pf, bool tx_pause)
722 {
723 struct enetc_ndev_priv *priv = netdev_priv(pf->si->ndev);
724 u32 pause_off_thresh = 0, pause_on_thresh = 0;
725 u32 init_quanta = 0, refresh_quanta = 0;
726 struct enetc_hw *hw = &pf->si->hw;
727
728 enetc_set_congestion_mode(priv, tx_pause);
729
730 if (tx_pause) {
731 /* When the port first enters congestion, send a PAUSE request
732 * with the maximum number of quanta. When the port exits
733 * congestion, it will automatically send a PAUSE frame with
734 * zero quanta.
735 */
736 init_quanta = 0xffff;
737
738 /* Also, set up the refresh timer to send follow-up PAUSE
739 * frames at half the quanta value, in case the congestion
740 * condition persists.
741 */
742 refresh_quanta = 0xffff / 2;
743
744 /* Start emitting PAUSE frames when 3 large frames (or more
745 * smaller frames) have accumulated in the FIFO waiting to be
746 * DMAed to the RX ring.
747 */
748 pause_on_thresh = 3 * ENETC_MAC_MAXFRM_SIZE;
749 pause_off_thresh = 1 * ENETC_MAC_MAXFRM_SIZE;
750 }
751
752 enetc_port_mac_wr(pf->si, ENETC4_PM_PAUSE_QUANTA(0), init_quanta);
753 enetc_port_mac_wr(pf->si, ENETC4_PM_PAUSE_THRESH(0), refresh_quanta);
754 enetc_port_wr(hw, ENETC4_PPAUONTR, pause_on_thresh);
755 enetc_port_wr(hw, ENETC4_PPAUOFFTR, pause_off_thresh);
756 }
757
enetc4_mac_wait_tx_empty(struct enetc_si * si,int mac)758 static void enetc4_mac_wait_tx_empty(struct enetc_si *si, int mac)
759 {
760 u32 val;
761
762 if (read_poll_timeout(enetc_port_rd, val,
763 val & PM_IEVENT_TX_EMPTY,
764 100, 10000, false, &si->hw,
765 ENETC4_PM_IEVENT(mac)))
766 dev_warn(&si->pdev->dev,
767 "MAC %d TX is not empty\n", mac);
768 }
769
enetc4_mac_tx_graceful_stop(struct enetc_pf * pf)770 static void enetc4_mac_tx_graceful_stop(struct enetc_pf *pf)
771 {
772 struct enetc_hw *hw = &pf->si->hw;
773 struct enetc_si *si = pf->si;
774 u32 val;
775
776 val = enetc_port_rd(hw, ENETC4_POR);
777 val |= POR_TXDIS;
778 enetc_port_wr(hw, ENETC4_POR, val);
779
780 if (enetc_is_pseudo_mac(si))
781 return;
782
783 enetc4_mac_wait_tx_empty(si, 0);
784 if (si->hw_features & ENETC_SI_F_QBU)
785 enetc4_mac_wait_tx_empty(si, 1);
786
787 val = enetc_port_mac_rd(si, ENETC4_PM_CMD_CFG(0));
788 val &= ~PM_CMD_CFG_TX_EN;
789 enetc_port_mac_wr(si, ENETC4_PM_CMD_CFG(0), val);
790 }
791
enetc4_mac_tx_enable(struct enetc_pf * pf)792 static void enetc4_mac_tx_enable(struct enetc_pf *pf)
793 {
794 struct enetc_hw *hw = &pf->si->hw;
795 struct enetc_si *si = pf->si;
796 u32 val;
797
798 val = enetc_port_mac_rd(si, ENETC4_PM_CMD_CFG(0));
799 val |= PM_CMD_CFG_TX_EN;
800 enetc_port_mac_wr(si, ENETC4_PM_CMD_CFG(0), val);
801
802 val = enetc_port_rd(hw, ENETC4_POR);
803 val &= ~POR_TXDIS;
804 enetc_port_wr(hw, ENETC4_POR, val);
805 }
806
enetc4_mac_wait_rx_empty(struct enetc_si * si,int mac)807 static void enetc4_mac_wait_rx_empty(struct enetc_si *si, int mac)
808 {
809 u32 val;
810
811 if (read_poll_timeout(enetc_port_rd, val,
812 val & PM_IEVENT_RX_EMPTY,
813 100, 10000, false, &si->hw,
814 ENETC4_PM_IEVENT(mac)))
815 dev_warn(&si->pdev->dev,
816 "MAC %d RX is not empty\n", mac);
817 }
818
enetc4_mac_rx_graceful_stop(struct enetc_pf * pf)819 static void enetc4_mac_rx_graceful_stop(struct enetc_pf *pf)
820 {
821 struct enetc_hw *hw = &pf->si->hw;
822 struct enetc_si *si = pf->si;
823 u32 val;
824
825 if (enetc_is_pseudo_mac(si))
826 goto check_rx_busy;
827
828 if (si->hw_features & ENETC_SI_F_QBU) {
829 val = enetc_port_rd(hw, ENETC4_PM_CMD_CFG(1));
830 val &= ~PM_CMD_CFG_RX_EN;
831 enetc_port_wr(hw, ENETC4_PM_CMD_CFG(1), val);
832 enetc4_mac_wait_rx_empty(si, 1);
833 }
834
835 val = enetc_port_rd(hw, ENETC4_PM_CMD_CFG(0));
836 val &= ~PM_CMD_CFG_RX_EN;
837 enetc_port_wr(hw, ENETC4_PM_CMD_CFG(0), val);
838 enetc4_mac_wait_rx_empty(si, 0);
839
840 check_rx_busy:
841 if (read_poll_timeout(enetc_port_rd, val,
842 !(val & PSR_RX_BUSY),
843 100, 10000, false, hw,
844 ENETC4_PSR))
845 dev_warn(&si->pdev->dev, "Port RX busy\n");
846
847 val = enetc_port_rd(hw, ENETC4_POR);
848 val |= POR_RXDIS;
849 enetc_port_wr(hw, ENETC4_POR, val);
850 }
851
enetc4_mac_rx_enable(struct enetc_pf * pf)852 static void enetc4_mac_rx_enable(struct enetc_pf *pf)
853 {
854 struct enetc_hw *hw = &pf->si->hw;
855 struct enetc_si *si = pf->si;
856 u32 val;
857
858 val = enetc_port_rd(hw, ENETC4_POR);
859 val &= ~POR_RXDIS;
860 enetc_port_wr(hw, ENETC4_POR, val);
861
862 val = enetc_port_mac_rd(si, ENETC4_PM_CMD_CFG(0));
863 val |= PM_CMD_CFG_RX_EN;
864 enetc_port_mac_wr(si, ENETC4_PM_CMD_CFG(0), val);
865 }
866
enetc4_pl_mac_link_up(struct phylink_config * config,struct phy_device * phy,unsigned int mode,phy_interface_t interface,int speed,int duplex,bool tx_pause,bool rx_pause)867 static void enetc4_pl_mac_link_up(struct phylink_config *config,
868 struct phy_device *phy, unsigned int mode,
869 phy_interface_t interface, int speed,
870 int duplex, bool tx_pause, bool rx_pause)
871 {
872 struct enetc_pf *pf = phylink_to_enetc_pf(config);
873 struct enetc_si *si = pf->si;
874 struct enetc_ndev_priv *priv;
875
876 priv = netdev_priv(si->ndev);
877 enetc4_set_port_speed(priv, speed);
878
879 if (phy_interface_mode_is_rgmii(interface))
880 enetc4_set_rgmii_mac(pf, speed, duplex);
881
882 if (interface == PHY_INTERFACE_MODE_RMII)
883 enetc4_set_rmii_mac(pf, speed, duplex);
884
885 if (duplex == DUPLEX_FULL) {
886 /* When preemption is enabled, generation of PAUSE frames
887 * must be disabled, as stated in the IEEE 802.3 standard.
888 */
889 if (priv->active_offloads & ENETC_F_QBU)
890 tx_pause = false;
891 }
892
893 enetc4_set_tx_pause(pf, tx_pause);
894 enetc4_set_rx_pause(pf, rx_pause);
895 enetc4_mac_tx_enable(pf);
896 enetc4_mac_rx_enable(pf);
897 }
898
enetc4_pl_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)899 static void enetc4_pl_mac_link_down(struct phylink_config *config,
900 unsigned int mode,
901 phy_interface_t interface)
902 {
903 struct enetc_pf *pf = phylink_to_enetc_pf(config);
904
905 enetc4_mac_rx_graceful_stop(pf);
906 enetc4_mac_tx_graceful_stop(pf);
907 }
908
909 static const struct phylink_mac_ops enetc_pl_mac_ops = {
910 .mac_select_pcs = enetc4_pl_mac_select_pcs,
911 .mac_config = enetc4_pl_mac_config,
912 .mac_link_up = enetc4_pl_mac_link_up,
913 .mac_link_down = enetc4_pl_mac_link_down,
914 };
915
enetc4_pci_remove(void * data)916 static void enetc4_pci_remove(void *data)
917 {
918 struct pci_dev *pdev = data;
919
920 enetc_pci_remove(pdev);
921 }
922
enetc4_link_init(struct enetc_ndev_priv * priv,struct device_node * node)923 static int enetc4_link_init(struct enetc_ndev_priv *priv,
924 struct device_node *node)
925 {
926 struct enetc_pf *pf = enetc_si_priv(priv->si);
927 struct device *dev = priv->dev;
928 int err;
929
930 err = of_get_phy_mode(node, &pf->if_mode);
931 if (err) {
932 dev_err(dev, "Failed to get PHY mode\n");
933 return err;
934 }
935
936 err = enetc_mdiobus_create(pf, node);
937 if (err) {
938 dev_err(dev, "Failed to create MDIO bus\n");
939 return err;
940 }
941
942 err = enetc_phylink_create(priv, node, &enetc_pl_mac_ops);
943 if (err) {
944 dev_err(dev, "Failed to create phylink\n");
945 goto err_phylink_create;
946 }
947
948 return 0;
949
950 err_phylink_create:
951 enetc_mdiobus_destroy(pf);
952
953 return err;
954 }
955
enetc4_link_deinit(struct enetc_ndev_priv * priv)956 static void enetc4_link_deinit(struct enetc_ndev_priv *priv)
957 {
958 struct enetc_pf *pf = enetc_si_priv(priv->si);
959
960 enetc_phylink_destroy(priv);
961 enetc_mdiobus_destroy(pf);
962 }
963
enetc4_pf_netdev_create(struct enetc_si * si)964 static int enetc4_pf_netdev_create(struct enetc_si *si)
965 {
966 struct device *dev = &si->pdev->dev;
967 struct enetc_ndev_priv *priv;
968 struct net_device *ndev;
969 int err;
970
971 ndev = alloc_etherdev_mqs(sizeof(struct enetc_ndev_priv),
972 si->num_tx_rings, si->num_rx_rings);
973 if (!ndev)
974 return -ENOMEM;
975
976 priv = netdev_priv(ndev);
977 priv->ref_clk = devm_clk_get_optional(dev, "ref");
978 if (IS_ERR(priv->ref_clk)) {
979 dev_err(dev, "Get reference clock failed\n");
980 err = PTR_ERR(priv->ref_clk);
981 goto err_clk_get;
982 }
983
984 enetc_pf_netdev_setup(si, ndev, &enetc4_ndev_ops);
985
986 enetc_init_si_rings_params(priv);
987
988 err = enetc_configure_si(priv);
989 if (err) {
990 dev_err(dev, "Failed to configure SI\n");
991 goto err_config_si;
992 }
993
994 err = enetc_alloc_msix(priv);
995 if (err) {
996 dev_err(dev, "Failed to alloc MSI-X\n");
997 goto err_alloc_msix;
998 }
999
1000 err = enetc4_link_init(priv, dev->of_node);
1001 if (err)
1002 goto err_link_init;
1003
1004 err = register_netdev(ndev);
1005 if (err) {
1006 dev_err(dev, "Failed to register netdev\n");
1007 goto err_reg_netdev;
1008 }
1009
1010 return 0;
1011
1012 err_reg_netdev:
1013 enetc4_link_deinit(priv);
1014 err_link_init:
1015 enetc_free_msix(priv);
1016 err_alloc_msix:
1017 err_config_si:
1018 err_clk_get:
1019 free_netdev(ndev);
1020
1021 return err;
1022 }
1023
enetc4_pf_netdev_destroy(struct enetc_si * si)1024 static void enetc4_pf_netdev_destroy(struct enetc_si *si)
1025 {
1026 struct enetc_ndev_priv *priv = netdev_priv(si->ndev);
1027 struct net_device *ndev = si->ndev;
1028
1029 unregister_netdev(ndev);
1030 enetc4_link_deinit(priv);
1031 enetc_free_msix(priv);
1032 free_netdev(ndev);
1033 }
1034
1035 static const struct enetc_si_ops enetc4_psi_ops = {
1036 .get_rss_table = enetc4_get_rss_table,
1037 .set_rss_table = enetc4_set_rss_table,
1038 };
1039
enetc4_pf_probe(struct pci_dev * pdev,const struct pci_device_id * ent)1040 static int enetc4_pf_probe(struct pci_dev *pdev,
1041 const struct pci_device_id *ent)
1042 {
1043 struct device *dev = &pdev->dev;
1044 struct enetc_si *si;
1045 struct enetc_pf *pf;
1046 int err;
1047
1048 err = enetc_pci_probe(pdev, KBUILD_MODNAME, sizeof(*pf));
1049 if (err)
1050 return dev_err_probe(dev, err, "PCIe probing failed\n");
1051
1052 err = devm_add_action_or_reset(dev, enetc4_pci_remove, pdev);
1053 if (err)
1054 return err;
1055
1056 /* si is the private data. */
1057 si = pci_get_drvdata(pdev);
1058 if (!si->hw.port || !si->hw.global)
1059 return dev_err_probe(dev, -ENODEV,
1060 "Couldn't map PF only space\n");
1061
1062 si->revision = enetc_get_ip_revision(&si->hw);
1063 si->ops = &enetc4_psi_ops;
1064 err = enetc_get_driver_data(si);
1065 if (err)
1066 return dev_err_probe(dev, err,
1067 "Could not get PF driver data\n");
1068
1069 err = enetc4_pf_struct_init(si);
1070 if (err)
1071 return err;
1072
1073 pf = enetc_si_priv(si);
1074 err = enetc4_pf_init(pf);
1075 if (err)
1076 return err;
1077
1078 enetc_get_si_caps(si);
1079
1080 err = enetc4_pf_netdev_create(si);
1081 if (err)
1082 goto err_netdev_create;
1083
1084 enetc_create_debugfs(si);
1085
1086 return 0;
1087
1088 err_netdev_create:
1089 enetc4_pf_free(pf);
1090
1091 return err;
1092 }
1093
enetc4_pf_remove(struct pci_dev * pdev)1094 static void enetc4_pf_remove(struct pci_dev *pdev)
1095 {
1096 struct enetc_si *si = pci_get_drvdata(pdev);
1097 struct enetc_pf *pf = enetc_si_priv(si);
1098
1099 enetc_remove_debugfs(si);
1100 enetc4_pf_netdev_destroy(si);
1101 enetc4_pf_free(pf);
1102 }
1103
1104 static const struct pci_device_id enetc4_pf_id_table[] = {
1105 { PCI_DEVICE(NXP_ENETC_VENDOR_ID, NXP_ENETC_PF_DEV_ID) },
1106 { PCI_DEVICE(NXP_ENETC_VENDOR_ID, NXP_ENETC_PPM_DEV_ID) },
1107 { 0, } /* End of table. */
1108 };
1109 MODULE_DEVICE_TABLE(pci, enetc4_pf_id_table);
1110
1111 static struct pci_driver enetc4_pf_driver = {
1112 .name = KBUILD_MODNAME,
1113 .id_table = enetc4_pf_id_table,
1114 .probe = enetc4_pf_probe,
1115 .remove = enetc4_pf_remove,
1116 };
1117 module_pci_driver(enetc4_pf_driver);
1118
1119 MODULE_DESCRIPTION("ENETC4 PF Driver");
1120 MODULE_LICENSE("Dual BSD/GPL");
1121