1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright (c) 2018, Intel Corporation. */
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun /* The driver transmit and receive code */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/prefetch.h>
7*4882a593Smuzhiyun #include <linux/mm.h>
8*4882a593Smuzhiyun #include <linux/bpf_trace.h>
9*4882a593Smuzhiyun #include <net/xdp.h>
10*4882a593Smuzhiyun #include "ice_txrx_lib.h"
11*4882a593Smuzhiyun #include "ice_lib.h"
12*4882a593Smuzhiyun #include "ice.h"
13*4882a593Smuzhiyun #include "ice_dcb_lib.h"
14*4882a593Smuzhiyun #include "ice_xsk.h"
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #define ICE_RX_HDR_SIZE 256
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #define FDIR_DESC_RXDID 0x40
19*4882a593Smuzhiyun #define ICE_FDIR_CLEAN_DELAY 10
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /**
22*4882a593Smuzhiyun * ice_prgm_fdir_fltr - Program a Flow Director filter
23*4882a593Smuzhiyun * @vsi: VSI to send dummy packet
24*4882a593Smuzhiyun * @fdir_desc: flow director descriptor
25*4882a593Smuzhiyun * @raw_packet: allocated buffer for flow director
26*4882a593Smuzhiyun */
27*4882a593Smuzhiyun int
ice_prgm_fdir_fltr(struct ice_vsi * vsi,struct ice_fltr_desc * fdir_desc,u8 * raw_packet)28*4882a593Smuzhiyun ice_prgm_fdir_fltr(struct ice_vsi *vsi, struct ice_fltr_desc *fdir_desc,
29*4882a593Smuzhiyun u8 *raw_packet)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun struct ice_tx_buf *tx_buf, *first;
32*4882a593Smuzhiyun struct ice_fltr_desc *f_desc;
33*4882a593Smuzhiyun struct ice_tx_desc *tx_desc;
34*4882a593Smuzhiyun struct ice_ring *tx_ring;
35*4882a593Smuzhiyun struct device *dev;
36*4882a593Smuzhiyun dma_addr_t dma;
37*4882a593Smuzhiyun u32 td_cmd;
38*4882a593Smuzhiyun u16 i;
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /* VSI and Tx ring */
41*4882a593Smuzhiyun if (!vsi)
42*4882a593Smuzhiyun return -ENOENT;
43*4882a593Smuzhiyun tx_ring = vsi->tx_rings[0];
44*4882a593Smuzhiyun if (!tx_ring || !tx_ring->desc)
45*4882a593Smuzhiyun return -ENOENT;
46*4882a593Smuzhiyun dev = tx_ring->dev;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /* we are using two descriptors to add/del a filter and we can wait */
49*4882a593Smuzhiyun for (i = ICE_FDIR_CLEAN_DELAY; ICE_DESC_UNUSED(tx_ring) < 2; i--) {
50*4882a593Smuzhiyun if (!i)
51*4882a593Smuzhiyun return -EAGAIN;
52*4882a593Smuzhiyun msleep_interruptible(1);
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun dma = dma_map_single(dev, raw_packet, ICE_FDIR_MAX_RAW_PKT_SIZE,
56*4882a593Smuzhiyun DMA_TO_DEVICE);
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun if (dma_mapping_error(dev, dma))
59*4882a593Smuzhiyun return -EINVAL;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /* grab the next descriptor */
62*4882a593Smuzhiyun i = tx_ring->next_to_use;
63*4882a593Smuzhiyun first = &tx_ring->tx_buf[i];
64*4882a593Smuzhiyun f_desc = ICE_TX_FDIRDESC(tx_ring, i);
65*4882a593Smuzhiyun memcpy(f_desc, fdir_desc, sizeof(*f_desc));
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun i++;
68*4882a593Smuzhiyun i = (i < tx_ring->count) ? i : 0;
69*4882a593Smuzhiyun tx_desc = ICE_TX_DESC(tx_ring, i);
70*4882a593Smuzhiyun tx_buf = &tx_ring->tx_buf[i];
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun i++;
73*4882a593Smuzhiyun tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun memset(tx_buf, 0, sizeof(*tx_buf));
76*4882a593Smuzhiyun dma_unmap_len_set(tx_buf, len, ICE_FDIR_MAX_RAW_PKT_SIZE);
77*4882a593Smuzhiyun dma_unmap_addr_set(tx_buf, dma, dma);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun tx_desc->buf_addr = cpu_to_le64(dma);
80*4882a593Smuzhiyun td_cmd = ICE_TXD_LAST_DESC_CMD | ICE_TX_DESC_CMD_DUMMY |
81*4882a593Smuzhiyun ICE_TX_DESC_CMD_RE;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun tx_buf->tx_flags = ICE_TX_FLAGS_DUMMY_PKT;
84*4882a593Smuzhiyun tx_buf->raw_buf = raw_packet;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun tx_desc->cmd_type_offset_bsz =
87*4882a593Smuzhiyun ice_build_ctob(td_cmd, 0, ICE_FDIR_MAX_RAW_PKT_SIZE, 0);
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /* Force memory write to complete before letting h/w know
90*4882a593Smuzhiyun * there are new descriptors to fetch.
91*4882a593Smuzhiyun */
92*4882a593Smuzhiyun wmb();
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* mark the data descriptor to be watched */
95*4882a593Smuzhiyun first->next_to_watch = tx_desc;
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun writel(tx_ring->next_to_use, tx_ring->tail);
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun return 0;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /**
103*4882a593Smuzhiyun * ice_unmap_and_free_tx_buf - Release a Tx buffer
104*4882a593Smuzhiyun * @ring: the ring that owns the buffer
105*4882a593Smuzhiyun * @tx_buf: the buffer to free
106*4882a593Smuzhiyun */
107*4882a593Smuzhiyun static void
ice_unmap_and_free_tx_buf(struct ice_ring * ring,struct ice_tx_buf * tx_buf)108*4882a593Smuzhiyun ice_unmap_and_free_tx_buf(struct ice_ring *ring, struct ice_tx_buf *tx_buf)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun if (tx_buf->skb) {
111*4882a593Smuzhiyun if (tx_buf->tx_flags & ICE_TX_FLAGS_DUMMY_PKT)
112*4882a593Smuzhiyun devm_kfree(ring->dev, tx_buf->raw_buf);
113*4882a593Smuzhiyun else if (ice_ring_is_xdp(ring))
114*4882a593Smuzhiyun page_frag_free(tx_buf->raw_buf);
115*4882a593Smuzhiyun else
116*4882a593Smuzhiyun dev_kfree_skb_any(tx_buf->skb);
117*4882a593Smuzhiyun if (dma_unmap_len(tx_buf, len))
118*4882a593Smuzhiyun dma_unmap_single(ring->dev,
119*4882a593Smuzhiyun dma_unmap_addr(tx_buf, dma),
120*4882a593Smuzhiyun dma_unmap_len(tx_buf, len),
121*4882a593Smuzhiyun DMA_TO_DEVICE);
122*4882a593Smuzhiyun } else if (dma_unmap_len(tx_buf, len)) {
123*4882a593Smuzhiyun dma_unmap_page(ring->dev,
124*4882a593Smuzhiyun dma_unmap_addr(tx_buf, dma),
125*4882a593Smuzhiyun dma_unmap_len(tx_buf, len),
126*4882a593Smuzhiyun DMA_TO_DEVICE);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun tx_buf->next_to_watch = NULL;
130*4882a593Smuzhiyun tx_buf->skb = NULL;
131*4882a593Smuzhiyun dma_unmap_len_set(tx_buf, len, 0);
132*4882a593Smuzhiyun /* tx_buf must be completely set up in the transmit path */
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
txring_txq(const struct ice_ring * ring)135*4882a593Smuzhiyun static struct netdev_queue *txring_txq(const struct ice_ring *ring)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun return netdev_get_tx_queue(ring->netdev, ring->q_index);
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /**
141*4882a593Smuzhiyun * ice_clean_tx_ring - Free any empty Tx buffers
142*4882a593Smuzhiyun * @tx_ring: ring to be cleaned
143*4882a593Smuzhiyun */
ice_clean_tx_ring(struct ice_ring * tx_ring)144*4882a593Smuzhiyun void ice_clean_tx_ring(struct ice_ring *tx_ring)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun u16 i;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun if (ice_ring_is_xdp(tx_ring) && tx_ring->xsk_pool) {
149*4882a593Smuzhiyun ice_xsk_clean_xdp_ring(tx_ring);
150*4882a593Smuzhiyun goto tx_skip_free;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /* ring already cleared, nothing to do */
154*4882a593Smuzhiyun if (!tx_ring->tx_buf)
155*4882a593Smuzhiyun return;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /* Free all the Tx ring sk_buffs */
158*4882a593Smuzhiyun for (i = 0; i < tx_ring->count; i++)
159*4882a593Smuzhiyun ice_unmap_and_free_tx_buf(tx_ring, &tx_ring->tx_buf[i]);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun tx_skip_free:
162*4882a593Smuzhiyun memset(tx_ring->tx_buf, 0, sizeof(*tx_ring->tx_buf) * tx_ring->count);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /* Zero out the descriptor ring */
165*4882a593Smuzhiyun memset(tx_ring->desc, 0, tx_ring->size);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun tx_ring->next_to_use = 0;
168*4882a593Smuzhiyun tx_ring->next_to_clean = 0;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun if (!tx_ring->netdev)
171*4882a593Smuzhiyun return;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /* cleanup Tx queue statistics */
174*4882a593Smuzhiyun netdev_tx_reset_queue(txring_txq(tx_ring));
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /**
178*4882a593Smuzhiyun * ice_free_tx_ring - Free Tx resources per queue
179*4882a593Smuzhiyun * @tx_ring: Tx descriptor ring for a specific queue
180*4882a593Smuzhiyun *
181*4882a593Smuzhiyun * Free all transmit software resources
182*4882a593Smuzhiyun */
ice_free_tx_ring(struct ice_ring * tx_ring)183*4882a593Smuzhiyun void ice_free_tx_ring(struct ice_ring *tx_ring)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun ice_clean_tx_ring(tx_ring);
186*4882a593Smuzhiyun devm_kfree(tx_ring->dev, tx_ring->tx_buf);
187*4882a593Smuzhiyun tx_ring->tx_buf = NULL;
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun if (tx_ring->desc) {
190*4882a593Smuzhiyun dmam_free_coherent(tx_ring->dev, tx_ring->size,
191*4882a593Smuzhiyun tx_ring->desc, tx_ring->dma);
192*4882a593Smuzhiyun tx_ring->desc = NULL;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun /**
197*4882a593Smuzhiyun * ice_clean_tx_irq - Reclaim resources after transmit completes
198*4882a593Smuzhiyun * @tx_ring: Tx ring to clean
199*4882a593Smuzhiyun * @napi_budget: Used to determine if we are in netpoll
200*4882a593Smuzhiyun *
201*4882a593Smuzhiyun * Returns true if there's any budget left (e.g. the clean is finished)
202*4882a593Smuzhiyun */
ice_clean_tx_irq(struct ice_ring * tx_ring,int napi_budget)203*4882a593Smuzhiyun static bool ice_clean_tx_irq(struct ice_ring *tx_ring, int napi_budget)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun unsigned int total_bytes = 0, total_pkts = 0;
206*4882a593Smuzhiyun unsigned int budget = ICE_DFLT_IRQ_WORK;
207*4882a593Smuzhiyun struct ice_vsi *vsi = tx_ring->vsi;
208*4882a593Smuzhiyun s16 i = tx_ring->next_to_clean;
209*4882a593Smuzhiyun struct ice_tx_desc *tx_desc;
210*4882a593Smuzhiyun struct ice_tx_buf *tx_buf;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun tx_buf = &tx_ring->tx_buf[i];
213*4882a593Smuzhiyun tx_desc = ICE_TX_DESC(tx_ring, i);
214*4882a593Smuzhiyun i -= tx_ring->count;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun prefetch(&vsi->state);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun do {
219*4882a593Smuzhiyun struct ice_tx_desc *eop_desc = tx_buf->next_to_watch;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /* if next_to_watch is not set then there is no work pending */
222*4882a593Smuzhiyun if (!eop_desc)
223*4882a593Smuzhiyun break;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun smp_rmb(); /* prevent any other reads prior to eop_desc */
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun /* if the descriptor isn't done, no work yet to do */
228*4882a593Smuzhiyun if (!(eop_desc->cmd_type_offset_bsz &
229*4882a593Smuzhiyun cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)))
230*4882a593Smuzhiyun break;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun /* clear next_to_watch to prevent false hangs */
233*4882a593Smuzhiyun tx_buf->next_to_watch = NULL;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun /* update the statistics for this packet */
236*4882a593Smuzhiyun total_bytes += tx_buf->bytecount;
237*4882a593Smuzhiyun total_pkts += tx_buf->gso_segs;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun if (ice_ring_is_xdp(tx_ring))
240*4882a593Smuzhiyun page_frag_free(tx_buf->raw_buf);
241*4882a593Smuzhiyun else
242*4882a593Smuzhiyun /* free the skb */
243*4882a593Smuzhiyun napi_consume_skb(tx_buf->skb, napi_budget);
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun /* unmap skb header data */
246*4882a593Smuzhiyun dma_unmap_single(tx_ring->dev,
247*4882a593Smuzhiyun dma_unmap_addr(tx_buf, dma),
248*4882a593Smuzhiyun dma_unmap_len(tx_buf, len),
249*4882a593Smuzhiyun DMA_TO_DEVICE);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /* clear tx_buf data */
252*4882a593Smuzhiyun tx_buf->skb = NULL;
253*4882a593Smuzhiyun dma_unmap_len_set(tx_buf, len, 0);
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun /* unmap remaining buffers */
256*4882a593Smuzhiyun while (tx_desc != eop_desc) {
257*4882a593Smuzhiyun tx_buf++;
258*4882a593Smuzhiyun tx_desc++;
259*4882a593Smuzhiyun i++;
260*4882a593Smuzhiyun if (unlikely(!i)) {
261*4882a593Smuzhiyun i -= tx_ring->count;
262*4882a593Smuzhiyun tx_buf = tx_ring->tx_buf;
263*4882a593Smuzhiyun tx_desc = ICE_TX_DESC(tx_ring, 0);
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun /* unmap any remaining paged data */
267*4882a593Smuzhiyun if (dma_unmap_len(tx_buf, len)) {
268*4882a593Smuzhiyun dma_unmap_page(tx_ring->dev,
269*4882a593Smuzhiyun dma_unmap_addr(tx_buf, dma),
270*4882a593Smuzhiyun dma_unmap_len(tx_buf, len),
271*4882a593Smuzhiyun DMA_TO_DEVICE);
272*4882a593Smuzhiyun dma_unmap_len_set(tx_buf, len, 0);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun /* move us one more past the eop_desc for start of next pkt */
277*4882a593Smuzhiyun tx_buf++;
278*4882a593Smuzhiyun tx_desc++;
279*4882a593Smuzhiyun i++;
280*4882a593Smuzhiyun if (unlikely(!i)) {
281*4882a593Smuzhiyun i -= tx_ring->count;
282*4882a593Smuzhiyun tx_buf = tx_ring->tx_buf;
283*4882a593Smuzhiyun tx_desc = ICE_TX_DESC(tx_ring, 0);
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun prefetch(tx_desc);
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun /* update budget accounting */
289*4882a593Smuzhiyun budget--;
290*4882a593Smuzhiyun } while (likely(budget));
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun i += tx_ring->count;
293*4882a593Smuzhiyun tx_ring->next_to_clean = i;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun ice_update_tx_ring_stats(tx_ring, total_pkts, total_bytes);
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun if (ice_ring_is_xdp(tx_ring))
298*4882a593Smuzhiyun return !!budget;
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun netdev_tx_completed_queue(txring_txq(tx_ring), total_pkts,
301*4882a593Smuzhiyun total_bytes);
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun #define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2))
304*4882a593Smuzhiyun if (unlikely(total_pkts && netif_carrier_ok(tx_ring->netdev) &&
305*4882a593Smuzhiyun (ICE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
306*4882a593Smuzhiyun /* Make sure that anybody stopping the queue after this
307*4882a593Smuzhiyun * sees the new next_to_clean.
308*4882a593Smuzhiyun */
309*4882a593Smuzhiyun smp_mb();
310*4882a593Smuzhiyun if (__netif_subqueue_stopped(tx_ring->netdev,
311*4882a593Smuzhiyun tx_ring->q_index) &&
312*4882a593Smuzhiyun !test_bit(__ICE_DOWN, vsi->state)) {
313*4882a593Smuzhiyun netif_wake_subqueue(tx_ring->netdev,
314*4882a593Smuzhiyun tx_ring->q_index);
315*4882a593Smuzhiyun ++tx_ring->tx_stats.restart_q;
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun return !!budget;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun /**
323*4882a593Smuzhiyun * ice_setup_tx_ring - Allocate the Tx descriptors
324*4882a593Smuzhiyun * @tx_ring: the Tx ring to set up
325*4882a593Smuzhiyun *
326*4882a593Smuzhiyun * Return 0 on success, negative on error
327*4882a593Smuzhiyun */
ice_setup_tx_ring(struct ice_ring * tx_ring)328*4882a593Smuzhiyun int ice_setup_tx_ring(struct ice_ring *tx_ring)
329*4882a593Smuzhiyun {
330*4882a593Smuzhiyun struct device *dev = tx_ring->dev;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun if (!dev)
333*4882a593Smuzhiyun return -ENOMEM;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /* warn if we are about to overwrite the pointer */
336*4882a593Smuzhiyun WARN_ON(tx_ring->tx_buf);
337*4882a593Smuzhiyun tx_ring->tx_buf =
338*4882a593Smuzhiyun devm_kzalloc(dev, sizeof(*tx_ring->tx_buf) * tx_ring->count,
339*4882a593Smuzhiyun GFP_KERNEL);
340*4882a593Smuzhiyun if (!tx_ring->tx_buf)
341*4882a593Smuzhiyun return -ENOMEM;
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun /* round up to nearest page */
344*4882a593Smuzhiyun tx_ring->size = ALIGN(tx_ring->count * sizeof(struct ice_tx_desc),
345*4882a593Smuzhiyun PAGE_SIZE);
346*4882a593Smuzhiyun tx_ring->desc = dmam_alloc_coherent(dev, tx_ring->size, &tx_ring->dma,
347*4882a593Smuzhiyun GFP_KERNEL);
348*4882a593Smuzhiyun if (!tx_ring->desc) {
349*4882a593Smuzhiyun dev_err(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
350*4882a593Smuzhiyun tx_ring->size);
351*4882a593Smuzhiyun goto err;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun tx_ring->next_to_use = 0;
355*4882a593Smuzhiyun tx_ring->next_to_clean = 0;
356*4882a593Smuzhiyun tx_ring->tx_stats.prev_pkt = -1;
357*4882a593Smuzhiyun return 0;
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun err:
360*4882a593Smuzhiyun devm_kfree(dev, tx_ring->tx_buf);
361*4882a593Smuzhiyun tx_ring->tx_buf = NULL;
362*4882a593Smuzhiyun return -ENOMEM;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun /**
366*4882a593Smuzhiyun * ice_clean_rx_ring - Free Rx buffers
367*4882a593Smuzhiyun * @rx_ring: ring to be cleaned
368*4882a593Smuzhiyun */
ice_clean_rx_ring(struct ice_ring * rx_ring)369*4882a593Smuzhiyun void ice_clean_rx_ring(struct ice_ring *rx_ring)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun struct device *dev = rx_ring->dev;
372*4882a593Smuzhiyun u16 i;
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun /* ring already cleared, nothing to do */
375*4882a593Smuzhiyun if (!rx_ring->rx_buf)
376*4882a593Smuzhiyun return;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun if (rx_ring->xsk_pool) {
379*4882a593Smuzhiyun ice_xsk_clean_rx_ring(rx_ring);
380*4882a593Smuzhiyun goto rx_skip_free;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun /* Free all the Rx ring sk_buffs */
384*4882a593Smuzhiyun for (i = 0; i < rx_ring->count; i++) {
385*4882a593Smuzhiyun struct ice_rx_buf *rx_buf = &rx_ring->rx_buf[i];
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun if (rx_buf->skb) {
388*4882a593Smuzhiyun dev_kfree_skb(rx_buf->skb);
389*4882a593Smuzhiyun rx_buf->skb = NULL;
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun if (!rx_buf->page)
392*4882a593Smuzhiyun continue;
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun /* Invalidate cache lines that may have been written to by
395*4882a593Smuzhiyun * device so that we avoid corrupting memory.
396*4882a593Smuzhiyun */
397*4882a593Smuzhiyun dma_sync_single_range_for_cpu(dev, rx_buf->dma,
398*4882a593Smuzhiyun rx_buf->page_offset,
399*4882a593Smuzhiyun rx_ring->rx_buf_len,
400*4882a593Smuzhiyun DMA_FROM_DEVICE);
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun /* free resources associated with mapping */
403*4882a593Smuzhiyun dma_unmap_page_attrs(dev, rx_buf->dma, ice_rx_pg_size(rx_ring),
404*4882a593Smuzhiyun DMA_FROM_DEVICE, ICE_RX_DMA_ATTR);
405*4882a593Smuzhiyun __page_frag_cache_drain(rx_buf->page, rx_buf->pagecnt_bias);
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun rx_buf->page = NULL;
408*4882a593Smuzhiyun rx_buf->page_offset = 0;
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun rx_skip_free:
412*4882a593Smuzhiyun memset(rx_ring->rx_buf, 0, sizeof(*rx_ring->rx_buf) * rx_ring->count);
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun /* Zero out the descriptor ring */
415*4882a593Smuzhiyun memset(rx_ring->desc, 0, rx_ring->size);
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun rx_ring->next_to_alloc = 0;
418*4882a593Smuzhiyun rx_ring->next_to_clean = 0;
419*4882a593Smuzhiyun rx_ring->next_to_use = 0;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun /**
423*4882a593Smuzhiyun * ice_free_rx_ring - Free Rx resources
424*4882a593Smuzhiyun * @rx_ring: ring to clean the resources from
425*4882a593Smuzhiyun *
426*4882a593Smuzhiyun * Free all receive software resources
427*4882a593Smuzhiyun */
ice_free_rx_ring(struct ice_ring * rx_ring)428*4882a593Smuzhiyun void ice_free_rx_ring(struct ice_ring *rx_ring)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun ice_clean_rx_ring(rx_ring);
431*4882a593Smuzhiyun if (rx_ring->vsi->type == ICE_VSI_PF)
432*4882a593Smuzhiyun if (xdp_rxq_info_is_reg(&rx_ring->xdp_rxq))
433*4882a593Smuzhiyun xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
434*4882a593Smuzhiyun rx_ring->xdp_prog = NULL;
435*4882a593Smuzhiyun devm_kfree(rx_ring->dev, rx_ring->rx_buf);
436*4882a593Smuzhiyun rx_ring->rx_buf = NULL;
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun if (rx_ring->desc) {
439*4882a593Smuzhiyun dmam_free_coherent(rx_ring->dev, rx_ring->size,
440*4882a593Smuzhiyun rx_ring->desc, rx_ring->dma);
441*4882a593Smuzhiyun rx_ring->desc = NULL;
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun /**
446*4882a593Smuzhiyun * ice_setup_rx_ring - Allocate the Rx descriptors
447*4882a593Smuzhiyun * @rx_ring: the Rx ring to set up
448*4882a593Smuzhiyun *
449*4882a593Smuzhiyun * Return 0 on success, negative on error
450*4882a593Smuzhiyun */
ice_setup_rx_ring(struct ice_ring * rx_ring)451*4882a593Smuzhiyun int ice_setup_rx_ring(struct ice_ring *rx_ring)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun struct device *dev = rx_ring->dev;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun if (!dev)
456*4882a593Smuzhiyun return -ENOMEM;
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun /* warn if we are about to overwrite the pointer */
459*4882a593Smuzhiyun WARN_ON(rx_ring->rx_buf);
460*4882a593Smuzhiyun rx_ring->rx_buf =
461*4882a593Smuzhiyun devm_kzalloc(dev, sizeof(*rx_ring->rx_buf) * rx_ring->count,
462*4882a593Smuzhiyun GFP_KERNEL);
463*4882a593Smuzhiyun if (!rx_ring->rx_buf)
464*4882a593Smuzhiyun return -ENOMEM;
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun /* round up to nearest page */
467*4882a593Smuzhiyun rx_ring->size = ALIGN(rx_ring->count * sizeof(union ice_32byte_rx_desc),
468*4882a593Smuzhiyun PAGE_SIZE);
469*4882a593Smuzhiyun rx_ring->desc = dmam_alloc_coherent(dev, rx_ring->size, &rx_ring->dma,
470*4882a593Smuzhiyun GFP_KERNEL);
471*4882a593Smuzhiyun if (!rx_ring->desc) {
472*4882a593Smuzhiyun dev_err(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
473*4882a593Smuzhiyun rx_ring->size);
474*4882a593Smuzhiyun goto err;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun rx_ring->next_to_use = 0;
478*4882a593Smuzhiyun rx_ring->next_to_clean = 0;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun if (ice_is_xdp_ena_vsi(rx_ring->vsi))
481*4882a593Smuzhiyun WRITE_ONCE(rx_ring->xdp_prog, rx_ring->vsi->xdp_prog);
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun if (rx_ring->vsi->type == ICE_VSI_PF &&
484*4882a593Smuzhiyun !xdp_rxq_info_is_reg(&rx_ring->xdp_rxq))
485*4882a593Smuzhiyun if (xdp_rxq_info_reg(&rx_ring->xdp_rxq, rx_ring->netdev,
486*4882a593Smuzhiyun rx_ring->q_index))
487*4882a593Smuzhiyun goto err;
488*4882a593Smuzhiyun return 0;
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun err:
491*4882a593Smuzhiyun devm_kfree(dev, rx_ring->rx_buf);
492*4882a593Smuzhiyun rx_ring->rx_buf = NULL;
493*4882a593Smuzhiyun return -ENOMEM;
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun /**
497*4882a593Smuzhiyun * ice_rx_offset - Return expected offset into page to access data
498*4882a593Smuzhiyun * @rx_ring: Ring we are requesting offset of
499*4882a593Smuzhiyun *
500*4882a593Smuzhiyun * Returns the offset value for ring into the data buffer.
501*4882a593Smuzhiyun */
ice_rx_offset(struct ice_ring * rx_ring)502*4882a593Smuzhiyun static unsigned int ice_rx_offset(struct ice_ring *rx_ring)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun if (ice_ring_uses_build_skb(rx_ring))
505*4882a593Smuzhiyun return ICE_SKB_PAD;
506*4882a593Smuzhiyun else if (ice_is_xdp_ena_vsi(rx_ring->vsi))
507*4882a593Smuzhiyun return XDP_PACKET_HEADROOM;
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun return 0;
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun static unsigned int
ice_rx_frame_truesize(struct ice_ring * rx_ring,unsigned int __maybe_unused size)513*4882a593Smuzhiyun ice_rx_frame_truesize(struct ice_ring *rx_ring, unsigned int __maybe_unused size)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun unsigned int truesize;
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun #if (PAGE_SIZE < 8192)
518*4882a593Smuzhiyun truesize = ice_rx_pg_size(rx_ring) / 2; /* Must be power-of-2 */
519*4882a593Smuzhiyun #else
520*4882a593Smuzhiyun truesize = ice_rx_offset(rx_ring) ?
521*4882a593Smuzhiyun SKB_DATA_ALIGN(ice_rx_offset(rx_ring) + size) +
522*4882a593Smuzhiyun SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) :
523*4882a593Smuzhiyun SKB_DATA_ALIGN(size);
524*4882a593Smuzhiyun #endif
525*4882a593Smuzhiyun return truesize;
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun /**
529*4882a593Smuzhiyun * ice_run_xdp - Executes an XDP program on initialized xdp_buff
530*4882a593Smuzhiyun * @rx_ring: Rx ring
531*4882a593Smuzhiyun * @xdp: xdp_buff used as input to the XDP program
532*4882a593Smuzhiyun * @xdp_prog: XDP program to run
533*4882a593Smuzhiyun *
534*4882a593Smuzhiyun * Returns any of ICE_XDP_{PASS, CONSUMED, TX, REDIR}
535*4882a593Smuzhiyun */
536*4882a593Smuzhiyun static int
ice_run_xdp(struct ice_ring * rx_ring,struct xdp_buff * xdp,struct bpf_prog * xdp_prog)537*4882a593Smuzhiyun ice_run_xdp(struct ice_ring *rx_ring, struct xdp_buff *xdp,
538*4882a593Smuzhiyun struct bpf_prog *xdp_prog)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun struct ice_ring *xdp_ring;
541*4882a593Smuzhiyun int err, result;
542*4882a593Smuzhiyun u32 act;
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun act = bpf_prog_run_xdp(xdp_prog, xdp);
545*4882a593Smuzhiyun switch (act) {
546*4882a593Smuzhiyun case XDP_PASS:
547*4882a593Smuzhiyun return ICE_XDP_PASS;
548*4882a593Smuzhiyun case XDP_TX:
549*4882a593Smuzhiyun xdp_ring = rx_ring->vsi->xdp_rings[smp_processor_id()];
550*4882a593Smuzhiyun result = ice_xmit_xdp_buff(xdp, xdp_ring);
551*4882a593Smuzhiyun if (result == ICE_XDP_CONSUMED)
552*4882a593Smuzhiyun goto out_failure;
553*4882a593Smuzhiyun return result;
554*4882a593Smuzhiyun case XDP_REDIRECT:
555*4882a593Smuzhiyun err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
556*4882a593Smuzhiyun if (err)
557*4882a593Smuzhiyun goto out_failure;
558*4882a593Smuzhiyun return ICE_XDP_REDIR;
559*4882a593Smuzhiyun default:
560*4882a593Smuzhiyun bpf_warn_invalid_xdp_action(act);
561*4882a593Smuzhiyun fallthrough;
562*4882a593Smuzhiyun case XDP_ABORTED:
563*4882a593Smuzhiyun out_failure:
564*4882a593Smuzhiyun trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
565*4882a593Smuzhiyun fallthrough;
566*4882a593Smuzhiyun case XDP_DROP:
567*4882a593Smuzhiyun return ICE_XDP_CONSUMED;
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun /**
572*4882a593Smuzhiyun * ice_xdp_xmit - submit packets to XDP ring for transmission
573*4882a593Smuzhiyun * @dev: netdev
574*4882a593Smuzhiyun * @n: number of XDP frames to be transmitted
575*4882a593Smuzhiyun * @frames: XDP frames to be transmitted
576*4882a593Smuzhiyun * @flags: transmit flags
577*4882a593Smuzhiyun *
578*4882a593Smuzhiyun * Returns number of frames successfully sent. Frames that fail are
579*4882a593Smuzhiyun * free'ed via XDP return API.
580*4882a593Smuzhiyun * For error cases, a negative errno code is returned and no-frames
581*4882a593Smuzhiyun * are transmitted (caller must handle freeing frames).
582*4882a593Smuzhiyun */
583*4882a593Smuzhiyun int
ice_xdp_xmit(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags)584*4882a593Smuzhiyun ice_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
585*4882a593Smuzhiyun u32 flags)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun struct ice_netdev_priv *np = netdev_priv(dev);
588*4882a593Smuzhiyun unsigned int queue_index = smp_processor_id();
589*4882a593Smuzhiyun struct ice_vsi *vsi = np->vsi;
590*4882a593Smuzhiyun struct ice_ring *xdp_ring;
591*4882a593Smuzhiyun int drops = 0, i;
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun if (test_bit(__ICE_DOWN, vsi->state))
594*4882a593Smuzhiyun return -ENETDOWN;
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun if (!ice_is_xdp_ena_vsi(vsi) || queue_index >= vsi->num_xdp_txq)
597*4882a593Smuzhiyun return -ENXIO;
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
600*4882a593Smuzhiyun return -EINVAL;
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun xdp_ring = vsi->xdp_rings[queue_index];
603*4882a593Smuzhiyun for (i = 0; i < n; i++) {
604*4882a593Smuzhiyun struct xdp_frame *xdpf = frames[i];
605*4882a593Smuzhiyun int err;
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun err = ice_xmit_xdp_ring(xdpf->data, xdpf->len, xdp_ring);
608*4882a593Smuzhiyun if (err != ICE_XDP_TX) {
609*4882a593Smuzhiyun xdp_return_frame_rx_napi(xdpf);
610*4882a593Smuzhiyun drops++;
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun if (unlikely(flags & XDP_XMIT_FLUSH))
615*4882a593Smuzhiyun ice_xdp_ring_update_tail(xdp_ring);
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun return n - drops;
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun /**
621*4882a593Smuzhiyun * ice_alloc_mapped_page - recycle or make a new page
622*4882a593Smuzhiyun * @rx_ring: ring to use
623*4882a593Smuzhiyun * @bi: rx_buf struct to modify
624*4882a593Smuzhiyun *
625*4882a593Smuzhiyun * Returns true if the page was successfully allocated or
626*4882a593Smuzhiyun * reused.
627*4882a593Smuzhiyun */
628*4882a593Smuzhiyun static bool
ice_alloc_mapped_page(struct ice_ring * rx_ring,struct ice_rx_buf * bi)629*4882a593Smuzhiyun ice_alloc_mapped_page(struct ice_ring *rx_ring, struct ice_rx_buf *bi)
630*4882a593Smuzhiyun {
631*4882a593Smuzhiyun struct page *page = bi->page;
632*4882a593Smuzhiyun dma_addr_t dma;
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun /* since we are recycling buffers we should seldom need to alloc */
635*4882a593Smuzhiyun if (likely(page))
636*4882a593Smuzhiyun return true;
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun /* alloc new page for storage */
639*4882a593Smuzhiyun page = dev_alloc_pages(ice_rx_pg_order(rx_ring));
640*4882a593Smuzhiyun if (unlikely(!page)) {
641*4882a593Smuzhiyun rx_ring->rx_stats.alloc_page_failed++;
642*4882a593Smuzhiyun return false;
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun /* map page for use */
646*4882a593Smuzhiyun dma = dma_map_page_attrs(rx_ring->dev, page, 0, ice_rx_pg_size(rx_ring),
647*4882a593Smuzhiyun DMA_FROM_DEVICE, ICE_RX_DMA_ATTR);
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun /* if mapping failed free memory back to system since
650*4882a593Smuzhiyun * there isn't much point in holding memory we can't use
651*4882a593Smuzhiyun */
652*4882a593Smuzhiyun if (dma_mapping_error(rx_ring->dev, dma)) {
653*4882a593Smuzhiyun __free_pages(page, ice_rx_pg_order(rx_ring));
654*4882a593Smuzhiyun rx_ring->rx_stats.alloc_page_failed++;
655*4882a593Smuzhiyun return false;
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun bi->dma = dma;
659*4882a593Smuzhiyun bi->page = page;
660*4882a593Smuzhiyun bi->page_offset = ice_rx_offset(rx_ring);
661*4882a593Smuzhiyun page_ref_add(page, USHRT_MAX - 1);
662*4882a593Smuzhiyun bi->pagecnt_bias = USHRT_MAX;
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun return true;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun /**
668*4882a593Smuzhiyun * ice_alloc_rx_bufs - Replace used receive buffers
669*4882a593Smuzhiyun * @rx_ring: ring to place buffers on
670*4882a593Smuzhiyun * @cleaned_count: number of buffers to replace
671*4882a593Smuzhiyun *
672*4882a593Smuzhiyun * Returns false if all allocations were successful, true if any fail. Returning
673*4882a593Smuzhiyun * true signals to the caller that we didn't replace cleaned_count buffers and
674*4882a593Smuzhiyun * there is more work to do.
675*4882a593Smuzhiyun *
676*4882a593Smuzhiyun * First, try to clean "cleaned_count" Rx buffers. Then refill the cleaned Rx
677*4882a593Smuzhiyun * buffers. Then bump tail at most one time. Grouping like this lets us avoid
678*4882a593Smuzhiyun * multiple tail writes per call.
679*4882a593Smuzhiyun */
ice_alloc_rx_bufs(struct ice_ring * rx_ring,u16 cleaned_count)680*4882a593Smuzhiyun bool ice_alloc_rx_bufs(struct ice_ring *rx_ring, u16 cleaned_count)
681*4882a593Smuzhiyun {
682*4882a593Smuzhiyun union ice_32b_rx_flex_desc *rx_desc;
683*4882a593Smuzhiyun u16 ntu = rx_ring->next_to_use;
684*4882a593Smuzhiyun struct ice_rx_buf *bi;
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun /* do nothing if no valid netdev defined */
687*4882a593Smuzhiyun if ((!rx_ring->netdev && rx_ring->vsi->type != ICE_VSI_CTRL) ||
688*4882a593Smuzhiyun !cleaned_count)
689*4882a593Smuzhiyun return false;
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun /* get the Rx descriptor and buffer based on next_to_use */
692*4882a593Smuzhiyun rx_desc = ICE_RX_DESC(rx_ring, ntu);
693*4882a593Smuzhiyun bi = &rx_ring->rx_buf[ntu];
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun do {
696*4882a593Smuzhiyun /* if we fail here, we have work remaining */
697*4882a593Smuzhiyun if (!ice_alloc_mapped_page(rx_ring, bi))
698*4882a593Smuzhiyun break;
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun /* sync the buffer for use by the device */
701*4882a593Smuzhiyun dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
702*4882a593Smuzhiyun bi->page_offset,
703*4882a593Smuzhiyun rx_ring->rx_buf_len,
704*4882a593Smuzhiyun DMA_FROM_DEVICE);
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun /* Refresh the desc even if buffer_addrs didn't change
707*4882a593Smuzhiyun * because each write-back erases this info.
708*4882a593Smuzhiyun */
709*4882a593Smuzhiyun rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun rx_desc++;
712*4882a593Smuzhiyun bi++;
713*4882a593Smuzhiyun ntu++;
714*4882a593Smuzhiyun if (unlikely(ntu == rx_ring->count)) {
715*4882a593Smuzhiyun rx_desc = ICE_RX_DESC(rx_ring, 0);
716*4882a593Smuzhiyun bi = rx_ring->rx_buf;
717*4882a593Smuzhiyun ntu = 0;
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun /* clear the status bits for the next_to_use descriptor */
721*4882a593Smuzhiyun rx_desc->wb.status_error0 = 0;
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun cleaned_count--;
724*4882a593Smuzhiyun } while (cleaned_count);
725*4882a593Smuzhiyun
726*4882a593Smuzhiyun if (rx_ring->next_to_use != ntu)
727*4882a593Smuzhiyun ice_release_rx_desc(rx_ring, ntu);
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun return !!cleaned_count;
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun /**
733*4882a593Smuzhiyun * ice_page_is_reserved - check if reuse is possible
734*4882a593Smuzhiyun * @page: page struct to check
735*4882a593Smuzhiyun */
ice_page_is_reserved(struct page * page)736*4882a593Smuzhiyun static bool ice_page_is_reserved(struct page *page)
737*4882a593Smuzhiyun {
738*4882a593Smuzhiyun return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
739*4882a593Smuzhiyun }
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun /**
742*4882a593Smuzhiyun * ice_rx_buf_adjust_pg_offset - Prepare Rx buffer for reuse
743*4882a593Smuzhiyun * @rx_buf: Rx buffer to adjust
744*4882a593Smuzhiyun * @size: Size of adjustment
745*4882a593Smuzhiyun *
746*4882a593Smuzhiyun * Update the offset within page so that Rx buf will be ready to be reused.
747*4882a593Smuzhiyun * For systems with PAGE_SIZE < 8192 this function will flip the page offset
748*4882a593Smuzhiyun * so the second half of page assigned to Rx buffer will be used, otherwise
749*4882a593Smuzhiyun * the offset is moved by "size" bytes
750*4882a593Smuzhiyun */
751*4882a593Smuzhiyun static void
ice_rx_buf_adjust_pg_offset(struct ice_rx_buf * rx_buf,unsigned int size)752*4882a593Smuzhiyun ice_rx_buf_adjust_pg_offset(struct ice_rx_buf *rx_buf, unsigned int size)
753*4882a593Smuzhiyun {
754*4882a593Smuzhiyun #if (PAGE_SIZE < 8192)
755*4882a593Smuzhiyun /* flip page offset to other buffer */
756*4882a593Smuzhiyun rx_buf->page_offset ^= size;
757*4882a593Smuzhiyun #else
758*4882a593Smuzhiyun /* move offset up to the next cache line */
759*4882a593Smuzhiyun rx_buf->page_offset += size;
760*4882a593Smuzhiyun #endif
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun /**
764*4882a593Smuzhiyun * ice_can_reuse_rx_page - Determine if page can be reused for another Rx
765*4882a593Smuzhiyun * @rx_buf: buffer containing the page
766*4882a593Smuzhiyun * @rx_buf_pgcnt: rx_buf page refcount pre xdp_do_redirect() call
767*4882a593Smuzhiyun *
768*4882a593Smuzhiyun * If page is reusable, we have a green light for calling ice_reuse_rx_page,
769*4882a593Smuzhiyun * which will assign the current buffer to the buffer that next_to_alloc is
770*4882a593Smuzhiyun * pointing to; otherwise, the DMA mapping needs to be destroyed and
771*4882a593Smuzhiyun * page freed
772*4882a593Smuzhiyun */
773*4882a593Smuzhiyun static bool
ice_can_reuse_rx_page(struct ice_rx_buf * rx_buf,int rx_buf_pgcnt)774*4882a593Smuzhiyun ice_can_reuse_rx_page(struct ice_rx_buf *rx_buf, int rx_buf_pgcnt)
775*4882a593Smuzhiyun {
776*4882a593Smuzhiyun unsigned int pagecnt_bias = rx_buf->pagecnt_bias;
777*4882a593Smuzhiyun struct page *page = rx_buf->page;
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun /* avoid re-using remote pages */
780*4882a593Smuzhiyun if (unlikely(ice_page_is_reserved(page)))
781*4882a593Smuzhiyun return false;
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun #if (PAGE_SIZE < 8192)
784*4882a593Smuzhiyun /* if we are only owner of page we can reuse it */
785*4882a593Smuzhiyun if (unlikely((rx_buf_pgcnt - pagecnt_bias) > 1))
786*4882a593Smuzhiyun return false;
787*4882a593Smuzhiyun #else
788*4882a593Smuzhiyun #define ICE_LAST_OFFSET \
789*4882a593Smuzhiyun (SKB_WITH_OVERHEAD(PAGE_SIZE) - ICE_RXBUF_2048)
790*4882a593Smuzhiyun if (rx_buf->page_offset > ICE_LAST_OFFSET)
791*4882a593Smuzhiyun return false;
792*4882a593Smuzhiyun #endif /* PAGE_SIZE < 8192) */
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun /* If we have drained the page fragment pool we need to update
795*4882a593Smuzhiyun * the pagecnt_bias and page count so that we fully restock the
796*4882a593Smuzhiyun * number of references the driver holds.
797*4882a593Smuzhiyun */
798*4882a593Smuzhiyun if (unlikely(pagecnt_bias == 1)) {
799*4882a593Smuzhiyun page_ref_add(page, USHRT_MAX - 1);
800*4882a593Smuzhiyun rx_buf->pagecnt_bias = USHRT_MAX;
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun return true;
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun
806*4882a593Smuzhiyun /**
807*4882a593Smuzhiyun * ice_add_rx_frag - Add contents of Rx buffer to sk_buff as a frag
808*4882a593Smuzhiyun * @rx_ring: Rx descriptor ring to transact packets on
809*4882a593Smuzhiyun * @rx_buf: buffer containing page to add
810*4882a593Smuzhiyun * @skb: sk_buff to place the data into
811*4882a593Smuzhiyun * @size: packet length from rx_desc
812*4882a593Smuzhiyun *
813*4882a593Smuzhiyun * This function will add the data contained in rx_buf->page to the skb.
814*4882a593Smuzhiyun * It will just attach the page as a frag to the skb.
815*4882a593Smuzhiyun * The function will then update the page offset.
816*4882a593Smuzhiyun */
817*4882a593Smuzhiyun static void
ice_add_rx_frag(struct ice_ring * rx_ring,struct ice_rx_buf * rx_buf,struct sk_buff * skb,unsigned int size)818*4882a593Smuzhiyun ice_add_rx_frag(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf,
819*4882a593Smuzhiyun struct sk_buff *skb, unsigned int size)
820*4882a593Smuzhiyun {
821*4882a593Smuzhiyun #if (PAGE_SIZE >= 8192)
822*4882a593Smuzhiyun unsigned int truesize = SKB_DATA_ALIGN(size + ice_rx_offset(rx_ring));
823*4882a593Smuzhiyun #else
824*4882a593Smuzhiyun unsigned int truesize = ice_rx_pg_size(rx_ring) / 2;
825*4882a593Smuzhiyun #endif
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun if (!size)
828*4882a593Smuzhiyun return;
829*4882a593Smuzhiyun skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buf->page,
830*4882a593Smuzhiyun rx_buf->page_offset, size, truesize);
831*4882a593Smuzhiyun
832*4882a593Smuzhiyun /* page is being used so we must update the page offset */
833*4882a593Smuzhiyun ice_rx_buf_adjust_pg_offset(rx_buf, truesize);
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun /**
837*4882a593Smuzhiyun * ice_reuse_rx_page - page flip buffer and store it back on the ring
838*4882a593Smuzhiyun * @rx_ring: Rx descriptor ring to store buffers on
839*4882a593Smuzhiyun * @old_buf: donor buffer to have page reused
840*4882a593Smuzhiyun *
841*4882a593Smuzhiyun * Synchronizes page for reuse by the adapter
842*4882a593Smuzhiyun */
843*4882a593Smuzhiyun static void
ice_reuse_rx_page(struct ice_ring * rx_ring,struct ice_rx_buf * old_buf)844*4882a593Smuzhiyun ice_reuse_rx_page(struct ice_ring *rx_ring, struct ice_rx_buf *old_buf)
845*4882a593Smuzhiyun {
846*4882a593Smuzhiyun u16 nta = rx_ring->next_to_alloc;
847*4882a593Smuzhiyun struct ice_rx_buf *new_buf;
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun new_buf = &rx_ring->rx_buf[nta];
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun /* update, and store next to alloc */
852*4882a593Smuzhiyun nta++;
853*4882a593Smuzhiyun rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun /* Transfer page from old buffer to new buffer.
856*4882a593Smuzhiyun * Move each member individually to avoid possible store
857*4882a593Smuzhiyun * forwarding stalls and unnecessary copy of skb.
858*4882a593Smuzhiyun */
859*4882a593Smuzhiyun new_buf->dma = old_buf->dma;
860*4882a593Smuzhiyun new_buf->page = old_buf->page;
861*4882a593Smuzhiyun new_buf->page_offset = old_buf->page_offset;
862*4882a593Smuzhiyun new_buf->pagecnt_bias = old_buf->pagecnt_bias;
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun /**
866*4882a593Smuzhiyun * ice_get_rx_buf - Fetch Rx buffer and synchronize data for use
867*4882a593Smuzhiyun * @rx_ring: Rx descriptor ring to transact packets on
868*4882a593Smuzhiyun * @skb: skb to be used
869*4882a593Smuzhiyun * @size: size of buffer to add to skb
870*4882a593Smuzhiyun * @rx_buf_pgcnt: rx_buf page refcount
871*4882a593Smuzhiyun *
872*4882a593Smuzhiyun * This function will pull an Rx buffer from the ring and synchronize it
873*4882a593Smuzhiyun * for use by the CPU.
874*4882a593Smuzhiyun */
875*4882a593Smuzhiyun static struct ice_rx_buf *
ice_get_rx_buf(struct ice_ring * rx_ring,struct sk_buff ** skb,const unsigned int size,int * rx_buf_pgcnt)876*4882a593Smuzhiyun ice_get_rx_buf(struct ice_ring *rx_ring, struct sk_buff **skb,
877*4882a593Smuzhiyun const unsigned int size, int *rx_buf_pgcnt)
878*4882a593Smuzhiyun {
879*4882a593Smuzhiyun struct ice_rx_buf *rx_buf;
880*4882a593Smuzhiyun
881*4882a593Smuzhiyun rx_buf = &rx_ring->rx_buf[rx_ring->next_to_clean];
882*4882a593Smuzhiyun *rx_buf_pgcnt =
883*4882a593Smuzhiyun #if (PAGE_SIZE < 8192)
884*4882a593Smuzhiyun page_count(rx_buf->page);
885*4882a593Smuzhiyun #else
886*4882a593Smuzhiyun 0;
887*4882a593Smuzhiyun #endif
888*4882a593Smuzhiyun prefetchw(rx_buf->page);
889*4882a593Smuzhiyun *skb = rx_buf->skb;
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun if (!size)
892*4882a593Smuzhiyun return rx_buf;
893*4882a593Smuzhiyun /* we are reusing so sync this buffer for CPU use */
894*4882a593Smuzhiyun dma_sync_single_range_for_cpu(rx_ring->dev, rx_buf->dma,
895*4882a593Smuzhiyun rx_buf->page_offset, size,
896*4882a593Smuzhiyun DMA_FROM_DEVICE);
897*4882a593Smuzhiyun
898*4882a593Smuzhiyun /* We have pulled a buffer for use, so decrement pagecnt_bias */
899*4882a593Smuzhiyun rx_buf->pagecnt_bias--;
900*4882a593Smuzhiyun
901*4882a593Smuzhiyun return rx_buf;
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun
904*4882a593Smuzhiyun /**
905*4882a593Smuzhiyun * ice_build_skb - Build skb around an existing buffer
906*4882a593Smuzhiyun * @rx_ring: Rx descriptor ring to transact packets on
907*4882a593Smuzhiyun * @rx_buf: Rx buffer to pull data from
908*4882a593Smuzhiyun * @xdp: xdp_buff pointing to the data
909*4882a593Smuzhiyun *
910*4882a593Smuzhiyun * This function builds an skb around an existing Rx buffer, taking care
911*4882a593Smuzhiyun * to set up the skb correctly and avoid any memcpy overhead.
912*4882a593Smuzhiyun */
913*4882a593Smuzhiyun static struct sk_buff *
ice_build_skb(struct ice_ring * rx_ring,struct ice_rx_buf * rx_buf,struct xdp_buff * xdp)914*4882a593Smuzhiyun ice_build_skb(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf,
915*4882a593Smuzhiyun struct xdp_buff *xdp)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun u8 metasize = xdp->data - xdp->data_meta;
918*4882a593Smuzhiyun #if (PAGE_SIZE < 8192)
919*4882a593Smuzhiyun unsigned int truesize = ice_rx_pg_size(rx_ring) / 2;
920*4882a593Smuzhiyun #else
921*4882a593Smuzhiyun unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
922*4882a593Smuzhiyun SKB_DATA_ALIGN(xdp->data_end -
923*4882a593Smuzhiyun xdp->data_hard_start);
924*4882a593Smuzhiyun #endif
925*4882a593Smuzhiyun struct sk_buff *skb;
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun /* Prefetch first cache line of first page. If xdp->data_meta
928*4882a593Smuzhiyun * is unused, this points exactly as xdp->data, otherwise we
929*4882a593Smuzhiyun * likely have a consumer accessing first few bytes of meta
930*4882a593Smuzhiyun * data, and then actual data.
931*4882a593Smuzhiyun */
932*4882a593Smuzhiyun net_prefetch(xdp->data_meta);
933*4882a593Smuzhiyun /* build an skb around the page buffer */
934*4882a593Smuzhiyun skb = build_skb(xdp->data_hard_start, truesize);
935*4882a593Smuzhiyun if (unlikely(!skb))
936*4882a593Smuzhiyun return NULL;
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun /* must to record Rx queue, otherwise OS features such as
939*4882a593Smuzhiyun * symmetric queue won't work
940*4882a593Smuzhiyun */
941*4882a593Smuzhiyun skb_record_rx_queue(skb, rx_ring->q_index);
942*4882a593Smuzhiyun
943*4882a593Smuzhiyun /* update pointers within the skb to store the data */
944*4882a593Smuzhiyun skb_reserve(skb, xdp->data - xdp->data_hard_start);
945*4882a593Smuzhiyun __skb_put(skb, xdp->data_end - xdp->data);
946*4882a593Smuzhiyun if (metasize)
947*4882a593Smuzhiyun skb_metadata_set(skb, metasize);
948*4882a593Smuzhiyun
949*4882a593Smuzhiyun /* buffer is used by skb, update page_offset */
950*4882a593Smuzhiyun ice_rx_buf_adjust_pg_offset(rx_buf, truesize);
951*4882a593Smuzhiyun
952*4882a593Smuzhiyun return skb;
953*4882a593Smuzhiyun }
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun /**
956*4882a593Smuzhiyun * ice_construct_skb - Allocate skb and populate it
957*4882a593Smuzhiyun * @rx_ring: Rx descriptor ring to transact packets on
958*4882a593Smuzhiyun * @rx_buf: Rx buffer to pull data from
959*4882a593Smuzhiyun * @xdp: xdp_buff pointing to the data
960*4882a593Smuzhiyun *
961*4882a593Smuzhiyun * This function allocates an skb. It then populates it with the page
962*4882a593Smuzhiyun * data from the current receive descriptor, taking care to set up the
963*4882a593Smuzhiyun * skb correctly.
964*4882a593Smuzhiyun */
965*4882a593Smuzhiyun static struct sk_buff *
ice_construct_skb(struct ice_ring * rx_ring,struct ice_rx_buf * rx_buf,struct xdp_buff * xdp)966*4882a593Smuzhiyun ice_construct_skb(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf,
967*4882a593Smuzhiyun struct xdp_buff *xdp)
968*4882a593Smuzhiyun {
969*4882a593Smuzhiyun unsigned int size = xdp->data_end - xdp->data;
970*4882a593Smuzhiyun unsigned int headlen;
971*4882a593Smuzhiyun struct sk_buff *skb;
972*4882a593Smuzhiyun
973*4882a593Smuzhiyun /* prefetch first cache line of first page */
974*4882a593Smuzhiyun net_prefetch(xdp->data);
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun /* allocate a skb to store the frags */
977*4882a593Smuzhiyun skb = __napi_alloc_skb(&rx_ring->q_vector->napi, ICE_RX_HDR_SIZE,
978*4882a593Smuzhiyun GFP_ATOMIC | __GFP_NOWARN);
979*4882a593Smuzhiyun if (unlikely(!skb))
980*4882a593Smuzhiyun return NULL;
981*4882a593Smuzhiyun
982*4882a593Smuzhiyun skb_record_rx_queue(skb, rx_ring->q_index);
983*4882a593Smuzhiyun /* Determine available headroom for copy */
984*4882a593Smuzhiyun headlen = size;
985*4882a593Smuzhiyun if (headlen > ICE_RX_HDR_SIZE)
986*4882a593Smuzhiyun headlen = eth_get_headlen(skb->dev, xdp->data, ICE_RX_HDR_SIZE);
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun /* align pull length to size of long to optimize memcpy performance */
989*4882a593Smuzhiyun memcpy(__skb_put(skb, headlen), xdp->data, ALIGN(headlen,
990*4882a593Smuzhiyun sizeof(long)));
991*4882a593Smuzhiyun
992*4882a593Smuzhiyun /* if we exhaust the linear part then add what is left as a frag */
993*4882a593Smuzhiyun size -= headlen;
994*4882a593Smuzhiyun if (size) {
995*4882a593Smuzhiyun #if (PAGE_SIZE >= 8192)
996*4882a593Smuzhiyun unsigned int truesize = SKB_DATA_ALIGN(size);
997*4882a593Smuzhiyun #else
998*4882a593Smuzhiyun unsigned int truesize = ice_rx_pg_size(rx_ring) / 2;
999*4882a593Smuzhiyun #endif
1000*4882a593Smuzhiyun skb_add_rx_frag(skb, 0, rx_buf->page,
1001*4882a593Smuzhiyun rx_buf->page_offset + headlen, size, truesize);
1002*4882a593Smuzhiyun /* buffer is used by skb, update page_offset */
1003*4882a593Smuzhiyun ice_rx_buf_adjust_pg_offset(rx_buf, truesize);
1004*4882a593Smuzhiyun } else {
1005*4882a593Smuzhiyun /* buffer is unused, reset bias back to rx_buf; data was copied
1006*4882a593Smuzhiyun * onto skb's linear part so there's no need for adjusting
1007*4882a593Smuzhiyun * page offset and we can reuse this buffer as-is
1008*4882a593Smuzhiyun */
1009*4882a593Smuzhiyun rx_buf->pagecnt_bias++;
1010*4882a593Smuzhiyun }
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun return skb;
1013*4882a593Smuzhiyun }
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun /**
1016*4882a593Smuzhiyun * ice_put_rx_buf - Clean up used buffer and either recycle or free
1017*4882a593Smuzhiyun * @rx_ring: Rx descriptor ring to transact packets on
1018*4882a593Smuzhiyun * @rx_buf: Rx buffer to pull data from
1019*4882a593Smuzhiyun * @rx_buf_pgcnt: Rx buffer page count pre xdp_do_redirect()
1020*4882a593Smuzhiyun *
1021*4882a593Smuzhiyun * This function will update next_to_clean and then clean up the contents
1022*4882a593Smuzhiyun * of the rx_buf. It will either recycle the buffer or unmap it and free
1023*4882a593Smuzhiyun * the associated resources.
1024*4882a593Smuzhiyun */
1025*4882a593Smuzhiyun static void
ice_put_rx_buf(struct ice_ring * rx_ring,struct ice_rx_buf * rx_buf,int rx_buf_pgcnt)1026*4882a593Smuzhiyun ice_put_rx_buf(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf,
1027*4882a593Smuzhiyun int rx_buf_pgcnt)
1028*4882a593Smuzhiyun {
1029*4882a593Smuzhiyun u16 ntc = rx_ring->next_to_clean + 1;
1030*4882a593Smuzhiyun
1031*4882a593Smuzhiyun /* fetch, update, and store next to clean */
1032*4882a593Smuzhiyun ntc = (ntc < rx_ring->count) ? ntc : 0;
1033*4882a593Smuzhiyun rx_ring->next_to_clean = ntc;
1034*4882a593Smuzhiyun
1035*4882a593Smuzhiyun if (!rx_buf)
1036*4882a593Smuzhiyun return;
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun if (ice_can_reuse_rx_page(rx_buf, rx_buf_pgcnt)) {
1039*4882a593Smuzhiyun /* hand second half of page back to the ring */
1040*4882a593Smuzhiyun ice_reuse_rx_page(rx_ring, rx_buf);
1041*4882a593Smuzhiyun } else {
1042*4882a593Smuzhiyun /* we are not reusing the buffer so unmap it */
1043*4882a593Smuzhiyun dma_unmap_page_attrs(rx_ring->dev, rx_buf->dma,
1044*4882a593Smuzhiyun ice_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
1045*4882a593Smuzhiyun ICE_RX_DMA_ATTR);
1046*4882a593Smuzhiyun __page_frag_cache_drain(rx_buf->page, rx_buf->pagecnt_bias);
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun
1049*4882a593Smuzhiyun /* clear contents of buffer_info */
1050*4882a593Smuzhiyun rx_buf->page = NULL;
1051*4882a593Smuzhiyun rx_buf->skb = NULL;
1052*4882a593Smuzhiyun }
1053*4882a593Smuzhiyun
1054*4882a593Smuzhiyun /**
1055*4882a593Smuzhiyun * ice_is_non_eop - process handling of non-EOP buffers
1056*4882a593Smuzhiyun * @rx_ring: Rx ring being processed
1057*4882a593Smuzhiyun * @rx_desc: Rx descriptor for current buffer
1058*4882a593Smuzhiyun * @skb: Current socket buffer containing buffer in progress
1059*4882a593Smuzhiyun *
1060*4882a593Smuzhiyun * If the buffer is an EOP buffer, this function exits returning false,
1061*4882a593Smuzhiyun * otherwise return true indicating that this is in fact a non-EOP buffer.
1062*4882a593Smuzhiyun */
1063*4882a593Smuzhiyun static bool
ice_is_non_eop(struct ice_ring * rx_ring,union ice_32b_rx_flex_desc * rx_desc,struct sk_buff * skb)1064*4882a593Smuzhiyun ice_is_non_eop(struct ice_ring *rx_ring, union ice_32b_rx_flex_desc *rx_desc,
1065*4882a593Smuzhiyun struct sk_buff *skb)
1066*4882a593Smuzhiyun {
1067*4882a593Smuzhiyun /* if we are the last buffer then there is nothing else to do */
1068*4882a593Smuzhiyun #define ICE_RXD_EOF BIT(ICE_RX_FLEX_DESC_STATUS0_EOF_S)
1069*4882a593Smuzhiyun if (likely(ice_test_staterr(rx_desc, ICE_RXD_EOF)))
1070*4882a593Smuzhiyun return false;
1071*4882a593Smuzhiyun
1072*4882a593Smuzhiyun /* place skb in next buffer to be received */
1073*4882a593Smuzhiyun rx_ring->rx_buf[rx_ring->next_to_clean].skb = skb;
1074*4882a593Smuzhiyun rx_ring->rx_stats.non_eop_descs++;
1075*4882a593Smuzhiyun
1076*4882a593Smuzhiyun return true;
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun /**
1080*4882a593Smuzhiyun * ice_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
1081*4882a593Smuzhiyun * @rx_ring: Rx descriptor ring to transact packets on
1082*4882a593Smuzhiyun * @budget: Total limit on number of packets to process
1083*4882a593Smuzhiyun *
1084*4882a593Smuzhiyun * This function provides a "bounce buffer" approach to Rx interrupt
1085*4882a593Smuzhiyun * processing. The advantage to this is that on systems that have
1086*4882a593Smuzhiyun * expensive overhead for IOMMU access this provides a means of avoiding
1087*4882a593Smuzhiyun * it by maintaining the mapping of the page to the system.
1088*4882a593Smuzhiyun *
1089*4882a593Smuzhiyun * Returns amount of work completed
1090*4882a593Smuzhiyun */
ice_clean_rx_irq(struct ice_ring * rx_ring,int budget)1091*4882a593Smuzhiyun int ice_clean_rx_irq(struct ice_ring *rx_ring, int budget)
1092*4882a593Smuzhiyun {
1093*4882a593Smuzhiyun unsigned int total_rx_bytes = 0, total_rx_pkts = 0;
1094*4882a593Smuzhiyun u16 cleaned_count = ICE_DESC_UNUSED(rx_ring);
1095*4882a593Smuzhiyun unsigned int xdp_res, xdp_xmit = 0;
1096*4882a593Smuzhiyun struct bpf_prog *xdp_prog = NULL;
1097*4882a593Smuzhiyun struct xdp_buff xdp;
1098*4882a593Smuzhiyun bool failure;
1099*4882a593Smuzhiyun
1100*4882a593Smuzhiyun xdp.rxq = &rx_ring->xdp_rxq;
1101*4882a593Smuzhiyun /* Frame size depend on rx_ring setup when PAGE_SIZE=4K */
1102*4882a593Smuzhiyun #if (PAGE_SIZE < 8192)
1103*4882a593Smuzhiyun xdp.frame_sz = ice_rx_frame_truesize(rx_ring, 0);
1104*4882a593Smuzhiyun #endif
1105*4882a593Smuzhiyun
1106*4882a593Smuzhiyun /* start the loop to process Rx packets bounded by 'budget' */
1107*4882a593Smuzhiyun while (likely(total_rx_pkts < (unsigned int)budget)) {
1108*4882a593Smuzhiyun union ice_32b_rx_flex_desc *rx_desc;
1109*4882a593Smuzhiyun struct ice_rx_buf *rx_buf;
1110*4882a593Smuzhiyun struct sk_buff *skb;
1111*4882a593Smuzhiyun unsigned int size;
1112*4882a593Smuzhiyun u16 stat_err_bits;
1113*4882a593Smuzhiyun int rx_buf_pgcnt;
1114*4882a593Smuzhiyun u16 vlan_tag = 0;
1115*4882a593Smuzhiyun u8 rx_ptype;
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun /* get the Rx desc from Rx ring based on 'next_to_clean' */
1118*4882a593Smuzhiyun rx_desc = ICE_RX_DESC(rx_ring, rx_ring->next_to_clean);
1119*4882a593Smuzhiyun
1120*4882a593Smuzhiyun /* status_error_len will always be zero for unused descriptors
1121*4882a593Smuzhiyun * because it's cleared in cleanup, and overlaps with hdr_addr
1122*4882a593Smuzhiyun * which is always zero because packet split isn't used, if the
1123*4882a593Smuzhiyun * hardware wrote DD then it will be non-zero
1124*4882a593Smuzhiyun */
1125*4882a593Smuzhiyun stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S);
1126*4882a593Smuzhiyun if (!ice_test_staterr(rx_desc, stat_err_bits))
1127*4882a593Smuzhiyun break;
1128*4882a593Smuzhiyun
1129*4882a593Smuzhiyun /* This memory barrier is needed to keep us from reading
1130*4882a593Smuzhiyun * any other fields out of the rx_desc until we know the
1131*4882a593Smuzhiyun * DD bit is set.
1132*4882a593Smuzhiyun */
1133*4882a593Smuzhiyun dma_rmb();
1134*4882a593Smuzhiyun
1135*4882a593Smuzhiyun if (rx_desc->wb.rxdid == FDIR_DESC_RXDID || !rx_ring->netdev) {
1136*4882a593Smuzhiyun ice_put_rx_buf(rx_ring, NULL, 0);
1137*4882a593Smuzhiyun cleaned_count++;
1138*4882a593Smuzhiyun continue;
1139*4882a593Smuzhiyun }
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun size = le16_to_cpu(rx_desc->wb.pkt_len) &
1142*4882a593Smuzhiyun ICE_RX_FLX_DESC_PKT_LEN_M;
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun /* retrieve a buffer from the ring */
1145*4882a593Smuzhiyun rx_buf = ice_get_rx_buf(rx_ring, &skb, size, &rx_buf_pgcnt);
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun if (!size) {
1148*4882a593Smuzhiyun xdp.data = NULL;
1149*4882a593Smuzhiyun xdp.data_end = NULL;
1150*4882a593Smuzhiyun xdp.data_hard_start = NULL;
1151*4882a593Smuzhiyun xdp.data_meta = NULL;
1152*4882a593Smuzhiyun goto construct_skb;
1153*4882a593Smuzhiyun }
1154*4882a593Smuzhiyun
1155*4882a593Smuzhiyun xdp.data = page_address(rx_buf->page) + rx_buf->page_offset;
1156*4882a593Smuzhiyun xdp.data_hard_start = xdp.data - ice_rx_offset(rx_ring);
1157*4882a593Smuzhiyun xdp.data_meta = xdp.data;
1158*4882a593Smuzhiyun xdp.data_end = xdp.data + size;
1159*4882a593Smuzhiyun #if (PAGE_SIZE > 4096)
1160*4882a593Smuzhiyun /* At larger PAGE_SIZE, frame_sz depend on len size */
1161*4882a593Smuzhiyun xdp.frame_sz = ice_rx_frame_truesize(rx_ring, size);
1162*4882a593Smuzhiyun #endif
1163*4882a593Smuzhiyun
1164*4882a593Smuzhiyun rcu_read_lock();
1165*4882a593Smuzhiyun xdp_prog = READ_ONCE(rx_ring->xdp_prog);
1166*4882a593Smuzhiyun if (!xdp_prog) {
1167*4882a593Smuzhiyun rcu_read_unlock();
1168*4882a593Smuzhiyun goto construct_skb;
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun
1171*4882a593Smuzhiyun xdp_res = ice_run_xdp(rx_ring, &xdp, xdp_prog);
1172*4882a593Smuzhiyun rcu_read_unlock();
1173*4882a593Smuzhiyun if (!xdp_res)
1174*4882a593Smuzhiyun goto construct_skb;
1175*4882a593Smuzhiyun if (xdp_res & (ICE_XDP_TX | ICE_XDP_REDIR)) {
1176*4882a593Smuzhiyun xdp_xmit |= xdp_res;
1177*4882a593Smuzhiyun ice_rx_buf_adjust_pg_offset(rx_buf, xdp.frame_sz);
1178*4882a593Smuzhiyun } else {
1179*4882a593Smuzhiyun rx_buf->pagecnt_bias++;
1180*4882a593Smuzhiyun }
1181*4882a593Smuzhiyun total_rx_bytes += size;
1182*4882a593Smuzhiyun total_rx_pkts++;
1183*4882a593Smuzhiyun
1184*4882a593Smuzhiyun cleaned_count++;
1185*4882a593Smuzhiyun ice_put_rx_buf(rx_ring, rx_buf, rx_buf_pgcnt);
1186*4882a593Smuzhiyun continue;
1187*4882a593Smuzhiyun construct_skb:
1188*4882a593Smuzhiyun if (skb) {
1189*4882a593Smuzhiyun ice_add_rx_frag(rx_ring, rx_buf, skb, size);
1190*4882a593Smuzhiyun } else if (likely(xdp.data)) {
1191*4882a593Smuzhiyun if (ice_ring_uses_build_skb(rx_ring))
1192*4882a593Smuzhiyun skb = ice_build_skb(rx_ring, rx_buf, &xdp);
1193*4882a593Smuzhiyun else
1194*4882a593Smuzhiyun skb = ice_construct_skb(rx_ring, rx_buf, &xdp);
1195*4882a593Smuzhiyun }
1196*4882a593Smuzhiyun /* exit if we failed to retrieve a buffer */
1197*4882a593Smuzhiyun if (!skb) {
1198*4882a593Smuzhiyun rx_ring->rx_stats.alloc_buf_failed++;
1199*4882a593Smuzhiyun if (rx_buf)
1200*4882a593Smuzhiyun rx_buf->pagecnt_bias++;
1201*4882a593Smuzhiyun break;
1202*4882a593Smuzhiyun }
1203*4882a593Smuzhiyun
1204*4882a593Smuzhiyun ice_put_rx_buf(rx_ring, rx_buf, rx_buf_pgcnt);
1205*4882a593Smuzhiyun cleaned_count++;
1206*4882a593Smuzhiyun
1207*4882a593Smuzhiyun /* skip if it is NOP desc */
1208*4882a593Smuzhiyun if (ice_is_non_eop(rx_ring, rx_desc, skb))
1209*4882a593Smuzhiyun continue;
1210*4882a593Smuzhiyun
1211*4882a593Smuzhiyun stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_RXE_S);
1212*4882a593Smuzhiyun if (unlikely(ice_test_staterr(rx_desc, stat_err_bits))) {
1213*4882a593Smuzhiyun dev_kfree_skb_any(skb);
1214*4882a593Smuzhiyun continue;
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun
1217*4882a593Smuzhiyun stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_L2TAG1P_S);
1218*4882a593Smuzhiyun if (ice_test_staterr(rx_desc, stat_err_bits))
1219*4882a593Smuzhiyun vlan_tag = le16_to_cpu(rx_desc->wb.l2tag1);
1220*4882a593Smuzhiyun
1221*4882a593Smuzhiyun /* pad the skb if needed, to make a valid ethernet frame */
1222*4882a593Smuzhiyun if (eth_skb_pad(skb)) {
1223*4882a593Smuzhiyun skb = NULL;
1224*4882a593Smuzhiyun continue;
1225*4882a593Smuzhiyun }
1226*4882a593Smuzhiyun
1227*4882a593Smuzhiyun /* probably a little skewed due to removing CRC */
1228*4882a593Smuzhiyun total_rx_bytes += skb->len;
1229*4882a593Smuzhiyun
1230*4882a593Smuzhiyun /* populate checksum, VLAN, and protocol */
1231*4882a593Smuzhiyun rx_ptype = le16_to_cpu(rx_desc->wb.ptype_flex_flags0) &
1232*4882a593Smuzhiyun ICE_RX_FLEX_DESC_PTYPE_M;
1233*4882a593Smuzhiyun
1234*4882a593Smuzhiyun ice_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
1235*4882a593Smuzhiyun
1236*4882a593Smuzhiyun /* send completed skb up the stack */
1237*4882a593Smuzhiyun ice_receive_skb(rx_ring, skb, vlan_tag);
1238*4882a593Smuzhiyun
1239*4882a593Smuzhiyun /* update budget accounting */
1240*4882a593Smuzhiyun total_rx_pkts++;
1241*4882a593Smuzhiyun }
1242*4882a593Smuzhiyun
1243*4882a593Smuzhiyun /* return up to cleaned_count buffers to hardware */
1244*4882a593Smuzhiyun failure = ice_alloc_rx_bufs(rx_ring, cleaned_count);
1245*4882a593Smuzhiyun
1246*4882a593Smuzhiyun if (xdp_prog)
1247*4882a593Smuzhiyun ice_finalize_xdp_rx(rx_ring, xdp_xmit);
1248*4882a593Smuzhiyun
1249*4882a593Smuzhiyun ice_update_rx_ring_stats(rx_ring, total_rx_pkts, total_rx_bytes);
1250*4882a593Smuzhiyun
1251*4882a593Smuzhiyun /* guarantee a trip back through this routine if there was a failure */
1252*4882a593Smuzhiyun return failure ? budget : (int)total_rx_pkts;
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun /**
1256*4882a593Smuzhiyun * ice_adjust_itr_by_size_and_speed - Adjust ITR based on current traffic
1257*4882a593Smuzhiyun * @port_info: port_info structure containing the current link speed
1258*4882a593Smuzhiyun * @avg_pkt_size: average size of Tx or Rx packets based on clean routine
1259*4882a593Smuzhiyun * @itr: ITR value to update
1260*4882a593Smuzhiyun *
1261*4882a593Smuzhiyun * Calculate how big of an increment should be applied to the ITR value passed
1262*4882a593Smuzhiyun * in based on wmem_default, SKB overhead, ethernet overhead, and the current
1263*4882a593Smuzhiyun * link speed.
1264*4882a593Smuzhiyun *
1265*4882a593Smuzhiyun * The following is a calculation derived from:
1266*4882a593Smuzhiyun * wmem_default / (size + overhead) = desired_pkts_per_int
1267*4882a593Smuzhiyun * rate / bits_per_byte / (size + ethernet overhead) = pkt_rate
1268*4882a593Smuzhiyun * (desired_pkt_rate / pkt_rate) * usecs_per_sec = ITR value
1269*4882a593Smuzhiyun *
1270*4882a593Smuzhiyun * Assuming wmem_default is 212992 and overhead is 640 bytes per
1271*4882a593Smuzhiyun * packet, (256 skb, 64 headroom, 320 shared info), we can reduce the
1272*4882a593Smuzhiyun * formula down to:
1273*4882a593Smuzhiyun *
1274*4882a593Smuzhiyun * wmem_default * bits_per_byte * usecs_per_sec pkt_size + 24
1275*4882a593Smuzhiyun * ITR = -------------------------------------------- * --------------
1276*4882a593Smuzhiyun * rate pkt_size + 640
1277*4882a593Smuzhiyun */
1278*4882a593Smuzhiyun static unsigned int
ice_adjust_itr_by_size_and_speed(struct ice_port_info * port_info,unsigned int avg_pkt_size,unsigned int itr)1279*4882a593Smuzhiyun ice_adjust_itr_by_size_and_speed(struct ice_port_info *port_info,
1280*4882a593Smuzhiyun unsigned int avg_pkt_size,
1281*4882a593Smuzhiyun unsigned int itr)
1282*4882a593Smuzhiyun {
1283*4882a593Smuzhiyun switch (port_info->phy.link_info.link_speed) {
1284*4882a593Smuzhiyun case ICE_AQ_LINK_SPEED_100GB:
1285*4882a593Smuzhiyun itr += DIV_ROUND_UP(17 * (avg_pkt_size + 24),
1286*4882a593Smuzhiyun avg_pkt_size + 640);
1287*4882a593Smuzhiyun break;
1288*4882a593Smuzhiyun case ICE_AQ_LINK_SPEED_50GB:
1289*4882a593Smuzhiyun itr += DIV_ROUND_UP(34 * (avg_pkt_size + 24),
1290*4882a593Smuzhiyun avg_pkt_size + 640);
1291*4882a593Smuzhiyun break;
1292*4882a593Smuzhiyun case ICE_AQ_LINK_SPEED_40GB:
1293*4882a593Smuzhiyun itr += DIV_ROUND_UP(43 * (avg_pkt_size + 24),
1294*4882a593Smuzhiyun avg_pkt_size + 640);
1295*4882a593Smuzhiyun break;
1296*4882a593Smuzhiyun case ICE_AQ_LINK_SPEED_25GB:
1297*4882a593Smuzhiyun itr += DIV_ROUND_UP(68 * (avg_pkt_size + 24),
1298*4882a593Smuzhiyun avg_pkt_size + 640);
1299*4882a593Smuzhiyun break;
1300*4882a593Smuzhiyun case ICE_AQ_LINK_SPEED_20GB:
1301*4882a593Smuzhiyun itr += DIV_ROUND_UP(85 * (avg_pkt_size + 24),
1302*4882a593Smuzhiyun avg_pkt_size + 640);
1303*4882a593Smuzhiyun break;
1304*4882a593Smuzhiyun case ICE_AQ_LINK_SPEED_10GB:
1305*4882a593Smuzhiyun default:
1306*4882a593Smuzhiyun itr += DIV_ROUND_UP(170 * (avg_pkt_size + 24),
1307*4882a593Smuzhiyun avg_pkt_size + 640);
1308*4882a593Smuzhiyun break;
1309*4882a593Smuzhiyun }
1310*4882a593Smuzhiyun
1311*4882a593Smuzhiyun if ((itr & ICE_ITR_MASK) > ICE_ITR_ADAPTIVE_MAX_USECS) {
1312*4882a593Smuzhiyun itr &= ICE_ITR_ADAPTIVE_LATENCY;
1313*4882a593Smuzhiyun itr += ICE_ITR_ADAPTIVE_MAX_USECS;
1314*4882a593Smuzhiyun }
1315*4882a593Smuzhiyun
1316*4882a593Smuzhiyun return itr;
1317*4882a593Smuzhiyun }
1318*4882a593Smuzhiyun
1319*4882a593Smuzhiyun /**
1320*4882a593Smuzhiyun * ice_update_itr - update the adaptive ITR value based on statistics
1321*4882a593Smuzhiyun * @q_vector: structure containing interrupt and ring information
1322*4882a593Smuzhiyun * @rc: structure containing ring performance data
1323*4882a593Smuzhiyun *
1324*4882a593Smuzhiyun * Stores a new ITR value based on packets and byte
1325*4882a593Smuzhiyun * counts during the last interrupt. The advantage of per interrupt
1326*4882a593Smuzhiyun * computation is faster updates and more accurate ITR for the current
1327*4882a593Smuzhiyun * traffic pattern. Constants in this function were computed
1328*4882a593Smuzhiyun * based on theoretical maximum wire speed and thresholds were set based
1329*4882a593Smuzhiyun * on testing data as well as attempting to minimize response time
1330*4882a593Smuzhiyun * while increasing bulk throughput.
1331*4882a593Smuzhiyun */
1332*4882a593Smuzhiyun static void
ice_update_itr(struct ice_q_vector * q_vector,struct ice_ring_container * rc)1333*4882a593Smuzhiyun ice_update_itr(struct ice_q_vector *q_vector, struct ice_ring_container *rc)
1334*4882a593Smuzhiyun {
1335*4882a593Smuzhiyun unsigned long next_update = jiffies;
1336*4882a593Smuzhiyun unsigned int packets, bytes, itr;
1337*4882a593Smuzhiyun bool container_is_rx;
1338*4882a593Smuzhiyun
1339*4882a593Smuzhiyun if (!rc->ring || !ITR_IS_DYNAMIC(rc->itr_setting))
1340*4882a593Smuzhiyun return;
1341*4882a593Smuzhiyun
1342*4882a593Smuzhiyun /* If itr_countdown is set it means we programmed an ITR within
1343*4882a593Smuzhiyun * the last 4 interrupt cycles. This has a side effect of us
1344*4882a593Smuzhiyun * potentially firing an early interrupt. In order to work around
1345*4882a593Smuzhiyun * this we need to throw out any data received for a few
1346*4882a593Smuzhiyun * interrupts following the update.
1347*4882a593Smuzhiyun */
1348*4882a593Smuzhiyun if (q_vector->itr_countdown) {
1349*4882a593Smuzhiyun itr = rc->target_itr;
1350*4882a593Smuzhiyun goto clear_counts;
1351*4882a593Smuzhiyun }
1352*4882a593Smuzhiyun
1353*4882a593Smuzhiyun container_is_rx = (&q_vector->rx == rc);
1354*4882a593Smuzhiyun /* For Rx we want to push the delay up and default to low latency.
1355*4882a593Smuzhiyun * for Tx we want to pull the delay down and default to high latency.
1356*4882a593Smuzhiyun */
1357*4882a593Smuzhiyun itr = container_is_rx ?
1358*4882a593Smuzhiyun ICE_ITR_ADAPTIVE_MIN_USECS | ICE_ITR_ADAPTIVE_LATENCY :
1359*4882a593Smuzhiyun ICE_ITR_ADAPTIVE_MAX_USECS | ICE_ITR_ADAPTIVE_LATENCY;
1360*4882a593Smuzhiyun
1361*4882a593Smuzhiyun /* If we didn't update within up to 1 - 2 jiffies we can assume
1362*4882a593Smuzhiyun * that either packets are coming in so slow there hasn't been
1363*4882a593Smuzhiyun * any work, or that there is so much work that NAPI is dealing
1364*4882a593Smuzhiyun * with interrupt moderation and we don't need to do anything.
1365*4882a593Smuzhiyun */
1366*4882a593Smuzhiyun if (time_after(next_update, rc->next_update))
1367*4882a593Smuzhiyun goto clear_counts;
1368*4882a593Smuzhiyun
1369*4882a593Smuzhiyun prefetch(q_vector->vsi->port_info);
1370*4882a593Smuzhiyun
1371*4882a593Smuzhiyun packets = rc->total_pkts;
1372*4882a593Smuzhiyun bytes = rc->total_bytes;
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun if (container_is_rx) {
1375*4882a593Smuzhiyun /* If Rx there are 1 to 4 packets and bytes are less than
1376*4882a593Smuzhiyun * 9000 assume insufficient data to use bulk rate limiting
1377*4882a593Smuzhiyun * approach unless Tx is already in bulk rate limiting. We
1378*4882a593Smuzhiyun * are likely latency driven.
1379*4882a593Smuzhiyun */
1380*4882a593Smuzhiyun if (packets && packets < 4 && bytes < 9000 &&
1381*4882a593Smuzhiyun (q_vector->tx.target_itr & ICE_ITR_ADAPTIVE_LATENCY)) {
1382*4882a593Smuzhiyun itr = ICE_ITR_ADAPTIVE_LATENCY;
1383*4882a593Smuzhiyun goto adjust_by_size_and_speed;
1384*4882a593Smuzhiyun }
1385*4882a593Smuzhiyun } else if (packets < 4) {
1386*4882a593Smuzhiyun /* If we have Tx and Rx ITR maxed and Tx ITR is running in
1387*4882a593Smuzhiyun * bulk mode and we are receiving 4 or fewer packets just
1388*4882a593Smuzhiyun * reset the ITR_ADAPTIVE_LATENCY bit for latency mode so
1389*4882a593Smuzhiyun * that the Rx can relax.
1390*4882a593Smuzhiyun */
1391*4882a593Smuzhiyun if (rc->target_itr == ICE_ITR_ADAPTIVE_MAX_USECS &&
1392*4882a593Smuzhiyun (q_vector->rx.target_itr & ICE_ITR_MASK) ==
1393*4882a593Smuzhiyun ICE_ITR_ADAPTIVE_MAX_USECS)
1394*4882a593Smuzhiyun goto clear_counts;
1395*4882a593Smuzhiyun } else if (packets > 32) {
1396*4882a593Smuzhiyun /* If we have processed over 32 packets in a single interrupt
1397*4882a593Smuzhiyun * for Tx assume we need to switch over to "bulk" mode.
1398*4882a593Smuzhiyun */
1399*4882a593Smuzhiyun rc->target_itr &= ~ICE_ITR_ADAPTIVE_LATENCY;
1400*4882a593Smuzhiyun }
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun /* We have no packets to actually measure against. This means
1403*4882a593Smuzhiyun * either one of the other queues on this vector is active or
1404*4882a593Smuzhiyun * we are a Tx queue doing TSO with too high of an interrupt rate.
1405*4882a593Smuzhiyun *
1406*4882a593Smuzhiyun * Between 4 and 56 we can assume that our current interrupt delay
1407*4882a593Smuzhiyun * is only slightly too low. As such we should increase it by a small
1408*4882a593Smuzhiyun * fixed amount.
1409*4882a593Smuzhiyun */
1410*4882a593Smuzhiyun if (packets < 56) {
1411*4882a593Smuzhiyun itr = rc->target_itr + ICE_ITR_ADAPTIVE_MIN_INC;
1412*4882a593Smuzhiyun if ((itr & ICE_ITR_MASK) > ICE_ITR_ADAPTIVE_MAX_USECS) {
1413*4882a593Smuzhiyun itr &= ICE_ITR_ADAPTIVE_LATENCY;
1414*4882a593Smuzhiyun itr += ICE_ITR_ADAPTIVE_MAX_USECS;
1415*4882a593Smuzhiyun }
1416*4882a593Smuzhiyun goto clear_counts;
1417*4882a593Smuzhiyun }
1418*4882a593Smuzhiyun
1419*4882a593Smuzhiyun if (packets <= 256) {
1420*4882a593Smuzhiyun itr = min(q_vector->tx.current_itr, q_vector->rx.current_itr);
1421*4882a593Smuzhiyun itr &= ICE_ITR_MASK;
1422*4882a593Smuzhiyun
1423*4882a593Smuzhiyun /* Between 56 and 112 is our "goldilocks" zone where we are
1424*4882a593Smuzhiyun * working out "just right". Just report that our current
1425*4882a593Smuzhiyun * ITR is good for us.
1426*4882a593Smuzhiyun */
1427*4882a593Smuzhiyun if (packets <= 112)
1428*4882a593Smuzhiyun goto clear_counts;
1429*4882a593Smuzhiyun
1430*4882a593Smuzhiyun /* If packet count is 128 or greater we are likely looking
1431*4882a593Smuzhiyun * at a slight overrun of the delay we want. Try halving
1432*4882a593Smuzhiyun * our delay to see if that will cut the number of packets
1433*4882a593Smuzhiyun * in half per interrupt.
1434*4882a593Smuzhiyun */
1435*4882a593Smuzhiyun itr >>= 1;
1436*4882a593Smuzhiyun itr &= ICE_ITR_MASK;
1437*4882a593Smuzhiyun if (itr < ICE_ITR_ADAPTIVE_MIN_USECS)
1438*4882a593Smuzhiyun itr = ICE_ITR_ADAPTIVE_MIN_USECS;
1439*4882a593Smuzhiyun
1440*4882a593Smuzhiyun goto clear_counts;
1441*4882a593Smuzhiyun }
1442*4882a593Smuzhiyun
1443*4882a593Smuzhiyun /* The paths below assume we are dealing with a bulk ITR since
1444*4882a593Smuzhiyun * number of packets is greater than 256. We are just going to have
1445*4882a593Smuzhiyun * to compute a value and try to bring the count under control,
1446*4882a593Smuzhiyun * though for smaller packet sizes there isn't much we can do as
1447*4882a593Smuzhiyun * NAPI polling will likely be kicking in sooner rather than later.
1448*4882a593Smuzhiyun */
1449*4882a593Smuzhiyun itr = ICE_ITR_ADAPTIVE_BULK;
1450*4882a593Smuzhiyun
1451*4882a593Smuzhiyun adjust_by_size_and_speed:
1452*4882a593Smuzhiyun
1453*4882a593Smuzhiyun /* based on checks above packets cannot be 0 so division is safe */
1454*4882a593Smuzhiyun itr = ice_adjust_itr_by_size_and_speed(q_vector->vsi->port_info,
1455*4882a593Smuzhiyun bytes / packets, itr);
1456*4882a593Smuzhiyun
1457*4882a593Smuzhiyun clear_counts:
1458*4882a593Smuzhiyun /* write back value */
1459*4882a593Smuzhiyun rc->target_itr = itr;
1460*4882a593Smuzhiyun
1461*4882a593Smuzhiyun /* next update should occur within next jiffy */
1462*4882a593Smuzhiyun rc->next_update = next_update + 1;
1463*4882a593Smuzhiyun
1464*4882a593Smuzhiyun rc->total_bytes = 0;
1465*4882a593Smuzhiyun rc->total_pkts = 0;
1466*4882a593Smuzhiyun }
1467*4882a593Smuzhiyun
1468*4882a593Smuzhiyun /**
1469*4882a593Smuzhiyun * ice_buildreg_itr - build value for writing to the GLINT_DYN_CTL register
1470*4882a593Smuzhiyun * @itr_idx: interrupt throttling index
1471*4882a593Smuzhiyun * @itr: interrupt throttling value in usecs
1472*4882a593Smuzhiyun */
ice_buildreg_itr(u16 itr_idx,u16 itr)1473*4882a593Smuzhiyun static u32 ice_buildreg_itr(u16 itr_idx, u16 itr)
1474*4882a593Smuzhiyun {
1475*4882a593Smuzhiyun /* The ITR value is reported in microseconds, and the register value is
1476*4882a593Smuzhiyun * recorded in 2 microsecond units. For this reason we only need to
1477*4882a593Smuzhiyun * shift by the GLINT_DYN_CTL_INTERVAL_S - ICE_ITR_GRAN_S to apply this
1478*4882a593Smuzhiyun * granularity as a shift instead of division. The mask makes sure the
1479*4882a593Smuzhiyun * ITR value is never odd so we don't accidentally write into the field
1480*4882a593Smuzhiyun * prior to the ITR field.
1481*4882a593Smuzhiyun */
1482*4882a593Smuzhiyun itr &= ICE_ITR_MASK;
1483*4882a593Smuzhiyun
1484*4882a593Smuzhiyun return GLINT_DYN_CTL_INTENA_M | GLINT_DYN_CTL_CLEARPBA_M |
1485*4882a593Smuzhiyun (itr_idx << GLINT_DYN_CTL_ITR_INDX_S) |
1486*4882a593Smuzhiyun (itr << (GLINT_DYN_CTL_INTERVAL_S - ICE_ITR_GRAN_S));
1487*4882a593Smuzhiyun }
1488*4882a593Smuzhiyun
1489*4882a593Smuzhiyun /* The act of updating the ITR will cause it to immediately trigger. In order
1490*4882a593Smuzhiyun * to prevent this from throwing off adaptive update statistics we defer the
1491*4882a593Smuzhiyun * update so that it can only happen so often. So after either Tx or Rx are
1492*4882a593Smuzhiyun * updated we make the adaptive scheme wait until either the ITR completely
1493*4882a593Smuzhiyun * expires via the next_update expiration or we have been through at least
1494*4882a593Smuzhiyun * 3 interrupts.
1495*4882a593Smuzhiyun */
1496*4882a593Smuzhiyun #define ITR_COUNTDOWN_START 3
1497*4882a593Smuzhiyun
1498*4882a593Smuzhiyun /**
1499*4882a593Smuzhiyun * ice_update_ena_itr - Update ITR and re-enable MSIX interrupt
1500*4882a593Smuzhiyun * @q_vector: q_vector for which ITR is being updated and interrupt enabled
1501*4882a593Smuzhiyun */
ice_update_ena_itr(struct ice_q_vector * q_vector)1502*4882a593Smuzhiyun static void ice_update_ena_itr(struct ice_q_vector *q_vector)
1503*4882a593Smuzhiyun {
1504*4882a593Smuzhiyun struct ice_ring_container *tx = &q_vector->tx;
1505*4882a593Smuzhiyun struct ice_ring_container *rx = &q_vector->rx;
1506*4882a593Smuzhiyun struct ice_vsi *vsi = q_vector->vsi;
1507*4882a593Smuzhiyun u32 itr_val;
1508*4882a593Smuzhiyun
1509*4882a593Smuzhiyun /* when exiting WB_ON_ITR lets set a low ITR value and trigger
1510*4882a593Smuzhiyun * interrupts to expire right away in case we have more work ready to go
1511*4882a593Smuzhiyun * already
1512*4882a593Smuzhiyun */
1513*4882a593Smuzhiyun if (q_vector->itr_countdown == ICE_IN_WB_ON_ITR_MODE) {
1514*4882a593Smuzhiyun itr_val = ice_buildreg_itr(rx->itr_idx, ICE_WB_ON_ITR_USECS);
1515*4882a593Smuzhiyun wr32(&vsi->back->hw, GLINT_DYN_CTL(q_vector->reg_idx), itr_val);
1516*4882a593Smuzhiyun /* set target back to last user set value */
1517*4882a593Smuzhiyun rx->target_itr = rx->itr_setting;
1518*4882a593Smuzhiyun /* set current to what we just wrote and dynamic if needed */
1519*4882a593Smuzhiyun rx->current_itr = ICE_WB_ON_ITR_USECS |
1520*4882a593Smuzhiyun (rx->itr_setting & ICE_ITR_DYNAMIC);
1521*4882a593Smuzhiyun /* allow normal interrupt flow to start */
1522*4882a593Smuzhiyun q_vector->itr_countdown = 0;
1523*4882a593Smuzhiyun return;
1524*4882a593Smuzhiyun }
1525*4882a593Smuzhiyun
1526*4882a593Smuzhiyun /* This will do nothing if dynamic updates are not enabled */
1527*4882a593Smuzhiyun ice_update_itr(q_vector, tx);
1528*4882a593Smuzhiyun ice_update_itr(q_vector, rx);
1529*4882a593Smuzhiyun
1530*4882a593Smuzhiyun /* This block of logic allows us to get away with only updating
1531*4882a593Smuzhiyun * one ITR value with each interrupt. The idea is to perform a
1532*4882a593Smuzhiyun * pseudo-lazy update with the following criteria.
1533*4882a593Smuzhiyun *
1534*4882a593Smuzhiyun * 1. Rx is given higher priority than Tx if both are in same state
1535*4882a593Smuzhiyun * 2. If we must reduce an ITR that is given highest priority.
1536*4882a593Smuzhiyun * 3. We then give priority to increasing ITR based on amount.
1537*4882a593Smuzhiyun */
1538*4882a593Smuzhiyun if (rx->target_itr < rx->current_itr) {
1539*4882a593Smuzhiyun /* Rx ITR needs to be reduced, this is highest priority */
1540*4882a593Smuzhiyun itr_val = ice_buildreg_itr(rx->itr_idx, rx->target_itr);
1541*4882a593Smuzhiyun rx->current_itr = rx->target_itr;
1542*4882a593Smuzhiyun q_vector->itr_countdown = ITR_COUNTDOWN_START;
1543*4882a593Smuzhiyun } else if ((tx->target_itr < tx->current_itr) ||
1544*4882a593Smuzhiyun ((rx->target_itr - rx->current_itr) <
1545*4882a593Smuzhiyun (tx->target_itr - tx->current_itr))) {
1546*4882a593Smuzhiyun /* Tx ITR needs to be reduced, this is second priority
1547*4882a593Smuzhiyun * Tx ITR needs to be increased more than Rx, fourth priority
1548*4882a593Smuzhiyun */
1549*4882a593Smuzhiyun itr_val = ice_buildreg_itr(tx->itr_idx, tx->target_itr);
1550*4882a593Smuzhiyun tx->current_itr = tx->target_itr;
1551*4882a593Smuzhiyun q_vector->itr_countdown = ITR_COUNTDOWN_START;
1552*4882a593Smuzhiyun } else if (rx->current_itr != rx->target_itr) {
1553*4882a593Smuzhiyun /* Rx ITR needs to be increased, third priority */
1554*4882a593Smuzhiyun itr_val = ice_buildreg_itr(rx->itr_idx, rx->target_itr);
1555*4882a593Smuzhiyun rx->current_itr = rx->target_itr;
1556*4882a593Smuzhiyun q_vector->itr_countdown = ITR_COUNTDOWN_START;
1557*4882a593Smuzhiyun } else {
1558*4882a593Smuzhiyun /* Still have to re-enable the interrupts */
1559*4882a593Smuzhiyun itr_val = ice_buildreg_itr(ICE_ITR_NONE, 0);
1560*4882a593Smuzhiyun if (q_vector->itr_countdown)
1561*4882a593Smuzhiyun q_vector->itr_countdown--;
1562*4882a593Smuzhiyun }
1563*4882a593Smuzhiyun
1564*4882a593Smuzhiyun if (!test_bit(__ICE_DOWN, q_vector->vsi->state))
1565*4882a593Smuzhiyun wr32(&q_vector->vsi->back->hw,
1566*4882a593Smuzhiyun GLINT_DYN_CTL(q_vector->reg_idx),
1567*4882a593Smuzhiyun itr_val);
1568*4882a593Smuzhiyun }
1569*4882a593Smuzhiyun
1570*4882a593Smuzhiyun /**
1571*4882a593Smuzhiyun * ice_set_wb_on_itr - set WB_ON_ITR for this q_vector
1572*4882a593Smuzhiyun * @q_vector: q_vector to set WB_ON_ITR on
1573*4882a593Smuzhiyun *
1574*4882a593Smuzhiyun * We need to tell hardware to write-back completed descriptors even when
1575*4882a593Smuzhiyun * interrupts are disabled. Descriptors will be written back on cache line
1576*4882a593Smuzhiyun * boundaries without WB_ON_ITR enabled, but if we don't enable WB_ON_ITR
1577*4882a593Smuzhiyun * descriptors may not be written back if they don't fill a cache line until the
1578*4882a593Smuzhiyun * next interrupt.
1579*4882a593Smuzhiyun *
1580*4882a593Smuzhiyun * This sets the write-back frequency to 2 microseconds as that is the minimum
1581*4882a593Smuzhiyun * value that's not 0 due to ITR granularity. Also, set the INTENA_MSK bit to
1582*4882a593Smuzhiyun * make sure hardware knows we aren't meddling with the INTENA_M bit.
1583*4882a593Smuzhiyun */
ice_set_wb_on_itr(struct ice_q_vector * q_vector)1584*4882a593Smuzhiyun static void ice_set_wb_on_itr(struct ice_q_vector *q_vector)
1585*4882a593Smuzhiyun {
1586*4882a593Smuzhiyun struct ice_vsi *vsi = q_vector->vsi;
1587*4882a593Smuzhiyun
1588*4882a593Smuzhiyun /* already in WB_ON_ITR mode no need to change it */
1589*4882a593Smuzhiyun if (q_vector->itr_countdown == ICE_IN_WB_ON_ITR_MODE)
1590*4882a593Smuzhiyun return;
1591*4882a593Smuzhiyun
1592*4882a593Smuzhiyun if (q_vector->num_ring_rx)
1593*4882a593Smuzhiyun wr32(&vsi->back->hw, GLINT_DYN_CTL(q_vector->reg_idx),
1594*4882a593Smuzhiyun ICE_GLINT_DYN_CTL_WB_ON_ITR(ICE_WB_ON_ITR_USECS,
1595*4882a593Smuzhiyun ICE_RX_ITR));
1596*4882a593Smuzhiyun
1597*4882a593Smuzhiyun if (q_vector->num_ring_tx)
1598*4882a593Smuzhiyun wr32(&vsi->back->hw, GLINT_DYN_CTL(q_vector->reg_idx),
1599*4882a593Smuzhiyun ICE_GLINT_DYN_CTL_WB_ON_ITR(ICE_WB_ON_ITR_USECS,
1600*4882a593Smuzhiyun ICE_TX_ITR));
1601*4882a593Smuzhiyun
1602*4882a593Smuzhiyun q_vector->itr_countdown = ICE_IN_WB_ON_ITR_MODE;
1603*4882a593Smuzhiyun }
1604*4882a593Smuzhiyun
1605*4882a593Smuzhiyun /**
1606*4882a593Smuzhiyun * ice_napi_poll - NAPI polling Rx/Tx cleanup routine
1607*4882a593Smuzhiyun * @napi: napi struct with our devices info in it
1608*4882a593Smuzhiyun * @budget: amount of work driver is allowed to do this pass, in packets
1609*4882a593Smuzhiyun *
1610*4882a593Smuzhiyun * This function will clean all queues associated with a q_vector.
1611*4882a593Smuzhiyun *
1612*4882a593Smuzhiyun * Returns the amount of work done
1613*4882a593Smuzhiyun */
ice_napi_poll(struct napi_struct * napi,int budget)1614*4882a593Smuzhiyun int ice_napi_poll(struct napi_struct *napi, int budget)
1615*4882a593Smuzhiyun {
1616*4882a593Smuzhiyun struct ice_q_vector *q_vector =
1617*4882a593Smuzhiyun container_of(napi, struct ice_q_vector, napi);
1618*4882a593Smuzhiyun bool clean_complete = true;
1619*4882a593Smuzhiyun struct ice_ring *ring;
1620*4882a593Smuzhiyun int budget_per_ring;
1621*4882a593Smuzhiyun int work_done = 0;
1622*4882a593Smuzhiyun
1623*4882a593Smuzhiyun /* Since the actual Tx work is minimal, we can give the Tx a larger
1624*4882a593Smuzhiyun * budget and be more aggressive about cleaning up the Tx descriptors.
1625*4882a593Smuzhiyun */
1626*4882a593Smuzhiyun ice_for_each_ring(ring, q_vector->tx) {
1627*4882a593Smuzhiyun bool wd = ring->xsk_pool ?
1628*4882a593Smuzhiyun ice_clean_tx_irq_zc(ring, budget) :
1629*4882a593Smuzhiyun ice_clean_tx_irq(ring, budget);
1630*4882a593Smuzhiyun
1631*4882a593Smuzhiyun if (!wd)
1632*4882a593Smuzhiyun clean_complete = false;
1633*4882a593Smuzhiyun }
1634*4882a593Smuzhiyun
1635*4882a593Smuzhiyun /* Handle case where we are called by netpoll with a budget of 0 */
1636*4882a593Smuzhiyun if (unlikely(budget <= 0))
1637*4882a593Smuzhiyun return budget;
1638*4882a593Smuzhiyun
1639*4882a593Smuzhiyun /* normally we have 1 Rx ring per q_vector */
1640*4882a593Smuzhiyun if (unlikely(q_vector->num_ring_rx > 1))
1641*4882a593Smuzhiyun /* We attempt to distribute budget to each Rx queue fairly, but
1642*4882a593Smuzhiyun * don't allow the budget to go below 1 because that would exit
1643*4882a593Smuzhiyun * polling early.
1644*4882a593Smuzhiyun */
1645*4882a593Smuzhiyun budget_per_ring = max_t(int, budget / q_vector->num_ring_rx, 1);
1646*4882a593Smuzhiyun else
1647*4882a593Smuzhiyun /* Max of 1 Rx ring in this q_vector so give it the budget */
1648*4882a593Smuzhiyun budget_per_ring = budget;
1649*4882a593Smuzhiyun
1650*4882a593Smuzhiyun ice_for_each_ring(ring, q_vector->rx) {
1651*4882a593Smuzhiyun int cleaned;
1652*4882a593Smuzhiyun
1653*4882a593Smuzhiyun /* A dedicated path for zero-copy allows making a single
1654*4882a593Smuzhiyun * comparison in the irq context instead of many inside the
1655*4882a593Smuzhiyun * ice_clean_rx_irq function and makes the codebase cleaner.
1656*4882a593Smuzhiyun */
1657*4882a593Smuzhiyun cleaned = ring->xsk_pool ?
1658*4882a593Smuzhiyun ice_clean_rx_irq_zc(ring, budget_per_ring) :
1659*4882a593Smuzhiyun ice_clean_rx_irq(ring, budget_per_ring);
1660*4882a593Smuzhiyun work_done += cleaned;
1661*4882a593Smuzhiyun /* if we clean as many as budgeted, we must not be done */
1662*4882a593Smuzhiyun if (cleaned >= budget_per_ring)
1663*4882a593Smuzhiyun clean_complete = false;
1664*4882a593Smuzhiyun }
1665*4882a593Smuzhiyun
1666*4882a593Smuzhiyun /* If work not completed, return budget and polling will return */
1667*4882a593Smuzhiyun if (!clean_complete)
1668*4882a593Smuzhiyun return budget;
1669*4882a593Smuzhiyun
1670*4882a593Smuzhiyun /* Exit the polling mode, but don't re-enable interrupts if stack might
1671*4882a593Smuzhiyun * poll us due to busy-polling
1672*4882a593Smuzhiyun */
1673*4882a593Smuzhiyun if (likely(napi_complete_done(napi, work_done)))
1674*4882a593Smuzhiyun ice_update_ena_itr(q_vector);
1675*4882a593Smuzhiyun else
1676*4882a593Smuzhiyun ice_set_wb_on_itr(q_vector);
1677*4882a593Smuzhiyun
1678*4882a593Smuzhiyun return min_t(int, work_done, budget - 1);
1679*4882a593Smuzhiyun }
1680*4882a593Smuzhiyun
1681*4882a593Smuzhiyun /**
1682*4882a593Smuzhiyun * __ice_maybe_stop_tx - 2nd level check for Tx stop conditions
1683*4882a593Smuzhiyun * @tx_ring: the ring to be checked
1684*4882a593Smuzhiyun * @size: the size buffer we want to assure is available
1685*4882a593Smuzhiyun *
1686*4882a593Smuzhiyun * Returns -EBUSY if a stop is needed, else 0
1687*4882a593Smuzhiyun */
__ice_maybe_stop_tx(struct ice_ring * tx_ring,unsigned int size)1688*4882a593Smuzhiyun static int __ice_maybe_stop_tx(struct ice_ring *tx_ring, unsigned int size)
1689*4882a593Smuzhiyun {
1690*4882a593Smuzhiyun netif_stop_subqueue(tx_ring->netdev, tx_ring->q_index);
1691*4882a593Smuzhiyun /* Memory barrier before checking head and tail */
1692*4882a593Smuzhiyun smp_mb();
1693*4882a593Smuzhiyun
1694*4882a593Smuzhiyun /* Check again in a case another CPU has just made room available. */
1695*4882a593Smuzhiyun if (likely(ICE_DESC_UNUSED(tx_ring) < size))
1696*4882a593Smuzhiyun return -EBUSY;
1697*4882a593Smuzhiyun
1698*4882a593Smuzhiyun /* A reprieve! - use start_subqueue because it doesn't call schedule */
1699*4882a593Smuzhiyun netif_start_subqueue(tx_ring->netdev, tx_ring->q_index);
1700*4882a593Smuzhiyun ++tx_ring->tx_stats.restart_q;
1701*4882a593Smuzhiyun return 0;
1702*4882a593Smuzhiyun }
1703*4882a593Smuzhiyun
1704*4882a593Smuzhiyun /**
1705*4882a593Smuzhiyun * ice_maybe_stop_tx - 1st level check for Tx stop conditions
1706*4882a593Smuzhiyun * @tx_ring: the ring to be checked
1707*4882a593Smuzhiyun * @size: the size buffer we want to assure is available
1708*4882a593Smuzhiyun *
1709*4882a593Smuzhiyun * Returns 0 if stop is not needed
1710*4882a593Smuzhiyun */
ice_maybe_stop_tx(struct ice_ring * tx_ring,unsigned int size)1711*4882a593Smuzhiyun static int ice_maybe_stop_tx(struct ice_ring *tx_ring, unsigned int size)
1712*4882a593Smuzhiyun {
1713*4882a593Smuzhiyun if (likely(ICE_DESC_UNUSED(tx_ring) >= size))
1714*4882a593Smuzhiyun return 0;
1715*4882a593Smuzhiyun
1716*4882a593Smuzhiyun return __ice_maybe_stop_tx(tx_ring, size);
1717*4882a593Smuzhiyun }
1718*4882a593Smuzhiyun
1719*4882a593Smuzhiyun /**
1720*4882a593Smuzhiyun * ice_tx_map - Build the Tx descriptor
1721*4882a593Smuzhiyun * @tx_ring: ring to send buffer on
1722*4882a593Smuzhiyun * @first: first buffer info buffer to use
1723*4882a593Smuzhiyun * @off: pointer to struct that holds offload parameters
1724*4882a593Smuzhiyun *
1725*4882a593Smuzhiyun * This function loops over the skb data pointed to by *first
1726*4882a593Smuzhiyun * and gets a physical address for each memory location and programs
1727*4882a593Smuzhiyun * it and the length into the transmit descriptor.
1728*4882a593Smuzhiyun */
1729*4882a593Smuzhiyun static void
ice_tx_map(struct ice_ring * tx_ring,struct ice_tx_buf * first,struct ice_tx_offload_params * off)1730*4882a593Smuzhiyun ice_tx_map(struct ice_ring *tx_ring, struct ice_tx_buf *first,
1731*4882a593Smuzhiyun struct ice_tx_offload_params *off)
1732*4882a593Smuzhiyun {
1733*4882a593Smuzhiyun u64 td_offset, td_tag, td_cmd;
1734*4882a593Smuzhiyun u16 i = tx_ring->next_to_use;
1735*4882a593Smuzhiyun unsigned int data_len, size;
1736*4882a593Smuzhiyun struct ice_tx_desc *tx_desc;
1737*4882a593Smuzhiyun struct ice_tx_buf *tx_buf;
1738*4882a593Smuzhiyun struct sk_buff *skb;
1739*4882a593Smuzhiyun skb_frag_t *frag;
1740*4882a593Smuzhiyun dma_addr_t dma;
1741*4882a593Smuzhiyun
1742*4882a593Smuzhiyun td_tag = off->td_l2tag1;
1743*4882a593Smuzhiyun td_cmd = off->td_cmd;
1744*4882a593Smuzhiyun td_offset = off->td_offset;
1745*4882a593Smuzhiyun skb = first->skb;
1746*4882a593Smuzhiyun
1747*4882a593Smuzhiyun data_len = skb->data_len;
1748*4882a593Smuzhiyun size = skb_headlen(skb);
1749*4882a593Smuzhiyun
1750*4882a593Smuzhiyun tx_desc = ICE_TX_DESC(tx_ring, i);
1751*4882a593Smuzhiyun
1752*4882a593Smuzhiyun if (first->tx_flags & ICE_TX_FLAGS_HW_VLAN) {
1753*4882a593Smuzhiyun td_cmd |= (u64)ICE_TX_DESC_CMD_IL2TAG1;
1754*4882a593Smuzhiyun td_tag = (first->tx_flags & ICE_TX_FLAGS_VLAN_M) >>
1755*4882a593Smuzhiyun ICE_TX_FLAGS_VLAN_S;
1756*4882a593Smuzhiyun }
1757*4882a593Smuzhiyun
1758*4882a593Smuzhiyun dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1759*4882a593Smuzhiyun
1760*4882a593Smuzhiyun tx_buf = first;
1761*4882a593Smuzhiyun
1762*4882a593Smuzhiyun for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1763*4882a593Smuzhiyun unsigned int max_data = ICE_MAX_DATA_PER_TXD_ALIGNED;
1764*4882a593Smuzhiyun
1765*4882a593Smuzhiyun if (dma_mapping_error(tx_ring->dev, dma))
1766*4882a593Smuzhiyun goto dma_error;
1767*4882a593Smuzhiyun
1768*4882a593Smuzhiyun /* record length, and DMA address */
1769*4882a593Smuzhiyun dma_unmap_len_set(tx_buf, len, size);
1770*4882a593Smuzhiyun dma_unmap_addr_set(tx_buf, dma, dma);
1771*4882a593Smuzhiyun
1772*4882a593Smuzhiyun /* align size to end of page */
1773*4882a593Smuzhiyun max_data += -dma & (ICE_MAX_READ_REQ_SIZE - 1);
1774*4882a593Smuzhiyun tx_desc->buf_addr = cpu_to_le64(dma);
1775*4882a593Smuzhiyun
1776*4882a593Smuzhiyun /* account for data chunks larger than the hardware
1777*4882a593Smuzhiyun * can handle
1778*4882a593Smuzhiyun */
1779*4882a593Smuzhiyun while (unlikely(size > ICE_MAX_DATA_PER_TXD)) {
1780*4882a593Smuzhiyun tx_desc->cmd_type_offset_bsz =
1781*4882a593Smuzhiyun ice_build_ctob(td_cmd, td_offset, max_data,
1782*4882a593Smuzhiyun td_tag);
1783*4882a593Smuzhiyun
1784*4882a593Smuzhiyun tx_desc++;
1785*4882a593Smuzhiyun i++;
1786*4882a593Smuzhiyun
1787*4882a593Smuzhiyun if (i == tx_ring->count) {
1788*4882a593Smuzhiyun tx_desc = ICE_TX_DESC(tx_ring, 0);
1789*4882a593Smuzhiyun i = 0;
1790*4882a593Smuzhiyun }
1791*4882a593Smuzhiyun
1792*4882a593Smuzhiyun dma += max_data;
1793*4882a593Smuzhiyun size -= max_data;
1794*4882a593Smuzhiyun
1795*4882a593Smuzhiyun max_data = ICE_MAX_DATA_PER_TXD_ALIGNED;
1796*4882a593Smuzhiyun tx_desc->buf_addr = cpu_to_le64(dma);
1797*4882a593Smuzhiyun }
1798*4882a593Smuzhiyun
1799*4882a593Smuzhiyun if (likely(!data_len))
1800*4882a593Smuzhiyun break;
1801*4882a593Smuzhiyun
1802*4882a593Smuzhiyun tx_desc->cmd_type_offset_bsz = ice_build_ctob(td_cmd, td_offset,
1803*4882a593Smuzhiyun size, td_tag);
1804*4882a593Smuzhiyun
1805*4882a593Smuzhiyun tx_desc++;
1806*4882a593Smuzhiyun i++;
1807*4882a593Smuzhiyun
1808*4882a593Smuzhiyun if (i == tx_ring->count) {
1809*4882a593Smuzhiyun tx_desc = ICE_TX_DESC(tx_ring, 0);
1810*4882a593Smuzhiyun i = 0;
1811*4882a593Smuzhiyun }
1812*4882a593Smuzhiyun
1813*4882a593Smuzhiyun size = skb_frag_size(frag);
1814*4882a593Smuzhiyun data_len -= size;
1815*4882a593Smuzhiyun
1816*4882a593Smuzhiyun dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
1817*4882a593Smuzhiyun DMA_TO_DEVICE);
1818*4882a593Smuzhiyun
1819*4882a593Smuzhiyun tx_buf = &tx_ring->tx_buf[i];
1820*4882a593Smuzhiyun }
1821*4882a593Smuzhiyun
1822*4882a593Smuzhiyun /* record bytecount for BQL */
1823*4882a593Smuzhiyun netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
1824*4882a593Smuzhiyun
1825*4882a593Smuzhiyun /* record SW timestamp if HW timestamp is not available */
1826*4882a593Smuzhiyun skb_tx_timestamp(first->skb);
1827*4882a593Smuzhiyun
1828*4882a593Smuzhiyun i++;
1829*4882a593Smuzhiyun if (i == tx_ring->count)
1830*4882a593Smuzhiyun i = 0;
1831*4882a593Smuzhiyun
1832*4882a593Smuzhiyun /* write last descriptor with RS and EOP bits */
1833*4882a593Smuzhiyun td_cmd |= (u64)ICE_TXD_LAST_DESC_CMD;
1834*4882a593Smuzhiyun tx_desc->cmd_type_offset_bsz =
1835*4882a593Smuzhiyun ice_build_ctob(td_cmd, td_offset, size, td_tag);
1836*4882a593Smuzhiyun
1837*4882a593Smuzhiyun /* Force memory writes to complete before letting h/w know there
1838*4882a593Smuzhiyun * are new descriptors to fetch.
1839*4882a593Smuzhiyun *
1840*4882a593Smuzhiyun * We also use this memory barrier to make certain all of the
1841*4882a593Smuzhiyun * status bits have been updated before next_to_watch is written.
1842*4882a593Smuzhiyun */
1843*4882a593Smuzhiyun wmb();
1844*4882a593Smuzhiyun
1845*4882a593Smuzhiyun /* set next_to_watch value indicating a packet is present */
1846*4882a593Smuzhiyun first->next_to_watch = tx_desc;
1847*4882a593Smuzhiyun
1848*4882a593Smuzhiyun tx_ring->next_to_use = i;
1849*4882a593Smuzhiyun
1850*4882a593Smuzhiyun ice_maybe_stop_tx(tx_ring, DESC_NEEDED);
1851*4882a593Smuzhiyun
1852*4882a593Smuzhiyun /* notify HW of packet */
1853*4882a593Smuzhiyun if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more())
1854*4882a593Smuzhiyun writel(i, tx_ring->tail);
1855*4882a593Smuzhiyun
1856*4882a593Smuzhiyun return;
1857*4882a593Smuzhiyun
1858*4882a593Smuzhiyun dma_error:
1859*4882a593Smuzhiyun /* clear DMA mappings for failed tx_buf map */
1860*4882a593Smuzhiyun for (;;) {
1861*4882a593Smuzhiyun tx_buf = &tx_ring->tx_buf[i];
1862*4882a593Smuzhiyun ice_unmap_and_free_tx_buf(tx_ring, tx_buf);
1863*4882a593Smuzhiyun if (tx_buf == first)
1864*4882a593Smuzhiyun break;
1865*4882a593Smuzhiyun if (i == 0)
1866*4882a593Smuzhiyun i = tx_ring->count;
1867*4882a593Smuzhiyun i--;
1868*4882a593Smuzhiyun }
1869*4882a593Smuzhiyun
1870*4882a593Smuzhiyun tx_ring->next_to_use = i;
1871*4882a593Smuzhiyun }
1872*4882a593Smuzhiyun
1873*4882a593Smuzhiyun /**
1874*4882a593Smuzhiyun * ice_tx_csum - Enable Tx checksum offloads
1875*4882a593Smuzhiyun * @first: pointer to the first descriptor
1876*4882a593Smuzhiyun * @off: pointer to struct that holds offload parameters
1877*4882a593Smuzhiyun *
1878*4882a593Smuzhiyun * Returns 0 or error (negative) if checksum offload can't happen, 1 otherwise.
1879*4882a593Smuzhiyun */
1880*4882a593Smuzhiyun static
ice_tx_csum(struct ice_tx_buf * first,struct ice_tx_offload_params * off)1881*4882a593Smuzhiyun int ice_tx_csum(struct ice_tx_buf *first, struct ice_tx_offload_params *off)
1882*4882a593Smuzhiyun {
1883*4882a593Smuzhiyun u32 l4_len = 0, l3_len = 0, l2_len = 0;
1884*4882a593Smuzhiyun struct sk_buff *skb = first->skb;
1885*4882a593Smuzhiyun union {
1886*4882a593Smuzhiyun struct iphdr *v4;
1887*4882a593Smuzhiyun struct ipv6hdr *v6;
1888*4882a593Smuzhiyun unsigned char *hdr;
1889*4882a593Smuzhiyun } ip;
1890*4882a593Smuzhiyun union {
1891*4882a593Smuzhiyun struct tcphdr *tcp;
1892*4882a593Smuzhiyun unsigned char *hdr;
1893*4882a593Smuzhiyun } l4;
1894*4882a593Smuzhiyun __be16 frag_off, protocol;
1895*4882a593Smuzhiyun unsigned char *exthdr;
1896*4882a593Smuzhiyun u32 offset, cmd = 0;
1897*4882a593Smuzhiyun u8 l4_proto = 0;
1898*4882a593Smuzhiyun
1899*4882a593Smuzhiyun if (skb->ip_summed != CHECKSUM_PARTIAL)
1900*4882a593Smuzhiyun return 0;
1901*4882a593Smuzhiyun
1902*4882a593Smuzhiyun ip.hdr = skb_network_header(skb);
1903*4882a593Smuzhiyun l4.hdr = skb_transport_header(skb);
1904*4882a593Smuzhiyun
1905*4882a593Smuzhiyun /* compute outer L2 header size */
1906*4882a593Smuzhiyun l2_len = ip.hdr - skb->data;
1907*4882a593Smuzhiyun offset = (l2_len / 2) << ICE_TX_DESC_LEN_MACLEN_S;
1908*4882a593Smuzhiyun
1909*4882a593Smuzhiyun protocol = vlan_get_protocol(skb);
1910*4882a593Smuzhiyun
1911*4882a593Smuzhiyun if (protocol == htons(ETH_P_IP))
1912*4882a593Smuzhiyun first->tx_flags |= ICE_TX_FLAGS_IPV4;
1913*4882a593Smuzhiyun else if (protocol == htons(ETH_P_IPV6))
1914*4882a593Smuzhiyun first->tx_flags |= ICE_TX_FLAGS_IPV6;
1915*4882a593Smuzhiyun
1916*4882a593Smuzhiyun if (skb->encapsulation) {
1917*4882a593Smuzhiyun bool gso_ena = false;
1918*4882a593Smuzhiyun u32 tunnel = 0;
1919*4882a593Smuzhiyun
1920*4882a593Smuzhiyun /* define outer network header type */
1921*4882a593Smuzhiyun if (first->tx_flags & ICE_TX_FLAGS_IPV4) {
1922*4882a593Smuzhiyun tunnel |= (first->tx_flags & ICE_TX_FLAGS_TSO) ?
1923*4882a593Smuzhiyun ICE_TX_CTX_EIPT_IPV4 :
1924*4882a593Smuzhiyun ICE_TX_CTX_EIPT_IPV4_NO_CSUM;
1925*4882a593Smuzhiyun l4_proto = ip.v4->protocol;
1926*4882a593Smuzhiyun } else if (first->tx_flags & ICE_TX_FLAGS_IPV6) {
1927*4882a593Smuzhiyun int ret;
1928*4882a593Smuzhiyun
1929*4882a593Smuzhiyun tunnel |= ICE_TX_CTX_EIPT_IPV6;
1930*4882a593Smuzhiyun exthdr = ip.hdr + sizeof(*ip.v6);
1931*4882a593Smuzhiyun l4_proto = ip.v6->nexthdr;
1932*4882a593Smuzhiyun ret = ipv6_skip_exthdr(skb, exthdr - skb->data,
1933*4882a593Smuzhiyun &l4_proto, &frag_off);
1934*4882a593Smuzhiyun if (ret < 0)
1935*4882a593Smuzhiyun return -1;
1936*4882a593Smuzhiyun }
1937*4882a593Smuzhiyun
1938*4882a593Smuzhiyun /* define outer transport */
1939*4882a593Smuzhiyun switch (l4_proto) {
1940*4882a593Smuzhiyun case IPPROTO_UDP:
1941*4882a593Smuzhiyun tunnel |= ICE_TXD_CTX_UDP_TUNNELING;
1942*4882a593Smuzhiyun first->tx_flags |= ICE_TX_FLAGS_TUNNEL;
1943*4882a593Smuzhiyun break;
1944*4882a593Smuzhiyun case IPPROTO_GRE:
1945*4882a593Smuzhiyun tunnel |= ICE_TXD_CTX_GRE_TUNNELING;
1946*4882a593Smuzhiyun first->tx_flags |= ICE_TX_FLAGS_TUNNEL;
1947*4882a593Smuzhiyun break;
1948*4882a593Smuzhiyun case IPPROTO_IPIP:
1949*4882a593Smuzhiyun case IPPROTO_IPV6:
1950*4882a593Smuzhiyun first->tx_flags |= ICE_TX_FLAGS_TUNNEL;
1951*4882a593Smuzhiyun l4.hdr = skb_inner_network_header(skb);
1952*4882a593Smuzhiyun break;
1953*4882a593Smuzhiyun default:
1954*4882a593Smuzhiyun if (first->tx_flags & ICE_TX_FLAGS_TSO)
1955*4882a593Smuzhiyun return -1;
1956*4882a593Smuzhiyun
1957*4882a593Smuzhiyun skb_checksum_help(skb);
1958*4882a593Smuzhiyun return 0;
1959*4882a593Smuzhiyun }
1960*4882a593Smuzhiyun
1961*4882a593Smuzhiyun /* compute outer L3 header size */
1962*4882a593Smuzhiyun tunnel |= ((l4.hdr - ip.hdr) / 4) <<
1963*4882a593Smuzhiyun ICE_TXD_CTX_QW0_EIPLEN_S;
1964*4882a593Smuzhiyun
1965*4882a593Smuzhiyun /* switch IP header pointer from outer to inner header */
1966*4882a593Smuzhiyun ip.hdr = skb_inner_network_header(skb);
1967*4882a593Smuzhiyun
1968*4882a593Smuzhiyun /* compute tunnel header size */
1969*4882a593Smuzhiyun tunnel |= ((ip.hdr - l4.hdr) / 2) <<
1970*4882a593Smuzhiyun ICE_TXD_CTX_QW0_NATLEN_S;
1971*4882a593Smuzhiyun
1972*4882a593Smuzhiyun gso_ena = skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL;
1973*4882a593Smuzhiyun /* indicate if we need to offload outer UDP header */
1974*4882a593Smuzhiyun if ((first->tx_flags & ICE_TX_FLAGS_TSO) && !gso_ena &&
1975*4882a593Smuzhiyun (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
1976*4882a593Smuzhiyun tunnel |= ICE_TXD_CTX_QW0_L4T_CS_M;
1977*4882a593Smuzhiyun
1978*4882a593Smuzhiyun /* record tunnel offload values */
1979*4882a593Smuzhiyun off->cd_tunnel_params |= tunnel;
1980*4882a593Smuzhiyun
1981*4882a593Smuzhiyun /* set DTYP=1 to indicate that it's an Tx context descriptor
1982*4882a593Smuzhiyun * in IPsec tunnel mode with Tx offloads in Quad word 1
1983*4882a593Smuzhiyun */
1984*4882a593Smuzhiyun off->cd_qw1 |= (u64)ICE_TX_DESC_DTYPE_CTX;
1985*4882a593Smuzhiyun
1986*4882a593Smuzhiyun /* switch L4 header pointer from outer to inner */
1987*4882a593Smuzhiyun l4.hdr = skb_inner_transport_header(skb);
1988*4882a593Smuzhiyun l4_proto = 0;
1989*4882a593Smuzhiyun
1990*4882a593Smuzhiyun /* reset type as we transition from outer to inner headers */
1991*4882a593Smuzhiyun first->tx_flags &= ~(ICE_TX_FLAGS_IPV4 | ICE_TX_FLAGS_IPV6);
1992*4882a593Smuzhiyun if (ip.v4->version == 4)
1993*4882a593Smuzhiyun first->tx_flags |= ICE_TX_FLAGS_IPV4;
1994*4882a593Smuzhiyun if (ip.v6->version == 6)
1995*4882a593Smuzhiyun first->tx_flags |= ICE_TX_FLAGS_IPV6;
1996*4882a593Smuzhiyun }
1997*4882a593Smuzhiyun
1998*4882a593Smuzhiyun /* Enable IP checksum offloads */
1999*4882a593Smuzhiyun if (first->tx_flags & ICE_TX_FLAGS_IPV4) {
2000*4882a593Smuzhiyun l4_proto = ip.v4->protocol;
2001*4882a593Smuzhiyun /* the stack computes the IP header already, the only time we
2002*4882a593Smuzhiyun * need the hardware to recompute it is in the case of TSO.
2003*4882a593Smuzhiyun */
2004*4882a593Smuzhiyun if (first->tx_flags & ICE_TX_FLAGS_TSO)
2005*4882a593Smuzhiyun cmd |= ICE_TX_DESC_CMD_IIPT_IPV4_CSUM;
2006*4882a593Smuzhiyun else
2007*4882a593Smuzhiyun cmd |= ICE_TX_DESC_CMD_IIPT_IPV4;
2008*4882a593Smuzhiyun
2009*4882a593Smuzhiyun } else if (first->tx_flags & ICE_TX_FLAGS_IPV6) {
2010*4882a593Smuzhiyun cmd |= ICE_TX_DESC_CMD_IIPT_IPV6;
2011*4882a593Smuzhiyun exthdr = ip.hdr + sizeof(*ip.v6);
2012*4882a593Smuzhiyun l4_proto = ip.v6->nexthdr;
2013*4882a593Smuzhiyun if (l4.hdr != exthdr)
2014*4882a593Smuzhiyun ipv6_skip_exthdr(skb, exthdr - skb->data, &l4_proto,
2015*4882a593Smuzhiyun &frag_off);
2016*4882a593Smuzhiyun } else {
2017*4882a593Smuzhiyun return -1;
2018*4882a593Smuzhiyun }
2019*4882a593Smuzhiyun
2020*4882a593Smuzhiyun /* compute inner L3 header size */
2021*4882a593Smuzhiyun l3_len = l4.hdr - ip.hdr;
2022*4882a593Smuzhiyun offset |= (l3_len / 4) << ICE_TX_DESC_LEN_IPLEN_S;
2023*4882a593Smuzhiyun
2024*4882a593Smuzhiyun /* Enable L4 checksum offloads */
2025*4882a593Smuzhiyun switch (l4_proto) {
2026*4882a593Smuzhiyun case IPPROTO_TCP:
2027*4882a593Smuzhiyun /* enable checksum offloads */
2028*4882a593Smuzhiyun cmd |= ICE_TX_DESC_CMD_L4T_EOFT_TCP;
2029*4882a593Smuzhiyun l4_len = l4.tcp->doff;
2030*4882a593Smuzhiyun offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S;
2031*4882a593Smuzhiyun break;
2032*4882a593Smuzhiyun case IPPROTO_UDP:
2033*4882a593Smuzhiyun /* enable UDP checksum offload */
2034*4882a593Smuzhiyun cmd |= ICE_TX_DESC_CMD_L4T_EOFT_UDP;
2035*4882a593Smuzhiyun l4_len = (sizeof(struct udphdr) >> 2);
2036*4882a593Smuzhiyun offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S;
2037*4882a593Smuzhiyun break;
2038*4882a593Smuzhiyun case IPPROTO_SCTP:
2039*4882a593Smuzhiyun /* enable SCTP checksum offload */
2040*4882a593Smuzhiyun cmd |= ICE_TX_DESC_CMD_L4T_EOFT_SCTP;
2041*4882a593Smuzhiyun l4_len = sizeof(struct sctphdr) >> 2;
2042*4882a593Smuzhiyun offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S;
2043*4882a593Smuzhiyun break;
2044*4882a593Smuzhiyun
2045*4882a593Smuzhiyun default:
2046*4882a593Smuzhiyun if (first->tx_flags & ICE_TX_FLAGS_TSO)
2047*4882a593Smuzhiyun return -1;
2048*4882a593Smuzhiyun skb_checksum_help(skb);
2049*4882a593Smuzhiyun return 0;
2050*4882a593Smuzhiyun }
2051*4882a593Smuzhiyun
2052*4882a593Smuzhiyun off->td_cmd |= cmd;
2053*4882a593Smuzhiyun off->td_offset |= offset;
2054*4882a593Smuzhiyun return 1;
2055*4882a593Smuzhiyun }
2056*4882a593Smuzhiyun
2057*4882a593Smuzhiyun /**
2058*4882a593Smuzhiyun * ice_tx_prepare_vlan_flags - prepare generic Tx VLAN tagging flags for HW
2059*4882a593Smuzhiyun * @tx_ring: ring to send buffer on
2060*4882a593Smuzhiyun * @first: pointer to struct ice_tx_buf
2061*4882a593Smuzhiyun *
2062*4882a593Smuzhiyun * Checks the skb and set up correspondingly several generic transmit flags
2063*4882a593Smuzhiyun * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
2064*4882a593Smuzhiyun */
2065*4882a593Smuzhiyun static void
ice_tx_prepare_vlan_flags(struct ice_ring * tx_ring,struct ice_tx_buf * first)2066*4882a593Smuzhiyun ice_tx_prepare_vlan_flags(struct ice_ring *tx_ring, struct ice_tx_buf *first)
2067*4882a593Smuzhiyun {
2068*4882a593Smuzhiyun struct sk_buff *skb = first->skb;
2069*4882a593Smuzhiyun
2070*4882a593Smuzhiyun /* nothing left to do, software offloaded VLAN */
2071*4882a593Smuzhiyun if (!skb_vlan_tag_present(skb) && eth_type_vlan(skb->protocol))
2072*4882a593Smuzhiyun return;
2073*4882a593Smuzhiyun
2074*4882a593Smuzhiyun /* currently, we always assume 802.1Q for VLAN insertion as VLAN
2075*4882a593Smuzhiyun * insertion for 802.1AD is not supported
2076*4882a593Smuzhiyun */
2077*4882a593Smuzhiyun if (skb_vlan_tag_present(skb)) {
2078*4882a593Smuzhiyun first->tx_flags |= skb_vlan_tag_get(skb) << ICE_TX_FLAGS_VLAN_S;
2079*4882a593Smuzhiyun first->tx_flags |= ICE_TX_FLAGS_HW_VLAN;
2080*4882a593Smuzhiyun }
2081*4882a593Smuzhiyun
2082*4882a593Smuzhiyun ice_tx_prepare_vlan_flags_dcb(tx_ring, first);
2083*4882a593Smuzhiyun }
2084*4882a593Smuzhiyun
2085*4882a593Smuzhiyun /**
2086*4882a593Smuzhiyun * ice_tso - computes mss and TSO length to prepare for TSO
2087*4882a593Smuzhiyun * @first: pointer to struct ice_tx_buf
2088*4882a593Smuzhiyun * @off: pointer to struct that holds offload parameters
2089*4882a593Smuzhiyun *
2090*4882a593Smuzhiyun * Returns 0 or error (negative) if TSO can't happen, 1 otherwise.
2091*4882a593Smuzhiyun */
2092*4882a593Smuzhiyun static
ice_tso(struct ice_tx_buf * first,struct ice_tx_offload_params * off)2093*4882a593Smuzhiyun int ice_tso(struct ice_tx_buf *first, struct ice_tx_offload_params *off)
2094*4882a593Smuzhiyun {
2095*4882a593Smuzhiyun struct sk_buff *skb = first->skb;
2096*4882a593Smuzhiyun union {
2097*4882a593Smuzhiyun struct iphdr *v4;
2098*4882a593Smuzhiyun struct ipv6hdr *v6;
2099*4882a593Smuzhiyun unsigned char *hdr;
2100*4882a593Smuzhiyun } ip;
2101*4882a593Smuzhiyun union {
2102*4882a593Smuzhiyun struct tcphdr *tcp;
2103*4882a593Smuzhiyun struct udphdr *udp;
2104*4882a593Smuzhiyun unsigned char *hdr;
2105*4882a593Smuzhiyun } l4;
2106*4882a593Smuzhiyun u64 cd_mss, cd_tso_len;
2107*4882a593Smuzhiyun u32 paylen;
2108*4882a593Smuzhiyun u8 l4_start;
2109*4882a593Smuzhiyun int err;
2110*4882a593Smuzhiyun
2111*4882a593Smuzhiyun if (skb->ip_summed != CHECKSUM_PARTIAL)
2112*4882a593Smuzhiyun return 0;
2113*4882a593Smuzhiyun
2114*4882a593Smuzhiyun if (!skb_is_gso(skb))
2115*4882a593Smuzhiyun return 0;
2116*4882a593Smuzhiyun
2117*4882a593Smuzhiyun err = skb_cow_head(skb, 0);
2118*4882a593Smuzhiyun if (err < 0)
2119*4882a593Smuzhiyun return err;
2120*4882a593Smuzhiyun
2121*4882a593Smuzhiyun /* cppcheck-suppress unreadVariable */
2122*4882a593Smuzhiyun ip.hdr = skb_network_header(skb);
2123*4882a593Smuzhiyun l4.hdr = skb_transport_header(skb);
2124*4882a593Smuzhiyun
2125*4882a593Smuzhiyun /* initialize outer IP header fields */
2126*4882a593Smuzhiyun if (ip.v4->version == 4) {
2127*4882a593Smuzhiyun ip.v4->tot_len = 0;
2128*4882a593Smuzhiyun ip.v4->check = 0;
2129*4882a593Smuzhiyun } else {
2130*4882a593Smuzhiyun ip.v6->payload_len = 0;
2131*4882a593Smuzhiyun }
2132*4882a593Smuzhiyun
2133*4882a593Smuzhiyun if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
2134*4882a593Smuzhiyun SKB_GSO_GRE_CSUM |
2135*4882a593Smuzhiyun SKB_GSO_IPXIP4 |
2136*4882a593Smuzhiyun SKB_GSO_IPXIP6 |
2137*4882a593Smuzhiyun SKB_GSO_UDP_TUNNEL |
2138*4882a593Smuzhiyun SKB_GSO_UDP_TUNNEL_CSUM)) {
2139*4882a593Smuzhiyun if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
2140*4882a593Smuzhiyun (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
2141*4882a593Smuzhiyun l4.udp->len = 0;
2142*4882a593Smuzhiyun
2143*4882a593Smuzhiyun /* determine offset of outer transport header */
2144*4882a593Smuzhiyun l4_start = (u8)(l4.hdr - skb->data);
2145*4882a593Smuzhiyun
2146*4882a593Smuzhiyun /* remove payload length from outer checksum */
2147*4882a593Smuzhiyun paylen = skb->len - l4_start;
2148*4882a593Smuzhiyun csum_replace_by_diff(&l4.udp->check,
2149*4882a593Smuzhiyun (__force __wsum)htonl(paylen));
2150*4882a593Smuzhiyun }
2151*4882a593Smuzhiyun
2152*4882a593Smuzhiyun /* reset pointers to inner headers */
2153*4882a593Smuzhiyun
2154*4882a593Smuzhiyun /* cppcheck-suppress unreadVariable */
2155*4882a593Smuzhiyun ip.hdr = skb_inner_network_header(skb);
2156*4882a593Smuzhiyun l4.hdr = skb_inner_transport_header(skb);
2157*4882a593Smuzhiyun
2158*4882a593Smuzhiyun /* initialize inner IP header fields */
2159*4882a593Smuzhiyun if (ip.v4->version == 4) {
2160*4882a593Smuzhiyun ip.v4->tot_len = 0;
2161*4882a593Smuzhiyun ip.v4->check = 0;
2162*4882a593Smuzhiyun } else {
2163*4882a593Smuzhiyun ip.v6->payload_len = 0;
2164*4882a593Smuzhiyun }
2165*4882a593Smuzhiyun }
2166*4882a593Smuzhiyun
2167*4882a593Smuzhiyun /* determine offset of transport header */
2168*4882a593Smuzhiyun l4_start = (u8)(l4.hdr - skb->data);
2169*4882a593Smuzhiyun
2170*4882a593Smuzhiyun /* remove payload length from checksum */
2171*4882a593Smuzhiyun paylen = skb->len - l4_start;
2172*4882a593Smuzhiyun
2173*4882a593Smuzhiyun if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
2174*4882a593Smuzhiyun csum_replace_by_diff(&l4.udp->check,
2175*4882a593Smuzhiyun (__force __wsum)htonl(paylen));
2176*4882a593Smuzhiyun /* compute length of UDP segmentation header */
2177*4882a593Smuzhiyun off->header_len = (u8)sizeof(l4.udp) + l4_start;
2178*4882a593Smuzhiyun } else {
2179*4882a593Smuzhiyun csum_replace_by_diff(&l4.tcp->check,
2180*4882a593Smuzhiyun (__force __wsum)htonl(paylen));
2181*4882a593Smuzhiyun /* compute length of TCP segmentation header */
2182*4882a593Smuzhiyun off->header_len = (u8)((l4.tcp->doff * 4) + l4_start);
2183*4882a593Smuzhiyun }
2184*4882a593Smuzhiyun
2185*4882a593Smuzhiyun /* update gso_segs and bytecount */
2186*4882a593Smuzhiyun first->gso_segs = skb_shinfo(skb)->gso_segs;
2187*4882a593Smuzhiyun first->bytecount += (first->gso_segs - 1) * off->header_len;
2188*4882a593Smuzhiyun
2189*4882a593Smuzhiyun cd_tso_len = skb->len - off->header_len;
2190*4882a593Smuzhiyun cd_mss = skb_shinfo(skb)->gso_size;
2191*4882a593Smuzhiyun
2192*4882a593Smuzhiyun /* record cdesc_qw1 with TSO parameters */
2193*4882a593Smuzhiyun off->cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX |
2194*4882a593Smuzhiyun (ICE_TX_CTX_DESC_TSO << ICE_TXD_CTX_QW1_CMD_S) |
2195*4882a593Smuzhiyun (cd_tso_len << ICE_TXD_CTX_QW1_TSO_LEN_S) |
2196*4882a593Smuzhiyun (cd_mss << ICE_TXD_CTX_QW1_MSS_S));
2197*4882a593Smuzhiyun first->tx_flags |= ICE_TX_FLAGS_TSO;
2198*4882a593Smuzhiyun return 1;
2199*4882a593Smuzhiyun }
2200*4882a593Smuzhiyun
2201*4882a593Smuzhiyun /**
2202*4882a593Smuzhiyun * ice_txd_use_count - estimate the number of descriptors needed for Tx
2203*4882a593Smuzhiyun * @size: transmit request size in bytes
2204*4882a593Smuzhiyun *
2205*4882a593Smuzhiyun * Due to hardware alignment restrictions (4K alignment), we need to
2206*4882a593Smuzhiyun * assume that we can have no more than 12K of data per descriptor, even
2207*4882a593Smuzhiyun * though each descriptor can take up to 16K - 1 bytes of aligned memory.
2208*4882a593Smuzhiyun * Thus, we need to divide by 12K. But division is slow! Instead,
2209*4882a593Smuzhiyun * we decompose the operation into shifts and one relatively cheap
2210*4882a593Smuzhiyun * multiply operation.
2211*4882a593Smuzhiyun *
2212*4882a593Smuzhiyun * To divide by 12K, we first divide by 4K, then divide by 3:
2213*4882a593Smuzhiyun * To divide by 4K, shift right by 12 bits
2214*4882a593Smuzhiyun * To divide by 3, multiply by 85, then divide by 256
2215*4882a593Smuzhiyun * (Divide by 256 is done by shifting right by 8 bits)
2216*4882a593Smuzhiyun * Finally, we add one to round up. Because 256 isn't an exact multiple of
2217*4882a593Smuzhiyun * 3, we'll underestimate near each multiple of 12K. This is actually more
2218*4882a593Smuzhiyun * accurate as we have 4K - 1 of wiggle room that we can fit into the last
2219*4882a593Smuzhiyun * segment. For our purposes this is accurate out to 1M which is orders of
2220*4882a593Smuzhiyun * magnitude greater than our largest possible GSO size.
2221*4882a593Smuzhiyun *
2222*4882a593Smuzhiyun * This would then be implemented as:
2223*4882a593Smuzhiyun * return (((size >> 12) * 85) >> 8) + ICE_DESCS_FOR_SKB_DATA_PTR;
2224*4882a593Smuzhiyun *
2225*4882a593Smuzhiyun * Since multiplication and division are commutative, we can reorder
2226*4882a593Smuzhiyun * operations into:
2227*4882a593Smuzhiyun * return ((size * 85) >> 20) + ICE_DESCS_FOR_SKB_DATA_PTR;
2228*4882a593Smuzhiyun */
ice_txd_use_count(unsigned int size)2229*4882a593Smuzhiyun static unsigned int ice_txd_use_count(unsigned int size)
2230*4882a593Smuzhiyun {
2231*4882a593Smuzhiyun return ((size * 85) >> 20) + ICE_DESCS_FOR_SKB_DATA_PTR;
2232*4882a593Smuzhiyun }
2233*4882a593Smuzhiyun
2234*4882a593Smuzhiyun /**
2235*4882a593Smuzhiyun * ice_xmit_desc_count - calculate number of Tx descriptors needed
2236*4882a593Smuzhiyun * @skb: send buffer
2237*4882a593Smuzhiyun *
2238*4882a593Smuzhiyun * Returns number of data descriptors needed for this skb.
2239*4882a593Smuzhiyun */
ice_xmit_desc_count(struct sk_buff * skb)2240*4882a593Smuzhiyun static unsigned int ice_xmit_desc_count(struct sk_buff *skb)
2241*4882a593Smuzhiyun {
2242*4882a593Smuzhiyun const skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
2243*4882a593Smuzhiyun unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
2244*4882a593Smuzhiyun unsigned int count = 0, size = skb_headlen(skb);
2245*4882a593Smuzhiyun
2246*4882a593Smuzhiyun for (;;) {
2247*4882a593Smuzhiyun count += ice_txd_use_count(size);
2248*4882a593Smuzhiyun
2249*4882a593Smuzhiyun if (!nr_frags--)
2250*4882a593Smuzhiyun break;
2251*4882a593Smuzhiyun
2252*4882a593Smuzhiyun size = skb_frag_size(frag++);
2253*4882a593Smuzhiyun }
2254*4882a593Smuzhiyun
2255*4882a593Smuzhiyun return count;
2256*4882a593Smuzhiyun }
2257*4882a593Smuzhiyun
2258*4882a593Smuzhiyun /**
2259*4882a593Smuzhiyun * __ice_chk_linearize - Check if there are more than 8 buffers per packet
2260*4882a593Smuzhiyun * @skb: send buffer
2261*4882a593Smuzhiyun *
2262*4882a593Smuzhiyun * Note: This HW can't DMA more than 8 buffers to build a packet on the wire
2263*4882a593Smuzhiyun * and so we need to figure out the cases where we need to linearize the skb.
2264*4882a593Smuzhiyun *
2265*4882a593Smuzhiyun * For TSO we need to count the TSO header and segment payload separately.
2266*4882a593Smuzhiyun * As such we need to check cases where we have 7 fragments or more as we
2267*4882a593Smuzhiyun * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
2268*4882a593Smuzhiyun * the segment payload in the first descriptor, and another 7 for the
2269*4882a593Smuzhiyun * fragments.
2270*4882a593Smuzhiyun */
__ice_chk_linearize(struct sk_buff * skb)2271*4882a593Smuzhiyun static bool __ice_chk_linearize(struct sk_buff *skb)
2272*4882a593Smuzhiyun {
2273*4882a593Smuzhiyun const skb_frag_t *frag, *stale;
2274*4882a593Smuzhiyun int nr_frags, sum;
2275*4882a593Smuzhiyun
2276*4882a593Smuzhiyun /* no need to check if number of frags is less than 7 */
2277*4882a593Smuzhiyun nr_frags = skb_shinfo(skb)->nr_frags;
2278*4882a593Smuzhiyun if (nr_frags < (ICE_MAX_BUF_TXD - 1))
2279*4882a593Smuzhiyun return false;
2280*4882a593Smuzhiyun
2281*4882a593Smuzhiyun /* We need to walk through the list and validate that each group
2282*4882a593Smuzhiyun * of 6 fragments totals at least gso_size.
2283*4882a593Smuzhiyun */
2284*4882a593Smuzhiyun nr_frags -= ICE_MAX_BUF_TXD - 2;
2285*4882a593Smuzhiyun frag = &skb_shinfo(skb)->frags[0];
2286*4882a593Smuzhiyun
2287*4882a593Smuzhiyun /* Initialize size to the negative value of gso_size minus 1. We
2288*4882a593Smuzhiyun * use this as the worst case scenario in which the frag ahead
2289*4882a593Smuzhiyun * of us only provides one byte which is why we are limited to 6
2290*4882a593Smuzhiyun * descriptors for a single transmit as the header and previous
2291*4882a593Smuzhiyun * fragment are already consuming 2 descriptors.
2292*4882a593Smuzhiyun */
2293*4882a593Smuzhiyun sum = 1 - skb_shinfo(skb)->gso_size;
2294*4882a593Smuzhiyun
2295*4882a593Smuzhiyun /* Add size of frags 0 through 4 to create our initial sum */
2296*4882a593Smuzhiyun sum += skb_frag_size(frag++);
2297*4882a593Smuzhiyun sum += skb_frag_size(frag++);
2298*4882a593Smuzhiyun sum += skb_frag_size(frag++);
2299*4882a593Smuzhiyun sum += skb_frag_size(frag++);
2300*4882a593Smuzhiyun sum += skb_frag_size(frag++);
2301*4882a593Smuzhiyun
2302*4882a593Smuzhiyun /* Walk through fragments adding latest fragment, testing it, and
2303*4882a593Smuzhiyun * then removing stale fragments from the sum.
2304*4882a593Smuzhiyun */
2305*4882a593Smuzhiyun for (stale = &skb_shinfo(skb)->frags[0];; stale++) {
2306*4882a593Smuzhiyun int stale_size = skb_frag_size(stale);
2307*4882a593Smuzhiyun
2308*4882a593Smuzhiyun sum += skb_frag_size(frag++);
2309*4882a593Smuzhiyun
2310*4882a593Smuzhiyun /* The stale fragment may present us with a smaller
2311*4882a593Smuzhiyun * descriptor than the actual fragment size. To account
2312*4882a593Smuzhiyun * for that we need to remove all the data on the front and
2313*4882a593Smuzhiyun * figure out what the remainder would be in the last
2314*4882a593Smuzhiyun * descriptor associated with the fragment.
2315*4882a593Smuzhiyun */
2316*4882a593Smuzhiyun if (stale_size > ICE_MAX_DATA_PER_TXD) {
2317*4882a593Smuzhiyun int align_pad = -(skb_frag_off(stale)) &
2318*4882a593Smuzhiyun (ICE_MAX_READ_REQ_SIZE - 1);
2319*4882a593Smuzhiyun
2320*4882a593Smuzhiyun sum -= align_pad;
2321*4882a593Smuzhiyun stale_size -= align_pad;
2322*4882a593Smuzhiyun
2323*4882a593Smuzhiyun do {
2324*4882a593Smuzhiyun sum -= ICE_MAX_DATA_PER_TXD_ALIGNED;
2325*4882a593Smuzhiyun stale_size -= ICE_MAX_DATA_PER_TXD_ALIGNED;
2326*4882a593Smuzhiyun } while (stale_size > ICE_MAX_DATA_PER_TXD);
2327*4882a593Smuzhiyun }
2328*4882a593Smuzhiyun
2329*4882a593Smuzhiyun /* if sum is negative we failed to make sufficient progress */
2330*4882a593Smuzhiyun if (sum < 0)
2331*4882a593Smuzhiyun return true;
2332*4882a593Smuzhiyun
2333*4882a593Smuzhiyun if (!nr_frags--)
2334*4882a593Smuzhiyun break;
2335*4882a593Smuzhiyun
2336*4882a593Smuzhiyun sum -= stale_size;
2337*4882a593Smuzhiyun }
2338*4882a593Smuzhiyun
2339*4882a593Smuzhiyun return false;
2340*4882a593Smuzhiyun }
2341*4882a593Smuzhiyun
2342*4882a593Smuzhiyun /**
2343*4882a593Smuzhiyun * ice_chk_linearize - Check if there are more than 8 fragments per packet
2344*4882a593Smuzhiyun * @skb: send buffer
2345*4882a593Smuzhiyun * @count: number of buffers used
2346*4882a593Smuzhiyun *
2347*4882a593Smuzhiyun * Note: Our HW can't scatter-gather more than 8 fragments to build
2348*4882a593Smuzhiyun * a packet on the wire and so we need to figure out the cases where we
2349*4882a593Smuzhiyun * need to linearize the skb.
2350*4882a593Smuzhiyun */
ice_chk_linearize(struct sk_buff * skb,unsigned int count)2351*4882a593Smuzhiyun static bool ice_chk_linearize(struct sk_buff *skb, unsigned int count)
2352*4882a593Smuzhiyun {
2353*4882a593Smuzhiyun /* Both TSO and single send will work if count is less than 8 */
2354*4882a593Smuzhiyun if (likely(count < ICE_MAX_BUF_TXD))
2355*4882a593Smuzhiyun return false;
2356*4882a593Smuzhiyun
2357*4882a593Smuzhiyun if (skb_is_gso(skb))
2358*4882a593Smuzhiyun return __ice_chk_linearize(skb);
2359*4882a593Smuzhiyun
2360*4882a593Smuzhiyun /* we can support up to 8 data buffers for a single send */
2361*4882a593Smuzhiyun return count != ICE_MAX_BUF_TXD;
2362*4882a593Smuzhiyun }
2363*4882a593Smuzhiyun
2364*4882a593Smuzhiyun /**
2365*4882a593Smuzhiyun * ice_xmit_frame_ring - Sends buffer on Tx ring
2366*4882a593Smuzhiyun * @skb: send buffer
2367*4882a593Smuzhiyun * @tx_ring: ring to send buffer on
2368*4882a593Smuzhiyun *
2369*4882a593Smuzhiyun * Returns NETDEV_TX_OK if sent, else an error code
2370*4882a593Smuzhiyun */
2371*4882a593Smuzhiyun static netdev_tx_t
ice_xmit_frame_ring(struct sk_buff * skb,struct ice_ring * tx_ring)2372*4882a593Smuzhiyun ice_xmit_frame_ring(struct sk_buff *skb, struct ice_ring *tx_ring)
2373*4882a593Smuzhiyun {
2374*4882a593Smuzhiyun struct ice_tx_offload_params offload = { 0 };
2375*4882a593Smuzhiyun struct ice_vsi *vsi = tx_ring->vsi;
2376*4882a593Smuzhiyun struct ice_tx_buf *first;
2377*4882a593Smuzhiyun struct ethhdr *eth;
2378*4882a593Smuzhiyun unsigned int count;
2379*4882a593Smuzhiyun int tso, csum;
2380*4882a593Smuzhiyun
2381*4882a593Smuzhiyun count = ice_xmit_desc_count(skb);
2382*4882a593Smuzhiyun if (ice_chk_linearize(skb, count)) {
2383*4882a593Smuzhiyun if (__skb_linearize(skb))
2384*4882a593Smuzhiyun goto out_drop;
2385*4882a593Smuzhiyun count = ice_txd_use_count(skb->len);
2386*4882a593Smuzhiyun tx_ring->tx_stats.tx_linearize++;
2387*4882a593Smuzhiyun }
2388*4882a593Smuzhiyun
2389*4882a593Smuzhiyun /* need: 1 descriptor per page * PAGE_SIZE/ICE_MAX_DATA_PER_TXD,
2390*4882a593Smuzhiyun * + 1 desc for skb_head_len/ICE_MAX_DATA_PER_TXD,
2391*4882a593Smuzhiyun * + 4 desc gap to avoid the cache line where head is,
2392*4882a593Smuzhiyun * + 1 desc for context descriptor,
2393*4882a593Smuzhiyun * otherwise try next time
2394*4882a593Smuzhiyun */
2395*4882a593Smuzhiyun if (ice_maybe_stop_tx(tx_ring, count + ICE_DESCS_PER_CACHE_LINE +
2396*4882a593Smuzhiyun ICE_DESCS_FOR_CTX_DESC)) {
2397*4882a593Smuzhiyun tx_ring->tx_stats.tx_busy++;
2398*4882a593Smuzhiyun return NETDEV_TX_BUSY;
2399*4882a593Smuzhiyun }
2400*4882a593Smuzhiyun
2401*4882a593Smuzhiyun offload.tx_ring = tx_ring;
2402*4882a593Smuzhiyun
2403*4882a593Smuzhiyun /* record the location of the first descriptor for this packet */
2404*4882a593Smuzhiyun first = &tx_ring->tx_buf[tx_ring->next_to_use];
2405*4882a593Smuzhiyun first->skb = skb;
2406*4882a593Smuzhiyun first->bytecount = max_t(unsigned int, skb->len, ETH_ZLEN);
2407*4882a593Smuzhiyun first->gso_segs = 1;
2408*4882a593Smuzhiyun first->tx_flags = 0;
2409*4882a593Smuzhiyun
2410*4882a593Smuzhiyun /* prepare the VLAN tagging flags for Tx */
2411*4882a593Smuzhiyun ice_tx_prepare_vlan_flags(tx_ring, first);
2412*4882a593Smuzhiyun
2413*4882a593Smuzhiyun /* set up TSO offload */
2414*4882a593Smuzhiyun tso = ice_tso(first, &offload);
2415*4882a593Smuzhiyun if (tso < 0)
2416*4882a593Smuzhiyun goto out_drop;
2417*4882a593Smuzhiyun
2418*4882a593Smuzhiyun /* always set up Tx checksum offload */
2419*4882a593Smuzhiyun csum = ice_tx_csum(first, &offload);
2420*4882a593Smuzhiyun if (csum < 0)
2421*4882a593Smuzhiyun goto out_drop;
2422*4882a593Smuzhiyun
2423*4882a593Smuzhiyun /* allow CONTROL frames egress from main VSI if FW LLDP disabled */
2424*4882a593Smuzhiyun eth = (struct ethhdr *)skb_mac_header(skb);
2425*4882a593Smuzhiyun if (unlikely((skb->priority == TC_PRIO_CONTROL ||
2426*4882a593Smuzhiyun eth->h_proto == htons(ETH_P_LLDP)) &&
2427*4882a593Smuzhiyun vsi->type == ICE_VSI_PF &&
2428*4882a593Smuzhiyun vsi->port_info->qos_cfg.is_sw_lldp))
2429*4882a593Smuzhiyun offload.cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX |
2430*4882a593Smuzhiyun ICE_TX_CTX_DESC_SWTCH_UPLINK <<
2431*4882a593Smuzhiyun ICE_TXD_CTX_QW1_CMD_S);
2432*4882a593Smuzhiyun
2433*4882a593Smuzhiyun if (offload.cd_qw1 & ICE_TX_DESC_DTYPE_CTX) {
2434*4882a593Smuzhiyun struct ice_tx_ctx_desc *cdesc;
2435*4882a593Smuzhiyun u16 i = tx_ring->next_to_use;
2436*4882a593Smuzhiyun
2437*4882a593Smuzhiyun /* grab the next descriptor */
2438*4882a593Smuzhiyun cdesc = ICE_TX_CTX_DESC(tx_ring, i);
2439*4882a593Smuzhiyun i++;
2440*4882a593Smuzhiyun tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2441*4882a593Smuzhiyun
2442*4882a593Smuzhiyun /* setup context descriptor */
2443*4882a593Smuzhiyun cdesc->tunneling_params = cpu_to_le32(offload.cd_tunnel_params);
2444*4882a593Smuzhiyun cdesc->l2tag2 = cpu_to_le16(offload.cd_l2tag2);
2445*4882a593Smuzhiyun cdesc->rsvd = cpu_to_le16(0);
2446*4882a593Smuzhiyun cdesc->qw1 = cpu_to_le64(offload.cd_qw1);
2447*4882a593Smuzhiyun }
2448*4882a593Smuzhiyun
2449*4882a593Smuzhiyun ice_tx_map(tx_ring, first, &offload);
2450*4882a593Smuzhiyun return NETDEV_TX_OK;
2451*4882a593Smuzhiyun
2452*4882a593Smuzhiyun out_drop:
2453*4882a593Smuzhiyun dev_kfree_skb_any(skb);
2454*4882a593Smuzhiyun return NETDEV_TX_OK;
2455*4882a593Smuzhiyun }
2456*4882a593Smuzhiyun
2457*4882a593Smuzhiyun /**
2458*4882a593Smuzhiyun * ice_start_xmit - Selects the correct VSI and Tx queue to send buffer
2459*4882a593Smuzhiyun * @skb: send buffer
2460*4882a593Smuzhiyun * @netdev: network interface device structure
2461*4882a593Smuzhiyun *
2462*4882a593Smuzhiyun * Returns NETDEV_TX_OK if sent, else an error code
2463*4882a593Smuzhiyun */
ice_start_xmit(struct sk_buff * skb,struct net_device * netdev)2464*4882a593Smuzhiyun netdev_tx_t ice_start_xmit(struct sk_buff *skb, struct net_device *netdev)
2465*4882a593Smuzhiyun {
2466*4882a593Smuzhiyun struct ice_netdev_priv *np = netdev_priv(netdev);
2467*4882a593Smuzhiyun struct ice_vsi *vsi = np->vsi;
2468*4882a593Smuzhiyun struct ice_ring *tx_ring;
2469*4882a593Smuzhiyun
2470*4882a593Smuzhiyun tx_ring = vsi->tx_rings[skb->queue_mapping];
2471*4882a593Smuzhiyun
2472*4882a593Smuzhiyun /* hardware can't handle really short frames, hardware padding works
2473*4882a593Smuzhiyun * beyond this point
2474*4882a593Smuzhiyun */
2475*4882a593Smuzhiyun if (skb_put_padto(skb, ICE_MIN_TX_LEN))
2476*4882a593Smuzhiyun return NETDEV_TX_OK;
2477*4882a593Smuzhiyun
2478*4882a593Smuzhiyun return ice_xmit_frame_ring(skb, tx_ring);
2479*4882a593Smuzhiyun }
2480*4882a593Smuzhiyun
2481*4882a593Smuzhiyun /**
2482*4882a593Smuzhiyun * ice_clean_ctrl_tx_irq - interrupt handler for flow director Tx queue
2483*4882a593Smuzhiyun * @tx_ring: tx_ring to clean
2484*4882a593Smuzhiyun */
ice_clean_ctrl_tx_irq(struct ice_ring * tx_ring)2485*4882a593Smuzhiyun void ice_clean_ctrl_tx_irq(struct ice_ring *tx_ring)
2486*4882a593Smuzhiyun {
2487*4882a593Smuzhiyun struct ice_vsi *vsi = tx_ring->vsi;
2488*4882a593Smuzhiyun s16 i = tx_ring->next_to_clean;
2489*4882a593Smuzhiyun int budget = ICE_DFLT_IRQ_WORK;
2490*4882a593Smuzhiyun struct ice_tx_desc *tx_desc;
2491*4882a593Smuzhiyun struct ice_tx_buf *tx_buf;
2492*4882a593Smuzhiyun
2493*4882a593Smuzhiyun tx_buf = &tx_ring->tx_buf[i];
2494*4882a593Smuzhiyun tx_desc = ICE_TX_DESC(tx_ring, i);
2495*4882a593Smuzhiyun i -= tx_ring->count;
2496*4882a593Smuzhiyun
2497*4882a593Smuzhiyun do {
2498*4882a593Smuzhiyun struct ice_tx_desc *eop_desc = tx_buf->next_to_watch;
2499*4882a593Smuzhiyun
2500*4882a593Smuzhiyun /* if next_to_watch is not set then there is no pending work */
2501*4882a593Smuzhiyun if (!eop_desc)
2502*4882a593Smuzhiyun break;
2503*4882a593Smuzhiyun
2504*4882a593Smuzhiyun /* prevent any other reads prior to eop_desc */
2505*4882a593Smuzhiyun smp_rmb();
2506*4882a593Smuzhiyun
2507*4882a593Smuzhiyun /* if the descriptor isn't done, no work to do */
2508*4882a593Smuzhiyun if (!(eop_desc->cmd_type_offset_bsz &
2509*4882a593Smuzhiyun cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)))
2510*4882a593Smuzhiyun break;
2511*4882a593Smuzhiyun
2512*4882a593Smuzhiyun /* clear next_to_watch to prevent false hangs */
2513*4882a593Smuzhiyun tx_buf->next_to_watch = NULL;
2514*4882a593Smuzhiyun tx_desc->buf_addr = 0;
2515*4882a593Smuzhiyun tx_desc->cmd_type_offset_bsz = 0;
2516*4882a593Smuzhiyun
2517*4882a593Smuzhiyun /* move past filter desc */
2518*4882a593Smuzhiyun tx_buf++;
2519*4882a593Smuzhiyun tx_desc++;
2520*4882a593Smuzhiyun i++;
2521*4882a593Smuzhiyun if (unlikely(!i)) {
2522*4882a593Smuzhiyun i -= tx_ring->count;
2523*4882a593Smuzhiyun tx_buf = tx_ring->tx_buf;
2524*4882a593Smuzhiyun tx_desc = ICE_TX_DESC(tx_ring, 0);
2525*4882a593Smuzhiyun }
2526*4882a593Smuzhiyun
2527*4882a593Smuzhiyun /* unmap the data header */
2528*4882a593Smuzhiyun if (dma_unmap_len(tx_buf, len))
2529*4882a593Smuzhiyun dma_unmap_single(tx_ring->dev,
2530*4882a593Smuzhiyun dma_unmap_addr(tx_buf, dma),
2531*4882a593Smuzhiyun dma_unmap_len(tx_buf, len),
2532*4882a593Smuzhiyun DMA_TO_DEVICE);
2533*4882a593Smuzhiyun if (tx_buf->tx_flags & ICE_TX_FLAGS_DUMMY_PKT)
2534*4882a593Smuzhiyun devm_kfree(tx_ring->dev, tx_buf->raw_buf);
2535*4882a593Smuzhiyun
2536*4882a593Smuzhiyun /* clear next_to_watch to prevent false hangs */
2537*4882a593Smuzhiyun tx_buf->raw_buf = NULL;
2538*4882a593Smuzhiyun tx_buf->tx_flags = 0;
2539*4882a593Smuzhiyun tx_buf->next_to_watch = NULL;
2540*4882a593Smuzhiyun dma_unmap_len_set(tx_buf, len, 0);
2541*4882a593Smuzhiyun tx_desc->buf_addr = 0;
2542*4882a593Smuzhiyun tx_desc->cmd_type_offset_bsz = 0;
2543*4882a593Smuzhiyun
2544*4882a593Smuzhiyun /* move past eop_desc for start of next FD desc */
2545*4882a593Smuzhiyun tx_buf++;
2546*4882a593Smuzhiyun tx_desc++;
2547*4882a593Smuzhiyun i++;
2548*4882a593Smuzhiyun if (unlikely(!i)) {
2549*4882a593Smuzhiyun i -= tx_ring->count;
2550*4882a593Smuzhiyun tx_buf = tx_ring->tx_buf;
2551*4882a593Smuzhiyun tx_desc = ICE_TX_DESC(tx_ring, 0);
2552*4882a593Smuzhiyun }
2553*4882a593Smuzhiyun
2554*4882a593Smuzhiyun budget--;
2555*4882a593Smuzhiyun } while (likely(budget));
2556*4882a593Smuzhiyun
2557*4882a593Smuzhiyun i += tx_ring->count;
2558*4882a593Smuzhiyun tx_ring->next_to_clean = i;
2559*4882a593Smuzhiyun
2560*4882a593Smuzhiyun /* re-enable interrupt if needed */
2561*4882a593Smuzhiyun ice_irq_dynamic_ena(&vsi->back->hw, vsi, vsi->q_vectors[0]);
2562*4882a593Smuzhiyun }
2563