blob: 37bd4e50ccde06fde388adc3098926066793405e [file] [log] [blame]
Jeff Kirsherae06c702018-03-22 10:08:48 -07001// SPDX-License-Identifier: GPL-2.0
Jeff Kirsher51dce242018-04-26 08:08:09 -07002/* Copyright(c) 2013 - 2018 Intel Corporation. */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003
Mitch Williams1c112a62014-04-04 04:43:06 +00004#include <linux/prefetch.h>
Mitch Williamsa132af22015-01-24 09:58:35 +00005#include <net/busy_poll.h>
Björn Töpel0c8493d2017-05-24 07:55:34 +02006#include <linux/bpf_trace.h>
Jesper Dangaard Brouer87128822018-01-03 11:25:23 +01007#include <net/xdp.h>
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00008#include "i40e.h"
Scott Petersoned0980c2017-04-13 04:45:44 -04009#include "i40e_trace.h"
Jesse Brandeburg206812b2014-02-12 01:45:33 +000010#include "i40e_prototype.h"
Björn Töpel20a739d2018-08-28 14:44:31 +020011#include "i40e_txrx_common.h"
Björn Töpel0a714182018-08-28 14:44:32 +020012#include "i40e_xsk.h"
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000013
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000014#define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
Alexander Duyck5e02f282016-09-12 14:18:41 -070015/**
16 * i40e_fdir - Generate a Flow Director descriptor based on fdata
17 * @tx_ring: Tx ring to send buffer on
18 * @fdata: Flow director filter data
19 * @add: Indicate if we are adding a rule or deleting one
20 *
21 **/
22static void i40e_fdir(struct i40e_ring *tx_ring,
23 struct i40e_fdir_filter *fdata, bool add)
24{
25 struct i40e_filter_program_desc *fdir_desc;
26 struct i40e_pf *pf = tx_ring->vsi->back;
27 u32 flex_ptype, dtype_cmd;
28 u16 i;
29
30 /* grab the next descriptor */
31 i = tx_ring->next_to_use;
32 fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
33
34 i++;
35 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
36
37 flex_ptype = I40E_TXD_FLTR_QW0_QINDEX_MASK &
38 (fdata->q_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT);
39
40 flex_ptype |= I40E_TXD_FLTR_QW0_FLEXOFF_MASK &
41 (fdata->flex_off << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT);
42
43 flex_ptype |= I40E_TXD_FLTR_QW0_PCTYPE_MASK &
44 (fdata->pctype << I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
45
Jacob Keller0e588de2017-02-06 14:38:50 -080046 flex_ptype |= I40E_TXD_FLTR_QW0_PCTYPE_MASK &
47 (fdata->flex_offset << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT);
48
Alexander Duyck5e02f282016-09-12 14:18:41 -070049 /* Use LAN VSI Id if not programmed by user */
50 flex_ptype |= I40E_TXD_FLTR_QW0_DEST_VSI_MASK &
51 ((u32)(fdata->dest_vsi ? : pf->vsi[pf->lan_vsi]->id) <<
52 I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT);
53
54 dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
55
56 dtype_cmd |= add ?
57 I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
58 I40E_TXD_FLTR_QW1_PCMD_SHIFT :
59 I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
60 I40E_TXD_FLTR_QW1_PCMD_SHIFT;
61
62 dtype_cmd |= I40E_TXD_FLTR_QW1_DEST_MASK &
63 (fdata->dest_ctl << I40E_TXD_FLTR_QW1_DEST_SHIFT);
64
65 dtype_cmd |= I40E_TXD_FLTR_QW1_FD_STATUS_MASK &
66 (fdata->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT);
67
68 if (fdata->cnt_index) {
69 dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
70 dtype_cmd |= I40E_TXD_FLTR_QW1_CNTINDEX_MASK &
71 ((u32)fdata->cnt_index <<
72 I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT);
73 }
74
75 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
76 fdir_desc->rsvd = cpu_to_le32(0);
77 fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
78 fdir_desc->fd_id = cpu_to_le32(fdata->fd_id);
79}
80
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +000081#define I40E_FD_CLEAN_DELAY 10
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000082/**
83 * i40e_program_fdir_filter - Program a Flow Director filter
Joseph Gasparakis17a73f62014-02-12 01:45:30 +000084 * @fdir_data: Packet data that will be filter parameters
85 * @raw_packet: the pre-allocated packet buffer for FDir
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000086 * @pf: The PF pointer
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000087 * @add: True for add/update, False for remove
88 **/
Alexander Duyck1eb846a2016-09-12 14:18:42 -070089static int i40e_program_fdir_filter(struct i40e_fdir_filter *fdir_data,
90 u8 *raw_packet, struct i40e_pf *pf,
91 bool add)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000092{
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +000093 struct i40e_tx_buffer *tx_buf, *first;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000094 struct i40e_tx_desc *tx_desc;
95 struct i40e_ring *tx_ring;
96 struct i40e_vsi *vsi;
97 struct device *dev;
98 dma_addr_t dma;
99 u32 td_cmd = 0;
100 u16 i;
101
102 /* find existing FDIR VSI */
Alexander Duyck4b816442016-10-11 15:26:53 -0700103 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000104 if (!vsi)
105 return -ENOENT;
106
Alexander Duyck9f65e152013-09-28 06:00:58 +0000107 tx_ring = vsi->tx_rings[0];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000108 dev = tx_ring->dev;
109
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000110 /* we need two descriptors to add/del a filter and we can wait */
Alexander Duycked245402016-09-14 16:24:32 -0700111 for (i = I40E_FD_CLEAN_DELAY; I40E_DESC_UNUSED(tx_ring) < 2; i--) {
112 if (!i)
113 return -EAGAIN;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000114 msleep_interruptible(1);
Alexander Duycked245402016-09-14 16:24:32 -0700115 }
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000116
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000117 dma = dma_map_single(dev, raw_packet,
118 I40E_FDIR_MAX_RAW_PACKET_SIZE, DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000119 if (dma_mapping_error(dev, dma))
120 goto dma_fail;
121
122 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000123 i = tx_ring->next_to_use;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000124 first = &tx_ring->tx_bi[i];
Alexander Duyck5e02f282016-09-12 14:18:41 -0700125 i40e_fdir(tx_ring, fdir_data, add);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000126
127 /* Now program a dummy descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000128 i = tx_ring->next_to_use;
129 tx_desc = I40E_TX_DESC(tx_ring, i);
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000130 tx_buf = &tx_ring->tx_bi[i];
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000131
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000132 tx_ring->next_to_use = ((i + 1) < tx_ring->count) ? i + 1 : 0;
133
134 memset(tx_buf, 0, sizeof(struct i40e_tx_buffer));
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000135
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000136 /* record length, and DMA address */
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000137 dma_unmap_len_set(tx_buf, len, I40E_FDIR_MAX_RAW_PACKET_SIZE);
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000138 dma_unmap_addr_set(tx_buf, dma, dma);
139
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000140 tx_desc->buffer_addr = cpu_to_le64(dma);
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000141 td_cmd = I40E_TXD_CMD | I40E_TX_DESC_CMD_DUMMY;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000142
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000143 tx_buf->tx_flags = I40E_TX_FLAGS_FD_SB;
144 tx_buf->raw_buf = (void *)raw_packet;
145
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000146 tx_desc->cmd_type_offset_bsz =
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000147 build_ctob(td_cmd, 0, I40E_FDIR_MAX_RAW_PACKET_SIZE, 0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000148
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000149 /* Force memory writes to complete before letting h/w
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000150 * know there are new descriptors to fetch.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000151 */
152 wmb();
153
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000154 /* Mark the data descriptor to be watched */
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000155 first->next_to_watch = tx_desc;
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000156
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000157 writel(tx_ring->next_to_use, tx_ring->tail);
158 return 0;
159
160dma_fail:
161 return -1;
162}
163
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000164#define IP_HEADER_OFFSET 14
165#define I40E_UDPIP_DUMMY_PACKET_LEN 42
166/**
167 * i40e_add_del_fdir_udpv4 - Add/Remove UDPv4 filters
168 * @vsi: pointer to the targeted VSI
169 * @fd_data: the flow director data required for the FDir descriptor
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000170 * @add: true adds a filter, false removes it
171 *
172 * Returns 0 if the filters were successfully added or removed
173 **/
174static int i40e_add_del_fdir_udpv4(struct i40e_vsi *vsi,
175 struct i40e_fdir_filter *fd_data,
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000176 bool add)
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000177{
178 struct i40e_pf *pf = vsi->back;
179 struct udphdr *udp;
180 struct iphdr *ip;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000181 u8 *raw_packet;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000182 int ret;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000183 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
184 0x45, 0, 0, 0x1c, 0, 0, 0x40, 0, 0x40, 0x11, 0, 0, 0, 0, 0, 0,
185 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
186
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000187 raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
188 if (!raw_packet)
189 return -ENOMEM;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000190 memcpy(raw_packet, packet, I40E_UDPIP_DUMMY_PACKET_LEN);
191
192 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
193 udp = (struct udphdr *)(raw_packet + IP_HEADER_OFFSET
194 + sizeof(struct iphdr));
195
Jacob Keller8ce43dc2017-02-06 14:38:39 -0800196 ip->daddr = fd_data->dst_ip;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000197 udp->dest = fd_data->dst_port;
Jacob Keller8ce43dc2017-02-06 14:38:39 -0800198 ip->saddr = fd_data->src_ip;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000199 udp->source = fd_data->src_port;
200
Jacob Keller0e588de2017-02-06 14:38:50 -0800201 if (fd_data->flex_filter) {
202 u8 *payload = raw_packet + I40E_UDPIP_DUMMY_PACKET_LEN;
203 __be16 pattern = fd_data->flex_word;
204 u16 off = fd_data->flex_offset;
205
206 *((__force __be16 *)(payload + off)) = pattern;
207 }
208
Kevin Scottb2d36c02014-04-09 05:58:59 +0000209 fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
210 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
211 if (ret) {
212 dev_info(&pf->pdev->dev,
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000213 "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
214 fd_data->pctype, fd_data->fd_id, ret);
Jacob Kellere5187ee2017-02-06 14:38:41 -0800215 /* Free the packet buffer since it wasn't added to the ring */
216 kfree(raw_packet);
217 return -EOPNOTSUPP;
Anjali Singhai Jain4205d372015-02-27 09:15:27 +0000218 } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
Anjali Singhai Jainf7233c52014-07-09 07:46:16 +0000219 if (add)
220 dev_info(&pf->pdev->dev,
221 "Filter OK for PCTYPE %d loc = %d\n",
222 fd_data->pctype, fd_data->fd_id);
223 else
224 dev_info(&pf->pdev->dev,
225 "Filter deleted for PCTYPE %d loc = %d\n",
226 fd_data->pctype, fd_data->fd_id);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000227 }
Kiran Patila42e7a32015-11-06 15:26:03 -0800228
Jacob Keller097dbf52017-02-06 14:38:46 -0800229 if (add)
230 pf->fd_udp4_filter_cnt++;
231 else
232 pf->fd_udp4_filter_cnt--;
233
Jacob Kellere5187ee2017-02-06 14:38:41 -0800234 return 0;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000235}
236
237#define I40E_TCPIP_DUMMY_PACKET_LEN 54
238/**
239 * i40e_add_del_fdir_tcpv4 - Add/Remove TCPv4 filters
240 * @vsi: pointer to the targeted VSI
241 * @fd_data: the flow director data required for the FDir descriptor
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000242 * @add: true adds a filter, false removes it
243 *
244 * Returns 0 if the filters were successfully added or removed
245 **/
246static int i40e_add_del_fdir_tcpv4(struct i40e_vsi *vsi,
247 struct i40e_fdir_filter *fd_data,
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000248 bool add)
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000249{
250 struct i40e_pf *pf = vsi->back;
251 struct tcphdr *tcp;
252 struct iphdr *ip;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000253 u8 *raw_packet;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000254 int ret;
255 /* Dummy packet */
256 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
257 0x45, 0, 0, 0x28, 0, 0, 0x40, 0, 0x40, 0x6, 0, 0, 0, 0, 0, 0,
258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0x11,
259 0x0, 0x72, 0, 0, 0, 0};
260
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000261 raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
262 if (!raw_packet)
263 return -ENOMEM;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000264 memcpy(raw_packet, packet, I40E_TCPIP_DUMMY_PACKET_LEN);
265
266 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
267 tcp = (struct tcphdr *)(raw_packet + IP_HEADER_OFFSET
268 + sizeof(struct iphdr));
269
Jacob Keller8ce43dc2017-02-06 14:38:39 -0800270 ip->daddr = fd_data->dst_ip;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000271 tcp->dest = fd_data->dst_port;
Jacob Keller8ce43dc2017-02-06 14:38:39 -0800272 ip->saddr = fd_data->src_ip;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000273 tcp->source = fd_data->src_port;
274
Jacob Keller0e588de2017-02-06 14:38:50 -0800275 if (fd_data->flex_filter) {
276 u8 *payload = raw_packet + I40E_TCPIP_DUMMY_PACKET_LEN;
277 __be16 pattern = fd_data->flex_word;
278 u16 off = fd_data->flex_offset;
279
280 *((__force __be16 *)(payload + off)) = pattern;
281 }
282
Kevin Scottb2d36c02014-04-09 05:58:59 +0000283 fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000284 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000285 if (ret) {
286 dev_info(&pf->pdev->dev,
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000287 "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
288 fd_data->pctype, fd_data->fd_id, ret);
Jacob Kellere5187ee2017-02-06 14:38:41 -0800289 /* Free the packet buffer since it wasn't added to the ring */
290 kfree(raw_packet);
291 return -EOPNOTSUPP;
Anjali Singhai Jain4205d372015-02-27 09:15:27 +0000292 } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
Anjali Singhai Jainf7233c52014-07-09 07:46:16 +0000293 if (add)
294 dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d loc = %d)\n",
295 fd_data->pctype, fd_data->fd_id);
296 else
297 dev_info(&pf->pdev->dev,
298 "Filter deleted for PCTYPE %d loc = %d\n",
299 fd_data->pctype, fd_data->fd_id);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000300 }
301
Jacob Keller377cc242017-02-06 14:38:42 -0800302 if (add) {
Jacob Keller097dbf52017-02-06 14:38:46 -0800303 pf->fd_tcp4_filter_cnt++;
Jacob Keller377cc242017-02-06 14:38:42 -0800304 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
305 I40E_DEBUG_FD & pf->hw.debug_mask)
306 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 flow being applied\n");
Jacob Keller134201a2018-03-16 01:26:32 -0700307 set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
Jacob Keller377cc242017-02-06 14:38:42 -0800308 } else {
Jacob Keller097dbf52017-02-06 14:38:46 -0800309 pf->fd_tcp4_filter_cnt--;
Jacob Keller377cc242017-02-06 14:38:42 -0800310 }
311
Jacob Kellere5187ee2017-02-06 14:38:41 -0800312 return 0;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000313}
314
Jacob Kellerf223c872017-02-06 14:38:51 -0800315#define I40E_SCTPIP_DUMMY_PACKET_LEN 46
316/**
317 * i40e_add_del_fdir_sctpv4 - Add/Remove SCTPv4 Flow Director filters for
318 * a specific flow spec
319 * @vsi: pointer to the targeted VSI
320 * @fd_data: the flow director data required for the FDir descriptor
321 * @add: true adds a filter, false removes it
322 *
323 * Returns 0 if the filters were successfully added or removed
324 **/
325static int i40e_add_del_fdir_sctpv4(struct i40e_vsi *vsi,
326 struct i40e_fdir_filter *fd_data,
327 bool add)
328{
329 struct i40e_pf *pf = vsi->back;
330 struct sctphdr *sctp;
331 struct iphdr *ip;
332 u8 *raw_packet;
333 int ret;
334 /* Dummy packet */
335 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
336 0x45, 0, 0, 0x20, 0, 0, 0x40, 0, 0x40, 0x84, 0, 0, 0, 0, 0, 0,
337 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
338
339 raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
340 if (!raw_packet)
341 return -ENOMEM;
342 memcpy(raw_packet, packet, I40E_SCTPIP_DUMMY_PACKET_LEN);
343
344 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
345 sctp = (struct sctphdr *)(raw_packet + IP_HEADER_OFFSET
346 + sizeof(struct iphdr));
347
348 ip->daddr = fd_data->dst_ip;
349 sctp->dest = fd_data->dst_port;
350 ip->saddr = fd_data->src_ip;
351 sctp->source = fd_data->src_port;
352
353 if (fd_data->flex_filter) {
354 u8 *payload = raw_packet + I40E_SCTPIP_DUMMY_PACKET_LEN;
355 __be16 pattern = fd_data->flex_word;
356 u16 off = fd_data->flex_offset;
357
358 *((__force __be16 *)(payload + off)) = pattern;
359 }
360
361 fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
362 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
363 if (ret) {
364 dev_info(&pf->pdev->dev,
365 "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
366 fd_data->pctype, fd_data->fd_id, ret);
367 /* Free the packet buffer since it wasn't added to the ring */
368 kfree(raw_packet);
369 return -EOPNOTSUPP;
370 } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
371 if (add)
372 dev_info(&pf->pdev->dev,
373 "Filter OK for PCTYPE %d loc = %d\n",
374 fd_data->pctype, fd_data->fd_id);
375 else
376 dev_info(&pf->pdev->dev,
377 "Filter deleted for PCTYPE %d loc = %d\n",
378 fd_data->pctype, fd_data->fd_id);
379 }
380
381 if (add)
382 pf->fd_sctp4_filter_cnt++;
383 else
384 pf->fd_sctp4_filter_cnt--;
385
386 return 0;
387}
388
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000389#define I40E_IP_DUMMY_PACKET_LEN 34
390/**
391 * i40e_add_del_fdir_ipv4 - Add/Remove IPv4 Flow Director filters for
392 * a specific flow spec
393 * @vsi: pointer to the targeted VSI
394 * @fd_data: the flow director data required for the FDir descriptor
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000395 * @add: true adds a filter, false removes it
396 *
397 * Returns 0 if the filters were successfully added or removed
398 **/
399static int i40e_add_del_fdir_ipv4(struct i40e_vsi *vsi,
400 struct i40e_fdir_filter *fd_data,
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000401 bool add)
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000402{
403 struct i40e_pf *pf = vsi->back;
404 struct iphdr *ip;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000405 u8 *raw_packet;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000406 int ret;
407 int i;
408 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
409 0x45, 0, 0, 0x14, 0, 0, 0x40, 0, 0x40, 0x10, 0, 0, 0, 0, 0, 0,
410 0, 0, 0, 0};
411
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000412 for (i = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
413 i <= I40E_FILTER_PCTYPE_FRAG_IPV4; i++) {
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000414 raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
415 if (!raw_packet)
416 return -ENOMEM;
417 memcpy(raw_packet, packet, I40E_IP_DUMMY_PACKET_LEN);
418 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
419
Jacob Keller8ce43dc2017-02-06 14:38:39 -0800420 ip->saddr = fd_data->src_ip;
421 ip->daddr = fd_data->dst_ip;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000422 ip->protocol = 0;
423
Jacob Keller0e588de2017-02-06 14:38:50 -0800424 if (fd_data->flex_filter) {
425 u8 *payload = raw_packet + I40E_IP_DUMMY_PACKET_LEN;
426 __be16 pattern = fd_data->flex_word;
427 u16 off = fd_data->flex_offset;
428
429 *((__force __be16 *)(payload + off)) = pattern;
430 }
431
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000432 fd_data->pctype = i;
433 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000434 if (ret) {
435 dev_info(&pf->pdev->dev,
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000436 "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
437 fd_data->pctype, fd_data->fd_id, ret);
Jacob Kellere5187ee2017-02-06 14:38:41 -0800438 /* The packet buffer wasn't added to the ring so we
439 * need to free it now.
440 */
441 kfree(raw_packet);
442 return -EOPNOTSUPP;
Anjali Singhai Jain4205d372015-02-27 09:15:27 +0000443 } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
Anjali Singhai Jainf7233c52014-07-09 07:46:16 +0000444 if (add)
445 dev_info(&pf->pdev->dev,
446 "Filter OK for PCTYPE %d loc = %d\n",
447 fd_data->pctype, fd_data->fd_id);
448 else
449 dev_info(&pf->pdev->dev,
450 "Filter deleted for PCTYPE %d loc = %d\n",
451 fd_data->pctype, fd_data->fd_id);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000452 }
453 }
454
Jacob Keller097dbf52017-02-06 14:38:46 -0800455 if (add)
456 pf->fd_ip4_filter_cnt++;
457 else
458 pf->fd_ip4_filter_cnt--;
459
Jacob Kellere5187ee2017-02-06 14:38:41 -0800460 return 0;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000461}
462
463/**
464 * i40e_add_del_fdir - Build raw packets to add/del fdir filter
465 * @vsi: pointer to the targeted VSI
Jacob Kellerf5254422018-04-20 01:41:33 -0700466 * @input: filter to add or delete
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000467 * @add: true adds a filter, false removes it
468 *
469 **/
470int i40e_add_del_fdir(struct i40e_vsi *vsi,
471 struct i40e_fdir_filter *input, bool add)
472{
473 struct i40e_pf *pf = vsi->back;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000474 int ret;
475
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000476 switch (input->flow_type & ~FLOW_EXT) {
477 case TCP_V4_FLOW:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000478 ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000479 break;
480 case UDP_V4_FLOW:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000481 ret = i40e_add_del_fdir_udpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000482 break;
Jacob Kellerf223c872017-02-06 14:38:51 -0800483 case SCTP_V4_FLOW:
484 ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
485 break;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000486 case IP_USER_FLOW:
487 switch (input->ip4_proto) {
488 case IPPROTO_TCP:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000489 ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000490 break;
491 case IPPROTO_UDP:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000492 ret = i40e_add_del_fdir_udpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000493 break;
Jacob Kellerf223c872017-02-06 14:38:51 -0800494 case IPPROTO_SCTP:
495 ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
496 break;
Alexander Duycke1da71c2016-09-14 16:24:35 -0700497 case IPPROTO_IP:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000498 ret = i40e_add_del_fdir_ipv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000499 break;
Alexander Duycke1da71c2016-09-14 16:24:35 -0700500 default:
501 /* We cannot support masking based on protocol */
Jacob Kellera346fb82017-04-05 07:50:53 -0400502 dev_info(&pf->pdev->dev, "Unsupported IPv4 protocol 0x%02x\n",
503 input->ip4_proto);
504 return -EINVAL;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000505 }
506 break;
507 default:
Jacob Kellera346fb82017-04-05 07:50:53 -0400508 dev_info(&pf->pdev->dev, "Unsupported flow type 0x%02x\n",
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000509 input->flow_type);
Jacob Kellera346fb82017-04-05 07:50:53 -0400510 return -EINVAL;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000511 }
512
Jacob Kellera158aea2017-02-09 23:44:27 -0800513 /* The buffer allocated here will be normally be freed by
514 * i40e_clean_fdir_tx_irq() as it reclaims resources after transmit
515 * completion. In the event of an error adding the buffer to the FDIR
516 * ring, it will immediately be freed. It may also be freed by
517 * i40e_clean_tx_ring() when closing the VSI.
518 */
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000519 return ret;
520}
521
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000522/**
523 * i40e_fd_handle_status - check the Programming Status for FD
524 * @rx_ring: the Rx ring for this descriptor
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000525 * @rx_desc: the Rx descriptor for programming Status, not a packet descriptor.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000526 * @prog_id: the id originally used for programming
527 *
528 * This is used to verify if the FD programming or invalidation
529 * requested by SW to the HW is successful or not and take actions accordingly.
530 **/
Björn Töpel20a739d2018-08-28 14:44:31 +0200531void i40e_fd_handle_status(struct i40e_ring *rx_ring,
532 union i40e_rx_desc *rx_desc, u8 prog_id)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000533{
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000534 struct i40e_pf *pf = rx_ring->vsi->back;
535 struct pci_dev *pdev = pf->pdev;
536 u32 fcnt_prog, fcnt_avail;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000537 u32 error;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000538 u64 qw;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000539
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000540 qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000541 error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
542 I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
543
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400544 if (error == BIT(I40E_RX_PROG_STATUS_DESC_FD_TBL_FULL_SHIFT)) {
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -0400545 pf->fd_inv = le32_to_cpu(rx_desc->wb.qword0.hi_dword.fd_id);
Anjali Singhai Jainf7233c52014-07-09 07:46:16 +0000546 if ((rx_desc->wb.qword0.hi_dword.fd_id != 0) ||
547 (I40E_DEBUG_FD & pf->hw.debug_mask))
548 dev_warn(&pdev->dev, "ntuple filter loc = %d, could not be added\n",
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -0400549 pf->fd_inv);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000550
Anjali Singhai Jain04294e32015-02-27 09:15:28 +0000551 /* Check if the programming error is for ATR.
552 * If so, auto disable ATR and set a state for
553 * flush in progress. Next time we come here if flush is in
554 * progress do nothing, once flush is complete the state will
555 * be cleared.
556 */
Jacob Keller0da36b92017-04-19 09:25:55 -0400557 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain04294e32015-02-27 09:15:28 +0000558 return;
559
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +0000560 pf->fd_add_err++;
561 /* store the current atr filter count */
562 pf->fd_atr_cnt = i40e_get_current_atr_cnt(pf);
563
Anjali Singhai Jain04294e32015-02-27 09:15:28 +0000564 if ((rx_desc->wb.qword0.hi_dword.fd_id == 0) &&
Jacob Keller134201a2018-03-16 01:26:32 -0700565 test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state)) {
566 /* These set_bit() calls aren't atomic with the
567 * test_bit() here, but worse case we potentially
568 * disable ATR and queue a flush right after SB
569 * support is re-enabled. That shouldn't cause an
570 * issue in practice
571 */
572 set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
Jacob Keller0da36b92017-04-19 09:25:55 -0400573 set_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
Anjali Singhai Jain04294e32015-02-27 09:15:28 +0000574 }
575
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000576 /* filter programming failed most likely due to table full */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +0000577 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +0000578 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000579 /* If ATR is running fcnt_prog can quickly change,
580 * if we are very close to full, it makes sense to disable
581 * FD ATR/SB and then re-enable it when there is room.
582 */
583 if (fcnt_prog >= (fcnt_avail - I40E_FDIR_BUFFER_FULL_MARGIN)) {
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +0000584 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
Jacob Keller134201a2018-03-16 01:26:32 -0700585 !test_and_set_bit(__I40E_FD_SB_AUTO_DISABLED,
586 pf->state))
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -0400587 if (I40E_DEBUG_FD & pf->hw.debug_mask)
588 dev_warn(&pdev->dev, "FD filter space full, new ntuple rules will not be added\n");
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000589 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400590 } else if (error == BIT(I40E_RX_PROG_STATUS_DESC_NO_FD_ENTRY_SHIFT)) {
Anjali Singhai Jain13c28842014-03-06 09:00:04 +0000591 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000592 dev_info(&pdev->dev, "ntuple filter fd_id = %d, could not be removed\n",
Anjali Singhai Jain13c28842014-03-06 09:00:04 +0000593 rx_desc->wb.qword0.hi_dword.fd_id);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000594 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000595}
596
597/**
Alexander Duycka5e9c572013-09-28 06:00:27 +0000598 * i40e_unmap_and_free_tx_resource - Release a Tx buffer
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000599 * @ring: the ring that owns the buffer
600 * @tx_buffer: the buffer to free
601 **/
Alexander Duycka5e9c572013-09-28 06:00:27 +0000602static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
603 struct i40e_tx_buffer *tx_buffer)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000604{
Alexander Duycka5e9c572013-09-28 06:00:27 +0000605 if (tx_buffer->skb) {
Alexander Duyck64bfd682016-09-12 14:18:39 -0700606 if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
607 kfree(tx_buffer->raw_buf);
Björn Töpel74608d12017-05-24 07:55:35 +0200608 else if (ring_is_xdp(ring))
Jesper Dangaard Brouer03993092018-04-17 16:46:32 +0200609 xdp_return_frame(tx_buffer->xdpf);
Alexander Duyck64bfd682016-09-12 14:18:39 -0700610 else
611 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000612 if (dma_unmap_len(tx_buffer, len))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000613 dma_unmap_single(ring->dev,
Alexander Duyck35a1e2a2013-09-28 06:00:17 +0000614 dma_unmap_addr(tx_buffer, dma),
615 dma_unmap_len(tx_buffer, len),
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000616 DMA_TO_DEVICE);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000617 } else if (dma_unmap_len(tx_buffer, len)) {
618 dma_unmap_page(ring->dev,
619 dma_unmap_addr(tx_buffer, dma),
620 dma_unmap_len(tx_buffer, len),
621 DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000622 }
Kiran Patila42e7a32015-11-06 15:26:03 -0800623
Alexander Duycka5e9c572013-09-28 06:00:27 +0000624 tx_buffer->next_to_watch = NULL;
625 tx_buffer->skb = NULL;
Alexander Duyck35a1e2a2013-09-28 06:00:17 +0000626 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000627 /* tx_buffer must be completely set up in the transmit path */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000628}
629
630/**
631 * i40e_clean_tx_ring - Free any empty Tx buffers
632 * @tx_ring: ring to be cleaned
633 **/
634void i40e_clean_tx_ring(struct i40e_ring *tx_ring)
635{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000636 unsigned long bi_size;
637 u16 i;
638
639 /* ring already cleared, nothing to do */
640 if (!tx_ring->tx_bi)
641 return;
642
643 /* Free all the Tx ring sk_buffs */
Alexander Duycka5e9c572013-09-28 06:00:27 +0000644 for (i = 0; i < tx_ring->count; i++)
645 i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000646
647 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
648 memset(tx_ring->tx_bi, 0, bi_size);
649
650 /* Zero out the descriptor ring */
651 memset(tx_ring->desc, 0, tx_ring->size);
652
653 tx_ring->next_to_use = 0;
654 tx_ring->next_to_clean = 0;
Alexander Duyck7070ce02013-09-28 06:00:37 +0000655
656 if (!tx_ring->netdev)
657 return;
658
659 /* cleanup Tx queue statistics */
Alexander Duycke486bdf2016-09-12 14:18:40 -0700660 netdev_tx_reset_queue(txring_txq(tx_ring));
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000661}
662
663/**
664 * i40e_free_tx_resources - Free Tx resources per queue
665 * @tx_ring: Tx descriptor ring for a specific queue
666 *
667 * Free all transmit software resources
668 **/
669void i40e_free_tx_resources(struct i40e_ring *tx_ring)
670{
671 i40e_clean_tx_ring(tx_ring);
672 kfree(tx_ring->tx_bi);
673 tx_ring->tx_bi = NULL;
674
675 if (tx_ring->desc) {
676 dma_free_coherent(tx_ring->dev, tx_ring->size,
677 tx_ring->desc, tx_ring->dma);
678 tx_ring->desc = NULL;
679 }
680}
681
Jesse Brandeburga68de582015-02-24 05:26:03 +0000682/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000683 * i40e_get_tx_pending - how many tx descriptors not processed
Jacob Kellerf5254422018-04-20 01:41:33 -0700684 * @ring: the ring of descriptors
Alan Brady04d410512018-02-12 09:16:59 -0500685 * @in_sw: use SW variables
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000686 *
687 * Since there is no access to the ring head register
688 * in XL710, we need to use our local copies
689 **/
Alan Brady04d410512018-02-12 09:16:59 -0500690u32 i40e_get_tx_pending(struct i40e_ring *ring, bool in_sw)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000691{
Jesse Brandeburga68de582015-02-24 05:26:03 +0000692 u32 head, tail;
693
Alan Brady04d410512018-02-12 09:16:59 -0500694 if (!in_sw) {
695 head = i40e_get_head(ring);
696 tail = readl(ring->tail);
697 } else {
698 head = ring->next_to_clean;
699 tail = ring->next_to_use;
700 }
Jesse Brandeburga68de582015-02-24 05:26:03 +0000701
702 if (head != tail)
703 return (head < tail) ?
704 tail - head : (tail + ring->count - head);
705
706 return 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000707}
708
Sudheer Mogilappagari07d44192017-12-18 05:17:25 -0500709/**
710 * i40e_detect_recover_hung - Function to detect and recover hung_queues
711 * @vsi: pointer to vsi struct with tx queues
712 *
713 * VSI has netdev and netdev has TX queues. This function is to check each of
714 * those TX queues if they are hung, trigger recovery by issuing SW interrupt.
715 **/
716void i40e_detect_recover_hung(struct i40e_vsi *vsi)
717{
718 struct i40e_ring *tx_ring = NULL;
719 struct net_device *netdev;
720 unsigned int i;
721 int packets;
722
723 if (!vsi)
724 return;
725
726 if (test_bit(__I40E_VSI_DOWN, vsi->state))
727 return;
728
729 netdev = vsi->netdev;
730 if (!netdev)
731 return;
732
733 if (!netif_carrier_ok(netdev))
734 return;
735
736 for (i = 0; i < vsi->num_queue_pairs; i++) {
737 tx_ring = vsi->tx_rings[i];
738 if (tx_ring && tx_ring->desc) {
739 /* If packet counter has not changed the queue is
740 * likely stalled, so force an interrupt for this
741 * queue.
742 *
743 * prev_pkt_ctr would be negative if there was no
744 * pending work.
745 */
746 packets = tx_ring->stats.packets & INT_MAX;
747 if (tx_ring->tx_stats.prev_pkt_ctr == packets) {
748 i40e_force_wb(vsi, tx_ring->q_vector);
749 continue;
750 }
751
752 /* Memory barrier between read of packet count and call
753 * to i40e_get_tx_pending()
754 */
755 smp_rmb();
756 tx_ring->tx_stats.prev_pkt_ctr =
Alan Brady04d410512018-02-12 09:16:59 -0500757 i40e_get_tx_pending(tx_ring, true) ? packets : -1;
Sudheer Mogilappagari07d44192017-12-18 05:17:25 -0500758 }
759 }
760}
761
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000762/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000763 * i40e_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duycka619afe2016-03-07 09:30:03 -0800764 * @vsi: the VSI we care about
765 * @tx_ring: Tx ring to clean
766 * @napi_budget: Used to determine if we are in netpoll
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000767 *
768 * Returns true if there's any budget left (e.g. the clean is finished)
769 **/
Alexander Duycka619afe2016-03-07 09:30:03 -0800770static bool i40e_clean_tx_irq(struct i40e_vsi *vsi,
771 struct i40e_ring *tx_ring, int napi_budget)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000772{
773 u16 i = tx_ring->next_to_clean;
774 struct i40e_tx_buffer *tx_buf;
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000775 struct i40e_tx_desc *tx_head;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000776 struct i40e_tx_desc *tx_desc;
Alexander Duycka619afe2016-03-07 09:30:03 -0800777 unsigned int total_bytes = 0, total_packets = 0;
778 unsigned int budget = vsi->work_limit;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000779
780 tx_buf = &tx_ring->tx_bi[i];
781 tx_desc = I40E_TX_DESC(tx_ring, i);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000782 i -= tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000783
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000784 tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
785
Alexander Duycka5e9c572013-09-28 06:00:27 +0000786 do {
787 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000788
789 /* if next_to_watch is not set then there is no work pending */
790 if (!eop_desc)
791 break;
792
Alexander Duycka5e9c572013-09-28 06:00:27 +0000793 /* prevent any other reads prior to eop_desc */
Brian King52c69122017-11-17 11:05:44 -0600794 smp_rmb();
Alexander Duycka5e9c572013-09-28 06:00:27 +0000795
Scott Petersoned0980c2017-04-13 04:45:44 -0400796 i40e_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000797 /* we have caught up to head, no work left to do */
798 if (tx_head == tx_desc)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000799 break;
800
Alexander Duyckc304fda2013-09-28 06:00:12 +0000801 /* clear next_to_watch to prevent false hangs */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000802 tx_buf->next_to_watch = NULL;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000803
Alexander Duycka5e9c572013-09-28 06:00:27 +0000804 /* update the statistics for this packet */
805 total_bytes += tx_buf->bytecount;
806 total_packets += tx_buf->gso_segs;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000807
Björn Töpel74608d12017-05-24 07:55:35 +0200808 /* free the skb/XDP data */
809 if (ring_is_xdp(tx_ring))
Jesper Dangaard Brouer03993092018-04-17 16:46:32 +0200810 xdp_return_frame(tx_buf->xdpf);
Björn Töpel74608d12017-05-24 07:55:35 +0200811 else
812 napi_consume_skb(tx_buf->skb, napi_budget);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000813
Alexander Duycka5e9c572013-09-28 06:00:27 +0000814 /* unmap skb header data */
815 dma_unmap_single(tx_ring->dev,
816 dma_unmap_addr(tx_buf, dma),
817 dma_unmap_len(tx_buf, len),
818 DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000819
Alexander Duycka5e9c572013-09-28 06:00:27 +0000820 /* clear tx_buffer data */
821 tx_buf->skb = NULL;
822 dma_unmap_len_set(tx_buf, len, 0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000823
Alexander Duycka5e9c572013-09-28 06:00:27 +0000824 /* unmap remaining buffers */
825 while (tx_desc != eop_desc) {
Scott Petersoned0980c2017-04-13 04:45:44 -0400826 i40e_trace(clean_tx_irq_unmap,
827 tx_ring, tx_desc, tx_buf);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000828
829 tx_buf++;
830 tx_desc++;
831 i++;
Alexander Duycka5e9c572013-09-28 06:00:27 +0000832 if (unlikely(!i)) {
833 i -= tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000834 tx_buf = tx_ring->tx_bi;
835 tx_desc = I40E_TX_DESC(tx_ring, 0);
836 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000837
Alexander Duycka5e9c572013-09-28 06:00:27 +0000838 /* unmap any remaining paged data */
839 if (dma_unmap_len(tx_buf, len)) {
840 dma_unmap_page(tx_ring->dev,
841 dma_unmap_addr(tx_buf, dma),
842 dma_unmap_len(tx_buf, len),
843 DMA_TO_DEVICE);
844 dma_unmap_len_set(tx_buf, len, 0);
845 }
846 }
847
848 /* move us one more past the eop_desc for start of next pkt */
849 tx_buf++;
850 tx_desc++;
851 i++;
852 if (unlikely(!i)) {
853 i -= tx_ring->count;
854 tx_buf = tx_ring->tx_bi;
855 tx_desc = I40E_TX_DESC(tx_ring, 0);
856 }
857
Jesse Brandeburg016890b2015-02-27 09:15:31 +0000858 prefetch(tx_desc);
859
Alexander Duycka5e9c572013-09-28 06:00:27 +0000860 /* update budget accounting */
861 budget--;
862 } while (likely(budget));
863
864 i += tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000865 tx_ring->next_to_clean = i;
Magnus Karlssona96e7472018-08-28 14:44:33 +0200866 i40e_update_tx_stats(tx_ring, total_packets, total_bytes);
867 i40e_arm_wb(tx_ring, vsi, budget);
Jesse Brandeburgd91649f2015-01-07 02:55:01 +0000868
Björn Töpel74608d12017-05-24 07:55:35 +0200869 if (ring_is_xdp(tx_ring))
870 return !!budget;
871
Alexander Duycke486bdf2016-09-12 14:18:40 -0700872 /* notify netdev of completed buffers */
873 netdev_tx_completed_queue(txring_txq(tx_ring),
Alexander Duyck7070ce02013-09-28 06:00:37 +0000874 total_packets, total_bytes);
875
Jesse Brandeburgb85c94b2017-06-20 15:16:59 -0700876#define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000877 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
878 (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
879 /* Make sure that anybody stopping the queue after this
880 * sees the new next_to_clean.
881 */
882 smp_mb();
883 if (__netif_subqueue_stopped(tx_ring->netdev,
884 tx_ring->queue_index) &&
Jacob Keller0da36b92017-04-19 09:25:55 -0400885 !test_bit(__I40E_VSI_DOWN, vsi->state)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000886 netif_wake_subqueue(tx_ring->netdev,
887 tx_ring->queue_index);
888 ++tx_ring->tx_stats.restart_queue;
889 }
890 }
891
Jesse Brandeburgd91649f2015-01-07 02:55:01 +0000892 return !!budget;
893}
894
895/**
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -0800896 * i40e_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
897 * @vsi: the VSI we care about
898 * @q_vector: the vector on which to enable writeback
899 *
900 **/
901static void i40e_enable_wb_on_itr(struct i40e_vsi *vsi,
902 struct i40e_q_vector *q_vector)
903{
904 u16 flags = q_vector->tx.ring[0].flags;
905 u32 val;
906
907 if (!(flags & I40E_TXR_FLAGS_WB_ON_ITR))
908 return;
909
910 if (q_vector->arm_wb_state)
911 return;
912
913 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
914 val = I40E_PFINT_DYN_CTLN_WB_ON_ITR_MASK |
915 I40E_PFINT_DYN_CTLN_ITR_INDX_MASK; /* set noitr */
916
917 wr32(&vsi->back->hw,
Alexander Duycka3f9fb52017-12-29 08:48:53 -0500918 I40E_PFINT_DYN_CTLN(q_vector->reg_idx),
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -0800919 val);
920 } else {
921 val = I40E_PFINT_DYN_CTL0_WB_ON_ITR_MASK |
922 I40E_PFINT_DYN_CTL0_ITR_INDX_MASK; /* set noitr */
923
924 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0, val);
925 }
926 q_vector->arm_wb_state = true;
927}
928
929/**
930 * i40e_force_wb - Issue SW Interrupt so HW does a wb
Jesse Brandeburgd91649f2015-01-07 02:55:01 +0000931 * @vsi: the VSI we care about
932 * @q_vector: the vector on which to force writeback
933 *
934 **/
Kiran Patilb03a8c12015-09-24 18:13:15 -0400935void i40e_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
Jesse Brandeburgd91649f2015-01-07 02:55:01 +0000936{
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -0800937 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -0400938 u32 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
939 I40E_PFINT_DYN_CTLN_ITR_INDX_MASK | /* set noitr */
940 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK |
941 I40E_PFINT_DYN_CTLN_SW_ITR_INDX_ENA_MASK;
942 /* allow 00 to be written to the index */
943
944 wr32(&vsi->back->hw,
Alexander Duycka3f9fb52017-12-29 08:48:53 -0500945 I40E_PFINT_DYN_CTLN(q_vector->reg_idx), val);
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -0400946 } else {
947 u32 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
948 I40E_PFINT_DYN_CTL0_ITR_INDX_MASK | /* set noitr */
949 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
950 I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK;
951 /* allow 00 to be written to the index */
952
953 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0, val);
954 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000955}
956
Alexander Duycka0073a42017-12-29 08:52:19 -0500957static inline bool i40e_container_is_rx(struct i40e_q_vector *q_vector,
958 struct i40e_ring_container *rc)
959{
960 return &q_vector->rx == rc;
961}
962
963static inline unsigned int i40e_itr_divisor(struct i40e_q_vector *q_vector)
964{
965 unsigned int divisor;
966
967 switch (q_vector->vsi->back->hw.phy.link_info.link_speed) {
968 case I40E_LINK_SPEED_40GB:
969 divisor = I40E_ITR_ADAPTIVE_MIN_INC * 1024;
970 break;
971 case I40E_LINK_SPEED_25GB:
972 case I40E_LINK_SPEED_20GB:
973 divisor = I40E_ITR_ADAPTIVE_MIN_INC * 512;
974 break;
975 default:
976 case I40E_LINK_SPEED_10GB:
977 divisor = I40E_ITR_ADAPTIVE_MIN_INC * 256;
978 break;
979 case I40E_LINK_SPEED_1GB:
980 case I40E_LINK_SPEED_100MB:
981 divisor = I40E_ITR_ADAPTIVE_MIN_INC * 32;
982 break;
983 }
984
985 return divisor;
986}
987
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000988/**
Alexander Duycka0073a42017-12-29 08:52:19 -0500989 * i40e_update_itr - update the dynamic ITR value based on statistics
990 * @q_vector: structure containing interrupt and ring information
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000991 * @rc: structure containing ring performance data
992 *
Alexander Duycka0073a42017-12-29 08:52:19 -0500993 * Stores a new ITR value based on packets and byte
994 * counts during the last interrupt. The advantage of per interrupt
995 * computation is faster updates and more accurate ITR for the current
996 * traffic pattern. Constants in this function were computed
997 * based on theoretical maximum wire speed and thresholds were set based
998 * on testing data as well as attempting to minimize response time
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000999 * while increasing bulk throughput.
1000 **/
Alexander Duycka0073a42017-12-29 08:52:19 -05001001static void i40e_update_itr(struct i40e_q_vector *q_vector,
1002 struct i40e_ring_container *rc)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001003{
Alexander Duycka0073a42017-12-29 08:52:19 -05001004 unsigned int avg_wire_size, packets, bytes, itr;
1005 unsigned long next_update = jiffies;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001006
Alexander Duycka0073a42017-12-29 08:52:19 -05001007 /* If we don't have any rings just leave ourselves set for maximum
1008 * possible latency so we take ourselves out of the equation.
1009 */
Alexander Duyck71dc3712017-12-29 08:49:53 -05001010 if (!rc->ring || !ITR_IS_DYNAMIC(rc->ring->itr_setting))
Alexander Duycka0073a42017-12-29 08:52:19 -05001011 return;
Alexander Duyck71dc3712017-12-29 08:49:53 -05001012
Alexander Duycka0073a42017-12-29 08:52:19 -05001013 /* For Rx we want to push the delay up and default to low latency.
1014 * for Tx we want to pull the delay down and default to high latency.
Jacob Keller742c9872017-07-14 09:10:13 -04001015 */
Alexander Duycka0073a42017-12-29 08:52:19 -05001016 itr = i40e_container_is_rx(q_vector, rc) ?
1017 I40E_ITR_ADAPTIVE_MIN_USECS | I40E_ITR_ADAPTIVE_LATENCY :
1018 I40E_ITR_ADAPTIVE_MAX_USECS | I40E_ITR_ADAPTIVE_LATENCY;
1019
1020 /* If we didn't update within up to 1 - 2 jiffies we can assume
1021 * that either packets are coming in so slow there hasn't been
1022 * any work, or that there is so much work that NAPI is dealing
1023 * with interrupt moderation and we don't need to do anything.
1024 */
1025 if (time_after(next_update, rc->next_update))
1026 goto clear_counts;
1027
1028 /* If itr_countdown is set it means we programmed an ITR within
1029 * the last 4 interrupt cycles. This has a side effect of us
1030 * potentially firing an early interrupt. In order to work around
1031 * this we need to throw out any data received for a few
1032 * interrupts following the update.
1033 */
1034 if (q_vector->itr_countdown) {
1035 itr = rc->target_itr;
1036 goto clear_counts;
Jacob Keller742c9872017-07-14 09:10:13 -04001037 }
1038
Alexander Duycka0073a42017-12-29 08:52:19 -05001039 packets = rc->total_packets;
1040 bytes = rc->total_bytes;
1041
1042 if (i40e_container_is_rx(q_vector, rc)) {
1043 /* If Rx there are 1 to 4 packets and bytes are less than
1044 * 9000 assume insufficient data to use bulk rate limiting
1045 * approach unless Tx is already in bulk rate limiting. We
1046 * are likely latency driven.
1047 */
1048 if (packets && packets < 4 && bytes < 9000 &&
1049 (q_vector->tx.target_itr & I40E_ITR_ADAPTIVE_LATENCY)) {
1050 itr = I40E_ITR_ADAPTIVE_LATENCY;
1051 goto adjust_by_size;
1052 }
1053 } else if (packets < 4) {
1054 /* If we have Tx and Rx ITR maxed and Tx ITR is running in
1055 * bulk mode and we are receiving 4 or fewer packets just
1056 * reset the ITR_ADAPTIVE_LATENCY bit for latency mode so
1057 * that the Rx can relax.
1058 */
1059 if (rc->target_itr == I40E_ITR_ADAPTIVE_MAX_USECS &&
1060 (q_vector->rx.target_itr & I40E_ITR_MASK) ==
1061 I40E_ITR_ADAPTIVE_MAX_USECS)
1062 goto clear_counts;
1063 } else if (packets > 32) {
1064 /* If we have processed over 32 packets in a single interrupt
1065 * for Tx assume we need to switch over to "bulk" mode.
1066 */
1067 rc->target_itr &= ~I40E_ITR_ADAPTIVE_LATENCY;
1068 }
1069
1070 /* We have no packets to actually measure against. This means
1071 * either one of the other queues on this vector is active or
1072 * we are a Tx queue doing TSO with too high of an interrupt rate.
Jesse Brandeburg51cc6d92015-09-28 14:16:52 -04001073 *
Alexander Duycka0073a42017-12-29 08:52:19 -05001074 * Between 4 and 56 we can assume that our current interrupt delay
1075 * is only slightly too low. As such we should increase it by a small
1076 * fixed amount.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001077 */
Alexander Duycka0073a42017-12-29 08:52:19 -05001078 if (packets < 56) {
1079 itr = rc->target_itr + I40E_ITR_ADAPTIVE_MIN_INC;
1080 if ((itr & I40E_ITR_MASK) > I40E_ITR_ADAPTIVE_MAX_USECS) {
1081 itr &= I40E_ITR_ADAPTIVE_LATENCY;
1082 itr += I40E_ITR_ADAPTIVE_MAX_USECS;
1083 }
1084 goto clear_counts;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001085 }
Jesse Brandeburgc56625d2015-09-28 14:16:53 -04001086
Alexander Duycka0073a42017-12-29 08:52:19 -05001087 if (packets <= 256) {
1088 itr = min(q_vector->tx.current_itr, q_vector->rx.current_itr);
1089 itr &= I40E_ITR_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001090
Alexander Duycka0073a42017-12-29 08:52:19 -05001091 /* Between 56 and 112 is our "goldilocks" zone where we are
1092 * working out "just right". Just report that our current
1093 * ITR is good for us.
1094 */
1095 if (packets <= 112)
1096 goto clear_counts;
1097
1098 /* If packet count is 128 or greater we are likely looking
1099 * at a slight overrun of the delay we want. Try halving
1100 * our delay to see if that will cut the number of packets
1101 * in half per interrupt.
1102 */
1103 itr /= 2;
1104 itr &= I40E_ITR_MASK;
1105 if (itr < I40E_ITR_ADAPTIVE_MIN_USECS)
1106 itr = I40E_ITR_ADAPTIVE_MIN_USECS;
1107
1108 goto clear_counts;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001109 }
1110
Alexander Duycka0073a42017-12-29 08:52:19 -05001111 /* The paths below assume we are dealing with a bulk ITR since
1112 * number of packets is greater than 256. We are just going to have
1113 * to compute a value and try to bring the count under control,
1114 * though for smaller packet sizes there isn't much we can do as
1115 * NAPI polling will likely be kicking in sooner rather than later.
1116 */
1117 itr = I40E_ITR_ADAPTIVE_BULK;
1118
1119adjust_by_size:
1120 /* If packet counts are 256 or greater we can assume we have a gross
1121 * overestimation of what the rate should be. Instead of trying to fine
1122 * tune it just use the formula below to try and dial in an exact value
1123 * give the current packet size of the frame.
1124 */
1125 avg_wire_size = bytes / packets;
1126
1127 /* The following is a crude approximation of:
1128 * wmem_default / (size + overhead) = desired_pkts_per_int
1129 * rate / bits_per_byte / (size + ethernet overhead) = pkt_rate
1130 * (desired_pkt_rate / pkt_rate) * usecs_per_sec = ITR value
1131 *
1132 * Assuming wmem_default is 212992 and overhead is 640 bytes per
1133 * packet, (256 skb, 64 headroom, 320 shared info), we can reduce the
1134 * formula down to
1135 *
1136 * (170 * (size + 24)) / (size + 640) = ITR
1137 *
1138 * We first do some math on the packet size and then finally bitshift
1139 * by 8 after rounding up. We also have to account for PCIe link speed
1140 * difference as ITR scales based on this.
1141 */
1142 if (avg_wire_size <= 60) {
1143 /* Start at 250k ints/sec */
1144 avg_wire_size = 4096;
1145 } else if (avg_wire_size <= 380) {
1146 /* 250K ints/sec to 60K ints/sec */
1147 avg_wire_size *= 40;
1148 avg_wire_size += 1696;
1149 } else if (avg_wire_size <= 1084) {
1150 /* 60K ints/sec to 36K ints/sec */
1151 avg_wire_size *= 15;
1152 avg_wire_size += 11452;
1153 } else if (avg_wire_size <= 1980) {
1154 /* 36K ints/sec to 30K ints/sec */
1155 avg_wire_size *= 5;
1156 avg_wire_size += 22420;
1157 } else {
1158 /* plateau at a limit of 30K ints/sec */
1159 avg_wire_size = 32256;
1160 }
1161
1162 /* If we are in low latency mode halve our delay which doubles the
1163 * rate to somewhere between 100K to 16K ints/sec
1164 */
1165 if (itr & I40E_ITR_ADAPTIVE_LATENCY)
1166 avg_wire_size /= 2;
1167
1168 /* Resultant value is 256 times larger than it needs to be. This
1169 * gives us room to adjust the value as needed to either increase
1170 * or decrease the value based on link speeds of 10G, 2.5G, 1G, etc.
1171 *
1172 * Use addition as we have already recorded the new latency flag
1173 * for the ITR value.
1174 */
1175 itr += DIV_ROUND_UP(avg_wire_size, i40e_itr_divisor(q_vector)) *
1176 I40E_ITR_ADAPTIVE_MIN_INC;
1177
1178 if ((itr & I40E_ITR_MASK) > I40E_ITR_ADAPTIVE_MAX_USECS) {
1179 itr &= I40E_ITR_ADAPTIVE_LATENCY;
1180 itr += I40E_ITR_ADAPTIVE_MAX_USECS;
1181 }
1182
1183clear_counts:
1184 /* write back value */
1185 rc->target_itr = itr;
1186
1187 /* next update should occur within next jiffy */
1188 rc->next_update = next_update + 1;
1189
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001190 rc->total_bytes = 0;
1191 rc->total_packets = 0;
1192}
1193
1194/**
Alexander Duyck2b9478f2017-10-04 08:44:43 -07001195 * i40e_reuse_rx_page - page flip buffer and store it back on the ring
1196 * @rx_ring: rx descriptor ring to store buffers on
1197 * @old_buff: donor buffer to have page reused
1198 *
1199 * Synchronizes page for reuse by the adapter
1200 **/
1201static void i40e_reuse_rx_page(struct i40e_ring *rx_ring,
1202 struct i40e_rx_buffer *old_buff)
1203{
1204 struct i40e_rx_buffer *new_buff;
1205 u16 nta = rx_ring->next_to_alloc;
1206
1207 new_buff = &rx_ring->rx_bi[nta];
1208
1209 /* update, and store next to alloc */
1210 nta++;
1211 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1212
1213 /* transfer page from old buffer to new buffer */
1214 new_buff->dma = old_buff->dma;
1215 new_buff->page = old_buff->page;
1216 new_buff->page_offset = old_buff->page_offset;
1217 new_buff->pagecnt_bias = old_buff->pagecnt_bias;
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02001218
1219 rx_ring->rx_stats.page_reuse_count++;
1220
1221 /* clear contents of buffer_info */
1222 old_buff->page = NULL;
Alexander Duyck2b9478f2017-10-04 08:44:43 -07001223}
1224
1225/**
Alexander Duyck0e626ff2017-04-10 05:18:43 -04001226 * i40e_rx_is_programming_status - check for programming status descriptor
1227 * @qw: qword representing status_error_len in CPU ordering
1228 *
1229 * The value of in the descriptor length field indicate if this
1230 * is a programming status descriptor for flow director or FCoE
1231 * by the value of I40E_RX_PROG_STATUS_DESC_LENGTH, otherwise
1232 * it is a packet descriptor.
1233 **/
1234static inline bool i40e_rx_is_programming_status(u64 qw)
1235{
1236 /* The Rx filter programming status and SPH bit occupy the same
1237 * spot in the descriptor. Since we don't support packet split we
1238 * can just reuse the bit as an indication that this is a
1239 * programming status descriptor.
1240 */
1241 return qw & I40E_RXD_QW1_LENGTH_SPH_MASK;
1242}
1243
1244/**
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02001245 * i40e_clean_programming_status - try clean the programming status descriptor
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001246 * @rx_ring: the rx ring that has this descriptor
1247 * @rx_desc: the rx descriptor written back by HW
Alexander Duyck0e626ff2017-04-10 05:18:43 -04001248 * @qw: qword representing status_error_len in CPU ordering
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001249 *
1250 * Flow director should handle FD_FILTER_STATUS to check its filter programming
1251 * status being successful or not and take actions accordingly. FCoE should
1252 * handle its context/filter programming/invalidation status and take actions.
1253 *
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02001254 * Returns an i40e_rx_buffer to reuse if the cleanup occurred, otherwise NULL.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001255 **/
Björn Töpel20a739d2018-08-28 14:44:31 +02001256struct i40e_rx_buffer *i40e_clean_programming_status(
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02001257 struct i40e_ring *rx_ring,
1258 union i40e_rx_desc *rx_desc,
1259 u64 qw)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001260{
Alexander Duyck2b9478f2017-10-04 08:44:43 -07001261 struct i40e_rx_buffer *rx_buffer;
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02001262 u32 ntc;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001263 u8 id;
1264
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02001265 if (!i40e_rx_is_programming_status(qw))
1266 return NULL;
1267
1268 ntc = rx_ring->next_to_clean;
1269
Alexander Duyck0e626ff2017-04-10 05:18:43 -04001270 /* fetch, update, and store next to clean */
Alexander Duyck2b9478f2017-10-04 08:44:43 -07001271 rx_buffer = &rx_ring->rx_bi[ntc++];
Alexander Duyck0e626ff2017-04-10 05:18:43 -04001272 ntc = (ntc < rx_ring->count) ? ntc : 0;
1273 rx_ring->next_to_clean = ntc;
1274
1275 prefetch(I40E_RX_DESC(rx_ring, ntc));
1276
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001277 id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
1278 I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
1279
1280 if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00001281 i40e_fd_handle_status(rx_ring, rx_desc, id);
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02001282
1283 return rx_buffer;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001284}
1285
1286/**
1287 * i40e_setup_tx_descriptors - Allocate the Tx descriptors
1288 * @tx_ring: the tx ring to set up
1289 *
1290 * Return 0 on success, negative on error
1291 **/
1292int i40e_setup_tx_descriptors(struct i40e_ring *tx_ring)
1293{
1294 struct device *dev = tx_ring->dev;
1295 int bi_size;
1296
1297 if (!dev)
1298 return -ENOMEM;
1299
Jesse Brandeburge908f812015-07-23 16:54:42 -04001300 /* warn if we are about to overwrite the pointer */
1301 WARN_ON(tx_ring->tx_bi);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001302 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
1303 tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
1304 if (!tx_ring->tx_bi)
1305 goto err;
1306
Florian Fainelli7d6d0672017-08-01 12:11:07 -07001307 u64_stats_init(&tx_ring->syncp);
1308
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001309 /* round up to nearest 4K */
1310 tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00001311 /* add u32 for head writeback, align after this takes care of
1312 * guaranteeing this is at least one cache line in size
1313 */
1314 tx_ring->size += sizeof(u32);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001315 tx_ring->size = ALIGN(tx_ring->size, 4096);
1316 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
1317 &tx_ring->dma, GFP_KERNEL);
1318 if (!tx_ring->desc) {
1319 dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
1320 tx_ring->size);
1321 goto err;
1322 }
1323
1324 tx_ring->next_to_use = 0;
1325 tx_ring->next_to_clean = 0;
Sudheer Mogilappagari07d44192017-12-18 05:17:25 -05001326 tx_ring->tx_stats.prev_pkt_ctr = -1;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001327 return 0;
1328
1329err:
1330 kfree(tx_ring->tx_bi);
1331 tx_ring->tx_bi = NULL;
1332 return -ENOMEM;
1333}
1334
1335/**
1336 * i40e_clean_rx_ring - Free Rx buffers
1337 * @rx_ring: ring to be cleaned
1338 **/
1339void i40e_clean_rx_ring(struct i40e_ring *rx_ring)
1340{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001341 unsigned long bi_size;
1342 u16 i;
1343
1344 /* ring already cleared, nothing to do */
1345 if (!rx_ring->rx_bi)
1346 return;
1347
Scott Petersone72e5652017-02-09 23:40:25 -08001348 if (rx_ring->skb) {
1349 dev_kfree_skb(rx_ring->skb);
1350 rx_ring->skb = NULL;
1351 }
1352
Björn Töpel0a714182018-08-28 14:44:32 +02001353 if (rx_ring->xsk_umem)
1354 goto skip_free;
1355
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001356 /* Free all the Rx ring sk_buffs */
1357 for (i = 0; i < rx_ring->count; i++) {
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001358 struct i40e_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
1359
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001360 if (!rx_bi->page)
1361 continue;
1362
Alexander Duyck59605bc2017-01-30 12:29:35 -08001363 /* Invalidate cache lines that may have been written to by
1364 * device so that we avoid corrupting memory.
1365 */
1366 dma_sync_single_range_for_cpu(rx_ring->dev,
1367 rx_bi->dma,
1368 rx_bi->page_offset,
Alexander Duyck98efd692017-04-05 07:51:01 -04001369 rx_ring->rx_buf_len,
Alexander Duyck59605bc2017-01-30 12:29:35 -08001370 DMA_FROM_DEVICE);
1371
1372 /* free resources associated with mapping */
1373 dma_unmap_page_attrs(rx_ring->dev, rx_bi->dma,
Alexander Duyck98efd692017-04-05 07:51:01 -04001374 i40e_rx_pg_size(rx_ring),
Alexander Duyck59605bc2017-01-30 12:29:35 -08001375 DMA_FROM_DEVICE,
1376 I40E_RX_DMA_ATTR);
Alexander Duyck98efd692017-04-05 07:51:01 -04001377
Alexander Duyck17936682017-02-21 15:55:39 -08001378 __page_frag_cache_drain(rx_bi->page, rx_bi->pagecnt_bias);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001379
1380 rx_bi->page = NULL;
1381 rx_bi->page_offset = 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001382 }
1383
Björn Töpel0a714182018-08-28 14:44:32 +02001384skip_free:
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001385 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
1386 memset(rx_ring->rx_bi, 0, bi_size);
1387
1388 /* Zero out the descriptor ring */
1389 memset(rx_ring->desc, 0, rx_ring->size);
1390
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001391 rx_ring->next_to_alloc = 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001392 rx_ring->next_to_clean = 0;
1393 rx_ring->next_to_use = 0;
1394}
1395
1396/**
1397 * i40e_free_rx_resources - Free Rx resources
1398 * @rx_ring: ring to clean the resources from
1399 *
1400 * Free all receive software resources
1401 **/
1402void i40e_free_rx_resources(struct i40e_ring *rx_ring)
1403{
1404 i40e_clean_rx_ring(rx_ring);
Jesper Dangaard Brouer87128822018-01-03 11:25:23 +01001405 if (rx_ring->vsi->type == I40E_VSI_MAIN)
1406 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
Björn Töpel0c8493d2017-05-24 07:55:34 +02001407 rx_ring->xdp_prog = NULL;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001408 kfree(rx_ring->rx_bi);
1409 rx_ring->rx_bi = NULL;
1410
1411 if (rx_ring->desc) {
1412 dma_free_coherent(rx_ring->dev, rx_ring->size,
1413 rx_ring->desc, rx_ring->dma);
1414 rx_ring->desc = NULL;
1415 }
1416}
1417
1418/**
1419 * i40e_setup_rx_descriptors - Allocate Rx descriptors
1420 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
1421 *
1422 * Returns 0 on success, negative on failure
1423 **/
1424int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring)
1425{
1426 struct device *dev = rx_ring->dev;
Jesper Dangaard Brouer87128822018-01-03 11:25:23 +01001427 int err = -ENOMEM;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001428 int bi_size;
1429
Jesse Brandeburge908f812015-07-23 16:54:42 -04001430 /* warn if we are about to overwrite the pointer */
1431 WARN_ON(rx_ring->rx_bi);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001432 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
1433 rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
1434 if (!rx_ring->rx_bi)
1435 goto err;
1436
Carolyn Wybornyf217d6c2015-02-09 17:42:31 -08001437 u64_stats_init(&rx_ring->syncp);
Carolyn Wyborny638702b2015-01-24 09:58:32 +00001438
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001439 /* Round up to nearest 4K */
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001440 rx_ring->size = rx_ring->count * sizeof(union i40e_32byte_rx_desc);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001441 rx_ring->size = ALIGN(rx_ring->size, 4096);
1442 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
1443 &rx_ring->dma, GFP_KERNEL);
1444
1445 if (!rx_ring->desc) {
1446 dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
1447 rx_ring->size);
1448 goto err;
1449 }
1450
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001451 rx_ring->next_to_alloc = 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001452 rx_ring->next_to_clean = 0;
1453 rx_ring->next_to_use = 0;
1454
Jesper Dangaard Brouer87128822018-01-03 11:25:23 +01001455 /* XDP RX-queue info only needed for RX rings exposed to XDP */
1456 if (rx_ring->vsi->type == I40E_VSI_MAIN) {
1457 err = xdp_rxq_info_reg(&rx_ring->xdp_rxq, rx_ring->netdev,
1458 rx_ring->queue_index);
1459 if (err < 0)
1460 goto err;
1461 }
1462
Björn Töpel0c8493d2017-05-24 07:55:34 +02001463 rx_ring->xdp_prog = rx_ring->vsi->xdp_prog;
1464
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001465 return 0;
1466err:
1467 kfree(rx_ring->rx_bi);
1468 rx_ring->rx_bi = NULL;
Jesper Dangaard Brouer87128822018-01-03 11:25:23 +01001469 return err;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001470}
1471
1472/**
1473 * i40e_release_rx_desc - Store the new tail and head values
1474 * @rx_ring: ring to bump
1475 * @val: new head index
1476 **/
Björn Töpel20a739d2018-08-28 14:44:31 +02001477void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001478{
1479 rx_ring->next_to_use = val;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001480
1481 /* update next to alloc since we have filled the ring */
1482 rx_ring->next_to_alloc = val;
1483
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001484 /* Force memory writes to complete before letting h/w
1485 * know there are new descriptors to fetch. (Only
1486 * applicable for weak-ordered memory model archs,
1487 * such as IA-64).
1488 */
1489 wmb();
1490 writel(val, rx_ring->tail);
1491}
1492
1493/**
Alexander Duyckca9ec082017-04-05 07:51:02 -04001494 * i40e_rx_offset - Return expected offset into page to access data
1495 * @rx_ring: Ring we are requesting offset of
1496 *
1497 * Returns the offset value for ring into the data buffer.
1498 */
1499static inline unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
1500{
1501 return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
1502}
1503
1504/**
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001505 * i40e_alloc_mapped_page - recycle or make a new page
1506 * @rx_ring: ring to use
1507 * @bi: rx_buffer struct to modify
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001508 *
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001509 * Returns true if the page was successfully allocated or
1510 * reused.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001511 **/
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001512static bool i40e_alloc_mapped_page(struct i40e_ring *rx_ring,
1513 struct i40e_rx_buffer *bi)
Mitch Williamsa132af22015-01-24 09:58:35 +00001514{
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001515 struct page *page = bi->page;
1516 dma_addr_t dma;
Mitch Williamsa132af22015-01-24 09:58:35 +00001517
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001518 /* since we are recycling buffers we should seldom need to alloc */
1519 if (likely(page)) {
1520 rx_ring->rx_stats.page_reuse_count++;
1521 return true;
Mitch Williamsa132af22015-01-24 09:58:35 +00001522 }
1523
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001524 /* alloc new page for storage */
Alexander Duyck98efd692017-04-05 07:51:01 -04001525 page = dev_alloc_pages(i40e_rx_pg_order(rx_ring));
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001526 if (unlikely(!page)) {
1527 rx_ring->rx_stats.alloc_page_failed++;
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001528 return false;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001529 }
1530
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001531 /* map page for use */
Alexander Duyck59605bc2017-01-30 12:29:35 -08001532 dma = dma_map_page_attrs(rx_ring->dev, page, 0,
Alexander Duyck98efd692017-04-05 07:51:01 -04001533 i40e_rx_pg_size(rx_ring),
Alexander Duyck59605bc2017-01-30 12:29:35 -08001534 DMA_FROM_DEVICE,
1535 I40E_RX_DMA_ATTR);
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001536
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001537 /* if mapping failed free memory back to system since
1538 * there isn't much point in holding memory we can't use
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001539 */
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001540 if (dma_mapping_error(rx_ring->dev, dma)) {
Alexander Duyck98efd692017-04-05 07:51:01 -04001541 __free_pages(page, i40e_rx_pg_order(rx_ring));
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001542 rx_ring->rx_stats.alloc_page_failed++;
1543 return false;
1544 }
1545
1546 bi->dma = dma;
1547 bi->page = page;
Alexander Duyckca9ec082017-04-05 07:51:02 -04001548 bi->page_offset = i40e_rx_offset(rx_ring);
Björn Töpel8ce29c62018-03-22 16:14:33 +01001549 page_ref_add(page, USHRT_MAX - 1);
1550 bi->pagecnt_bias = USHRT_MAX;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001551
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001552 return true;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001553}
1554
1555/**
1556 * i40e_receive_skb - Send a completed packet up the stack
1557 * @rx_ring: rx ring in play
1558 * @skb: packet to send up
1559 * @vlan_tag: vlan tag for packet
1560 **/
Björn Töpel20a739d2018-08-28 14:44:31 +02001561void i40e_receive_skb(struct i40e_ring *rx_ring,
1562 struct sk_buff *skb, u16 vlan_tag)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001563{
1564 struct i40e_q_vector *q_vector = rx_ring->q_vector;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001565
Jesse Brandeburga149f2c2016-04-12 08:30:49 -07001566 if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1567 (vlan_tag & VLAN_VID_MASK))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001568 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
1569
Alexander Duyck8b650352015-09-24 09:04:32 -07001570 napi_gro_receive(&q_vector->napi, skb);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001571}
1572
1573/**
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001574 * i40e_alloc_rx_buffers - Replace used receive buffers
1575 * @rx_ring: ring to place buffers on
1576 * @cleaned_count: number of buffers to replace
1577 *
1578 * Returns false if all allocations were successful, true if any fail
1579 **/
1580bool i40e_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
1581{
1582 u16 ntu = rx_ring->next_to_use;
1583 union i40e_rx_desc *rx_desc;
1584 struct i40e_rx_buffer *bi;
1585
1586 /* do nothing if no valid netdev defined */
1587 if (!rx_ring->netdev || !cleaned_count)
1588 return false;
1589
1590 rx_desc = I40E_RX_DESC(rx_ring, ntu);
1591 bi = &rx_ring->rx_bi[ntu];
1592
1593 do {
1594 if (!i40e_alloc_mapped_page(rx_ring, bi))
1595 goto no_buffers;
1596
Alexander Duyck59605bc2017-01-30 12:29:35 -08001597 /* sync the buffer for use by the device */
1598 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
1599 bi->page_offset,
Alexander Duyck98efd692017-04-05 07:51:01 -04001600 rx_ring->rx_buf_len,
Alexander Duyck59605bc2017-01-30 12:29:35 -08001601 DMA_FROM_DEVICE);
1602
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001603 /* Refresh the desc even if buffer_addrs didn't change
1604 * because each write-back erases this info.
1605 */
1606 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001607
1608 rx_desc++;
1609 bi++;
1610 ntu++;
1611 if (unlikely(ntu == rx_ring->count)) {
1612 rx_desc = I40E_RX_DESC(rx_ring, 0);
1613 bi = rx_ring->rx_bi;
1614 ntu = 0;
1615 }
1616
1617 /* clear the status bits for the next_to_use descriptor */
1618 rx_desc->wb.qword1.status_error_len = 0;
1619
1620 cleaned_count--;
1621 } while (cleaned_count);
1622
1623 if (rx_ring->next_to_use != ntu)
1624 i40e_release_rx_desc(rx_ring, ntu);
1625
1626 return false;
1627
1628no_buffers:
1629 if (rx_ring->next_to_use != ntu)
1630 i40e_release_rx_desc(rx_ring, ntu);
1631
1632 /* make sure to come back via polling to try again after
1633 * allocation failure
1634 */
1635 return true;
1636}
1637
1638/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001639 * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
1640 * @vsi: the VSI we care about
1641 * @skb: skb currently being received and modified
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001642 * @rx_desc: the receive descriptor
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001643 **/
1644static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
1645 struct sk_buff *skb,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001646 union i40e_rx_desc *rx_desc)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001647{
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001648 struct i40e_rx_ptype_decoded decoded;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001649 u32 rx_error, rx_status;
Alexander Duyck858296c82016-06-14 15:45:42 -07001650 bool ipv4, ipv6;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001651 u8 ptype;
1652 u64 qword;
1653
1654 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1655 ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT;
1656 rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
1657 I40E_RXD_QW1_ERROR_SHIFT;
1658 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1659 I40E_RXD_QW1_STATUS_SHIFT;
1660 decoded = decode_rx_desc_ptype(ptype);
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001661
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001662 skb->ip_summed = CHECKSUM_NONE;
1663
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001664 skb_checksum_none_assert(skb);
1665
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001666 /* Rx csum enabled and ip headers found? */
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001667 if (!(vsi->netdev->features & NETIF_F_RXCSUM))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001668 return;
1669
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001670 /* did the hardware decode the packet and checksum? */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001671 if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001672 return;
1673
1674 /* both known and outer_ip must be set for the below code to work */
1675 if (!(decoded.known && decoded.outer_ip))
1676 return;
1677
Alexander Duyckfad57332016-01-24 21:17:22 -08001678 ipv4 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
1679 (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4);
1680 ipv6 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
1681 (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6);
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001682
1683 if (ipv4 &&
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001684 (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
1685 BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001686 goto checksum_fail;
1687
Jesse Brandeburgddf1d0d2014-02-13 03:48:39 -08001688 /* likely incorrect csum if alternate IP extension headers found */
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001689 if (ipv6 &&
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001690 rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001691 /* don't increment checksum err here, non-fatal err */
Shannon Nelson8ee75a82013-12-21 05:44:46 +00001692 return;
1693
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001694 /* there was some L4 error, count error and punt packet to the stack */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001695 if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001696 goto checksum_fail;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001697
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001698 /* handle packets that were not able to be checksummed due
1699 * to arrival speed, in this case the stack can compute
1700 * the csum.
1701 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001702 if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001703 return;
1704
Alexander Duyck858296c82016-06-14 15:45:42 -07001705 /* If there is an outer header present that might contain a checksum
1706 * we need to bump the checksum level by 1 to reflect the fact that
1707 * we are indicating we validated the inner checksum.
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001708 */
Alexander Duyck858296c82016-06-14 15:45:42 -07001709 if (decoded.tunnel_type >= I40E_RX_PTYPE_TUNNEL_IP_GRENAT)
1710 skb->csum_level = 1;
Alexander Duyckfad57332016-01-24 21:17:22 -08001711
Alexander Duyck858296c82016-06-14 15:45:42 -07001712 /* Only report checksum unnecessary for TCP, UDP, or SCTP */
1713 switch (decoded.inner_prot) {
1714 case I40E_RX_PTYPE_INNER_PROT_TCP:
1715 case I40E_RX_PTYPE_INNER_PROT_UDP:
1716 case I40E_RX_PTYPE_INNER_PROT_SCTP:
1717 skb->ip_summed = CHECKSUM_UNNECESSARY;
1718 /* fall though */
1719 default:
1720 break;
1721 }
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001722
1723 return;
1724
1725checksum_fail:
1726 vsi->back->hw_csum_rx_error++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001727}
1728
1729/**
Anjali Singhai Jain857942f2015-12-09 15:50:21 -08001730 * i40e_ptype_to_htype - get a hash type
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001731 * @ptype: the ptype value from the descriptor
1732 *
1733 * Returns a hash type to be used by skb_set_hash
1734 **/
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001735static inline int i40e_ptype_to_htype(u8 ptype)
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001736{
1737 struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
1738
1739 if (!decoded.known)
1740 return PKT_HASH_TYPE_NONE;
1741
1742 if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1743 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
1744 return PKT_HASH_TYPE_L4;
1745 else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1746 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
1747 return PKT_HASH_TYPE_L3;
1748 else
1749 return PKT_HASH_TYPE_L2;
1750}
1751
1752/**
Anjali Singhai Jain857942f2015-12-09 15:50:21 -08001753 * i40e_rx_hash - set the hash value in the skb
1754 * @ring: descriptor ring
1755 * @rx_desc: specific descriptor
Jacob Kellerf5254422018-04-20 01:41:33 -07001756 * @skb: skb currently being received and modified
1757 * @rx_ptype: Rx packet type
Anjali Singhai Jain857942f2015-12-09 15:50:21 -08001758 **/
1759static inline void i40e_rx_hash(struct i40e_ring *ring,
1760 union i40e_rx_desc *rx_desc,
1761 struct sk_buff *skb,
1762 u8 rx_ptype)
1763{
1764 u32 hash;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001765 const __le64 rss_mask =
Anjali Singhai Jain857942f2015-12-09 15:50:21 -08001766 cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
1767 I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
1768
Mitch Williamsa876c3b2016-05-03 15:13:18 -07001769 if (!(ring->netdev->features & NETIF_F_RXHASH))
Anjali Singhai Jain857942f2015-12-09 15:50:21 -08001770 return;
1771
1772 if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
1773 hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
1774 skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
1775 }
1776}
1777
1778/**
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001779 * i40e_process_skb_fields - Populate skb header fields from Rx descriptor
1780 * @rx_ring: rx descriptor ring packet is being transacted on
1781 * @rx_desc: pointer to the EOP Rx descriptor
1782 * @skb: pointer to current skb being populated
1783 * @rx_ptype: the packet type decoded by hardware
Mitch Williamsa132af22015-01-24 09:58:35 +00001784 *
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001785 * This function checks the ring, descriptor, and packet information in
1786 * order to populate the hash, checksum, VLAN, protocol, and
1787 * other fields within the skb.
Mitch Williamsa132af22015-01-24 09:58:35 +00001788 **/
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001789void i40e_process_skb_fields(struct i40e_ring *rx_ring,
1790 union i40e_rx_desc *rx_desc, struct sk_buff *skb,
1791 u8 rx_ptype)
1792{
1793 u64 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1794 u32 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1795 I40E_RXD_QW1_STATUS_SHIFT;
Jacob Keller144ed172016-10-05 09:30:42 -07001796 u32 tsynvalid = rx_status & I40E_RXD_QW1_STATUS_TSYNVALID_MASK;
1797 u32 tsyn = (rx_status & I40E_RXD_QW1_STATUS_TSYNINDX_MASK) >>
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001798 I40E_RXD_QW1_STATUS_TSYNINDX_SHIFT;
1799
Jacob Keller12490502016-10-05 09:30:44 -07001800 if (unlikely(tsynvalid))
Jacob Keller144ed172016-10-05 09:30:42 -07001801 i40e_ptp_rx_hwtstamp(rx_ring->vsi->back, skb, tsyn);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001802
1803 i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
1804
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001805 i40e_rx_checksum(rx_ring->vsi, skb, rx_desc);
1806
1807 skb_record_rx_queue(skb, rx_ring->queue_index);
Alexander Duycka5b268e2017-02-21 15:55:46 -08001808
1809 /* modifies the skb - consumes the enet header */
1810 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001811}
1812
1813/**
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001814 * i40e_cleanup_headers - Correct empty headers
1815 * @rx_ring: rx descriptor ring packet is being transacted on
1816 * @skb: pointer to current skb being fixed
Björn Töpel0c8493d2017-05-24 07:55:34 +02001817 * @rx_desc: pointer to the EOP Rx descriptor
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001818 *
1819 * Also address the case where we are pulling data in on pages only
1820 * and as such no data is present in the skb header.
1821 *
1822 * In addition if skb is not at least 60 bytes we need to pad it so that
1823 * it is large enough to qualify as a valid Ethernet frame.
1824 *
1825 * Returns true if an error was encountered and skb was freed.
1826 **/
Björn Töpel0c8493d2017-05-24 07:55:34 +02001827static bool i40e_cleanup_headers(struct i40e_ring *rx_ring, struct sk_buff *skb,
1828 union i40e_rx_desc *rx_desc)
1829
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001830{
Björn Töpel0c8493d2017-05-24 07:55:34 +02001831 /* XDP packets use error pointer so abort at this point */
1832 if (IS_ERR(skb))
1833 return true;
1834
1835 /* ERR_MASK will only have valid bits if EOP set, and
1836 * what we are doing here is actually checking
1837 * I40E_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
1838 * the error field
1839 */
1840 if (unlikely(i40e_test_staterr(rx_desc,
1841 BIT(I40E_RXD_QW1_ERROR_SHIFT)))) {
1842 dev_kfree_skb_any(skb);
1843 return true;
1844 }
1845
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001846 /* if eth_skb_pad returns an error the skb was freed */
1847 if (eth_skb_pad(skb))
1848 return true;
1849
1850 return false;
1851}
1852
1853/**
Scott Peterson9b37c932017-02-09 23:43:30 -08001854 * i40e_page_is_reusable - check if any reuse is possible
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001855 * @page: page struct to check
Scott Peterson9b37c932017-02-09 23:43:30 -08001856 *
1857 * A page is not reusable if it was allocated under low memory
1858 * conditions, or it's not in the same NUMA node as this CPU.
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001859 */
Scott Peterson9b37c932017-02-09 23:43:30 -08001860static inline bool i40e_page_is_reusable(struct page *page)
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001861{
Scott Peterson9b37c932017-02-09 23:43:30 -08001862 return (page_to_nid(page) == numa_mem_id()) &&
1863 !page_is_pfmemalloc(page);
1864}
1865
1866/**
1867 * i40e_can_reuse_rx_page - Determine if this page can be reused by
1868 * the adapter for another receive
1869 *
1870 * @rx_buffer: buffer containing the page
Scott Peterson9b37c932017-02-09 23:43:30 -08001871 *
1872 * If page is reusable, rx_buffer->page_offset is adjusted to point to
1873 * an unused region in the page.
1874 *
1875 * For small pages, @truesize will be a constant value, half the size
1876 * of the memory at page. We'll attempt to alternate between high and
1877 * low halves of the page, with one half ready for use by the hardware
1878 * and the other half being consumed by the stack. We use the page
1879 * ref count to determine whether the stack has finished consuming the
1880 * portion of this page that was passed up with a previous packet. If
1881 * the page ref count is >1, we'll assume the "other" half page is
1882 * still busy, and this page cannot be reused.
1883 *
1884 * For larger pages, @truesize will be the actual space used by the
1885 * received packet (adjusted upward to an even multiple of the cache
1886 * line size). This will advance through the page by the amount
1887 * actually consumed by the received packets while there is still
1888 * space for a buffer. Each region of larger pages will be used at
1889 * most once, after which the page will not be reused.
1890 *
1891 * In either case, if the page is reusable its refcount is increased.
1892 **/
Alexander Duycka0cfc312017-03-14 10:15:24 -07001893static bool i40e_can_reuse_rx_page(struct i40e_rx_buffer *rx_buffer)
Scott Peterson9b37c932017-02-09 23:43:30 -08001894{
Alexander Duycka0cfc312017-03-14 10:15:24 -07001895 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
1896 struct page *page = rx_buffer->page;
Scott Peterson9b37c932017-02-09 23:43:30 -08001897
1898 /* Is any reuse possible? */
1899 if (unlikely(!i40e_page_is_reusable(page)))
1900 return false;
1901
1902#if (PAGE_SIZE < 8192)
1903 /* if we are only owner of page we can reuse it */
Alexander Duycka0cfc312017-03-14 10:15:24 -07001904 if (unlikely((page_count(page) - pagecnt_bias) > 1))
Scott Peterson9b37c932017-02-09 23:43:30 -08001905 return false;
Scott Peterson9b37c932017-02-09 23:43:30 -08001906#else
Alexander Duyck98efd692017-04-05 07:51:01 -04001907#define I40E_LAST_OFFSET \
1908 (SKB_WITH_OVERHEAD(PAGE_SIZE) - I40E_RXBUFFER_2048)
1909 if (rx_buffer->page_offset > I40E_LAST_OFFSET)
Scott Peterson9b37c932017-02-09 23:43:30 -08001910 return false;
1911#endif
1912
Alexander Duyck17936682017-02-21 15:55:39 -08001913 /* If we have drained the page fragment pool we need to update
1914 * the pagecnt_bias and page count so that we fully restock the
1915 * number of references the driver holds.
1916 */
Björn Töpel8ce29c62018-03-22 16:14:33 +01001917 if (unlikely(pagecnt_bias == 1)) {
1918 page_ref_add(page, USHRT_MAX - 1);
Alexander Duyck17936682017-02-21 15:55:39 -08001919 rx_buffer->pagecnt_bias = USHRT_MAX;
1920 }
Alexander Duycka0cfc312017-03-14 10:15:24 -07001921
Scott Peterson9b37c932017-02-09 23:43:30 -08001922 return true;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001923}
1924
1925/**
1926 * i40e_add_rx_frag - Add contents of Rx buffer to sk_buff
1927 * @rx_ring: rx descriptor ring to transact packets on
1928 * @rx_buffer: buffer containing page to add
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001929 * @skb: sk_buff to place the data into
Alexander Duycka0cfc312017-03-14 10:15:24 -07001930 * @size: packet length from rx_desc
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001931 *
1932 * This function will add the data contained in rx_buffer->page to the skb.
Alexander Duyckfa2343e2017-03-14 10:15:25 -07001933 * It will just attach the page as a frag to the skb.
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001934 *
Alexander Duyckfa2343e2017-03-14 10:15:25 -07001935 * The function will then update the page offset.
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001936 **/
Alexander Duycka0cfc312017-03-14 10:15:24 -07001937static void i40e_add_rx_frag(struct i40e_ring *rx_ring,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001938 struct i40e_rx_buffer *rx_buffer,
Alexander Duycka0cfc312017-03-14 10:15:24 -07001939 struct sk_buff *skb,
1940 unsigned int size)
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001941{
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001942#if (PAGE_SIZE < 8192)
Alexander Duyck98efd692017-04-05 07:51:01 -04001943 unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001944#else
Alexander Duyckca9ec082017-04-05 07:51:02 -04001945 unsigned int truesize = SKB_DATA_ALIGN(size + i40e_rx_offset(rx_ring));
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001946#endif
Scott Peterson9b37c932017-02-09 23:43:30 -08001947
Alexander Duyckfa2343e2017-03-14 10:15:25 -07001948 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
1949 rx_buffer->page_offset, size, truesize);
Scott Peterson9b37c932017-02-09 23:43:30 -08001950
Alexander Duycka0cfc312017-03-14 10:15:24 -07001951 /* page is being used so we must update the page offset */
1952#if (PAGE_SIZE < 8192)
1953 rx_buffer->page_offset ^= truesize;
1954#else
1955 rx_buffer->page_offset += truesize;
1956#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001957}
1958
1959/**
Alexander Duyck9a064122017-03-14 10:15:23 -07001960 * i40e_get_rx_buffer - Fetch Rx buffer and synchronize data for use
1961 * @rx_ring: rx descriptor ring to transact packets on
1962 * @size: size of buffer to add to skb
1963 *
1964 * This function will pull an Rx buffer from the ring and synchronize it
1965 * for use by the CPU.
1966 */
1967static struct i40e_rx_buffer *i40e_get_rx_buffer(struct i40e_ring *rx_ring,
1968 const unsigned int size)
1969{
1970 struct i40e_rx_buffer *rx_buffer;
1971
1972 rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
1973 prefetchw(rx_buffer->page);
1974
1975 /* we are reusing so sync this buffer for CPU use */
1976 dma_sync_single_range_for_cpu(rx_ring->dev,
1977 rx_buffer->dma,
1978 rx_buffer->page_offset,
1979 size,
1980 DMA_FROM_DEVICE);
1981
Alexander Duycka0cfc312017-03-14 10:15:24 -07001982 /* We have pulled a buffer for use, so decrement pagecnt_bias */
1983 rx_buffer->pagecnt_bias--;
1984
Alexander Duyck9a064122017-03-14 10:15:23 -07001985 return rx_buffer;
1986}
1987
1988/**
Alexander Duyckfa2343e2017-03-14 10:15:25 -07001989 * i40e_construct_skb - Allocate skb and populate it
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001990 * @rx_ring: rx descriptor ring to transact packets on
Alexander Duyck9a064122017-03-14 10:15:23 -07001991 * @rx_buffer: rx buffer to pull data from
Björn Töpel0c8493d2017-05-24 07:55:34 +02001992 * @xdp: xdp_buff pointing to the data
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001993 *
Alexander Duyckfa2343e2017-03-14 10:15:25 -07001994 * This function allocates an skb. It then populates it with the page
1995 * data from the current receive descriptor, taking care to set up the
1996 * skb correctly.
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001997 */
Alexander Duyckfa2343e2017-03-14 10:15:25 -07001998static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
1999 struct i40e_rx_buffer *rx_buffer,
Björn Töpel0c8493d2017-05-24 07:55:34 +02002000 struct xdp_buff *xdp)
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002001{
Björn Töpel0c8493d2017-05-24 07:55:34 +02002002 unsigned int size = xdp->data_end - xdp->data;
Alexander Duyckfa2343e2017-03-14 10:15:25 -07002003#if (PAGE_SIZE < 8192)
Alexander Duyck98efd692017-04-05 07:51:01 -04002004 unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
Alexander Duyckfa2343e2017-03-14 10:15:25 -07002005#else
2006 unsigned int truesize = SKB_DATA_ALIGN(size);
2007#endif
2008 unsigned int headlen;
2009 struct sk_buff *skb;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002010
Alexander Duyckfa2343e2017-03-14 10:15:25 -07002011 /* prefetch first cache line of first page */
Björn Töpel0c8493d2017-05-24 07:55:34 +02002012 prefetch(xdp->data);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002013#if L1_CACHE_BYTES < 128
Björn Töpel0c8493d2017-05-24 07:55:34 +02002014 prefetch(xdp->data + L1_CACHE_BYTES);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002015#endif
Daniel Borkmanncc5b1142018-05-28 11:07:20 +02002016 /* Note, we get here by enabling legacy-rx via:
2017 *
2018 * ethtool --set-priv-flags <dev> legacy-rx on
2019 *
2020 * In this mode, we currently get 0 extra XDP headroom as
2021 * opposed to having legacy-rx off, where we process XDP
2022 * packets going to stack via i40e_build_skb(). The latter
2023 * provides us currently with 192 bytes of headroom.
2024 *
2025 * For i40e_construct_skb() mode it means that the
2026 * xdp->data_meta will always point to xdp->data, since
2027 * the helper cannot expand the head. Should this ever
2028 * change in future for legacy-rx mode on, then lets also
2029 * add xdp->data_meta handling here.
2030 */
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002031
Alexander Duyckfa2343e2017-03-14 10:15:25 -07002032 /* allocate a skb to store the frags */
2033 skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
2034 I40E_RX_HDR_SIZE,
2035 GFP_ATOMIC | __GFP_NOWARN);
2036 if (unlikely(!skb))
2037 return NULL;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002038
Alexander Duyckfa2343e2017-03-14 10:15:25 -07002039 /* Determine available headroom for copy */
2040 headlen = size;
2041 if (headlen > I40E_RX_HDR_SIZE)
Björn Töpel0c8493d2017-05-24 07:55:34 +02002042 headlen = eth_get_headlen(xdp->data, I40E_RX_HDR_SIZE);
Alexander Duyckfa2343e2017-03-14 10:15:25 -07002043
2044 /* align pull length to size of long to optimize memcpy performance */
Björn Töpel0c8493d2017-05-24 07:55:34 +02002045 memcpy(__skb_put(skb, headlen), xdp->data,
2046 ALIGN(headlen, sizeof(long)));
Alexander Duyckfa2343e2017-03-14 10:15:25 -07002047
2048 /* update all of the pointers */
2049 size -= headlen;
2050 if (size) {
2051 skb_add_rx_frag(skb, 0, rx_buffer->page,
2052 rx_buffer->page_offset + headlen,
2053 size, truesize);
2054
2055 /* buffer is used by skb, update page_offset */
2056#if (PAGE_SIZE < 8192)
2057 rx_buffer->page_offset ^= truesize;
2058#else
2059 rx_buffer->page_offset += truesize;
2060#endif
2061 } else {
2062 /* buffer is unused, reset bias back to rx_buffer */
2063 rx_buffer->pagecnt_bias++;
2064 }
Alexander Duycka0cfc312017-03-14 10:15:24 -07002065
2066 return skb;
2067}
2068
2069/**
Alexander Duyckf8b45b72017-04-05 07:51:03 -04002070 * i40e_build_skb - Build skb around an existing buffer
2071 * @rx_ring: Rx descriptor ring to transact packets on
2072 * @rx_buffer: Rx buffer to pull data from
Björn Töpel0c8493d2017-05-24 07:55:34 +02002073 * @xdp: xdp_buff pointing to the data
Alexander Duyckf8b45b72017-04-05 07:51:03 -04002074 *
2075 * This function builds an skb around an existing Rx buffer, taking care
2076 * to set up the skb correctly and avoid any memcpy overhead.
2077 */
2078static struct sk_buff *i40e_build_skb(struct i40e_ring *rx_ring,
2079 struct i40e_rx_buffer *rx_buffer,
Björn Töpel0c8493d2017-05-24 07:55:34 +02002080 struct xdp_buff *xdp)
Alexander Duyckf8b45b72017-04-05 07:51:03 -04002081{
Daniel Borkmanncc5b1142018-05-28 11:07:20 +02002082 unsigned int metasize = xdp->data - xdp->data_meta;
Alexander Duyckf8b45b72017-04-05 07:51:03 -04002083#if (PAGE_SIZE < 8192)
2084 unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
2085#else
Björn Töpel2aae9182017-05-15 06:52:00 +02002086 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
Daniel Borkmannc51818d2018-06-19 14:33:54 -07002087 SKB_DATA_ALIGN(xdp->data_end -
2088 xdp->data_hard_start);
Alexander Duyckf8b45b72017-04-05 07:51:03 -04002089#endif
2090 struct sk_buff *skb;
2091
Daniel Borkmanncc5b1142018-05-28 11:07:20 +02002092 /* Prefetch first cache line of first page. If xdp->data_meta
2093 * is unused, this points exactly as xdp->data, otherwise we
2094 * likely have a consumer accessing first few bytes of meta
2095 * data, and then actual data.
2096 */
2097 prefetch(xdp->data_meta);
Alexander Duyckf8b45b72017-04-05 07:51:03 -04002098#if L1_CACHE_BYTES < 128
Daniel Borkmanncc5b1142018-05-28 11:07:20 +02002099 prefetch(xdp->data_meta + L1_CACHE_BYTES);
Alexander Duyckf8b45b72017-04-05 07:51:03 -04002100#endif
2101 /* build an skb around the page buffer */
Björn Töpel0c8493d2017-05-24 07:55:34 +02002102 skb = build_skb(xdp->data_hard_start, truesize);
Alexander Duyckf8b45b72017-04-05 07:51:03 -04002103 if (unlikely(!skb))
2104 return NULL;
2105
2106 /* update pointers within the skb to store the data */
Daniel Borkmannc51818d2018-06-19 14:33:54 -07002107 skb_reserve(skb, xdp->data - xdp->data_hard_start);
Daniel Borkmanncc5b1142018-05-28 11:07:20 +02002108 __skb_put(skb, xdp->data_end - xdp->data);
2109 if (metasize)
2110 skb_metadata_set(skb, metasize);
Alexander Duyckf8b45b72017-04-05 07:51:03 -04002111
2112 /* buffer is used by skb, update page_offset */
2113#if (PAGE_SIZE < 8192)
2114 rx_buffer->page_offset ^= truesize;
2115#else
2116 rx_buffer->page_offset += truesize;
2117#endif
2118
2119 return skb;
2120}
2121
2122/**
Alexander Duycka0cfc312017-03-14 10:15:24 -07002123 * i40e_put_rx_buffer - Clean up used buffer and either recycle or free
2124 * @rx_ring: rx descriptor ring to transact packets on
2125 * @rx_buffer: rx buffer to pull data from
2126 *
2127 * This function will clean up the contents of the rx_buffer. It will
Alan Brady11a350c2017-12-29 08:48:33 -05002128 * either recycle the buffer or unmap it and free the associated resources.
Alexander Duycka0cfc312017-03-14 10:15:24 -07002129 */
2130static void i40e_put_rx_buffer(struct i40e_ring *rx_ring,
2131 struct i40e_rx_buffer *rx_buffer)
2132{
2133 if (i40e_can_reuse_rx_page(rx_buffer)) {
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002134 /* hand second half of page back to the ring */
2135 i40e_reuse_rx_page(rx_ring, rx_buffer);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002136 } else {
2137 /* we are not reusing the buffer so unmap it */
Alexander Duyck98efd692017-04-05 07:51:01 -04002138 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
2139 i40e_rx_pg_size(rx_ring),
Alexander Duyck59605bc2017-01-30 12:29:35 -08002140 DMA_FROM_DEVICE, I40E_RX_DMA_ATTR);
Alexander Duyck17936682017-02-21 15:55:39 -08002141 __page_frag_cache_drain(rx_buffer->page,
2142 rx_buffer->pagecnt_bias);
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02002143 /* clear contents of buffer_info */
2144 rx_buffer->page = NULL;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002145 }
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002146}
2147
2148/**
2149 * i40e_is_non_eop - process handling of non-EOP buffers
2150 * @rx_ring: Rx ring being processed
2151 * @rx_desc: Rx descriptor for current buffer
2152 * @skb: Current socket buffer containing buffer in progress
2153 *
2154 * This function updates next to clean. If the buffer is an EOP buffer
2155 * this function exits returning false, otherwise it will place the
2156 * sk_buff in the next buffer to be chained and return true indicating
2157 * that this is in fact a non-EOP buffer.
2158 **/
2159static bool i40e_is_non_eop(struct i40e_ring *rx_ring,
2160 union i40e_rx_desc *rx_desc,
2161 struct sk_buff *skb)
2162{
2163 u32 ntc = rx_ring->next_to_clean + 1;
2164
2165 /* fetch, update, and store next to clean */
2166 ntc = (ntc < rx_ring->count) ? ntc : 0;
2167 rx_ring->next_to_clean = ntc;
2168
2169 prefetch(I40E_RX_DESC(rx_ring, ntc));
2170
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002171 /* if we are the last buffer then there is nothing else to do */
2172#define I40E_RXD_EOF BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)
2173 if (likely(i40e_test_staterr(rx_desc, I40E_RXD_EOF)))
2174 return false;
2175
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002176 rx_ring->rx_stats.non_eop_descs++;
2177
2178 return true;
2179}
2180
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +02002181static int i40e_xmit_xdp_ring(struct xdp_frame *xdpf,
Björn Töpel74608d12017-05-24 07:55:35 +02002182 struct i40e_ring *xdp_ring);
Björn Töpel0c8493d2017-05-24 07:55:34 +02002183
Björn Töpel20a739d2018-08-28 14:44:31 +02002184int i40e_xmit_xdp_tx_ring(struct xdp_buff *xdp, struct i40e_ring *xdp_ring)
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +02002185{
2186 struct xdp_frame *xdpf = convert_to_xdp_frame(xdp);
2187
2188 if (unlikely(!xdpf))
2189 return I40E_XDP_CONSUMED;
2190
2191 return i40e_xmit_xdp_ring(xdpf, xdp_ring);
2192}
2193
Björn Töpel0c8493d2017-05-24 07:55:34 +02002194/**
2195 * i40e_run_xdp - run an XDP program
2196 * @rx_ring: Rx ring being processed
2197 * @xdp: XDP buffer containing the frame
2198 **/
2199static struct sk_buff *i40e_run_xdp(struct i40e_ring *rx_ring,
2200 struct xdp_buff *xdp)
2201{
Björn Töpeld9314c472018-03-22 16:14:34 +01002202 int err, result = I40E_XDP_PASS;
Björn Töpel74608d12017-05-24 07:55:35 +02002203 struct i40e_ring *xdp_ring;
Björn Töpel0c8493d2017-05-24 07:55:34 +02002204 struct bpf_prog *xdp_prog;
2205 u32 act;
2206
2207 rcu_read_lock();
2208 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
2209
2210 if (!xdp_prog)
2211 goto xdp_out;
2212
Jesper Dangaard Brouerb411ef12018-04-17 16:46:02 +02002213 prefetchw(xdp->data_hard_start); /* xdp_frame write */
2214
Björn Töpel0c8493d2017-05-24 07:55:34 +02002215 act = bpf_prog_run_xdp(xdp_prog, xdp);
2216 switch (act) {
2217 case XDP_PASS:
2218 break;
Björn Töpel74608d12017-05-24 07:55:35 +02002219 case XDP_TX:
2220 xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +02002221 result = i40e_xmit_xdp_tx_ring(xdp, xdp_ring);
Björn Töpel74608d12017-05-24 07:55:35 +02002222 break;
Björn Töpeld9314c472018-03-22 16:14:34 +01002223 case XDP_REDIRECT:
2224 err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
Jesper Dangaard Brouer2e689312018-06-26 17:39:53 +02002225 result = !err ? I40E_XDP_REDIR : I40E_XDP_CONSUMED;
Björn Töpeld9314c472018-03-22 16:14:34 +01002226 break;
Björn Töpel0c8493d2017-05-24 07:55:34 +02002227 default:
2228 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvaf7c3ca22018-08-07 18:20:27 -05002229 /* fall through */
Björn Töpel0c8493d2017-05-24 07:55:34 +02002230 case XDP_ABORTED:
2231 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
Gustavo A. R. Silvaf7c3ca22018-08-07 18:20:27 -05002232 /* fall through -- handle aborts by dropping packet */
Björn Töpel0c8493d2017-05-24 07:55:34 +02002233 case XDP_DROP:
2234 result = I40E_XDP_CONSUMED;
2235 break;
2236 }
2237xdp_out:
2238 rcu_read_unlock();
2239 return ERR_PTR(-result);
2240}
2241
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002242/**
Björn Töpel74608d12017-05-24 07:55:35 +02002243 * i40e_rx_buffer_flip - adjusted rx_buffer to point to an unused region
2244 * @rx_ring: Rx ring
2245 * @rx_buffer: Rx buffer to adjust
2246 * @size: Size of adjustment
2247 **/
2248static void i40e_rx_buffer_flip(struct i40e_ring *rx_ring,
2249 struct i40e_rx_buffer *rx_buffer,
2250 unsigned int size)
2251{
2252#if (PAGE_SIZE < 8192)
2253 unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
2254
2255 rx_buffer->page_offset ^= truesize;
2256#else
2257 unsigned int truesize = SKB_DATA_ALIGN(i40e_rx_offset(rx_ring) + size);
2258
2259 rx_buffer->page_offset += truesize;
2260#endif
2261}
2262
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02002263/**
2264 * i40e_xdp_ring_update_tail - Updates the XDP Tx ring tail register
2265 * @xdp_ring: XDP Tx ring
2266 *
2267 * This function updates the XDP Tx ring tail register.
2268 **/
Björn Töpel20a739d2018-08-28 14:44:31 +02002269void i40e_xdp_ring_update_tail(struct i40e_ring *xdp_ring)
Björn Töpeld9314c472018-03-22 16:14:34 +01002270{
2271 /* Force memory writes to complete before letting h/w
2272 * know there are new descriptors to fetch.
2273 */
2274 wmb();
2275 writel_relaxed(xdp_ring->next_to_use, xdp_ring->tail);
2276}
2277
Björn Töpel74608d12017-05-24 07:55:35 +02002278/**
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02002279 * i40e_update_rx_stats - Update Rx ring statistics
2280 * @rx_ring: rx descriptor ring
2281 * @total_rx_bytes: number of bytes received
2282 * @total_rx_packets: number of packets received
2283 *
2284 * This function updates the Rx ring statistics.
2285 **/
Björn Töpel20a739d2018-08-28 14:44:31 +02002286void i40e_update_rx_stats(struct i40e_ring *rx_ring,
2287 unsigned int total_rx_bytes,
2288 unsigned int total_rx_packets)
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02002289{
2290 u64_stats_update_begin(&rx_ring->syncp);
2291 rx_ring->stats.packets += total_rx_packets;
2292 rx_ring->stats.bytes += total_rx_bytes;
2293 u64_stats_update_end(&rx_ring->syncp);
2294 rx_ring->q_vector->rx.total_packets += total_rx_packets;
2295 rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
2296}
2297
2298/**
2299 * i40e_finalize_xdp_rx - Bump XDP Tx tail and/or flush redirect map
2300 * @rx_ring: Rx ring
2301 * @xdp_res: Result of the receive batch
2302 *
2303 * This function bumps XDP Tx tail and/or flush redirect map, and
2304 * should be called when a batch of packets has been processed in the
2305 * napi loop.
2306 **/
Björn Töpel20a739d2018-08-28 14:44:31 +02002307void i40e_finalize_xdp_rx(struct i40e_ring *rx_ring, unsigned int xdp_res)
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02002308{
2309 if (xdp_res & I40E_XDP_REDIR)
2310 xdp_do_flush_map();
2311
2312 if (xdp_res & I40E_XDP_TX) {
2313 struct i40e_ring *xdp_ring =
2314 rx_ring->vsi->xdp_rings[rx_ring->queue_index];
2315
2316 i40e_xdp_ring_update_tail(xdp_ring);
2317 }
2318}
2319
2320/**
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002321 * i40e_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
2322 * @rx_ring: rx descriptor ring to transact packets on
2323 * @budget: Total limit on number of packets to process
2324 *
2325 * This function provides a "bounce buffer" approach to Rx interrupt
2326 * processing. The advantage to this is that on systems that have
2327 * expensive overhead for IOMMU access this provides a means of avoiding
2328 * it by maintaining the mapping of the page to the system.
2329 *
2330 * Returns amount of work completed
2331 **/
2332static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
Mitch Williamsa132af22015-01-24 09:58:35 +00002333{
2334 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
Scott Petersone72e5652017-02-09 23:40:25 -08002335 struct sk_buff *skb = rx_ring->skb;
Mitch Williamsa132af22015-01-24 09:58:35 +00002336 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
Jesper Dangaard Brouer2e689312018-06-26 17:39:53 +02002337 unsigned int xdp_xmit = 0;
2338 bool failure = false;
Jesper Dangaard Brouer87128822018-01-03 11:25:23 +01002339 struct xdp_buff xdp;
2340
2341 xdp.rxq = &rx_ring->xdp_rxq;
Mitch Williamsa132af22015-01-24 09:58:35 +00002342
Jesse Brandeburgb85c94b2017-06-20 15:16:59 -07002343 while (likely(total_rx_packets < (unsigned int)budget)) {
Alexander Duyck9a064122017-03-14 10:15:23 -07002344 struct i40e_rx_buffer *rx_buffer;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002345 union i40e_rx_desc *rx_desc;
Alexander Duyckd57c0e02017-03-14 10:15:22 -07002346 unsigned int size;
Mitch Williamsa132af22015-01-24 09:58:35 +00002347 u16 vlan_tag;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002348 u8 rx_ptype;
2349 u64 qword;
2350
Mitch Williamsa132af22015-01-24 09:58:35 +00002351 /* return some buffers to hardware, one at a time is too slow */
2352 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08002353 failure = failure ||
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002354 i40e_alloc_rx_buffers(rx_ring, cleaned_count);
Mitch Williamsa132af22015-01-24 09:58:35 +00002355 cleaned_count = 0;
2356 }
2357
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002358 rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
2359
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002360 /* status_error_len will always be zero for unused descriptors
2361 * because it's cleared in cleanup, and overlaps with hdr_addr
2362 * which is always zero because packet split isn't used, if the
Alexander Duyckd57c0e02017-03-14 10:15:22 -07002363 * hardware wrote DD then the length will be non-zero
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002364 */
Alexander Duyckd57c0e02017-03-14 10:15:22 -07002365 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002366
Mitch Williamsa132af22015-01-24 09:58:35 +00002367 /* This memory barrier is needed to keep us from reading
Alexander Duyckd57c0e02017-03-14 10:15:22 -07002368 * any other fields out of the rx_desc until we have
2369 * verified the descriptor has been written back.
Mitch Williamsa132af22015-01-24 09:58:35 +00002370 */
Alexander Duyck67317162015-04-08 18:49:43 -07002371 dma_rmb();
Mitch Williamsa132af22015-01-24 09:58:35 +00002372
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02002373 rx_buffer = i40e_clean_programming_status(rx_ring, rx_desc,
2374 qword);
2375 if (unlikely(rx_buffer)) {
2376 i40e_reuse_rx_page(rx_ring, rx_buffer);
Alexander Duyck62b4c662017-10-21 18:12:29 -07002377 cleaned_count++;
Alexander Duyck0e626ff2017-04-10 05:18:43 -04002378 continue;
2379 }
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02002380
Alexander Duyck0e626ff2017-04-10 05:18:43 -04002381 size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
2382 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
2383 if (!size)
2384 break;
2385
Scott Petersoned0980c2017-04-13 04:45:44 -04002386 i40e_trace(clean_rx_irq, rx_ring, rx_desc, skb);
Alexander Duyck9a064122017-03-14 10:15:23 -07002387 rx_buffer = i40e_get_rx_buffer(rx_ring, size);
2388
Alexander Duyckfa2343e2017-03-14 10:15:25 -07002389 /* retrieve a buffer from the ring */
Björn Töpel0c8493d2017-05-24 07:55:34 +02002390 if (!skb) {
2391 xdp.data = page_address(rx_buffer->page) +
2392 rx_buffer->page_offset;
Daniel Borkmanncc5b1142018-05-28 11:07:20 +02002393 xdp.data_meta = xdp.data;
Björn Töpel0c8493d2017-05-24 07:55:34 +02002394 xdp.data_hard_start = xdp.data -
2395 i40e_rx_offset(rx_ring);
2396 xdp.data_end = xdp.data + size;
2397
2398 skb = i40e_run_xdp(rx_ring, &xdp);
2399 }
2400
2401 if (IS_ERR(skb)) {
Jesper Dangaard Brouer2e689312018-06-26 17:39:53 +02002402 unsigned int xdp_res = -PTR_ERR(skb);
2403
2404 if (xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR)) {
2405 xdp_xmit |= xdp_res;
Björn Töpel74608d12017-05-24 07:55:35 +02002406 i40e_rx_buffer_flip(rx_ring, rx_buffer, size);
2407 } else {
2408 rx_buffer->pagecnt_bias++;
2409 }
Björn Töpel0c8493d2017-05-24 07:55:34 +02002410 total_rx_bytes += size;
2411 total_rx_packets++;
Björn Töpel0c8493d2017-05-24 07:55:34 +02002412 } else if (skb) {
Alexander Duyckfa2343e2017-03-14 10:15:25 -07002413 i40e_add_rx_frag(rx_ring, rx_buffer, skb, size);
Björn Töpel0c8493d2017-05-24 07:55:34 +02002414 } else if (ring_uses_build_skb(rx_ring)) {
2415 skb = i40e_build_skb(rx_ring, rx_buffer, &xdp);
2416 } else {
2417 skb = i40e_construct_skb(rx_ring, rx_buffer, &xdp);
2418 }
Alexander Duyckfa2343e2017-03-14 10:15:25 -07002419
2420 /* exit if we failed to retrieve a buffer */
2421 if (!skb) {
2422 rx_ring->rx_stats.alloc_buff_failed++;
2423 rx_buffer->pagecnt_bias++;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002424 break;
Alexander Duyckfa2343e2017-03-14 10:15:25 -07002425 }
Mitch Williamsa132af22015-01-24 09:58:35 +00002426
Alexander Duycka0cfc312017-03-14 10:15:24 -07002427 i40e_put_rx_buffer(rx_ring, rx_buffer);
Mitch Williamsa132af22015-01-24 09:58:35 +00002428 cleaned_count++;
2429
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002430 if (i40e_is_non_eop(rx_ring, rx_desc, skb))
Mitch Williamsa132af22015-01-24 09:58:35 +00002431 continue;
Mitch Williamsa132af22015-01-24 09:58:35 +00002432
Björn Töpel0c8493d2017-05-24 07:55:34 +02002433 if (i40e_cleanup_headers(rx_ring, skb, rx_desc)) {
Scott Petersone72e5652017-02-09 23:40:25 -08002434 skb = NULL;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002435 continue;
Scott Petersone72e5652017-02-09 23:40:25 -08002436 }
Mitch Williamsa132af22015-01-24 09:58:35 +00002437
2438 /* probably a little skewed due to removing CRC */
2439 total_rx_bytes += skb->len;
Mitch Williamsa132af22015-01-24 09:58:35 +00002440
Alexander Duyck99dad8b2016-09-27 11:28:50 -07002441 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
2442 rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
2443 I40E_RXD_QW1_PTYPE_SHIFT;
2444
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002445 /* populate checksum, VLAN, and protocol */
2446 i40e_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
Mitch Williamsa132af22015-01-24 09:58:35 +00002447
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002448 vlan_tag = (qword & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)) ?
2449 le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) : 0;
2450
Scott Petersoned0980c2017-04-13 04:45:44 -04002451 i40e_trace(clean_rx_irq_rx, rx_ring, rx_desc, skb);
Mitch Williamsa132af22015-01-24 09:58:35 +00002452 i40e_receive_skb(rx_ring, skb, vlan_tag);
Scott Petersone72e5652017-02-09 23:40:25 -08002453 skb = NULL;
Mitch Williamsa132af22015-01-24 09:58:35 +00002454
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002455 /* update budget accounting */
2456 total_rx_packets++;
2457 }
Mitch Williamsa132af22015-01-24 09:58:35 +00002458
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02002459 i40e_finalize_xdp_rx(rx_ring, xdp_xmit);
Scott Petersone72e5652017-02-09 23:40:25 -08002460 rx_ring->skb = skb;
2461
Björn Töpel6d7aad1d2018-08-28 14:44:30 +02002462 i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets);
Mitch Williamsa132af22015-01-24 09:58:35 +00002463
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002464 /* guarantee a trip back through this routine if there was a failure */
Jesse Brandeburgb85c94b2017-06-20 15:16:59 -07002465 return failure ? budget : (int)total_rx_packets;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002466}
2467
Alexander Duyck92418fb2017-12-29 08:51:08 -05002468static inline u32 i40e_buildreg_itr(const int type, u16 itr)
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04002469{
2470 u32 val;
2471
Alexander Duyck4ff17922017-12-29 08:50:55 -05002472 /* We don't bother with setting the CLEARPBA bit as the data sheet
2473 * points out doing so is "meaningless since it was already
2474 * auto-cleared". The auto-clearing happens when the interrupt is
2475 * asserted.
2476 *
2477 * Hardware errata 28 for also indicates that writing to a
2478 * xxINT_DYN_CTLx CSR with INTENA_MSK (bit 31) set to 0 will clear
2479 * an event in the PBA anyway so we need to rely on the automask
2480 * to hold pending events for us until the interrupt is re-enabled
Alexander Duyck92418fb2017-12-29 08:51:08 -05002481 *
2482 * The itr value is reported in microseconds, and the register
2483 * value is recorded in 2 microsecond units. For this reason we
2484 * only need to shift by the interval shift - 1 instead of the
2485 * full value.
Alexander Duyck4ff17922017-12-29 08:50:55 -05002486 */
Alexander Duyck92418fb2017-12-29 08:51:08 -05002487 itr &= I40E_ITR_MASK;
2488
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04002489 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04002490 (type << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT) |
Alexander Duyck92418fb2017-12-29 08:51:08 -05002491 (itr << (I40E_PFINT_DYN_CTLN_INTERVAL_SHIFT - 1));
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04002492
2493 return val;
2494}
2495
2496/* a small macro to shorten up some long lines */
2497#define INTREG I40E_PFINT_DYN_CTLN
2498
Alexander Duycka0073a42017-12-29 08:52:19 -05002499/* The act of updating the ITR will cause it to immediately trigger. In order
2500 * to prevent this from throwing off adaptive update statistics we defer the
2501 * update so that it can only happen so often. So after either Tx or Rx are
2502 * updated we make the adaptive scheme wait until either the ITR completely
2503 * expires via the next_update expiration or we have been through at least
2504 * 3 interrupts.
2505 */
2506#define ITR_COUNTDOWN_START 3
2507
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002508/**
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04002509 * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
2510 * @vsi: the VSI we care about
2511 * @q_vector: q_vector for which itr is being updated and interrupt enabled
2512 *
2513 **/
2514static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
2515 struct i40e_q_vector *q_vector)
2516{
2517 struct i40e_hw *hw = &vsi->back->hw;
Alexander Duyck556fdfd2017-12-29 08:51:25 -05002518 u32 intval;
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04002519
Jacob Keller9254c0e2017-07-14 09:10:09 -04002520 /* If we don't have MSIX, then we only need to re-enable icr0 */
2521 if (!(vsi->back->flags & I40E_FLAG_MSIX_ENABLED)) {
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04002522 i40e_irq_dynamic_enable_icr0(vsi->back);
Jacob Keller9254c0e2017-07-14 09:10:09 -04002523 return;
2524 }
2525
Alexander Duycka0073a42017-12-29 08:52:19 -05002526 /* These will do nothing if dynamic updates are not enabled */
2527 i40e_update_itr(q_vector, &q_vector->tx);
2528 i40e_update_itr(q_vector, &q_vector->rx);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04002529
Alexander Duycka0073a42017-12-29 08:52:19 -05002530 /* This block of logic allows us to get away with only updating
2531 * one ITR value with each interrupt. The idea is to perform a
2532 * pseudo-lazy update with the following criteria.
2533 *
2534 * 1. Rx is given higher priority than Tx if both are in same state
2535 * 2. If we must reduce an ITR that is given highest priority.
2536 * 3. We then give priority to increasing ITR based on amount.
2537 */
2538 if (q_vector->rx.target_itr < q_vector->rx.current_itr) {
2539 /* Rx ITR needs to be reduced, this is highest priority */
Alexander Duyck556fdfd2017-12-29 08:51:25 -05002540 intval = i40e_buildreg_itr(I40E_RX_ITR,
2541 q_vector->rx.target_itr);
2542 q_vector->rx.current_itr = q_vector->rx.target_itr;
Alexander Duycka0073a42017-12-29 08:52:19 -05002543 q_vector->itr_countdown = ITR_COUNTDOWN_START;
2544 } else if ((q_vector->tx.target_itr < q_vector->tx.current_itr) ||
2545 ((q_vector->rx.target_itr - q_vector->rx.current_itr) <
2546 (q_vector->tx.target_itr - q_vector->tx.current_itr))) {
2547 /* Tx ITR needs to be reduced, this is second priority
2548 * Tx ITR needs to be increased more than Rx, fourth priority
2549 */
Alexander Duyck556fdfd2017-12-29 08:51:25 -05002550 intval = i40e_buildreg_itr(I40E_TX_ITR,
2551 q_vector->tx.target_itr);
2552 q_vector->tx.current_itr = q_vector->tx.target_itr;
Alexander Duycka0073a42017-12-29 08:52:19 -05002553 q_vector->itr_countdown = ITR_COUNTDOWN_START;
2554 } else if (q_vector->rx.current_itr != q_vector->rx.target_itr) {
2555 /* Rx ITR needs to be increased, third priority */
2556 intval = i40e_buildreg_itr(I40E_RX_ITR,
2557 q_vector->rx.target_itr);
2558 q_vector->rx.current_itr = q_vector->rx.target_itr;
2559 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Alexander Duyck556fdfd2017-12-29 08:51:25 -05002560 } else {
Alexander Duycka0073a42017-12-29 08:52:19 -05002561 /* No ITR update, lowest priority */
Alexander Duyck556fdfd2017-12-29 08:51:25 -05002562 intval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
Alexander Duycka0073a42017-12-29 08:52:19 -05002563 if (q_vector->itr_countdown)
2564 q_vector->itr_countdown--;
Alexander Duyck556fdfd2017-12-29 08:51:25 -05002565 }
2566
Jacob Keller0da36b92017-04-19 09:25:55 -04002567 if (!test_bit(__I40E_VSI_DOWN, vsi->state))
Alexander Duyck556fdfd2017-12-29 08:51:25 -05002568 wr32(hw, INTREG(q_vector->reg_idx), intval);
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04002569}
2570
2571/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002572 * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
2573 * @napi: napi struct with our devices info in it
2574 * @budget: amount of work driver is allowed to do this pass, in packets
2575 *
2576 * This function will clean all queues associated with a q_vector.
2577 *
2578 * Returns the amount of work done
2579 **/
2580int i40e_napi_poll(struct napi_struct *napi, int budget)
2581{
2582 struct i40e_q_vector *q_vector =
2583 container_of(napi, struct i40e_q_vector, napi);
2584 struct i40e_vsi *vsi = q_vector->vsi;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002585 struct i40e_ring *ring;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002586 bool clean_complete = true;
Jesse Brandeburgd91649f2015-01-07 02:55:01 +00002587 bool arm_wb = false;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002588 int budget_per_ring;
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07002589 int work_done = 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002590
Jacob Keller0da36b92017-04-19 09:25:55 -04002591 if (test_bit(__I40E_VSI_DOWN, vsi->state)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002592 napi_complete(napi);
2593 return 0;
2594 }
2595
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002596 /* Since the actual Tx work is minimal, we can give the Tx a larger
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002597 * budget and be more aggressive about cleaning up the Tx descriptors.
2598 */
Jesse Brandeburgd91649f2015-01-07 02:55:01 +00002599 i40e_for_each_ring(ring, q_vector->tx) {
Magnus Karlsson1328dcd2018-08-28 14:44:34 +02002600 bool wd = ring->xsk_umem ?
2601 i40e_clean_xdp_tx_irq(vsi, ring, budget) :
2602 i40e_clean_tx_irq(vsi, ring, budget);
2603
2604 if (!wd) {
Alexander Duyckf2edaaa2016-03-07 09:29:57 -08002605 clean_complete = false;
2606 continue;
2607 }
2608 arm_wb |= ring->arm_wb;
Jesse Brandeburg0deda862015-07-23 16:54:34 -04002609 ring->arm_wb = false;
Jesse Brandeburgd91649f2015-01-07 02:55:01 +00002610 }
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002611
Alexander Duyckc67cace2015-09-24 09:04:26 -07002612 /* Handle case where we are called by netpoll with a budget of 0 */
2613 if (budget <= 0)
2614 goto tx_only;
2615
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002616 /* We attempt to distribute budget to each Rx queue fairly, but don't
2617 * allow the budget to go below 1 because that would exit polling early.
2618 */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002619 budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002620
Mitch Williamsa132af22015-01-24 09:58:35 +00002621 i40e_for_each_ring(ring, q_vector->rx) {
Björn Töpel0a714182018-08-28 14:44:32 +02002622 int cleaned = ring->xsk_umem ?
2623 i40e_clean_rx_irq_zc(ring, budget_per_ring) :
2624 i40e_clean_rx_irq(ring, budget_per_ring);
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07002625
2626 work_done += cleaned;
Alexander Duyckf2edaaa2016-03-07 09:29:57 -08002627 /* if we clean as many as budgeted, we must not be done */
2628 if (cleaned >= budget_per_ring)
2629 clean_complete = false;
Mitch Williamsa132af22015-01-24 09:58:35 +00002630 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002631
2632 /* If work not completed, return budget and polling will return */
Jesse Brandeburgd91649f2015-01-07 02:55:01 +00002633 if (!clean_complete) {
Alan Brady96db7762016-09-14 16:24:38 -07002634 int cpu_id = smp_processor_id();
2635
2636 /* It is possible that the interrupt affinity has changed but,
2637 * if the cpu is pegged at 100%, polling will never exit while
2638 * traffic continues and the interrupt will be stuck on this
2639 * cpu. We check to make sure affinity is correct before we
2640 * continue to poll, otherwise we must stop polling so the
2641 * interrupt can move to the correct cpu.
2642 */
Jacob Keller6d977722017-07-14 09:10:11 -04002643 if (!cpumask_test_cpu(cpu_id, &q_vector->affinity_mask)) {
2644 /* Tell napi that we are done polling */
2645 napi_complete_done(napi, work_done);
2646
2647 /* Force an interrupt */
2648 i40e_force_wb(vsi, q_vector);
2649
2650 /* Return budget-1 so that polling stops */
2651 return budget - 1;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -04002652 }
Jacob Keller6d977722017-07-14 09:10:11 -04002653tx_only:
2654 if (arm_wb) {
2655 q_vector->tx.ring[0].tx_stats.tx_force_wb++;
2656 i40e_enable_wb_on_itr(vsi, q_vector);
2657 }
2658 return budget;
Jesse Brandeburgd91649f2015-01-07 02:55:01 +00002659 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002660
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04002661 if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
2662 q_vector->arm_wb_state = false;
2663
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002664 /* Work is done so exit the polling mode and re-enable the interrupt */
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07002665 napi_complete_done(napi, work_done);
Alan Brady96db7762016-09-14 16:24:38 -07002666
Jacob Keller6d977722017-07-14 09:10:11 -04002667 i40e_update_enable_itr(vsi, q_vector);
Alan Brady96db7762016-09-14 16:24:38 -07002668
Alexander Duyck6beb84a2016-11-08 13:05:16 -08002669 return min(work_done, budget - 1);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002670}
2671
2672/**
2673 * i40e_atr - Add a Flow Director ATR filter
2674 * @tx_ring: ring to add programming descriptor to
2675 * @skb: send buffer
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002676 * @tx_flags: send tx flags
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002677 **/
2678static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
Alexander Duyck6b037cd2016-01-24 21:17:36 -08002679 u32 tx_flags)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002680{
2681 struct i40e_filter_program_desc *fdir_desc;
2682 struct i40e_pf *pf = tx_ring->vsi->back;
2683 union {
2684 unsigned char *network;
2685 struct iphdr *ipv4;
2686 struct ipv6hdr *ipv6;
2687 } hdr;
2688 struct tcphdr *th;
2689 unsigned int hlen;
2690 u32 flex_ptype, dtype_cmd;
Alexander Duyckffcc55c2016-01-25 19:32:54 -08002691 int l4_proto;
Alexander Duyckfc4ac672013-09-28 06:00:22 +00002692 u16 i;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002693
2694 /* make sure ATR is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002695 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002696 return;
2697
Jacob Keller134201a2018-03-16 01:26:32 -07002698 if (test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00002699 return;
2700
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002701 /* if sampling is disabled do nothing */
2702 if (!tx_ring->atr_sample_rate)
2703 return;
2704
Alexander Duyck6b037cd2016-01-24 21:17:36 -08002705 /* Currently only IPv4/IPv6 with TCP is supported */
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002706 if (!(tx_flags & (I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6)))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002707 return;
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002708
Alexander Duyckffcc55c2016-01-25 19:32:54 -08002709 /* snag network header to get L4 type and address */
2710 hdr.network = (tx_flags & I40E_TX_FLAGS_UDP_TUNNEL) ?
2711 skb_inner_network_header(skb) : skb_network_header(skb);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002712
Alexander Duyck6b037cd2016-01-24 21:17:36 -08002713 /* Note: tx_flags gets modified to reflect inner protocols in
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002714 * tx_enable_csum function if encap is enabled.
2715 */
Alexander Duyckffcc55c2016-01-25 19:32:54 -08002716 if (tx_flags & I40E_TX_FLAGS_IPV4) {
2717 /* access ihl as u8 to avoid unaligned access on ia64 */
2718 hlen = (hdr.network[0] & 0x0F) << 2;
2719 l4_proto = hdr.ipv4->protocol;
2720 } else {
Jesse Brandeburg601a2e72017-06-20 15:16:58 -07002721 /* find the start of the innermost ipv6 header */
2722 unsigned int inner_hlen = hdr.network - skb->data;
2723 unsigned int h_offset = inner_hlen;
2724
2725 /* this function updates h_offset to the end of the header */
2726 l4_proto =
2727 ipv6_find_hdr(skb, &h_offset, IPPROTO_TCP, NULL, NULL);
2728 /* hlen will contain our best estimate of the tcp header */
2729 hlen = h_offset - inner_hlen;
Alexander Duyckffcc55c2016-01-25 19:32:54 -08002730 }
2731
Alexander Duyck6b037cd2016-01-24 21:17:36 -08002732 if (l4_proto != IPPROTO_TCP)
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002733 return;
2734
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002735 th = (struct tcphdr *)(hdr.network + hlen);
2736
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002737 /* Due to lack of space, no more new filters can be programmed */
Jacob Keller134201a2018-03-16 01:26:32 -07002738 if (th->syn && test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002739 return;
Jacob Keller6964e532017-06-12 15:38:36 -07002740 if (pf->flags & I40E_FLAG_HW_ATR_EVICT_ENABLED) {
Anjali Singhai Jain52eb95e2015-06-05 12:20:33 -04002741 /* HW ATR eviction will take care of removing filters on FIN
2742 * and RST packets.
2743 */
2744 if (th->fin || th->rst)
2745 return;
2746 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002747
2748 tx_ring->atr_count++;
2749
Anjali Singhai Jaince806782014-03-06 08:59:54 +00002750 /* sample on all syn/fin/rst packets or once every atr sample rate */
2751 if (!th->fin &&
2752 !th->syn &&
2753 !th->rst &&
2754 (tx_ring->atr_count < tx_ring->atr_sample_rate))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002755 return;
2756
2757 tx_ring->atr_count = 0;
2758
2759 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +00002760 i = tx_ring->next_to_use;
2761 fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
2762
2763 i++;
2764 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002765
2766 flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
2767 I40E_TXD_FLTR_QW0_QINDEX_MASK;
Alexander Duyck6b037cd2016-01-24 21:17:36 -08002768 flex_ptype |= (tx_flags & I40E_TX_FLAGS_IPV4) ?
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002769 (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
2770 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
2771 (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
2772 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
2773
2774 flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
2775
2776 dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
2777
Anjali Singhai Jaince806782014-03-06 08:59:54 +00002778 dtype_cmd |= (th->fin || th->rst) ?
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002779 (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
2780 I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
2781 (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
2782 I40E_TXD_FLTR_QW1_PCMD_SHIFT);
2783
2784 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
2785 I40E_TXD_FLTR_QW1_DEST_SHIFT;
2786
2787 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
2788 I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
2789
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00002790 dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
Singhai, Anjali6a899022015-12-14 12:21:18 -08002791 if (!(tx_flags & I40E_TX_FLAGS_UDP_TUNNEL))
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04002792 dtype_cmd |=
2793 ((u32)I40E_FD_ATR_STAT_IDX(pf->hw.pf_id) <<
2794 I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
2795 I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
2796 else
2797 dtype_cmd |=
2798 ((u32)I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id) <<
2799 I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
2800 I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00002801
Jacob Keller6964e532017-06-12 15:38:36 -07002802 if (pf->flags & I40E_FLAG_HW_ATR_EVICT_ENABLED)
Anjali Singhai Jain52eb95e2015-06-05 12:20:33 -04002803 dtype_cmd |= I40E_TXD_FLTR_QW1_ATR_MASK;
2804
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002805 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
Jesse Brandeburg99753ea2014-06-04 04:22:49 +00002806 fdir_desc->rsvd = cpu_to_le32(0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002807 fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
Jesse Brandeburg99753ea2014-06-04 04:22:49 +00002808 fdir_desc->fd_id = cpu_to_le32(0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002809}
2810
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002811/**
2812 * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
2813 * @skb: send buffer
2814 * @tx_ring: ring to send buffer on
2815 * @flags: the tx flags to be set
2816 *
2817 * Checks the skb and set up correspondingly several generic transmit flags
2818 * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
2819 *
2820 * Returns error code indicate the frame should be dropped upon error and the
2821 * otherwise returns 0 to indicate the flags has been set properly.
2822 **/
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002823static inline int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
2824 struct i40e_ring *tx_ring,
2825 u32 *flags)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002826{
2827 __be16 protocol = skb->protocol;
2828 u32 tx_flags = 0;
2829
Greg Rose31eaacc2015-03-31 00:45:03 -07002830 if (protocol == htons(ETH_P_8021Q) &&
2831 !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
2832 /* When HW VLAN acceleration is turned off by the user the
2833 * stack sets the protocol to 8021q so that the driver
2834 * can take any steps required to support the SW only
2835 * VLAN handling. In our case the driver doesn't need
2836 * to take any further steps so just set the protocol
2837 * to the encapsulated ethertype.
2838 */
2839 skb->protocol = vlan_get_protocol(skb);
2840 goto out;
2841 }
2842
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002843 /* if we have a HW VLAN tag being added, default to the HW one */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002844 if (skb_vlan_tag_present(skb)) {
2845 tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002846 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
2847 /* else if it is a SW VLAN, check the next protocol and store the tag */
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00002848 } else if (protocol == htons(ETH_P_8021Q)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002849 struct vlan_hdr *vhdr, _vhdr;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002850
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002851 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
2852 if (!vhdr)
2853 return -EINVAL;
2854
2855 protocol = vhdr->h_vlan_encapsulated_proto;
2856 tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
2857 tx_flags |= I40E_TX_FLAGS_SW_VLAN;
2858 }
2859
Neerav Parikhd40d00b2015-02-24 06:58:40 +00002860 if (!(tx_ring->vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2861 goto out;
2862
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002863 /* Insert 802.1p priority into VLAN header */
Vasu Dev38e00432014-08-01 13:27:03 -07002864 if ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
2865 (skb->priority != TC_PRIO_CONTROL)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002866 tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
2867 tx_flags |= (skb->priority & 0x7) <<
2868 I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
2869 if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
2870 struct vlan_ethhdr *vhdr;
Francois Romieudd225bc2014-03-30 03:14:48 +00002871 int rc;
2872
2873 rc = skb_cow_head(skb, 0);
2874 if (rc < 0)
2875 return rc;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002876 vhdr = (struct vlan_ethhdr *)skb->data;
2877 vhdr->h_vlan_TCI = htons(tx_flags >>
2878 I40E_TX_FLAGS_VLAN_SHIFT);
2879 } else {
2880 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
2881 }
2882 }
Neerav Parikhd40d00b2015-02-24 06:58:40 +00002883
2884out:
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002885 *flags = tx_flags;
2886 return 0;
2887}
2888
2889/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002890 * i40e_tso - set up the tso context descriptor
Alexander Duyck52ea3e82016-11-28 16:05:59 -08002891 * @first: pointer to first Tx buffer for xmit
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002892 * @hdr_len: ptr to the size of the packet header
Shannon Nelson9c883bd2015-10-21 19:47:02 -04002893 * @cd_type_cmd_tso_mss: Quad Word 1
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002894 *
2895 * Returns 0 if no TSO can happen, 1 if tso is going, or error
2896 **/
Alexander Duyck52ea3e82016-11-28 16:05:59 -08002897static int i40e_tso(struct i40e_tx_buffer *first, u8 *hdr_len,
2898 u64 *cd_type_cmd_tso_mss)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002899{
Alexander Duyck52ea3e82016-11-28 16:05:59 -08002900 struct sk_buff *skb = first->skb;
Alexander Duyck03f9d6a2016-01-24 21:16:20 -08002901 u64 cd_cmd, cd_tso_len, cd_mss;
Alexander Duyckc7770192016-01-24 21:16:35 -08002902 union {
2903 struct iphdr *v4;
2904 struct ipv6hdr *v6;
2905 unsigned char *hdr;
2906 } ip;
Alexander Duyckc49a7bc2016-01-24 21:16:28 -08002907 union {
2908 struct tcphdr *tcp;
Alexander Duyck54532052016-01-24 21:17:29 -08002909 struct udphdr *udp;
Alexander Duyckc49a7bc2016-01-24 21:16:28 -08002910 unsigned char *hdr;
2911 } l4;
2912 u32 paylen, l4_offset;
Alexander Duyck52ea3e82016-11-28 16:05:59 -08002913 u16 gso_segs, gso_size;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002914 int err;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002915
Shannon Nelsone9f65632016-01-04 10:33:04 -08002916 if (skb->ip_summed != CHECKSUM_PARTIAL)
2917 return 0;
2918
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002919 if (!skb_is_gso(skb))
2920 return 0;
2921
Francois Romieudd225bc2014-03-30 03:14:48 +00002922 err = skb_cow_head(skb, 0);
2923 if (err < 0)
2924 return err;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002925
Alexander Duyckc7770192016-01-24 21:16:35 -08002926 ip.hdr = skb_network_header(skb);
2927 l4.hdr = skb_transport_header(skb);
Anjali Singhaidf230752014-12-19 02:58:16 +00002928
Alexander Duyckc7770192016-01-24 21:16:35 -08002929 /* initialize outer IP header fields */
2930 if (ip.v4->version == 4) {
2931 ip.v4->tot_len = 0;
2932 ip.v4->check = 0;
Alexander Duyckc49a7bc2016-01-24 21:16:28 -08002933 } else {
Alexander Duyckc7770192016-01-24 21:16:35 -08002934 ip.v6->payload_len = 0;
2935 }
2936
Alexander Duyck577389a2016-04-02 00:06:56 -07002937 if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002938 SKB_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002939 SKB_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002940 SKB_GSO_IPXIP6 |
Alexander Duyck577389a2016-04-02 00:06:56 -07002941 SKB_GSO_UDP_TUNNEL |
Alexander Duyck54532052016-01-24 21:17:29 -08002942 SKB_GSO_UDP_TUNNEL_CSUM)) {
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002943 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
2944 (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
2945 l4.udp->len = 0;
2946
Alexander Duyck54532052016-01-24 21:17:29 -08002947 /* determine offset of outer transport header */
2948 l4_offset = l4.hdr - skb->data;
2949
2950 /* remove payload length from outer checksum */
Alexander Duyck24d41e52016-03-18 16:06:47 -07002951 paylen = skb->len - l4_offset;
Jacob Kellerb9c015d2016-12-12 15:44:17 -08002952 csum_replace_by_diff(&l4.udp->check,
2953 (__force __wsum)htonl(paylen));
Alexander Duyck54532052016-01-24 21:17:29 -08002954 }
2955
Alexander Duyckc7770192016-01-24 21:16:35 -08002956 /* reset pointers to inner headers */
2957 ip.hdr = skb_inner_network_header(skb);
2958 l4.hdr = skb_inner_transport_header(skb);
2959
2960 /* initialize inner IP header fields */
2961 if (ip.v4->version == 4) {
2962 ip.v4->tot_len = 0;
2963 ip.v4->check = 0;
2964 } else {
2965 ip.v6->payload_len = 0;
2966 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002967 }
2968
Alexander Duyckc49a7bc2016-01-24 21:16:28 -08002969 /* determine offset of inner transport header */
2970 l4_offset = l4.hdr - skb->data;
2971
2972 /* remove payload length from inner checksum */
Alexander Duyck24d41e52016-03-18 16:06:47 -07002973 paylen = skb->len - l4_offset;
Jacob Kellerb9c015d2016-12-12 15:44:17 -08002974 csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
Alexander Duyckc49a7bc2016-01-24 21:16:28 -08002975
2976 /* compute length of segmentation header */
2977 *hdr_len = (l4.tcp->doff * 4) + l4_offset;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002978
Alexander Duyck52ea3e82016-11-28 16:05:59 -08002979 /* pull values out of skb_shinfo */
2980 gso_size = skb_shinfo(skb)->gso_size;
2981 gso_segs = skb_shinfo(skb)->gso_segs;
2982
2983 /* update GSO size and bytecount with header size */
2984 first->gso_segs = gso_segs;
2985 first->bytecount += (first->gso_segs - 1) * *hdr_len;
2986
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002987 /* find the field values */
2988 cd_cmd = I40E_TX_CTX_DESC_TSO;
2989 cd_tso_len = skb->len - *hdr_len;
Alexander Duyck52ea3e82016-11-28 16:05:59 -08002990 cd_mss = gso_size;
Alexander Duyck03f9d6a2016-01-24 21:16:20 -08002991 *cd_type_cmd_tso_mss |= (cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
2992 (cd_tso_len << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
2993 (cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002994 return 1;
2995}
2996
2997/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002998 * i40e_tsyn - set up the tsyn context descriptor
2999 * @tx_ring: ptr to the ring to send
3000 * @skb: ptr to the skb we're sending
3001 * @tx_flags: the collected send information
Shannon Nelson9c883bd2015-10-21 19:47:02 -04003002 * @cd_type_cmd_tso_mss: Quad Word 1
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003003 *
3004 * Returns 0 if no Tx timestamp can happen and 1 if the timestamp will happen
3005 **/
3006static int i40e_tsyn(struct i40e_ring *tx_ring, struct sk_buff *skb,
3007 u32 tx_flags, u64 *cd_type_cmd_tso_mss)
3008{
3009 struct i40e_pf *pf;
3010
3011 if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
3012 return 0;
3013
3014 /* Tx timestamps cannot be sampled when doing TSO */
3015 if (tx_flags & I40E_TX_FLAGS_TSO)
3016 return 0;
3017
3018 /* only timestamp the outbound packet if the user has requested it and
3019 * we are not already transmitting a packet to be timestamped
3020 */
3021 pf = i40e_netdev_to_pf(tx_ring->netdev);
Jacob Keller22b47772014-12-14 01:55:09 +00003022 if (!(pf->flags & I40E_FLAG_PTP))
3023 return 0;
3024
Jakub Kicinski9ce34f02014-03-15 14:55:42 +00003025 if (pf->ptp_tx &&
Jacob Keller0da36b92017-04-19 09:25:55 -04003026 !test_and_set_bit_lock(__I40E_PTP_TX_IN_PROGRESS, pf->state)) {
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003027 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Jacob Keller0bc07062017-05-03 10:29:02 -07003028 pf->ptp_tx_start = jiffies;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003029 pf->ptp_tx_skb = skb_get(skb);
3030 } else {
Jacob Keller2955fac2017-05-03 10:28:58 -07003031 pf->tx_hwtstamp_skipped++;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003032 return 0;
3033 }
3034
3035 *cd_type_cmd_tso_mss |= (u64)I40E_TX_CTX_DESC_TSYN <<
3036 I40E_TXD_CTX_QW1_CMD_SHIFT;
3037
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003038 return 1;
3039}
3040
3041/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003042 * i40e_tx_enable_csum - Enable Tx checksum offloads
3043 * @skb: send buffer
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04003044 * @tx_flags: pointer to Tx flags currently set
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003045 * @td_cmd: Tx descriptor command bits to set
3046 * @td_offset: Tx descriptor header offsets to set
Jean Sacren554f4542015-10-13 01:06:28 -06003047 * @tx_ring: Tx descriptor ring
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003048 * @cd_tunneling: ptr to context desc bits
3049 **/
Alexander Duyck529f1f62016-01-24 21:17:10 -08003050static int i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
3051 u32 *td_cmd, u32 *td_offset,
3052 struct i40e_ring *tx_ring,
3053 u32 *cd_tunneling)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003054{
Alexander Duyckb96b78f2016-01-24 21:16:42 -08003055 union {
3056 struct iphdr *v4;
3057 struct ipv6hdr *v6;
3058 unsigned char *hdr;
3059 } ip;
3060 union {
3061 struct tcphdr *tcp;
3062 struct udphdr *udp;
3063 unsigned char *hdr;
3064 } l4;
Alexander Duycka3fd9d82016-01-24 21:16:54 -08003065 unsigned char *exthdr;
Jesse Brandeburgd1bd7432016-04-01 03:56:04 -07003066 u32 offset, cmd = 0;
Alexander Duycka3fd9d82016-01-24 21:16:54 -08003067 __be16 frag_off;
Alexander Duyckb96b78f2016-01-24 21:16:42 -08003068 u8 l4_proto = 0;
3069
Alexander Duyck529f1f62016-01-24 21:17:10 -08003070 if (skb->ip_summed != CHECKSUM_PARTIAL)
3071 return 0;
3072
Alexander Duyckb96b78f2016-01-24 21:16:42 -08003073 ip.hdr = skb_network_header(skb);
3074 l4.hdr = skb_transport_header(skb);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003075
Alexander Duyck475b4202016-01-24 21:17:01 -08003076 /* compute outer L2 header size */
3077 offset = ((ip.hdr - skb->data) / 2) << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
3078
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003079 if (skb->encapsulation) {
Jesse Brandeburgd1bd7432016-04-01 03:56:04 -07003080 u32 tunnel = 0;
Alexander Duycka0064722016-01-24 21:16:48 -08003081 /* define outer network header type */
3082 if (*tx_flags & I40E_TX_FLAGS_IPV4) {
Alexander Duyck475b4202016-01-24 21:17:01 -08003083 tunnel |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
3084 I40E_TX_CTX_EXT_IP_IPV4 :
3085 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
3086
Alexander Duycka0064722016-01-24 21:16:48 -08003087 l4_proto = ip.v4->protocol;
3088 } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
Alexander Duyck475b4202016-01-24 21:17:01 -08003089 tunnel |= I40E_TX_CTX_EXT_IP_IPV6;
Alexander Duycka3fd9d82016-01-24 21:16:54 -08003090
3091 exthdr = ip.hdr + sizeof(*ip.v6);
Alexander Duycka0064722016-01-24 21:16:48 -08003092 l4_proto = ip.v6->nexthdr;
Alexander Duycka3fd9d82016-01-24 21:16:54 -08003093 if (l4.hdr != exthdr)
3094 ipv6_skip_exthdr(skb, exthdr - skb->data,
3095 &l4_proto, &frag_off);
Alexander Duycka0064722016-01-24 21:16:48 -08003096 }
3097
3098 /* define outer transport */
3099 switch (l4_proto) {
Anjali Singhai Jain45991202015-02-27 09:15:29 +00003100 case IPPROTO_UDP:
Alexander Duyck475b4202016-01-24 21:17:01 -08003101 tunnel |= I40E_TXD_CTX_UDP_TUNNELING;
Singhai, Anjali6a899022015-12-14 12:21:18 -08003102 *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
Anjali Singhai Jain45991202015-02-27 09:15:29 +00003103 break;
Shannon Nelsonc1d17912015-09-25 19:26:04 +00003104 case IPPROTO_GRE:
Alexander Duyck475b4202016-01-24 21:17:01 -08003105 tunnel |= I40E_TXD_CTX_GRE_TUNNELING;
Alexander Duycka0064722016-01-24 21:16:48 -08003106 *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
Shannon Nelsonc1d17912015-09-25 19:26:04 +00003107 break;
Alexander Duyck577389a2016-04-02 00:06:56 -07003108 case IPPROTO_IPIP:
3109 case IPPROTO_IPV6:
3110 *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
3111 l4.hdr = skb_inner_network_header(skb);
3112 break;
Anjali Singhai Jain45991202015-02-27 09:15:29 +00003113 default:
Alexander Duyck529f1f62016-01-24 21:17:10 -08003114 if (*tx_flags & I40E_TX_FLAGS_TSO)
3115 return -1;
3116
3117 skb_checksum_help(skb);
3118 return 0;
Anjali Singhai Jain45991202015-02-27 09:15:29 +00003119 }
Alexander Duyckb96b78f2016-01-24 21:16:42 -08003120
Alexander Duyck577389a2016-04-02 00:06:56 -07003121 /* compute outer L3 header size */
3122 tunnel |= ((l4.hdr - ip.hdr) / 4) <<
3123 I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
3124
3125 /* switch IP header pointer from outer to inner header */
3126 ip.hdr = skb_inner_network_header(skb);
3127
Alexander Duyck475b4202016-01-24 21:17:01 -08003128 /* compute tunnel header size */
3129 tunnel |= ((ip.hdr - l4.hdr) / 2) <<
3130 I40E_TXD_CTX_QW0_NATLEN_SHIFT;
3131
Alexander Duyck54532052016-01-24 21:17:29 -08003132 /* indicate if we need to offload outer UDP header */
3133 if ((*tx_flags & I40E_TX_FLAGS_TSO) &&
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04003134 !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
Alexander Duyck54532052016-01-24 21:17:29 -08003135 (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
3136 tunnel |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
3137
Alexander Duyck475b4202016-01-24 21:17:01 -08003138 /* record tunnel offload values */
3139 *cd_tunneling |= tunnel;
3140
Alexander Duyckb96b78f2016-01-24 21:16:42 -08003141 /* switch L4 header pointer from outer to inner */
Alexander Duyckb96b78f2016-01-24 21:16:42 -08003142 l4.hdr = skb_inner_transport_header(skb);
Alexander Duycka0064722016-01-24 21:16:48 -08003143 l4_proto = 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003144
Alexander Duycka0064722016-01-24 21:16:48 -08003145 /* reset type as we transition from outer to inner headers */
3146 *tx_flags &= ~(I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6);
3147 if (ip.v4->version == 4)
3148 *tx_flags |= I40E_TX_FLAGS_IPV4;
3149 if (ip.v6->version == 6)
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04003150 *tx_flags |= I40E_TX_FLAGS_IPV6;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003151 }
3152
3153 /* Enable IP checksum offloads */
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04003154 if (*tx_flags & I40E_TX_FLAGS_IPV4) {
Alexander Duyckb96b78f2016-01-24 21:16:42 -08003155 l4_proto = ip.v4->protocol;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003156 /* the stack computes the IP header already, the only time we
3157 * need the hardware to recompute it is in the case of TSO.
3158 */
Alexander Duyck475b4202016-01-24 21:17:01 -08003159 cmd |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
3160 I40E_TX_DESC_CMD_IIPT_IPV4_CSUM :
3161 I40E_TX_DESC_CMD_IIPT_IPV4;
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04003162 } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
Alexander Duyck475b4202016-01-24 21:17:01 -08003163 cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
Alexander Duycka3fd9d82016-01-24 21:16:54 -08003164
3165 exthdr = ip.hdr + sizeof(*ip.v6);
3166 l4_proto = ip.v6->nexthdr;
3167 if (l4.hdr != exthdr)
3168 ipv6_skip_exthdr(skb, exthdr - skb->data,
3169 &l4_proto, &frag_off);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003170 }
Alexander Duyckb96b78f2016-01-24 21:16:42 -08003171
Alexander Duyck475b4202016-01-24 21:17:01 -08003172 /* compute inner L3 header size */
3173 offset |= ((l4.hdr - ip.hdr) / 4) << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003174
3175 /* Enable L4 checksum offloads */
Alexander Duyckb96b78f2016-01-24 21:16:42 -08003176 switch (l4_proto) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003177 case IPPROTO_TCP:
3178 /* enable checksum offloads */
Alexander Duyck475b4202016-01-24 21:17:01 -08003179 cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
3180 offset |= l4.tcp->doff << I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003181 break;
3182 case IPPROTO_SCTP:
3183 /* enable SCTP checksum offload */
Alexander Duyck475b4202016-01-24 21:17:01 -08003184 cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
3185 offset |= (sizeof(struct sctphdr) >> 2) <<
3186 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003187 break;
3188 case IPPROTO_UDP:
3189 /* enable UDP checksum offload */
Alexander Duyck475b4202016-01-24 21:17:01 -08003190 cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
3191 offset |= (sizeof(struct udphdr) >> 2) <<
3192 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003193 break;
3194 default:
Alexander Duyck529f1f62016-01-24 21:17:10 -08003195 if (*tx_flags & I40E_TX_FLAGS_TSO)
3196 return -1;
3197 skb_checksum_help(skb);
3198 return 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003199 }
Alexander Duyck475b4202016-01-24 21:17:01 -08003200
3201 *td_cmd |= cmd;
3202 *td_offset |= offset;
Alexander Duyck529f1f62016-01-24 21:17:10 -08003203
3204 return 1;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003205}
3206
3207/**
3208 * i40e_create_tx_ctx Build the Tx context descriptor
3209 * @tx_ring: ring to create the descriptor on
3210 * @cd_type_cmd_tso_mss: Quad Word 1
3211 * @cd_tunneling: Quad Word 0 - bits 0-31
3212 * @cd_l2tag2: Quad Word 0 - bits 32-63
3213 **/
3214static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
3215 const u64 cd_type_cmd_tso_mss,
3216 const u32 cd_tunneling, const u32 cd_l2tag2)
3217{
3218 struct i40e_tx_context_desc *context_desc;
Alexander Duyckfc4ac672013-09-28 06:00:22 +00003219 int i = tx_ring->next_to_use;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003220
Jesse Brandeburgff40dd52014-02-14 02:14:41 +00003221 if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
3222 !cd_tunneling && !cd_l2tag2)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003223 return;
3224
3225 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +00003226 context_desc = I40E_TX_CTXTDESC(tx_ring, i);
3227
3228 i++;
3229 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003230
3231 /* cpu_to_le32 and assign to struct fields */
3232 context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
3233 context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
Jesse Brandeburg3efbbb22014-06-04 20:41:54 +00003234 context_desc->rsvd = cpu_to_le16(0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003235 context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
3236}
3237
3238/**
Eric Dumazet4567dc12014-10-07 13:30:23 -07003239 * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
3240 * @tx_ring: the ring to be checked
3241 * @size: the size buffer we want to assure is available
3242 *
3243 * Returns -EBUSY if a stop is needed, else 0
3244 **/
Alexander Duyck4ec441d2016-02-17 11:02:43 -08003245int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
Eric Dumazet4567dc12014-10-07 13:30:23 -07003246{
3247 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
3248 /* Memory barrier before checking head and tail */
3249 smp_mb();
3250
3251 /* Check again in a case another CPU has just made room available. */
3252 if (likely(I40E_DESC_UNUSED(tx_ring) < size))
3253 return -EBUSY;
3254
3255 /* A reprieve! - use start_queue because it doesn't call schedule */
3256 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
3257 ++tx_ring->tx_stats.restart_queue;
3258 return 0;
3259}
3260
3261/**
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07003262 * __i40e_chk_linearize - Check if there are more than 8 buffers per packet
Anjali Singhai71da6192015-02-21 06:42:35 +00003263 * @skb: send buffer
Anjali Singhai71da6192015-02-21 06:42:35 +00003264 *
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07003265 * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire
3266 * and so we need to figure out the cases where we need to linearize the skb.
3267 *
3268 * For TSO we need to count the TSO header and segment payload separately.
3269 * As such we need to check cases where we have 7 fragments or more as we
3270 * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
3271 * the segment payload in the first descriptor, and another 7 for the
3272 * fragments.
Anjali Singhai71da6192015-02-21 06:42:35 +00003273 **/
Alexander Duyck2d374902016-02-17 11:02:50 -08003274bool __i40e_chk_linearize(struct sk_buff *skb)
Anjali Singhai71da6192015-02-21 06:42:35 +00003275{
Alexander Duyck2d374902016-02-17 11:02:50 -08003276 const struct skb_frag_struct *frag, *stale;
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07003277 int nr_frags, sum;
Anjali Singhai71da6192015-02-21 06:42:35 +00003278
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07003279 /* no need to check if number of frags is less than 7 */
Alexander Duyck2d374902016-02-17 11:02:50 -08003280 nr_frags = skb_shinfo(skb)->nr_frags;
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07003281 if (nr_frags < (I40E_MAX_BUFFER_TXD - 1))
Alexander Duyck2d374902016-02-17 11:02:50 -08003282 return false;
Anjali Singhai71da6192015-02-21 06:42:35 +00003283
Alexander Duyck2d374902016-02-17 11:02:50 -08003284 /* We need to walk through the list and validate that each group
Alexander Duyck841493a2016-09-06 18:05:04 -07003285 * of 6 fragments totals at least gso_size.
Alexander Duyck2d374902016-02-17 11:02:50 -08003286 */
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07003287 nr_frags -= I40E_MAX_BUFFER_TXD - 2;
Alexander Duyck2d374902016-02-17 11:02:50 -08003288 frag = &skb_shinfo(skb)->frags[0];
3289
3290 /* Initialize size to the negative value of gso_size minus 1. We
3291 * use this as the worst case scenerio in which the frag ahead
3292 * of us only provides one byte which is why we are limited to 6
3293 * descriptors for a single transmit as the header and previous
3294 * fragment are already consuming 2 descriptors.
3295 */
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07003296 sum = 1 - skb_shinfo(skb)->gso_size;
Alexander Duyck2d374902016-02-17 11:02:50 -08003297
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07003298 /* Add size of frags 0 through 4 to create our initial sum */
3299 sum += skb_frag_size(frag++);
3300 sum += skb_frag_size(frag++);
3301 sum += skb_frag_size(frag++);
3302 sum += skb_frag_size(frag++);
3303 sum += skb_frag_size(frag++);
Alexander Duyck2d374902016-02-17 11:02:50 -08003304
3305 /* Walk through fragments adding latest fragment, testing it, and
3306 * then removing stale fragments from the sum.
3307 */
Alexander Duyck248de222017-12-08 10:55:04 -08003308 for (stale = &skb_shinfo(skb)->frags[0];; stale++) {
3309 int stale_size = skb_frag_size(stale);
3310
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07003311 sum += skb_frag_size(frag++);
Alexander Duyck2d374902016-02-17 11:02:50 -08003312
Alexander Duyck248de222017-12-08 10:55:04 -08003313 /* The stale fragment may present us with a smaller
3314 * descriptor than the actual fragment size. To account
3315 * for that we need to remove all the data on the front and
3316 * figure out what the remainder would be in the last
3317 * descriptor associated with the fragment.
3318 */
3319 if (stale_size > I40E_MAX_DATA_PER_TXD) {
3320 int align_pad = -(stale->page_offset) &
3321 (I40E_MAX_READ_REQ_SIZE - 1);
3322
3323 sum -= align_pad;
3324 stale_size -= align_pad;
3325
3326 do {
3327 sum -= I40E_MAX_DATA_PER_TXD_ALIGNED;
3328 stale_size -= I40E_MAX_DATA_PER_TXD_ALIGNED;
3329 } while (stale_size > I40E_MAX_DATA_PER_TXD);
3330 }
3331
Alexander Duyck2d374902016-02-17 11:02:50 -08003332 /* if sum is negative we failed to make sufficient progress */
3333 if (sum < 0)
3334 return true;
3335
Alexander Duyck841493a2016-09-06 18:05:04 -07003336 if (!nr_frags--)
Alexander Duyck2d374902016-02-17 11:02:50 -08003337 break;
3338
Alexander Duyck248de222017-12-08 10:55:04 -08003339 sum -= stale_size;
Anjali Singhai71da6192015-02-21 06:42:35 +00003340 }
3341
Alexander Duyck2d374902016-02-17 11:02:50 -08003342 return false;
Anjali Singhai71da6192015-02-21 06:42:35 +00003343}
3344
3345/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003346 * i40e_tx_map - Build the Tx descriptor
3347 * @tx_ring: ring to send buffer on
3348 * @skb: send buffer
3349 * @first: first buffer info buffer to use
3350 * @tx_flags: collected send information
3351 * @hdr_len: size of the packet header
3352 * @td_cmd: the command field in the descriptor
3353 * @td_offset: offset for checksum or crc
Jacob Keller69077572017-05-03 10:28:54 -07003354 *
3355 * Returns 0 on success, -1 on failure to DMA
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003356 **/
Jacob Keller69077572017-05-03 10:28:54 -07003357static inline int i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
3358 struct i40e_tx_buffer *first, u32 tx_flags,
3359 const u8 hdr_len, u32 td_cmd, u32 td_offset)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003360{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003361 unsigned int data_len = skb->data_len;
3362 unsigned int size = skb_headlen(skb);
Alexander Duycka5e9c572013-09-28 06:00:27 +00003363 struct skb_frag_struct *frag;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003364 struct i40e_tx_buffer *tx_bi;
3365 struct i40e_tx_desc *tx_desc;
Alexander Duycka5e9c572013-09-28 06:00:27 +00003366 u16 i = tx_ring->next_to_use;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003367 u32 td_tag = 0;
3368 dma_addr_t dma;
Alexander Duyck1dc8b532016-10-11 15:26:54 -07003369 u16 desc_count = 1;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003370
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003371 if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
3372 td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
3373 td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
3374 I40E_TX_FLAGS_VLAN_SHIFT;
3375 }
3376
Alexander Duycka5e9c572013-09-28 06:00:27 +00003377 first->tx_flags = tx_flags;
3378
3379 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
3380
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003381 tx_desc = I40E_TX_DESC(tx_ring, i);
Alexander Duycka5e9c572013-09-28 06:00:27 +00003382 tx_bi = first;
3383
3384 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
Alexander Duyck5c4654d2016-02-19 12:17:08 -08003385 unsigned int max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
3386
Alexander Duycka5e9c572013-09-28 06:00:27 +00003387 if (dma_mapping_error(tx_ring->dev, dma))
3388 goto dma_error;
3389
3390 /* record length, and DMA address */
3391 dma_unmap_len_set(tx_bi, len, size);
3392 dma_unmap_addr_set(tx_bi, dma, dma);
3393
Alexander Duyck5c4654d2016-02-19 12:17:08 -08003394 /* align size to end of page */
3395 max_data += -dma & (I40E_MAX_READ_REQ_SIZE - 1);
Alexander Duycka5e9c572013-09-28 06:00:27 +00003396 tx_desc->buffer_addr = cpu_to_le64(dma);
3397
3398 while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003399 tx_desc->cmd_type_offset_bsz =
3400 build_ctob(td_cmd, td_offset,
Alexander Duyck5c4654d2016-02-19 12:17:08 -08003401 max_data, td_tag);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003402
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003403 tx_desc++;
3404 i++;
Anjali Singhai58044742015-09-25 18:26:13 -07003405 desc_count++;
3406
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003407 if (i == tx_ring->count) {
3408 tx_desc = I40E_TX_DESC(tx_ring, 0);
3409 i = 0;
3410 }
Alexander Duycka5e9c572013-09-28 06:00:27 +00003411
Alexander Duyck5c4654d2016-02-19 12:17:08 -08003412 dma += max_data;
3413 size -= max_data;
Alexander Duycka5e9c572013-09-28 06:00:27 +00003414
Alexander Duyck5c4654d2016-02-19 12:17:08 -08003415 max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
Alexander Duycka5e9c572013-09-28 06:00:27 +00003416 tx_desc->buffer_addr = cpu_to_le64(dma);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003417 }
3418
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003419 if (likely(!data_len))
3420 break;
3421
Alexander Duycka5e9c572013-09-28 06:00:27 +00003422 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
3423 size, td_tag);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003424
3425 tx_desc++;
3426 i++;
Anjali Singhai58044742015-09-25 18:26:13 -07003427 desc_count++;
3428
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003429 if (i == tx_ring->count) {
3430 tx_desc = I40E_TX_DESC(tx_ring, 0);
3431 i = 0;
3432 }
3433
Alexander Duycka5e9c572013-09-28 06:00:27 +00003434 size = skb_frag_size(frag);
3435 data_len -= size;
3436
3437 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
3438 DMA_TO_DEVICE);
3439
3440 tx_bi = &tx_ring->tx_bi[i];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003441 }
3442
Alexander Duyck1dc8b532016-10-11 15:26:54 -07003443 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
Alexander Duycka5e9c572013-09-28 06:00:27 +00003444
3445 i++;
3446 if (i == tx_ring->count)
3447 i = 0;
3448
3449 tx_ring->next_to_use = i;
3450
Eric Dumazet4567dc12014-10-07 13:30:23 -07003451 i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
Anjali Singhai58044742015-09-25 18:26:13 -07003452
Alexander Duyck1dc8b532016-10-11 15:26:54 -07003453 /* write last descriptor with EOP bit */
3454 td_cmd |= I40E_TX_DESC_CMD_EOP;
3455
Jacob Kellera5340d92017-08-29 05:32:42 -04003456 /* We OR these values together to check both against 4 (WB_STRIDE)
3457 * below. This is safe since we don't re-use desc_count afterwards.
Alexander Duyck1dc8b532016-10-11 15:26:54 -07003458 */
3459 desc_count |= ++tx_ring->packet_stride;
3460
Jacob Kellera5340d92017-08-29 05:32:42 -04003461 if (desc_count >= WB_STRIDE) {
Alexander Duyck1dc8b532016-10-11 15:26:54 -07003462 /* write last descriptor with RS bit set */
3463 td_cmd |= I40E_TX_DESC_CMD_RS;
Anjali Singhai58044742015-09-25 18:26:13 -07003464 tx_ring->packet_stride = 0;
Anjali Singhai58044742015-09-25 18:26:13 -07003465 }
Anjali Singhai58044742015-09-25 18:26:13 -07003466
3467 tx_desc->cmd_type_offset_bsz =
Alexander Duyck1dc8b532016-10-11 15:26:54 -07003468 build_ctob(td_cmd, td_offset, size, td_tag);
3469
3470 /* Force memory writes to complete before letting h/w know there
3471 * are new descriptors to fetch.
3472 *
3473 * We also use this memory barrier to make certain all of the
3474 * status bits have been updated before next_to_watch is written.
3475 */
3476 wmb();
3477
3478 /* set next_to_watch value indicating a packet is present */
3479 first->next_to_watch = tx_desc;
Anjali Singhai58044742015-09-25 18:26:13 -07003480
Alexander Duycka5e9c572013-09-28 06:00:27 +00003481 /* notify HW of packet */
Jacob Kellera5340d92017-08-29 05:32:42 -04003482 if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
Anjali Singhai58044742015-09-25 18:26:13 -07003483 writel(i, tx_ring->tail);
Alexander Duyck1dc8b532016-10-11 15:26:54 -07003484
3485 /* we need this if more than one processor can write to our tail
3486 * at a time, it synchronizes IO on IA64/Altix systems
3487 */
3488 mmiowb();
Anjali Singhai58044742015-09-25 18:26:13 -07003489 }
Alexander Duyck1dc8b532016-10-11 15:26:54 -07003490
Jacob Keller69077572017-05-03 10:28:54 -07003491 return 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003492
3493dma_error:
Alexander Duycka5e9c572013-09-28 06:00:27 +00003494 dev_info(tx_ring->dev, "TX DMA map failed\n");
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003495
3496 /* clear dma mappings for failed tx_bi map */
3497 for (;;) {
3498 tx_bi = &tx_ring->tx_bi[i];
Alexander Duycka5e9c572013-09-28 06:00:27 +00003499 i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003500 if (tx_bi == first)
3501 break;
3502 if (i == 0)
3503 i = tx_ring->count;
3504 i--;
3505 }
3506
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003507 tx_ring->next_to_use = i;
Jacob Keller69077572017-05-03 10:28:54 -07003508
3509 return -1;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003510}
3511
3512/**
Björn Töpel74608d12017-05-24 07:55:35 +02003513 * i40e_xmit_xdp_ring - transmits an XDP buffer to an XDP Tx ring
3514 * @xdp: data to transmit
3515 * @xdp_ring: XDP Tx ring
3516 **/
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +02003517static int i40e_xmit_xdp_ring(struct xdp_frame *xdpf,
Björn Töpel74608d12017-05-24 07:55:35 +02003518 struct i40e_ring *xdp_ring)
3519{
Björn Töpel74608d12017-05-24 07:55:35 +02003520 u16 i = xdp_ring->next_to_use;
3521 struct i40e_tx_buffer *tx_bi;
3522 struct i40e_tx_desc *tx_desc;
Jesper Dangaard Brouer44fa2db2018-04-17 16:46:37 +02003523 u32 size = xdpf->len;
Björn Töpel74608d12017-05-24 07:55:35 +02003524 dma_addr_t dma;
3525
3526 if (!unlikely(I40E_DESC_UNUSED(xdp_ring))) {
3527 xdp_ring->tx_stats.tx_busy++;
3528 return I40E_XDP_CONSUMED;
3529 }
3530
Jesper Dangaard Brouerb411ef12018-04-17 16:46:02 +02003531 dma = dma_map_single(xdp_ring->dev, xdpf->data, size, DMA_TO_DEVICE);
Björn Töpel74608d12017-05-24 07:55:35 +02003532 if (dma_mapping_error(xdp_ring->dev, dma))
3533 return I40E_XDP_CONSUMED;
3534
3535 tx_bi = &xdp_ring->tx_bi[i];
3536 tx_bi->bytecount = size;
3537 tx_bi->gso_segs = 1;
Jesper Dangaard Brouerb411ef12018-04-17 16:46:02 +02003538 tx_bi->xdpf = xdpf;
Björn Töpel74608d12017-05-24 07:55:35 +02003539
3540 /* record length, and DMA address */
3541 dma_unmap_len_set(tx_bi, len, size);
3542 dma_unmap_addr_set(tx_bi, dma, dma);
3543
3544 tx_desc = I40E_TX_DESC(xdp_ring, i);
3545 tx_desc->buffer_addr = cpu_to_le64(dma);
3546 tx_desc->cmd_type_offset_bsz = build_ctob(I40E_TX_DESC_CMD_ICRC
3547 | I40E_TXD_CMD,
3548 0, size, 0);
3549
3550 /* Make certain all of the status bits have been updated
3551 * before next_to_watch is written.
3552 */
3553 smp_wmb();
3554
3555 i++;
3556 if (i == xdp_ring->count)
3557 i = 0;
3558
3559 tx_bi->next_to_watch = tx_desc;
3560 xdp_ring->next_to_use = i;
3561
3562 return I40E_XDP_TX;
3563}
3564
3565/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003566 * i40e_xmit_frame_ring - Sends buffer on Tx ring
3567 * @skb: send buffer
3568 * @tx_ring: ring to send buffer on
3569 *
3570 * Returns NETDEV_TX_OK if sent, else an error code
3571 **/
3572static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
3573 struct i40e_ring *tx_ring)
3574{
3575 u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
3576 u32 cd_tunneling = 0, cd_l2tag2 = 0;
3577 struct i40e_tx_buffer *first;
3578 u32 td_offset = 0;
3579 u32 tx_flags = 0;
3580 __be16 protocol;
3581 u32 td_cmd = 0;
3582 u8 hdr_len = 0;
Alexander Duyck4ec441d2016-02-17 11:02:43 -08003583 int tso, count;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003584 int tsyn;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003585
Jesse Brandeburgb74118f2015-10-26 19:44:30 -04003586 /* prefetch the data, we'll need it later */
3587 prefetch(skb->data);
3588
Scott Petersoned0980c2017-04-13 04:45:44 -04003589 i40e_trace(xmit_frame_ring, skb, tx_ring);
3590
Alexander Duyck4ec441d2016-02-17 11:02:43 -08003591 count = i40e_xmit_descriptor_count(skb);
Alexander Duyck2d374902016-02-17 11:02:50 -08003592 if (i40e_chk_linearize(skb, count)) {
Alexander Duyck52ea3e82016-11-28 16:05:59 -08003593 if (__skb_linearize(skb)) {
3594 dev_kfree_skb_any(skb);
3595 return NETDEV_TX_OK;
3596 }
Alexander Duyck5c4654d2016-02-19 12:17:08 -08003597 count = i40e_txd_use_count(skb->len);
Alexander Duyck2d374902016-02-17 11:02:50 -08003598 tx_ring->tx_stats.tx_linearize++;
3599 }
Alexander Duyck4ec441d2016-02-17 11:02:43 -08003600
3601 /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
3602 * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
3603 * + 4 desc gap to avoid the cache line where head is,
3604 * + 1 desc for context descriptor,
3605 * otherwise try next time
3606 */
3607 if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
3608 tx_ring->tx_stats.tx_busy++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003609 return NETDEV_TX_BUSY;
Alexander Duyck4ec441d2016-02-17 11:02:43 -08003610 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003611
Alexander Duyck52ea3e82016-11-28 16:05:59 -08003612 /* record the location of the first descriptor for this packet */
3613 first = &tx_ring->tx_bi[tx_ring->next_to_use];
3614 first->skb = skb;
3615 first->bytecount = skb->len;
3616 first->gso_segs = 1;
3617
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003618 /* prepare the xmit flags */
3619 if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
3620 goto out_drop;
3621
3622 /* obtain protocol of skb */
Vlad Yasevich3d34dd02014-08-25 10:34:52 -04003623 protocol = vlan_get_protocol(skb);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003624
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003625 /* setup IPv4/IPv6 offloads */
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00003626 if (protocol == htons(ETH_P_IP))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003627 tx_flags |= I40E_TX_FLAGS_IPV4;
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00003628 else if (protocol == htons(ETH_P_IPV6))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003629 tx_flags |= I40E_TX_FLAGS_IPV6;
3630
Alexander Duyck52ea3e82016-11-28 16:05:59 -08003631 tso = i40e_tso(first, &hdr_len, &cd_type_cmd_tso_mss);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003632
3633 if (tso < 0)
3634 goto out_drop;
3635 else if (tso)
3636 tx_flags |= I40E_TX_FLAGS_TSO;
3637
Alexander Duyck3bc67972016-02-17 11:02:56 -08003638 /* Always offload the checksum, since it's in the data descriptor */
3639 tso = i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
3640 tx_ring, &cd_tunneling);
3641 if (tso < 0)
3642 goto out_drop;
3643
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003644 tsyn = i40e_tsyn(tx_ring, skb, tx_flags, &cd_type_cmd_tso_mss);
3645
3646 if (tsyn)
3647 tx_flags |= I40E_TX_FLAGS_TSYN;
3648
Jakub Kicinski259afec2014-03-15 14:55:37 +00003649 skb_tx_timestamp(skb);
3650
Alexander Duyckb1941302013-09-28 06:00:32 +00003651 /* always enable CRC insertion offload */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003652 td_cmd |= I40E_TX_DESC_CMD_ICRC;
3653
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003654 i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
3655 cd_tunneling, cd_l2tag2);
3656
3657 /* Add Flow Director ATR if it's enabled.
3658 *
3659 * NOTE: this must always be directly before the data descriptor.
3660 */
Alexander Duyck6b037cd2016-01-24 21:17:36 -08003661 i40e_atr(tx_ring, skb, tx_flags);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003662
Jacob Keller69077572017-05-03 10:28:54 -07003663 if (i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
3664 td_cmd, td_offset))
3665 goto cleanup_tx_tstamp;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003666
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003667 return NETDEV_TX_OK;
3668
3669out_drop:
Scott Petersoned0980c2017-04-13 04:45:44 -04003670 i40e_trace(xmit_frame_ring_drop, first->skb, tx_ring);
Alexander Duyck52ea3e82016-11-28 16:05:59 -08003671 dev_kfree_skb_any(first->skb);
3672 first->skb = NULL;
Jacob Keller69077572017-05-03 10:28:54 -07003673cleanup_tx_tstamp:
3674 if (unlikely(tx_flags & I40E_TX_FLAGS_TSYN)) {
3675 struct i40e_pf *pf = i40e_netdev_to_pf(tx_ring->netdev);
3676
3677 dev_kfree_skb_any(pf->ptp_tx_skb);
3678 pf->ptp_tx_skb = NULL;
3679 clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
3680 }
3681
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003682 return NETDEV_TX_OK;
3683}
3684
3685/**
3686 * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
3687 * @skb: send buffer
3688 * @netdev: network interface device structure
3689 *
3690 * Returns NETDEV_TX_OK if sent, else an error code
3691 **/
3692netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3693{
3694 struct i40e_netdev_priv *np = netdev_priv(netdev);
3695 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck9f65e152013-09-28 06:00:58 +00003696 struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003697
3698 /* hardware can't handle really short frames, hardware padding works
3699 * beyond this point
3700 */
Alexander Duycka94d9e22014-12-03 08:17:39 -08003701 if (skb_put_padto(skb, I40E_MIN_TX_LEN))
3702 return NETDEV_TX_OK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003703
3704 return i40e_xmit_frame_ring(skb, tx_ring);
3705}
Björn Töpeld9314c472018-03-22 16:14:34 +01003706
3707/**
3708 * i40e_xdp_xmit - Implements ndo_xdp_xmit
3709 * @dev: netdev
3710 * @xdp: XDP buffer
3711 *
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02003712 * Returns number of frames successfully sent. Frames that fail are
3713 * free'ed via XDP return API.
3714 *
3715 * For error cases, a negative errno code is returned and no-frames
3716 * are transmitted (caller must handle freeing frames).
Björn Töpeld9314c472018-03-22 16:14:34 +01003717 **/
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +02003718int i40e_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
3719 u32 flags)
Björn Töpeld9314c472018-03-22 16:14:34 +01003720{
3721 struct i40e_netdev_priv *np = netdev_priv(dev);
3722 unsigned int queue_index = smp_processor_id();
3723 struct i40e_vsi *vsi = np->vsi;
Jesper Dangaard Brouercdb57ed2018-05-31 10:59:52 +02003724 struct i40e_ring *xdp_ring;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02003725 int drops = 0;
3726 int i;
Björn Töpeld9314c472018-03-22 16:14:34 +01003727
3728 if (test_bit(__I40E_VSI_DOWN, vsi->state))
3729 return -ENETDOWN;
3730
3731 if (!i40e_enabled_xdp_vsi(vsi) || queue_index >= vsi->num_queue_pairs)
3732 return -ENXIO;
3733
Jesper Dangaard Brouercdb57ed2018-05-31 10:59:52 +02003734 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
Jesper Dangaard Brouer42b33462018-05-31 10:59:47 +02003735 return -EINVAL;
3736
Jesper Dangaard Brouercdb57ed2018-05-31 10:59:52 +02003737 xdp_ring = vsi->xdp_rings[queue_index];
3738
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02003739 for (i = 0; i < n; i++) {
3740 struct xdp_frame *xdpf = frames[i];
3741 int err;
Björn Töpeld9314c472018-03-22 16:14:34 +01003742
Jesper Dangaard Brouercdb57ed2018-05-31 10:59:52 +02003743 err = i40e_xmit_xdp_ring(xdpf, xdp_ring);
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02003744 if (err != I40E_XDP_TX) {
3745 xdp_return_frame_rx_napi(xdpf);
3746 drops++;
3747 }
3748 }
3749
Jesper Dangaard Brouercdb57ed2018-05-31 10:59:52 +02003750 if (unlikely(flags & XDP_XMIT_FLUSH))
3751 i40e_xdp_ring_update_tail(xdp_ring);
3752
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +02003753 return n - drops;
Björn Töpeld9314c472018-03-22 16:14:34 +01003754}