blob: 1dbdcf8e0710c4b923eba9294979802560f9f7e4 [file] [log] [blame]
Greg Rose7f12ad72013-12-21 06:12:51 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -08004 * Copyright(c) 2013 - 2016 Intel Corporation.
Greg Rose7f12ad72013-12-21 06:12:51 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Jesse Brandeburgb8316072014-04-05 07:46:11 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
Greg Rose7f12ad72013-12-21 06:12:51 +000018 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
Paul Gortmaker7ed3f5f2014-01-11 04:00:31 +000027#include <linux/prefetch.h>
Mitch Williamsa132af22015-01-24 09:58:35 +000028#include <net/busy_poll.h>
Paul Gortmaker7ed3f5f2014-01-11 04:00:31 +000029
Greg Rose7f12ad72013-12-21 06:12:51 +000030#include "i40evf.h"
Jesse Brandeburg206812b2014-02-12 01:45:33 +000031#include "i40e_prototype.h"
Greg Rose7f12ad72013-12-21 06:12:51 +000032
33static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
34 u32 td_tag)
35{
36 return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
37 ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
38 ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
39 ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
40 ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
41}
42
43#define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
44
45/**
46 * i40e_unmap_and_free_tx_resource - Release a Tx buffer
47 * @ring: the ring that owns the buffer
48 * @tx_buffer: the buffer to free
49 **/
50static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
51 struct i40e_tx_buffer *tx_buffer)
52{
53 if (tx_buffer->skb) {
Kiran Patila42e7a32015-11-06 15:26:03 -080054 dev_kfree_skb_any(tx_buffer->skb);
Greg Rose7f12ad72013-12-21 06:12:51 +000055 if (dma_unmap_len(tx_buffer, len))
56 dma_unmap_single(ring->dev,
57 dma_unmap_addr(tx_buffer, dma),
58 dma_unmap_len(tx_buffer, len),
59 DMA_TO_DEVICE);
60 } else if (dma_unmap_len(tx_buffer, len)) {
61 dma_unmap_page(ring->dev,
62 dma_unmap_addr(tx_buffer, dma),
63 dma_unmap_len(tx_buffer, len),
64 DMA_TO_DEVICE);
65 }
Kiran Patila42e7a32015-11-06 15:26:03 -080066
67 if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
68 kfree(tx_buffer->raw_buf);
69
Greg Rose7f12ad72013-12-21 06:12:51 +000070 tx_buffer->next_to_watch = NULL;
71 tx_buffer->skb = NULL;
72 dma_unmap_len_set(tx_buffer, len, 0);
73 /* tx_buffer must be completely set up in the transmit path */
74}
75
76/**
77 * i40evf_clean_tx_ring - Free any empty Tx buffers
78 * @tx_ring: ring to be cleaned
79 **/
80void i40evf_clean_tx_ring(struct i40e_ring *tx_ring)
81{
82 unsigned long bi_size;
83 u16 i;
84
85 /* ring already cleared, nothing to do */
86 if (!tx_ring->tx_bi)
87 return;
88
89 /* Free all the Tx ring sk_buffs */
90 for (i = 0; i < tx_ring->count; i++)
91 i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
92
93 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
94 memset(tx_ring->tx_bi, 0, bi_size);
95
96 /* Zero out the descriptor ring */
97 memset(tx_ring->desc, 0, tx_ring->size);
98
99 tx_ring->next_to_use = 0;
100 tx_ring->next_to_clean = 0;
101
102 if (!tx_ring->netdev)
103 return;
104
105 /* cleanup Tx queue statistics */
106 netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
107 tx_ring->queue_index));
108}
109
110/**
111 * i40evf_free_tx_resources - Free Tx resources per queue
112 * @tx_ring: Tx descriptor ring for a specific queue
113 *
114 * Free all transmit software resources
115 **/
116void i40evf_free_tx_resources(struct i40e_ring *tx_ring)
117{
118 i40evf_clean_tx_ring(tx_ring);
119 kfree(tx_ring->tx_bi);
120 tx_ring->tx_bi = NULL;
121
122 if (tx_ring->desc) {
123 dma_free_coherent(tx_ring->dev, tx_ring->size,
124 tx_ring->desc, tx_ring->dma);
125 tx_ring->desc = NULL;
126 }
127}
128
129/**
Kiran Patil9c6c1252015-11-06 15:26:02 -0800130 * i40evf_get_tx_pending - how many Tx descriptors not processed
131 * @tx_ring: the ring of descriptors
Jesse Brandeburga68de582015-02-24 05:26:03 +0000132 *
Kiran Patil9c6c1252015-11-06 15:26:02 -0800133 * Since there is no access to the ring head register
134 * in XL710, we need to use our local copies
Jesse Brandeburga68de582015-02-24 05:26:03 +0000135 **/
Kiran Patil9c6c1252015-11-06 15:26:02 -0800136u32 i40evf_get_tx_pending(struct i40e_ring *ring)
Jesse Brandeburga68de582015-02-24 05:26:03 +0000137{
Kiran Patil9c6c1252015-11-06 15:26:02 -0800138 u32 head, tail;
Jesse Brandeburga68de582015-02-24 05:26:03 +0000139
Kiran Patil9c6c1252015-11-06 15:26:02 -0800140 head = i40e_get_head(ring);
141 tail = readl(ring->tail);
142
143 if (head != tail)
144 return (head < tail) ?
145 tail - head : (tail + ring->count - head);
146
147 return 0;
Jesse Brandeburga68de582015-02-24 05:26:03 +0000148}
149
Anjali Singhai Jainc29af372015-01-10 01:07:19 +0000150#define WB_STRIDE 0x3
151
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000152/**
Greg Rose7f12ad72013-12-21 06:12:51 +0000153 * i40e_clean_tx_irq - Reclaim resources after transmit completes
154 * @tx_ring: tx ring to clean
155 * @budget: how many cleans we're allowed
156 *
157 * Returns true if there's any budget left (e.g. the clean is finished)
158 **/
159static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
160{
161 u16 i = tx_ring->next_to_clean;
162 struct i40e_tx_buffer *tx_buf;
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000163 struct i40e_tx_desc *tx_head;
Greg Rose7f12ad72013-12-21 06:12:51 +0000164 struct i40e_tx_desc *tx_desc;
165 unsigned int total_packets = 0;
166 unsigned int total_bytes = 0;
167
168 tx_buf = &tx_ring->tx_bi[i];
169 tx_desc = I40E_TX_DESC(tx_ring, i);
170 i -= tx_ring->count;
171
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000172 tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
173
Greg Rose7f12ad72013-12-21 06:12:51 +0000174 do {
175 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
176
177 /* if next_to_watch is not set then there is no work pending */
178 if (!eop_desc)
179 break;
180
181 /* prevent any other reads prior to eop_desc */
182 read_barrier_depends();
183
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000184 /* we have caught up to head, no work left to do */
185 if (tx_head == tx_desc)
Greg Rose7f12ad72013-12-21 06:12:51 +0000186 break;
187
188 /* clear next_to_watch to prevent false hangs */
189 tx_buf->next_to_watch = NULL;
190
191 /* update the statistics for this packet */
192 total_bytes += tx_buf->bytecount;
193 total_packets += tx_buf->gso_segs;
194
195 /* free the skb */
196 dev_kfree_skb_any(tx_buf->skb);
197
198 /* unmap skb header data */
199 dma_unmap_single(tx_ring->dev,
200 dma_unmap_addr(tx_buf, dma),
201 dma_unmap_len(tx_buf, len),
202 DMA_TO_DEVICE);
203
204 /* clear tx_buffer data */
205 tx_buf->skb = NULL;
206 dma_unmap_len_set(tx_buf, len, 0);
207
208 /* unmap remaining buffers */
209 while (tx_desc != eop_desc) {
210
211 tx_buf++;
212 tx_desc++;
213 i++;
214 if (unlikely(!i)) {
215 i -= tx_ring->count;
216 tx_buf = tx_ring->tx_bi;
217 tx_desc = I40E_TX_DESC(tx_ring, 0);
218 }
219
220 /* unmap any remaining paged data */
221 if (dma_unmap_len(tx_buf, len)) {
222 dma_unmap_page(tx_ring->dev,
223 dma_unmap_addr(tx_buf, dma),
224 dma_unmap_len(tx_buf, len),
225 DMA_TO_DEVICE);
226 dma_unmap_len_set(tx_buf, len, 0);
227 }
228 }
229
230 /* move us one more past the eop_desc for start of next pkt */
231 tx_buf++;
232 tx_desc++;
233 i++;
234 if (unlikely(!i)) {
235 i -= tx_ring->count;
236 tx_buf = tx_ring->tx_bi;
237 tx_desc = I40E_TX_DESC(tx_ring, 0);
238 }
239
Jesse Brandeburg016890b2015-02-27 09:15:31 +0000240 prefetch(tx_desc);
241
Greg Rose7f12ad72013-12-21 06:12:51 +0000242 /* update budget accounting */
243 budget--;
244 } while (likely(budget));
245
246 i += tx_ring->count;
247 tx_ring->next_to_clean = i;
248 u64_stats_update_begin(&tx_ring->syncp);
249 tx_ring->stats.bytes += total_bytes;
250 tx_ring->stats.packets += total_packets;
251 u64_stats_update_end(&tx_ring->syncp);
252 tx_ring->q_vector->tx.total_bytes += total_bytes;
253 tx_ring->q_vector->tx.total_packets += total_packets;
254
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -0800255 if (tx_ring->flags & I40E_TXR_FLAGS_WB_ON_ITR) {
256 unsigned int j = 0;
257 /* check to see if there are < 4 descriptors
258 * waiting to be written back, then kick the hardware to force
259 * them to be written back in case we stay in NAPI.
260 * In this mode on X722 we do not enable Interrupt.
261 */
262 j = i40evf_get_tx_pending(tx_ring);
263
264 if (budget &&
265 ((j / (WB_STRIDE + 1)) == 0) && (j > 0) &&
266 !test_bit(__I40E_DOWN, &tx_ring->vsi->state) &&
267 (I40E_DESC_UNUSED(tx_ring) != tx_ring->count))
268 tx_ring->arm_wb = true;
269 }
270
Greg Rose7f12ad72013-12-21 06:12:51 +0000271 netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
272 tx_ring->queue_index),
273 total_packets, total_bytes);
274
275#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
276 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
277 (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
278 /* Make sure that anybody stopping the queue after this
279 * sees the new next_to_clean.
280 */
281 smp_mb();
282 if (__netif_subqueue_stopped(tx_ring->netdev,
283 tx_ring->queue_index) &&
284 !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
285 netif_wake_subqueue(tx_ring->netdev,
286 tx_ring->queue_index);
287 ++tx_ring->tx_stats.restart_queue;
288 }
289 }
290
Kiran Patilb03a8c12015-09-24 18:13:15 -0400291 return !!budget;
Greg Rose7f12ad72013-12-21 06:12:51 +0000292}
293
294/**
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -0800295 * i40evf_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
296 * @vsi: the VSI we care about
297 * @q_vector: the vector on which to enable writeback
298 *
299 **/
300static void i40e_enable_wb_on_itr(struct i40e_vsi *vsi,
301 struct i40e_q_vector *q_vector)
302{
303 u16 flags = q_vector->tx.ring[0].flags;
304 u32 val;
305
306 if (!(flags & I40E_TXR_FLAGS_WB_ON_ITR))
307 return;
308
309 if (q_vector->arm_wb_state)
310 return;
311
312 val = I40E_VFINT_DYN_CTLN1_WB_ON_ITR_MASK |
313 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK; /* set noitr */
314
315 wr32(&vsi->back->hw,
316 I40E_VFINT_DYN_CTLN1(q_vector->v_idx +
317 vsi->base_vector - 1), val);
318 q_vector->arm_wb_state = true;
319}
320
321/**
322 * i40evf_force_wb - Issue SW Interrupt so HW does a wb
Anjali Singhai Jainc29af372015-01-10 01:07:19 +0000323 * @vsi: the VSI we care about
324 * @q_vector: the vector on which to force writeback
325 *
326 **/
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -0800327void i40evf_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
Anjali Singhai Jainc29af372015-01-10 01:07:19 +0000328{
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -0800329 u32 val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
330 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK | /* set noitr */
331 I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
332 I40E_VFINT_DYN_CTLN1_SW_ITR_INDX_ENA_MASK
333 /* allow 00 to be written to the index */;
Anjali Singhai Jainc29af372015-01-10 01:07:19 +0000334
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -0800335 wr32(&vsi->back->hw,
336 I40E_VFINT_DYN_CTLN1(q_vector->v_idx + vsi->base_vector - 1),
337 val);
Anjali Singhai Jainc29af372015-01-10 01:07:19 +0000338}
339
340/**
Greg Rose7f12ad72013-12-21 06:12:51 +0000341 * i40e_set_new_dynamic_itr - Find new ITR level
342 * @rc: structure containing ring performance data
343 *
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -0400344 * Returns true if ITR changed, false if not
345 *
Greg Rose7f12ad72013-12-21 06:12:51 +0000346 * Stores a new ITR value based on packets and byte counts during
347 * the last interrupt. The advantage of per interrupt computation
348 * is faster updates and more accurate ITR for the current traffic
349 * pattern. Constants in this function were computed based on
350 * theoretical maximum wire speed and thresholds were set based on
351 * testing data as well as attempting to minimize response time
352 * while increasing bulk throughput.
353 **/
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -0400354static bool i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
Greg Rose7f12ad72013-12-21 06:12:51 +0000355{
356 enum i40e_latency_range new_latency_range = rc->latency_range;
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400357 struct i40e_q_vector *qv = rc->ring->q_vector;
Greg Rose7f12ad72013-12-21 06:12:51 +0000358 u32 new_itr = rc->itr;
359 int bytes_per_int;
Jesse Brandeburg51cc6d92015-09-28 14:16:52 -0400360 int usecs;
Greg Rose7f12ad72013-12-21 06:12:51 +0000361
362 if (rc->total_packets == 0 || !rc->itr)
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -0400363 return false;
Greg Rose7f12ad72013-12-21 06:12:51 +0000364
365 /* simple throttlerate management
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400366 * 0-10MB/s lowest (50000 ints/s)
Greg Rose7f12ad72013-12-21 06:12:51 +0000367 * 10-20MB/s low (20000 ints/s)
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400368 * 20-1249MB/s bulk (18000 ints/s)
369 * > 40000 Rx packets per second (8000 ints/s)
Jesse Brandeburg51cc6d92015-09-28 14:16:52 -0400370 *
371 * The math works out because the divisor is in 10^(-6) which
372 * turns the bytes/us input value into MB/s values, but
373 * make sure to use usecs, as the register values written
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400374 * are in 2 usec increments in the ITR registers, and make sure
375 * to use the smoothed values that the countdown timer gives us.
Greg Rose7f12ad72013-12-21 06:12:51 +0000376 */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400377 usecs = (rc->itr << 1) * ITR_COUNTDOWN_START;
Jesse Brandeburg51cc6d92015-09-28 14:16:52 -0400378 bytes_per_int = rc->total_bytes / usecs;
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400379
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -0400380 switch (new_latency_range) {
Greg Rose7f12ad72013-12-21 06:12:51 +0000381 case I40E_LOWEST_LATENCY:
382 if (bytes_per_int > 10)
383 new_latency_range = I40E_LOW_LATENCY;
384 break;
385 case I40E_LOW_LATENCY:
386 if (bytes_per_int > 20)
387 new_latency_range = I40E_BULK_LATENCY;
388 else if (bytes_per_int <= 10)
389 new_latency_range = I40E_LOWEST_LATENCY;
390 break;
391 case I40E_BULK_LATENCY:
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400392 case I40E_ULTRA_LATENCY:
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -0400393 default:
394 if (bytes_per_int <= 20)
395 new_latency_range = I40E_LOW_LATENCY;
Greg Rose7f12ad72013-12-21 06:12:51 +0000396 break;
397 }
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400398
399 /* this is to adjust RX more aggressively when streaming small
400 * packets. The value of 40000 was picked as it is just beyond
401 * what the hardware can receive per second if in low latency
402 * mode.
403 */
404#define RX_ULTRA_PACKET_RATE 40000
405
406 if ((((rc->total_packets * 1000000) / usecs) > RX_ULTRA_PACKET_RATE) &&
407 (&qv->rx == rc))
408 new_latency_range = I40E_ULTRA_LATENCY;
409
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -0400410 rc->latency_range = new_latency_range;
Greg Rose7f12ad72013-12-21 06:12:51 +0000411
412 switch (new_latency_range) {
413 case I40E_LOWEST_LATENCY:
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400414 new_itr = I40E_ITR_50K;
Greg Rose7f12ad72013-12-21 06:12:51 +0000415 break;
416 case I40E_LOW_LATENCY:
417 new_itr = I40E_ITR_20K;
418 break;
419 case I40E_BULK_LATENCY:
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400420 new_itr = I40E_ITR_18K;
421 break;
422 case I40E_ULTRA_LATENCY:
Greg Rose7f12ad72013-12-21 06:12:51 +0000423 new_itr = I40E_ITR_8K;
424 break;
425 default:
426 break;
427 }
428
Greg Rose7f12ad72013-12-21 06:12:51 +0000429 rc->total_bytes = 0;
430 rc->total_packets = 0;
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -0400431
432 if (new_itr != rc->itr) {
433 rc->itr = new_itr;
434 return true;
435 }
436
437 return false;
Greg Rose7f12ad72013-12-21 06:12:51 +0000438}
439
Jesse Brandeburg4eeb1ff2015-11-18 17:35:42 -0800440/**
Greg Rose7f12ad72013-12-21 06:12:51 +0000441 * i40evf_setup_tx_descriptors - Allocate the Tx descriptors
442 * @tx_ring: the tx ring to set up
443 *
444 * Return 0 on success, negative on error
445 **/
446int i40evf_setup_tx_descriptors(struct i40e_ring *tx_ring)
447{
448 struct device *dev = tx_ring->dev;
449 int bi_size;
450
451 if (!dev)
452 return -ENOMEM;
453
Mitch Williams67c818a2015-06-19 08:56:30 -0700454 /* warn if we are about to overwrite the pointer */
455 WARN_ON(tx_ring->tx_bi);
Greg Rose7f12ad72013-12-21 06:12:51 +0000456 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
457 tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
458 if (!tx_ring->tx_bi)
459 goto err;
460
461 /* round up to nearest 4K */
462 tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000463 /* add u32 for head writeback, align after this takes care of
464 * guaranteeing this is at least one cache line in size
465 */
466 tx_ring->size += sizeof(u32);
Greg Rose7f12ad72013-12-21 06:12:51 +0000467 tx_ring->size = ALIGN(tx_ring->size, 4096);
468 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
469 &tx_ring->dma, GFP_KERNEL);
470 if (!tx_ring->desc) {
471 dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
472 tx_ring->size);
473 goto err;
474 }
475
476 tx_ring->next_to_use = 0;
477 tx_ring->next_to_clean = 0;
478 return 0;
479
480err:
481 kfree(tx_ring->tx_bi);
482 tx_ring->tx_bi = NULL;
483 return -ENOMEM;
484}
485
486/**
487 * i40evf_clean_rx_ring - Free Rx buffers
488 * @rx_ring: ring to be cleaned
489 **/
490void i40evf_clean_rx_ring(struct i40e_ring *rx_ring)
491{
492 struct device *dev = rx_ring->dev;
493 struct i40e_rx_buffer *rx_bi;
494 unsigned long bi_size;
495 u16 i;
496
497 /* ring already cleared, nothing to do */
498 if (!rx_ring->rx_bi)
499 return;
500
Mitch Williamsa132af22015-01-24 09:58:35 +0000501 if (ring_is_ps_enabled(rx_ring)) {
502 int bufsz = ALIGN(rx_ring->rx_hdr_len, 256) * rx_ring->count;
503
504 rx_bi = &rx_ring->rx_bi[0];
505 if (rx_bi->hdr_buf) {
506 dma_free_coherent(dev,
507 bufsz,
508 rx_bi->hdr_buf,
509 rx_bi->dma);
510 for (i = 0; i < rx_ring->count; i++) {
511 rx_bi = &rx_ring->rx_bi[i];
512 rx_bi->dma = 0;
Shannon Nelson37a29732015-02-27 09:15:19 +0000513 rx_bi->hdr_buf = NULL;
Mitch Williamsa132af22015-01-24 09:58:35 +0000514 }
515 }
516 }
Greg Rose7f12ad72013-12-21 06:12:51 +0000517 /* Free all the Rx ring sk_buffs */
518 for (i = 0; i < rx_ring->count; i++) {
519 rx_bi = &rx_ring->rx_bi[i];
520 if (rx_bi->dma) {
521 dma_unmap_single(dev,
522 rx_bi->dma,
523 rx_ring->rx_buf_len,
524 DMA_FROM_DEVICE);
525 rx_bi->dma = 0;
526 }
527 if (rx_bi->skb) {
528 dev_kfree_skb(rx_bi->skb);
529 rx_bi->skb = NULL;
530 }
531 if (rx_bi->page) {
532 if (rx_bi->page_dma) {
533 dma_unmap_page(dev,
534 rx_bi->page_dma,
535 PAGE_SIZE / 2,
536 DMA_FROM_DEVICE);
537 rx_bi->page_dma = 0;
538 }
539 __free_page(rx_bi->page);
540 rx_bi->page = NULL;
541 rx_bi->page_offset = 0;
542 }
543 }
544
545 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
546 memset(rx_ring->rx_bi, 0, bi_size);
547
548 /* Zero out the descriptor ring */
549 memset(rx_ring->desc, 0, rx_ring->size);
550
551 rx_ring->next_to_clean = 0;
552 rx_ring->next_to_use = 0;
553}
554
555/**
556 * i40evf_free_rx_resources - Free Rx resources
557 * @rx_ring: ring to clean the resources from
558 *
559 * Free all receive software resources
560 **/
561void i40evf_free_rx_resources(struct i40e_ring *rx_ring)
562{
563 i40evf_clean_rx_ring(rx_ring);
564 kfree(rx_ring->rx_bi);
565 rx_ring->rx_bi = NULL;
566
567 if (rx_ring->desc) {
568 dma_free_coherent(rx_ring->dev, rx_ring->size,
569 rx_ring->desc, rx_ring->dma);
570 rx_ring->desc = NULL;
571 }
572}
573
574/**
Mitch Williamsa132af22015-01-24 09:58:35 +0000575 * i40evf_alloc_rx_headers - allocate rx header buffers
576 * @rx_ring: ring to alloc buffers
577 *
578 * Allocate rx header buffers for the entire ring. As these are static,
579 * this is only called when setting up a new ring.
580 **/
581void i40evf_alloc_rx_headers(struct i40e_ring *rx_ring)
582{
583 struct device *dev = rx_ring->dev;
584 struct i40e_rx_buffer *rx_bi;
585 dma_addr_t dma;
586 void *buffer;
587 int buf_size;
588 int i;
589
590 if (rx_ring->rx_bi[0].hdr_buf)
591 return;
592 /* Make sure the buffers don't cross cache line boundaries. */
593 buf_size = ALIGN(rx_ring->rx_hdr_len, 256);
594 buffer = dma_alloc_coherent(dev, buf_size * rx_ring->count,
595 &dma, GFP_KERNEL);
596 if (!buffer)
597 return;
598 for (i = 0; i < rx_ring->count; i++) {
599 rx_bi = &rx_ring->rx_bi[i];
600 rx_bi->dma = dma + (i * buf_size);
601 rx_bi->hdr_buf = buffer + (i * buf_size);
602 }
603}
604
605/**
Greg Rose7f12ad72013-12-21 06:12:51 +0000606 * i40evf_setup_rx_descriptors - Allocate Rx descriptors
607 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
608 *
609 * Returns 0 on success, negative on failure
610 **/
611int i40evf_setup_rx_descriptors(struct i40e_ring *rx_ring)
612{
613 struct device *dev = rx_ring->dev;
614 int bi_size;
615
Mitch Williams67c818a2015-06-19 08:56:30 -0700616 /* warn if we are about to overwrite the pointer */
617 WARN_ON(rx_ring->rx_bi);
Greg Rose7f12ad72013-12-21 06:12:51 +0000618 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
619 rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
620 if (!rx_ring->rx_bi)
621 goto err;
622
Carolyn Wybornyf217d6c2015-02-09 17:42:31 -0800623 u64_stats_init(&rx_ring->syncp);
Carolyn Wyborny638702b2015-01-24 09:58:32 +0000624
Greg Rose7f12ad72013-12-21 06:12:51 +0000625 /* Round up to nearest 4K */
626 rx_ring->size = ring_is_16byte_desc_enabled(rx_ring)
627 ? rx_ring->count * sizeof(union i40e_16byte_rx_desc)
628 : rx_ring->count * sizeof(union i40e_32byte_rx_desc);
629 rx_ring->size = ALIGN(rx_ring->size, 4096);
630 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
631 &rx_ring->dma, GFP_KERNEL);
632
633 if (!rx_ring->desc) {
634 dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
635 rx_ring->size);
636 goto err;
637 }
638
639 rx_ring->next_to_clean = 0;
640 rx_ring->next_to_use = 0;
641
642 return 0;
643err:
644 kfree(rx_ring->rx_bi);
645 rx_ring->rx_bi = NULL;
646 return -ENOMEM;
647}
648
649/**
650 * i40e_release_rx_desc - Store the new tail and head values
651 * @rx_ring: ring to bump
652 * @val: new head index
653 **/
654static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
655{
656 rx_ring->next_to_use = val;
657 /* Force memory writes to complete before letting h/w
658 * know there are new descriptors to fetch. (Only
659 * applicable for weak-ordered memory model archs,
660 * such as IA-64).
661 */
662 wmb();
663 writel(val, rx_ring->tail);
664}
665
666/**
Mitch Williamsa132af22015-01-24 09:58:35 +0000667 * i40evf_alloc_rx_buffers_ps - Replace used receive buffers; packet split
Greg Rose7f12ad72013-12-21 06:12:51 +0000668 * @rx_ring: ring to place buffers on
669 * @cleaned_count: number of buffers to replace
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800670 *
671 * Returns true if any errors on allocation
Greg Rose7f12ad72013-12-21 06:12:51 +0000672 **/
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800673bool i40evf_alloc_rx_buffers_ps(struct i40e_ring *rx_ring, u16 cleaned_count)
Mitch Williamsa132af22015-01-24 09:58:35 +0000674{
675 u16 i = rx_ring->next_to_use;
676 union i40e_rx_desc *rx_desc;
677 struct i40e_rx_buffer *bi;
678
679 /* do nothing if no valid netdev defined */
680 if (!rx_ring->netdev || !cleaned_count)
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800681 return false;
Mitch Williamsa132af22015-01-24 09:58:35 +0000682
683 while (cleaned_count--) {
684 rx_desc = I40E_RX_DESC(rx_ring, i);
685 bi = &rx_ring->rx_bi[i];
686
687 if (bi->skb) /* desc is in use */
688 goto no_buffers;
689 if (!bi->page) {
690 bi->page = alloc_page(GFP_ATOMIC);
691 if (!bi->page) {
692 rx_ring->rx_stats.alloc_page_failed++;
693 goto no_buffers;
694 }
695 }
696
697 if (!bi->page_dma) {
698 /* use a half page if we're re-using */
699 bi->page_offset ^= PAGE_SIZE / 2;
700 bi->page_dma = dma_map_page(rx_ring->dev,
701 bi->page,
702 bi->page_offset,
703 PAGE_SIZE / 2,
704 DMA_FROM_DEVICE);
705 if (dma_mapping_error(rx_ring->dev,
706 bi->page_dma)) {
707 rx_ring->rx_stats.alloc_page_failed++;
708 bi->page_dma = 0;
709 goto no_buffers;
710 }
711 }
712
713 dma_sync_single_range_for_device(rx_ring->dev,
Jesse Brandeburg3578fa02016-01-04 10:33:03 -0800714 rx_ring->rx_bi[0].dma,
715 i * rx_ring->rx_hdr_len,
Mitch Williamsa132af22015-01-24 09:58:35 +0000716 rx_ring->rx_hdr_len,
717 DMA_FROM_DEVICE);
718 /* Refresh the desc even if buffer_addrs didn't change
719 * because each write-back erases this info.
720 */
721 rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
722 rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
723 i++;
724 if (i == rx_ring->count)
725 i = 0;
726 }
727
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800728 if (rx_ring->next_to_use != i)
729 i40e_release_rx_desc(rx_ring, i);
730
731 return false;
732
Mitch Williamsa132af22015-01-24 09:58:35 +0000733no_buffers:
734 if (rx_ring->next_to_use != i)
735 i40e_release_rx_desc(rx_ring, i);
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800736
737 /* make sure to come back via polling to try again after
738 * allocation failure
739 */
740 return true;
Mitch Williamsa132af22015-01-24 09:58:35 +0000741}
742
743/**
744 * i40evf_alloc_rx_buffers_1buf - Replace used receive buffers; single buffer
745 * @rx_ring: ring to place buffers on
746 * @cleaned_count: number of buffers to replace
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800747 *
748 * Returns true if any errors on allocation
Mitch Williamsa132af22015-01-24 09:58:35 +0000749 **/
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800750bool i40evf_alloc_rx_buffers_1buf(struct i40e_ring *rx_ring, u16 cleaned_count)
Greg Rose7f12ad72013-12-21 06:12:51 +0000751{
752 u16 i = rx_ring->next_to_use;
753 union i40e_rx_desc *rx_desc;
754 struct i40e_rx_buffer *bi;
755 struct sk_buff *skb;
756
757 /* do nothing if no valid netdev defined */
758 if (!rx_ring->netdev || !cleaned_count)
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800759 return false;
Greg Rose7f12ad72013-12-21 06:12:51 +0000760
761 while (cleaned_count--) {
762 rx_desc = I40E_RX_DESC(rx_ring, i);
763 bi = &rx_ring->rx_bi[i];
764 skb = bi->skb;
765
766 if (!skb) {
Jesse Brandeburgdd1a5df2016-01-13 16:51:48 -0800767 skb = __netdev_alloc_skb_ip_align(rx_ring->netdev,
768 rx_ring->rx_buf_len,
769 GFP_ATOMIC |
770 __GFP_NOWARN);
Greg Rose7f12ad72013-12-21 06:12:51 +0000771 if (!skb) {
772 rx_ring->rx_stats.alloc_buff_failed++;
773 goto no_buffers;
774 }
775 /* initialize queue mapping */
776 skb_record_rx_queue(skb, rx_ring->queue_index);
777 bi->skb = skb;
778 }
779
780 if (!bi->dma) {
781 bi->dma = dma_map_single(rx_ring->dev,
782 skb->data,
783 rx_ring->rx_buf_len,
784 DMA_FROM_DEVICE);
785 if (dma_mapping_error(rx_ring->dev, bi->dma)) {
786 rx_ring->rx_stats.alloc_buff_failed++;
787 bi->dma = 0;
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800788 dev_kfree_skb(bi->skb);
789 bi->skb = NULL;
Greg Rose7f12ad72013-12-21 06:12:51 +0000790 goto no_buffers;
791 }
792 }
793
Mitch Williamsa132af22015-01-24 09:58:35 +0000794 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
795 rx_desc->read.hdr_addr = 0;
Greg Rose7f12ad72013-12-21 06:12:51 +0000796 i++;
797 if (i == rx_ring->count)
798 i = 0;
799 }
800
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800801 if (rx_ring->next_to_use != i)
802 i40e_release_rx_desc(rx_ring, i);
803
804 return false;
805
Greg Rose7f12ad72013-12-21 06:12:51 +0000806no_buffers:
807 if (rx_ring->next_to_use != i)
808 i40e_release_rx_desc(rx_ring, i);
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800809
810 /* make sure to come back via polling to try again after
811 * allocation failure
812 */
813 return true;
Greg Rose7f12ad72013-12-21 06:12:51 +0000814}
815
816/**
817 * i40e_receive_skb - Send a completed packet up the stack
818 * @rx_ring: rx ring in play
819 * @skb: packet to send up
820 * @vlan_tag: vlan tag for packet
821 **/
822static void i40e_receive_skb(struct i40e_ring *rx_ring,
823 struct sk_buff *skb, u16 vlan_tag)
824{
825 struct i40e_q_vector *q_vector = rx_ring->q_vector;
Greg Rose7f12ad72013-12-21 06:12:51 +0000826
827 if (vlan_tag & VLAN_VID_MASK)
828 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
829
Alexander Duyck8b650352015-09-24 09:04:32 -0700830 napi_gro_receive(&q_vector->napi, skb);
Greg Rose7f12ad72013-12-21 06:12:51 +0000831}
832
833/**
834 * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
835 * @vsi: the VSI we care about
836 * @skb: skb currently being received and modified
837 * @rx_status: status value of last descriptor in packet
838 * @rx_error: error value of last descriptor in packet
839 * @rx_ptype: ptype value of last descriptor in packet
840 **/
841static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
842 struct sk_buff *skb,
843 u32 rx_status,
844 u32 rx_error,
845 u16 rx_ptype)
846{
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000847 struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(rx_ptype);
848 bool ipv4 = false, ipv6 = false;
Greg Rose7f12ad72013-12-21 06:12:51 +0000849 bool ipv4_tunnel, ipv6_tunnel;
850 __wsum rx_udp_csum;
Greg Rose7f12ad72013-12-21 06:12:51 +0000851 struct iphdr *iph;
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000852 __sum16 csum;
Greg Rose7f12ad72013-12-21 06:12:51 +0000853
Anjali Singhai Jainf8faaa42015-02-24 06:58:48 +0000854 ipv4_tunnel = (rx_ptype >= I40E_RX_PTYPE_GRENAT4_MAC_PAY3) &&
855 (rx_ptype <= I40E_RX_PTYPE_GRENAT4_MACVLAN_IPV6_ICMP_PAY4);
856 ipv6_tunnel = (rx_ptype >= I40E_RX_PTYPE_GRENAT6_MAC_PAY3) &&
857 (rx_ptype <= I40E_RX_PTYPE_GRENAT6_MACVLAN_IPV6_ICMP_PAY4);
Greg Rose7f12ad72013-12-21 06:12:51 +0000858
Greg Rose7f12ad72013-12-21 06:12:51 +0000859 skb->ip_summed = CHECKSUM_NONE;
860
861 /* Rx csum enabled and ip headers found? */
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000862 if (!(vsi->netdev->features & NETIF_F_RXCSUM))
Greg Rose7f12ad72013-12-21 06:12:51 +0000863 return;
864
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000865 /* did the hardware decode the packet and checksum? */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400866 if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000867 return;
868
869 /* both known and outer_ip must be set for the below code to work */
870 if (!(decoded.known && decoded.outer_ip))
871 return;
872
873 if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
874 decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4)
875 ipv4 = true;
876 else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
877 decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6)
878 ipv6 = true;
879
880 if (ipv4 &&
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400881 (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
882 BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000883 goto checksum_fail;
884
Jesse Brandeburgddf1d0d2014-02-13 03:48:39 -0800885 /* likely incorrect csum if alternate IP extension headers found */
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000886 if (ipv6 &&
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400887 rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000888 /* don't increment checksum err here, non-fatal err */
Greg Rose7f12ad72013-12-21 06:12:51 +0000889 return;
890
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000891 /* there was some L4 error, count error and punt packet to the stack */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400892 if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000893 goto checksum_fail;
Greg Rose7f12ad72013-12-21 06:12:51 +0000894
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000895 /* handle packets that were not able to be checksummed due
896 * to arrival speed, in this case the stack can compute
897 * the csum.
898 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400899 if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000900 return;
901
902 /* If VXLAN traffic has an outer UDPv4 checksum we need to check
903 * it in the driver, hardware does not do it for us.
904 * Since L3L4P bit was set we assume a valid IHL value (>=5)
905 * so the total length of IPv4 header is IHL*4 bytes
906 * The UDP_0 bit *may* bet set if the *inner* header is UDP
907 */
Anjali Singhai Jain818f2e72015-03-31 00:44:59 -0700908 if (ipv4_tunnel) {
Greg Rose7f12ad72013-12-21 06:12:51 +0000909 skb->transport_header = skb->mac_header +
910 sizeof(struct ethhdr) +
911 (ip_hdr(skb)->ihl * 4);
912
913 /* Add 4 bytes for VLAN tagged packets */
914 skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
915 skb->protocol == htons(ETH_P_8021AD))
916 ? VLAN_HLEN : 0;
917
Anjali Singhai Jain818f2e72015-03-31 00:44:59 -0700918 if ((ip_hdr(skb)->protocol == IPPROTO_UDP) &&
919 (udp_hdr(skb)->check != 0)) {
920 rx_udp_csum = udp_csum(skb);
921 iph = ip_hdr(skb);
922 csum = csum_tcpudp_magic(iph->saddr, iph->daddr,
923 (skb->len -
924 skb_transport_offset(skb)),
925 IPPROTO_UDP, rx_udp_csum);
Greg Rose7f12ad72013-12-21 06:12:51 +0000926
Anjali Singhai Jain818f2e72015-03-31 00:44:59 -0700927 if (udp_hdr(skb)->check != csum)
928 goto checksum_fail;
929
930 } /* else its GRE and so no outer UDP header */
Greg Rose7f12ad72013-12-21 06:12:51 +0000931 }
932
933 skb->ip_summed = CHECKSUM_UNNECESSARY;
Tom Herbert407fa082014-08-27 21:27:43 -0700934 skb->csum_level = ipv4_tunnel || ipv6_tunnel;
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000935
936 return;
937
938checksum_fail:
939 vsi->back->hw_csum_rx_error++;
Greg Rose7f12ad72013-12-21 06:12:51 +0000940}
941
942/**
Anjali Singhai Jain857942f2015-12-09 15:50:21 -0800943 * i40e_ptype_to_htype - get a hash type
Jesse Brandeburg206812b2014-02-12 01:45:33 +0000944 * @ptype: the ptype value from the descriptor
945 *
946 * Returns a hash type to be used by skb_set_hash
947 **/
Anjali Singhai Jain857942f2015-12-09 15:50:21 -0800948static inline enum pkt_hash_types i40e_ptype_to_htype(u8 ptype)
Jesse Brandeburg206812b2014-02-12 01:45:33 +0000949{
950 struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
951
952 if (!decoded.known)
953 return PKT_HASH_TYPE_NONE;
954
955 if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
956 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
957 return PKT_HASH_TYPE_L4;
958 else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
959 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
960 return PKT_HASH_TYPE_L3;
961 else
962 return PKT_HASH_TYPE_L2;
963}
964
965/**
Anjali Singhai Jain857942f2015-12-09 15:50:21 -0800966 * i40e_rx_hash - set the hash value in the skb
967 * @ring: descriptor ring
968 * @rx_desc: specific descriptor
969 **/
970static inline void i40e_rx_hash(struct i40e_ring *ring,
971 union i40e_rx_desc *rx_desc,
972 struct sk_buff *skb,
973 u8 rx_ptype)
974{
975 u32 hash;
976 const __le64 rss_mask =
977 cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
978 I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
979
980 if (ring->netdev->features & NETIF_F_RXHASH)
981 return;
982
983 if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
984 hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
985 skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
986 }
987}
988
989/**
Mitch Williamsa132af22015-01-24 09:58:35 +0000990 * i40e_clean_rx_irq_ps - Reclaim resources after receive; packet split
Greg Rose7f12ad72013-12-21 06:12:51 +0000991 * @rx_ring: rx ring to clean
992 * @budget: how many cleans we're allowed
993 *
994 * Returns true if there's any budget left (e.g. the clean is finished)
995 **/
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800996static int i40e_clean_rx_irq_ps(struct i40e_ring *rx_ring, const int budget)
Greg Rose7f12ad72013-12-21 06:12:51 +0000997{
998 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
999 u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
1000 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
Jiang Liu27ca2752015-08-17 11:19:03 +08001001 const int current_node = numa_mem_id();
Greg Rose7f12ad72013-12-21 06:12:51 +00001002 struct i40e_vsi *vsi = rx_ring->vsi;
1003 u16 i = rx_ring->next_to_clean;
1004 union i40e_rx_desc *rx_desc;
1005 u32 rx_error, rx_status;
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001006 bool failure = false;
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001007 u8 rx_ptype;
Greg Rose7f12ad72013-12-21 06:12:51 +00001008 u64 qword;
Greg Rose7f12ad72013-12-21 06:12:51 +00001009
Mitch Williamsa132af22015-01-24 09:58:35 +00001010 do {
Greg Rose7f12ad72013-12-21 06:12:51 +00001011 struct i40e_rx_buffer *rx_bi;
1012 struct sk_buff *skb;
1013 u16 vlan_tag;
Mitch Williamsa132af22015-01-24 09:58:35 +00001014 /* return some buffers to hardware, one at a time is too slow */
1015 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001016 failure = failure ||
1017 i40evf_alloc_rx_buffers_ps(rx_ring,
1018 cleaned_count);
Mitch Williamsa132af22015-01-24 09:58:35 +00001019 cleaned_count = 0;
1020 }
1021
1022 i = rx_ring->next_to_clean;
1023 rx_desc = I40E_RX_DESC(rx_ring, i);
1024 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1025 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1026 I40E_RXD_QW1_STATUS_SHIFT;
1027
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001028 if (!(rx_status & BIT(I40E_RX_DESC_STATUS_DD_SHIFT)))
Mitch Williamsa132af22015-01-24 09:58:35 +00001029 break;
1030
1031 /* This memory barrier is needed to keep us from reading
1032 * any other fields out of the rx_desc until we know the
1033 * DD bit is set.
1034 */
Alexander Duyck67317162015-04-08 18:49:43 -07001035 dma_rmb();
Greg Rose7f12ad72013-12-21 06:12:51 +00001036 rx_bi = &rx_ring->rx_bi[i];
1037 skb = rx_bi->skb;
Mitch Williamsa132af22015-01-24 09:58:35 +00001038 if (likely(!skb)) {
Jesse Brandeburgdd1a5df2016-01-13 16:51:48 -08001039 skb = __netdev_alloc_skb_ip_align(rx_ring->netdev,
1040 rx_ring->rx_hdr_len,
1041 GFP_ATOMIC |
1042 __GFP_NOWARN);
Jesse Brandeburg8b6ed9c2015-03-31 00:45:01 -07001043 if (!skb) {
Mitch Williamsa132af22015-01-24 09:58:35 +00001044 rx_ring->rx_stats.alloc_buff_failed++;
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001045 failure = true;
Jesse Brandeburg8b6ed9c2015-03-31 00:45:01 -07001046 break;
1047 }
1048
Mitch Williamsa132af22015-01-24 09:58:35 +00001049 /* initialize queue mapping */
1050 skb_record_rx_queue(skb, rx_ring->queue_index);
1051 /* we are reusing so sync this buffer for CPU use */
1052 dma_sync_single_range_for_cpu(rx_ring->dev,
Jesse Brandeburg3578fa02016-01-04 10:33:03 -08001053 rx_ring->rx_bi[0].dma,
1054 i * rx_ring->rx_hdr_len,
Mitch Williamsa132af22015-01-24 09:58:35 +00001055 rx_ring->rx_hdr_len,
1056 DMA_FROM_DEVICE);
1057 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001058 rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
1059 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
1060 rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
1061 I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
1062 rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
1063 I40E_RXD_QW1_LENGTH_SPH_SHIFT;
1064
1065 rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
1066 I40E_RXD_QW1_ERROR_SHIFT;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001067 rx_hbo = rx_error & BIT(I40E_RX_DESC_ERROR_HBO_SHIFT);
1068 rx_error &= ~BIT(I40E_RX_DESC_ERROR_HBO_SHIFT);
Greg Rose7f12ad72013-12-21 06:12:51 +00001069
1070 rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
1071 I40E_RXD_QW1_PTYPE_SHIFT;
Mitch Williamsa132af22015-01-24 09:58:35 +00001072 prefetch(rx_bi->page);
Greg Rose7f12ad72013-12-21 06:12:51 +00001073 rx_bi->skb = NULL;
Mitch Williamsa132af22015-01-24 09:58:35 +00001074 cleaned_count++;
1075 if (rx_hbo || rx_sph) {
1076 int len;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001077
Greg Rose7f12ad72013-12-21 06:12:51 +00001078 if (rx_hbo)
1079 len = I40E_RX_HDR_SIZE;
Greg Rose7f12ad72013-12-21 06:12:51 +00001080 else
Mitch Williamsa132af22015-01-24 09:58:35 +00001081 len = rx_header_len;
1082 memcpy(__skb_put(skb, len), rx_bi->hdr_buf, len);
1083 } else if (skb->len == 0) {
1084 int len;
Greg Rose7f12ad72013-12-21 06:12:51 +00001085
Mitch Williamsa132af22015-01-24 09:58:35 +00001086 len = (rx_packet_len > skb_headlen(skb) ?
1087 skb_headlen(skb) : rx_packet_len);
1088 memcpy(__skb_put(skb, len),
1089 rx_bi->page + rx_bi->page_offset,
1090 len);
1091 rx_bi->page_offset += len;
1092 rx_packet_len -= len;
Greg Rose7f12ad72013-12-21 06:12:51 +00001093 }
1094
1095 /* Get the rest of the data if this was a header split */
Mitch Williamsa132af22015-01-24 09:58:35 +00001096 if (rx_packet_len) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001097 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
1098 rx_bi->page,
1099 rx_bi->page_offset,
1100 rx_packet_len);
1101
1102 skb->len += rx_packet_len;
1103 skb->data_len += rx_packet_len;
1104 skb->truesize += rx_packet_len;
1105
1106 if ((page_count(rx_bi->page) == 1) &&
1107 (page_to_nid(rx_bi->page) == current_node))
1108 get_page(rx_bi->page);
1109 else
1110 rx_bi->page = NULL;
1111
1112 dma_unmap_page(rx_ring->dev,
1113 rx_bi->page_dma,
1114 PAGE_SIZE / 2,
1115 DMA_FROM_DEVICE);
1116 rx_bi->page_dma = 0;
1117 }
Mitch Williamsa132af22015-01-24 09:58:35 +00001118 I40E_RX_INCREMENT(rx_ring, i);
Greg Rose7f12ad72013-12-21 06:12:51 +00001119
1120 if (unlikely(
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001121 !(rx_status & BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001122 struct i40e_rx_buffer *next_buffer;
1123
1124 next_buffer = &rx_ring->rx_bi[i];
Mitch Williamsa132af22015-01-24 09:58:35 +00001125 next_buffer->skb = skb;
Greg Rose7f12ad72013-12-21 06:12:51 +00001126 rx_ring->rx_stats.non_eop_descs++;
Mitch Williamsa132af22015-01-24 09:58:35 +00001127 continue;
Greg Rose7f12ad72013-12-21 06:12:51 +00001128 }
1129
1130 /* ERR_MASK will only have valid bits if EOP set */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001131 if (unlikely(rx_error & BIT(I40E_RX_DESC_ERROR_RXE_SHIFT))) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001132 dev_kfree_skb_any(skb);
Mitch Williamsa132af22015-01-24 09:58:35 +00001133 continue;
1134 }
1135
Anjali Singhai Jain857942f2015-12-09 15:50:21 -08001136 i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
1137
Mitch Williamsa132af22015-01-24 09:58:35 +00001138 /* probably a little skewed due to removing CRC */
1139 total_rx_bytes += skb->len;
1140 total_rx_packets++;
1141
1142 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1143
1144 i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
1145
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001146 vlan_tag = rx_status & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
Mitch Williamsa132af22015-01-24 09:58:35 +00001147 ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
1148 : 0;
1149#ifdef I40E_FCOE
1150 if (!i40e_fcoe_handle_offload(rx_ring, rx_desc, skb)) {
1151 dev_kfree_skb_any(skb);
1152 continue;
1153 }
1154#endif
Mitch Williamsa132af22015-01-24 09:58:35 +00001155 i40e_receive_skb(rx_ring, skb, vlan_tag);
1156
Mitch Williamsa132af22015-01-24 09:58:35 +00001157 rx_desc->wb.qword1.status_error_len = 0;
1158
1159 } while (likely(total_rx_packets < budget));
1160
1161 u64_stats_update_begin(&rx_ring->syncp);
1162 rx_ring->stats.packets += total_rx_packets;
1163 rx_ring->stats.bytes += total_rx_bytes;
1164 u64_stats_update_end(&rx_ring->syncp);
1165 rx_ring->q_vector->rx.total_packets += total_rx_packets;
1166 rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
1167
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001168 return failure ? budget : total_rx_packets;
Mitch Williamsa132af22015-01-24 09:58:35 +00001169}
1170
1171/**
1172 * i40e_clean_rx_irq_1buf - Reclaim resources after receive; single buffer
1173 * @rx_ring: rx ring to clean
1174 * @budget: how many cleans we're allowed
1175 *
1176 * Returns number of packets cleaned
1177 **/
1178static int i40e_clean_rx_irq_1buf(struct i40e_ring *rx_ring, int budget)
1179{
1180 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1181 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
1182 struct i40e_vsi *vsi = rx_ring->vsi;
1183 union i40e_rx_desc *rx_desc;
1184 u32 rx_error, rx_status;
1185 u16 rx_packet_len;
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001186 bool failure = false;
Mitch Williamsa132af22015-01-24 09:58:35 +00001187 u8 rx_ptype;
1188 u64 qword;
1189 u16 i;
1190
1191 do {
1192 struct i40e_rx_buffer *rx_bi;
1193 struct sk_buff *skb;
1194 u16 vlan_tag;
1195 /* return some buffers to hardware, one at a time is too slow */
1196 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001197 failure = failure ||
1198 i40evf_alloc_rx_buffers_1buf(rx_ring,
1199 cleaned_count);
Mitch Williamsa132af22015-01-24 09:58:35 +00001200 cleaned_count = 0;
1201 }
1202
1203 i = rx_ring->next_to_clean;
1204 rx_desc = I40E_RX_DESC(rx_ring, i);
1205 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1206 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1207 I40E_RXD_QW1_STATUS_SHIFT;
1208
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001209 if (!(rx_status & BIT(I40E_RX_DESC_STATUS_DD_SHIFT)))
Mitch Williamsa132af22015-01-24 09:58:35 +00001210 break;
1211
1212 /* This memory barrier is needed to keep us from reading
1213 * any other fields out of the rx_desc until we know the
1214 * DD bit is set.
1215 */
Alexander Duyck67317162015-04-08 18:49:43 -07001216 dma_rmb();
Mitch Williamsa132af22015-01-24 09:58:35 +00001217
1218 rx_bi = &rx_ring->rx_bi[i];
1219 skb = rx_bi->skb;
1220 prefetch(skb->data);
1221
1222 rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
1223 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
1224
1225 rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
1226 I40E_RXD_QW1_ERROR_SHIFT;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001227 rx_error &= ~BIT(I40E_RX_DESC_ERROR_HBO_SHIFT);
Mitch Williamsa132af22015-01-24 09:58:35 +00001228
1229 rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
1230 I40E_RXD_QW1_PTYPE_SHIFT;
1231 rx_bi->skb = NULL;
1232 cleaned_count++;
1233
1234 /* Get the header and possibly the whole packet
1235 * If this is an skb from previous receive dma will be 0
1236 */
1237 skb_put(skb, rx_packet_len);
1238 dma_unmap_single(rx_ring->dev, rx_bi->dma, rx_ring->rx_buf_len,
1239 DMA_FROM_DEVICE);
1240 rx_bi->dma = 0;
1241
1242 I40E_RX_INCREMENT(rx_ring, i);
1243
1244 if (unlikely(
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001245 !(rx_status & BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
Mitch Williamsa132af22015-01-24 09:58:35 +00001246 rx_ring->rx_stats.non_eop_descs++;
1247 continue;
1248 }
1249
1250 /* ERR_MASK will only have valid bits if EOP set */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001251 if (unlikely(rx_error & BIT(I40E_RX_DESC_ERROR_RXE_SHIFT))) {
Mitch Williamsa132af22015-01-24 09:58:35 +00001252 dev_kfree_skb_any(skb);
Mitch Williamsa132af22015-01-24 09:58:35 +00001253 continue;
Greg Rose7f12ad72013-12-21 06:12:51 +00001254 }
1255
Anjali Singhai Jain857942f2015-12-09 15:50:21 -08001256 i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
Greg Rose7f12ad72013-12-21 06:12:51 +00001257 /* probably a little skewed due to removing CRC */
1258 total_rx_bytes += skb->len;
1259 total_rx_packets++;
1260
1261 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1262
1263 i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
1264
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001265 vlan_tag = rx_status & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
Greg Rose7f12ad72013-12-21 06:12:51 +00001266 ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
1267 : 0;
1268 i40e_receive_skb(rx_ring, skb, vlan_tag);
1269
Greg Rose7f12ad72013-12-21 06:12:51 +00001270 rx_desc->wb.qword1.status_error_len = 0;
Mitch Williamsa132af22015-01-24 09:58:35 +00001271 } while (likely(total_rx_packets < budget));
Greg Rose7f12ad72013-12-21 06:12:51 +00001272
Greg Rose7f12ad72013-12-21 06:12:51 +00001273 u64_stats_update_begin(&rx_ring->syncp);
1274 rx_ring->stats.packets += total_rx_packets;
1275 rx_ring->stats.bytes += total_rx_bytes;
1276 u64_stats_update_end(&rx_ring->syncp);
1277 rx_ring->q_vector->rx.total_packets += total_rx_packets;
1278 rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
1279
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001280 return failure ? budget : total_rx_packets;
Greg Rose7f12ad72013-12-21 06:12:51 +00001281}
1282
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001283static u32 i40e_buildreg_itr(const int type, const u16 itr)
1284{
1285 u32 val;
1286
1287 val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08001288 /* Don't clear PBA because that can cause lost interrupts that
1289 * came in while we were cleaning/polling
1290 */
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001291 (type << I40E_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
1292 (itr << I40E_VFINT_DYN_CTLN1_INTERVAL_SHIFT);
1293
1294 return val;
1295}
1296
1297/* a small macro to shorten up some long lines */
1298#define INTREG I40E_VFINT_DYN_CTLN1
1299
Greg Rose7f12ad72013-12-21 06:12:51 +00001300/**
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001301 * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
1302 * @vsi: the VSI we care about
1303 * @q_vector: q_vector for which itr is being updated and interrupt enabled
1304 *
1305 **/
1306static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
1307 struct i40e_q_vector *q_vector)
1308{
1309 struct i40e_hw *hw = &vsi->back->hw;
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001310 bool rx = false, tx = false;
1311 u32 rxval, txval;
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001312 int vector;
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001313
1314 vector = (q_vector->v_idx + vsi->base_vector);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04001315
1316 /* avoid dynamic calculation if in countdown mode OR if
1317 * all dynamic is disabled
1318 */
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001319 rxval = txval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
1320
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04001321 if (q_vector->itr_countdown > 0 ||
1322 (!ITR_IS_DYNAMIC(vsi->rx_itr_setting) &&
1323 !ITR_IS_DYNAMIC(vsi->tx_itr_setting))) {
1324 goto enable_int;
1325 }
1326
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001327 if (ITR_IS_DYNAMIC(vsi->rx_itr_setting)) {
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001328 rx = i40e_set_new_dynamic_itr(&q_vector->rx);
1329 rxval = i40e_buildreg_itr(I40E_RX_ITR, q_vector->rx.itr);
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001330 }
Jesse Brandeburg4eeb1ff2015-11-18 17:35:42 -08001331
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001332 if (ITR_IS_DYNAMIC(vsi->tx_itr_setting)) {
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001333 tx = i40e_set_new_dynamic_itr(&q_vector->tx);
1334 txval = i40e_buildreg_itr(I40E_TX_ITR, q_vector->tx.itr);
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001335 }
Jesse Brandeburg4eeb1ff2015-11-18 17:35:42 -08001336
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001337 if (rx || tx) {
1338 /* get the higher of the two ITR adjustments and
1339 * use the same value for both ITR registers
1340 * when in adaptive mode (Rx and/or Tx)
1341 */
1342 u16 itr = max(q_vector->tx.itr, q_vector->rx.itr);
1343
1344 q_vector->tx.itr = q_vector->rx.itr = itr;
1345 txval = i40e_buildreg_itr(I40E_TX_ITR, itr);
1346 tx = true;
1347 rxval = i40e_buildreg_itr(I40E_RX_ITR, itr);
1348 rx = true;
1349 }
1350
1351 /* only need to enable the interrupt once, but need
1352 * to possibly update both ITR values
1353 */
1354 if (rx) {
1355 /* set the INTENA_MSK_MASK so that this first write
1356 * won't actually enable the interrupt, instead just
1357 * updating the ITR (it's bit 31 PF and VF)
1358 */
1359 rxval |= BIT(31);
1360 /* don't check _DOWN because interrupt isn't being enabled */
1361 wr32(hw, INTREG(vector - 1), rxval);
1362 }
1363
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04001364enable_int:
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001365 if (!test_bit(__I40E_DOWN, &vsi->state))
1366 wr32(hw, INTREG(vector - 1), txval);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04001367
1368 if (q_vector->itr_countdown)
1369 q_vector->itr_countdown--;
1370 else
1371 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001372}
1373
1374/**
Greg Rose7f12ad72013-12-21 06:12:51 +00001375 * i40evf_napi_poll - NAPI polling Rx/Tx cleanup routine
1376 * @napi: napi struct with our devices info in it
1377 * @budget: amount of work driver is allowed to do this pass, in packets
1378 *
1379 * This function will clean all queues associated with a q_vector.
1380 *
1381 * Returns the amount of work done
1382 **/
1383int i40evf_napi_poll(struct napi_struct *napi, int budget)
1384{
1385 struct i40e_q_vector *q_vector =
1386 container_of(napi, struct i40e_q_vector, napi);
1387 struct i40e_vsi *vsi = q_vector->vsi;
1388 struct i40e_ring *ring;
1389 bool clean_complete = true;
Anjali Singhai Jainc29af372015-01-10 01:07:19 +00001390 bool arm_wb = false;
Greg Rose7f12ad72013-12-21 06:12:51 +00001391 int budget_per_ring;
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07001392 int work_done = 0;
Greg Rose7f12ad72013-12-21 06:12:51 +00001393
1394 if (test_bit(__I40E_DOWN, &vsi->state)) {
1395 napi_complete(napi);
1396 return 0;
1397 }
1398
1399 /* Since the actual Tx work is minimal, we can give the Tx a larger
1400 * budget and be more aggressive about cleaning up the Tx descriptors.
1401 */
Anjali Singhai Jainc29af372015-01-10 01:07:19 +00001402 i40e_for_each_ring(ring, q_vector->tx) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001403 clean_complete &= i40e_clean_tx_irq(ring, vsi->work_limit);
Mitch Williams44cdb792015-11-06 15:26:11 -08001404 arm_wb = arm_wb || ring->arm_wb;
Jesse Brandeburg0deda862015-07-23 16:54:34 -04001405 ring->arm_wb = false;
Anjali Singhai Jainc29af372015-01-10 01:07:19 +00001406 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001407
Alexander Duyckc67cace2015-09-24 09:04:26 -07001408 /* Handle case where we are called by netpoll with a budget of 0 */
1409 if (budget <= 0)
1410 goto tx_only;
1411
Greg Rose7f12ad72013-12-21 06:12:51 +00001412 /* We attempt to distribute budget to each Rx queue fairly, but don't
1413 * allow the budget to go below 1 because that would exit polling early.
1414 */
1415 budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
1416
Mitch Williamsa132af22015-01-24 09:58:35 +00001417 i40e_for_each_ring(ring, q_vector->rx) {
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07001418 int cleaned;
1419
Mitch Williamsa132af22015-01-24 09:58:35 +00001420 if (ring_is_ps_enabled(ring))
1421 cleaned = i40e_clean_rx_irq_ps(ring, budget_per_ring);
1422 else
1423 cleaned = i40e_clean_rx_irq_1buf(ring, budget_per_ring);
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07001424
1425 work_done += cleaned;
Mitch Williamsa132af22015-01-24 09:58:35 +00001426 /* if we didn't clean as many as budgeted, we must be done */
1427 clean_complete &= (budget_per_ring != cleaned);
1428 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001429
1430 /* If work not completed, return budget and polling will return */
Anjali Singhai Jainc29af372015-01-10 01:07:19 +00001431 if (!clean_complete) {
Alexander Duyckc67cace2015-09-24 09:04:26 -07001432tx_only:
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -04001433 if (arm_wb) {
1434 q_vector->tx.ring[0].tx_stats.tx_force_wb++;
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -08001435 i40e_enable_wb_on_itr(vsi, q_vector);
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -04001436 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001437 return budget;
Anjali Singhai Jainc29af372015-01-10 01:07:19 +00001438 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001439
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04001440 if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
1441 q_vector->arm_wb_state = false;
1442
Greg Rose7f12ad72013-12-21 06:12:51 +00001443 /* Work is done so exit the polling mode and re-enable the interrupt */
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07001444 napi_complete_done(napi, work_done);
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001445 i40e_update_enable_itr(vsi, q_vector);
Greg Rose7f12ad72013-12-21 06:12:51 +00001446 return 0;
1447}
1448
1449/**
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04001450 * i40evf_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
Greg Rose7f12ad72013-12-21 06:12:51 +00001451 * @skb: send buffer
1452 * @tx_ring: ring to send buffer on
1453 * @flags: the tx flags to be set
1454 *
1455 * Checks the skb and set up correspondingly several generic transmit flags
1456 * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
1457 *
1458 * Returns error code indicate the frame should be dropped upon error and the
1459 * otherwise returns 0 to indicate the flags has been set properly.
1460 **/
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04001461static inline int i40evf_tx_prepare_vlan_flags(struct sk_buff *skb,
1462 struct i40e_ring *tx_ring,
1463 u32 *flags)
Greg Rose7f12ad72013-12-21 06:12:51 +00001464{
1465 __be16 protocol = skb->protocol;
1466 u32 tx_flags = 0;
1467
Greg Rose31eaacc2015-03-31 00:45:03 -07001468 if (protocol == htons(ETH_P_8021Q) &&
1469 !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
1470 /* When HW VLAN acceleration is turned off by the user the
1471 * stack sets the protocol to 8021q so that the driver
1472 * can take any steps required to support the SW only
1473 * VLAN handling. In our case the driver doesn't need
1474 * to take any further steps so just set the protocol
1475 * to the encapsulated ethertype.
1476 */
1477 skb->protocol = vlan_get_protocol(skb);
1478 goto out;
1479 }
1480
Greg Rose7f12ad72013-12-21 06:12:51 +00001481 /* if we have a HW VLAN tag being added, default to the HW one */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001482 if (skb_vlan_tag_present(skb)) {
1483 tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
Greg Rose7f12ad72013-12-21 06:12:51 +00001484 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1485 /* else if it is a SW VLAN, check the next protocol and store the tag */
1486 } else if (protocol == htons(ETH_P_8021Q)) {
1487 struct vlan_hdr *vhdr, _vhdr;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001488
Greg Rose7f12ad72013-12-21 06:12:51 +00001489 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
1490 if (!vhdr)
1491 return -EINVAL;
1492
1493 protocol = vhdr->h_vlan_encapsulated_proto;
1494 tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
1495 tx_flags |= I40E_TX_FLAGS_SW_VLAN;
1496 }
1497
Greg Rose31eaacc2015-03-31 00:45:03 -07001498out:
Greg Rose7f12ad72013-12-21 06:12:51 +00001499 *flags = tx_flags;
1500 return 0;
1501}
1502
1503/**
1504 * i40e_tso - set up the tso context descriptor
1505 * @tx_ring: ptr to the ring to send
1506 * @skb: ptr to the skb we're sending
Greg Rose7f12ad72013-12-21 06:12:51 +00001507 * @hdr_len: ptr to the size of the packet header
Shannon Nelson9c883bd2015-10-21 19:47:02 -04001508 * @cd_type_cmd_tso_mss: Quad Word 1
Greg Rose7f12ad72013-12-21 06:12:51 +00001509 *
1510 * Returns 0 if no TSO can happen, 1 if tso is going, or error
1511 **/
1512static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
Shannon Nelson9c883bd2015-10-21 19:47:02 -04001513 u8 *hdr_len, u64 *cd_type_cmd_tso_mss)
Greg Rose7f12ad72013-12-21 06:12:51 +00001514{
1515 u32 cd_cmd, cd_tso_len, cd_mss;
Francois Romieufe6d4aa2014-03-30 03:14:53 +00001516 struct ipv6hdr *ipv6h;
Greg Rose7f12ad72013-12-21 06:12:51 +00001517 struct tcphdr *tcph;
1518 struct iphdr *iph;
1519 u32 l4len;
1520 int err;
Greg Rose7f12ad72013-12-21 06:12:51 +00001521
Shannon Nelsone9f65632016-01-04 10:33:04 -08001522 if (skb->ip_summed != CHECKSUM_PARTIAL)
1523 return 0;
1524
Greg Rose7f12ad72013-12-21 06:12:51 +00001525 if (!skb_is_gso(skb))
1526 return 0;
1527
Francois Romieufe6d4aa2014-03-30 03:14:53 +00001528 err = skb_cow_head(skb, 0);
1529 if (err < 0)
1530 return err;
Greg Rose7f12ad72013-12-21 06:12:51 +00001531
Anjali Singhai85e76d02015-02-21 06:44:16 +00001532 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
1533 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
1534
1535 if (iph->version == 4) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001536 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1537 iph->tot_len = 0;
1538 iph->check = 0;
1539 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1540 0, IPPROTO_TCP, 0);
Anjali Singhai85e76d02015-02-21 06:44:16 +00001541 } else if (ipv6h->version == 6) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001542 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1543 ipv6h->payload_len = 0;
1544 tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
1545 0, IPPROTO_TCP, 0);
1546 }
1547
1548 l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
1549 *hdr_len = (skb->encapsulation
1550 ? (skb_inner_transport_header(skb) - skb->data)
1551 : skb_transport_offset(skb)) + l4len;
1552
1553 /* find the field values */
1554 cd_cmd = I40E_TX_CTX_DESC_TSO;
1555 cd_tso_len = skb->len - *hdr_len;
1556 cd_mss = skb_shinfo(skb)->gso_size;
1557 *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
1558 ((u64)cd_tso_len <<
1559 I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
1560 ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
1561 return 1;
1562}
1563
1564/**
1565 * i40e_tx_enable_csum - Enable Tx checksum offloads
1566 * @skb: send buffer
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001567 * @tx_flags: pointer to Tx flags currently set
Greg Rose7f12ad72013-12-21 06:12:51 +00001568 * @td_cmd: Tx descriptor command bits to set
1569 * @td_offset: Tx descriptor header offsets to set
1570 * @cd_tunneling: ptr to context desc bits
1571 **/
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001572static void i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
Greg Rose7f12ad72013-12-21 06:12:51 +00001573 u32 *td_cmd, u32 *td_offset,
1574 struct i40e_ring *tx_ring,
1575 u32 *cd_tunneling)
1576{
1577 struct ipv6hdr *this_ipv6_hdr;
1578 unsigned int this_tcp_hdrlen;
1579 struct iphdr *this_ip_hdr;
1580 u32 network_hdr_len;
1581 u8 l4_hdr = 0;
Anjali Singhai Jain527274c2015-06-05 12:20:31 -04001582 struct udphdr *oudph;
1583 struct iphdr *oiph;
Anjali Singhai Jain45991202015-02-27 09:15:29 +00001584 u32 l4_tunnel = 0;
Greg Rose7f12ad72013-12-21 06:12:51 +00001585
1586 if (skb->encapsulation) {
Anjali Singhai Jain45991202015-02-27 09:15:29 +00001587 switch (ip_hdr(skb)->protocol) {
1588 case IPPROTO_UDP:
Anjali Singhai Jain527274c2015-06-05 12:20:31 -04001589 oudph = udp_hdr(skb);
1590 oiph = ip_hdr(skb);
Anjali Singhai Jain45991202015-02-27 09:15:29 +00001591 l4_tunnel = I40E_TXD_CTX_UDP_TUNNELING;
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001592 *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
Anjali Singhai Jain45991202015-02-27 09:15:29 +00001593 break;
1594 default:
1595 return;
1596 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001597 network_hdr_len = skb_inner_network_header_len(skb);
1598 this_ip_hdr = inner_ip_hdr(skb);
1599 this_ipv6_hdr = inner_ipv6_hdr(skb);
1600 this_tcp_hdrlen = inner_tcp_hdrlen(skb);
1601
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001602 if (*tx_flags & I40E_TX_FLAGS_IPV4) {
1603 if (*tx_flags & I40E_TX_FLAGS_TSO) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001604 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
1605 ip_hdr(skb)->check = 0;
1606 } else {
1607 *cd_tunneling |=
1608 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1609 }
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001610 } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
Anjali Singhai85e76d02015-02-21 06:44:16 +00001611 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001612 if (*tx_flags & I40E_TX_FLAGS_TSO)
Greg Rose7f12ad72013-12-21 06:12:51 +00001613 ip_hdr(skb)->check = 0;
Greg Rose7f12ad72013-12-21 06:12:51 +00001614 }
1615
1616 /* Now set the ctx descriptor fields */
1617 *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
Anjali Singhai Jain45991202015-02-27 09:15:29 +00001618 I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
1619 l4_tunnel |
Greg Rose7f12ad72013-12-21 06:12:51 +00001620 ((skb_inner_network_offset(skb) -
1621 skb_transport_offset(skb)) >> 1) <<
1622 I40E_TXD_CTX_QW0_NATLEN_SHIFT;
Anjali Singhai85e76d02015-02-21 06:44:16 +00001623 if (this_ip_hdr->version == 6) {
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001624 *tx_flags &= ~I40E_TX_FLAGS_IPV4;
1625 *tx_flags |= I40E_TX_FLAGS_IPV6;
Anjali Singhai85e76d02015-02-21 06:44:16 +00001626 }
1627
Anjali Singhai Jain527274c2015-06-05 12:20:31 -04001628 if ((tx_ring->flags & I40E_TXR_FLAGS_OUTER_UDP_CSUM) &&
1629 (l4_tunnel == I40E_TXD_CTX_UDP_TUNNELING) &&
1630 (*cd_tunneling & I40E_TXD_CTX_QW0_EXT_IP_MASK)) {
1631 oudph->check = ~csum_tcpudp_magic(oiph->saddr,
1632 oiph->daddr,
1633 (skb->len - skb_transport_offset(skb)),
1634 IPPROTO_UDP, 0);
1635 *cd_tunneling |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
1636 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001637 } else {
1638 network_hdr_len = skb_network_header_len(skb);
1639 this_ip_hdr = ip_hdr(skb);
1640 this_ipv6_hdr = ipv6_hdr(skb);
1641 this_tcp_hdrlen = tcp_hdrlen(skb);
1642 }
1643
1644 /* Enable IP checksum offloads */
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001645 if (*tx_flags & I40E_TX_FLAGS_IPV4) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001646 l4_hdr = this_ip_hdr->protocol;
1647 /* the stack computes the IP header already, the only time we
1648 * need the hardware to recompute it is in the case of TSO.
1649 */
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001650 if (*tx_flags & I40E_TX_FLAGS_TSO) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001651 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
1652 this_ip_hdr->check = 0;
1653 } else {
1654 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
1655 }
1656 /* Now set the td_offset for IP header length */
1657 *td_offset = (network_hdr_len >> 2) <<
1658 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001659 } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001660 l4_hdr = this_ipv6_hdr->nexthdr;
1661 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
1662 /* Now set the td_offset for IP header length */
1663 *td_offset = (network_hdr_len >> 2) <<
1664 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1665 }
1666 /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
1667 *td_offset |= (skb_network_offset(skb) >> 1) <<
1668 I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
1669
1670 /* Enable L4 checksum offloads */
1671 switch (l4_hdr) {
1672 case IPPROTO_TCP:
1673 /* enable checksum offloads */
1674 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
1675 *td_offset |= (this_tcp_hdrlen >> 2) <<
1676 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1677 break;
1678 case IPPROTO_SCTP:
1679 /* enable SCTP checksum offload */
1680 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
1681 *td_offset |= (sizeof(struct sctphdr) >> 2) <<
1682 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1683 break;
1684 case IPPROTO_UDP:
1685 /* enable UDP checksum offload */
1686 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
1687 *td_offset |= (sizeof(struct udphdr) >> 2) <<
1688 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1689 break;
1690 default:
1691 break;
1692 }
1693}
1694
1695/**
1696 * i40e_create_tx_ctx Build the Tx context descriptor
1697 * @tx_ring: ring to create the descriptor on
1698 * @cd_type_cmd_tso_mss: Quad Word 1
1699 * @cd_tunneling: Quad Word 0 - bits 0-31
1700 * @cd_l2tag2: Quad Word 0 - bits 32-63
1701 **/
1702static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
1703 const u64 cd_type_cmd_tso_mss,
1704 const u32 cd_tunneling, const u32 cd_l2tag2)
1705{
1706 struct i40e_tx_context_desc *context_desc;
1707 int i = tx_ring->next_to_use;
1708
Jesse Brandeburgff40dd52014-02-14 02:14:41 +00001709 if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
1710 !cd_tunneling && !cd_l2tag2)
Greg Rose7f12ad72013-12-21 06:12:51 +00001711 return;
1712
1713 /* grab the next descriptor */
1714 context_desc = I40E_TX_CTXTDESC(tx_ring, i);
1715
1716 i++;
1717 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
1718
1719 /* cpu_to_le32 and assign to struct fields */
1720 context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
1721 context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
Jesse Brandeburg3efbbb22014-06-04 20:41:54 +00001722 context_desc->rsvd = cpu_to_le16(0);
Greg Rose7f12ad72013-12-21 06:12:51 +00001723 context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
1724}
1725
Jesse Brandeburg4eeb1ff2015-11-18 17:35:42 -08001726/**
Anjali Singhai71da6192015-02-21 06:42:35 +00001727 * i40e_chk_linearize - Check if there are more than 8 fragments per packet
1728 * @skb: send buffer
1729 * @tx_flags: collected send information
Anjali Singhai71da6192015-02-21 06:42:35 +00001730 *
1731 * Note: Our HW can't scatter-gather more than 8 fragments to build
1732 * a packet on the wire and so we need to figure out the cases where we
1733 * need to linearize the skb.
1734 **/
Anjali Singhai Jain30520832015-05-08 15:35:52 -07001735static bool i40e_chk_linearize(struct sk_buff *skb, u32 tx_flags)
Anjali Singhai71da6192015-02-21 06:42:35 +00001736{
1737 struct skb_frag_struct *frag;
1738 bool linearize = false;
1739 unsigned int size = 0;
1740 u16 num_frags;
1741 u16 gso_segs;
1742
1743 num_frags = skb_shinfo(skb)->nr_frags;
1744 gso_segs = skb_shinfo(skb)->gso_segs;
1745
1746 if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO)) {
Anjali Singhai Jain30520832015-05-08 15:35:52 -07001747 u16 j = 0;
Anjali Singhai71da6192015-02-21 06:42:35 +00001748
1749 if (num_frags < (I40E_MAX_BUFFER_TXD))
1750 goto linearize_chk_done;
1751 /* try the simple math, if we have too many frags per segment */
1752 if (DIV_ROUND_UP((num_frags + gso_segs), gso_segs) >
1753 I40E_MAX_BUFFER_TXD) {
1754 linearize = true;
1755 goto linearize_chk_done;
1756 }
1757 frag = &skb_shinfo(skb)->frags[0];
Anjali Singhai71da6192015-02-21 06:42:35 +00001758 /* we might still have more fragments per segment */
1759 do {
1760 size += skb_frag_size(frag);
1761 frag++; j++;
Anjali Singhai Jain30520832015-05-08 15:35:52 -07001762 if ((size >= skb_shinfo(skb)->gso_size) &&
1763 (j < I40E_MAX_BUFFER_TXD)) {
1764 size = (size % skb_shinfo(skb)->gso_size);
1765 j = (size) ? 1 : 0;
1766 }
Anjali Singhai71da6192015-02-21 06:42:35 +00001767 if (j == I40E_MAX_BUFFER_TXD) {
Anjali Singhai Jain30520832015-05-08 15:35:52 -07001768 linearize = true;
1769 break;
Anjali Singhai71da6192015-02-21 06:42:35 +00001770 }
1771 num_frags--;
1772 } while (num_frags);
1773 } else {
1774 if (num_frags >= I40E_MAX_BUFFER_TXD)
1775 linearize = true;
1776 }
1777
1778linearize_chk_done:
1779 return linearize;
1780}
1781
Greg Rose7f12ad72013-12-21 06:12:51 +00001782/**
Jesse Brandeburg8f6a2b02015-04-16 20:06:09 -04001783 * __i40evf_maybe_stop_tx - 2nd level check for tx stop conditions
1784 * @tx_ring: the ring to be checked
1785 * @size: the size buffer we want to assure is available
1786 *
1787 * Returns -EBUSY if a stop is needed, else 0
1788 **/
1789static inline int __i40evf_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
1790{
1791 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
1792 /* Memory barrier before checking head and tail */
1793 smp_mb();
1794
1795 /* Check again in a case another CPU has just made room available. */
1796 if (likely(I40E_DESC_UNUSED(tx_ring) < size))
1797 return -EBUSY;
1798
1799 /* A reprieve! - use start_queue because it doesn't call schedule */
1800 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
1801 ++tx_ring->tx_stats.restart_queue;
1802 return 0;
1803}
1804
1805/**
1806 * i40evf_maybe_stop_tx - 1st level check for tx stop conditions
1807 * @tx_ring: the ring to be checked
1808 * @size: the size buffer we want to assure is available
1809 *
1810 * Returns 0 if stop is not needed
1811 **/
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04001812static inline int i40evf_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
Jesse Brandeburg8f6a2b02015-04-16 20:06:09 -04001813{
1814 if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
1815 return 0;
1816 return __i40evf_maybe_stop_tx(tx_ring, size);
1817}
1818
1819/**
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04001820 * i40evf_tx_map - Build the Tx descriptor
Greg Rose7f12ad72013-12-21 06:12:51 +00001821 * @tx_ring: ring to send buffer on
1822 * @skb: send buffer
1823 * @first: first buffer info buffer to use
1824 * @tx_flags: collected send information
1825 * @hdr_len: size of the packet header
1826 * @td_cmd: the command field in the descriptor
1827 * @td_offset: offset for checksum or crc
1828 **/
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04001829static inline void i40evf_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
1830 struct i40e_tx_buffer *first, u32 tx_flags,
1831 const u8 hdr_len, u32 td_cmd, u32 td_offset)
Greg Rose7f12ad72013-12-21 06:12:51 +00001832{
1833 unsigned int data_len = skb->data_len;
1834 unsigned int size = skb_headlen(skb);
1835 struct skb_frag_struct *frag;
1836 struct i40e_tx_buffer *tx_bi;
1837 struct i40e_tx_desc *tx_desc;
1838 u16 i = tx_ring->next_to_use;
1839 u32 td_tag = 0;
1840 dma_addr_t dma;
1841 u16 gso_segs;
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001842 u16 desc_count = 0;
1843 bool tail_bump = true;
1844 bool do_rs = false;
Greg Rose7f12ad72013-12-21 06:12:51 +00001845
1846 if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
1847 td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
1848 td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
1849 I40E_TX_FLAGS_VLAN_SHIFT;
1850 }
1851
1852 if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
1853 gso_segs = skb_shinfo(skb)->gso_segs;
1854 else
1855 gso_segs = 1;
1856
1857 /* multiply data chunks by size of headers */
1858 first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
1859 first->gso_segs = gso_segs;
1860 first->skb = skb;
1861 first->tx_flags = tx_flags;
1862
1863 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1864
1865 tx_desc = I40E_TX_DESC(tx_ring, i);
1866 tx_bi = first;
1867
1868 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1869 if (dma_mapping_error(tx_ring->dev, dma))
1870 goto dma_error;
1871
1872 /* record length, and DMA address */
1873 dma_unmap_len_set(tx_bi, len, size);
1874 dma_unmap_addr_set(tx_bi, dma, dma);
1875
1876 tx_desc->buffer_addr = cpu_to_le64(dma);
1877
1878 while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
1879 tx_desc->cmd_type_offset_bsz =
1880 build_ctob(td_cmd, td_offset,
1881 I40E_MAX_DATA_PER_TXD, td_tag);
1882
1883 tx_desc++;
1884 i++;
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001885 desc_count++;
1886
Greg Rose7f12ad72013-12-21 06:12:51 +00001887 if (i == tx_ring->count) {
1888 tx_desc = I40E_TX_DESC(tx_ring, 0);
1889 i = 0;
1890 }
1891
1892 dma += I40E_MAX_DATA_PER_TXD;
1893 size -= I40E_MAX_DATA_PER_TXD;
1894
1895 tx_desc->buffer_addr = cpu_to_le64(dma);
1896 }
1897
1898 if (likely(!data_len))
1899 break;
1900
1901 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
1902 size, td_tag);
1903
1904 tx_desc++;
1905 i++;
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001906 desc_count++;
1907
Greg Rose7f12ad72013-12-21 06:12:51 +00001908 if (i == tx_ring->count) {
1909 tx_desc = I40E_TX_DESC(tx_ring, 0);
1910 i = 0;
1911 }
1912
1913 size = skb_frag_size(frag);
1914 data_len -= size;
1915
1916 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
1917 DMA_TO_DEVICE);
1918
1919 tx_bi = &tx_ring->tx_bi[i];
1920 }
1921
Greg Rose7f12ad72013-12-21 06:12:51 +00001922 /* set next_to_watch value indicating a packet is present */
1923 first->next_to_watch = tx_desc;
1924
1925 i++;
1926 if (i == tx_ring->count)
1927 i = 0;
1928
1929 tx_ring->next_to_use = i;
1930
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001931 netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
1932 tx_ring->queue_index),
1933 first->bytecount);
Jesse Brandeburg8f6a2b02015-04-16 20:06:09 -04001934 i40evf_maybe_stop_tx(tx_ring, DESC_NEEDED);
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001935
1936 /* Algorithm to optimize tail and RS bit setting:
1937 * if xmit_more is supported
1938 * if xmit_more is true
1939 * do not update tail and do not mark RS bit.
1940 * if xmit_more is false and last xmit_more was false
1941 * if every packet spanned less than 4 desc
1942 * then set RS bit on 4th packet and update tail
1943 * on every packet
1944 * else
1945 * update tail and set RS bit on every packet.
1946 * if xmit_more is false and last_xmit_more was true
1947 * update tail and set RS bit.
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001948 *
1949 * Optimization: wmb to be issued only in case of tail update.
1950 * Also optimize the Descriptor WB path for RS bit with the same
1951 * algorithm.
1952 *
1953 * Note: If there are less than 4 packets
1954 * pending and interrupts were disabled the service task will
1955 * trigger a force WB.
1956 */
1957 if (skb->xmit_more &&
1958 !netif_xmit_stopped(netdev_get_tx_queue(tx_ring->netdev,
1959 tx_ring->queue_index))) {
1960 tx_ring->flags |= I40E_TXR_FLAGS_LAST_XMIT_MORE_SET;
1961 tail_bump = false;
1962 } else if (!skb->xmit_more &&
1963 !netif_xmit_stopped(netdev_get_tx_queue(tx_ring->netdev,
1964 tx_ring->queue_index)) &&
1965 (!(tx_ring->flags & I40E_TXR_FLAGS_LAST_XMIT_MORE_SET)) &&
1966 (tx_ring->packet_stride < WB_STRIDE) &&
1967 (desc_count < WB_STRIDE)) {
1968 tx_ring->packet_stride++;
1969 } else {
1970 tx_ring->packet_stride = 0;
1971 tx_ring->flags &= ~I40E_TXR_FLAGS_LAST_XMIT_MORE_SET;
1972 do_rs = true;
1973 }
1974 if (do_rs)
1975 tx_ring->packet_stride = 0;
1976
1977 tx_desc->cmd_type_offset_bsz =
1978 build_ctob(td_cmd, td_offset, size, td_tag) |
1979 cpu_to_le64((u64)(do_rs ? I40E_TXD_CMD :
1980 I40E_TX_DESC_CMD_EOP) <<
1981 I40E_TXD_QW1_CMD_SHIFT);
1982
Greg Rose7f12ad72013-12-21 06:12:51 +00001983 /* notify HW of packet */
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001984 if (!tail_bump)
Jesse Brandeburg489ce7a2015-04-27 14:57:08 -04001985 prefetchw(tx_desc + 1);
Greg Rose7f12ad72013-12-21 06:12:51 +00001986
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001987 if (tail_bump) {
1988 /* Force memory writes to complete before letting h/w
1989 * know there are new descriptors to fetch. (Only
1990 * applicable for weak-ordered memory model archs,
1991 * such as IA-64).
1992 */
1993 wmb();
1994 writel(i, tx_ring->tail);
1995 }
1996
Greg Rose7f12ad72013-12-21 06:12:51 +00001997 return;
1998
1999dma_error:
2000 dev_info(tx_ring->dev, "TX DMA map failed\n");
2001
2002 /* clear dma mappings for failed tx_bi map */
2003 for (;;) {
2004 tx_bi = &tx_ring->tx_bi[i];
2005 i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
2006 if (tx_bi == first)
2007 break;
2008 if (i == 0)
2009 i = tx_ring->count;
2010 i--;
2011 }
2012
2013 tx_ring->next_to_use = i;
2014}
2015
2016/**
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002017 * i40evf_xmit_descriptor_count - calculate number of tx descriptors needed
Greg Rose7f12ad72013-12-21 06:12:51 +00002018 * @skb: send buffer
2019 * @tx_ring: ring to send buffer on
2020 *
2021 * Returns number of data descriptors needed for this skb. Returns 0 to indicate
2022 * there is not enough descriptors available in this ring since we need at least
2023 * one descriptor.
2024 **/
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002025static inline int i40evf_xmit_descriptor_count(struct sk_buff *skb,
2026 struct i40e_ring *tx_ring)
Greg Rose7f12ad72013-12-21 06:12:51 +00002027{
Greg Rose7f12ad72013-12-21 06:12:51 +00002028 unsigned int f;
Greg Rose7f12ad72013-12-21 06:12:51 +00002029 int count = 0;
2030
2031 /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
2032 * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
Jesse Brandeburgbe560522014-02-06 05:51:13 +00002033 * + 4 desc gap to avoid the cache line where head is,
Greg Rose7f12ad72013-12-21 06:12:51 +00002034 * + 1 desc for context descriptor,
2035 * otherwise try next time
2036 */
Greg Rose7f12ad72013-12-21 06:12:51 +00002037 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
2038 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
Jesse Brandeburg980093e2014-05-10 04:49:12 +00002039
Greg Rose7f12ad72013-12-21 06:12:51 +00002040 count += TXD_USE_COUNT(skb_headlen(skb));
Jesse Brandeburg8f6a2b02015-04-16 20:06:09 -04002041 if (i40evf_maybe_stop_tx(tx_ring, count + 4 + 1)) {
Greg Rose7f12ad72013-12-21 06:12:51 +00002042 tx_ring->tx_stats.tx_busy++;
2043 return 0;
2044 }
2045 return count;
2046}
2047
2048/**
2049 * i40e_xmit_frame_ring - Sends buffer on Tx ring
2050 * @skb: send buffer
2051 * @tx_ring: ring to send buffer on
2052 *
2053 * Returns NETDEV_TX_OK if sent, else an error code
2054 **/
2055static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
2056 struct i40e_ring *tx_ring)
2057{
2058 u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
2059 u32 cd_tunneling = 0, cd_l2tag2 = 0;
2060 struct i40e_tx_buffer *first;
2061 u32 td_offset = 0;
2062 u32 tx_flags = 0;
2063 __be16 protocol;
2064 u32 td_cmd = 0;
2065 u8 hdr_len = 0;
2066 int tso;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002067
Jesse Brandeburgb74118f2015-10-26 19:44:30 -04002068 /* prefetch the data, we'll need it later */
2069 prefetch(skb->data);
2070
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002071 if (0 == i40evf_xmit_descriptor_count(skb, tx_ring))
Greg Rose7f12ad72013-12-21 06:12:51 +00002072 return NETDEV_TX_BUSY;
2073
2074 /* prepare the xmit flags */
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002075 if (i40evf_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
Greg Rose7f12ad72013-12-21 06:12:51 +00002076 goto out_drop;
2077
2078 /* obtain protocol of skb */
Vlad Yasevicha12c4152014-08-25 10:34:53 -04002079 protocol = vlan_get_protocol(skb);
Greg Rose7f12ad72013-12-21 06:12:51 +00002080
2081 /* record the location of the first descriptor for this packet */
2082 first = &tx_ring->tx_bi[tx_ring->next_to_use];
2083
2084 /* setup IPv4/IPv6 offloads */
2085 if (protocol == htons(ETH_P_IP))
2086 tx_flags |= I40E_TX_FLAGS_IPV4;
2087 else if (protocol == htons(ETH_P_IPV6))
2088 tx_flags |= I40E_TX_FLAGS_IPV6;
2089
Shannon Nelson9c883bd2015-10-21 19:47:02 -04002090 tso = i40e_tso(tx_ring, skb, &hdr_len, &cd_type_cmd_tso_mss);
Greg Rose7f12ad72013-12-21 06:12:51 +00002091
2092 if (tso < 0)
2093 goto out_drop;
2094 else if (tso)
2095 tx_flags |= I40E_TX_FLAGS_TSO;
2096
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -04002097 if (i40e_chk_linearize(skb, tx_flags)) {
Anjali Singhai71da6192015-02-21 06:42:35 +00002098 if (skb_linearize(skb))
2099 goto out_drop;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -04002100 tx_ring->tx_stats.tx_linearize++;
2101 }
Greg Rose7f12ad72013-12-21 06:12:51 +00002102 skb_tx_timestamp(skb);
2103
2104 /* always enable CRC insertion offload */
2105 td_cmd |= I40E_TX_DESC_CMD_ICRC;
2106
2107 /* Always offload the checksum, since it's in the data descriptor */
2108 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2109 tx_flags |= I40E_TX_FLAGS_CSUM;
2110
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002111 i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
Greg Rose7f12ad72013-12-21 06:12:51 +00002112 tx_ring, &cd_tunneling);
2113 }
2114
2115 i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
2116 cd_tunneling, cd_l2tag2);
2117
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002118 i40evf_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
2119 td_cmd, td_offset);
Greg Rose7f12ad72013-12-21 06:12:51 +00002120
Greg Rose7f12ad72013-12-21 06:12:51 +00002121 return NETDEV_TX_OK;
2122
2123out_drop:
2124 dev_kfree_skb_any(skb);
2125 return NETDEV_TX_OK;
2126}
2127
2128/**
2129 * i40evf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
2130 * @skb: send buffer
2131 * @netdev: network interface device structure
2132 *
2133 * Returns NETDEV_TX_OK if sent, else an error code
2134 **/
2135netdev_tx_t i40evf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2136{
2137 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams0dd438d2015-10-26 19:44:40 -04002138 struct i40e_ring *tx_ring = &adapter->tx_rings[skb->queue_mapping];
Greg Rose7f12ad72013-12-21 06:12:51 +00002139
2140 /* hardware can't handle really short frames, hardware padding works
2141 * beyond this point
2142 */
2143 if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
2144 if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
2145 return NETDEV_TX_OK;
2146 skb->len = I40E_MIN_TX_LEN;
2147 skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
2148 }
2149
2150 return i40e_xmit_frame_ring(skb, tx_ring);
2151}