xref: /openbmc/linux/drivers/net/ethernet/freescale/enetc/enetc.c (revision 1c95443e44e1f468b6635fb9ce9809f210a2d66e)
1d4fd0404SClaudiu Manoil // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2d4fd0404SClaudiu Manoil /* Copyright 2017-2019 NXP */
3d4fd0404SClaudiu Manoil 
4d4fd0404SClaudiu Manoil #include "enetc.h"
5d1b15102SVladimir Oltean #include <linux/bpf_trace.h>
6d4fd0404SClaudiu Manoil #include <linux/tcp.h>
7d4fd0404SClaudiu Manoil #include <linux/udp.h>
8bbcbf2eeSStephen Rothwell #include <linux/vmalloc.h>
97294380cSYangbo Lu #include <linux/ptp_classify.h>
10edce2a93SIoana Ciornei #include <net/ip6_checksum.h>
11847cbfc0SVladimir Oltean #include <net/pkt_sched.h>
12fb8629e2SIoana Ciornei #include <net/tso.h>
13d4fd0404SClaudiu Manoil 
1412717decSVladimir Oltean u32 enetc_port_mac_rd(struct enetc_si *si, u32 reg)
1512717decSVladimir Oltean {
1612717decSVladimir Oltean 	return enetc_port_rd(&si->hw, reg);
1712717decSVladimir Oltean }
1812717decSVladimir Oltean EXPORT_SYMBOL_GPL(enetc_port_mac_rd);
1912717decSVladimir Oltean 
2012717decSVladimir Oltean void enetc_port_mac_wr(struct enetc_si *si, u32 reg, u32 val)
2112717decSVladimir Oltean {
2212717decSVladimir Oltean 	enetc_port_wr(&si->hw, reg, val);
2312717decSVladimir Oltean 	if (si->hw_features & ENETC_SI_F_QBU)
2412717decSVladimir Oltean 		enetc_port_wr(&si->hw, reg + ENETC_PMAC_OFFSET, val);
2512717decSVladimir Oltean }
2612717decSVladimir Oltean EXPORT_SYMBOL_GPL(enetc_port_mac_wr);
2712717decSVladimir Oltean 
2882714539SVladimir Oltean static void enetc_change_preemptible_tcs(struct enetc_ndev_priv *priv,
2982714539SVladimir Oltean 					 u8 preemptible_tcs)
3001e23b2bSVladimir Oltean {
3182714539SVladimir Oltean 	priv->preemptible_tcs = preemptible_tcs;
3282714539SVladimir Oltean 	enetc_mm_commit_preemptible_tcs(priv);
3301e23b2bSVladimir Oltean }
3401e23b2bSVladimir Oltean 
357eab503bSVladimir Oltean static int enetc_num_stack_tx_queues(struct enetc_ndev_priv *priv)
367eab503bSVladimir Oltean {
377eab503bSVladimir Oltean 	int num_tx_rings = priv->num_tx_rings;
387eab503bSVladimir Oltean 
391c81a9b3SVladimir Oltean 	if (priv->xdp_prog)
407eab503bSVladimir Oltean 		return num_tx_rings - num_possible_cpus();
417eab503bSVladimir Oltean 
427eab503bSVladimir Oltean 	return num_tx_rings;
437eab503bSVladimir Oltean }
447eab503bSVladimir Oltean 
457eab503bSVladimir Oltean static struct enetc_bdr *enetc_rx_ring_from_xdp_tx_ring(struct enetc_ndev_priv *priv,
467eab503bSVladimir Oltean 							struct enetc_bdr *tx_ring)
477eab503bSVladimir Oltean {
487eab503bSVladimir Oltean 	int index = &priv->tx_ring[tx_ring->index] - priv->xdp_tx_ring;
497eab503bSVladimir Oltean 
507eab503bSVladimir Oltean 	return priv->rx_ring[index];
517eab503bSVladimir Oltean }
527eab503bSVladimir Oltean 
539d2b68ccSVladimir Oltean static struct sk_buff *enetc_tx_swbd_get_skb(struct enetc_tx_swbd *tx_swbd)
549d2b68ccSVladimir Oltean {
559d2b68ccSVladimir Oltean 	if (tx_swbd->is_xdp_tx || tx_swbd->is_xdp_redirect)
569d2b68ccSVladimir Oltean 		return NULL;
579d2b68ccSVladimir Oltean 
589d2b68ccSVladimir Oltean 	return tx_swbd->skb;
599d2b68ccSVladimir Oltean }
609d2b68ccSVladimir Oltean 
619d2b68ccSVladimir Oltean static struct xdp_frame *
629d2b68ccSVladimir Oltean enetc_tx_swbd_get_xdp_frame(struct enetc_tx_swbd *tx_swbd)
639d2b68ccSVladimir Oltean {
649d2b68ccSVladimir Oltean 	if (tx_swbd->is_xdp_redirect)
659d2b68ccSVladimir Oltean 		return tx_swbd->xdp_frame;
669d2b68ccSVladimir Oltean 
679d2b68ccSVladimir Oltean 	return NULL;
689d2b68ccSVladimir Oltean }
699d2b68ccSVladimir Oltean 
70d4fd0404SClaudiu Manoil static void enetc_unmap_tx_buff(struct enetc_bdr *tx_ring,
71d4fd0404SClaudiu Manoil 				struct enetc_tx_swbd *tx_swbd)
72d4fd0404SClaudiu Manoil {
737ed2bc80SVladimir Oltean 	/* For XDP_TX, pages come from RX, whereas for the other contexts where
747ed2bc80SVladimir Oltean 	 * we have is_dma_page_set, those come from skb_frag_dma_map. We need
757ed2bc80SVladimir Oltean 	 * to match the DMA mapping length, so we need to differentiate those.
767ed2bc80SVladimir Oltean 	 */
77d4fd0404SClaudiu Manoil 	if (tx_swbd->is_dma_page)
78d4fd0404SClaudiu Manoil 		dma_unmap_page(tx_ring->dev, tx_swbd->dma,
797ed2bc80SVladimir Oltean 			       tx_swbd->is_xdp_tx ? PAGE_SIZE : tx_swbd->len,
807ed2bc80SVladimir Oltean 			       tx_swbd->dir);
81d4fd0404SClaudiu Manoil 	else
82d4fd0404SClaudiu Manoil 		dma_unmap_single(tx_ring->dev, tx_swbd->dma,
837ed2bc80SVladimir Oltean 				 tx_swbd->len, tx_swbd->dir);
84d4fd0404SClaudiu Manoil 	tx_swbd->dma = 0;
85d4fd0404SClaudiu Manoil }
86d4fd0404SClaudiu Manoil 
879d2b68ccSVladimir Oltean static void enetc_free_tx_frame(struct enetc_bdr *tx_ring,
88d4fd0404SClaudiu Manoil 				struct enetc_tx_swbd *tx_swbd)
89d4fd0404SClaudiu Manoil {
909d2b68ccSVladimir Oltean 	struct xdp_frame *xdp_frame = enetc_tx_swbd_get_xdp_frame(tx_swbd);
919d2b68ccSVladimir Oltean 	struct sk_buff *skb = enetc_tx_swbd_get_skb(tx_swbd);
929d2b68ccSVladimir Oltean 
93d4fd0404SClaudiu Manoil 	if (tx_swbd->dma)
94d4fd0404SClaudiu Manoil 		enetc_unmap_tx_buff(tx_ring, tx_swbd);
95d4fd0404SClaudiu Manoil 
969d2b68ccSVladimir Oltean 	if (xdp_frame) {
979d2b68ccSVladimir Oltean 		xdp_return_frame(tx_swbd->xdp_frame);
989d2b68ccSVladimir Oltean 		tx_swbd->xdp_frame = NULL;
999d2b68ccSVladimir Oltean 	} else if (skb) {
1009d2b68ccSVladimir Oltean 		dev_kfree_skb_any(skb);
101d4fd0404SClaudiu Manoil 		tx_swbd->skb = NULL;
102d4fd0404SClaudiu Manoil 	}
103d4fd0404SClaudiu Manoil }
104d4fd0404SClaudiu Manoil 
1057ed2bc80SVladimir Oltean /* Let H/W know BD ring has been updated */
1067ed2bc80SVladimir Oltean static void enetc_update_tx_ring_tail(struct enetc_bdr *tx_ring)
1077ed2bc80SVladimir Oltean {
1087ed2bc80SVladimir Oltean 	/* includes wmb() */
1097ed2bc80SVladimir Oltean 	enetc_wr_reg_hot(tx_ring->tpir, tx_ring->next_to_use);
1107ed2bc80SVladimir Oltean }
1117ed2bc80SVladimir Oltean 
1127294380cSYangbo Lu static int enetc_ptp_parse(struct sk_buff *skb, u8 *udp,
1137294380cSYangbo Lu 			   u8 *msgtype, u8 *twostep,
1147294380cSYangbo Lu 			   u16 *correction_offset, u16 *body_offset)
1157294380cSYangbo Lu {
1167294380cSYangbo Lu 	unsigned int ptp_class;
1177294380cSYangbo Lu 	struct ptp_header *hdr;
1187294380cSYangbo Lu 	unsigned int type;
1197294380cSYangbo Lu 	u8 *base;
1207294380cSYangbo Lu 
1217294380cSYangbo Lu 	ptp_class = ptp_classify_raw(skb);
1227294380cSYangbo Lu 	if (ptp_class == PTP_CLASS_NONE)
1237294380cSYangbo Lu 		return -EINVAL;
1247294380cSYangbo Lu 
1257294380cSYangbo Lu 	hdr = ptp_parse_header(skb, ptp_class);
1267294380cSYangbo Lu 	if (!hdr)
1277294380cSYangbo Lu 		return -EINVAL;
1287294380cSYangbo Lu 
1297294380cSYangbo Lu 	type = ptp_class & PTP_CLASS_PMASK;
1307294380cSYangbo Lu 	if (type == PTP_CLASS_IPV4 || type == PTP_CLASS_IPV6)
1317294380cSYangbo Lu 		*udp = 1;
1327294380cSYangbo Lu 	else
1337294380cSYangbo Lu 		*udp = 0;
1347294380cSYangbo Lu 
1357294380cSYangbo Lu 	*msgtype = ptp_get_msgtype(hdr, ptp_class);
1367294380cSYangbo Lu 	*twostep = hdr->flag_field[0] & 0x2;
1377294380cSYangbo Lu 
1387294380cSYangbo Lu 	base = skb_mac_header(skb);
1397294380cSYangbo Lu 	*correction_offset = (u8 *)&hdr->correction - base;
1407294380cSYangbo Lu 	*body_offset = (u8 *)hdr + sizeof(struct ptp_header) - base;
1417294380cSYangbo Lu 
1427294380cSYangbo Lu 	return 0;
1437294380cSYangbo Lu }
1447294380cSYangbo Lu 
145f768e751SYangbo Lu static int enetc_map_tx_buffs(struct enetc_bdr *tx_ring, struct sk_buff *skb)
146d4fd0404SClaudiu Manoil {
1477294380cSYangbo Lu 	bool do_vlan, do_onestep_tstamp = false, do_twostep_tstamp = false;
1487294380cSYangbo Lu 	struct enetc_ndev_priv *priv = netdev_priv(tx_ring->ndev);
1497294380cSYangbo Lu 	struct enetc_hw *hw = &priv->si->hw;
150d4fd0404SClaudiu Manoil 	struct enetc_tx_swbd *tx_swbd;
151d4fd0404SClaudiu Manoil 	int len = skb_headlen(skb);
152d4fd0404SClaudiu Manoil 	union enetc_tx_bd temp_bd;
1537294380cSYangbo Lu 	u8 msgtype, twostep, udp;
154d4fd0404SClaudiu Manoil 	union enetc_tx_bd *txbd;
1557294380cSYangbo Lu 	u16 offset1, offset2;
156d4fd0404SClaudiu Manoil 	int i, count = 0;
1577294380cSYangbo Lu 	skb_frag_t *frag;
158d4fd0404SClaudiu Manoil 	unsigned int f;
159d4fd0404SClaudiu Manoil 	dma_addr_t dma;
160d4fd0404SClaudiu Manoil 	u8 flags = 0;
161d4fd0404SClaudiu Manoil 
162d4fd0404SClaudiu Manoil 	i = tx_ring->next_to_use;
163d4fd0404SClaudiu Manoil 	txbd = ENETC_TXBD(*tx_ring, i);
164d4fd0404SClaudiu Manoil 	prefetchw(txbd);
165d4fd0404SClaudiu Manoil 
166d4fd0404SClaudiu Manoil 	dma = dma_map_single(tx_ring->dev, skb->data, len, DMA_TO_DEVICE);
167d4fd0404SClaudiu Manoil 	if (unlikely(dma_mapping_error(tx_ring->dev, dma)))
168d4fd0404SClaudiu Manoil 		goto dma_err;
169d4fd0404SClaudiu Manoil 
170d4fd0404SClaudiu Manoil 	temp_bd.addr = cpu_to_le64(dma);
171d4fd0404SClaudiu Manoil 	temp_bd.buf_len = cpu_to_le16(len);
172d4fd0404SClaudiu Manoil 	temp_bd.lstatus = 0;
173d4fd0404SClaudiu Manoil 
174d4fd0404SClaudiu Manoil 	tx_swbd = &tx_ring->tx_swbd[i];
175d4fd0404SClaudiu Manoil 	tx_swbd->dma = dma;
176d4fd0404SClaudiu Manoil 	tx_swbd->len = len;
177d4fd0404SClaudiu Manoil 	tx_swbd->is_dma_page = 0;
1787ed2bc80SVladimir Oltean 	tx_swbd->dir = DMA_TO_DEVICE;
179d4fd0404SClaudiu Manoil 	count++;
180d4fd0404SClaudiu Manoil 
181d4fd0404SClaudiu Manoil 	do_vlan = skb_vlan_tag_present(skb);
1827294380cSYangbo Lu 	if (skb->cb[0] & ENETC_F_TX_ONESTEP_SYNC_TSTAMP) {
1837294380cSYangbo Lu 		if (enetc_ptp_parse(skb, &udp, &msgtype, &twostep, &offset1,
1847294380cSYangbo Lu 				    &offset2) ||
1857294380cSYangbo Lu 		    msgtype != PTP_MSGTYPE_SYNC || twostep)
1867294380cSYangbo Lu 			WARN_ONCE(1, "Bad packet for one-step timestamping\n");
1877294380cSYangbo Lu 		else
1887294380cSYangbo Lu 			do_onestep_tstamp = true;
1897294380cSYangbo Lu 	} else if (skb->cb[0] & ENETC_F_TX_TSTAMP) {
1907294380cSYangbo Lu 		do_twostep_tstamp = true;
1917294380cSYangbo Lu 	}
192d4fd0404SClaudiu Manoil 
1937294380cSYangbo Lu 	tx_swbd->do_twostep_tstamp = do_twostep_tstamp;
194285e8dedSPo Liu 	tx_swbd->qbv_en = !!(priv->active_offloads & ENETC_F_QBV);
195285e8dedSPo Liu 	tx_swbd->check_wb = tx_swbd->do_twostep_tstamp || tx_swbd->qbv_en;
1967294380cSYangbo Lu 
1977294380cSYangbo Lu 	if (do_vlan || do_onestep_tstamp || do_twostep_tstamp)
198d4fd0404SClaudiu Manoil 		flags |= ENETC_TXBD_FLAGS_EX;
199d4fd0404SClaudiu Manoil 
20082728b91SClaudiu Manoil 	if (tx_ring->tsd_enable)
2010d08c9ecSPo Liu 		flags |= ENETC_TXBD_FLAGS_TSE | ENETC_TXBD_FLAGS_TXSTART;
202d4fd0404SClaudiu Manoil 
203d4fd0404SClaudiu Manoil 	/* first BD needs frm_len and offload flags set */
204d4fd0404SClaudiu Manoil 	temp_bd.frm_len = cpu_to_le16(skb->len);
205d4fd0404SClaudiu Manoil 	temp_bd.flags = flags;
206d4fd0404SClaudiu Manoil 
20782728b91SClaudiu Manoil 	if (flags & ENETC_TXBD_FLAGS_TSE)
20882728b91SClaudiu Manoil 		temp_bd.txstart = enetc_txbd_set_tx_start(skb->skb_mstamp_ns,
20982728b91SClaudiu Manoil 							  flags);
2100d08c9ecSPo Liu 
211d4fd0404SClaudiu Manoil 	if (flags & ENETC_TXBD_FLAGS_EX) {
212d4fd0404SClaudiu Manoil 		u8 e_flags = 0;
213d4fd0404SClaudiu Manoil 		*txbd = temp_bd;
214d4fd0404SClaudiu Manoil 		enetc_clear_tx_bd(&temp_bd);
215d4fd0404SClaudiu Manoil 
216d4fd0404SClaudiu Manoil 		/* add extension BD for VLAN and/or timestamping */
217d4fd0404SClaudiu Manoil 		flags = 0;
218d4fd0404SClaudiu Manoil 		tx_swbd++;
219d4fd0404SClaudiu Manoil 		txbd++;
220d4fd0404SClaudiu Manoil 		i++;
221d4fd0404SClaudiu Manoil 		if (unlikely(i == tx_ring->bd_count)) {
222d4fd0404SClaudiu Manoil 			i = 0;
223d4fd0404SClaudiu Manoil 			tx_swbd = tx_ring->tx_swbd;
224d4fd0404SClaudiu Manoil 			txbd = ENETC_TXBD(*tx_ring, 0);
225d4fd0404SClaudiu Manoil 		}
226d4fd0404SClaudiu Manoil 		prefetchw(txbd);
227d4fd0404SClaudiu Manoil 
228d4fd0404SClaudiu Manoil 		if (do_vlan) {
229d4fd0404SClaudiu Manoil 			temp_bd.ext.vid = cpu_to_le16(skb_vlan_tag_get(skb));
230d4fd0404SClaudiu Manoil 			temp_bd.ext.tpid = 0; /* < C-TAG */
231d4fd0404SClaudiu Manoil 			e_flags |= ENETC_TXBD_E_FLAGS_VLAN_INS;
232d4fd0404SClaudiu Manoil 		}
233d4fd0404SClaudiu Manoil 
2347294380cSYangbo Lu 		if (do_onestep_tstamp) {
2357294380cSYangbo Lu 			u32 lo, hi, val;
2367294380cSYangbo Lu 			u64 sec, nsec;
2377294380cSYangbo Lu 			u8 *data;
2387294380cSYangbo Lu 
2397294380cSYangbo Lu 			lo = enetc_rd_hot(hw, ENETC_SICTR0);
2407294380cSYangbo Lu 			hi = enetc_rd_hot(hw, ENETC_SICTR1);
2417294380cSYangbo Lu 			sec = (u64)hi << 32 | lo;
2427294380cSYangbo Lu 			nsec = do_div(sec, 1000000000);
2437294380cSYangbo Lu 
2447294380cSYangbo Lu 			/* Configure extension BD */
2457294380cSYangbo Lu 			temp_bd.ext.tstamp = cpu_to_le32(lo & 0x3fffffff);
2467294380cSYangbo Lu 			e_flags |= ENETC_TXBD_E_FLAGS_ONE_STEP_PTP;
2477294380cSYangbo Lu 
2487294380cSYangbo Lu 			/* Update originTimestamp field of Sync packet
2497294380cSYangbo Lu 			 * - 48 bits seconds field
2507294380cSYangbo Lu 			 * - 32 bits nanseconds field
2517294380cSYangbo Lu 			 */
2527294380cSYangbo Lu 			data = skb_mac_header(skb);
2537294380cSYangbo Lu 			*(__be16 *)(data + offset2) =
2547294380cSYangbo Lu 				htons((sec >> 32) & 0xffff);
2557294380cSYangbo Lu 			*(__be32 *)(data + offset2 + 2) =
2567294380cSYangbo Lu 				htonl(sec & 0xffffffff);
2577294380cSYangbo Lu 			*(__be32 *)(data + offset2 + 6) = htonl(nsec);
2587294380cSYangbo Lu 
2597294380cSYangbo Lu 			/* Configure single-step register */
2607294380cSYangbo Lu 			val = ENETC_PM0_SINGLE_STEP_EN;
2617294380cSYangbo Lu 			val |= ENETC_SET_SINGLE_STEP_OFFSET(offset1);
2627294380cSYangbo Lu 			if (udp)
2637294380cSYangbo Lu 				val |= ENETC_PM0_SINGLE_STEP_CH;
2647294380cSYangbo Lu 
26512717decSVladimir Oltean 			enetc_port_mac_wr(priv->si, ENETC_PM0_SINGLE_STEP,
26612717decSVladimir Oltean 					  val);
2677294380cSYangbo Lu 		} else if (do_twostep_tstamp) {
268d4fd0404SClaudiu Manoil 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
269d4fd0404SClaudiu Manoil 			e_flags |= ENETC_TXBD_E_FLAGS_TWO_STEP_PTP;
270d4fd0404SClaudiu Manoil 		}
271d4fd0404SClaudiu Manoil 
272d4fd0404SClaudiu Manoil 		temp_bd.ext.e_flags = e_flags;
273d4fd0404SClaudiu Manoil 		count++;
274d4fd0404SClaudiu Manoil 	}
275d4fd0404SClaudiu Manoil 
276d4fd0404SClaudiu Manoil 	frag = &skb_shinfo(skb)->frags[0];
277d4fd0404SClaudiu Manoil 	for (f = 0; f < skb_shinfo(skb)->nr_frags; f++, frag++) {
278d4fd0404SClaudiu Manoil 		len = skb_frag_size(frag);
279d4fd0404SClaudiu Manoil 		dma = skb_frag_dma_map(tx_ring->dev, frag, 0, len,
280d4fd0404SClaudiu Manoil 				       DMA_TO_DEVICE);
281d4fd0404SClaudiu Manoil 		if (dma_mapping_error(tx_ring->dev, dma))
282d4fd0404SClaudiu Manoil 			goto dma_err;
283d4fd0404SClaudiu Manoil 
284d4fd0404SClaudiu Manoil 		*txbd = temp_bd;
285d4fd0404SClaudiu Manoil 		enetc_clear_tx_bd(&temp_bd);
286d4fd0404SClaudiu Manoil 
287d4fd0404SClaudiu Manoil 		flags = 0;
288d4fd0404SClaudiu Manoil 		tx_swbd++;
289d4fd0404SClaudiu Manoil 		txbd++;
290d4fd0404SClaudiu Manoil 		i++;
291d4fd0404SClaudiu Manoil 		if (unlikely(i == tx_ring->bd_count)) {
292d4fd0404SClaudiu Manoil 			i = 0;
293d4fd0404SClaudiu Manoil 			tx_swbd = tx_ring->tx_swbd;
294d4fd0404SClaudiu Manoil 			txbd = ENETC_TXBD(*tx_ring, 0);
295d4fd0404SClaudiu Manoil 		}
296d4fd0404SClaudiu Manoil 		prefetchw(txbd);
297d4fd0404SClaudiu Manoil 
298d4fd0404SClaudiu Manoil 		temp_bd.addr = cpu_to_le64(dma);
299d4fd0404SClaudiu Manoil 		temp_bd.buf_len = cpu_to_le16(len);
300d4fd0404SClaudiu Manoil 
301d4fd0404SClaudiu Manoil 		tx_swbd->dma = dma;
302d4fd0404SClaudiu Manoil 		tx_swbd->len = len;
303d4fd0404SClaudiu Manoil 		tx_swbd->is_dma_page = 1;
3047ed2bc80SVladimir Oltean 		tx_swbd->dir = DMA_TO_DEVICE;
305d4fd0404SClaudiu Manoil 		count++;
306d4fd0404SClaudiu Manoil 	}
307d4fd0404SClaudiu Manoil 
308d4fd0404SClaudiu Manoil 	/* last BD needs 'F' bit set */
309d4fd0404SClaudiu Manoil 	flags |= ENETC_TXBD_FLAGS_F;
310d4fd0404SClaudiu Manoil 	temp_bd.flags = flags;
311d4fd0404SClaudiu Manoil 	*txbd = temp_bd;
312d4fd0404SClaudiu Manoil 
313d504498dSVladimir Oltean 	tx_ring->tx_swbd[i].is_eof = true;
314d4fd0404SClaudiu Manoil 	tx_ring->tx_swbd[i].skb = skb;
315d4fd0404SClaudiu Manoil 
316d4fd0404SClaudiu Manoil 	enetc_bdr_idx_inc(tx_ring, &i);
317d4fd0404SClaudiu Manoil 	tx_ring->next_to_use = i;
318d4fd0404SClaudiu Manoil 
3194caefbceSMichael Walle 	skb_tx_timestamp(skb);
3204caefbceSMichael Walle 
3217ed2bc80SVladimir Oltean 	enetc_update_tx_ring_tail(tx_ring);
322d4fd0404SClaudiu Manoil 
323d4fd0404SClaudiu Manoil 	return count;
324d4fd0404SClaudiu Manoil 
325d4fd0404SClaudiu Manoil dma_err:
326d4fd0404SClaudiu Manoil 	dev_err(tx_ring->dev, "DMA map error");
327d4fd0404SClaudiu Manoil 
328d4fd0404SClaudiu Manoil 	do {
329d4fd0404SClaudiu Manoil 		tx_swbd = &tx_ring->tx_swbd[i];
3309d2b68ccSVladimir Oltean 		enetc_free_tx_frame(tx_ring, tx_swbd);
331d4fd0404SClaudiu Manoil 		if (i == 0)
332d4fd0404SClaudiu Manoil 			i = tx_ring->bd_count;
333d4fd0404SClaudiu Manoil 		i--;
334d4fd0404SClaudiu Manoil 	} while (count--);
335d4fd0404SClaudiu Manoil 
336d4fd0404SClaudiu Manoil 	return 0;
337d4fd0404SClaudiu Manoil }
338d4fd0404SClaudiu Manoil 
339fb8629e2SIoana Ciornei static void enetc_map_tx_tso_hdr(struct enetc_bdr *tx_ring, struct sk_buff *skb,
340fb8629e2SIoana Ciornei 				 struct enetc_tx_swbd *tx_swbd,
341fb8629e2SIoana Ciornei 				 union enetc_tx_bd *txbd, int *i, int hdr_len,
342fb8629e2SIoana Ciornei 				 int data_len)
343fb8629e2SIoana Ciornei {
344fb8629e2SIoana Ciornei 	union enetc_tx_bd txbd_tmp;
345fb8629e2SIoana Ciornei 	u8 flags = 0, e_flags = 0;
346fb8629e2SIoana Ciornei 	dma_addr_t addr;
347fb8629e2SIoana Ciornei 
348fb8629e2SIoana Ciornei 	enetc_clear_tx_bd(&txbd_tmp);
349fb8629e2SIoana Ciornei 	addr = tx_ring->tso_headers_dma + *i * TSO_HEADER_SIZE;
350fb8629e2SIoana Ciornei 
351fb8629e2SIoana Ciornei 	if (skb_vlan_tag_present(skb))
352fb8629e2SIoana Ciornei 		flags |= ENETC_TXBD_FLAGS_EX;
353fb8629e2SIoana Ciornei 
354fb8629e2SIoana Ciornei 	txbd_tmp.addr = cpu_to_le64(addr);
355fb8629e2SIoana Ciornei 	txbd_tmp.buf_len = cpu_to_le16(hdr_len);
356fb8629e2SIoana Ciornei 
357fb8629e2SIoana Ciornei 	/* first BD needs frm_len and offload flags set */
358fb8629e2SIoana Ciornei 	txbd_tmp.frm_len = cpu_to_le16(hdr_len + data_len);
359fb8629e2SIoana Ciornei 	txbd_tmp.flags = flags;
360fb8629e2SIoana Ciornei 
361fb8629e2SIoana Ciornei 	/* For the TSO header we do not set the dma address since we do not
362fb8629e2SIoana Ciornei 	 * want it unmapped when we do cleanup. We still set len so that we
363fb8629e2SIoana Ciornei 	 * count the bytes sent.
364fb8629e2SIoana Ciornei 	 */
365fb8629e2SIoana Ciornei 	tx_swbd->len = hdr_len;
366fb8629e2SIoana Ciornei 	tx_swbd->do_twostep_tstamp = false;
367fb8629e2SIoana Ciornei 	tx_swbd->check_wb = false;
368fb8629e2SIoana Ciornei 
369fb8629e2SIoana Ciornei 	/* Actually write the header in the BD */
370fb8629e2SIoana Ciornei 	*txbd = txbd_tmp;
371fb8629e2SIoana Ciornei 
372fb8629e2SIoana Ciornei 	/* Add extension BD for VLAN */
373fb8629e2SIoana Ciornei 	if (flags & ENETC_TXBD_FLAGS_EX) {
374fb8629e2SIoana Ciornei 		/* Get the next BD */
375fb8629e2SIoana Ciornei 		enetc_bdr_idx_inc(tx_ring, i);
376fb8629e2SIoana Ciornei 		txbd = ENETC_TXBD(*tx_ring, *i);
377fb8629e2SIoana Ciornei 		tx_swbd = &tx_ring->tx_swbd[*i];
378fb8629e2SIoana Ciornei 		prefetchw(txbd);
379fb8629e2SIoana Ciornei 
380fb8629e2SIoana Ciornei 		/* Setup the VLAN fields */
381fb8629e2SIoana Ciornei 		enetc_clear_tx_bd(&txbd_tmp);
382fb8629e2SIoana Ciornei 		txbd_tmp.ext.vid = cpu_to_le16(skb_vlan_tag_get(skb));
383fb8629e2SIoana Ciornei 		txbd_tmp.ext.tpid = 0; /* < C-TAG */
384fb8629e2SIoana Ciornei 		e_flags |= ENETC_TXBD_E_FLAGS_VLAN_INS;
385fb8629e2SIoana Ciornei 
386fb8629e2SIoana Ciornei 		/* Write the BD */
387fb8629e2SIoana Ciornei 		txbd_tmp.ext.e_flags = e_flags;
388fb8629e2SIoana Ciornei 		*txbd = txbd_tmp;
389fb8629e2SIoana Ciornei 	}
390fb8629e2SIoana Ciornei }
391fb8629e2SIoana Ciornei 
392fb8629e2SIoana Ciornei static int enetc_map_tx_tso_data(struct enetc_bdr *tx_ring, struct sk_buff *skb,
393fb8629e2SIoana Ciornei 				 struct enetc_tx_swbd *tx_swbd,
394fb8629e2SIoana Ciornei 				 union enetc_tx_bd *txbd, char *data,
395fb8629e2SIoana Ciornei 				 int size, bool last_bd)
396fb8629e2SIoana Ciornei {
397fb8629e2SIoana Ciornei 	union enetc_tx_bd txbd_tmp;
398fb8629e2SIoana Ciornei 	dma_addr_t addr;
399fb8629e2SIoana Ciornei 	u8 flags = 0;
400fb8629e2SIoana Ciornei 
401fb8629e2SIoana Ciornei 	enetc_clear_tx_bd(&txbd_tmp);
402fb8629e2SIoana Ciornei 
403fb8629e2SIoana Ciornei 	addr = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE);
404fb8629e2SIoana Ciornei 	if (unlikely(dma_mapping_error(tx_ring->dev, addr))) {
405fb8629e2SIoana Ciornei 		netdev_err(tx_ring->ndev, "DMA map error\n");
406fb8629e2SIoana Ciornei 		return -ENOMEM;
407fb8629e2SIoana Ciornei 	}
408fb8629e2SIoana Ciornei 
409fb8629e2SIoana Ciornei 	if (last_bd) {
410fb8629e2SIoana Ciornei 		flags |= ENETC_TXBD_FLAGS_F;
411fb8629e2SIoana Ciornei 		tx_swbd->is_eof = 1;
412fb8629e2SIoana Ciornei 	}
413fb8629e2SIoana Ciornei 
414fb8629e2SIoana Ciornei 	txbd_tmp.addr = cpu_to_le64(addr);
415fb8629e2SIoana Ciornei 	txbd_tmp.buf_len = cpu_to_le16(size);
416fb8629e2SIoana Ciornei 	txbd_tmp.flags = flags;
417fb8629e2SIoana Ciornei 
418fb8629e2SIoana Ciornei 	tx_swbd->dma = addr;
419fb8629e2SIoana Ciornei 	tx_swbd->len = size;
420fb8629e2SIoana Ciornei 	tx_swbd->dir = DMA_TO_DEVICE;
421fb8629e2SIoana Ciornei 
422fb8629e2SIoana Ciornei 	*txbd = txbd_tmp;
423fb8629e2SIoana Ciornei 
424fb8629e2SIoana Ciornei 	return 0;
425fb8629e2SIoana Ciornei }
426fb8629e2SIoana Ciornei 
427fb8629e2SIoana Ciornei static __wsum enetc_tso_hdr_csum(struct tso_t *tso, struct sk_buff *skb,
428fb8629e2SIoana Ciornei 				 char *hdr, int hdr_len, int *l4_hdr_len)
429fb8629e2SIoana Ciornei {
430fb8629e2SIoana Ciornei 	char *l4_hdr = hdr + skb_transport_offset(skb);
431fb8629e2SIoana Ciornei 	int mac_hdr_len = skb_network_offset(skb);
432fb8629e2SIoana Ciornei 
433fb8629e2SIoana Ciornei 	if (tso->tlen != sizeof(struct udphdr)) {
434fb8629e2SIoana Ciornei 		struct tcphdr *tcph = (struct tcphdr *)(l4_hdr);
435fb8629e2SIoana Ciornei 
436fb8629e2SIoana Ciornei 		tcph->check = 0;
437fb8629e2SIoana Ciornei 	} else {
438fb8629e2SIoana Ciornei 		struct udphdr *udph = (struct udphdr *)(l4_hdr);
439fb8629e2SIoana Ciornei 
440fb8629e2SIoana Ciornei 		udph->check = 0;
441fb8629e2SIoana Ciornei 	}
442fb8629e2SIoana Ciornei 
443fb8629e2SIoana Ciornei 	/* Compute the IP checksum. This is necessary since tso_build_hdr()
444fb8629e2SIoana Ciornei 	 * already incremented the IP ID field.
445fb8629e2SIoana Ciornei 	 */
446fb8629e2SIoana Ciornei 	if (!tso->ipv6) {
447fb8629e2SIoana Ciornei 		struct iphdr *iph = (void *)(hdr + mac_hdr_len);
448fb8629e2SIoana Ciornei 
449fb8629e2SIoana Ciornei 		iph->check = 0;
450fb8629e2SIoana Ciornei 		iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
451fb8629e2SIoana Ciornei 	}
452fb8629e2SIoana Ciornei 
453fb8629e2SIoana Ciornei 	/* Compute the checksum over the L4 header. */
454fb8629e2SIoana Ciornei 	*l4_hdr_len = hdr_len - skb_transport_offset(skb);
455fb8629e2SIoana Ciornei 	return csum_partial(l4_hdr, *l4_hdr_len, 0);
456fb8629e2SIoana Ciornei }
457fb8629e2SIoana Ciornei 
458fb8629e2SIoana Ciornei static void enetc_tso_complete_csum(struct enetc_bdr *tx_ring, struct tso_t *tso,
459fb8629e2SIoana Ciornei 				    struct sk_buff *skb, char *hdr, int len,
460fb8629e2SIoana Ciornei 				    __wsum sum)
461fb8629e2SIoana Ciornei {
462fb8629e2SIoana Ciornei 	char *l4_hdr = hdr + skb_transport_offset(skb);
463fb8629e2SIoana Ciornei 	__sum16 csum_final;
464fb8629e2SIoana Ciornei 
465fb8629e2SIoana Ciornei 	/* Complete the L4 checksum by appending the pseudo-header to the
466fb8629e2SIoana Ciornei 	 * already computed checksum.
467fb8629e2SIoana Ciornei 	 */
468fb8629e2SIoana Ciornei 	if (!tso->ipv6)
469fb8629e2SIoana Ciornei 		csum_final = csum_tcpudp_magic(ip_hdr(skb)->saddr,
470fb8629e2SIoana Ciornei 					       ip_hdr(skb)->daddr,
471fb8629e2SIoana Ciornei 					       len, ip_hdr(skb)->protocol, sum);
472fb8629e2SIoana Ciornei 	else
473fb8629e2SIoana Ciornei 		csum_final = csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
474fb8629e2SIoana Ciornei 					     &ipv6_hdr(skb)->daddr,
475fb8629e2SIoana Ciornei 					     len, ipv6_hdr(skb)->nexthdr, sum);
476fb8629e2SIoana Ciornei 
477fb8629e2SIoana Ciornei 	if (tso->tlen != sizeof(struct udphdr)) {
478fb8629e2SIoana Ciornei 		struct tcphdr *tcph = (struct tcphdr *)(l4_hdr);
479fb8629e2SIoana Ciornei 
480fb8629e2SIoana Ciornei 		tcph->check = csum_final;
481fb8629e2SIoana Ciornei 	} else {
482fb8629e2SIoana Ciornei 		struct udphdr *udph = (struct udphdr *)(l4_hdr);
483fb8629e2SIoana Ciornei 
484fb8629e2SIoana Ciornei 		udph->check = csum_final;
485fb8629e2SIoana Ciornei 	}
486fb8629e2SIoana Ciornei }
487fb8629e2SIoana Ciornei 
488fb8629e2SIoana Ciornei static int enetc_map_tx_tso_buffs(struct enetc_bdr *tx_ring, struct sk_buff *skb)
489fb8629e2SIoana Ciornei {
490fb8629e2SIoana Ciornei 	int hdr_len, total_len, data_len;
491fb8629e2SIoana Ciornei 	struct enetc_tx_swbd *tx_swbd;
492fb8629e2SIoana Ciornei 	union enetc_tx_bd *txbd;
493fb8629e2SIoana Ciornei 	struct tso_t tso;
494fb8629e2SIoana Ciornei 	__wsum csum, csum2;
495fb8629e2SIoana Ciornei 	int count = 0, pos;
496fb8629e2SIoana Ciornei 	int err, i, bd_data_num;
497fb8629e2SIoana Ciornei 
498fb8629e2SIoana Ciornei 	/* Initialize the TSO handler, and prepare the first payload */
499fb8629e2SIoana Ciornei 	hdr_len = tso_start(skb, &tso);
500fb8629e2SIoana Ciornei 	total_len = skb->len - hdr_len;
501fb8629e2SIoana Ciornei 	i = tx_ring->next_to_use;
502fb8629e2SIoana Ciornei 
503fb8629e2SIoana Ciornei 	while (total_len > 0) {
504fb8629e2SIoana Ciornei 		char *hdr;
505fb8629e2SIoana Ciornei 
506fb8629e2SIoana Ciornei 		/* Get the BD */
507fb8629e2SIoana Ciornei 		txbd = ENETC_TXBD(*tx_ring, i);
508fb8629e2SIoana Ciornei 		tx_swbd = &tx_ring->tx_swbd[i];
509fb8629e2SIoana Ciornei 		prefetchw(txbd);
510fb8629e2SIoana Ciornei 
511fb8629e2SIoana Ciornei 		/* Determine the length of this packet */
512fb8629e2SIoana Ciornei 		data_len = min_t(int, skb_shinfo(skb)->gso_size, total_len);
513fb8629e2SIoana Ciornei 		total_len -= data_len;
514fb8629e2SIoana Ciornei 
515fb8629e2SIoana Ciornei 		/* prepare packet headers: MAC + IP + TCP */
516fb8629e2SIoana Ciornei 		hdr = tx_ring->tso_headers + i * TSO_HEADER_SIZE;
517fb8629e2SIoana Ciornei 		tso_build_hdr(skb, hdr, &tso, data_len, total_len == 0);
518fb8629e2SIoana Ciornei 
519fb8629e2SIoana Ciornei 		/* compute the csum over the L4 header */
520fb8629e2SIoana Ciornei 		csum = enetc_tso_hdr_csum(&tso, skb, hdr, hdr_len, &pos);
521fb8629e2SIoana Ciornei 		enetc_map_tx_tso_hdr(tx_ring, skb, tx_swbd, txbd, &i, hdr_len, data_len);
522fb8629e2SIoana Ciornei 		bd_data_num = 0;
523fb8629e2SIoana Ciornei 		count++;
524fb8629e2SIoana Ciornei 
525fb8629e2SIoana Ciornei 		while (data_len > 0) {
526fb8629e2SIoana Ciornei 			int size;
527fb8629e2SIoana Ciornei 
528fb8629e2SIoana Ciornei 			size = min_t(int, tso.size, data_len);
529fb8629e2SIoana Ciornei 
530fb8629e2SIoana Ciornei 			/* Advance the index in the BDR */
531fb8629e2SIoana Ciornei 			enetc_bdr_idx_inc(tx_ring, &i);
532fb8629e2SIoana Ciornei 			txbd = ENETC_TXBD(*tx_ring, i);
533fb8629e2SIoana Ciornei 			tx_swbd = &tx_ring->tx_swbd[i];
534fb8629e2SIoana Ciornei 			prefetchw(txbd);
535fb8629e2SIoana Ciornei 
536fb8629e2SIoana Ciornei 			/* Compute the checksum over this segment of data and
537fb8629e2SIoana Ciornei 			 * add it to the csum already computed (over the L4
538fb8629e2SIoana Ciornei 			 * header and possible other data segments).
539fb8629e2SIoana Ciornei 			 */
540fb8629e2SIoana Ciornei 			csum2 = csum_partial(tso.data, size, 0);
541fb8629e2SIoana Ciornei 			csum = csum_block_add(csum, csum2, pos);
542fb8629e2SIoana Ciornei 			pos += size;
543fb8629e2SIoana Ciornei 
544fb8629e2SIoana Ciornei 			err = enetc_map_tx_tso_data(tx_ring, skb, tx_swbd, txbd,
545fb8629e2SIoana Ciornei 						    tso.data, size,
546fb8629e2SIoana Ciornei 						    size == data_len);
547fb8629e2SIoana Ciornei 			if (err)
548fb8629e2SIoana Ciornei 				goto err_map_data;
549fb8629e2SIoana Ciornei 
550fb8629e2SIoana Ciornei 			data_len -= size;
551fb8629e2SIoana Ciornei 			count++;
552fb8629e2SIoana Ciornei 			bd_data_num++;
553fb8629e2SIoana Ciornei 			tso_build_data(skb, &tso, size);
554fb8629e2SIoana Ciornei 
555fb8629e2SIoana Ciornei 			if (unlikely(bd_data_num >= ENETC_MAX_SKB_FRAGS && data_len))
556fb8629e2SIoana Ciornei 				goto err_chained_bd;
557fb8629e2SIoana Ciornei 		}
558fb8629e2SIoana Ciornei 
559fb8629e2SIoana Ciornei 		enetc_tso_complete_csum(tx_ring, &tso, skb, hdr, pos, csum);
560fb8629e2SIoana Ciornei 
561fb8629e2SIoana Ciornei 		if (total_len == 0)
562fb8629e2SIoana Ciornei 			tx_swbd->skb = skb;
563fb8629e2SIoana Ciornei 
564fb8629e2SIoana Ciornei 		/* Go to the next BD */
565fb8629e2SIoana Ciornei 		enetc_bdr_idx_inc(tx_ring, &i);
566fb8629e2SIoana Ciornei 	}
567fb8629e2SIoana Ciornei 
568fb8629e2SIoana Ciornei 	tx_ring->next_to_use = i;
569fb8629e2SIoana Ciornei 	enetc_update_tx_ring_tail(tx_ring);
570fb8629e2SIoana Ciornei 
571fb8629e2SIoana Ciornei 	return count;
572fb8629e2SIoana Ciornei 
573fb8629e2SIoana Ciornei err_map_data:
574fb8629e2SIoana Ciornei 	dev_err(tx_ring->dev, "DMA map error");
575fb8629e2SIoana Ciornei 
576fb8629e2SIoana Ciornei err_chained_bd:
577fb8629e2SIoana Ciornei 	do {
578fb8629e2SIoana Ciornei 		tx_swbd = &tx_ring->tx_swbd[i];
579fb8629e2SIoana Ciornei 		enetc_free_tx_frame(tx_ring, tx_swbd);
580fb8629e2SIoana Ciornei 		if (i == 0)
581fb8629e2SIoana Ciornei 			i = tx_ring->bd_count;
582fb8629e2SIoana Ciornei 		i--;
583fb8629e2SIoana Ciornei 	} while (count--);
584fb8629e2SIoana Ciornei 
585fb8629e2SIoana Ciornei 	return 0;
586fb8629e2SIoana Ciornei }
587fb8629e2SIoana Ciornei 
5887294380cSYangbo Lu static netdev_tx_t enetc_start_xmit(struct sk_buff *skb,
5897294380cSYangbo Lu 				    struct net_device *ndev)
5900486185eSVladimir Oltean {
5910486185eSVladimir Oltean 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
5920486185eSVladimir Oltean 	struct enetc_bdr *tx_ring;
593acede3c5SIoana Ciornei 	int count, err;
5940486185eSVladimir Oltean 
5957ce9c3d3SYangbo Lu 	/* Queue one-step Sync packet if already locked */
5967ce9c3d3SYangbo Lu 	if (skb->cb[0] & ENETC_F_TX_ONESTEP_SYNC_TSTAMP) {
5977ce9c3d3SYangbo Lu 		if (test_and_set_bit_lock(ENETC_TX_ONESTEP_TSTAMP_IN_PROGRESS,
5987ce9c3d3SYangbo Lu 					  &priv->flags)) {
5997ce9c3d3SYangbo Lu 			skb_queue_tail(&priv->tx_skbs, skb);
6007ce9c3d3SYangbo Lu 			return NETDEV_TX_OK;
6017ce9c3d3SYangbo Lu 		}
6027ce9c3d3SYangbo Lu 	}
6037ce9c3d3SYangbo Lu 
6040486185eSVladimir Oltean 	tx_ring = priv->tx_ring[skb->queue_mapping];
6050486185eSVladimir Oltean 
606fb8629e2SIoana Ciornei 	if (skb_is_gso(skb)) {
607fb8629e2SIoana Ciornei 		if (enetc_bd_unused(tx_ring) < tso_count_descs(skb)) {
608fb8629e2SIoana Ciornei 			netif_stop_subqueue(ndev, tx_ring->index);
609fb8629e2SIoana Ciornei 			return NETDEV_TX_BUSY;
610fb8629e2SIoana Ciornei 		}
611fb8629e2SIoana Ciornei 
612fb8629e2SIoana Ciornei 		enetc_lock_mdio();
613fb8629e2SIoana Ciornei 		count = enetc_map_tx_tso_buffs(tx_ring, skb);
614fb8629e2SIoana Ciornei 		enetc_unlock_mdio();
615fb8629e2SIoana Ciornei 	} else {
6160486185eSVladimir Oltean 		if (unlikely(skb_shinfo(skb)->nr_frags > ENETC_MAX_SKB_FRAGS))
6170486185eSVladimir Oltean 			if (unlikely(skb_linearize(skb)))
6180486185eSVladimir Oltean 				goto drop_packet_err;
6190486185eSVladimir Oltean 
6200486185eSVladimir Oltean 		count = skb_shinfo(skb)->nr_frags + 1; /* fragments + head */
6210486185eSVladimir Oltean 		if (enetc_bd_unused(tx_ring) < ENETC_TXBDS_NEEDED(count)) {
6220486185eSVladimir Oltean 			netif_stop_subqueue(ndev, tx_ring->index);
6230486185eSVladimir Oltean 			return NETDEV_TX_BUSY;
6240486185eSVladimir Oltean 		}
6250486185eSVladimir Oltean 
626acede3c5SIoana Ciornei 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
627acede3c5SIoana Ciornei 			err = skb_checksum_help(skb);
628acede3c5SIoana Ciornei 			if (err)
629acede3c5SIoana Ciornei 				goto drop_packet_err;
630acede3c5SIoana Ciornei 		}
6310486185eSVladimir Oltean 		enetc_lock_mdio();
632f768e751SYangbo Lu 		count = enetc_map_tx_buffs(tx_ring, skb);
6330486185eSVladimir Oltean 		enetc_unlock_mdio();
634fb8629e2SIoana Ciornei 	}
6350486185eSVladimir Oltean 
6360486185eSVladimir Oltean 	if (unlikely(!count))
6370486185eSVladimir Oltean 		goto drop_packet_err;
6380486185eSVladimir Oltean 
6390486185eSVladimir Oltean 	if (enetc_bd_unused(tx_ring) < ENETC_TXBDS_MAX_NEEDED)
6400486185eSVladimir Oltean 		netif_stop_subqueue(ndev, tx_ring->index);
6410486185eSVladimir Oltean 
6420486185eSVladimir Oltean 	return NETDEV_TX_OK;
6430486185eSVladimir Oltean 
6440486185eSVladimir Oltean drop_packet_err:
6450486185eSVladimir Oltean 	dev_kfree_skb_any(skb);
6460486185eSVladimir Oltean 	return NETDEV_TX_OK;
6470486185eSVladimir Oltean }
6480486185eSVladimir Oltean 
6497294380cSYangbo Lu netdev_tx_t enetc_xmit(struct sk_buff *skb, struct net_device *ndev)
6507294380cSYangbo Lu {
6517294380cSYangbo Lu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
6527294380cSYangbo Lu 	u8 udp, msgtype, twostep;
6537294380cSYangbo Lu 	u16 offset1, offset2;
6547294380cSYangbo Lu 
6557294380cSYangbo Lu 	/* Mark tx timestamp type on skb->cb[0] if requires */
6567294380cSYangbo Lu 	if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
6577294380cSYangbo Lu 	    (priv->active_offloads & ENETC_F_TX_TSTAMP_MASK)) {
6587294380cSYangbo Lu 		skb->cb[0] = priv->active_offloads & ENETC_F_TX_TSTAMP_MASK;
6597294380cSYangbo Lu 	} else {
6607294380cSYangbo Lu 		skb->cb[0] = 0;
6617294380cSYangbo Lu 	}
6627294380cSYangbo Lu 
6637294380cSYangbo Lu 	/* Fall back to two-step timestamp if not one-step Sync packet */
6647294380cSYangbo Lu 	if (skb->cb[0] & ENETC_F_TX_ONESTEP_SYNC_TSTAMP) {
6657294380cSYangbo Lu 		if (enetc_ptp_parse(skb, &udp, &msgtype, &twostep,
6667294380cSYangbo Lu 				    &offset1, &offset2) ||
6677294380cSYangbo Lu 		    msgtype != PTP_MSGTYPE_SYNC || twostep != 0)
6687294380cSYangbo Lu 			skb->cb[0] = ENETC_F_TX_TSTAMP;
6697294380cSYangbo Lu 	}
6707294380cSYangbo Lu 
6717294380cSYangbo Lu 	return enetc_start_xmit(skb, ndev);
6727294380cSYangbo Lu }
673e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_xmit);
6747294380cSYangbo Lu 
675d4fd0404SClaudiu Manoil static irqreturn_t enetc_msix(int irq, void *data)
676d4fd0404SClaudiu Manoil {
677d4fd0404SClaudiu Manoil 	struct enetc_int_vector	*v = data;
678d4fd0404SClaudiu Manoil 	int i;
679d4fd0404SClaudiu Manoil 
680fd5736bfSAlex Marginean 	enetc_lock_mdio();
681fd5736bfSAlex Marginean 
682d4fd0404SClaudiu Manoil 	/* disable interrupts */
683fd5736bfSAlex Marginean 	enetc_wr_reg_hot(v->rbier, 0);
684fd5736bfSAlex Marginean 	enetc_wr_reg_hot(v->ricr1, v->rx_ictt);
685d4fd0404SClaudiu Manoil 
6860574e200SClaudiu Manoil 	for_each_set_bit(i, &v->tx_rings_map, ENETC_MAX_NUM_TXQS)
687fd5736bfSAlex Marginean 		enetc_wr_reg_hot(v->tbier_base + ENETC_BDR_OFF(i), 0);
688fd5736bfSAlex Marginean 
689fd5736bfSAlex Marginean 	enetc_unlock_mdio();
690d4fd0404SClaudiu Manoil 
691215602a8SJiafei Pan 	napi_schedule(&v->napi);
692d4fd0404SClaudiu Manoil 
693d4fd0404SClaudiu Manoil 	return IRQ_HANDLED;
694d4fd0404SClaudiu Manoil }
695d4fd0404SClaudiu Manoil 
696ae0e6a5dSClaudiu Manoil static void enetc_rx_dim_work(struct work_struct *w)
697ae0e6a5dSClaudiu Manoil {
698ae0e6a5dSClaudiu Manoil 	struct dim *dim = container_of(w, struct dim, work);
699ae0e6a5dSClaudiu Manoil 	struct dim_cq_moder moder =
700ae0e6a5dSClaudiu Manoil 		net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
701ae0e6a5dSClaudiu Manoil 	struct enetc_int_vector	*v =
702ae0e6a5dSClaudiu Manoil 		container_of(dim, struct enetc_int_vector, rx_dim);
703ae0e6a5dSClaudiu Manoil 
704ae0e6a5dSClaudiu Manoil 	v->rx_ictt = enetc_usecs_to_cycles(moder.usec);
705ae0e6a5dSClaudiu Manoil 	dim->state = DIM_START_MEASURE;
706ae0e6a5dSClaudiu Manoil }
707ae0e6a5dSClaudiu Manoil 
708ae0e6a5dSClaudiu Manoil static void enetc_rx_net_dim(struct enetc_int_vector *v)
709ae0e6a5dSClaudiu Manoil {
7109f7afa05SClaudiu Manoil 	struct dim_sample dim_sample = {};
711ae0e6a5dSClaudiu Manoil 
712ae0e6a5dSClaudiu Manoil 	v->comp_cnt++;
713ae0e6a5dSClaudiu Manoil 
714ae0e6a5dSClaudiu Manoil 	if (!v->rx_napi_work)
715ae0e6a5dSClaudiu Manoil 		return;
716ae0e6a5dSClaudiu Manoil 
717ae0e6a5dSClaudiu Manoil 	dim_update_sample(v->comp_cnt,
718ae0e6a5dSClaudiu Manoil 			  v->rx_ring.stats.packets,
719ae0e6a5dSClaudiu Manoil 			  v->rx_ring.stats.bytes,
720ae0e6a5dSClaudiu Manoil 			  &dim_sample);
721ae0e6a5dSClaudiu Manoil 	net_dim(&v->rx_dim, dim_sample);
722ae0e6a5dSClaudiu Manoil }
723ae0e6a5dSClaudiu Manoil 
724d4fd0404SClaudiu Manoil static int enetc_bd_ready_count(struct enetc_bdr *tx_ring, int ci)
725d4fd0404SClaudiu Manoil {
726fd5736bfSAlex Marginean 	int pi = enetc_rd_reg_hot(tx_ring->tcir) & ENETC_TBCIR_IDX_MASK;
727d4fd0404SClaudiu Manoil 
728d4fd0404SClaudiu Manoil 	return pi >= ci ? pi - ci : tx_ring->bd_count - ci + pi;
729d4fd0404SClaudiu Manoil }
730d4fd0404SClaudiu Manoil 
73165d0cbb4SVladimir Oltean static bool enetc_page_reusable(struct page *page)
73265d0cbb4SVladimir Oltean {
73365d0cbb4SVladimir Oltean 	return (!page_is_pfmemalloc(page) && page_ref_count(page) == 1);
73465d0cbb4SVladimir Oltean }
73565d0cbb4SVladimir Oltean 
73665d0cbb4SVladimir Oltean static void enetc_reuse_page(struct enetc_bdr *rx_ring,
73765d0cbb4SVladimir Oltean 			     struct enetc_rx_swbd *old)
73865d0cbb4SVladimir Oltean {
73965d0cbb4SVladimir Oltean 	struct enetc_rx_swbd *new;
74065d0cbb4SVladimir Oltean 
74165d0cbb4SVladimir Oltean 	new = &rx_ring->rx_swbd[rx_ring->next_to_alloc];
74265d0cbb4SVladimir Oltean 
74365d0cbb4SVladimir Oltean 	/* next buf that may reuse a page */
74465d0cbb4SVladimir Oltean 	enetc_bdr_idx_inc(rx_ring, &rx_ring->next_to_alloc);
74565d0cbb4SVladimir Oltean 
74665d0cbb4SVladimir Oltean 	/* copy page reference */
74765d0cbb4SVladimir Oltean 	*new = *old;
74865d0cbb4SVladimir Oltean }
74965d0cbb4SVladimir Oltean 
750d3982312SY.b. Lu static void enetc_get_tx_tstamp(struct enetc_hw *hw, union enetc_tx_bd *txbd,
751d3982312SY.b. Lu 				u64 *tstamp)
752d3982312SY.b. Lu {
753cec4f328SY.b. Lu 	u32 lo, hi, tstamp_lo;
754d3982312SY.b. Lu 
7556d36ecdbSVladimir Oltean 	lo = enetc_rd_hot(hw, ENETC_SICTR0);
7566d36ecdbSVladimir Oltean 	hi = enetc_rd_hot(hw, ENETC_SICTR1);
757cec4f328SY.b. Lu 	tstamp_lo = le32_to_cpu(txbd->wb.tstamp);
758cec4f328SY.b. Lu 	if (lo <= tstamp_lo)
759d3982312SY.b. Lu 		hi -= 1;
760cec4f328SY.b. Lu 	*tstamp = (u64)hi << 32 | tstamp_lo;
761d3982312SY.b. Lu }
762d3982312SY.b. Lu 
763d3982312SY.b. Lu static void enetc_tstamp_tx(struct sk_buff *skb, u64 tstamp)
764d3982312SY.b. Lu {
765d3982312SY.b. Lu 	struct skb_shared_hwtstamps shhwtstamps;
766d3982312SY.b. Lu 
767d3982312SY.b. Lu 	if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) {
768d3982312SY.b. Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
769d3982312SY.b. Lu 		shhwtstamps.hwtstamp = ns_to_ktime(tstamp);
770847cbfc0SVladimir Oltean 		skb_txtime_consumed(skb);
771d3982312SY.b. Lu 		skb_tstamp_tx(skb, &shhwtstamps);
772d3982312SY.b. Lu 	}
773d3982312SY.b. Lu }
774d3982312SY.b. Lu 
7757ed2bc80SVladimir Oltean static void enetc_recycle_xdp_tx_buff(struct enetc_bdr *tx_ring,
7767ed2bc80SVladimir Oltean 				      struct enetc_tx_swbd *tx_swbd)
7777ed2bc80SVladimir Oltean {
7787ed2bc80SVladimir Oltean 	struct enetc_ndev_priv *priv = netdev_priv(tx_ring->ndev);
7797ed2bc80SVladimir Oltean 	struct enetc_rx_swbd rx_swbd = {
7807ed2bc80SVladimir Oltean 		.dma = tx_swbd->dma,
7817ed2bc80SVladimir Oltean 		.page = tx_swbd->page,
7827ed2bc80SVladimir Oltean 		.page_offset = tx_swbd->page_offset,
7837ed2bc80SVladimir Oltean 		.dir = tx_swbd->dir,
7847ed2bc80SVladimir Oltean 		.len = tx_swbd->len,
7857ed2bc80SVladimir Oltean 	};
7867eab503bSVladimir Oltean 	struct enetc_bdr *rx_ring;
7877eab503bSVladimir Oltean 
7887eab503bSVladimir Oltean 	rx_ring = enetc_rx_ring_from_xdp_tx_ring(priv, tx_ring);
7897ed2bc80SVladimir Oltean 
7907ed2bc80SVladimir Oltean 	if (likely(enetc_swbd_unused(rx_ring))) {
7917ed2bc80SVladimir Oltean 		enetc_reuse_page(rx_ring, &rx_swbd);
7927ed2bc80SVladimir Oltean 
7937ed2bc80SVladimir Oltean 		/* sync for use by the device */
7947ed2bc80SVladimir Oltean 		dma_sync_single_range_for_device(rx_ring->dev, rx_swbd.dma,
7957ed2bc80SVladimir Oltean 						 rx_swbd.page_offset,
7967ed2bc80SVladimir Oltean 						 ENETC_RXB_DMA_SIZE_XDP,
7977ed2bc80SVladimir Oltean 						 rx_swbd.dir);
7987ed2bc80SVladimir Oltean 
7997ed2bc80SVladimir Oltean 		rx_ring->stats.recycles++;
8007ed2bc80SVladimir Oltean 	} else {
8017ed2bc80SVladimir Oltean 		/* RX ring is already full, we need to unmap and free the
8027ed2bc80SVladimir Oltean 		 * page, since there's nothing useful we can do with it.
8037ed2bc80SVladimir Oltean 		 */
8047ed2bc80SVladimir Oltean 		rx_ring->stats.recycle_failures++;
8057ed2bc80SVladimir Oltean 
8067ed2bc80SVladimir Oltean 		dma_unmap_page(rx_ring->dev, rx_swbd.dma, PAGE_SIZE,
8077ed2bc80SVladimir Oltean 			       rx_swbd.dir);
8087ed2bc80SVladimir Oltean 		__free_page(rx_swbd.page);
8097ed2bc80SVladimir Oltean 	}
8107ed2bc80SVladimir Oltean 
8117ed2bc80SVladimir Oltean 	rx_ring->xdp.xdp_tx_in_flight--;
8127ed2bc80SVladimir Oltean }
8137ed2bc80SVladimir Oltean 
814d4fd0404SClaudiu Manoil static bool enetc_clean_tx_ring(struct enetc_bdr *tx_ring, int napi_budget)
815d4fd0404SClaudiu Manoil {
816285e8dedSPo Liu 	int tx_frm_cnt = 0, tx_byte_cnt = 0, tx_win_drop = 0;
817d4fd0404SClaudiu Manoil 	struct net_device *ndev = tx_ring->ndev;
8187294380cSYangbo Lu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
819d4fd0404SClaudiu Manoil 	struct enetc_tx_swbd *tx_swbd;
820d4fd0404SClaudiu Manoil 	int i, bds_to_clean;
8217294380cSYangbo Lu 	bool do_twostep_tstamp;
822d3982312SY.b. Lu 	u64 tstamp = 0;
823d4fd0404SClaudiu Manoil 
824d4fd0404SClaudiu Manoil 	i = tx_ring->next_to_clean;
825d4fd0404SClaudiu Manoil 	tx_swbd = &tx_ring->tx_swbd[i];
826fd5736bfSAlex Marginean 
827d4fd0404SClaudiu Manoil 	bds_to_clean = enetc_bd_ready_count(tx_ring, i);
828d4fd0404SClaudiu Manoil 
8297294380cSYangbo Lu 	do_twostep_tstamp = false;
830d3982312SY.b. Lu 
831d4fd0404SClaudiu Manoil 	while (bds_to_clean && tx_frm_cnt < ENETC_DEFAULT_TX_WORK) {
8329d2b68ccSVladimir Oltean 		struct xdp_frame *xdp_frame = enetc_tx_swbd_get_xdp_frame(tx_swbd);
8339d2b68ccSVladimir Oltean 		struct sk_buff *skb = enetc_tx_swbd_get_skb(tx_swbd);
834a93580a0SVladimir Oltean 		bool is_eof = tx_swbd->is_eof;
8359d2b68ccSVladimir Oltean 
836d3982312SY.b. Lu 		if (unlikely(tx_swbd->check_wb)) {
837ae77bdbcSVladimir Oltean 			union enetc_tx_bd *txbd = ENETC_TXBD(*tx_ring, i);
838d3982312SY.b. Lu 
839d3982312SY.b. Lu 			if (txbd->flags & ENETC_TXBD_FLAGS_W &&
8407294380cSYangbo Lu 			    tx_swbd->do_twostep_tstamp) {
841d3982312SY.b. Lu 				enetc_get_tx_tstamp(&priv->si->hw, txbd,
842d3982312SY.b. Lu 						    &tstamp);
8437294380cSYangbo Lu 				do_twostep_tstamp = true;
844d3982312SY.b. Lu 			}
845285e8dedSPo Liu 
846285e8dedSPo Liu 			if (tx_swbd->qbv_en &&
847285e8dedSPo Liu 			    txbd->wb.status & ENETC_TXBD_STATS_WIN)
848285e8dedSPo Liu 				tx_win_drop++;
849d3982312SY.b. Lu 		}
850d3982312SY.b. Lu 
8517ed2bc80SVladimir Oltean 		if (tx_swbd->is_xdp_tx)
8527ed2bc80SVladimir Oltean 			enetc_recycle_xdp_tx_buff(tx_ring, tx_swbd);
8537ed2bc80SVladimir Oltean 		else if (likely(tx_swbd->dma))
854d4fd0404SClaudiu Manoil 			enetc_unmap_tx_buff(tx_ring, tx_swbd);
855f4a0be84SClaudiu Manoil 
8569d2b68ccSVladimir Oltean 		if (xdp_frame) {
8579d2b68ccSVladimir Oltean 			xdp_return_frame(xdp_frame);
8589d2b68ccSVladimir Oltean 		} else if (skb) {
85952066149SVladimir Oltean 			if (unlikely(skb->cb[0] & ENETC_F_TX_ONESTEP_SYNC_TSTAMP)) {
8607294380cSYangbo Lu 				/* Start work to release lock for next one-step
8617294380cSYangbo Lu 				 * timestamping packet. And send one skb in
8627294380cSYangbo Lu 				 * tx_skbs queue if has.
8637294380cSYangbo Lu 				 */
864b6faf160SYangbo Lu 				schedule_work(&priv->tx_onestep_tstamp);
8657294380cSYangbo Lu 			} else if (unlikely(do_twostep_tstamp)) {
8669d2b68ccSVladimir Oltean 				enetc_tstamp_tx(skb, tstamp);
8677294380cSYangbo Lu 				do_twostep_tstamp = false;
868d3982312SY.b. Lu 			}
8699d2b68ccSVladimir Oltean 			napi_consume_skb(skb, napi_budget);
870d4fd0404SClaudiu Manoil 		}
871d4fd0404SClaudiu Manoil 
872d4fd0404SClaudiu Manoil 		tx_byte_cnt += tx_swbd->len;
8731ee8d6f3SVladimir Oltean 		/* Scrub the swbd here so we don't have to do that
8741ee8d6f3SVladimir Oltean 		 * when we reuse it during xmit
8751ee8d6f3SVladimir Oltean 		 */
8761ee8d6f3SVladimir Oltean 		memset(tx_swbd, 0, sizeof(*tx_swbd));
877d4fd0404SClaudiu Manoil 
878d4fd0404SClaudiu Manoil 		bds_to_clean--;
879d4fd0404SClaudiu Manoil 		tx_swbd++;
880d4fd0404SClaudiu Manoil 		i++;
881d4fd0404SClaudiu Manoil 		if (unlikely(i == tx_ring->bd_count)) {
882d4fd0404SClaudiu Manoil 			i = 0;
883d4fd0404SClaudiu Manoil 			tx_swbd = tx_ring->tx_swbd;
884d4fd0404SClaudiu Manoil 		}
885d4fd0404SClaudiu Manoil 
886d4fd0404SClaudiu Manoil 		/* BD iteration loop end */
887a93580a0SVladimir Oltean 		if (is_eof) {
888d4fd0404SClaudiu Manoil 			tx_frm_cnt++;
889d4fd0404SClaudiu Manoil 			/* re-arm interrupt source */
890fd5736bfSAlex Marginean 			enetc_wr_reg_hot(tx_ring->idr, BIT(tx_ring->index) |
891d4fd0404SClaudiu Manoil 					 BIT(16 + tx_ring->index));
892d4fd0404SClaudiu Manoil 		}
893d4fd0404SClaudiu Manoil 
894d4fd0404SClaudiu Manoil 		if (unlikely(!bds_to_clean))
895d4fd0404SClaudiu Manoil 			bds_to_clean = enetc_bd_ready_count(tx_ring, i);
896d4fd0404SClaudiu Manoil 	}
897d4fd0404SClaudiu Manoil 
898d4fd0404SClaudiu Manoil 	tx_ring->next_to_clean = i;
899d4fd0404SClaudiu Manoil 	tx_ring->stats.packets += tx_frm_cnt;
900d4fd0404SClaudiu Manoil 	tx_ring->stats.bytes += tx_byte_cnt;
901285e8dedSPo Liu 	tx_ring->stats.win_drop += tx_win_drop;
902d4fd0404SClaudiu Manoil 
903d4fd0404SClaudiu Manoil 	if (unlikely(tx_frm_cnt && netif_carrier_ok(ndev) &&
904d4fd0404SClaudiu Manoil 		     __netif_subqueue_stopped(ndev, tx_ring->index) &&
905d4fd0404SClaudiu Manoil 		     (enetc_bd_unused(tx_ring) >= ENETC_TXBDS_MAX_NEEDED))) {
906d4fd0404SClaudiu Manoil 		netif_wake_subqueue(ndev, tx_ring->index);
907d4fd0404SClaudiu Manoil 	}
908d4fd0404SClaudiu Manoil 
909d4fd0404SClaudiu Manoil 	return tx_frm_cnt != ENETC_DEFAULT_TX_WORK;
910d4fd0404SClaudiu Manoil }
911d4fd0404SClaudiu Manoil 
912d4fd0404SClaudiu Manoil static bool enetc_new_page(struct enetc_bdr *rx_ring,
913d4fd0404SClaudiu Manoil 			   struct enetc_rx_swbd *rx_swbd)
914d4fd0404SClaudiu Manoil {
9157ed2bc80SVladimir Oltean 	bool xdp = !!(rx_ring->xdp.prog);
916d4fd0404SClaudiu Manoil 	struct page *page;
917d4fd0404SClaudiu Manoil 	dma_addr_t addr;
918d4fd0404SClaudiu Manoil 
919d4fd0404SClaudiu Manoil 	page = dev_alloc_page();
920d4fd0404SClaudiu Manoil 	if (unlikely(!page))
921d4fd0404SClaudiu Manoil 		return false;
922d4fd0404SClaudiu Manoil 
9237ed2bc80SVladimir Oltean 	/* For XDP_TX, we forgo dma_unmap -> dma_map */
9247ed2bc80SVladimir Oltean 	rx_swbd->dir = xdp ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
9257ed2bc80SVladimir Oltean 
9267ed2bc80SVladimir Oltean 	addr = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, rx_swbd->dir);
927d4fd0404SClaudiu Manoil 	if (unlikely(dma_mapping_error(rx_ring->dev, addr))) {
928d4fd0404SClaudiu Manoil 		__free_page(page);
929d4fd0404SClaudiu Manoil 
930d4fd0404SClaudiu Manoil 		return false;
931d4fd0404SClaudiu Manoil 	}
932d4fd0404SClaudiu Manoil 
933d4fd0404SClaudiu Manoil 	rx_swbd->dma = addr;
934d4fd0404SClaudiu Manoil 	rx_swbd->page = page;
935d1b15102SVladimir Oltean 	rx_swbd->page_offset = rx_ring->buffer_offset;
936d4fd0404SClaudiu Manoil 
937d4fd0404SClaudiu Manoil 	return true;
938d4fd0404SClaudiu Manoil }
939d4fd0404SClaudiu Manoil 
940d4fd0404SClaudiu Manoil static int enetc_refill_rx_ring(struct enetc_bdr *rx_ring, const int buff_cnt)
941d4fd0404SClaudiu Manoil {
942d4fd0404SClaudiu Manoil 	struct enetc_rx_swbd *rx_swbd;
943d4fd0404SClaudiu Manoil 	union enetc_rx_bd *rxbd;
944d4fd0404SClaudiu Manoil 	int i, j;
945d4fd0404SClaudiu Manoil 
946d4fd0404SClaudiu Manoil 	i = rx_ring->next_to_use;
947d4fd0404SClaudiu Manoil 	rx_swbd = &rx_ring->rx_swbd[i];
948714239acSClaudiu Manoil 	rxbd = enetc_rxbd(rx_ring, i);
949d4fd0404SClaudiu Manoil 
950d4fd0404SClaudiu Manoil 	for (j = 0; j < buff_cnt; j++) {
951d4fd0404SClaudiu Manoil 		/* try reuse page */
952d4fd0404SClaudiu Manoil 		if (unlikely(!rx_swbd->page)) {
953d4fd0404SClaudiu Manoil 			if (unlikely(!enetc_new_page(rx_ring, rx_swbd))) {
954d4fd0404SClaudiu Manoil 				rx_ring->stats.rx_alloc_errs++;
955d4fd0404SClaudiu Manoil 				break;
956d4fd0404SClaudiu Manoil 			}
957d4fd0404SClaudiu Manoil 		}
958d4fd0404SClaudiu Manoil 
959d4fd0404SClaudiu Manoil 		/* update RxBD */
960d4fd0404SClaudiu Manoil 		rxbd->w.addr = cpu_to_le64(rx_swbd->dma +
961d4fd0404SClaudiu Manoil 					   rx_swbd->page_offset);
962d4fd0404SClaudiu Manoil 		/* clear 'R" as well */
963d4fd0404SClaudiu Manoil 		rxbd->r.lstatus = 0;
964d4fd0404SClaudiu Manoil 
965c027aa92SVladimir Oltean 		enetc_rxbd_next(rx_ring, &rxbd, &i);
966c027aa92SVladimir Oltean 		rx_swbd = &rx_ring->rx_swbd[i];
967d4fd0404SClaudiu Manoil 	}
968d4fd0404SClaudiu Manoil 
969d4fd0404SClaudiu Manoil 	if (likely(j)) {
970d4fd0404SClaudiu Manoil 		rx_ring->next_to_alloc = i; /* keep track from page reuse */
971d4fd0404SClaudiu Manoil 		rx_ring->next_to_use = i;
9727a5222cbSVladimir Oltean 
9737a5222cbSVladimir Oltean 		/* update ENETC's consumer index */
9747a5222cbSVladimir Oltean 		enetc_wr_reg_hot(rx_ring->rcir, rx_ring->next_to_use);
975d4fd0404SClaudiu Manoil 	}
976d4fd0404SClaudiu Manoil 
977d4fd0404SClaudiu Manoil 	return j;
978d4fd0404SClaudiu Manoil }
979d4fd0404SClaudiu Manoil 
980434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
981d3982312SY.b. Lu static void enetc_get_rx_tstamp(struct net_device *ndev,
982d3982312SY.b. Lu 				union enetc_rx_bd *rxbd,
983d3982312SY.b. Lu 				struct sk_buff *skb)
984d3982312SY.b. Lu {
985d3982312SY.b. Lu 	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
986d3982312SY.b. Lu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
987d3982312SY.b. Lu 	struct enetc_hw *hw = &priv->si->hw;
988cec4f328SY.b. Lu 	u32 lo, hi, tstamp_lo;
989d3982312SY.b. Lu 	u64 tstamp;
990d3982312SY.b. Lu 
991cec4f328SY.b. Lu 	if (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_TSTMP) {
992fd5736bfSAlex Marginean 		lo = enetc_rd_reg_hot(hw->reg + ENETC_SICTR0);
993fd5736bfSAlex Marginean 		hi = enetc_rd_reg_hot(hw->reg + ENETC_SICTR1);
994434cebabSClaudiu Manoil 		rxbd = enetc_rxbd_ext(rxbd);
995434cebabSClaudiu Manoil 		tstamp_lo = le32_to_cpu(rxbd->ext.tstamp);
996cec4f328SY.b. Lu 		if (lo <= tstamp_lo)
997d3982312SY.b. Lu 			hi -= 1;
998d3982312SY.b. Lu 
999cec4f328SY.b. Lu 		tstamp = (u64)hi << 32 | tstamp_lo;
1000d3982312SY.b. Lu 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
1001d3982312SY.b. Lu 		shhwtstamps->hwtstamp = ns_to_ktime(tstamp);
1002d3982312SY.b. Lu 	}
1003d3982312SY.b. Lu }
1004d3982312SY.b. Lu #endif
1005d3982312SY.b. Lu 
1006d4fd0404SClaudiu Manoil static void enetc_get_offloads(struct enetc_bdr *rx_ring,
1007d4fd0404SClaudiu Manoil 			       union enetc_rx_bd *rxbd, struct sk_buff *skb)
1008d4fd0404SClaudiu Manoil {
1009d3982312SY.b. Lu 	struct enetc_ndev_priv *priv = netdev_priv(rx_ring->ndev);
1010827b6fd0SVladimir Oltean 
1011d3982312SY.b. Lu 	/* TODO: hashing */
1012d4fd0404SClaudiu Manoil 	if (rx_ring->ndev->features & NETIF_F_RXCSUM) {
1013d4fd0404SClaudiu Manoil 		u16 inet_csum = le16_to_cpu(rxbd->r.inet_csum);
1014d4fd0404SClaudiu Manoil 
1015d4fd0404SClaudiu Manoil 		skb->csum = csum_unfold((__force __sum16)~htons(inet_csum));
1016d4fd0404SClaudiu Manoil 		skb->ip_summed = CHECKSUM_COMPLETE;
1017d4fd0404SClaudiu Manoil 	}
1018d4fd0404SClaudiu Manoil 
1019827b6fd0SVladimir Oltean 	if (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_VLAN) {
1020827b6fd0SVladimir Oltean 		__be16 tpid = 0;
1021827b6fd0SVladimir Oltean 
1022827b6fd0SVladimir Oltean 		switch (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_TPID) {
1023827b6fd0SVladimir Oltean 		case 0:
1024827b6fd0SVladimir Oltean 			tpid = htons(ETH_P_8021Q);
1025827b6fd0SVladimir Oltean 			break;
1026827b6fd0SVladimir Oltean 		case 1:
1027827b6fd0SVladimir Oltean 			tpid = htons(ETH_P_8021AD);
1028827b6fd0SVladimir Oltean 			break;
1029827b6fd0SVladimir Oltean 		case 2:
1030827b6fd0SVladimir Oltean 			tpid = htons(enetc_port_rd(&priv->si->hw,
1031827b6fd0SVladimir Oltean 						   ENETC_PCVLANR1));
1032827b6fd0SVladimir Oltean 			break;
1033827b6fd0SVladimir Oltean 		case 3:
1034827b6fd0SVladimir Oltean 			tpid = htons(enetc_port_rd(&priv->si->hw,
1035827b6fd0SVladimir Oltean 						   ENETC_PCVLANR2));
1036827b6fd0SVladimir Oltean 			break;
1037827b6fd0SVladimir Oltean 		default:
1038827b6fd0SVladimir Oltean 			break;
1039827b6fd0SVladimir Oltean 		}
1040827b6fd0SVladimir Oltean 
1041827b6fd0SVladimir Oltean 		__vlan_hwaccel_put_tag(skb, tpid, le16_to_cpu(rxbd->r.vlan_opt));
1042827b6fd0SVladimir Oltean 	}
1043827b6fd0SVladimir Oltean 
1044434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
1045d3982312SY.b. Lu 	if (priv->active_offloads & ENETC_F_RX_TSTAMP)
1046d3982312SY.b. Lu 		enetc_get_rx_tstamp(rx_ring->ndev, rxbd, skb);
1047d3982312SY.b. Lu #endif
1048d4fd0404SClaudiu Manoil }
1049d4fd0404SClaudiu Manoil 
10507ed2bc80SVladimir Oltean /* This gets called during the non-XDP NAPI poll cycle as well as on XDP_PASS,
10517ed2bc80SVladimir Oltean  * so it needs to work with both DMA_FROM_DEVICE as well as DMA_BIDIRECTIONAL
10527ed2bc80SVladimir Oltean  * mapped buffers.
10537ed2bc80SVladimir Oltean  */
1054d4fd0404SClaudiu Manoil static struct enetc_rx_swbd *enetc_get_rx_buff(struct enetc_bdr *rx_ring,
1055d4fd0404SClaudiu Manoil 					       int i, u16 size)
1056d4fd0404SClaudiu Manoil {
1057d4fd0404SClaudiu Manoil 	struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[i];
1058d4fd0404SClaudiu Manoil 
1059d4fd0404SClaudiu Manoil 	dma_sync_single_range_for_cpu(rx_ring->dev, rx_swbd->dma,
1060d4fd0404SClaudiu Manoil 				      rx_swbd->page_offset,
10617ed2bc80SVladimir Oltean 				      size, rx_swbd->dir);
1062d4fd0404SClaudiu Manoil 	return rx_swbd;
1063d4fd0404SClaudiu Manoil }
1064d4fd0404SClaudiu Manoil 
10656b04830dSVladimir Oltean /* Reuse the current page without performing half-page buffer flipping */
1066d4fd0404SClaudiu Manoil static void enetc_put_rx_buff(struct enetc_bdr *rx_ring,
1067d4fd0404SClaudiu Manoil 			      struct enetc_rx_swbd *rx_swbd)
1068d4fd0404SClaudiu Manoil {
1069d1b15102SVladimir Oltean 	size_t buffer_size = ENETC_RXB_TRUESIZE - rx_ring->buffer_offset;
1070d1b15102SVladimir Oltean 
1071d4fd0404SClaudiu Manoil 	enetc_reuse_page(rx_ring, rx_swbd);
1072d4fd0404SClaudiu Manoil 
1073d4fd0404SClaudiu Manoil 	dma_sync_single_range_for_device(rx_ring->dev, rx_swbd->dma,
1074d4fd0404SClaudiu Manoil 					 rx_swbd->page_offset,
10757ed2bc80SVladimir Oltean 					 buffer_size, rx_swbd->dir);
10766b04830dSVladimir Oltean 
10776b04830dSVladimir Oltean 	rx_swbd->page = NULL;
10786b04830dSVladimir Oltean }
10796b04830dSVladimir Oltean 
10806b04830dSVladimir Oltean /* Reuse the current page by performing half-page buffer flipping */
10816b04830dSVladimir Oltean static void enetc_flip_rx_buff(struct enetc_bdr *rx_ring,
10826b04830dSVladimir Oltean 			       struct enetc_rx_swbd *rx_swbd)
10836b04830dSVladimir Oltean {
10846b04830dSVladimir Oltean 	if (likely(enetc_page_reusable(rx_swbd->page))) {
10856b04830dSVladimir Oltean 		rx_swbd->page_offset ^= ENETC_RXB_TRUESIZE;
10866b04830dSVladimir Oltean 		page_ref_inc(rx_swbd->page);
10876b04830dSVladimir Oltean 
10886b04830dSVladimir Oltean 		enetc_put_rx_buff(rx_ring, rx_swbd);
1089d4fd0404SClaudiu Manoil 	} else {
10907ed2bc80SVladimir Oltean 		dma_unmap_page(rx_ring->dev, rx_swbd->dma, PAGE_SIZE,
10917ed2bc80SVladimir Oltean 			       rx_swbd->dir);
1092d4fd0404SClaudiu Manoil 		rx_swbd->page = NULL;
1093d4fd0404SClaudiu Manoil 	}
10946b04830dSVladimir Oltean }
1095d4fd0404SClaudiu Manoil 
1096d4fd0404SClaudiu Manoil static struct sk_buff *enetc_map_rx_buff_to_skb(struct enetc_bdr *rx_ring,
1097d4fd0404SClaudiu Manoil 						int i, u16 size)
1098d4fd0404SClaudiu Manoil {
1099d4fd0404SClaudiu Manoil 	struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size);
1100d4fd0404SClaudiu Manoil 	struct sk_buff *skb;
1101d4fd0404SClaudiu Manoil 	void *ba;
1102d4fd0404SClaudiu Manoil 
1103d4fd0404SClaudiu Manoil 	ba = page_address(rx_swbd->page) + rx_swbd->page_offset;
1104d1b15102SVladimir Oltean 	skb = build_skb(ba - rx_ring->buffer_offset, ENETC_RXB_TRUESIZE);
1105d4fd0404SClaudiu Manoil 	if (unlikely(!skb)) {
1106d4fd0404SClaudiu Manoil 		rx_ring->stats.rx_alloc_errs++;
1107d4fd0404SClaudiu Manoil 		return NULL;
1108d4fd0404SClaudiu Manoil 	}
1109d4fd0404SClaudiu Manoil 
1110d1b15102SVladimir Oltean 	skb_reserve(skb, rx_ring->buffer_offset);
1111d4fd0404SClaudiu Manoil 	__skb_put(skb, size);
1112d4fd0404SClaudiu Manoil 
11136b04830dSVladimir Oltean 	enetc_flip_rx_buff(rx_ring, rx_swbd);
1114d4fd0404SClaudiu Manoil 
1115d4fd0404SClaudiu Manoil 	return skb;
1116d4fd0404SClaudiu Manoil }
1117d4fd0404SClaudiu Manoil 
1118d4fd0404SClaudiu Manoil static void enetc_add_rx_buff_to_skb(struct enetc_bdr *rx_ring, int i,
1119d4fd0404SClaudiu Manoil 				     u16 size, struct sk_buff *skb)
1120d4fd0404SClaudiu Manoil {
1121d4fd0404SClaudiu Manoil 	struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size);
1122d4fd0404SClaudiu Manoil 
1123d4fd0404SClaudiu Manoil 	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_swbd->page,
1124d4fd0404SClaudiu Manoil 			rx_swbd->page_offset, size, ENETC_RXB_TRUESIZE);
1125d4fd0404SClaudiu Manoil 
11266b04830dSVladimir Oltean 	enetc_flip_rx_buff(rx_ring, rx_swbd);
1127d4fd0404SClaudiu Manoil }
1128d4fd0404SClaudiu Manoil 
11292fa423f5SVladimir Oltean static bool enetc_check_bd_errors_and_consume(struct enetc_bdr *rx_ring,
11302fa423f5SVladimir Oltean 					      u32 bd_status,
11312fa423f5SVladimir Oltean 					      union enetc_rx_bd **rxbd, int *i)
11322fa423f5SVladimir Oltean {
11332fa423f5SVladimir Oltean 	if (likely(!(bd_status & ENETC_RXBD_LSTATUS(ENETC_RXBD_ERR_MASK))))
11342fa423f5SVladimir Oltean 		return false;
11352fa423f5SVladimir Oltean 
1136672f9a21SVladimir Oltean 	enetc_put_rx_buff(rx_ring, &rx_ring->rx_swbd[*i]);
11372fa423f5SVladimir Oltean 	enetc_rxbd_next(rx_ring, rxbd, i);
11382fa423f5SVladimir Oltean 
11392fa423f5SVladimir Oltean 	while (!(bd_status & ENETC_RXBD_LSTATUS_F)) {
11402fa423f5SVladimir Oltean 		dma_rmb();
11412fa423f5SVladimir Oltean 		bd_status = le32_to_cpu((*rxbd)->r.lstatus);
11422fa423f5SVladimir Oltean 
1143672f9a21SVladimir Oltean 		enetc_put_rx_buff(rx_ring, &rx_ring->rx_swbd[*i]);
11442fa423f5SVladimir Oltean 		enetc_rxbd_next(rx_ring, rxbd, i);
11452fa423f5SVladimir Oltean 	}
11462fa423f5SVladimir Oltean 
11472fa423f5SVladimir Oltean 	rx_ring->ndev->stats.rx_dropped++;
11482fa423f5SVladimir Oltean 	rx_ring->ndev->stats.rx_errors++;
11492fa423f5SVladimir Oltean 
11502fa423f5SVladimir Oltean 	return true;
11512fa423f5SVladimir Oltean }
11522fa423f5SVladimir Oltean 
1153a800abd3SVladimir Oltean static struct sk_buff *enetc_build_skb(struct enetc_bdr *rx_ring,
1154a800abd3SVladimir Oltean 				       u32 bd_status, union enetc_rx_bd **rxbd,
1155d1b15102SVladimir Oltean 				       int *i, int *cleaned_cnt, int buffer_size)
1156a800abd3SVladimir Oltean {
1157a800abd3SVladimir Oltean 	struct sk_buff *skb;
1158a800abd3SVladimir Oltean 	u16 size;
1159a800abd3SVladimir Oltean 
1160a800abd3SVladimir Oltean 	size = le16_to_cpu((*rxbd)->r.buf_len);
1161a800abd3SVladimir Oltean 	skb = enetc_map_rx_buff_to_skb(rx_ring, *i, size);
1162a800abd3SVladimir Oltean 	if (!skb)
1163a800abd3SVladimir Oltean 		return NULL;
1164a800abd3SVladimir Oltean 
1165a800abd3SVladimir Oltean 	enetc_get_offloads(rx_ring, *rxbd, skb);
1166a800abd3SVladimir Oltean 
1167a800abd3SVladimir Oltean 	(*cleaned_cnt)++;
1168a800abd3SVladimir Oltean 
1169a800abd3SVladimir Oltean 	enetc_rxbd_next(rx_ring, rxbd, i);
1170a800abd3SVladimir Oltean 
1171a800abd3SVladimir Oltean 	/* not last BD in frame? */
1172a800abd3SVladimir Oltean 	while (!(bd_status & ENETC_RXBD_LSTATUS_F)) {
1173a800abd3SVladimir Oltean 		bd_status = le32_to_cpu((*rxbd)->r.lstatus);
1174d1b15102SVladimir Oltean 		size = buffer_size;
1175a800abd3SVladimir Oltean 
1176a800abd3SVladimir Oltean 		if (bd_status & ENETC_RXBD_LSTATUS_F) {
1177a800abd3SVladimir Oltean 			dma_rmb();
1178a800abd3SVladimir Oltean 			size = le16_to_cpu((*rxbd)->r.buf_len);
1179a800abd3SVladimir Oltean 		}
1180a800abd3SVladimir Oltean 
1181a800abd3SVladimir Oltean 		enetc_add_rx_buff_to_skb(rx_ring, *i, size, skb);
1182a800abd3SVladimir Oltean 
1183a800abd3SVladimir Oltean 		(*cleaned_cnt)++;
1184a800abd3SVladimir Oltean 
1185a800abd3SVladimir Oltean 		enetc_rxbd_next(rx_ring, rxbd, i);
1186a800abd3SVladimir Oltean 	}
1187a800abd3SVladimir Oltean 
1188a800abd3SVladimir Oltean 	skb_record_rx_queue(skb, rx_ring->index);
1189a800abd3SVladimir Oltean 	skb->protocol = eth_type_trans(skb, rx_ring->ndev);
1190a800abd3SVladimir Oltean 
1191a800abd3SVladimir Oltean 	return skb;
1192a800abd3SVladimir Oltean }
1193a800abd3SVladimir Oltean 
1194d4fd0404SClaudiu Manoil #define ENETC_RXBD_BUNDLE 16 /* # of BDs to update at once */
1195d4fd0404SClaudiu Manoil 
1196d4fd0404SClaudiu Manoil static int enetc_clean_rx_ring(struct enetc_bdr *rx_ring,
1197d4fd0404SClaudiu Manoil 			       struct napi_struct *napi, int work_limit)
1198d4fd0404SClaudiu Manoil {
1199d4fd0404SClaudiu Manoil 	int rx_frm_cnt = 0, rx_byte_cnt = 0;
1200d4fd0404SClaudiu Manoil 	int cleaned_cnt, i;
1201d4fd0404SClaudiu Manoil 
1202d4fd0404SClaudiu Manoil 	cleaned_cnt = enetc_bd_unused(rx_ring);
1203d4fd0404SClaudiu Manoil 	/* next descriptor to process */
1204d4fd0404SClaudiu Manoil 	i = rx_ring->next_to_clean;
1205d4fd0404SClaudiu Manoil 
1206d4fd0404SClaudiu Manoil 	while (likely(rx_frm_cnt < work_limit)) {
1207d4fd0404SClaudiu Manoil 		union enetc_rx_bd *rxbd;
1208d4fd0404SClaudiu Manoil 		struct sk_buff *skb;
1209d4fd0404SClaudiu Manoil 		u32 bd_status;
1210d4fd0404SClaudiu Manoil 
12117a5222cbSVladimir Oltean 		if (cleaned_cnt >= ENETC_RXBD_BUNDLE)
12127a5222cbSVladimir Oltean 			cleaned_cnt -= enetc_refill_rx_ring(rx_ring,
12137a5222cbSVladimir Oltean 							    cleaned_cnt);
1214d4fd0404SClaudiu Manoil 
1215714239acSClaudiu Manoil 		rxbd = enetc_rxbd(rx_ring, i);
1216d4fd0404SClaudiu Manoil 		bd_status = le32_to_cpu(rxbd->r.lstatus);
12176d36ecdbSVladimir Oltean 		if (!bd_status)
1218d4fd0404SClaudiu Manoil 			break;
1219d4fd0404SClaudiu Manoil 
1220fd5736bfSAlex Marginean 		enetc_wr_reg_hot(rx_ring->idr, BIT(rx_ring->index));
1221d4fd0404SClaudiu Manoil 		dma_rmb(); /* for reading other rxbd fields */
12222fa423f5SVladimir Oltean 
12232fa423f5SVladimir Oltean 		if (enetc_check_bd_errors_and_consume(rx_ring, bd_status,
12242fa423f5SVladimir Oltean 						      &rxbd, &i))
12252fa423f5SVladimir Oltean 			break;
12262fa423f5SVladimir Oltean 
1227a800abd3SVladimir Oltean 		skb = enetc_build_skb(rx_ring, bd_status, &rxbd, &i,
1228d1b15102SVladimir Oltean 				      &cleaned_cnt, ENETC_RXB_DMA_SIZE);
12296d36ecdbSVladimir Oltean 		if (!skb)
1230d4fd0404SClaudiu Manoil 			break;
1231d4fd0404SClaudiu Manoil 
12327190d0ffSWei Fang 		/* When set, the outer VLAN header is extracted and reported
12337190d0ffSWei Fang 		 * in the receive buffer descriptor. So rx_byte_cnt should
12347190d0ffSWei Fang 		 * add the length of the extracted VLAN header.
12357190d0ffSWei Fang 		 */
12367190d0ffSWei Fang 		if (bd_status & ENETC_RXBD_FLAG_VLAN)
12377190d0ffSWei Fang 			rx_byte_cnt += VLAN_HLEN;
12387190d0ffSWei Fang 		rx_byte_cnt += skb->len + ETH_HLEN;
1239a800abd3SVladimir Oltean 		rx_frm_cnt++;
1240d4fd0404SClaudiu Manoil 
1241d4fd0404SClaudiu Manoil 		napi_gro_receive(napi, skb);
1242d4fd0404SClaudiu Manoil 	}
1243d4fd0404SClaudiu Manoil 
1244d4fd0404SClaudiu Manoil 	rx_ring->next_to_clean = i;
1245d4fd0404SClaudiu Manoil 
1246d4fd0404SClaudiu Manoil 	rx_ring->stats.packets += rx_frm_cnt;
1247d4fd0404SClaudiu Manoil 	rx_ring->stats.bytes += rx_byte_cnt;
1248d4fd0404SClaudiu Manoil 
1249d4fd0404SClaudiu Manoil 	return rx_frm_cnt;
1250d4fd0404SClaudiu Manoil }
1251d4fd0404SClaudiu Manoil 
12527ed2bc80SVladimir Oltean static void enetc_xdp_map_tx_buff(struct enetc_bdr *tx_ring, int i,
12537ed2bc80SVladimir Oltean 				  struct enetc_tx_swbd *tx_swbd,
12547ed2bc80SVladimir Oltean 				  int frm_len)
12557ed2bc80SVladimir Oltean {
12567ed2bc80SVladimir Oltean 	union enetc_tx_bd *txbd = ENETC_TXBD(*tx_ring, i);
12577ed2bc80SVladimir Oltean 
12587ed2bc80SVladimir Oltean 	prefetchw(txbd);
12597ed2bc80SVladimir Oltean 
12607ed2bc80SVladimir Oltean 	enetc_clear_tx_bd(txbd);
12617ed2bc80SVladimir Oltean 	txbd->addr = cpu_to_le64(tx_swbd->dma + tx_swbd->page_offset);
12627ed2bc80SVladimir Oltean 	txbd->buf_len = cpu_to_le16(tx_swbd->len);
12637ed2bc80SVladimir Oltean 	txbd->frm_len = cpu_to_le16(frm_len);
12647ed2bc80SVladimir Oltean 
12657ed2bc80SVladimir Oltean 	memcpy(&tx_ring->tx_swbd[i], tx_swbd, sizeof(*tx_swbd));
12667ed2bc80SVladimir Oltean }
12677ed2bc80SVladimir Oltean 
12687ed2bc80SVladimir Oltean /* Puts in the TX ring one XDP frame, mapped as an array of TX software buffer
12697ed2bc80SVladimir Oltean  * descriptors.
12707ed2bc80SVladimir Oltean  */
12717ed2bc80SVladimir Oltean static bool enetc_xdp_tx(struct enetc_bdr *tx_ring,
12727ed2bc80SVladimir Oltean 			 struct enetc_tx_swbd *xdp_tx_arr, int num_tx_swbd)
12737ed2bc80SVladimir Oltean {
12747ed2bc80SVladimir Oltean 	struct enetc_tx_swbd *tmp_tx_swbd = xdp_tx_arr;
12757ed2bc80SVladimir Oltean 	int i, k, frm_len = tmp_tx_swbd->len;
12767ed2bc80SVladimir Oltean 
12777ed2bc80SVladimir Oltean 	if (unlikely(enetc_bd_unused(tx_ring) < ENETC_TXBDS_NEEDED(num_tx_swbd)))
12787ed2bc80SVladimir Oltean 		return false;
12797ed2bc80SVladimir Oltean 
12807ed2bc80SVladimir Oltean 	while (unlikely(!tmp_tx_swbd->is_eof)) {
12817ed2bc80SVladimir Oltean 		tmp_tx_swbd++;
12827ed2bc80SVladimir Oltean 		frm_len += tmp_tx_swbd->len;
12837ed2bc80SVladimir Oltean 	}
12847ed2bc80SVladimir Oltean 
12857ed2bc80SVladimir Oltean 	i = tx_ring->next_to_use;
12867ed2bc80SVladimir Oltean 
12877ed2bc80SVladimir Oltean 	for (k = 0; k < num_tx_swbd; k++) {
12887ed2bc80SVladimir Oltean 		struct enetc_tx_swbd *xdp_tx_swbd = &xdp_tx_arr[k];
12897ed2bc80SVladimir Oltean 
12907ed2bc80SVladimir Oltean 		enetc_xdp_map_tx_buff(tx_ring, i, xdp_tx_swbd, frm_len);
12917ed2bc80SVladimir Oltean 
12927ed2bc80SVladimir Oltean 		/* last BD needs 'F' bit set */
12937ed2bc80SVladimir Oltean 		if (xdp_tx_swbd->is_eof) {
12947ed2bc80SVladimir Oltean 			union enetc_tx_bd *txbd = ENETC_TXBD(*tx_ring, i);
12957ed2bc80SVladimir Oltean 
12967ed2bc80SVladimir Oltean 			txbd->flags = ENETC_TXBD_FLAGS_F;
12977ed2bc80SVladimir Oltean 		}
12987ed2bc80SVladimir Oltean 
12997ed2bc80SVladimir Oltean 		enetc_bdr_idx_inc(tx_ring, &i);
13007ed2bc80SVladimir Oltean 	}
13017ed2bc80SVladimir Oltean 
13027ed2bc80SVladimir Oltean 	tx_ring->next_to_use = i;
13037ed2bc80SVladimir Oltean 
13047ed2bc80SVladimir Oltean 	return true;
13057ed2bc80SVladimir Oltean }
13067ed2bc80SVladimir Oltean 
13079d2b68ccSVladimir Oltean static int enetc_xdp_frame_to_xdp_tx_swbd(struct enetc_bdr *tx_ring,
13089d2b68ccSVladimir Oltean 					  struct enetc_tx_swbd *xdp_tx_arr,
13099d2b68ccSVladimir Oltean 					  struct xdp_frame *xdp_frame)
13109d2b68ccSVladimir Oltean {
13119d2b68ccSVladimir Oltean 	struct enetc_tx_swbd *xdp_tx_swbd = &xdp_tx_arr[0];
13129d2b68ccSVladimir Oltean 	struct skb_shared_info *shinfo;
13139d2b68ccSVladimir Oltean 	void *data = xdp_frame->data;
13149d2b68ccSVladimir Oltean 	int len = xdp_frame->len;
13159d2b68ccSVladimir Oltean 	skb_frag_t *frag;
13169d2b68ccSVladimir Oltean 	dma_addr_t dma;
13179d2b68ccSVladimir Oltean 	unsigned int f;
13189d2b68ccSVladimir Oltean 	int n = 0;
13199d2b68ccSVladimir Oltean 
13209d2b68ccSVladimir Oltean 	dma = dma_map_single(tx_ring->dev, data, len, DMA_TO_DEVICE);
13219d2b68ccSVladimir Oltean 	if (unlikely(dma_mapping_error(tx_ring->dev, dma))) {
13229d2b68ccSVladimir Oltean 		netdev_err(tx_ring->ndev, "DMA map error\n");
13239d2b68ccSVladimir Oltean 		return -1;
13249d2b68ccSVladimir Oltean 	}
13259d2b68ccSVladimir Oltean 
13269d2b68ccSVladimir Oltean 	xdp_tx_swbd->dma = dma;
13279d2b68ccSVladimir Oltean 	xdp_tx_swbd->dir = DMA_TO_DEVICE;
13289d2b68ccSVladimir Oltean 	xdp_tx_swbd->len = len;
13299d2b68ccSVladimir Oltean 	xdp_tx_swbd->is_xdp_redirect = true;
13309d2b68ccSVladimir Oltean 	xdp_tx_swbd->is_eof = false;
13319d2b68ccSVladimir Oltean 	xdp_tx_swbd->xdp_frame = NULL;
13329d2b68ccSVladimir Oltean 
13339d2b68ccSVladimir Oltean 	n++;
1334c7030d14SLorenzo Bianconi 
1335c7030d14SLorenzo Bianconi 	if (!xdp_frame_has_frags(xdp_frame))
1336c7030d14SLorenzo Bianconi 		goto out;
1337c7030d14SLorenzo Bianconi 
13389d2b68ccSVladimir Oltean 	xdp_tx_swbd = &xdp_tx_arr[n];
13399d2b68ccSVladimir Oltean 
13409d2b68ccSVladimir Oltean 	shinfo = xdp_get_shared_info_from_frame(xdp_frame);
13419d2b68ccSVladimir Oltean 
13429d2b68ccSVladimir Oltean 	for (f = 0, frag = &shinfo->frags[0]; f < shinfo->nr_frags;
13439d2b68ccSVladimir Oltean 	     f++, frag++) {
13449d2b68ccSVladimir Oltean 		data = skb_frag_address(frag);
13459d2b68ccSVladimir Oltean 		len = skb_frag_size(frag);
13469d2b68ccSVladimir Oltean 
13479d2b68ccSVladimir Oltean 		dma = dma_map_single(tx_ring->dev, data, len, DMA_TO_DEVICE);
13489d2b68ccSVladimir Oltean 		if (unlikely(dma_mapping_error(tx_ring->dev, dma))) {
13499d2b68ccSVladimir Oltean 			/* Undo the DMA mapping for all fragments */
1350626b598aSDan Carpenter 			while (--n >= 0)
13519d2b68ccSVladimir Oltean 				enetc_unmap_tx_buff(tx_ring, &xdp_tx_arr[n]);
13529d2b68ccSVladimir Oltean 
13539d2b68ccSVladimir Oltean 			netdev_err(tx_ring->ndev, "DMA map error\n");
13549d2b68ccSVladimir Oltean 			return -1;
13559d2b68ccSVladimir Oltean 		}
13569d2b68ccSVladimir Oltean 
13579d2b68ccSVladimir Oltean 		xdp_tx_swbd->dma = dma;
13589d2b68ccSVladimir Oltean 		xdp_tx_swbd->dir = DMA_TO_DEVICE;
13599d2b68ccSVladimir Oltean 		xdp_tx_swbd->len = len;
13609d2b68ccSVladimir Oltean 		xdp_tx_swbd->is_xdp_redirect = true;
13619d2b68ccSVladimir Oltean 		xdp_tx_swbd->is_eof = false;
13629d2b68ccSVladimir Oltean 		xdp_tx_swbd->xdp_frame = NULL;
13639d2b68ccSVladimir Oltean 
13649d2b68ccSVladimir Oltean 		n++;
13659d2b68ccSVladimir Oltean 		xdp_tx_swbd = &xdp_tx_arr[n];
13669d2b68ccSVladimir Oltean 	}
1367c7030d14SLorenzo Bianconi out:
13689d2b68ccSVladimir Oltean 	xdp_tx_arr[n - 1].is_eof = true;
13699d2b68ccSVladimir Oltean 	xdp_tx_arr[n - 1].xdp_frame = xdp_frame;
13709d2b68ccSVladimir Oltean 
13719d2b68ccSVladimir Oltean 	return n;
13729d2b68ccSVladimir Oltean }
13739d2b68ccSVladimir Oltean 
13749d2b68ccSVladimir Oltean int enetc_xdp_xmit(struct net_device *ndev, int num_frames,
13759d2b68ccSVladimir Oltean 		   struct xdp_frame **frames, u32 flags)
13769d2b68ccSVladimir Oltean {
13779d2b68ccSVladimir Oltean 	struct enetc_tx_swbd xdp_redirect_arr[ENETC_MAX_SKB_FRAGS] = {0};
13789d2b68ccSVladimir Oltean 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
13799d2b68ccSVladimir Oltean 	struct enetc_bdr *tx_ring;
13809d2b68ccSVladimir Oltean 	int xdp_tx_bd_cnt, i, k;
13819d2b68ccSVladimir Oltean 	int xdp_tx_frm_cnt = 0;
13829d2b68ccSVladimir Oltean 
138324e39309SVladimir Oltean 	enetc_lock_mdio();
138424e39309SVladimir Oltean 
13857eab503bSVladimir Oltean 	tx_ring = priv->xdp_tx_ring[smp_processor_id()];
13869d2b68ccSVladimir Oltean 
13879d2b68ccSVladimir Oltean 	prefetchw(ENETC_TXBD(*tx_ring, tx_ring->next_to_use));
13889d2b68ccSVladimir Oltean 
13899d2b68ccSVladimir Oltean 	for (k = 0; k < num_frames; k++) {
13909d2b68ccSVladimir Oltean 		xdp_tx_bd_cnt = enetc_xdp_frame_to_xdp_tx_swbd(tx_ring,
13919d2b68ccSVladimir Oltean 							       xdp_redirect_arr,
13929d2b68ccSVladimir Oltean 							       frames[k]);
13939d2b68ccSVladimir Oltean 		if (unlikely(xdp_tx_bd_cnt < 0))
13949d2b68ccSVladimir Oltean 			break;
13959d2b68ccSVladimir Oltean 
13969d2b68ccSVladimir Oltean 		if (unlikely(!enetc_xdp_tx(tx_ring, xdp_redirect_arr,
13979d2b68ccSVladimir Oltean 					   xdp_tx_bd_cnt))) {
13989d2b68ccSVladimir Oltean 			for (i = 0; i < xdp_tx_bd_cnt; i++)
13999d2b68ccSVladimir Oltean 				enetc_unmap_tx_buff(tx_ring,
14009d2b68ccSVladimir Oltean 						    &xdp_redirect_arr[i]);
14019d2b68ccSVladimir Oltean 			tx_ring->stats.xdp_tx_drops++;
14029d2b68ccSVladimir Oltean 			break;
14039d2b68ccSVladimir Oltean 		}
14049d2b68ccSVladimir Oltean 
14059d2b68ccSVladimir Oltean 		xdp_tx_frm_cnt++;
14069d2b68ccSVladimir Oltean 	}
14079d2b68ccSVladimir Oltean 
14089d2b68ccSVladimir Oltean 	if (unlikely((flags & XDP_XMIT_FLUSH) || k != xdp_tx_frm_cnt))
14099d2b68ccSVladimir Oltean 		enetc_update_tx_ring_tail(tx_ring);
14109d2b68ccSVladimir Oltean 
14119d2b68ccSVladimir Oltean 	tx_ring->stats.xdp_tx += xdp_tx_frm_cnt;
14129d2b68ccSVladimir Oltean 
141324e39309SVladimir Oltean 	enetc_unlock_mdio();
141424e39309SVladimir Oltean 
14159d2b68ccSVladimir Oltean 	return xdp_tx_frm_cnt;
14169d2b68ccSVladimir Oltean }
1417e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_xdp_xmit);
14189d2b68ccSVladimir Oltean 
1419d1b15102SVladimir Oltean static void enetc_map_rx_buff_to_xdp(struct enetc_bdr *rx_ring, int i,
1420d1b15102SVladimir Oltean 				     struct xdp_buff *xdp_buff, u16 size)
1421d1b15102SVladimir Oltean {
1422d1b15102SVladimir Oltean 	struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size);
1423d1b15102SVladimir Oltean 	void *hard_start = page_address(rx_swbd->page) + rx_swbd->page_offset;
1424d1b15102SVladimir Oltean 
14257ed2bc80SVladimir Oltean 	/* To be used for XDP_TX */
14267ed2bc80SVladimir Oltean 	rx_swbd->len = size;
14277ed2bc80SVladimir Oltean 
1428d1b15102SVladimir Oltean 	xdp_prepare_buff(xdp_buff, hard_start - rx_ring->buffer_offset,
1429d1b15102SVladimir Oltean 			 rx_ring->buffer_offset, size, false);
1430d1b15102SVladimir Oltean }
1431d1b15102SVladimir Oltean 
1432d1b15102SVladimir Oltean static void enetc_add_rx_buff_to_xdp(struct enetc_bdr *rx_ring, int i,
1433d1b15102SVladimir Oltean 				     u16 size, struct xdp_buff *xdp_buff)
1434d1b15102SVladimir Oltean {
1435d1b15102SVladimir Oltean 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp_buff);
1436d1b15102SVladimir Oltean 	struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size);
1437c7030d14SLorenzo Bianconi 	skb_frag_t *frag;
1438d1b15102SVladimir Oltean 
14397ed2bc80SVladimir Oltean 	/* To be used for XDP_TX */
14407ed2bc80SVladimir Oltean 	rx_swbd->len = size;
14417ed2bc80SVladimir Oltean 
14428feb020fSLorenzo Bianconi 	if (!xdp_buff_has_frags(xdp_buff)) {
14438feb020fSLorenzo Bianconi 		xdp_buff_set_frags_flag(xdp_buff);
14448feb020fSLorenzo Bianconi 		shinfo->xdp_frags_size = size;
1445c7030d14SLorenzo Bianconi 		shinfo->nr_frags = 0;
14468feb020fSLorenzo Bianconi 	} else {
14478feb020fSLorenzo Bianconi 		shinfo->xdp_frags_size += size;
14488feb020fSLorenzo Bianconi 	}
14498feb020fSLorenzo Bianconi 
14508feb020fSLorenzo Bianconi 	if (page_is_pfmemalloc(rx_swbd->page))
14518feb020fSLorenzo Bianconi 		xdp_buff_set_frag_pfmemalloc(xdp_buff);
14528feb020fSLorenzo Bianconi 
1453c7030d14SLorenzo Bianconi 	frag = &shinfo->frags[shinfo->nr_frags];
1454b51f4113SYunsheng Lin 	skb_frag_fill_page_desc(frag, rx_swbd->page, rx_swbd->page_offset,
1455b51f4113SYunsheng Lin 				size);
1456d1b15102SVladimir Oltean 
1457d1b15102SVladimir Oltean 	shinfo->nr_frags++;
1458d1b15102SVladimir Oltean }
1459d1b15102SVladimir Oltean 
1460d1b15102SVladimir Oltean static void enetc_build_xdp_buff(struct enetc_bdr *rx_ring, u32 bd_status,
1461d1b15102SVladimir Oltean 				 union enetc_rx_bd **rxbd, int *i,
1462d1b15102SVladimir Oltean 				 int *cleaned_cnt, struct xdp_buff *xdp_buff)
1463d1b15102SVladimir Oltean {
1464d1b15102SVladimir Oltean 	u16 size = le16_to_cpu((*rxbd)->r.buf_len);
1465d1b15102SVladimir Oltean 
1466d1b15102SVladimir Oltean 	xdp_init_buff(xdp_buff, ENETC_RXB_TRUESIZE, &rx_ring->xdp.rxq);
1467d1b15102SVladimir Oltean 
1468d1b15102SVladimir Oltean 	enetc_map_rx_buff_to_xdp(rx_ring, *i, xdp_buff, size);
1469d1b15102SVladimir Oltean 	(*cleaned_cnt)++;
1470d1b15102SVladimir Oltean 	enetc_rxbd_next(rx_ring, rxbd, i);
1471d1b15102SVladimir Oltean 
1472d1b15102SVladimir Oltean 	/* not last BD in frame? */
1473d1b15102SVladimir Oltean 	while (!(bd_status & ENETC_RXBD_LSTATUS_F)) {
1474d1b15102SVladimir Oltean 		bd_status = le32_to_cpu((*rxbd)->r.lstatus);
1475d1b15102SVladimir Oltean 		size = ENETC_RXB_DMA_SIZE_XDP;
1476d1b15102SVladimir Oltean 
1477d1b15102SVladimir Oltean 		if (bd_status & ENETC_RXBD_LSTATUS_F) {
1478d1b15102SVladimir Oltean 			dma_rmb();
1479d1b15102SVladimir Oltean 			size = le16_to_cpu((*rxbd)->r.buf_len);
1480d1b15102SVladimir Oltean 		}
1481d1b15102SVladimir Oltean 
1482d1b15102SVladimir Oltean 		enetc_add_rx_buff_to_xdp(rx_ring, *i, size, xdp_buff);
1483d1b15102SVladimir Oltean 		(*cleaned_cnt)++;
1484d1b15102SVladimir Oltean 		enetc_rxbd_next(rx_ring, rxbd, i);
1485d1b15102SVladimir Oltean 	}
1486d1b15102SVladimir Oltean }
1487d1b15102SVladimir Oltean 
14887ed2bc80SVladimir Oltean /* Convert RX buffer descriptors to TX buffer descriptors. These will be
148992ff9a6eSVladimir Oltean  * recycled back into the RX ring in enetc_clean_tx_ring.
14907ed2bc80SVladimir Oltean  */
14917ed2bc80SVladimir Oltean static int enetc_rx_swbd_to_xdp_tx_swbd(struct enetc_tx_swbd *xdp_tx_arr,
14927ed2bc80SVladimir Oltean 					struct enetc_bdr *rx_ring,
14937ed2bc80SVladimir Oltean 					int rx_ring_first, int rx_ring_last)
14947ed2bc80SVladimir Oltean {
14957ed2bc80SVladimir Oltean 	int n = 0;
14967ed2bc80SVladimir Oltean 
14977ed2bc80SVladimir Oltean 	for (; rx_ring_first != rx_ring_last;
14987ed2bc80SVladimir Oltean 	     n++, enetc_bdr_idx_inc(rx_ring, &rx_ring_first)) {
14997ed2bc80SVladimir Oltean 		struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[rx_ring_first];
15007ed2bc80SVladimir Oltean 		struct enetc_tx_swbd *tx_swbd = &xdp_tx_arr[n];
15017ed2bc80SVladimir Oltean 
15027ed2bc80SVladimir Oltean 		/* No need to dma_map, we already have DMA_BIDIRECTIONAL */
15037ed2bc80SVladimir Oltean 		tx_swbd->dma = rx_swbd->dma;
15047ed2bc80SVladimir Oltean 		tx_swbd->dir = rx_swbd->dir;
15057ed2bc80SVladimir Oltean 		tx_swbd->page = rx_swbd->page;
15067ed2bc80SVladimir Oltean 		tx_swbd->page_offset = rx_swbd->page_offset;
15077ed2bc80SVladimir Oltean 		tx_swbd->len = rx_swbd->len;
15087ed2bc80SVladimir Oltean 		tx_swbd->is_dma_page = true;
15097ed2bc80SVladimir Oltean 		tx_swbd->is_xdp_tx = true;
15107ed2bc80SVladimir Oltean 		tx_swbd->is_eof = false;
15117ed2bc80SVladimir Oltean 	}
15127ed2bc80SVladimir Oltean 
15137ed2bc80SVladimir Oltean 	/* We rely on caller providing an rx_ring_last > rx_ring_first */
15147ed2bc80SVladimir Oltean 	xdp_tx_arr[n - 1].is_eof = true;
15157ed2bc80SVladimir Oltean 
15167ed2bc80SVladimir Oltean 	return n;
15177ed2bc80SVladimir Oltean }
15187ed2bc80SVladimir Oltean 
1519d1b15102SVladimir Oltean static void enetc_xdp_drop(struct enetc_bdr *rx_ring, int rx_ring_first,
1520d1b15102SVladimir Oltean 			   int rx_ring_last)
1521d1b15102SVladimir Oltean {
1522d1b15102SVladimir Oltean 	while (rx_ring_first != rx_ring_last) {
15236b04830dSVladimir Oltean 		enetc_put_rx_buff(rx_ring,
1524d1b15102SVladimir Oltean 				  &rx_ring->rx_swbd[rx_ring_first]);
1525d1b15102SVladimir Oltean 		enetc_bdr_idx_inc(rx_ring, &rx_ring_first);
1526d1b15102SVladimir Oltean 	}
1527d1b15102SVladimir Oltean }
1528d1b15102SVladimir Oltean 
1529d1b15102SVladimir Oltean static int enetc_clean_rx_ring_xdp(struct enetc_bdr *rx_ring,
1530d1b15102SVladimir Oltean 				   struct napi_struct *napi, int work_limit,
1531d1b15102SVladimir Oltean 				   struct bpf_prog *prog)
1532d1b15102SVladimir Oltean {
15339d2b68ccSVladimir Oltean 	int xdp_tx_bd_cnt, xdp_tx_frm_cnt = 0, xdp_redirect_frm_cnt = 0;
15347ed2bc80SVladimir Oltean 	struct enetc_tx_swbd xdp_tx_arr[ENETC_MAX_SKB_FRAGS] = {0};
15357ed2bc80SVladimir Oltean 	struct enetc_ndev_priv *priv = netdev_priv(rx_ring->ndev);
1536d1b15102SVladimir Oltean 	int rx_frm_cnt = 0, rx_byte_cnt = 0;
15377eab503bSVladimir Oltean 	struct enetc_bdr *tx_ring;
1538d1b15102SVladimir Oltean 	int cleaned_cnt, i;
1539d1b15102SVladimir Oltean 	u32 xdp_act;
1540d1b15102SVladimir Oltean 
1541d1b15102SVladimir Oltean 	cleaned_cnt = enetc_bd_unused(rx_ring);
1542d1b15102SVladimir Oltean 	/* next descriptor to process */
1543d1b15102SVladimir Oltean 	i = rx_ring->next_to_clean;
1544d1b15102SVladimir Oltean 
1545d1b15102SVladimir Oltean 	while (likely(rx_frm_cnt < work_limit)) {
1546d1b15102SVladimir Oltean 		union enetc_rx_bd *rxbd, *orig_rxbd;
1547d1b15102SVladimir Oltean 		int orig_i, orig_cleaned_cnt;
1548d1b15102SVladimir Oltean 		struct xdp_buff xdp_buff;
1549d1b15102SVladimir Oltean 		struct sk_buff *skb;
1550d1b15102SVladimir Oltean 		u32 bd_status;
1551628050ecSVladimir Oltean 		int err;
1552d1b15102SVladimir Oltean 
1553d1b15102SVladimir Oltean 		rxbd = enetc_rxbd(rx_ring, i);
1554d1b15102SVladimir Oltean 		bd_status = le32_to_cpu(rxbd->r.lstatus);
1555d1b15102SVladimir Oltean 		if (!bd_status)
1556d1b15102SVladimir Oltean 			break;
1557d1b15102SVladimir Oltean 
1558d1b15102SVladimir Oltean 		enetc_wr_reg_hot(rx_ring->idr, BIT(rx_ring->index));
1559d1b15102SVladimir Oltean 		dma_rmb(); /* for reading other rxbd fields */
1560d1b15102SVladimir Oltean 
1561d1b15102SVladimir Oltean 		if (enetc_check_bd_errors_and_consume(rx_ring, bd_status,
1562d1b15102SVladimir Oltean 						      &rxbd, &i))
1563d1b15102SVladimir Oltean 			break;
1564d1b15102SVladimir Oltean 
1565d1b15102SVladimir Oltean 		orig_rxbd = rxbd;
1566d1b15102SVladimir Oltean 		orig_cleaned_cnt = cleaned_cnt;
1567d1b15102SVladimir Oltean 		orig_i = i;
1568d1b15102SVladimir Oltean 
1569d1b15102SVladimir Oltean 		enetc_build_xdp_buff(rx_ring, bd_status, &rxbd, &i,
1570d1b15102SVladimir Oltean 				     &cleaned_cnt, &xdp_buff);
1571d1b15102SVladimir Oltean 
1572fdebd850SWei Fang 		/* When set, the outer VLAN header is extracted and reported
1573fdebd850SWei Fang 		 * in the receive buffer descriptor. So rx_byte_cnt should
1574fdebd850SWei Fang 		 * add the length of the extracted VLAN header.
1575fdebd850SWei Fang 		 */
1576fdebd850SWei Fang 		if (bd_status & ENETC_RXBD_FLAG_VLAN)
1577fdebd850SWei Fang 			rx_byte_cnt += VLAN_HLEN;
1578fdebd850SWei Fang 		rx_byte_cnt += xdp_get_buff_len(&xdp_buff);
1579fdebd850SWei Fang 
1580d1b15102SVladimir Oltean 		xdp_act = bpf_prog_run_xdp(prog, &xdp_buff);
1581d1b15102SVladimir Oltean 
1582d1b15102SVladimir Oltean 		switch (xdp_act) {
1583975acc83SVladimir Oltean 		default:
1584c8064e5bSPaolo Abeni 			bpf_warn_invalid_xdp_action(rx_ring->ndev, prog, xdp_act);
1585975acc83SVladimir Oltean 			fallthrough;
1586d1b15102SVladimir Oltean 		case XDP_ABORTED:
1587d1b15102SVladimir Oltean 			trace_xdp_exception(rx_ring->ndev, prog, xdp_act);
1588d1b15102SVladimir Oltean 			fallthrough;
1589d1b15102SVladimir Oltean 		case XDP_DROP:
1590d1b15102SVladimir Oltean 			enetc_xdp_drop(rx_ring, orig_i, i);
1591*1c95443eSWei Fang 			rx_ring->stats.xdp_drops++;
1592d1b15102SVladimir Oltean 			break;
1593d1b15102SVladimir Oltean 		case XDP_PASS:
1594d1b15102SVladimir Oltean 			rxbd = orig_rxbd;
1595d1b15102SVladimir Oltean 			cleaned_cnt = orig_cleaned_cnt;
1596d1b15102SVladimir Oltean 			i = orig_i;
1597d1b15102SVladimir Oltean 
1598d1b15102SVladimir Oltean 			skb = enetc_build_skb(rx_ring, bd_status, &rxbd,
1599d1b15102SVladimir Oltean 					      &i, &cleaned_cnt,
1600d1b15102SVladimir Oltean 					      ENETC_RXB_DMA_SIZE_XDP);
1601d1b15102SVladimir Oltean 			if (unlikely(!skb))
16028f50d8bbSVladimir Oltean 				goto out;
1603d1b15102SVladimir Oltean 
1604d1b15102SVladimir Oltean 			napi_gro_receive(napi, skb);
1605d1b15102SVladimir Oltean 			break;
16067ed2bc80SVladimir Oltean 		case XDP_TX:
16077eab503bSVladimir Oltean 			tx_ring = priv->xdp_tx_ring[rx_ring->index];
16087ed2bc80SVladimir Oltean 			xdp_tx_bd_cnt = enetc_rx_swbd_to_xdp_tx_swbd(xdp_tx_arr,
16097ed2bc80SVladimir Oltean 								     rx_ring,
16107ed2bc80SVladimir Oltean 								     orig_i, i);
16117ed2bc80SVladimir Oltean 
16127ed2bc80SVladimir Oltean 			if (!enetc_xdp_tx(tx_ring, xdp_tx_arr, xdp_tx_bd_cnt)) {
16137ed2bc80SVladimir Oltean 				enetc_xdp_drop(rx_ring, orig_i, i);
16147ed2bc80SVladimir Oltean 				tx_ring->stats.xdp_tx_drops++;
16157ed2bc80SVladimir Oltean 			} else {
16167ed2bc80SVladimir Oltean 				tx_ring->stats.xdp_tx += xdp_tx_bd_cnt;
16177ed2bc80SVladimir Oltean 				rx_ring->xdp.xdp_tx_in_flight += xdp_tx_bd_cnt;
16187ed2bc80SVladimir Oltean 				xdp_tx_frm_cnt++;
161992ff9a6eSVladimir Oltean 				/* The XDP_TX enqueue was successful, so we
162092ff9a6eSVladimir Oltean 				 * need to scrub the RX software BDs because
162192ff9a6eSVladimir Oltean 				 * the ownership of the buffers no longer
162292ff9a6eSVladimir Oltean 				 * belongs to the RX ring, and we must prevent
162392ff9a6eSVladimir Oltean 				 * enetc_refill_rx_ring() from reusing
162492ff9a6eSVladimir Oltean 				 * rx_swbd->page.
162592ff9a6eSVladimir Oltean 				 */
162692ff9a6eSVladimir Oltean 				while (orig_i != i) {
162792ff9a6eSVladimir Oltean 					rx_ring->rx_swbd[orig_i].page = NULL;
162892ff9a6eSVladimir Oltean 					enetc_bdr_idx_inc(rx_ring, &orig_i);
162992ff9a6eSVladimir Oltean 				}
16307ed2bc80SVladimir Oltean 			}
16317ed2bc80SVladimir Oltean 			break;
16329d2b68ccSVladimir Oltean 		case XDP_REDIRECT:
1633628050ecSVladimir Oltean 			err = xdp_do_redirect(rx_ring->ndev, &xdp_buff, prog);
1634628050ecSVladimir Oltean 			if (unlikely(err)) {
1635628050ecSVladimir Oltean 				enetc_xdp_drop(rx_ring, orig_i, i);
1636628050ecSVladimir Oltean 				rx_ring->stats.xdp_redirect_failures++;
1637628050ecSVladimir Oltean 			} else {
16389d2b68ccSVladimir Oltean 				while (orig_i != i) {
16396b04830dSVladimir Oltean 					enetc_flip_rx_buff(rx_ring,
16409d2b68ccSVladimir Oltean 							   &rx_ring->rx_swbd[orig_i]);
16419d2b68ccSVladimir Oltean 					enetc_bdr_idx_inc(rx_ring, &orig_i);
16429d2b68ccSVladimir Oltean 				}
16439d2b68ccSVladimir Oltean 				xdp_redirect_frm_cnt++;
16449d2b68ccSVladimir Oltean 				rx_ring->stats.xdp_redirect++;
16459d2b68ccSVladimir Oltean 			}
1646d1b15102SVladimir Oltean 		}
1647d1b15102SVladimir Oltean 
1648d1b15102SVladimir Oltean 		rx_frm_cnt++;
1649d1b15102SVladimir Oltean 	}
1650d1b15102SVladimir Oltean 
16518f50d8bbSVladimir Oltean out:
1652d1b15102SVladimir Oltean 	rx_ring->next_to_clean = i;
1653d1b15102SVladimir Oltean 
1654d1b15102SVladimir Oltean 	rx_ring->stats.packets += rx_frm_cnt;
1655d1b15102SVladimir Oltean 	rx_ring->stats.bytes += rx_byte_cnt;
1656d1b15102SVladimir Oltean 
16579d2b68ccSVladimir Oltean 	if (xdp_redirect_frm_cnt)
16589d2b68ccSVladimir Oltean 		xdp_do_flush_map();
16599d2b68ccSVladimir Oltean 
16607ed2bc80SVladimir Oltean 	if (xdp_tx_frm_cnt)
16617ed2bc80SVladimir Oltean 		enetc_update_tx_ring_tail(tx_ring);
16627ed2bc80SVladimir Oltean 
16637ed2bc80SVladimir Oltean 	if (cleaned_cnt > rx_ring->xdp.xdp_tx_in_flight)
16647ed2bc80SVladimir Oltean 		enetc_refill_rx_ring(rx_ring, enetc_bd_unused(rx_ring) -
16657ed2bc80SVladimir Oltean 				     rx_ring->xdp.xdp_tx_in_flight);
16667ed2bc80SVladimir Oltean 
1667d1b15102SVladimir Oltean 	return rx_frm_cnt;
1668d1b15102SVladimir Oltean }
1669d1b15102SVladimir Oltean 
16708580b3c3SVladimir Oltean static int enetc_poll(struct napi_struct *napi, int budget)
16718580b3c3SVladimir Oltean {
16728580b3c3SVladimir Oltean 	struct enetc_int_vector
16738580b3c3SVladimir Oltean 		*v = container_of(napi, struct enetc_int_vector, napi);
1674d1b15102SVladimir Oltean 	struct enetc_bdr *rx_ring = &v->rx_ring;
1675d1b15102SVladimir Oltean 	struct bpf_prog *prog;
16768580b3c3SVladimir Oltean 	bool complete = true;
16778580b3c3SVladimir Oltean 	int work_done;
16788580b3c3SVladimir Oltean 	int i;
16798580b3c3SVladimir Oltean 
16808580b3c3SVladimir Oltean 	enetc_lock_mdio();
16818580b3c3SVladimir Oltean 
16828580b3c3SVladimir Oltean 	for (i = 0; i < v->count_tx_rings; i++)
16838580b3c3SVladimir Oltean 		if (!enetc_clean_tx_ring(&v->tx_ring[i], budget))
16848580b3c3SVladimir Oltean 			complete = false;
16858580b3c3SVladimir Oltean 
1686d1b15102SVladimir Oltean 	prog = rx_ring->xdp.prog;
1687d1b15102SVladimir Oltean 	if (prog)
1688d1b15102SVladimir Oltean 		work_done = enetc_clean_rx_ring_xdp(rx_ring, napi, budget, prog);
1689d1b15102SVladimir Oltean 	else
1690d1b15102SVladimir Oltean 		work_done = enetc_clean_rx_ring(rx_ring, napi, budget);
16918580b3c3SVladimir Oltean 	if (work_done == budget)
16928580b3c3SVladimir Oltean 		complete = false;
16938580b3c3SVladimir Oltean 	if (work_done)
16948580b3c3SVladimir Oltean 		v->rx_napi_work = true;
16958580b3c3SVladimir Oltean 
16968580b3c3SVladimir Oltean 	if (!complete) {
16978580b3c3SVladimir Oltean 		enetc_unlock_mdio();
16988580b3c3SVladimir Oltean 		return budget;
16998580b3c3SVladimir Oltean 	}
17008580b3c3SVladimir Oltean 
17018580b3c3SVladimir Oltean 	napi_complete_done(napi, work_done);
17028580b3c3SVladimir Oltean 
17038580b3c3SVladimir Oltean 	if (likely(v->rx_dim_en))
17048580b3c3SVladimir Oltean 		enetc_rx_net_dim(v);
17058580b3c3SVladimir Oltean 
17068580b3c3SVladimir Oltean 	v->rx_napi_work = false;
17078580b3c3SVladimir Oltean 
17088580b3c3SVladimir Oltean 	/* enable interrupts */
17098580b3c3SVladimir Oltean 	enetc_wr_reg_hot(v->rbier, ENETC_RBIER_RXTIE);
17108580b3c3SVladimir Oltean 
17118580b3c3SVladimir Oltean 	for_each_set_bit(i, &v->tx_rings_map, ENETC_MAX_NUM_TXQS)
17128580b3c3SVladimir Oltean 		enetc_wr_reg_hot(v->tbier_base + ENETC_BDR_OFF(i),
17138580b3c3SVladimir Oltean 				 ENETC_TBIER_TXTIE);
17148580b3c3SVladimir Oltean 
17158580b3c3SVladimir Oltean 	enetc_unlock_mdio();
17168580b3c3SVladimir Oltean 
17178580b3c3SVladimir Oltean 	return work_done;
17188580b3c3SVladimir Oltean }
17198580b3c3SVladimir Oltean 
1720d4fd0404SClaudiu Manoil /* Probing and Init */
1721d382563fSClaudiu Manoil #define ENETC_MAX_RFS_SIZE 64
1722d4fd0404SClaudiu Manoil void enetc_get_si_caps(struct enetc_si *si)
1723d4fd0404SClaudiu Manoil {
1724d4fd0404SClaudiu Manoil 	struct enetc_hw *hw = &si->hw;
1725d4fd0404SClaudiu Manoil 	u32 val;
1726d4fd0404SClaudiu Manoil 
1727d4fd0404SClaudiu Manoil 	/* find out how many of various resources we have to work with */
1728d4fd0404SClaudiu Manoil 	val = enetc_rd(hw, ENETC_SICAPR0);
1729d4fd0404SClaudiu Manoil 	si->num_rx_rings = (val >> 16) & 0xff;
1730d4fd0404SClaudiu Manoil 	si->num_tx_rings = val & 0xff;
1731d382563fSClaudiu Manoil 
1732d382563fSClaudiu Manoil 	val = enetc_rd(hw, ENETC_SIRFSCAPR);
1733d382563fSClaudiu Manoil 	si->num_fs_entries = ENETC_SIRFSCAPR_GET_NUM_RFS(val);
1734d382563fSClaudiu Manoil 	si->num_fs_entries = min(si->num_fs_entries, ENETC_MAX_RFS_SIZE);
1735d382563fSClaudiu Manoil 
1736d382563fSClaudiu Manoil 	si->num_rss = 0;
1737d382563fSClaudiu Manoil 	val = enetc_rd(hw, ENETC_SIPCAPR0);
1738d382563fSClaudiu Manoil 	if (val & ENETC_SIPCAPR0_RSS) {
17392e47cb41SPo Liu 		u32 rss;
17402e47cb41SPo Liu 
17412e47cb41SPo Liu 		rss = enetc_rd(hw, ENETC_SIRSSCAPR);
17422e47cb41SPo Liu 		si->num_rss = ENETC_SIRSSCAPR_GET_NUM_RSS(rss);
1743d382563fSClaudiu Manoil 	}
17442e47cb41SPo Liu 
17452e47cb41SPo Liu 	if (val & ENETC_SIPCAPR0_QBV)
17462e47cb41SPo Liu 		si->hw_features |= ENETC_SI_F_QBV;
174779e49982SPo Liu 
174894557a9aSVladimir Oltean 	if (val & ENETC_SIPCAPR0_QBU)
174994557a9aSVladimir Oltean 		si->hw_features |= ENETC_SI_F_QBU;
175094557a9aSVladimir Oltean 
175179e49982SPo Liu 	if (val & ENETC_SIPCAPR0_PSFP)
175279e49982SPo Liu 		si->hw_features |= ENETC_SI_F_PSFP;
1753d4fd0404SClaudiu Manoil }
1754e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_get_si_caps);
1755d4fd0404SClaudiu Manoil 
1756f3ce29e1SVladimir Oltean static int enetc_dma_alloc_bdr(struct enetc_bdr_resource *res)
1757d4fd0404SClaudiu Manoil {
1758f3ce29e1SVladimir Oltean 	size_t bd_base_size = res->bd_count * res->bd_size;
1759f3ce29e1SVladimir Oltean 
1760f3ce29e1SVladimir Oltean 	res->bd_base = dma_alloc_coherent(res->dev, bd_base_size,
1761f3ce29e1SVladimir Oltean 					  &res->bd_dma_base, GFP_KERNEL);
1762f3ce29e1SVladimir Oltean 	if (!res->bd_base)
1763d4fd0404SClaudiu Manoil 		return -ENOMEM;
1764d4fd0404SClaudiu Manoil 
1765d4fd0404SClaudiu Manoil 	/* h/w requires 128B alignment */
1766f3ce29e1SVladimir Oltean 	if (!IS_ALIGNED(res->bd_dma_base, 128)) {
1767f3ce29e1SVladimir Oltean 		dma_free_coherent(res->dev, bd_base_size, res->bd_base,
1768f3ce29e1SVladimir Oltean 				  res->bd_dma_base);
1769d4fd0404SClaudiu Manoil 		return -EINVAL;
1770d4fd0404SClaudiu Manoil 	}
1771d4fd0404SClaudiu Manoil 
1772d4fd0404SClaudiu Manoil 	return 0;
1773d4fd0404SClaudiu Manoil }
1774d4fd0404SClaudiu Manoil 
1775f3ce29e1SVladimir Oltean static void enetc_dma_free_bdr(const struct enetc_bdr_resource *res)
17760d6cfd0fSVladimir Oltean {
1777f3ce29e1SVladimir Oltean 	size_t bd_base_size = res->bd_count * res->bd_size;
1778f3ce29e1SVladimir Oltean 
1779f3ce29e1SVladimir Oltean 	dma_free_coherent(res->dev, bd_base_size, res->bd_base,
1780f3ce29e1SVladimir Oltean 			  res->bd_dma_base);
17810d6cfd0fSVladimir Oltean }
17820d6cfd0fSVladimir Oltean 
1783f3ce29e1SVladimir Oltean static int enetc_alloc_tx_resource(struct enetc_bdr_resource *res,
1784f3ce29e1SVladimir Oltean 				   struct device *dev, size_t bd_count)
1785d4fd0404SClaudiu Manoil {
1786d4fd0404SClaudiu Manoil 	int err;
1787d4fd0404SClaudiu Manoil 
1788f3ce29e1SVladimir Oltean 	res->dev = dev;
1789f3ce29e1SVladimir Oltean 	res->bd_count = bd_count;
1790f3ce29e1SVladimir Oltean 	res->bd_size = sizeof(union enetc_tx_bd);
1791f3ce29e1SVladimir Oltean 
1792fa87c546SJulia Lawall 	res->tx_swbd = vcalloc(bd_count, sizeof(*res->tx_swbd));
1793f3ce29e1SVladimir Oltean 	if (!res->tx_swbd)
1794d4fd0404SClaudiu Manoil 		return -ENOMEM;
1795d4fd0404SClaudiu Manoil 
1796f3ce29e1SVladimir Oltean 	err = enetc_dma_alloc_bdr(res);
1797fb8629e2SIoana Ciornei 	if (err)
1798fb8629e2SIoana Ciornei 		goto err_alloc_bdr;
1799fb8629e2SIoana Ciornei 
1800f3ce29e1SVladimir Oltean 	res->tso_headers = dma_alloc_coherent(dev, bd_count * TSO_HEADER_SIZE,
1801f3ce29e1SVladimir Oltean 					      &res->tso_headers_dma,
1802fb8629e2SIoana Ciornei 					      GFP_KERNEL);
1803f3ce29e1SVladimir Oltean 	if (!res->tso_headers) {
1804e79d8264SDan Carpenter 		err = -ENOMEM;
1805fb8629e2SIoana Ciornei 		goto err_alloc_tso;
1806e79d8264SDan Carpenter 	}
1807d4fd0404SClaudiu Manoil 
1808d4fd0404SClaudiu Manoil 	return 0;
1809fb8629e2SIoana Ciornei 
1810fb8629e2SIoana Ciornei err_alloc_tso:
1811f3ce29e1SVladimir Oltean 	enetc_dma_free_bdr(res);
1812fb8629e2SIoana Ciornei err_alloc_bdr:
1813f3ce29e1SVladimir Oltean 	vfree(res->tx_swbd);
1814f3ce29e1SVladimir Oltean 	res->tx_swbd = NULL;
1815fb8629e2SIoana Ciornei 
1816fb8629e2SIoana Ciornei 	return err;
1817d4fd0404SClaudiu Manoil }
1818d4fd0404SClaudiu Manoil 
1819f3ce29e1SVladimir Oltean static void enetc_free_tx_resource(const struct enetc_bdr_resource *res)
1820d4fd0404SClaudiu Manoil {
1821f3ce29e1SVladimir Oltean 	dma_free_coherent(res->dev, res->bd_count * TSO_HEADER_SIZE,
1822f3ce29e1SVladimir Oltean 			  res->tso_headers, res->tso_headers_dma);
1823f3ce29e1SVladimir Oltean 	enetc_dma_free_bdr(res);
1824f3ce29e1SVladimir Oltean 	vfree(res->tx_swbd);
1825d4fd0404SClaudiu Manoil }
1826d4fd0404SClaudiu Manoil 
1827f3ce29e1SVladimir Oltean static struct enetc_bdr_resource *
1828f3ce29e1SVladimir Oltean enetc_alloc_tx_resources(struct enetc_ndev_priv *priv)
1829d4fd0404SClaudiu Manoil {
1830f3ce29e1SVladimir Oltean 	struct enetc_bdr_resource *tx_res;
1831d4fd0404SClaudiu Manoil 	int i, err;
1832d4fd0404SClaudiu Manoil 
1833f3ce29e1SVladimir Oltean 	tx_res = kcalloc(priv->num_tx_rings, sizeof(*tx_res), GFP_KERNEL);
1834f3ce29e1SVladimir Oltean 	if (!tx_res)
1835f3ce29e1SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1836f3ce29e1SVladimir Oltean 
1837d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++) {
1838f3ce29e1SVladimir Oltean 		struct enetc_bdr *tx_ring = priv->tx_ring[i];
1839d4fd0404SClaudiu Manoil 
1840f3ce29e1SVladimir Oltean 		err = enetc_alloc_tx_resource(&tx_res[i], tx_ring->dev,
1841f3ce29e1SVladimir Oltean 					      tx_ring->bd_count);
1842d4fd0404SClaudiu Manoil 		if (err)
1843d4fd0404SClaudiu Manoil 			goto fail;
1844d4fd0404SClaudiu Manoil 	}
1845d4fd0404SClaudiu Manoil 
1846f3ce29e1SVladimir Oltean 	return tx_res;
1847d4fd0404SClaudiu Manoil 
1848d4fd0404SClaudiu Manoil fail:
1849d4fd0404SClaudiu Manoil 	while (i-- > 0)
1850f3ce29e1SVladimir Oltean 		enetc_free_tx_resource(&tx_res[i]);
1851d4fd0404SClaudiu Manoil 
1852f3ce29e1SVladimir Oltean 	kfree(tx_res);
1853f3ce29e1SVladimir Oltean 
1854f3ce29e1SVladimir Oltean 	return ERR_PTR(err);
1855d4fd0404SClaudiu Manoil }
1856d4fd0404SClaudiu Manoil 
1857f3ce29e1SVladimir Oltean static void enetc_free_tx_resources(const struct enetc_bdr_resource *tx_res,
1858f3ce29e1SVladimir Oltean 				    size_t num_resources)
1859d4fd0404SClaudiu Manoil {
1860f3ce29e1SVladimir Oltean 	size_t i;
1861d4fd0404SClaudiu Manoil 
1862f3ce29e1SVladimir Oltean 	for (i = 0; i < num_resources; i++)
1863f3ce29e1SVladimir Oltean 		enetc_free_tx_resource(&tx_res[i]);
1864f3ce29e1SVladimir Oltean 
1865f3ce29e1SVladimir Oltean 	kfree(tx_res);
1866d4fd0404SClaudiu Manoil }
1867d4fd0404SClaudiu Manoil 
1868f3ce29e1SVladimir Oltean static int enetc_alloc_rx_resource(struct enetc_bdr_resource *res,
1869f3ce29e1SVladimir Oltean 				   struct device *dev, size_t bd_count,
1870f3ce29e1SVladimir Oltean 				   bool extended)
1871d4fd0404SClaudiu Manoil {
1872d4fd0404SClaudiu Manoil 	int err;
1873d4fd0404SClaudiu Manoil 
1874f3ce29e1SVladimir Oltean 	res->dev = dev;
1875f3ce29e1SVladimir Oltean 	res->bd_count = bd_count;
1876f3ce29e1SVladimir Oltean 	res->bd_size = sizeof(union enetc_rx_bd);
1877f3ce29e1SVladimir Oltean 	if (extended)
1878f3ce29e1SVladimir Oltean 		res->bd_size *= 2;
1879f3ce29e1SVladimir Oltean 
1880fa87c546SJulia Lawall 	res->rx_swbd = vcalloc(bd_count, sizeof(struct enetc_rx_swbd));
1881f3ce29e1SVladimir Oltean 	if (!res->rx_swbd)
1882d4fd0404SClaudiu Manoil 		return -ENOMEM;
1883d4fd0404SClaudiu Manoil 
1884f3ce29e1SVladimir Oltean 	err = enetc_dma_alloc_bdr(res);
1885d4fd0404SClaudiu Manoil 	if (err) {
1886f3ce29e1SVladimir Oltean 		vfree(res->rx_swbd);
1887d4fd0404SClaudiu Manoil 		return err;
1888d4fd0404SClaudiu Manoil 	}
1889d4fd0404SClaudiu Manoil 
1890d4fd0404SClaudiu Manoil 	return 0;
1891d4fd0404SClaudiu Manoil }
1892d4fd0404SClaudiu Manoil 
1893f3ce29e1SVladimir Oltean static void enetc_free_rx_resource(const struct enetc_bdr_resource *res)
1894d4fd0404SClaudiu Manoil {
1895f3ce29e1SVladimir Oltean 	enetc_dma_free_bdr(res);
1896f3ce29e1SVladimir Oltean 	vfree(res->rx_swbd);
1897d4fd0404SClaudiu Manoil }
1898d4fd0404SClaudiu Manoil 
1899f3ce29e1SVladimir Oltean static struct enetc_bdr_resource *
1900f3ce29e1SVladimir Oltean enetc_alloc_rx_resources(struct enetc_ndev_priv *priv, bool extended)
1901d4fd0404SClaudiu Manoil {
1902f3ce29e1SVladimir Oltean 	struct enetc_bdr_resource *rx_res;
1903d4fd0404SClaudiu Manoil 	int i, err;
1904d4fd0404SClaudiu Manoil 
1905f3ce29e1SVladimir Oltean 	rx_res = kcalloc(priv->num_rx_rings, sizeof(*rx_res), GFP_KERNEL);
1906f3ce29e1SVladimir Oltean 	if (!rx_res)
1907f3ce29e1SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1908d4fd0404SClaudiu Manoil 
1909f3ce29e1SVladimir Oltean 	for (i = 0; i < priv->num_rx_rings; i++) {
1910f3ce29e1SVladimir Oltean 		struct enetc_bdr *rx_ring = priv->rx_ring[i];
1911f3ce29e1SVladimir Oltean 
1912f3ce29e1SVladimir Oltean 		err = enetc_alloc_rx_resource(&rx_res[i], rx_ring->dev,
1913f3ce29e1SVladimir Oltean 					      rx_ring->bd_count, extended);
1914d4fd0404SClaudiu Manoil 		if (err)
1915d4fd0404SClaudiu Manoil 			goto fail;
1916d4fd0404SClaudiu Manoil 	}
1917d4fd0404SClaudiu Manoil 
1918f3ce29e1SVladimir Oltean 	return rx_res;
1919d4fd0404SClaudiu Manoil 
1920d4fd0404SClaudiu Manoil fail:
1921d4fd0404SClaudiu Manoil 	while (i-- > 0)
1922f3ce29e1SVladimir Oltean 		enetc_free_rx_resource(&rx_res[i]);
1923d4fd0404SClaudiu Manoil 
1924f3ce29e1SVladimir Oltean 	kfree(rx_res);
1925f3ce29e1SVladimir Oltean 
1926f3ce29e1SVladimir Oltean 	return ERR_PTR(err);
1927d4fd0404SClaudiu Manoil }
1928d4fd0404SClaudiu Manoil 
1929f3ce29e1SVladimir Oltean static void enetc_free_rx_resources(const struct enetc_bdr_resource *rx_res,
1930f3ce29e1SVladimir Oltean 				    size_t num_resources)
1931f3ce29e1SVladimir Oltean {
1932f3ce29e1SVladimir Oltean 	size_t i;
1933f3ce29e1SVladimir Oltean 
1934f3ce29e1SVladimir Oltean 	for (i = 0; i < num_resources; i++)
1935f3ce29e1SVladimir Oltean 		enetc_free_rx_resource(&rx_res[i]);
1936f3ce29e1SVladimir Oltean 
1937f3ce29e1SVladimir Oltean 	kfree(rx_res);
1938f3ce29e1SVladimir Oltean }
1939f3ce29e1SVladimir Oltean 
1940f3ce29e1SVladimir Oltean static void enetc_assign_tx_resource(struct enetc_bdr *tx_ring,
1941f3ce29e1SVladimir Oltean 				     const struct enetc_bdr_resource *res)
1942f3ce29e1SVladimir Oltean {
1943f3ce29e1SVladimir Oltean 	tx_ring->bd_base = res ? res->bd_base : NULL;
1944f3ce29e1SVladimir Oltean 	tx_ring->bd_dma_base = res ? res->bd_dma_base : 0;
1945f3ce29e1SVladimir Oltean 	tx_ring->tx_swbd = res ? res->tx_swbd : NULL;
1946f3ce29e1SVladimir Oltean 	tx_ring->tso_headers = res ? res->tso_headers : NULL;
1947f3ce29e1SVladimir Oltean 	tx_ring->tso_headers_dma = res ? res->tso_headers_dma : 0;
1948f3ce29e1SVladimir Oltean }
1949f3ce29e1SVladimir Oltean 
1950f3ce29e1SVladimir Oltean static void enetc_assign_rx_resource(struct enetc_bdr *rx_ring,
1951f3ce29e1SVladimir Oltean 				     const struct enetc_bdr_resource *res)
1952f3ce29e1SVladimir Oltean {
1953f3ce29e1SVladimir Oltean 	rx_ring->bd_base = res ? res->bd_base : NULL;
1954f3ce29e1SVladimir Oltean 	rx_ring->bd_dma_base = res ? res->bd_dma_base : 0;
1955f3ce29e1SVladimir Oltean 	rx_ring->rx_swbd = res ? res->rx_swbd : NULL;
1956f3ce29e1SVladimir Oltean }
1957f3ce29e1SVladimir Oltean 
1958f3ce29e1SVladimir Oltean static void enetc_assign_tx_resources(struct enetc_ndev_priv *priv,
1959f3ce29e1SVladimir Oltean 				      const struct enetc_bdr_resource *res)
1960d4fd0404SClaudiu Manoil {
1961d4fd0404SClaudiu Manoil 	int i;
1962d4fd0404SClaudiu Manoil 
1963f3ce29e1SVladimir Oltean 	if (priv->tx_res)
1964f3ce29e1SVladimir Oltean 		enetc_free_tx_resources(priv->tx_res, priv->num_tx_rings);
1965f3ce29e1SVladimir Oltean 
1966f3ce29e1SVladimir Oltean 	for (i = 0; i < priv->num_tx_rings; i++) {
1967f3ce29e1SVladimir Oltean 		enetc_assign_tx_resource(priv->tx_ring[i],
1968f3ce29e1SVladimir Oltean 					 res ? &res[i] : NULL);
1969f3ce29e1SVladimir Oltean 	}
1970f3ce29e1SVladimir Oltean 
1971f3ce29e1SVladimir Oltean 	priv->tx_res = res;
1972f3ce29e1SVladimir Oltean }
1973f3ce29e1SVladimir Oltean 
1974f3ce29e1SVladimir Oltean static void enetc_assign_rx_resources(struct enetc_ndev_priv *priv,
1975f3ce29e1SVladimir Oltean 				      const struct enetc_bdr_resource *res)
1976f3ce29e1SVladimir Oltean {
1977f3ce29e1SVladimir Oltean 	int i;
1978f3ce29e1SVladimir Oltean 
1979f3ce29e1SVladimir Oltean 	if (priv->rx_res)
1980f3ce29e1SVladimir Oltean 		enetc_free_rx_resources(priv->rx_res, priv->num_rx_rings);
1981f3ce29e1SVladimir Oltean 
1982f3ce29e1SVladimir Oltean 	for (i = 0; i < priv->num_rx_rings; i++) {
1983f3ce29e1SVladimir Oltean 		enetc_assign_rx_resource(priv->rx_ring[i],
1984f3ce29e1SVladimir Oltean 					 res ? &res[i] : NULL);
1985f3ce29e1SVladimir Oltean 	}
1986f3ce29e1SVladimir Oltean 
1987f3ce29e1SVladimir Oltean 	priv->rx_res = res;
1988d4fd0404SClaudiu Manoil }
1989d4fd0404SClaudiu Manoil 
1990d4fd0404SClaudiu Manoil static void enetc_free_tx_ring(struct enetc_bdr *tx_ring)
1991d4fd0404SClaudiu Manoil {
1992d4fd0404SClaudiu Manoil 	int i;
1993d4fd0404SClaudiu Manoil 
1994d4fd0404SClaudiu Manoil 	for (i = 0; i < tx_ring->bd_count; i++) {
1995d4fd0404SClaudiu Manoil 		struct enetc_tx_swbd *tx_swbd = &tx_ring->tx_swbd[i];
1996d4fd0404SClaudiu Manoil 
19979d2b68ccSVladimir Oltean 		enetc_free_tx_frame(tx_ring, tx_swbd);
1998d4fd0404SClaudiu Manoil 	}
1999d4fd0404SClaudiu Manoil }
2000d4fd0404SClaudiu Manoil 
2001d4fd0404SClaudiu Manoil static void enetc_free_rx_ring(struct enetc_bdr *rx_ring)
2002d4fd0404SClaudiu Manoil {
2003d4fd0404SClaudiu Manoil 	int i;
2004d4fd0404SClaudiu Manoil 
2005d4fd0404SClaudiu Manoil 	for (i = 0; i < rx_ring->bd_count; i++) {
2006d4fd0404SClaudiu Manoil 		struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[i];
2007d4fd0404SClaudiu Manoil 
2008d4fd0404SClaudiu Manoil 		if (!rx_swbd->page)
2009d4fd0404SClaudiu Manoil 			continue;
2010d4fd0404SClaudiu Manoil 
20117ed2bc80SVladimir Oltean 		dma_unmap_page(rx_ring->dev, rx_swbd->dma, PAGE_SIZE,
20127ed2bc80SVladimir Oltean 			       rx_swbd->dir);
2013d4fd0404SClaudiu Manoil 		__free_page(rx_swbd->page);
2014d4fd0404SClaudiu Manoil 		rx_swbd->page = NULL;
2015d4fd0404SClaudiu Manoil 	}
2016d4fd0404SClaudiu Manoil }
2017d4fd0404SClaudiu Manoil 
2018d4fd0404SClaudiu Manoil static void enetc_free_rxtx_rings(struct enetc_ndev_priv *priv)
2019d4fd0404SClaudiu Manoil {
2020d4fd0404SClaudiu Manoil 	int i;
2021d4fd0404SClaudiu Manoil 
2022d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
2023d4fd0404SClaudiu Manoil 		enetc_free_rx_ring(priv->rx_ring[i]);
2024d4fd0404SClaudiu Manoil 
2025d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
2026d4fd0404SClaudiu Manoil 		enetc_free_tx_ring(priv->tx_ring[i]);
2027d4fd0404SClaudiu Manoil }
2028d4fd0404SClaudiu Manoil 
2029d382563fSClaudiu Manoil static int enetc_setup_default_rss_table(struct enetc_si *si, int num_groups)
2030d382563fSClaudiu Manoil {
2031d382563fSClaudiu Manoil 	int *rss_table;
2032d382563fSClaudiu Manoil 	int i;
2033d382563fSClaudiu Manoil 
2034d382563fSClaudiu Manoil 	rss_table = kmalloc_array(si->num_rss, sizeof(*rss_table), GFP_KERNEL);
2035d382563fSClaudiu Manoil 	if (!rss_table)
2036d382563fSClaudiu Manoil 		return -ENOMEM;
2037d382563fSClaudiu Manoil 
2038d382563fSClaudiu Manoil 	/* Set up RSS table defaults */
2039d382563fSClaudiu Manoil 	for (i = 0; i < si->num_rss; i++)
2040d382563fSClaudiu Manoil 		rss_table[i] = i % num_groups;
2041d382563fSClaudiu Manoil 
2042d382563fSClaudiu Manoil 	enetc_set_rss_table(si, rss_table, si->num_rss);
2043d382563fSClaudiu Manoil 
2044d382563fSClaudiu Manoil 	kfree(rss_table);
2045d382563fSClaudiu Manoil 
2046d382563fSClaudiu Manoil 	return 0;
2047d382563fSClaudiu Manoil }
2048d382563fSClaudiu Manoil 
2049c646d10dSVladimir Oltean int enetc_configure_si(struct enetc_ndev_priv *priv)
2050d4fd0404SClaudiu Manoil {
2051d4fd0404SClaudiu Manoil 	struct enetc_si *si = priv->si;
2052d4fd0404SClaudiu Manoil 	struct enetc_hw *hw = &si->hw;
2053d382563fSClaudiu Manoil 	int err;
2054d4fd0404SClaudiu Manoil 
2055d4fd0404SClaudiu Manoil 	/* set SI cache attributes */
2056d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICAR0,
2057d4fd0404SClaudiu Manoil 		 ENETC_SICAR_RD_COHERENT | ENETC_SICAR_WR_COHERENT);
2058d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICAR1, ENETC_SICAR_MSI);
2059d4fd0404SClaudiu Manoil 	/* enable SI */
2060d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SIMR, ENETC_SIMR_EN);
2061d4fd0404SClaudiu Manoil 
2062d382563fSClaudiu Manoil 	if (si->num_rss) {
2063d382563fSClaudiu Manoil 		err = enetc_setup_default_rss_table(si, priv->num_rx_rings);
2064d382563fSClaudiu Manoil 		if (err)
2065d382563fSClaudiu Manoil 			return err;
2066d382563fSClaudiu Manoil 	}
2067d382563fSClaudiu Manoil 
2068d4fd0404SClaudiu Manoil 	return 0;
2069d4fd0404SClaudiu Manoil }
2070e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_configure_si);
2071d4fd0404SClaudiu Manoil 
2072d4fd0404SClaudiu Manoil void enetc_init_si_rings_params(struct enetc_ndev_priv *priv)
2073d4fd0404SClaudiu Manoil {
2074d4fd0404SClaudiu Manoil 	struct enetc_si *si = priv->si;
2075d4fd0404SClaudiu Manoil 	int cpus = num_online_cpus();
2076d4fd0404SClaudiu Manoil 
207702293dd4SClaudiu Manoil 	priv->tx_bd_count = ENETC_TX_RING_DEFAULT_SIZE;
207802293dd4SClaudiu Manoil 	priv->rx_bd_count = ENETC_RX_RING_DEFAULT_SIZE;
2079d4fd0404SClaudiu Manoil 
2080d4fd0404SClaudiu Manoil 	/* Enable all available TX rings in order to configure as many
2081d4fd0404SClaudiu Manoil 	 * priorities as possible, when needed.
2082d4fd0404SClaudiu Manoil 	 * TODO: Make # of TX rings run-time configurable
2083d4fd0404SClaudiu Manoil 	 */
2084d4fd0404SClaudiu Manoil 	priv->num_rx_rings = min_t(int, cpus, si->num_rx_rings);
2085d4fd0404SClaudiu Manoil 	priv->num_tx_rings = si->num_tx_rings;
2086d4fd0404SClaudiu Manoil 	priv->bdr_int_num = cpus;
2087ae0e6a5dSClaudiu Manoil 	priv->ic_mode = ENETC_IC_RX_ADAPTIVE | ENETC_IC_TX_MANUAL;
2088ae0e6a5dSClaudiu Manoil 	priv->tx_ictt = ENETC_TXIC_TIMETHR;
2089d4fd0404SClaudiu Manoil }
2090e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_init_si_rings_params);
2091d4fd0404SClaudiu Manoil 
2092d4fd0404SClaudiu Manoil int enetc_alloc_si_resources(struct enetc_ndev_priv *priv)
2093d4fd0404SClaudiu Manoil {
2094d4fd0404SClaudiu Manoil 	struct enetc_si *si = priv->si;
2095d4fd0404SClaudiu Manoil 
2096d382563fSClaudiu Manoil 	priv->cls_rules = kcalloc(si->num_fs_entries, sizeof(*priv->cls_rules),
2097d382563fSClaudiu Manoil 				  GFP_KERNEL);
20984b47c0b8SVladimir Oltean 	if (!priv->cls_rules)
20994b47c0b8SVladimir Oltean 		return -ENOMEM;
2100d382563fSClaudiu Manoil 
2101d4fd0404SClaudiu Manoil 	return 0;
2102d4fd0404SClaudiu Manoil }
2103e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_alloc_si_resources);
2104d4fd0404SClaudiu Manoil 
2105d4fd0404SClaudiu Manoil void enetc_free_si_resources(struct enetc_ndev_priv *priv)
2106d4fd0404SClaudiu Manoil {
2107d382563fSClaudiu Manoil 	kfree(priv->cls_rules);
2108d4fd0404SClaudiu Manoil }
2109e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_free_si_resources);
2110d4fd0404SClaudiu Manoil 
2111d4fd0404SClaudiu Manoil static void enetc_setup_txbdr(struct enetc_hw *hw, struct enetc_bdr *tx_ring)
2112d4fd0404SClaudiu Manoil {
2113d4fd0404SClaudiu Manoil 	int idx = tx_ring->index;
2114d4fd0404SClaudiu Manoil 	u32 tbmr;
2115d4fd0404SClaudiu Manoil 
2116d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBBAR0,
2117d4fd0404SClaudiu Manoil 		       lower_32_bits(tx_ring->bd_dma_base));
2118d4fd0404SClaudiu Manoil 
2119d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBBAR1,
2120d4fd0404SClaudiu Manoil 		       upper_32_bits(tx_ring->bd_dma_base));
2121d4fd0404SClaudiu Manoil 
2122d4fd0404SClaudiu Manoil 	WARN_ON(!IS_ALIGNED(tx_ring->bd_count, 64)); /* multiple of 64 */
2123d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBLENR,
2124d4fd0404SClaudiu Manoil 		       ENETC_RTBLENR_LEN(tx_ring->bd_count));
2125d4fd0404SClaudiu Manoil 
2126d4fd0404SClaudiu Manoil 	/* clearing PI/CI registers for Tx not supported, adjust sw indexes */
2127d4fd0404SClaudiu Manoil 	tx_ring->next_to_use = enetc_txbdr_rd(hw, idx, ENETC_TBPIR);
2128d4fd0404SClaudiu Manoil 	tx_ring->next_to_clean = enetc_txbdr_rd(hw, idx, ENETC_TBCIR);
2129d4fd0404SClaudiu Manoil 
2130d4fd0404SClaudiu Manoil 	/* enable Tx ints by setting pkt thr to 1 */
213112460a0aSClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBICR0, ENETC_TBICR0_ICEN | 0x1);
2132d4fd0404SClaudiu Manoil 
2133ff58fda0SVladimir Oltean 	tbmr = ENETC_TBMR_SET_PRIO(tx_ring->prio);
2134d4fd0404SClaudiu Manoil 	if (tx_ring->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
2135d4fd0404SClaudiu Manoil 		tbmr |= ENETC_TBMR_VIH;
2136d4fd0404SClaudiu Manoil 
2137d4fd0404SClaudiu Manoil 	/* enable ring */
2138d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBMR, tbmr);
2139d4fd0404SClaudiu Manoil 
2140d4fd0404SClaudiu Manoil 	tx_ring->tpir = hw->reg + ENETC_BDR(TX, idx, ENETC_TBPIR);
2141d4fd0404SClaudiu Manoil 	tx_ring->tcir = hw->reg + ENETC_BDR(TX, idx, ENETC_TBCIR);
2142d4fd0404SClaudiu Manoil 	tx_ring->idr = hw->reg + ENETC_SITXIDR;
2143d4fd0404SClaudiu Manoil }
2144d4fd0404SClaudiu Manoil 
2145d075db51SVladimir Oltean static void enetc_setup_rxbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring,
2146d075db51SVladimir Oltean 			      bool extended)
2147d4fd0404SClaudiu Manoil {
2148d4fd0404SClaudiu Manoil 	int idx = rx_ring->index;
2149ff58fda0SVladimir Oltean 	u32 rbmr = 0;
2150d4fd0404SClaudiu Manoil 
2151d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBBAR0,
2152d4fd0404SClaudiu Manoil 		       lower_32_bits(rx_ring->bd_dma_base));
2153d4fd0404SClaudiu Manoil 
2154d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBBAR1,
2155d4fd0404SClaudiu Manoil 		       upper_32_bits(rx_ring->bd_dma_base));
2156d4fd0404SClaudiu Manoil 
2157d4fd0404SClaudiu Manoil 	WARN_ON(!IS_ALIGNED(rx_ring->bd_count, 64)); /* multiple of 64 */
2158d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBLENR,
2159d4fd0404SClaudiu Manoil 		       ENETC_RTBLENR_LEN(rx_ring->bd_count));
2160d4fd0404SClaudiu Manoil 
2161d1b15102SVladimir Oltean 	if (rx_ring->xdp.prog)
2162d1b15102SVladimir Oltean 		enetc_rxbdr_wr(hw, idx, ENETC_RBBSR, ENETC_RXB_DMA_SIZE_XDP);
2163d1b15102SVladimir Oltean 	else
2164d4fd0404SClaudiu Manoil 		enetc_rxbdr_wr(hw, idx, ENETC_RBBSR, ENETC_RXB_DMA_SIZE);
2165d4fd0404SClaudiu Manoil 
216684ce1ca3SVladimir Oltean 	/* Also prepare the consumer index in case page allocation never
216784ce1ca3SVladimir Oltean 	 * succeeds. In that case, hardware will never advance producer index
216884ce1ca3SVladimir Oltean 	 * to match consumer index, and will drop all frames.
216984ce1ca3SVladimir Oltean 	 */
2170d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBPIR, 0);
217184ce1ca3SVladimir Oltean 	enetc_rxbdr_wr(hw, idx, ENETC_RBCIR, 1);
2172d4fd0404SClaudiu Manoil 
2173d4fd0404SClaudiu Manoil 	/* enable Rx ints by setting pkt thr to 1 */
217412460a0aSClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBICR0, ENETC_RBICR0_ICEN | 0x1);
2175d4fd0404SClaudiu Manoil 
2176d075db51SVladimir Oltean 	rx_ring->ext_en = extended;
2177434cebabSClaudiu Manoil 	if (rx_ring->ext_en)
2178d3982312SY.b. Lu 		rbmr |= ENETC_RBMR_BDS;
2179434cebabSClaudiu Manoil 
2180d4fd0404SClaudiu Manoil 	if (rx_ring->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
2181d4fd0404SClaudiu Manoil 		rbmr |= ENETC_RBMR_VTE;
2182d4fd0404SClaudiu Manoil 
2183d4fd0404SClaudiu Manoil 	rx_ring->rcir = hw->reg + ENETC_BDR(RX, idx, ENETC_RBCIR);
2184d4fd0404SClaudiu Manoil 	rx_ring->idr = hw->reg + ENETC_SIRXIDR;
2185d4fd0404SClaudiu Manoil 
2186fbf1cff9SVladimir Oltean 	rx_ring->next_to_clean = 0;
2187fbf1cff9SVladimir Oltean 	rx_ring->next_to_use = 0;
2188fbf1cff9SVladimir Oltean 	rx_ring->next_to_alloc = 0;
2189fbf1cff9SVladimir Oltean 
21907a5222cbSVladimir Oltean 	enetc_lock_mdio();
2191d4fd0404SClaudiu Manoil 	enetc_refill_rx_ring(rx_ring, enetc_bd_unused(rx_ring));
21927a5222cbSVladimir Oltean 	enetc_unlock_mdio();
2193d4fd0404SClaudiu Manoil 
2194d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBMR, rbmr);
2195d4fd0404SClaudiu Manoil }
2196d4fd0404SClaudiu Manoil 
2197d075db51SVladimir Oltean static void enetc_setup_bdrs(struct enetc_ndev_priv *priv, bool extended)
2198d4fd0404SClaudiu Manoil {
2199715bf261SVladimir Oltean 	struct enetc_hw *hw = &priv->si->hw;
2200d4fd0404SClaudiu Manoil 	int i;
2201d4fd0404SClaudiu Manoil 
2202d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
2203715bf261SVladimir Oltean 		enetc_setup_txbdr(hw, priv->tx_ring[i]);
2204d4fd0404SClaudiu Manoil 
2205d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
2206d075db51SVladimir Oltean 		enetc_setup_rxbdr(hw, priv->rx_ring[i], extended);
2207d4fd0404SClaudiu Manoil }
2208d4fd0404SClaudiu Manoil 
2209ff58fda0SVladimir Oltean static void enetc_enable_txbdr(struct enetc_hw *hw, struct enetc_bdr *tx_ring)
2210ff58fda0SVladimir Oltean {
2211ff58fda0SVladimir Oltean 	int idx = tx_ring->index;
2212ff58fda0SVladimir Oltean 	u32 tbmr;
2213ff58fda0SVladimir Oltean 
2214ff58fda0SVladimir Oltean 	tbmr = enetc_txbdr_rd(hw, idx, ENETC_TBMR);
2215ff58fda0SVladimir Oltean 	tbmr |= ENETC_TBMR_EN;
2216ff58fda0SVladimir Oltean 	enetc_txbdr_wr(hw, idx, ENETC_TBMR, tbmr);
2217ff58fda0SVladimir Oltean }
2218ff58fda0SVladimir Oltean 
2219ff58fda0SVladimir Oltean static void enetc_enable_rxbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring)
2220ff58fda0SVladimir Oltean {
2221ff58fda0SVladimir Oltean 	int idx = rx_ring->index;
2222ff58fda0SVladimir Oltean 	u32 rbmr;
2223ff58fda0SVladimir Oltean 
2224ff58fda0SVladimir Oltean 	rbmr = enetc_rxbdr_rd(hw, idx, ENETC_RBMR);
2225ff58fda0SVladimir Oltean 	rbmr |= ENETC_RBMR_EN;
2226ff58fda0SVladimir Oltean 	enetc_rxbdr_wr(hw, idx, ENETC_RBMR, rbmr);
2227ff58fda0SVladimir Oltean }
2228ff58fda0SVladimir Oltean 
2229ff58fda0SVladimir Oltean static void enetc_enable_bdrs(struct enetc_ndev_priv *priv)
2230ff58fda0SVladimir Oltean {
2231ff58fda0SVladimir Oltean 	struct enetc_hw *hw = &priv->si->hw;
2232ff58fda0SVladimir Oltean 	int i;
2233ff58fda0SVladimir Oltean 
2234ff58fda0SVladimir Oltean 	for (i = 0; i < priv->num_tx_rings; i++)
2235ff58fda0SVladimir Oltean 		enetc_enable_txbdr(hw, priv->tx_ring[i]);
2236ff58fda0SVladimir Oltean 
2237ff58fda0SVladimir Oltean 	for (i = 0; i < priv->num_rx_rings; i++)
2238ff58fda0SVladimir Oltean 		enetc_enable_rxbdr(hw, priv->rx_ring[i]);
2239ff58fda0SVladimir Oltean }
2240ff58fda0SVladimir Oltean 
2241ff58fda0SVladimir Oltean static void enetc_disable_rxbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring)
2242d4fd0404SClaudiu Manoil {
2243d4fd0404SClaudiu Manoil 	int idx = rx_ring->index;
2244d4fd0404SClaudiu Manoil 
2245d4fd0404SClaudiu Manoil 	/* disable EN bit on ring */
2246d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBMR, 0);
2247d4fd0404SClaudiu Manoil }
2248d4fd0404SClaudiu Manoil 
2249ff58fda0SVladimir Oltean static void enetc_disable_txbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring)
2250d4fd0404SClaudiu Manoil {
2251ff58fda0SVladimir Oltean 	int idx = rx_ring->index;
2252d4fd0404SClaudiu Manoil 
2253d4fd0404SClaudiu Manoil 	/* disable EN bit on ring */
2254d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBMR, 0);
2255ff58fda0SVladimir Oltean }
2256ff58fda0SVladimir Oltean 
2257ff58fda0SVladimir Oltean static void enetc_disable_bdrs(struct enetc_ndev_priv *priv)
2258ff58fda0SVladimir Oltean {
2259ff58fda0SVladimir Oltean 	struct enetc_hw *hw = &priv->si->hw;
2260ff58fda0SVladimir Oltean 	int i;
2261ff58fda0SVladimir Oltean 
2262ff58fda0SVladimir Oltean 	for (i = 0; i < priv->num_tx_rings; i++)
2263ff58fda0SVladimir Oltean 		enetc_disable_txbdr(hw, priv->tx_ring[i]);
2264ff58fda0SVladimir Oltean 
2265ff58fda0SVladimir Oltean 	for (i = 0; i < priv->num_rx_rings; i++)
2266ff58fda0SVladimir Oltean 		enetc_disable_rxbdr(hw, priv->rx_ring[i]);
2267ff58fda0SVladimir Oltean }
2268ff58fda0SVladimir Oltean 
2269ff58fda0SVladimir Oltean static void enetc_wait_txbdr(struct enetc_hw *hw, struct enetc_bdr *tx_ring)
2270ff58fda0SVladimir Oltean {
2271ff58fda0SVladimir Oltean 	int delay = 8, timeout = 100;
2272ff58fda0SVladimir Oltean 	int idx = tx_ring->index;
2273d4fd0404SClaudiu Manoil 
2274d4fd0404SClaudiu Manoil 	/* wait for busy to clear */
2275d4fd0404SClaudiu Manoil 	while (delay < timeout &&
2276d4fd0404SClaudiu Manoil 	       enetc_txbdr_rd(hw, idx, ENETC_TBSR) & ENETC_TBSR_BUSY) {
2277d4fd0404SClaudiu Manoil 		msleep(delay);
2278d4fd0404SClaudiu Manoil 		delay *= 2;
2279d4fd0404SClaudiu Manoil 	}
2280d4fd0404SClaudiu Manoil 
2281d4fd0404SClaudiu Manoil 	if (delay >= timeout)
2282d4fd0404SClaudiu Manoil 		netdev_warn(tx_ring->ndev, "timeout for tx ring #%d clear\n",
2283d4fd0404SClaudiu Manoil 			    idx);
2284d4fd0404SClaudiu Manoil }
2285d4fd0404SClaudiu Manoil 
2286ff58fda0SVladimir Oltean static void enetc_wait_bdrs(struct enetc_ndev_priv *priv)
2287d4fd0404SClaudiu Manoil {
2288715bf261SVladimir Oltean 	struct enetc_hw *hw = &priv->si->hw;
2289d4fd0404SClaudiu Manoil 	int i;
2290d4fd0404SClaudiu Manoil 
2291d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
2292ff58fda0SVladimir Oltean 		enetc_wait_txbdr(hw, priv->tx_ring[i]);
2293d4fd0404SClaudiu Manoil }
2294d4fd0404SClaudiu Manoil 
2295d4fd0404SClaudiu Manoil static int enetc_setup_irqs(struct enetc_ndev_priv *priv)
2296d4fd0404SClaudiu Manoil {
2297d4fd0404SClaudiu Manoil 	struct pci_dev *pdev = priv->si->pdev;
2298715bf261SVladimir Oltean 	struct enetc_hw *hw = &priv->si->hw;
2299d4fd0404SClaudiu Manoil 	int i, j, err;
2300d4fd0404SClaudiu Manoil 
2301d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
2302d4fd0404SClaudiu Manoil 		int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i);
2303d4fd0404SClaudiu Manoil 		struct enetc_int_vector *v = priv->int_vector[i];
2304d4fd0404SClaudiu Manoil 		int entry = ENETC_BDR_INT_BASE_IDX + i;
2305d4fd0404SClaudiu Manoil 
2306d4fd0404SClaudiu Manoil 		snprintf(v->name, sizeof(v->name), "%s-rxtx%d",
2307d4fd0404SClaudiu Manoil 			 priv->ndev->name, i);
23081e8fc4ffSJinjie Ruan 		err = request_irq(irq, enetc_msix, IRQF_NO_AUTOEN, v->name, v);
2309d4fd0404SClaudiu Manoil 		if (err) {
2310d4fd0404SClaudiu Manoil 			dev_err(priv->dev, "request_irq() failed!\n");
2311d4fd0404SClaudiu Manoil 			goto irq_err;
2312d4fd0404SClaudiu Manoil 		}
2313d4fd0404SClaudiu Manoil 
2314d4fd0404SClaudiu Manoil 		v->tbier_base = hw->reg + ENETC_BDR(TX, 0, ENETC_TBIER);
2315d4fd0404SClaudiu Manoil 		v->rbier = hw->reg + ENETC_BDR(RX, i, ENETC_RBIER);
231691571081SClaudiu Manoil 		v->ricr1 = hw->reg + ENETC_BDR(RX, i, ENETC_RBICR1);
2317d4fd0404SClaudiu Manoil 
2318d4fd0404SClaudiu Manoil 		enetc_wr(hw, ENETC_SIMSIRRV(i), entry);
2319d4fd0404SClaudiu Manoil 
2320d4fd0404SClaudiu Manoil 		for (j = 0; j < v->count_tx_rings; j++) {
2321d4fd0404SClaudiu Manoil 			int idx = v->tx_ring[j].index;
2322d4fd0404SClaudiu Manoil 
2323d4fd0404SClaudiu Manoil 			enetc_wr(hw, ENETC_SIMSITRV(idx), entry);
2324d4fd0404SClaudiu Manoil 		}
23257237a494SClaudiu Manoil 		irq_set_affinity_hint(irq, get_cpu_mask(i % num_online_cpus()));
2326d4fd0404SClaudiu Manoil 	}
2327d4fd0404SClaudiu Manoil 
2328d4fd0404SClaudiu Manoil 	return 0;
2329d4fd0404SClaudiu Manoil 
2330d4fd0404SClaudiu Manoil irq_err:
2331d4fd0404SClaudiu Manoil 	while (i--) {
2332d4fd0404SClaudiu Manoil 		int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i);
2333d4fd0404SClaudiu Manoil 
2334d4fd0404SClaudiu Manoil 		irq_set_affinity_hint(irq, NULL);
2335d4fd0404SClaudiu Manoil 		free_irq(irq, priv->int_vector[i]);
2336d4fd0404SClaudiu Manoil 	}
2337d4fd0404SClaudiu Manoil 
2338d4fd0404SClaudiu Manoil 	return err;
2339d4fd0404SClaudiu Manoil }
2340d4fd0404SClaudiu Manoil 
2341d4fd0404SClaudiu Manoil static void enetc_free_irqs(struct enetc_ndev_priv *priv)
2342d4fd0404SClaudiu Manoil {
2343d4fd0404SClaudiu Manoil 	struct pci_dev *pdev = priv->si->pdev;
2344d4fd0404SClaudiu Manoil 	int i;
2345d4fd0404SClaudiu Manoil 
2346d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
2347d4fd0404SClaudiu Manoil 		int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i);
2348d4fd0404SClaudiu Manoil 
2349d4fd0404SClaudiu Manoil 		irq_set_affinity_hint(irq, NULL);
2350d4fd0404SClaudiu Manoil 		free_irq(irq, priv->int_vector[i]);
2351d4fd0404SClaudiu Manoil 	}
2352d4fd0404SClaudiu Manoil }
2353d4fd0404SClaudiu Manoil 
2354bbb96dc7SClaudiu Manoil static void enetc_setup_interrupts(struct enetc_ndev_priv *priv)
2355d4fd0404SClaudiu Manoil {
235691571081SClaudiu Manoil 	struct enetc_hw *hw = &priv->si->hw;
235791571081SClaudiu Manoil 	u32 icpt, ictt;
2358d4fd0404SClaudiu Manoil 	int i;
2359d4fd0404SClaudiu Manoil 
2360d4fd0404SClaudiu Manoil 	/* enable Tx & Rx event indication */
2361ae0e6a5dSClaudiu Manoil 	if (priv->ic_mode &
2362ae0e6a5dSClaudiu Manoil 	    (ENETC_IC_RX_MANUAL | ENETC_IC_RX_ADAPTIVE)) {
236391571081SClaudiu Manoil 		icpt = ENETC_RBICR0_SET_ICPT(ENETC_RXIC_PKTTHR);
236491571081SClaudiu Manoil 		/* init to non-0 minimum, will be adjusted later */
236591571081SClaudiu Manoil 		ictt = 0x1;
236691571081SClaudiu Manoil 	} else {
236791571081SClaudiu Manoil 		icpt = 0x1; /* enable Rx ints by setting pkt thr to 1 */
236891571081SClaudiu Manoil 		ictt = 0;
2369d4fd0404SClaudiu Manoil 	}
2370d4fd0404SClaudiu Manoil 
237191571081SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++) {
237291571081SClaudiu Manoil 		enetc_rxbdr_wr(hw, i, ENETC_RBICR1, ictt);
237391571081SClaudiu Manoil 		enetc_rxbdr_wr(hw, i, ENETC_RBICR0, ENETC_RBICR0_ICEN | icpt);
237491571081SClaudiu Manoil 		enetc_rxbdr_wr(hw, i, ENETC_RBIER, ENETC_RBIER_RXTIE);
237591571081SClaudiu Manoil 	}
237691571081SClaudiu Manoil 
237791571081SClaudiu Manoil 	if (priv->ic_mode & ENETC_IC_TX_MANUAL)
237891571081SClaudiu Manoil 		icpt = ENETC_TBICR0_SET_ICPT(ENETC_TXIC_PKTTHR);
237991571081SClaudiu Manoil 	else
238091571081SClaudiu Manoil 		icpt = 0x1; /* enable Tx ints by setting pkt thr to 1 */
238191571081SClaudiu Manoil 
2382d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++) {
238391571081SClaudiu Manoil 		enetc_txbdr_wr(hw, i, ENETC_TBICR1, priv->tx_ictt);
238491571081SClaudiu Manoil 		enetc_txbdr_wr(hw, i, ENETC_TBICR0, ENETC_TBICR0_ICEN | icpt);
238591571081SClaudiu Manoil 		enetc_txbdr_wr(hw, i, ENETC_TBIER, ENETC_TBIER_TXTIE);
2386d4fd0404SClaudiu Manoil 	}
2387d4fd0404SClaudiu Manoil }
2388d4fd0404SClaudiu Manoil 
2389bbb96dc7SClaudiu Manoil static void enetc_clear_interrupts(struct enetc_ndev_priv *priv)
2390d4fd0404SClaudiu Manoil {
2391715bf261SVladimir Oltean 	struct enetc_hw *hw = &priv->si->hw;
2392d4fd0404SClaudiu Manoil 	int i;
2393d4fd0404SClaudiu Manoil 
2394d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
2395715bf261SVladimir Oltean 		enetc_txbdr_wr(hw, i, ENETC_TBIER, 0);
2396d4fd0404SClaudiu Manoil 
2397d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
2398715bf261SVladimir Oltean 		enetc_rxbdr_wr(hw, i, ENETC_RBIER, 0);
2399d4fd0404SClaudiu Manoil }
2400d4fd0404SClaudiu Manoil 
240171b77a7aSClaudiu Manoil static int enetc_phylink_connect(struct net_device *ndev)
2402d4fd0404SClaudiu Manoil {
24032e47cb41SPo Liu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2404a6a10d45SYangbo Lu 	struct ethtool_eee edata;
240571b77a7aSClaudiu Manoil 	int err;
2406d4fd0404SClaudiu Manoil 
2407598ca0d0SVladimir Oltean 	if (!priv->phylink) {
2408598ca0d0SVladimir Oltean 		/* phy-less mode */
2409598ca0d0SVladimir Oltean 		netif_carrier_on(ndev);
2410598ca0d0SVladimir Oltean 		return 0;
2411598ca0d0SVladimir Oltean 	}
2412d4fd0404SClaudiu Manoil 
241371b77a7aSClaudiu Manoil 	err = phylink_of_phy_connect(priv->phylink, priv->dev->of_node, 0);
241471b77a7aSClaudiu Manoil 	if (err) {
2415d4fd0404SClaudiu Manoil 		dev_err(&ndev->dev, "could not attach to PHY\n");
241671b77a7aSClaudiu Manoil 		return err;
2417d4fd0404SClaudiu Manoil 	}
2418d4fd0404SClaudiu Manoil 
2419a6a10d45SYangbo Lu 	/* disable EEE autoneg, until ENETC driver supports it */
2420a6a10d45SYangbo Lu 	memset(&edata, 0, sizeof(struct ethtool_eee));
242171b77a7aSClaudiu Manoil 	phylink_ethtool_set_eee(priv->phylink, &edata);
2422a6a10d45SYangbo Lu 
2423598ca0d0SVladimir Oltean 	phylink_start(priv->phylink);
2424598ca0d0SVladimir Oltean 
2425d4fd0404SClaudiu Manoil 	return 0;
2426d4fd0404SClaudiu Manoil }
2427d4fd0404SClaudiu Manoil 
24287294380cSYangbo Lu static void enetc_tx_onestep_tstamp(struct work_struct *work)
24297294380cSYangbo Lu {
24307294380cSYangbo Lu 	struct enetc_ndev_priv *priv;
24317294380cSYangbo Lu 	struct sk_buff *skb;
24327294380cSYangbo Lu 
24337294380cSYangbo Lu 	priv = container_of(work, struct enetc_ndev_priv, tx_onestep_tstamp);
24347294380cSYangbo Lu 
24353c463721SVladimir Oltean 	netif_tx_lock_bh(priv->ndev);
24367294380cSYangbo Lu 
24377294380cSYangbo Lu 	clear_bit_unlock(ENETC_TX_ONESTEP_TSTAMP_IN_PROGRESS, &priv->flags);
24387294380cSYangbo Lu 	skb = skb_dequeue(&priv->tx_skbs);
24397294380cSYangbo Lu 	if (skb)
24407294380cSYangbo Lu 		enetc_start_xmit(skb, priv->ndev);
24417294380cSYangbo Lu 
24423c463721SVladimir Oltean 	netif_tx_unlock_bh(priv->ndev);
24437294380cSYangbo Lu }
24447294380cSYangbo Lu 
24457294380cSYangbo Lu static void enetc_tx_onestep_tstamp_init(struct enetc_ndev_priv *priv)
24467294380cSYangbo Lu {
24477294380cSYangbo Lu 	INIT_WORK(&priv->tx_onestep_tstamp, enetc_tx_onestep_tstamp);
24487294380cSYangbo Lu 	skb_queue_head_init(&priv->tx_skbs);
24497294380cSYangbo Lu }
24507294380cSYangbo Lu 
245191571081SClaudiu Manoil void enetc_start(struct net_device *ndev)
2452bbb96dc7SClaudiu Manoil {
2453bbb96dc7SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2454bbb96dc7SClaudiu Manoil 	int i;
2455bbb96dc7SClaudiu Manoil 
2456bbb96dc7SClaudiu Manoil 	enetc_setup_interrupts(priv);
2457bbb96dc7SClaudiu Manoil 
2458bbb96dc7SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
2459bbb96dc7SClaudiu Manoil 		int irq = pci_irq_vector(priv->si->pdev,
2460bbb96dc7SClaudiu Manoil 					 ENETC_BDR_INT_BASE_IDX + i);
2461bbb96dc7SClaudiu Manoil 
2462bbb96dc7SClaudiu Manoil 		napi_enable(&priv->int_vector[i]->napi);
2463bbb96dc7SClaudiu Manoil 		enable_irq(irq);
2464bbb96dc7SClaudiu Manoil 	}
2465bbb96dc7SClaudiu Manoil 
2466ff58fda0SVladimir Oltean 	enetc_enable_bdrs(priv);
2467ff58fda0SVladimir Oltean 
2468bbb96dc7SClaudiu Manoil 	netif_tx_start_all_queues(ndev);
2469bbb96dc7SClaudiu Manoil }
2470e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_start);
2471bbb96dc7SClaudiu Manoil 
2472d4fd0404SClaudiu Manoil int enetc_open(struct net_device *ndev)
2473d4fd0404SClaudiu Manoil {
2474d4fd0404SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2475f3ce29e1SVladimir Oltean 	struct enetc_bdr_resource *tx_res, *rx_res;
2476d075db51SVladimir Oltean 	bool extended;
2477bbb96dc7SClaudiu Manoil 	int err;
2478d4fd0404SClaudiu Manoil 
2479d075db51SVladimir Oltean 	extended = !!(priv->active_offloads & ENETC_F_RX_TSTAMP);
2480d075db51SVladimir Oltean 
2481d4fd0404SClaudiu Manoil 	err = enetc_setup_irqs(priv);
2482d4fd0404SClaudiu Manoil 	if (err)
2483d4fd0404SClaudiu Manoil 		return err;
2484d4fd0404SClaudiu Manoil 
248571b77a7aSClaudiu Manoil 	err = enetc_phylink_connect(ndev);
2486d4fd0404SClaudiu Manoil 	if (err)
2487d4fd0404SClaudiu Manoil 		goto err_phy_connect;
2488d4fd0404SClaudiu Manoil 
2489f3ce29e1SVladimir Oltean 	tx_res = enetc_alloc_tx_resources(priv);
2490f3ce29e1SVladimir Oltean 	if (IS_ERR(tx_res)) {
2491f3ce29e1SVladimir Oltean 		err = PTR_ERR(tx_res);
2492d4fd0404SClaudiu Manoil 		goto err_alloc_tx;
2493f3ce29e1SVladimir Oltean 	}
2494d4fd0404SClaudiu Manoil 
2495f3ce29e1SVladimir Oltean 	rx_res = enetc_alloc_rx_resources(priv, extended);
2496f3ce29e1SVladimir Oltean 	if (IS_ERR(rx_res)) {
2497f3ce29e1SVladimir Oltean 		err = PTR_ERR(rx_res);
2498d4fd0404SClaudiu Manoil 		goto err_alloc_rx;
2499f3ce29e1SVladimir Oltean 	}
2500d4fd0404SClaudiu Manoil 
25017294380cSYangbo Lu 	enetc_tx_onestep_tstamp_init(priv);
2502f3ce29e1SVladimir Oltean 	enetc_assign_tx_resources(priv, tx_res);
2503f3ce29e1SVladimir Oltean 	enetc_assign_rx_resources(priv, rx_res);
2504d075db51SVladimir Oltean 	enetc_setup_bdrs(priv, extended);
2505bbb96dc7SClaudiu Manoil 	enetc_start(ndev);
2506d4fd0404SClaudiu Manoil 
2507d4fd0404SClaudiu Manoil 	return 0;
2508d4fd0404SClaudiu Manoil 
2509d4fd0404SClaudiu Manoil err_alloc_rx:
2510f3ce29e1SVladimir Oltean 	enetc_free_tx_resources(tx_res, priv->num_tx_rings);
2511d4fd0404SClaudiu Manoil err_alloc_tx:
251271b77a7aSClaudiu Manoil 	if (priv->phylink)
251371b77a7aSClaudiu Manoil 		phylink_disconnect_phy(priv->phylink);
2514d4fd0404SClaudiu Manoil err_phy_connect:
2515d4fd0404SClaudiu Manoil 	enetc_free_irqs(priv);
2516d4fd0404SClaudiu Manoil 
2517d4fd0404SClaudiu Manoil 	return err;
2518d4fd0404SClaudiu Manoil }
2519e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_open);
2520d4fd0404SClaudiu Manoil 
252191571081SClaudiu Manoil void enetc_stop(struct net_device *ndev)
2522d4fd0404SClaudiu Manoil {
2523d4fd0404SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2524d4fd0404SClaudiu Manoil 	int i;
2525d4fd0404SClaudiu Manoil 
2526d4fd0404SClaudiu Manoil 	netif_tx_stop_all_queues(ndev);
2527d4fd0404SClaudiu Manoil 
2528ff58fda0SVladimir Oltean 	enetc_disable_bdrs(priv);
2529ff58fda0SVladimir Oltean 
2530d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
2531bbb96dc7SClaudiu Manoil 		int irq = pci_irq_vector(priv->si->pdev,
2532bbb96dc7SClaudiu Manoil 					 ENETC_BDR_INT_BASE_IDX + i);
2533bbb96dc7SClaudiu Manoil 
2534bbb96dc7SClaudiu Manoil 		disable_irq(irq);
2535d4fd0404SClaudiu Manoil 		napi_synchronize(&priv->int_vector[i]->napi);
2536d4fd0404SClaudiu Manoil 		napi_disable(&priv->int_vector[i]->napi);
2537d4fd0404SClaudiu Manoil 	}
2538d4fd0404SClaudiu Manoil 
2539ff58fda0SVladimir Oltean 	enetc_wait_bdrs(priv);
2540ff58fda0SVladimir Oltean 
2541bbb96dc7SClaudiu Manoil 	enetc_clear_interrupts(priv);
2542bbb96dc7SClaudiu Manoil }
2543e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_stop);
2544bbb96dc7SClaudiu Manoil 
2545bbb96dc7SClaudiu Manoil int enetc_close(struct net_device *ndev)
2546bbb96dc7SClaudiu Manoil {
2547bbb96dc7SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2548bbb96dc7SClaudiu Manoil 
2549bbb96dc7SClaudiu Manoil 	enetc_stop(ndev);
2550d4fd0404SClaudiu Manoil 
2551598ca0d0SVladimir Oltean 	if (priv->phylink) {
2552598ca0d0SVladimir Oltean 		phylink_stop(priv->phylink);
255371b77a7aSClaudiu Manoil 		phylink_disconnect_phy(priv->phylink);
2554598ca0d0SVladimir Oltean 	} else {
2555598ca0d0SVladimir Oltean 		netif_carrier_off(ndev);
2556598ca0d0SVladimir Oltean 	}
2557598ca0d0SVladimir Oltean 
2558d4fd0404SClaudiu Manoil 	enetc_free_rxtx_rings(priv);
2559f3ce29e1SVladimir Oltean 
2560f3ce29e1SVladimir Oltean 	/* Avoids dangling pointers and also frees old resources */
2561f3ce29e1SVladimir Oltean 	enetc_assign_rx_resources(priv, NULL);
2562f3ce29e1SVladimir Oltean 	enetc_assign_tx_resources(priv, NULL);
2563f3ce29e1SVladimir Oltean 
2564d4fd0404SClaudiu Manoil 	enetc_free_irqs(priv);
2565d4fd0404SClaudiu Manoil 
2566d4fd0404SClaudiu Manoil 	return 0;
2567d4fd0404SClaudiu Manoil }
2568e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_close);
2569d4fd0404SClaudiu Manoil 
2570c33bfaf9SVladimir Oltean static int enetc_reconfigure(struct enetc_ndev_priv *priv, bool extended,
2571c33bfaf9SVladimir Oltean 			     int (*cb)(struct enetc_ndev_priv *priv, void *ctx),
2572c33bfaf9SVladimir Oltean 			     void *ctx)
25735093406cSVladimir Oltean {
25745093406cSVladimir Oltean 	struct enetc_bdr_resource *tx_res, *rx_res;
25755093406cSVladimir Oltean 	int err;
25765093406cSVladimir Oltean 
25775093406cSVladimir Oltean 	ASSERT_RTNL();
25785093406cSVladimir Oltean 
2579c33bfaf9SVladimir Oltean 	/* If the interface is down, run the callback right away,
2580c33bfaf9SVladimir Oltean 	 * without reconfiguration.
2581c33bfaf9SVladimir Oltean 	 */
2582c33bfaf9SVladimir Oltean 	if (!netif_running(priv->ndev)) {
258346a0ecf9SVladimir Oltean 		if (cb) {
258446a0ecf9SVladimir Oltean 			err = cb(priv, ctx);
258546a0ecf9SVladimir Oltean 			if (err)
258646a0ecf9SVladimir Oltean 				return err;
258746a0ecf9SVladimir Oltean 		}
2588c33bfaf9SVladimir Oltean 
25895093406cSVladimir Oltean 		return 0;
2590c33bfaf9SVladimir Oltean 	}
25915093406cSVladimir Oltean 
25925093406cSVladimir Oltean 	tx_res = enetc_alloc_tx_resources(priv);
25935093406cSVladimir Oltean 	if (IS_ERR(tx_res)) {
25945093406cSVladimir Oltean 		err = PTR_ERR(tx_res);
25955093406cSVladimir Oltean 		goto out;
25965093406cSVladimir Oltean 	}
25975093406cSVladimir Oltean 
25985093406cSVladimir Oltean 	rx_res = enetc_alloc_rx_resources(priv, extended);
25995093406cSVladimir Oltean 	if (IS_ERR(rx_res)) {
26005093406cSVladimir Oltean 		err = PTR_ERR(rx_res);
26015093406cSVladimir Oltean 		goto out_free_tx_res;
26025093406cSVladimir Oltean 	}
26035093406cSVladimir Oltean 
26045093406cSVladimir Oltean 	enetc_stop(priv->ndev);
26055093406cSVladimir Oltean 	enetc_free_rxtx_rings(priv);
26065093406cSVladimir Oltean 
2607c33bfaf9SVladimir Oltean 	/* Interface is down, run optional callback now */
260846a0ecf9SVladimir Oltean 	if (cb) {
260946a0ecf9SVladimir Oltean 		err = cb(priv, ctx);
261046a0ecf9SVladimir Oltean 		if (err)
261146a0ecf9SVladimir Oltean 			goto out_restart;
261246a0ecf9SVladimir Oltean 	}
2613c33bfaf9SVladimir Oltean 
26145093406cSVladimir Oltean 	enetc_assign_tx_resources(priv, tx_res);
26155093406cSVladimir Oltean 	enetc_assign_rx_resources(priv, rx_res);
26165093406cSVladimir Oltean 	enetc_setup_bdrs(priv, extended);
26175093406cSVladimir Oltean 	enetc_start(priv->ndev);
26185093406cSVladimir Oltean 
26195093406cSVladimir Oltean 	return 0;
26205093406cSVladimir Oltean 
262146a0ecf9SVladimir Oltean out_restart:
262246a0ecf9SVladimir Oltean 	enetc_setup_bdrs(priv, extended);
262346a0ecf9SVladimir Oltean 	enetc_start(priv->ndev);
262446a0ecf9SVladimir Oltean 	enetc_free_rx_resources(rx_res, priv->num_rx_rings);
26255093406cSVladimir Oltean out_free_tx_res:
26265093406cSVladimir Oltean 	enetc_free_tx_resources(tx_res, priv->num_tx_rings);
26275093406cSVladimir Oltean out:
26285093406cSVladimir Oltean 	return err;
26295093406cSVladimir Oltean }
26305093406cSVladimir Oltean 
26311a353111SVladimir Oltean static void enetc_debug_tx_ring_prios(struct enetc_ndev_priv *priv)
26321a353111SVladimir Oltean {
26331a353111SVladimir Oltean 	int i;
26341a353111SVladimir Oltean 
26351a353111SVladimir Oltean 	for (i = 0; i < priv->num_tx_rings; i++)
26361a353111SVladimir Oltean 		netdev_dbg(priv->ndev, "TX ring %d prio %d\n", i,
26371a353111SVladimir Oltean 			   priv->tx_ring[i]->prio);
26381a353111SVladimir Oltean }
26391a353111SVladimir Oltean 
26405353599aSVladimir Oltean void enetc_reset_tc_mqprio(struct net_device *ndev)
2641cbe9e835SCamelia Groza {
2642cbe9e835SCamelia Groza 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2643715bf261SVladimir Oltean 	struct enetc_hw *hw = &priv->si->hw;
2644cbe9e835SCamelia Groza 	struct enetc_bdr *tx_ring;
26457eab503bSVladimir Oltean 	int num_stack_tx_queues;
2646cbe9e835SCamelia Groza 	int i;
2647cbe9e835SCamelia Groza 
26487eab503bSVladimir Oltean 	num_stack_tx_queues = enetc_num_stack_tx_queues(priv);
2649cbe9e835SCamelia Groza 
2650cbe9e835SCamelia Groza 	netdev_reset_tc(ndev);
26517eab503bSVladimir Oltean 	netif_set_real_num_tx_queues(ndev, num_stack_tx_queues);
2652800db2d1SVladimir Oltean 	priv->min_num_stack_tx_queues = num_possible_cpus();
2653cbe9e835SCamelia Groza 
2654cbe9e835SCamelia Groza 	/* Reset all ring priorities to 0 */
2655cbe9e835SCamelia Groza 	for (i = 0; i < priv->num_tx_rings; i++) {
2656cbe9e835SCamelia Groza 		tx_ring = priv->tx_ring[i];
2657290b5fe0SVladimir Oltean 		tx_ring->prio = 0;
2658290b5fe0SVladimir Oltean 		enetc_set_bdr_prio(hw, tx_ring->index, tx_ring->prio);
2659cbe9e835SCamelia Groza 	}
2660cbe9e835SCamelia Groza 
26611a353111SVladimir Oltean 	enetc_debug_tx_ring_prios(priv);
266201e23b2bSVladimir Oltean 
266382714539SVladimir Oltean 	enetc_change_preemptible_tcs(priv, 0);
26641a353111SVladimir Oltean }
26655353599aSVladimir Oltean EXPORT_SYMBOL_GPL(enetc_reset_tc_mqprio);
26661a353111SVladimir Oltean 
26671a353111SVladimir Oltean int enetc_setup_tc_mqprio(struct net_device *ndev, void *type_data)
26681a353111SVladimir Oltean {
266950764da3SVladimir Oltean 	struct tc_mqprio_qopt_offload *mqprio = type_data;
26701a353111SVladimir Oltean 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
267150764da3SVladimir Oltean 	struct tc_mqprio_qopt *qopt = &mqprio->qopt;
26721a353111SVladimir Oltean 	struct enetc_hw *hw = &priv->si->hw;
26731a353111SVladimir Oltean 	int num_stack_tx_queues = 0;
26741a353111SVladimir Oltean 	struct enetc_bdr *tx_ring;
267550764da3SVladimir Oltean 	u8 num_tc = qopt->num_tc;
26761a353111SVladimir Oltean 	int offset, count;
26771a353111SVladimir Oltean 	int err, tc, q;
26781a353111SVladimir Oltean 
26791a353111SVladimir Oltean 	if (!num_tc) {
26801a353111SVladimir Oltean 		enetc_reset_tc_mqprio(ndev);
2681cbe9e835SCamelia Groza 		return 0;
2682cbe9e835SCamelia Groza 	}
2683cbe9e835SCamelia Groza 
26841a353111SVladimir Oltean 	err = netdev_set_num_tc(ndev, num_tc);
26851a353111SVladimir Oltean 	if (err)
26861a353111SVladimir Oltean 		return err;
26871a353111SVladimir Oltean 
26881a353111SVladimir Oltean 	for (tc = 0; tc < num_tc; tc++) {
268950764da3SVladimir Oltean 		offset = qopt->offset[tc];
269050764da3SVladimir Oltean 		count = qopt->count[tc];
26911a353111SVladimir Oltean 		num_stack_tx_queues += count;
26921a353111SVladimir Oltean 
26931a353111SVladimir Oltean 		err = netdev_set_tc_queue(ndev, tc, count, offset);
26941a353111SVladimir Oltean 		if (err)
26951a353111SVladimir Oltean 			goto err_reset_tc;
26961a353111SVladimir Oltean 
26971a353111SVladimir Oltean 		for (q = offset; q < offset + count; q++) {
26981a353111SVladimir Oltean 			tx_ring = priv->tx_ring[q];
26991a353111SVladimir Oltean 			/* The prio_tc_map is skb_tx_hash()'s way of selecting
27001a353111SVladimir Oltean 			 * between TX queues based on skb->priority. As such,
27011a353111SVladimir Oltean 			 * there's nothing to offload based on it.
27021a353111SVladimir Oltean 			 * Make the mqprio "traffic class" be the priority of
27031a353111SVladimir Oltean 			 * this ring group, and leave the Tx IPV to traffic
27041a353111SVladimir Oltean 			 * class mapping as its default mapping value of 1:1.
2705cbe9e835SCamelia Groza 			 */
27061a353111SVladimir Oltean 			tx_ring->prio = tc;
2707290b5fe0SVladimir Oltean 			enetc_set_bdr_prio(hw, tx_ring->index, tx_ring->prio);
2708cbe9e835SCamelia Groza 		}
27091a353111SVladimir Oltean 	}
2710cbe9e835SCamelia Groza 
27111a353111SVladimir Oltean 	err = netif_set_real_num_tx_queues(ndev, num_stack_tx_queues);
27121a353111SVladimir Oltean 	if (err)
27131a353111SVladimir Oltean 		goto err_reset_tc;
2714cbe9e835SCamelia Groza 
27151a353111SVladimir Oltean 	priv->min_num_stack_tx_queues = num_stack_tx_queues;
2716cbe9e835SCamelia Groza 
27171a353111SVladimir Oltean 	enetc_debug_tx_ring_prios(priv);
2718cbe9e835SCamelia Groza 
271982714539SVladimir Oltean 	enetc_change_preemptible_tcs(priv, mqprio->preemptible_tcs);
272001e23b2bSVladimir Oltean 
2721cbe9e835SCamelia Groza 	return 0;
27221a353111SVladimir Oltean 
27231a353111SVladimir Oltean err_reset_tc:
27241a353111SVladimir Oltean 	enetc_reset_tc_mqprio(ndev);
27251a353111SVladimir Oltean 	return err;
2726cbe9e835SCamelia Groza }
2727e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_setup_tc_mqprio);
2728cbe9e835SCamelia Groza 
2729c33bfaf9SVladimir Oltean static int enetc_reconfigure_xdp_cb(struct enetc_ndev_priv *priv, void *ctx)
2730d1b15102SVladimir Oltean {
2731c33bfaf9SVladimir Oltean 	struct bpf_prog *old_prog, *prog = ctx;
27324ea1dd74SVladimir Oltean 	int num_stack_tx_queues;
27334ea1dd74SVladimir Oltean 	int err, i;
2734d1b15102SVladimir Oltean 
2735d1b15102SVladimir Oltean 	old_prog = xchg(&priv->xdp_prog, prog);
27364ea1dd74SVladimir Oltean 
27374ea1dd74SVladimir Oltean 	num_stack_tx_queues = enetc_num_stack_tx_queues(priv);
27384ea1dd74SVladimir Oltean 	err = netif_set_real_num_tx_queues(priv->ndev, num_stack_tx_queues);
27394ea1dd74SVladimir Oltean 	if (err) {
27404ea1dd74SVladimir Oltean 		xchg(&priv->xdp_prog, old_prog);
27414ea1dd74SVladimir Oltean 		return err;
27424ea1dd74SVladimir Oltean 	}
27434ea1dd74SVladimir Oltean 
2744d1b15102SVladimir Oltean 	if (old_prog)
2745d1b15102SVladimir Oltean 		bpf_prog_put(old_prog);
2746d1b15102SVladimir Oltean 
2747d1b15102SVladimir Oltean 	for (i = 0; i < priv->num_rx_rings; i++) {
2748d1b15102SVladimir Oltean 		struct enetc_bdr *rx_ring = priv->rx_ring[i];
2749d1b15102SVladimir Oltean 
2750d1b15102SVladimir Oltean 		rx_ring->xdp.prog = prog;
2751d1b15102SVladimir Oltean 
2752d1b15102SVladimir Oltean 		if (prog)
2753d1b15102SVladimir Oltean 			rx_ring->buffer_offset = XDP_PACKET_HEADROOM;
2754d1b15102SVladimir Oltean 		else
2755d1b15102SVladimir Oltean 			rx_ring->buffer_offset = ENETC_RXB_PAD;
2756d1b15102SVladimir Oltean 	}
2757d1b15102SVladimir Oltean 
2758d1b15102SVladimir Oltean 	return 0;
2759d1b15102SVladimir Oltean }
2760d1b15102SVladimir Oltean 
2761c33bfaf9SVladimir Oltean static int enetc_setup_xdp_prog(struct net_device *ndev, struct bpf_prog *prog,
2762c33bfaf9SVladimir Oltean 				struct netlink_ext_ack *extack)
2763c33bfaf9SVladimir Oltean {
2764800db2d1SVladimir Oltean 	int num_xdp_tx_queues = prog ? num_possible_cpus() : 0;
2765c33bfaf9SVladimir Oltean 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2766c33bfaf9SVladimir Oltean 	bool extended;
2767c33bfaf9SVladimir Oltean 
2768800db2d1SVladimir Oltean 	if (priv->min_num_stack_tx_queues + num_xdp_tx_queues >
2769800db2d1SVladimir Oltean 	    priv->num_tx_rings) {
2770800db2d1SVladimir Oltean 		NL_SET_ERR_MSG_FMT_MOD(extack,
2771997a5310SArnd Bergmann 				       "Reserving %d XDP TXQs leaves under %d for stack (total %d)",
2772800db2d1SVladimir Oltean 				       num_xdp_tx_queues,
2773800db2d1SVladimir Oltean 				       priv->min_num_stack_tx_queues,
2774800db2d1SVladimir Oltean 				       priv->num_tx_rings);
2775800db2d1SVladimir Oltean 		return -EBUSY;
2776800db2d1SVladimir Oltean 	}
2777800db2d1SVladimir Oltean 
2778c33bfaf9SVladimir Oltean 	extended = !!(priv->active_offloads & ENETC_F_RX_TSTAMP);
2779c33bfaf9SVladimir Oltean 
2780c33bfaf9SVladimir Oltean 	/* The buffer layout is changing, so we need to drain the old
2781c33bfaf9SVladimir Oltean 	 * RX buffers and seed new ones.
2782c33bfaf9SVladimir Oltean 	 */
2783c33bfaf9SVladimir Oltean 	return enetc_reconfigure(priv, extended, enetc_reconfigure_xdp_cb, prog);
2784c33bfaf9SVladimir Oltean }
2785c33bfaf9SVladimir Oltean 
2786766338c7SVladimir Oltean int enetc_setup_bpf(struct net_device *ndev, struct netdev_bpf *bpf)
2787d1b15102SVladimir Oltean {
2788766338c7SVladimir Oltean 	switch (bpf->command) {
2789d1b15102SVladimir Oltean 	case XDP_SETUP_PROG:
2790766338c7SVladimir Oltean 		return enetc_setup_xdp_prog(ndev, bpf->prog, bpf->extack);
2791d1b15102SVladimir Oltean 	default:
2792d1b15102SVladimir Oltean 		return -EINVAL;
2793d1b15102SVladimir Oltean 	}
2794d1b15102SVladimir Oltean 
2795d1b15102SVladimir Oltean 	return 0;
2796d1b15102SVladimir Oltean }
2797e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_setup_bpf);
2798d1b15102SVladimir Oltean 
2799d4fd0404SClaudiu Manoil struct net_device_stats *enetc_get_stats(struct net_device *ndev)
2800d4fd0404SClaudiu Manoil {
2801d4fd0404SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2802d4fd0404SClaudiu Manoil 	struct net_device_stats *stats = &ndev->stats;
2803d4fd0404SClaudiu Manoil 	unsigned long packets = 0, bytes = 0;
2804285e8dedSPo Liu 	unsigned long tx_dropped = 0;
2805d4fd0404SClaudiu Manoil 	int i;
2806d4fd0404SClaudiu Manoil 
2807d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++) {
2808d4fd0404SClaudiu Manoil 		packets += priv->rx_ring[i]->stats.packets;
2809d4fd0404SClaudiu Manoil 		bytes	+= priv->rx_ring[i]->stats.bytes;
2810d4fd0404SClaudiu Manoil 	}
2811d4fd0404SClaudiu Manoil 
2812d4fd0404SClaudiu Manoil 	stats->rx_packets = packets;
2813d4fd0404SClaudiu Manoil 	stats->rx_bytes = bytes;
2814d4fd0404SClaudiu Manoil 	bytes = 0;
2815d4fd0404SClaudiu Manoil 	packets = 0;
2816d4fd0404SClaudiu Manoil 
2817d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++) {
2818d4fd0404SClaudiu Manoil 		packets += priv->tx_ring[i]->stats.packets;
2819d4fd0404SClaudiu Manoil 		bytes	+= priv->tx_ring[i]->stats.bytes;
2820285e8dedSPo Liu 		tx_dropped += priv->tx_ring[i]->stats.win_drop;
2821d4fd0404SClaudiu Manoil 	}
2822d4fd0404SClaudiu Manoil 
2823d4fd0404SClaudiu Manoil 	stats->tx_packets = packets;
2824d4fd0404SClaudiu Manoil 	stats->tx_bytes = bytes;
2825285e8dedSPo Liu 	stats->tx_dropped = tx_dropped;
2826d4fd0404SClaudiu Manoil 
2827d4fd0404SClaudiu Manoil 	return stats;
2828d4fd0404SClaudiu Manoil }
2829e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_get_stats);
2830d4fd0404SClaudiu Manoil 
2831d382563fSClaudiu Manoil static int enetc_set_rss(struct net_device *ndev, int en)
2832d382563fSClaudiu Manoil {
2833d382563fSClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2834d382563fSClaudiu Manoil 	struct enetc_hw *hw = &priv->si->hw;
2835d382563fSClaudiu Manoil 	u32 reg;
2836d382563fSClaudiu Manoil 
2837d382563fSClaudiu Manoil 	enetc_wr(hw, ENETC_SIRBGCR, priv->num_rx_rings);
2838d382563fSClaudiu Manoil 
2839d382563fSClaudiu Manoil 	reg = enetc_rd(hw, ENETC_SIMR);
2840d382563fSClaudiu Manoil 	reg &= ~ENETC_SIMR_RSSE;
2841d382563fSClaudiu Manoil 	reg |= (en) ? ENETC_SIMR_RSSE : 0;
2842d382563fSClaudiu Manoil 	enetc_wr(hw, ENETC_SIMR, reg);
2843d382563fSClaudiu Manoil 
2844d382563fSClaudiu Manoil 	return 0;
2845d382563fSClaudiu Manoil }
2846d382563fSClaudiu Manoil 
28479deba33fSClaudiu Manoil static void enetc_enable_rxvlan(struct net_device *ndev, bool en)
28489deba33fSClaudiu Manoil {
28499deba33fSClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2850715bf261SVladimir Oltean 	struct enetc_hw *hw = &priv->si->hw;
28519deba33fSClaudiu Manoil 	int i;
28529deba33fSClaudiu Manoil 
28539deba33fSClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
2854715bf261SVladimir Oltean 		enetc_bdr_enable_rxvlan(hw, i, en);
28559deba33fSClaudiu Manoil }
28569deba33fSClaudiu Manoil 
28579deba33fSClaudiu Manoil static void enetc_enable_txvlan(struct net_device *ndev, bool en)
28589deba33fSClaudiu Manoil {
28599deba33fSClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2860715bf261SVladimir Oltean 	struct enetc_hw *hw = &priv->si->hw;
28619deba33fSClaudiu Manoil 	int i;
28629deba33fSClaudiu Manoil 
28639deba33fSClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
2864715bf261SVladimir Oltean 		enetc_bdr_enable_txvlan(hw, i, en);
28659deba33fSClaudiu Manoil }
28669deba33fSClaudiu Manoil 
2867fed38e64SVladimir Oltean void enetc_set_features(struct net_device *ndev, netdev_features_t features)
2868d382563fSClaudiu Manoil {
2869d382563fSClaudiu Manoil 	netdev_features_t changed = ndev->features ^ features;
2870d382563fSClaudiu Manoil 
2871d382563fSClaudiu Manoil 	if (changed & NETIF_F_RXHASH)
2872d382563fSClaudiu Manoil 		enetc_set_rss(ndev, !!(features & NETIF_F_RXHASH));
2873d382563fSClaudiu Manoil 
28749deba33fSClaudiu Manoil 	if (changed & NETIF_F_HW_VLAN_CTAG_RX)
28759deba33fSClaudiu Manoil 		enetc_enable_rxvlan(ndev,
28769deba33fSClaudiu Manoil 				    !!(features & NETIF_F_HW_VLAN_CTAG_RX));
28779deba33fSClaudiu Manoil 
28789deba33fSClaudiu Manoil 	if (changed & NETIF_F_HW_VLAN_CTAG_TX)
28799deba33fSClaudiu Manoil 		enetc_enable_txvlan(ndev,
28809deba33fSClaudiu Manoil 				    !!(features & NETIF_F_HW_VLAN_CTAG_TX));
2881d382563fSClaudiu Manoil }
2882e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_set_features);
2883d382563fSClaudiu Manoil 
2884434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
2885d3982312SY.b. Lu static int enetc_hwtstamp_set(struct net_device *ndev, struct ifreq *ifr)
2886d3982312SY.b. Lu {
2887d3982312SY.b. Lu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
28885093406cSVladimir Oltean 	int err, new_offloads = priv->active_offloads;
2889d3982312SY.b. Lu 	struct hwtstamp_config config;
2890d3982312SY.b. Lu 
2891d3982312SY.b. Lu 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2892d3982312SY.b. Lu 		return -EFAULT;
2893d3982312SY.b. Lu 
2894d3982312SY.b. Lu 	switch (config.tx_type) {
2895d3982312SY.b. Lu 	case HWTSTAMP_TX_OFF:
28965093406cSVladimir Oltean 		new_offloads &= ~ENETC_F_TX_TSTAMP_MASK;
2897d3982312SY.b. Lu 		break;
2898d3982312SY.b. Lu 	case HWTSTAMP_TX_ON:
28995093406cSVladimir Oltean 		new_offloads &= ~ENETC_F_TX_TSTAMP_MASK;
29005093406cSVladimir Oltean 		new_offloads |= ENETC_F_TX_TSTAMP;
2901d3982312SY.b. Lu 		break;
29027294380cSYangbo Lu 	case HWTSTAMP_TX_ONESTEP_SYNC:
29035093406cSVladimir Oltean 		new_offloads &= ~ENETC_F_TX_TSTAMP_MASK;
29045093406cSVladimir Oltean 		new_offloads |= ENETC_F_TX_ONESTEP_SYNC_TSTAMP;
29057294380cSYangbo Lu 		break;
2906d3982312SY.b. Lu 	default:
2907d3982312SY.b. Lu 		return -ERANGE;
2908d3982312SY.b. Lu 	}
2909d3982312SY.b. Lu 
2910d3982312SY.b. Lu 	switch (config.rx_filter) {
2911d3982312SY.b. Lu 	case HWTSTAMP_FILTER_NONE:
29125093406cSVladimir Oltean 		new_offloads &= ~ENETC_F_RX_TSTAMP;
2913d3982312SY.b. Lu 		break;
2914d3982312SY.b. Lu 	default:
29155093406cSVladimir Oltean 		new_offloads |= ENETC_F_RX_TSTAMP;
2916d3982312SY.b. Lu 		config.rx_filter = HWTSTAMP_FILTER_ALL;
2917d3982312SY.b. Lu 	}
2918d3982312SY.b. Lu 
29195093406cSVladimir Oltean 	if ((new_offloads ^ priv->active_offloads) & ENETC_F_RX_TSTAMP) {
29205093406cSVladimir Oltean 		bool extended = !!(new_offloads & ENETC_F_RX_TSTAMP);
29215093406cSVladimir Oltean 
2922c33bfaf9SVladimir Oltean 		err = enetc_reconfigure(priv, extended, NULL, NULL);
29235093406cSVladimir Oltean 		if (err)
29245093406cSVladimir Oltean 			return err;
2925434cebabSClaudiu Manoil 	}
2926434cebabSClaudiu Manoil 
29275093406cSVladimir Oltean 	priv->active_offloads = new_offloads;
29285093406cSVladimir Oltean 
2929d3982312SY.b. Lu 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
2930d3982312SY.b. Lu 	       -EFAULT : 0;
2931d3982312SY.b. Lu }
2932d3982312SY.b. Lu 
2933d3982312SY.b. Lu static int enetc_hwtstamp_get(struct net_device *ndev, struct ifreq *ifr)
2934d3982312SY.b. Lu {
2935d3982312SY.b. Lu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2936d3982312SY.b. Lu 	struct hwtstamp_config config;
2937d3982312SY.b. Lu 
2938d3982312SY.b. Lu 	config.flags = 0;
2939d3982312SY.b. Lu 
29407294380cSYangbo Lu 	if (priv->active_offloads & ENETC_F_TX_ONESTEP_SYNC_TSTAMP)
29417294380cSYangbo Lu 		config.tx_type = HWTSTAMP_TX_ONESTEP_SYNC;
29427294380cSYangbo Lu 	else if (priv->active_offloads & ENETC_F_TX_TSTAMP)
2943d3982312SY.b. Lu 		config.tx_type = HWTSTAMP_TX_ON;
2944d3982312SY.b. Lu 	else
2945d3982312SY.b. Lu 		config.tx_type = HWTSTAMP_TX_OFF;
2946d3982312SY.b. Lu 
2947d3982312SY.b. Lu 	config.rx_filter = (priv->active_offloads & ENETC_F_RX_TSTAMP) ?
2948d3982312SY.b. Lu 			    HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE;
2949d3982312SY.b. Lu 
2950d3982312SY.b. Lu 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
2951d3982312SY.b. Lu 	       -EFAULT : 0;
2952d3982312SY.b. Lu }
2953d3982312SY.b. Lu #endif
2954d3982312SY.b. Lu 
2955d3982312SY.b. Lu int enetc_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2956d3982312SY.b. Lu {
295771b77a7aSClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2958434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
2959d3982312SY.b. Lu 	if (cmd == SIOCSHWTSTAMP)
2960d3982312SY.b. Lu 		return enetc_hwtstamp_set(ndev, rq);
2961d3982312SY.b. Lu 	if (cmd == SIOCGHWTSTAMP)
2962d3982312SY.b. Lu 		return enetc_hwtstamp_get(ndev, rq);
2963d3982312SY.b. Lu #endif
2964a613bafeSMichael Walle 
296571b77a7aSClaudiu Manoil 	if (!priv->phylink)
2966c55b810aSMichael Walle 		return -EOPNOTSUPP;
296771b77a7aSClaudiu Manoil 
296871b77a7aSClaudiu Manoil 	return phylink_mii_ioctl(priv->phylink, rq, cmd);
2969d3982312SY.b. Lu }
2970e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_ioctl);
2971d3982312SY.b. Lu 
2972d4fd0404SClaudiu Manoil int enetc_alloc_msix(struct enetc_ndev_priv *priv)
2973d4fd0404SClaudiu Manoil {
2974d4fd0404SClaudiu Manoil 	struct pci_dev *pdev = priv->si->pdev;
29754ea1dd74SVladimir Oltean 	int num_stack_tx_queues;
29767eab503bSVladimir Oltean 	int first_xdp_tx_ring;
2977d4fd0404SClaudiu Manoil 	int i, n, err, nvec;
29787eab503bSVladimir Oltean 	int v_tx_rings;
2979d4fd0404SClaudiu Manoil 
2980d4fd0404SClaudiu Manoil 	nvec = ENETC_BDR_INT_BASE_IDX + priv->bdr_int_num;
2981d4fd0404SClaudiu Manoil 	/* allocate MSIX for both messaging and Rx/Tx interrupts */
2982d4fd0404SClaudiu Manoil 	n = pci_alloc_irq_vectors(pdev, nvec, nvec, PCI_IRQ_MSIX);
2983d4fd0404SClaudiu Manoil 
2984d4fd0404SClaudiu Manoil 	if (n < 0)
2985d4fd0404SClaudiu Manoil 		return n;
2986d4fd0404SClaudiu Manoil 
2987d4fd0404SClaudiu Manoil 	if (n != nvec)
2988d4fd0404SClaudiu Manoil 		return -EPERM;
2989d4fd0404SClaudiu Manoil 
2990d4fd0404SClaudiu Manoil 	/* # of tx rings per int vector */
2991d4fd0404SClaudiu Manoil 	v_tx_rings = priv->num_tx_rings / priv->bdr_int_num;
2992d4fd0404SClaudiu Manoil 
2993d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
2994d4fd0404SClaudiu Manoil 		struct enetc_int_vector *v;
2995d4fd0404SClaudiu Manoil 		struct enetc_bdr *bdr;
2996d4fd0404SClaudiu Manoil 		int j;
2997d4fd0404SClaudiu Manoil 
29981260e772SGustavo A. R. Silva 		v = kzalloc(struct_size(v, tx_ring, v_tx_rings), GFP_KERNEL);
2999d4fd0404SClaudiu Manoil 		if (!v) {
3000d4fd0404SClaudiu Manoil 			err = -ENOMEM;
3001d4fd0404SClaudiu Manoil 			goto fail;
3002d4fd0404SClaudiu Manoil 		}
3003d4fd0404SClaudiu Manoil 
3004d4fd0404SClaudiu Manoil 		priv->int_vector[i] = v;
3005d4fd0404SClaudiu Manoil 
3006d1b15102SVladimir Oltean 		bdr = &v->rx_ring;
3007d1b15102SVladimir Oltean 		bdr->index = i;
3008d1b15102SVladimir Oltean 		bdr->ndev = priv->ndev;
3009d1b15102SVladimir Oltean 		bdr->dev = priv->dev;
3010d1b15102SVladimir Oltean 		bdr->bd_count = priv->rx_bd_count;
3011d1b15102SVladimir Oltean 		bdr->buffer_offset = ENETC_RXB_PAD;
3012d1b15102SVladimir Oltean 		priv->rx_ring[i] = bdr;
3013d1b15102SVladimir Oltean 
3014d1b15102SVladimir Oltean 		err = xdp_rxq_info_reg(&bdr->xdp.rxq, priv->ndev, i, 0);
3015d1b15102SVladimir Oltean 		if (err) {
3016d1b15102SVladimir Oltean 			kfree(v);
3017d1b15102SVladimir Oltean 			goto fail;
3018d1b15102SVladimir Oltean 		}
3019d1b15102SVladimir Oltean 
3020d1b15102SVladimir Oltean 		err = xdp_rxq_info_reg_mem_model(&bdr->xdp.rxq,
3021d1b15102SVladimir Oltean 						 MEM_TYPE_PAGE_SHARED, NULL);
3022d1b15102SVladimir Oltean 		if (err) {
3023d1b15102SVladimir Oltean 			xdp_rxq_info_unreg(&bdr->xdp.rxq);
3024d1b15102SVladimir Oltean 			kfree(v);
3025d1b15102SVladimir Oltean 			goto fail;
3026d1b15102SVladimir Oltean 		}
3027d1b15102SVladimir Oltean 
3028ae0e6a5dSClaudiu Manoil 		/* init defaults for adaptive IC */
3029ae0e6a5dSClaudiu Manoil 		if (priv->ic_mode & ENETC_IC_RX_ADAPTIVE) {
3030ae0e6a5dSClaudiu Manoil 			v->rx_ictt = 0x1;
3031ae0e6a5dSClaudiu Manoil 			v->rx_dim_en = true;
3032ae0e6a5dSClaudiu Manoil 		}
3033ae0e6a5dSClaudiu Manoil 		INIT_WORK(&v->rx_dim.work, enetc_rx_dim_work);
3034b48b89f9SJakub Kicinski 		netif_napi_add(priv->ndev, &v->napi, enetc_poll);
3035d4fd0404SClaudiu Manoil 		v->count_tx_rings = v_tx_rings;
3036d4fd0404SClaudiu Manoil 
3037d4fd0404SClaudiu Manoil 		for (j = 0; j < v_tx_rings; j++) {
3038d4fd0404SClaudiu Manoil 			int idx;
3039d4fd0404SClaudiu Manoil 
3040d4fd0404SClaudiu Manoil 			/* default tx ring mapping policy */
30416c5e6b4cSClaudiu Manoil 			idx = priv->bdr_int_num * j + i;
3042d4fd0404SClaudiu Manoil 			__set_bit(idx, &v->tx_rings_map);
3043d4fd0404SClaudiu Manoil 			bdr = &v->tx_ring[j];
3044d4fd0404SClaudiu Manoil 			bdr->index = idx;
3045d4fd0404SClaudiu Manoil 			bdr->ndev = priv->ndev;
3046d4fd0404SClaudiu Manoil 			bdr->dev = priv->dev;
3047d4fd0404SClaudiu Manoil 			bdr->bd_count = priv->tx_bd_count;
3048d4fd0404SClaudiu Manoil 			priv->tx_ring[idx] = bdr;
3049d4fd0404SClaudiu Manoil 		}
3050d4fd0404SClaudiu Manoil 	}
3051d4fd0404SClaudiu Manoil 
30524ea1dd74SVladimir Oltean 	num_stack_tx_queues = enetc_num_stack_tx_queues(priv);
30534ea1dd74SVladimir Oltean 
30544ea1dd74SVladimir Oltean 	err = netif_set_real_num_tx_queues(priv->ndev, num_stack_tx_queues);
30554ea1dd74SVladimir Oltean 	if (err)
30564ea1dd74SVladimir Oltean 		goto fail;
30574ea1dd74SVladimir Oltean 
30584ea1dd74SVladimir Oltean 	err = netif_set_real_num_rx_queues(priv->ndev, priv->num_rx_rings);
30594ea1dd74SVladimir Oltean 	if (err)
30604ea1dd74SVladimir Oltean 		goto fail;
30614ea1dd74SVladimir Oltean 
3062800db2d1SVladimir Oltean 	priv->min_num_stack_tx_queues = num_possible_cpus();
30637eab503bSVladimir Oltean 	first_xdp_tx_ring = priv->num_tx_rings - num_possible_cpus();
30647eab503bSVladimir Oltean 	priv->xdp_tx_ring = &priv->tx_ring[first_xdp_tx_ring];
30657eab503bSVladimir Oltean 
3066d4fd0404SClaudiu Manoil 	return 0;
3067d4fd0404SClaudiu Manoil 
3068d4fd0404SClaudiu Manoil fail:
3069d4fd0404SClaudiu Manoil 	while (i--) {
3070d1b15102SVladimir Oltean 		struct enetc_int_vector *v = priv->int_vector[i];
3071d1b15102SVladimir Oltean 		struct enetc_bdr *rx_ring = &v->rx_ring;
3072d1b15102SVladimir Oltean 
3073d1b15102SVladimir Oltean 		xdp_rxq_info_unreg_mem_model(&rx_ring->xdp.rxq);
3074d1b15102SVladimir Oltean 		xdp_rxq_info_unreg(&rx_ring->xdp.rxq);
3075d1b15102SVladimir Oltean 		netif_napi_del(&v->napi);
3076d1b15102SVladimir Oltean 		cancel_work_sync(&v->rx_dim.work);
3077d1b15102SVladimir Oltean 		kfree(v);
3078d4fd0404SClaudiu Manoil 	}
3079d4fd0404SClaudiu Manoil 
3080d4fd0404SClaudiu Manoil 	pci_free_irq_vectors(pdev);
3081d4fd0404SClaudiu Manoil 
3082d4fd0404SClaudiu Manoil 	return err;
3083d4fd0404SClaudiu Manoil }
3084e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_alloc_msix);
3085d4fd0404SClaudiu Manoil 
3086d4fd0404SClaudiu Manoil void enetc_free_msix(struct enetc_ndev_priv *priv)
3087d4fd0404SClaudiu Manoil {
3088d4fd0404SClaudiu Manoil 	int i;
3089d4fd0404SClaudiu Manoil 
3090d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
3091d4fd0404SClaudiu Manoil 		struct enetc_int_vector *v = priv->int_vector[i];
3092d1b15102SVladimir Oltean 		struct enetc_bdr *rx_ring = &v->rx_ring;
3093d4fd0404SClaudiu Manoil 
3094d1b15102SVladimir Oltean 		xdp_rxq_info_unreg_mem_model(&rx_ring->xdp.rxq);
3095d1b15102SVladimir Oltean 		xdp_rxq_info_unreg(&rx_ring->xdp.rxq);
3096d4fd0404SClaudiu Manoil 		netif_napi_del(&v->napi);
3097ae0e6a5dSClaudiu Manoil 		cancel_work_sync(&v->rx_dim.work);
3098d4fd0404SClaudiu Manoil 	}
3099d4fd0404SClaudiu Manoil 
3100d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
3101d4fd0404SClaudiu Manoil 		priv->rx_ring[i] = NULL;
3102d4fd0404SClaudiu Manoil 
3103d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
3104d4fd0404SClaudiu Manoil 		priv->tx_ring[i] = NULL;
3105d4fd0404SClaudiu Manoil 
3106d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
3107d4fd0404SClaudiu Manoil 		kfree(priv->int_vector[i]);
3108d4fd0404SClaudiu Manoil 		priv->int_vector[i] = NULL;
3109d4fd0404SClaudiu Manoil 	}
3110d4fd0404SClaudiu Manoil 
3111d4fd0404SClaudiu Manoil 	/* disable all MSIX for this device */
3112d4fd0404SClaudiu Manoil 	pci_free_irq_vectors(priv->si->pdev);
3113d4fd0404SClaudiu Manoil }
3114e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_free_msix);
3115d4fd0404SClaudiu Manoil 
3116d4fd0404SClaudiu Manoil static void enetc_kfree_si(struct enetc_si *si)
3117d4fd0404SClaudiu Manoil {
3118d4fd0404SClaudiu Manoil 	char *p = (char *)si - si->pad;
3119d4fd0404SClaudiu Manoil 
3120d4fd0404SClaudiu Manoil 	kfree(p);
3121d4fd0404SClaudiu Manoil }
3122d4fd0404SClaudiu Manoil 
3123d4fd0404SClaudiu Manoil static void enetc_detect_errata(struct enetc_si *si)
3124d4fd0404SClaudiu Manoil {
3125d4fd0404SClaudiu Manoil 	if (si->pdev->revision == ENETC_REV1)
312682728b91SClaudiu Manoil 		si->errata = ENETC_ERR_VLAN_ISOL | ENETC_ERR_UCMCSWP;
3127d4fd0404SClaudiu Manoil }
3128d4fd0404SClaudiu Manoil 
3129d4fd0404SClaudiu Manoil int enetc_pci_probe(struct pci_dev *pdev, const char *name, int sizeof_priv)
3130d4fd0404SClaudiu Manoil {
3131d4fd0404SClaudiu Manoil 	struct enetc_si *si, *p;
3132d4fd0404SClaudiu Manoil 	struct enetc_hw *hw;
3133d4fd0404SClaudiu Manoil 	size_t alloc_size;
3134d4fd0404SClaudiu Manoil 	int err, len;
3135d4fd0404SClaudiu Manoil 
3136d4fd0404SClaudiu Manoil 	pcie_flr(pdev);
3137d4fd0404SClaudiu Manoil 	err = pci_enable_device_mem(pdev);
3138a72691eeSCai Huoqing 	if (err)
3139a72691eeSCai Huoqing 		return dev_err_probe(&pdev->dev, err, "device enable failed\n");
3140d4fd0404SClaudiu Manoil 
3141d4fd0404SClaudiu Manoil 	/* set up for high or low dma */
3142d4fd0404SClaudiu Manoil 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3143d4fd0404SClaudiu Manoil 	if (err) {
3144cfcfc8f5SChristophe JAILLET 		dev_err(&pdev->dev, "DMA configuration failed: 0x%x\n", err);
3145d4fd0404SClaudiu Manoil 		goto err_dma;
3146d4fd0404SClaudiu Manoil 	}
3147d4fd0404SClaudiu Manoil 
3148d4fd0404SClaudiu Manoil 	err = pci_request_mem_regions(pdev, name);
3149d4fd0404SClaudiu Manoil 	if (err) {
3150d4fd0404SClaudiu Manoil 		dev_err(&pdev->dev, "pci_request_regions failed err=%d\n", err);
3151d4fd0404SClaudiu Manoil 		goto err_pci_mem_reg;
3152d4fd0404SClaudiu Manoil 	}
3153d4fd0404SClaudiu Manoil 
3154d4fd0404SClaudiu Manoil 	pci_set_master(pdev);
3155d4fd0404SClaudiu Manoil 
3156d4fd0404SClaudiu Manoil 	alloc_size = sizeof(struct enetc_si);
3157d4fd0404SClaudiu Manoil 	if (sizeof_priv) {
3158d4fd0404SClaudiu Manoil 		/* align priv to 32B */
3159d4fd0404SClaudiu Manoil 		alloc_size = ALIGN(alloc_size, ENETC_SI_ALIGN);
3160d4fd0404SClaudiu Manoil 		alloc_size += sizeof_priv;
3161d4fd0404SClaudiu Manoil 	}
3162d4fd0404SClaudiu Manoil 	/* force 32B alignment for enetc_si */
3163d4fd0404SClaudiu Manoil 	alloc_size += ENETC_SI_ALIGN - 1;
3164d4fd0404SClaudiu Manoil 
3165d4fd0404SClaudiu Manoil 	p = kzalloc(alloc_size, GFP_KERNEL);
3166d4fd0404SClaudiu Manoil 	if (!p) {
3167d4fd0404SClaudiu Manoil 		err = -ENOMEM;
3168d4fd0404SClaudiu Manoil 		goto err_alloc_si;
3169d4fd0404SClaudiu Manoil 	}
3170d4fd0404SClaudiu Manoil 
3171d4fd0404SClaudiu Manoil 	si = PTR_ALIGN(p, ENETC_SI_ALIGN);
3172d4fd0404SClaudiu Manoil 	si->pad = (char *)si - (char *)p;
3173d4fd0404SClaudiu Manoil 
3174d4fd0404SClaudiu Manoil 	pci_set_drvdata(pdev, si);
3175d4fd0404SClaudiu Manoil 	si->pdev = pdev;
3176d4fd0404SClaudiu Manoil 	hw = &si->hw;
3177d4fd0404SClaudiu Manoil 
3178d4fd0404SClaudiu Manoil 	len = pci_resource_len(pdev, ENETC_BAR_REGS);
3179d4fd0404SClaudiu Manoil 	hw->reg = ioremap(pci_resource_start(pdev, ENETC_BAR_REGS), len);
3180d4fd0404SClaudiu Manoil 	if (!hw->reg) {
3181d4fd0404SClaudiu Manoil 		err = -ENXIO;
3182d4fd0404SClaudiu Manoil 		dev_err(&pdev->dev, "ioremap() failed\n");
3183d4fd0404SClaudiu Manoil 		goto err_ioremap;
3184d4fd0404SClaudiu Manoil 	}
3185d4fd0404SClaudiu Manoil 	if (len > ENETC_PORT_BASE)
3186d4fd0404SClaudiu Manoil 		hw->port = hw->reg + ENETC_PORT_BASE;
3187d4fd0404SClaudiu Manoil 	if (len > ENETC_GLOBAL_BASE)
3188d4fd0404SClaudiu Manoil 		hw->global = hw->reg + ENETC_GLOBAL_BASE;
3189d4fd0404SClaudiu Manoil 
3190d4fd0404SClaudiu Manoil 	enetc_detect_errata(si);
3191d4fd0404SClaudiu Manoil 
3192d4fd0404SClaudiu Manoil 	return 0;
3193d4fd0404SClaudiu Manoil 
3194d4fd0404SClaudiu Manoil err_ioremap:
3195d4fd0404SClaudiu Manoil 	enetc_kfree_si(si);
3196d4fd0404SClaudiu Manoil err_alloc_si:
3197d4fd0404SClaudiu Manoil 	pci_release_mem_regions(pdev);
3198d4fd0404SClaudiu Manoil err_pci_mem_reg:
3199d4fd0404SClaudiu Manoil err_dma:
3200d4fd0404SClaudiu Manoil 	pci_disable_device(pdev);
3201d4fd0404SClaudiu Manoil 
3202d4fd0404SClaudiu Manoil 	return err;
3203d4fd0404SClaudiu Manoil }
3204e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_pci_probe);
3205d4fd0404SClaudiu Manoil 
3206d4fd0404SClaudiu Manoil void enetc_pci_remove(struct pci_dev *pdev)
3207d4fd0404SClaudiu Manoil {
3208d4fd0404SClaudiu Manoil 	struct enetc_si *si = pci_get_drvdata(pdev);
3209d4fd0404SClaudiu Manoil 	struct enetc_hw *hw = &si->hw;
3210d4fd0404SClaudiu Manoil 
3211d4fd0404SClaudiu Manoil 	iounmap(hw->reg);
3212d4fd0404SClaudiu Manoil 	enetc_kfree_si(si);
3213d4fd0404SClaudiu Manoil 	pci_release_mem_regions(pdev);
3214d4fd0404SClaudiu Manoil 	pci_disable_device(pdev);
3215d4fd0404SClaudiu Manoil }
3216e3972399SVladimir Oltean EXPORT_SYMBOL_GPL(enetc_pci_remove);
3217e3972399SVladimir Oltean 
3218e3972399SVladimir Oltean MODULE_LICENSE("Dual BSD/GPL");
3219