xref: /openbmc/linux/drivers/net/ethernet/freescale/enetc/enetc.c (revision 626b598aa8becc95ad1c56e721c90ddfc6a8c409)
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>
9847cbfc0SVladimir Oltean #include <net/pkt_sched.h>
10d4fd0404SClaudiu Manoil 
119d2b68ccSVladimir Oltean static struct sk_buff *enetc_tx_swbd_get_skb(struct enetc_tx_swbd *tx_swbd)
129d2b68ccSVladimir Oltean {
139d2b68ccSVladimir Oltean 	if (tx_swbd->is_xdp_tx || tx_swbd->is_xdp_redirect)
149d2b68ccSVladimir Oltean 		return NULL;
159d2b68ccSVladimir Oltean 
169d2b68ccSVladimir Oltean 	return tx_swbd->skb;
179d2b68ccSVladimir Oltean }
189d2b68ccSVladimir Oltean 
199d2b68ccSVladimir Oltean static struct xdp_frame *
209d2b68ccSVladimir Oltean enetc_tx_swbd_get_xdp_frame(struct enetc_tx_swbd *tx_swbd)
219d2b68ccSVladimir Oltean {
229d2b68ccSVladimir Oltean 	if (tx_swbd->is_xdp_redirect)
239d2b68ccSVladimir Oltean 		return tx_swbd->xdp_frame;
249d2b68ccSVladimir Oltean 
259d2b68ccSVladimir Oltean 	return NULL;
269d2b68ccSVladimir Oltean }
279d2b68ccSVladimir Oltean 
28d4fd0404SClaudiu Manoil static void enetc_unmap_tx_buff(struct enetc_bdr *tx_ring,
29d4fd0404SClaudiu Manoil 				struct enetc_tx_swbd *tx_swbd)
30d4fd0404SClaudiu Manoil {
317ed2bc80SVladimir Oltean 	/* For XDP_TX, pages come from RX, whereas for the other contexts where
327ed2bc80SVladimir Oltean 	 * we have is_dma_page_set, those come from skb_frag_dma_map. We need
337ed2bc80SVladimir Oltean 	 * to match the DMA mapping length, so we need to differentiate those.
347ed2bc80SVladimir Oltean 	 */
35d4fd0404SClaudiu Manoil 	if (tx_swbd->is_dma_page)
36d4fd0404SClaudiu Manoil 		dma_unmap_page(tx_ring->dev, tx_swbd->dma,
377ed2bc80SVladimir Oltean 			       tx_swbd->is_xdp_tx ? PAGE_SIZE : tx_swbd->len,
387ed2bc80SVladimir Oltean 			       tx_swbd->dir);
39d4fd0404SClaudiu Manoil 	else
40d4fd0404SClaudiu Manoil 		dma_unmap_single(tx_ring->dev, tx_swbd->dma,
417ed2bc80SVladimir Oltean 				 tx_swbd->len, tx_swbd->dir);
42d4fd0404SClaudiu Manoil 	tx_swbd->dma = 0;
43d4fd0404SClaudiu Manoil }
44d4fd0404SClaudiu Manoil 
459d2b68ccSVladimir Oltean static void enetc_free_tx_frame(struct enetc_bdr *tx_ring,
46d4fd0404SClaudiu Manoil 				struct enetc_tx_swbd *tx_swbd)
47d4fd0404SClaudiu Manoil {
489d2b68ccSVladimir Oltean 	struct xdp_frame *xdp_frame = enetc_tx_swbd_get_xdp_frame(tx_swbd);
499d2b68ccSVladimir Oltean 	struct sk_buff *skb = enetc_tx_swbd_get_skb(tx_swbd);
509d2b68ccSVladimir Oltean 
51d4fd0404SClaudiu Manoil 	if (tx_swbd->dma)
52d4fd0404SClaudiu Manoil 		enetc_unmap_tx_buff(tx_ring, tx_swbd);
53d4fd0404SClaudiu Manoil 
549d2b68ccSVladimir Oltean 	if (xdp_frame) {
559d2b68ccSVladimir Oltean 		xdp_return_frame(tx_swbd->xdp_frame);
569d2b68ccSVladimir Oltean 		tx_swbd->xdp_frame = NULL;
579d2b68ccSVladimir Oltean 	} else if (skb) {
589d2b68ccSVladimir Oltean 		dev_kfree_skb_any(skb);
59d4fd0404SClaudiu Manoil 		tx_swbd->skb = NULL;
60d4fd0404SClaudiu Manoil 	}
61d4fd0404SClaudiu Manoil }
62d4fd0404SClaudiu Manoil 
637ed2bc80SVladimir Oltean /* Let H/W know BD ring has been updated */
647ed2bc80SVladimir Oltean static void enetc_update_tx_ring_tail(struct enetc_bdr *tx_ring)
657ed2bc80SVladimir Oltean {
667ed2bc80SVladimir Oltean 	/* includes wmb() */
677ed2bc80SVladimir Oltean 	enetc_wr_reg_hot(tx_ring->tpir, tx_ring->next_to_use);
687ed2bc80SVladimir Oltean }
697ed2bc80SVladimir Oltean 
70d3982312SY.b. Lu static int enetc_map_tx_buffs(struct enetc_bdr *tx_ring, struct sk_buff *skb,
71d3982312SY.b. Lu 			      int active_offloads)
72d4fd0404SClaudiu Manoil {
73d4fd0404SClaudiu Manoil 	struct enetc_tx_swbd *tx_swbd;
74d7840976SMatthew Wilcox (Oracle) 	skb_frag_t *frag;
75d4fd0404SClaudiu Manoil 	int len = skb_headlen(skb);
76d4fd0404SClaudiu Manoil 	union enetc_tx_bd temp_bd;
77d4fd0404SClaudiu Manoil 	union enetc_tx_bd *txbd;
78d4fd0404SClaudiu Manoil 	bool do_vlan, do_tstamp;
79d4fd0404SClaudiu Manoil 	int i, count = 0;
80d4fd0404SClaudiu Manoil 	unsigned int f;
81d4fd0404SClaudiu Manoil 	dma_addr_t dma;
82d4fd0404SClaudiu Manoil 	u8 flags = 0;
83d4fd0404SClaudiu Manoil 
84d4fd0404SClaudiu Manoil 	i = tx_ring->next_to_use;
85d4fd0404SClaudiu Manoil 	txbd = ENETC_TXBD(*tx_ring, i);
86d4fd0404SClaudiu Manoil 	prefetchw(txbd);
87d4fd0404SClaudiu Manoil 
88d4fd0404SClaudiu Manoil 	dma = dma_map_single(tx_ring->dev, skb->data, len, DMA_TO_DEVICE);
89d4fd0404SClaudiu Manoil 	if (unlikely(dma_mapping_error(tx_ring->dev, dma)))
90d4fd0404SClaudiu Manoil 		goto dma_err;
91d4fd0404SClaudiu Manoil 
92d4fd0404SClaudiu Manoil 	temp_bd.addr = cpu_to_le64(dma);
93d4fd0404SClaudiu Manoil 	temp_bd.buf_len = cpu_to_le16(len);
94d4fd0404SClaudiu Manoil 	temp_bd.lstatus = 0;
95d4fd0404SClaudiu Manoil 
96d4fd0404SClaudiu Manoil 	tx_swbd = &tx_ring->tx_swbd[i];
97d4fd0404SClaudiu Manoil 	tx_swbd->dma = dma;
98d4fd0404SClaudiu Manoil 	tx_swbd->len = len;
99d4fd0404SClaudiu Manoil 	tx_swbd->is_dma_page = 0;
1007ed2bc80SVladimir Oltean 	tx_swbd->dir = DMA_TO_DEVICE;
101d4fd0404SClaudiu Manoil 	count++;
102d4fd0404SClaudiu Manoil 
103d4fd0404SClaudiu Manoil 	do_vlan = skb_vlan_tag_present(skb);
104d3982312SY.b. Lu 	do_tstamp = (active_offloads & ENETC_F_TX_TSTAMP) &&
105d3982312SY.b. Lu 		    (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP);
106d3982312SY.b. Lu 	tx_swbd->do_tstamp = do_tstamp;
107d3982312SY.b. Lu 	tx_swbd->check_wb = tx_swbd->do_tstamp;
108d4fd0404SClaudiu Manoil 
109d4fd0404SClaudiu Manoil 	if (do_vlan || do_tstamp)
110d4fd0404SClaudiu Manoil 		flags |= ENETC_TXBD_FLAGS_EX;
111d4fd0404SClaudiu Manoil 
11282728b91SClaudiu Manoil 	if (tx_ring->tsd_enable)
1130d08c9ecSPo Liu 		flags |= ENETC_TXBD_FLAGS_TSE | ENETC_TXBD_FLAGS_TXSTART;
114d4fd0404SClaudiu Manoil 
115d4fd0404SClaudiu Manoil 	/* first BD needs frm_len and offload flags set */
116d4fd0404SClaudiu Manoil 	temp_bd.frm_len = cpu_to_le16(skb->len);
117d4fd0404SClaudiu Manoil 	temp_bd.flags = flags;
118d4fd0404SClaudiu Manoil 
11982728b91SClaudiu Manoil 	if (flags & ENETC_TXBD_FLAGS_TSE)
12082728b91SClaudiu Manoil 		temp_bd.txstart = enetc_txbd_set_tx_start(skb->skb_mstamp_ns,
12182728b91SClaudiu Manoil 							  flags);
1220d08c9ecSPo Liu 
123d4fd0404SClaudiu Manoil 	if (flags & ENETC_TXBD_FLAGS_EX) {
124d4fd0404SClaudiu Manoil 		u8 e_flags = 0;
125d4fd0404SClaudiu Manoil 		*txbd = temp_bd;
126d4fd0404SClaudiu Manoil 		enetc_clear_tx_bd(&temp_bd);
127d4fd0404SClaudiu Manoil 
128d4fd0404SClaudiu Manoil 		/* add extension BD for VLAN and/or timestamping */
129d4fd0404SClaudiu Manoil 		flags = 0;
130d4fd0404SClaudiu Manoil 		tx_swbd++;
131d4fd0404SClaudiu Manoil 		txbd++;
132d4fd0404SClaudiu Manoil 		i++;
133d4fd0404SClaudiu Manoil 		if (unlikely(i == tx_ring->bd_count)) {
134d4fd0404SClaudiu Manoil 			i = 0;
135d4fd0404SClaudiu Manoil 			tx_swbd = tx_ring->tx_swbd;
136d4fd0404SClaudiu Manoil 			txbd = ENETC_TXBD(*tx_ring, 0);
137d4fd0404SClaudiu Manoil 		}
138d4fd0404SClaudiu Manoil 		prefetchw(txbd);
139d4fd0404SClaudiu Manoil 
140d4fd0404SClaudiu Manoil 		if (do_vlan) {
141d4fd0404SClaudiu Manoil 			temp_bd.ext.vid = cpu_to_le16(skb_vlan_tag_get(skb));
142d4fd0404SClaudiu Manoil 			temp_bd.ext.tpid = 0; /* < C-TAG */
143d4fd0404SClaudiu Manoil 			e_flags |= ENETC_TXBD_E_FLAGS_VLAN_INS;
144d4fd0404SClaudiu Manoil 		}
145d4fd0404SClaudiu Manoil 
146d4fd0404SClaudiu Manoil 		if (do_tstamp) {
147d4fd0404SClaudiu Manoil 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
148d4fd0404SClaudiu Manoil 			e_flags |= ENETC_TXBD_E_FLAGS_TWO_STEP_PTP;
149d4fd0404SClaudiu Manoil 		}
150d4fd0404SClaudiu Manoil 
151d4fd0404SClaudiu Manoil 		temp_bd.ext.e_flags = e_flags;
152d4fd0404SClaudiu Manoil 		count++;
153d4fd0404SClaudiu Manoil 	}
154d4fd0404SClaudiu Manoil 
155d4fd0404SClaudiu Manoil 	frag = &skb_shinfo(skb)->frags[0];
156d4fd0404SClaudiu Manoil 	for (f = 0; f < skb_shinfo(skb)->nr_frags; f++, frag++) {
157d4fd0404SClaudiu Manoil 		len = skb_frag_size(frag);
158d4fd0404SClaudiu Manoil 		dma = skb_frag_dma_map(tx_ring->dev, frag, 0, len,
159d4fd0404SClaudiu Manoil 				       DMA_TO_DEVICE);
160d4fd0404SClaudiu Manoil 		if (dma_mapping_error(tx_ring->dev, dma))
161d4fd0404SClaudiu Manoil 			goto dma_err;
162d4fd0404SClaudiu Manoil 
163d4fd0404SClaudiu Manoil 		*txbd = temp_bd;
164d4fd0404SClaudiu Manoil 		enetc_clear_tx_bd(&temp_bd);
165d4fd0404SClaudiu Manoil 
166d4fd0404SClaudiu Manoil 		flags = 0;
167d4fd0404SClaudiu Manoil 		tx_swbd++;
168d4fd0404SClaudiu Manoil 		txbd++;
169d4fd0404SClaudiu Manoil 		i++;
170d4fd0404SClaudiu Manoil 		if (unlikely(i == tx_ring->bd_count)) {
171d4fd0404SClaudiu Manoil 			i = 0;
172d4fd0404SClaudiu Manoil 			tx_swbd = tx_ring->tx_swbd;
173d4fd0404SClaudiu Manoil 			txbd = ENETC_TXBD(*tx_ring, 0);
174d4fd0404SClaudiu Manoil 		}
175d4fd0404SClaudiu Manoil 		prefetchw(txbd);
176d4fd0404SClaudiu Manoil 
177d4fd0404SClaudiu Manoil 		temp_bd.addr = cpu_to_le64(dma);
178d4fd0404SClaudiu Manoil 		temp_bd.buf_len = cpu_to_le16(len);
179d4fd0404SClaudiu Manoil 
180d4fd0404SClaudiu Manoil 		tx_swbd->dma = dma;
181d4fd0404SClaudiu Manoil 		tx_swbd->len = len;
182d4fd0404SClaudiu Manoil 		tx_swbd->is_dma_page = 1;
1837ed2bc80SVladimir Oltean 		tx_swbd->dir = DMA_TO_DEVICE;
184d4fd0404SClaudiu Manoil 		count++;
185d4fd0404SClaudiu Manoil 	}
186d4fd0404SClaudiu Manoil 
187d4fd0404SClaudiu Manoil 	/* last BD needs 'F' bit set */
188d4fd0404SClaudiu Manoil 	flags |= ENETC_TXBD_FLAGS_F;
189d4fd0404SClaudiu Manoil 	temp_bd.flags = flags;
190d4fd0404SClaudiu Manoil 	*txbd = temp_bd;
191d4fd0404SClaudiu Manoil 
192d504498dSVladimir Oltean 	tx_ring->tx_swbd[i].is_eof = true;
193d4fd0404SClaudiu Manoil 	tx_ring->tx_swbd[i].skb = skb;
194d4fd0404SClaudiu Manoil 
195d4fd0404SClaudiu Manoil 	enetc_bdr_idx_inc(tx_ring, &i);
196d4fd0404SClaudiu Manoil 	tx_ring->next_to_use = i;
197d4fd0404SClaudiu Manoil 
1984caefbceSMichael Walle 	skb_tx_timestamp(skb);
1994caefbceSMichael Walle 
2007ed2bc80SVladimir Oltean 	enetc_update_tx_ring_tail(tx_ring);
201d4fd0404SClaudiu Manoil 
202d4fd0404SClaudiu Manoil 	return count;
203d4fd0404SClaudiu Manoil 
204d4fd0404SClaudiu Manoil dma_err:
205d4fd0404SClaudiu Manoil 	dev_err(tx_ring->dev, "DMA map error");
206d4fd0404SClaudiu Manoil 
207d4fd0404SClaudiu Manoil 	do {
208d4fd0404SClaudiu Manoil 		tx_swbd = &tx_ring->tx_swbd[i];
2099d2b68ccSVladimir Oltean 		enetc_free_tx_frame(tx_ring, tx_swbd);
210d4fd0404SClaudiu Manoil 		if (i == 0)
211d4fd0404SClaudiu Manoil 			i = tx_ring->bd_count;
212d4fd0404SClaudiu Manoil 		i--;
213d4fd0404SClaudiu Manoil 	} while (count--);
214d4fd0404SClaudiu Manoil 
215d4fd0404SClaudiu Manoil 	return 0;
216d4fd0404SClaudiu Manoil }
217d4fd0404SClaudiu Manoil 
2180486185eSVladimir Oltean netdev_tx_t enetc_xmit(struct sk_buff *skb, struct net_device *ndev)
2190486185eSVladimir Oltean {
2200486185eSVladimir Oltean 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2210486185eSVladimir Oltean 	struct enetc_bdr *tx_ring;
2220486185eSVladimir Oltean 	int count;
2230486185eSVladimir Oltean 
2240486185eSVladimir Oltean 	tx_ring = priv->tx_ring[skb->queue_mapping];
2250486185eSVladimir Oltean 
2260486185eSVladimir Oltean 	if (unlikely(skb_shinfo(skb)->nr_frags > ENETC_MAX_SKB_FRAGS))
2270486185eSVladimir Oltean 		if (unlikely(skb_linearize(skb)))
2280486185eSVladimir Oltean 			goto drop_packet_err;
2290486185eSVladimir Oltean 
2300486185eSVladimir Oltean 	count = skb_shinfo(skb)->nr_frags + 1; /* fragments + head */
2310486185eSVladimir Oltean 	if (enetc_bd_unused(tx_ring) < ENETC_TXBDS_NEEDED(count)) {
2320486185eSVladimir Oltean 		netif_stop_subqueue(ndev, tx_ring->index);
2330486185eSVladimir Oltean 		return NETDEV_TX_BUSY;
2340486185eSVladimir Oltean 	}
2350486185eSVladimir Oltean 
2360486185eSVladimir Oltean 	enetc_lock_mdio();
2370486185eSVladimir Oltean 	count = enetc_map_tx_buffs(tx_ring, skb, priv->active_offloads);
2380486185eSVladimir Oltean 	enetc_unlock_mdio();
2390486185eSVladimir Oltean 
2400486185eSVladimir Oltean 	if (unlikely(!count))
2410486185eSVladimir Oltean 		goto drop_packet_err;
2420486185eSVladimir Oltean 
2430486185eSVladimir Oltean 	if (enetc_bd_unused(tx_ring) < ENETC_TXBDS_MAX_NEEDED)
2440486185eSVladimir Oltean 		netif_stop_subqueue(ndev, tx_ring->index);
2450486185eSVladimir Oltean 
2460486185eSVladimir Oltean 	return NETDEV_TX_OK;
2470486185eSVladimir Oltean 
2480486185eSVladimir Oltean drop_packet_err:
2490486185eSVladimir Oltean 	dev_kfree_skb_any(skb);
2500486185eSVladimir Oltean 	return NETDEV_TX_OK;
2510486185eSVladimir Oltean }
2520486185eSVladimir Oltean 
253d4fd0404SClaudiu Manoil static irqreturn_t enetc_msix(int irq, void *data)
254d4fd0404SClaudiu Manoil {
255d4fd0404SClaudiu Manoil 	struct enetc_int_vector	*v = data;
256d4fd0404SClaudiu Manoil 	int i;
257d4fd0404SClaudiu Manoil 
258fd5736bfSAlex Marginean 	enetc_lock_mdio();
259fd5736bfSAlex Marginean 
260d4fd0404SClaudiu Manoil 	/* disable interrupts */
261fd5736bfSAlex Marginean 	enetc_wr_reg_hot(v->rbier, 0);
262fd5736bfSAlex Marginean 	enetc_wr_reg_hot(v->ricr1, v->rx_ictt);
263d4fd0404SClaudiu Manoil 
2640574e200SClaudiu Manoil 	for_each_set_bit(i, &v->tx_rings_map, ENETC_MAX_NUM_TXQS)
265fd5736bfSAlex Marginean 		enetc_wr_reg_hot(v->tbier_base + ENETC_BDR_OFF(i), 0);
266fd5736bfSAlex Marginean 
267fd5736bfSAlex Marginean 	enetc_unlock_mdio();
268d4fd0404SClaudiu Manoil 
269215602a8SJiafei Pan 	napi_schedule(&v->napi);
270d4fd0404SClaudiu Manoil 
271d4fd0404SClaudiu Manoil 	return IRQ_HANDLED;
272d4fd0404SClaudiu Manoil }
273d4fd0404SClaudiu Manoil 
274ae0e6a5dSClaudiu Manoil static void enetc_rx_dim_work(struct work_struct *w)
275ae0e6a5dSClaudiu Manoil {
276ae0e6a5dSClaudiu Manoil 	struct dim *dim = container_of(w, struct dim, work);
277ae0e6a5dSClaudiu Manoil 	struct dim_cq_moder moder =
278ae0e6a5dSClaudiu Manoil 		net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
279ae0e6a5dSClaudiu Manoil 	struct enetc_int_vector	*v =
280ae0e6a5dSClaudiu Manoil 		container_of(dim, struct enetc_int_vector, rx_dim);
281ae0e6a5dSClaudiu Manoil 
282ae0e6a5dSClaudiu Manoil 	v->rx_ictt = enetc_usecs_to_cycles(moder.usec);
283ae0e6a5dSClaudiu Manoil 	dim->state = DIM_START_MEASURE;
284ae0e6a5dSClaudiu Manoil }
285ae0e6a5dSClaudiu Manoil 
286ae0e6a5dSClaudiu Manoil static void enetc_rx_net_dim(struct enetc_int_vector *v)
287ae0e6a5dSClaudiu Manoil {
288ae0e6a5dSClaudiu Manoil 	struct dim_sample dim_sample;
289ae0e6a5dSClaudiu Manoil 
290ae0e6a5dSClaudiu Manoil 	v->comp_cnt++;
291ae0e6a5dSClaudiu Manoil 
292ae0e6a5dSClaudiu Manoil 	if (!v->rx_napi_work)
293ae0e6a5dSClaudiu Manoil 		return;
294ae0e6a5dSClaudiu Manoil 
295ae0e6a5dSClaudiu Manoil 	dim_update_sample(v->comp_cnt,
296ae0e6a5dSClaudiu Manoil 			  v->rx_ring.stats.packets,
297ae0e6a5dSClaudiu Manoil 			  v->rx_ring.stats.bytes,
298ae0e6a5dSClaudiu Manoil 			  &dim_sample);
299ae0e6a5dSClaudiu Manoil 	net_dim(&v->rx_dim, dim_sample);
300ae0e6a5dSClaudiu Manoil }
301ae0e6a5dSClaudiu Manoil 
302d4fd0404SClaudiu Manoil static int enetc_bd_ready_count(struct enetc_bdr *tx_ring, int ci)
303d4fd0404SClaudiu Manoil {
304fd5736bfSAlex Marginean 	int pi = enetc_rd_reg_hot(tx_ring->tcir) & ENETC_TBCIR_IDX_MASK;
305d4fd0404SClaudiu Manoil 
306d4fd0404SClaudiu Manoil 	return pi >= ci ? pi - ci : tx_ring->bd_count - ci + pi;
307d4fd0404SClaudiu Manoil }
308d4fd0404SClaudiu Manoil 
30965d0cbb4SVladimir Oltean static bool enetc_page_reusable(struct page *page)
31065d0cbb4SVladimir Oltean {
31165d0cbb4SVladimir Oltean 	return (!page_is_pfmemalloc(page) && page_ref_count(page) == 1);
31265d0cbb4SVladimir Oltean }
31365d0cbb4SVladimir Oltean 
31465d0cbb4SVladimir Oltean static void enetc_reuse_page(struct enetc_bdr *rx_ring,
31565d0cbb4SVladimir Oltean 			     struct enetc_rx_swbd *old)
31665d0cbb4SVladimir Oltean {
31765d0cbb4SVladimir Oltean 	struct enetc_rx_swbd *new;
31865d0cbb4SVladimir Oltean 
31965d0cbb4SVladimir Oltean 	new = &rx_ring->rx_swbd[rx_ring->next_to_alloc];
32065d0cbb4SVladimir Oltean 
32165d0cbb4SVladimir Oltean 	/* next buf that may reuse a page */
32265d0cbb4SVladimir Oltean 	enetc_bdr_idx_inc(rx_ring, &rx_ring->next_to_alloc);
32365d0cbb4SVladimir Oltean 
32465d0cbb4SVladimir Oltean 	/* copy page reference */
32565d0cbb4SVladimir Oltean 	*new = *old;
32665d0cbb4SVladimir Oltean }
32765d0cbb4SVladimir Oltean 
328d3982312SY.b. Lu static void enetc_get_tx_tstamp(struct enetc_hw *hw, union enetc_tx_bd *txbd,
329d3982312SY.b. Lu 				u64 *tstamp)
330d3982312SY.b. Lu {
331cec4f328SY.b. Lu 	u32 lo, hi, tstamp_lo;
332d3982312SY.b. Lu 
3336d36ecdbSVladimir Oltean 	lo = enetc_rd_hot(hw, ENETC_SICTR0);
3346d36ecdbSVladimir Oltean 	hi = enetc_rd_hot(hw, ENETC_SICTR1);
335cec4f328SY.b. Lu 	tstamp_lo = le32_to_cpu(txbd->wb.tstamp);
336cec4f328SY.b. Lu 	if (lo <= tstamp_lo)
337d3982312SY.b. Lu 		hi -= 1;
338cec4f328SY.b. Lu 	*tstamp = (u64)hi << 32 | tstamp_lo;
339d3982312SY.b. Lu }
340d3982312SY.b. Lu 
341d3982312SY.b. Lu static void enetc_tstamp_tx(struct sk_buff *skb, u64 tstamp)
342d3982312SY.b. Lu {
343d3982312SY.b. Lu 	struct skb_shared_hwtstamps shhwtstamps;
344d3982312SY.b. Lu 
345d3982312SY.b. Lu 	if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) {
346d3982312SY.b. Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
347d3982312SY.b. Lu 		shhwtstamps.hwtstamp = ns_to_ktime(tstamp);
348847cbfc0SVladimir Oltean 		skb_txtime_consumed(skb);
349d3982312SY.b. Lu 		skb_tstamp_tx(skb, &shhwtstamps);
350d3982312SY.b. Lu 	}
351d3982312SY.b. Lu }
352d3982312SY.b. Lu 
3537ed2bc80SVladimir Oltean static void enetc_recycle_xdp_tx_buff(struct enetc_bdr *tx_ring,
3547ed2bc80SVladimir Oltean 				      struct enetc_tx_swbd *tx_swbd)
3557ed2bc80SVladimir Oltean {
3567ed2bc80SVladimir Oltean 	struct enetc_ndev_priv *priv = netdev_priv(tx_ring->ndev);
3577ed2bc80SVladimir Oltean 	struct enetc_bdr *rx_ring = priv->rx_ring[tx_ring->index];
3587ed2bc80SVladimir Oltean 	struct enetc_rx_swbd rx_swbd = {
3597ed2bc80SVladimir Oltean 		.dma = tx_swbd->dma,
3607ed2bc80SVladimir Oltean 		.page = tx_swbd->page,
3617ed2bc80SVladimir Oltean 		.page_offset = tx_swbd->page_offset,
3627ed2bc80SVladimir Oltean 		.dir = tx_swbd->dir,
3637ed2bc80SVladimir Oltean 		.len = tx_swbd->len,
3647ed2bc80SVladimir Oltean 	};
3657ed2bc80SVladimir Oltean 
3667ed2bc80SVladimir Oltean 	if (likely(enetc_swbd_unused(rx_ring))) {
3677ed2bc80SVladimir Oltean 		enetc_reuse_page(rx_ring, &rx_swbd);
3687ed2bc80SVladimir Oltean 
3697ed2bc80SVladimir Oltean 		/* sync for use by the device */
3707ed2bc80SVladimir Oltean 		dma_sync_single_range_for_device(rx_ring->dev, rx_swbd.dma,
3717ed2bc80SVladimir Oltean 						 rx_swbd.page_offset,
3727ed2bc80SVladimir Oltean 						 ENETC_RXB_DMA_SIZE_XDP,
3737ed2bc80SVladimir Oltean 						 rx_swbd.dir);
3747ed2bc80SVladimir Oltean 
3757ed2bc80SVladimir Oltean 		rx_ring->stats.recycles++;
3767ed2bc80SVladimir Oltean 	} else {
3777ed2bc80SVladimir Oltean 		/* RX ring is already full, we need to unmap and free the
3787ed2bc80SVladimir Oltean 		 * page, since there's nothing useful we can do with it.
3797ed2bc80SVladimir Oltean 		 */
3807ed2bc80SVladimir Oltean 		rx_ring->stats.recycle_failures++;
3817ed2bc80SVladimir Oltean 
3827ed2bc80SVladimir Oltean 		dma_unmap_page(rx_ring->dev, rx_swbd.dma, PAGE_SIZE,
3837ed2bc80SVladimir Oltean 			       rx_swbd.dir);
3847ed2bc80SVladimir Oltean 		__free_page(rx_swbd.page);
3857ed2bc80SVladimir Oltean 	}
3867ed2bc80SVladimir Oltean 
3877ed2bc80SVladimir Oltean 	rx_ring->xdp.xdp_tx_in_flight--;
3887ed2bc80SVladimir Oltean }
3897ed2bc80SVladimir Oltean 
390d4fd0404SClaudiu Manoil static bool enetc_clean_tx_ring(struct enetc_bdr *tx_ring, int napi_budget)
391d4fd0404SClaudiu Manoil {
392d4fd0404SClaudiu Manoil 	struct net_device *ndev = tx_ring->ndev;
393d4fd0404SClaudiu Manoil 	int tx_frm_cnt = 0, tx_byte_cnt = 0;
394d4fd0404SClaudiu Manoil 	struct enetc_tx_swbd *tx_swbd;
395d4fd0404SClaudiu Manoil 	int i, bds_to_clean;
396d3982312SY.b. Lu 	bool do_tstamp;
397d3982312SY.b. Lu 	u64 tstamp = 0;
398d4fd0404SClaudiu Manoil 
399d4fd0404SClaudiu Manoil 	i = tx_ring->next_to_clean;
400d4fd0404SClaudiu Manoil 	tx_swbd = &tx_ring->tx_swbd[i];
401fd5736bfSAlex Marginean 
402d4fd0404SClaudiu Manoil 	bds_to_clean = enetc_bd_ready_count(tx_ring, i);
403d4fd0404SClaudiu Manoil 
404d3982312SY.b. Lu 	do_tstamp = false;
405d3982312SY.b. Lu 
406d4fd0404SClaudiu Manoil 	while (bds_to_clean && tx_frm_cnt < ENETC_DEFAULT_TX_WORK) {
4079d2b68ccSVladimir Oltean 		struct xdp_frame *xdp_frame = enetc_tx_swbd_get_xdp_frame(tx_swbd);
4089d2b68ccSVladimir Oltean 		struct sk_buff *skb = enetc_tx_swbd_get_skb(tx_swbd);
4099d2b68ccSVladimir Oltean 
410d3982312SY.b. Lu 		if (unlikely(tx_swbd->check_wb)) {
411d3982312SY.b. Lu 			struct enetc_ndev_priv *priv = netdev_priv(ndev);
412d3982312SY.b. Lu 			union enetc_tx_bd *txbd;
413d3982312SY.b. Lu 
414d3982312SY.b. Lu 			txbd = ENETC_TXBD(*tx_ring, i);
415d3982312SY.b. Lu 
416d3982312SY.b. Lu 			if (txbd->flags & ENETC_TXBD_FLAGS_W &&
417d3982312SY.b. Lu 			    tx_swbd->do_tstamp) {
418d3982312SY.b. Lu 				enetc_get_tx_tstamp(&priv->si->hw, txbd,
419d3982312SY.b. Lu 						    &tstamp);
420d3982312SY.b. Lu 				do_tstamp = true;
421d3982312SY.b. Lu 			}
422d3982312SY.b. Lu 		}
423d3982312SY.b. Lu 
4247ed2bc80SVladimir Oltean 		if (tx_swbd->is_xdp_tx)
4257ed2bc80SVladimir Oltean 			enetc_recycle_xdp_tx_buff(tx_ring, tx_swbd);
4267ed2bc80SVladimir Oltean 		else if (likely(tx_swbd->dma))
427d4fd0404SClaudiu Manoil 			enetc_unmap_tx_buff(tx_ring, tx_swbd);
428f4a0be84SClaudiu Manoil 
4299d2b68ccSVladimir Oltean 		if (xdp_frame) {
4309d2b68ccSVladimir Oltean 			xdp_return_frame(xdp_frame);
4319d2b68ccSVladimir Oltean 			tx_swbd->xdp_frame = NULL;
4329d2b68ccSVladimir Oltean 		} else if (skb) {
433d3982312SY.b. Lu 			if (unlikely(do_tstamp)) {
4349d2b68ccSVladimir Oltean 				enetc_tstamp_tx(skb, tstamp);
435d3982312SY.b. Lu 				do_tstamp = false;
436d3982312SY.b. Lu 			}
4379d2b68ccSVladimir Oltean 			napi_consume_skb(skb, napi_budget);
438d4fd0404SClaudiu Manoil 			tx_swbd->skb = NULL;
439d4fd0404SClaudiu Manoil 		}
440d4fd0404SClaudiu Manoil 
441d4fd0404SClaudiu Manoil 		tx_byte_cnt += tx_swbd->len;
4421ee8d6f3SVladimir Oltean 		/* Scrub the swbd here so we don't have to do that
4431ee8d6f3SVladimir Oltean 		 * when we reuse it during xmit
4441ee8d6f3SVladimir Oltean 		 */
4451ee8d6f3SVladimir Oltean 		memset(tx_swbd, 0, sizeof(*tx_swbd));
446d4fd0404SClaudiu Manoil 
447d4fd0404SClaudiu Manoil 		bds_to_clean--;
448d4fd0404SClaudiu Manoil 		tx_swbd++;
449d4fd0404SClaudiu Manoil 		i++;
450d4fd0404SClaudiu Manoil 		if (unlikely(i == tx_ring->bd_count)) {
451d4fd0404SClaudiu Manoil 			i = 0;
452d4fd0404SClaudiu Manoil 			tx_swbd = tx_ring->tx_swbd;
453d4fd0404SClaudiu Manoil 		}
454d4fd0404SClaudiu Manoil 
455d4fd0404SClaudiu Manoil 		/* BD iteration loop end */
456d504498dSVladimir Oltean 		if (tx_swbd->is_eof) {
457d4fd0404SClaudiu Manoil 			tx_frm_cnt++;
458d4fd0404SClaudiu Manoil 			/* re-arm interrupt source */
459fd5736bfSAlex Marginean 			enetc_wr_reg_hot(tx_ring->idr, BIT(tx_ring->index) |
460d4fd0404SClaudiu Manoil 					 BIT(16 + tx_ring->index));
461d4fd0404SClaudiu Manoil 		}
462d4fd0404SClaudiu Manoil 
463d4fd0404SClaudiu Manoil 		if (unlikely(!bds_to_clean))
464d4fd0404SClaudiu Manoil 			bds_to_clean = enetc_bd_ready_count(tx_ring, i);
465d4fd0404SClaudiu Manoil 	}
466d4fd0404SClaudiu Manoil 
467d4fd0404SClaudiu Manoil 	tx_ring->next_to_clean = i;
468d4fd0404SClaudiu Manoil 	tx_ring->stats.packets += tx_frm_cnt;
469d4fd0404SClaudiu Manoil 	tx_ring->stats.bytes += tx_byte_cnt;
470d4fd0404SClaudiu Manoil 
471d4fd0404SClaudiu Manoil 	if (unlikely(tx_frm_cnt && netif_carrier_ok(ndev) &&
472d4fd0404SClaudiu Manoil 		     __netif_subqueue_stopped(ndev, tx_ring->index) &&
473d4fd0404SClaudiu Manoil 		     (enetc_bd_unused(tx_ring) >= ENETC_TXBDS_MAX_NEEDED))) {
474d4fd0404SClaudiu Manoil 		netif_wake_subqueue(ndev, tx_ring->index);
475d4fd0404SClaudiu Manoil 	}
476d4fd0404SClaudiu Manoil 
477d4fd0404SClaudiu Manoil 	return tx_frm_cnt != ENETC_DEFAULT_TX_WORK;
478d4fd0404SClaudiu Manoil }
479d4fd0404SClaudiu Manoil 
480d4fd0404SClaudiu Manoil static bool enetc_new_page(struct enetc_bdr *rx_ring,
481d4fd0404SClaudiu Manoil 			   struct enetc_rx_swbd *rx_swbd)
482d4fd0404SClaudiu Manoil {
4837ed2bc80SVladimir Oltean 	bool xdp = !!(rx_ring->xdp.prog);
484d4fd0404SClaudiu Manoil 	struct page *page;
485d4fd0404SClaudiu Manoil 	dma_addr_t addr;
486d4fd0404SClaudiu Manoil 
487d4fd0404SClaudiu Manoil 	page = dev_alloc_page();
488d4fd0404SClaudiu Manoil 	if (unlikely(!page))
489d4fd0404SClaudiu Manoil 		return false;
490d4fd0404SClaudiu Manoil 
4917ed2bc80SVladimir Oltean 	/* For XDP_TX, we forgo dma_unmap -> dma_map */
4927ed2bc80SVladimir Oltean 	rx_swbd->dir = xdp ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
4937ed2bc80SVladimir Oltean 
4947ed2bc80SVladimir Oltean 	addr = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, rx_swbd->dir);
495d4fd0404SClaudiu Manoil 	if (unlikely(dma_mapping_error(rx_ring->dev, addr))) {
496d4fd0404SClaudiu Manoil 		__free_page(page);
497d4fd0404SClaudiu Manoil 
498d4fd0404SClaudiu Manoil 		return false;
499d4fd0404SClaudiu Manoil 	}
500d4fd0404SClaudiu Manoil 
501d4fd0404SClaudiu Manoil 	rx_swbd->dma = addr;
502d4fd0404SClaudiu Manoil 	rx_swbd->page = page;
503d1b15102SVladimir Oltean 	rx_swbd->page_offset = rx_ring->buffer_offset;
504d4fd0404SClaudiu Manoil 
505d4fd0404SClaudiu Manoil 	return true;
506d4fd0404SClaudiu Manoil }
507d4fd0404SClaudiu Manoil 
508d4fd0404SClaudiu Manoil static int enetc_refill_rx_ring(struct enetc_bdr *rx_ring, const int buff_cnt)
509d4fd0404SClaudiu Manoil {
510d4fd0404SClaudiu Manoil 	struct enetc_rx_swbd *rx_swbd;
511d4fd0404SClaudiu Manoil 	union enetc_rx_bd *rxbd;
512d4fd0404SClaudiu Manoil 	int i, j;
513d4fd0404SClaudiu Manoil 
514d4fd0404SClaudiu Manoil 	i = rx_ring->next_to_use;
515d4fd0404SClaudiu Manoil 	rx_swbd = &rx_ring->rx_swbd[i];
516714239acSClaudiu Manoil 	rxbd = enetc_rxbd(rx_ring, i);
517d4fd0404SClaudiu Manoil 
518d4fd0404SClaudiu Manoil 	for (j = 0; j < buff_cnt; j++) {
519d4fd0404SClaudiu Manoil 		/* try reuse page */
520d4fd0404SClaudiu Manoil 		if (unlikely(!rx_swbd->page)) {
521d4fd0404SClaudiu Manoil 			if (unlikely(!enetc_new_page(rx_ring, rx_swbd))) {
522d4fd0404SClaudiu Manoil 				rx_ring->stats.rx_alloc_errs++;
523d4fd0404SClaudiu Manoil 				break;
524d4fd0404SClaudiu Manoil 			}
525d4fd0404SClaudiu Manoil 		}
526d4fd0404SClaudiu Manoil 
527d4fd0404SClaudiu Manoil 		/* update RxBD */
528d4fd0404SClaudiu Manoil 		rxbd->w.addr = cpu_to_le64(rx_swbd->dma +
529d4fd0404SClaudiu Manoil 					   rx_swbd->page_offset);
530d4fd0404SClaudiu Manoil 		/* clear 'R" as well */
531d4fd0404SClaudiu Manoil 		rxbd->r.lstatus = 0;
532d4fd0404SClaudiu Manoil 
533c027aa92SVladimir Oltean 		enetc_rxbd_next(rx_ring, &rxbd, &i);
534c027aa92SVladimir Oltean 		rx_swbd = &rx_ring->rx_swbd[i];
535d4fd0404SClaudiu Manoil 	}
536d4fd0404SClaudiu Manoil 
537d4fd0404SClaudiu Manoil 	if (likely(j)) {
538d4fd0404SClaudiu Manoil 		rx_ring->next_to_alloc = i; /* keep track from page reuse */
539d4fd0404SClaudiu Manoil 		rx_ring->next_to_use = i;
5407a5222cbSVladimir Oltean 
5417a5222cbSVladimir Oltean 		/* update ENETC's consumer index */
5427a5222cbSVladimir Oltean 		enetc_wr_reg_hot(rx_ring->rcir, rx_ring->next_to_use);
543d4fd0404SClaudiu Manoil 	}
544d4fd0404SClaudiu Manoil 
545d4fd0404SClaudiu Manoil 	return j;
546d4fd0404SClaudiu Manoil }
547d4fd0404SClaudiu Manoil 
548434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
549d3982312SY.b. Lu static void enetc_get_rx_tstamp(struct net_device *ndev,
550d3982312SY.b. Lu 				union enetc_rx_bd *rxbd,
551d3982312SY.b. Lu 				struct sk_buff *skb)
552d3982312SY.b. Lu {
553d3982312SY.b. Lu 	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
554d3982312SY.b. Lu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
555d3982312SY.b. Lu 	struct enetc_hw *hw = &priv->si->hw;
556cec4f328SY.b. Lu 	u32 lo, hi, tstamp_lo;
557d3982312SY.b. Lu 	u64 tstamp;
558d3982312SY.b. Lu 
559cec4f328SY.b. Lu 	if (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_TSTMP) {
560fd5736bfSAlex Marginean 		lo = enetc_rd_reg_hot(hw->reg + ENETC_SICTR0);
561fd5736bfSAlex Marginean 		hi = enetc_rd_reg_hot(hw->reg + ENETC_SICTR1);
562434cebabSClaudiu Manoil 		rxbd = enetc_rxbd_ext(rxbd);
563434cebabSClaudiu Manoil 		tstamp_lo = le32_to_cpu(rxbd->ext.tstamp);
564cec4f328SY.b. Lu 		if (lo <= tstamp_lo)
565d3982312SY.b. Lu 			hi -= 1;
566d3982312SY.b. Lu 
567cec4f328SY.b. Lu 		tstamp = (u64)hi << 32 | tstamp_lo;
568d3982312SY.b. Lu 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
569d3982312SY.b. Lu 		shhwtstamps->hwtstamp = ns_to_ktime(tstamp);
570d3982312SY.b. Lu 	}
571d3982312SY.b. Lu }
572d3982312SY.b. Lu #endif
573d3982312SY.b. Lu 
574d4fd0404SClaudiu Manoil static void enetc_get_offloads(struct enetc_bdr *rx_ring,
575d4fd0404SClaudiu Manoil 			       union enetc_rx_bd *rxbd, struct sk_buff *skb)
576d4fd0404SClaudiu Manoil {
577d3982312SY.b. Lu 	struct enetc_ndev_priv *priv = netdev_priv(rx_ring->ndev);
578827b6fd0SVladimir Oltean 
579d3982312SY.b. Lu 	/* TODO: hashing */
580d4fd0404SClaudiu Manoil 	if (rx_ring->ndev->features & NETIF_F_RXCSUM) {
581d4fd0404SClaudiu Manoil 		u16 inet_csum = le16_to_cpu(rxbd->r.inet_csum);
582d4fd0404SClaudiu Manoil 
583d4fd0404SClaudiu Manoil 		skb->csum = csum_unfold((__force __sum16)~htons(inet_csum));
584d4fd0404SClaudiu Manoil 		skb->ip_summed = CHECKSUM_COMPLETE;
585d4fd0404SClaudiu Manoil 	}
586d4fd0404SClaudiu Manoil 
587827b6fd0SVladimir Oltean 	if (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_VLAN) {
588827b6fd0SVladimir Oltean 		__be16 tpid = 0;
589827b6fd0SVladimir Oltean 
590827b6fd0SVladimir Oltean 		switch (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_TPID) {
591827b6fd0SVladimir Oltean 		case 0:
592827b6fd0SVladimir Oltean 			tpid = htons(ETH_P_8021Q);
593827b6fd0SVladimir Oltean 			break;
594827b6fd0SVladimir Oltean 		case 1:
595827b6fd0SVladimir Oltean 			tpid = htons(ETH_P_8021AD);
596827b6fd0SVladimir Oltean 			break;
597827b6fd0SVladimir Oltean 		case 2:
598827b6fd0SVladimir Oltean 			tpid = htons(enetc_port_rd(&priv->si->hw,
599827b6fd0SVladimir Oltean 						   ENETC_PCVLANR1));
600827b6fd0SVladimir Oltean 			break;
601827b6fd0SVladimir Oltean 		case 3:
602827b6fd0SVladimir Oltean 			tpid = htons(enetc_port_rd(&priv->si->hw,
603827b6fd0SVladimir Oltean 						   ENETC_PCVLANR2));
604827b6fd0SVladimir Oltean 			break;
605827b6fd0SVladimir Oltean 		default:
606827b6fd0SVladimir Oltean 			break;
607827b6fd0SVladimir Oltean 		}
608827b6fd0SVladimir Oltean 
609827b6fd0SVladimir Oltean 		__vlan_hwaccel_put_tag(skb, tpid, le16_to_cpu(rxbd->r.vlan_opt));
610827b6fd0SVladimir Oltean 	}
611827b6fd0SVladimir Oltean 
612434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
613d3982312SY.b. Lu 	if (priv->active_offloads & ENETC_F_RX_TSTAMP)
614d3982312SY.b. Lu 		enetc_get_rx_tstamp(rx_ring->ndev, rxbd, skb);
615d3982312SY.b. Lu #endif
616d4fd0404SClaudiu Manoil }
617d4fd0404SClaudiu Manoil 
6187ed2bc80SVladimir Oltean /* This gets called during the non-XDP NAPI poll cycle as well as on XDP_PASS,
6197ed2bc80SVladimir Oltean  * so it needs to work with both DMA_FROM_DEVICE as well as DMA_BIDIRECTIONAL
6207ed2bc80SVladimir Oltean  * mapped buffers.
6217ed2bc80SVladimir Oltean  */
622d4fd0404SClaudiu Manoil static struct enetc_rx_swbd *enetc_get_rx_buff(struct enetc_bdr *rx_ring,
623d4fd0404SClaudiu Manoil 					       int i, u16 size)
624d4fd0404SClaudiu Manoil {
625d4fd0404SClaudiu Manoil 	struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[i];
626d4fd0404SClaudiu Manoil 
627d4fd0404SClaudiu Manoil 	dma_sync_single_range_for_cpu(rx_ring->dev, rx_swbd->dma,
628d4fd0404SClaudiu Manoil 				      rx_swbd->page_offset,
6297ed2bc80SVladimir Oltean 				      size, rx_swbd->dir);
630d4fd0404SClaudiu Manoil 	return rx_swbd;
631d4fd0404SClaudiu Manoil }
632d4fd0404SClaudiu Manoil 
633d4fd0404SClaudiu Manoil static void enetc_put_rx_buff(struct enetc_bdr *rx_ring,
634d4fd0404SClaudiu Manoil 			      struct enetc_rx_swbd *rx_swbd)
635d4fd0404SClaudiu Manoil {
636d4fd0404SClaudiu Manoil 	if (likely(enetc_page_reusable(rx_swbd->page))) {
637d1b15102SVladimir Oltean 		size_t buffer_size = ENETC_RXB_TRUESIZE - rx_ring->buffer_offset;
638d1b15102SVladimir Oltean 
639d4fd0404SClaudiu Manoil 		rx_swbd->page_offset ^= ENETC_RXB_TRUESIZE;
640d4fd0404SClaudiu Manoil 		page_ref_inc(rx_swbd->page);
641d4fd0404SClaudiu Manoil 
642d4fd0404SClaudiu Manoil 		enetc_reuse_page(rx_ring, rx_swbd);
643d4fd0404SClaudiu Manoil 
644d4fd0404SClaudiu Manoil 		/* sync for use by the device */
645d4fd0404SClaudiu Manoil 		dma_sync_single_range_for_device(rx_ring->dev, rx_swbd->dma,
646d4fd0404SClaudiu Manoil 						 rx_swbd->page_offset,
6477ed2bc80SVladimir Oltean 						 buffer_size, rx_swbd->dir);
648d4fd0404SClaudiu Manoil 	} else {
6497ed2bc80SVladimir Oltean 		dma_unmap_page(rx_ring->dev, rx_swbd->dma, PAGE_SIZE,
6507ed2bc80SVladimir Oltean 			       rx_swbd->dir);
651d4fd0404SClaudiu Manoil 	}
652d4fd0404SClaudiu Manoil 
653d4fd0404SClaudiu Manoil 	rx_swbd->page = NULL;
654d4fd0404SClaudiu Manoil }
655d4fd0404SClaudiu Manoil 
656d4fd0404SClaudiu Manoil static struct sk_buff *enetc_map_rx_buff_to_skb(struct enetc_bdr *rx_ring,
657d4fd0404SClaudiu Manoil 						int i, u16 size)
658d4fd0404SClaudiu Manoil {
659d4fd0404SClaudiu Manoil 	struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size);
660d4fd0404SClaudiu Manoil 	struct sk_buff *skb;
661d4fd0404SClaudiu Manoil 	void *ba;
662d4fd0404SClaudiu Manoil 
663d4fd0404SClaudiu Manoil 	ba = page_address(rx_swbd->page) + rx_swbd->page_offset;
664d1b15102SVladimir Oltean 	skb = build_skb(ba - rx_ring->buffer_offset, ENETC_RXB_TRUESIZE);
665d4fd0404SClaudiu Manoil 	if (unlikely(!skb)) {
666d4fd0404SClaudiu Manoil 		rx_ring->stats.rx_alloc_errs++;
667d4fd0404SClaudiu Manoil 		return NULL;
668d4fd0404SClaudiu Manoil 	}
669d4fd0404SClaudiu Manoil 
670d1b15102SVladimir Oltean 	skb_reserve(skb, rx_ring->buffer_offset);
671d4fd0404SClaudiu Manoil 	__skb_put(skb, size);
672d4fd0404SClaudiu Manoil 
673d4fd0404SClaudiu Manoil 	enetc_put_rx_buff(rx_ring, rx_swbd);
674d4fd0404SClaudiu Manoil 
675d4fd0404SClaudiu Manoil 	return skb;
676d4fd0404SClaudiu Manoil }
677d4fd0404SClaudiu Manoil 
678d4fd0404SClaudiu Manoil static void enetc_add_rx_buff_to_skb(struct enetc_bdr *rx_ring, int i,
679d4fd0404SClaudiu Manoil 				     u16 size, struct sk_buff *skb)
680d4fd0404SClaudiu Manoil {
681d4fd0404SClaudiu Manoil 	struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size);
682d4fd0404SClaudiu Manoil 
683d4fd0404SClaudiu Manoil 	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_swbd->page,
684d4fd0404SClaudiu Manoil 			rx_swbd->page_offset, size, ENETC_RXB_TRUESIZE);
685d4fd0404SClaudiu Manoil 
686d4fd0404SClaudiu Manoil 	enetc_put_rx_buff(rx_ring, rx_swbd);
687d4fd0404SClaudiu Manoil }
688d4fd0404SClaudiu Manoil 
6892fa423f5SVladimir Oltean static bool enetc_check_bd_errors_and_consume(struct enetc_bdr *rx_ring,
6902fa423f5SVladimir Oltean 					      u32 bd_status,
6912fa423f5SVladimir Oltean 					      union enetc_rx_bd **rxbd, int *i)
6922fa423f5SVladimir Oltean {
6932fa423f5SVladimir Oltean 	if (likely(!(bd_status & ENETC_RXBD_LSTATUS(ENETC_RXBD_ERR_MASK))))
6942fa423f5SVladimir Oltean 		return false;
6952fa423f5SVladimir Oltean 
6962fa423f5SVladimir Oltean 	enetc_rxbd_next(rx_ring, rxbd, i);
6972fa423f5SVladimir Oltean 
6982fa423f5SVladimir Oltean 	while (!(bd_status & ENETC_RXBD_LSTATUS_F)) {
6992fa423f5SVladimir Oltean 		dma_rmb();
7002fa423f5SVladimir Oltean 		bd_status = le32_to_cpu((*rxbd)->r.lstatus);
7012fa423f5SVladimir Oltean 
7022fa423f5SVladimir Oltean 		enetc_rxbd_next(rx_ring, rxbd, i);
7032fa423f5SVladimir Oltean 	}
7042fa423f5SVladimir Oltean 
7052fa423f5SVladimir Oltean 	rx_ring->ndev->stats.rx_dropped++;
7062fa423f5SVladimir Oltean 	rx_ring->ndev->stats.rx_errors++;
7072fa423f5SVladimir Oltean 
7082fa423f5SVladimir Oltean 	return true;
7092fa423f5SVladimir Oltean }
7102fa423f5SVladimir Oltean 
711a800abd3SVladimir Oltean static struct sk_buff *enetc_build_skb(struct enetc_bdr *rx_ring,
712a800abd3SVladimir Oltean 				       u32 bd_status, union enetc_rx_bd **rxbd,
713d1b15102SVladimir Oltean 				       int *i, int *cleaned_cnt, int buffer_size)
714a800abd3SVladimir Oltean {
715a800abd3SVladimir Oltean 	struct sk_buff *skb;
716a800abd3SVladimir Oltean 	u16 size;
717a800abd3SVladimir Oltean 
718a800abd3SVladimir Oltean 	size = le16_to_cpu((*rxbd)->r.buf_len);
719a800abd3SVladimir Oltean 	skb = enetc_map_rx_buff_to_skb(rx_ring, *i, size);
720a800abd3SVladimir Oltean 	if (!skb)
721a800abd3SVladimir Oltean 		return NULL;
722a800abd3SVladimir Oltean 
723a800abd3SVladimir Oltean 	enetc_get_offloads(rx_ring, *rxbd, skb);
724a800abd3SVladimir Oltean 
725a800abd3SVladimir Oltean 	(*cleaned_cnt)++;
726a800abd3SVladimir Oltean 
727a800abd3SVladimir Oltean 	enetc_rxbd_next(rx_ring, rxbd, i);
728a800abd3SVladimir Oltean 
729a800abd3SVladimir Oltean 	/* not last BD in frame? */
730a800abd3SVladimir Oltean 	while (!(bd_status & ENETC_RXBD_LSTATUS_F)) {
731a800abd3SVladimir Oltean 		bd_status = le32_to_cpu((*rxbd)->r.lstatus);
732d1b15102SVladimir Oltean 		size = buffer_size;
733a800abd3SVladimir Oltean 
734a800abd3SVladimir Oltean 		if (bd_status & ENETC_RXBD_LSTATUS_F) {
735a800abd3SVladimir Oltean 			dma_rmb();
736a800abd3SVladimir Oltean 			size = le16_to_cpu((*rxbd)->r.buf_len);
737a800abd3SVladimir Oltean 		}
738a800abd3SVladimir Oltean 
739a800abd3SVladimir Oltean 		enetc_add_rx_buff_to_skb(rx_ring, *i, size, skb);
740a800abd3SVladimir Oltean 
741a800abd3SVladimir Oltean 		(*cleaned_cnt)++;
742a800abd3SVladimir Oltean 
743a800abd3SVladimir Oltean 		enetc_rxbd_next(rx_ring, rxbd, i);
744a800abd3SVladimir Oltean 	}
745a800abd3SVladimir Oltean 
746a800abd3SVladimir Oltean 	skb_record_rx_queue(skb, rx_ring->index);
747a800abd3SVladimir Oltean 	skb->protocol = eth_type_trans(skb, rx_ring->ndev);
748a800abd3SVladimir Oltean 
749a800abd3SVladimir Oltean 	return skb;
750a800abd3SVladimir Oltean }
751a800abd3SVladimir Oltean 
752d4fd0404SClaudiu Manoil #define ENETC_RXBD_BUNDLE 16 /* # of BDs to update at once */
753d4fd0404SClaudiu Manoil 
754d4fd0404SClaudiu Manoil static int enetc_clean_rx_ring(struct enetc_bdr *rx_ring,
755d4fd0404SClaudiu Manoil 			       struct napi_struct *napi, int work_limit)
756d4fd0404SClaudiu Manoil {
757d4fd0404SClaudiu Manoil 	int rx_frm_cnt = 0, rx_byte_cnt = 0;
758d4fd0404SClaudiu Manoil 	int cleaned_cnt, i;
759d4fd0404SClaudiu Manoil 
760d4fd0404SClaudiu Manoil 	cleaned_cnt = enetc_bd_unused(rx_ring);
761d4fd0404SClaudiu Manoil 	/* next descriptor to process */
762d4fd0404SClaudiu Manoil 	i = rx_ring->next_to_clean;
763d4fd0404SClaudiu Manoil 
764d4fd0404SClaudiu Manoil 	while (likely(rx_frm_cnt < work_limit)) {
765d4fd0404SClaudiu Manoil 		union enetc_rx_bd *rxbd;
766d4fd0404SClaudiu Manoil 		struct sk_buff *skb;
767d4fd0404SClaudiu Manoil 		u32 bd_status;
768d4fd0404SClaudiu Manoil 
7697a5222cbSVladimir Oltean 		if (cleaned_cnt >= ENETC_RXBD_BUNDLE)
7707a5222cbSVladimir Oltean 			cleaned_cnt -= enetc_refill_rx_ring(rx_ring,
7717a5222cbSVladimir Oltean 							    cleaned_cnt);
772d4fd0404SClaudiu Manoil 
773714239acSClaudiu Manoil 		rxbd = enetc_rxbd(rx_ring, i);
774d4fd0404SClaudiu Manoil 		bd_status = le32_to_cpu(rxbd->r.lstatus);
7756d36ecdbSVladimir Oltean 		if (!bd_status)
776d4fd0404SClaudiu Manoil 			break;
777d4fd0404SClaudiu Manoil 
778fd5736bfSAlex Marginean 		enetc_wr_reg_hot(rx_ring->idr, BIT(rx_ring->index));
779d4fd0404SClaudiu Manoil 		dma_rmb(); /* for reading other rxbd fields */
7802fa423f5SVladimir Oltean 
7812fa423f5SVladimir Oltean 		if (enetc_check_bd_errors_and_consume(rx_ring, bd_status,
7822fa423f5SVladimir Oltean 						      &rxbd, &i))
7832fa423f5SVladimir Oltean 			break;
7842fa423f5SVladimir Oltean 
785a800abd3SVladimir Oltean 		skb = enetc_build_skb(rx_ring, bd_status, &rxbd, &i,
786d1b15102SVladimir Oltean 				      &cleaned_cnt, ENETC_RXB_DMA_SIZE);
7876d36ecdbSVladimir Oltean 		if (!skb)
788d4fd0404SClaudiu Manoil 			break;
789d4fd0404SClaudiu Manoil 
790d4fd0404SClaudiu Manoil 		rx_byte_cnt += skb->len;
791a800abd3SVladimir Oltean 		rx_frm_cnt++;
792d4fd0404SClaudiu Manoil 
793d4fd0404SClaudiu Manoil 		napi_gro_receive(napi, skb);
794d4fd0404SClaudiu Manoil 	}
795d4fd0404SClaudiu Manoil 
796d4fd0404SClaudiu Manoil 	rx_ring->next_to_clean = i;
797d4fd0404SClaudiu Manoil 
798d4fd0404SClaudiu Manoil 	rx_ring->stats.packets += rx_frm_cnt;
799d4fd0404SClaudiu Manoil 	rx_ring->stats.bytes += rx_byte_cnt;
800d4fd0404SClaudiu Manoil 
801d4fd0404SClaudiu Manoil 	return rx_frm_cnt;
802d4fd0404SClaudiu Manoil }
803d4fd0404SClaudiu Manoil 
8047ed2bc80SVladimir Oltean static void enetc_xdp_map_tx_buff(struct enetc_bdr *tx_ring, int i,
8057ed2bc80SVladimir Oltean 				  struct enetc_tx_swbd *tx_swbd,
8067ed2bc80SVladimir Oltean 				  int frm_len)
8077ed2bc80SVladimir Oltean {
8087ed2bc80SVladimir Oltean 	union enetc_tx_bd *txbd = ENETC_TXBD(*tx_ring, i);
8097ed2bc80SVladimir Oltean 
8107ed2bc80SVladimir Oltean 	prefetchw(txbd);
8117ed2bc80SVladimir Oltean 
8127ed2bc80SVladimir Oltean 	enetc_clear_tx_bd(txbd);
8137ed2bc80SVladimir Oltean 	txbd->addr = cpu_to_le64(tx_swbd->dma + tx_swbd->page_offset);
8147ed2bc80SVladimir Oltean 	txbd->buf_len = cpu_to_le16(tx_swbd->len);
8157ed2bc80SVladimir Oltean 	txbd->frm_len = cpu_to_le16(frm_len);
8167ed2bc80SVladimir Oltean 
8177ed2bc80SVladimir Oltean 	memcpy(&tx_ring->tx_swbd[i], tx_swbd, sizeof(*tx_swbd));
8187ed2bc80SVladimir Oltean }
8197ed2bc80SVladimir Oltean 
8207ed2bc80SVladimir Oltean /* Puts in the TX ring one XDP frame, mapped as an array of TX software buffer
8217ed2bc80SVladimir Oltean  * descriptors.
8227ed2bc80SVladimir Oltean  */
8237ed2bc80SVladimir Oltean static bool enetc_xdp_tx(struct enetc_bdr *tx_ring,
8247ed2bc80SVladimir Oltean 			 struct enetc_tx_swbd *xdp_tx_arr, int num_tx_swbd)
8257ed2bc80SVladimir Oltean {
8267ed2bc80SVladimir Oltean 	struct enetc_tx_swbd *tmp_tx_swbd = xdp_tx_arr;
8277ed2bc80SVladimir Oltean 	int i, k, frm_len = tmp_tx_swbd->len;
8287ed2bc80SVladimir Oltean 
8297ed2bc80SVladimir Oltean 	if (unlikely(enetc_bd_unused(tx_ring) < ENETC_TXBDS_NEEDED(num_tx_swbd)))
8307ed2bc80SVladimir Oltean 		return false;
8317ed2bc80SVladimir Oltean 
8327ed2bc80SVladimir Oltean 	while (unlikely(!tmp_tx_swbd->is_eof)) {
8337ed2bc80SVladimir Oltean 		tmp_tx_swbd++;
8347ed2bc80SVladimir Oltean 		frm_len += tmp_tx_swbd->len;
8357ed2bc80SVladimir Oltean 	}
8367ed2bc80SVladimir Oltean 
8377ed2bc80SVladimir Oltean 	i = tx_ring->next_to_use;
8387ed2bc80SVladimir Oltean 
8397ed2bc80SVladimir Oltean 	for (k = 0; k < num_tx_swbd; k++) {
8407ed2bc80SVladimir Oltean 		struct enetc_tx_swbd *xdp_tx_swbd = &xdp_tx_arr[k];
8417ed2bc80SVladimir Oltean 
8427ed2bc80SVladimir Oltean 		enetc_xdp_map_tx_buff(tx_ring, i, xdp_tx_swbd, frm_len);
8437ed2bc80SVladimir Oltean 
8447ed2bc80SVladimir Oltean 		/* last BD needs 'F' bit set */
8457ed2bc80SVladimir Oltean 		if (xdp_tx_swbd->is_eof) {
8467ed2bc80SVladimir Oltean 			union enetc_tx_bd *txbd = ENETC_TXBD(*tx_ring, i);
8477ed2bc80SVladimir Oltean 
8487ed2bc80SVladimir Oltean 			txbd->flags = ENETC_TXBD_FLAGS_F;
8497ed2bc80SVladimir Oltean 		}
8507ed2bc80SVladimir Oltean 
8517ed2bc80SVladimir Oltean 		enetc_bdr_idx_inc(tx_ring, &i);
8527ed2bc80SVladimir Oltean 	}
8537ed2bc80SVladimir Oltean 
8547ed2bc80SVladimir Oltean 	tx_ring->next_to_use = i;
8557ed2bc80SVladimir Oltean 
8567ed2bc80SVladimir Oltean 	return true;
8577ed2bc80SVladimir Oltean }
8587ed2bc80SVladimir Oltean 
8599d2b68ccSVladimir Oltean static int enetc_xdp_frame_to_xdp_tx_swbd(struct enetc_bdr *tx_ring,
8609d2b68ccSVladimir Oltean 					  struct enetc_tx_swbd *xdp_tx_arr,
8619d2b68ccSVladimir Oltean 					  struct xdp_frame *xdp_frame)
8629d2b68ccSVladimir Oltean {
8639d2b68ccSVladimir Oltean 	struct enetc_tx_swbd *xdp_tx_swbd = &xdp_tx_arr[0];
8649d2b68ccSVladimir Oltean 	struct skb_shared_info *shinfo;
8659d2b68ccSVladimir Oltean 	void *data = xdp_frame->data;
8669d2b68ccSVladimir Oltean 	int len = xdp_frame->len;
8679d2b68ccSVladimir Oltean 	skb_frag_t *frag;
8689d2b68ccSVladimir Oltean 	dma_addr_t dma;
8699d2b68ccSVladimir Oltean 	unsigned int f;
8709d2b68ccSVladimir Oltean 	int n = 0;
8719d2b68ccSVladimir Oltean 
8729d2b68ccSVladimir Oltean 	dma = dma_map_single(tx_ring->dev, data, len, DMA_TO_DEVICE);
8739d2b68ccSVladimir Oltean 	if (unlikely(dma_mapping_error(tx_ring->dev, dma))) {
8749d2b68ccSVladimir Oltean 		netdev_err(tx_ring->ndev, "DMA map error\n");
8759d2b68ccSVladimir Oltean 		return -1;
8769d2b68ccSVladimir Oltean 	}
8779d2b68ccSVladimir Oltean 
8789d2b68ccSVladimir Oltean 	xdp_tx_swbd->dma = dma;
8799d2b68ccSVladimir Oltean 	xdp_tx_swbd->dir = DMA_TO_DEVICE;
8809d2b68ccSVladimir Oltean 	xdp_tx_swbd->len = len;
8819d2b68ccSVladimir Oltean 	xdp_tx_swbd->is_xdp_redirect = true;
8829d2b68ccSVladimir Oltean 	xdp_tx_swbd->is_eof = false;
8839d2b68ccSVladimir Oltean 	xdp_tx_swbd->xdp_frame = NULL;
8849d2b68ccSVladimir Oltean 
8859d2b68ccSVladimir Oltean 	n++;
8869d2b68ccSVladimir Oltean 	xdp_tx_swbd = &xdp_tx_arr[n];
8879d2b68ccSVladimir Oltean 
8889d2b68ccSVladimir Oltean 	shinfo = xdp_get_shared_info_from_frame(xdp_frame);
8899d2b68ccSVladimir Oltean 
8909d2b68ccSVladimir Oltean 	for (f = 0, frag = &shinfo->frags[0]; f < shinfo->nr_frags;
8919d2b68ccSVladimir Oltean 	     f++, frag++) {
8929d2b68ccSVladimir Oltean 		data = skb_frag_address(frag);
8939d2b68ccSVladimir Oltean 		len = skb_frag_size(frag);
8949d2b68ccSVladimir Oltean 
8959d2b68ccSVladimir Oltean 		dma = dma_map_single(tx_ring->dev, data, len, DMA_TO_DEVICE);
8969d2b68ccSVladimir Oltean 		if (unlikely(dma_mapping_error(tx_ring->dev, dma))) {
8979d2b68ccSVladimir Oltean 			/* Undo the DMA mapping for all fragments */
898*626b598aSDan Carpenter 			while (--n >= 0)
8999d2b68ccSVladimir Oltean 				enetc_unmap_tx_buff(tx_ring, &xdp_tx_arr[n]);
9009d2b68ccSVladimir Oltean 
9019d2b68ccSVladimir Oltean 			netdev_err(tx_ring->ndev, "DMA map error\n");
9029d2b68ccSVladimir Oltean 			return -1;
9039d2b68ccSVladimir Oltean 		}
9049d2b68ccSVladimir Oltean 
9059d2b68ccSVladimir Oltean 		xdp_tx_swbd->dma = dma;
9069d2b68ccSVladimir Oltean 		xdp_tx_swbd->dir = DMA_TO_DEVICE;
9079d2b68ccSVladimir Oltean 		xdp_tx_swbd->len = len;
9089d2b68ccSVladimir Oltean 		xdp_tx_swbd->is_xdp_redirect = true;
9099d2b68ccSVladimir Oltean 		xdp_tx_swbd->is_eof = false;
9109d2b68ccSVladimir Oltean 		xdp_tx_swbd->xdp_frame = NULL;
9119d2b68ccSVladimir Oltean 
9129d2b68ccSVladimir Oltean 		n++;
9139d2b68ccSVladimir Oltean 		xdp_tx_swbd = &xdp_tx_arr[n];
9149d2b68ccSVladimir Oltean 	}
9159d2b68ccSVladimir Oltean 
9169d2b68ccSVladimir Oltean 	xdp_tx_arr[n - 1].is_eof = true;
9179d2b68ccSVladimir Oltean 	xdp_tx_arr[n - 1].xdp_frame = xdp_frame;
9189d2b68ccSVladimir Oltean 
9199d2b68ccSVladimir Oltean 	return n;
9209d2b68ccSVladimir Oltean }
9219d2b68ccSVladimir Oltean 
9229d2b68ccSVladimir Oltean int enetc_xdp_xmit(struct net_device *ndev, int num_frames,
9239d2b68ccSVladimir Oltean 		   struct xdp_frame **frames, u32 flags)
9249d2b68ccSVladimir Oltean {
9259d2b68ccSVladimir Oltean 	struct enetc_tx_swbd xdp_redirect_arr[ENETC_MAX_SKB_FRAGS] = {0};
9269d2b68ccSVladimir Oltean 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
9279d2b68ccSVladimir Oltean 	struct enetc_bdr *tx_ring;
9289d2b68ccSVladimir Oltean 	int xdp_tx_bd_cnt, i, k;
9299d2b68ccSVladimir Oltean 	int xdp_tx_frm_cnt = 0;
9309d2b68ccSVladimir Oltean 
9319d2b68ccSVladimir Oltean 	tx_ring = priv->tx_ring[smp_processor_id()];
9329d2b68ccSVladimir Oltean 
9339d2b68ccSVladimir Oltean 	prefetchw(ENETC_TXBD(*tx_ring, tx_ring->next_to_use));
9349d2b68ccSVladimir Oltean 
9359d2b68ccSVladimir Oltean 	for (k = 0; k < num_frames; k++) {
9369d2b68ccSVladimir Oltean 		xdp_tx_bd_cnt = enetc_xdp_frame_to_xdp_tx_swbd(tx_ring,
9379d2b68ccSVladimir Oltean 							       xdp_redirect_arr,
9389d2b68ccSVladimir Oltean 							       frames[k]);
9399d2b68ccSVladimir Oltean 		if (unlikely(xdp_tx_bd_cnt < 0))
9409d2b68ccSVladimir Oltean 			break;
9419d2b68ccSVladimir Oltean 
9429d2b68ccSVladimir Oltean 		if (unlikely(!enetc_xdp_tx(tx_ring, xdp_redirect_arr,
9439d2b68ccSVladimir Oltean 					   xdp_tx_bd_cnt))) {
9449d2b68ccSVladimir Oltean 			for (i = 0; i < xdp_tx_bd_cnt; i++)
9459d2b68ccSVladimir Oltean 				enetc_unmap_tx_buff(tx_ring,
9469d2b68ccSVladimir Oltean 						    &xdp_redirect_arr[i]);
9479d2b68ccSVladimir Oltean 			tx_ring->stats.xdp_tx_drops++;
9489d2b68ccSVladimir Oltean 			break;
9499d2b68ccSVladimir Oltean 		}
9509d2b68ccSVladimir Oltean 
9519d2b68ccSVladimir Oltean 		xdp_tx_frm_cnt++;
9529d2b68ccSVladimir Oltean 	}
9539d2b68ccSVladimir Oltean 
9549d2b68ccSVladimir Oltean 	if (unlikely((flags & XDP_XMIT_FLUSH) || k != xdp_tx_frm_cnt))
9559d2b68ccSVladimir Oltean 		enetc_update_tx_ring_tail(tx_ring);
9569d2b68ccSVladimir Oltean 
9579d2b68ccSVladimir Oltean 	tx_ring->stats.xdp_tx += xdp_tx_frm_cnt;
9589d2b68ccSVladimir Oltean 
9599d2b68ccSVladimir Oltean 	return xdp_tx_frm_cnt;
9609d2b68ccSVladimir Oltean }
9619d2b68ccSVladimir Oltean 
962d1b15102SVladimir Oltean static void enetc_map_rx_buff_to_xdp(struct enetc_bdr *rx_ring, int i,
963d1b15102SVladimir Oltean 				     struct xdp_buff *xdp_buff, u16 size)
964d1b15102SVladimir Oltean {
965d1b15102SVladimir Oltean 	struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size);
966d1b15102SVladimir Oltean 	void *hard_start = page_address(rx_swbd->page) + rx_swbd->page_offset;
967d1b15102SVladimir Oltean 	struct skb_shared_info *shinfo;
968d1b15102SVladimir Oltean 
9697ed2bc80SVladimir Oltean 	/* To be used for XDP_TX */
9707ed2bc80SVladimir Oltean 	rx_swbd->len = size;
9717ed2bc80SVladimir Oltean 
972d1b15102SVladimir Oltean 	xdp_prepare_buff(xdp_buff, hard_start - rx_ring->buffer_offset,
973d1b15102SVladimir Oltean 			 rx_ring->buffer_offset, size, false);
974d1b15102SVladimir Oltean 
975d1b15102SVladimir Oltean 	shinfo = xdp_get_shared_info_from_buff(xdp_buff);
976d1b15102SVladimir Oltean 	shinfo->nr_frags = 0;
977d1b15102SVladimir Oltean }
978d1b15102SVladimir Oltean 
979d1b15102SVladimir Oltean static void enetc_add_rx_buff_to_xdp(struct enetc_bdr *rx_ring, int i,
980d1b15102SVladimir Oltean 				     u16 size, struct xdp_buff *xdp_buff)
981d1b15102SVladimir Oltean {
982d1b15102SVladimir Oltean 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp_buff);
983d1b15102SVladimir Oltean 	struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size);
984d1b15102SVladimir Oltean 	skb_frag_t *frag = &shinfo->frags[shinfo->nr_frags];
985d1b15102SVladimir Oltean 
9867ed2bc80SVladimir Oltean 	/* To be used for XDP_TX */
9877ed2bc80SVladimir Oltean 	rx_swbd->len = size;
9887ed2bc80SVladimir Oltean 
989d1b15102SVladimir Oltean 	skb_frag_off_set(frag, rx_swbd->page_offset);
990d1b15102SVladimir Oltean 	skb_frag_size_set(frag, size);
991d1b15102SVladimir Oltean 	__skb_frag_set_page(frag, rx_swbd->page);
992d1b15102SVladimir Oltean 
993d1b15102SVladimir Oltean 	shinfo->nr_frags++;
994d1b15102SVladimir Oltean }
995d1b15102SVladimir Oltean 
996d1b15102SVladimir Oltean static void enetc_build_xdp_buff(struct enetc_bdr *rx_ring, u32 bd_status,
997d1b15102SVladimir Oltean 				 union enetc_rx_bd **rxbd, int *i,
998d1b15102SVladimir Oltean 				 int *cleaned_cnt, struct xdp_buff *xdp_buff)
999d1b15102SVladimir Oltean {
1000d1b15102SVladimir Oltean 	u16 size = le16_to_cpu((*rxbd)->r.buf_len);
1001d1b15102SVladimir Oltean 
1002d1b15102SVladimir Oltean 	xdp_init_buff(xdp_buff, ENETC_RXB_TRUESIZE, &rx_ring->xdp.rxq);
1003d1b15102SVladimir Oltean 
1004d1b15102SVladimir Oltean 	enetc_map_rx_buff_to_xdp(rx_ring, *i, xdp_buff, size);
1005d1b15102SVladimir Oltean 	(*cleaned_cnt)++;
1006d1b15102SVladimir Oltean 	enetc_rxbd_next(rx_ring, rxbd, i);
1007d1b15102SVladimir Oltean 
1008d1b15102SVladimir Oltean 	/* not last BD in frame? */
1009d1b15102SVladimir Oltean 	while (!(bd_status & ENETC_RXBD_LSTATUS_F)) {
1010d1b15102SVladimir Oltean 		bd_status = le32_to_cpu((*rxbd)->r.lstatus);
1011d1b15102SVladimir Oltean 		size = ENETC_RXB_DMA_SIZE_XDP;
1012d1b15102SVladimir Oltean 
1013d1b15102SVladimir Oltean 		if (bd_status & ENETC_RXBD_LSTATUS_F) {
1014d1b15102SVladimir Oltean 			dma_rmb();
1015d1b15102SVladimir Oltean 			size = le16_to_cpu((*rxbd)->r.buf_len);
1016d1b15102SVladimir Oltean 		}
1017d1b15102SVladimir Oltean 
1018d1b15102SVladimir Oltean 		enetc_add_rx_buff_to_xdp(rx_ring, *i, size, xdp_buff);
1019d1b15102SVladimir Oltean 		(*cleaned_cnt)++;
1020d1b15102SVladimir Oltean 		enetc_rxbd_next(rx_ring, rxbd, i);
1021d1b15102SVladimir Oltean 	}
1022d1b15102SVladimir Oltean }
1023d1b15102SVladimir Oltean 
1024d1b15102SVladimir Oltean /* Reuse the current page without performing half-page buffer flipping */
1025d1b15102SVladimir Oltean static void enetc_put_xdp_buff(struct enetc_bdr *rx_ring,
1026d1b15102SVladimir Oltean 			       struct enetc_rx_swbd *rx_swbd)
1027d1b15102SVladimir Oltean {
1028d1b15102SVladimir Oltean 	enetc_reuse_page(rx_ring, rx_swbd);
1029d1b15102SVladimir Oltean 
1030d1b15102SVladimir Oltean 	dma_sync_single_range_for_device(rx_ring->dev, rx_swbd->dma,
1031d1b15102SVladimir Oltean 					 rx_swbd->page_offset,
1032d1b15102SVladimir Oltean 					 ENETC_RXB_DMA_SIZE_XDP,
10337ed2bc80SVladimir Oltean 					 rx_swbd->dir);
1034d1b15102SVladimir Oltean 
1035d1b15102SVladimir Oltean 	rx_swbd->page = NULL;
1036d1b15102SVladimir Oltean }
1037d1b15102SVladimir Oltean 
10387ed2bc80SVladimir Oltean /* Convert RX buffer descriptors to TX buffer descriptors. These will be
10397ed2bc80SVladimir Oltean  * recycled back into the RX ring in enetc_clean_tx_ring. We need to scrub the
10407ed2bc80SVladimir Oltean  * RX software BDs because the ownership of the buffer no longer belongs to the
10417ed2bc80SVladimir Oltean  * RX ring, so enetc_refill_rx_ring may not reuse rx_swbd->page.
10427ed2bc80SVladimir Oltean  */
10437ed2bc80SVladimir Oltean static int enetc_rx_swbd_to_xdp_tx_swbd(struct enetc_tx_swbd *xdp_tx_arr,
10447ed2bc80SVladimir Oltean 					struct enetc_bdr *rx_ring,
10457ed2bc80SVladimir Oltean 					int rx_ring_first, int rx_ring_last)
10467ed2bc80SVladimir Oltean {
10477ed2bc80SVladimir Oltean 	int n = 0;
10487ed2bc80SVladimir Oltean 
10497ed2bc80SVladimir Oltean 	for (; rx_ring_first != rx_ring_last;
10507ed2bc80SVladimir Oltean 	     n++, enetc_bdr_idx_inc(rx_ring, &rx_ring_first)) {
10517ed2bc80SVladimir Oltean 		struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[rx_ring_first];
10527ed2bc80SVladimir Oltean 		struct enetc_tx_swbd *tx_swbd = &xdp_tx_arr[n];
10537ed2bc80SVladimir Oltean 
10547ed2bc80SVladimir Oltean 		/* No need to dma_map, we already have DMA_BIDIRECTIONAL */
10557ed2bc80SVladimir Oltean 		tx_swbd->dma = rx_swbd->dma;
10567ed2bc80SVladimir Oltean 		tx_swbd->dir = rx_swbd->dir;
10577ed2bc80SVladimir Oltean 		tx_swbd->page = rx_swbd->page;
10587ed2bc80SVladimir Oltean 		tx_swbd->page_offset = rx_swbd->page_offset;
10597ed2bc80SVladimir Oltean 		tx_swbd->len = rx_swbd->len;
10607ed2bc80SVladimir Oltean 		tx_swbd->is_dma_page = true;
10617ed2bc80SVladimir Oltean 		tx_swbd->is_xdp_tx = true;
10627ed2bc80SVladimir Oltean 		tx_swbd->is_eof = false;
10637ed2bc80SVladimir Oltean 		memset(rx_swbd, 0, sizeof(*rx_swbd));
10647ed2bc80SVladimir Oltean 	}
10657ed2bc80SVladimir Oltean 
10667ed2bc80SVladimir Oltean 	/* We rely on caller providing an rx_ring_last > rx_ring_first */
10677ed2bc80SVladimir Oltean 	xdp_tx_arr[n - 1].is_eof = true;
10687ed2bc80SVladimir Oltean 
10697ed2bc80SVladimir Oltean 	return n;
10707ed2bc80SVladimir Oltean }
10717ed2bc80SVladimir Oltean 
1072d1b15102SVladimir Oltean static void enetc_xdp_drop(struct enetc_bdr *rx_ring, int rx_ring_first,
1073d1b15102SVladimir Oltean 			   int rx_ring_last)
1074d1b15102SVladimir Oltean {
1075d1b15102SVladimir Oltean 	while (rx_ring_first != rx_ring_last) {
1076d1b15102SVladimir Oltean 		enetc_put_xdp_buff(rx_ring,
1077d1b15102SVladimir Oltean 				   &rx_ring->rx_swbd[rx_ring_first]);
1078d1b15102SVladimir Oltean 		enetc_bdr_idx_inc(rx_ring, &rx_ring_first);
1079d1b15102SVladimir Oltean 	}
1080d1b15102SVladimir Oltean 	rx_ring->stats.xdp_drops++;
1081d1b15102SVladimir Oltean }
1082d1b15102SVladimir Oltean 
10839d2b68ccSVladimir Oltean static void enetc_xdp_free(struct enetc_bdr *rx_ring, int rx_ring_first,
10849d2b68ccSVladimir Oltean 			   int rx_ring_last)
10859d2b68ccSVladimir Oltean {
10869d2b68ccSVladimir Oltean 	while (rx_ring_first != rx_ring_last) {
10879d2b68ccSVladimir Oltean 		struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[rx_ring_first];
10889d2b68ccSVladimir Oltean 
10899d2b68ccSVladimir Oltean 		if (rx_swbd->page) {
10909d2b68ccSVladimir Oltean 			dma_unmap_page(rx_ring->dev, rx_swbd->dma, PAGE_SIZE,
10919d2b68ccSVladimir Oltean 				       rx_swbd->dir);
10929d2b68ccSVladimir Oltean 			__free_page(rx_swbd->page);
10939d2b68ccSVladimir Oltean 			rx_swbd->page = NULL;
10949d2b68ccSVladimir Oltean 		}
10959d2b68ccSVladimir Oltean 		enetc_bdr_idx_inc(rx_ring, &rx_ring_first);
10969d2b68ccSVladimir Oltean 	}
10979d2b68ccSVladimir Oltean 	rx_ring->stats.xdp_redirect_failures++;
10989d2b68ccSVladimir Oltean }
10999d2b68ccSVladimir Oltean 
1100d1b15102SVladimir Oltean static int enetc_clean_rx_ring_xdp(struct enetc_bdr *rx_ring,
1101d1b15102SVladimir Oltean 				   struct napi_struct *napi, int work_limit,
1102d1b15102SVladimir Oltean 				   struct bpf_prog *prog)
1103d1b15102SVladimir Oltean {
11049d2b68ccSVladimir Oltean 	int xdp_tx_bd_cnt, xdp_tx_frm_cnt = 0, xdp_redirect_frm_cnt = 0;
11057ed2bc80SVladimir Oltean 	struct enetc_tx_swbd xdp_tx_arr[ENETC_MAX_SKB_FRAGS] = {0};
11067ed2bc80SVladimir Oltean 	struct enetc_ndev_priv *priv = netdev_priv(rx_ring->ndev);
11077ed2bc80SVladimir Oltean 	struct enetc_bdr *tx_ring = priv->tx_ring[rx_ring->index];
1108d1b15102SVladimir Oltean 	int rx_frm_cnt = 0, rx_byte_cnt = 0;
1109d1b15102SVladimir Oltean 	int cleaned_cnt, i;
1110d1b15102SVladimir Oltean 	u32 xdp_act;
1111d1b15102SVladimir Oltean 
1112d1b15102SVladimir Oltean 	cleaned_cnt = enetc_bd_unused(rx_ring);
1113d1b15102SVladimir Oltean 	/* next descriptor to process */
1114d1b15102SVladimir Oltean 	i = rx_ring->next_to_clean;
1115d1b15102SVladimir Oltean 
1116d1b15102SVladimir Oltean 	while (likely(rx_frm_cnt < work_limit)) {
1117d1b15102SVladimir Oltean 		union enetc_rx_bd *rxbd, *orig_rxbd;
1118d1b15102SVladimir Oltean 		int orig_i, orig_cleaned_cnt;
1119d1b15102SVladimir Oltean 		struct xdp_buff xdp_buff;
1120d1b15102SVladimir Oltean 		struct sk_buff *skb;
11219d2b68ccSVladimir Oltean 		int tmp_orig_i, err;
1122d1b15102SVladimir Oltean 		u32 bd_status;
1123d1b15102SVladimir Oltean 
1124d1b15102SVladimir Oltean 		rxbd = enetc_rxbd(rx_ring, i);
1125d1b15102SVladimir Oltean 		bd_status = le32_to_cpu(rxbd->r.lstatus);
1126d1b15102SVladimir Oltean 		if (!bd_status)
1127d1b15102SVladimir Oltean 			break;
1128d1b15102SVladimir Oltean 
1129d1b15102SVladimir Oltean 		enetc_wr_reg_hot(rx_ring->idr, BIT(rx_ring->index));
1130d1b15102SVladimir Oltean 		dma_rmb(); /* for reading other rxbd fields */
1131d1b15102SVladimir Oltean 
1132d1b15102SVladimir Oltean 		if (enetc_check_bd_errors_and_consume(rx_ring, bd_status,
1133d1b15102SVladimir Oltean 						      &rxbd, &i))
1134d1b15102SVladimir Oltean 			break;
1135d1b15102SVladimir Oltean 
1136d1b15102SVladimir Oltean 		orig_rxbd = rxbd;
1137d1b15102SVladimir Oltean 		orig_cleaned_cnt = cleaned_cnt;
1138d1b15102SVladimir Oltean 		orig_i = i;
1139d1b15102SVladimir Oltean 
1140d1b15102SVladimir Oltean 		enetc_build_xdp_buff(rx_ring, bd_status, &rxbd, &i,
1141d1b15102SVladimir Oltean 				     &cleaned_cnt, &xdp_buff);
1142d1b15102SVladimir Oltean 
1143d1b15102SVladimir Oltean 		xdp_act = bpf_prog_run_xdp(prog, &xdp_buff);
1144d1b15102SVladimir Oltean 
1145d1b15102SVladimir Oltean 		switch (xdp_act) {
1146d1b15102SVladimir Oltean 		case XDP_ABORTED:
1147d1b15102SVladimir Oltean 			trace_xdp_exception(rx_ring->ndev, prog, xdp_act);
1148d1b15102SVladimir Oltean 			fallthrough;
1149d1b15102SVladimir Oltean 		case XDP_DROP:
1150d1b15102SVladimir Oltean 			enetc_xdp_drop(rx_ring, orig_i, i);
1151d1b15102SVladimir Oltean 			break;
1152d1b15102SVladimir Oltean 		case XDP_PASS:
1153d1b15102SVladimir Oltean 			rxbd = orig_rxbd;
1154d1b15102SVladimir Oltean 			cleaned_cnt = orig_cleaned_cnt;
1155d1b15102SVladimir Oltean 			i = orig_i;
1156d1b15102SVladimir Oltean 
1157d1b15102SVladimir Oltean 			skb = enetc_build_skb(rx_ring, bd_status, &rxbd,
1158d1b15102SVladimir Oltean 					      &i, &cleaned_cnt,
1159d1b15102SVladimir Oltean 					      ENETC_RXB_DMA_SIZE_XDP);
1160d1b15102SVladimir Oltean 			if (unlikely(!skb))
1161d1b15102SVladimir Oltean 				/* Exit the switch/case, not the loop */
1162d1b15102SVladimir Oltean 				break;
1163d1b15102SVladimir Oltean 
1164d1b15102SVladimir Oltean 			napi_gro_receive(napi, skb);
1165d1b15102SVladimir Oltean 			break;
11667ed2bc80SVladimir Oltean 		case XDP_TX:
11677ed2bc80SVladimir Oltean 			xdp_tx_bd_cnt = enetc_rx_swbd_to_xdp_tx_swbd(xdp_tx_arr,
11687ed2bc80SVladimir Oltean 								     rx_ring,
11697ed2bc80SVladimir Oltean 								     orig_i, i);
11707ed2bc80SVladimir Oltean 
11717ed2bc80SVladimir Oltean 			if (!enetc_xdp_tx(tx_ring, xdp_tx_arr, xdp_tx_bd_cnt)) {
11727ed2bc80SVladimir Oltean 				enetc_xdp_drop(rx_ring, orig_i, i);
11737ed2bc80SVladimir Oltean 				tx_ring->stats.xdp_tx_drops++;
11747ed2bc80SVladimir Oltean 			} else {
11757ed2bc80SVladimir Oltean 				tx_ring->stats.xdp_tx += xdp_tx_bd_cnt;
11767ed2bc80SVladimir Oltean 				rx_ring->xdp.xdp_tx_in_flight += xdp_tx_bd_cnt;
11777ed2bc80SVladimir Oltean 				xdp_tx_frm_cnt++;
11787ed2bc80SVladimir Oltean 			}
11797ed2bc80SVladimir Oltean 			break;
11809d2b68ccSVladimir Oltean 		case XDP_REDIRECT:
11819d2b68ccSVladimir Oltean 			/* xdp_return_frame does not support S/G in the sense
11829d2b68ccSVladimir Oltean 			 * that it leaks the fragments (__xdp_return should not
11839d2b68ccSVladimir Oltean 			 * call page_frag_free only for the initial buffer).
11849d2b68ccSVladimir Oltean 			 * Until XDP_REDIRECT gains support for S/G let's keep
11859d2b68ccSVladimir Oltean 			 * the code structure in place, but dead. We drop the
11869d2b68ccSVladimir Oltean 			 * S/G frames ourselves to avoid memory leaks which
11879d2b68ccSVladimir Oltean 			 * would otherwise leave the kernel OOM.
11889d2b68ccSVladimir Oltean 			 */
11899d2b68ccSVladimir Oltean 			if (unlikely(cleaned_cnt - orig_cleaned_cnt != 1)) {
11909d2b68ccSVladimir Oltean 				enetc_xdp_drop(rx_ring, orig_i, i);
11919d2b68ccSVladimir Oltean 				rx_ring->stats.xdp_redirect_sg++;
11929d2b68ccSVladimir Oltean 				break;
11939d2b68ccSVladimir Oltean 			}
11949d2b68ccSVladimir Oltean 
11959d2b68ccSVladimir Oltean 			tmp_orig_i = orig_i;
11969d2b68ccSVladimir Oltean 
11979d2b68ccSVladimir Oltean 			while (orig_i != i) {
11989d2b68ccSVladimir Oltean 				enetc_put_rx_buff(rx_ring,
11999d2b68ccSVladimir Oltean 						  &rx_ring->rx_swbd[orig_i]);
12009d2b68ccSVladimir Oltean 				enetc_bdr_idx_inc(rx_ring, &orig_i);
12019d2b68ccSVladimir Oltean 			}
12029d2b68ccSVladimir Oltean 
12039d2b68ccSVladimir Oltean 			err = xdp_do_redirect(rx_ring->ndev, &xdp_buff, prog);
12049d2b68ccSVladimir Oltean 			if (unlikely(err)) {
12059d2b68ccSVladimir Oltean 				enetc_xdp_free(rx_ring, tmp_orig_i, i);
12069d2b68ccSVladimir Oltean 			} else {
12079d2b68ccSVladimir Oltean 				xdp_redirect_frm_cnt++;
12089d2b68ccSVladimir Oltean 				rx_ring->stats.xdp_redirect++;
12099d2b68ccSVladimir Oltean 			}
12109d2b68ccSVladimir Oltean 
12119d2b68ccSVladimir Oltean 			if (unlikely(xdp_redirect_frm_cnt > ENETC_DEFAULT_TX_WORK)) {
12129d2b68ccSVladimir Oltean 				xdp_do_flush_map();
12139d2b68ccSVladimir Oltean 				xdp_redirect_frm_cnt = 0;
12149d2b68ccSVladimir Oltean 			}
12159d2b68ccSVladimir Oltean 
12169d2b68ccSVladimir Oltean 			break;
1217d1b15102SVladimir Oltean 		default:
1218d1b15102SVladimir Oltean 			bpf_warn_invalid_xdp_action(xdp_act);
1219d1b15102SVladimir Oltean 		}
1220d1b15102SVladimir Oltean 
1221d1b15102SVladimir Oltean 		rx_frm_cnt++;
1222d1b15102SVladimir Oltean 	}
1223d1b15102SVladimir Oltean 
1224d1b15102SVladimir Oltean 	rx_ring->next_to_clean = i;
1225d1b15102SVladimir Oltean 
1226d1b15102SVladimir Oltean 	rx_ring->stats.packets += rx_frm_cnt;
1227d1b15102SVladimir Oltean 	rx_ring->stats.bytes += rx_byte_cnt;
1228d1b15102SVladimir Oltean 
12299d2b68ccSVladimir Oltean 	if (xdp_redirect_frm_cnt)
12309d2b68ccSVladimir Oltean 		xdp_do_flush_map();
12319d2b68ccSVladimir Oltean 
12327ed2bc80SVladimir Oltean 	if (xdp_tx_frm_cnt)
12337ed2bc80SVladimir Oltean 		enetc_update_tx_ring_tail(tx_ring);
12347ed2bc80SVladimir Oltean 
12357ed2bc80SVladimir Oltean 	if (cleaned_cnt > rx_ring->xdp.xdp_tx_in_flight)
12367ed2bc80SVladimir Oltean 		enetc_refill_rx_ring(rx_ring, enetc_bd_unused(rx_ring) -
12377ed2bc80SVladimir Oltean 				     rx_ring->xdp.xdp_tx_in_flight);
12387ed2bc80SVladimir Oltean 
1239d1b15102SVladimir Oltean 	return rx_frm_cnt;
1240d1b15102SVladimir Oltean }
1241d1b15102SVladimir Oltean 
12428580b3c3SVladimir Oltean static int enetc_poll(struct napi_struct *napi, int budget)
12438580b3c3SVladimir Oltean {
12448580b3c3SVladimir Oltean 	struct enetc_int_vector
12458580b3c3SVladimir Oltean 		*v = container_of(napi, struct enetc_int_vector, napi);
1246d1b15102SVladimir Oltean 	struct enetc_bdr *rx_ring = &v->rx_ring;
1247d1b15102SVladimir Oltean 	struct bpf_prog *prog;
12488580b3c3SVladimir Oltean 	bool complete = true;
12498580b3c3SVladimir Oltean 	int work_done;
12508580b3c3SVladimir Oltean 	int i;
12518580b3c3SVladimir Oltean 
12528580b3c3SVladimir Oltean 	enetc_lock_mdio();
12538580b3c3SVladimir Oltean 
12548580b3c3SVladimir Oltean 	for (i = 0; i < v->count_tx_rings; i++)
12558580b3c3SVladimir Oltean 		if (!enetc_clean_tx_ring(&v->tx_ring[i], budget))
12568580b3c3SVladimir Oltean 			complete = false;
12578580b3c3SVladimir Oltean 
1258d1b15102SVladimir Oltean 	prog = rx_ring->xdp.prog;
1259d1b15102SVladimir Oltean 	if (prog)
1260d1b15102SVladimir Oltean 		work_done = enetc_clean_rx_ring_xdp(rx_ring, napi, budget, prog);
1261d1b15102SVladimir Oltean 	else
1262d1b15102SVladimir Oltean 		work_done = enetc_clean_rx_ring(rx_ring, napi, budget);
12638580b3c3SVladimir Oltean 	if (work_done == budget)
12648580b3c3SVladimir Oltean 		complete = false;
12658580b3c3SVladimir Oltean 	if (work_done)
12668580b3c3SVladimir Oltean 		v->rx_napi_work = true;
12678580b3c3SVladimir Oltean 
12688580b3c3SVladimir Oltean 	if (!complete) {
12698580b3c3SVladimir Oltean 		enetc_unlock_mdio();
12708580b3c3SVladimir Oltean 		return budget;
12718580b3c3SVladimir Oltean 	}
12728580b3c3SVladimir Oltean 
12738580b3c3SVladimir Oltean 	napi_complete_done(napi, work_done);
12748580b3c3SVladimir Oltean 
12758580b3c3SVladimir Oltean 	if (likely(v->rx_dim_en))
12768580b3c3SVladimir Oltean 		enetc_rx_net_dim(v);
12778580b3c3SVladimir Oltean 
12788580b3c3SVladimir Oltean 	v->rx_napi_work = false;
12798580b3c3SVladimir Oltean 
12808580b3c3SVladimir Oltean 	/* enable interrupts */
12818580b3c3SVladimir Oltean 	enetc_wr_reg_hot(v->rbier, ENETC_RBIER_RXTIE);
12828580b3c3SVladimir Oltean 
12838580b3c3SVladimir Oltean 	for_each_set_bit(i, &v->tx_rings_map, ENETC_MAX_NUM_TXQS)
12848580b3c3SVladimir Oltean 		enetc_wr_reg_hot(v->tbier_base + ENETC_BDR_OFF(i),
12858580b3c3SVladimir Oltean 				 ENETC_TBIER_TXTIE);
12868580b3c3SVladimir Oltean 
12878580b3c3SVladimir Oltean 	enetc_unlock_mdio();
12888580b3c3SVladimir Oltean 
12898580b3c3SVladimir Oltean 	return work_done;
12908580b3c3SVladimir Oltean }
12918580b3c3SVladimir Oltean 
1292d4fd0404SClaudiu Manoil /* Probing and Init */
1293d382563fSClaudiu Manoil #define ENETC_MAX_RFS_SIZE 64
1294d4fd0404SClaudiu Manoil void enetc_get_si_caps(struct enetc_si *si)
1295d4fd0404SClaudiu Manoil {
1296d4fd0404SClaudiu Manoil 	struct enetc_hw *hw = &si->hw;
1297d4fd0404SClaudiu Manoil 	u32 val;
1298d4fd0404SClaudiu Manoil 
1299d4fd0404SClaudiu Manoil 	/* find out how many of various resources we have to work with */
1300d4fd0404SClaudiu Manoil 	val = enetc_rd(hw, ENETC_SICAPR0);
1301d4fd0404SClaudiu Manoil 	si->num_rx_rings = (val >> 16) & 0xff;
1302d4fd0404SClaudiu Manoil 	si->num_tx_rings = val & 0xff;
1303d382563fSClaudiu Manoil 
1304d382563fSClaudiu Manoil 	val = enetc_rd(hw, ENETC_SIRFSCAPR);
1305d382563fSClaudiu Manoil 	si->num_fs_entries = ENETC_SIRFSCAPR_GET_NUM_RFS(val);
1306d382563fSClaudiu Manoil 	si->num_fs_entries = min(si->num_fs_entries, ENETC_MAX_RFS_SIZE);
1307d382563fSClaudiu Manoil 
1308d382563fSClaudiu Manoil 	si->num_rss = 0;
1309d382563fSClaudiu Manoil 	val = enetc_rd(hw, ENETC_SIPCAPR0);
1310d382563fSClaudiu Manoil 	if (val & ENETC_SIPCAPR0_RSS) {
13112e47cb41SPo Liu 		u32 rss;
13122e47cb41SPo Liu 
13132e47cb41SPo Liu 		rss = enetc_rd(hw, ENETC_SIRSSCAPR);
13142e47cb41SPo Liu 		si->num_rss = ENETC_SIRSSCAPR_GET_NUM_RSS(rss);
1315d382563fSClaudiu Manoil 	}
13162e47cb41SPo Liu 
13172e47cb41SPo Liu 	if (val & ENETC_SIPCAPR0_QBV)
13182e47cb41SPo Liu 		si->hw_features |= ENETC_SI_F_QBV;
131979e49982SPo Liu 
132079e49982SPo Liu 	if (val & ENETC_SIPCAPR0_PSFP)
132179e49982SPo Liu 		si->hw_features |= ENETC_SI_F_PSFP;
1322d4fd0404SClaudiu Manoil }
1323d4fd0404SClaudiu Manoil 
1324d4fd0404SClaudiu Manoil static int enetc_dma_alloc_bdr(struct enetc_bdr *r, size_t bd_size)
1325d4fd0404SClaudiu Manoil {
1326d4fd0404SClaudiu Manoil 	r->bd_base = dma_alloc_coherent(r->dev, r->bd_count * bd_size,
1327d4fd0404SClaudiu Manoil 					&r->bd_dma_base, GFP_KERNEL);
1328d4fd0404SClaudiu Manoil 	if (!r->bd_base)
1329d4fd0404SClaudiu Manoil 		return -ENOMEM;
1330d4fd0404SClaudiu Manoil 
1331d4fd0404SClaudiu Manoil 	/* h/w requires 128B alignment */
1332d4fd0404SClaudiu Manoil 	if (!IS_ALIGNED(r->bd_dma_base, 128)) {
1333d4fd0404SClaudiu Manoil 		dma_free_coherent(r->dev, r->bd_count * bd_size, r->bd_base,
1334d4fd0404SClaudiu Manoil 				  r->bd_dma_base);
1335d4fd0404SClaudiu Manoil 		return -EINVAL;
1336d4fd0404SClaudiu Manoil 	}
1337d4fd0404SClaudiu Manoil 
1338d4fd0404SClaudiu Manoil 	return 0;
1339d4fd0404SClaudiu Manoil }
1340d4fd0404SClaudiu Manoil 
1341d4fd0404SClaudiu Manoil static int enetc_alloc_txbdr(struct enetc_bdr *txr)
1342d4fd0404SClaudiu Manoil {
1343d4fd0404SClaudiu Manoil 	int err;
1344d4fd0404SClaudiu Manoil 
1345d4fd0404SClaudiu Manoil 	txr->tx_swbd = vzalloc(txr->bd_count * sizeof(struct enetc_tx_swbd));
1346d4fd0404SClaudiu Manoil 	if (!txr->tx_swbd)
1347d4fd0404SClaudiu Manoil 		return -ENOMEM;
1348d4fd0404SClaudiu Manoil 
1349d4fd0404SClaudiu Manoil 	err = enetc_dma_alloc_bdr(txr, sizeof(union enetc_tx_bd));
1350d4fd0404SClaudiu Manoil 	if (err) {
1351d4fd0404SClaudiu Manoil 		vfree(txr->tx_swbd);
1352d4fd0404SClaudiu Manoil 		return err;
1353d4fd0404SClaudiu Manoil 	}
1354d4fd0404SClaudiu Manoil 
1355d4fd0404SClaudiu Manoil 	txr->next_to_clean = 0;
1356d4fd0404SClaudiu Manoil 	txr->next_to_use = 0;
1357d4fd0404SClaudiu Manoil 
1358d4fd0404SClaudiu Manoil 	return 0;
1359d4fd0404SClaudiu Manoil }
1360d4fd0404SClaudiu Manoil 
1361d4fd0404SClaudiu Manoil static void enetc_free_txbdr(struct enetc_bdr *txr)
1362d4fd0404SClaudiu Manoil {
1363d4fd0404SClaudiu Manoil 	int size, i;
1364d4fd0404SClaudiu Manoil 
1365d4fd0404SClaudiu Manoil 	for (i = 0; i < txr->bd_count; i++)
13669d2b68ccSVladimir Oltean 		enetc_free_tx_frame(txr, &txr->tx_swbd[i]);
1367d4fd0404SClaudiu Manoil 
1368d4fd0404SClaudiu Manoil 	size = txr->bd_count * sizeof(union enetc_tx_bd);
1369d4fd0404SClaudiu Manoil 
1370d4fd0404SClaudiu Manoil 	dma_free_coherent(txr->dev, size, txr->bd_base, txr->bd_dma_base);
1371d4fd0404SClaudiu Manoil 	txr->bd_base = NULL;
1372d4fd0404SClaudiu Manoil 
1373d4fd0404SClaudiu Manoil 	vfree(txr->tx_swbd);
1374d4fd0404SClaudiu Manoil 	txr->tx_swbd = NULL;
1375d4fd0404SClaudiu Manoil }
1376d4fd0404SClaudiu Manoil 
1377d4fd0404SClaudiu Manoil static int enetc_alloc_tx_resources(struct enetc_ndev_priv *priv)
1378d4fd0404SClaudiu Manoil {
1379d4fd0404SClaudiu Manoil 	int i, err;
1380d4fd0404SClaudiu Manoil 
1381d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++) {
1382d4fd0404SClaudiu Manoil 		err = enetc_alloc_txbdr(priv->tx_ring[i]);
1383d4fd0404SClaudiu Manoil 
1384d4fd0404SClaudiu Manoil 		if (err)
1385d4fd0404SClaudiu Manoil 			goto fail;
1386d4fd0404SClaudiu Manoil 	}
1387d4fd0404SClaudiu Manoil 
1388d4fd0404SClaudiu Manoil 	return 0;
1389d4fd0404SClaudiu Manoil 
1390d4fd0404SClaudiu Manoil fail:
1391d4fd0404SClaudiu Manoil 	while (i-- > 0)
1392d4fd0404SClaudiu Manoil 		enetc_free_txbdr(priv->tx_ring[i]);
1393d4fd0404SClaudiu Manoil 
1394d4fd0404SClaudiu Manoil 	return err;
1395d4fd0404SClaudiu Manoil }
1396d4fd0404SClaudiu Manoil 
1397d4fd0404SClaudiu Manoil static void enetc_free_tx_resources(struct enetc_ndev_priv *priv)
1398d4fd0404SClaudiu Manoil {
1399d4fd0404SClaudiu Manoil 	int i;
1400d4fd0404SClaudiu Manoil 
1401d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
1402d4fd0404SClaudiu Manoil 		enetc_free_txbdr(priv->tx_ring[i]);
1403d4fd0404SClaudiu Manoil }
1404d4fd0404SClaudiu Manoil 
1405434cebabSClaudiu Manoil static int enetc_alloc_rxbdr(struct enetc_bdr *rxr, bool extended)
1406d4fd0404SClaudiu Manoil {
1407434cebabSClaudiu Manoil 	size_t size = sizeof(union enetc_rx_bd);
1408d4fd0404SClaudiu Manoil 	int err;
1409d4fd0404SClaudiu Manoil 
1410d4fd0404SClaudiu Manoil 	rxr->rx_swbd = vzalloc(rxr->bd_count * sizeof(struct enetc_rx_swbd));
1411d4fd0404SClaudiu Manoil 	if (!rxr->rx_swbd)
1412d4fd0404SClaudiu Manoil 		return -ENOMEM;
1413d4fd0404SClaudiu Manoil 
1414434cebabSClaudiu Manoil 	if (extended)
1415434cebabSClaudiu Manoil 		size *= 2;
1416434cebabSClaudiu Manoil 
1417434cebabSClaudiu Manoil 	err = enetc_dma_alloc_bdr(rxr, size);
1418d4fd0404SClaudiu Manoil 	if (err) {
1419d4fd0404SClaudiu Manoil 		vfree(rxr->rx_swbd);
1420d4fd0404SClaudiu Manoil 		return err;
1421d4fd0404SClaudiu Manoil 	}
1422d4fd0404SClaudiu Manoil 
1423d4fd0404SClaudiu Manoil 	rxr->next_to_clean = 0;
1424d4fd0404SClaudiu Manoil 	rxr->next_to_use = 0;
1425d4fd0404SClaudiu Manoil 	rxr->next_to_alloc = 0;
1426434cebabSClaudiu Manoil 	rxr->ext_en = extended;
1427d4fd0404SClaudiu Manoil 
1428d4fd0404SClaudiu Manoil 	return 0;
1429d4fd0404SClaudiu Manoil }
1430d4fd0404SClaudiu Manoil 
1431d4fd0404SClaudiu Manoil static void enetc_free_rxbdr(struct enetc_bdr *rxr)
1432d4fd0404SClaudiu Manoil {
1433d4fd0404SClaudiu Manoil 	int size;
1434d4fd0404SClaudiu Manoil 
1435d4fd0404SClaudiu Manoil 	size = rxr->bd_count * sizeof(union enetc_rx_bd);
1436d4fd0404SClaudiu Manoil 
1437d4fd0404SClaudiu Manoil 	dma_free_coherent(rxr->dev, size, rxr->bd_base, rxr->bd_dma_base);
1438d4fd0404SClaudiu Manoil 	rxr->bd_base = NULL;
1439d4fd0404SClaudiu Manoil 
1440d4fd0404SClaudiu Manoil 	vfree(rxr->rx_swbd);
1441d4fd0404SClaudiu Manoil 	rxr->rx_swbd = NULL;
1442d4fd0404SClaudiu Manoil }
1443d4fd0404SClaudiu Manoil 
1444d4fd0404SClaudiu Manoil static int enetc_alloc_rx_resources(struct enetc_ndev_priv *priv)
1445d4fd0404SClaudiu Manoil {
1446434cebabSClaudiu Manoil 	bool extended = !!(priv->active_offloads & ENETC_F_RX_TSTAMP);
1447d4fd0404SClaudiu Manoil 	int i, err;
1448d4fd0404SClaudiu Manoil 
1449d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++) {
1450434cebabSClaudiu Manoil 		err = enetc_alloc_rxbdr(priv->rx_ring[i], extended);
1451d4fd0404SClaudiu Manoil 
1452d4fd0404SClaudiu Manoil 		if (err)
1453d4fd0404SClaudiu Manoil 			goto fail;
1454d4fd0404SClaudiu Manoil 	}
1455d4fd0404SClaudiu Manoil 
1456d4fd0404SClaudiu Manoil 	return 0;
1457d4fd0404SClaudiu Manoil 
1458d4fd0404SClaudiu Manoil fail:
1459d4fd0404SClaudiu Manoil 	while (i-- > 0)
1460d4fd0404SClaudiu Manoil 		enetc_free_rxbdr(priv->rx_ring[i]);
1461d4fd0404SClaudiu Manoil 
1462d4fd0404SClaudiu Manoil 	return err;
1463d4fd0404SClaudiu Manoil }
1464d4fd0404SClaudiu Manoil 
1465d4fd0404SClaudiu Manoil static void enetc_free_rx_resources(struct enetc_ndev_priv *priv)
1466d4fd0404SClaudiu Manoil {
1467d4fd0404SClaudiu Manoil 	int i;
1468d4fd0404SClaudiu Manoil 
1469d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
1470d4fd0404SClaudiu Manoil 		enetc_free_rxbdr(priv->rx_ring[i]);
1471d4fd0404SClaudiu Manoil }
1472d4fd0404SClaudiu Manoil 
1473d4fd0404SClaudiu Manoil static void enetc_free_tx_ring(struct enetc_bdr *tx_ring)
1474d4fd0404SClaudiu Manoil {
1475d4fd0404SClaudiu Manoil 	int i;
1476d4fd0404SClaudiu Manoil 
1477d4fd0404SClaudiu Manoil 	if (!tx_ring->tx_swbd)
1478d4fd0404SClaudiu Manoil 		return;
1479d4fd0404SClaudiu Manoil 
1480d4fd0404SClaudiu Manoil 	for (i = 0; i < tx_ring->bd_count; i++) {
1481d4fd0404SClaudiu Manoil 		struct enetc_tx_swbd *tx_swbd = &tx_ring->tx_swbd[i];
1482d4fd0404SClaudiu Manoil 
14839d2b68ccSVladimir Oltean 		enetc_free_tx_frame(tx_ring, tx_swbd);
1484d4fd0404SClaudiu Manoil 	}
1485d4fd0404SClaudiu Manoil 
1486d4fd0404SClaudiu Manoil 	tx_ring->next_to_clean = 0;
1487d4fd0404SClaudiu Manoil 	tx_ring->next_to_use = 0;
1488d4fd0404SClaudiu Manoil }
1489d4fd0404SClaudiu Manoil 
1490d4fd0404SClaudiu Manoil static void enetc_free_rx_ring(struct enetc_bdr *rx_ring)
1491d4fd0404SClaudiu Manoil {
1492d4fd0404SClaudiu Manoil 	int i;
1493d4fd0404SClaudiu Manoil 
1494d4fd0404SClaudiu Manoil 	if (!rx_ring->rx_swbd)
1495d4fd0404SClaudiu Manoil 		return;
1496d4fd0404SClaudiu Manoil 
1497d4fd0404SClaudiu Manoil 	for (i = 0; i < rx_ring->bd_count; i++) {
1498d4fd0404SClaudiu Manoil 		struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[i];
1499d4fd0404SClaudiu Manoil 
1500d4fd0404SClaudiu Manoil 		if (!rx_swbd->page)
1501d4fd0404SClaudiu Manoil 			continue;
1502d4fd0404SClaudiu Manoil 
15037ed2bc80SVladimir Oltean 		dma_unmap_page(rx_ring->dev, rx_swbd->dma, PAGE_SIZE,
15047ed2bc80SVladimir Oltean 			       rx_swbd->dir);
1505d4fd0404SClaudiu Manoil 		__free_page(rx_swbd->page);
1506d4fd0404SClaudiu Manoil 		rx_swbd->page = NULL;
1507d4fd0404SClaudiu Manoil 	}
1508d4fd0404SClaudiu Manoil 
1509d4fd0404SClaudiu Manoil 	rx_ring->next_to_clean = 0;
1510d4fd0404SClaudiu Manoil 	rx_ring->next_to_use = 0;
1511d4fd0404SClaudiu Manoil 	rx_ring->next_to_alloc = 0;
1512d4fd0404SClaudiu Manoil }
1513d4fd0404SClaudiu Manoil 
1514d4fd0404SClaudiu Manoil static void enetc_free_rxtx_rings(struct enetc_ndev_priv *priv)
1515d4fd0404SClaudiu Manoil {
1516d4fd0404SClaudiu Manoil 	int i;
1517d4fd0404SClaudiu Manoil 
1518d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
1519d4fd0404SClaudiu Manoil 		enetc_free_rx_ring(priv->rx_ring[i]);
1520d4fd0404SClaudiu Manoil 
1521d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
1522d4fd0404SClaudiu Manoil 		enetc_free_tx_ring(priv->tx_ring[i]);
1523d4fd0404SClaudiu Manoil }
1524d4fd0404SClaudiu Manoil 
1525d382563fSClaudiu Manoil static int enetc_setup_default_rss_table(struct enetc_si *si, int num_groups)
1526d382563fSClaudiu Manoil {
1527d382563fSClaudiu Manoil 	int *rss_table;
1528d382563fSClaudiu Manoil 	int i;
1529d382563fSClaudiu Manoil 
1530d382563fSClaudiu Manoil 	rss_table = kmalloc_array(si->num_rss, sizeof(*rss_table), GFP_KERNEL);
1531d382563fSClaudiu Manoil 	if (!rss_table)
1532d382563fSClaudiu Manoil 		return -ENOMEM;
1533d382563fSClaudiu Manoil 
1534d382563fSClaudiu Manoil 	/* Set up RSS table defaults */
1535d382563fSClaudiu Manoil 	for (i = 0; i < si->num_rss; i++)
1536d382563fSClaudiu Manoil 		rss_table[i] = i % num_groups;
1537d382563fSClaudiu Manoil 
1538d382563fSClaudiu Manoil 	enetc_set_rss_table(si, rss_table, si->num_rss);
1539d382563fSClaudiu Manoil 
1540d382563fSClaudiu Manoil 	kfree(rss_table);
1541d382563fSClaudiu Manoil 
1542d382563fSClaudiu Manoil 	return 0;
1543d382563fSClaudiu Manoil }
1544d382563fSClaudiu Manoil 
1545c646d10dSVladimir Oltean int enetc_configure_si(struct enetc_ndev_priv *priv)
1546d4fd0404SClaudiu Manoil {
1547d4fd0404SClaudiu Manoil 	struct enetc_si *si = priv->si;
1548d4fd0404SClaudiu Manoil 	struct enetc_hw *hw = &si->hw;
1549d382563fSClaudiu Manoil 	int err;
1550d4fd0404SClaudiu Manoil 
1551d4fd0404SClaudiu Manoil 	/* set SI cache attributes */
1552d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICAR0,
1553d4fd0404SClaudiu Manoil 		 ENETC_SICAR_RD_COHERENT | ENETC_SICAR_WR_COHERENT);
1554d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICAR1, ENETC_SICAR_MSI);
1555d4fd0404SClaudiu Manoil 	/* enable SI */
1556d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SIMR, ENETC_SIMR_EN);
1557d4fd0404SClaudiu Manoil 
1558d382563fSClaudiu Manoil 	if (si->num_rss) {
1559d382563fSClaudiu Manoil 		err = enetc_setup_default_rss_table(si, priv->num_rx_rings);
1560d382563fSClaudiu Manoil 		if (err)
1561d382563fSClaudiu Manoil 			return err;
1562d382563fSClaudiu Manoil 	}
1563d382563fSClaudiu Manoil 
1564d4fd0404SClaudiu Manoil 	return 0;
1565d4fd0404SClaudiu Manoil }
1566d4fd0404SClaudiu Manoil 
1567d4fd0404SClaudiu Manoil void enetc_init_si_rings_params(struct enetc_ndev_priv *priv)
1568d4fd0404SClaudiu Manoil {
1569d4fd0404SClaudiu Manoil 	struct enetc_si *si = priv->si;
1570d4fd0404SClaudiu Manoil 	int cpus = num_online_cpus();
1571d4fd0404SClaudiu Manoil 
157202293dd4SClaudiu Manoil 	priv->tx_bd_count = ENETC_TX_RING_DEFAULT_SIZE;
157302293dd4SClaudiu Manoil 	priv->rx_bd_count = ENETC_RX_RING_DEFAULT_SIZE;
1574d4fd0404SClaudiu Manoil 
1575d4fd0404SClaudiu Manoil 	/* Enable all available TX rings in order to configure as many
1576d4fd0404SClaudiu Manoil 	 * priorities as possible, when needed.
1577d4fd0404SClaudiu Manoil 	 * TODO: Make # of TX rings run-time configurable
1578d4fd0404SClaudiu Manoil 	 */
1579d4fd0404SClaudiu Manoil 	priv->num_rx_rings = min_t(int, cpus, si->num_rx_rings);
1580d4fd0404SClaudiu Manoil 	priv->num_tx_rings = si->num_tx_rings;
1581d4fd0404SClaudiu Manoil 	priv->bdr_int_num = cpus;
1582ae0e6a5dSClaudiu Manoil 	priv->ic_mode = ENETC_IC_RX_ADAPTIVE | ENETC_IC_TX_MANUAL;
1583ae0e6a5dSClaudiu Manoil 	priv->tx_ictt = ENETC_TXIC_TIMETHR;
1584d4fd0404SClaudiu Manoil }
1585d4fd0404SClaudiu Manoil 
1586d4fd0404SClaudiu Manoil int enetc_alloc_si_resources(struct enetc_ndev_priv *priv)
1587d4fd0404SClaudiu Manoil {
1588d4fd0404SClaudiu Manoil 	struct enetc_si *si = priv->si;
1589d4fd0404SClaudiu Manoil 
1590d382563fSClaudiu Manoil 	priv->cls_rules = kcalloc(si->num_fs_entries, sizeof(*priv->cls_rules),
1591d382563fSClaudiu Manoil 				  GFP_KERNEL);
15924b47c0b8SVladimir Oltean 	if (!priv->cls_rules)
15934b47c0b8SVladimir Oltean 		return -ENOMEM;
1594d382563fSClaudiu Manoil 
1595d4fd0404SClaudiu Manoil 	return 0;
1596d4fd0404SClaudiu Manoil }
1597d4fd0404SClaudiu Manoil 
1598d4fd0404SClaudiu Manoil void enetc_free_si_resources(struct enetc_ndev_priv *priv)
1599d4fd0404SClaudiu Manoil {
1600d382563fSClaudiu Manoil 	kfree(priv->cls_rules);
1601d4fd0404SClaudiu Manoil }
1602d4fd0404SClaudiu Manoil 
1603d4fd0404SClaudiu Manoil static void enetc_setup_txbdr(struct enetc_hw *hw, struct enetc_bdr *tx_ring)
1604d4fd0404SClaudiu Manoil {
1605d4fd0404SClaudiu Manoil 	int idx = tx_ring->index;
1606d4fd0404SClaudiu Manoil 	u32 tbmr;
1607d4fd0404SClaudiu Manoil 
1608d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBBAR0,
1609d4fd0404SClaudiu Manoil 		       lower_32_bits(tx_ring->bd_dma_base));
1610d4fd0404SClaudiu Manoil 
1611d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBBAR1,
1612d4fd0404SClaudiu Manoil 		       upper_32_bits(tx_ring->bd_dma_base));
1613d4fd0404SClaudiu Manoil 
1614d4fd0404SClaudiu Manoil 	WARN_ON(!IS_ALIGNED(tx_ring->bd_count, 64)); /* multiple of 64 */
1615d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBLENR,
1616d4fd0404SClaudiu Manoil 		       ENETC_RTBLENR_LEN(tx_ring->bd_count));
1617d4fd0404SClaudiu Manoil 
1618d4fd0404SClaudiu Manoil 	/* clearing PI/CI registers for Tx not supported, adjust sw indexes */
1619d4fd0404SClaudiu Manoil 	tx_ring->next_to_use = enetc_txbdr_rd(hw, idx, ENETC_TBPIR);
1620d4fd0404SClaudiu Manoil 	tx_ring->next_to_clean = enetc_txbdr_rd(hw, idx, ENETC_TBCIR);
1621d4fd0404SClaudiu Manoil 
1622d4fd0404SClaudiu Manoil 	/* enable Tx ints by setting pkt thr to 1 */
162312460a0aSClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBICR0, ENETC_TBICR0_ICEN | 0x1);
1624d4fd0404SClaudiu Manoil 
1625d4fd0404SClaudiu Manoil 	tbmr = ENETC_TBMR_EN;
1626d4fd0404SClaudiu Manoil 	if (tx_ring->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
1627d4fd0404SClaudiu Manoil 		tbmr |= ENETC_TBMR_VIH;
1628d4fd0404SClaudiu Manoil 
1629d4fd0404SClaudiu Manoil 	/* enable ring */
1630d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBMR, tbmr);
1631d4fd0404SClaudiu Manoil 
1632d4fd0404SClaudiu Manoil 	tx_ring->tpir = hw->reg + ENETC_BDR(TX, idx, ENETC_TBPIR);
1633d4fd0404SClaudiu Manoil 	tx_ring->tcir = hw->reg + ENETC_BDR(TX, idx, ENETC_TBCIR);
1634d4fd0404SClaudiu Manoil 	tx_ring->idr = hw->reg + ENETC_SITXIDR;
1635d4fd0404SClaudiu Manoil }
1636d4fd0404SClaudiu Manoil 
1637d4fd0404SClaudiu Manoil static void enetc_setup_rxbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring)
1638d4fd0404SClaudiu Manoil {
1639d4fd0404SClaudiu Manoil 	int idx = rx_ring->index;
1640d4fd0404SClaudiu Manoil 	u32 rbmr;
1641d4fd0404SClaudiu Manoil 
1642d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBBAR0,
1643d4fd0404SClaudiu Manoil 		       lower_32_bits(rx_ring->bd_dma_base));
1644d4fd0404SClaudiu Manoil 
1645d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBBAR1,
1646d4fd0404SClaudiu Manoil 		       upper_32_bits(rx_ring->bd_dma_base));
1647d4fd0404SClaudiu Manoil 
1648d4fd0404SClaudiu Manoil 	WARN_ON(!IS_ALIGNED(rx_ring->bd_count, 64)); /* multiple of 64 */
1649d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBLENR,
1650d4fd0404SClaudiu Manoil 		       ENETC_RTBLENR_LEN(rx_ring->bd_count));
1651d4fd0404SClaudiu Manoil 
1652d1b15102SVladimir Oltean 	if (rx_ring->xdp.prog)
1653d1b15102SVladimir Oltean 		enetc_rxbdr_wr(hw, idx, ENETC_RBBSR, ENETC_RXB_DMA_SIZE_XDP);
1654d1b15102SVladimir Oltean 	else
1655d4fd0404SClaudiu Manoil 		enetc_rxbdr_wr(hw, idx, ENETC_RBBSR, ENETC_RXB_DMA_SIZE);
1656d4fd0404SClaudiu Manoil 
1657d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBPIR, 0);
1658d4fd0404SClaudiu Manoil 
1659d4fd0404SClaudiu Manoil 	/* enable Rx ints by setting pkt thr to 1 */
166012460a0aSClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBICR0, ENETC_RBICR0_ICEN | 0x1);
1661d4fd0404SClaudiu Manoil 
1662d4fd0404SClaudiu Manoil 	rbmr = ENETC_RBMR_EN;
1663434cebabSClaudiu Manoil 
1664434cebabSClaudiu Manoil 	if (rx_ring->ext_en)
1665d3982312SY.b. Lu 		rbmr |= ENETC_RBMR_BDS;
1666434cebabSClaudiu Manoil 
1667d4fd0404SClaudiu Manoil 	if (rx_ring->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
1668d4fd0404SClaudiu Manoil 		rbmr |= ENETC_RBMR_VTE;
1669d4fd0404SClaudiu Manoil 
1670d4fd0404SClaudiu Manoil 	rx_ring->rcir = hw->reg + ENETC_BDR(RX, idx, ENETC_RBCIR);
1671d4fd0404SClaudiu Manoil 	rx_ring->idr = hw->reg + ENETC_SIRXIDR;
1672d4fd0404SClaudiu Manoil 
16737a5222cbSVladimir Oltean 	enetc_lock_mdio();
1674d4fd0404SClaudiu Manoil 	enetc_refill_rx_ring(rx_ring, enetc_bd_unused(rx_ring));
16757a5222cbSVladimir Oltean 	enetc_unlock_mdio();
1676d4fd0404SClaudiu Manoil 
1677d4fd0404SClaudiu Manoil 	/* enable ring */
1678d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBMR, rbmr);
1679d4fd0404SClaudiu Manoil }
1680d4fd0404SClaudiu Manoil 
1681d4fd0404SClaudiu Manoil static void enetc_setup_bdrs(struct enetc_ndev_priv *priv)
1682d4fd0404SClaudiu Manoil {
1683d4fd0404SClaudiu Manoil 	int i;
1684d4fd0404SClaudiu Manoil 
1685d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
1686d4fd0404SClaudiu Manoil 		enetc_setup_txbdr(&priv->si->hw, priv->tx_ring[i]);
1687d4fd0404SClaudiu Manoil 
1688d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
1689d4fd0404SClaudiu Manoil 		enetc_setup_rxbdr(&priv->si->hw, priv->rx_ring[i]);
1690d4fd0404SClaudiu Manoil }
1691d4fd0404SClaudiu Manoil 
1692d4fd0404SClaudiu Manoil static void enetc_clear_rxbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring)
1693d4fd0404SClaudiu Manoil {
1694d4fd0404SClaudiu Manoil 	int idx = rx_ring->index;
1695d4fd0404SClaudiu Manoil 
1696d4fd0404SClaudiu Manoil 	/* disable EN bit on ring */
1697d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBMR, 0);
1698d4fd0404SClaudiu Manoil }
1699d4fd0404SClaudiu Manoil 
1700d4fd0404SClaudiu Manoil static void enetc_clear_txbdr(struct enetc_hw *hw, struct enetc_bdr *tx_ring)
1701d4fd0404SClaudiu Manoil {
1702d4fd0404SClaudiu Manoil 	int delay = 8, timeout = 100;
1703d4fd0404SClaudiu Manoil 	int idx = tx_ring->index;
1704d4fd0404SClaudiu Manoil 
1705d4fd0404SClaudiu Manoil 	/* disable EN bit on ring */
1706d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBMR, 0);
1707d4fd0404SClaudiu Manoil 
1708d4fd0404SClaudiu Manoil 	/* wait for busy to clear */
1709d4fd0404SClaudiu Manoil 	while (delay < timeout &&
1710d4fd0404SClaudiu Manoil 	       enetc_txbdr_rd(hw, idx, ENETC_TBSR) & ENETC_TBSR_BUSY) {
1711d4fd0404SClaudiu Manoil 		msleep(delay);
1712d4fd0404SClaudiu Manoil 		delay *= 2;
1713d4fd0404SClaudiu Manoil 	}
1714d4fd0404SClaudiu Manoil 
1715d4fd0404SClaudiu Manoil 	if (delay >= timeout)
1716d4fd0404SClaudiu Manoil 		netdev_warn(tx_ring->ndev, "timeout for tx ring #%d clear\n",
1717d4fd0404SClaudiu Manoil 			    idx);
1718d4fd0404SClaudiu Manoil }
1719d4fd0404SClaudiu Manoil 
1720d4fd0404SClaudiu Manoil static void enetc_clear_bdrs(struct enetc_ndev_priv *priv)
1721d4fd0404SClaudiu Manoil {
1722d4fd0404SClaudiu Manoil 	int i;
1723d4fd0404SClaudiu Manoil 
1724d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
1725d4fd0404SClaudiu Manoil 		enetc_clear_txbdr(&priv->si->hw, priv->tx_ring[i]);
1726d4fd0404SClaudiu Manoil 
1727d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
1728d4fd0404SClaudiu Manoil 		enetc_clear_rxbdr(&priv->si->hw, priv->rx_ring[i]);
1729d4fd0404SClaudiu Manoil 
1730d4fd0404SClaudiu Manoil 	udelay(1);
1731d4fd0404SClaudiu Manoil }
1732d4fd0404SClaudiu Manoil 
1733d4fd0404SClaudiu Manoil static int enetc_setup_irqs(struct enetc_ndev_priv *priv)
1734d4fd0404SClaudiu Manoil {
1735d4fd0404SClaudiu Manoil 	struct pci_dev *pdev = priv->si->pdev;
1736d4fd0404SClaudiu Manoil 	cpumask_t cpu_mask;
1737d4fd0404SClaudiu Manoil 	int i, j, err;
1738d4fd0404SClaudiu Manoil 
1739d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
1740d4fd0404SClaudiu Manoil 		int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i);
1741d4fd0404SClaudiu Manoil 		struct enetc_int_vector *v = priv->int_vector[i];
1742d4fd0404SClaudiu Manoil 		int entry = ENETC_BDR_INT_BASE_IDX + i;
1743d4fd0404SClaudiu Manoil 		struct enetc_hw *hw = &priv->si->hw;
1744d4fd0404SClaudiu Manoil 
1745d4fd0404SClaudiu Manoil 		snprintf(v->name, sizeof(v->name), "%s-rxtx%d",
1746d4fd0404SClaudiu Manoil 			 priv->ndev->name, i);
1747d4fd0404SClaudiu Manoil 		err = request_irq(irq, enetc_msix, 0, v->name, v);
1748d4fd0404SClaudiu Manoil 		if (err) {
1749d4fd0404SClaudiu Manoil 			dev_err(priv->dev, "request_irq() failed!\n");
1750d4fd0404SClaudiu Manoil 			goto irq_err;
1751d4fd0404SClaudiu Manoil 		}
1752bbb96dc7SClaudiu Manoil 		disable_irq(irq);
1753d4fd0404SClaudiu Manoil 
1754d4fd0404SClaudiu Manoil 		v->tbier_base = hw->reg + ENETC_BDR(TX, 0, ENETC_TBIER);
1755d4fd0404SClaudiu Manoil 		v->rbier = hw->reg + ENETC_BDR(RX, i, ENETC_RBIER);
175691571081SClaudiu Manoil 		v->ricr1 = hw->reg + ENETC_BDR(RX, i, ENETC_RBICR1);
1757d4fd0404SClaudiu Manoil 
1758d4fd0404SClaudiu Manoil 		enetc_wr(hw, ENETC_SIMSIRRV(i), entry);
1759d4fd0404SClaudiu Manoil 
1760d4fd0404SClaudiu Manoil 		for (j = 0; j < v->count_tx_rings; j++) {
1761d4fd0404SClaudiu Manoil 			int idx = v->tx_ring[j].index;
1762d4fd0404SClaudiu Manoil 
1763d4fd0404SClaudiu Manoil 			enetc_wr(hw, ENETC_SIMSITRV(idx), entry);
1764d4fd0404SClaudiu Manoil 		}
1765d4fd0404SClaudiu Manoil 		cpumask_clear(&cpu_mask);
1766d4fd0404SClaudiu Manoil 		cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
1767d4fd0404SClaudiu Manoil 		irq_set_affinity_hint(irq, &cpu_mask);
1768d4fd0404SClaudiu Manoil 	}
1769d4fd0404SClaudiu Manoil 
1770d4fd0404SClaudiu Manoil 	return 0;
1771d4fd0404SClaudiu Manoil 
1772d4fd0404SClaudiu Manoil irq_err:
1773d4fd0404SClaudiu Manoil 	while (i--) {
1774d4fd0404SClaudiu Manoil 		int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i);
1775d4fd0404SClaudiu Manoil 
1776d4fd0404SClaudiu Manoil 		irq_set_affinity_hint(irq, NULL);
1777d4fd0404SClaudiu Manoil 		free_irq(irq, priv->int_vector[i]);
1778d4fd0404SClaudiu Manoil 	}
1779d4fd0404SClaudiu Manoil 
1780d4fd0404SClaudiu Manoil 	return err;
1781d4fd0404SClaudiu Manoil }
1782d4fd0404SClaudiu Manoil 
1783d4fd0404SClaudiu Manoil static void enetc_free_irqs(struct enetc_ndev_priv *priv)
1784d4fd0404SClaudiu Manoil {
1785d4fd0404SClaudiu Manoil 	struct pci_dev *pdev = priv->si->pdev;
1786d4fd0404SClaudiu Manoil 	int i;
1787d4fd0404SClaudiu Manoil 
1788d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
1789d4fd0404SClaudiu Manoil 		int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i);
1790d4fd0404SClaudiu Manoil 
1791d4fd0404SClaudiu Manoil 		irq_set_affinity_hint(irq, NULL);
1792d4fd0404SClaudiu Manoil 		free_irq(irq, priv->int_vector[i]);
1793d4fd0404SClaudiu Manoil 	}
1794d4fd0404SClaudiu Manoil }
1795d4fd0404SClaudiu Manoil 
1796bbb96dc7SClaudiu Manoil static void enetc_setup_interrupts(struct enetc_ndev_priv *priv)
1797d4fd0404SClaudiu Manoil {
179891571081SClaudiu Manoil 	struct enetc_hw *hw = &priv->si->hw;
179991571081SClaudiu Manoil 	u32 icpt, ictt;
1800d4fd0404SClaudiu Manoil 	int i;
1801d4fd0404SClaudiu Manoil 
1802d4fd0404SClaudiu Manoil 	/* enable Tx & Rx event indication */
1803ae0e6a5dSClaudiu Manoil 	if (priv->ic_mode &
1804ae0e6a5dSClaudiu Manoil 	    (ENETC_IC_RX_MANUAL | ENETC_IC_RX_ADAPTIVE)) {
180591571081SClaudiu Manoil 		icpt = ENETC_RBICR0_SET_ICPT(ENETC_RXIC_PKTTHR);
180691571081SClaudiu Manoil 		/* init to non-0 minimum, will be adjusted later */
180791571081SClaudiu Manoil 		ictt = 0x1;
180891571081SClaudiu Manoil 	} else {
180991571081SClaudiu Manoil 		icpt = 0x1; /* enable Rx ints by setting pkt thr to 1 */
181091571081SClaudiu Manoil 		ictt = 0;
1811d4fd0404SClaudiu Manoil 	}
1812d4fd0404SClaudiu Manoil 
181391571081SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++) {
181491571081SClaudiu Manoil 		enetc_rxbdr_wr(hw, i, ENETC_RBICR1, ictt);
181591571081SClaudiu Manoil 		enetc_rxbdr_wr(hw, i, ENETC_RBICR0, ENETC_RBICR0_ICEN | icpt);
181691571081SClaudiu Manoil 		enetc_rxbdr_wr(hw, i, ENETC_RBIER, ENETC_RBIER_RXTIE);
181791571081SClaudiu Manoil 	}
181891571081SClaudiu Manoil 
181991571081SClaudiu Manoil 	if (priv->ic_mode & ENETC_IC_TX_MANUAL)
182091571081SClaudiu Manoil 		icpt = ENETC_TBICR0_SET_ICPT(ENETC_TXIC_PKTTHR);
182191571081SClaudiu Manoil 	else
182291571081SClaudiu Manoil 		icpt = 0x1; /* enable Tx ints by setting pkt thr to 1 */
182391571081SClaudiu Manoil 
1824d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++) {
182591571081SClaudiu Manoil 		enetc_txbdr_wr(hw, i, ENETC_TBICR1, priv->tx_ictt);
182691571081SClaudiu Manoil 		enetc_txbdr_wr(hw, i, ENETC_TBICR0, ENETC_TBICR0_ICEN | icpt);
182791571081SClaudiu Manoil 		enetc_txbdr_wr(hw, i, ENETC_TBIER, ENETC_TBIER_TXTIE);
1828d4fd0404SClaudiu Manoil 	}
1829d4fd0404SClaudiu Manoil }
1830d4fd0404SClaudiu Manoil 
1831bbb96dc7SClaudiu Manoil static void enetc_clear_interrupts(struct enetc_ndev_priv *priv)
1832d4fd0404SClaudiu Manoil {
1833d4fd0404SClaudiu Manoil 	int i;
1834d4fd0404SClaudiu Manoil 
1835d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
1836d4fd0404SClaudiu Manoil 		enetc_txbdr_wr(&priv->si->hw, i, ENETC_TBIER, 0);
1837d4fd0404SClaudiu Manoil 
1838d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
1839d4fd0404SClaudiu Manoil 		enetc_rxbdr_wr(&priv->si->hw, i, ENETC_RBIER, 0);
1840d4fd0404SClaudiu Manoil }
1841d4fd0404SClaudiu Manoil 
184271b77a7aSClaudiu Manoil static int enetc_phylink_connect(struct net_device *ndev)
1843d4fd0404SClaudiu Manoil {
18442e47cb41SPo Liu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1845a6a10d45SYangbo Lu 	struct ethtool_eee edata;
184671b77a7aSClaudiu Manoil 	int err;
1847d4fd0404SClaudiu Manoil 
184871b77a7aSClaudiu Manoil 	if (!priv->phylink)
1849d4fd0404SClaudiu Manoil 		return 0; /* phy-less mode */
1850d4fd0404SClaudiu Manoil 
185171b77a7aSClaudiu Manoil 	err = phylink_of_phy_connect(priv->phylink, priv->dev->of_node, 0);
185271b77a7aSClaudiu Manoil 	if (err) {
1853d4fd0404SClaudiu Manoil 		dev_err(&ndev->dev, "could not attach to PHY\n");
185471b77a7aSClaudiu Manoil 		return err;
1855d4fd0404SClaudiu Manoil 	}
1856d4fd0404SClaudiu Manoil 
1857a6a10d45SYangbo Lu 	/* disable EEE autoneg, until ENETC driver supports it */
1858a6a10d45SYangbo Lu 	memset(&edata, 0, sizeof(struct ethtool_eee));
185971b77a7aSClaudiu Manoil 	phylink_ethtool_set_eee(priv->phylink, &edata);
1860a6a10d45SYangbo Lu 
1861d4fd0404SClaudiu Manoil 	return 0;
1862d4fd0404SClaudiu Manoil }
1863d4fd0404SClaudiu Manoil 
186491571081SClaudiu Manoil void enetc_start(struct net_device *ndev)
1865bbb96dc7SClaudiu Manoil {
1866bbb96dc7SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1867bbb96dc7SClaudiu Manoil 	int i;
1868bbb96dc7SClaudiu Manoil 
1869bbb96dc7SClaudiu Manoil 	enetc_setup_interrupts(priv);
1870bbb96dc7SClaudiu Manoil 
1871bbb96dc7SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
1872bbb96dc7SClaudiu Manoil 		int irq = pci_irq_vector(priv->si->pdev,
1873bbb96dc7SClaudiu Manoil 					 ENETC_BDR_INT_BASE_IDX + i);
1874bbb96dc7SClaudiu Manoil 
1875bbb96dc7SClaudiu Manoil 		napi_enable(&priv->int_vector[i]->napi);
1876bbb96dc7SClaudiu Manoil 		enable_irq(irq);
1877bbb96dc7SClaudiu Manoil 	}
1878bbb96dc7SClaudiu Manoil 
187971b77a7aSClaudiu Manoil 	if (priv->phylink)
188071b77a7aSClaudiu Manoil 		phylink_start(priv->phylink);
1881bbb96dc7SClaudiu Manoil 	else
1882bbb96dc7SClaudiu Manoil 		netif_carrier_on(ndev);
1883bbb96dc7SClaudiu Manoil 
1884bbb96dc7SClaudiu Manoil 	netif_tx_start_all_queues(ndev);
1885bbb96dc7SClaudiu Manoil }
1886bbb96dc7SClaudiu Manoil 
1887d4fd0404SClaudiu Manoil int enetc_open(struct net_device *ndev)
1888d4fd0404SClaudiu Manoil {
1889d4fd0404SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1890bbb96dc7SClaudiu Manoil 	int err;
1891d4fd0404SClaudiu Manoil 
1892d4fd0404SClaudiu Manoil 	err = enetc_setup_irqs(priv);
1893d4fd0404SClaudiu Manoil 	if (err)
1894d4fd0404SClaudiu Manoil 		return err;
1895d4fd0404SClaudiu Manoil 
189671b77a7aSClaudiu Manoil 	err = enetc_phylink_connect(ndev);
1897d4fd0404SClaudiu Manoil 	if (err)
1898d4fd0404SClaudiu Manoil 		goto err_phy_connect;
1899d4fd0404SClaudiu Manoil 
1900d4fd0404SClaudiu Manoil 	err = enetc_alloc_tx_resources(priv);
1901d4fd0404SClaudiu Manoil 	if (err)
1902d4fd0404SClaudiu Manoil 		goto err_alloc_tx;
1903d4fd0404SClaudiu Manoil 
1904d4fd0404SClaudiu Manoil 	err = enetc_alloc_rx_resources(priv);
1905d4fd0404SClaudiu Manoil 	if (err)
1906d4fd0404SClaudiu Manoil 		goto err_alloc_rx;
1907d4fd0404SClaudiu Manoil 
1908d4fd0404SClaudiu Manoil 	err = netif_set_real_num_tx_queues(ndev, priv->num_tx_rings);
1909d4fd0404SClaudiu Manoil 	if (err)
1910d4fd0404SClaudiu Manoil 		goto err_set_queues;
1911d4fd0404SClaudiu Manoil 
1912d4fd0404SClaudiu Manoil 	err = netif_set_real_num_rx_queues(ndev, priv->num_rx_rings);
1913d4fd0404SClaudiu Manoil 	if (err)
1914d4fd0404SClaudiu Manoil 		goto err_set_queues;
1915d4fd0404SClaudiu Manoil 
1916bbb96dc7SClaudiu Manoil 	enetc_setup_bdrs(priv);
1917bbb96dc7SClaudiu Manoil 	enetc_start(ndev);
1918d4fd0404SClaudiu Manoil 
1919d4fd0404SClaudiu Manoil 	return 0;
1920d4fd0404SClaudiu Manoil 
1921d4fd0404SClaudiu Manoil err_set_queues:
1922d4fd0404SClaudiu Manoil 	enetc_free_rx_resources(priv);
1923d4fd0404SClaudiu Manoil err_alloc_rx:
1924d4fd0404SClaudiu Manoil 	enetc_free_tx_resources(priv);
1925d4fd0404SClaudiu Manoil err_alloc_tx:
192671b77a7aSClaudiu Manoil 	if (priv->phylink)
192771b77a7aSClaudiu Manoil 		phylink_disconnect_phy(priv->phylink);
1928d4fd0404SClaudiu Manoil err_phy_connect:
1929d4fd0404SClaudiu Manoil 	enetc_free_irqs(priv);
1930d4fd0404SClaudiu Manoil 
1931d4fd0404SClaudiu Manoil 	return err;
1932d4fd0404SClaudiu Manoil }
1933d4fd0404SClaudiu Manoil 
193491571081SClaudiu Manoil void enetc_stop(struct net_device *ndev)
1935d4fd0404SClaudiu Manoil {
1936d4fd0404SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1937d4fd0404SClaudiu Manoil 	int i;
1938d4fd0404SClaudiu Manoil 
1939d4fd0404SClaudiu Manoil 	netif_tx_stop_all_queues(ndev);
1940d4fd0404SClaudiu Manoil 
1941d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
1942bbb96dc7SClaudiu Manoil 		int irq = pci_irq_vector(priv->si->pdev,
1943bbb96dc7SClaudiu Manoil 					 ENETC_BDR_INT_BASE_IDX + i);
1944bbb96dc7SClaudiu Manoil 
1945bbb96dc7SClaudiu Manoil 		disable_irq(irq);
1946d4fd0404SClaudiu Manoil 		napi_synchronize(&priv->int_vector[i]->napi);
1947d4fd0404SClaudiu Manoil 		napi_disable(&priv->int_vector[i]->napi);
1948d4fd0404SClaudiu Manoil 	}
1949d4fd0404SClaudiu Manoil 
195071b77a7aSClaudiu Manoil 	if (priv->phylink)
195171b77a7aSClaudiu Manoil 		phylink_stop(priv->phylink);
1952bbb96dc7SClaudiu Manoil 	else
1953bbb96dc7SClaudiu Manoil 		netif_carrier_off(ndev);
1954bbb96dc7SClaudiu Manoil 
1955bbb96dc7SClaudiu Manoil 	enetc_clear_interrupts(priv);
1956bbb96dc7SClaudiu Manoil }
1957bbb96dc7SClaudiu Manoil 
1958bbb96dc7SClaudiu Manoil int enetc_close(struct net_device *ndev)
1959bbb96dc7SClaudiu Manoil {
1960bbb96dc7SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1961bbb96dc7SClaudiu Manoil 
1962bbb96dc7SClaudiu Manoil 	enetc_stop(ndev);
1963d4fd0404SClaudiu Manoil 	enetc_clear_bdrs(priv);
1964d4fd0404SClaudiu Manoil 
196571b77a7aSClaudiu Manoil 	if (priv->phylink)
196671b77a7aSClaudiu Manoil 		phylink_disconnect_phy(priv->phylink);
1967d4fd0404SClaudiu Manoil 	enetc_free_rxtx_rings(priv);
1968d4fd0404SClaudiu Manoil 	enetc_free_rx_resources(priv);
1969d4fd0404SClaudiu Manoil 	enetc_free_tx_resources(priv);
1970d4fd0404SClaudiu Manoil 	enetc_free_irqs(priv);
1971d4fd0404SClaudiu Manoil 
1972d4fd0404SClaudiu Manoil 	return 0;
1973d4fd0404SClaudiu Manoil }
1974d4fd0404SClaudiu Manoil 
197513baf667SMao Wenan static int enetc_setup_tc_mqprio(struct net_device *ndev, void *type_data)
1976cbe9e835SCamelia Groza {
1977cbe9e835SCamelia Groza 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1978cbe9e835SCamelia Groza 	struct tc_mqprio_qopt *mqprio = type_data;
1979cbe9e835SCamelia Groza 	struct enetc_bdr *tx_ring;
1980cbe9e835SCamelia Groza 	u8 num_tc;
1981cbe9e835SCamelia Groza 	int i;
1982cbe9e835SCamelia Groza 
1983cbe9e835SCamelia Groza 	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
1984cbe9e835SCamelia Groza 	num_tc = mqprio->num_tc;
1985cbe9e835SCamelia Groza 
1986cbe9e835SCamelia Groza 	if (!num_tc) {
1987cbe9e835SCamelia Groza 		netdev_reset_tc(ndev);
1988cbe9e835SCamelia Groza 		netif_set_real_num_tx_queues(ndev, priv->num_tx_rings);
1989cbe9e835SCamelia Groza 
1990cbe9e835SCamelia Groza 		/* Reset all ring priorities to 0 */
1991cbe9e835SCamelia Groza 		for (i = 0; i < priv->num_tx_rings; i++) {
1992cbe9e835SCamelia Groza 			tx_ring = priv->tx_ring[i];
1993cbe9e835SCamelia Groza 			enetc_set_bdr_prio(&priv->si->hw, tx_ring->index, 0);
1994cbe9e835SCamelia Groza 		}
1995cbe9e835SCamelia Groza 
1996cbe9e835SCamelia Groza 		return 0;
1997cbe9e835SCamelia Groza 	}
1998cbe9e835SCamelia Groza 
1999cbe9e835SCamelia Groza 	/* Check if we have enough BD rings available to accommodate all TCs */
2000cbe9e835SCamelia Groza 	if (num_tc > priv->num_tx_rings) {
2001cbe9e835SCamelia Groza 		netdev_err(ndev, "Max %d traffic classes supported\n",
2002cbe9e835SCamelia Groza 			   priv->num_tx_rings);
2003cbe9e835SCamelia Groza 		return -EINVAL;
2004cbe9e835SCamelia Groza 	}
2005cbe9e835SCamelia Groza 
2006cbe9e835SCamelia Groza 	/* For the moment, we use only one BD ring per TC.
2007cbe9e835SCamelia Groza 	 *
2008cbe9e835SCamelia Groza 	 * Configure num_tc BD rings with increasing priorities.
2009cbe9e835SCamelia Groza 	 */
2010cbe9e835SCamelia Groza 	for (i = 0; i < num_tc; i++) {
2011cbe9e835SCamelia Groza 		tx_ring = priv->tx_ring[i];
2012cbe9e835SCamelia Groza 		enetc_set_bdr_prio(&priv->si->hw, tx_ring->index, i);
2013cbe9e835SCamelia Groza 	}
2014cbe9e835SCamelia Groza 
2015cbe9e835SCamelia Groza 	/* Reset the number of netdev queues based on the TC count */
2016cbe9e835SCamelia Groza 	netif_set_real_num_tx_queues(ndev, num_tc);
2017cbe9e835SCamelia Groza 
2018cbe9e835SCamelia Groza 	netdev_set_num_tc(ndev, num_tc);
2019cbe9e835SCamelia Groza 
2020cbe9e835SCamelia Groza 	/* Each TC is associated with one netdev queue */
2021cbe9e835SCamelia Groza 	for (i = 0; i < num_tc; i++)
2022cbe9e835SCamelia Groza 		netdev_set_tc_queue(ndev, i, 1, i);
2023cbe9e835SCamelia Groza 
2024cbe9e835SCamelia Groza 	return 0;
2025cbe9e835SCamelia Groza }
2026cbe9e835SCamelia Groza 
202734c6adf1SPo Liu int enetc_setup_tc(struct net_device *ndev, enum tc_setup_type type,
202834c6adf1SPo Liu 		   void *type_data)
202934c6adf1SPo Liu {
203034c6adf1SPo Liu 	switch (type) {
203134c6adf1SPo Liu 	case TC_SETUP_QDISC_MQPRIO:
203234c6adf1SPo Liu 		return enetc_setup_tc_mqprio(ndev, type_data);
203334c6adf1SPo Liu 	case TC_SETUP_QDISC_TAPRIO:
203434c6adf1SPo Liu 		return enetc_setup_tc_taprio(ndev, type_data);
2035c431047cSPo Liu 	case TC_SETUP_QDISC_CBS:
2036c431047cSPo Liu 		return enetc_setup_tc_cbs(ndev, type_data);
20370d08c9ecSPo Liu 	case TC_SETUP_QDISC_ETF:
20380d08c9ecSPo Liu 		return enetc_setup_tc_txtime(ndev, type_data);
2039888ae5a3SPo Liu 	case TC_SETUP_BLOCK:
2040888ae5a3SPo Liu 		return enetc_setup_tc_psfp(ndev, type_data);
204134c6adf1SPo Liu 	default:
204234c6adf1SPo Liu 		return -EOPNOTSUPP;
204334c6adf1SPo Liu 	}
204434c6adf1SPo Liu }
204534c6adf1SPo Liu 
2046d1b15102SVladimir Oltean static int enetc_setup_xdp_prog(struct net_device *dev, struct bpf_prog *prog,
2047d1b15102SVladimir Oltean 				struct netlink_ext_ack *extack)
2048d1b15102SVladimir Oltean {
2049d1b15102SVladimir Oltean 	struct enetc_ndev_priv *priv = netdev_priv(dev);
2050d1b15102SVladimir Oltean 	struct bpf_prog *old_prog;
2051d1b15102SVladimir Oltean 	bool is_up;
2052d1b15102SVladimir Oltean 	int i;
2053d1b15102SVladimir Oltean 
2054d1b15102SVladimir Oltean 	/* The buffer layout is changing, so we need to drain the old
2055d1b15102SVladimir Oltean 	 * RX buffers and seed new ones.
2056d1b15102SVladimir Oltean 	 */
2057d1b15102SVladimir Oltean 	is_up = netif_running(dev);
2058d1b15102SVladimir Oltean 	if (is_up)
2059d1b15102SVladimir Oltean 		dev_close(dev);
2060d1b15102SVladimir Oltean 
2061d1b15102SVladimir Oltean 	old_prog = xchg(&priv->xdp_prog, prog);
2062d1b15102SVladimir Oltean 	if (old_prog)
2063d1b15102SVladimir Oltean 		bpf_prog_put(old_prog);
2064d1b15102SVladimir Oltean 
2065d1b15102SVladimir Oltean 	for (i = 0; i < priv->num_rx_rings; i++) {
2066d1b15102SVladimir Oltean 		struct enetc_bdr *rx_ring = priv->rx_ring[i];
2067d1b15102SVladimir Oltean 
2068d1b15102SVladimir Oltean 		rx_ring->xdp.prog = prog;
2069d1b15102SVladimir Oltean 
2070d1b15102SVladimir Oltean 		if (prog)
2071d1b15102SVladimir Oltean 			rx_ring->buffer_offset = XDP_PACKET_HEADROOM;
2072d1b15102SVladimir Oltean 		else
2073d1b15102SVladimir Oltean 			rx_ring->buffer_offset = ENETC_RXB_PAD;
2074d1b15102SVladimir Oltean 	}
2075d1b15102SVladimir Oltean 
2076d1b15102SVladimir Oltean 	if (is_up)
2077d1b15102SVladimir Oltean 		return dev_open(dev, extack);
2078d1b15102SVladimir Oltean 
2079d1b15102SVladimir Oltean 	return 0;
2080d1b15102SVladimir Oltean }
2081d1b15102SVladimir Oltean 
2082d1b15102SVladimir Oltean int enetc_setup_bpf(struct net_device *dev, struct netdev_bpf *xdp)
2083d1b15102SVladimir Oltean {
2084d1b15102SVladimir Oltean 	switch (xdp->command) {
2085d1b15102SVladimir Oltean 	case XDP_SETUP_PROG:
2086d1b15102SVladimir Oltean 		return enetc_setup_xdp_prog(dev, xdp->prog, xdp->extack);
2087d1b15102SVladimir Oltean 	default:
2088d1b15102SVladimir Oltean 		return -EINVAL;
2089d1b15102SVladimir Oltean 	}
2090d1b15102SVladimir Oltean 
2091d1b15102SVladimir Oltean 	return 0;
2092d1b15102SVladimir Oltean }
2093d1b15102SVladimir Oltean 
2094d4fd0404SClaudiu Manoil struct net_device_stats *enetc_get_stats(struct net_device *ndev)
2095d4fd0404SClaudiu Manoil {
2096d4fd0404SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2097d4fd0404SClaudiu Manoil 	struct net_device_stats *stats = &ndev->stats;
2098d4fd0404SClaudiu Manoil 	unsigned long packets = 0, bytes = 0;
2099d4fd0404SClaudiu Manoil 	int i;
2100d4fd0404SClaudiu Manoil 
2101d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++) {
2102d4fd0404SClaudiu Manoil 		packets += priv->rx_ring[i]->stats.packets;
2103d4fd0404SClaudiu Manoil 		bytes	+= priv->rx_ring[i]->stats.bytes;
2104d4fd0404SClaudiu Manoil 	}
2105d4fd0404SClaudiu Manoil 
2106d4fd0404SClaudiu Manoil 	stats->rx_packets = packets;
2107d4fd0404SClaudiu Manoil 	stats->rx_bytes = bytes;
2108d4fd0404SClaudiu Manoil 	bytes = 0;
2109d4fd0404SClaudiu Manoil 	packets = 0;
2110d4fd0404SClaudiu Manoil 
2111d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++) {
2112d4fd0404SClaudiu Manoil 		packets += priv->tx_ring[i]->stats.packets;
2113d4fd0404SClaudiu Manoil 		bytes	+= priv->tx_ring[i]->stats.bytes;
2114d4fd0404SClaudiu Manoil 	}
2115d4fd0404SClaudiu Manoil 
2116d4fd0404SClaudiu Manoil 	stats->tx_packets = packets;
2117d4fd0404SClaudiu Manoil 	stats->tx_bytes = bytes;
2118d4fd0404SClaudiu Manoil 
2119d4fd0404SClaudiu Manoil 	return stats;
2120d4fd0404SClaudiu Manoil }
2121d4fd0404SClaudiu Manoil 
2122d382563fSClaudiu Manoil static int enetc_set_rss(struct net_device *ndev, int en)
2123d382563fSClaudiu Manoil {
2124d382563fSClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2125d382563fSClaudiu Manoil 	struct enetc_hw *hw = &priv->si->hw;
2126d382563fSClaudiu Manoil 	u32 reg;
2127d382563fSClaudiu Manoil 
2128d382563fSClaudiu Manoil 	enetc_wr(hw, ENETC_SIRBGCR, priv->num_rx_rings);
2129d382563fSClaudiu Manoil 
2130d382563fSClaudiu Manoil 	reg = enetc_rd(hw, ENETC_SIMR);
2131d382563fSClaudiu Manoil 	reg &= ~ENETC_SIMR_RSSE;
2132d382563fSClaudiu Manoil 	reg |= (en) ? ENETC_SIMR_RSSE : 0;
2133d382563fSClaudiu Manoil 	enetc_wr(hw, ENETC_SIMR, reg);
2134d382563fSClaudiu Manoil 
2135d382563fSClaudiu Manoil 	return 0;
2136d382563fSClaudiu Manoil }
2137d382563fSClaudiu Manoil 
213879e49982SPo Liu static int enetc_set_psfp(struct net_device *ndev, int en)
213979e49982SPo Liu {
214079e49982SPo Liu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2141888ae5a3SPo Liu 	int err;
214279e49982SPo Liu 
214379e49982SPo Liu 	if (en) {
2144888ae5a3SPo Liu 		err = enetc_psfp_enable(priv);
2145888ae5a3SPo Liu 		if (err)
2146888ae5a3SPo Liu 			return err;
2147888ae5a3SPo Liu 
214879e49982SPo Liu 		priv->active_offloads |= ENETC_F_QCI;
2149888ae5a3SPo Liu 		return 0;
215079e49982SPo Liu 	}
215179e49982SPo Liu 
2152888ae5a3SPo Liu 	err = enetc_psfp_disable(priv);
2153888ae5a3SPo Liu 	if (err)
2154888ae5a3SPo Liu 		return err;
2155888ae5a3SPo Liu 
2156888ae5a3SPo Liu 	priv->active_offloads &= ~ENETC_F_QCI;
2157888ae5a3SPo Liu 
215879e49982SPo Liu 	return 0;
215979e49982SPo Liu }
216079e49982SPo Liu 
21619deba33fSClaudiu Manoil static void enetc_enable_rxvlan(struct net_device *ndev, bool en)
21629deba33fSClaudiu Manoil {
21639deba33fSClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
21649deba33fSClaudiu Manoil 	int i;
21659deba33fSClaudiu Manoil 
21669deba33fSClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
21679deba33fSClaudiu Manoil 		enetc_bdr_enable_rxvlan(&priv->si->hw, i, en);
21689deba33fSClaudiu Manoil }
21699deba33fSClaudiu Manoil 
21709deba33fSClaudiu Manoil static void enetc_enable_txvlan(struct net_device *ndev, bool en)
21719deba33fSClaudiu Manoil {
21729deba33fSClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
21739deba33fSClaudiu Manoil 	int i;
21749deba33fSClaudiu Manoil 
21759deba33fSClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
21769deba33fSClaudiu Manoil 		enetc_bdr_enable_txvlan(&priv->si->hw, i, en);
21779deba33fSClaudiu Manoil }
21789deba33fSClaudiu Manoil 
2179d382563fSClaudiu Manoil int enetc_set_features(struct net_device *ndev,
2180d382563fSClaudiu Manoil 		       netdev_features_t features)
2181d382563fSClaudiu Manoil {
2182d382563fSClaudiu Manoil 	netdev_features_t changed = ndev->features ^ features;
2183888ae5a3SPo Liu 	int err = 0;
2184d382563fSClaudiu Manoil 
2185d382563fSClaudiu Manoil 	if (changed & NETIF_F_RXHASH)
2186d382563fSClaudiu Manoil 		enetc_set_rss(ndev, !!(features & NETIF_F_RXHASH));
2187d382563fSClaudiu Manoil 
21889deba33fSClaudiu Manoil 	if (changed & NETIF_F_HW_VLAN_CTAG_RX)
21899deba33fSClaudiu Manoil 		enetc_enable_rxvlan(ndev,
21909deba33fSClaudiu Manoil 				    !!(features & NETIF_F_HW_VLAN_CTAG_RX));
21919deba33fSClaudiu Manoil 
21929deba33fSClaudiu Manoil 	if (changed & NETIF_F_HW_VLAN_CTAG_TX)
21939deba33fSClaudiu Manoil 		enetc_enable_txvlan(ndev,
21949deba33fSClaudiu Manoil 				    !!(features & NETIF_F_HW_VLAN_CTAG_TX));
21959deba33fSClaudiu Manoil 
219679e49982SPo Liu 	if (changed & NETIF_F_HW_TC)
2197888ae5a3SPo Liu 		err = enetc_set_psfp(ndev, !!(features & NETIF_F_HW_TC));
219879e49982SPo Liu 
2199888ae5a3SPo Liu 	return err;
2200d382563fSClaudiu Manoil }
2201d382563fSClaudiu Manoil 
2202434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
2203d3982312SY.b. Lu static int enetc_hwtstamp_set(struct net_device *ndev, struct ifreq *ifr)
2204d3982312SY.b. Lu {
2205d3982312SY.b. Lu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2206d3982312SY.b. Lu 	struct hwtstamp_config config;
2207434cebabSClaudiu Manoil 	int ao;
2208d3982312SY.b. Lu 
2209d3982312SY.b. Lu 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2210d3982312SY.b. Lu 		return -EFAULT;
2211d3982312SY.b. Lu 
2212d3982312SY.b. Lu 	switch (config.tx_type) {
2213d3982312SY.b. Lu 	case HWTSTAMP_TX_OFF:
2214d3982312SY.b. Lu 		priv->active_offloads &= ~ENETC_F_TX_TSTAMP;
2215d3982312SY.b. Lu 		break;
2216d3982312SY.b. Lu 	case HWTSTAMP_TX_ON:
2217d3982312SY.b. Lu 		priv->active_offloads |= ENETC_F_TX_TSTAMP;
2218d3982312SY.b. Lu 		break;
2219d3982312SY.b. Lu 	default:
2220d3982312SY.b. Lu 		return -ERANGE;
2221d3982312SY.b. Lu 	}
2222d3982312SY.b. Lu 
2223434cebabSClaudiu Manoil 	ao = priv->active_offloads;
2224d3982312SY.b. Lu 	switch (config.rx_filter) {
2225d3982312SY.b. Lu 	case HWTSTAMP_FILTER_NONE:
2226d3982312SY.b. Lu 		priv->active_offloads &= ~ENETC_F_RX_TSTAMP;
2227d3982312SY.b. Lu 		break;
2228d3982312SY.b. Lu 	default:
2229d3982312SY.b. Lu 		priv->active_offloads |= ENETC_F_RX_TSTAMP;
2230d3982312SY.b. Lu 		config.rx_filter = HWTSTAMP_FILTER_ALL;
2231d3982312SY.b. Lu 	}
2232d3982312SY.b. Lu 
2233434cebabSClaudiu Manoil 	if (netif_running(ndev) && ao != priv->active_offloads) {
2234434cebabSClaudiu Manoil 		enetc_close(ndev);
2235434cebabSClaudiu Manoil 		enetc_open(ndev);
2236434cebabSClaudiu Manoil 	}
2237434cebabSClaudiu Manoil 
2238d3982312SY.b. Lu 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
2239d3982312SY.b. Lu 	       -EFAULT : 0;
2240d3982312SY.b. Lu }
2241d3982312SY.b. Lu 
2242d3982312SY.b. Lu static int enetc_hwtstamp_get(struct net_device *ndev, struct ifreq *ifr)
2243d3982312SY.b. Lu {
2244d3982312SY.b. Lu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2245d3982312SY.b. Lu 	struct hwtstamp_config config;
2246d3982312SY.b. Lu 
2247d3982312SY.b. Lu 	config.flags = 0;
2248d3982312SY.b. Lu 
2249d3982312SY.b. Lu 	if (priv->active_offloads & ENETC_F_TX_TSTAMP)
2250d3982312SY.b. Lu 		config.tx_type = HWTSTAMP_TX_ON;
2251d3982312SY.b. Lu 	else
2252d3982312SY.b. Lu 		config.tx_type = HWTSTAMP_TX_OFF;
2253d3982312SY.b. Lu 
2254d3982312SY.b. Lu 	config.rx_filter = (priv->active_offloads & ENETC_F_RX_TSTAMP) ?
2255d3982312SY.b. Lu 			    HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE;
2256d3982312SY.b. Lu 
2257d3982312SY.b. Lu 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
2258d3982312SY.b. Lu 	       -EFAULT : 0;
2259d3982312SY.b. Lu }
2260d3982312SY.b. Lu #endif
2261d3982312SY.b. Lu 
2262d3982312SY.b. Lu int enetc_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2263d3982312SY.b. Lu {
226471b77a7aSClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
2265434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
2266d3982312SY.b. Lu 	if (cmd == SIOCSHWTSTAMP)
2267d3982312SY.b. Lu 		return enetc_hwtstamp_set(ndev, rq);
2268d3982312SY.b. Lu 	if (cmd == SIOCGHWTSTAMP)
2269d3982312SY.b. Lu 		return enetc_hwtstamp_get(ndev, rq);
2270d3982312SY.b. Lu #endif
2271a613bafeSMichael Walle 
227271b77a7aSClaudiu Manoil 	if (!priv->phylink)
2273c55b810aSMichael Walle 		return -EOPNOTSUPP;
227471b77a7aSClaudiu Manoil 
227571b77a7aSClaudiu Manoil 	return phylink_mii_ioctl(priv->phylink, rq, cmd);
2276d3982312SY.b. Lu }
2277d3982312SY.b. Lu 
2278d4fd0404SClaudiu Manoil int enetc_alloc_msix(struct enetc_ndev_priv *priv)
2279d4fd0404SClaudiu Manoil {
2280d4fd0404SClaudiu Manoil 	struct pci_dev *pdev = priv->si->pdev;
22811260e772SGustavo A. R. Silva 	int v_tx_rings;
2282d4fd0404SClaudiu Manoil 	int i, n, err, nvec;
2283d4fd0404SClaudiu Manoil 
2284d4fd0404SClaudiu Manoil 	nvec = ENETC_BDR_INT_BASE_IDX + priv->bdr_int_num;
2285d4fd0404SClaudiu Manoil 	/* allocate MSIX for both messaging and Rx/Tx interrupts */
2286d4fd0404SClaudiu Manoil 	n = pci_alloc_irq_vectors(pdev, nvec, nvec, PCI_IRQ_MSIX);
2287d4fd0404SClaudiu Manoil 
2288d4fd0404SClaudiu Manoil 	if (n < 0)
2289d4fd0404SClaudiu Manoil 		return n;
2290d4fd0404SClaudiu Manoil 
2291d4fd0404SClaudiu Manoil 	if (n != nvec)
2292d4fd0404SClaudiu Manoil 		return -EPERM;
2293d4fd0404SClaudiu Manoil 
2294d4fd0404SClaudiu Manoil 	/* # of tx rings per int vector */
2295d4fd0404SClaudiu Manoil 	v_tx_rings = priv->num_tx_rings / priv->bdr_int_num;
2296d4fd0404SClaudiu Manoil 
2297d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
2298d4fd0404SClaudiu Manoil 		struct enetc_int_vector *v;
2299d4fd0404SClaudiu Manoil 		struct enetc_bdr *bdr;
2300d4fd0404SClaudiu Manoil 		int j;
2301d4fd0404SClaudiu Manoil 
23021260e772SGustavo A. R. Silva 		v = kzalloc(struct_size(v, tx_ring, v_tx_rings), GFP_KERNEL);
2303d4fd0404SClaudiu Manoil 		if (!v) {
2304d4fd0404SClaudiu Manoil 			err = -ENOMEM;
2305d4fd0404SClaudiu Manoil 			goto fail;
2306d4fd0404SClaudiu Manoil 		}
2307d4fd0404SClaudiu Manoil 
2308d4fd0404SClaudiu Manoil 		priv->int_vector[i] = v;
2309d4fd0404SClaudiu Manoil 
2310d1b15102SVladimir Oltean 		bdr = &v->rx_ring;
2311d1b15102SVladimir Oltean 		bdr->index = i;
2312d1b15102SVladimir Oltean 		bdr->ndev = priv->ndev;
2313d1b15102SVladimir Oltean 		bdr->dev = priv->dev;
2314d1b15102SVladimir Oltean 		bdr->bd_count = priv->rx_bd_count;
2315d1b15102SVladimir Oltean 		bdr->buffer_offset = ENETC_RXB_PAD;
2316d1b15102SVladimir Oltean 		priv->rx_ring[i] = bdr;
2317d1b15102SVladimir Oltean 
2318d1b15102SVladimir Oltean 		err = xdp_rxq_info_reg(&bdr->xdp.rxq, priv->ndev, i, 0);
2319d1b15102SVladimir Oltean 		if (err) {
2320d1b15102SVladimir Oltean 			kfree(v);
2321d1b15102SVladimir Oltean 			goto fail;
2322d1b15102SVladimir Oltean 		}
2323d1b15102SVladimir Oltean 
2324d1b15102SVladimir Oltean 		err = xdp_rxq_info_reg_mem_model(&bdr->xdp.rxq,
2325d1b15102SVladimir Oltean 						 MEM_TYPE_PAGE_SHARED, NULL);
2326d1b15102SVladimir Oltean 		if (err) {
2327d1b15102SVladimir Oltean 			xdp_rxq_info_unreg(&bdr->xdp.rxq);
2328d1b15102SVladimir Oltean 			kfree(v);
2329d1b15102SVladimir Oltean 			goto fail;
2330d1b15102SVladimir Oltean 		}
2331d1b15102SVladimir Oltean 
2332ae0e6a5dSClaudiu Manoil 		/* init defaults for adaptive IC */
2333ae0e6a5dSClaudiu Manoil 		if (priv->ic_mode & ENETC_IC_RX_ADAPTIVE) {
2334ae0e6a5dSClaudiu Manoil 			v->rx_ictt = 0x1;
2335ae0e6a5dSClaudiu Manoil 			v->rx_dim_en = true;
2336ae0e6a5dSClaudiu Manoil 		}
2337ae0e6a5dSClaudiu Manoil 		INIT_WORK(&v->rx_dim.work, enetc_rx_dim_work);
2338d4fd0404SClaudiu Manoil 		netif_napi_add(priv->ndev, &v->napi, enetc_poll,
2339d4fd0404SClaudiu Manoil 			       NAPI_POLL_WEIGHT);
2340d4fd0404SClaudiu Manoil 		v->count_tx_rings = v_tx_rings;
2341d4fd0404SClaudiu Manoil 
2342d4fd0404SClaudiu Manoil 		for (j = 0; j < v_tx_rings; j++) {
2343d4fd0404SClaudiu Manoil 			int idx;
2344d4fd0404SClaudiu Manoil 
2345d4fd0404SClaudiu Manoil 			/* default tx ring mapping policy */
2346d4fd0404SClaudiu Manoil 			if (priv->bdr_int_num == ENETC_MAX_BDR_INT)
2347d4fd0404SClaudiu Manoil 				idx = 2 * j + i; /* 2 CPUs */
2348d4fd0404SClaudiu Manoil 			else
2349d4fd0404SClaudiu Manoil 				idx = j + i * v_tx_rings; /* default */
2350d4fd0404SClaudiu Manoil 
2351d4fd0404SClaudiu Manoil 			__set_bit(idx, &v->tx_rings_map);
2352d4fd0404SClaudiu Manoil 			bdr = &v->tx_ring[j];
2353d4fd0404SClaudiu Manoil 			bdr->index = idx;
2354d4fd0404SClaudiu Manoil 			bdr->ndev = priv->ndev;
2355d4fd0404SClaudiu Manoil 			bdr->dev = priv->dev;
2356d4fd0404SClaudiu Manoil 			bdr->bd_count = priv->tx_bd_count;
2357d4fd0404SClaudiu Manoil 			priv->tx_ring[idx] = bdr;
2358d4fd0404SClaudiu Manoil 		}
2359d4fd0404SClaudiu Manoil 	}
2360d4fd0404SClaudiu Manoil 
2361d4fd0404SClaudiu Manoil 	return 0;
2362d4fd0404SClaudiu Manoil 
2363d4fd0404SClaudiu Manoil fail:
2364d4fd0404SClaudiu Manoil 	while (i--) {
2365d1b15102SVladimir Oltean 		struct enetc_int_vector *v = priv->int_vector[i];
2366d1b15102SVladimir Oltean 		struct enetc_bdr *rx_ring = &v->rx_ring;
2367d1b15102SVladimir Oltean 
2368d1b15102SVladimir Oltean 		xdp_rxq_info_unreg_mem_model(&rx_ring->xdp.rxq);
2369d1b15102SVladimir Oltean 		xdp_rxq_info_unreg(&rx_ring->xdp.rxq);
2370d1b15102SVladimir Oltean 		netif_napi_del(&v->napi);
2371d1b15102SVladimir Oltean 		cancel_work_sync(&v->rx_dim.work);
2372d1b15102SVladimir Oltean 		kfree(v);
2373d4fd0404SClaudiu Manoil 	}
2374d4fd0404SClaudiu Manoil 
2375d4fd0404SClaudiu Manoil 	pci_free_irq_vectors(pdev);
2376d4fd0404SClaudiu Manoil 
2377d4fd0404SClaudiu Manoil 	return err;
2378d4fd0404SClaudiu Manoil }
2379d4fd0404SClaudiu Manoil 
2380d4fd0404SClaudiu Manoil void enetc_free_msix(struct enetc_ndev_priv *priv)
2381d4fd0404SClaudiu Manoil {
2382d4fd0404SClaudiu Manoil 	int i;
2383d4fd0404SClaudiu Manoil 
2384d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
2385d4fd0404SClaudiu Manoil 		struct enetc_int_vector *v = priv->int_vector[i];
2386d1b15102SVladimir Oltean 		struct enetc_bdr *rx_ring = &v->rx_ring;
2387d4fd0404SClaudiu Manoil 
2388d1b15102SVladimir Oltean 		xdp_rxq_info_unreg_mem_model(&rx_ring->xdp.rxq);
2389d1b15102SVladimir Oltean 		xdp_rxq_info_unreg(&rx_ring->xdp.rxq);
2390d4fd0404SClaudiu Manoil 		netif_napi_del(&v->napi);
2391ae0e6a5dSClaudiu Manoil 		cancel_work_sync(&v->rx_dim.work);
2392d4fd0404SClaudiu Manoil 	}
2393d4fd0404SClaudiu Manoil 
2394d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
2395d4fd0404SClaudiu Manoil 		priv->rx_ring[i] = NULL;
2396d4fd0404SClaudiu Manoil 
2397d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
2398d4fd0404SClaudiu Manoil 		priv->tx_ring[i] = NULL;
2399d4fd0404SClaudiu Manoil 
2400d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
2401d4fd0404SClaudiu Manoil 		kfree(priv->int_vector[i]);
2402d4fd0404SClaudiu Manoil 		priv->int_vector[i] = NULL;
2403d4fd0404SClaudiu Manoil 	}
2404d4fd0404SClaudiu Manoil 
2405d4fd0404SClaudiu Manoil 	/* disable all MSIX for this device */
2406d4fd0404SClaudiu Manoil 	pci_free_irq_vectors(priv->si->pdev);
2407d4fd0404SClaudiu Manoil }
2408d4fd0404SClaudiu Manoil 
2409d4fd0404SClaudiu Manoil static void enetc_kfree_si(struct enetc_si *si)
2410d4fd0404SClaudiu Manoil {
2411d4fd0404SClaudiu Manoil 	char *p = (char *)si - si->pad;
2412d4fd0404SClaudiu Manoil 
2413d4fd0404SClaudiu Manoil 	kfree(p);
2414d4fd0404SClaudiu Manoil }
2415d4fd0404SClaudiu Manoil 
2416d4fd0404SClaudiu Manoil static void enetc_detect_errata(struct enetc_si *si)
2417d4fd0404SClaudiu Manoil {
2418d4fd0404SClaudiu Manoil 	if (si->pdev->revision == ENETC_REV1)
241982728b91SClaudiu Manoil 		si->errata = ENETC_ERR_VLAN_ISOL | ENETC_ERR_UCMCSWP;
2420d4fd0404SClaudiu Manoil }
2421d4fd0404SClaudiu Manoil 
2422d4fd0404SClaudiu Manoil int enetc_pci_probe(struct pci_dev *pdev, const char *name, int sizeof_priv)
2423d4fd0404SClaudiu Manoil {
2424d4fd0404SClaudiu Manoil 	struct enetc_si *si, *p;
2425d4fd0404SClaudiu Manoil 	struct enetc_hw *hw;
2426d4fd0404SClaudiu Manoil 	size_t alloc_size;
2427d4fd0404SClaudiu Manoil 	int err, len;
2428d4fd0404SClaudiu Manoil 
2429d4fd0404SClaudiu Manoil 	pcie_flr(pdev);
2430d4fd0404SClaudiu Manoil 	err = pci_enable_device_mem(pdev);
2431d4fd0404SClaudiu Manoil 	if (err) {
2432d4fd0404SClaudiu Manoil 		dev_err(&pdev->dev, "device enable failed\n");
2433d4fd0404SClaudiu Manoil 		return err;
2434d4fd0404SClaudiu Manoil 	}
2435d4fd0404SClaudiu Manoil 
2436d4fd0404SClaudiu Manoil 	/* set up for high or low dma */
2437d4fd0404SClaudiu Manoil 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2438d4fd0404SClaudiu Manoil 	if (err) {
2439d4fd0404SClaudiu Manoil 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2440d4fd0404SClaudiu Manoil 		if (err) {
2441d4fd0404SClaudiu Manoil 			dev_err(&pdev->dev,
2442d4fd0404SClaudiu Manoil 				"DMA configuration failed: 0x%x\n", err);
2443d4fd0404SClaudiu Manoil 			goto err_dma;
2444d4fd0404SClaudiu Manoil 		}
2445d4fd0404SClaudiu Manoil 	}
2446d4fd0404SClaudiu Manoil 
2447d4fd0404SClaudiu Manoil 	err = pci_request_mem_regions(pdev, name);
2448d4fd0404SClaudiu Manoil 	if (err) {
2449d4fd0404SClaudiu Manoil 		dev_err(&pdev->dev, "pci_request_regions failed err=%d\n", err);
2450d4fd0404SClaudiu Manoil 		goto err_pci_mem_reg;
2451d4fd0404SClaudiu Manoil 	}
2452d4fd0404SClaudiu Manoil 
2453d4fd0404SClaudiu Manoil 	pci_set_master(pdev);
2454d4fd0404SClaudiu Manoil 
2455d4fd0404SClaudiu Manoil 	alloc_size = sizeof(struct enetc_si);
2456d4fd0404SClaudiu Manoil 	if (sizeof_priv) {
2457d4fd0404SClaudiu Manoil 		/* align priv to 32B */
2458d4fd0404SClaudiu Manoil 		alloc_size = ALIGN(alloc_size, ENETC_SI_ALIGN);
2459d4fd0404SClaudiu Manoil 		alloc_size += sizeof_priv;
2460d4fd0404SClaudiu Manoil 	}
2461d4fd0404SClaudiu Manoil 	/* force 32B alignment for enetc_si */
2462d4fd0404SClaudiu Manoil 	alloc_size += ENETC_SI_ALIGN - 1;
2463d4fd0404SClaudiu Manoil 
2464d4fd0404SClaudiu Manoil 	p = kzalloc(alloc_size, GFP_KERNEL);
2465d4fd0404SClaudiu Manoil 	if (!p) {
2466d4fd0404SClaudiu Manoil 		err = -ENOMEM;
2467d4fd0404SClaudiu Manoil 		goto err_alloc_si;
2468d4fd0404SClaudiu Manoil 	}
2469d4fd0404SClaudiu Manoil 
2470d4fd0404SClaudiu Manoil 	si = PTR_ALIGN(p, ENETC_SI_ALIGN);
2471d4fd0404SClaudiu Manoil 	si->pad = (char *)si - (char *)p;
2472d4fd0404SClaudiu Manoil 
2473d4fd0404SClaudiu Manoil 	pci_set_drvdata(pdev, si);
2474d4fd0404SClaudiu Manoil 	si->pdev = pdev;
2475d4fd0404SClaudiu Manoil 	hw = &si->hw;
2476d4fd0404SClaudiu Manoil 
2477d4fd0404SClaudiu Manoil 	len = pci_resource_len(pdev, ENETC_BAR_REGS);
2478d4fd0404SClaudiu Manoil 	hw->reg = ioremap(pci_resource_start(pdev, ENETC_BAR_REGS), len);
2479d4fd0404SClaudiu Manoil 	if (!hw->reg) {
2480d4fd0404SClaudiu Manoil 		err = -ENXIO;
2481d4fd0404SClaudiu Manoil 		dev_err(&pdev->dev, "ioremap() failed\n");
2482d4fd0404SClaudiu Manoil 		goto err_ioremap;
2483d4fd0404SClaudiu Manoil 	}
2484d4fd0404SClaudiu Manoil 	if (len > ENETC_PORT_BASE)
2485d4fd0404SClaudiu Manoil 		hw->port = hw->reg + ENETC_PORT_BASE;
2486d4fd0404SClaudiu Manoil 	if (len > ENETC_GLOBAL_BASE)
2487d4fd0404SClaudiu Manoil 		hw->global = hw->reg + ENETC_GLOBAL_BASE;
2488d4fd0404SClaudiu Manoil 
2489d4fd0404SClaudiu Manoil 	enetc_detect_errata(si);
2490d4fd0404SClaudiu Manoil 
2491d4fd0404SClaudiu Manoil 	return 0;
2492d4fd0404SClaudiu Manoil 
2493d4fd0404SClaudiu Manoil err_ioremap:
2494d4fd0404SClaudiu Manoil 	enetc_kfree_si(si);
2495d4fd0404SClaudiu Manoil err_alloc_si:
2496d4fd0404SClaudiu Manoil 	pci_release_mem_regions(pdev);
2497d4fd0404SClaudiu Manoil err_pci_mem_reg:
2498d4fd0404SClaudiu Manoil err_dma:
2499d4fd0404SClaudiu Manoil 	pci_disable_device(pdev);
2500d4fd0404SClaudiu Manoil 
2501d4fd0404SClaudiu Manoil 	return err;
2502d4fd0404SClaudiu Manoil }
2503d4fd0404SClaudiu Manoil 
2504d4fd0404SClaudiu Manoil void enetc_pci_remove(struct pci_dev *pdev)
2505d4fd0404SClaudiu Manoil {
2506d4fd0404SClaudiu Manoil 	struct enetc_si *si = pci_get_drvdata(pdev);
2507d4fd0404SClaudiu Manoil 	struct enetc_hw *hw = &si->hw;
2508d4fd0404SClaudiu Manoil 
2509d4fd0404SClaudiu Manoil 	iounmap(hw->reg);
2510d4fd0404SClaudiu Manoil 	enetc_kfree_si(si);
2511d4fd0404SClaudiu Manoil 	pci_release_mem_regions(pdev);
2512d4fd0404SClaudiu Manoil 	pci_disable_device(pdev);
2513d4fd0404SClaudiu Manoil }
2514