blob: 01bfab729b0134b5193515a2de24f7edb8a3010a [file] [log] [blame]
Greg Rose7f12ad72013-12-21 06:12:51 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
Jesse Brandeburgaf1a2a92014-02-13 03:48:41 -08004 * Copyright(c) 2013 - 2014 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 *
15 * The full GNU General Public License is included in this distribution in
16 * the file called "COPYING".
17 *
18 * Contact Information:
19 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
20 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
21 *
22 ******************************************************************************/
23
Paul Gortmaker7ed3f5f2014-01-11 04:00:31 +000024#include <linux/prefetch.h>
25
Greg Rose7f12ad72013-12-21 06:12:51 +000026#include "i40evf.h"
Jesse Brandeburg206812b2014-02-12 01:45:33 +000027#include "i40e_prototype.h"
Greg Rose7f12ad72013-12-21 06:12:51 +000028
29static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
30 u32 td_tag)
31{
32 return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
33 ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
34 ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
35 ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
36 ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
37}
38
39#define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
40
41/**
42 * i40e_unmap_and_free_tx_resource - Release a Tx buffer
43 * @ring: the ring that owns the buffer
44 * @tx_buffer: the buffer to free
45 **/
46static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
47 struct i40e_tx_buffer *tx_buffer)
48{
49 if (tx_buffer->skb) {
50 dev_kfree_skb_any(tx_buffer->skb);
51 if (dma_unmap_len(tx_buffer, len))
52 dma_unmap_single(ring->dev,
53 dma_unmap_addr(tx_buffer, dma),
54 dma_unmap_len(tx_buffer, len),
55 DMA_TO_DEVICE);
56 } else if (dma_unmap_len(tx_buffer, len)) {
57 dma_unmap_page(ring->dev,
58 dma_unmap_addr(tx_buffer, dma),
59 dma_unmap_len(tx_buffer, len),
60 DMA_TO_DEVICE);
61 }
62 tx_buffer->next_to_watch = NULL;
63 tx_buffer->skb = NULL;
64 dma_unmap_len_set(tx_buffer, len, 0);
65 /* tx_buffer must be completely set up in the transmit path */
66}
67
68/**
69 * i40evf_clean_tx_ring - Free any empty Tx buffers
70 * @tx_ring: ring to be cleaned
71 **/
72void i40evf_clean_tx_ring(struct i40e_ring *tx_ring)
73{
74 unsigned long bi_size;
75 u16 i;
76
77 /* ring already cleared, nothing to do */
78 if (!tx_ring->tx_bi)
79 return;
80
81 /* Free all the Tx ring sk_buffs */
82 for (i = 0; i < tx_ring->count; i++)
83 i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
84
85 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
86 memset(tx_ring->tx_bi, 0, bi_size);
87
88 /* Zero out the descriptor ring */
89 memset(tx_ring->desc, 0, tx_ring->size);
90
91 tx_ring->next_to_use = 0;
92 tx_ring->next_to_clean = 0;
93
94 if (!tx_ring->netdev)
95 return;
96
97 /* cleanup Tx queue statistics */
98 netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
99 tx_ring->queue_index));
100}
101
102/**
103 * i40evf_free_tx_resources - Free Tx resources per queue
104 * @tx_ring: Tx descriptor ring for a specific queue
105 *
106 * Free all transmit software resources
107 **/
108void i40evf_free_tx_resources(struct i40e_ring *tx_ring)
109{
110 i40evf_clean_tx_ring(tx_ring);
111 kfree(tx_ring->tx_bi);
112 tx_ring->tx_bi = NULL;
113
114 if (tx_ring->desc) {
115 dma_free_coherent(tx_ring->dev, tx_ring->size,
116 tx_ring->desc, tx_ring->dma);
117 tx_ring->desc = NULL;
118 }
119}
120
121/**
122 * i40e_get_tx_pending - how many tx descriptors not processed
123 * @tx_ring: the ring of descriptors
124 *
125 * Since there is no access to the ring head register
126 * in XL710, we need to use our local copies
127 **/
128static u32 i40e_get_tx_pending(struct i40e_ring *ring)
129{
130 u32 ntu = ((ring->next_to_clean <= ring->next_to_use)
131 ? ring->next_to_use
132 : ring->next_to_use + ring->count);
133 return ntu - ring->next_to_clean;
134}
135
136/**
137 * i40e_check_tx_hang - Is there a hang in the Tx queue
138 * @tx_ring: the ring of descriptors
139 **/
140static bool i40e_check_tx_hang(struct i40e_ring *tx_ring)
141{
142 u32 tx_pending = i40e_get_tx_pending(tx_ring);
143 bool ret = false;
144
145 clear_check_for_tx_hang(tx_ring);
146
147 /* Check for a hung queue, but be thorough. This verifies
148 * that a transmit has been completed since the previous
149 * check AND there is at least one packet pending. The
150 * ARMED bit is set to indicate a potential hang. The
151 * bit is cleared if a pause frame is received to remove
152 * false hang detection due to PFC or 802.3x frames. By
153 * requiring this to fail twice we avoid races with
154 * PFC clearing the ARMED bit and conditions where we
155 * run the check_tx_hang logic with a transmit completion
156 * pending but without time to complete it yet.
157 */
158 if ((tx_ring->tx_stats.tx_done_old == tx_ring->stats.packets) &&
159 tx_pending) {
160 /* make sure it is true for two checks in a row */
161 ret = test_and_set_bit(__I40E_HANG_CHECK_ARMED,
162 &tx_ring->state);
163 } else {
164 /* update completed stats and disarm the hang check */
165 tx_ring->tx_stats.tx_done_old = tx_ring->stats.packets;
166 clear_bit(__I40E_HANG_CHECK_ARMED, &tx_ring->state);
167 }
168
169 return ret;
170}
171
172/**
173 * i40e_clean_tx_irq - Reclaim resources after transmit completes
174 * @tx_ring: tx ring to clean
175 * @budget: how many cleans we're allowed
176 *
177 * Returns true if there's any budget left (e.g. the clean is finished)
178 **/
179static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
180{
181 u16 i = tx_ring->next_to_clean;
182 struct i40e_tx_buffer *tx_buf;
183 struct i40e_tx_desc *tx_desc;
184 unsigned int total_packets = 0;
185 unsigned int total_bytes = 0;
186
187 tx_buf = &tx_ring->tx_bi[i];
188 tx_desc = I40E_TX_DESC(tx_ring, i);
189 i -= tx_ring->count;
190
191 do {
192 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
193
194 /* if next_to_watch is not set then there is no work pending */
195 if (!eop_desc)
196 break;
197
198 /* prevent any other reads prior to eop_desc */
199 read_barrier_depends();
200
201 /* if the descriptor isn't done, no work yet to do */
202 if (!(eop_desc->cmd_type_offset_bsz &
203 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
204 break;
205
206 /* clear next_to_watch to prevent false hangs */
207 tx_buf->next_to_watch = NULL;
208
209 /* update the statistics for this packet */
210 total_bytes += tx_buf->bytecount;
211 total_packets += tx_buf->gso_segs;
212
213 /* free the skb */
214 dev_kfree_skb_any(tx_buf->skb);
215
216 /* unmap skb header data */
217 dma_unmap_single(tx_ring->dev,
218 dma_unmap_addr(tx_buf, dma),
219 dma_unmap_len(tx_buf, len),
220 DMA_TO_DEVICE);
221
222 /* clear tx_buffer data */
223 tx_buf->skb = NULL;
224 dma_unmap_len_set(tx_buf, len, 0);
225
226 /* unmap remaining buffers */
227 while (tx_desc != eop_desc) {
228
229 tx_buf++;
230 tx_desc++;
231 i++;
232 if (unlikely(!i)) {
233 i -= tx_ring->count;
234 tx_buf = tx_ring->tx_bi;
235 tx_desc = I40E_TX_DESC(tx_ring, 0);
236 }
237
238 /* unmap any remaining paged data */
239 if (dma_unmap_len(tx_buf, len)) {
240 dma_unmap_page(tx_ring->dev,
241 dma_unmap_addr(tx_buf, dma),
242 dma_unmap_len(tx_buf, len),
243 DMA_TO_DEVICE);
244 dma_unmap_len_set(tx_buf, len, 0);
245 }
246 }
247
248 /* move us one more past the eop_desc for start of next pkt */
249 tx_buf++;
250 tx_desc++;
251 i++;
252 if (unlikely(!i)) {
253 i -= tx_ring->count;
254 tx_buf = tx_ring->tx_bi;
255 tx_desc = I40E_TX_DESC(tx_ring, 0);
256 }
257
258 /* update budget accounting */
259 budget--;
260 } while (likely(budget));
261
262 i += tx_ring->count;
263 tx_ring->next_to_clean = i;
264 u64_stats_update_begin(&tx_ring->syncp);
265 tx_ring->stats.bytes += total_bytes;
266 tx_ring->stats.packets += total_packets;
267 u64_stats_update_end(&tx_ring->syncp);
268 tx_ring->q_vector->tx.total_bytes += total_bytes;
269 tx_ring->q_vector->tx.total_packets += total_packets;
270
271 if (check_for_tx_hang(tx_ring) && i40e_check_tx_hang(tx_ring)) {
272 /* schedule immediate reset if we believe we hung */
273 dev_info(tx_ring->dev, "Detected Tx Unit Hang\n"
274 " VSI <%d>\n"
275 " Tx Queue <%d>\n"
276 " next_to_use <%x>\n"
277 " next_to_clean <%x>\n",
278 tx_ring->vsi->seid,
279 tx_ring->queue_index,
280 tx_ring->next_to_use, i);
281 dev_info(tx_ring->dev, "tx_bi[next_to_clean]\n"
282 " time_stamp <%lx>\n"
283 " jiffies <%lx>\n",
284 tx_ring->tx_bi[i].time_stamp, jiffies);
285
286 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
287
288 dev_info(tx_ring->dev,
289 "tx hang detected on queue %d, resetting adapter\n",
290 tx_ring->queue_index);
291
292 tx_ring->netdev->netdev_ops->ndo_tx_timeout(tx_ring->netdev);
293
294 /* the adapter is about to reset, no point in enabling stuff */
295 return true;
296 }
297
298 netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
299 tx_ring->queue_index),
300 total_packets, total_bytes);
301
302#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
303 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
304 (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
305 /* Make sure that anybody stopping the queue after this
306 * sees the new next_to_clean.
307 */
308 smp_mb();
309 if (__netif_subqueue_stopped(tx_ring->netdev,
310 tx_ring->queue_index) &&
311 !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
312 netif_wake_subqueue(tx_ring->netdev,
313 tx_ring->queue_index);
314 ++tx_ring->tx_stats.restart_queue;
315 }
316 }
317
318 return budget > 0;
319}
320
321/**
322 * i40e_set_new_dynamic_itr - Find new ITR level
323 * @rc: structure containing ring performance data
324 *
325 * Stores a new ITR value based on packets and byte counts during
326 * the last interrupt. The advantage of per interrupt computation
327 * is faster updates and more accurate ITR for the current traffic
328 * pattern. Constants in this function were computed based on
329 * theoretical maximum wire speed and thresholds were set based on
330 * testing data as well as attempting to minimize response time
331 * while increasing bulk throughput.
332 **/
333static void i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
334{
335 enum i40e_latency_range new_latency_range = rc->latency_range;
336 u32 new_itr = rc->itr;
337 int bytes_per_int;
338
339 if (rc->total_packets == 0 || !rc->itr)
340 return;
341
342 /* simple throttlerate management
343 * 0-10MB/s lowest (100000 ints/s)
344 * 10-20MB/s low (20000 ints/s)
345 * 20-1249MB/s bulk (8000 ints/s)
346 */
347 bytes_per_int = rc->total_bytes / rc->itr;
348 switch (rc->itr) {
349 case I40E_LOWEST_LATENCY:
350 if (bytes_per_int > 10)
351 new_latency_range = I40E_LOW_LATENCY;
352 break;
353 case I40E_LOW_LATENCY:
354 if (bytes_per_int > 20)
355 new_latency_range = I40E_BULK_LATENCY;
356 else if (bytes_per_int <= 10)
357 new_latency_range = I40E_LOWEST_LATENCY;
358 break;
359 case I40E_BULK_LATENCY:
360 if (bytes_per_int <= 20)
361 rc->latency_range = I40E_LOW_LATENCY;
362 break;
363 }
364
365 switch (new_latency_range) {
366 case I40E_LOWEST_LATENCY:
367 new_itr = I40E_ITR_100K;
368 break;
369 case I40E_LOW_LATENCY:
370 new_itr = I40E_ITR_20K;
371 break;
372 case I40E_BULK_LATENCY:
373 new_itr = I40E_ITR_8K;
374 break;
375 default:
376 break;
377 }
378
379 if (new_itr != rc->itr) {
380 /* do an exponential smoothing */
381 new_itr = (10 * new_itr * rc->itr) /
382 ((9 * new_itr) + rc->itr);
383 rc->itr = new_itr & I40E_MAX_ITR;
384 }
385
386 rc->total_bytes = 0;
387 rc->total_packets = 0;
388}
389
390/**
391 * i40e_update_dynamic_itr - Adjust ITR based on bytes per int
392 * @q_vector: the vector to adjust
393 **/
394static void i40e_update_dynamic_itr(struct i40e_q_vector *q_vector)
395{
396 u16 vector = q_vector->vsi->base_vector + q_vector->v_idx;
397 struct i40e_hw *hw = &q_vector->vsi->back->hw;
398 u32 reg_addr;
399 u16 old_itr;
400
401 reg_addr = I40E_VFINT_ITRN1(I40E_RX_ITR, vector - 1);
402 old_itr = q_vector->rx.itr;
403 i40e_set_new_dynamic_itr(&q_vector->rx);
404 if (old_itr != q_vector->rx.itr)
405 wr32(hw, reg_addr, q_vector->rx.itr);
406
407 reg_addr = I40E_VFINT_ITRN1(I40E_TX_ITR, vector - 1);
408 old_itr = q_vector->tx.itr;
409 i40e_set_new_dynamic_itr(&q_vector->tx);
410 if (old_itr != q_vector->tx.itr)
411 wr32(hw, reg_addr, q_vector->tx.itr);
412}
413
414/**
415 * i40evf_setup_tx_descriptors - Allocate the Tx descriptors
416 * @tx_ring: the tx ring to set up
417 *
418 * Return 0 on success, negative on error
419 **/
420int i40evf_setup_tx_descriptors(struct i40e_ring *tx_ring)
421{
422 struct device *dev = tx_ring->dev;
423 int bi_size;
424
425 if (!dev)
426 return -ENOMEM;
427
428 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
429 tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
430 if (!tx_ring->tx_bi)
431 goto err;
432
433 /* round up to nearest 4K */
434 tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
435 tx_ring->size = ALIGN(tx_ring->size, 4096);
436 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
437 &tx_ring->dma, GFP_KERNEL);
438 if (!tx_ring->desc) {
439 dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
440 tx_ring->size);
441 goto err;
442 }
443
444 tx_ring->next_to_use = 0;
445 tx_ring->next_to_clean = 0;
446 return 0;
447
448err:
449 kfree(tx_ring->tx_bi);
450 tx_ring->tx_bi = NULL;
451 return -ENOMEM;
452}
453
454/**
455 * i40evf_clean_rx_ring - Free Rx buffers
456 * @rx_ring: ring to be cleaned
457 **/
458void i40evf_clean_rx_ring(struct i40e_ring *rx_ring)
459{
460 struct device *dev = rx_ring->dev;
461 struct i40e_rx_buffer *rx_bi;
462 unsigned long bi_size;
463 u16 i;
464
465 /* ring already cleared, nothing to do */
466 if (!rx_ring->rx_bi)
467 return;
468
469 /* Free all the Rx ring sk_buffs */
470 for (i = 0; i < rx_ring->count; i++) {
471 rx_bi = &rx_ring->rx_bi[i];
472 if (rx_bi->dma) {
473 dma_unmap_single(dev,
474 rx_bi->dma,
475 rx_ring->rx_buf_len,
476 DMA_FROM_DEVICE);
477 rx_bi->dma = 0;
478 }
479 if (rx_bi->skb) {
480 dev_kfree_skb(rx_bi->skb);
481 rx_bi->skb = NULL;
482 }
483 if (rx_bi->page) {
484 if (rx_bi->page_dma) {
485 dma_unmap_page(dev,
486 rx_bi->page_dma,
487 PAGE_SIZE / 2,
488 DMA_FROM_DEVICE);
489 rx_bi->page_dma = 0;
490 }
491 __free_page(rx_bi->page);
492 rx_bi->page = NULL;
493 rx_bi->page_offset = 0;
494 }
495 }
496
497 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
498 memset(rx_ring->rx_bi, 0, bi_size);
499
500 /* Zero out the descriptor ring */
501 memset(rx_ring->desc, 0, rx_ring->size);
502
503 rx_ring->next_to_clean = 0;
504 rx_ring->next_to_use = 0;
505}
506
507/**
508 * i40evf_free_rx_resources - Free Rx resources
509 * @rx_ring: ring to clean the resources from
510 *
511 * Free all receive software resources
512 **/
513void i40evf_free_rx_resources(struct i40e_ring *rx_ring)
514{
515 i40evf_clean_rx_ring(rx_ring);
516 kfree(rx_ring->rx_bi);
517 rx_ring->rx_bi = NULL;
518
519 if (rx_ring->desc) {
520 dma_free_coherent(rx_ring->dev, rx_ring->size,
521 rx_ring->desc, rx_ring->dma);
522 rx_ring->desc = NULL;
523 }
524}
525
526/**
527 * i40evf_setup_rx_descriptors - Allocate Rx descriptors
528 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
529 *
530 * Returns 0 on success, negative on failure
531 **/
532int i40evf_setup_rx_descriptors(struct i40e_ring *rx_ring)
533{
534 struct device *dev = rx_ring->dev;
535 int bi_size;
536
537 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
538 rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
539 if (!rx_ring->rx_bi)
540 goto err;
541
542 /* Round up to nearest 4K */
543 rx_ring->size = ring_is_16byte_desc_enabled(rx_ring)
544 ? rx_ring->count * sizeof(union i40e_16byte_rx_desc)
545 : rx_ring->count * sizeof(union i40e_32byte_rx_desc);
546 rx_ring->size = ALIGN(rx_ring->size, 4096);
547 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
548 &rx_ring->dma, GFP_KERNEL);
549
550 if (!rx_ring->desc) {
551 dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
552 rx_ring->size);
553 goto err;
554 }
555
556 rx_ring->next_to_clean = 0;
557 rx_ring->next_to_use = 0;
558
559 return 0;
560err:
561 kfree(rx_ring->rx_bi);
562 rx_ring->rx_bi = NULL;
563 return -ENOMEM;
564}
565
566/**
567 * i40e_release_rx_desc - Store the new tail and head values
568 * @rx_ring: ring to bump
569 * @val: new head index
570 **/
571static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
572{
573 rx_ring->next_to_use = val;
574 /* Force memory writes to complete before letting h/w
575 * know there are new descriptors to fetch. (Only
576 * applicable for weak-ordered memory model archs,
577 * such as IA-64).
578 */
579 wmb();
580 writel(val, rx_ring->tail);
581}
582
583/**
584 * i40evf_alloc_rx_buffers - Replace used receive buffers; packet split
585 * @rx_ring: ring to place buffers on
586 * @cleaned_count: number of buffers to replace
587 **/
588void i40evf_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
589{
590 u16 i = rx_ring->next_to_use;
591 union i40e_rx_desc *rx_desc;
592 struct i40e_rx_buffer *bi;
593 struct sk_buff *skb;
594
595 /* do nothing if no valid netdev defined */
596 if (!rx_ring->netdev || !cleaned_count)
597 return;
598
599 while (cleaned_count--) {
600 rx_desc = I40E_RX_DESC(rx_ring, i);
601 bi = &rx_ring->rx_bi[i];
602 skb = bi->skb;
603
604 if (!skb) {
605 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
606 rx_ring->rx_buf_len);
607 if (!skb) {
608 rx_ring->rx_stats.alloc_buff_failed++;
609 goto no_buffers;
610 }
611 /* initialize queue mapping */
612 skb_record_rx_queue(skb, rx_ring->queue_index);
613 bi->skb = skb;
614 }
615
616 if (!bi->dma) {
617 bi->dma = dma_map_single(rx_ring->dev,
618 skb->data,
619 rx_ring->rx_buf_len,
620 DMA_FROM_DEVICE);
621 if (dma_mapping_error(rx_ring->dev, bi->dma)) {
622 rx_ring->rx_stats.alloc_buff_failed++;
623 bi->dma = 0;
624 goto no_buffers;
625 }
626 }
627
628 if (ring_is_ps_enabled(rx_ring)) {
629 if (!bi->page) {
630 bi->page = alloc_page(GFP_ATOMIC);
631 if (!bi->page) {
632 rx_ring->rx_stats.alloc_page_failed++;
633 goto no_buffers;
634 }
635 }
636
637 if (!bi->page_dma) {
638 /* use a half page if we're re-using */
639 bi->page_offset ^= PAGE_SIZE / 2;
640 bi->page_dma = dma_map_page(rx_ring->dev,
641 bi->page,
642 bi->page_offset,
643 PAGE_SIZE / 2,
644 DMA_FROM_DEVICE);
645 if (dma_mapping_error(rx_ring->dev,
646 bi->page_dma)) {
647 rx_ring->rx_stats.alloc_page_failed++;
648 bi->page_dma = 0;
649 goto no_buffers;
650 }
651 }
652
653 /* Refresh the desc even if buffer_addrs didn't change
654 * because each write-back erases this info.
655 */
656 rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
657 rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
658 } else {
659 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
660 rx_desc->read.hdr_addr = 0;
661 }
662 i++;
663 if (i == rx_ring->count)
664 i = 0;
665 }
666
667no_buffers:
668 if (rx_ring->next_to_use != i)
669 i40e_release_rx_desc(rx_ring, i);
670}
671
672/**
673 * i40e_receive_skb - Send a completed packet up the stack
674 * @rx_ring: rx ring in play
675 * @skb: packet to send up
676 * @vlan_tag: vlan tag for packet
677 **/
678static void i40e_receive_skb(struct i40e_ring *rx_ring,
679 struct sk_buff *skb, u16 vlan_tag)
680{
681 struct i40e_q_vector *q_vector = rx_ring->q_vector;
682 struct i40e_vsi *vsi = rx_ring->vsi;
683 u64 flags = vsi->back->flags;
684
685 if (vlan_tag & VLAN_VID_MASK)
686 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
687
688 if (flags & I40E_FLAG_IN_NETPOLL)
689 netif_rx(skb);
690 else
691 napi_gro_receive(&q_vector->napi, skb);
692}
693
694/**
695 * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
696 * @vsi: the VSI we care about
697 * @skb: skb currently being received and modified
698 * @rx_status: status value of last descriptor in packet
699 * @rx_error: error value of last descriptor in packet
700 * @rx_ptype: ptype value of last descriptor in packet
701 **/
702static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
703 struct sk_buff *skb,
704 u32 rx_status,
705 u32 rx_error,
706 u16 rx_ptype)
707{
708 bool ipv4_tunnel, ipv6_tunnel;
709 __wsum rx_udp_csum;
710 __sum16 csum;
711 struct iphdr *iph;
712
713 ipv4_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT4_MAC_PAY3) &&
714 (rx_ptype < I40E_RX_PTYPE_GRENAT4_MACVLAN_IPV6_ICMP_PAY4);
715 ipv6_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT6_MAC_PAY3) &&
716 (rx_ptype < I40E_RX_PTYPE_GRENAT6_MACVLAN_IPV6_ICMP_PAY4);
717
718 skb->encapsulation = ipv4_tunnel || ipv6_tunnel;
719 skb->ip_summed = CHECKSUM_NONE;
720
721 /* Rx csum enabled and ip headers found? */
722 if (!(vsi->netdev->features & NETIF_F_RXCSUM &&
723 rx_status & (1 << I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
724 return;
725
Jesse Brandeburgddf1d0d2014-02-13 03:48:39 -0800726 /* likely incorrect csum if alternate IP extension headers found */
Greg Rose7f12ad72013-12-21 06:12:51 +0000727 if (rx_status & (1 << I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
728 return;
729
730 /* IP or L4 or outmost IP checksum error */
731 if (rx_error & ((1 << I40E_RX_DESC_ERROR_IPE_SHIFT) |
732 (1 << I40E_RX_DESC_ERROR_L4E_SHIFT) |
733 (1 << I40E_RX_DESC_ERROR_EIPE_SHIFT))) {
734 vsi->back->hw_csum_rx_error++;
735 return;
736 }
737
738 if (ipv4_tunnel &&
739 !(rx_status & (1 << I40E_RX_DESC_STATUS_UDP_0_SHIFT))) {
740 /* If VXLAN traffic has an outer UDPv4 checksum we need to check
741 * it in the driver, hardware does not do it for us.
742 * Since L3L4P bit was set we assume a valid IHL value (>=5)
743 * so the total length of IPv4 header is IHL*4 bytes
744 */
745 skb->transport_header = skb->mac_header +
746 sizeof(struct ethhdr) +
747 (ip_hdr(skb)->ihl * 4);
748
749 /* Add 4 bytes for VLAN tagged packets */
750 skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
751 skb->protocol == htons(ETH_P_8021AD))
752 ? VLAN_HLEN : 0;
753
754 rx_udp_csum = udp_csum(skb);
755 iph = ip_hdr(skb);
756 csum = csum_tcpudp_magic(
757 iph->saddr, iph->daddr,
758 (skb->len - skb_transport_offset(skb)),
759 IPPROTO_UDP, rx_udp_csum);
760
761 if (udp_hdr(skb)->check != csum) {
762 vsi->back->hw_csum_rx_error++;
763 return;
764 }
765 }
766
767 skb->ip_summed = CHECKSUM_UNNECESSARY;
768}
769
770/**
771 * i40e_rx_hash - returns the hash value from the Rx descriptor
772 * @ring: descriptor ring
773 * @rx_desc: specific descriptor
774 **/
775static inline u32 i40e_rx_hash(struct i40e_ring *ring,
776 union i40e_rx_desc *rx_desc)
777{
778 const __le64 rss_mask =
779 cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
780 I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
781
782 if ((ring->netdev->features & NETIF_F_RXHASH) &&
783 (rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask)
784 return le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
785 else
786 return 0;
787}
788
789/**
Jesse Brandeburg206812b2014-02-12 01:45:33 +0000790 * i40e_ptype_to_hash - get a hash type
791 * @ptype: the ptype value from the descriptor
792 *
793 * Returns a hash type to be used by skb_set_hash
794 **/
795static inline enum pkt_hash_types i40e_ptype_to_hash(u8 ptype)
796{
797 struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
798
799 if (!decoded.known)
800 return PKT_HASH_TYPE_NONE;
801
802 if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
803 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
804 return PKT_HASH_TYPE_L4;
805 else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
806 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
807 return PKT_HASH_TYPE_L3;
808 else
809 return PKT_HASH_TYPE_L2;
810}
811
812/**
Greg Rose7f12ad72013-12-21 06:12:51 +0000813 * i40e_clean_rx_irq - Reclaim resources after receive completes
814 * @rx_ring: rx ring to clean
815 * @budget: how many cleans we're allowed
816 *
817 * Returns true if there's any budget left (e.g. the clean is finished)
818 **/
819static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
820{
821 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
822 u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
823 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
824 const int current_node = numa_node_id();
825 struct i40e_vsi *vsi = rx_ring->vsi;
826 u16 i = rx_ring->next_to_clean;
827 union i40e_rx_desc *rx_desc;
828 u32 rx_error, rx_status;
Jesse Brandeburg206812b2014-02-12 01:45:33 +0000829 u8 rx_ptype;
Greg Rose7f12ad72013-12-21 06:12:51 +0000830 u64 qword;
Greg Rose7f12ad72013-12-21 06:12:51 +0000831
832 rx_desc = I40E_RX_DESC(rx_ring, i);
833 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Jesse Brandeburgaf1a2a92014-02-13 03:48:41 -0800834 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
835 I40E_RXD_QW1_STATUS_SHIFT;
Greg Rose7f12ad72013-12-21 06:12:51 +0000836
837 while (rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)) {
838 union i40e_rx_desc *next_rxd;
839 struct i40e_rx_buffer *rx_bi;
840 struct sk_buff *skb;
841 u16 vlan_tag;
842 rx_bi = &rx_ring->rx_bi[i];
843 skb = rx_bi->skb;
844 prefetch(skb->data);
845
846 rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
847 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
848 rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
849 I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
850 rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
851 I40E_RXD_QW1_LENGTH_SPH_SHIFT;
852
853 rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
854 I40E_RXD_QW1_ERROR_SHIFT;
855 rx_hbo = rx_error & (1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
856 rx_error &= ~(1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
857
858 rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
859 I40E_RXD_QW1_PTYPE_SHIFT;
860 rx_bi->skb = NULL;
861
862 /* This memory barrier is needed to keep us from reading
863 * any other fields out of the rx_desc until we know the
864 * STATUS_DD bit is set
865 */
866 rmb();
867
868 /* Get the header and possibly the whole packet
869 * If this is an skb from previous receive dma will be 0
870 */
871 if (rx_bi->dma) {
872 u16 len;
873
874 if (rx_hbo)
875 len = I40E_RX_HDR_SIZE;
876 else if (rx_sph)
877 len = rx_header_len;
878 else if (rx_packet_len)
879 len = rx_packet_len; /* 1buf/no split found */
880 else
881 len = rx_header_len; /* split always mode */
882
883 skb_put(skb, len);
884 dma_unmap_single(rx_ring->dev,
885 rx_bi->dma,
886 rx_ring->rx_buf_len,
887 DMA_FROM_DEVICE);
888 rx_bi->dma = 0;
889 }
890
891 /* Get the rest of the data if this was a header split */
892 if (ring_is_ps_enabled(rx_ring) && rx_packet_len) {
893
894 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
895 rx_bi->page,
896 rx_bi->page_offset,
897 rx_packet_len);
898
899 skb->len += rx_packet_len;
900 skb->data_len += rx_packet_len;
901 skb->truesize += rx_packet_len;
902
903 if ((page_count(rx_bi->page) == 1) &&
904 (page_to_nid(rx_bi->page) == current_node))
905 get_page(rx_bi->page);
906 else
907 rx_bi->page = NULL;
908
909 dma_unmap_page(rx_ring->dev,
910 rx_bi->page_dma,
911 PAGE_SIZE / 2,
912 DMA_FROM_DEVICE);
913 rx_bi->page_dma = 0;
914 }
915 I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
916
917 if (unlikely(
918 !(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
919 struct i40e_rx_buffer *next_buffer;
920
921 next_buffer = &rx_ring->rx_bi[i];
922
923 if (ring_is_ps_enabled(rx_ring)) {
924 rx_bi->skb = next_buffer->skb;
925 rx_bi->dma = next_buffer->dma;
926 next_buffer->skb = skb;
927 next_buffer->dma = 0;
928 }
929 rx_ring->rx_stats.non_eop_descs++;
930 goto next_desc;
931 }
932
933 /* ERR_MASK will only have valid bits if EOP set */
934 if (unlikely(rx_error & (1 << I40E_RX_DESC_ERROR_RXE_SHIFT))) {
935 dev_kfree_skb_any(skb);
936 goto next_desc;
937 }
938
Jesse Brandeburg206812b2014-02-12 01:45:33 +0000939 skb_set_hash(skb, i40e_rx_hash(rx_ring, rx_desc),
940 i40e_ptype_to_hash(rx_ptype));
Greg Rose7f12ad72013-12-21 06:12:51 +0000941 /* probably a little skewed due to removing CRC */
942 total_rx_bytes += skb->len;
943 total_rx_packets++;
944
945 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
946
947 i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
948
949 vlan_tag = rx_status & (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
950 ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
951 : 0;
952 i40e_receive_skb(rx_ring, skb, vlan_tag);
953
954 rx_ring->netdev->last_rx = jiffies;
955 budget--;
956next_desc:
957 rx_desc->wb.qword1.status_error_len = 0;
958 if (!budget)
959 break;
960
961 cleaned_count++;
962 /* return some buffers to hardware, one at a time is too slow */
963 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
964 i40evf_alloc_rx_buffers(rx_ring, cleaned_count);
965 cleaned_count = 0;
966 }
967
968 /* use prefetched values */
969 rx_desc = next_rxd;
970 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
971 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
972 I40E_RXD_QW1_STATUS_SHIFT;
973 }
974
975 rx_ring->next_to_clean = i;
976 u64_stats_update_begin(&rx_ring->syncp);
977 rx_ring->stats.packets += total_rx_packets;
978 rx_ring->stats.bytes += total_rx_bytes;
979 u64_stats_update_end(&rx_ring->syncp);
980 rx_ring->q_vector->rx.total_packets += total_rx_packets;
981 rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
982
983 if (cleaned_count)
984 i40evf_alloc_rx_buffers(rx_ring, cleaned_count);
985
986 return budget > 0;
987}
988
989/**
990 * i40evf_napi_poll - NAPI polling Rx/Tx cleanup routine
991 * @napi: napi struct with our devices info in it
992 * @budget: amount of work driver is allowed to do this pass, in packets
993 *
994 * This function will clean all queues associated with a q_vector.
995 *
996 * Returns the amount of work done
997 **/
998int i40evf_napi_poll(struct napi_struct *napi, int budget)
999{
1000 struct i40e_q_vector *q_vector =
1001 container_of(napi, struct i40e_q_vector, napi);
1002 struct i40e_vsi *vsi = q_vector->vsi;
1003 struct i40e_ring *ring;
1004 bool clean_complete = true;
1005 int budget_per_ring;
1006
1007 if (test_bit(__I40E_DOWN, &vsi->state)) {
1008 napi_complete(napi);
1009 return 0;
1010 }
1011
1012 /* Since the actual Tx work is minimal, we can give the Tx a larger
1013 * budget and be more aggressive about cleaning up the Tx descriptors.
1014 */
1015 i40e_for_each_ring(ring, q_vector->tx)
1016 clean_complete &= i40e_clean_tx_irq(ring, vsi->work_limit);
1017
1018 /* We attempt to distribute budget to each Rx queue fairly, but don't
1019 * allow the budget to go below 1 because that would exit polling early.
1020 */
1021 budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
1022
1023 i40e_for_each_ring(ring, q_vector->rx)
1024 clean_complete &= i40e_clean_rx_irq(ring, budget_per_ring);
1025
1026 /* If work not completed, return budget and polling will return */
1027 if (!clean_complete)
1028 return budget;
1029
1030 /* Work is done so exit the polling mode and re-enable the interrupt */
1031 napi_complete(napi);
1032 if (ITR_IS_DYNAMIC(vsi->rx_itr_setting) ||
1033 ITR_IS_DYNAMIC(vsi->tx_itr_setting))
1034 i40e_update_dynamic_itr(q_vector);
1035
1036 if (!test_bit(__I40E_DOWN, &vsi->state))
1037 i40evf_irq_enable_queues(vsi->back, 1 << q_vector->v_idx);
1038
1039 return 0;
1040}
1041
1042/**
1043 * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
1044 * @skb: send buffer
1045 * @tx_ring: ring to send buffer on
1046 * @flags: the tx flags to be set
1047 *
1048 * Checks the skb and set up correspondingly several generic transmit flags
1049 * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
1050 *
1051 * Returns error code indicate the frame should be dropped upon error and the
1052 * otherwise returns 0 to indicate the flags has been set properly.
1053 **/
1054static int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
1055 struct i40e_ring *tx_ring,
1056 u32 *flags)
1057{
1058 __be16 protocol = skb->protocol;
1059 u32 tx_flags = 0;
1060
1061 /* if we have a HW VLAN tag being added, default to the HW one */
1062 if (vlan_tx_tag_present(skb)) {
1063 tx_flags |= vlan_tx_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
1064 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1065 /* else if it is a SW VLAN, check the next protocol and store the tag */
1066 } else if (protocol == htons(ETH_P_8021Q)) {
1067 struct vlan_hdr *vhdr, _vhdr;
1068 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
1069 if (!vhdr)
1070 return -EINVAL;
1071
1072 protocol = vhdr->h_vlan_encapsulated_proto;
1073 tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
1074 tx_flags |= I40E_TX_FLAGS_SW_VLAN;
1075 }
1076
1077 *flags = tx_flags;
1078 return 0;
1079}
1080
1081/**
1082 * i40e_tso - set up the tso context descriptor
1083 * @tx_ring: ptr to the ring to send
1084 * @skb: ptr to the skb we're sending
1085 * @tx_flags: the collected send information
1086 * @protocol: the send protocol
1087 * @hdr_len: ptr to the size of the packet header
1088 * @cd_tunneling: ptr to context descriptor bits
1089 *
1090 * Returns 0 if no TSO can happen, 1 if tso is going, or error
1091 **/
1092static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
1093 u32 tx_flags, __be16 protocol, u8 *hdr_len,
1094 u64 *cd_type_cmd_tso_mss, u32 *cd_tunneling)
1095{
1096 u32 cd_cmd, cd_tso_len, cd_mss;
1097 struct tcphdr *tcph;
1098 struct iphdr *iph;
1099 u32 l4len;
1100 int err;
1101 struct ipv6hdr *ipv6h;
1102
1103 if (!skb_is_gso(skb))
1104 return 0;
1105
1106 if (skb_header_cloned(skb)) {
1107 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1108 if (err)
1109 return err;
1110 }
1111
1112 if (protocol == htons(ETH_P_IP)) {
1113 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
1114 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1115 iph->tot_len = 0;
1116 iph->check = 0;
1117 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1118 0, IPPROTO_TCP, 0);
1119 } else if (skb_is_gso_v6(skb)) {
1120
1121 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb)
1122 : ipv6_hdr(skb);
1123 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1124 ipv6h->payload_len = 0;
1125 tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
1126 0, IPPROTO_TCP, 0);
1127 }
1128
1129 l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
1130 *hdr_len = (skb->encapsulation
1131 ? (skb_inner_transport_header(skb) - skb->data)
1132 : skb_transport_offset(skb)) + l4len;
1133
1134 /* find the field values */
1135 cd_cmd = I40E_TX_CTX_DESC_TSO;
1136 cd_tso_len = skb->len - *hdr_len;
1137 cd_mss = skb_shinfo(skb)->gso_size;
1138 *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
1139 ((u64)cd_tso_len <<
1140 I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
1141 ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
1142 return 1;
1143}
1144
1145/**
1146 * i40e_tx_enable_csum - Enable Tx checksum offloads
1147 * @skb: send buffer
1148 * @tx_flags: Tx flags currently set
1149 * @td_cmd: Tx descriptor command bits to set
1150 * @td_offset: Tx descriptor header offsets to set
1151 * @cd_tunneling: ptr to context desc bits
1152 **/
1153static void i40e_tx_enable_csum(struct sk_buff *skb, u32 tx_flags,
1154 u32 *td_cmd, u32 *td_offset,
1155 struct i40e_ring *tx_ring,
1156 u32 *cd_tunneling)
1157{
1158 struct ipv6hdr *this_ipv6_hdr;
1159 unsigned int this_tcp_hdrlen;
1160 struct iphdr *this_ip_hdr;
1161 u32 network_hdr_len;
1162 u8 l4_hdr = 0;
1163
1164 if (skb->encapsulation) {
1165 network_hdr_len = skb_inner_network_header_len(skb);
1166 this_ip_hdr = inner_ip_hdr(skb);
1167 this_ipv6_hdr = inner_ipv6_hdr(skb);
1168 this_tcp_hdrlen = inner_tcp_hdrlen(skb);
1169
1170 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1171
1172 if (tx_flags & I40E_TX_FLAGS_TSO) {
1173 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
1174 ip_hdr(skb)->check = 0;
1175 } else {
1176 *cd_tunneling |=
1177 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1178 }
1179 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1180 if (tx_flags & I40E_TX_FLAGS_TSO) {
1181 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
1182 ip_hdr(skb)->check = 0;
1183 } else {
1184 *cd_tunneling |=
1185 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1186 }
1187 }
1188
1189 /* Now set the ctx descriptor fields */
1190 *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
1191 I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
1192 I40E_TXD_CTX_UDP_TUNNELING |
1193 ((skb_inner_network_offset(skb) -
1194 skb_transport_offset(skb)) >> 1) <<
1195 I40E_TXD_CTX_QW0_NATLEN_SHIFT;
1196
1197 } else {
1198 network_hdr_len = skb_network_header_len(skb);
1199 this_ip_hdr = ip_hdr(skb);
1200 this_ipv6_hdr = ipv6_hdr(skb);
1201 this_tcp_hdrlen = tcp_hdrlen(skb);
1202 }
1203
1204 /* Enable IP checksum offloads */
1205 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1206 l4_hdr = this_ip_hdr->protocol;
1207 /* the stack computes the IP header already, the only time we
1208 * need the hardware to recompute it is in the case of TSO.
1209 */
1210 if (tx_flags & I40E_TX_FLAGS_TSO) {
1211 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
1212 this_ip_hdr->check = 0;
1213 } else {
1214 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
1215 }
1216 /* Now set the td_offset for IP header length */
1217 *td_offset = (network_hdr_len >> 2) <<
1218 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1219 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1220 l4_hdr = this_ipv6_hdr->nexthdr;
1221 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
1222 /* Now set the td_offset for IP header length */
1223 *td_offset = (network_hdr_len >> 2) <<
1224 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1225 }
1226 /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
1227 *td_offset |= (skb_network_offset(skb) >> 1) <<
1228 I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
1229
1230 /* Enable L4 checksum offloads */
1231 switch (l4_hdr) {
1232 case IPPROTO_TCP:
1233 /* enable checksum offloads */
1234 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
1235 *td_offset |= (this_tcp_hdrlen >> 2) <<
1236 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1237 break;
1238 case IPPROTO_SCTP:
1239 /* enable SCTP checksum offload */
1240 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
1241 *td_offset |= (sizeof(struct sctphdr) >> 2) <<
1242 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1243 break;
1244 case IPPROTO_UDP:
1245 /* enable UDP checksum offload */
1246 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
1247 *td_offset |= (sizeof(struct udphdr) >> 2) <<
1248 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1249 break;
1250 default:
1251 break;
1252 }
1253}
1254
1255/**
1256 * i40e_create_tx_ctx Build the Tx context descriptor
1257 * @tx_ring: ring to create the descriptor on
1258 * @cd_type_cmd_tso_mss: Quad Word 1
1259 * @cd_tunneling: Quad Word 0 - bits 0-31
1260 * @cd_l2tag2: Quad Word 0 - bits 32-63
1261 **/
1262static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
1263 const u64 cd_type_cmd_tso_mss,
1264 const u32 cd_tunneling, const u32 cd_l2tag2)
1265{
1266 struct i40e_tx_context_desc *context_desc;
1267 int i = tx_ring->next_to_use;
1268
1269 if (!cd_type_cmd_tso_mss && !cd_tunneling && !cd_l2tag2)
1270 return;
1271
1272 /* grab the next descriptor */
1273 context_desc = I40E_TX_CTXTDESC(tx_ring, i);
1274
1275 i++;
1276 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
1277
1278 /* cpu_to_le32 and assign to struct fields */
1279 context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
1280 context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
1281 context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
1282}
1283
1284/**
1285 * i40e_tx_map - Build the Tx descriptor
1286 * @tx_ring: ring to send buffer on
1287 * @skb: send buffer
1288 * @first: first buffer info buffer to use
1289 * @tx_flags: collected send information
1290 * @hdr_len: size of the packet header
1291 * @td_cmd: the command field in the descriptor
1292 * @td_offset: offset for checksum or crc
1293 **/
1294static void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
1295 struct i40e_tx_buffer *first, u32 tx_flags,
1296 const u8 hdr_len, u32 td_cmd, u32 td_offset)
1297{
1298 unsigned int data_len = skb->data_len;
1299 unsigned int size = skb_headlen(skb);
1300 struct skb_frag_struct *frag;
1301 struct i40e_tx_buffer *tx_bi;
1302 struct i40e_tx_desc *tx_desc;
1303 u16 i = tx_ring->next_to_use;
1304 u32 td_tag = 0;
1305 dma_addr_t dma;
1306 u16 gso_segs;
1307
1308 if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
1309 td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
1310 td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
1311 I40E_TX_FLAGS_VLAN_SHIFT;
1312 }
1313
1314 if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
1315 gso_segs = skb_shinfo(skb)->gso_segs;
1316 else
1317 gso_segs = 1;
1318
1319 /* multiply data chunks by size of headers */
1320 first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
1321 first->gso_segs = gso_segs;
1322 first->skb = skb;
1323 first->tx_flags = tx_flags;
1324
1325 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1326
1327 tx_desc = I40E_TX_DESC(tx_ring, i);
1328 tx_bi = first;
1329
1330 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1331 if (dma_mapping_error(tx_ring->dev, dma))
1332 goto dma_error;
1333
1334 /* record length, and DMA address */
1335 dma_unmap_len_set(tx_bi, len, size);
1336 dma_unmap_addr_set(tx_bi, dma, dma);
1337
1338 tx_desc->buffer_addr = cpu_to_le64(dma);
1339
1340 while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
1341 tx_desc->cmd_type_offset_bsz =
1342 build_ctob(td_cmd, td_offset,
1343 I40E_MAX_DATA_PER_TXD, td_tag);
1344
1345 tx_desc++;
1346 i++;
1347 if (i == tx_ring->count) {
1348 tx_desc = I40E_TX_DESC(tx_ring, 0);
1349 i = 0;
1350 }
1351
1352 dma += I40E_MAX_DATA_PER_TXD;
1353 size -= I40E_MAX_DATA_PER_TXD;
1354
1355 tx_desc->buffer_addr = cpu_to_le64(dma);
1356 }
1357
1358 if (likely(!data_len))
1359 break;
1360
1361 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
1362 size, td_tag);
1363
1364 tx_desc++;
1365 i++;
1366 if (i == tx_ring->count) {
1367 tx_desc = I40E_TX_DESC(tx_ring, 0);
1368 i = 0;
1369 }
1370
1371 size = skb_frag_size(frag);
1372 data_len -= size;
1373
1374 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
1375 DMA_TO_DEVICE);
1376
1377 tx_bi = &tx_ring->tx_bi[i];
1378 }
1379
1380 tx_desc->cmd_type_offset_bsz =
1381 build_ctob(td_cmd, td_offset, size, td_tag) |
1382 cpu_to_le64((u64)I40E_TXD_CMD << I40E_TXD_QW1_CMD_SHIFT);
1383
1384 netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
1385 tx_ring->queue_index),
1386 first->bytecount);
1387
1388 /* set the timestamp */
1389 first->time_stamp = jiffies;
1390
1391 /* Force memory writes to complete before letting h/w
1392 * know there are new descriptors to fetch. (Only
1393 * applicable for weak-ordered memory model archs,
1394 * such as IA-64).
1395 */
1396 wmb();
1397
1398 /* set next_to_watch value indicating a packet is present */
1399 first->next_to_watch = tx_desc;
1400
1401 i++;
1402 if (i == tx_ring->count)
1403 i = 0;
1404
1405 tx_ring->next_to_use = i;
1406
1407 /* notify HW of packet */
1408 writel(i, tx_ring->tail);
1409
1410 return;
1411
1412dma_error:
1413 dev_info(tx_ring->dev, "TX DMA map failed\n");
1414
1415 /* clear dma mappings for failed tx_bi map */
1416 for (;;) {
1417 tx_bi = &tx_ring->tx_bi[i];
1418 i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
1419 if (tx_bi == first)
1420 break;
1421 if (i == 0)
1422 i = tx_ring->count;
1423 i--;
1424 }
1425
1426 tx_ring->next_to_use = i;
1427}
1428
1429/**
1430 * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
1431 * @tx_ring: the ring to be checked
1432 * @size: the size buffer we want to assure is available
1433 *
1434 * Returns -EBUSY if a stop is needed, else 0
1435 **/
1436static inline int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
1437{
1438 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
1439 /* Memory barrier before checking head and tail */
1440 smp_mb();
1441
1442 /* Check again in a case another CPU has just made room available. */
1443 if (likely(I40E_DESC_UNUSED(tx_ring) < size))
1444 return -EBUSY;
1445
1446 /* A reprieve! - use start_queue because it doesn't call schedule */
1447 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
1448 ++tx_ring->tx_stats.restart_queue;
1449 return 0;
1450}
1451
1452/**
1453 * i40e_maybe_stop_tx - 1st level check for tx stop conditions
1454 * @tx_ring: the ring to be checked
1455 * @size: the size buffer we want to assure is available
1456 *
1457 * Returns 0 if stop is not needed
1458 **/
1459static int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
1460{
1461 if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
1462 return 0;
1463 return __i40e_maybe_stop_tx(tx_ring, size);
1464}
1465
1466/**
1467 * i40e_xmit_descriptor_count - calculate number of tx descriptors needed
1468 * @skb: send buffer
1469 * @tx_ring: ring to send buffer on
1470 *
1471 * Returns number of data descriptors needed for this skb. Returns 0 to indicate
1472 * there is not enough descriptors available in this ring since we need at least
1473 * one descriptor.
1474 **/
1475static int i40e_xmit_descriptor_count(struct sk_buff *skb,
1476 struct i40e_ring *tx_ring)
1477{
1478#if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
1479 unsigned int f;
1480#endif
1481 int count = 0;
1482
1483 /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
1484 * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
1485 * + 2 desc gap to keep tail from touching head,
1486 * + 1 desc for context descriptor,
1487 * otherwise try next time
1488 */
1489#if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
1490 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1491 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
1492#else
1493 count += skb_shinfo(skb)->nr_frags;
1494#endif
1495 count += TXD_USE_COUNT(skb_headlen(skb));
1496 if (i40e_maybe_stop_tx(tx_ring, count + 3)) {
1497 tx_ring->tx_stats.tx_busy++;
1498 return 0;
1499 }
1500 return count;
1501}
1502
1503/**
1504 * i40e_xmit_frame_ring - Sends buffer on Tx ring
1505 * @skb: send buffer
1506 * @tx_ring: ring to send buffer on
1507 *
1508 * Returns NETDEV_TX_OK if sent, else an error code
1509 **/
1510static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
1511 struct i40e_ring *tx_ring)
1512{
1513 u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
1514 u32 cd_tunneling = 0, cd_l2tag2 = 0;
1515 struct i40e_tx_buffer *first;
1516 u32 td_offset = 0;
1517 u32 tx_flags = 0;
1518 __be16 protocol;
1519 u32 td_cmd = 0;
1520 u8 hdr_len = 0;
1521 int tso;
1522 if (0 == i40e_xmit_descriptor_count(skb, tx_ring))
1523 return NETDEV_TX_BUSY;
1524
1525 /* prepare the xmit flags */
1526 if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
1527 goto out_drop;
1528
1529 /* obtain protocol of skb */
1530 protocol = skb->protocol;
1531
1532 /* record the location of the first descriptor for this packet */
1533 first = &tx_ring->tx_bi[tx_ring->next_to_use];
1534
1535 /* setup IPv4/IPv6 offloads */
1536 if (protocol == htons(ETH_P_IP))
1537 tx_flags |= I40E_TX_FLAGS_IPV4;
1538 else if (protocol == htons(ETH_P_IPV6))
1539 tx_flags |= I40E_TX_FLAGS_IPV6;
1540
1541 tso = i40e_tso(tx_ring, skb, tx_flags, protocol, &hdr_len,
1542 &cd_type_cmd_tso_mss, &cd_tunneling);
1543
1544 if (tso < 0)
1545 goto out_drop;
1546 else if (tso)
1547 tx_flags |= I40E_TX_FLAGS_TSO;
1548
1549 skb_tx_timestamp(skb);
1550
1551 /* always enable CRC insertion offload */
1552 td_cmd |= I40E_TX_DESC_CMD_ICRC;
1553
1554 /* Always offload the checksum, since it's in the data descriptor */
1555 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1556 tx_flags |= I40E_TX_FLAGS_CSUM;
1557
1558 i40e_tx_enable_csum(skb, tx_flags, &td_cmd, &td_offset,
1559 tx_ring, &cd_tunneling);
1560 }
1561
1562 i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
1563 cd_tunneling, cd_l2tag2);
1564
1565 i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
1566 td_cmd, td_offset);
1567
1568 i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
1569
1570 return NETDEV_TX_OK;
1571
1572out_drop:
1573 dev_kfree_skb_any(skb);
1574 return NETDEV_TX_OK;
1575}
1576
1577/**
1578 * i40evf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
1579 * @skb: send buffer
1580 * @netdev: network interface device structure
1581 *
1582 * Returns NETDEV_TX_OK if sent, else an error code
1583 **/
1584netdev_tx_t i40evf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1585{
1586 struct i40evf_adapter *adapter = netdev_priv(netdev);
1587 struct i40e_ring *tx_ring = adapter->tx_rings[skb->queue_mapping];
1588
1589 /* hardware can't handle really short frames, hardware padding works
1590 * beyond this point
1591 */
1592 if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
1593 if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
1594 return NETDEV_TX_OK;
1595 skb->len = I40E_MIN_TX_LEN;
1596 skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
1597 }
1598
1599 return i40e_xmit_frame_ring(skb, tx_ring);
1600}