blob: fb6cd7e5d3be2737176b137e79f43f2c58b8586f [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
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800132 * @in_sw: is tx_pending being checked in SW or HW
Jesse Brandeburga68de582015-02-24 05:26:03 +0000133 *
Kiran Patil9c6c1252015-11-06 15:26:02 -0800134 * Since there is no access to the ring head register
135 * in XL710, we need to use our local copies
Jesse Brandeburga68de582015-02-24 05:26:03 +0000136 **/
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800137u32 i40evf_get_tx_pending(struct i40e_ring *ring, bool in_sw)
Jesse Brandeburga68de582015-02-24 05:26:03 +0000138{
Kiran Patil9c6c1252015-11-06 15:26:02 -0800139 u32 head, tail;
Jesse Brandeburga68de582015-02-24 05:26:03 +0000140
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800141 if (!in_sw)
142 head = i40e_get_head(ring);
143 else
144 head = ring->next_to_clean;
Kiran Patil9c6c1252015-11-06 15:26:02 -0800145 tail = readl(ring->tail);
146
147 if (head != tail)
148 return (head < tail) ?
149 tail - head : (tail + ring->count - head);
150
151 return 0;
Jesse Brandeburga68de582015-02-24 05:26:03 +0000152}
153
Anjali Singhai Jainc29af372015-01-10 01:07:19 +0000154#define WB_STRIDE 0x3
155
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000156/**
Greg Rose7f12ad72013-12-21 06:12:51 +0000157 * i40e_clean_tx_irq - Reclaim resources after transmit completes
158 * @tx_ring: tx ring to clean
159 * @budget: how many cleans we're allowed
160 *
161 * Returns true if there's any budget left (e.g. the clean is finished)
162 **/
163static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
164{
165 u16 i = tx_ring->next_to_clean;
166 struct i40e_tx_buffer *tx_buf;
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000167 struct i40e_tx_desc *tx_head;
Greg Rose7f12ad72013-12-21 06:12:51 +0000168 struct i40e_tx_desc *tx_desc;
169 unsigned int total_packets = 0;
170 unsigned int total_bytes = 0;
171
172 tx_buf = &tx_ring->tx_bi[i];
173 tx_desc = I40E_TX_DESC(tx_ring, i);
174 i -= tx_ring->count;
175
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000176 tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
177
Greg Rose7f12ad72013-12-21 06:12:51 +0000178 do {
179 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
180
181 /* if next_to_watch is not set then there is no work pending */
182 if (!eop_desc)
183 break;
184
185 /* prevent any other reads prior to eop_desc */
186 read_barrier_depends();
187
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000188 /* we have caught up to head, no work left to do */
189 if (tx_head == tx_desc)
Greg Rose7f12ad72013-12-21 06:12:51 +0000190 break;
191
192 /* clear next_to_watch to prevent false hangs */
193 tx_buf->next_to_watch = NULL;
194
195 /* update the statistics for this packet */
196 total_bytes += tx_buf->bytecount;
197 total_packets += tx_buf->gso_segs;
198
199 /* free the skb */
200 dev_kfree_skb_any(tx_buf->skb);
201
202 /* unmap skb header data */
203 dma_unmap_single(tx_ring->dev,
204 dma_unmap_addr(tx_buf, dma),
205 dma_unmap_len(tx_buf, len),
206 DMA_TO_DEVICE);
207
208 /* clear tx_buffer data */
209 tx_buf->skb = NULL;
210 dma_unmap_len_set(tx_buf, len, 0);
211
212 /* unmap remaining buffers */
213 while (tx_desc != eop_desc) {
214
215 tx_buf++;
216 tx_desc++;
217 i++;
218 if (unlikely(!i)) {
219 i -= tx_ring->count;
220 tx_buf = tx_ring->tx_bi;
221 tx_desc = I40E_TX_DESC(tx_ring, 0);
222 }
223
224 /* unmap any remaining paged data */
225 if (dma_unmap_len(tx_buf, len)) {
226 dma_unmap_page(tx_ring->dev,
227 dma_unmap_addr(tx_buf, dma),
228 dma_unmap_len(tx_buf, len),
229 DMA_TO_DEVICE);
230 dma_unmap_len_set(tx_buf, len, 0);
231 }
232 }
233
234 /* move us one more past the eop_desc for start of next pkt */
235 tx_buf++;
236 tx_desc++;
237 i++;
238 if (unlikely(!i)) {
239 i -= tx_ring->count;
240 tx_buf = tx_ring->tx_bi;
241 tx_desc = I40E_TX_DESC(tx_ring, 0);
242 }
243
Jesse Brandeburg016890b2015-02-27 09:15:31 +0000244 prefetch(tx_desc);
245
Greg Rose7f12ad72013-12-21 06:12:51 +0000246 /* update budget accounting */
247 budget--;
248 } while (likely(budget));
249
250 i += tx_ring->count;
251 tx_ring->next_to_clean = i;
252 u64_stats_update_begin(&tx_ring->syncp);
253 tx_ring->stats.bytes += total_bytes;
254 tx_ring->stats.packets += total_packets;
255 u64_stats_update_end(&tx_ring->syncp);
256 tx_ring->q_vector->tx.total_bytes += total_bytes;
257 tx_ring->q_vector->tx.total_packets += total_packets;
258
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -0800259 if (tx_ring->flags & I40E_TXR_FLAGS_WB_ON_ITR) {
260 unsigned int j = 0;
261 /* check to see if there are < 4 descriptors
262 * waiting to be written back, then kick the hardware to force
263 * them to be written back in case we stay in NAPI.
264 * In this mode on X722 we do not enable Interrupt.
265 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800266 j = i40evf_get_tx_pending(tx_ring, false);
Anjali Singhai Jainf6d83d12015-12-22 14:25:07 -0800267
268 if (budget &&
269 ((j / (WB_STRIDE + 1)) == 0) && (j > 0) &&
270 !test_bit(__I40E_DOWN, &tx_ring->vsi->state) &&
271 (I40E_DESC_UNUSED(tx_ring) != tx_ring->count))
272 tx_ring->arm_wb = true;
273 }
274
Greg Rose7f12ad72013-12-21 06:12:51 +0000275 netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
276 tx_ring->queue_index),
277 total_packets, total_bytes);
278
279#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
280 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
281 (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
282 /* Make sure that anybody stopping the queue after this
283 * sees the new next_to_clean.
284 */
285 smp_mb();
286 if (__netif_subqueue_stopped(tx_ring->netdev,
287 tx_ring->queue_index) &&
288 !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
289 netif_wake_subqueue(tx_ring->netdev,
290 tx_ring->queue_index);
291 ++tx_ring->tx_stats.restart_queue;
292 }
293 }
294
Kiran Patilb03a8c12015-09-24 18:13:15 -0400295 return !!budget;
Greg Rose7f12ad72013-12-21 06:12:51 +0000296}
297
298/**
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -0800299 * i40evf_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
300 * @vsi: the VSI we care about
301 * @q_vector: the vector on which to enable writeback
302 *
303 **/
304static void i40e_enable_wb_on_itr(struct i40e_vsi *vsi,
305 struct i40e_q_vector *q_vector)
306{
307 u16 flags = q_vector->tx.ring[0].flags;
308 u32 val;
309
310 if (!(flags & I40E_TXR_FLAGS_WB_ON_ITR))
311 return;
312
313 if (q_vector->arm_wb_state)
314 return;
315
316 val = I40E_VFINT_DYN_CTLN1_WB_ON_ITR_MASK |
317 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK; /* set noitr */
318
319 wr32(&vsi->back->hw,
320 I40E_VFINT_DYN_CTLN1(q_vector->v_idx +
321 vsi->base_vector - 1), val);
322 q_vector->arm_wb_state = true;
323}
324
325/**
326 * i40evf_force_wb - Issue SW Interrupt so HW does a wb
Anjali Singhai Jainc29af372015-01-10 01:07:19 +0000327 * @vsi: the VSI we care about
328 * @q_vector: the vector on which to force writeback
329 *
330 **/
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -0800331void i40evf_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
Anjali Singhai Jainc29af372015-01-10 01:07:19 +0000332{
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -0800333 u32 val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
334 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK | /* set noitr */
335 I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
336 I40E_VFINT_DYN_CTLN1_SW_ITR_INDX_ENA_MASK
337 /* allow 00 to be written to the index */;
Anjali Singhai Jainc29af372015-01-10 01:07:19 +0000338
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -0800339 wr32(&vsi->back->hw,
340 I40E_VFINT_DYN_CTLN1(q_vector->v_idx + vsi->base_vector - 1),
341 val);
Anjali Singhai Jainc29af372015-01-10 01:07:19 +0000342}
343
344/**
Greg Rose7f12ad72013-12-21 06:12:51 +0000345 * i40e_set_new_dynamic_itr - Find new ITR level
346 * @rc: structure containing ring performance data
347 *
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -0400348 * Returns true if ITR changed, false if not
349 *
Greg Rose7f12ad72013-12-21 06:12:51 +0000350 * Stores a new ITR value based on packets and byte counts during
351 * the last interrupt. The advantage of per interrupt computation
352 * is faster updates and more accurate ITR for the current traffic
353 * pattern. Constants in this function were computed based on
354 * theoretical maximum wire speed and thresholds were set based on
355 * testing data as well as attempting to minimize response time
356 * while increasing bulk throughput.
357 **/
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -0400358static bool i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
Greg Rose7f12ad72013-12-21 06:12:51 +0000359{
360 enum i40e_latency_range new_latency_range = rc->latency_range;
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400361 struct i40e_q_vector *qv = rc->ring->q_vector;
Greg Rose7f12ad72013-12-21 06:12:51 +0000362 u32 new_itr = rc->itr;
363 int bytes_per_int;
Jesse Brandeburg51cc6d92015-09-28 14:16:52 -0400364 int usecs;
Greg Rose7f12ad72013-12-21 06:12:51 +0000365
366 if (rc->total_packets == 0 || !rc->itr)
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -0400367 return false;
Greg Rose7f12ad72013-12-21 06:12:51 +0000368
369 /* simple throttlerate management
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400370 * 0-10MB/s lowest (50000 ints/s)
Greg Rose7f12ad72013-12-21 06:12:51 +0000371 * 10-20MB/s low (20000 ints/s)
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400372 * 20-1249MB/s bulk (18000 ints/s)
373 * > 40000 Rx packets per second (8000 ints/s)
Jesse Brandeburg51cc6d92015-09-28 14:16:52 -0400374 *
375 * The math works out because the divisor is in 10^(-6) which
376 * turns the bytes/us input value into MB/s values, but
377 * make sure to use usecs, as the register values written
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400378 * are in 2 usec increments in the ITR registers, and make sure
379 * to use the smoothed values that the countdown timer gives us.
Greg Rose7f12ad72013-12-21 06:12:51 +0000380 */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400381 usecs = (rc->itr << 1) * ITR_COUNTDOWN_START;
Jesse Brandeburg51cc6d92015-09-28 14:16:52 -0400382 bytes_per_int = rc->total_bytes / usecs;
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400383
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -0400384 switch (new_latency_range) {
Greg Rose7f12ad72013-12-21 06:12:51 +0000385 case I40E_LOWEST_LATENCY:
386 if (bytes_per_int > 10)
387 new_latency_range = I40E_LOW_LATENCY;
388 break;
389 case I40E_LOW_LATENCY:
390 if (bytes_per_int > 20)
391 new_latency_range = I40E_BULK_LATENCY;
392 else if (bytes_per_int <= 10)
393 new_latency_range = I40E_LOWEST_LATENCY;
394 break;
395 case I40E_BULK_LATENCY:
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400396 case I40E_ULTRA_LATENCY:
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -0400397 default:
398 if (bytes_per_int <= 20)
399 new_latency_range = I40E_LOW_LATENCY;
Greg Rose7f12ad72013-12-21 06:12:51 +0000400 break;
401 }
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400402
403 /* this is to adjust RX more aggressively when streaming small
404 * packets. The value of 40000 was picked as it is just beyond
405 * what the hardware can receive per second if in low latency
406 * mode.
407 */
408#define RX_ULTRA_PACKET_RATE 40000
409
410 if ((((rc->total_packets * 1000000) / usecs) > RX_ULTRA_PACKET_RATE) &&
411 (&qv->rx == rc))
412 new_latency_range = I40E_ULTRA_LATENCY;
413
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -0400414 rc->latency_range = new_latency_range;
Greg Rose7f12ad72013-12-21 06:12:51 +0000415
416 switch (new_latency_range) {
417 case I40E_LOWEST_LATENCY:
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400418 new_itr = I40E_ITR_50K;
Greg Rose7f12ad72013-12-21 06:12:51 +0000419 break;
420 case I40E_LOW_LATENCY:
421 new_itr = I40E_ITR_20K;
422 break;
423 case I40E_BULK_LATENCY:
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400424 new_itr = I40E_ITR_18K;
425 break;
426 case I40E_ULTRA_LATENCY:
Greg Rose7f12ad72013-12-21 06:12:51 +0000427 new_itr = I40E_ITR_8K;
428 break;
429 default:
430 break;
431 }
432
Greg Rose7f12ad72013-12-21 06:12:51 +0000433 rc->total_bytes = 0;
434 rc->total_packets = 0;
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -0400435
436 if (new_itr != rc->itr) {
437 rc->itr = new_itr;
438 return true;
439 }
440
441 return false;
Greg Rose7f12ad72013-12-21 06:12:51 +0000442}
443
Jesse Brandeburg4eeb1ff2015-11-18 17:35:42 -0800444/**
Greg Rose7f12ad72013-12-21 06:12:51 +0000445 * i40evf_setup_tx_descriptors - Allocate the Tx descriptors
446 * @tx_ring: the tx ring to set up
447 *
448 * Return 0 on success, negative on error
449 **/
450int i40evf_setup_tx_descriptors(struct i40e_ring *tx_ring)
451{
452 struct device *dev = tx_ring->dev;
453 int bi_size;
454
455 if (!dev)
456 return -ENOMEM;
457
Mitch Williams67c818a2015-06-19 08:56:30 -0700458 /* warn if we are about to overwrite the pointer */
459 WARN_ON(tx_ring->tx_bi);
Greg Rose7f12ad72013-12-21 06:12:51 +0000460 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
461 tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
462 if (!tx_ring->tx_bi)
463 goto err;
464
465 /* round up to nearest 4K */
466 tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000467 /* add u32 for head writeback, align after this takes care of
468 * guaranteeing this is at least one cache line in size
469 */
470 tx_ring->size += sizeof(u32);
Greg Rose7f12ad72013-12-21 06:12:51 +0000471 tx_ring->size = ALIGN(tx_ring->size, 4096);
472 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
473 &tx_ring->dma, GFP_KERNEL);
474 if (!tx_ring->desc) {
475 dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
476 tx_ring->size);
477 goto err;
478 }
479
480 tx_ring->next_to_use = 0;
481 tx_ring->next_to_clean = 0;
482 return 0;
483
484err:
485 kfree(tx_ring->tx_bi);
486 tx_ring->tx_bi = NULL;
487 return -ENOMEM;
488}
489
490/**
491 * i40evf_clean_rx_ring - Free Rx buffers
492 * @rx_ring: ring to be cleaned
493 **/
494void i40evf_clean_rx_ring(struct i40e_ring *rx_ring)
495{
496 struct device *dev = rx_ring->dev;
497 struct i40e_rx_buffer *rx_bi;
498 unsigned long bi_size;
499 u16 i;
500
501 /* ring already cleared, nothing to do */
502 if (!rx_ring->rx_bi)
503 return;
504
Mitch Williamsa132af22015-01-24 09:58:35 +0000505 if (ring_is_ps_enabled(rx_ring)) {
506 int bufsz = ALIGN(rx_ring->rx_hdr_len, 256) * rx_ring->count;
507
508 rx_bi = &rx_ring->rx_bi[0];
509 if (rx_bi->hdr_buf) {
510 dma_free_coherent(dev,
511 bufsz,
512 rx_bi->hdr_buf,
513 rx_bi->dma);
514 for (i = 0; i < rx_ring->count; i++) {
515 rx_bi = &rx_ring->rx_bi[i];
516 rx_bi->dma = 0;
Shannon Nelson37a29732015-02-27 09:15:19 +0000517 rx_bi->hdr_buf = NULL;
Mitch Williamsa132af22015-01-24 09:58:35 +0000518 }
519 }
520 }
Greg Rose7f12ad72013-12-21 06:12:51 +0000521 /* Free all the Rx ring sk_buffs */
522 for (i = 0; i < rx_ring->count; i++) {
523 rx_bi = &rx_ring->rx_bi[i];
524 if (rx_bi->dma) {
525 dma_unmap_single(dev,
526 rx_bi->dma,
527 rx_ring->rx_buf_len,
528 DMA_FROM_DEVICE);
529 rx_bi->dma = 0;
530 }
531 if (rx_bi->skb) {
532 dev_kfree_skb(rx_bi->skb);
533 rx_bi->skb = NULL;
534 }
535 if (rx_bi->page) {
536 if (rx_bi->page_dma) {
537 dma_unmap_page(dev,
538 rx_bi->page_dma,
Mitch Williamsf16704e2016-01-13 16:51:49 -0800539 PAGE_SIZE,
Greg Rose7f12ad72013-12-21 06:12:51 +0000540 DMA_FROM_DEVICE);
541 rx_bi->page_dma = 0;
542 }
543 __free_page(rx_bi->page);
544 rx_bi->page = NULL;
545 rx_bi->page_offset = 0;
546 }
547 }
548
549 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
550 memset(rx_ring->rx_bi, 0, bi_size);
551
552 /* Zero out the descriptor ring */
553 memset(rx_ring->desc, 0, rx_ring->size);
554
555 rx_ring->next_to_clean = 0;
556 rx_ring->next_to_use = 0;
557}
558
559/**
560 * i40evf_free_rx_resources - Free Rx resources
561 * @rx_ring: ring to clean the resources from
562 *
563 * Free all receive software resources
564 **/
565void i40evf_free_rx_resources(struct i40e_ring *rx_ring)
566{
567 i40evf_clean_rx_ring(rx_ring);
568 kfree(rx_ring->rx_bi);
569 rx_ring->rx_bi = NULL;
570
571 if (rx_ring->desc) {
572 dma_free_coherent(rx_ring->dev, rx_ring->size,
573 rx_ring->desc, rx_ring->dma);
574 rx_ring->desc = NULL;
575 }
576}
577
578/**
Mitch Williamsa132af22015-01-24 09:58:35 +0000579 * i40evf_alloc_rx_headers - allocate rx header buffers
580 * @rx_ring: ring to alloc buffers
581 *
582 * Allocate rx header buffers for the entire ring. As these are static,
583 * this is only called when setting up a new ring.
584 **/
585void i40evf_alloc_rx_headers(struct i40e_ring *rx_ring)
586{
587 struct device *dev = rx_ring->dev;
588 struct i40e_rx_buffer *rx_bi;
589 dma_addr_t dma;
590 void *buffer;
591 int buf_size;
592 int i;
593
594 if (rx_ring->rx_bi[0].hdr_buf)
595 return;
596 /* Make sure the buffers don't cross cache line boundaries. */
597 buf_size = ALIGN(rx_ring->rx_hdr_len, 256);
598 buffer = dma_alloc_coherent(dev, buf_size * rx_ring->count,
599 &dma, GFP_KERNEL);
600 if (!buffer)
601 return;
602 for (i = 0; i < rx_ring->count; i++) {
603 rx_bi = &rx_ring->rx_bi[i];
604 rx_bi->dma = dma + (i * buf_size);
605 rx_bi->hdr_buf = buffer + (i * buf_size);
606 }
607}
608
609/**
Greg Rose7f12ad72013-12-21 06:12:51 +0000610 * i40evf_setup_rx_descriptors - Allocate Rx descriptors
611 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
612 *
613 * Returns 0 on success, negative on failure
614 **/
615int i40evf_setup_rx_descriptors(struct i40e_ring *rx_ring)
616{
617 struct device *dev = rx_ring->dev;
618 int bi_size;
619
Mitch Williams67c818a2015-06-19 08:56:30 -0700620 /* warn if we are about to overwrite the pointer */
621 WARN_ON(rx_ring->rx_bi);
Greg Rose7f12ad72013-12-21 06:12:51 +0000622 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
623 rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
624 if (!rx_ring->rx_bi)
625 goto err;
626
Carolyn Wybornyf217d6c2015-02-09 17:42:31 -0800627 u64_stats_init(&rx_ring->syncp);
Carolyn Wyborny638702b2015-01-24 09:58:32 +0000628
Greg Rose7f12ad72013-12-21 06:12:51 +0000629 /* Round up to nearest 4K */
630 rx_ring->size = ring_is_16byte_desc_enabled(rx_ring)
631 ? rx_ring->count * sizeof(union i40e_16byte_rx_desc)
632 : rx_ring->count * sizeof(union i40e_32byte_rx_desc);
633 rx_ring->size = ALIGN(rx_ring->size, 4096);
634 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
635 &rx_ring->dma, GFP_KERNEL);
636
637 if (!rx_ring->desc) {
638 dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
639 rx_ring->size);
640 goto err;
641 }
642
643 rx_ring->next_to_clean = 0;
644 rx_ring->next_to_use = 0;
645
646 return 0;
647err:
648 kfree(rx_ring->rx_bi);
649 rx_ring->rx_bi = NULL;
650 return -ENOMEM;
651}
652
653/**
654 * i40e_release_rx_desc - Store the new tail and head values
655 * @rx_ring: ring to bump
656 * @val: new head index
657 **/
658static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
659{
660 rx_ring->next_to_use = val;
661 /* Force memory writes to complete before letting h/w
662 * know there are new descriptors to fetch. (Only
663 * applicable for weak-ordered memory model archs,
664 * such as IA-64).
665 */
666 wmb();
667 writel(val, rx_ring->tail);
668}
669
670/**
Mitch Williamsa132af22015-01-24 09:58:35 +0000671 * i40evf_alloc_rx_buffers_ps - Replace used receive buffers; packet split
Greg Rose7f12ad72013-12-21 06:12:51 +0000672 * @rx_ring: ring to place buffers on
673 * @cleaned_count: number of buffers to replace
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800674 *
675 * Returns true if any errors on allocation
Greg Rose7f12ad72013-12-21 06:12:51 +0000676 **/
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800677bool i40evf_alloc_rx_buffers_ps(struct i40e_ring *rx_ring, u16 cleaned_count)
Mitch Williamsa132af22015-01-24 09:58:35 +0000678{
679 u16 i = rx_ring->next_to_use;
680 union i40e_rx_desc *rx_desc;
681 struct i40e_rx_buffer *bi;
Mitch Williamsf16704e2016-01-13 16:51:49 -0800682 const int current_node = numa_node_id();
Mitch Williamsa132af22015-01-24 09:58:35 +0000683
684 /* do nothing if no valid netdev defined */
685 if (!rx_ring->netdev || !cleaned_count)
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800686 return false;
Mitch Williamsa132af22015-01-24 09:58:35 +0000687
688 while (cleaned_count--) {
689 rx_desc = I40E_RX_DESC(rx_ring, i);
690 bi = &rx_ring->rx_bi[i];
691
692 if (bi->skb) /* desc is in use */
693 goto no_buffers;
Mitch Williamsf16704e2016-01-13 16:51:49 -0800694
695 /* If we've been moved to a different NUMA node, release the
696 * page so we can get a new one on the current node.
697 */
698 if (bi->page && page_to_nid(bi->page) != current_node) {
699 dma_unmap_page(rx_ring->dev,
700 bi->page_dma,
701 PAGE_SIZE,
702 DMA_FROM_DEVICE);
703 __free_page(bi->page);
704 bi->page = NULL;
705 bi->page_dma = 0;
706 rx_ring->rx_stats.realloc_count++;
707 } else if (bi->page) {
708 rx_ring->rx_stats.page_reuse_count++;
709 }
710
Mitch Williamsa132af22015-01-24 09:58:35 +0000711 if (!bi->page) {
712 bi->page = alloc_page(GFP_ATOMIC);
713 if (!bi->page) {
714 rx_ring->rx_stats.alloc_page_failed++;
715 goto no_buffers;
716 }
Mitch Williamsa132af22015-01-24 09:58:35 +0000717 bi->page_dma = dma_map_page(rx_ring->dev,
718 bi->page,
Mitch Williamsf16704e2016-01-13 16:51:49 -0800719 0,
720 PAGE_SIZE,
Mitch Williamsa132af22015-01-24 09:58:35 +0000721 DMA_FROM_DEVICE);
Mitch Williamsf16704e2016-01-13 16:51:49 -0800722 if (dma_mapping_error(rx_ring->dev, bi->page_dma)) {
Mitch Williamsa132af22015-01-24 09:58:35 +0000723 rx_ring->rx_stats.alloc_page_failed++;
Mitch Williamsf16704e2016-01-13 16:51:49 -0800724 __free_page(bi->page);
725 bi->page = NULL;
Mitch Williamsa132af22015-01-24 09:58:35 +0000726 bi->page_dma = 0;
Mitch Williamsf16704e2016-01-13 16:51:49 -0800727 bi->page_offset = 0;
Mitch Williamsa132af22015-01-24 09:58:35 +0000728 goto no_buffers;
729 }
Mitch Williamsf16704e2016-01-13 16:51:49 -0800730 bi->page_offset = 0;
Mitch Williamsa132af22015-01-24 09:58:35 +0000731 }
732
Mitch Williamsa132af22015-01-24 09:58:35 +0000733 /* Refresh the desc even if buffer_addrs didn't change
734 * because each write-back erases this info.
735 */
Mitch Williamsf16704e2016-01-13 16:51:49 -0800736 rx_desc->read.pkt_addr =
737 cpu_to_le64(bi->page_dma + bi->page_offset);
Mitch Williamsa132af22015-01-24 09:58:35 +0000738 rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
739 i++;
740 if (i == rx_ring->count)
741 i = 0;
742 }
743
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800744 if (rx_ring->next_to_use != i)
745 i40e_release_rx_desc(rx_ring, i);
746
747 return false;
748
Mitch Williamsa132af22015-01-24 09:58:35 +0000749no_buffers:
750 if (rx_ring->next_to_use != i)
751 i40e_release_rx_desc(rx_ring, i);
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800752
753 /* make sure to come back via polling to try again after
754 * allocation failure
755 */
756 return true;
Mitch Williamsa132af22015-01-24 09:58:35 +0000757}
758
759/**
760 * i40evf_alloc_rx_buffers_1buf - Replace used receive buffers; single buffer
761 * @rx_ring: ring to place buffers on
762 * @cleaned_count: number of buffers to replace
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800763 *
764 * Returns true if any errors on allocation
Mitch Williamsa132af22015-01-24 09:58:35 +0000765 **/
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800766bool i40evf_alloc_rx_buffers_1buf(struct i40e_ring *rx_ring, u16 cleaned_count)
Greg Rose7f12ad72013-12-21 06:12:51 +0000767{
768 u16 i = rx_ring->next_to_use;
769 union i40e_rx_desc *rx_desc;
770 struct i40e_rx_buffer *bi;
771 struct sk_buff *skb;
772
773 /* do nothing if no valid netdev defined */
774 if (!rx_ring->netdev || !cleaned_count)
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800775 return false;
Greg Rose7f12ad72013-12-21 06:12:51 +0000776
777 while (cleaned_count--) {
778 rx_desc = I40E_RX_DESC(rx_ring, i);
779 bi = &rx_ring->rx_bi[i];
780 skb = bi->skb;
781
782 if (!skb) {
Jesse Brandeburgdd1a5df2016-01-13 16:51:48 -0800783 skb = __netdev_alloc_skb_ip_align(rx_ring->netdev,
784 rx_ring->rx_buf_len,
785 GFP_ATOMIC |
786 __GFP_NOWARN);
Greg Rose7f12ad72013-12-21 06:12:51 +0000787 if (!skb) {
788 rx_ring->rx_stats.alloc_buff_failed++;
789 goto no_buffers;
790 }
791 /* initialize queue mapping */
792 skb_record_rx_queue(skb, rx_ring->queue_index);
793 bi->skb = skb;
794 }
795
796 if (!bi->dma) {
797 bi->dma = dma_map_single(rx_ring->dev,
798 skb->data,
799 rx_ring->rx_buf_len,
800 DMA_FROM_DEVICE);
801 if (dma_mapping_error(rx_ring->dev, bi->dma)) {
802 rx_ring->rx_stats.alloc_buff_failed++;
803 bi->dma = 0;
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800804 dev_kfree_skb(bi->skb);
805 bi->skb = NULL;
Greg Rose7f12ad72013-12-21 06:12:51 +0000806 goto no_buffers;
807 }
808 }
809
Mitch Williamsa132af22015-01-24 09:58:35 +0000810 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
811 rx_desc->read.hdr_addr = 0;
Greg Rose7f12ad72013-12-21 06:12:51 +0000812 i++;
813 if (i == rx_ring->count)
814 i = 0;
815 }
816
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800817 if (rx_ring->next_to_use != i)
818 i40e_release_rx_desc(rx_ring, i);
819
820 return false;
821
Greg Rose7f12ad72013-12-21 06:12:51 +0000822no_buffers:
823 if (rx_ring->next_to_use != i)
824 i40e_release_rx_desc(rx_ring, i);
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -0800825
826 /* make sure to come back via polling to try again after
827 * allocation failure
828 */
829 return true;
Greg Rose7f12ad72013-12-21 06:12:51 +0000830}
831
832/**
833 * i40e_receive_skb - Send a completed packet up the stack
834 * @rx_ring: rx ring in play
835 * @skb: packet to send up
836 * @vlan_tag: vlan tag for packet
837 **/
838static void i40e_receive_skb(struct i40e_ring *rx_ring,
839 struct sk_buff *skb, u16 vlan_tag)
840{
841 struct i40e_q_vector *q_vector = rx_ring->q_vector;
Greg Rose7f12ad72013-12-21 06:12:51 +0000842
843 if (vlan_tag & VLAN_VID_MASK)
844 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
845
Alexander Duyck8b650352015-09-24 09:04:32 -0700846 napi_gro_receive(&q_vector->napi, skb);
Greg Rose7f12ad72013-12-21 06:12:51 +0000847}
848
849/**
850 * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
851 * @vsi: the VSI we care about
852 * @skb: skb currently being received and modified
853 * @rx_status: status value of last descriptor in packet
854 * @rx_error: error value of last descriptor in packet
855 * @rx_ptype: ptype value of last descriptor in packet
856 **/
857static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
858 struct sk_buff *skb,
859 u32 rx_status,
860 u32 rx_error,
861 u16 rx_ptype)
862{
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000863 struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(rx_ptype);
864 bool ipv4 = false, ipv6 = false;
Greg Rose7f12ad72013-12-21 06:12:51 +0000865 bool ipv4_tunnel, ipv6_tunnel;
866 __wsum rx_udp_csum;
Greg Rose7f12ad72013-12-21 06:12:51 +0000867 struct iphdr *iph;
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000868 __sum16 csum;
Greg Rose7f12ad72013-12-21 06:12:51 +0000869
Anjali Singhai Jainf8faaa42015-02-24 06:58:48 +0000870 ipv4_tunnel = (rx_ptype >= I40E_RX_PTYPE_GRENAT4_MAC_PAY3) &&
871 (rx_ptype <= I40E_RX_PTYPE_GRENAT4_MACVLAN_IPV6_ICMP_PAY4);
872 ipv6_tunnel = (rx_ptype >= I40E_RX_PTYPE_GRENAT6_MAC_PAY3) &&
873 (rx_ptype <= I40E_RX_PTYPE_GRENAT6_MACVLAN_IPV6_ICMP_PAY4);
Greg Rose7f12ad72013-12-21 06:12:51 +0000874
Greg Rose7f12ad72013-12-21 06:12:51 +0000875 skb->ip_summed = CHECKSUM_NONE;
876
877 /* Rx csum enabled and ip headers found? */
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000878 if (!(vsi->netdev->features & NETIF_F_RXCSUM))
Greg Rose7f12ad72013-12-21 06:12:51 +0000879 return;
880
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000881 /* did the hardware decode the packet and checksum? */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400882 if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000883 return;
884
885 /* both known and outer_ip must be set for the below code to work */
886 if (!(decoded.known && decoded.outer_ip))
887 return;
888
889 if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
890 decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4)
891 ipv4 = true;
892 else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
893 decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6)
894 ipv6 = true;
895
896 if (ipv4 &&
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400897 (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
898 BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000899 goto checksum_fail;
900
Jesse Brandeburgddf1d0d2014-02-13 03:48:39 -0800901 /* likely incorrect csum if alternate IP extension headers found */
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000902 if (ipv6 &&
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400903 rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000904 /* don't increment checksum err here, non-fatal err */
Greg Rose7f12ad72013-12-21 06:12:51 +0000905 return;
906
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000907 /* there was some L4 error, count error and punt packet to the stack */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400908 if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000909 goto checksum_fail;
Greg Rose7f12ad72013-12-21 06:12:51 +0000910
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000911 /* handle packets that were not able to be checksummed due
912 * to arrival speed, in this case the stack can compute
913 * the csum.
914 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400915 if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000916 return;
917
918 /* If VXLAN traffic has an outer UDPv4 checksum we need to check
919 * it in the driver, hardware does not do it for us.
920 * Since L3L4P bit was set we assume a valid IHL value (>=5)
921 * so the total length of IPv4 header is IHL*4 bytes
922 * The UDP_0 bit *may* bet set if the *inner* header is UDP
923 */
Anjali Singhai Jain818f2e72015-03-31 00:44:59 -0700924 if (ipv4_tunnel) {
Greg Rose7f12ad72013-12-21 06:12:51 +0000925 skb->transport_header = skb->mac_header +
926 sizeof(struct ethhdr) +
927 (ip_hdr(skb)->ihl * 4);
928
929 /* Add 4 bytes for VLAN tagged packets */
930 skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
931 skb->protocol == htons(ETH_P_8021AD))
932 ? VLAN_HLEN : 0;
933
Anjali Singhai Jain818f2e72015-03-31 00:44:59 -0700934 if ((ip_hdr(skb)->protocol == IPPROTO_UDP) &&
935 (udp_hdr(skb)->check != 0)) {
936 rx_udp_csum = udp_csum(skb);
937 iph = ip_hdr(skb);
938 csum = csum_tcpudp_magic(iph->saddr, iph->daddr,
939 (skb->len -
940 skb_transport_offset(skb)),
941 IPPROTO_UDP, rx_udp_csum);
Greg Rose7f12ad72013-12-21 06:12:51 +0000942
Anjali Singhai Jain818f2e72015-03-31 00:44:59 -0700943 if (udp_hdr(skb)->check != csum)
944 goto checksum_fail;
945
946 } /* else its GRE and so no outer UDP header */
Greg Rose7f12ad72013-12-21 06:12:51 +0000947 }
948
949 skb->ip_summed = CHECKSUM_UNNECESSARY;
Tom Herbert407fa082014-08-27 21:27:43 -0700950 skb->csum_level = ipv4_tunnel || ipv6_tunnel;
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +0000951
952 return;
953
954checksum_fail:
955 vsi->back->hw_csum_rx_error++;
Greg Rose7f12ad72013-12-21 06:12:51 +0000956}
957
958/**
Anjali Singhai Jain857942f2015-12-09 15:50:21 -0800959 * i40e_ptype_to_htype - get a hash type
Jesse Brandeburg206812b2014-02-12 01:45:33 +0000960 * @ptype: the ptype value from the descriptor
961 *
962 * Returns a hash type to be used by skb_set_hash
963 **/
Anjali Singhai Jain857942f2015-12-09 15:50:21 -0800964static inline enum pkt_hash_types i40e_ptype_to_htype(u8 ptype)
Jesse Brandeburg206812b2014-02-12 01:45:33 +0000965{
966 struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
967
968 if (!decoded.known)
969 return PKT_HASH_TYPE_NONE;
970
971 if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
972 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
973 return PKT_HASH_TYPE_L4;
974 else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
975 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
976 return PKT_HASH_TYPE_L3;
977 else
978 return PKT_HASH_TYPE_L2;
979}
980
981/**
Anjali Singhai Jain857942f2015-12-09 15:50:21 -0800982 * i40e_rx_hash - set the hash value in the skb
983 * @ring: descriptor ring
984 * @rx_desc: specific descriptor
985 **/
986static inline void i40e_rx_hash(struct i40e_ring *ring,
987 union i40e_rx_desc *rx_desc,
988 struct sk_buff *skb,
989 u8 rx_ptype)
990{
991 u32 hash;
992 const __le64 rss_mask =
993 cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
994 I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
995
996 if (ring->netdev->features & NETIF_F_RXHASH)
997 return;
998
999 if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
1000 hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
1001 skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
1002 }
1003}
1004
1005/**
Mitch Williamsa132af22015-01-24 09:58:35 +00001006 * i40e_clean_rx_irq_ps - Reclaim resources after receive; packet split
Greg Rose7f12ad72013-12-21 06:12:51 +00001007 * @rx_ring: rx ring to clean
1008 * @budget: how many cleans we're allowed
1009 *
1010 * Returns true if there's any budget left (e.g. the clean is finished)
1011 **/
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001012static int i40e_clean_rx_irq_ps(struct i40e_ring *rx_ring, const int budget)
Greg Rose7f12ad72013-12-21 06:12:51 +00001013{
1014 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1015 u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
1016 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
Greg Rose7f12ad72013-12-21 06:12:51 +00001017 struct i40e_vsi *vsi = rx_ring->vsi;
1018 u16 i = rx_ring->next_to_clean;
1019 union i40e_rx_desc *rx_desc;
1020 u32 rx_error, rx_status;
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001021 bool failure = false;
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001022 u8 rx_ptype;
Greg Rose7f12ad72013-12-21 06:12:51 +00001023 u64 qword;
Mitch Williamsf16704e2016-01-13 16:51:49 -08001024 u32 copysize;
Greg Rose7f12ad72013-12-21 06:12:51 +00001025
Mitch Williamsa132af22015-01-24 09:58:35 +00001026 do {
Greg Rose7f12ad72013-12-21 06:12:51 +00001027 struct i40e_rx_buffer *rx_bi;
1028 struct sk_buff *skb;
1029 u16 vlan_tag;
Mitch Williamsa132af22015-01-24 09:58:35 +00001030 /* return some buffers to hardware, one at a time is too slow */
1031 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001032 failure = failure ||
1033 i40evf_alloc_rx_buffers_ps(rx_ring,
1034 cleaned_count);
Mitch Williamsa132af22015-01-24 09:58:35 +00001035 cleaned_count = 0;
1036 }
1037
1038 i = rx_ring->next_to_clean;
1039 rx_desc = I40E_RX_DESC(rx_ring, i);
1040 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1041 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1042 I40E_RXD_QW1_STATUS_SHIFT;
1043
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001044 if (!(rx_status & BIT(I40E_RX_DESC_STATUS_DD_SHIFT)))
Mitch Williamsa132af22015-01-24 09:58:35 +00001045 break;
1046
1047 /* This memory barrier is needed to keep us from reading
1048 * any other fields out of the rx_desc until we know the
1049 * DD bit is set.
1050 */
Alexander Duyck67317162015-04-08 18:49:43 -07001051 dma_rmb();
Mitch Williamsf16704e2016-01-13 16:51:49 -08001052 /* sync header buffer for reading */
1053 dma_sync_single_range_for_cpu(rx_ring->dev,
1054 rx_ring->rx_bi[0].dma,
1055 i * rx_ring->rx_hdr_len,
1056 rx_ring->rx_hdr_len,
1057 DMA_FROM_DEVICE);
Greg Rose7f12ad72013-12-21 06:12:51 +00001058 rx_bi = &rx_ring->rx_bi[i];
1059 skb = rx_bi->skb;
Mitch Williamsa132af22015-01-24 09:58:35 +00001060 if (likely(!skb)) {
Jesse Brandeburgdd1a5df2016-01-13 16:51:48 -08001061 skb = __netdev_alloc_skb_ip_align(rx_ring->netdev,
1062 rx_ring->rx_hdr_len,
1063 GFP_ATOMIC |
1064 __GFP_NOWARN);
Jesse Brandeburg8b6ed9c2015-03-31 00:45:01 -07001065 if (!skb) {
Mitch Williamsa132af22015-01-24 09:58:35 +00001066 rx_ring->rx_stats.alloc_buff_failed++;
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001067 failure = true;
Jesse Brandeburg8b6ed9c2015-03-31 00:45:01 -07001068 break;
1069 }
1070
Mitch Williamsa132af22015-01-24 09:58:35 +00001071 /* initialize queue mapping */
1072 skb_record_rx_queue(skb, rx_ring->queue_index);
1073 /* we are reusing so sync this buffer for CPU use */
1074 dma_sync_single_range_for_cpu(rx_ring->dev,
Jesse Brandeburg3578fa02016-01-04 10:33:03 -08001075 rx_ring->rx_bi[0].dma,
1076 i * rx_ring->rx_hdr_len,
Mitch Williamsa132af22015-01-24 09:58:35 +00001077 rx_ring->rx_hdr_len,
1078 DMA_FROM_DEVICE);
1079 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001080 rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
1081 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
1082 rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
1083 I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
1084 rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
1085 I40E_RXD_QW1_LENGTH_SPH_SHIFT;
1086
1087 rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
1088 I40E_RXD_QW1_ERROR_SHIFT;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001089 rx_hbo = rx_error & BIT(I40E_RX_DESC_ERROR_HBO_SHIFT);
1090 rx_error &= ~BIT(I40E_RX_DESC_ERROR_HBO_SHIFT);
Greg Rose7f12ad72013-12-21 06:12:51 +00001091
1092 rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
1093 I40E_RXD_QW1_PTYPE_SHIFT;
Mitch Williamsf16704e2016-01-13 16:51:49 -08001094 /* sync half-page for reading */
1095 dma_sync_single_range_for_cpu(rx_ring->dev,
1096 rx_bi->page_dma,
1097 rx_bi->page_offset,
1098 PAGE_SIZE / 2,
1099 DMA_FROM_DEVICE);
1100 prefetch(page_address(rx_bi->page) + rx_bi->page_offset);
Greg Rose7f12ad72013-12-21 06:12:51 +00001101 rx_bi->skb = NULL;
Mitch Williamsa132af22015-01-24 09:58:35 +00001102 cleaned_count++;
Mitch Williamsf16704e2016-01-13 16:51:49 -08001103 copysize = 0;
Mitch Williamsa132af22015-01-24 09:58:35 +00001104 if (rx_hbo || rx_sph) {
1105 int len;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001106
Greg Rose7f12ad72013-12-21 06:12:51 +00001107 if (rx_hbo)
1108 len = I40E_RX_HDR_SIZE;
Greg Rose7f12ad72013-12-21 06:12:51 +00001109 else
Mitch Williamsa132af22015-01-24 09:58:35 +00001110 len = rx_header_len;
1111 memcpy(__skb_put(skb, len), rx_bi->hdr_buf, len);
1112 } else if (skb->len == 0) {
1113 int len;
Mitch Williamsf16704e2016-01-13 16:51:49 -08001114 unsigned char *va = page_address(rx_bi->page) +
1115 rx_bi->page_offset;
Greg Rose7f12ad72013-12-21 06:12:51 +00001116
Mitch Williamsf16704e2016-01-13 16:51:49 -08001117 len = min(rx_packet_len, rx_ring->rx_hdr_len);
1118 memcpy(__skb_put(skb, len), va, len);
1119 copysize = len;
Mitch Williamsa132af22015-01-24 09:58:35 +00001120 rx_packet_len -= len;
Greg Rose7f12ad72013-12-21 06:12:51 +00001121 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001122 /* Get the rest of the data if this was a header split */
Mitch Williamsa132af22015-01-24 09:58:35 +00001123 if (rx_packet_len) {
Mitch Williamsf16704e2016-01-13 16:51:49 -08001124 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1125 rx_bi->page,
1126 rx_bi->page_offset + copysize,
1127 rx_packet_len, I40E_RXBUFFER_2048);
Greg Rose7f12ad72013-12-21 06:12:51 +00001128
Mitch Williamsf16704e2016-01-13 16:51:49 -08001129 /* If the page count is more than 2, then both halves
1130 * of the page are used and we need to free it. Do it
1131 * here instead of in the alloc code. Otherwise one
1132 * of the half-pages might be released between now and
1133 * then, and we wouldn't know which one to use.
Mitch Williams16fd08b2016-01-15 14:33:15 -08001134 * Don't call get_page and free_page since those are
1135 * both expensive atomic operations that just change
1136 * the refcount in opposite directions. Just give the
1137 * page to the stack; he can have our refcount.
Mitch Williamsf16704e2016-01-13 16:51:49 -08001138 */
1139 if (page_count(rx_bi->page) > 2) {
1140 dma_unmap_page(rx_ring->dev,
1141 rx_bi->page_dma,
1142 PAGE_SIZE,
1143 DMA_FROM_DEVICE);
Greg Rose7f12ad72013-12-21 06:12:51 +00001144 rx_bi->page = NULL;
Mitch Williamsf16704e2016-01-13 16:51:49 -08001145 rx_bi->page_dma = 0;
1146 rx_ring->rx_stats.realloc_count++;
Mitch Williams16fd08b2016-01-15 14:33:15 -08001147 } else {
1148 get_page(rx_bi->page);
1149 /* switch to the other half-page here; the
1150 * allocation code programs the right addr
1151 * into HW. If we haven't used this half-page,
1152 * the address won't be changed, and HW can
1153 * just use it next time through.
1154 */
1155 rx_bi->page_offset ^= PAGE_SIZE / 2;
Mitch Williamsf16704e2016-01-13 16:51:49 -08001156 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001157
Greg Rose7f12ad72013-12-21 06:12:51 +00001158 }
Mitch Williamsa132af22015-01-24 09:58:35 +00001159 I40E_RX_INCREMENT(rx_ring, i);
Greg Rose7f12ad72013-12-21 06:12:51 +00001160
1161 if (unlikely(
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001162 !(rx_status & BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001163 struct i40e_rx_buffer *next_buffer;
1164
1165 next_buffer = &rx_ring->rx_bi[i];
Mitch Williamsa132af22015-01-24 09:58:35 +00001166 next_buffer->skb = skb;
Greg Rose7f12ad72013-12-21 06:12:51 +00001167 rx_ring->rx_stats.non_eop_descs++;
Mitch Williamsa132af22015-01-24 09:58:35 +00001168 continue;
Greg Rose7f12ad72013-12-21 06:12:51 +00001169 }
1170
1171 /* ERR_MASK will only have valid bits if EOP set */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001172 if (unlikely(rx_error & BIT(I40E_RX_DESC_ERROR_RXE_SHIFT))) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001173 dev_kfree_skb_any(skb);
Mitch Williamsa132af22015-01-24 09:58:35 +00001174 continue;
1175 }
1176
Anjali Singhai Jain857942f2015-12-09 15:50:21 -08001177 i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
1178
Mitch Williamsa132af22015-01-24 09:58:35 +00001179 /* probably a little skewed due to removing CRC */
1180 total_rx_bytes += skb->len;
1181 total_rx_packets++;
1182
1183 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1184
1185 i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
1186
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001187 vlan_tag = rx_status & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
Mitch Williamsa132af22015-01-24 09:58:35 +00001188 ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
1189 : 0;
1190#ifdef I40E_FCOE
1191 if (!i40e_fcoe_handle_offload(rx_ring, rx_desc, skb)) {
1192 dev_kfree_skb_any(skb);
1193 continue;
1194 }
1195#endif
Mitch Williamsa132af22015-01-24 09:58:35 +00001196 i40e_receive_skb(rx_ring, skb, vlan_tag);
1197
Mitch Williamsa132af22015-01-24 09:58:35 +00001198 rx_desc->wb.qword1.status_error_len = 0;
1199
1200 } while (likely(total_rx_packets < budget));
1201
1202 u64_stats_update_begin(&rx_ring->syncp);
1203 rx_ring->stats.packets += total_rx_packets;
1204 rx_ring->stats.bytes += total_rx_bytes;
1205 u64_stats_update_end(&rx_ring->syncp);
1206 rx_ring->q_vector->rx.total_packets += total_rx_packets;
1207 rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
1208
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001209 return failure ? budget : total_rx_packets;
Mitch Williamsa132af22015-01-24 09:58:35 +00001210}
1211
1212/**
1213 * i40e_clean_rx_irq_1buf - Reclaim resources after receive; single buffer
1214 * @rx_ring: rx ring to clean
1215 * @budget: how many cleans we're allowed
1216 *
1217 * Returns number of packets cleaned
1218 **/
1219static int i40e_clean_rx_irq_1buf(struct i40e_ring *rx_ring, int budget)
1220{
1221 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1222 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
1223 struct i40e_vsi *vsi = rx_ring->vsi;
1224 union i40e_rx_desc *rx_desc;
1225 u32 rx_error, rx_status;
1226 u16 rx_packet_len;
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001227 bool failure = false;
Mitch Williamsa132af22015-01-24 09:58:35 +00001228 u8 rx_ptype;
1229 u64 qword;
1230 u16 i;
1231
1232 do {
1233 struct i40e_rx_buffer *rx_bi;
1234 struct sk_buff *skb;
1235 u16 vlan_tag;
1236 /* return some buffers to hardware, one at a time is too slow */
1237 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001238 failure = failure ||
1239 i40evf_alloc_rx_buffers_1buf(rx_ring,
1240 cleaned_count);
Mitch Williamsa132af22015-01-24 09:58:35 +00001241 cleaned_count = 0;
1242 }
1243
1244 i = rx_ring->next_to_clean;
1245 rx_desc = I40E_RX_DESC(rx_ring, i);
1246 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1247 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1248 I40E_RXD_QW1_STATUS_SHIFT;
1249
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001250 if (!(rx_status & BIT(I40E_RX_DESC_STATUS_DD_SHIFT)))
Mitch Williamsa132af22015-01-24 09:58:35 +00001251 break;
1252
1253 /* This memory barrier is needed to keep us from reading
1254 * any other fields out of the rx_desc until we know the
1255 * DD bit is set.
1256 */
Alexander Duyck67317162015-04-08 18:49:43 -07001257 dma_rmb();
Mitch Williamsa132af22015-01-24 09:58:35 +00001258
1259 rx_bi = &rx_ring->rx_bi[i];
1260 skb = rx_bi->skb;
1261 prefetch(skb->data);
1262
1263 rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
1264 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
1265
1266 rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
1267 I40E_RXD_QW1_ERROR_SHIFT;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001268 rx_error &= ~BIT(I40E_RX_DESC_ERROR_HBO_SHIFT);
Mitch Williamsa132af22015-01-24 09:58:35 +00001269
1270 rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
1271 I40E_RXD_QW1_PTYPE_SHIFT;
1272 rx_bi->skb = NULL;
1273 cleaned_count++;
1274
1275 /* Get the header and possibly the whole packet
1276 * If this is an skb from previous receive dma will be 0
1277 */
1278 skb_put(skb, rx_packet_len);
1279 dma_unmap_single(rx_ring->dev, rx_bi->dma, rx_ring->rx_buf_len,
1280 DMA_FROM_DEVICE);
1281 rx_bi->dma = 0;
1282
1283 I40E_RX_INCREMENT(rx_ring, i);
1284
1285 if (unlikely(
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001286 !(rx_status & BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
Mitch Williamsa132af22015-01-24 09:58:35 +00001287 rx_ring->rx_stats.non_eop_descs++;
1288 continue;
1289 }
1290
1291 /* ERR_MASK will only have valid bits if EOP set */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001292 if (unlikely(rx_error & BIT(I40E_RX_DESC_ERROR_RXE_SHIFT))) {
Mitch Williamsa132af22015-01-24 09:58:35 +00001293 dev_kfree_skb_any(skb);
Mitch Williamsa132af22015-01-24 09:58:35 +00001294 continue;
Greg Rose7f12ad72013-12-21 06:12:51 +00001295 }
1296
Anjali Singhai Jain857942f2015-12-09 15:50:21 -08001297 i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
Greg Rose7f12ad72013-12-21 06:12:51 +00001298 /* probably a little skewed due to removing CRC */
1299 total_rx_bytes += skb->len;
1300 total_rx_packets++;
1301
1302 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1303
1304 i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
1305
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001306 vlan_tag = rx_status & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
Greg Rose7f12ad72013-12-21 06:12:51 +00001307 ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
1308 : 0;
1309 i40e_receive_skb(rx_ring, skb, vlan_tag);
1310
Greg Rose7f12ad72013-12-21 06:12:51 +00001311 rx_desc->wb.qword1.status_error_len = 0;
Mitch Williamsa132af22015-01-24 09:58:35 +00001312 } while (likely(total_rx_packets < budget));
Greg Rose7f12ad72013-12-21 06:12:51 +00001313
Greg Rose7f12ad72013-12-21 06:12:51 +00001314 u64_stats_update_begin(&rx_ring->syncp);
1315 rx_ring->stats.packets += total_rx_packets;
1316 rx_ring->stats.bytes += total_rx_bytes;
1317 u64_stats_update_end(&rx_ring->syncp);
1318 rx_ring->q_vector->rx.total_packets += total_rx_packets;
1319 rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
1320
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001321 return failure ? budget : total_rx_packets;
Greg Rose7f12ad72013-12-21 06:12:51 +00001322}
1323
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001324static u32 i40e_buildreg_itr(const int type, const u16 itr)
1325{
1326 u32 val;
1327
1328 val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08001329 /* Don't clear PBA because that can cause lost interrupts that
1330 * came in while we were cleaning/polling
1331 */
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001332 (type << I40E_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
1333 (itr << I40E_VFINT_DYN_CTLN1_INTERVAL_SHIFT);
1334
1335 return val;
1336}
1337
1338/* a small macro to shorten up some long lines */
1339#define INTREG I40E_VFINT_DYN_CTLN1
1340
Greg Rose7f12ad72013-12-21 06:12:51 +00001341/**
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001342 * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
1343 * @vsi: the VSI we care about
1344 * @q_vector: q_vector for which itr is being updated and interrupt enabled
1345 *
1346 **/
1347static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
1348 struct i40e_q_vector *q_vector)
1349{
1350 struct i40e_hw *hw = &vsi->back->hw;
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001351 bool rx = false, tx = false;
1352 u32 rxval, txval;
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001353 int vector;
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001354
1355 vector = (q_vector->v_idx + vsi->base_vector);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04001356
1357 /* avoid dynamic calculation if in countdown mode OR if
1358 * all dynamic is disabled
1359 */
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001360 rxval = txval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
1361
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04001362 if (q_vector->itr_countdown > 0 ||
1363 (!ITR_IS_DYNAMIC(vsi->rx_itr_setting) &&
1364 !ITR_IS_DYNAMIC(vsi->tx_itr_setting))) {
1365 goto enable_int;
1366 }
1367
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001368 if (ITR_IS_DYNAMIC(vsi->rx_itr_setting)) {
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001369 rx = i40e_set_new_dynamic_itr(&q_vector->rx);
1370 rxval = i40e_buildreg_itr(I40E_RX_ITR, q_vector->rx.itr);
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001371 }
Jesse Brandeburg4eeb1ff2015-11-18 17:35:42 -08001372
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001373 if (ITR_IS_DYNAMIC(vsi->tx_itr_setting)) {
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001374 tx = i40e_set_new_dynamic_itr(&q_vector->tx);
1375 txval = i40e_buildreg_itr(I40E_TX_ITR, q_vector->tx.itr);
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001376 }
Jesse Brandeburg4eeb1ff2015-11-18 17:35:42 -08001377
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001378 if (rx || tx) {
1379 /* get the higher of the two ITR adjustments and
1380 * use the same value for both ITR registers
1381 * when in adaptive mode (Rx and/or Tx)
1382 */
1383 u16 itr = max(q_vector->tx.itr, q_vector->rx.itr);
1384
1385 q_vector->tx.itr = q_vector->rx.itr = itr;
1386 txval = i40e_buildreg_itr(I40E_TX_ITR, itr);
1387 tx = true;
1388 rxval = i40e_buildreg_itr(I40E_RX_ITR, itr);
1389 rx = true;
1390 }
1391
1392 /* only need to enable the interrupt once, but need
1393 * to possibly update both ITR values
1394 */
1395 if (rx) {
1396 /* set the INTENA_MSK_MASK so that this first write
1397 * won't actually enable the interrupt, instead just
1398 * updating the ITR (it's bit 31 PF and VF)
1399 */
1400 rxval |= BIT(31);
1401 /* don't check _DOWN because interrupt isn't being enabled */
1402 wr32(hw, INTREG(vector - 1), rxval);
1403 }
1404
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04001405enable_int:
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001406 if (!test_bit(__I40E_DOWN, &vsi->state))
1407 wr32(hw, INTREG(vector - 1), txval);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04001408
1409 if (q_vector->itr_countdown)
1410 q_vector->itr_countdown--;
1411 else
1412 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001413}
1414
1415/**
Greg Rose7f12ad72013-12-21 06:12:51 +00001416 * i40evf_napi_poll - NAPI polling Rx/Tx cleanup routine
1417 * @napi: napi struct with our devices info in it
1418 * @budget: amount of work driver is allowed to do this pass, in packets
1419 *
1420 * This function will clean all queues associated with a q_vector.
1421 *
1422 * Returns the amount of work done
1423 **/
1424int i40evf_napi_poll(struct napi_struct *napi, int budget)
1425{
1426 struct i40e_q_vector *q_vector =
1427 container_of(napi, struct i40e_q_vector, napi);
1428 struct i40e_vsi *vsi = q_vector->vsi;
1429 struct i40e_ring *ring;
1430 bool clean_complete = true;
Anjali Singhai Jainc29af372015-01-10 01:07:19 +00001431 bool arm_wb = false;
Greg Rose7f12ad72013-12-21 06:12:51 +00001432 int budget_per_ring;
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07001433 int work_done = 0;
Greg Rose7f12ad72013-12-21 06:12:51 +00001434
1435 if (test_bit(__I40E_DOWN, &vsi->state)) {
1436 napi_complete(napi);
1437 return 0;
1438 }
1439
1440 /* Since the actual Tx work is minimal, we can give the Tx a larger
1441 * budget and be more aggressive about cleaning up the Tx descriptors.
1442 */
Anjali Singhai Jainc29af372015-01-10 01:07:19 +00001443 i40e_for_each_ring(ring, q_vector->tx) {
Mitch Williams1a36d7f2016-01-13 16:51:50 -08001444 clean_complete = clean_complete &&
1445 i40e_clean_tx_irq(ring, vsi->work_limit);
Mitch Williams44cdb792015-11-06 15:26:11 -08001446 arm_wb = arm_wb || ring->arm_wb;
Jesse Brandeburg0deda862015-07-23 16:54:34 -04001447 ring->arm_wb = false;
Anjali Singhai Jainc29af372015-01-10 01:07:19 +00001448 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001449
Alexander Duyckc67cace2015-09-24 09:04:26 -07001450 /* Handle case where we are called by netpoll with a budget of 0 */
1451 if (budget <= 0)
1452 goto tx_only;
1453
Greg Rose7f12ad72013-12-21 06:12:51 +00001454 /* We attempt to distribute budget to each Rx queue fairly, but don't
1455 * allow the budget to go below 1 because that would exit polling early.
1456 */
1457 budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
1458
Mitch Williamsa132af22015-01-24 09:58:35 +00001459 i40e_for_each_ring(ring, q_vector->rx) {
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07001460 int cleaned;
1461
Mitch Williamsa132af22015-01-24 09:58:35 +00001462 if (ring_is_ps_enabled(ring))
1463 cleaned = i40e_clean_rx_irq_ps(ring, budget_per_ring);
1464 else
1465 cleaned = i40e_clean_rx_irq_1buf(ring, budget_per_ring);
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07001466
1467 work_done += cleaned;
Mitch Williamsa132af22015-01-24 09:58:35 +00001468 /* if we didn't clean as many as budgeted, we must be done */
Mitch Williams1a36d7f2016-01-13 16:51:50 -08001469 clean_complete = clean_complete && (budget_per_ring > cleaned);
Mitch Williamsa132af22015-01-24 09:58:35 +00001470 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001471
1472 /* If work not completed, return budget and polling will return */
Anjali Singhai Jainc29af372015-01-10 01:07:19 +00001473 if (!clean_complete) {
Alexander Duyckc67cace2015-09-24 09:04:26 -07001474tx_only:
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -04001475 if (arm_wb) {
1476 q_vector->tx.ring[0].tx_stats.tx_force_wb++;
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -08001477 i40e_enable_wb_on_itr(vsi, q_vector);
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -04001478 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001479 return budget;
Anjali Singhai Jainc29af372015-01-10 01:07:19 +00001480 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001481
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04001482 if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
1483 q_vector->arm_wb_state = false;
1484
Greg Rose7f12ad72013-12-21 06:12:51 +00001485 /* Work is done so exit the polling mode and re-enable the interrupt */
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07001486 napi_complete_done(napi, work_done);
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001487 i40e_update_enable_itr(vsi, q_vector);
Greg Rose7f12ad72013-12-21 06:12:51 +00001488 return 0;
1489}
1490
1491/**
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04001492 * i40evf_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
Greg Rose7f12ad72013-12-21 06:12:51 +00001493 * @skb: send buffer
1494 * @tx_ring: ring to send buffer on
1495 * @flags: the tx flags to be set
1496 *
1497 * Checks the skb and set up correspondingly several generic transmit flags
1498 * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
1499 *
1500 * Returns error code indicate the frame should be dropped upon error and the
1501 * otherwise returns 0 to indicate the flags has been set properly.
1502 **/
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04001503static inline int i40evf_tx_prepare_vlan_flags(struct sk_buff *skb,
1504 struct i40e_ring *tx_ring,
1505 u32 *flags)
Greg Rose7f12ad72013-12-21 06:12:51 +00001506{
1507 __be16 protocol = skb->protocol;
1508 u32 tx_flags = 0;
1509
Greg Rose31eaacc2015-03-31 00:45:03 -07001510 if (protocol == htons(ETH_P_8021Q) &&
1511 !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
1512 /* When HW VLAN acceleration is turned off by the user the
1513 * stack sets the protocol to 8021q so that the driver
1514 * can take any steps required to support the SW only
1515 * VLAN handling. In our case the driver doesn't need
1516 * to take any further steps so just set the protocol
1517 * to the encapsulated ethertype.
1518 */
1519 skb->protocol = vlan_get_protocol(skb);
1520 goto out;
1521 }
1522
Greg Rose7f12ad72013-12-21 06:12:51 +00001523 /* if we have a HW VLAN tag being added, default to the HW one */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001524 if (skb_vlan_tag_present(skb)) {
1525 tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
Greg Rose7f12ad72013-12-21 06:12:51 +00001526 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1527 /* else if it is a SW VLAN, check the next protocol and store the tag */
1528 } else if (protocol == htons(ETH_P_8021Q)) {
1529 struct vlan_hdr *vhdr, _vhdr;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001530
Greg Rose7f12ad72013-12-21 06:12:51 +00001531 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
1532 if (!vhdr)
1533 return -EINVAL;
1534
1535 protocol = vhdr->h_vlan_encapsulated_proto;
1536 tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
1537 tx_flags |= I40E_TX_FLAGS_SW_VLAN;
1538 }
1539
Greg Rose31eaacc2015-03-31 00:45:03 -07001540out:
Greg Rose7f12ad72013-12-21 06:12:51 +00001541 *flags = tx_flags;
1542 return 0;
1543}
1544
1545/**
1546 * i40e_tso - set up the tso context descriptor
1547 * @tx_ring: ptr to the ring to send
1548 * @skb: ptr to the skb we're sending
Greg Rose7f12ad72013-12-21 06:12:51 +00001549 * @hdr_len: ptr to the size of the packet header
Shannon Nelson9c883bd2015-10-21 19:47:02 -04001550 * @cd_type_cmd_tso_mss: Quad Word 1
Greg Rose7f12ad72013-12-21 06:12:51 +00001551 *
1552 * Returns 0 if no TSO can happen, 1 if tso is going, or error
1553 **/
1554static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
Shannon Nelson9c883bd2015-10-21 19:47:02 -04001555 u8 *hdr_len, u64 *cd_type_cmd_tso_mss)
Greg Rose7f12ad72013-12-21 06:12:51 +00001556{
1557 u32 cd_cmd, cd_tso_len, cd_mss;
Francois Romieufe6d4aa2014-03-30 03:14:53 +00001558 struct ipv6hdr *ipv6h;
Greg Rose7f12ad72013-12-21 06:12:51 +00001559 struct tcphdr *tcph;
1560 struct iphdr *iph;
1561 u32 l4len;
1562 int err;
Greg Rose7f12ad72013-12-21 06:12:51 +00001563
Shannon Nelsone9f65632016-01-04 10:33:04 -08001564 if (skb->ip_summed != CHECKSUM_PARTIAL)
1565 return 0;
1566
Greg Rose7f12ad72013-12-21 06:12:51 +00001567 if (!skb_is_gso(skb))
1568 return 0;
1569
Francois Romieufe6d4aa2014-03-30 03:14:53 +00001570 err = skb_cow_head(skb, 0);
1571 if (err < 0)
1572 return err;
Greg Rose7f12ad72013-12-21 06:12:51 +00001573
Anjali Singhai85e76d02015-02-21 06:44:16 +00001574 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
1575 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
1576
1577 if (iph->version == 4) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001578 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1579 iph->tot_len = 0;
1580 iph->check = 0;
1581 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1582 0, IPPROTO_TCP, 0);
Anjali Singhai85e76d02015-02-21 06:44:16 +00001583 } else if (ipv6h->version == 6) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001584 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1585 ipv6h->payload_len = 0;
1586 tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
1587 0, IPPROTO_TCP, 0);
1588 }
1589
1590 l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
1591 *hdr_len = (skb->encapsulation
1592 ? (skb_inner_transport_header(skb) - skb->data)
1593 : skb_transport_offset(skb)) + l4len;
1594
1595 /* find the field values */
1596 cd_cmd = I40E_TX_CTX_DESC_TSO;
1597 cd_tso_len = skb->len - *hdr_len;
1598 cd_mss = skb_shinfo(skb)->gso_size;
1599 *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
1600 ((u64)cd_tso_len <<
1601 I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
1602 ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
1603 return 1;
1604}
1605
1606/**
1607 * i40e_tx_enable_csum - Enable Tx checksum offloads
1608 * @skb: send buffer
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001609 * @tx_flags: pointer to Tx flags currently set
Greg Rose7f12ad72013-12-21 06:12:51 +00001610 * @td_cmd: Tx descriptor command bits to set
1611 * @td_offset: Tx descriptor header offsets to set
1612 * @cd_tunneling: ptr to context desc bits
1613 **/
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001614static void i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
Greg Rose7f12ad72013-12-21 06:12:51 +00001615 u32 *td_cmd, u32 *td_offset,
1616 struct i40e_ring *tx_ring,
1617 u32 *cd_tunneling)
1618{
1619 struct ipv6hdr *this_ipv6_hdr;
1620 unsigned int this_tcp_hdrlen;
1621 struct iphdr *this_ip_hdr;
1622 u32 network_hdr_len;
1623 u8 l4_hdr = 0;
Anjali Singhai Jain527274c2015-06-05 12:20:31 -04001624 struct udphdr *oudph;
1625 struct iphdr *oiph;
Anjali Singhai Jain45991202015-02-27 09:15:29 +00001626 u32 l4_tunnel = 0;
Greg Rose7f12ad72013-12-21 06:12:51 +00001627
1628 if (skb->encapsulation) {
Anjali Singhai Jain45991202015-02-27 09:15:29 +00001629 switch (ip_hdr(skb)->protocol) {
1630 case IPPROTO_UDP:
Anjali Singhai Jain527274c2015-06-05 12:20:31 -04001631 oudph = udp_hdr(skb);
1632 oiph = ip_hdr(skb);
Anjali Singhai Jain45991202015-02-27 09:15:29 +00001633 l4_tunnel = I40E_TXD_CTX_UDP_TUNNELING;
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001634 *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
Anjali Singhai Jain45991202015-02-27 09:15:29 +00001635 break;
1636 default:
1637 return;
1638 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001639 network_hdr_len = skb_inner_network_header_len(skb);
1640 this_ip_hdr = inner_ip_hdr(skb);
1641 this_ipv6_hdr = inner_ipv6_hdr(skb);
1642 this_tcp_hdrlen = inner_tcp_hdrlen(skb);
1643
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001644 if (*tx_flags & I40E_TX_FLAGS_IPV4) {
1645 if (*tx_flags & I40E_TX_FLAGS_TSO) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001646 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
1647 ip_hdr(skb)->check = 0;
1648 } else {
1649 *cd_tunneling |=
1650 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1651 }
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001652 } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
Anjali Singhai85e76d02015-02-21 06:44:16 +00001653 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001654 if (*tx_flags & I40E_TX_FLAGS_TSO)
Greg Rose7f12ad72013-12-21 06:12:51 +00001655 ip_hdr(skb)->check = 0;
Greg Rose7f12ad72013-12-21 06:12:51 +00001656 }
1657
1658 /* Now set the ctx descriptor fields */
1659 *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
Anjali Singhai Jain45991202015-02-27 09:15:29 +00001660 I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
1661 l4_tunnel |
Greg Rose7f12ad72013-12-21 06:12:51 +00001662 ((skb_inner_network_offset(skb) -
1663 skb_transport_offset(skb)) >> 1) <<
1664 I40E_TXD_CTX_QW0_NATLEN_SHIFT;
Anjali Singhai85e76d02015-02-21 06:44:16 +00001665 if (this_ip_hdr->version == 6) {
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001666 *tx_flags &= ~I40E_TX_FLAGS_IPV4;
1667 *tx_flags |= I40E_TX_FLAGS_IPV6;
Anjali Singhai85e76d02015-02-21 06:44:16 +00001668 }
1669
Anjali Singhai Jain527274c2015-06-05 12:20:31 -04001670 if ((tx_ring->flags & I40E_TXR_FLAGS_OUTER_UDP_CSUM) &&
1671 (l4_tunnel == I40E_TXD_CTX_UDP_TUNNELING) &&
1672 (*cd_tunneling & I40E_TXD_CTX_QW0_EXT_IP_MASK)) {
1673 oudph->check = ~csum_tcpudp_magic(oiph->saddr,
1674 oiph->daddr,
1675 (skb->len - skb_transport_offset(skb)),
1676 IPPROTO_UDP, 0);
1677 *cd_tunneling |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
1678 }
Greg Rose7f12ad72013-12-21 06:12:51 +00001679 } else {
1680 network_hdr_len = skb_network_header_len(skb);
1681 this_ip_hdr = ip_hdr(skb);
1682 this_ipv6_hdr = ipv6_hdr(skb);
1683 this_tcp_hdrlen = tcp_hdrlen(skb);
1684 }
1685
1686 /* Enable IP checksum offloads */
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001687 if (*tx_flags & I40E_TX_FLAGS_IPV4) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001688 l4_hdr = this_ip_hdr->protocol;
1689 /* the stack computes the IP header already, the only time we
1690 * need the hardware to recompute it is in the case of TSO.
1691 */
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001692 if (*tx_flags & I40E_TX_FLAGS_TSO) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001693 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
1694 this_ip_hdr->check = 0;
1695 } else {
1696 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
1697 }
1698 /* Now set the td_offset for IP header length */
1699 *td_offset = (network_hdr_len >> 2) <<
1700 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04001701 } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
Greg Rose7f12ad72013-12-21 06:12:51 +00001702 l4_hdr = this_ipv6_hdr->nexthdr;
1703 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
1704 /* Now set the td_offset for IP header length */
1705 *td_offset = (network_hdr_len >> 2) <<
1706 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1707 }
1708 /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
1709 *td_offset |= (skb_network_offset(skb) >> 1) <<
1710 I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
1711
1712 /* Enable L4 checksum offloads */
1713 switch (l4_hdr) {
1714 case IPPROTO_TCP:
1715 /* enable checksum offloads */
1716 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
1717 *td_offset |= (this_tcp_hdrlen >> 2) <<
1718 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1719 break;
1720 case IPPROTO_SCTP:
1721 /* enable SCTP checksum offload */
1722 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
1723 *td_offset |= (sizeof(struct sctphdr) >> 2) <<
1724 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1725 break;
1726 case IPPROTO_UDP:
1727 /* enable UDP checksum offload */
1728 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
1729 *td_offset |= (sizeof(struct udphdr) >> 2) <<
1730 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1731 break;
1732 default:
1733 break;
1734 }
1735}
1736
1737/**
1738 * i40e_create_tx_ctx Build the Tx context descriptor
1739 * @tx_ring: ring to create the descriptor on
1740 * @cd_type_cmd_tso_mss: Quad Word 1
1741 * @cd_tunneling: Quad Word 0 - bits 0-31
1742 * @cd_l2tag2: Quad Word 0 - bits 32-63
1743 **/
1744static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
1745 const u64 cd_type_cmd_tso_mss,
1746 const u32 cd_tunneling, const u32 cd_l2tag2)
1747{
1748 struct i40e_tx_context_desc *context_desc;
1749 int i = tx_ring->next_to_use;
1750
Jesse Brandeburgff40dd52014-02-14 02:14:41 +00001751 if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
1752 !cd_tunneling && !cd_l2tag2)
Greg Rose7f12ad72013-12-21 06:12:51 +00001753 return;
1754
1755 /* grab the next descriptor */
1756 context_desc = I40E_TX_CTXTDESC(tx_ring, i);
1757
1758 i++;
1759 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
1760
1761 /* cpu_to_le32 and assign to struct fields */
1762 context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
1763 context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
Jesse Brandeburg3efbbb22014-06-04 20:41:54 +00001764 context_desc->rsvd = cpu_to_le16(0);
Greg Rose7f12ad72013-12-21 06:12:51 +00001765 context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
1766}
1767
Jesse Brandeburg4eeb1ff2015-11-18 17:35:42 -08001768/**
Anjali Singhai71da6192015-02-21 06:42:35 +00001769 * i40e_chk_linearize - Check if there are more than 8 fragments per packet
1770 * @skb: send buffer
1771 * @tx_flags: collected send information
Anjali Singhai71da6192015-02-21 06:42:35 +00001772 *
1773 * Note: Our HW can't scatter-gather more than 8 fragments to build
1774 * a packet on the wire and so we need to figure out the cases where we
1775 * need to linearize the skb.
1776 **/
Anjali Singhai Jain30520832015-05-08 15:35:52 -07001777static bool i40e_chk_linearize(struct sk_buff *skb, u32 tx_flags)
Anjali Singhai71da6192015-02-21 06:42:35 +00001778{
1779 struct skb_frag_struct *frag;
1780 bool linearize = false;
1781 unsigned int size = 0;
1782 u16 num_frags;
1783 u16 gso_segs;
1784
1785 num_frags = skb_shinfo(skb)->nr_frags;
1786 gso_segs = skb_shinfo(skb)->gso_segs;
1787
1788 if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO)) {
Anjali Singhai Jain30520832015-05-08 15:35:52 -07001789 u16 j = 0;
Anjali Singhai71da6192015-02-21 06:42:35 +00001790
1791 if (num_frags < (I40E_MAX_BUFFER_TXD))
1792 goto linearize_chk_done;
1793 /* try the simple math, if we have too many frags per segment */
1794 if (DIV_ROUND_UP((num_frags + gso_segs), gso_segs) >
1795 I40E_MAX_BUFFER_TXD) {
1796 linearize = true;
1797 goto linearize_chk_done;
1798 }
1799 frag = &skb_shinfo(skb)->frags[0];
Anjali Singhai71da6192015-02-21 06:42:35 +00001800 /* we might still have more fragments per segment */
1801 do {
1802 size += skb_frag_size(frag);
1803 frag++; j++;
Anjali Singhai Jain30520832015-05-08 15:35:52 -07001804 if ((size >= skb_shinfo(skb)->gso_size) &&
1805 (j < I40E_MAX_BUFFER_TXD)) {
1806 size = (size % skb_shinfo(skb)->gso_size);
1807 j = (size) ? 1 : 0;
1808 }
Anjali Singhai71da6192015-02-21 06:42:35 +00001809 if (j == I40E_MAX_BUFFER_TXD) {
Anjali Singhai Jain30520832015-05-08 15:35:52 -07001810 linearize = true;
1811 break;
Anjali Singhai71da6192015-02-21 06:42:35 +00001812 }
1813 num_frags--;
1814 } while (num_frags);
1815 } else {
1816 if (num_frags >= I40E_MAX_BUFFER_TXD)
1817 linearize = true;
1818 }
1819
1820linearize_chk_done:
1821 return linearize;
1822}
1823
Greg Rose7f12ad72013-12-21 06:12:51 +00001824/**
Jesse Brandeburg8f6a2b02015-04-16 20:06:09 -04001825 * __i40evf_maybe_stop_tx - 2nd level check for tx stop conditions
1826 * @tx_ring: the ring to be checked
1827 * @size: the size buffer we want to assure is available
1828 *
1829 * Returns -EBUSY if a stop is needed, else 0
1830 **/
1831static inline int __i40evf_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
1832{
1833 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
1834 /* Memory barrier before checking head and tail */
1835 smp_mb();
1836
1837 /* Check again in a case another CPU has just made room available. */
1838 if (likely(I40E_DESC_UNUSED(tx_ring) < size))
1839 return -EBUSY;
1840
1841 /* A reprieve! - use start_queue because it doesn't call schedule */
1842 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
1843 ++tx_ring->tx_stats.restart_queue;
1844 return 0;
1845}
1846
1847/**
1848 * i40evf_maybe_stop_tx - 1st level check for tx stop conditions
1849 * @tx_ring: the ring to be checked
1850 * @size: the size buffer we want to assure is available
1851 *
1852 * Returns 0 if stop is not needed
1853 **/
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04001854static inline int i40evf_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
Jesse Brandeburg8f6a2b02015-04-16 20:06:09 -04001855{
1856 if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
1857 return 0;
1858 return __i40evf_maybe_stop_tx(tx_ring, size);
1859}
1860
1861/**
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04001862 * i40evf_tx_map - Build the Tx descriptor
Greg Rose7f12ad72013-12-21 06:12:51 +00001863 * @tx_ring: ring to send buffer on
1864 * @skb: send buffer
1865 * @first: first buffer info buffer to use
1866 * @tx_flags: collected send information
1867 * @hdr_len: size of the packet header
1868 * @td_cmd: the command field in the descriptor
1869 * @td_offset: offset for checksum or crc
1870 **/
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04001871static inline void i40evf_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
1872 struct i40e_tx_buffer *first, u32 tx_flags,
1873 const u8 hdr_len, u32 td_cmd, u32 td_offset)
Greg Rose7f12ad72013-12-21 06:12:51 +00001874{
1875 unsigned int data_len = skb->data_len;
1876 unsigned int size = skb_headlen(skb);
1877 struct skb_frag_struct *frag;
1878 struct i40e_tx_buffer *tx_bi;
1879 struct i40e_tx_desc *tx_desc;
1880 u16 i = tx_ring->next_to_use;
1881 u32 td_tag = 0;
1882 dma_addr_t dma;
1883 u16 gso_segs;
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001884 u16 desc_count = 0;
1885 bool tail_bump = true;
1886 bool do_rs = false;
Greg Rose7f12ad72013-12-21 06:12:51 +00001887
1888 if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
1889 td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
1890 td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
1891 I40E_TX_FLAGS_VLAN_SHIFT;
1892 }
1893
1894 if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
1895 gso_segs = skb_shinfo(skb)->gso_segs;
1896 else
1897 gso_segs = 1;
1898
1899 /* multiply data chunks by size of headers */
1900 first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
1901 first->gso_segs = gso_segs;
1902 first->skb = skb;
1903 first->tx_flags = tx_flags;
1904
1905 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1906
1907 tx_desc = I40E_TX_DESC(tx_ring, i);
1908 tx_bi = first;
1909
1910 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1911 if (dma_mapping_error(tx_ring->dev, dma))
1912 goto dma_error;
1913
1914 /* record length, and DMA address */
1915 dma_unmap_len_set(tx_bi, len, size);
1916 dma_unmap_addr_set(tx_bi, dma, dma);
1917
1918 tx_desc->buffer_addr = cpu_to_le64(dma);
1919
1920 while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
1921 tx_desc->cmd_type_offset_bsz =
1922 build_ctob(td_cmd, td_offset,
1923 I40E_MAX_DATA_PER_TXD, td_tag);
1924
1925 tx_desc++;
1926 i++;
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001927 desc_count++;
1928
Greg Rose7f12ad72013-12-21 06:12:51 +00001929 if (i == tx_ring->count) {
1930 tx_desc = I40E_TX_DESC(tx_ring, 0);
1931 i = 0;
1932 }
1933
1934 dma += I40E_MAX_DATA_PER_TXD;
1935 size -= I40E_MAX_DATA_PER_TXD;
1936
1937 tx_desc->buffer_addr = cpu_to_le64(dma);
1938 }
1939
1940 if (likely(!data_len))
1941 break;
1942
1943 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
1944 size, td_tag);
1945
1946 tx_desc++;
1947 i++;
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001948 desc_count++;
1949
Greg Rose7f12ad72013-12-21 06:12:51 +00001950 if (i == tx_ring->count) {
1951 tx_desc = I40E_TX_DESC(tx_ring, 0);
1952 i = 0;
1953 }
1954
1955 size = skb_frag_size(frag);
1956 data_len -= size;
1957
1958 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
1959 DMA_TO_DEVICE);
1960
1961 tx_bi = &tx_ring->tx_bi[i];
1962 }
1963
Greg Rose7f12ad72013-12-21 06:12:51 +00001964 /* set next_to_watch value indicating a packet is present */
1965 first->next_to_watch = tx_desc;
1966
1967 i++;
1968 if (i == tx_ring->count)
1969 i = 0;
1970
1971 tx_ring->next_to_use = i;
1972
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001973 netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
1974 tx_ring->queue_index),
1975 first->bytecount);
Jesse Brandeburg8f6a2b02015-04-16 20:06:09 -04001976 i40evf_maybe_stop_tx(tx_ring, DESC_NEEDED);
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001977
1978 /* Algorithm to optimize tail and RS bit setting:
1979 * if xmit_more is supported
1980 * if xmit_more is true
1981 * do not update tail and do not mark RS bit.
1982 * if xmit_more is false and last xmit_more was false
1983 * if every packet spanned less than 4 desc
1984 * then set RS bit on 4th packet and update tail
1985 * on every packet
1986 * else
1987 * update tail and set RS bit on every packet.
1988 * if xmit_more is false and last_xmit_more was true
1989 * update tail and set RS bit.
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04001990 *
1991 * Optimization: wmb to be issued only in case of tail update.
1992 * Also optimize the Descriptor WB path for RS bit with the same
1993 * algorithm.
1994 *
1995 * Note: If there are less than 4 packets
1996 * pending and interrupts were disabled the service task will
1997 * trigger a force WB.
1998 */
1999 if (skb->xmit_more &&
2000 !netif_xmit_stopped(netdev_get_tx_queue(tx_ring->netdev,
2001 tx_ring->queue_index))) {
2002 tx_ring->flags |= I40E_TXR_FLAGS_LAST_XMIT_MORE_SET;
2003 tail_bump = false;
2004 } else if (!skb->xmit_more &&
2005 !netif_xmit_stopped(netdev_get_tx_queue(tx_ring->netdev,
2006 tx_ring->queue_index)) &&
2007 (!(tx_ring->flags & I40E_TXR_FLAGS_LAST_XMIT_MORE_SET)) &&
2008 (tx_ring->packet_stride < WB_STRIDE) &&
2009 (desc_count < WB_STRIDE)) {
2010 tx_ring->packet_stride++;
2011 } else {
2012 tx_ring->packet_stride = 0;
2013 tx_ring->flags &= ~I40E_TXR_FLAGS_LAST_XMIT_MORE_SET;
2014 do_rs = true;
2015 }
2016 if (do_rs)
2017 tx_ring->packet_stride = 0;
2018
2019 tx_desc->cmd_type_offset_bsz =
2020 build_ctob(td_cmd, td_offset, size, td_tag) |
2021 cpu_to_le64((u64)(do_rs ? I40E_TXD_CMD :
2022 I40E_TX_DESC_CMD_EOP) <<
2023 I40E_TXD_QW1_CMD_SHIFT);
2024
Greg Rose7f12ad72013-12-21 06:12:51 +00002025 /* notify HW of packet */
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04002026 if (!tail_bump)
Jesse Brandeburg489ce7a2015-04-27 14:57:08 -04002027 prefetchw(tx_desc + 1);
Greg Rose7f12ad72013-12-21 06:12:51 +00002028
Anjali Singhai Jain6a7fded2015-10-26 19:44:29 -04002029 if (tail_bump) {
2030 /* Force memory writes to complete before letting h/w
2031 * know there are new descriptors to fetch. (Only
2032 * applicable for weak-ordered memory model archs,
2033 * such as IA-64).
2034 */
2035 wmb();
2036 writel(i, tx_ring->tail);
2037 }
2038
Greg Rose7f12ad72013-12-21 06:12:51 +00002039 return;
2040
2041dma_error:
2042 dev_info(tx_ring->dev, "TX DMA map failed\n");
2043
2044 /* clear dma mappings for failed tx_bi map */
2045 for (;;) {
2046 tx_bi = &tx_ring->tx_bi[i];
2047 i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
2048 if (tx_bi == first)
2049 break;
2050 if (i == 0)
2051 i = tx_ring->count;
2052 i--;
2053 }
2054
2055 tx_ring->next_to_use = i;
2056}
2057
2058/**
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002059 * i40evf_xmit_descriptor_count - calculate number of tx descriptors needed
Greg Rose7f12ad72013-12-21 06:12:51 +00002060 * @skb: send buffer
2061 * @tx_ring: ring to send buffer on
2062 *
2063 * Returns number of data descriptors needed for this skb. Returns 0 to indicate
2064 * there is not enough descriptors available in this ring since we need at least
2065 * one descriptor.
2066 **/
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002067static inline int i40evf_xmit_descriptor_count(struct sk_buff *skb,
2068 struct i40e_ring *tx_ring)
Greg Rose7f12ad72013-12-21 06:12:51 +00002069{
Greg Rose7f12ad72013-12-21 06:12:51 +00002070 unsigned int f;
Greg Rose7f12ad72013-12-21 06:12:51 +00002071 int count = 0;
2072
2073 /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
2074 * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
Jesse Brandeburgbe560522014-02-06 05:51:13 +00002075 * + 4 desc gap to avoid the cache line where head is,
Greg Rose7f12ad72013-12-21 06:12:51 +00002076 * + 1 desc for context descriptor,
2077 * otherwise try next time
2078 */
Greg Rose7f12ad72013-12-21 06:12:51 +00002079 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
2080 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
Jesse Brandeburg980093e2014-05-10 04:49:12 +00002081
Greg Rose7f12ad72013-12-21 06:12:51 +00002082 count += TXD_USE_COUNT(skb_headlen(skb));
Jesse Brandeburg8f6a2b02015-04-16 20:06:09 -04002083 if (i40evf_maybe_stop_tx(tx_ring, count + 4 + 1)) {
Greg Rose7f12ad72013-12-21 06:12:51 +00002084 tx_ring->tx_stats.tx_busy++;
2085 return 0;
2086 }
2087 return count;
2088}
2089
2090/**
2091 * i40e_xmit_frame_ring - Sends buffer on Tx ring
2092 * @skb: send buffer
2093 * @tx_ring: ring to send buffer on
2094 *
2095 * Returns NETDEV_TX_OK if sent, else an error code
2096 **/
2097static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
2098 struct i40e_ring *tx_ring)
2099{
2100 u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
2101 u32 cd_tunneling = 0, cd_l2tag2 = 0;
2102 struct i40e_tx_buffer *first;
2103 u32 td_offset = 0;
2104 u32 tx_flags = 0;
2105 __be16 protocol;
2106 u32 td_cmd = 0;
2107 u8 hdr_len = 0;
2108 int tso;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002109
Jesse Brandeburgb74118f2015-10-26 19:44:30 -04002110 /* prefetch the data, we'll need it later */
2111 prefetch(skb->data);
2112
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002113 if (0 == i40evf_xmit_descriptor_count(skb, tx_ring))
Greg Rose7f12ad72013-12-21 06:12:51 +00002114 return NETDEV_TX_BUSY;
2115
2116 /* prepare the xmit flags */
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002117 if (i40evf_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
Greg Rose7f12ad72013-12-21 06:12:51 +00002118 goto out_drop;
2119
2120 /* obtain protocol of skb */
Vlad Yasevicha12c4152014-08-25 10:34:53 -04002121 protocol = vlan_get_protocol(skb);
Greg Rose7f12ad72013-12-21 06:12:51 +00002122
2123 /* record the location of the first descriptor for this packet */
2124 first = &tx_ring->tx_bi[tx_ring->next_to_use];
2125
2126 /* setup IPv4/IPv6 offloads */
2127 if (protocol == htons(ETH_P_IP))
2128 tx_flags |= I40E_TX_FLAGS_IPV4;
2129 else if (protocol == htons(ETH_P_IPV6))
2130 tx_flags |= I40E_TX_FLAGS_IPV6;
2131
Shannon Nelson9c883bd2015-10-21 19:47:02 -04002132 tso = i40e_tso(tx_ring, skb, &hdr_len, &cd_type_cmd_tso_mss);
Greg Rose7f12ad72013-12-21 06:12:51 +00002133
2134 if (tso < 0)
2135 goto out_drop;
2136 else if (tso)
2137 tx_flags |= I40E_TX_FLAGS_TSO;
2138
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -04002139 if (i40e_chk_linearize(skb, tx_flags)) {
Anjali Singhai71da6192015-02-21 06:42:35 +00002140 if (skb_linearize(skb))
2141 goto out_drop;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -04002142 tx_ring->tx_stats.tx_linearize++;
2143 }
Greg Rose7f12ad72013-12-21 06:12:51 +00002144 skb_tx_timestamp(skb);
2145
2146 /* always enable CRC insertion offload */
2147 td_cmd |= I40E_TX_DESC_CMD_ICRC;
2148
2149 /* Always offload the checksum, since it's in the data descriptor */
2150 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2151 tx_flags |= I40E_TX_FLAGS_CSUM;
2152
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002153 i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
Greg Rose7f12ad72013-12-21 06:12:51 +00002154 tx_ring, &cd_tunneling);
2155 }
2156
2157 i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
2158 cd_tunneling, cd_l2tag2);
2159
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002160 i40evf_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
2161 td_cmd, td_offset);
Greg Rose7f12ad72013-12-21 06:12:51 +00002162
Greg Rose7f12ad72013-12-21 06:12:51 +00002163 return NETDEV_TX_OK;
2164
2165out_drop:
2166 dev_kfree_skb_any(skb);
2167 return NETDEV_TX_OK;
2168}
2169
2170/**
2171 * i40evf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
2172 * @skb: send buffer
2173 * @netdev: network interface device structure
2174 *
2175 * Returns NETDEV_TX_OK if sent, else an error code
2176 **/
2177netdev_tx_t i40evf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2178{
2179 struct i40evf_adapter *adapter = netdev_priv(netdev);
Mitch Williams0dd438d2015-10-26 19:44:40 -04002180 struct i40e_ring *tx_ring = &adapter->tx_rings[skb->queue_mapping];
Greg Rose7f12ad72013-12-21 06:12:51 +00002181
2182 /* hardware can't handle really short frames, hardware padding works
2183 * beyond this point
2184 */
2185 if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
2186 if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
2187 return NETDEV_TX_OK;
2188 skb->len = I40E_MIN_TX_LEN;
2189 skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
2190 }
2191
2192 return i40e_xmit_frame_ring(skb, tx_ring);
2193}