xref: /openbmc/linux/drivers/net/ethernet/freescale/enetc/enetc.c (revision 82728b91f124b5976f07cd62b3783df72e7c4a27)
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"
5d4fd0404SClaudiu Manoil #include <linux/tcp.h>
6d4fd0404SClaudiu Manoil #include <linux/udp.h>
7bbcbf2eeSStephen Rothwell #include <linux/vmalloc.h>
8d4fd0404SClaudiu Manoil 
9d4fd0404SClaudiu Manoil /* ENETC overhead: optional extension BD + 1 BD gap */
10d4fd0404SClaudiu Manoil #define ENETC_TXBDS_NEEDED(val)	((val) + 2)
11d4fd0404SClaudiu Manoil /* max # of chained Tx BDs is 15, including head and extension BD */
12d4fd0404SClaudiu Manoil #define ENETC_MAX_SKB_FRAGS	13
13d4fd0404SClaudiu Manoil #define ENETC_TXBDS_MAX_NEEDED	ENETC_TXBDS_NEEDED(ENETC_MAX_SKB_FRAGS + 1)
14d4fd0404SClaudiu Manoil 
15d3982312SY.b. Lu static int enetc_map_tx_buffs(struct enetc_bdr *tx_ring, struct sk_buff *skb,
16d3982312SY.b. Lu 			      int active_offloads);
17d4fd0404SClaudiu Manoil 
18d4fd0404SClaudiu Manoil netdev_tx_t enetc_xmit(struct sk_buff *skb, struct net_device *ndev)
19d4fd0404SClaudiu Manoil {
20d4fd0404SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
21d4fd0404SClaudiu Manoil 	struct enetc_bdr *tx_ring;
22d4fd0404SClaudiu Manoil 	int count;
23d4fd0404SClaudiu Manoil 
24d4fd0404SClaudiu Manoil 	tx_ring = priv->tx_ring[skb->queue_mapping];
25d4fd0404SClaudiu Manoil 
26d4fd0404SClaudiu Manoil 	if (unlikely(skb_shinfo(skb)->nr_frags > ENETC_MAX_SKB_FRAGS))
27d4fd0404SClaudiu Manoil 		if (unlikely(skb_linearize(skb)))
28d4fd0404SClaudiu Manoil 			goto drop_packet_err;
29d4fd0404SClaudiu Manoil 
30d4fd0404SClaudiu Manoil 	count = skb_shinfo(skb)->nr_frags + 1; /* fragments + head */
31d4fd0404SClaudiu Manoil 	if (enetc_bd_unused(tx_ring) < ENETC_TXBDS_NEEDED(count)) {
32d4fd0404SClaudiu Manoil 		netif_stop_subqueue(ndev, tx_ring->index);
33d4fd0404SClaudiu Manoil 		return NETDEV_TX_BUSY;
34d4fd0404SClaudiu Manoil 	}
35d4fd0404SClaudiu Manoil 
36d3982312SY.b. Lu 	count = enetc_map_tx_buffs(tx_ring, skb, priv->active_offloads);
37d4fd0404SClaudiu Manoil 	if (unlikely(!count))
38d4fd0404SClaudiu Manoil 		goto drop_packet_err;
39d4fd0404SClaudiu Manoil 
40d4fd0404SClaudiu Manoil 	if (enetc_bd_unused(tx_ring) < ENETC_TXBDS_MAX_NEEDED)
41d4fd0404SClaudiu Manoil 		netif_stop_subqueue(ndev, tx_ring->index);
42d4fd0404SClaudiu Manoil 
43d4fd0404SClaudiu Manoil 	return NETDEV_TX_OK;
44d4fd0404SClaudiu Manoil 
45d4fd0404SClaudiu Manoil drop_packet_err:
46d4fd0404SClaudiu Manoil 	dev_kfree_skb_any(skb);
47d4fd0404SClaudiu Manoil 	return NETDEV_TX_OK;
48d4fd0404SClaudiu Manoil }
49d4fd0404SClaudiu Manoil 
50d4fd0404SClaudiu Manoil static void enetc_unmap_tx_buff(struct enetc_bdr *tx_ring,
51d4fd0404SClaudiu Manoil 				struct enetc_tx_swbd *tx_swbd)
52d4fd0404SClaudiu Manoil {
53d4fd0404SClaudiu Manoil 	if (tx_swbd->is_dma_page)
54d4fd0404SClaudiu Manoil 		dma_unmap_page(tx_ring->dev, tx_swbd->dma,
55d4fd0404SClaudiu Manoil 			       tx_swbd->len, DMA_TO_DEVICE);
56d4fd0404SClaudiu Manoil 	else
57d4fd0404SClaudiu Manoil 		dma_unmap_single(tx_ring->dev, tx_swbd->dma,
58d4fd0404SClaudiu Manoil 				 tx_swbd->len, DMA_TO_DEVICE);
59d4fd0404SClaudiu Manoil 	tx_swbd->dma = 0;
60d4fd0404SClaudiu Manoil }
61d4fd0404SClaudiu Manoil 
62d4fd0404SClaudiu Manoil static void enetc_free_tx_skb(struct enetc_bdr *tx_ring,
63d4fd0404SClaudiu Manoil 			      struct enetc_tx_swbd *tx_swbd)
64d4fd0404SClaudiu Manoil {
65d4fd0404SClaudiu Manoil 	if (tx_swbd->dma)
66d4fd0404SClaudiu Manoil 		enetc_unmap_tx_buff(tx_ring, tx_swbd);
67d4fd0404SClaudiu Manoil 
68d4fd0404SClaudiu Manoil 	if (tx_swbd->skb) {
69d4fd0404SClaudiu Manoil 		dev_kfree_skb_any(tx_swbd->skb);
70d4fd0404SClaudiu Manoil 		tx_swbd->skb = NULL;
71d4fd0404SClaudiu Manoil 	}
72d4fd0404SClaudiu Manoil }
73d4fd0404SClaudiu Manoil 
74d3982312SY.b. Lu static int enetc_map_tx_buffs(struct enetc_bdr *tx_ring, struct sk_buff *skb,
75d3982312SY.b. Lu 			      int active_offloads)
76d4fd0404SClaudiu Manoil {
77d4fd0404SClaudiu Manoil 	struct enetc_tx_swbd *tx_swbd;
78d7840976SMatthew Wilcox (Oracle) 	skb_frag_t *frag;
79d4fd0404SClaudiu Manoil 	int len = skb_headlen(skb);
80d4fd0404SClaudiu Manoil 	union enetc_tx_bd temp_bd;
81d4fd0404SClaudiu Manoil 	union enetc_tx_bd *txbd;
82d4fd0404SClaudiu Manoil 	bool do_vlan, do_tstamp;
83d4fd0404SClaudiu Manoil 	int i, count = 0;
84d4fd0404SClaudiu Manoil 	unsigned int f;
85d4fd0404SClaudiu Manoil 	dma_addr_t dma;
86d4fd0404SClaudiu Manoil 	u8 flags = 0;
87d4fd0404SClaudiu Manoil 
88d4fd0404SClaudiu Manoil 	i = tx_ring->next_to_use;
89d4fd0404SClaudiu Manoil 	txbd = ENETC_TXBD(*tx_ring, i);
90d4fd0404SClaudiu Manoil 	prefetchw(txbd);
91d4fd0404SClaudiu Manoil 
92d4fd0404SClaudiu Manoil 	dma = dma_map_single(tx_ring->dev, skb->data, len, DMA_TO_DEVICE);
93d4fd0404SClaudiu Manoil 	if (unlikely(dma_mapping_error(tx_ring->dev, dma)))
94d4fd0404SClaudiu Manoil 		goto dma_err;
95d4fd0404SClaudiu Manoil 
96d4fd0404SClaudiu Manoil 	temp_bd.addr = cpu_to_le64(dma);
97d4fd0404SClaudiu Manoil 	temp_bd.buf_len = cpu_to_le16(len);
98d4fd0404SClaudiu Manoil 	temp_bd.lstatus = 0;
99d4fd0404SClaudiu Manoil 
100d4fd0404SClaudiu Manoil 	tx_swbd = &tx_ring->tx_swbd[i];
101d4fd0404SClaudiu Manoil 	tx_swbd->dma = dma;
102d4fd0404SClaudiu Manoil 	tx_swbd->len = len;
103d4fd0404SClaudiu Manoil 	tx_swbd->is_dma_page = 0;
104d4fd0404SClaudiu Manoil 	count++;
105d4fd0404SClaudiu Manoil 
106d4fd0404SClaudiu Manoil 	do_vlan = skb_vlan_tag_present(skb);
107d3982312SY.b. Lu 	do_tstamp = (active_offloads & ENETC_F_TX_TSTAMP) &&
108d3982312SY.b. Lu 		    (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP);
109d3982312SY.b. Lu 	tx_swbd->do_tstamp = do_tstamp;
110d3982312SY.b. Lu 	tx_swbd->check_wb = tx_swbd->do_tstamp;
111d4fd0404SClaudiu Manoil 
112d4fd0404SClaudiu Manoil 	if (do_vlan || do_tstamp)
113d4fd0404SClaudiu Manoil 		flags |= ENETC_TXBD_FLAGS_EX;
114d4fd0404SClaudiu Manoil 
115*82728b91SClaudiu Manoil 	if (tx_ring->tsd_enable)
1160d08c9ecSPo Liu 		flags |= ENETC_TXBD_FLAGS_TSE | ENETC_TXBD_FLAGS_TXSTART;
117d4fd0404SClaudiu Manoil 
118d4fd0404SClaudiu Manoil 	/* first BD needs frm_len and offload flags set */
119d4fd0404SClaudiu Manoil 	temp_bd.frm_len = cpu_to_le16(skb->len);
120d4fd0404SClaudiu Manoil 	temp_bd.flags = flags;
121d4fd0404SClaudiu Manoil 
122*82728b91SClaudiu Manoil 	if (flags & ENETC_TXBD_FLAGS_TSE)
123*82728b91SClaudiu Manoil 		temp_bd.txstart = enetc_txbd_set_tx_start(skb->skb_mstamp_ns,
124*82728b91SClaudiu Manoil 							  flags);
1250d08c9ecSPo Liu 
126d4fd0404SClaudiu Manoil 	if (flags & ENETC_TXBD_FLAGS_EX) {
127d4fd0404SClaudiu Manoil 		u8 e_flags = 0;
128d4fd0404SClaudiu Manoil 		*txbd = temp_bd;
129d4fd0404SClaudiu Manoil 		enetc_clear_tx_bd(&temp_bd);
130d4fd0404SClaudiu Manoil 
131d4fd0404SClaudiu Manoil 		/* add extension BD for VLAN and/or timestamping */
132d4fd0404SClaudiu Manoil 		flags = 0;
133d4fd0404SClaudiu Manoil 		tx_swbd++;
134d4fd0404SClaudiu Manoil 		txbd++;
135d4fd0404SClaudiu Manoil 		i++;
136d4fd0404SClaudiu Manoil 		if (unlikely(i == tx_ring->bd_count)) {
137d4fd0404SClaudiu Manoil 			i = 0;
138d4fd0404SClaudiu Manoil 			tx_swbd = tx_ring->tx_swbd;
139d4fd0404SClaudiu Manoil 			txbd = ENETC_TXBD(*tx_ring, 0);
140d4fd0404SClaudiu Manoil 		}
141d4fd0404SClaudiu Manoil 		prefetchw(txbd);
142d4fd0404SClaudiu Manoil 
143d4fd0404SClaudiu Manoil 		if (do_vlan) {
144d4fd0404SClaudiu Manoil 			temp_bd.ext.vid = cpu_to_le16(skb_vlan_tag_get(skb));
145d4fd0404SClaudiu Manoil 			temp_bd.ext.tpid = 0; /* < C-TAG */
146d4fd0404SClaudiu Manoil 			e_flags |= ENETC_TXBD_E_FLAGS_VLAN_INS;
147d4fd0404SClaudiu Manoil 		}
148d4fd0404SClaudiu Manoil 
149d4fd0404SClaudiu Manoil 		if (do_tstamp) {
150d4fd0404SClaudiu Manoil 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
151d4fd0404SClaudiu Manoil 			e_flags |= ENETC_TXBD_E_FLAGS_TWO_STEP_PTP;
152d4fd0404SClaudiu Manoil 		}
153d4fd0404SClaudiu Manoil 
154d4fd0404SClaudiu Manoil 		temp_bd.ext.e_flags = e_flags;
155d4fd0404SClaudiu Manoil 		count++;
156d4fd0404SClaudiu Manoil 	}
157d4fd0404SClaudiu Manoil 
158d4fd0404SClaudiu Manoil 	frag = &skb_shinfo(skb)->frags[0];
159d4fd0404SClaudiu Manoil 	for (f = 0; f < skb_shinfo(skb)->nr_frags; f++, frag++) {
160d4fd0404SClaudiu Manoil 		len = skb_frag_size(frag);
161d4fd0404SClaudiu Manoil 		dma = skb_frag_dma_map(tx_ring->dev, frag, 0, len,
162d4fd0404SClaudiu Manoil 				       DMA_TO_DEVICE);
163d4fd0404SClaudiu Manoil 		if (dma_mapping_error(tx_ring->dev, dma))
164d4fd0404SClaudiu Manoil 			goto dma_err;
165d4fd0404SClaudiu Manoil 
166d4fd0404SClaudiu Manoil 		*txbd = temp_bd;
167d4fd0404SClaudiu Manoil 		enetc_clear_tx_bd(&temp_bd);
168d4fd0404SClaudiu Manoil 
169d4fd0404SClaudiu Manoil 		flags = 0;
170d4fd0404SClaudiu Manoil 		tx_swbd++;
171d4fd0404SClaudiu Manoil 		txbd++;
172d4fd0404SClaudiu Manoil 		i++;
173d4fd0404SClaudiu Manoil 		if (unlikely(i == tx_ring->bd_count)) {
174d4fd0404SClaudiu Manoil 			i = 0;
175d4fd0404SClaudiu Manoil 			tx_swbd = tx_ring->tx_swbd;
176d4fd0404SClaudiu Manoil 			txbd = ENETC_TXBD(*tx_ring, 0);
177d4fd0404SClaudiu Manoil 		}
178d4fd0404SClaudiu Manoil 		prefetchw(txbd);
179d4fd0404SClaudiu Manoil 
180d4fd0404SClaudiu Manoil 		temp_bd.addr = cpu_to_le64(dma);
181d4fd0404SClaudiu Manoil 		temp_bd.buf_len = cpu_to_le16(len);
182d4fd0404SClaudiu Manoil 
183d4fd0404SClaudiu Manoil 		tx_swbd->dma = dma;
184d4fd0404SClaudiu Manoil 		tx_swbd->len = len;
185d4fd0404SClaudiu Manoil 		tx_swbd->is_dma_page = 1;
186d4fd0404SClaudiu Manoil 		count++;
187d4fd0404SClaudiu Manoil 	}
188d4fd0404SClaudiu Manoil 
189d4fd0404SClaudiu Manoil 	/* last BD needs 'F' bit set */
190d4fd0404SClaudiu Manoil 	flags |= ENETC_TXBD_FLAGS_F;
191d4fd0404SClaudiu Manoil 	temp_bd.flags = flags;
192d4fd0404SClaudiu Manoil 	*txbd = temp_bd;
193d4fd0404SClaudiu Manoil 
194d4fd0404SClaudiu Manoil 	tx_ring->tx_swbd[i].skb = skb;
195d4fd0404SClaudiu Manoil 
196d4fd0404SClaudiu Manoil 	enetc_bdr_idx_inc(tx_ring, &i);
197d4fd0404SClaudiu Manoil 	tx_ring->next_to_use = i;
198d4fd0404SClaudiu Manoil 
1994caefbceSMichael Walle 	skb_tx_timestamp(skb);
2004caefbceSMichael Walle 
201d4fd0404SClaudiu Manoil 	/* let H/W know BD ring has been updated */
202d4fd0404SClaudiu Manoil 	enetc_wr_reg(tx_ring->tpir, i); /* includes wmb() */
203d4fd0404SClaudiu Manoil 
204d4fd0404SClaudiu Manoil 	return count;
205d4fd0404SClaudiu Manoil 
206d4fd0404SClaudiu Manoil dma_err:
207d4fd0404SClaudiu Manoil 	dev_err(tx_ring->dev, "DMA map error");
208d4fd0404SClaudiu Manoil 
209d4fd0404SClaudiu Manoil 	do {
210d4fd0404SClaudiu Manoil 		tx_swbd = &tx_ring->tx_swbd[i];
211d4fd0404SClaudiu Manoil 		enetc_free_tx_skb(tx_ring, tx_swbd);
212d4fd0404SClaudiu Manoil 		if (i == 0)
213d4fd0404SClaudiu Manoil 			i = tx_ring->bd_count;
214d4fd0404SClaudiu Manoil 		i--;
215d4fd0404SClaudiu Manoil 	} while (count--);
216d4fd0404SClaudiu Manoil 
217d4fd0404SClaudiu Manoil 	return 0;
218d4fd0404SClaudiu Manoil }
219d4fd0404SClaudiu Manoil 
220d4fd0404SClaudiu Manoil static irqreturn_t enetc_msix(int irq, void *data)
221d4fd0404SClaudiu Manoil {
222d4fd0404SClaudiu Manoil 	struct enetc_int_vector	*v = data;
223d4fd0404SClaudiu Manoil 	int i;
224d4fd0404SClaudiu Manoil 
225d4fd0404SClaudiu Manoil 	/* disable interrupts */
226d4fd0404SClaudiu Manoil 	enetc_wr_reg(v->rbier, 0);
22791571081SClaudiu Manoil 	enetc_wr_reg(v->ricr1, v->rx_ictt);
228d4fd0404SClaudiu Manoil 
2290574e200SClaudiu Manoil 	for_each_set_bit(i, &v->tx_rings_map, ENETC_MAX_NUM_TXQS)
230d4fd0404SClaudiu Manoil 		enetc_wr_reg(v->tbier_base + ENETC_BDR_OFF(i), 0);
231d4fd0404SClaudiu Manoil 
232215602a8SJiafei Pan 	napi_schedule(&v->napi);
233d4fd0404SClaudiu Manoil 
234d4fd0404SClaudiu Manoil 	return IRQ_HANDLED;
235d4fd0404SClaudiu Manoil }
236d4fd0404SClaudiu Manoil 
237d4fd0404SClaudiu Manoil static bool enetc_clean_tx_ring(struct enetc_bdr *tx_ring, int napi_budget);
238d4fd0404SClaudiu Manoil static int enetc_clean_rx_ring(struct enetc_bdr *rx_ring,
239d4fd0404SClaudiu Manoil 			       struct napi_struct *napi, int work_limit);
240d4fd0404SClaudiu Manoil 
241ae0e6a5dSClaudiu Manoil static void enetc_rx_dim_work(struct work_struct *w)
242ae0e6a5dSClaudiu Manoil {
243ae0e6a5dSClaudiu Manoil 	struct dim *dim = container_of(w, struct dim, work);
244ae0e6a5dSClaudiu Manoil 	struct dim_cq_moder moder =
245ae0e6a5dSClaudiu Manoil 		net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
246ae0e6a5dSClaudiu Manoil 	struct enetc_int_vector	*v =
247ae0e6a5dSClaudiu Manoil 		container_of(dim, struct enetc_int_vector, rx_dim);
248ae0e6a5dSClaudiu Manoil 
249ae0e6a5dSClaudiu Manoil 	v->rx_ictt = enetc_usecs_to_cycles(moder.usec);
250ae0e6a5dSClaudiu Manoil 	dim->state = DIM_START_MEASURE;
251ae0e6a5dSClaudiu Manoil }
252ae0e6a5dSClaudiu Manoil 
253ae0e6a5dSClaudiu Manoil static void enetc_rx_net_dim(struct enetc_int_vector *v)
254ae0e6a5dSClaudiu Manoil {
255ae0e6a5dSClaudiu Manoil 	struct dim_sample dim_sample;
256ae0e6a5dSClaudiu Manoil 
257ae0e6a5dSClaudiu Manoil 	v->comp_cnt++;
258ae0e6a5dSClaudiu Manoil 
259ae0e6a5dSClaudiu Manoil 	if (!v->rx_napi_work)
260ae0e6a5dSClaudiu Manoil 		return;
261ae0e6a5dSClaudiu Manoil 
262ae0e6a5dSClaudiu Manoil 	dim_update_sample(v->comp_cnt,
263ae0e6a5dSClaudiu Manoil 			  v->rx_ring.stats.packets,
264ae0e6a5dSClaudiu Manoil 			  v->rx_ring.stats.bytes,
265ae0e6a5dSClaudiu Manoil 			  &dim_sample);
266ae0e6a5dSClaudiu Manoil 	net_dim(&v->rx_dim, dim_sample);
267ae0e6a5dSClaudiu Manoil }
268ae0e6a5dSClaudiu Manoil 
269d4fd0404SClaudiu Manoil static int enetc_poll(struct napi_struct *napi, int budget)
270d4fd0404SClaudiu Manoil {
271d4fd0404SClaudiu Manoil 	struct enetc_int_vector
272d4fd0404SClaudiu Manoil 		*v = container_of(napi, struct enetc_int_vector, napi);
273d4fd0404SClaudiu Manoil 	bool complete = true;
274d4fd0404SClaudiu Manoil 	int work_done;
275d4fd0404SClaudiu Manoil 	int i;
276d4fd0404SClaudiu Manoil 
277d4fd0404SClaudiu Manoil 	for (i = 0; i < v->count_tx_rings; i++)
278d4fd0404SClaudiu Manoil 		if (!enetc_clean_tx_ring(&v->tx_ring[i], budget))
279d4fd0404SClaudiu Manoil 			complete = false;
280d4fd0404SClaudiu Manoil 
281d4fd0404SClaudiu Manoil 	work_done = enetc_clean_rx_ring(&v->rx_ring, napi, budget);
282d4fd0404SClaudiu Manoil 	if (work_done == budget)
283d4fd0404SClaudiu Manoil 		complete = false;
284ae0e6a5dSClaudiu Manoil 	if (work_done)
285ae0e6a5dSClaudiu Manoil 		v->rx_napi_work = true;
286d4fd0404SClaudiu Manoil 
287d4fd0404SClaudiu Manoil 	if (!complete)
288d4fd0404SClaudiu Manoil 		return budget;
289d4fd0404SClaudiu Manoil 
290d4fd0404SClaudiu Manoil 	napi_complete_done(napi, work_done);
291d4fd0404SClaudiu Manoil 
292ae0e6a5dSClaudiu Manoil 	if (likely(v->rx_dim_en))
293ae0e6a5dSClaudiu Manoil 		enetc_rx_net_dim(v);
294ae0e6a5dSClaudiu Manoil 
295ae0e6a5dSClaudiu Manoil 	v->rx_napi_work = false;
296ae0e6a5dSClaudiu Manoil 
297d4fd0404SClaudiu Manoil 	/* enable interrupts */
298d4fd0404SClaudiu Manoil 	enetc_wr_reg(v->rbier, ENETC_RBIER_RXTIE);
299d4fd0404SClaudiu Manoil 
3000574e200SClaudiu Manoil 	for_each_set_bit(i, &v->tx_rings_map, ENETC_MAX_NUM_TXQS)
301d4fd0404SClaudiu Manoil 		enetc_wr_reg(v->tbier_base + ENETC_BDR_OFF(i),
302d4fd0404SClaudiu Manoil 			     ENETC_TBIER_TXTIE);
303d4fd0404SClaudiu Manoil 
304d4fd0404SClaudiu Manoil 	return work_done;
305d4fd0404SClaudiu Manoil }
306d4fd0404SClaudiu Manoil 
307d4fd0404SClaudiu Manoil static int enetc_bd_ready_count(struct enetc_bdr *tx_ring, int ci)
308d4fd0404SClaudiu Manoil {
309d4fd0404SClaudiu Manoil 	int pi = enetc_rd_reg(tx_ring->tcir) & ENETC_TBCIR_IDX_MASK;
310d4fd0404SClaudiu Manoil 
311d4fd0404SClaudiu Manoil 	return pi >= ci ? pi - ci : tx_ring->bd_count - ci + pi;
312d4fd0404SClaudiu Manoil }
313d4fd0404SClaudiu Manoil 
314d3982312SY.b. Lu static void enetc_get_tx_tstamp(struct enetc_hw *hw, union enetc_tx_bd *txbd,
315d3982312SY.b. Lu 				u64 *tstamp)
316d3982312SY.b. Lu {
317cec4f328SY.b. Lu 	u32 lo, hi, tstamp_lo;
318d3982312SY.b. Lu 
319d3982312SY.b. Lu 	lo = enetc_rd(hw, ENETC_SICTR0);
320d3982312SY.b. Lu 	hi = enetc_rd(hw, ENETC_SICTR1);
321cec4f328SY.b. Lu 	tstamp_lo = le32_to_cpu(txbd->wb.tstamp);
322cec4f328SY.b. Lu 	if (lo <= tstamp_lo)
323d3982312SY.b. Lu 		hi -= 1;
324cec4f328SY.b. Lu 	*tstamp = (u64)hi << 32 | tstamp_lo;
325d3982312SY.b. Lu }
326d3982312SY.b. Lu 
327d3982312SY.b. Lu static void enetc_tstamp_tx(struct sk_buff *skb, u64 tstamp)
328d3982312SY.b. Lu {
329d3982312SY.b. Lu 	struct skb_shared_hwtstamps shhwtstamps;
330d3982312SY.b. Lu 
331d3982312SY.b. Lu 	if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) {
332d3982312SY.b. Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
333d3982312SY.b. Lu 		shhwtstamps.hwtstamp = ns_to_ktime(tstamp);
334d3982312SY.b. Lu 		skb_tstamp_tx(skb, &shhwtstamps);
335d3982312SY.b. Lu 	}
336d3982312SY.b. Lu }
337d3982312SY.b. Lu 
338d4fd0404SClaudiu Manoil static bool enetc_clean_tx_ring(struct enetc_bdr *tx_ring, int napi_budget)
339d4fd0404SClaudiu Manoil {
340d4fd0404SClaudiu Manoil 	struct net_device *ndev = tx_ring->ndev;
341d4fd0404SClaudiu Manoil 	int tx_frm_cnt = 0, tx_byte_cnt = 0;
342d4fd0404SClaudiu Manoil 	struct enetc_tx_swbd *tx_swbd;
343d4fd0404SClaudiu Manoil 	int i, bds_to_clean;
344d3982312SY.b. Lu 	bool do_tstamp;
345d3982312SY.b. Lu 	u64 tstamp = 0;
346d4fd0404SClaudiu Manoil 
347d4fd0404SClaudiu Manoil 	i = tx_ring->next_to_clean;
348d4fd0404SClaudiu Manoil 	tx_swbd = &tx_ring->tx_swbd[i];
349d4fd0404SClaudiu Manoil 	bds_to_clean = enetc_bd_ready_count(tx_ring, i);
350d4fd0404SClaudiu Manoil 
351d3982312SY.b. Lu 	do_tstamp = false;
352d3982312SY.b. Lu 
353d4fd0404SClaudiu Manoil 	while (bds_to_clean && tx_frm_cnt < ENETC_DEFAULT_TX_WORK) {
354d4fd0404SClaudiu Manoil 		bool is_eof = !!tx_swbd->skb;
355d4fd0404SClaudiu Manoil 
356d3982312SY.b. Lu 		if (unlikely(tx_swbd->check_wb)) {
357d3982312SY.b. Lu 			struct enetc_ndev_priv *priv = netdev_priv(ndev);
358d3982312SY.b. Lu 			union enetc_tx_bd *txbd;
359d3982312SY.b. Lu 
360d3982312SY.b. Lu 			txbd = ENETC_TXBD(*tx_ring, i);
361d3982312SY.b. Lu 
362d3982312SY.b. Lu 			if (txbd->flags & ENETC_TXBD_FLAGS_W &&
363d3982312SY.b. Lu 			    tx_swbd->do_tstamp) {
364d3982312SY.b. Lu 				enetc_get_tx_tstamp(&priv->si->hw, txbd,
365d3982312SY.b. Lu 						    &tstamp);
366d3982312SY.b. Lu 				do_tstamp = true;
367d3982312SY.b. Lu 			}
368d3982312SY.b. Lu 		}
369d3982312SY.b. Lu 
370f4a0be84SClaudiu Manoil 		if (likely(tx_swbd->dma))
371d4fd0404SClaudiu Manoil 			enetc_unmap_tx_buff(tx_ring, tx_swbd);
372f4a0be84SClaudiu Manoil 
373d4fd0404SClaudiu Manoil 		if (is_eof) {
374d3982312SY.b. Lu 			if (unlikely(do_tstamp)) {
375d3982312SY.b. Lu 				enetc_tstamp_tx(tx_swbd->skb, tstamp);
376d3982312SY.b. Lu 				do_tstamp = false;
377d3982312SY.b. Lu 			}
378d4fd0404SClaudiu Manoil 			napi_consume_skb(tx_swbd->skb, napi_budget);
379d4fd0404SClaudiu Manoil 			tx_swbd->skb = NULL;
380d4fd0404SClaudiu Manoil 		}
381d4fd0404SClaudiu Manoil 
382d4fd0404SClaudiu Manoil 		tx_byte_cnt += tx_swbd->len;
383d4fd0404SClaudiu Manoil 
384d4fd0404SClaudiu Manoil 		bds_to_clean--;
385d4fd0404SClaudiu Manoil 		tx_swbd++;
386d4fd0404SClaudiu Manoil 		i++;
387d4fd0404SClaudiu Manoil 		if (unlikely(i == tx_ring->bd_count)) {
388d4fd0404SClaudiu Manoil 			i = 0;
389d4fd0404SClaudiu Manoil 			tx_swbd = tx_ring->tx_swbd;
390d4fd0404SClaudiu Manoil 		}
391d4fd0404SClaudiu Manoil 
392d4fd0404SClaudiu Manoil 		/* BD iteration loop end */
393d4fd0404SClaudiu Manoil 		if (is_eof) {
394d4fd0404SClaudiu Manoil 			tx_frm_cnt++;
395d4fd0404SClaudiu Manoil 			/* re-arm interrupt source */
396d4fd0404SClaudiu Manoil 			enetc_wr_reg(tx_ring->idr, BIT(tx_ring->index) |
397d4fd0404SClaudiu Manoil 				     BIT(16 + tx_ring->index));
398d4fd0404SClaudiu Manoil 		}
399d4fd0404SClaudiu Manoil 
400d4fd0404SClaudiu Manoil 		if (unlikely(!bds_to_clean))
401d4fd0404SClaudiu Manoil 			bds_to_clean = enetc_bd_ready_count(tx_ring, i);
402d4fd0404SClaudiu Manoil 	}
403d4fd0404SClaudiu Manoil 
404d4fd0404SClaudiu Manoil 	tx_ring->next_to_clean = i;
405d4fd0404SClaudiu Manoil 	tx_ring->stats.packets += tx_frm_cnt;
406d4fd0404SClaudiu Manoil 	tx_ring->stats.bytes += tx_byte_cnt;
407d4fd0404SClaudiu Manoil 
408d4fd0404SClaudiu Manoil 	if (unlikely(tx_frm_cnt && netif_carrier_ok(ndev) &&
409d4fd0404SClaudiu Manoil 		     __netif_subqueue_stopped(ndev, tx_ring->index) &&
410d4fd0404SClaudiu Manoil 		     (enetc_bd_unused(tx_ring) >= ENETC_TXBDS_MAX_NEEDED))) {
411d4fd0404SClaudiu Manoil 		netif_wake_subqueue(ndev, tx_ring->index);
412d4fd0404SClaudiu Manoil 	}
413d4fd0404SClaudiu Manoil 
414d4fd0404SClaudiu Manoil 	return tx_frm_cnt != ENETC_DEFAULT_TX_WORK;
415d4fd0404SClaudiu Manoil }
416d4fd0404SClaudiu Manoil 
417d4fd0404SClaudiu Manoil static bool enetc_new_page(struct enetc_bdr *rx_ring,
418d4fd0404SClaudiu Manoil 			   struct enetc_rx_swbd *rx_swbd)
419d4fd0404SClaudiu Manoil {
420d4fd0404SClaudiu Manoil 	struct page *page;
421d4fd0404SClaudiu Manoil 	dma_addr_t addr;
422d4fd0404SClaudiu Manoil 
423d4fd0404SClaudiu Manoil 	page = dev_alloc_page();
424d4fd0404SClaudiu Manoil 	if (unlikely(!page))
425d4fd0404SClaudiu Manoil 		return false;
426d4fd0404SClaudiu Manoil 
427d4fd0404SClaudiu Manoil 	addr = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
428d4fd0404SClaudiu Manoil 	if (unlikely(dma_mapping_error(rx_ring->dev, addr))) {
429d4fd0404SClaudiu Manoil 		__free_page(page);
430d4fd0404SClaudiu Manoil 
431d4fd0404SClaudiu Manoil 		return false;
432d4fd0404SClaudiu Manoil 	}
433d4fd0404SClaudiu Manoil 
434d4fd0404SClaudiu Manoil 	rx_swbd->dma = addr;
435d4fd0404SClaudiu Manoil 	rx_swbd->page = page;
436d4fd0404SClaudiu Manoil 	rx_swbd->page_offset = ENETC_RXB_PAD;
437d4fd0404SClaudiu Manoil 
438d4fd0404SClaudiu Manoil 	return true;
439d4fd0404SClaudiu Manoil }
440d4fd0404SClaudiu Manoil 
441d4fd0404SClaudiu Manoil static int enetc_refill_rx_ring(struct enetc_bdr *rx_ring, const int buff_cnt)
442d4fd0404SClaudiu Manoil {
443d4fd0404SClaudiu Manoil 	struct enetc_rx_swbd *rx_swbd;
444d4fd0404SClaudiu Manoil 	union enetc_rx_bd *rxbd;
445d4fd0404SClaudiu Manoil 	int i, j;
446d4fd0404SClaudiu Manoil 
447d4fd0404SClaudiu Manoil 	i = rx_ring->next_to_use;
448d4fd0404SClaudiu Manoil 	rx_swbd = &rx_ring->rx_swbd[i];
449714239acSClaudiu Manoil 	rxbd = enetc_rxbd(rx_ring, i);
450d4fd0404SClaudiu Manoil 
451d4fd0404SClaudiu Manoil 	for (j = 0; j < buff_cnt; j++) {
452d4fd0404SClaudiu Manoil 		/* try reuse page */
453d4fd0404SClaudiu Manoil 		if (unlikely(!rx_swbd->page)) {
454d4fd0404SClaudiu Manoil 			if (unlikely(!enetc_new_page(rx_ring, rx_swbd))) {
455d4fd0404SClaudiu Manoil 				rx_ring->stats.rx_alloc_errs++;
456d4fd0404SClaudiu Manoil 				break;
457d4fd0404SClaudiu Manoil 			}
458d4fd0404SClaudiu Manoil 		}
459d4fd0404SClaudiu Manoil 
460d4fd0404SClaudiu Manoil 		/* update RxBD */
461d4fd0404SClaudiu Manoil 		rxbd->w.addr = cpu_to_le64(rx_swbd->dma +
462d4fd0404SClaudiu Manoil 					   rx_swbd->page_offset);
463d4fd0404SClaudiu Manoil 		/* clear 'R" as well */
464d4fd0404SClaudiu Manoil 		rxbd->r.lstatus = 0;
465d4fd0404SClaudiu Manoil 
466714239acSClaudiu Manoil 		rxbd = enetc_rxbd_next(rx_ring, rxbd, i);
467d4fd0404SClaudiu Manoil 		rx_swbd++;
468d4fd0404SClaudiu Manoil 		i++;
469d4fd0404SClaudiu Manoil 		if (unlikely(i == rx_ring->bd_count)) {
470d4fd0404SClaudiu Manoil 			i = 0;
471d4fd0404SClaudiu Manoil 			rx_swbd = rx_ring->rx_swbd;
472d4fd0404SClaudiu Manoil 		}
473d4fd0404SClaudiu Manoil 	}
474d4fd0404SClaudiu Manoil 
475d4fd0404SClaudiu Manoil 	if (likely(j)) {
476d4fd0404SClaudiu Manoil 		rx_ring->next_to_alloc = i; /* keep track from page reuse */
477d4fd0404SClaudiu Manoil 		rx_ring->next_to_use = i;
478d4fd0404SClaudiu Manoil 		/* update ENETC's consumer index */
479d4fd0404SClaudiu Manoil 		enetc_wr_reg(rx_ring->rcir, i);
480d4fd0404SClaudiu Manoil 	}
481d4fd0404SClaudiu Manoil 
482d4fd0404SClaudiu Manoil 	return j;
483d4fd0404SClaudiu Manoil }
484d4fd0404SClaudiu Manoil 
485434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
486d3982312SY.b. Lu static void enetc_get_rx_tstamp(struct net_device *ndev,
487d3982312SY.b. Lu 				union enetc_rx_bd *rxbd,
488d3982312SY.b. Lu 				struct sk_buff *skb)
489d3982312SY.b. Lu {
490d3982312SY.b. Lu 	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
491d3982312SY.b. Lu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
492d3982312SY.b. Lu 	struct enetc_hw *hw = &priv->si->hw;
493cec4f328SY.b. Lu 	u32 lo, hi, tstamp_lo;
494d3982312SY.b. Lu 	u64 tstamp;
495d3982312SY.b. Lu 
496cec4f328SY.b. Lu 	if (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_TSTMP) {
497d3982312SY.b. Lu 		lo = enetc_rd(hw, ENETC_SICTR0);
498d3982312SY.b. Lu 		hi = enetc_rd(hw, ENETC_SICTR1);
499434cebabSClaudiu Manoil 		rxbd = enetc_rxbd_ext(rxbd);
500434cebabSClaudiu Manoil 		tstamp_lo = le32_to_cpu(rxbd->ext.tstamp);
501cec4f328SY.b. Lu 		if (lo <= tstamp_lo)
502d3982312SY.b. Lu 			hi -= 1;
503d3982312SY.b. Lu 
504cec4f328SY.b. Lu 		tstamp = (u64)hi << 32 | tstamp_lo;
505d3982312SY.b. Lu 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
506d3982312SY.b. Lu 		shhwtstamps->hwtstamp = ns_to_ktime(tstamp);
507d3982312SY.b. Lu 	}
508d3982312SY.b. Lu }
509d3982312SY.b. Lu #endif
510d3982312SY.b. Lu 
511d4fd0404SClaudiu Manoil static void enetc_get_offloads(struct enetc_bdr *rx_ring,
512d4fd0404SClaudiu Manoil 			       union enetc_rx_bd *rxbd, struct sk_buff *skb)
513d4fd0404SClaudiu Manoil {
514434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
515d3982312SY.b. Lu 	struct enetc_ndev_priv *priv = netdev_priv(rx_ring->ndev);
516d3982312SY.b. Lu #endif
517d3982312SY.b. Lu 	/* TODO: hashing */
518d4fd0404SClaudiu Manoil 	if (rx_ring->ndev->features & NETIF_F_RXCSUM) {
519d4fd0404SClaudiu Manoil 		u16 inet_csum = le16_to_cpu(rxbd->r.inet_csum);
520d4fd0404SClaudiu Manoil 
521d4fd0404SClaudiu Manoil 		skb->csum = csum_unfold((__force __sum16)~htons(inet_csum));
522d4fd0404SClaudiu Manoil 		skb->ip_summed = CHECKSUM_COMPLETE;
523d4fd0404SClaudiu Manoil 	}
524d4fd0404SClaudiu Manoil 
525d4fd0404SClaudiu Manoil 	/* copy VLAN to skb, if one is extracted, for now we assume it's a
526d4fd0404SClaudiu Manoil 	 * standard TPID, but HW also supports custom values
527d4fd0404SClaudiu Manoil 	 */
528d4fd0404SClaudiu Manoil 	if (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_VLAN)
529d4fd0404SClaudiu Manoil 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
530d4fd0404SClaudiu Manoil 				       le16_to_cpu(rxbd->r.vlan_opt));
531434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
532d3982312SY.b. Lu 	if (priv->active_offloads & ENETC_F_RX_TSTAMP)
533d3982312SY.b. Lu 		enetc_get_rx_tstamp(rx_ring->ndev, rxbd, skb);
534d3982312SY.b. Lu #endif
535d4fd0404SClaudiu Manoil }
536d4fd0404SClaudiu Manoil 
537d4fd0404SClaudiu Manoil static void enetc_process_skb(struct enetc_bdr *rx_ring,
538d4fd0404SClaudiu Manoil 			      struct sk_buff *skb)
539d4fd0404SClaudiu Manoil {
540d4fd0404SClaudiu Manoil 	skb_record_rx_queue(skb, rx_ring->index);
541d4fd0404SClaudiu Manoil 	skb->protocol = eth_type_trans(skb, rx_ring->ndev);
542d4fd0404SClaudiu Manoil }
543d4fd0404SClaudiu Manoil 
544d4fd0404SClaudiu Manoil static bool enetc_page_reusable(struct page *page)
545d4fd0404SClaudiu Manoil {
546d4fd0404SClaudiu Manoil 	return (!page_is_pfmemalloc(page) && page_ref_count(page) == 1);
547d4fd0404SClaudiu Manoil }
548d4fd0404SClaudiu Manoil 
549d4fd0404SClaudiu Manoil static void enetc_reuse_page(struct enetc_bdr *rx_ring,
550d4fd0404SClaudiu Manoil 			     struct enetc_rx_swbd *old)
551d4fd0404SClaudiu Manoil {
552d4fd0404SClaudiu Manoil 	struct enetc_rx_swbd *new;
553d4fd0404SClaudiu Manoil 
554d4fd0404SClaudiu Manoil 	new = &rx_ring->rx_swbd[rx_ring->next_to_alloc];
555d4fd0404SClaudiu Manoil 
556d4fd0404SClaudiu Manoil 	/* next buf that may reuse a page */
557d4fd0404SClaudiu Manoil 	enetc_bdr_idx_inc(rx_ring, &rx_ring->next_to_alloc);
558d4fd0404SClaudiu Manoil 
559d4fd0404SClaudiu Manoil 	/* copy page reference */
560d4fd0404SClaudiu Manoil 	*new = *old;
561d4fd0404SClaudiu Manoil }
562d4fd0404SClaudiu Manoil 
563d4fd0404SClaudiu Manoil static struct enetc_rx_swbd *enetc_get_rx_buff(struct enetc_bdr *rx_ring,
564d4fd0404SClaudiu Manoil 					       int i, u16 size)
565d4fd0404SClaudiu Manoil {
566d4fd0404SClaudiu Manoil 	struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[i];
567d4fd0404SClaudiu Manoil 
568d4fd0404SClaudiu Manoil 	dma_sync_single_range_for_cpu(rx_ring->dev, rx_swbd->dma,
569d4fd0404SClaudiu Manoil 				      rx_swbd->page_offset,
570d4fd0404SClaudiu Manoil 				      size, DMA_FROM_DEVICE);
571d4fd0404SClaudiu Manoil 	return rx_swbd;
572d4fd0404SClaudiu Manoil }
573d4fd0404SClaudiu Manoil 
574d4fd0404SClaudiu Manoil static void enetc_put_rx_buff(struct enetc_bdr *rx_ring,
575d4fd0404SClaudiu Manoil 			      struct enetc_rx_swbd *rx_swbd)
576d4fd0404SClaudiu Manoil {
577d4fd0404SClaudiu Manoil 	if (likely(enetc_page_reusable(rx_swbd->page))) {
578d4fd0404SClaudiu Manoil 		rx_swbd->page_offset ^= ENETC_RXB_TRUESIZE;
579d4fd0404SClaudiu Manoil 		page_ref_inc(rx_swbd->page);
580d4fd0404SClaudiu Manoil 
581d4fd0404SClaudiu Manoil 		enetc_reuse_page(rx_ring, rx_swbd);
582d4fd0404SClaudiu Manoil 
583d4fd0404SClaudiu Manoil 		/* sync for use by the device */
584d4fd0404SClaudiu Manoil 		dma_sync_single_range_for_device(rx_ring->dev, rx_swbd->dma,
585d4fd0404SClaudiu Manoil 						 rx_swbd->page_offset,
586d4fd0404SClaudiu Manoil 						 ENETC_RXB_DMA_SIZE,
587d4fd0404SClaudiu Manoil 						 DMA_FROM_DEVICE);
588d4fd0404SClaudiu Manoil 	} else {
589d4fd0404SClaudiu Manoil 		dma_unmap_page(rx_ring->dev, rx_swbd->dma,
590d4fd0404SClaudiu Manoil 			       PAGE_SIZE, DMA_FROM_DEVICE);
591d4fd0404SClaudiu Manoil 	}
592d4fd0404SClaudiu Manoil 
593d4fd0404SClaudiu Manoil 	rx_swbd->page = NULL;
594d4fd0404SClaudiu Manoil }
595d4fd0404SClaudiu Manoil 
596d4fd0404SClaudiu Manoil static struct sk_buff *enetc_map_rx_buff_to_skb(struct enetc_bdr *rx_ring,
597d4fd0404SClaudiu Manoil 						int i, u16 size)
598d4fd0404SClaudiu Manoil {
599d4fd0404SClaudiu Manoil 	struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size);
600d4fd0404SClaudiu Manoil 	struct sk_buff *skb;
601d4fd0404SClaudiu Manoil 	void *ba;
602d4fd0404SClaudiu Manoil 
603d4fd0404SClaudiu Manoil 	ba = page_address(rx_swbd->page) + rx_swbd->page_offset;
604d4fd0404SClaudiu Manoil 	skb = build_skb(ba - ENETC_RXB_PAD, ENETC_RXB_TRUESIZE);
605d4fd0404SClaudiu Manoil 	if (unlikely(!skb)) {
606d4fd0404SClaudiu Manoil 		rx_ring->stats.rx_alloc_errs++;
607d4fd0404SClaudiu Manoil 		return NULL;
608d4fd0404SClaudiu Manoil 	}
609d4fd0404SClaudiu Manoil 
610d4fd0404SClaudiu Manoil 	skb_reserve(skb, ENETC_RXB_PAD);
611d4fd0404SClaudiu Manoil 	__skb_put(skb, size);
612d4fd0404SClaudiu Manoil 
613d4fd0404SClaudiu Manoil 	enetc_put_rx_buff(rx_ring, rx_swbd);
614d4fd0404SClaudiu Manoil 
615d4fd0404SClaudiu Manoil 	return skb;
616d4fd0404SClaudiu Manoil }
617d4fd0404SClaudiu Manoil 
618d4fd0404SClaudiu Manoil static void enetc_add_rx_buff_to_skb(struct enetc_bdr *rx_ring, int i,
619d4fd0404SClaudiu Manoil 				     u16 size, struct sk_buff *skb)
620d4fd0404SClaudiu Manoil {
621d4fd0404SClaudiu Manoil 	struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size);
622d4fd0404SClaudiu Manoil 
623d4fd0404SClaudiu Manoil 	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_swbd->page,
624d4fd0404SClaudiu Manoil 			rx_swbd->page_offset, size, ENETC_RXB_TRUESIZE);
625d4fd0404SClaudiu Manoil 
626d4fd0404SClaudiu Manoil 	enetc_put_rx_buff(rx_ring, rx_swbd);
627d4fd0404SClaudiu Manoil }
628d4fd0404SClaudiu Manoil 
629d4fd0404SClaudiu Manoil #define ENETC_RXBD_BUNDLE 16 /* # of BDs to update at once */
630d4fd0404SClaudiu Manoil 
631d4fd0404SClaudiu Manoil static int enetc_clean_rx_ring(struct enetc_bdr *rx_ring,
632d4fd0404SClaudiu Manoil 			       struct napi_struct *napi, int work_limit)
633d4fd0404SClaudiu Manoil {
634d4fd0404SClaudiu Manoil 	int rx_frm_cnt = 0, rx_byte_cnt = 0;
635d4fd0404SClaudiu Manoil 	int cleaned_cnt, i;
636d4fd0404SClaudiu Manoil 
637d4fd0404SClaudiu Manoil 	cleaned_cnt = enetc_bd_unused(rx_ring);
638d4fd0404SClaudiu Manoil 	/* next descriptor to process */
639d4fd0404SClaudiu Manoil 	i = rx_ring->next_to_clean;
640d4fd0404SClaudiu Manoil 
641d4fd0404SClaudiu Manoil 	while (likely(rx_frm_cnt < work_limit)) {
642d4fd0404SClaudiu Manoil 		union enetc_rx_bd *rxbd;
643d4fd0404SClaudiu Manoil 		struct sk_buff *skb;
644d4fd0404SClaudiu Manoil 		u32 bd_status;
645d4fd0404SClaudiu Manoil 		u16 size;
646d4fd0404SClaudiu Manoil 
647d4fd0404SClaudiu Manoil 		if (cleaned_cnt >= ENETC_RXBD_BUNDLE) {
648d4fd0404SClaudiu Manoil 			int count = enetc_refill_rx_ring(rx_ring, cleaned_cnt);
649d4fd0404SClaudiu Manoil 
650d4fd0404SClaudiu Manoil 			cleaned_cnt -= count;
651d4fd0404SClaudiu Manoil 		}
652d4fd0404SClaudiu Manoil 
653714239acSClaudiu Manoil 		rxbd = enetc_rxbd(rx_ring, i);
654d4fd0404SClaudiu Manoil 		bd_status = le32_to_cpu(rxbd->r.lstatus);
655d4fd0404SClaudiu Manoil 		if (!bd_status)
656d4fd0404SClaudiu Manoil 			break;
657d4fd0404SClaudiu Manoil 
658d4fd0404SClaudiu Manoil 		enetc_wr_reg(rx_ring->idr, BIT(rx_ring->index));
659d4fd0404SClaudiu Manoil 		dma_rmb(); /* for reading other rxbd fields */
660d4fd0404SClaudiu Manoil 		size = le16_to_cpu(rxbd->r.buf_len);
661d4fd0404SClaudiu Manoil 		skb = enetc_map_rx_buff_to_skb(rx_ring, i, size);
662d4fd0404SClaudiu Manoil 		if (!skb)
663d4fd0404SClaudiu Manoil 			break;
664d4fd0404SClaudiu Manoil 
665d4fd0404SClaudiu Manoil 		enetc_get_offloads(rx_ring, rxbd, skb);
666d4fd0404SClaudiu Manoil 
667d4fd0404SClaudiu Manoil 		cleaned_cnt++;
668714239acSClaudiu Manoil 
669714239acSClaudiu Manoil 		rxbd = enetc_rxbd_next(rx_ring, rxbd, i);
670714239acSClaudiu Manoil 		if (unlikely(++i == rx_ring->bd_count))
671d4fd0404SClaudiu Manoil 			i = 0;
672d4fd0404SClaudiu Manoil 
673d4fd0404SClaudiu Manoil 		if (unlikely(bd_status &
674d4fd0404SClaudiu Manoil 			     ENETC_RXBD_LSTATUS(ENETC_RXBD_ERR_MASK))) {
675d4fd0404SClaudiu Manoil 			dev_kfree_skb(skb);
676d4fd0404SClaudiu Manoil 			while (!(bd_status & ENETC_RXBD_LSTATUS_F)) {
677d4fd0404SClaudiu Manoil 				dma_rmb();
678d4fd0404SClaudiu Manoil 				bd_status = le32_to_cpu(rxbd->r.lstatus);
679714239acSClaudiu Manoil 
680714239acSClaudiu Manoil 				rxbd = enetc_rxbd_next(rx_ring, rxbd, i);
681714239acSClaudiu Manoil 				if (unlikely(++i == rx_ring->bd_count))
682d4fd0404SClaudiu Manoil 					i = 0;
683d4fd0404SClaudiu Manoil 			}
684d4fd0404SClaudiu Manoil 
685d4fd0404SClaudiu Manoil 			rx_ring->ndev->stats.rx_dropped++;
686d4fd0404SClaudiu Manoil 			rx_ring->ndev->stats.rx_errors++;
687d4fd0404SClaudiu Manoil 
688d4fd0404SClaudiu Manoil 			break;
689d4fd0404SClaudiu Manoil 		}
690d4fd0404SClaudiu Manoil 
691d4fd0404SClaudiu Manoil 		/* not last BD in frame? */
692d4fd0404SClaudiu Manoil 		while (!(bd_status & ENETC_RXBD_LSTATUS_F)) {
693d4fd0404SClaudiu Manoil 			bd_status = le32_to_cpu(rxbd->r.lstatus);
694d4fd0404SClaudiu Manoil 			size = ENETC_RXB_DMA_SIZE;
695d4fd0404SClaudiu Manoil 
696d4fd0404SClaudiu Manoil 			if (bd_status & ENETC_RXBD_LSTATUS_F) {
697d4fd0404SClaudiu Manoil 				dma_rmb();
698d4fd0404SClaudiu Manoil 				size = le16_to_cpu(rxbd->r.buf_len);
699d4fd0404SClaudiu Manoil 			}
700d4fd0404SClaudiu Manoil 
701d4fd0404SClaudiu Manoil 			enetc_add_rx_buff_to_skb(rx_ring, i, size, skb);
702d4fd0404SClaudiu Manoil 
703d4fd0404SClaudiu Manoil 			cleaned_cnt++;
704714239acSClaudiu Manoil 
705714239acSClaudiu Manoil 			rxbd = enetc_rxbd_next(rx_ring, rxbd, i);
706714239acSClaudiu Manoil 			if (unlikely(++i == rx_ring->bd_count))
707d4fd0404SClaudiu Manoil 				i = 0;
708d4fd0404SClaudiu Manoil 		}
709d4fd0404SClaudiu Manoil 
710d4fd0404SClaudiu Manoil 		rx_byte_cnt += skb->len;
711d4fd0404SClaudiu Manoil 
712d4fd0404SClaudiu Manoil 		enetc_process_skb(rx_ring, skb);
713d4fd0404SClaudiu Manoil 
714d4fd0404SClaudiu Manoil 		napi_gro_receive(napi, skb);
715d4fd0404SClaudiu Manoil 
716d4fd0404SClaudiu Manoil 		rx_frm_cnt++;
717d4fd0404SClaudiu Manoil 	}
718d4fd0404SClaudiu Manoil 
719d4fd0404SClaudiu Manoil 	rx_ring->next_to_clean = i;
720d4fd0404SClaudiu Manoil 
721d4fd0404SClaudiu Manoil 	rx_ring->stats.packets += rx_frm_cnt;
722d4fd0404SClaudiu Manoil 	rx_ring->stats.bytes += rx_byte_cnt;
723d4fd0404SClaudiu Manoil 
724d4fd0404SClaudiu Manoil 	return rx_frm_cnt;
725d4fd0404SClaudiu Manoil }
726d4fd0404SClaudiu Manoil 
727d4fd0404SClaudiu Manoil /* Probing and Init */
728d382563fSClaudiu Manoil #define ENETC_MAX_RFS_SIZE 64
729d4fd0404SClaudiu Manoil void enetc_get_si_caps(struct enetc_si *si)
730d4fd0404SClaudiu Manoil {
731d4fd0404SClaudiu Manoil 	struct enetc_hw *hw = &si->hw;
732d4fd0404SClaudiu Manoil 	u32 val;
733d4fd0404SClaudiu Manoil 
734d4fd0404SClaudiu Manoil 	/* find out how many of various resources we have to work with */
735d4fd0404SClaudiu Manoil 	val = enetc_rd(hw, ENETC_SICAPR0);
736d4fd0404SClaudiu Manoil 	si->num_rx_rings = (val >> 16) & 0xff;
737d4fd0404SClaudiu Manoil 	si->num_tx_rings = val & 0xff;
738d382563fSClaudiu Manoil 
739d382563fSClaudiu Manoil 	val = enetc_rd(hw, ENETC_SIRFSCAPR);
740d382563fSClaudiu Manoil 	si->num_fs_entries = ENETC_SIRFSCAPR_GET_NUM_RFS(val);
741d382563fSClaudiu Manoil 	si->num_fs_entries = min(si->num_fs_entries, ENETC_MAX_RFS_SIZE);
742d382563fSClaudiu Manoil 
743d382563fSClaudiu Manoil 	si->num_rss = 0;
744d382563fSClaudiu Manoil 	val = enetc_rd(hw, ENETC_SIPCAPR0);
745d382563fSClaudiu Manoil 	if (val & ENETC_SIPCAPR0_RSS) {
7462e47cb41SPo Liu 		u32 rss;
7472e47cb41SPo Liu 
7482e47cb41SPo Liu 		rss = enetc_rd(hw, ENETC_SIRSSCAPR);
7492e47cb41SPo Liu 		si->num_rss = ENETC_SIRSSCAPR_GET_NUM_RSS(rss);
750d382563fSClaudiu Manoil 	}
7512e47cb41SPo Liu 
7522e47cb41SPo Liu 	if (val & ENETC_SIPCAPR0_QBV)
7532e47cb41SPo Liu 		si->hw_features |= ENETC_SI_F_QBV;
75479e49982SPo Liu 
75579e49982SPo Liu 	if (val & ENETC_SIPCAPR0_PSFP)
75679e49982SPo Liu 		si->hw_features |= ENETC_SI_F_PSFP;
757d4fd0404SClaudiu Manoil }
758d4fd0404SClaudiu Manoil 
759d4fd0404SClaudiu Manoil static int enetc_dma_alloc_bdr(struct enetc_bdr *r, size_t bd_size)
760d4fd0404SClaudiu Manoil {
761d4fd0404SClaudiu Manoil 	r->bd_base = dma_alloc_coherent(r->dev, r->bd_count * bd_size,
762d4fd0404SClaudiu Manoil 					&r->bd_dma_base, GFP_KERNEL);
763d4fd0404SClaudiu Manoil 	if (!r->bd_base)
764d4fd0404SClaudiu Manoil 		return -ENOMEM;
765d4fd0404SClaudiu Manoil 
766d4fd0404SClaudiu Manoil 	/* h/w requires 128B alignment */
767d4fd0404SClaudiu Manoil 	if (!IS_ALIGNED(r->bd_dma_base, 128)) {
768d4fd0404SClaudiu Manoil 		dma_free_coherent(r->dev, r->bd_count * bd_size, r->bd_base,
769d4fd0404SClaudiu Manoil 				  r->bd_dma_base);
770d4fd0404SClaudiu Manoil 		return -EINVAL;
771d4fd0404SClaudiu Manoil 	}
772d4fd0404SClaudiu Manoil 
773d4fd0404SClaudiu Manoil 	return 0;
774d4fd0404SClaudiu Manoil }
775d4fd0404SClaudiu Manoil 
776d4fd0404SClaudiu Manoil static int enetc_alloc_txbdr(struct enetc_bdr *txr)
777d4fd0404SClaudiu Manoil {
778d4fd0404SClaudiu Manoil 	int err;
779d4fd0404SClaudiu Manoil 
780d4fd0404SClaudiu Manoil 	txr->tx_swbd = vzalloc(txr->bd_count * sizeof(struct enetc_tx_swbd));
781d4fd0404SClaudiu Manoil 	if (!txr->tx_swbd)
782d4fd0404SClaudiu Manoil 		return -ENOMEM;
783d4fd0404SClaudiu Manoil 
784d4fd0404SClaudiu Manoil 	err = enetc_dma_alloc_bdr(txr, sizeof(union enetc_tx_bd));
785d4fd0404SClaudiu Manoil 	if (err) {
786d4fd0404SClaudiu Manoil 		vfree(txr->tx_swbd);
787d4fd0404SClaudiu Manoil 		return err;
788d4fd0404SClaudiu Manoil 	}
789d4fd0404SClaudiu Manoil 
790d4fd0404SClaudiu Manoil 	txr->next_to_clean = 0;
791d4fd0404SClaudiu Manoil 	txr->next_to_use = 0;
792d4fd0404SClaudiu Manoil 
793d4fd0404SClaudiu Manoil 	return 0;
794d4fd0404SClaudiu Manoil }
795d4fd0404SClaudiu Manoil 
796d4fd0404SClaudiu Manoil static void enetc_free_txbdr(struct enetc_bdr *txr)
797d4fd0404SClaudiu Manoil {
798d4fd0404SClaudiu Manoil 	int size, i;
799d4fd0404SClaudiu Manoil 
800d4fd0404SClaudiu Manoil 	for (i = 0; i < txr->bd_count; i++)
801d4fd0404SClaudiu Manoil 		enetc_free_tx_skb(txr, &txr->tx_swbd[i]);
802d4fd0404SClaudiu Manoil 
803d4fd0404SClaudiu Manoil 	size = txr->bd_count * sizeof(union enetc_tx_bd);
804d4fd0404SClaudiu Manoil 
805d4fd0404SClaudiu Manoil 	dma_free_coherent(txr->dev, size, txr->bd_base, txr->bd_dma_base);
806d4fd0404SClaudiu Manoil 	txr->bd_base = NULL;
807d4fd0404SClaudiu Manoil 
808d4fd0404SClaudiu Manoil 	vfree(txr->tx_swbd);
809d4fd0404SClaudiu Manoil 	txr->tx_swbd = NULL;
810d4fd0404SClaudiu Manoil }
811d4fd0404SClaudiu Manoil 
812d4fd0404SClaudiu Manoil static int enetc_alloc_tx_resources(struct enetc_ndev_priv *priv)
813d4fd0404SClaudiu Manoil {
814d4fd0404SClaudiu Manoil 	int i, err;
815d4fd0404SClaudiu Manoil 
816d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++) {
817d4fd0404SClaudiu Manoil 		err = enetc_alloc_txbdr(priv->tx_ring[i]);
818d4fd0404SClaudiu Manoil 
819d4fd0404SClaudiu Manoil 		if (err)
820d4fd0404SClaudiu Manoil 			goto fail;
821d4fd0404SClaudiu Manoil 	}
822d4fd0404SClaudiu Manoil 
823d4fd0404SClaudiu Manoil 	return 0;
824d4fd0404SClaudiu Manoil 
825d4fd0404SClaudiu Manoil fail:
826d4fd0404SClaudiu Manoil 	while (i-- > 0)
827d4fd0404SClaudiu Manoil 		enetc_free_txbdr(priv->tx_ring[i]);
828d4fd0404SClaudiu Manoil 
829d4fd0404SClaudiu Manoil 	return err;
830d4fd0404SClaudiu Manoil }
831d4fd0404SClaudiu Manoil 
832d4fd0404SClaudiu Manoil static void enetc_free_tx_resources(struct enetc_ndev_priv *priv)
833d4fd0404SClaudiu Manoil {
834d4fd0404SClaudiu Manoil 	int i;
835d4fd0404SClaudiu Manoil 
836d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
837d4fd0404SClaudiu Manoil 		enetc_free_txbdr(priv->tx_ring[i]);
838d4fd0404SClaudiu Manoil }
839d4fd0404SClaudiu Manoil 
840434cebabSClaudiu Manoil static int enetc_alloc_rxbdr(struct enetc_bdr *rxr, bool extended)
841d4fd0404SClaudiu Manoil {
842434cebabSClaudiu Manoil 	size_t size = sizeof(union enetc_rx_bd);
843d4fd0404SClaudiu Manoil 	int err;
844d4fd0404SClaudiu Manoil 
845d4fd0404SClaudiu Manoil 	rxr->rx_swbd = vzalloc(rxr->bd_count * sizeof(struct enetc_rx_swbd));
846d4fd0404SClaudiu Manoil 	if (!rxr->rx_swbd)
847d4fd0404SClaudiu Manoil 		return -ENOMEM;
848d4fd0404SClaudiu Manoil 
849434cebabSClaudiu Manoil 	if (extended)
850434cebabSClaudiu Manoil 		size *= 2;
851434cebabSClaudiu Manoil 
852434cebabSClaudiu Manoil 	err = enetc_dma_alloc_bdr(rxr, size);
853d4fd0404SClaudiu Manoil 	if (err) {
854d4fd0404SClaudiu Manoil 		vfree(rxr->rx_swbd);
855d4fd0404SClaudiu Manoil 		return err;
856d4fd0404SClaudiu Manoil 	}
857d4fd0404SClaudiu Manoil 
858d4fd0404SClaudiu Manoil 	rxr->next_to_clean = 0;
859d4fd0404SClaudiu Manoil 	rxr->next_to_use = 0;
860d4fd0404SClaudiu Manoil 	rxr->next_to_alloc = 0;
861434cebabSClaudiu Manoil 	rxr->ext_en = extended;
862d4fd0404SClaudiu Manoil 
863d4fd0404SClaudiu Manoil 	return 0;
864d4fd0404SClaudiu Manoil }
865d4fd0404SClaudiu Manoil 
866d4fd0404SClaudiu Manoil static void enetc_free_rxbdr(struct enetc_bdr *rxr)
867d4fd0404SClaudiu Manoil {
868d4fd0404SClaudiu Manoil 	int size;
869d4fd0404SClaudiu Manoil 
870d4fd0404SClaudiu Manoil 	size = rxr->bd_count * sizeof(union enetc_rx_bd);
871d4fd0404SClaudiu Manoil 
872d4fd0404SClaudiu Manoil 	dma_free_coherent(rxr->dev, size, rxr->bd_base, rxr->bd_dma_base);
873d4fd0404SClaudiu Manoil 	rxr->bd_base = NULL;
874d4fd0404SClaudiu Manoil 
875d4fd0404SClaudiu Manoil 	vfree(rxr->rx_swbd);
876d4fd0404SClaudiu Manoil 	rxr->rx_swbd = NULL;
877d4fd0404SClaudiu Manoil }
878d4fd0404SClaudiu Manoil 
879d4fd0404SClaudiu Manoil static int enetc_alloc_rx_resources(struct enetc_ndev_priv *priv)
880d4fd0404SClaudiu Manoil {
881434cebabSClaudiu Manoil 	bool extended = !!(priv->active_offloads & ENETC_F_RX_TSTAMP);
882d4fd0404SClaudiu Manoil 	int i, err;
883d4fd0404SClaudiu Manoil 
884d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++) {
885434cebabSClaudiu Manoil 		err = enetc_alloc_rxbdr(priv->rx_ring[i], extended);
886d4fd0404SClaudiu Manoil 
887d4fd0404SClaudiu Manoil 		if (err)
888d4fd0404SClaudiu Manoil 			goto fail;
889d4fd0404SClaudiu Manoil 	}
890d4fd0404SClaudiu Manoil 
891d4fd0404SClaudiu Manoil 	return 0;
892d4fd0404SClaudiu Manoil 
893d4fd0404SClaudiu Manoil fail:
894d4fd0404SClaudiu Manoil 	while (i-- > 0)
895d4fd0404SClaudiu Manoil 		enetc_free_rxbdr(priv->rx_ring[i]);
896d4fd0404SClaudiu Manoil 
897d4fd0404SClaudiu Manoil 	return err;
898d4fd0404SClaudiu Manoil }
899d4fd0404SClaudiu Manoil 
900d4fd0404SClaudiu Manoil static void enetc_free_rx_resources(struct enetc_ndev_priv *priv)
901d4fd0404SClaudiu Manoil {
902d4fd0404SClaudiu Manoil 	int i;
903d4fd0404SClaudiu Manoil 
904d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
905d4fd0404SClaudiu Manoil 		enetc_free_rxbdr(priv->rx_ring[i]);
906d4fd0404SClaudiu Manoil }
907d4fd0404SClaudiu Manoil 
908d4fd0404SClaudiu Manoil static void enetc_free_tx_ring(struct enetc_bdr *tx_ring)
909d4fd0404SClaudiu Manoil {
910d4fd0404SClaudiu Manoil 	int i;
911d4fd0404SClaudiu Manoil 
912d4fd0404SClaudiu Manoil 	if (!tx_ring->tx_swbd)
913d4fd0404SClaudiu Manoil 		return;
914d4fd0404SClaudiu Manoil 
915d4fd0404SClaudiu Manoil 	for (i = 0; i < tx_ring->bd_count; i++) {
916d4fd0404SClaudiu Manoil 		struct enetc_tx_swbd *tx_swbd = &tx_ring->tx_swbd[i];
917d4fd0404SClaudiu Manoil 
918d4fd0404SClaudiu Manoil 		enetc_free_tx_skb(tx_ring, tx_swbd);
919d4fd0404SClaudiu Manoil 	}
920d4fd0404SClaudiu Manoil 
921d4fd0404SClaudiu Manoil 	tx_ring->next_to_clean = 0;
922d4fd0404SClaudiu Manoil 	tx_ring->next_to_use = 0;
923d4fd0404SClaudiu Manoil }
924d4fd0404SClaudiu Manoil 
925d4fd0404SClaudiu Manoil static void enetc_free_rx_ring(struct enetc_bdr *rx_ring)
926d4fd0404SClaudiu Manoil {
927d4fd0404SClaudiu Manoil 	int i;
928d4fd0404SClaudiu Manoil 
929d4fd0404SClaudiu Manoil 	if (!rx_ring->rx_swbd)
930d4fd0404SClaudiu Manoil 		return;
931d4fd0404SClaudiu Manoil 
932d4fd0404SClaudiu Manoil 	for (i = 0; i < rx_ring->bd_count; i++) {
933d4fd0404SClaudiu Manoil 		struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[i];
934d4fd0404SClaudiu Manoil 
935d4fd0404SClaudiu Manoil 		if (!rx_swbd->page)
936d4fd0404SClaudiu Manoil 			continue;
937d4fd0404SClaudiu Manoil 
938d4fd0404SClaudiu Manoil 		dma_unmap_page(rx_ring->dev, rx_swbd->dma,
939d4fd0404SClaudiu Manoil 			       PAGE_SIZE, DMA_FROM_DEVICE);
940d4fd0404SClaudiu Manoil 		__free_page(rx_swbd->page);
941d4fd0404SClaudiu Manoil 		rx_swbd->page = NULL;
942d4fd0404SClaudiu Manoil 	}
943d4fd0404SClaudiu Manoil 
944d4fd0404SClaudiu Manoil 	rx_ring->next_to_clean = 0;
945d4fd0404SClaudiu Manoil 	rx_ring->next_to_use = 0;
946d4fd0404SClaudiu Manoil 	rx_ring->next_to_alloc = 0;
947d4fd0404SClaudiu Manoil }
948d4fd0404SClaudiu Manoil 
949d4fd0404SClaudiu Manoil static void enetc_free_rxtx_rings(struct enetc_ndev_priv *priv)
950d4fd0404SClaudiu Manoil {
951d4fd0404SClaudiu Manoil 	int i;
952d4fd0404SClaudiu Manoil 
953d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
954d4fd0404SClaudiu Manoil 		enetc_free_rx_ring(priv->rx_ring[i]);
955d4fd0404SClaudiu Manoil 
956d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
957d4fd0404SClaudiu Manoil 		enetc_free_tx_ring(priv->tx_ring[i]);
958d4fd0404SClaudiu Manoil }
959d4fd0404SClaudiu Manoil 
960d4fd0404SClaudiu Manoil static int enetc_alloc_cbdr(struct device *dev, struct enetc_cbdr *cbdr)
961d4fd0404SClaudiu Manoil {
962d4fd0404SClaudiu Manoil 	int size = cbdr->bd_count * sizeof(struct enetc_cbd);
963d4fd0404SClaudiu Manoil 
964d4fd0404SClaudiu Manoil 	cbdr->bd_base = dma_alloc_coherent(dev, size, &cbdr->bd_dma_base,
965d4fd0404SClaudiu Manoil 					   GFP_KERNEL);
966d4fd0404SClaudiu Manoil 	if (!cbdr->bd_base)
967d4fd0404SClaudiu Manoil 		return -ENOMEM;
968d4fd0404SClaudiu Manoil 
969d4fd0404SClaudiu Manoil 	/* h/w requires 128B alignment */
970d4fd0404SClaudiu Manoil 	if (!IS_ALIGNED(cbdr->bd_dma_base, 128)) {
971d4fd0404SClaudiu Manoil 		dma_free_coherent(dev, size, cbdr->bd_base, cbdr->bd_dma_base);
972d4fd0404SClaudiu Manoil 		return -EINVAL;
973d4fd0404SClaudiu Manoil 	}
974d4fd0404SClaudiu Manoil 
975d4fd0404SClaudiu Manoil 	cbdr->next_to_clean = 0;
976d4fd0404SClaudiu Manoil 	cbdr->next_to_use = 0;
977d4fd0404SClaudiu Manoil 
978d4fd0404SClaudiu Manoil 	return 0;
979d4fd0404SClaudiu Manoil }
980d4fd0404SClaudiu Manoil 
981d4fd0404SClaudiu Manoil static void enetc_free_cbdr(struct device *dev, struct enetc_cbdr *cbdr)
982d4fd0404SClaudiu Manoil {
983d4fd0404SClaudiu Manoil 	int size = cbdr->bd_count * sizeof(struct enetc_cbd);
984d4fd0404SClaudiu Manoil 
985d4fd0404SClaudiu Manoil 	dma_free_coherent(dev, size, cbdr->bd_base, cbdr->bd_dma_base);
986d4fd0404SClaudiu Manoil 	cbdr->bd_base = NULL;
987d4fd0404SClaudiu Manoil }
988d4fd0404SClaudiu Manoil 
989d4fd0404SClaudiu Manoil static void enetc_setup_cbdr(struct enetc_hw *hw, struct enetc_cbdr *cbdr)
990d4fd0404SClaudiu Manoil {
991d4fd0404SClaudiu Manoil 	/* set CBDR cache attributes */
992d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICAR2,
993d4fd0404SClaudiu Manoil 		 ENETC_SICAR_RD_COHERENT | ENETC_SICAR_WR_COHERENT);
994d4fd0404SClaudiu Manoil 
995d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICBDRBAR0, lower_32_bits(cbdr->bd_dma_base));
996d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICBDRBAR1, upper_32_bits(cbdr->bd_dma_base));
997d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICBDRLENR, ENETC_RTBLENR_LEN(cbdr->bd_count));
998d4fd0404SClaudiu Manoil 
999d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICBDRPIR, 0);
1000d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICBDRCIR, 0);
1001d4fd0404SClaudiu Manoil 
1002d4fd0404SClaudiu Manoil 	/* enable ring */
1003d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICBDRMR, BIT(31));
1004d4fd0404SClaudiu Manoil 
1005d4fd0404SClaudiu Manoil 	cbdr->pir = hw->reg + ENETC_SICBDRPIR;
1006d4fd0404SClaudiu Manoil 	cbdr->cir = hw->reg + ENETC_SICBDRCIR;
1007d4fd0404SClaudiu Manoil }
1008d4fd0404SClaudiu Manoil 
1009d4fd0404SClaudiu Manoil static void enetc_clear_cbdr(struct enetc_hw *hw)
1010d4fd0404SClaudiu Manoil {
1011d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICBDRMR, 0);
1012d4fd0404SClaudiu Manoil }
1013d4fd0404SClaudiu Manoil 
1014d382563fSClaudiu Manoil static int enetc_setup_default_rss_table(struct enetc_si *si, int num_groups)
1015d382563fSClaudiu Manoil {
1016d382563fSClaudiu Manoil 	int *rss_table;
1017d382563fSClaudiu Manoil 	int i;
1018d382563fSClaudiu Manoil 
1019d382563fSClaudiu Manoil 	rss_table = kmalloc_array(si->num_rss, sizeof(*rss_table), GFP_KERNEL);
1020d382563fSClaudiu Manoil 	if (!rss_table)
1021d382563fSClaudiu Manoil 		return -ENOMEM;
1022d382563fSClaudiu Manoil 
1023d382563fSClaudiu Manoil 	/* Set up RSS table defaults */
1024d382563fSClaudiu Manoil 	for (i = 0; i < si->num_rss; i++)
1025d382563fSClaudiu Manoil 		rss_table[i] = i % num_groups;
1026d382563fSClaudiu Manoil 
1027d382563fSClaudiu Manoil 	enetc_set_rss_table(si, rss_table, si->num_rss);
1028d382563fSClaudiu Manoil 
1029d382563fSClaudiu Manoil 	kfree(rss_table);
1030d382563fSClaudiu Manoil 
1031d382563fSClaudiu Manoil 	return 0;
1032d382563fSClaudiu Manoil }
1033d382563fSClaudiu Manoil 
1034d4fd0404SClaudiu Manoil static int enetc_configure_si(struct enetc_ndev_priv *priv)
1035d4fd0404SClaudiu Manoil {
1036d4fd0404SClaudiu Manoil 	struct enetc_si *si = priv->si;
1037d4fd0404SClaudiu Manoil 	struct enetc_hw *hw = &si->hw;
1038d382563fSClaudiu Manoil 	int err;
1039d4fd0404SClaudiu Manoil 
1040d4fd0404SClaudiu Manoil 	enetc_setup_cbdr(hw, &si->cbd_ring);
1041d4fd0404SClaudiu Manoil 	/* set SI cache attributes */
1042d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICAR0,
1043d4fd0404SClaudiu Manoil 		 ENETC_SICAR_RD_COHERENT | ENETC_SICAR_WR_COHERENT);
1044d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SICAR1, ENETC_SICAR_MSI);
1045d4fd0404SClaudiu Manoil 	/* enable SI */
1046d4fd0404SClaudiu Manoil 	enetc_wr(hw, ENETC_SIMR, ENETC_SIMR_EN);
1047d4fd0404SClaudiu Manoil 
1048d382563fSClaudiu Manoil 	if (si->num_rss) {
1049d382563fSClaudiu Manoil 		err = enetc_setup_default_rss_table(si, priv->num_rx_rings);
1050d382563fSClaudiu Manoil 		if (err)
1051d382563fSClaudiu Manoil 			return err;
1052d382563fSClaudiu Manoil 	}
1053d382563fSClaudiu Manoil 
1054d4fd0404SClaudiu Manoil 	return 0;
1055d4fd0404SClaudiu Manoil }
1056d4fd0404SClaudiu Manoil 
1057d4fd0404SClaudiu Manoil void enetc_init_si_rings_params(struct enetc_ndev_priv *priv)
1058d4fd0404SClaudiu Manoil {
1059d4fd0404SClaudiu Manoil 	struct enetc_si *si = priv->si;
1060d4fd0404SClaudiu Manoil 	int cpus = num_online_cpus();
1061d4fd0404SClaudiu Manoil 
106202293dd4SClaudiu Manoil 	priv->tx_bd_count = ENETC_TX_RING_DEFAULT_SIZE;
106302293dd4SClaudiu Manoil 	priv->rx_bd_count = ENETC_RX_RING_DEFAULT_SIZE;
1064d4fd0404SClaudiu Manoil 
1065d4fd0404SClaudiu Manoil 	/* Enable all available TX rings in order to configure as many
1066d4fd0404SClaudiu Manoil 	 * priorities as possible, when needed.
1067d4fd0404SClaudiu Manoil 	 * TODO: Make # of TX rings run-time configurable
1068d4fd0404SClaudiu Manoil 	 */
1069d4fd0404SClaudiu Manoil 	priv->num_rx_rings = min_t(int, cpus, si->num_rx_rings);
1070d4fd0404SClaudiu Manoil 	priv->num_tx_rings = si->num_tx_rings;
1071d4fd0404SClaudiu Manoil 	priv->bdr_int_num = cpus;
1072ae0e6a5dSClaudiu Manoil 	priv->ic_mode = ENETC_IC_RX_ADAPTIVE | ENETC_IC_TX_MANUAL;
1073ae0e6a5dSClaudiu Manoil 	priv->tx_ictt = ENETC_TXIC_TIMETHR;
1074d4fd0404SClaudiu Manoil 
1075d4fd0404SClaudiu Manoil 	/* SI specific */
1076d4fd0404SClaudiu Manoil 	si->cbd_ring.bd_count = ENETC_CBDR_DEFAULT_SIZE;
1077d4fd0404SClaudiu Manoil }
1078d4fd0404SClaudiu Manoil 
1079d4fd0404SClaudiu Manoil int enetc_alloc_si_resources(struct enetc_ndev_priv *priv)
1080d4fd0404SClaudiu Manoil {
1081d4fd0404SClaudiu Manoil 	struct enetc_si *si = priv->si;
1082d4fd0404SClaudiu Manoil 	int err;
1083d4fd0404SClaudiu Manoil 
1084d4fd0404SClaudiu Manoil 	err = enetc_alloc_cbdr(priv->dev, &si->cbd_ring);
1085d4fd0404SClaudiu Manoil 	if (err)
1086d4fd0404SClaudiu Manoil 		return err;
1087d4fd0404SClaudiu Manoil 
1088d382563fSClaudiu Manoil 	priv->cls_rules = kcalloc(si->num_fs_entries, sizeof(*priv->cls_rules),
1089d382563fSClaudiu Manoil 				  GFP_KERNEL);
1090d382563fSClaudiu Manoil 	if (!priv->cls_rules) {
1091d382563fSClaudiu Manoil 		err = -ENOMEM;
1092d382563fSClaudiu Manoil 		goto err_alloc_cls;
1093d382563fSClaudiu Manoil 	}
1094d382563fSClaudiu Manoil 
1095d4fd0404SClaudiu Manoil 	err = enetc_configure_si(priv);
1096d4fd0404SClaudiu Manoil 	if (err)
1097d4fd0404SClaudiu Manoil 		goto err_config_si;
1098d4fd0404SClaudiu Manoil 
1099d4fd0404SClaudiu Manoil 	return 0;
1100d4fd0404SClaudiu Manoil 
1101d4fd0404SClaudiu Manoil err_config_si:
1102d382563fSClaudiu Manoil 	kfree(priv->cls_rules);
1103d382563fSClaudiu Manoil err_alloc_cls:
1104d4fd0404SClaudiu Manoil 	enetc_clear_cbdr(&si->hw);
1105d4fd0404SClaudiu Manoil 	enetc_free_cbdr(priv->dev, &si->cbd_ring);
1106d4fd0404SClaudiu Manoil 
1107d4fd0404SClaudiu Manoil 	return err;
1108d4fd0404SClaudiu Manoil }
1109d4fd0404SClaudiu Manoil 
1110d4fd0404SClaudiu Manoil void enetc_free_si_resources(struct enetc_ndev_priv *priv)
1111d4fd0404SClaudiu Manoil {
1112d4fd0404SClaudiu Manoil 	struct enetc_si *si = priv->si;
1113d4fd0404SClaudiu Manoil 
1114d4fd0404SClaudiu Manoil 	enetc_clear_cbdr(&si->hw);
1115d4fd0404SClaudiu Manoil 	enetc_free_cbdr(priv->dev, &si->cbd_ring);
1116d382563fSClaudiu Manoil 
1117d382563fSClaudiu Manoil 	kfree(priv->cls_rules);
1118d4fd0404SClaudiu Manoil }
1119d4fd0404SClaudiu Manoil 
1120d4fd0404SClaudiu Manoil static void enetc_setup_txbdr(struct enetc_hw *hw, struct enetc_bdr *tx_ring)
1121d4fd0404SClaudiu Manoil {
1122d4fd0404SClaudiu Manoil 	int idx = tx_ring->index;
1123d4fd0404SClaudiu Manoil 	u32 tbmr;
1124d4fd0404SClaudiu Manoil 
1125d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBBAR0,
1126d4fd0404SClaudiu Manoil 		       lower_32_bits(tx_ring->bd_dma_base));
1127d4fd0404SClaudiu Manoil 
1128d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBBAR1,
1129d4fd0404SClaudiu Manoil 		       upper_32_bits(tx_ring->bd_dma_base));
1130d4fd0404SClaudiu Manoil 
1131d4fd0404SClaudiu Manoil 	WARN_ON(!IS_ALIGNED(tx_ring->bd_count, 64)); /* multiple of 64 */
1132d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBLENR,
1133d4fd0404SClaudiu Manoil 		       ENETC_RTBLENR_LEN(tx_ring->bd_count));
1134d4fd0404SClaudiu Manoil 
1135d4fd0404SClaudiu Manoil 	/* clearing PI/CI registers for Tx not supported, adjust sw indexes */
1136d4fd0404SClaudiu Manoil 	tx_ring->next_to_use = enetc_txbdr_rd(hw, idx, ENETC_TBPIR);
1137d4fd0404SClaudiu Manoil 	tx_ring->next_to_clean = enetc_txbdr_rd(hw, idx, ENETC_TBCIR);
1138d4fd0404SClaudiu Manoil 
1139d4fd0404SClaudiu Manoil 	/* enable Tx ints by setting pkt thr to 1 */
114012460a0aSClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBICR0, ENETC_TBICR0_ICEN | 0x1);
1141d4fd0404SClaudiu Manoil 
1142d4fd0404SClaudiu Manoil 	tbmr = ENETC_TBMR_EN;
1143d4fd0404SClaudiu Manoil 	if (tx_ring->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
1144d4fd0404SClaudiu Manoil 		tbmr |= ENETC_TBMR_VIH;
1145d4fd0404SClaudiu Manoil 
1146d4fd0404SClaudiu Manoil 	/* enable ring */
1147d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBMR, tbmr);
1148d4fd0404SClaudiu Manoil 
1149d4fd0404SClaudiu Manoil 	tx_ring->tpir = hw->reg + ENETC_BDR(TX, idx, ENETC_TBPIR);
1150d4fd0404SClaudiu Manoil 	tx_ring->tcir = hw->reg + ENETC_BDR(TX, idx, ENETC_TBCIR);
1151d4fd0404SClaudiu Manoil 	tx_ring->idr = hw->reg + ENETC_SITXIDR;
1152d4fd0404SClaudiu Manoil }
1153d4fd0404SClaudiu Manoil 
1154d4fd0404SClaudiu Manoil static void enetc_setup_rxbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring)
1155d4fd0404SClaudiu Manoil {
1156d4fd0404SClaudiu Manoil 	int idx = rx_ring->index;
1157d4fd0404SClaudiu Manoil 	u32 rbmr;
1158d4fd0404SClaudiu Manoil 
1159d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBBAR0,
1160d4fd0404SClaudiu Manoil 		       lower_32_bits(rx_ring->bd_dma_base));
1161d4fd0404SClaudiu Manoil 
1162d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBBAR1,
1163d4fd0404SClaudiu Manoil 		       upper_32_bits(rx_ring->bd_dma_base));
1164d4fd0404SClaudiu Manoil 
1165d4fd0404SClaudiu Manoil 	WARN_ON(!IS_ALIGNED(rx_ring->bd_count, 64)); /* multiple of 64 */
1166d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBLENR,
1167d4fd0404SClaudiu Manoil 		       ENETC_RTBLENR_LEN(rx_ring->bd_count));
1168d4fd0404SClaudiu Manoil 
1169d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBBSR, ENETC_RXB_DMA_SIZE);
1170d4fd0404SClaudiu Manoil 
1171d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBPIR, 0);
1172d4fd0404SClaudiu Manoil 
1173d4fd0404SClaudiu Manoil 	/* enable Rx ints by setting pkt thr to 1 */
117412460a0aSClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBICR0, ENETC_RBICR0_ICEN | 0x1);
1175d4fd0404SClaudiu Manoil 
1176d4fd0404SClaudiu Manoil 	rbmr = ENETC_RBMR_EN;
1177434cebabSClaudiu Manoil 
1178434cebabSClaudiu Manoil 	if (rx_ring->ext_en)
1179d3982312SY.b. Lu 		rbmr |= ENETC_RBMR_BDS;
1180434cebabSClaudiu Manoil 
1181d4fd0404SClaudiu Manoil 	if (rx_ring->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
1182d4fd0404SClaudiu Manoil 		rbmr |= ENETC_RBMR_VTE;
1183d4fd0404SClaudiu Manoil 
1184d4fd0404SClaudiu Manoil 	rx_ring->rcir = hw->reg + ENETC_BDR(RX, idx, ENETC_RBCIR);
1185d4fd0404SClaudiu Manoil 	rx_ring->idr = hw->reg + ENETC_SIRXIDR;
1186d4fd0404SClaudiu Manoil 
1187d4fd0404SClaudiu Manoil 	enetc_refill_rx_ring(rx_ring, enetc_bd_unused(rx_ring));
1188d4fd0404SClaudiu Manoil 
1189d4fd0404SClaudiu Manoil 	/* enable ring */
1190d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBMR, rbmr);
1191d4fd0404SClaudiu Manoil }
1192d4fd0404SClaudiu Manoil 
1193d4fd0404SClaudiu Manoil static void enetc_setup_bdrs(struct enetc_ndev_priv *priv)
1194d4fd0404SClaudiu Manoil {
1195d4fd0404SClaudiu Manoil 	int i;
1196d4fd0404SClaudiu Manoil 
1197d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
1198d4fd0404SClaudiu Manoil 		enetc_setup_txbdr(&priv->si->hw, priv->tx_ring[i]);
1199d4fd0404SClaudiu Manoil 
1200d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
1201d4fd0404SClaudiu Manoil 		enetc_setup_rxbdr(&priv->si->hw, priv->rx_ring[i]);
1202d4fd0404SClaudiu Manoil }
1203d4fd0404SClaudiu Manoil 
1204d4fd0404SClaudiu Manoil static void enetc_clear_rxbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring)
1205d4fd0404SClaudiu Manoil {
1206d4fd0404SClaudiu Manoil 	int idx = rx_ring->index;
1207d4fd0404SClaudiu Manoil 
1208d4fd0404SClaudiu Manoil 	/* disable EN bit on ring */
1209d4fd0404SClaudiu Manoil 	enetc_rxbdr_wr(hw, idx, ENETC_RBMR, 0);
1210d4fd0404SClaudiu Manoil }
1211d4fd0404SClaudiu Manoil 
1212d4fd0404SClaudiu Manoil static void enetc_clear_txbdr(struct enetc_hw *hw, struct enetc_bdr *tx_ring)
1213d4fd0404SClaudiu Manoil {
1214d4fd0404SClaudiu Manoil 	int delay = 8, timeout = 100;
1215d4fd0404SClaudiu Manoil 	int idx = tx_ring->index;
1216d4fd0404SClaudiu Manoil 
1217d4fd0404SClaudiu Manoil 	/* disable EN bit on ring */
1218d4fd0404SClaudiu Manoil 	enetc_txbdr_wr(hw, idx, ENETC_TBMR, 0);
1219d4fd0404SClaudiu Manoil 
1220d4fd0404SClaudiu Manoil 	/* wait for busy to clear */
1221d4fd0404SClaudiu Manoil 	while (delay < timeout &&
1222d4fd0404SClaudiu Manoil 	       enetc_txbdr_rd(hw, idx, ENETC_TBSR) & ENETC_TBSR_BUSY) {
1223d4fd0404SClaudiu Manoil 		msleep(delay);
1224d4fd0404SClaudiu Manoil 		delay *= 2;
1225d4fd0404SClaudiu Manoil 	}
1226d4fd0404SClaudiu Manoil 
1227d4fd0404SClaudiu Manoil 	if (delay >= timeout)
1228d4fd0404SClaudiu Manoil 		netdev_warn(tx_ring->ndev, "timeout for tx ring #%d clear\n",
1229d4fd0404SClaudiu Manoil 			    idx);
1230d4fd0404SClaudiu Manoil }
1231d4fd0404SClaudiu Manoil 
1232d4fd0404SClaudiu Manoil static void enetc_clear_bdrs(struct enetc_ndev_priv *priv)
1233d4fd0404SClaudiu Manoil {
1234d4fd0404SClaudiu Manoil 	int i;
1235d4fd0404SClaudiu Manoil 
1236d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
1237d4fd0404SClaudiu Manoil 		enetc_clear_txbdr(&priv->si->hw, priv->tx_ring[i]);
1238d4fd0404SClaudiu Manoil 
1239d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
1240d4fd0404SClaudiu Manoil 		enetc_clear_rxbdr(&priv->si->hw, priv->rx_ring[i]);
1241d4fd0404SClaudiu Manoil 
1242d4fd0404SClaudiu Manoil 	udelay(1);
1243d4fd0404SClaudiu Manoil }
1244d4fd0404SClaudiu Manoil 
1245d4fd0404SClaudiu Manoil static int enetc_setup_irqs(struct enetc_ndev_priv *priv)
1246d4fd0404SClaudiu Manoil {
1247d4fd0404SClaudiu Manoil 	struct pci_dev *pdev = priv->si->pdev;
1248d4fd0404SClaudiu Manoil 	cpumask_t cpu_mask;
1249d4fd0404SClaudiu Manoil 	int i, j, err;
1250d4fd0404SClaudiu Manoil 
1251d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
1252d4fd0404SClaudiu Manoil 		int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i);
1253d4fd0404SClaudiu Manoil 		struct enetc_int_vector *v = priv->int_vector[i];
1254d4fd0404SClaudiu Manoil 		int entry = ENETC_BDR_INT_BASE_IDX + i;
1255d4fd0404SClaudiu Manoil 		struct enetc_hw *hw = &priv->si->hw;
1256d4fd0404SClaudiu Manoil 
1257d4fd0404SClaudiu Manoil 		snprintf(v->name, sizeof(v->name), "%s-rxtx%d",
1258d4fd0404SClaudiu Manoil 			 priv->ndev->name, i);
1259d4fd0404SClaudiu Manoil 		err = request_irq(irq, enetc_msix, 0, v->name, v);
1260d4fd0404SClaudiu Manoil 		if (err) {
1261d4fd0404SClaudiu Manoil 			dev_err(priv->dev, "request_irq() failed!\n");
1262d4fd0404SClaudiu Manoil 			goto irq_err;
1263d4fd0404SClaudiu Manoil 		}
1264bbb96dc7SClaudiu Manoil 		disable_irq(irq);
1265d4fd0404SClaudiu Manoil 
1266d4fd0404SClaudiu Manoil 		v->tbier_base = hw->reg + ENETC_BDR(TX, 0, ENETC_TBIER);
1267d4fd0404SClaudiu Manoil 		v->rbier = hw->reg + ENETC_BDR(RX, i, ENETC_RBIER);
126891571081SClaudiu Manoil 		v->ricr1 = hw->reg + ENETC_BDR(RX, i, ENETC_RBICR1);
1269d4fd0404SClaudiu Manoil 
1270d4fd0404SClaudiu Manoil 		enetc_wr(hw, ENETC_SIMSIRRV(i), entry);
1271d4fd0404SClaudiu Manoil 
1272d4fd0404SClaudiu Manoil 		for (j = 0; j < v->count_tx_rings; j++) {
1273d4fd0404SClaudiu Manoil 			int idx = v->tx_ring[j].index;
1274d4fd0404SClaudiu Manoil 
1275d4fd0404SClaudiu Manoil 			enetc_wr(hw, ENETC_SIMSITRV(idx), entry);
1276d4fd0404SClaudiu Manoil 		}
1277d4fd0404SClaudiu Manoil 		cpumask_clear(&cpu_mask);
1278d4fd0404SClaudiu Manoil 		cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
1279d4fd0404SClaudiu Manoil 		irq_set_affinity_hint(irq, &cpu_mask);
1280d4fd0404SClaudiu Manoil 	}
1281d4fd0404SClaudiu Manoil 
1282d4fd0404SClaudiu Manoil 	return 0;
1283d4fd0404SClaudiu Manoil 
1284d4fd0404SClaudiu Manoil irq_err:
1285d4fd0404SClaudiu Manoil 	while (i--) {
1286d4fd0404SClaudiu Manoil 		int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i);
1287d4fd0404SClaudiu Manoil 
1288d4fd0404SClaudiu Manoil 		irq_set_affinity_hint(irq, NULL);
1289d4fd0404SClaudiu Manoil 		free_irq(irq, priv->int_vector[i]);
1290d4fd0404SClaudiu Manoil 	}
1291d4fd0404SClaudiu Manoil 
1292d4fd0404SClaudiu Manoil 	return err;
1293d4fd0404SClaudiu Manoil }
1294d4fd0404SClaudiu Manoil 
1295d4fd0404SClaudiu Manoil static void enetc_free_irqs(struct enetc_ndev_priv *priv)
1296d4fd0404SClaudiu Manoil {
1297d4fd0404SClaudiu Manoil 	struct pci_dev *pdev = priv->si->pdev;
1298d4fd0404SClaudiu Manoil 	int i;
1299d4fd0404SClaudiu Manoil 
1300d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
1301d4fd0404SClaudiu Manoil 		int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i);
1302d4fd0404SClaudiu Manoil 
1303d4fd0404SClaudiu Manoil 		irq_set_affinity_hint(irq, NULL);
1304d4fd0404SClaudiu Manoil 		free_irq(irq, priv->int_vector[i]);
1305d4fd0404SClaudiu Manoil 	}
1306d4fd0404SClaudiu Manoil }
1307d4fd0404SClaudiu Manoil 
1308bbb96dc7SClaudiu Manoil static void enetc_setup_interrupts(struct enetc_ndev_priv *priv)
1309d4fd0404SClaudiu Manoil {
131091571081SClaudiu Manoil 	struct enetc_hw *hw = &priv->si->hw;
131191571081SClaudiu Manoil 	u32 icpt, ictt;
1312d4fd0404SClaudiu Manoil 	int i;
1313d4fd0404SClaudiu Manoil 
1314d4fd0404SClaudiu Manoil 	/* enable Tx & Rx event indication */
1315ae0e6a5dSClaudiu Manoil 	if (priv->ic_mode &
1316ae0e6a5dSClaudiu Manoil 	    (ENETC_IC_RX_MANUAL | ENETC_IC_RX_ADAPTIVE)) {
131791571081SClaudiu Manoil 		icpt = ENETC_RBICR0_SET_ICPT(ENETC_RXIC_PKTTHR);
131891571081SClaudiu Manoil 		/* init to non-0 minimum, will be adjusted later */
131991571081SClaudiu Manoil 		ictt = 0x1;
132091571081SClaudiu Manoil 	} else {
132191571081SClaudiu Manoil 		icpt = 0x1; /* enable Rx ints by setting pkt thr to 1 */
132291571081SClaudiu Manoil 		ictt = 0;
1323d4fd0404SClaudiu Manoil 	}
1324d4fd0404SClaudiu Manoil 
132591571081SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++) {
132691571081SClaudiu Manoil 		enetc_rxbdr_wr(hw, i, ENETC_RBICR1, ictt);
132791571081SClaudiu Manoil 		enetc_rxbdr_wr(hw, i, ENETC_RBICR0, ENETC_RBICR0_ICEN | icpt);
132891571081SClaudiu Manoil 		enetc_rxbdr_wr(hw, i, ENETC_RBIER, ENETC_RBIER_RXTIE);
132991571081SClaudiu Manoil 	}
133091571081SClaudiu Manoil 
133191571081SClaudiu Manoil 	if (priv->ic_mode & ENETC_IC_TX_MANUAL)
133291571081SClaudiu Manoil 		icpt = ENETC_TBICR0_SET_ICPT(ENETC_TXIC_PKTTHR);
133391571081SClaudiu Manoil 	else
133491571081SClaudiu Manoil 		icpt = 0x1; /* enable Tx ints by setting pkt thr to 1 */
133591571081SClaudiu Manoil 
1336d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++) {
133791571081SClaudiu Manoil 		enetc_txbdr_wr(hw, i, ENETC_TBICR1, priv->tx_ictt);
133891571081SClaudiu Manoil 		enetc_txbdr_wr(hw, i, ENETC_TBICR0, ENETC_TBICR0_ICEN | icpt);
133991571081SClaudiu Manoil 		enetc_txbdr_wr(hw, i, ENETC_TBIER, ENETC_TBIER_TXTIE);
1340d4fd0404SClaudiu Manoil 	}
1341d4fd0404SClaudiu Manoil }
1342d4fd0404SClaudiu Manoil 
1343bbb96dc7SClaudiu Manoil static void enetc_clear_interrupts(struct enetc_ndev_priv *priv)
1344d4fd0404SClaudiu Manoil {
1345d4fd0404SClaudiu Manoil 	int i;
1346d4fd0404SClaudiu Manoil 
1347d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
1348d4fd0404SClaudiu Manoil 		enetc_txbdr_wr(&priv->si->hw, i, ENETC_TBIER, 0);
1349d4fd0404SClaudiu Manoil 
1350d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
1351d4fd0404SClaudiu Manoil 		enetc_rxbdr_wr(&priv->si->hw, i, ENETC_RBIER, 0);
1352d4fd0404SClaudiu Manoil }
1353d4fd0404SClaudiu Manoil 
135471b77a7aSClaudiu Manoil static int enetc_phylink_connect(struct net_device *ndev)
1355d4fd0404SClaudiu Manoil {
13562e47cb41SPo Liu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1357a6a10d45SYangbo Lu 	struct ethtool_eee edata;
135871b77a7aSClaudiu Manoil 	int err;
1359d4fd0404SClaudiu Manoil 
136071b77a7aSClaudiu Manoil 	if (!priv->phylink)
1361d4fd0404SClaudiu Manoil 		return 0; /* phy-less mode */
1362d4fd0404SClaudiu Manoil 
136371b77a7aSClaudiu Manoil 	err = phylink_of_phy_connect(priv->phylink, priv->dev->of_node, 0);
136471b77a7aSClaudiu Manoil 	if (err) {
1365d4fd0404SClaudiu Manoil 		dev_err(&ndev->dev, "could not attach to PHY\n");
136671b77a7aSClaudiu Manoil 		return err;
1367d4fd0404SClaudiu Manoil 	}
1368d4fd0404SClaudiu Manoil 
1369a6a10d45SYangbo Lu 	/* disable EEE autoneg, until ENETC driver supports it */
1370a6a10d45SYangbo Lu 	memset(&edata, 0, sizeof(struct ethtool_eee));
137171b77a7aSClaudiu Manoil 	phylink_ethtool_set_eee(priv->phylink, &edata);
1372a6a10d45SYangbo Lu 
1373d4fd0404SClaudiu Manoil 	return 0;
1374d4fd0404SClaudiu Manoil }
1375d4fd0404SClaudiu Manoil 
137691571081SClaudiu Manoil void enetc_start(struct net_device *ndev)
1377bbb96dc7SClaudiu Manoil {
1378bbb96dc7SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1379bbb96dc7SClaudiu Manoil 	int i;
1380bbb96dc7SClaudiu Manoil 
1381bbb96dc7SClaudiu Manoil 	enetc_setup_interrupts(priv);
1382bbb96dc7SClaudiu Manoil 
1383bbb96dc7SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
1384bbb96dc7SClaudiu Manoil 		int irq = pci_irq_vector(priv->si->pdev,
1385bbb96dc7SClaudiu Manoil 					 ENETC_BDR_INT_BASE_IDX + i);
1386bbb96dc7SClaudiu Manoil 
1387bbb96dc7SClaudiu Manoil 		napi_enable(&priv->int_vector[i]->napi);
1388bbb96dc7SClaudiu Manoil 		enable_irq(irq);
1389bbb96dc7SClaudiu Manoil 	}
1390bbb96dc7SClaudiu Manoil 
139171b77a7aSClaudiu Manoil 	if (priv->phylink)
139271b77a7aSClaudiu Manoil 		phylink_start(priv->phylink);
1393bbb96dc7SClaudiu Manoil 	else
1394bbb96dc7SClaudiu Manoil 		netif_carrier_on(ndev);
1395bbb96dc7SClaudiu Manoil 
1396bbb96dc7SClaudiu Manoil 	netif_tx_start_all_queues(ndev);
1397bbb96dc7SClaudiu Manoil }
1398bbb96dc7SClaudiu Manoil 
1399d4fd0404SClaudiu Manoil int enetc_open(struct net_device *ndev)
1400d4fd0404SClaudiu Manoil {
1401d4fd0404SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1402bbb96dc7SClaudiu Manoil 	int err;
1403d4fd0404SClaudiu Manoil 
1404d4fd0404SClaudiu Manoil 	err = enetc_setup_irqs(priv);
1405d4fd0404SClaudiu Manoil 	if (err)
1406d4fd0404SClaudiu Manoil 		return err;
1407d4fd0404SClaudiu Manoil 
140871b77a7aSClaudiu Manoil 	err = enetc_phylink_connect(ndev);
1409d4fd0404SClaudiu Manoil 	if (err)
1410d4fd0404SClaudiu Manoil 		goto err_phy_connect;
1411d4fd0404SClaudiu Manoil 
1412d4fd0404SClaudiu Manoil 	err = enetc_alloc_tx_resources(priv);
1413d4fd0404SClaudiu Manoil 	if (err)
1414d4fd0404SClaudiu Manoil 		goto err_alloc_tx;
1415d4fd0404SClaudiu Manoil 
1416d4fd0404SClaudiu Manoil 	err = enetc_alloc_rx_resources(priv);
1417d4fd0404SClaudiu Manoil 	if (err)
1418d4fd0404SClaudiu Manoil 		goto err_alloc_rx;
1419d4fd0404SClaudiu Manoil 
1420d4fd0404SClaudiu Manoil 	err = netif_set_real_num_tx_queues(ndev, priv->num_tx_rings);
1421d4fd0404SClaudiu Manoil 	if (err)
1422d4fd0404SClaudiu Manoil 		goto err_set_queues;
1423d4fd0404SClaudiu Manoil 
1424d4fd0404SClaudiu Manoil 	err = netif_set_real_num_rx_queues(ndev, priv->num_rx_rings);
1425d4fd0404SClaudiu Manoil 	if (err)
1426d4fd0404SClaudiu Manoil 		goto err_set_queues;
1427d4fd0404SClaudiu Manoil 
1428bbb96dc7SClaudiu Manoil 	enetc_setup_bdrs(priv);
1429bbb96dc7SClaudiu Manoil 	enetc_start(ndev);
1430d4fd0404SClaudiu Manoil 
1431d4fd0404SClaudiu Manoil 	return 0;
1432d4fd0404SClaudiu Manoil 
1433d4fd0404SClaudiu Manoil err_set_queues:
1434d4fd0404SClaudiu Manoil 	enetc_free_rx_resources(priv);
1435d4fd0404SClaudiu Manoil err_alloc_rx:
1436d4fd0404SClaudiu Manoil 	enetc_free_tx_resources(priv);
1437d4fd0404SClaudiu Manoil err_alloc_tx:
143871b77a7aSClaudiu Manoil 	if (priv->phylink)
143971b77a7aSClaudiu Manoil 		phylink_disconnect_phy(priv->phylink);
1440d4fd0404SClaudiu Manoil err_phy_connect:
1441d4fd0404SClaudiu Manoil 	enetc_free_irqs(priv);
1442d4fd0404SClaudiu Manoil 
1443d4fd0404SClaudiu Manoil 	return err;
1444d4fd0404SClaudiu Manoil }
1445d4fd0404SClaudiu Manoil 
144691571081SClaudiu Manoil void enetc_stop(struct net_device *ndev)
1447d4fd0404SClaudiu Manoil {
1448d4fd0404SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1449d4fd0404SClaudiu Manoil 	int i;
1450d4fd0404SClaudiu Manoil 
1451d4fd0404SClaudiu Manoil 	netif_tx_stop_all_queues(ndev);
1452d4fd0404SClaudiu Manoil 
1453d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
1454bbb96dc7SClaudiu Manoil 		int irq = pci_irq_vector(priv->si->pdev,
1455bbb96dc7SClaudiu Manoil 					 ENETC_BDR_INT_BASE_IDX + i);
1456bbb96dc7SClaudiu Manoil 
1457bbb96dc7SClaudiu Manoil 		disable_irq(irq);
1458d4fd0404SClaudiu Manoil 		napi_synchronize(&priv->int_vector[i]->napi);
1459d4fd0404SClaudiu Manoil 		napi_disable(&priv->int_vector[i]->napi);
1460d4fd0404SClaudiu Manoil 	}
1461d4fd0404SClaudiu Manoil 
146271b77a7aSClaudiu Manoil 	if (priv->phylink)
146371b77a7aSClaudiu Manoil 		phylink_stop(priv->phylink);
1464bbb96dc7SClaudiu Manoil 	else
1465bbb96dc7SClaudiu Manoil 		netif_carrier_off(ndev);
1466bbb96dc7SClaudiu Manoil 
1467bbb96dc7SClaudiu Manoil 	enetc_clear_interrupts(priv);
1468bbb96dc7SClaudiu Manoil }
1469bbb96dc7SClaudiu Manoil 
1470bbb96dc7SClaudiu Manoil int enetc_close(struct net_device *ndev)
1471bbb96dc7SClaudiu Manoil {
1472bbb96dc7SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1473bbb96dc7SClaudiu Manoil 
1474bbb96dc7SClaudiu Manoil 	enetc_stop(ndev);
1475d4fd0404SClaudiu Manoil 	enetc_clear_bdrs(priv);
1476d4fd0404SClaudiu Manoil 
147771b77a7aSClaudiu Manoil 	if (priv->phylink)
147871b77a7aSClaudiu Manoil 		phylink_disconnect_phy(priv->phylink);
1479d4fd0404SClaudiu Manoil 	enetc_free_rxtx_rings(priv);
1480d4fd0404SClaudiu Manoil 	enetc_free_rx_resources(priv);
1481d4fd0404SClaudiu Manoil 	enetc_free_tx_resources(priv);
1482d4fd0404SClaudiu Manoil 	enetc_free_irqs(priv);
1483d4fd0404SClaudiu Manoil 
1484d4fd0404SClaudiu Manoil 	return 0;
1485d4fd0404SClaudiu Manoil }
1486d4fd0404SClaudiu Manoil 
148713baf667SMao Wenan static int enetc_setup_tc_mqprio(struct net_device *ndev, void *type_data)
1488cbe9e835SCamelia Groza {
1489cbe9e835SCamelia Groza 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1490cbe9e835SCamelia Groza 	struct tc_mqprio_qopt *mqprio = type_data;
1491cbe9e835SCamelia Groza 	struct enetc_bdr *tx_ring;
1492cbe9e835SCamelia Groza 	u8 num_tc;
1493cbe9e835SCamelia Groza 	int i;
1494cbe9e835SCamelia Groza 
1495cbe9e835SCamelia Groza 	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
1496cbe9e835SCamelia Groza 	num_tc = mqprio->num_tc;
1497cbe9e835SCamelia Groza 
1498cbe9e835SCamelia Groza 	if (!num_tc) {
1499cbe9e835SCamelia Groza 		netdev_reset_tc(ndev);
1500cbe9e835SCamelia Groza 		netif_set_real_num_tx_queues(ndev, priv->num_tx_rings);
1501cbe9e835SCamelia Groza 
1502cbe9e835SCamelia Groza 		/* Reset all ring priorities to 0 */
1503cbe9e835SCamelia Groza 		for (i = 0; i < priv->num_tx_rings; i++) {
1504cbe9e835SCamelia Groza 			tx_ring = priv->tx_ring[i];
1505cbe9e835SCamelia Groza 			enetc_set_bdr_prio(&priv->si->hw, tx_ring->index, 0);
1506cbe9e835SCamelia Groza 		}
1507cbe9e835SCamelia Groza 
1508cbe9e835SCamelia Groza 		return 0;
1509cbe9e835SCamelia Groza 	}
1510cbe9e835SCamelia Groza 
1511cbe9e835SCamelia Groza 	/* Check if we have enough BD rings available to accommodate all TCs */
1512cbe9e835SCamelia Groza 	if (num_tc > priv->num_tx_rings) {
1513cbe9e835SCamelia Groza 		netdev_err(ndev, "Max %d traffic classes supported\n",
1514cbe9e835SCamelia Groza 			   priv->num_tx_rings);
1515cbe9e835SCamelia Groza 		return -EINVAL;
1516cbe9e835SCamelia Groza 	}
1517cbe9e835SCamelia Groza 
1518cbe9e835SCamelia Groza 	/* For the moment, we use only one BD ring per TC.
1519cbe9e835SCamelia Groza 	 *
1520cbe9e835SCamelia Groza 	 * Configure num_tc BD rings with increasing priorities.
1521cbe9e835SCamelia Groza 	 */
1522cbe9e835SCamelia Groza 	for (i = 0; i < num_tc; i++) {
1523cbe9e835SCamelia Groza 		tx_ring = priv->tx_ring[i];
1524cbe9e835SCamelia Groza 		enetc_set_bdr_prio(&priv->si->hw, tx_ring->index, i);
1525cbe9e835SCamelia Groza 	}
1526cbe9e835SCamelia Groza 
1527cbe9e835SCamelia Groza 	/* Reset the number of netdev queues based on the TC count */
1528cbe9e835SCamelia Groza 	netif_set_real_num_tx_queues(ndev, num_tc);
1529cbe9e835SCamelia Groza 
1530cbe9e835SCamelia Groza 	netdev_set_num_tc(ndev, num_tc);
1531cbe9e835SCamelia Groza 
1532cbe9e835SCamelia Groza 	/* Each TC is associated with one netdev queue */
1533cbe9e835SCamelia Groza 	for (i = 0; i < num_tc; i++)
1534cbe9e835SCamelia Groza 		netdev_set_tc_queue(ndev, i, 1, i);
1535cbe9e835SCamelia Groza 
1536cbe9e835SCamelia Groza 	return 0;
1537cbe9e835SCamelia Groza }
1538cbe9e835SCamelia Groza 
153934c6adf1SPo Liu int enetc_setup_tc(struct net_device *ndev, enum tc_setup_type type,
154034c6adf1SPo Liu 		   void *type_data)
154134c6adf1SPo Liu {
154234c6adf1SPo Liu 	switch (type) {
154334c6adf1SPo Liu 	case TC_SETUP_QDISC_MQPRIO:
154434c6adf1SPo Liu 		return enetc_setup_tc_mqprio(ndev, type_data);
154534c6adf1SPo Liu 	case TC_SETUP_QDISC_TAPRIO:
154634c6adf1SPo Liu 		return enetc_setup_tc_taprio(ndev, type_data);
1547c431047cSPo Liu 	case TC_SETUP_QDISC_CBS:
1548c431047cSPo Liu 		return enetc_setup_tc_cbs(ndev, type_data);
15490d08c9ecSPo Liu 	case TC_SETUP_QDISC_ETF:
15500d08c9ecSPo Liu 		return enetc_setup_tc_txtime(ndev, type_data);
1551888ae5a3SPo Liu 	case TC_SETUP_BLOCK:
1552888ae5a3SPo Liu 		return enetc_setup_tc_psfp(ndev, type_data);
155334c6adf1SPo Liu 	default:
155434c6adf1SPo Liu 		return -EOPNOTSUPP;
155534c6adf1SPo Liu 	}
155634c6adf1SPo Liu }
155734c6adf1SPo Liu 
1558d4fd0404SClaudiu Manoil struct net_device_stats *enetc_get_stats(struct net_device *ndev)
1559d4fd0404SClaudiu Manoil {
1560d4fd0404SClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1561d4fd0404SClaudiu Manoil 	struct net_device_stats *stats = &ndev->stats;
1562d4fd0404SClaudiu Manoil 	unsigned long packets = 0, bytes = 0;
1563d4fd0404SClaudiu Manoil 	int i;
1564d4fd0404SClaudiu Manoil 
1565d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++) {
1566d4fd0404SClaudiu Manoil 		packets += priv->rx_ring[i]->stats.packets;
1567d4fd0404SClaudiu Manoil 		bytes	+= priv->rx_ring[i]->stats.bytes;
1568d4fd0404SClaudiu Manoil 	}
1569d4fd0404SClaudiu Manoil 
1570d4fd0404SClaudiu Manoil 	stats->rx_packets = packets;
1571d4fd0404SClaudiu Manoil 	stats->rx_bytes = bytes;
1572d4fd0404SClaudiu Manoil 	bytes = 0;
1573d4fd0404SClaudiu Manoil 	packets = 0;
1574d4fd0404SClaudiu Manoil 
1575d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++) {
1576d4fd0404SClaudiu Manoil 		packets += priv->tx_ring[i]->stats.packets;
1577d4fd0404SClaudiu Manoil 		bytes	+= priv->tx_ring[i]->stats.bytes;
1578d4fd0404SClaudiu Manoil 	}
1579d4fd0404SClaudiu Manoil 
1580d4fd0404SClaudiu Manoil 	stats->tx_packets = packets;
1581d4fd0404SClaudiu Manoil 	stats->tx_bytes = bytes;
1582d4fd0404SClaudiu Manoil 
1583d4fd0404SClaudiu Manoil 	return stats;
1584d4fd0404SClaudiu Manoil }
1585d4fd0404SClaudiu Manoil 
1586d382563fSClaudiu Manoil static int enetc_set_rss(struct net_device *ndev, int en)
1587d382563fSClaudiu Manoil {
1588d382563fSClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1589d382563fSClaudiu Manoil 	struct enetc_hw *hw = &priv->si->hw;
1590d382563fSClaudiu Manoil 	u32 reg;
1591d382563fSClaudiu Manoil 
1592d382563fSClaudiu Manoil 	enetc_wr(hw, ENETC_SIRBGCR, priv->num_rx_rings);
1593d382563fSClaudiu Manoil 
1594d382563fSClaudiu Manoil 	reg = enetc_rd(hw, ENETC_SIMR);
1595d382563fSClaudiu Manoil 	reg &= ~ENETC_SIMR_RSSE;
1596d382563fSClaudiu Manoil 	reg |= (en) ? ENETC_SIMR_RSSE : 0;
1597d382563fSClaudiu Manoil 	enetc_wr(hw, ENETC_SIMR, reg);
1598d382563fSClaudiu Manoil 
1599d382563fSClaudiu Manoil 	return 0;
1600d382563fSClaudiu Manoil }
1601d382563fSClaudiu Manoil 
160279e49982SPo Liu static int enetc_set_psfp(struct net_device *ndev, int en)
160379e49982SPo Liu {
160479e49982SPo Liu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1605888ae5a3SPo Liu 	int err;
160679e49982SPo Liu 
160779e49982SPo Liu 	if (en) {
1608888ae5a3SPo Liu 		err = enetc_psfp_enable(priv);
1609888ae5a3SPo Liu 		if (err)
1610888ae5a3SPo Liu 			return err;
1611888ae5a3SPo Liu 
161279e49982SPo Liu 		priv->active_offloads |= ENETC_F_QCI;
1613888ae5a3SPo Liu 		return 0;
161479e49982SPo Liu 	}
161579e49982SPo Liu 
1616888ae5a3SPo Liu 	err = enetc_psfp_disable(priv);
1617888ae5a3SPo Liu 	if (err)
1618888ae5a3SPo Liu 		return err;
1619888ae5a3SPo Liu 
1620888ae5a3SPo Liu 	priv->active_offloads &= ~ENETC_F_QCI;
1621888ae5a3SPo Liu 
162279e49982SPo Liu 	return 0;
162379e49982SPo Liu }
162479e49982SPo Liu 
16259deba33fSClaudiu Manoil static void enetc_enable_rxvlan(struct net_device *ndev, bool en)
16269deba33fSClaudiu Manoil {
16279deba33fSClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
16289deba33fSClaudiu Manoil 	int i;
16299deba33fSClaudiu Manoil 
16309deba33fSClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
16319deba33fSClaudiu Manoil 		enetc_bdr_enable_rxvlan(&priv->si->hw, i, en);
16329deba33fSClaudiu Manoil }
16339deba33fSClaudiu Manoil 
16349deba33fSClaudiu Manoil static void enetc_enable_txvlan(struct net_device *ndev, bool en)
16359deba33fSClaudiu Manoil {
16369deba33fSClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
16379deba33fSClaudiu Manoil 	int i;
16389deba33fSClaudiu Manoil 
16399deba33fSClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
16409deba33fSClaudiu Manoil 		enetc_bdr_enable_txvlan(&priv->si->hw, i, en);
16419deba33fSClaudiu Manoil }
16429deba33fSClaudiu Manoil 
1643d382563fSClaudiu Manoil int enetc_set_features(struct net_device *ndev,
1644d382563fSClaudiu Manoil 		       netdev_features_t features)
1645d382563fSClaudiu Manoil {
1646d382563fSClaudiu Manoil 	netdev_features_t changed = ndev->features ^ features;
1647888ae5a3SPo Liu 	int err = 0;
1648d382563fSClaudiu Manoil 
1649d382563fSClaudiu Manoil 	if (changed & NETIF_F_RXHASH)
1650d382563fSClaudiu Manoil 		enetc_set_rss(ndev, !!(features & NETIF_F_RXHASH));
1651d382563fSClaudiu Manoil 
16529deba33fSClaudiu Manoil 	if (changed & NETIF_F_HW_VLAN_CTAG_RX)
16539deba33fSClaudiu Manoil 		enetc_enable_rxvlan(ndev,
16549deba33fSClaudiu Manoil 				    !!(features & NETIF_F_HW_VLAN_CTAG_RX));
16559deba33fSClaudiu Manoil 
16569deba33fSClaudiu Manoil 	if (changed & NETIF_F_HW_VLAN_CTAG_TX)
16579deba33fSClaudiu Manoil 		enetc_enable_txvlan(ndev,
16589deba33fSClaudiu Manoil 				    !!(features & NETIF_F_HW_VLAN_CTAG_TX));
16599deba33fSClaudiu Manoil 
166079e49982SPo Liu 	if (changed & NETIF_F_HW_TC)
1661888ae5a3SPo Liu 		err = enetc_set_psfp(ndev, !!(features & NETIF_F_HW_TC));
166279e49982SPo Liu 
1663888ae5a3SPo Liu 	return err;
1664d382563fSClaudiu Manoil }
1665d382563fSClaudiu Manoil 
1666434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
1667d3982312SY.b. Lu static int enetc_hwtstamp_set(struct net_device *ndev, struct ifreq *ifr)
1668d3982312SY.b. Lu {
1669d3982312SY.b. Lu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1670d3982312SY.b. Lu 	struct hwtstamp_config config;
1671434cebabSClaudiu Manoil 	int ao;
1672d3982312SY.b. Lu 
1673d3982312SY.b. Lu 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1674d3982312SY.b. Lu 		return -EFAULT;
1675d3982312SY.b. Lu 
1676d3982312SY.b. Lu 	switch (config.tx_type) {
1677d3982312SY.b. Lu 	case HWTSTAMP_TX_OFF:
1678d3982312SY.b. Lu 		priv->active_offloads &= ~ENETC_F_TX_TSTAMP;
1679d3982312SY.b. Lu 		break;
1680d3982312SY.b. Lu 	case HWTSTAMP_TX_ON:
1681d3982312SY.b. Lu 		priv->active_offloads |= ENETC_F_TX_TSTAMP;
1682d3982312SY.b. Lu 		break;
1683d3982312SY.b. Lu 	default:
1684d3982312SY.b. Lu 		return -ERANGE;
1685d3982312SY.b. Lu 	}
1686d3982312SY.b. Lu 
1687434cebabSClaudiu Manoil 	ao = priv->active_offloads;
1688d3982312SY.b. Lu 	switch (config.rx_filter) {
1689d3982312SY.b. Lu 	case HWTSTAMP_FILTER_NONE:
1690d3982312SY.b. Lu 		priv->active_offloads &= ~ENETC_F_RX_TSTAMP;
1691d3982312SY.b. Lu 		break;
1692d3982312SY.b. Lu 	default:
1693d3982312SY.b. Lu 		priv->active_offloads |= ENETC_F_RX_TSTAMP;
1694d3982312SY.b. Lu 		config.rx_filter = HWTSTAMP_FILTER_ALL;
1695d3982312SY.b. Lu 	}
1696d3982312SY.b. Lu 
1697434cebabSClaudiu Manoil 	if (netif_running(ndev) && ao != priv->active_offloads) {
1698434cebabSClaudiu Manoil 		enetc_close(ndev);
1699434cebabSClaudiu Manoil 		enetc_open(ndev);
1700434cebabSClaudiu Manoil 	}
1701434cebabSClaudiu Manoil 
1702d3982312SY.b. Lu 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1703d3982312SY.b. Lu 	       -EFAULT : 0;
1704d3982312SY.b. Lu }
1705d3982312SY.b. Lu 
1706d3982312SY.b. Lu static int enetc_hwtstamp_get(struct net_device *ndev, struct ifreq *ifr)
1707d3982312SY.b. Lu {
1708d3982312SY.b. Lu 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1709d3982312SY.b. Lu 	struct hwtstamp_config config;
1710d3982312SY.b. Lu 
1711d3982312SY.b. Lu 	config.flags = 0;
1712d3982312SY.b. Lu 
1713d3982312SY.b. Lu 	if (priv->active_offloads & ENETC_F_TX_TSTAMP)
1714d3982312SY.b. Lu 		config.tx_type = HWTSTAMP_TX_ON;
1715d3982312SY.b. Lu 	else
1716d3982312SY.b. Lu 		config.tx_type = HWTSTAMP_TX_OFF;
1717d3982312SY.b. Lu 
1718d3982312SY.b. Lu 	config.rx_filter = (priv->active_offloads & ENETC_F_RX_TSTAMP) ?
1719d3982312SY.b. Lu 			    HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE;
1720d3982312SY.b. Lu 
1721d3982312SY.b. Lu 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1722d3982312SY.b. Lu 	       -EFAULT : 0;
1723d3982312SY.b. Lu }
1724d3982312SY.b. Lu #endif
1725d3982312SY.b. Lu 
1726d3982312SY.b. Lu int enetc_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
1727d3982312SY.b. Lu {
172871b77a7aSClaudiu Manoil 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1729434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
1730d3982312SY.b. Lu 	if (cmd == SIOCSHWTSTAMP)
1731d3982312SY.b. Lu 		return enetc_hwtstamp_set(ndev, rq);
1732d3982312SY.b. Lu 	if (cmd == SIOCGHWTSTAMP)
1733d3982312SY.b. Lu 		return enetc_hwtstamp_get(ndev, rq);
1734d3982312SY.b. Lu #endif
1735a613bafeSMichael Walle 
173671b77a7aSClaudiu Manoil 	if (!priv->phylink)
1737c55b810aSMichael Walle 		return -EOPNOTSUPP;
173871b77a7aSClaudiu Manoil 
173971b77a7aSClaudiu Manoil 	return phylink_mii_ioctl(priv->phylink, rq, cmd);
1740d3982312SY.b. Lu }
1741d3982312SY.b. Lu 
1742d4fd0404SClaudiu Manoil int enetc_alloc_msix(struct enetc_ndev_priv *priv)
1743d4fd0404SClaudiu Manoil {
1744d4fd0404SClaudiu Manoil 	struct pci_dev *pdev = priv->si->pdev;
17451260e772SGustavo A. R. Silva 	int v_tx_rings;
1746d4fd0404SClaudiu Manoil 	int i, n, err, nvec;
1747d4fd0404SClaudiu Manoil 
1748d4fd0404SClaudiu Manoil 	nvec = ENETC_BDR_INT_BASE_IDX + priv->bdr_int_num;
1749d4fd0404SClaudiu Manoil 	/* allocate MSIX for both messaging and Rx/Tx interrupts */
1750d4fd0404SClaudiu Manoil 	n = pci_alloc_irq_vectors(pdev, nvec, nvec, PCI_IRQ_MSIX);
1751d4fd0404SClaudiu Manoil 
1752d4fd0404SClaudiu Manoil 	if (n < 0)
1753d4fd0404SClaudiu Manoil 		return n;
1754d4fd0404SClaudiu Manoil 
1755d4fd0404SClaudiu Manoil 	if (n != nvec)
1756d4fd0404SClaudiu Manoil 		return -EPERM;
1757d4fd0404SClaudiu Manoil 
1758d4fd0404SClaudiu Manoil 	/* # of tx rings per int vector */
1759d4fd0404SClaudiu Manoil 	v_tx_rings = priv->num_tx_rings / priv->bdr_int_num;
1760d4fd0404SClaudiu Manoil 
1761d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
1762d4fd0404SClaudiu Manoil 		struct enetc_int_vector *v;
1763d4fd0404SClaudiu Manoil 		struct enetc_bdr *bdr;
1764d4fd0404SClaudiu Manoil 		int j;
1765d4fd0404SClaudiu Manoil 
17661260e772SGustavo A. R. Silva 		v = kzalloc(struct_size(v, tx_ring, v_tx_rings), GFP_KERNEL);
1767d4fd0404SClaudiu Manoil 		if (!v) {
1768d4fd0404SClaudiu Manoil 			err = -ENOMEM;
1769d4fd0404SClaudiu Manoil 			goto fail;
1770d4fd0404SClaudiu Manoil 		}
1771d4fd0404SClaudiu Manoil 
1772d4fd0404SClaudiu Manoil 		priv->int_vector[i] = v;
1773d4fd0404SClaudiu Manoil 
1774ae0e6a5dSClaudiu Manoil 		/* init defaults for adaptive IC */
1775ae0e6a5dSClaudiu Manoil 		if (priv->ic_mode & ENETC_IC_RX_ADAPTIVE) {
1776ae0e6a5dSClaudiu Manoil 			v->rx_ictt = 0x1;
1777ae0e6a5dSClaudiu Manoil 			v->rx_dim_en = true;
1778ae0e6a5dSClaudiu Manoil 		}
1779ae0e6a5dSClaudiu Manoil 		INIT_WORK(&v->rx_dim.work, enetc_rx_dim_work);
1780d4fd0404SClaudiu Manoil 		netif_napi_add(priv->ndev, &v->napi, enetc_poll,
1781d4fd0404SClaudiu Manoil 			       NAPI_POLL_WEIGHT);
1782d4fd0404SClaudiu Manoil 		v->count_tx_rings = v_tx_rings;
1783d4fd0404SClaudiu Manoil 
1784d4fd0404SClaudiu Manoil 		for (j = 0; j < v_tx_rings; j++) {
1785d4fd0404SClaudiu Manoil 			int idx;
1786d4fd0404SClaudiu Manoil 
1787d4fd0404SClaudiu Manoil 			/* default tx ring mapping policy */
1788d4fd0404SClaudiu Manoil 			if (priv->bdr_int_num == ENETC_MAX_BDR_INT)
1789d4fd0404SClaudiu Manoil 				idx = 2 * j + i; /* 2 CPUs */
1790d4fd0404SClaudiu Manoil 			else
1791d4fd0404SClaudiu Manoil 				idx = j + i * v_tx_rings; /* default */
1792d4fd0404SClaudiu Manoil 
1793d4fd0404SClaudiu Manoil 			__set_bit(idx, &v->tx_rings_map);
1794d4fd0404SClaudiu Manoil 			bdr = &v->tx_ring[j];
1795d4fd0404SClaudiu Manoil 			bdr->index = idx;
1796d4fd0404SClaudiu Manoil 			bdr->ndev = priv->ndev;
1797d4fd0404SClaudiu Manoil 			bdr->dev = priv->dev;
1798d4fd0404SClaudiu Manoil 			bdr->bd_count = priv->tx_bd_count;
1799d4fd0404SClaudiu Manoil 			priv->tx_ring[idx] = bdr;
1800d4fd0404SClaudiu Manoil 		}
1801d4fd0404SClaudiu Manoil 
1802d4fd0404SClaudiu Manoil 		bdr = &v->rx_ring;
1803d4fd0404SClaudiu Manoil 		bdr->index = i;
1804d4fd0404SClaudiu Manoil 		bdr->ndev = priv->ndev;
1805d4fd0404SClaudiu Manoil 		bdr->dev = priv->dev;
1806d4fd0404SClaudiu Manoil 		bdr->bd_count = priv->rx_bd_count;
1807d4fd0404SClaudiu Manoil 		priv->rx_ring[i] = bdr;
1808d4fd0404SClaudiu Manoil 	}
1809d4fd0404SClaudiu Manoil 
1810d4fd0404SClaudiu Manoil 	return 0;
1811d4fd0404SClaudiu Manoil 
1812d4fd0404SClaudiu Manoil fail:
1813d4fd0404SClaudiu Manoil 	while (i--) {
1814d4fd0404SClaudiu Manoil 		netif_napi_del(&priv->int_vector[i]->napi);
1815ae0e6a5dSClaudiu Manoil 		cancel_work_sync(&priv->int_vector[i]->rx_dim.work);
1816d4fd0404SClaudiu Manoil 		kfree(priv->int_vector[i]);
1817d4fd0404SClaudiu Manoil 	}
1818d4fd0404SClaudiu Manoil 
1819d4fd0404SClaudiu Manoil 	pci_free_irq_vectors(pdev);
1820d4fd0404SClaudiu Manoil 
1821d4fd0404SClaudiu Manoil 	return err;
1822d4fd0404SClaudiu Manoil }
1823d4fd0404SClaudiu Manoil 
1824d4fd0404SClaudiu Manoil void enetc_free_msix(struct enetc_ndev_priv *priv)
1825d4fd0404SClaudiu Manoil {
1826d4fd0404SClaudiu Manoil 	int i;
1827d4fd0404SClaudiu Manoil 
1828d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
1829d4fd0404SClaudiu Manoil 		struct enetc_int_vector *v = priv->int_vector[i];
1830d4fd0404SClaudiu Manoil 
1831d4fd0404SClaudiu Manoil 		netif_napi_del(&v->napi);
1832ae0e6a5dSClaudiu Manoil 		cancel_work_sync(&v->rx_dim.work);
1833d4fd0404SClaudiu Manoil 	}
1834d4fd0404SClaudiu Manoil 
1835d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_rx_rings; i++)
1836d4fd0404SClaudiu Manoil 		priv->rx_ring[i] = NULL;
1837d4fd0404SClaudiu Manoil 
1838d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->num_tx_rings; i++)
1839d4fd0404SClaudiu Manoil 		priv->tx_ring[i] = NULL;
1840d4fd0404SClaudiu Manoil 
1841d4fd0404SClaudiu Manoil 	for (i = 0; i < priv->bdr_int_num; i++) {
1842d4fd0404SClaudiu Manoil 		kfree(priv->int_vector[i]);
1843d4fd0404SClaudiu Manoil 		priv->int_vector[i] = NULL;
1844d4fd0404SClaudiu Manoil 	}
1845d4fd0404SClaudiu Manoil 
1846d4fd0404SClaudiu Manoil 	/* disable all MSIX for this device */
1847d4fd0404SClaudiu Manoil 	pci_free_irq_vectors(priv->si->pdev);
1848d4fd0404SClaudiu Manoil }
1849d4fd0404SClaudiu Manoil 
1850d4fd0404SClaudiu Manoil static void enetc_kfree_si(struct enetc_si *si)
1851d4fd0404SClaudiu Manoil {
1852d4fd0404SClaudiu Manoil 	char *p = (char *)si - si->pad;
1853d4fd0404SClaudiu Manoil 
1854d4fd0404SClaudiu Manoil 	kfree(p);
1855d4fd0404SClaudiu Manoil }
1856d4fd0404SClaudiu Manoil 
1857d4fd0404SClaudiu Manoil static void enetc_detect_errata(struct enetc_si *si)
1858d4fd0404SClaudiu Manoil {
1859d4fd0404SClaudiu Manoil 	if (si->pdev->revision == ENETC_REV1)
1860*82728b91SClaudiu Manoil 		si->errata = ENETC_ERR_VLAN_ISOL | ENETC_ERR_UCMCSWP;
1861d4fd0404SClaudiu Manoil }
1862d4fd0404SClaudiu Manoil 
1863d4fd0404SClaudiu Manoil int enetc_pci_probe(struct pci_dev *pdev, const char *name, int sizeof_priv)
1864d4fd0404SClaudiu Manoil {
1865d4fd0404SClaudiu Manoil 	struct enetc_si *si, *p;
1866d4fd0404SClaudiu Manoil 	struct enetc_hw *hw;
1867d4fd0404SClaudiu Manoil 	size_t alloc_size;
1868d4fd0404SClaudiu Manoil 	int err, len;
1869d4fd0404SClaudiu Manoil 
1870d4fd0404SClaudiu Manoil 	pcie_flr(pdev);
1871d4fd0404SClaudiu Manoil 	err = pci_enable_device_mem(pdev);
1872d4fd0404SClaudiu Manoil 	if (err) {
1873d4fd0404SClaudiu Manoil 		dev_err(&pdev->dev, "device enable failed\n");
1874d4fd0404SClaudiu Manoil 		return err;
1875d4fd0404SClaudiu Manoil 	}
1876d4fd0404SClaudiu Manoil 
1877d4fd0404SClaudiu Manoil 	/* set up for high or low dma */
1878d4fd0404SClaudiu Manoil 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1879d4fd0404SClaudiu Manoil 	if (err) {
1880d4fd0404SClaudiu Manoil 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1881d4fd0404SClaudiu Manoil 		if (err) {
1882d4fd0404SClaudiu Manoil 			dev_err(&pdev->dev,
1883d4fd0404SClaudiu Manoil 				"DMA configuration failed: 0x%x\n", err);
1884d4fd0404SClaudiu Manoil 			goto err_dma;
1885d4fd0404SClaudiu Manoil 		}
1886d4fd0404SClaudiu Manoil 	}
1887d4fd0404SClaudiu Manoil 
1888d4fd0404SClaudiu Manoil 	err = pci_request_mem_regions(pdev, name);
1889d4fd0404SClaudiu Manoil 	if (err) {
1890d4fd0404SClaudiu Manoil 		dev_err(&pdev->dev, "pci_request_regions failed err=%d\n", err);
1891d4fd0404SClaudiu Manoil 		goto err_pci_mem_reg;
1892d4fd0404SClaudiu Manoil 	}
1893d4fd0404SClaudiu Manoil 
1894d4fd0404SClaudiu Manoil 	pci_set_master(pdev);
1895d4fd0404SClaudiu Manoil 
1896d4fd0404SClaudiu Manoil 	alloc_size = sizeof(struct enetc_si);
1897d4fd0404SClaudiu Manoil 	if (sizeof_priv) {
1898d4fd0404SClaudiu Manoil 		/* align priv to 32B */
1899d4fd0404SClaudiu Manoil 		alloc_size = ALIGN(alloc_size, ENETC_SI_ALIGN);
1900d4fd0404SClaudiu Manoil 		alloc_size += sizeof_priv;
1901d4fd0404SClaudiu Manoil 	}
1902d4fd0404SClaudiu Manoil 	/* force 32B alignment for enetc_si */
1903d4fd0404SClaudiu Manoil 	alloc_size += ENETC_SI_ALIGN - 1;
1904d4fd0404SClaudiu Manoil 
1905d4fd0404SClaudiu Manoil 	p = kzalloc(alloc_size, GFP_KERNEL);
1906d4fd0404SClaudiu Manoil 	if (!p) {
1907d4fd0404SClaudiu Manoil 		err = -ENOMEM;
1908d4fd0404SClaudiu Manoil 		goto err_alloc_si;
1909d4fd0404SClaudiu Manoil 	}
1910d4fd0404SClaudiu Manoil 
1911d4fd0404SClaudiu Manoil 	si = PTR_ALIGN(p, ENETC_SI_ALIGN);
1912d4fd0404SClaudiu Manoil 	si->pad = (char *)si - (char *)p;
1913d4fd0404SClaudiu Manoil 
1914d4fd0404SClaudiu Manoil 	pci_set_drvdata(pdev, si);
1915d4fd0404SClaudiu Manoil 	si->pdev = pdev;
1916d4fd0404SClaudiu Manoil 	hw = &si->hw;
1917d4fd0404SClaudiu Manoil 
1918d4fd0404SClaudiu Manoil 	len = pci_resource_len(pdev, ENETC_BAR_REGS);
1919d4fd0404SClaudiu Manoil 	hw->reg = ioremap(pci_resource_start(pdev, ENETC_BAR_REGS), len);
1920d4fd0404SClaudiu Manoil 	if (!hw->reg) {
1921d4fd0404SClaudiu Manoil 		err = -ENXIO;
1922d4fd0404SClaudiu Manoil 		dev_err(&pdev->dev, "ioremap() failed\n");
1923d4fd0404SClaudiu Manoil 		goto err_ioremap;
1924d4fd0404SClaudiu Manoil 	}
1925d4fd0404SClaudiu Manoil 	if (len > ENETC_PORT_BASE)
1926d4fd0404SClaudiu Manoil 		hw->port = hw->reg + ENETC_PORT_BASE;
1927d4fd0404SClaudiu Manoil 	if (len > ENETC_GLOBAL_BASE)
1928d4fd0404SClaudiu Manoil 		hw->global = hw->reg + ENETC_GLOBAL_BASE;
1929d4fd0404SClaudiu Manoil 
1930d4fd0404SClaudiu Manoil 	enetc_detect_errata(si);
1931d4fd0404SClaudiu Manoil 
1932d4fd0404SClaudiu Manoil 	return 0;
1933d4fd0404SClaudiu Manoil 
1934d4fd0404SClaudiu Manoil err_ioremap:
1935d4fd0404SClaudiu Manoil 	enetc_kfree_si(si);
1936d4fd0404SClaudiu Manoil err_alloc_si:
1937d4fd0404SClaudiu Manoil 	pci_release_mem_regions(pdev);
1938d4fd0404SClaudiu Manoil err_pci_mem_reg:
1939d4fd0404SClaudiu Manoil err_dma:
1940d4fd0404SClaudiu Manoil 	pci_disable_device(pdev);
1941d4fd0404SClaudiu Manoil 
1942d4fd0404SClaudiu Manoil 	return err;
1943d4fd0404SClaudiu Manoil }
1944d4fd0404SClaudiu Manoil 
1945d4fd0404SClaudiu Manoil void enetc_pci_remove(struct pci_dev *pdev)
1946d4fd0404SClaudiu Manoil {
1947d4fd0404SClaudiu Manoil 	struct enetc_si *si = pci_get_drvdata(pdev);
1948d4fd0404SClaudiu Manoil 	struct enetc_hw *hw = &si->hw;
1949d4fd0404SClaudiu Manoil 
1950d4fd0404SClaudiu Manoil 	iounmap(hw->reg);
1951d4fd0404SClaudiu Manoil 	enetc_kfree_si(si);
1952d4fd0404SClaudiu Manoil 	pci_release_mem_regions(pdev);
1953d4fd0404SClaudiu Manoil 	pci_disable_device(pdev);
1954d4fd0404SClaudiu Manoil }
1955