1 /*******************************************************************************
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
4 * Copyright(c) 2013 - 2014 Intel Corporation.
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.
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
15 * The full GNU General Public License is included in this distribution in
16 * the file called "COPYING".
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
22 ******************************************************************************/
24 #include <linux/prefetch.h>
27 #include "i40e_prototype.h"
29 static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
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));
39 #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
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
46 static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
47 struct i40e_tx_buffer *tx_buffer)
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),
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),
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 */
69 * i40evf_clean_tx_ring - Free any empty Tx buffers
70 * @tx_ring: ring to be cleaned
72 void i40evf_clean_tx_ring(struct i40e_ring *tx_ring)
74 unsigned long bi_size;
77 /* ring already cleared, nothing to do */
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]);
85 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
86 memset(tx_ring->tx_bi, 0, bi_size);
88 /* Zero out the descriptor ring */
89 memset(tx_ring->desc, 0, tx_ring->size);
91 tx_ring->next_to_use = 0;
92 tx_ring->next_to_clean = 0;
97 /* cleanup Tx queue statistics */
98 netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
99 tx_ring->queue_index));
103 * i40evf_free_tx_resources - Free Tx resources per queue
104 * @tx_ring: Tx descriptor ring for a specific queue
106 * Free all transmit software resources
108 void i40evf_free_tx_resources(struct i40e_ring *tx_ring)
110 i40evf_clean_tx_ring(tx_ring);
111 kfree(tx_ring->tx_bi);
112 tx_ring->tx_bi = NULL;
115 dma_free_coherent(tx_ring->dev, tx_ring->size,
116 tx_ring->desc, tx_ring->dma);
117 tx_ring->desc = NULL;
122 * i40e_get_tx_pending - how many tx descriptors not processed
123 * @tx_ring: the ring of descriptors
125 * Since there is no access to the ring head register
126 * in XL710, we need to use our local copies
128 static u32 i40e_get_tx_pending(struct i40e_ring *ring)
130 u32 ntu = ((ring->next_to_clean <= ring->next_to_use)
132 : ring->next_to_use + ring->count);
133 return ntu - ring->next_to_clean;
137 * i40e_check_tx_hang - Is there a hang in the Tx queue
138 * @tx_ring: the ring of descriptors
140 static bool i40e_check_tx_hang(struct i40e_ring *tx_ring)
142 u32 tx_pending = i40e_get_tx_pending(tx_ring);
145 clear_check_for_tx_hang(tx_ring);
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.
158 if ((tx_ring->tx_stats.tx_done_old == tx_ring->stats.packets) &&
160 /* make sure it is true for two checks in a row */
161 ret = test_and_set_bit(__I40E_HANG_CHECK_ARMED,
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);
173 * i40e_get_head - Retrieve head from head writeback
174 * @tx_ring: tx ring to fetch head of
176 * Returns value of Tx ring head based on value stored
177 * in head write-back location
179 static inline u32 i40e_get_head(struct i40e_ring *tx_ring)
181 void *head = (struct i40e_tx_desc *)tx_ring->desc + tx_ring->count;
183 return le32_to_cpu(*(volatile __le32 *)head);
187 * i40e_clean_tx_irq - Reclaim resources after transmit completes
188 * @tx_ring: tx ring to clean
189 * @budget: how many cleans we're allowed
191 * Returns true if there's any budget left (e.g. the clean is finished)
193 static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
195 u16 i = tx_ring->next_to_clean;
196 struct i40e_tx_buffer *tx_buf;
197 struct i40e_tx_desc *tx_head;
198 struct i40e_tx_desc *tx_desc;
199 unsigned int total_packets = 0;
200 unsigned int total_bytes = 0;
202 tx_buf = &tx_ring->tx_bi[i];
203 tx_desc = I40E_TX_DESC(tx_ring, i);
206 tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
209 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
211 /* if next_to_watch is not set then there is no work pending */
215 /* prevent any other reads prior to eop_desc */
216 read_barrier_depends();
218 /* we have caught up to head, no work left to do */
219 if (tx_head == tx_desc)
222 /* clear next_to_watch to prevent false hangs */
223 tx_buf->next_to_watch = NULL;
225 /* update the statistics for this packet */
226 total_bytes += tx_buf->bytecount;
227 total_packets += tx_buf->gso_segs;
230 dev_kfree_skb_any(tx_buf->skb);
232 /* unmap skb header data */
233 dma_unmap_single(tx_ring->dev,
234 dma_unmap_addr(tx_buf, dma),
235 dma_unmap_len(tx_buf, len),
238 /* clear tx_buffer data */
240 dma_unmap_len_set(tx_buf, len, 0);
242 /* unmap remaining buffers */
243 while (tx_desc != eop_desc) {
250 tx_buf = tx_ring->tx_bi;
251 tx_desc = I40E_TX_DESC(tx_ring, 0);
254 /* unmap any remaining paged data */
255 if (dma_unmap_len(tx_buf, len)) {
256 dma_unmap_page(tx_ring->dev,
257 dma_unmap_addr(tx_buf, dma),
258 dma_unmap_len(tx_buf, len),
260 dma_unmap_len_set(tx_buf, len, 0);
264 /* move us one more past the eop_desc for start of next pkt */
270 tx_buf = tx_ring->tx_bi;
271 tx_desc = I40E_TX_DESC(tx_ring, 0);
274 /* update budget accounting */
276 } while (likely(budget));
279 tx_ring->next_to_clean = i;
280 u64_stats_update_begin(&tx_ring->syncp);
281 tx_ring->stats.bytes += total_bytes;
282 tx_ring->stats.packets += total_packets;
283 u64_stats_update_end(&tx_ring->syncp);
284 tx_ring->q_vector->tx.total_bytes += total_bytes;
285 tx_ring->q_vector->tx.total_packets += total_packets;
287 if (check_for_tx_hang(tx_ring) && i40e_check_tx_hang(tx_ring)) {
288 /* schedule immediate reset if we believe we hung */
289 dev_info(tx_ring->dev, "Detected Tx Unit Hang\n"
292 " next_to_use <%x>\n"
293 " next_to_clean <%x>\n",
295 tx_ring->queue_index,
296 tx_ring->next_to_use, i);
297 dev_info(tx_ring->dev, "tx_bi[next_to_clean]\n"
298 " time_stamp <%lx>\n"
300 tx_ring->tx_bi[i].time_stamp, jiffies);
302 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
304 dev_info(tx_ring->dev,
305 "tx hang detected on queue %d, resetting adapter\n",
306 tx_ring->queue_index);
308 tx_ring->netdev->netdev_ops->ndo_tx_timeout(tx_ring->netdev);
310 /* the adapter is about to reset, no point in enabling stuff */
314 netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
315 tx_ring->queue_index),
316 total_packets, total_bytes);
318 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
319 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
320 (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
321 /* Make sure that anybody stopping the queue after this
322 * sees the new next_to_clean.
325 if (__netif_subqueue_stopped(tx_ring->netdev,
326 tx_ring->queue_index) &&
327 !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
328 netif_wake_subqueue(tx_ring->netdev,
329 tx_ring->queue_index);
330 ++tx_ring->tx_stats.restart_queue;
338 * i40e_set_new_dynamic_itr - Find new ITR level
339 * @rc: structure containing ring performance data
341 * Stores a new ITR value based on packets and byte counts during
342 * the last interrupt. The advantage of per interrupt computation
343 * is faster updates and more accurate ITR for the current traffic
344 * pattern. Constants in this function were computed based on
345 * theoretical maximum wire speed and thresholds were set based on
346 * testing data as well as attempting to minimize response time
347 * while increasing bulk throughput.
349 static void i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
351 enum i40e_latency_range new_latency_range = rc->latency_range;
352 u32 new_itr = rc->itr;
355 if (rc->total_packets == 0 || !rc->itr)
358 /* simple throttlerate management
359 * 0-10MB/s lowest (100000 ints/s)
360 * 10-20MB/s low (20000 ints/s)
361 * 20-1249MB/s bulk (8000 ints/s)
363 bytes_per_int = rc->total_bytes / rc->itr;
365 case I40E_LOWEST_LATENCY:
366 if (bytes_per_int > 10)
367 new_latency_range = I40E_LOW_LATENCY;
369 case I40E_LOW_LATENCY:
370 if (bytes_per_int > 20)
371 new_latency_range = I40E_BULK_LATENCY;
372 else if (bytes_per_int <= 10)
373 new_latency_range = I40E_LOWEST_LATENCY;
375 case I40E_BULK_LATENCY:
376 if (bytes_per_int <= 20)
377 rc->latency_range = I40E_LOW_LATENCY;
381 switch (new_latency_range) {
382 case I40E_LOWEST_LATENCY:
383 new_itr = I40E_ITR_100K;
385 case I40E_LOW_LATENCY:
386 new_itr = I40E_ITR_20K;
388 case I40E_BULK_LATENCY:
389 new_itr = I40E_ITR_8K;
395 if (new_itr != rc->itr) {
396 /* do an exponential smoothing */
397 new_itr = (10 * new_itr * rc->itr) /
398 ((9 * new_itr) + rc->itr);
399 rc->itr = new_itr & I40E_MAX_ITR;
403 rc->total_packets = 0;
407 * i40e_update_dynamic_itr - Adjust ITR based on bytes per int
408 * @q_vector: the vector to adjust
410 static void i40e_update_dynamic_itr(struct i40e_q_vector *q_vector)
412 u16 vector = q_vector->vsi->base_vector + q_vector->v_idx;
413 struct i40e_hw *hw = &q_vector->vsi->back->hw;
417 reg_addr = I40E_VFINT_ITRN1(I40E_RX_ITR, vector - 1);
418 old_itr = q_vector->rx.itr;
419 i40e_set_new_dynamic_itr(&q_vector->rx);
420 if (old_itr != q_vector->rx.itr)
421 wr32(hw, reg_addr, q_vector->rx.itr);
423 reg_addr = I40E_VFINT_ITRN1(I40E_TX_ITR, vector - 1);
424 old_itr = q_vector->tx.itr;
425 i40e_set_new_dynamic_itr(&q_vector->tx);
426 if (old_itr != q_vector->tx.itr)
427 wr32(hw, reg_addr, q_vector->tx.itr);
431 * i40evf_setup_tx_descriptors - Allocate the Tx descriptors
432 * @tx_ring: the tx ring to set up
434 * Return 0 on success, negative on error
436 int i40evf_setup_tx_descriptors(struct i40e_ring *tx_ring)
438 struct device *dev = tx_ring->dev;
444 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
445 tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
449 /* round up to nearest 4K */
450 tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
451 /* add u32 for head writeback, align after this takes care of
452 * guaranteeing this is at least one cache line in size
454 tx_ring->size += sizeof(u32);
455 tx_ring->size = ALIGN(tx_ring->size, 4096);
456 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
457 &tx_ring->dma, GFP_KERNEL);
458 if (!tx_ring->desc) {
459 dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
464 tx_ring->next_to_use = 0;
465 tx_ring->next_to_clean = 0;
469 kfree(tx_ring->tx_bi);
470 tx_ring->tx_bi = NULL;
475 * i40evf_clean_rx_ring - Free Rx buffers
476 * @rx_ring: ring to be cleaned
478 void i40evf_clean_rx_ring(struct i40e_ring *rx_ring)
480 struct device *dev = rx_ring->dev;
481 struct i40e_rx_buffer *rx_bi;
482 unsigned long bi_size;
485 /* ring already cleared, nothing to do */
489 /* Free all the Rx ring sk_buffs */
490 for (i = 0; i < rx_ring->count; i++) {
491 rx_bi = &rx_ring->rx_bi[i];
493 dma_unmap_single(dev,
500 dev_kfree_skb(rx_bi->skb);
504 if (rx_bi->page_dma) {
511 __free_page(rx_bi->page);
513 rx_bi->page_offset = 0;
517 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
518 memset(rx_ring->rx_bi, 0, bi_size);
520 /* Zero out the descriptor ring */
521 memset(rx_ring->desc, 0, rx_ring->size);
523 rx_ring->next_to_clean = 0;
524 rx_ring->next_to_use = 0;
528 * i40evf_free_rx_resources - Free Rx resources
529 * @rx_ring: ring to clean the resources from
531 * Free all receive software resources
533 void i40evf_free_rx_resources(struct i40e_ring *rx_ring)
535 i40evf_clean_rx_ring(rx_ring);
536 kfree(rx_ring->rx_bi);
537 rx_ring->rx_bi = NULL;
540 dma_free_coherent(rx_ring->dev, rx_ring->size,
541 rx_ring->desc, rx_ring->dma);
542 rx_ring->desc = NULL;
547 * i40evf_setup_rx_descriptors - Allocate Rx descriptors
548 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
550 * Returns 0 on success, negative on failure
552 int i40evf_setup_rx_descriptors(struct i40e_ring *rx_ring)
554 struct device *dev = rx_ring->dev;
557 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
558 rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
562 /* Round up to nearest 4K */
563 rx_ring->size = ring_is_16byte_desc_enabled(rx_ring)
564 ? rx_ring->count * sizeof(union i40e_16byte_rx_desc)
565 : rx_ring->count * sizeof(union i40e_32byte_rx_desc);
566 rx_ring->size = ALIGN(rx_ring->size, 4096);
567 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
568 &rx_ring->dma, GFP_KERNEL);
570 if (!rx_ring->desc) {
571 dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
576 rx_ring->next_to_clean = 0;
577 rx_ring->next_to_use = 0;
581 kfree(rx_ring->rx_bi);
582 rx_ring->rx_bi = NULL;
587 * i40e_release_rx_desc - Store the new tail and head values
588 * @rx_ring: ring to bump
589 * @val: new head index
591 static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
593 rx_ring->next_to_use = val;
594 /* Force memory writes to complete before letting h/w
595 * know there are new descriptors to fetch. (Only
596 * applicable for weak-ordered memory model archs,
600 writel(val, rx_ring->tail);
604 * i40evf_alloc_rx_buffers - Replace used receive buffers; packet split
605 * @rx_ring: ring to place buffers on
606 * @cleaned_count: number of buffers to replace
608 void i40evf_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
610 u16 i = rx_ring->next_to_use;
611 union i40e_rx_desc *rx_desc;
612 struct i40e_rx_buffer *bi;
615 /* do nothing if no valid netdev defined */
616 if (!rx_ring->netdev || !cleaned_count)
619 while (cleaned_count--) {
620 rx_desc = I40E_RX_DESC(rx_ring, i);
621 bi = &rx_ring->rx_bi[i];
625 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
626 rx_ring->rx_buf_len);
628 rx_ring->rx_stats.alloc_buff_failed++;
631 /* initialize queue mapping */
632 skb_record_rx_queue(skb, rx_ring->queue_index);
637 bi->dma = dma_map_single(rx_ring->dev,
641 if (dma_mapping_error(rx_ring->dev, bi->dma)) {
642 rx_ring->rx_stats.alloc_buff_failed++;
648 if (ring_is_ps_enabled(rx_ring)) {
650 bi->page = alloc_page(GFP_ATOMIC);
652 rx_ring->rx_stats.alloc_page_failed++;
658 /* use a half page if we're re-using */
659 bi->page_offset ^= PAGE_SIZE / 2;
660 bi->page_dma = dma_map_page(rx_ring->dev,
665 if (dma_mapping_error(rx_ring->dev,
667 rx_ring->rx_stats.alloc_page_failed++;
673 /* Refresh the desc even if buffer_addrs didn't change
674 * because each write-back erases this info.
676 rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
677 rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
679 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
680 rx_desc->read.hdr_addr = 0;
683 if (i == rx_ring->count)
688 if (rx_ring->next_to_use != i)
689 i40e_release_rx_desc(rx_ring, i);
693 * i40e_receive_skb - Send a completed packet up the stack
694 * @rx_ring: rx ring in play
695 * @skb: packet to send up
696 * @vlan_tag: vlan tag for packet
698 static void i40e_receive_skb(struct i40e_ring *rx_ring,
699 struct sk_buff *skb, u16 vlan_tag)
701 struct i40e_q_vector *q_vector = rx_ring->q_vector;
702 struct i40e_vsi *vsi = rx_ring->vsi;
703 u64 flags = vsi->back->flags;
705 if (vlan_tag & VLAN_VID_MASK)
706 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
708 if (flags & I40E_FLAG_IN_NETPOLL)
711 napi_gro_receive(&q_vector->napi, skb);
715 * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
716 * @vsi: the VSI we care about
717 * @skb: skb currently being received and modified
718 * @rx_status: status value of last descriptor in packet
719 * @rx_error: error value of last descriptor in packet
720 * @rx_ptype: ptype value of last descriptor in packet
722 static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
728 bool ipv4_tunnel, ipv6_tunnel;
733 ipv4_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT4_MAC_PAY3) &&
734 (rx_ptype < I40E_RX_PTYPE_GRENAT4_MACVLAN_IPV6_ICMP_PAY4);
735 ipv6_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT6_MAC_PAY3) &&
736 (rx_ptype < I40E_RX_PTYPE_GRENAT6_MACVLAN_IPV6_ICMP_PAY4);
738 skb->encapsulation = ipv4_tunnel || ipv6_tunnel;
739 skb->ip_summed = CHECKSUM_NONE;
741 /* Rx csum enabled and ip headers found? */
742 if (!(vsi->netdev->features & NETIF_F_RXCSUM &&
743 rx_status & (1 << I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
746 /* likely incorrect csum if alternate IP extension headers found */
747 if (rx_status & (1 << I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
750 /* IP or L4 or outmost IP checksum error */
751 if (rx_error & ((1 << I40E_RX_DESC_ERROR_IPE_SHIFT) |
752 (1 << I40E_RX_DESC_ERROR_L4E_SHIFT) |
753 (1 << I40E_RX_DESC_ERROR_EIPE_SHIFT))) {
754 vsi->back->hw_csum_rx_error++;
759 !(rx_status & (1 << I40E_RX_DESC_STATUS_UDP_0_SHIFT))) {
760 /* If VXLAN traffic has an outer UDPv4 checksum we need to check
761 * it in the driver, hardware does not do it for us.
762 * Since L3L4P bit was set we assume a valid IHL value (>=5)
763 * so the total length of IPv4 header is IHL*4 bytes
765 skb->transport_header = skb->mac_header +
766 sizeof(struct ethhdr) +
767 (ip_hdr(skb)->ihl * 4);
769 /* Add 4 bytes for VLAN tagged packets */
770 skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
771 skb->protocol == htons(ETH_P_8021AD))
774 rx_udp_csum = udp_csum(skb);
776 csum = csum_tcpudp_magic(
777 iph->saddr, iph->daddr,
778 (skb->len - skb_transport_offset(skb)),
779 IPPROTO_UDP, rx_udp_csum);
781 if (udp_hdr(skb)->check != csum) {
782 vsi->back->hw_csum_rx_error++;
787 skb->ip_summed = CHECKSUM_UNNECESSARY;
791 * i40e_rx_hash - returns the hash value from the Rx descriptor
792 * @ring: descriptor ring
793 * @rx_desc: specific descriptor
795 static inline u32 i40e_rx_hash(struct i40e_ring *ring,
796 union i40e_rx_desc *rx_desc)
798 const __le64 rss_mask =
799 cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
800 I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
802 if ((ring->netdev->features & NETIF_F_RXHASH) &&
803 (rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask)
804 return le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
810 * i40e_ptype_to_hash - get a hash type
811 * @ptype: the ptype value from the descriptor
813 * Returns a hash type to be used by skb_set_hash
815 static inline enum pkt_hash_types i40e_ptype_to_hash(u8 ptype)
817 struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
820 return PKT_HASH_TYPE_NONE;
822 if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
823 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
824 return PKT_HASH_TYPE_L4;
825 else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
826 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
827 return PKT_HASH_TYPE_L3;
829 return PKT_HASH_TYPE_L2;
833 * i40e_clean_rx_irq - Reclaim resources after receive completes
834 * @rx_ring: rx ring to clean
835 * @budget: how many cleans we're allowed
837 * Returns true if there's any budget left (e.g. the clean is finished)
839 static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
841 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
842 u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
843 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
844 const int current_node = numa_node_id();
845 struct i40e_vsi *vsi = rx_ring->vsi;
846 u16 i = rx_ring->next_to_clean;
847 union i40e_rx_desc *rx_desc;
848 u32 rx_error, rx_status;
852 rx_desc = I40E_RX_DESC(rx_ring, i);
853 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
854 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
855 I40E_RXD_QW1_STATUS_SHIFT;
857 while (rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)) {
858 union i40e_rx_desc *next_rxd;
859 struct i40e_rx_buffer *rx_bi;
862 rx_bi = &rx_ring->rx_bi[i];
866 rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
867 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
868 rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
869 I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
870 rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
871 I40E_RXD_QW1_LENGTH_SPH_SHIFT;
873 rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
874 I40E_RXD_QW1_ERROR_SHIFT;
875 rx_hbo = rx_error & (1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
876 rx_error &= ~(1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
878 rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
879 I40E_RXD_QW1_PTYPE_SHIFT;
882 /* This memory barrier is needed to keep us from reading
883 * any other fields out of the rx_desc until we know the
884 * STATUS_DD bit is set
888 /* Get the header and possibly the whole packet
889 * If this is an skb from previous receive dma will be 0
895 len = I40E_RX_HDR_SIZE;
898 else if (rx_packet_len)
899 len = rx_packet_len; /* 1buf/no split found */
901 len = rx_header_len; /* split always mode */
904 dma_unmap_single(rx_ring->dev,
911 /* Get the rest of the data if this was a header split */
912 if (ring_is_ps_enabled(rx_ring) && rx_packet_len) {
914 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
919 skb->len += rx_packet_len;
920 skb->data_len += rx_packet_len;
921 skb->truesize += rx_packet_len;
923 if ((page_count(rx_bi->page) == 1) &&
924 (page_to_nid(rx_bi->page) == current_node))
925 get_page(rx_bi->page);
929 dma_unmap_page(rx_ring->dev,
935 I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
938 !(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
939 struct i40e_rx_buffer *next_buffer;
941 next_buffer = &rx_ring->rx_bi[i];
943 if (ring_is_ps_enabled(rx_ring)) {
944 rx_bi->skb = next_buffer->skb;
945 rx_bi->dma = next_buffer->dma;
946 next_buffer->skb = skb;
947 next_buffer->dma = 0;
949 rx_ring->rx_stats.non_eop_descs++;
953 /* ERR_MASK will only have valid bits if EOP set */
954 if (unlikely(rx_error & (1 << I40E_RX_DESC_ERROR_RXE_SHIFT))) {
955 dev_kfree_skb_any(skb);
959 skb_set_hash(skb, i40e_rx_hash(rx_ring, rx_desc),
960 i40e_ptype_to_hash(rx_ptype));
961 /* probably a little skewed due to removing CRC */
962 total_rx_bytes += skb->len;
965 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
967 i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
969 vlan_tag = rx_status & (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
970 ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
972 i40e_receive_skb(rx_ring, skb, vlan_tag);
974 rx_ring->netdev->last_rx = jiffies;
977 rx_desc->wb.qword1.status_error_len = 0;
982 /* return some buffers to hardware, one at a time is too slow */
983 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
984 i40evf_alloc_rx_buffers(rx_ring, cleaned_count);
988 /* use prefetched values */
990 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
991 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
992 I40E_RXD_QW1_STATUS_SHIFT;
995 rx_ring->next_to_clean = i;
996 u64_stats_update_begin(&rx_ring->syncp);
997 rx_ring->stats.packets += total_rx_packets;
998 rx_ring->stats.bytes += total_rx_bytes;
999 u64_stats_update_end(&rx_ring->syncp);
1000 rx_ring->q_vector->rx.total_packets += total_rx_packets;
1001 rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
1004 i40evf_alloc_rx_buffers(rx_ring, cleaned_count);
1010 * i40evf_napi_poll - NAPI polling Rx/Tx cleanup routine
1011 * @napi: napi struct with our devices info in it
1012 * @budget: amount of work driver is allowed to do this pass, in packets
1014 * This function will clean all queues associated with a q_vector.
1016 * Returns the amount of work done
1018 int i40evf_napi_poll(struct napi_struct *napi, int budget)
1020 struct i40e_q_vector *q_vector =
1021 container_of(napi, struct i40e_q_vector, napi);
1022 struct i40e_vsi *vsi = q_vector->vsi;
1023 struct i40e_ring *ring;
1024 bool clean_complete = true;
1025 int budget_per_ring;
1027 if (test_bit(__I40E_DOWN, &vsi->state)) {
1028 napi_complete(napi);
1032 /* Since the actual Tx work is minimal, we can give the Tx a larger
1033 * budget and be more aggressive about cleaning up the Tx descriptors.
1035 i40e_for_each_ring(ring, q_vector->tx)
1036 clean_complete &= i40e_clean_tx_irq(ring, vsi->work_limit);
1038 /* We attempt to distribute budget to each Rx queue fairly, but don't
1039 * allow the budget to go below 1 because that would exit polling early.
1041 budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
1043 i40e_for_each_ring(ring, q_vector->rx)
1044 clean_complete &= i40e_clean_rx_irq(ring, budget_per_ring);
1046 /* If work not completed, return budget and polling will return */
1047 if (!clean_complete)
1050 /* Work is done so exit the polling mode and re-enable the interrupt */
1051 napi_complete(napi);
1052 if (ITR_IS_DYNAMIC(vsi->rx_itr_setting) ||
1053 ITR_IS_DYNAMIC(vsi->tx_itr_setting))
1054 i40e_update_dynamic_itr(q_vector);
1056 if (!test_bit(__I40E_DOWN, &vsi->state))
1057 i40evf_irq_enable_queues(vsi->back, 1 << q_vector->v_idx);
1063 * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
1065 * @tx_ring: ring to send buffer on
1066 * @flags: the tx flags to be set
1068 * Checks the skb and set up correspondingly several generic transmit flags
1069 * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
1071 * Returns error code indicate the frame should be dropped upon error and the
1072 * otherwise returns 0 to indicate the flags has been set properly.
1074 static int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
1075 struct i40e_ring *tx_ring,
1078 __be16 protocol = skb->protocol;
1081 /* if we have a HW VLAN tag being added, default to the HW one */
1082 if (vlan_tx_tag_present(skb)) {
1083 tx_flags |= vlan_tx_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
1084 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1085 /* else if it is a SW VLAN, check the next protocol and store the tag */
1086 } else if (protocol == htons(ETH_P_8021Q)) {
1087 struct vlan_hdr *vhdr, _vhdr;
1088 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
1092 protocol = vhdr->h_vlan_encapsulated_proto;
1093 tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
1094 tx_flags |= I40E_TX_FLAGS_SW_VLAN;
1102 * i40e_tso - set up the tso context descriptor
1103 * @tx_ring: ptr to the ring to send
1104 * @skb: ptr to the skb we're sending
1105 * @tx_flags: the collected send information
1106 * @protocol: the send protocol
1107 * @hdr_len: ptr to the size of the packet header
1108 * @cd_tunneling: ptr to context descriptor bits
1110 * Returns 0 if no TSO can happen, 1 if tso is going, or error
1112 static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
1113 u32 tx_flags, __be16 protocol, u8 *hdr_len,
1114 u64 *cd_type_cmd_tso_mss, u32 *cd_tunneling)
1116 u32 cd_cmd, cd_tso_len, cd_mss;
1117 struct tcphdr *tcph;
1121 struct ipv6hdr *ipv6h;
1123 if (!skb_is_gso(skb))
1126 if (skb_header_cloned(skb)) {
1127 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1132 if (protocol == htons(ETH_P_IP)) {
1133 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
1134 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1137 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1139 } else if (skb_is_gso_v6(skb)) {
1141 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb)
1143 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1144 ipv6h->payload_len = 0;
1145 tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
1149 l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
1150 *hdr_len = (skb->encapsulation
1151 ? (skb_inner_transport_header(skb) - skb->data)
1152 : skb_transport_offset(skb)) + l4len;
1154 /* find the field values */
1155 cd_cmd = I40E_TX_CTX_DESC_TSO;
1156 cd_tso_len = skb->len - *hdr_len;
1157 cd_mss = skb_shinfo(skb)->gso_size;
1158 *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
1160 I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
1161 ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
1166 * i40e_tx_enable_csum - Enable Tx checksum offloads
1168 * @tx_flags: Tx flags currently set
1169 * @td_cmd: Tx descriptor command bits to set
1170 * @td_offset: Tx descriptor header offsets to set
1171 * @cd_tunneling: ptr to context desc bits
1173 static void i40e_tx_enable_csum(struct sk_buff *skb, u32 tx_flags,
1174 u32 *td_cmd, u32 *td_offset,
1175 struct i40e_ring *tx_ring,
1178 struct ipv6hdr *this_ipv6_hdr;
1179 unsigned int this_tcp_hdrlen;
1180 struct iphdr *this_ip_hdr;
1181 u32 network_hdr_len;
1184 if (skb->encapsulation) {
1185 network_hdr_len = skb_inner_network_header_len(skb);
1186 this_ip_hdr = inner_ip_hdr(skb);
1187 this_ipv6_hdr = inner_ipv6_hdr(skb);
1188 this_tcp_hdrlen = inner_tcp_hdrlen(skb);
1190 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1192 if (tx_flags & I40E_TX_FLAGS_TSO) {
1193 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
1194 ip_hdr(skb)->check = 0;
1197 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1199 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1200 if (tx_flags & I40E_TX_FLAGS_TSO) {
1201 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
1202 ip_hdr(skb)->check = 0;
1205 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1209 /* Now set the ctx descriptor fields */
1210 *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
1211 I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
1212 I40E_TXD_CTX_UDP_TUNNELING |
1213 ((skb_inner_network_offset(skb) -
1214 skb_transport_offset(skb)) >> 1) <<
1215 I40E_TXD_CTX_QW0_NATLEN_SHIFT;
1218 network_hdr_len = skb_network_header_len(skb);
1219 this_ip_hdr = ip_hdr(skb);
1220 this_ipv6_hdr = ipv6_hdr(skb);
1221 this_tcp_hdrlen = tcp_hdrlen(skb);
1224 /* Enable IP checksum offloads */
1225 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1226 l4_hdr = this_ip_hdr->protocol;
1227 /* the stack computes the IP header already, the only time we
1228 * need the hardware to recompute it is in the case of TSO.
1230 if (tx_flags & I40E_TX_FLAGS_TSO) {
1231 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
1232 this_ip_hdr->check = 0;
1234 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
1236 /* Now set the td_offset for IP header length */
1237 *td_offset = (network_hdr_len >> 2) <<
1238 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1239 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1240 l4_hdr = this_ipv6_hdr->nexthdr;
1241 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
1242 /* Now set the td_offset for IP header length */
1243 *td_offset = (network_hdr_len >> 2) <<
1244 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1246 /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
1247 *td_offset |= (skb_network_offset(skb) >> 1) <<
1248 I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
1250 /* Enable L4 checksum offloads */
1253 /* enable checksum offloads */
1254 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
1255 *td_offset |= (this_tcp_hdrlen >> 2) <<
1256 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1259 /* enable SCTP checksum offload */
1260 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
1261 *td_offset |= (sizeof(struct sctphdr) >> 2) <<
1262 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1265 /* enable UDP checksum offload */
1266 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
1267 *td_offset |= (sizeof(struct udphdr) >> 2) <<
1268 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1276 * i40e_create_tx_ctx Build the Tx context descriptor
1277 * @tx_ring: ring to create the descriptor on
1278 * @cd_type_cmd_tso_mss: Quad Word 1
1279 * @cd_tunneling: Quad Word 0 - bits 0-31
1280 * @cd_l2tag2: Quad Word 0 - bits 32-63
1282 static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
1283 const u64 cd_type_cmd_tso_mss,
1284 const u32 cd_tunneling, const u32 cd_l2tag2)
1286 struct i40e_tx_context_desc *context_desc;
1287 int i = tx_ring->next_to_use;
1289 if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
1290 !cd_tunneling && !cd_l2tag2)
1293 /* grab the next descriptor */
1294 context_desc = I40E_TX_CTXTDESC(tx_ring, i);
1297 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
1299 /* cpu_to_le32 and assign to struct fields */
1300 context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
1301 context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
1302 context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
1306 * i40e_tx_map - Build the Tx descriptor
1307 * @tx_ring: ring to send buffer on
1309 * @first: first buffer info buffer to use
1310 * @tx_flags: collected send information
1311 * @hdr_len: size of the packet header
1312 * @td_cmd: the command field in the descriptor
1313 * @td_offset: offset for checksum or crc
1315 static void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
1316 struct i40e_tx_buffer *first, u32 tx_flags,
1317 const u8 hdr_len, u32 td_cmd, u32 td_offset)
1319 unsigned int data_len = skb->data_len;
1320 unsigned int size = skb_headlen(skb);
1321 struct skb_frag_struct *frag;
1322 struct i40e_tx_buffer *tx_bi;
1323 struct i40e_tx_desc *tx_desc;
1324 u16 i = tx_ring->next_to_use;
1329 if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
1330 td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
1331 td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
1332 I40E_TX_FLAGS_VLAN_SHIFT;
1335 if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
1336 gso_segs = skb_shinfo(skb)->gso_segs;
1340 /* multiply data chunks by size of headers */
1341 first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
1342 first->gso_segs = gso_segs;
1344 first->tx_flags = tx_flags;
1346 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1348 tx_desc = I40E_TX_DESC(tx_ring, i);
1351 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1352 if (dma_mapping_error(tx_ring->dev, dma))
1355 /* record length, and DMA address */
1356 dma_unmap_len_set(tx_bi, len, size);
1357 dma_unmap_addr_set(tx_bi, dma, dma);
1359 tx_desc->buffer_addr = cpu_to_le64(dma);
1361 while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
1362 tx_desc->cmd_type_offset_bsz =
1363 build_ctob(td_cmd, td_offset,
1364 I40E_MAX_DATA_PER_TXD, td_tag);
1368 if (i == tx_ring->count) {
1369 tx_desc = I40E_TX_DESC(tx_ring, 0);
1373 dma += I40E_MAX_DATA_PER_TXD;
1374 size -= I40E_MAX_DATA_PER_TXD;
1376 tx_desc->buffer_addr = cpu_to_le64(dma);
1379 if (likely(!data_len))
1382 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
1387 if (i == tx_ring->count) {
1388 tx_desc = I40E_TX_DESC(tx_ring, 0);
1392 size = skb_frag_size(frag);
1395 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
1398 tx_bi = &tx_ring->tx_bi[i];
1401 /* Place RS bit on last descriptor of any packet that spans across the
1402 * 4th descriptor (WB_STRIDE aka 0x3) in a 64B cacheline.
1404 #define WB_STRIDE 0x3
1405 if (((i & WB_STRIDE) != WB_STRIDE) &&
1406 (first <= &tx_ring->tx_bi[i]) &&
1407 (first >= &tx_ring->tx_bi[i & ~WB_STRIDE])) {
1408 tx_desc->cmd_type_offset_bsz =
1409 build_ctob(td_cmd, td_offset, size, td_tag) |
1410 cpu_to_le64((u64)I40E_TX_DESC_CMD_EOP <<
1411 I40E_TXD_QW1_CMD_SHIFT);
1413 tx_desc->cmd_type_offset_bsz =
1414 build_ctob(td_cmd, td_offset, size, td_tag) |
1415 cpu_to_le64((u64)I40E_TXD_CMD <<
1416 I40E_TXD_QW1_CMD_SHIFT);
1419 netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
1420 tx_ring->queue_index),
1423 /* set the timestamp */
1424 first->time_stamp = jiffies;
1426 /* Force memory writes to complete before letting h/w
1427 * know there are new descriptors to fetch. (Only
1428 * applicable for weak-ordered memory model archs,
1433 /* set next_to_watch value indicating a packet is present */
1434 first->next_to_watch = tx_desc;
1437 if (i == tx_ring->count)
1440 tx_ring->next_to_use = i;
1442 /* notify HW of packet */
1443 writel(i, tx_ring->tail);
1448 dev_info(tx_ring->dev, "TX DMA map failed\n");
1450 /* clear dma mappings for failed tx_bi map */
1452 tx_bi = &tx_ring->tx_bi[i];
1453 i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
1461 tx_ring->next_to_use = i;
1465 * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
1466 * @tx_ring: the ring to be checked
1467 * @size: the size buffer we want to assure is available
1469 * Returns -EBUSY if a stop is needed, else 0
1471 static inline int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
1473 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
1474 /* Memory barrier before checking head and tail */
1477 /* Check again in a case another CPU has just made room available. */
1478 if (likely(I40E_DESC_UNUSED(tx_ring) < size))
1481 /* A reprieve! - use start_queue because it doesn't call schedule */
1482 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
1483 ++tx_ring->tx_stats.restart_queue;
1488 * i40e_maybe_stop_tx - 1st level check for tx stop conditions
1489 * @tx_ring: the ring to be checked
1490 * @size: the size buffer we want to assure is available
1492 * Returns 0 if stop is not needed
1494 static int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
1496 if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
1498 return __i40e_maybe_stop_tx(tx_ring, size);
1502 * i40e_xmit_descriptor_count - calculate number of tx descriptors needed
1504 * @tx_ring: ring to send buffer on
1506 * Returns number of data descriptors needed for this skb. Returns 0 to indicate
1507 * there is not enough descriptors available in this ring since we need at least
1510 static int i40e_xmit_descriptor_count(struct sk_buff *skb,
1511 struct i40e_ring *tx_ring)
1513 #if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
1518 /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
1519 * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
1520 * + 4 desc gap to avoid the cache line where head is,
1521 * + 1 desc for context descriptor,
1522 * otherwise try next time
1524 #if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
1525 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1526 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
1528 count += skb_shinfo(skb)->nr_frags;
1530 count += TXD_USE_COUNT(skb_headlen(skb));
1531 if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
1532 tx_ring->tx_stats.tx_busy++;
1539 * i40e_xmit_frame_ring - Sends buffer on Tx ring
1541 * @tx_ring: ring to send buffer on
1543 * Returns NETDEV_TX_OK if sent, else an error code
1545 static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
1546 struct i40e_ring *tx_ring)
1548 u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
1549 u32 cd_tunneling = 0, cd_l2tag2 = 0;
1550 struct i40e_tx_buffer *first;
1557 if (0 == i40e_xmit_descriptor_count(skb, tx_ring))
1558 return NETDEV_TX_BUSY;
1560 /* prepare the xmit flags */
1561 if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
1564 /* obtain protocol of skb */
1565 protocol = skb->protocol;
1567 /* record the location of the first descriptor for this packet */
1568 first = &tx_ring->tx_bi[tx_ring->next_to_use];
1570 /* setup IPv4/IPv6 offloads */
1571 if (protocol == htons(ETH_P_IP))
1572 tx_flags |= I40E_TX_FLAGS_IPV4;
1573 else if (protocol == htons(ETH_P_IPV6))
1574 tx_flags |= I40E_TX_FLAGS_IPV6;
1576 tso = i40e_tso(tx_ring, skb, tx_flags, protocol, &hdr_len,
1577 &cd_type_cmd_tso_mss, &cd_tunneling);
1582 tx_flags |= I40E_TX_FLAGS_TSO;
1584 skb_tx_timestamp(skb);
1586 /* always enable CRC insertion offload */
1587 td_cmd |= I40E_TX_DESC_CMD_ICRC;
1589 /* Always offload the checksum, since it's in the data descriptor */
1590 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1591 tx_flags |= I40E_TX_FLAGS_CSUM;
1593 i40e_tx_enable_csum(skb, tx_flags, &td_cmd, &td_offset,
1594 tx_ring, &cd_tunneling);
1597 i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
1598 cd_tunneling, cd_l2tag2);
1600 i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
1603 i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
1605 return NETDEV_TX_OK;
1608 dev_kfree_skb_any(skb);
1609 return NETDEV_TX_OK;
1613 * i40evf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
1615 * @netdev: network interface device structure
1617 * Returns NETDEV_TX_OK if sent, else an error code
1619 netdev_tx_t i40evf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1621 struct i40evf_adapter *adapter = netdev_priv(netdev);
1622 struct i40e_ring *tx_ring = adapter->tx_rings[skb->queue_mapping];
1624 /* hardware can't handle really short frames, hardware padding works
1627 if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
1628 if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
1629 return NETDEV_TX_OK;
1630 skb->len = I40E_MIN_TX_LEN;
1631 skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
1634 return i40e_xmit_frame_ring(skb, tx_ring);