1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright(c) 2013 - 2018 Intel Corporation. */
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include <linux/prefetch.h>
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include "iavf.h"
7*4882a593Smuzhiyun #include "iavf_trace.h"
8*4882a593Smuzhiyun #include "iavf_prototype.h"
9*4882a593Smuzhiyun
build_ctob(u32 td_cmd,u32 td_offset,unsigned int size,u32 td_tag)10*4882a593Smuzhiyun static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
11*4882a593Smuzhiyun u32 td_tag)
12*4882a593Smuzhiyun {
13*4882a593Smuzhiyun return cpu_to_le64(IAVF_TX_DESC_DTYPE_DATA |
14*4882a593Smuzhiyun ((u64)td_cmd << IAVF_TXD_QW1_CMD_SHIFT) |
15*4882a593Smuzhiyun ((u64)td_offset << IAVF_TXD_QW1_OFFSET_SHIFT) |
16*4882a593Smuzhiyun ((u64)size << IAVF_TXD_QW1_TX_BUF_SZ_SHIFT) |
17*4882a593Smuzhiyun ((u64)td_tag << IAVF_TXD_QW1_L2TAG1_SHIFT));
18*4882a593Smuzhiyun }
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #define IAVF_TXD_CMD (IAVF_TX_DESC_CMD_EOP | IAVF_TX_DESC_CMD_RS)
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /**
23*4882a593Smuzhiyun * iavf_unmap_and_free_tx_resource - Release a Tx buffer
24*4882a593Smuzhiyun * @ring: the ring that owns the buffer
25*4882a593Smuzhiyun * @tx_buffer: the buffer to free
26*4882a593Smuzhiyun **/
iavf_unmap_and_free_tx_resource(struct iavf_ring * ring,struct iavf_tx_buffer * tx_buffer)27*4882a593Smuzhiyun static void iavf_unmap_and_free_tx_resource(struct iavf_ring *ring,
28*4882a593Smuzhiyun struct iavf_tx_buffer *tx_buffer)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun if (tx_buffer->skb) {
31*4882a593Smuzhiyun if (tx_buffer->tx_flags & IAVF_TX_FLAGS_FD_SB)
32*4882a593Smuzhiyun kfree(tx_buffer->raw_buf);
33*4882a593Smuzhiyun else
34*4882a593Smuzhiyun dev_kfree_skb_any(tx_buffer->skb);
35*4882a593Smuzhiyun if (dma_unmap_len(tx_buffer, len))
36*4882a593Smuzhiyun dma_unmap_single(ring->dev,
37*4882a593Smuzhiyun dma_unmap_addr(tx_buffer, dma),
38*4882a593Smuzhiyun dma_unmap_len(tx_buffer, len),
39*4882a593Smuzhiyun DMA_TO_DEVICE);
40*4882a593Smuzhiyun } else if (dma_unmap_len(tx_buffer, len)) {
41*4882a593Smuzhiyun dma_unmap_page(ring->dev,
42*4882a593Smuzhiyun dma_unmap_addr(tx_buffer, dma),
43*4882a593Smuzhiyun dma_unmap_len(tx_buffer, len),
44*4882a593Smuzhiyun DMA_TO_DEVICE);
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun tx_buffer->next_to_watch = NULL;
48*4882a593Smuzhiyun tx_buffer->skb = NULL;
49*4882a593Smuzhiyun dma_unmap_len_set(tx_buffer, len, 0);
50*4882a593Smuzhiyun /* tx_buffer must be completely set up in the transmit path */
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun * iavf_clean_tx_ring - Free any empty Tx buffers
55*4882a593Smuzhiyun * @tx_ring: ring to be cleaned
56*4882a593Smuzhiyun **/
iavf_clean_tx_ring(struct iavf_ring * tx_ring)57*4882a593Smuzhiyun void iavf_clean_tx_ring(struct iavf_ring *tx_ring)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun unsigned long bi_size;
60*4882a593Smuzhiyun u16 i;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* ring already cleared, nothing to do */
63*4882a593Smuzhiyun if (!tx_ring->tx_bi)
64*4882a593Smuzhiyun return;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* Free all the Tx ring sk_buffs */
67*4882a593Smuzhiyun for (i = 0; i < tx_ring->count; i++)
68*4882a593Smuzhiyun iavf_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun bi_size = sizeof(struct iavf_tx_buffer) * tx_ring->count;
71*4882a593Smuzhiyun memset(tx_ring->tx_bi, 0, bi_size);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* Zero out the descriptor ring */
74*4882a593Smuzhiyun memset(tx_ring->desc, 0, tx_ring->size);
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun tx_ring->next_to_use = 0;
77*4882a593Smuzhiyun tx_ring->next_to_clean = 0;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun if (!tx_ring->netdev)
80*4882a593Smuzhiyun return;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /* cleanup Tx queue statistics */
83*4882a593Smuzhiyun netdev_tx_reset_queue(txring_txq(tx_ring));
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /**
87*4882a593Smuzhiyun * iavf_free_tx_resources - Free Tx resources per queue
88*4882a593Smuzhiyun * @tx_ring: Tx descriptor ring for a specific queue
89*4882a593Smuzhiyun *
90*4882a593Smuzhiyun * Free all transmit software resources
91*4882a593Smuzhiyun **/
iavf_free_tx_resources(struct iavf_ring * tx_ring)92*4882a593Smuzhiyun void iavf_free_tx_resources(struct iavf_ring *tx_ring)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun iavf_clean_tx_ring(tx_ring);
95*4882a593Smuzhiyun kfree(tx_ring->tx_bi);
96*4882a593Smuzhiyun tx_ring->tx_bi = NULL;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun if (tx_ring->desc) {
99*4882a593Smuzhiyun dma_free_coherent(tx_ring->dev, tx_ring->size,
100*4882a593Smuzhiyun tx_ring->desc, tx_ring->dma);
101*4882a593Smuzhiyun tx_ring->desc = NULL;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /**
106*4882a593Smuzhiyun * iavf_get_tx_pending - how many Tx descriptors not processed
107*4882a593Smuzhiyun * @ring: the ring of descriptors
108*4882a593Smuzhiyun * @in_sw: is tx_pending being checked in SW or HW
109*4882a593Smuzhiyun *
110*4882a593Smuzhiyun * Since there is no access to the ring head register
111*4882a593Smuzhiyun * in XL710, we need to use our local copies
112*4882a593Smuzhiyun **/
iavf_get_tx_pending(struct iavf_ring * ring,bool in_sw)113*4882a593Smuzhiyun u32 iavf_get_tx_pending(struct iavf_ring *ring, bool in_sw)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun u32 head, tail;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /* underlying hardware might not allow access and/or always return
118*4882a593Smuzhiyun * 0 for the head/tail registers so just use the cached values
119*4882a593Smuzhiyun */
120*4882a593Smuzhiyun head = ring->next_to_clean;
121*4882a593Smuzhiyun tail = ring->next_to_use;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun if (head != tail)
124*4882a593Smuzhiyun return (head < tail) ?
125*4882a593Smuzhiyun tail - head : (tail + ring->count - head);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun return 0;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun /**
131*4882a593Smuzhiyun * iavf_detect_recover_hung - Function to detect and recover hung_queues
132*4882a593Smuzhiyun * @vsi: pointer to vsi struct with tx queues
133*4882a593Smuzhiyun *
134*4882a593Smuzhiyun * VSI has netdev and netdev has TX queues. This function is to check each of
135*4882a593Smuzhiyun * those TX queues if they are hung, trigger recovery by issuing SW interrupt.
136*4882a593Smuzhiyun **/
iavf_detect_recover_hung(struct iavf_vsi * vsi)137*4882a593Smuzhiyun void iavf_detect_recover_hung(struct iavf_vsi *vsi)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun struct iavf_ring *tx_ring = NULL;
140*4882a593Smuzhiyun struct net_device *netdev;
141*4882a593Smuzhiyun unsigned int i;
142*4882a593Smuzhiyun int packets;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun if (!vsi)
145*4882a593Smuzhiyun return;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun if (test_bit(__IAVF_VSI_DOWN, vsi->state))
148*4882a593Smuzhiyun return;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun netdev = vsi->netdev;
151*4882a593Smuzhiyun if (!netdev)
152*4882a593Smuzhiyun return;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun if (!netif_carrier_ok(netdev))
155*4882a593Smuzhiyun return;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun for (i = 0; i < vsi->back->num_active_queues; i++) {
158*4882a593Smuzhiyun tx_ring = &vsi->back->tx_rings[i];
159*4882a593Smuzhiyun if (tx_ring && tx_ring->desc) {
160*4882a593Smuzhiyun /* If packet counter has not changed the queue is
161*4882a593Smuzhiyun * likely stalled, so force an interrupt for this
162*4882a593Smuzhiyun * queue.
163*4882a593Smuzhiyun *
164*4882a593Smuzhiyun * prev_pkt_ctr would be negative if there was no
165*4882a593Smuzhiyun * pending work.
166*4882a593Smuzhiyun */
167*4882a593Smuzhiyun packets = tx_ring->stats.packets & INT_MAX;
168*4882a593Smuzhiyun if (tx_ring->tx_stats.prev_pkt_ctr == packets) {
169*4882a593Smuzhiyun iavf_force_wb(vsi, tx_ring->q_vector);
170*4882a593Smuzhiyun continue;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /* Memory barrier between read of packet count and call
174*4882a593Smuzhiyun * to iavf_get_tx_pending()
175*4882a593Smuzhiyun */
176*4882a593Smuzhiyun smp_rmb();
177*4882a593Smuzhiyun tx_ring->tx_stats.prev_pkt_ctr =
178*4882a593Smuzhiyun iavf_get_tx_pending(tx_ring, true) ? packets : -1;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun #define WB_STRIDE 4
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /**
186*4882a593Smuzhiyun * iavf_clean_tx_irq - Reclaim resources after transmit completes
187*4882a593Smuzhiyun * @vsi: the VSI we care about
188*4882a593Smuzhiyun * @tx_ring: Tx ring to clean
189*4882a593Smuzhiyun * @napi_budget: Used to determine if we are in netpoll
190*4882a593Smuzhiyun *
191*4882a593Smuzhiyun * Returns true if there's any budget left (e.g. the clean is finished)
192*4882a593Smuzhiyun **/
iavf_clean_tx_irq(struct iavf_vsi * vsi,struct iavf_ring * tx_ring,int napi_budget)193*4882a593Smuzhiyun static bool iavf_clean_tx_irq(struct iavf_vsi *vsi,
194*4882a593Smuzhiyun struct iavf_ring *tx_ring, int napi_budget)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun int i = tx_ring->next_to_clean;
197*4882a593Smuzhiyun struct iavf_tx_buffer *tx_buf;
198*4882a593Smuzhiyun struct iavf_tx_desc *tx_desc;
199*4882a593Smuzhiyun unsigned int total_bytes = 0, total_packets = 0;
200*4882a593Smuzhiyun unsigned int budget = vsi->work_limit;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun tx_buf = &tx_ring->tx_bi[i];
203*4882a593Smuzhiyun tx_desc = IAVF_TX_DESC(tx_ring, i);
204*4882a593Smuzhiyun i -= tx_ring->count;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun do {
207*4882a593Smuzhiyun struct iavf_tx_desc *eop_desc = tx_buf->next_to_watch;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /* if next_to_watch is not set then there is no work pending */
210*4882a593Smuzhiyun if (!eop_desc)
211*4882a593Smuzhiyun break;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun /* prevent any other reads prior to eop_desc */
214*4882a593Smuzhiyun smp_rmb();
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun iavf_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf);
217*4882a593Smuzhiyun /* if the descriptor isn't done, no work yet to do */
218*4882a593Smuzhiyun if (!(eop_desc->cmd_type_offset_bsz &
219*4882a593Smuzhiyun cpu_to_le64(IAVF_TX_DESC_DTYPE_DESC_DONE)))
220*4882a593Smuzhiyun break;
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /* clear next_to_watch to prevent false hangs */
223*4882a593Smuzhiyun tx_buf->next_to_watch = NULL;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /* update the statistics for this packet */
226*4882a593Smuzhiyun total_bytes += tx_buf->bytecount;
227*4882a593Smuzhiyun total_packets += tx_buf->gso_segs;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun /* free the skb */
230*4882a593Smuzhiyun napi_consume_skb(tx_buf->skb, napi_budget);
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun /* unmap skb header data */
233*4882a593Smuzhiyun dma_unmap_single(tx_ring->dev,
234*4882a593Smuzhiyun dma_unmap_addr(tx_buf, dma),
235*4882a593Smuzhiyun dma_unmap_len(tx_buf, len),
236*4882a593Smuzhiyun DMA_TO_DEVICE);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /* clear tx_buffer data */
239*4882a593Smuzhiyun tx_buf->skb = NULL;
240*4882a593Smuzhiyun dma_unmap_len_set(tx_buf, len, 0);
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /* unmap remaining buffers */
243*4882a593Smuzhiyun while (tx_desc != eop_desc) {
244*4882a593Smuzhiyun iavf_trace(clean_tx_irq_unmap,
245*4882a593Smuzhiyun tx_ring, tx_desc, tx_buf);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun tx_buf++;
248*4882a593Smuzhiyun tx_desc++;
249*4882a593Smuzhiyun i++;
250*4882a593Smuzhiyun if (unlikely(!i)) {
251*4882a593Smuzhiyun i -= tx_ring->count;
252*4882a593Smuzhiyun tx_buf = tx_ring->tx_bi;
253*4882a593Smuzhiyun tx_desc = IAVF_TX_DESC(tx_ring, 0);
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun /* unmap any remaining paged data */
257*4882a593Smuzhiyun if (dma_unmap_len(tx_buf, len)) {
258*4882a593Smuzhiyun dma_unmap_page(tx_ring->dev,
259*4882a593Smuzhiyun dma_unmap_addr(tx_buf, dma),
260*4882a593Smuzhiyun dma_unmap_len(tx_buf, len),
261*4882a593Smuzhiyun DMA_TO_DEVICE);
262*4882a593Smuzhiyun dma_unmap_len_set(tx_buf, len, 0);
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun /* move us one more past the eop_desc for start of next pkt */
267*4882a593Smuzhiyun tx_buf++;
268*4882a593Smuzhiyun tx_desc++;
269*4882a593Smuzhiyun i++;
270*4882a593Smuzhiyun if (unlikely(!i)) {
271*4882a593Smuzhiyun i -= tx_ring->count;
272*4882a593Smuzhiyun tx_buf = tx_ring->tx_bi;
273*4882a593Smuzhiyun tx_desc = IAVF_TX_DESC(tx_ring, 0);
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun prefetch(tx_desc);
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun /* update budget accounting */
279*4882a593Smuzhiyun budget--;
280*4882a593Smuzhiyun } while (likely(budget));
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun i += tx_ring->count;
283*4882a593Smuzhiyun tx_ring->next_to_clean = i;
284*4882a593Smuzhiyun u64_stats_update_begin(&tx_ring->syncp);
285*4882a593Smuzhiyun tx_ring->stats.bytes += total_bytes;
286*4882a593Smuzhiyun tx_ring->stats.packets += total_packets;
287*4882a593Smuzhiyun u64_stats_update_end(&tx_ring->syncp);
288*4882a593Smuzhiyun tx_ring->q_vector->tx.total_bytes += total_bytes;
289*4882a593Smuzhiyun tx_ring->q_vector->tx.total_packets += total_packets;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun if (tx_ring->flags & IAVF_TXR_FLAGS_WB_ON_ITR) {
292*4882a593Smuzhiyun /* check to see if there are < 4 descriptors
293*4882a593Smuzhiyun * waiting to be written back, then kick the hardware to force
294*4882a593Smuzhiyun * them to be written back in case we stay in NAPI.
295*4882a593Smuzhiyun * In this mode on X722 we do not enable Interrupt.
296*4882a593Smuzhiyun */
297*4882a593Smuzhiyun unsigned int j = iavf_get_tx_pending(tx_ring, false);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun if (budget &&
300*4882a593Smuzhiyun ((j / WB_STRIDE) == 0) && (j > 0) &&
301*4882a593Smuzhiyun !test_bit(__IAVF_VSI_DOWN, vsi->state) &&
302*4882a593Smuzhiyun (IAVF_DESC_UNUSED(tx_ring) != tx_ring->count))
303*4882a593Smuzhiyun tx_ring->arm_wb = true;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /* notify netdev of completed buffers */
307*4882a593Smuzhiyun netdev_tx_completed_queue(txring_txq(tx_ring),
308*4882a593Smuzhiyun total_packets, total_bytes);
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun #define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2))
311*4882a593Smuzhiyun if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
312*4882a593Smuzhiyun (IAVF_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
313*4882a593Smuzhiyun /* Make sure that anybody stopping the queue after this
314*4882a593Smuzhiyun * sees the new next_to_clean.
315*4882a593Smuzhiyun */
316*4882a593Smuzhiyun smp_mb();
317*4882a593Smuzhiyun if (__netif_subqueue_stopped(tx_ring->netdev,
318*4882a593Smuzhiyun tx_ring->queue_index) &&
319*4882a593Smuzhiyun !test_bit(__IAVF_VSI_DOWN, vsi->state)) {
320*4882a593Smuzhiyun netif_wake_subqueue(tx_ring->netdev,
321*4882a593Smuzhiyun tx_ring->queue_index);
322*4882a593Smuzhiyun ++tx_ring->tx_stats.restart_queue;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun return !!budget;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun /**
330*4882a593Smuzhiyun * iavf_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
331*4882a593Smuzhiyun * @vsi: the VSI we care about
332*4882a593Smuzhiyun * @q_vector: the vector on which to enable writeback
333*4882a593Smuzhiyun *
334*4882a593Smuzhiyun **/
iavf_enable_wb_on_itr(struct iavf_vsi * vsi,struct iavf_q_vector * q_vector)335*4882a593Smuzhiyun static void iavf_enable_wb_on_itr(struct iavf_vsi *vsi,
336*4882a593Smuzhiyun struct iavf_q_vector *q_vector)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun u16 flags = q_vector->tx.ring[0].flags;
339*4882a593Smuzhiyun u32 val;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun if (!(flags & IAVF_TXR_FLAGS_WB_ON_ITR))
342*4882a593Smuzhiyun return;
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun if (q_vector->arm_wb_state)
345*4882a593Smuzhiyun return;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun val = IAVF_VFINT_DYN_CTLN1_WB_ON_ITR_MASK |
348*4882a593Smuzhiyun IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK; /* set noitr */
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun wr32(&vsi->back->hw,
351*4882a593Smuzhiyun IAVF_VFINT_DYN_CTLN1(q_vector->reg_idx), val);
352*4882a593Smuzhiyun q_vector->arm_wb_state = true;
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun /**
356*4882a593Smuzhiyun * iavf_force_wb - Issue SW Interrupt so HW does a wb
357*4882a593Smuzhiyun * @vsi: the VSI we care about
358*4882a593Smuzhiyun * @q_vector: the vector on which to force writeback
359*4882a593Smuzhiyun *
360*4882a593Smuzhiyun **/
iavf_force_wb(struct iavf_vsi * vsi,struct iavf_q_vector * q_vector)361*4882a593Smuzhiyun void iavf_force_wb(struct iavf_vsi *vsi, struct iavf_q_vector *q_vector)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun u32 val = IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
364*4882a593Smuzhiyun IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK | /* set noitr */
365*4882a593Smuzhiyun IAVF_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
366*4882a593Smuzhiyun IAVF_VFINT_DYN_CTLN1_SW_ITR_INDX_ENA_MASK
367*4882a593Smuzhiyun /* allow 00 to be written to the index */;
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun wr32(&vsi->back->hw,
370*4882a593Smuzhiyun IAVF_VFINT_DYN_CTLN1(q_vector->reg_idx),
371*4882a593Smuzhiyun val);
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun
iavf_container_is_rx(struct iavf_q_vector * q_vector,struct iavf_ring_container * rc)374*4882a593Smuzhiyun static inline bool iavf_container_is_rx(struct iavf_q_vector *q_vector,
375*4882a593Smuzhiyun struct iavf_ring_container *rc)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun return &q_vector->rx == rc;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun
iavf_itr_divisor(struct iavf_q_vector * q_vector)380*4882a593Smuzhiyun static inline unsigned int iavf_itr_divisor(struct iavf_q_vector *q_vector)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun unsigned int divisor;
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun switch (q_vector->adapter->link_speed) {
385*4882a593Smuzhiyun case VIRTCHNL_LINK_SPEED_40GB:
386*4882a593Smuzhiyun divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 1024;
387*4882a593Smuzhiyun break;
388*4882a593Smuzhiyun case VIRTCHNL_LINK_SPEED_25GB:
389*4882a593Smuzhiyun case VIRTCHNL_LINK_SPEED_20GB:
390*4882a593Smuzhiyun divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 512;
391*4882a593Smuzhiyun break;
392*4882a593Smuzhiyun default:
393*4882a593Smuzhiyun case VIRTCHNL_LINK_SPEED_10GB:
394*4882a593Smuzhiyun divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 256;
395*4882a593Smuzhiyun break;
396*4882a593Smuzhiyun case VIRTCHNL_LINK_SPEED_1GB:
397*4882a593Smuzhiyun case VIRTCHNL_LINK_SPEED_100MB:
398*4882a593Smuzhiyun divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 32;
399*4882a593Smuzhiyun break;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun return divisor;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun /**
406*4882a593Smuzhiyun * iavf_update_itr - update the dynamic ITR value based on statistics
407*4882a593Smuzhiyun * @q_vector: structure containing interrupt and ring information
408*4882a593Smuzhiyun * @rc: structure containing ring performance data
409*4882a593Smuzhiyun *
410*4882a593Smuzhiyun * Stores a new ITR value based on packets and byte
411*4882a593Smuzhiyun * counts during the last interrupt. The advantage of per interrupt
412*4882a593Smuzhiyun * computation is faster updates and more accurate ITR for the current
413*4882a593Smuzhiyun * traffic pattern. Constants in this function were computed
414*4882a593Smuzhiyun * based on theoretical maximum wire speed and thresholds were set based
415*4882a593Smuzhiyun * on testing data as well as attempting to minimize response time
416*4882a593Smuzhiyun * while increasing bulk throughput.
417*4882a593Smuzhiyun **/
iavf_update_itr(struct iavf_q_vector * q_vector,struct iavf_ring_container * rc)418*4882a593Smuzhiyun static void iavf_update_itr(struct iavf_q_vector *q_vector,
419*4882a593Smuzhiyun struct iavf_ring_container *rc)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun unsigned int avg_wire_size, packets, bytes, itr;
422*4882a593Smuzhiyun unsigned long next_update = jiffies;
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun /* If we don't have any rings just leave ourselves set for maximum
425*4882a593Smuzhiyun * possible latency so we take ourselves out of the equation.
426*4882a593Smuzhiyun */
427*4882a593Smuzhiyun if (!rc->ring || !ITR_IS_DYNAMIC(rc->ring->itr_setting))
428*4882a593Smuzhiyun return;
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun /* For Rx we want to push the delay up and default to low latency.
431*4882a593Smuzhiyun * for Tx we want to pull the delay down and default to high latency.
432*4882a593Smuzhiyun */
433*4882a593Smuzhiyun itr = iavf_container_is_rx(q_vector, rc) ?
434*4882a593Smuzhiyun IAVF_ITR_ADAPTIVE_MIN_USECS | IAVF_ITR_ADAPTIVE_LATENCY :
435*4882a593Smuzhiyun IAVF_ITR_ADAPTIVE_MAX_USECS | IAVF_ITR_ADAPTIVE_LATENCY;
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun /* If we didn't update within up to 1 - 2 jiffies we can assume
438*4882a593Smuzhiyun * that either packets are coming in so slow there hasn't been
439*4882a593Smuzhiyun * any work, or that there is so much work that NAPI is dealing
440*4882a593Smuzhiyun * with interrupt moderation and we don't need to do anything.
441*4882a593Smuzhiyun */
442*4882a593Smuzhiyun if (time_after(next_update, rc->next_update))
443*4882a593Smuzhiyun goto clear_counts;
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun /* If itr_countdown is set it means we programmed an ITR within
446*4882a593Smuzhiyun * the last 4 interrupt cycles. This has a side effect of us
447*4882a593Smuzhiyun * potentially firing an early interrupt. In order to work around
448*4882a593Smuzhiyun * this we need to throw out any data received for a few
449*4882a593Smuzhiyun * interrupts following the update.
450*4882a593Smuzhiyun */
451*4882a593Smuzhiyun if (q_vector->itr_countdown) {
452*4882a593Smuzhiyun itr = rc->target_itr;
453*4882a593Smuzhiyun goto clear_counts;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun packets = rc->total_packets;
457*4882a593Smuzhiyun bytes = rc->total_bytes;
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun if (iavf_container_is_rx(q_vector, rc)) {
460*4882a593Smuzhiyun /* If Rx there are 1 to 4 packets and bytes are less than
461*4882a593Smuzhiyun * 9000 assume insufficient data to use bulk rate limiting
462*4882a593Smuzhiyun * approach unless Tx is already in bulk rate limiting. We
463*4882a593Smuzhiyun * are likely latency driven.
464*4882a593Smuzhiyun */
465*4882a593Smuzhiyun if (packets && packets < 4 && bytes < 9000 &&
466*4882a593Smuzhiyun (q_vector->tx.target_itr & IAVF_ITR_ADAPTIVE_LATENCY)) {
467*4882a593Smuzhiyun itr = IAVF_ITR_ADAPTIVE_LATENCY;
468*4882a593Smuzhiyun goto adjust_by_size;
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun } else if (packets < 4) {
471*4882a593Smuzhiyun /* If we have Tx and Rx ITR maxed and Tx ITR is running in
472*4882a593Smuzhiyun * bulk mode and we are receiving 4 or fewer packets just
473*4882a593Smuzhiyun * reset the ITR_ADAPTIVE_LATENCY bit for latency mode so
474*4882a593Smuzhiyun * that the Rx can relax.
475*4882a593Smuzhiyun */
476*4882a593Smuzhiyun if (rc->target_itr == IAVF_ITR_ADAPTIVE_MAX_USECS &&
477*4882a593Smuzhiyun (q_vector->rx.target_itr & IAVF_ITR_MASK) ==
478*4882a593Smuzhiyun IAVF_ITR_ADAPTIVE_MAX_USECS)
479*4882a593Smuzhiyun goto clear_counts;
480*4882a593Smuzhiyun } else if (packets > 32) {
481*4882a593Smuzhiyun /* If we have processed over 32 packets in a single interrupt
482*4882a593Smuzhiyun * for Tx assume we need to switch over to "bulk" mode.
483*4882a593Smuzhiyun */
484*4882a593Smuzhiyun rc->target_itr &= ~IAVF_ITR_ADAPTIVE_LATENCY;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun /* We have no packets to actually measure against. This means
488*4882a593Smuzhiyun * either one of the other queues on this vector is active or
489*4882a593Smuzhiyun * we are a Tx queue doing TSO with too high of an interrupt rate.
490*4882a593Smuzhiyun *
491*4882a593Smuzhiyun * Between 4 and 56 we can assume that our current interrupt delay
492*4882a593Smuzhiyun * is only slightly too low. As such we should increase it by a small
493*4882a593Smuzhiyun * fixed amount.
494*4882a593Smuzhiyun */
495*4882a593Smuzhiyun if (packets < 56) {
496*4882a593Smuzhiyun itr = rc->target_itr + IAVF_ITR_ADAPTIVE_MIN_INC;
497*4882a593Smuzhiyun if ((itr & IAVF_ITR_MASK) > IAVF_ITR_ADAPTIVE_MAX_USECS) {
498*4882a593Smuzhiyun itr &= IAVF_ITR_ADAPTIVE_LATENCY;
499*4882a593Smuzhiyun itr += IAVF_ITR_ADAPTIVE_MAX_USECS;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun goto clear_counts;
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun if (packets <= 256) {
505*4882a593Smuzhiyun itr = min(q_vector->tx.current_itr, q_vector->rx.current_itr);
506*4882a593Smuzhiyun itr &= IAVF_ITR_MASK;
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun /* Between 56 and 112 is our "goldilocks" zone where we are
509*4882a593Smuzhiyun * working out "just right". Just report that our current
510*4882a593Smuzhiyun * ITR is good for us.
511*4882a593Smuzhiyun */
512*4882a593Smuzhiyun if (packets <= 112)
513*4882a593Smuzhiyun goto clear_counts;
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun /* If packet count is 128 or greater we are likely looking
516*4882a593Smuzhiyun * at a slight overrun of the delay we want. Try halving
517*4882a593Smuzhiyun * our delay to see if that will cut the number of packets
518*4882a593Smuzhiyun * in half per interrupt.
519*4882a593Smuzhiyun */
520*4882a593Smuzhiyun itr /= 2;
521*4882a593Smuzhiyun itr &= IAVF_ITR_MASK;
522*4882a593Smuzhiyun if (itr < IAVF_ITR_ADAPTIVE_MIN_USECS)
523*4882a593Smuzhiyun itr = IAVF_ITR_ADAPTIVE_MIN_USECS;
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun goto clear_counts;
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun /* The paths below assume we are dealing with a bulk ITR since
529*4882a593Smuzhiyun * number of packets is greater than 256. We are just going to have
530*4882a593Smuzhiyun * to compute a value and try to bring the count under control,
531*4882a593Smuzhiyun * though for smaller packet sizes there isn't much we can do as
532*4882a593Smuzhiyun * NAPI polling will likely be kicking in sooner rather than later.
533*4882a593Smuzhiyun */
534*4882a593Smuzhiyun itr = IAVF_ITR_ADAPTIVE_BULK;
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun adjust_by_size:
537*4882a593Smuzhiyun /* If packet counts are 256 or greater we can assume we have a gross
538*4882a593Smuzhiyun * overestimation of what the rate should be. Instead of trying to fine
539*4882a593Smuzhiyun * tune it just use the formula below to try and dial in an exact value
540*4882a593Smuzhiyun * give the current packet size of the frame.
541*4882a593Smuzhiyun */
542*4882a593Smuzhiyun avg_wire_size = bytes / packets;
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun /* The following is a crude approximation of:
545*4882a593Smuzhiyun * wmem_default / (size + overhead) = desired_pkts_per_int
546*4882a593Smuzhiyun * rate / bits_per_byte / (size + ethernet overhead) = pkt_rate
547*4882a593Smuzhiyun * (desired_pkt_rate / pkt_rate) * usecs_per_sec = ITR value
548*4882a593Smuzhiyun *
549*4882a593Smuzhiyun * Assuming wmem_default is 212992 and overhead is 640 bytes per
550*4882a593Smuzhiyun * packet, (256 skb, 64 headroom, 320 shared info), we can reduce the
551*4882a593Smuzhiyun * formula down to
552*4882a593Smuzhiyun *
553*4882a593Smuzhiyun * (170 * (size + 24)) / (size + 640) = ITR
554*4882a593Smuzhiyun *
555*4882a593Smuzhiyun * We first do some math on the packet size and then finally bitshift
556*4882a593Smuzhiyun * by 8 after rounding up. We also have to account for PCIe link speed
557*4882a593Smuzhiyun * difference as ITR scales based on this.
558*4882a593Smuzhiyun */
559*4882a593Smuzhiyun if (avg_wire_size <= 60) {
560*4882a593Smuzhiyun /* Start at 250k ints/sec */
561*4882a593Smuzhiyun avg_wire_size = 4096;
562*4882a593Smuzhiyun } else if (avg_wire_size <= 380) {
563*4882a593Smuzhiyun /* 250K ints/sec to 60K ints/sec */
564*4882a593Smuzhiyun avg_wire_size *= 40;
565*4882a593Smuzhiyun avg_wire_size += 1696;
566*4882a593Smuzhiyun } else if (avg_wire_size <= 1084) {
567*4882a593Smuzhiyun /* 60K ints/sec to 36K ints/sec */
568*4882a593Smuzhiyun avg_wire_size *= 15;
569*4882a593Smuzhiyun avg_wire_size += 11452;
570*4882a593Smuzhiyun } else if (avg_wire_size <= 1980) {
571*4882a593Smuzhiyun /* 36K ints/sec to 30K ints/sec */
572*4882a593Smuzhiyun avg_wire_size *= 5;
573*4882a593Smuzhiyun avg_wire_size += 22420;
574*4882a593Smuzhiyun } else {
575*4882a593Smuzhiyun /* plateau at a limit of 30K ints/sec */
576*4882a593Smuzhiyun avg_wire_size = 32256;
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun /* If we are in low latency mode halve our delay which doubles the
580*4882a593Smuzhiyun * rate to somewhere between 100K to 16K ints/sec
581*4882a593Smuzhiyun */
582*4882a593Smuzhiyun if (itr & IAVF_ITR_ADAPTIVE_LATENCY)
583*4882a593Smuzhiyun avg_wire_size /= 2;
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun /* Resultant value is 256 times larger than it needs to be. This
586*4882a593Smuzhiyun * gives us room to adjust the value as needed to either increase
587*4882a593Smuzhiyun * or decrease the value based on link speeds of 10G, 2.5G, 1G, etc.
588*4882a593Smuzhiyun *
589*4882a593Smuzhiyun * Use addition as we have already recorded the new latency flag
590*4882a593Smuzhiyun * for the ITR value.
591*4882a593Smuzhiyun */
592*4882a593Smuzhiyun itr += DIV_ROUND_UP(avg_wire_size, iavf_itr_divisor(q_vector)) *
593*4882a593Smuzhiyun IAVF_ITR_ADAPTIVE_MIN_INC;
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun if ((itr & IAVF_ITR_MASK) > IAVF_ITR_ADAPTIVE_MAX_USECS) {
596*4882a593Smuzhiyun itr &= IAVF_ITR_ADAPTIVE_LATENCY;
597*4882a593Smuzhiyun itr += IAVF_ITR_ADAPTIVE_MAX_USECS;
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun clear_counts:
601*4882a593Smuzhiyun /* write back value */
602*4882a593Smuzhiyun rc->target_itr = itr;
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun /* next update should occur within next jiffy */
605*4882a593Smuzhiyun rc->next_update = next_update + 1;
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun rc->total_bytes = 0;
608*4882a593Smuzhiyun rc->total_packets = 0;
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun /**
612*4882a593Smuzhiyun * iavf_setup_tx_descriptors - Allocate the Tx descriptors
613*4882a593Smuzhiyun * @tx_ring: the tx ring to set up
614*4882a593Smuzhiyun *
615*4882a593Smuzhiyun * Return 0 on success, negative on error
616*4882a593Smuzhiyun **/
iavf_setup_tx_descriptors(struct iavf_ring * tx_ring)617*4882a593Smuzhiyun int iavf_setup_tx_descriptors(struct iavf_ring *tx_ring)
618*4882a593Smuzhiyun {
619*4882a593Smuzhiyun struct device *dev = tx_ring->dev;
620*4882a593Smuzhiyun int bi_size;
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun if (!dev)
623*4882a593Smuzhiyun return -ENOMEM;
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun /* warn if we are about to overwrite the pointer */
626*4882a593Smuzhiyun WARN_ON(tx_ring->tx_bi);
627*4882a593Smuzhiyun bi_size = sizeof(struct iavf_tx_buffer) * tx_ring->count;
628*4882a593Smuzhiyun tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
629*4882a593Smuzhiyun if (!tx_ring->tx_bi)
630*4882a593Smuzhiyun goto err;
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun /* round up to nearest 4K */
633*4882a593Smuzhiyun tx_ring->size = tx_ring->count * sizeof(struct iavf_tx_desc);
634*4882a593Smuzhiyun tx_ring->size = ALIGN(tx_ring->size, 4096);
635*4882a593Smuzhiyun tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
636*4882a593Smuzhiyun &tx_ring->dma, GFP_KERNEL);
637*4882a593Smuzhiyun if (!tx_ring->desc) {
638*4882a593Smuzhiyun dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
639*4882a593Smuzhiyun tx_ring->size);
640*4882a593Smuzhiyun goto err;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun tx_ring->next_to_use = 0;
644*4882a593Smuzhiyun tx_ring->next_to_clean = 0;
645*4882a593Smuzhiyun tx_ring->tx_stats.prev_pkt_ctr = -1;
646*4882a593Smuzhiyun return 0;
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun err:
649*4882a593Smuzhiyun kfree(tx_ring->tx_bi);
650*4882a593Smuzhiyun tx_ring->tx_bi = NULL;
651*4882a593Smuzhiyun return -ENOMEM;
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun /**
655*4882a593Smuzhiyun * iavf_clean_rx_ring - Free Rx buffers
656*4882a593Smuzhiyun * @rx_ring: ring to be cleaned
657*4882a593Smuzhiyun **/
iavf_clean_rx_ring(struct iavf_ring * rx_ring)658*4882a593Smuzhiyun void iavf_clean_rx_ring(struct iavf_ring *rx_ring)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun unsigned long bi_size;
661*4882a593Smuzhiyun u16 i;
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun /* ring already cleared, nothing to do */
664*4882a593Smuzhiyun if (!rx_ring->rx_bi)
665*4882a593Smuzhiyun return;
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun if (rx_ring->skb) {
668*4882a593Smuzhiyun dev_kfree_skb(rx_ring->skb);
669*4882a593Smuzhiyun rx_ring->skb = NULL;
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun /* Free all the Rx ring sk_buffs */
673*4882a593Smuzhiyun for (i = 0; i < rx_ring->count; i++) {
674*4882a593Smuzhiyun struct iavf_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun if (!rx_bi->page)
677*4882a593Smuzhiyun continue;
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun /* Invalidate cache lines that may have been written to by
680*4882a593Smuzhiyun * device so that we avoid corrupting memory.
681*4882a593Smuzhiyun */
682*4882a593Smuzhiyun dma_sync_single_range_for_cpu(rx_ring->dev,
683*4882a593Smuzhiyun rx_bi->dma,
684*4882a593Smuzhiyun rx_bi->page_offset,
685*4882a593Smuzhiyun rx_ring->rx_buf_len,
686*4882a593Smuzhiyun DMA_FROM_DEVICE);
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun /* free resources associated with mapping */
689*4882a593Smuzhiyun dma_unmap_page_attrs(rx_ring->dev, rx_bi->dma,
690*4882a593Smuzhiyun iavf_rx_pg_size(rx_ring),
691*4882a593Smuzhiyun DMA_FROM_DEVICE,
692*4882a593Smuzhiyun IAVF_RX_DMA_ATTR);
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun __page_frag_cache_drain(rx_bi->page, rx_bi->pagecnt_bias);
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun rx_bi->page = NULL;
697*4882a593Smuzhiyun rx_bi->page_offset = 0;
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun bi_size = sizeof(struct iavf_rx_buffer) * rx_ring->count;
701*4882a593Smuzhiyun memset(rx_ring->rx_bi, 0, bi_size);
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun /* Zero out the descriptor ring */
704*4882a593Smuzhiyun memset(rx_ring->desc, 0, rx_ring->size);
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun rx_ring->next_to_alloc = 0;
707*4882a593Smuzhiyun rx_ring->next_to_clean = 0;
708*4882a593Smuzhiyun rx_ring->next_to_use = 0;
709*4882a593Smuzhiyun }
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun /**
712*4882a593Smuzhiyun * iavf_free_rx_resources - Free Rx resources
713*4882a593Smuzhiyun * @rx_ring: ring to clean the resources from
714*4882a593Smuzhiyun *
715*4882a593Smuzhiyun * Free all receive software resources
716*4882a593Smuzhiyun **/
iavf_free_rx_resources(struct iavf_ring * rx_ring)717*4882a593Smuzhiyun void iavf_free_rx_resources(struct iavf_ring *rx_ring)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun iavf_clean_rx_ring(rx_ring);
720*4882a593Smuzhiyun kfree(rx_ring->rx_bi);
721*4882a593Smuzhiyun rx_ring->rx_bi = NULL;
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun if (rx_ring->desc) {
724*4882a593Smuzhiyun dma_free_coherent(rx_ring->dev, rx_ring->size,
725*4882a593Smuzhiyun rx_ring->desc, rx_ring->dma);
726*4882a593Smuzhiyun rx_ring->desc = NULL;
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun
730*4882a593Smuzhiyun /**
731*4882a593Smuzhiyun * iavf_setup_rx_descriptors - Allocate Rx descriptors
732*4882a593Smuzhiyun * @rx_ring: Rx descriptor ring (for a specific queue) to setup
733*4882a593Smuzhiyun *
734*4882a593Smuzhiyun * Returns 0 on success, negative on failure
735*4882a593Smuzhiyun **/
iavf_setup_rx_descriptors(struct iavf_ring * rx_ring)736*4882a593Smuzhiyun int iavf_setup_rx_descriptors(struct iavf_ring *rx_ring)
737*4882a593Smuzhiyun {
738*4882a593Smuzhiyun struct device *dev = rx_ring->dev;
739*4882a593Smuzhiyun int bi_size;
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun /* warn if we are about to overwrite the pointer */
742*4882a593Smuzhiyun WARN_ON(rx_ring->rx_bi);
743*4882a593Smuzhiyun bi_size = sizeof(struct iavf_rx_buffer) * rx_ring->count;
744*4882a593Smuzhiyun rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
745*4882a593Smuzhiyun if (!rx_ring->rx_bi)
746*4882a593Smuzhiyun goto err;
747*4882a593Smuzhiyun
748*4882a593Smuzhiyun u64_stats_init(&rx_ring->syncp);
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun /* Round up to nearest 4K */
751*4882a593Smuzhiyun rx_ring->size = rx_ring->count * sizeof(union iavf_32byte_rx_desc);
752*4882a593Smuzhiyun rx_ring->size = ALIGN(rx_ring->size, 4096);
753*4882a593Smuzhiyun rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
754*4882a593Smuzhiyun &rx_ring->dma, GFP_KERNEL);
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun if (!rx_ring->desc) {
757*4882a593Smuzhiyun dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
758*4882a593Smuzhiyun rx_ring->size);
759*4882a593Smuzhiyun goto err;
760*4882a593Smuzhiyun }
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun rx_ring->next_to_alloc = 0;
763*4882a593Smuzhiyun rx_ring->next_to_clean = 0;
764*4882a593Smuzhiyun rx_ring->next_to_use = 0;
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun return 0;
767*4882a593Smuzhiyun err:
768*4882a593Smuzhiyun kfree(rx_ring->rx_bi);
769*4882a593Smuzhiyun rx_ring->rx_bi = NULL;
770*4882a593Smuzhiyun return -ENOMEM;
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun /**
774*4882a593Smuzhiyun * iavf_release_rx_desc - Store the new tail and head values
775*4882a593Smuzhiyun * @rx_ring: ring to bump
776*4882a593Smuzhiyun * @val: new head index
777*4882a593Smuzhiyun **/
iavf_release_rx_desc(struct iavf_ring * rx_ring,u32 val)778*4882a593Smuzhiyun static inline void iavf_release_rx_desc(struct iavf_ring *rx_ring, u32 val)
779*4882a593Smuzhiyun {
780*4882a593Smuzhiyun rx_ring->next_to_use = val;
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun /* update next to alloc since we have filled the ring */
783*4882a593Smuzhiyun rx_ring->next_to_alloc = val;
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun /* Force memory writes to complete before letting h/w
786*4882a593Smuzhiyun * know there are new descriptors to fetch. (Only
787*4882a593Smuzhiyun * applicable for weak-ordered memory model archs,
788*4882a593Smuzhiyun * such as IA-64).
789*4882a593Smuzhiyun */
790*4882a593Smuzhiyun wmb();
791*4882a593Smuzhiyun writel(val, rx_ring->tail);
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun /**
795*4882a593Smuzhiyun * iavf_rx_offset - Return expected offset into page to access data
796*4882a593Smuzhiyun * @rx_ring: Ring we are requesting offset of
797*4882a593Smuzhiyun *
798*4882a593Smuzhiyun * Returns the offset value for ring into the data buffer.
799*4882a593Smuzhiyun */
iavf_rx_offset(struct iavf_ring * rx_ring)800*4882a593Smuzhiyun static inline unsigned int iavf_rx_offset(struct iavf_ring *rx_ring)
801*4882a593Smuzhiyun {
802*4882a593Smuzhiyun return ring_uses_build_skb(rx_ring) ? IAVF_SKB_PAD : 0;
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun
805*4882a593Smuzhiyun /**
806*4882a593Smuzhiyun * iavf_alloc_mapped_page - recycle or make a new page
807*4882a593Smuzhiyun * @rx_ring: ring to use
808*4882a593Smuzhiyun * @bi: rx_buffer struct to modify
809*4882a593Smuzhiyun *
810*4882a593Smuzhiyun * Returns true if the page was successfully allocated or
811*4882a593Smuzhiyun * reused.
812*4882a593Smuzhiyun **/
iavf_alloc_mapped_page(struct iavf_ring * rx_ring,struct iavf_rx_buffer * bi)813*4882a593Smuzhiyun static bool iavf_alloc_mapped_page(struct iavf_ring *rx_ring,
814*4882a593Smuzhiyun struct iavf_rx_buffer *bi)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun struct page *page = bi->page;
817*4882a593Smuzhiyun dma_addr_t dma;
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun /* since we are recycling buffers we should seldom need to alloc */
820*4882a593Smuzhiyun if (likely(page)) {
821*4882a593Smuzhiyun rx_ring->rx_stats.page_reuse_count++;
822*4882a593Smuzhiyun return true;
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun /* alloc new page for storage */
826*4882a593Smuzhiyun page = dev_alloc_pages(iavf_rx_pg_order(rx_ring));
827*4882a593Smuzhiyun if (unlikely(!page)) {
828*4882a593Smuzhiyun rx_ring->rx_stats.alloc_page_failed++;
829*4882a593Smuzhiyun return false;
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun
832*4882a593Smuzhiyun /* map page for use */
833*4882a593Smuzhiyun dma = dma_map_page_attrs(rx_ring->dev, page, 0,
834*4882a593Smuzhiyun iavf_rx_pg_size(rx_ring),
835*4882a593Smuzhiyun DMA_FROM_DEVICE,
836*4882a593Smuzhiyun IAVF_RX_DMA_ATTR);
837*4882a593Smuzhiyun
838*4882a593Smuzhiyun /* if mapping failed free memory back to system since
839*4882a593Smuzhiyun * there isn't much point in holding memory we can't use
840*4882a593Smuzhiyun */
841*4882a593Smuzhiyun if (dma_mapping_error(rx_ring->dev, dma)) {
842*4882a593Smuzhiyun __free_pages(page, iavf_rx_pg_order(rx_ring));
843*4882a593Smuzhiyun rx_ring->rx_stats.alloc_page_failed++;
844*4882a593Smuzhiyun return false;
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun
847*4882a593Smuzhiyun bi->dma = dma;
848*4882a593Smuzhiyun bi->page = page;
849*4882a593Smuzhiyun bi->page_offset = iavf_rx_offset(rx_ring);
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun /* initialize pagecnt_bias to 1 representing we fully own page */
852*4882a593Smuzhiyun bi->pagecnt_bias = 1;
853*4882a593Smuzhiyun
854*4882a593Smuzhiyun return true;
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun /**
858*4882a593Smuzhiyun * iavf_receive_skb - Send a completed packet up the stack
859*4882a593Smuzhiyun * @rx_ring: rx ring in play
860*4882a593Smuzhiyun * @skb: packet to send up
861*4882a593Smuzhiyun * @vlan_tag: vlan tag for packet
862*4882a593Smuzhiyun **/
iavf_receive_skb(struct iavf_ring * rx_ring,struct sk_buff * skb,u16 vlan_tag)863*4882a593Smuzhiyun static void iavf_receive_skb(struct iavf_ring *rx_ring,
864*4882a593Smuzhiyun struct sk_buff *skb, u16 vlan_tag)
865*4882a593Smuzhiyun {
866*4882a593Smuzhiyun struct iavf_q_vector *q_vector = rx_ring->q_vector;
867*4882a593Smuzhiyun
868*4882a593Smuzhiyun if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
869*4882a593Smuzhiyun (vlan_tag & VLAN_VID_MASK))
870*4882a593Smuzhiyun __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
871*4882a593Smuzhiyun
872*4882a593Smuzhiyun napi_gro_receive(&q_vector->napi, skb);
873*4882a593Smuzhiyun }
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun /**
876*4882a593Smuzhiyun * iavf_alloc_rx_buffers - Replace used receive buffers
877*4882a593Smuzhiyun * @rx_ring: ring to place buffers on
878*4882a593Smuzhiyun * @cleaned_count: number of buffers to replace
879*4882a593Smuzhiyun *
880*4882a593Smuzhiyun * Returns false if all allocations were successful, true if any fail
881*4882a593Smuzhiyun **/
iavf_alloc_rx_buffers(struct iavf_ring * rx_ring,u16 cleaned_count)882*4882a593Smuzhiyun bool iavf_alloc_rx_buffers(struct iavf_ring *rx_ring, u16 cleaned_count)
883*4882a593Smuzhiyun {
884*4882a593Smuzhiyun u16 ntu = rx_ring->next_to_use;
885*4882a593Smuzhiyun union iavf_rx_desc *rx_desc;
886*4882a593Smuzhiyun struct iavf_rx_buffer *bi;
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun /* do nothing if no valid netdev defined */
889*4882a593Smuzhiyun if (!rx_ring->netdev || !cleaned_count)
890*4882a593Smuzhiyun return false;
891*4882a593Smuzhiyun
892*4882a593Smuzhiyun rx_desc = IAVF_RX_DESC(rx_ring, ntu);
893*4882a593Smuzhiyun bi = &rx_ring->rx_bi[ntu];
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun do {
896*4882a593Smuzhiyun if (!iavf_alloc_mapped_page(rx_ring, bi))
897*4882a593Smuzhiyun goto no_buffers;
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun /* sync the buffer for use by the device */
900*4882a593Smuzhiyun dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
901*4882a593Smuzhiyun bi->page_offset,
902*4882a593Smuzhiyun rx_ring->rx_buf_len,
903*4882a593Smuzhiyun DMA_FROM_DEVICE);
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun /* Refresh the desc even if buffer_addrs didn't change
906*4882a593Smuzhiyun * because each write-back erases this info.
907*4882a593Smuzhiyun */
908*4882a593Smuzhiyun rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
909*4882a593Smuzhiyun
910*4882a593Smuzhiyun rx_desc++;
911*4882a593Smuzhiyun bi++;
912*4882a593Smuzhiyun ntu++;
913*4882a593Smuzhiyun if (unlikely(ntu == rx_ring->count)) {
914*4882a593Smuzhiyun rx_desc = IAVF_RX_DESC(rx_ring, 0);
915*4882a593Smuzhiyun bi = rx_ring->rx_bi;
916*4882a593Smuzhiyun ntu = 0;
917*4882a593Smuzhiyun }
918*4882a593Smuzhiyun
919*4882a593Smuzhiyun /* clear the status bits for the next_to_use descriptor */
920*4882a593Smuzhiyun rx_desc->wb.qword1.status_error_len = 0;
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun cleaned_count--;
923*4882a593Smuzhiyun } while (cleaned_count);
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun if (rx_ring->next_to_use != ntu)
926*4882a593Smuzhiyun iavf_release_rx_desc(rx_ring, ntu);
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun return false;
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun no_buffers:
931*4882a593Smuzhiyun if (rx_ring->next_to_use != ntu)
932*4882a593Smuzhiyun iavf_release_rx_desc(rx_ring, ntu);
933*4882a593Smuzhiyun
934*4882a593Smuzhiyun /* make sure to come back via polling to try again after
935*4882a593Smuzhiyun * allocation failure
936*4882a593Smuzhiyun */
937*4882a593Smuzhiyun return true;
938*4882a593Smuzhiyun }
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun /**
941*4882a593Smuzhiyun * iavf_rx_checksum - Indicate in skb if hw indicated a good cksum
942*4882a593Smuzhiyun * @vsi: the VSI we care about
943*4882a593Smuzhiyun * @skb: skb currently being received and modified
944*4882a593Smuzhiyun * @rx_desc: the receive descriptor
945*4882a593Smuzhiyun **/
iavf_rx_checksum(struct iavf_vsi * vsi,struct sk_buff * skb,union iavf_rx_desc * rx_desc)946*4882a593Smuzhiyun static inline void iavf_rx_checksum(struct iavf_vsi *vsi,
947*4882a593Smuzhiyun struct sk_buff *skb,
948*4882a593Smuzhiyun union iavf_rx_desc *rx_desc)
949*4882a593Smuzhiyun {
950*4882a593Smuzhiyun struct iavf_rx_ptype_decoded decoded;
951*4882a593Smuzhiyun u32 rx_error, rx_status;
952*4882a593Smuzhiyun bool ipv4, ipv6;
953*4882a593Smuzhiyun u8 ptype;
954*4882a593Smuzhiyun u64 qword;
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
957*4882a593Smuzhiyun ptype = (qword & IAVF_RXD_QW1_PTYPE_MASK) >> IAVF_RXD_QW1_PTYPE_SHIFT;
958*4882a593Smuzhiyun rx_error = (qword & IAVF_RXD_QW1_ERROR_MASK) >>
959*4882a593Smuzhiyun IAVF_RXD_QW1_ERROR_SHIFT;
960*4882a593Smuzhiyun rx_status = (qword & IAVF_RXD_QW1_STATUS_MASK) >>
961*4882a593Smuzhiyun IAVF_RXD_QW1_STATUS_SHIFT;
962*4882a593Smuzhiyun decoded = decode_rx_desc_ptype(ptype);
963*4882a593Smuzhiyun
964*4882a593Smuzhiyun skb->ip_summed = CHECKSUM_NONE;
965*4882a593Smuzhiyun
966*4882a593Smuzhiyun skb_checksum_none_assert(skb);
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun /* Rx csum enabled and ip headers found? */
969*4882a593Smuzhiyun if (!(vsi->netdev->features & NETIF_F_RXCSUM))
970*4882a593Smuzhiyun return;
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun /* did the hardware decode the packet and checksum? */
973*4882a593Smuzhiyun if (!(rx_status & BIT(IAVF_RX_DESC_STATUS_L3L4P_SHIFT)))
974*4882a593Smuzhiyun return;
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun /* both known and outer_ip must be set for the below code to work */
977*4882a593Smuzhiyun if (!(decoded.known && decoded.outer_ip))
978*4882a593Smuzhiyun return;
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun ipv4 = (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP) &&
981*4882a593Smuzhiyun (decoded.outer_ip_ver == IAVF_RX_PTYPE_OUTER_IPV4);
982*4882a593Smuzhiyun ipv6 = (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP) &&
983*4882a593Smuzhiyun (decoded.outer_ip_ver == IAVF_RX_PTYPE_OUTER_IPV6);
984*4882a593Smuzhiyun
985*4882a593Smuzhiyun if (ipv4 &&
986*4882a593Smuzhiyun (rx_error & (BIT(IAVF_RX_DESC_ERROR_IPE_SHIFT) |
987*4882a593Smuzhiyun BIT(IAVF_RX_DESC_ERROR_EIPE_SHIFT))))
988*4882a593Smuzhiyun goto checksum_fail;
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun /* likely incorrect csum if alternate IP extension headers found */
991*4882a593Smuzhiyun if (ipv6 &&
992*4882a593Smuzhiyun rx_status & BIT(IAVF_RX_DESC_STATUS_IPV6EXADD_SHIFT))
993*4882a593Smuzhiyun /* don't increment checksum err here, non-fatal err */
994*4882a593Smuzhiyun return;
995*4882a593Smuzhiyun
996*4882a593Smuzhiyun /* there was some L4 error, count error and punt packet to the stack */
997*4882a593Smuzhiyun if (rx_error & BIT(IAVF_RX_DESC_ERROR_L4E_SHIFT))
998*4882a593Smuzhiyun goto checksum_fail;
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun /* handle packets that were not able to be checksummed due
1001*4882a593Smuzhiyun * to arrival speed, in this case the stack can compute
1002*4882a593Smuzhiyun * the csum.
1003*4882a593Smuzhiyun */
1004*4882a593Smuzhiyun if (rx_error & BIT(IAVF_RX_DESC_ERROR_PPRS_SHIFT))
1005*4882a593Smuzhiyun return;
1006*4882a593Smuzhiyun
1007*4882a593Smuzhiyun /* Only report checksum unnecessary for TCP, UDP, or SCTP */
1008*4882a593Smuzhiyun switch (decoded.inner_prot) {
1009*4882a593Smuzhiyun case IAVF_RX_PTYPE_INNER_PROT_TCP:
1010*4882a593Smuzhiyun case IAVF_RX_PTYPE_INNER_PROT_UDP:
1011*4882a593Smuzhiyun case IAVF_RX_PTYPE_INNER_PROT_SCTP:
1012*4882a593Smuzhiyun skb->ip_summed = CHECKSUM_UNNECESSARY;
1013*4882a593Smuzhiyun fallthrough;
1014*4882a593Smuzhiyun default:
1015*4882a593Smuzhiyun break;
1016*4882a593Smuzhiyun }
1017*4882a593Smuzhiyun
1018*4882a593Smuzhiyun return;
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun checksum_fail:
1021*4882a593Smuzhiyun vsi->back->hw_csum_rx_error++;
1022*4882a593Smuzhiyun }
1023*4882a593Smuzhiyun
1024*4882a593Smuzhiyun /**
1025*4882a593Smuzhiyun * iavf_ptype_to_htype - get a hash type
1026*4882a593Smuzhiyun * @ptype: the ptype value from the descriptor
1027*4882a593Smuzhiyun *
1028*4882a593Smuzhiyun * Returns a hash type to be used by skb_set_hash
1029*4882a593Smuzhiyun **/
iavf_ptype_to_htype(u8 ptype)1030*4882a593Smuzhiyun static inline int iavf_ptype_to_htype(u8 ptype)
1031*4882a593Smuzhiyun {
1032*4882a593Smuzhiyun struct iavf_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
1033*4882a593Smuzhiyun
1034*4882a593Smuzhiyun if (!decoded.known)
1035*4882a593Smuzhiyun return PKT_HASH_TYPE_NONE;
1036*4882a593Smuzhiyun
1037*4882a593Smuzhiyun if (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP &&
1038*4882a593Smuzhiyun decoded.payload_layer == IAVF_RX_PTYPE_PAYLOAD_LAYER_PAY4)
1039*4882a593Smuzhiyun return PKT_HASH_TYPE_L4;
1040*4882a593Smuzhiyun else if (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP &&
1041*4882a593Smuzhiyun decoded.payload_layer == IAVF_RX_PTYPE_PAYLOAD_LAYER_PAY3)
1042*4882a593Smuzhiyun return PKT_HASH_TYPE_L3;
1043*4882a593Smuzhiyun else
1044*4882a593Smuzhiyun return PKT_HASH_TYPE_L2;
1045*4882a593Smuzhiyun }
1046*4882a593Smuzhiyun
1047*4882a593Smuzhiyun /**
1048*4882a593Smuzhiyun * iavf_rx_hash - set the hash value in the skb
1049*4882a593Smuzhiyun * @ring: descriptor ring
1050*4882a593Smuzhiyun * @rx_desc: specific descriptor
1051*4882a593Smuzhiyun * @skb: skb currently being received and modified
1052*4882a593Smuzhiyun * @rx_ptype: Rx packet type
1053*4882a593Smuzhiyun **/
iavf_rx_hash(struct iavf_ring * ring,union iavf_rx_desc * rx_desc,struct sk_buff * skb,u8 rx_ptype)1054*4882a593Smuzhiyun static inline void iavf_rx_hash(struct iavf_ring *ring,
1055*4882a593Smuzhiyun union iavf_rx_desc *rx_desc,
1056*4882a593Smuzhiyun struct sk_buff *skb,
1057*4882a593Smuzhiyun u8 rx_ptype)
1058*4882a593Smuzhiyun {
1059*4882a593Smuzhiyun u32 hash;
1060*4882a593Smuzhiyun const __le64 rss_mask =
1061*4882a593Smuzhiyun cpu_to_le64((u64)IAVF_RX_DESC_FLTSTAT_RSS_HASH <<
1062*4882a593Smuzhiyun IAVF_RX_DESC_STATUS_FLTSTAT_SHIFT);
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun if (ring->netdev->features & NETIF_F_RXHASH)
1065*4882a593Smuzhiyun return;
1066*4882a593Smuzhiyun
1067*4882a593Smuzhiyun if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
1068*4882a593Smuzhiyun hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
1069*4882a593Smuzhiyun skb_set_hash(skb, hash, iavf_ptype_to_htype(rx_ptype));
1070*4882a593Smuzhiyun }
1071*4882a593Smuzhiyun }
1072*4882a593Smuzhiyun
1073*4882a593Smuzhiyun /**
1074*4882a593Smuzhiyun * iavf_process_skb_fields - Populate skb header fields from Rx descriptor
1075*4882a593Smuzhiyun * @rx_ring: rx descriptor ring packet is being transacted on
1076*4882a593Smuzhiyun * @rx_desc: pointer to the EOP Rx descriptor
1077*4882a593Smuzhiyun * @skb: pointer to current skb being populated
1078*4882a593Smuzhiyun * @rx_ptype: the packet type decoded by hardware
1079*4882a593Smuzhiyun *
1080*4882a593Smuzhiyun * This function checks the ring, descriptor, and packet information in
1081*4882a593Smuzhiyun * order to populate the hash, checksum, VLAN, protocol, and
1082*4882a593Smuzhiyun * other fields within the skb.
1083*4882a593Smuzhiyun **/
1084*4882a593Smuzhiyun static inline
iavf_process_skb_fields(struct iavf_ring * rx_ring,union iavf_rx_desc * rx_desc,struct sk_buff * skb,u8 rx_ptype)1085*4882a593Smuzhiyun void iavf_process_skb_fields(struct iavf_ring *rx_ring,
1086*4882a593Smuzhiyun union iavf_rx_desc *rx_desc, struct sk_buff *skb,
1087*4882a593Smuzhiyun u8 rx_ptype)
1088*4882a593Smuzhiyun {
1089*4882a593Smuzhiyun iavf_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun iavf_rx_checksum(rx_ring->vsi, skb, rx_desc);
1092*4882a593Smuzhiyun
1093*4882a593Smuzhiyun skb_record_rx_queue(skb, rx_ring->queue_index);
1094*4882a593Smuzhiyun
1095*4882a593Smuzhiyun /* modifies the skb - consumes the enet header */
1096*4882a593Smuzhiyun skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1097*4882a593Smuzhiyun }
1098*4882a593Smuzhiyun
1099*4882a593Smuzhiyun /**
1100*4882a593Smuzhiyun * iavf_cleanup_headers - Correct empty headers
1101*4882a593Smuzhiyun * @rx_ring: rx descriptor ring packet is being transacted on
1102*4882a593Smuzhiyun * @skb: pointer to current skb being fixed
1103*4882a593Smuzhiyun *
1104*4882a593Smuzhiyun * Also address the case where we are pulling data in on pages only
1105*4882a593Smuzhiyun * and as such no data is present in the skb header.
1106*4882a593Smuzhiyun *
1107*4882a593Smuzhiyun * In addition if skb is not at least 60 bytes we need to pad it so that
1108*4882a593Smuzhiyun * it is large enough to qualify as a valid Ethernet frame.
1109*4882a593Smuzhiyun *
1110*4882a593Smuzhiyun * Returns true if an error was encountered and skb was freed.
1111*4882a593Smuzhiyun **/
iavf_cleanup_headers(struct iavf_ring * rx_ring,struct sk_buff * skb)1112*4882a593Smuzhiyun static bool iavf_cleanup_headers(struct iavf_ring *rx_ring, struct sk_buff *skb)
1113*4882a593Smuzhiyun {
1114*4882a593Smuzhiyun /* if eth_skb_pad returns an error the skb was freed */
1115*4882a593Smuzhiyun if (eth_skb_pad(skb))
1116*4882a593Smuzhiyun return true;
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun return false;
1119*4882a593Smuzhiyun }
1120*4882a593Smuzhiyun
1121*4882a593Smuzhiyun /**
1122*4882a593Smuzhiyun * iavf_reuse_rx_page - page flip buffer and store it back on the ring
1123*4882a593Smuzhiyun * @rx_ring: rx descriptor ring to store buffers on
1124*4882a593Smuzhiyun * @old_buff: donor buffer to have page reused
1125*4882a593Smuzhiyun *
1126*4882a593Smuzhiyun * Synchronizes page for reuse by the adapter
1127*4882a593Smuzhiyun **/
iavf_reuse_rx_page(struct iavf_ring * rx_ring,struct iavf_rx_buffer * old_buff)1128*4882a593Smuzhiyun static void iavf_reuse_rx_page(struct iavf_ring *rx_ring,
1129*4882a593Smuzhiyun struct iavf_rx_buffer *old_buff)
1130*4882a593Smuzhiyun {
1131*4882a593Smuzhiyun struct iavf_rx_buffer *new_buff;
1132*4882a593Smuzhiyun u16 nta = rx_ring->next_to_alloc;
1133*4882a593Smuzhiyun
1134*4882a593Smuzhiyun new_buff = &rx_ring->rx_bi[nta];
1135*4882a593Smuzhiyun
1136*4882a593Smuzhiyun /* update, and store next to alloc */
1137*4882a593Smuzhiyun nta++;
1138*4882a593Smuzhiyun rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1139*4882a593Smuzhiyun
1140*4882a593Smuzhiyun /* transfer page from old buffer to new buffer */
1141*4882a593Smuzhiyun new_buff->dma = old_buff->dma;
1142*4882a593Smuzhiyun new_buff->page = old_buff->page;
1143*4882a593Smuzhiyun new_buff->page_offset = old_buff->page_offset;
1144*4882a593Smuzhiyun new_buff->pagecnt_bias = old_buff->pagecnt_bias;
1145*4882a593Smuzhiyun }
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun /**
1148*4882a593Smuzhiyun * iavf_page_is_reusable - check if any reuse is possible
1149*4882a593Smuzhiyun * @page: page struct to check
1150*4882a593Smuzhiyun *
1151*4882a593Smuzhiyun * A page is not reusable if it was allocated under low memory
1152*4882a593Smuzhiyun * conditions, or it's not in the same NUMA node as this CPU.
1153*4882a593Smuzhiyun */
iavf_page_is_reusable(struct page * page)1154*4882a593Smuzhiyun static inline bool iavf_page_is_reusable(struct page *page)
1155*4882a593Smuzhiyun {
1156*4882a593Smuzhiyun return (page_to_nid(page) == numa_mem_id()) &&
1157*4882a593Smuzhiyun !page_is_pfmemalloc(page);
1158*4882a593Smuzhiyun }
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun /**
1161*4882a593Smuzhiyun * iavf_can_reuse_rx_page - Determine if this page can be reused by
1162*4882a593Smuzhiyun * the adapter for another receive
1163*4882a593Smuzhiyun *
1164*4882a593Smuzhiyun * @rx_buffer: buffer containing the page
1165*4882a593Smuzhiyun *
1166*4882a593Smuzhiyun * If page is reusable, rx_buffer->page_offset is adjusted to point to
1167*4882a593Smuzhiyun * an unused region in the page.
1168*4882a593Smuzhiyun *
1169*4882a593Smuzhiyun * For small pages, @truesize will be a constant value, half the size
1170*4882a593Smuzhiyun * of the memory at page. We'll attempt to alternate between high and
1171*4882a593Smuzhiyun * low halves of the page, with one half ready for use by the hardware
1172*4882a593Smuzhiyun * and the other half being consumed by the stack. We use the page
1173*4882a593Smuzhiyun * ref count to determine whether the stack has finished consuming the
1174*4882a593Smuzhiyun * portion of this page that was passed up with a previous packet. If
1175*4882a593Smuzhiyun * the page ref count is >1, we'll assume the "other" half page is
1176*4882a593Smuzhiyun * still busy, and this page cannot be reused.
1177*4882a593Smuzhiyun *
1178*4882a593Smuzhiyun * For larger pages, @truesize will be the actual space used by the
1179*4882a593Smuzhiyun * received packet (adjusted upward to an even multiple of the cache
1180*4882a593Smuzhiyun * line size). This will advance through the page by the amount
1181*4882a593Smuzhiyun * actually consumed by the received packets while there is still
1182*4882a593Smuzhiyun * space for a buffer. Each region of larger pages will be used at
1183*4882a593Smuzhiyun * most once, after which the page will not be reused.
1184*4882a593Smuzhiyun *
1185*4882a593Smuzhiyun * In either case, if the page is reusable its refcount is increased.
1186*4882a593Smuzhiyun **/
iavf_can_reuse_rx_page(struct iavf_rx_buffer * rx_buffer)1187*4882a593Smuzhiyun static bool iavf_can_reuse_rx_page(struct iavf_rx_buffer *rx_buffer)
1188*4882a593Smuzhiyun {
1189*4882a593Smuzhiyun unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
1190*4882a593Smuzhiyun struct page *page = rx_buffer->page;
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun /* Is any reuse possible? */
1193*4882a593Smuzhiyun if (unlikely(!iavf_page_is_reusable(page)))
1194*4882a593Smuzhiyun return false;
1195*4882a593Smuzhiyun
1196*4882a593Smuzhiyun #if (PAGE_SIZE < 8192)
1197*4882a593Smuzhiyun /* if we are only owner of page we can reuse it */
1198*4882a593Smuzhiyun if (unlikely((page_count(page) - pagecnt_bias) > 1))
1199*4882a593Smuzhiyun return false;
1200*4882a593Smuzhiyun #else
1201*4882a593Smuzhiyun #define IAVF_LAST_OFFSET \
1202*4882a593Smuzhiyun (SKB_WITH_OVERHEAD(PAGE_SIZE) - IAVF_RXBUFFER_2048)
1203*4882a593Smuzhiyun if (rx_buffer->page_offset > IAVF_LAST_OFFSET)
1204*4882a593Smuzhiyun return false;
1205*4882a593Smuzhiyun #endif
1206*4882a593Smuzhiyun
1207*4882a593Smuzhiyun /* If we have drained the page fragment pool we need to update
1208*4882a593Smuzhiyun * the pagecnt_bias and page count so that we fully restock the
1209*4882a593Smuzhiyun * number of references the driver holds.
1210*4882a593Smuzhiyun */
1211*4882a593Smuzhiyun if (unlikely(!pagecnt_bias)) {
1212*4882a593Smuzhiyun page_ref_add(page, USHRT_MAX);
1213*4882a593Smuzhiyun rx_buffer->pagecnt_bias = USHRT_MAX;
1214*4882a593Smuzhiyun }
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun return true;
1217*4882a593Smuzhiyun }
1218*4882a593Smuzhiyun
1219*4882a593Smuzhiyun /**
1220*4882a593Smuzhiyun * iavf_add_rx_frag - Add contents of Rx buffer to sk_buff
1221*4882a593Smuzhiyun * @rx_ring: rx descriptor ring to transact packets on
1222*4882a593Smuzhiyun * @rx_buffer: buffer containing page to add
1223*4882a593Smuzhiyun * @skb: sk_buff to place the data into
1224*4882a593Smuzhiyun * @size: packet length from rx_desc
1225*4882a593Smuzhiyun *
1226*4882a593Smuzhiyun * This function will add the data contained in rx_buffer->page to the skb.
1227*4882a593Smuzhiyun * It will just attach the page as a frag to the skb.
1228*4882a593Smuzhiyun *
1229*4882a593Smuzhiyun * The function will then update the page offset.
1230*4882a593Smuzhiyun **/
iavf_add_rx_frag(struct iavf_ring * rx_ring,struct iavf_rx_buffer * rx_buffer,struct sk_buff * skb,unsigned int size)1231*4882a593Smuzhiyun static void iavf_add_rx_frag(struct iavf_ring *rx_ring,
1232*4882a593Smuzhiyun struct iavf_rx_buffer *rx_buffer,
1233*4882a593Smuzhiyun struct sk_buff *skb,
1234*4882a593Smuzhiyun unsigned int size)
1235*4882a593Smuzhiyun {
1236*4882a593Smuzhiyun #if (PAGE_SIZE < 8192)
1237*4882a593Smuzhiyun unsigned int truesize = iavf_rx_pg_size(rx_ring) / 2;
1238*4882a593Smuzhiyun #else
1239*4882a593Smuzhiyun unsigned int truesize = SKB_DATA_ALIGN(size + iavf_rx_offset(rx_ring));
1240*4882a593Smuzhiyun #endif
1241*4882a593Smuzhiyun
1242*4882a593Smuzhiyun if (!size)
1243*4882a593Smuzhiyun return;
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
1246*4882a593Smuzhiyun rx_buffer->page_offset, size, truesize);
1247*4882a593Smuzhiyun
1248*4882a593Smuzhiyun /* page is being used so we must update the page offset */
1249*4882a593Smuzhiyun #if (PAGE_SIZE < 8192)
1250*4882a593Smuzhiyun rx_buffer->page_offset ^= truesize;
1251*4882a593Smuzhiyun #else
1252*4882a593Smuzhiyun rx_buffer->page_offset += truesize;
1253*4882a593Smuzhiyun #endif
1254*4882a593Smuzhiyun }
1255*4882a593Smuzhiyun
1256*4882a593Smuzhiyun /**
1257*4882a593Smuzhiyun * iavf_get_rx_buffer - Fetch Rx buffer and synchronize data for use
1258*4882a593Smuzhiyun * @rx_ring: rx descriptor ring to transact packets on
1259*4882a593Smuzhiyun * @size: size of buffer to add to skb
1260*4882a593Smuzhiyun *
1261*4882a593Smuzhiyun * This function will pull an Rx buffer from the ring and synchronize it
1262*4882a593Smuzhiyun * for use by the CPU.
1263*4882a593Smuzhiyun */
iavf_get_rx_buffer(struct iavf_ring * rx_ring,const unsigned int size)1264*4882a593Smuzhiyun static struct iavf_rx_buffer *iavf_get_rx_buffer(struct iavf_ring *rx_ring,
1265*4882a593Smuzhiyun const unsigned int size)
1266*4882a593Smuzhiyun {
1267*4882a593Smuzhiyun struct iavf_rx_buffer *rx_buffer;
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
1270*4882a593Smuzhiyun prefetchw(rx_buffer->page);
1271*4882a593Smuzhiyun if (!size)
1272*4882a593Smuzhiyun return rx_buffer;
1273*4882a593Smuzhiyun
1274*4882a593Smuzhiyun /* we are reusing so sync this buffer for CPU use */
1275*4882a593Smuzhiyun dma_sync_single_range_for_cpu(rx_ring->dev,
1276*4882a593Smuzhiyun rx_buffer->dma,
1277*4882a593Smuzhiyun rx_buffer->page_offset,
1278*4882a593Smuzhiyun size,
1279*4882a593Smuzhiyun DMA_FROM_DEVICE);
1280*4882a593Smuzhiyun
1281*4882a593Smuzhiyun /* We have pulled a buffer for use, so decrement pagecnt_bias */
1282*4882a593Smuzhiyun rx_buffer->pagecnt_bias--;
1283*4882a593Smuzhiyun
1284*4882a593Smuzhiyun return rx_buffer;
1285*4882a593Smuzhiyun }
1286*4882a593Smuzhiyun
1287*4882a593Smuzhiyun /**
1288*4882a593Smuzhiyun * iavf_construct_skb - Allocate skb and populate it
1289*4882a593Smuzhiyun * @rx_ring: rx descriptor ring to transact packets on
1290*4882a593Smuzhiyun * @rx_buffer: rx buffer to pull data from
1291*4882a593Smuzhiyun * @size: size of buffer to add to skb
1292*4882a593Smuzhiyun *
1293*4882a593Smuzhiyun * This function allocates an skb. It then populates it with the page
1294*4882a593Smuzhiyun * data from the current receive descriptor, taking care to set up the
1295*4882a593Smuzhiyun * skb correctly.
1296*4882a593Smuzhiyun */
iavf_construct_skb(struct iavf_ring * rx_ring,struct iavf_rx_buffer * rx_buffer,unsigned int size)1297*4882a593Smuzhiyun static struct sk_buff *iavf_construct_skb(struct iavf_ring *rx_ring,
1298*4882a593Smuzhiyun struct iavf_rx_buffer *rx_buffer,
1299*4882a593Smuzhiyun unsigned int size)
1300*4882a593Smuzhiyun {
1301*4882a593Smuzhiyun void *va;
1302*4882a593Smuzhiyun #if (PAGE_SIZE < 8192)
1303*4882a593Smuzhiyun unsigned int truesize = iavf_rx_pg_size(rx_ring) / 2;
1304*4882a593Smuzhiyun #else
1305*4882a593Smuzhiyun unsigned int truesize = SKB_DATA_ALIGN(size);
1306*4882a593Smuzhiyun #endif
1307*4882a593Smuzhiyun unsigned int headlen;
1308*4882a593Smuzhiyun struct sk_buff *skb;
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun if (!rx_buffer)
1311*4882a593Smuzhiyun return NULL;
1312*4882a593Smuzhiyun /* prefetch first cache line of first page */
1313*4882a593Smuzhiyun va = page_address(rx_buffer->page) + rx_buffer->page_offset;
1314*4882a593Smuzhiyun net_prefetch(va);
1315*4882a593Smuzhiyun
1316*4882a593Smuzhiyun /* allocate a skb to store the frags */
1317*4882a593Smuzhiyun skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
1318*4882a593Smuzhiyun IAVF_RX_HDR_SIZE,
1319*4882a593Smuzhiyun GFP_ATOMIC | __GFP_NOWARN);
1320*4882a593Smuzhiyun if (unlikely(!skb))
1321*4882a593Smuzhiyun return NULL;
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun /* Determine available headroom for copy */
1324*4882a593Smuzhiyun headlen = size;
1325*4882a593Smuzhiyun if (headlen > IAVF_RX_HDR_SIZE)
1326*4882a593Smuzhiyun headlen = eth_get_headlen(skb->dev, va, IAVF_RX_HDR_SIZE);
1327*4882a593Smuzhiyun
1328*4882a593Smuzhiyun /* align pull length to size of long to optimize memcpy performance */
1329*4882a593Smuzhiyun memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun /* update all of the pointers */
1332*4882a593Smuzhiyun size -= headlen;
1333*4882a593Smuzhiyun if (size) {
1334*4882a593Smuzhiyun skb_add_rx_frag(skb, 0, rx_buffer->page,
1335*4882a593Smuzhiyun rx_buffer->page_offset + headlen,
1336*4882a593Smuzhiyun size, truesize);
1337*4882a593Smuzhiyun
1338*4882a593Smuzhiyun /* buffer is used by skb, update page_offset */
1339*4882a593Smuzhiyun #if (PAGE_SIZE < 8192)
1340*4882a593Smuzhiyun rx_buffer->page_offset ^= truesize;
1341*4882a593Smuzhiyun #else
1342*4882a593Smuzhiyun rx_buffer->page_offset += truesize;
1343*4882a593Smuzhiyun #endif
1344*4882a593Smuzhiyun } else {
1345*4882a593Smuzhiyun /* buffer is unused, reset bias back to rx_buffer */
1346*4882a593Smuzhiyun rx_buffer->pagecnt_bias++;
1347*4882a593Smuzhiyun }
1348*4882a593Smuzhiyun
1349*4882a593Smuzhiyun return skb;
1350*4882a593Smuzhiyun }
1351*4882a593Smuzhiyun
1352*4882a593Smuzhiyun /**
1353*4882a593Smuzhiyun * iavf_build_skb - Build skb around an existing buffer
1354*4882a593Smuzhiyun * @rx_ring: Rx descriptor ring to transact packets on
1355*4882a593Smuzhiyun * @rx_buffer: Rx buffer to pull data from
1356*4882a593Smuzhiyun * @size: size of buffer to add to skb
1357*4882a593Smuzhiyun *
1358*4882a593Smuzhiyun * This function builds an skb around an existing Rx buffer, taking care
1359*4882a593Smuzhiyun * to set up the skb correctly and avoid any memcpy overhead.
1360*4882a593Smuzhiyun */
iavf_build_skb(struct iavf_ring * rx_ring,struct iavf_rx_buffer * rx_buffer,unsigned int size)1361*4882a593Smuzhiyun static struct sk_buff *iavf_build_skb(struct iavf_ring *rx_ring,
1362*4882a593Smuzhiyun struct iavf_rx_buffer *rx_buffer,
1363*4882a593Smuzhiyun unsigned int size)
1364*4882a593Smuzhiyun {
1365*4882a593Smuzhiyun void *va;
1366*4882a593Smuzhiyun #if (PAGE_SIZE < 8192)
1367*4882a593Smuzhiyun unsigned int truesize = iavf_rx_pg_size(rx_ring) / 2;
1368*4882a593Smuzhiyun #else
1369*4882a593Smuzhiyun unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1370*4882a593Smuzhiyun SKB_DATA_ALIGN(IAVF_SKB_PAD + size);
1371*4882a593Smuzhiyun #endif
1372*4882a593Smuzhiyun struct sk_buff *skb;
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun if (!rx_buffer || !size)
1375*4882a593Smuzhiyun return NULL;
1376*4882a593Smuzhiyun /* prefetch first cache line of first page */
1377*4882a593Smuzhiyun va = page_address(rx_buffer->page) + rx_buffer->page_offset;
1378*4882a593Smuzhiyun net_prefetch(va);
1379*4882a593Smuzhiyun
1380*4882a593Smuzhiyun /* build an skb around the page buffer */
1381*4882a593Smuzhiyun skb = build_skb(va - IAVF_SKB_PAD, truesize);
1382*4882a593Smuzhiyun if (unlikely(!skb))
1383*4882a593Smuzhiyun return NULL;
1384*4882a593Smuzhiyun
1385*4882a593Smuzhiyun /* update pointers within the skb to store the data */
1386*4882a593Smuzhiyun skb_reserve(skb, IAVF_SKB_PAD);
1387*4882a593Smuzhiyun __skb_put(skb, size);
1388*4882a593Smuzhiyun
1389*4882a593Smuzhiyun /* buffer is used by skb, update page_offset */
1390*4882a593Smuzhiyun #if (PAGE_SIZE < 8192)
1391*4882a593Smuzhiyun rx_buffer->page_offset ^= truesize;
1392*4882a593Smuzhiyun #else
1393*4882a593Smuzhiyun rx_buffer->page_offset += truesize;
1394*4882a593Smuzhiyun #endif
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun return skb;
1397*4882a593Smuzhiyun }
1398*4882a593Smuzhiyun
1399*4882a593Smuzhiyun /**
1400*4882a593Smuzhiyun * iavf_put_rx_buffer - Clean up used buffer and either recycle or free
1401*4882a593Smuzhiyun * @rx_ring: rx descriptor ring to transact packets on
1402*4882a593Smuzhiyun * @rx_buffer: rx buffer to pull data from
1403*4882a593Smuzhiyun *
1404*4882a593Smuzhiyun * This function will clean up the contents of the rx_buffer. It will
1405*4882a593Smuzhiyun * either recycle the buffer or unmap it and free the associated resources.
1406*4882a593Smuzhiyun */
iavf_put_rx_buffer(struct iavf_ring * rx_ring,struct iavf_rx_buffer * rx_buffer)1407*4882a593Smuzhiyun static void iavf_put_rx_buffer(struct iavf_ring *rx_ring,
1408*4882a593Smuzhiyun struct iavf_rx_buffer *rx_buffer)
1409*4882a593Smuzhiyun {
1410*4882a593Smuzhiyun if (!rx_buffer)
1411*4882a593Smuzhiyun return;
1412*4882a593Smuzhiyun
1413*4882a593Smuzhiyun if (iavf_can_reuse_rx_page(rx_buffer)) {
1414*4882a593Smuzhiyun /* hand second half of page back to the ring */
1415*4882a593Smuzhiyun iavf_reuse_rx_page(rx_ring, rx_buffer);
1416*4882a593Smuzhiyun rx_ring->rx_stats.page_reuse_count++;
1417*4882a593Smuzhiyun } else {
1418*4882a593Smuzhiyun /* we are not reusing the buffer so unmap it */
1419*4882a593Smuzhiyun dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
1420*4882a593Smuzhiyun iavf_rx_pg_size(rx_ring),
1421*4882a593Smuzhiyun DMA_FROM_DEVICE, IAVF_RX_DMA_ATTR);
1422*4882a593Smuzhiyun __page_frag_cache_drain(rx_buffer->page,
1423*4882a593Smuzhiyun rx_buffer->pagecnt_bias);
1424*4882a593Smuzhiyun }
1425*4882a593Smuzhiyun
1426*4882a593Smuzhiyun /* clear contents of buffer_info */
1427*4882a593Smuzhiyun rx_buffer->page = NULL;
1428*4882a593Smuzhiyun }
1429*4882a593Smuzhiyun
1430*4882a593Smuzhiyun /**
1431*4882a593Smuzhiyun * iavf_is_non_eop - process handling of non-EOP buffers
1432*4882a593Smuzhiyun * @rx_ring: Rx ring being processed
1433*4882a593Smuzhiyun * @rx_desc: Rx descriptor for current buffer
1434*4882a593Smuzhiyun * @skb: Current socket buffer containing buffer in progress
1435*4882a593Smuzhiyun *
1436*4882a593Smuzhiyun * This function updates next to clean. If the buffer is an EOP buffer
1437*4882a593Smuzhiyun * this function exits returning false, otherwise it will place the
1438*4882a593Smuzhiyun * sk_buff in the next buffer to be chained and return true indicating
1439*4882a593Smuzhiyun * that this is in fact a non-EOP buffer.
1440*4882a593Smuzhiyun **/
iavf_is_non_eop(struct iavf_ring * rx_ring,union iavf_rx_desc * rx_desc,struct sk_buff * skb)1441*4882a593Smuzhiyun static bool iavf_is_non_eop(struct iavf_ring *rx_ring,
1442*4882a593Smuzhiyun union iavf_rx_desc *rx_desc,
1443*4882a593Smuzhiyun struct sk_buff *skb)
1444*4882a593Smuzhiyun {
1445*4882a593Smuzhiyun u32 ntc = rx_ring->next_to_clean + 1;
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun /* fetch, update, and store next to clean */
1448*4882a593Smuzhiyun ntc = (ntc < rx_ring->count) ? ntc : 0;
1449*4882a593Smuzhiyun rx_ring->next_to_clean = ntc;
1450*4882a593Smuzhiyun
1451*4882a593Smuzhiyun prefetch(IAVF_RX_DESC(rx_ring, ntc));
1452*4882a593Smuzhiyun
1453*4882a593Smuzhiyun /* if we are the last buffer then there is nothing else to do */
1454*4882a593Smuzhiyun #define IAVF_RXD_EOF BIT(IAVF_RX_DESC_STATUS_EOF_SHIFT)
1455*4882a593Smuzhiyun if (likely(iavf_test_staterr(rx_desc, IAVF_RXD_EOF)))
1456*4882a593Smuzhiyun return false;
1457*4882a593Smuzhiyun
1458*4882a593Smuzhiyun rx_ring->rx_stats.non_eop_descs++;
1459*4882a593Smuzhiyun
1460*4882a593Smuzhiyun return true;
1461*4882a593Smuzhiyun }
1462*4882a593Smuzhiyun
1463*4882a593Smuzhiyun /**
1464*4882a593Smuzhiyun * iavf_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
1465*4882a593Smuzhiyun * @rx_ring: rx descriptor ring to transact packets on
1466*4882a593Smuzhiyun * @budget: Total limit on number of packets to process
1467*4882a593Smuzhiyun *
1468*4882a593Smuzhiyun * This function provides a "bounce buffer" approach to Rx interrupt
1469*4882a593Smuzhiyun * processing. The advantage to this is that on systems that have
1470*4882a593Smuzhiyun * expensive overhead for IOMMU access this provides a means of avoiding
1471*4882a593Smuzhiyun * it by maintaining the mapping of the page to the system.
1472*4882a593Smuzhiyun *
1473*4882a593Smuzhiyun * Returns amount of work completed
1474*4882a593Smuzhiyun **/
iavf_clean_rx_irq(struct iavf_ring * rx_ring,int budget)1475*4882a593Smuzhiyun static int iavf_clean_rx_irq(struct iavf_ring *rx_ring, int budget)
1476*4882a593Smuzhiyun {
1477*4882a593Smuzhiyun unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1478*4882a593Smuzhiyun struct sk_buff *skb = rx_ring->skb;
1479*4882a593Smuzhiyun u16 cleaned_count = IAVF_DESC_UNUSED(rx_ring);
1480*4882a593Smuzhiyun bool failure = false;
1481*4882a593Smuzhiyun
1482*4882a593Smuzhiyun while (likely(total_rx_packets < (unsigned int)budget)) {
1483*4882a593Smuzhiyun struct iavf_rx_buffer *rx_buffer;
1484*4882a593Smuzhiyun union iavf_rx_desc *rx_desc;
1485*4882a593Smuzhiyun unsigned int size;
1486*4882a593Smuzhiyun u16 vlan_tag;
1487*4882a593Smuzhiyun u8 rx_ptype;
1488*4882a593Smuzhiyun u64 qword;
1489*4882a593Smuzhiyun
1490*4882a593Smuzhiyun /* return some buffers to hardware, one at a time is too slow */
1491*4882a593Smuzhiyun if (cleaned_count >= IAVF_RX_BUFFER_WRITE) {
1492*4882a593Smuzhiyun failure = failure ||
1493*4882a593Smuzhiyun iavf_alloc_rx_buffers(rx_ring, cleaned_count);
1494*4882a593Smuzhiyun cleaned_count = 0;
1495*4882a593Smuzhiyun }
1496*4882a593Smuzhiyun
1497*4882a593Smuzhiyun rx_desc = IAVF_RX_DESC(rx_ring, rx_ring->next_to_clean);
1498*4882a593Smuzhiyun
1499*4882a593Smuzhiyun /* status_error_len will always be zero for unused descriptors
1500*4882a593Smuzhiyun * because it's cleared in cleanup, and overlaps with hdr_addr
1501*4882a593Smuzhiyun * which is always zero because packet split isn't used, if the
1502*4882a593Smuzhiyun * hardware wrote DD then the length will be non-zero
1503*4882a593Smuzhiyun */
1504*4882a593Smuzhiyun qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1505*4882a593Smuzhiyun
1506*4882a593Smuzhiyun /* This memory barrier is needed to keep us from reading
1507*4882a593Smuzhiyun * any other fields out of the rx_desc until we have
1508*4882a593Smuzhiyun * verified the descriptor has been written back.
1509*4882a593Smuzhiyun */
1510*4882a593Smuzhiyun dma_rmb();
1511*4882a593Smuzhiyun #define IAVF_RXD_DD BIT(IAVF_RX_DESC_STATUS_DD_SHIFT)
1512*4882a593Smuzhiyun if (!iavf_test_staterr(rx_desc, IAVF_RXD_DD))
1513*4882a593Smuzhiyun break;
1514*4882a593Smuzhiyun
1515*4882a593Smuzhiyun size = (qword & IAVF_RXD_QW1_LENGTH_PBUF_MASK) >>
1516*4882a593Smuzhiyun IAVF_RXD_QW1_LENGTH_PBUF_SHIFT;
1517*4882a593Smuzhiyun
1518*4882a593Smuzhiyun iavf_trace(clean_rx_irq, rx_ring, rx_desc, skb);
1519*4882a593Smuzhiyun rx_buffer = iavf_get_rx_buffer(rx_ring, size);
1520*4882a593Smuzhiyun
1521*4882a593Smuzhiyun /* retrieve a buffer from the ring */
1522*4882a593Smuzhiyun if (skb)
1523*4882a593Smuzhiyun iavf_add_rx_frag(rx_ring, rx_buffer, skb, size);
1524*4882a593Smuzhiyun else if (ring_uses_build_skb(rx_ring))
1525*4882a593Smuzhiyun skb = iavf_build_skb(rx_ring, rx_buffer, size);
1526*4882a593Smuzhiyun else
1527*4882a593Smuzhiyun skb = iavf_construct_skb(rx_ring, rx_buffer, size);
1528*4882a593Smuzhiyun
1529*4882a593Smuzhiyun /* exit if we failed to retrieve a buffer */
1530*4882a593Smuzhiyun if (!skb) {
1531*4882a593Smuzhiyun rx_ring->rx_stats.alloc_buff_failed++;
1532*4882a593Smuzhiyun if (rx_buffer && size)
1533*4882a593Smuzhiyun rx_buffer->pagecnt_bias++;
1534*4882a593Smuzhiyun break;
1535*4882a593Smuzhiyun }
1536*4882a593Smuzhiyun
1537*4882a593Smuzhiyun iavf_put_rx_buffer(rx_ring, rx_buffer);
1538*4882a593Smuzhiyun cleaned_count++;
1539*4882a593Smuzhiyun
1540*4882a593Smuzhiyun if (iavf_is_non_eop(rx_ring, rx_desc, skb))
1541*4882a593Smuzhiyun continue;
1542*4882a593Smuzhiyun
1543*4882a593Smuzhiyun /* ERR_MASK will only have valid bits if EOP set, and
1544*4882a593Smuzhiyun * what we are doing here is actually checking
1545*4882a593Smuzhiyun * IAVF_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
1546*4882a593Smuzhiyun * the error field
1547*4882a593Smuzhiyun */
1548*4882a593Smuzhiyun if (unlikely(iavf_test_staterr(rx_desc, BIT(IAVF_RXD_QW1_ERROR_SHIFT)))) {
1549*4882a593Smuzhiyun dev_kfree_skb_any(skb);
1550*4882a593Smuzhiyun skb = NULL;
1551*4882a593Smuzhiyun continue;
1552*4882a593Smuzhiyun }
1553*4882a593Smuzhiyun
1554*4882a593Smuzhiyun if (iavf_cleanup_headers(rx_ring, skb)) {
1555*4882a593Smuzhiyun skb = NULL;
1556*4882a593Smuzhiyun continue;
1557*4882a593Smuzhiyun }
1558*4882a593Smuzhiyun
1559*4882a593Smuzhiyun /* probably a little skewed due to removing CRC */
1560*4882a593Smuzhiyun total_rx_bytes += skb->len;
1561*4882a593Smuzhiyun
1562*4882a593Smuzhiyun qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1563*4882a593Smuzhiyun rx_ptype = (qword & IAVF_RXD_QW1_PTYPE_MASK) >>
1564*4882a593Smuzhiyun IAVF_RXD_QW1_PTYPE_SHIFT;
1565*4882a593Smuzhiyun
1566*4882a593Smuzhiyun /* populate checksum, VLAN, and protocol */
1567*4882a593Smuzhiyun iavf_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
1568*4882a593Smuzhiyun
1569*4882a593Smuzhiyun
1570*4882a593Smuzhiyun vlan_tag = (qword & BIT(IAVF_RX_DESC_STATUS_L2TAG1P_SHIFT)) ?
1571*4882a593Smuzhiyun le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) : 0;
1572*4882a593Smuzhiyun
1573*4882a593Smuzhiyun iavf_trace(clean_rx_irq_rx, rx_ring, rx_desc, skb);
1574*4882a593Smuzhiyun iavf_receive_skb(rx_ring, skb, vlan_tag);
1575*4882a593Smuzhiyun skb = NULL;
1576*4882a593Smuzhiyun
1577*4882a593Smuzhiyun /* update budget accounting */
1578*4882a593Smuzhiyun total_rx_packets++;
1579*4882a593Smuzhiyun }
1580*4882a593Smuzhiyun
1581*4882a593Smuzhiyun rx_ring->skb = skb;
1582*4882a593Smuzhiyun
1583*4882a593Smuzhiyun u64_stats_update_begin(&rx_ring->syncp);
1584*4882a593Smuzhiyun rx_ring->stats.packets += total_rx_packets;
1585*4882a593Smuzhiyun rx_ring->stats.bytes += total_rx_bytes;
1586*4882a593Smuzhiyun u64_stats_update_end(&rx_ring->syncp);
1587*4882a593Smuzhiyun rx_ring->q_vector->rx.total_packets += total_rx_packets;
1588*4882a593Smuzhiyun rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
1589*4882a593Smuzhiyun
1590*4882a593Smuzhiyun /* guarantee a trip back through this routine if there was a failure */
1591*4882a593Smuzhiyun return failure ? budget : (int)total_rx_packets;
1592*4882a593Smuzhiyun }
1593*4882a593Smuzhiyun
iavf_buildreg_itr(const int type,u16 itr)1594*4882a593Smuzhiyun static inline u32 iavf_buildreg_itr(const int type, u16 itr)
1595*4882a593Smuzhiyun {
1596*4882a593Smuzhiyun u32 val;
1597*4882a593Smuzhiyun
1598*4882a593Smuzhiyun /* We don't bother with setting the CLEARPBA bit as the data sheet
1599*4882a593Smuzhiyun * points out doing so is "meaningless since it was already
1600*4882a593Smuzhiyun * auto-cleared". The auto-clearing happens when the interrupt is
1601*4882a593Smuzhiyun * asserted.
1602*4882a593Smuzhiyun *
1603*4882a593Smuzhiyun * Hardware errata 28 for also indicates that writing to a
1604*4882a593Smuzhiyun * xxINT_DYN_CTLx CSR with INTENA_MSK (bit 31) set to 0 will clear
1605*4882a593Smuzhiyun * an event in the PBA anyway so we need to rely on the automask
1606*4882a593Smuzhiyun * to hold pending events for us until the interrupt is re-enabled
1607*4882a593Smuzhiyun *
1608*4882a593Smuzhiyun * The itr value is reported in microseconds, and the register
1609*4882a593Smuzhiyun * value is recorded in 2 microsecond units. For this reason we
1610*4882a593Smuzhiyun * only need to shift by the interval shift - 1 instead of the
1611*4882a593Smuzhiyun * full value.
1612*4882a593Smuzhiyun */
1613*4882a593Smuzhiyun itr &= IAVF_ITR_MASK;
1614*4882a593Smuzhiyun
1615*4882a593Smuzhiyun val = IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
1616*4882a593Smuzhiyun (type << IAVF_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
1617*4882a593Smuzhiyun (itr << (IAVF_VFINT_DYN_CTLN1_INTERVAL_SHIFT - 1));
1618*4882a593Smuzhiyun
1619*4882a593Smuzhiyun return val;
1620*4882a593Smuzhiyun }
1621*4882a593Smuzhiyun
1622*4882a593Smuzhiyun /* a small macro to shorten up some long lines */
1623*4882a593Smuzhiyun #define INTREG IAVF_VFINT_DYN_CTLN1
1624*4882a593Smuzhiyun
1625*4882a593Smuzhiyun /* The act of updating the ITR will cause it to immediately trigger. In order
1626*4882a593Smuzhiyun * to prevent this from throwing off adaptive update statistics we defer the
1627*4882a593Smuzhiyun * update so that it can only happen so often. So after either Tx or Rx are
1628*4882a593Smuzhiyun * updated we make the adaptive scheme wait until either the ITR completely
1629*4882a593Smuzhiyun * expires via the next_update expiration or we have been through at least
1630*4882a593Smuzhiyun * 3 interrupts.
1631*4882a593Smuzhiyun */
1632*4882a593Smuzhiyun #define ITR_COUNTDOWN_START 3
1633*4882a593Smuzhiyun
1634*4882a593Smuzhiyun /**
1635*4882a593Smuzhiyun * iavf_update_enable_itr - Update itr and re-enable MSIX interrupt
1636*4882a593Smuzhiyun * @vsi: the VSI we care about
1637*4882a593Smuzhiyun * @q_vector: q_vector for which itr is being updated and interrupt enabled
1638*4882a593Smuzhiyun *
1639*4882a593Smuzhiyun **/
iavf_update_enable_itr(struct iavf_vsi * vsi,struct iavf_q_vector * q_vector)1640*4882a593Smuzhiyun static inline void iavf_update_enable_itr(struct iavf_vsi *vsi,
1641*4882a593Smuzhiyun struct iavf_q_vector *q_vector)
1642*4882a593Smuzhiyun {
1643*4882a593Smuzhiyun struct iavf_hw *hw = &vsi->back->hw;
1644*4882a593Smuzhiyun u32 intval;
1645*4882a593Smuzhiyun
1646*4882a593Smuzhiyun /* These will do nothing if dynamic updates are not enabled */
1647*4882a593Smuzhiyun iavf_update_itr(q_vector, &q_vector->tx);
1648*4882a593Smuzhiyun iavf_update_itr(q_vector, &q_vector->rx);
1649*4882a593Smuzhiyun
1650*4882a593Smuzhiyun /* This block of logic allows us to get away with only updating
1651*4882a593Smuzhiyun * one ITR value with each interrupt. The idea is to perform a
1652*4882a593Smuzhiyun * pseudo-lazy update with the following criteria.
1653*4882a593Smuzhiyun *
1654*4882a593Smuzhiyun * 1. Rx is given higher priority than Tx if both are in same state
1655*4882a593Smuzhiyun * 2. If we must reduce an ITR that is given highest priority.
1656*4882a593Smuzhiyun * 3. We then give priority to increasing ITR based on amount.
1657*4882a593Smuzhiyun */
1658*4882a593Smuzhiyun if (q_vector->rx.target_itr < q_vector->rx.current_itr) {
1659*4882a593Smuzhiyun /* Rx ITR needs to be reduced, this is highest priority */
1660*4882a593Smuzhiyun intval = iavf_buildreg_itr(IAVF_RX_ITR,
1661*4882a593Smuzhiyun q_vector->rx.target_itr);
1662*4882a593Smuzhiyun q_vector->rx.current_itr = q_vector->rx.target_itr;
1663*4882a593Smuzhiyun q_vector->itr_countdown = ITR_COUNTDOWN_START;
1664*4882a593Smuzhiyun } else if ((q_vector->tx.target_itr < q_vector->tx.current_itr) ||
1665*4882a593Smuzhiyun ((q_vector->rx.target_itr - q_vector->rx.current_itr) <
1666*4882a593Smuzhiyun (q_vector->tx.target_itr - q_vector->tx.current_itr))) {
1667*4882a593Smuzhiyun /* Tx ITR needs to be reduced, this is second priority
1668*4882a593Smuzhiyun * Tx ITR needs to be increased more than Rx, fourth priority
1669*4882a593Smuzhiyun */
1670*4882a593Smuzhiyun intval = iavf_buildreg_itr(IAVF_TX_ITR,
1671*4882a593Smuzhiyun q_vector->tx.target_itr);
1672*4882a593Smuzhiyun q_vector->tx.current_itr = q_vector->tx.target_itr;
1673*4882a593Smuzhiyun q_vector->itr_countdown = ITR_COUNTDOWN_START;
1674*4882a593Smuzhiyun } else if (q_vector->rx.current_itr != q_vector->rx.target_itr) {
1675*4882a593Smuzhiyun /* Rx ITR needs to be increased, third priority */
1676*4882a593Smuzhiyun intval = iavf_buildreg_itr(IAVF_RX_ITR,
1677*4882a593Smuzhiyun q_vector->rx.target_itr);
1678*4882a593Smuzhiyun q_vector->rx.current_itr = q_vector->rx.target_itr;
1679*4882a593Smuzhiyun q_vector->itr_countdown = ITR_COUNTDOWN_START;
1680*4882a593Smuzhiyun } else {
1681*4882a593Smuzhiyun /* No ITR update, lowest priority */
1682*4882a593Smuzhiyun intval = iavf_buildreg_itr(IAVF_ITR_NONE, 0);
1683*4882a593Smuzhiyun if (q_vector->itr_countdown)
1684*4882a593Smuzhiyun q_vector->itr_countdown--;
1685*4882a593Smuzhiyun }
1686*4882a593Smuzhiyun
1687*4882a593Smuzhiyun if (!test_bit(__IAVF_VSI_DOWN, vsi->state))
1688*4882a593Smuzhiyun wr32(hw, INTREG(q_vector->reg_idx), intval);
1689*4882a593Smuzhiyun }
1690*4882a593Smuzhiyun
1691*4882a593Smuzhiyun /**
1692*4882a593Smuzhiyun * iavf_napi_poll - NAPI polling Rx/Tx cleanup routine
1693*4882a593Smuzhiyun * @napi: napi struct with our devices info in it
1694*4882a593Smuzhiyun * @budget: amount of work driver is allowed to do this pass, in packets
1695*4882a593Smuzhiyun *
1696*4882a593Smuzhiyun * This function will clean all queues associated with a q_vector.
1697*4882a593Smuzhiyun *
1698*4882a593Smuzhiyun * Returns the amount of work done
1699*4882a593Smuzhiyun **/
iavf_napi_poll(struct napi_struct * napi,int budget)1700*4882a593Smuzhiyun int iavf_napi_poll(struct napi_struct *napi, int budget)
1701*4882a593Smuzhiyun {
1702*4882a593Smuzhiyun struct iavf_q_vector *q_vector =
1703*4882a593Smuzhiyun container_of(napi, struct iavf_q_vector, napi);
1704*4882a593Smuzhiyun struct iavf_vsi *vsi = q_vector->vsi;
1705*4882a593Smuzhiyun struct iavf_ring *ring;
1706*4882a593Smuzhiyun bool clean_complete = true;
1707*4882a593Smuzhiyun bool arm_wb = false;
1708*4882a593Smuzhiyun int budget_per_ring;
1709*4882a593Smuzhiyun int work_done = 0;
1710*4882a593Smuzhiyun
1711*4882a593Smuzhiyun if (test_bit(__IAVF_VSI_DOWN, vsi->state)) {
1712*4882a593Smuzhiyun napi_complete(napi);
1713*4882a593Smuzhiyun return 0;
1714*4882a593Smuzhiyun }
1715*4882a593Smuzhiyun
1716*4882a593Smuzhiyun /* Since the actual Tx work is minimal, we can give the Tx a larger
1717*4882a593Smuzhiyun * budget and be more aggressive about cleaning up the Tx descriptors.
1718*4882a593Smuzhiyun */
1719*4882a593Smuzhiyun iavf_for_each_ring(ring, q_vector->tx) {
1720*4882a593Smuzhiyun if (!iavf_clean_tx_irq(vsi, ring, budget)) {
1721*4882a593Smuzhiyun clean_complete = false;
1722*4882a593Smuzhiyun continue;
1723*4882a593Smuzhiyun }
1724*4882a593Smuzhiyun arm_wb |= ring->arm_wb;
1725*4882a593Smuzhiyun ring->arm_wb = false;
1726*4882a593Smuzhiyun }
1727*4882a593Smuzhiyun
1728*4882a593Smuzhiyun /* Handle case where we are called by netpoll with a budget of 0 */
1729*4882a593Smuzhiyun if (budget <= 0)
1730*4882a593Smuzhiyun goto tx_only;
1731*4882a593Smuzhiyun
1732*4882a593Smuzhiyun /* We attempt to distribute budget to each Rx queue fairly, but don't
1733*4882a593Smuzhiyun * allow the budget to go below 1 because that would exit polling early.
1734*4882a593Smuzhiyun */
1735*4882a593Smuzhiyun budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
1736*4882a593Smuzhiyun
1737*4882a593Smuzhiyun iavf_for_each_ring(ring, q_vector->rx) {
1738*4882a593Smuzhiyun int cleaned = iavf_clean_rx_irq(ring, budget_per_ring);
1739*4882a593Smuzhiyun
1740*4882a593Smuzhiyun work_done += cleaned;
1741*4882a593Smuzhiyun /* if we clean as many as budgeted, we must not be done */
1742*4882a593Smuzhiyun if (cleaned >= budget_per_ring)
1743*4882a593Smuzhiyun clean_complete = false;
1744*4882a593Smuzhiyun }
1745*4882a593Smuzhiyun
1746*4882a593Smuzhiyun /* If work not completed, return budget and polling will return */
1747*4882a593Smuzhiyun if (!clean_complete) {
1748*4882a593Smuzhiyun int cpu_id = smp_processor_id();
1749*4882a593Smuzhiyun
1750*4882a593Smuzhiyun /* It is possible that the interrupt affinity has changed but,
1751*4882a593Smuzhiyun * if the cpu is pegged at 100%, polling will never exit while
1752*4882a593Smuzhiyun * traffic continues and the interrupt will be stuck on this
1753*4882a593Smuzhiyun * cpu. We check to make sure affinity is correct before we
1754*4882a593Smuzhiyun * continue to poll, otherwise we must stop polling so the
1755*4882a593Smuzhiyun * interrupt can move to the correct cpu.
1756*4882a593Smuzhiyun */
1757*4882a593Smuzhiyun if (!cpumask_test_cpu(cpu_id, &q_vector->affinity_mask)) {
1758*4882a593Smuzhiyun /* Tell napi that we are done polling */
1759*4882a593Smuzhiyun napi_complete_done(napi, work_done);
1760*4882a593Smuzhiyun
1761*4882a593Smuzhiyun /* Force an interrupt */
1762*4882a593Smuzhiyun iavf_force_wb(vsi, q_vector);
1763*4882a593Smuzhiyun
1764*4882a593Smuzhiyun /* Return budget-1 so that polling stops */
1765*4882a593Smuzhiyun return budget - 1;
1766*4882a593Smuzhiyun }
1767*4882a593Smuzhiyun tx_only:
1768*4882a593Smuzhiyun if (arm_wb) {
1769*4882a593Smuzhiyun q_vector->tx.ring[0].tx_stats.tx_force_wb++;
1770*4882a593Smuzhiyun iavf_enable_wb_on_itr(vsi, q_vector);
1771*4882a593Smuzhiyun }
1772*4882a593Smuzhiyun return budget;
1773*4882a593Smuzhiyun }
1774*4882a593Smuzhiyun
1775*4882a593Smuzhiyun if (vsi->back->flags & IAVF_TXR_FLAGS_WB_ON_ITR)
1776*4882a593Smuzhiyun q_vector->arm_wb_state = false;
1777*4882a593Smuzhiyun
1778*4882a593Smuzhiyun /* Exit the polling mode, but don't re-enable interrupts if stack might
1779*4882a593Smuzhiyun * poll us due to busy-polling
1780*4882a593Smuzhiyun */
1781*4882a593Smuzhiyun if (likely(napi_complete_done(napi, work_done)))
1782*4882a593Smuzhiyun iavf_update_enable_itr(vsi, q_vector);
1783*4882a593Smuzhiyun
1784*4882a593Smuzhiyun return min(work_done, budget - 1);
1785*4882a593Smuzhiyun }
1786*4882a593Smuzhiyun
1787*4882a593Smuzhiyun /**
1788*4882a593Smuzhiyun * iavf_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
1789*4882a593Smuzhiyun * @skb: send buffer
1790*4882a593Smuzhiyun * @tx_ring: ring to send buffer on
1791*4882a593Smuzhiyun * @flags: the tx flags to be set
1792*4882a593Smuzhiyun *
1793*4882a593Smuzhiyun * Checks the skb and set up correspondingly several generic transmit flags
1794*4882a593Smuzhiyun * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
1795*4882a593Smuzhiyun *
1796*4882a593Smuzhiyun * Returns error code indicate the frame should be dropped upon error and the
1797*4882a593Smuzhiyun * otherwise returns 0 to indicate the flags has been set properly.
1798*4882a593Smuzhiyun **/
iavf_tx_prepare_vlan_flags(struct sk_buff * skb,struct iavf_ring * tx_ring,u32 * flags)1799*4882a593Smuzhiyun static inline int iavf_tx_prepare_vlan_flags(struct sk_buff *skb,
1800*4882a593Smuzhiyun struct iavf_ring *tx_ring,
1801*4882a593Smuzhiyun u32 *flags)
1802*4882a593Smuzhiyun {
1803*4882a593Smuzhiyun __be16 protocol = skb->protocol;
1804*4882a593Smuzhiyun u32 tx_flags = 0;
1805*4882a593Smuzhiyun
1806*4882a593Smuzhiyun if (protocol == htons(ETH_P_8021Q) &&
1807*4882a593Smuzhiyun !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
1808*4882a593Smuzhiyun /* When HW VLAN acceleration is turned off by the user the
1809*4882a593Smuzhiyun * stack sets the protocol to 8021q so that the driver
1810*4882a593Smuzhiyun * can take any steps required to support the SW only
1811*4882a593Smuzhiyun * VLAN handling. In our case the driver doesn't need
1812*4882a593Smuzhiyun * to take any further steps so just set the protocol
1813*4882a593Smuzhiyun * to the encapsulated ethertype.
1814*4882a593Smuzhiyun */
1815*4882a593Smuzhiyun skb->protocol = vlan_get_protocol(skb);
1816*4882a593Smuzhiyun goto out;
1817*4882a593Smuzhiyun }
1818*4882a593Smuzhiyun
1819*4882a593Smuzhiyun /* if we have a HW VLAN tag being added, default to the HW one */
1820*4882a593Smuzhiyun if (skb_vlan_tag_present(skb)) {
1821*4882a593Smuzhiyun tx_flags |= skb_vlan_tag_get(skb) << IAVF_TX_FLAGS_VLAN_SHIFT;
1822*4882a593Smuzhiyun tx_flags |= IAVF_TX_FLAGS_HW_VLAN;
1823*4882a593Smuzhiyun /* else if it is a SW VLAN, check the next protocol and store the tag */
1824*4882a593Smuzhiyun } else if (protocol == htons(ETH_P_8021Q)) {
1825*4882a593Smuzhiyun struct vlan_hdr *vhdr, _vhdr;
1826*4882a593Smuzhiyun
1827*4882a593Smuzhiyun vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
1828*4882a593Smuzhiyun if (!vhdr)
1829*4882a593Smuzhiyun return -EINVAL;
1830*4882a593Smuzhiyun
1831*4882a593Smuzhiyun protocol = vhdr->h_vlan_encapsulated_proto;
1832*4882a593Smuzhiyun tx_flags |= ntohs(vhdr->h_vlan_TCI) << IAVF_TX_FLAGS_VLAN_SHIFT;
1833*4882a593Smuzhiyun tx_flags |= IAVF_TX_FLAGS_SW_VLAN;
1834*4882a593Smuzhiyun }
1835*4882a593Smuzhiyun
1836*4882a593Smuzhiyun out:
1837*4882a593Smuzhiyun *flags = tx_flags;
1838*4882a593Smuzhiyun return 0;
1839*4882a593Smuzhiyun }
1840*4882a593Smuzhiyun
1841*4882a593Smuzhiyun /**
1842*4882a593Smuzhiyun * iavf_tso - set up the tso context descriptor
1843*4882a593Smuzhiyun * @first: pointer to first Tx buffer for xmit
1844*4882a593Smuzhiyun * @hdr_len: ptr to the size of the packet header
1845*4882a593Smuzhiyun * @cd_type_cmd_tso_mss: Quad Word 1
1846*4882a593Smuzhiyun *
1847*4882a593Smuzhiyun * Returns 0 if no TSO can happen, 1 if tso is going, or error
1848*4882a593Smuzhiyun **/
iavf_tso(struct iavf_tx_buffer * first,u8 * hdr_len,u64 * cd_type_cmd_tso_mss)1849*4882a593Smuzhiyun static int iavf_tso(struct iavf_tx_buffer *first, u8 *hdr_len,
1850*4882a593Smuzhiyun u64 *cd_type_cmd_tso_mss)
1851*4882a593Smuzhiyun {
1852*4882a593Smuzhiyun struct sk_buff *skb = first->skb;
1853*4882a593Smuzhiyun u64 cd_cmd, cd_tso_len, cd_mss;
1854*4882a593Smuzhiyun union {
1855*4882a593Smuzhiyun struct iphdr *v4;
1856*4882a593Smuzhiyun struct ipv6hdr *v6;
1857*4882a593Smuzhiyun unsigned char *hdr;
1858*4882a593Smuzhiyun } ip;
1859*4882a593Smuzhiyun union {
1860*4882a593Smuzhiyun struct tcphdr *tcp;
1861*4882a593Smuzhiyun struct udphdr *udp;
1862*4882a593Smuzhiyun unsigned char *hdr;
1863*4882a593Smuzhiyun } l4;
1864*4882a593Smuzhiyun u32 paylen, l4_offset;
1865*4882a593Smuzhiyun u16 gso_segs, gso_size;
1866*4882a593Smuzhiyun int err;
1867*4882a593Smuzhiyun
1868*4882a593Smuzhiyun if (skb->ip_summed != CHECKSUM_PARTIAL)
1869*4882a593Smuzhiyun return 0;
1870*4882a593Smuzhiyun
1871*4882a593Smuzhiyun if (!skb_is_gso(skb))
1872*4882a593Smuzhiyun return 0;
1873*4882a593Smuzhiyun
1874*4882a593Smuzhiyun err = skb_cow_head(skb, 0);
1875*4882a593Smuzhiyun if (err < 0)
1876*4882a593Smuzhiyun return err;
1877*4882a593Smuzhiyun
1878*4882a593Smuzhiyun ip.hdr = skb_network_header(skb);
1879*4882a593Smuzhiyun l4.hdr = skb_transport_header(skb);
1880*4882a593Smuzhiyun
1881*4882a593Smuzhiyun /* initialize outer IP header fields */
1882*4882a593Smuzhiyun if (ip.v4->version == 4) {
1883*4882a593Smuzhiyun ip.v4->tot_len = 0;
1884*4882a593Smuzhiyun ip.v4->check = 0;
1885*4882a593Smuzhiyun } else {
1886*4882a593Smuzhiyun ip.v6->payload_len = 0;
1887*4882a593Smuzhiyun }
1888*4882a593Smuzhiyun
1889*4882a593Smuzhiyun if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
1890*4882a593Smuzhiyun SKB_GSO_GRE_CSUM |
1891*4882a593Smuzhiyun SKB_GSO_IPXIP4 |
1892*4882a593Smuzhiyun SKB_GSO_IPXIP6 |
1893*4882a593Smuzhiyun SKB_GSO_UDP_TUNNEL |
1894*4882a593Smuzhiyun SKB_GSO_UDP_TUNNEL_CSUM)) {
1895*4882a593Smuzhiyun if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
1896*4882a593Smuzhiyun (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
1897*4882a593Smuzhiyun l4.udp->len = 0;
1898*4882a593Smuzhiyun
1899*4882a593Smuzhiyun /* determine offset of outer transport header */
1900*4882a593Smuzhiyun l4_offset = l4.hdr - skb->data;
1901*4882a593Smuzhiyun
1902*4882a593Smuzhiyun /* remove payload length from outer checksum */
1903*4882a593Smuzhiyun paylen = skb->len - l4_offset;
1904*4882a593Smuzhiyun csum_replace_by_diff(&l4.udp->check,
1905*4882a593Smuzhiyun (__force __wsum)htonl(paylen));
1906*4882a593Smuzhiyun }
1907*4882a593Smuzhiyun
1908*4882a593Smuzhiyun /* reset pointers to inner headers */
1909*4882a593Smuzhiyun ip.hdr = skb_inner_network_header(skb);
1910*4882a593Smuzhiyun l4.hdr = skb_inner_transport_header(skb);
1911*4882a593Smuzhiyun
1912*4882a593Smuzhiyun /* initialize inner IP header fields */
1913*4882a593Smuzhiyun if (ip.v4->version == 4) {
1914*4882a593Smuzhiyun ip.v4->tot_len = 0;
1915*4882a593Smuzhiyun ip.v4->check = 0;
1916*4882a593Smuzhiyun } else {
1917*4882a593Smuzhiyun ip.v6->payload_len = 0;
1918*4882a593Smuzhiyun }
1919*4882a593Smuzhiyun }
1920*4882a593Smuzhiyun
1921*4882a593Smuzhiyun /* determine offset of inner transport header */
1922*4882a593Smuzhiyun l4_offset = l4.hdr - skb->data;
1923*4882a593Smuzhiyun
1924*4882a593Smuzhiyun /* remove payload length from inner checksum */
1925*4882a593Smuzhiyun paylen = skb->len - l4_offset;
1926*4882a593Smuzhiyun csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
1927*4882a593Smuzhiyun
1928*4882a593Smuzhiyun /* compute length of segmentation header */
1929*4882a593Smuzhiyun *hdr_len = (l4.tcp->doff * 4) + l4_offset;
1930*4882a593Smuzhiyun
1931*4882a593Smuzhiyun /* pull values out of skb_shinfo */
1932*4882a593Smuzhiyun gso_size = skb_shinfo(skb)->gso_size;
1933*4882a593Smuzhiyun gso_segs = skb_shinfo(skb)->gso_segs;
1934*4882a593Smuzhiyun
1935*4882a593Smuzhiyun /* update GSO size and bytecount with header size */
1936*4882a593Smuzhiyun first->gso_segs = gso_segs;
1937*4882a593Smuzhiyun first->bytecount += (first->gso_segs - 1) * *hdr_len;
1938*4882a593Smuzhiyun
1939*4882a593Smuzhiyun /* find the field values */
1940*4882a593Smuzhiyun cd_cmd = IAVF_TX_CTX_DESC_TSO;
1941*4882a593Smuzhiyun cd_tso_len = skb->len - *hdr_len;
1942*4882a593Smuzhiyun cd_mss = gso_size;
1943*4882a593Smuzhiyun *cd_type_cmd_tso_mss |= (cd_cmd << IAVF_TXD_CTX_QW1_CMD_SHIFT) |
1944*4882a593Smuzhiyun (cd_tso_len << IAVF_TXD_CTX_QW1_TSO_LEN_SHIFT) |
1945*4882a593Smuzhiyun (cd_mss << IAVF_TXD_CTX_QW1_MSS_SHIFT);
1946*4882a593Smuzhiyun return 1;
1947*4882a593Smuzhiyun }
1948*4882a593Smuzhiyun
1949*4882a593Smuzhiyun /**
1950*4882a593Smuzhiyun * iavf_tx_enable_csum - Enable Tx checksum offloads
1951*4882a593Smuzhiyun * @skb: send buffer
1952*4882a593Smuzhiyun * @tx_flags: pointer to Tx flags currently set
1953*4882a593Smuzhiyun * @td_cmd: Tx descriptor command bits to set
1954*4882a593Smuzhiyun * @td_offset: Tx descriptor header offsets to set
1955*4882a593Smuzhiyun * @tx_ring: Tx descriptor ring
1956*4882a593Smuzhiyun * @cd_tunneling: ptr to context desc bits
1957*4882a593Smuzhiyun **/
iavf_tx_enable_csum(struct sk_buff * skb,u32 * tx_flags,u32 * td_cmd,u32 * td_offset,struct iavf_ring * tx_ring,u32 * cd_tunneling)1958*4882a593Smuzhiyun static int iavf_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
1959*4882a593Smuzhiyun u32 *td_cmd, u32 *td_offset,
1960*4882a593Smuzhiyun struct iavf_ring *tx_ring,
1961*4882a593Smuzhiyun u32 *cd_tunneling)
1962*4882a593Smuzhiyun {
1963*4882a593Smuzhiyun union {
1964*4882a593Smuzhiyun struct iphdr *v4;
1965*4882a593Smuzhiyun struct ipv6hdr *v6;
1966*4882a593Smuzhiyun unsigned char *hdr;
1967*4882a593Smuzhiyun } ip;
1968*4882a593Smuzhiyun union {
1969*4882a593Smuzhiyun struct tcphdr *tcp;
1970*4882a593Smuzhiyun struct udphdr *udp;
1971*4882a593Smuzhiyun unsigned char *hdr;
1972*4882a593Smuzhiyun } l4;
1973*4882a593Smuzhiyun unsigned char *exthdr;
1974*4882a593Smuzhiyun u32 offset, cmd = 0;
1975*4882a593Smuzhiyun __be16 frag_off;
1976*4882a593Smuzhiyun u8 l4_proto = 0;
1977*4882a593Smuzhiyun
1978*4882a593Smuzhiyun if (skb->ip_summed != CHECKSUM_PARTIAL)
1979*4882a593Smuzhiyun return 0;
1980*4882a593Smuzhiyun
1981*4882a593Smuzhiyun ip.hdr = skb_network_header(skb);
1982*4882a593Smuzhiyun l4.hdr = skb_transport_header(skb);
1983*4882a593Smuzhiyun
1984*4882a593Smuzhiyun /* compute outer L2 header size */
1985*4882a593Smuzhiyun offset = ((ip.hdr - skb->data) / 2) << IAVF_TX_DESC_LENGTH_MACLEN_SHIFT;
1986*4882a593Smuzhiyun
1987*4882a593Smuzhiyun if (skb->encapsulation) {
1988*4882a593Smuzhiyun u32 tunnel = 0;
1989*4882a593Smuzhiyun /* define outer network header type */
1990*4882a593Smuzhiyun if (*tx_flags & IAVF_TX_FLAGS_IPV4) {
1991*4882a593Smuzhiyun tunnel |= (*tx_flags & IAVF_TX_FLAGS_TSO) ?
1992*4882a593Smuzhiyun IAVF_TX_CTX_EXT_IP_IPV4 :
1993*4882a593Smuzhiyun IAVF_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1994*4882a593Smuzhiyun
1995*4882a593Smuzhiyun l4_proto = ip.v4->protocol;
1996*4882a593Smuzhiyun } else if (*tx_flags & IAVF_TX_FLAGS_IPV6) {
1997*4882a593Smuzhiyun tunnel |= IAVF_TX_CTX_EXT_IP_IPV6;
1998*4882a593Smuzhiyun
1999*4882a593Smuzhiyun exthdr = ip.hdr + sizeof(*ip.v6);
2000*4882a593Smuzhiyun l4_proto = ip.v6->nexthdr;
2001*4882a593Smuzhiyun if (l4.hdr != exthdr)
2002*4882a593Smuzhiyun ipv6_skip_exthdr(skb, exthdr - skb->data,
2003*4882a593Smuzhiyun &l4_proto, &frag_off);
2004*4882a593Smuzhiyun }
2005*4882a593Smuzhiyun
2006*4882a593Smuzhiyun /* define outer transport */
2007*4882a593Smuzhiyun switch (l4_proto) {
2008*4882a593Smuzhiyun case IPPROTO_UDP:
2009*4882a593Smuzhiyun tunnel |= IAVF_TXD_CTX_UDP_TUNNELING;
2010*4882a593Smuzhiyun *tx_flags |= IAVF_TX_FLAGS_VXLAN_TUNNEL;
2011*4882a593Smuzhiyun break;
2012*4882a593Smuzhiyun case IPPROTO_GRE:
2013*4882a593Smuzhiyun tunnel |= IAVF_TXD_CTX_GRE_TUNNELING;
2014*4882a593Smuzhiyun *tx_flags |= IAVF_TX_FLAGS_VXLAN_TUNNEL;
2015*4882a593Smuzhiyun break;
2016*4882a593Smuzhiyun case IPPROTO_IPIP:
2017*4882a593Smuzhiyun case IPPROTO_IPV6:
2018*4882a593Smuzhiyun *tx_flags |= IAVF_TX_FLAGS_VXLAN_TUNNEL;
2019*4882a593Smuzhiyun l4.hdr = skb_inner_network_header(skb);
2020*4882a593Smuzhiyun break;
2021*4882a593Smuzhiyun default:
2022*4882a593Smuzhiyun if (*tx_flags & IAVF_TX_FLAGS_TSO)
2023*4882a593Smuzhiyun return -1;
2024*4882a593Smuzhiyun
2025*4882a593Smuzhiyun skb_checksum_help(skb);
2026*4882a593Smuzhiyun return 0;
2027*4882a593Smuzhiyun }
2028*4882a593Smuzhiyun
2029*4882a593Smuzhiyun /* compute outer L3 header size */
2030*4882a593Smuzhiyun tunnel |= ((l4.hdr - ip.hdr) / 4) <<
2031*4882a593Smuzhiyun IAVF_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
2032*4882a593Smuzhiyun
2033*4882a593Smuzhiyun /* switch IP header pointer from outer to inner header */
2034*4882a593Smuzhiyun ip.hdr = skb_inner_network_header(skb);
2035*4882a593Smuzhiyun
2036*4882a593Smuzhiyun /* compute tunnel header size */
2037*4882a593Smuzhiyun tunnel |= ((ip.hdr - l4.hdr) / 2) <<
2038*4882a593Smuzhiyun IAVF_TXD_CTX_QW0_NATLEN_SHIFT;
2039*4882a593Smuzhiyun
2040*4882a593Smuzhiyun /* indicate if we need to offload outer UDP header */
2041*4882a593Smuzhiyun if ((*tx_flags & IAVF_TX_FLAGS_TSO) &&
2042*4882a593Smuzhiyun !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
2043*4882a593Smuzhiyun (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
2044*4882a593Smuzhiyun tunnel |= IAVF_TXD_CTX_QW0_L4T_CS_MASK;
2045*4882a593Smuzhiyun
2046*4882a593Smuzhiyun /* record tunnel offload values */
2047*4882a593Smuzhiyun *cd_tunneling |= tunnel;
2048*4882a593Smuzhiyun
2049*4882a593Smuzhiyun /* switch L4 header pointer from outer to inner */
2050*4882a593Smuzhiyun l4.hdr = skb_inner_transport_header(skb);
2051*4882a593Smuzhiyun l4_proto = 0;
2052*4882a593Smuzhiyun
2053*4882a593Smuzhiyun /* reset type as we transition from outer to inner headers */
2054*4882a593Smuzhiyun *tx_flags &= ~(IAVF_TX_FLAGS_IPV4 | IAVF_TX_FLAGS_IPV6);
2055*4882a593Smuzhiyun if (ip.v4->version == 4)
2056*4882a593Smuzhiyun *tx_flags |= IAVF_TX_FLAGS_IPV4;
2057*4882a593Smuzhiyun if (ip.v6->version == 6)
2058*4882a593Smuzhiyun *tx_flags |= IAVF_TX_FLAGS_IPV6;
2059*4882a593Smuzhiyun }
2060*4882a593Smuzhiyun
2061*4882a593Smuzhiyun /* Enable IP checksum offloads */
2062*4882a593Smuzhiyun if (*tx_flags & IAVF_TX_FLAGS_IPV4) {
2063*4882a593Smuzhiyun l4_proto = ip.v4->protocol;
2064*4882a593Smuzhiyun /* the stack computes the IP header already, the only time we
2065*4882a593Smuzhiyun * need the hardware to recompute it is in the case of TSO.
2066*4882a593Smuzhiyun */
2067*4882a593Smuzhiyun cmd |= (*tx_flags & IAVF_TX_FLAGS_TSO) ?
2068*4882a593Smuzhiyun IAVF_TX_DESC_CMD_IIPT_IPV4_CSUM :
2069*4882a593Smuzhiyun IAVF_TX_DESC_CMD_IIPT_IPV4;
2070*4882a593Smuzhiyun } else if (*tx_flags & IAVF_TX_FLAGS_IPV6) {
2071*4882a593Smuzhiyun cmd |= IAVF_TX_DESC_CMD_IIPT_IPV6;
2072*4882a593Smuzhiyun
2073*4882a593Smuzhiyun exthdr = ip.hdr + sizeof(*ip.v6);
2074*4882a593Smuzhiyun l4_proto = ip.v6->nexthdr;
2075*4882a593Smuzhiyun if (l4.hdr != exthdr)
2076*4882a593Smuzhiyun ipv6_skip_exthdr(skb, exthdr - skb->data,
2077*4882a593Smuzhiyun &l4_proto, &frag_off);
2078*4882a593Smuzhiyun }
2079*4882a593Smuzhiyun
2080*4882a593Smuzhiyun /* compute inner L3 header size */
2081*4882a593Smuzhiyun offset |= ((l4.hdr - ip.hdr) / 4) << IAVF_TX_DESC_LENGTH_IPLEN_SHIFT;
2082*4882a593Smuzhiyun
2083*4882a593Smuzhiyun /* Enable L4 checksum offloads */
2084*4882a593Smuzhiyun switch (l4_proto) {
2085*4882a593Smuzhiyun case IPPROTO_TCP:
2086*4882a593Smuzhiyun /* enable checksum offloads */
2087*4882a593Smuzhiyun cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_TCP;
2088*4882a593Smuzhiyun offset |= l4.tcp->doff << IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
2089*4882a593Smuzhiyun break;
2090*4882a593Smuzhiyun case IPPROTO_SCTP:
2091*4882a593Smuzhiyun /* enable SCTP checksum offload */
2092*4882a593Smuzhiyun cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_SCTP;
2093*4882a593Smuzhiyun offset |= (sizeof(struct sctphdr) >> 2) <<
2094*4882a593Smuzhiyun IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
2095*4882a593Smuzhiyun break;
2096*4882a593Smuzhiyun case IPPROTO_UDP:
2097*4882a593Smuzhiyun /* enable UDP checksum offload */
2098*4882a593Smuzhiyun cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_UDP;
2099*4882a593Smuzhiyun offset |= (sizeof(struct udphdr) >> 2) <<
2100*4882a593Smuzhiyun IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
2101*4882a593Smuzhiyun break;
2102*4882a593Smuzhiyun default:
2103*4882a593Smuzhiyun if (*tx_flags & IAVF_TX_FLAGS_TSO)
2104*4882a593Smuzhiyun return -1;
2105*4882a593Smuzhiyun skb_checksum_help(skb);
2106*4882a593Smuzhiyun return 0;
2107*4882a593Smuzhiyun }
2108*4882a593Smuzhiyun
2109*4882a593Smuzhiyun *td_cmd |= cmd;
2110*4882a593Smuzhiyun *td_offset |= offset;
2111*4882a593Smuzhiyun
2112*4882a593Smuzhiyun return 1;
2113*4882a593Smuzhiyun }
2114*4882a593Smuzhiyun
2115*4882a593Smuzhiyun /**
2116*4882a593Smuzhiyun * iavf_create_tx_ctx Build the Tx context descriptor
2117*4882a593Smuzhiyun * @tx_ring: ring to create the descriptor on
2118*4882a593Smuzhiyun * @cd_type_cmd_tso_mss: Quad Word 1
2119*4882a593Smuzhiyun * @cd_tunneling: Quad Word 0 - bits 0-31
2120*4882a593Smuzhiyun * @cd_l2tag2: Quad Word 0 - bits 32-63
2121*4882a593Smuzhiyun **/
iavf_create_tx_ctx(struct iavf_ring * tx_ring,const u64 cd_type_cmd_tso_mss,const u32 cd_tunneling,const u32 cd_l2tag2)2122*4882a593Smuzhiyun static void iavf_create_tx_ctx(struct iavf_ring *tx_ring,
2123*4882a593Smuzhiyun const u64 cd_type_cmd_tso_mss,
2124*4882a593Smuzhiyun const u32 cd_tunneling, const u32 cd_l2tag2)
2125*4882a593Smuzhiyun {
2126*4882a593Smuzhiyun struct iavf_tx_context_desc *context_desc;
2127*4882a593Smuzhiyun int i = tx_ring->next_to_use;
2128*4882a593Smuzhiyun
2129*4882a593Smuzhiyun if ((cd_type_cmd_tso_mss == IAVF_TX_DESC_DTYPE_CONTEXT) &&
2130*4882a593Smuzhiyun !cd_tunneling && !cd_l2tag2)
2131*4882a593Smuzhiyun return;
2132*4882a593Smuzhiyun
2133*4882a593Smuzhiyun /* grab the next descriptor */
2134*4882a593Smuzhiyun context_desc = IAVF_TX_CTXTDESC(tx_ring, i);
2135*4882a593Smuzhiyun
2136*4882a593Smuzhiyun i++;
2137*4882a593Smuzhiyun tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
2138*4882a593Smuzhiyun
2139*4882a593Smuzhiyun /* cpu_to_le32 and assign to struct fields */
2140*4882a593Smuzhiyun context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
2141*4882a593Smuzhiyun context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
2142*4882a593Smuzhiyun context_desc->rsvd = cpu_to_le16(0);
2143*4882a593Smuzhiyun context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
2144*4882a593Smuzhiyun }
2145*4882a593Smuzhiyun
2146*4882a593Smuzhiyun /**
2147*4882a593Smuzhiyun * __iavf_chk_linearize - Check if there are more than 8 buffers per packet
2148*4882a593Smuzhiyun * @skb: send buffer
2149*4882a593Smuzhiyun *
2150*4882a593Smuzhiyun * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire
2151*4882a593Smuzhiyun * and so we need to figure out the cases where we need to linearize the skb.
2152*4882a593Smuzhiyun *
2153*4882a593Smuzhiyun * For TSO we need to count the TSO header and segment payload separately.
2154*4882a593Smuzhiyun * As such we need to check cases where we have 7 fragments or more as we
2155*4882a593Smuzhiyun * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
2156*4882a593Smuzhiyun * the segment payload in the first descriptor, and another 7 for the
2157*4882a593Smuzhiyun * fragments.
2158*4882a593Smuzhiyun **/
__iavf_chk_linearize(struct sk_buff * skb)2159*4882a593Smuzhiyun bool __iavf_chk_linearize(struct sk_buff *skb)
2160*4882a593Smuzhiyun {
2161*4882a593Smuzhiyun const skb_frag_t *frag, *stale;
2162*4882a593Smuzhiyun int nr_frags, sum;
2163*4882a593Smuzhiyun
2164*4882a593Smuzhiyun /* no need to check if number of frags is less than 7 */
2165*4882a593Smuzhiyun nr_frags = skb_shinfo(skb)->nr_frags;
2166*4882a593Smuzhiyun if (nr_frags < (IAVF_MAX_BUFFER_TXD - 1))
2167*4882a593Smuzhiyun return false;
2168*4882a593Smuzhiyun
2169*4882a593Smuzhiyun /* We need to walk through the list and validate that each group
2170*4882a593Smuzhiyun * of 6 fragments totals at least gso_size.
2171*4882a593Smuzhiyun */
2172*4882a593Smuzhiyun nr_frags -= IAVF_MAX_BUFFER_TXD - 2;
2173*4882a593Smuzhiyun frag = &skb_shinfo(skb)->frags[0];
2174*4882a593Smuzhiyun
2175*4882a593Smuzhiyun /* Initialize size to the negative value of gso_size minus 1. We
2176*4882a593Smuzhiyun * use this as the worst case scenerio in which the frag ahead
2177*4882a593Smuzhiyun * of us only provides one byte which is why we are limited to 6
2178*4882a593Smuzhiyun * descriptors for a single transmit as the header and previous
2179*4882a593Smuzhiyun * fragment are already consuming 2 descriptors.
2180*4882a593Smuzhiyun */
2181*4882a593Smuzhiyun sum = 1 - skb_shinfo(skb)->gso_size;
2182*4882a593Smuzhiyun
2183*4882a593Smuzhiyun /* Add size of frags 0 through 4 to create our initial sum */
2184*4882a593Smuzhiyun sum += skb_frag_size(frag++);
2185*4882a593Smuzhiyun sum += skb_frag_size(frag++);
2186*4882a593Smuzhiyun sum += skb_frag_size(frag++);
2187*4882a593Smuzhiyun sum += skb_frag_size(frag++);
2188*4882a593Smuzhiyun sum += skb_frag_size(frag++);
2189*4882a593Smuzhiyun
2190*4882a593Smuzhiyun /* Walk through fragments adding latest fragment, testing it, and
2191*4882a593Smuzhiyun * then removing stale fragments from the sum.
2192*4882a593Smuzhiyun */
2193*4882a593Smuzhiyun for (stale = &skb_shinfo(skb)->frags[0];; stale++) {
2194*4882a593Smuzhiyun int stale_size = skb_frag_size(stale);
2195*4882a593Smuzhiyun
2196*4882a593Smuzhiyun sum += skb_frag_size(frag++);
2197*4882a593Smuzhiyun
2198*4882a593Smuzhiyun /* The stale fragment may present us with a smaller
2199*4882a593Smuzhiyun * descriptor than the actual fragment size. To account
2200*4882a593Smuzhiyun * for that we need to remove all the data on the front and
2201*4882a593Smuzhiyun * figure out what the remainder would be in the last
2202*4882a593Smuzhiyun * descriptor associated with the fragment.
2203*4882a593Smuzhiyun */
2204*4882a593Smuzhiyun if (stale_size > IAVF_MAX_DATA_PER_TXD) {
2205*4882a593Smuzhiyun int align_pad = -(skb_frag_off(stale)) &
2206*4882a593Smuzhiyun (IAVF_MAX_READ_REQ_SIZE - 1);
2207*4882a593Smuzhiyun
2208*4882a593Smuzhiyun sum -= align_pad;
2209*4882a593Smuzhiyun stale_size -= align_pad;
2210*4882a593Smuzhiyun
2211*4882a593Smuzhiyun do {
2212*4882a593Smuzhiyun sum -= IAVF_MAX_DATA_PER_TXD_ALIGNED;
2213*4882a593Smuzhiyun stale_size -= IAVF_MAX_DATA_PER_TXD_ALIGNED;
2214*4882a593Smuzhiyun } while (stale_size > IAVF_MAX_DATA_PER_TXD);
2215*4882a593Smuzhiyun }
2216*4882a593Smuzhiyun
2217*4882a593Smuzhiyun /* if sum is negative we failed to make sufficient progress */
2218*4882a593Smuzhiyun if (sum < 0)
2219*4882a593Smuzhiyun return true;
2220*4882a593Smuzhiyun
2221*4882a593Smuzhiyun if (!nr_frags--)
2222*4882a593Smuzhiyun break;
2223*4882a593Smuzhiyun
2224*4882a593Smuzhiyun sum -= stale_size;
2225*4882a593Smuzhiyun }
2226*4882a593Smuzhiyun
2227*4882a593Smuzhiyun return false;
2228*4882a593Smuzhiyun }
2229*4882a593Smuzhiyun
2230*4882a593Smuzhiyun /**
2231*4882a593Smuzhiyun * __iavf_maybe_stop_tx - 2nd level check for tx stop conditions
2232*4882a593Smuzhiyun * @tx_ring: the ring to be checked
2233*4882a593Smuzhiyun * @size: the size buffer we want to assure is available
2234*4882a593Smuzhiyun *
2235*4882a593Smuzhiyun * Returns -EBUSY if a stop is needed, else 0
2236*4882a593Smuzhiyun **/
__iavf_maybe_stop_tx(struct iavf_ring * tx_ring,int size)2237*4882a593Smuzhiyun int __iavf_maybe_stop_tx(struct iavf_ring *tx_ring, int size)
2238*4882a593Smuzhiyun {
2239*4882a593Smuzhiyun netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
2240*4882a593Smuzhiyun /* Memory barrier before checking head and tail */
2241*4882a593Smuzhiyun smp_mb();
2242*4882a593Smuzhiyun
2243*4882a593Smuzhiyun /* Check again in a case another CPU has just made room available. */
2244*4882a593Smuzhiyun if (likely(IAVF_DESC_UNUSED(tx_ring) < size))
2245*4882a593Smuzhiyun return -EBUSY;
2246*4882a593Smuzhiyun
2247*4882a593Smuzhiyun /* A reprieve! - use start_queue because it doesn't call schedule */
2248*4882a593Smuzhiyun netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
2249*4882a593Smuzhiyun ++tx_ring->tx_stats.restart_queue;
2250*4882a593Smuzhiyun return 0;
2251*4882a593Smuzhiyun }
2252*4882a593Smuzhiyun
2253*4882a593Smuzhiyun /**
2254*4882a593Smuzhiyun * iavf_tx_map - Build the Tx descriptor
2255*4882a593Smuzhiyun * @tx_ring: ring to send buffer on
2256*4882a593Smuzhiyun * @skb: send buffer
2257*4882a593Smuzhiyun * @first: first buffer info buffer to use
2258*4882a593Smuzhiyun * @tx_flags: collected send information
2259*4882a593Smuzhiyun * @hdr_len: size of the packet header
2260*4882a593Smuzhiyun * @td_cmd: the command field in the descriptor
2261*4882a593Smuzhiyun * @td_offset: offset for checksum or crc
2262*4882a593Smuzhiyun **/
iavf_tx_map(struct iavf_ring * tx_ring,struct sk_buff * skb,struct iavf_tx_buffer * first,u32 tx_flags,const u8 hdr_len,u32 td_cmd,u32 td_offset)2263*4882a593Smuzhiyun static inline void iavf_tx_map(struct iavf_ring *tx_ring, struct sk_buff *skb,
2264*4882a593Smuzhiyun struct iavf_tx_buffer *first, u32 tx_flags,
2265*4882a593Smuzhiyun const u8 hdr_len, u32 td_cmd, u32 td_offset)
2266*4882a593Smuzhiyun {
2267*4882a593Smuzhiyun unsigned int data_len = skb->data_len;
2268*4882a593Smuzhiyun unsigned int size = skb_headlen(skb);
2269*4882a593Smuzhiyun skb_frag_t *frag;
2270*4882a593Smuzhiyun struct iavf_tx_buffer *tx_bi;
2271*4882a593Smuzhiyun struct iavf_tx_desc *tx_desc;
2272*4882a593Smuzhiyun u16 i = tx_ring->next_to_use;
2273*4882a593Smuzhiyun u32 td_tag = 0;
2274*4882a593Smuzhiyun dma_addr_t dma;
2275*4882a593Smuzhiyun
2276*4882a593Smuzhiyun if (tx_flags & IAVF_TX_FLAGS_HW_VLAN) {
2277*4882a593Smuzhiyun td_cmd |= IAVF_TX_DESC_CMD_IL2TAG1;
2278*4882a593Smuzhiyun td_tag = (tx_flags & IAVF_TX_FLAGS_VLAN_MASK) >>
2279*4882a593Smuzhiyun IAVF_TX_FLAGS_VLAN_SHIFT;
2280*4882a593Smuzhiyun }
2281*4882a593Smuzhiyun
2282*4882a593Smuzhiyun first->tx_flags = tx_flags;
2283*4882a593Smuzhiyun
2284*4882a593Smuzhiyun dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
2285*4882a593Smuzhiyun
2286*4882a593Smuzhiyun tx_desc = IAVF_TX_DESC(tx_ring, i);
2287*4882a593Smuzhiyun tx_bi = first;
2288*4882a593Smuzhiyun
2289*4882a593Smuzhiyun for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
2290*4882a593Smuzhiyun unsigned int max_data = IAVF_MAX_DATA_PER_TXD_ALIGNED;
2291*4882a593Smuzhiyun
2292*4882a593Smuzhiyun if (dma_mapping_error(tx_ring->dev, dma))
2293*4882a593Smuzhiyun goto dma_error;
2294*4882a593Smuzhiyun
2295*4882a593Smuzhiyun /* record length, and DMA address */
2296*4882a593Smuzhiyun dma_unmap_len_set(tx_bi, len, size);
2297*4882a593Smuzhiyun dma_unmap_addr_set(tx_bi, dma, dma);
2298*4882a593Smuzhiyun
2299*4882a593Smuzhiyun /* align size to end of page */
2300*4882a593Smuzhiyun max_data += -dma & (IAVF_MAX_READ_REQ_SIZE - 1);
2301*4882a593Smuzhiyun tx_desc->buffer_addr = cpu_to_le64(dma);
2302*4882a593Smuzhiyun
2303*4882a593Smuzhiyun while (unlikely(size > IAVF_MAX_DATA_PER_TXD)) {
2304*4882a593Smuzhiyun tx_desc->cmd_type_offset_bsz =
2305*4882a593Smuzhiyun build_ctob(td_cmd, td_offset,
2306*4882a593Smuzhiyun max_data, td_tag);
2307*4882a593Smuzhiyun
2308*4882a593Smuzhiyun tx_desc++;
2309*4882a593Smuzhiyun i++;
2310*4882a593Smuzhiyun
2311*4882a593Smuzhiyun if (i == tx_ring->count) {
2312*4882a593Smuzhiyun tx_desc = IAVF_TX_DESC(tx_ring, 0);
2313*4882a593Smuzhiyun i = 0;
2314*4882a593Smuzhiyun }
2315*4882a593Smuzhiyun
2316*4882a593Smuzhiyun dma += max_data;
2317*4882a593Smuzhiyun size -= max_data;
2318*4882a593Smuzhiyun
2319*4882a593Smuzhiyun max_data = IAVF_MAX_DATA_PER_TXD_ALIGNED;
2320*4882a593Smuzhiyun tx_desc->buffer_addr = cpu_to_le64(dma);
2321*4882a593Smuzhiyun }
2322*4882a593Smuzhiyun
2323*4882a593Smuzhiyun if (likely(!data_len))
2324*4882a593Smuzhiyun break;
2325*4882a593Smuzhiyun
2326*4882a593Smuzhiyun tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
2327*4882a593Smuzhiyun size, td_tag);
2328*4882a593Smuzhiyun
2329*4882a593Smuzhiyun tx_desc++;
2330*4882a593Smuzhiyun i++;
2331*4882a593Smuzhiyun
2332*4882a593Smuzhiyun if (i == tx_ring->count) {
2333*4882a593Smuzhiyun tx_desc = IAVF_TX_DESC(tx_ring, 0);
2334*4882a593Smuzhiyun i = 0;
2335*4882a593Smuzhiyun }
2336*4882a593Smuzhiyun
2337*4882a593Smuzhiyun size = skb_frag_size(frag);
2338*4882a593Smuzhiyun data_len -= size;
2339*4882a593Smuzhiyun
2340*4882a593Smuzhiyun dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
2341*4882a593Smuzhiyun DMA_TO_DEVICE);
2342*4882a593Smuzhiyun
2343*4882a593Smuzhiyun tx_bi = &tx_ring->tx_bi[i];
2344*4882a593Smuzhiyun }
2345*4882a593Smuzhiyun
2346*4882a593Smuzhiyun netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
2347*4882a593Smuzhiyun
2348*4882a593Smuzhiyun i++;
2349*4882a593Smuzhiyun if (i == tx_ring->count)
2350*4882a593Smuzhiyun i = 0;
2351*4882a593Smuzhiyun
2352*4882a593Smuzhiyun tx_ring->next_to_use = i;
2353*4882a593Smuzhiyun
2354*4882a593Smuzhiyun iavf_maybe_stop_tx(tx_ring, DESC_NEEDED);
2355*4882a593Smuzhiyun
2356*4882a593Smuzhiyun /* write last descriptor with RS and EOP bits */
2357*4882a593Smuzhiyun td_cmd |= IAVF_TXD_CMD;
2358*4882a593Smuzhiyun tx_desc->cmd_type_offset_bsz =
2359*4882a593Smuzhiyun build_ctob(td_cmd, td_offset, size, td_tag);
2360*4882a593Smuzhiyun
2361*4882a593Smuzhiyun skb_tx_timestamp(skb);
2362*4882a593Smuzhiyun
2363*4882a593Smuzhiyun /* Force memory writes to complete before letting h/w know there
2364*4882a593Smuzhiyun * are new descriptors to fetch.
2365*4882a593Smuzhiyun *
2366*4882a593Smuzhiyun * We also use this memory barrier to make certain all of the
2367*4882a593Smuzhiyun * status bits have been updated before next_to_watch is written.
2368*4882a593Smuzhiyun */
2369*4882a593Smuzhiyun wmb();
2370*4882a593Smuzhiyun
2371*4882a593Smuzhiyun /* set next_to_watch value indicating a packet is present */
2372*4882a593Smuzhiyun first->next_to_watch = tx_desc;
2373*4882a593Smuzhiyun
2374*4882a593Smuzhiyun /* notify HW of packet */
2375*4882a593Smuzhiyun if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more()) {
2376*4882a593Smuzhiyun writel(i, tx_ring->tail);
2377*4882a593Smuzhiyun }
2378*4882a593Smuzhiyun
2379*4882a593Smuzhiyun return;
2380*4882a593Smuzhiyun
2381*4882a593Smuzhiyun dma_error:
2382*4882a593Smuzhiyun dev_info(tx_ring->dev, "TX DMA map failed\n");
2383*4882a593Smuzhiyun
2384*4882a593Smuzhiyun /* clear dma mappings for failed tx_bi map */
2385*4882a593Smuzhiyun for (;;) {
2386*4882a593Smuzhiyun tx_bi = &tx_ring->tx_bi[i];
2387*4882a593Smuzhiyun iavf_unmap_and_free_tx_resource(tx_ring, tx_bi);
2388*4882a593Smuzhiyun if (tx_bi == first)
2389*4882a593Smuzhiyun break;
2390*4882a593Smuzhiyun if (i == 0)
2391*4882a593Smuzhiyun i = tx_ring->count;
2392*4882a593Smuzhiyun i--;
2393*4882a593Smuzhiyun }
2394*4882a593Smuzhiyun
2395*4882a593Smuzhiyun tx_ring->next_to_use = i;
2396*4882a593Smuzhiyun }
2397*4882a593Smuzhiyun
2398*4882a593Smuzhiyun /**
2399*4882a593Smuzhiyun * iavf_xmit_frame_ring - Sends buffer on Tx ring
2400*4882a593Smuzhiyun * @skb: send buffer
2401*4882a593Smuzhiyun * @tx_ring: ring to send buffer on
2402*4882a593Smuzhiyun *
2403*4882a593Smuzhiyun * Returns NETDEV_TX_OK if sent, else an error code
2404*4882a593Smuzhiyun **/
iavf_xmit_frame_ring(struct sk_buff * skb,struct iavf_ring * tx_ring)2405*4882a593Smuzhiyun static netdev_tx_t iavf_xmit_frame_ring(struct sk_buff *skb,
2406*4882a593Smuzhiyun struct iavf_ring *tx_ring)
2407*4882a593Smuzhiyun {
2408*4882a593Smuzhiyun u64 cd_type_cmd_tso_mss = IAVF_TX_DESC_DTYPE_CONTEXT;
2409*4882a593Smuzhiyun u32 cd_tunneling = 0, cd_l2tag2 = 0;
2410*4882a593Smuzhiyun struct iavf_tx_buffer *first;
2411*4882a593Smuzhiyun u32 td_offset = 0;
2412*4882a593Smuzhiyun u32 tx_flags = 0;
2413*4882a593Smuzhiyun __be16 protocol;
2414*4882a593Smuzhiyun u32 td_cmd = 0;
2415*4882a593Smuzhiyun u8 hdr_len = 0;
2416*4882a593Smuzhiyun int tso, count;
2417*4882a593Smuzhiyun
2418*4882a593Smuzhiyun /* prefetch the data, we'll need it later */
2419*4882a593Smuzhiyun prefetch(skb->data);
2420*4882a593Smuzhiyun
2421*4882a593Smuzhiyun iavf_trace(xmit_frame_ring, skb, tx_ring);
2422*4882a593Smuzhiyun
2423*4882a593Smuzhiyun count = iavf_xmit_descriptor_count(skb);
2424*4882a593Smuzhiyun if (iavf_chk_linearize(skb, count)) {
2425*4882a593Smuzhiyun if (__skb_linearize(skb)) {
2426*4882a593Smuzhiyun dev_kfree_skb_any(skb);
2427*4882a593Smuzhiyun return NETDEV_TX_OK;
2428*4882a593Smuzhiyun }
2429*4882a593Smuzhiyun count = iavf_txd_use_count(skb->len);
2430*4882a593Smuzhiyun tx_ring->tx_stats.tx_linearize++;
2431*4882a593Smuzhiyun }
2432*4882a593Smuzhiyun
2433*4882a593Smuzhiyun /* need: 1 descriptor per page * PAGE_SIZE/IAVF_MAX_DATA_PER_TXD,
2434*4882a593Smuzhiyun * + 1 desc for skb_head_len/IAVF_MAX_DATA_PER_TXD,
2435*4882a593Smuzhiyun * + 4 desc gap to avoid the cache line where head is,
2436*4882a593Smuzhiyun * + 1 desc for context descriptor,
2437*4882a593Smuzhiyun * otherwise try next time
2438*4882a593Smuzhiyun */
2439*4882a593Smuzhiyun if (iavf_maybe_stop_tx(tx_ring, count + 4 + 1)) {
2440*4882a593Smuzhiyun tx_ring->tx_stats.tx_busy++;
2441*4882a593Smuzhiyun return NETDEV_TX_BUSY;
2442*4882a593Smuzhiyun }
2443*4882a593Smuzhiyun
2444*4882a593Smuzhiyun /* record the location of the first descriptor for this packet */
2445*4882a593Smuzhiyun first = &tx_ring->tx_bi[tx_ring->next_to_use];
2446*4882a593Smuzhiyun first->skb = skb;
2447*4882a593Smuzhiyun first->bytecount = skb->len;
2448*4882a593Smuzhiyun first->gso_segs = 1;
2449*4882a593Smuzhiyun
2450*4882a593Smuzhiyun /* prepare the xmit flags */
2451*4882a593Smuzhiyun if (iavf_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
2452*4882a593Smuzhiyun goto out_drop;
2453*4882a593Smuzhiyun
2454*4882a593Smuzhiyun /* obtain protocol of skb */
2455*4882a593Smuzhiyun protocol = vlan_get_protocol(skb);
2456*4882a593Smuzhiyun
2457*4882a593Smuzhiyun /* setup IPv4/IPv6 offloads */
2458*4882a593Smuzhiyun if (protocol == htons(ETH_P_IP))
2459*4882a593Smuzhiyun tx_flags |= IAVF_TX_FLAGS_IPV4;
2460*4882a593Smuzhiyun else if (protocol == htons(ETH_P_IPV6))
2461*4882a593Smuzhiyun tx_flags |= IAVF_TX_FLAGS_IPV6;
2462*4882a593Smuzhiyun
2463*4882a593Smuzhiyun tso = iavf_tso(first, &hdr_len, &cd_type_cmd_tso_mss);
2464*4882a593Smuzhiyun
2465*4882a593Smuzhiyun if (tso < 0)
2466*4882a593Smuzhiyun goto out_drop;
2467*4882a593Smuzhiyun else if (tso)
2468*4882a593Smuzhiyun tx_flags |= IAVF_TX_FLAGS_TSO;
2469*4882a593Smuzhiyun
2470*4882a593Smuzhiyun /* Always offload the checksum, since it's in the data descriptor */
2471*4882a593Smuzhiyun tso = iavf_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
2472*4882a593Smuzhiyun tx_ring, &cd_tunneling);
2473*4882a593Smuzhiyun if (tso < 0)
2474*4882a593Smuzhiyun goto out_drop;
2475*4882a593Smuzhiyun
2476*4882a593Smuzhiyun /* always enable CRC insertion offload */
2477*4882a593Smuzhiyun td_cmd |= IAVF_TX_DESC_CMD_ICRC;
2478*4882a593Smuzhiyun
2479*4882a593Smuzhiyun iavf_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
2480*4882a593Smuzhiyun cd_tunneling, cd_l2tag2);
2481*4882a593Smuzhiyun
2482*4882a593Smuzhiyun iavf_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
2483*4882a593Smuzhiyun td_cmd, td_offset);
2484*4882a593Smuzhiyun
2485*4882a593Smuzhiyun return NETDEV_TX_OK;
2486*4882a593Smuzhiyun
2487*4882a593Smuzhiyun out_drop:
2488*4882a593Smuzhiyun iavf_trace(xmit_frame_ring_drop, first->skb, tx_ring);
2489*4882a593Smuzhiyun dev_kfree_skb_any(first->skb);
2490*4882a593Smuzhiyun first->skb = NULL;
2491*4882a593Smuzhiyun return NETDEV_TX_OK;
2492*4882a593Smuzhiyun }
2493*4882a593Smuzhiyun
2494*4882a593Smuzhiyun /**
2495*4882a593Smuzhiyun * iavf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
2496*4882a593Smuzhiyun * @skb: send buffer
2497*4882a593Smuzhiyun * @netdev: network interface device structure
2498*4882a593Smuzhiyun *
2499*4882a593Smuzhiyun * Returns NETDEV_TX_OK if sent, else an error code
2500*4882a593Smuzhiyun **/
iavf_xmit_frame(struct sk_buff * skb,struct net_device * netdev)2501*4882a593Smuzhiyun netdev_tx_t iavf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2502*4882a593Smuzhiyun {
2503*4882a593Smuzhiyun struct iavf_adapter *adapter = netdev_priv(netdev);
2504*4882a593Smuzhiyun struct iavf_ring *tx_ring = &adapter->tx_rings[skb->queue_mapping];
2505*4882a593Smuzhiyun
2506*4882a593Smuzhiyun /* hardware can't handle really short frames, hardware padding works
2507*4882a593Smuzhiyun * beyond this point
2508*4882a593Smuzhiyun */
2509*4882a593Smuzhiyun if (unlikely(skb->len < IAVF_MIN_TX_LEN)) {
2510*4882a593Smuzhiyun if (skb_pad(skb, IAVF_MIN_TX_LEN - skb->len))
2511*4882a593Smuzhiyun return NETDEV_TX_OK;
2512*4882a593Smuzhiyun skb->len = IAVF_MIN_TX_LEN;
2513*4882a593Smuzhiyun skb_set_tail_pointer(skb, IAVF_MIN_TX_LEN);
2514*4882a593Smuzhiyun }
2515*4882a593Smuzhiyun
2516*4882a593Smuzhiyun return iavf_xmit_frame_ring(skb, tx_ring);
2517*4882a593Smuzhiyun }
2518