blob: 855ae1e359dffa66a703e69d8460d671710803f1 [file] [log] [blame]
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -08004 * Copyright(c) 2013 - 2016 Intel Corporation.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
Mitch Williams1c112a62014-04-04 04:43:06 +000027#include <linux/prefetch.h>
Mitch Williamsa132af22015-01-24 09:58:35 +000028#include <net/busy_poll.h>
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000029#include "i40e.h"
Jesse Brandeburg206812b2014-02-12 01:45:33 +000030#include "i40e_prototype.h"
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000031
32static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
33 u32 td_tag)
34{
35 return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
36 ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
37 ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
38 ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
39 ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
40}
41
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000042#define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
Alexander Duyck5e02f282016-09-12 14:18:41 -070043/**
44 * i40e_fdir - Generate a Flow Director descriptor based on fdata
45 * @tx_ring: Tx ring to send buffer on
46 * @fdata: Flow director filter data
47 * @add: Indicate if we are adding a rule or deleting one
48 *
49 **/
50static void i40e_fdir(struct i40e_ring *tx_ring,
51 struct i40e_fdir_filter *fdata, bool add)
52{
53 struct i40e_filter_program_desc *fdir_desc;
54 struct i40e_pf *pf = tx_ring->vsi->back;
55 u32 flex_ptype, dtype_cmd;
56 u16 i;
57
58 /* grab the next descriptor */
59 i = tx_ring->next_to_use;
60 fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
61
62 i++;
63 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
64
65 flex_ptype = I40E_TXD_FLTR_QW0_QINDEX_MASK &
66 (fdata->q_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT);
67
68 flex_ptype |= I40E_TXD_FLTR_QW0_FLEXOFF_MASK &
69 (fdata->flex_off << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT);
70
71 flex_ptype |= I40E_TXD_FLTR_QW0_PCTYPE_MASK &
72 (fdata->pctype << I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
73
Jacob Keller0e588de2017-02-06 14:38:50 -080074 flex_ptype |= I40E_TXD_FLTR_QW0_PCTYPE_MASK &
75 (fdata->flex_offset << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT);
76
Alexander Duyck5e02f282016-09-12 14:18:41 -070077 /* Use LAN VSI Id if not programmed by user */
78 flex_ptype |= I40E_TXD_FLTR_QW0_DEST_VSI_MASK &
79 ((u32)(fdata->dest_vsi ? : pf->vsi[pf->lan_vsi]->id) <<
80 I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT);
81
82 dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
83
84 dtype_cmd |= add ?
85 I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
86 I40E_TXD_FLTR_QW1_PCMD_SHIFT :
87 I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
88 I40E_TXD_FLTR_QW1_PCMD_SHIFT;
89
90 dtype_cmd |= I40E_TXD_FLTR_QW1_DEST_MASK &
91 (fdata->dest_ctl << I40E_TXD_FLTR_QW1_DEST_SHIFT);
92
93 dtype_cmd |= I40E_TXD_FLTR_QW1_FD_STATUS_MASK &
94 (fdata->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT);
95
96 if (fdata->cnt_index) {
97 dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
98 dtype_cmd |= I40E_TXD_FLTR_QW1_CNTINDEX_MASK &
99 ((u32)fdata->cnt_index <<
100 I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT);
101 }
102
103 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
104 fdir_desc->rsvd = cpu_to_le32(0);
105 fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
106 fdir_desc->fd_id = cpu_to_le32(fdata->fd_id);
107}
108
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000109#define I40E_FD_CLEAN_DELAY 10
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000110/**
111 * i40e_program_fdir_filter - Program a Flow Director filter
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000112 * @fdir_data: Packet data that will be filter parameters
113 * @raw_packet: the pre-allocated packet buffer for FDir
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000114 * @pf: The PF pointer
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000115 * @add: True for add/update, False for remove
116 **/
Alexander Duyck1eb846a2016-09-12 14:18:42 -0700117static int i40e_program_fdir_filter(struct i40e_fdir_filter *fdir_data,
118 u8 *raw_packet, struct i40e_pf *pf,
119 bool add)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000120{
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000121 struct i40e_tx_buffer *tx_buf, *first;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000122 struct i40e_tx_desc *tx_desc;
123 struct i40e_ring *tx_ring;
124 struct i40e_vsi *vsi;
125 struct device *dev;
126 dma_addr_t dma;
127 u32 td_cmd = 0;
128 u16 i;
129
130 /* find existing FDIR VSI */
Alexander Duyck4b816442016-10-11 15:26:53 -0700131 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000132 if (!vsi)
133 return -ENOENT;
134
Alexander Duyck9f65e152013-09-28 06:00:58 +0000135 tx_ring = vsi->tx_rings[0];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000136 dev = tx_ring->dev;
137
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000138 /* we need two descriptors to add/del a filter and we can wait */
Alexander Duycked245402016-09-14 16:24:32 -0700139 for (i = I40E_FD_CLEAN_DELAY; I40E_DESC_UNUSED(tx_ring) < 2; i--) {
140 if (!i)
141 return -EAGAIN;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000142 msleep_interruptible(1);
Alexander Duycked245402016-09-14 16:24:32 -0700143 }
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000144
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000145 dma = dma_map_single(dev, raw_packet,
146 I40E_FDIR_MAX_RAW_PACKET_SIZE, DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000147 if (dma_mapping_error(dev, dma))
148 goto dma_fail;
149
150 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000151 i = tx_ring->next_to_use;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000152 first = &tx_ring->tx_bi[i];
Alexander Duyck5e02f282016-09-12 14:18:41 -0700153 i40e_fdir(tx_ring, fdir_data, add);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000154
155 /* Now program a dummy descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000156 i = tx_ring->next_to_use;
157 tx_desc = I40E_TX_DESC(tx_ring, i);
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000158 tx_buf = &tx_ring->tx_bi[i];
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000159
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000160 tx_ring->next_to_use = ((i + 1) < tx_ring->count) ? i + 1 : 0;
161
162 memset(tx_buf, 0, sizeof(struct i40e_tx_buffer));
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000163
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000164 /* record length, and DMA address */
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000165 dma_unmap_len_set(tx_buf, len, I40E_FDIR_MAX_RAW_PACKET_SIZE);
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000166 dma_unmap_addr_set(tx_buf, dma, dma);
167
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000168 tx_desc->buffer_addr = cpu_to_le64(dma);
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000169 td_cmd = I40E_TXD_CMD | I40E_TX_DESC_CMD_DUMMY;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000170
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000171 tx_buf->tx_flags = I40E_TX_FLAGS_FD_SB;
172 tx_buf->raw_buf = (void *)raw_packet;
173
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000174 tx_desc->cmd_type_offset_bsz =
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000175 build_ctob(td_cmd, 0, I40E_FDIR_MAX_RAW_PACKET_SIZE, 0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000176
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000177 /* Force memory writes to complete before letting h/w
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000178 * know there are new descriptors to fetch.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000179 */
180 wmb();
181
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000182 /* Mark the data descriptor to be watched */
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000183 first->next_to_watch = tx_desc;
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000184
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000185 writel(tx_ring->next_to_use, tx_ring->tail);
186 return 0;
187
188dma_fail:
189 return -1;
190}
191
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000192#define IP_HEADER_OFFSET 14
193#define I40E_UDPIP_DUMMY_PACKET_LEN 42
194/**
195 * i40e_add_del_fdir_udpv4 - Add/Remove UDPv4 filters
196 * @vsi: pointer to the targeted VSI
197 * @fd_data: the flow director data required for the FDir descriptor
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000198 * @add: true adds a filter, false removes it
199 *
200 * Returns 0 if the filters were successfully added or removed
201 **/
202static int i40e_add_del_fdir_udpv4(struct i40e_vsi *vsi,
203 struct i40e_fdir_filter *fd_data,
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000204 bool add)
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000205{
206 struct i40e_pf *pf = vsi->back;
207 struct udphdr *udp;
208 struct iphdr *ip;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000209 u8 *raw_packet;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000210 int ret;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000211 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
212 0x45, 0, 0, 0x1c, 0, 0, 0x40, 0, 0x40, 0x11, 0, 0, 0, 0, 0, 0,
213 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
214
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000215 raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
216 if (!raw_packet)
217 return -ENOMEM;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000218 memcpy(raw_packet, packet, I40E_UDPIP_DUMMY_PACKET_LEN);
219
220 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
221 udp = (struct udphdr *)(raw_packet + IP_HEADER_OFFSET
222 + sizeof(struct iphdr));
223
Jacob Keller8ce43dc2017-02-06 14:38:39 -0800224 ip->daddr = fd_data->dst_ip;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000225 udp->dest = fd_data->dst_port;
Jacob Keller8ce43dc2017-02-06 14:38:39 -0800226 ip->saddr = fd_data->src_ip;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000227 udp->source = fd_data->src_port;
228
Jacob Keller0e588de2017-02-06 14:38:50 -0800229 if (fd_data->flex_filter) {
230 u8 *payload = raw_packet + I40E_UDPIP_DUMMY_PACKET_LEN;
231 __be16 pattern = fd_data->flex_word;
232 u16 off = fd_data->flex_offset;
233
234 *((__force __be16 *)(payload + off)) = pattern;
235 }
236
Kevin Scottb2d36c02014-04-09 05:58:59 +0000237 fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
238 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
239 if (ret) {
240 dev_info(&pf->pdev->dev,
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000241 "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
242 fd_data->pctype, fd_data->fd_id, ret);
Jacob Kellere5187ee2017-02-06 14:38:41 -0800243 /* Free the packet buffer since it wasn't added to the ring */
244 kfree(raw_packet);
245 return -EOPNOTSUPP;
Anjali Singhai Jain4205d372015-02-27 09:15:27 +0000246 } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
Anjali Singhai Jainf7233c52014-07-09 07:46:16 +0000247 if (add)
248 dev_info(&pf->pdev->dev,
249 "Filter OK for PCTYPE %d loc = %d\n",
250 fd_data->pctype, fd_data->fd_id);
251 else
252 dev_info(&pf->pdev->dev,
253 "Filter deleted for PCTYPE %d loc = %d\n",
254 fd_data->pctype, fd_data->fd_id);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000255 }
Kiran Patila42e7a32015-11-06 15:26:03 -0800256
Jacob Keller097dbf52017-02-06 14:38:46 -0800257 if (add)
258 pf->fd_udp4_filter_cnt++;
259 else
260 pf->fd_udp4_filter_cnt--;
261
Jacob Kellere5187ee2017-02-06 14:38:41 -0800262 return 0;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000263}
264
265#define I40E_TCPIP_DUMMY_PACKET_LEN 54
266/**
267 * i40e_add_del_fdir_tcpv4 - Add/Remove TCPv4 filters
268 * @vsi: pointer to the targeted VSI
269 * @fd_data: the flow director data required for the FDir descriptor
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000270 * @add: true adds a filter, false removes it
271 *
272 * Returns 0 if the filters were successfully added or removed
273 **/
274static int i40e_add_del_fdir_tcpv4(struct i40e_vsi *vsi,
275 struct i40e_fdir_filter *fd_data,
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000276 bool add)
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000277{
278 struct i40e_pf *pf = vsi->back;
279 struct tcphdr *tcp;
280 struct iphdr *ip;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000281 u8 *raw_packet;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000282 int ret;
283 /* Dummy packet */
284 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
285 0x45, 0, 0, 0x28, 0, 0, 0x40, 0, 0x40, 0x6, 0, 0, 0, 0, 0, 0,
286 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0x11,
287 0x0, 0x72, 0, 0, 0, 0};
288
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000289 raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
290 if (!raw_packet)
291 return -ENOMEM;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000292 memcpy(raw_packet, packet, I40E_TCPIP_DUMMY_PACKET_LEN);
293
294 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
295 tcp = (struct tcphdr *)(raw_packet + IP_HEADER_OFFSET
296 + sizeof(struct iphdr));
297
Jacob Keller8ce43dc2017-02-06 14:38:39 -0800298 ip->daddr = fd_data->dst_ip;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000299 tcp->dest = fd_data->dst_port;
Jacob Keller8ce43dc2017-02-06 14:38:39 -0800300 ip->saddr = fd_data->src_ip;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000301 tcp->source = fd_data->src_port;
302
Jacob Keller0e588de2017-02-06 14:38:50 -0800303 if (fd_data->flex_filter) {
304 u8 *payload = raw_packet + I40E_TCPIP_DUMMY_PACKET_LEN;
305 __be16 pattern = fd_data->flex_word;
306 u16 off = fd_data->flex_offset;
307
308 *((__force __be16 *)(payload + off)) = pattern;
309 }
310
Kevin Scottb2d36c02014-04-09 05:58:59 +0000311 fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000312 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000313 if (ret) {
314 dev_info(&pf->pdev->dev,
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000315 "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
316 fd_data->pctype, fd_data->fd_id, ret);
Jacob Kellere5187ee2017-02-06 14:38:41 -0800317 /* Free the packet buffer since it wasn't added to the ring */
318 kfree(raw_packet);
319 return -EOPNOTSUPP;
Anjali Singhai Jain4205d372015-02-27 09:15:27 +0000320 } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
Anjali Singhai Jainf7233c52014-07-09 07:46:16 +0000321 if (add)
322 dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d loc = %d)\n",
323 fd_data->pctype, fd_data->fd_id);
324 else
325 dev_info(&pf->pdev->dev,
326 "Filter deleted for PCTYPE %d loc = %d\n",
327 fd_data->pctype, fd_data->fd_id);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000328 }
329
Jacob Keller377cc242017-02-06 14:38:42 -0800330 if (add) {
Jacob Keller097dbf52017-02-06 14:38:46 -0800331 pf->fd_tcp4_filter_cnt++;
Jacob Keller377cc242017-02-06 14:38:42 -0800332 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
333 I40E_DEBUG_FD & pf->hw.debug_mask)
334 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 flow being applied\n");
335 pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
336 } else {
Jacob Keller097dbf52017-02-06 14:38:46 -0800337 pf->fd_tcp4_filter_cnt--;
338 if (pf->fd_tcp4_filter_cnt == 0) {
Jacob Keller377cc242017-02-06 14:38:42 -0800339 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
340 I40E_DEBUG_FD & pf->hw.debug_mask)
341 dev_info(&pf->pdev->dev, "ATR re-enabled due to no sideband TCP/IPv4 rules\n");
342 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
343 }
344 }
345
Jacob Kellere5187ee2017-02-06 14:38:41 -0800346 return 0;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000347}
348
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000349#define I40E_IP_DUMMY_PACKET_LEN 34
350/**
351 * i40e_add_del_fdir_ipv4 - Add/Remove IPv4 Flow Director filters for
352 * a specific flow spec
353 * @vsi: pointer to the targeted VSI
354 * @fd_data: the flow director data required for the FDir descriptor
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000355 * @add: true adds a filter, false removes it
356 *
357 * Returns 0 if the filters were successfully added or removed
358 **/
359static int i40e_add_del_fdir_ipv4(struct i40e_vsi *vsi,
360 struct i40e_fdir_filter *fd_data,
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000361 bool add)
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000362{
363 struct i40e_pf *pf = vsi->back;
364 struct iphdr *ip;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000365 u8 *raw_packet;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000366 int ret;
367 int i;
368 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
369 0x45, 0, 0, 0x14, 0, 0, 0x40, 0, 0x40, 0x10, 0, 0, 0, 0, 0, 0,
370 0, 0, 0, 0};
371
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000372 for (i = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
373 i <= I40E_FILTER_PCTYPE_FRAG_IPV4; i++) {
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000374 raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
375 if (!raw_packet)
376 return -ENOMEM;
377 memcpy(raw_packet, packet, I40E_IP_DUMMY_PACKET_LEN);
378 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
379
Jacob Keller8ce43dc2017-02-06 14:38:39 -0800380 ip->saddr = fd_data->src_ip;
381 ip->daddr = fd_data->dst_ip;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000382 ip->protocol = 0;
383
Jacob Keller0e588de2017-02-06 14:38:50 -0800384 if (fd_data->flex_filter) {
385 u8 *payload = raw_packet + I40E_IP_DUMMY_PACKET_LEN;
386 __be16 pattern = fd_data->flex_word;
387 u16 off = fd_data->flex_offset;
388
389 *((__force __be16 *)(payload + off)) = pattern;
390 }
391
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000392 fd_data->pctype = i;
393 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000394 if (ret) {
395 dev_info(&pf->pdev->dev,
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000396 "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
397 fd_data->pctype, fd_data->fd_id, ret);
Jacob Kellere5187ee2017-02-06 14:38:41 -0800398 /* The packet buffer wasn't added to the ring so we
399 * need to free it now.
400 */
401 kfree(raw_packet);
402 return -EOPNOTSUPP;
Anjali Singhai Jain4205d372015-02-27 09:15:27 +0000403 } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
Anjali Singhai Jainf7233c52014-07-09 07:46:16 +0000404 if (add)
405 dev_info(&pf->pdev->dev,
406 "Filter OK for PCTYPE %d loc = %d\n",
407 fd_data->pctype, fd_data->fd_id);
408 else
409 dev_info(&pf->pdev->dev,
410 "Filter deleted for PCTYPE %d loc = %d\n",
411 fd_data->pctype, fd_data->fd_id);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000412 }
413 }
414
Jacob Keller097dbf52017-02-06 14:38:46 -0800415 if (add)
416 pf->fd_ip4_filter_cnt++;
417 else
418 pf->fd_ip4_filter_cnt--;
419
Jacob Kellere5187ee2017-02-06 14:38:41 -0800420 return 0;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000421}
422
423/**
424 * i40e_add_del_fdir - Build raw packets to add/del fdir filter
425 * @vsi: pointer to the targeted VSI
426 * @cmd: command to get or set RX flow classification rules
427 * @add: true adds a filter, false removes it
428 *
429 **/
430int i40e_add_del_fdir(struct i40e_vsi *vsi,
431 struct i40e_fdir_filter *input, bool add)
432{
433 struct i40e_pf *pf = vsi->back;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000434 int ret;
435
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000436 switch (input->flow_type & ~FLOW_EXT) {
437 case TCP_V4_FLOW:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000438 ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000439 break;
440 case UDP_V4_FLOW:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000441 ret = i40e_add_del_fdir_udpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000442 break;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000443 case IP_USER_FLOW:
444 switch (input->ip4_proto) {
445 case IPPROTO_TCP:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000446 ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000447 break;
448 case IPPROTO_UDP:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000449 ret = i40e_add_del_fdir_udpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000450 break;
Alexander Duycke1da71c2016-09-14 16:24:35 -0700451 case IPPROTO_IP:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000452 ret = i40e_add_del_fdir_ipv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000453 break;
Alexander Duycke1da71c2016-09-14 16:24:35 -0700454 default:
455 /* We cannot support masking based on protocol */
456 goto unsupported_flow;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000457 }
458 break;
459 default:
Alexander Duycke1da71c2016-09-14 16:24:35 -0700460unsupported_flow:
Jakub Kicinskic5ffe7e2014-04-02 10:33:22 +0000461 dev_info(&pf->pdev->dev, "Could not specify spec type %d\n",
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000462 input->flow_type);
463 ret = -EINVAL;
464 }
465
Jacob Kellera158aea2017-02-09 23:44:27 -0800466 /* The buffer allocated here will be normally be freed by
467 * i40e_clean_fdir_tx_irq() as it reclaims resources after transmit
468 * completion. In the event of an error adding the buffer to the FDIR
469 * ring, it will immediately be freed. It may also be freed by
470 * i40e_clean_tx_ring() when closing the VSI.
471 */
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000472 return ret;
473}
474
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000475/**
476 * i40e_fd_handle_status - check the Programming Status for FD
477 * @rx_ring: the Rx ring for this descriptor
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000478 * @rx_desc: the Rx descriptor for programming Status, not a packet descriptor.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000479 * @prog_id: the id originally used for programming
480 *
481 * This is used to verify if the FD programming or invalidation
482 * requested by SW to the HW is successful or not and take actions accordingly.
483 **/
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000484static void i40e_fd_handle_status(struct i40e_ring *rx_ring,
485 union i40e_rx_desc *rx_desc, u8 prog_id)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000486{
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000487 struct i40e_pf *pf = rx_ring->vsi->back;
488 struct pci_dev *pdev = pf->pdev;
489 u32 fcnt_prog, fcnt_avail;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000490 u32 error;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000491 u64 qw;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000492
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000493 qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000494 error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
495 I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
496
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400497 if (error == BIT(I40E_RX_PROG_STATUS_DESC_FD_TBL_FULL_SHIFT)) {
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -0400498 pf->fd_inv = le32_to_cpu(rx_desc->wb.qword0.hi_dword.fd_id);
Anjali Singhai Jainf7233c52014-07-09 07:46:16 +0000499 if ((rx_desc->wb.qword0.hi_dword.fd_id != 0) ||
500 (I40E_DEBUG_FD & pf->hw.debug_mask))
501 dev_warn(&pdev->dev, "ntuple filter loc = %d, could not be added\n",
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -0400502 pf->fd_inv);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000503
Anjali Singhai Jain04294e32015-02-27 09:15:28 +0000504 /* Check if the programming error is for ATR.
505 * If so, auto disable ATR and set a state for
506 * flush in progress. Next time we come here if flush is in
507 * progress do nothing, once flush is complete the state will
508 * be cleared.
509 */
510 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
511 return;
512
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +0000513 pf->fd_add_err++;
514 /* store the current atr filter count */
515 pf->fd_atr_cnt = i40e_get_current_atr_cnt(pf);
516
Anjali Singhai Jain04294e32015-02-27 09:15:28 +0000517 if ((rx_desc->wb.qword0.hi_dword.fd_id == 0) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -0800518 (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
519 pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +0000520 set_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
521 }
522
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000523 /* filter programming failed most likely due to table full */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +0000524 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +0000525 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000526 /* If ATR is running fcnt_prog can quickly change,
527 * if we are very close to full, it makes sense to disable
528 * FD ATR/SB and then re-enable it when there is room.
529 */
530 if (fcnt_prog >= (fcnt_avail - I40E_FDIR_BUFFER_FULL_MARGIN)) {
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +0000531 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -0800532 !(pf->hw_disabled_flags &
Anjali Singhai Jainb814ba62014-06-04 20:41:48 +0000533 I40E_FLAG_FD_SB_ENABLED)) {
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -0400534 if (I40E_DEBUG_FD & pf->hw.debug_mask)
535 dev_warn(&pdev->dev, "FD filter space full, new ntuple rules will not be added\n");
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -0800536 pf->hw_disabled_flags |=
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000537 I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000538 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000539 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400540 } else if (error == BIT(I40E_RX_PROG_STATUS_DESC_NO_FD_ENTRY_SHIFT)) {
Anjali Singhai Jain13c28842014-03-06 09:00:04 +0000541 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000542 dev_info(&pdev->dev, "ntuple filter fd_id = %d, could not be removed\n",
Anjali Singhai Jain13c28842014-03-06 09:00:04 +0000543 rx_desc->wb.qword0.hi_dword.fd_id);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000544 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000545}
546
547/**
Alexander Duycka5e9c572013-09-28 06:00:27 +0000548 * i40e_unmap_and_free_tx_resource - Release a Tx buffer
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000549 * @ring: the ring that owns the buffer
550 * @tx_buffer: the buffer to free
551 **/
Alexander Duycka5e9c572013-09-28 06:00:27 +0000552static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
553 struct i40e_tx_buffer *tx_buffer)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000554{
Alexander Duycka5e9c572013-09-28 06:00:27 +0000555 if (tx_buffer->skb) {
Alexander Duyck64bfd682016-09-12 14:18:39 -0700556 if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
557 kfree(tx_buffer->raw_buf);
558 else
559 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000560 if (dma_unmap_len(tx_buffer, len))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000561 dma_unmap_single(ring->dev,
Alexander Duyck35a1e2a2013-09-28 06:00:17 +0000562 dma_unmap_addr(tx_buffer, dma),
563 dma_unmap_len(tx_buffer, len),
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000564 DMA_TO_DEVICE);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000565 } else if (dma_unmap_len(tx_buffer, len)) {
566 dma_unmap_page(ring->dev,
567 dma_unmap_addr(tx_buffer, dma),
568 dma_unmap_len(tx_buffer, len),
569 DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000570 }
Kiran Patila42e7a32015-11-06 15:26:03 -0800571
Alexander Duycka5e9c572013-09-28 06:00:27 +0000572 tx_buffer->next_to_watch = NULL;
573 tx_buffer->skb = NULL;
Alexander Duyck35a1e2a2013-09-28 06:00:17 +0000574 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000575 /* tx_buffer must be completely set up in the transmit path */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000576}
577
578/**
579 * i40e_clean_tx_ring - Free any empty Tx buffers
580 * @tx_ring: ring to be cleaned
581 **/
582void i40e_clean_tx_ring(struct i40e_ring *tx_ring)
583{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000584 unsigned long bi_size;
585 u16 i;
586
587 /* ring already cleared, nothing to do */
588 if (!tx_ring->tx_bi)
589 return;
590
591 /* Free all the Tx ring sk_buffs */
Alexander Duycka5e9c572013-09-28 06:00:27 +0000592 for (i = 0; i < tx_ring->count; i++)
593 i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000594
595 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
596 memset(tx_ring->tx_bi, 0, bi_size);
597
598 /* Zero out the descriptor ring */
599 memset(tx_ring->desc, 0, tx_ring->size);
600
601 tx_ring->next_to_use = 0;
602 tx_ring->next_to_clean = 0;
Alexander Duyck7070ce02013-09-28 06:00:37 +0000603
604 if (!tx_ring->netdev)
605 return;
606
607 /* cleanup Tx queue statistics */
Alexander Duycke486bdf2016-09-12 14:18:40 -0700608 netdev_tx_reset_queue(txring_txq(tx_ring));
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000609}
610
611/**
612 * i40e_free_tx_resources - Free Tx resources per queue
613 * @tx_ring: Tx descriptor ring for a specific queue
614 *
615 * Free all transmit software resources
616 **/
617void i40e_free_tx_resources(struct i40e_ring *tx_ring)
618{
619 i40e_clean_tx_ring(tx_ring);
620 kfree(tx_ring->tx_bi);
621 tx_ring->tx_bi = NULL;
622
623 if (tx_ring->desc) {
624 dma_free_coherent(tx_ring->dev, tx_ring->size,
625 tx_ring->desc, tx_ring->dma);
626 tx_ring->desc = NULL;
627 }
628}
629
Jesse Brandeburga68de582015-02-24 05:26:03 +0000630/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000631 * i40e_get_tx_pending - how many tx descriptors not processed
632 * @tx_ring: the ring of descriptors
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800633 * @in_sw: is tx_pending being checked in SW or HW
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000634 *
635 * Since there is no access to the ring head register
636 * in XL710, we need to use our local copies
637 **/
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800638u32 i40e_get_tx_pending(struct i40e_ring *ring, bool in_sw)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000639{
Jesse Brandeburga68de582015-02-24 05:26:03 +0000640 u32 head, tail;
641
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800642 if (!in_sw)
643 head = i40e_get_head(ring);
644 else
645 head = ring->next_to_clean;
Jesse Brandeburga68de582015-02-24 05:26:03 +0000646 tail = readl(ring->tail);
647
648 if (head != tail)
649 return (head < tail) ?
650 tail - head : (tail + ring->count - head);
651
652 return 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000653}
654
Alexander Duyck1dc8b532016-10-11 15:26:54 -0700655#define WB_STRIDE 4
Jesse Brandeburgd91649f2015-01-07 02:55:01 +0000656
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000657/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000658 * i40e_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duycka619afe2016-03-07 09:30:03 -0800659 * @vsi: the VSI we care about
660 * @tx_ring: Tx ring to clean
661 * @napi_budget: Used to determine if we are in netpoll
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000662 *
663 * Returns true if there's any budget left (e.g. the clean is finished)
664 **/
Alexander Duycka619afe2016-03-07 09:30:03 -0800665static bool i40e_clean_tx_irq(struct i40e_vsi *vsi,
666 struct i40e_ring *tx_ring, int napi_budget)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000667{
668 u16 i = tx_ring->next_to_clean;
669 struct i40e_tx_buffer *tx_buf;
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000670 struct i40e_tx_desc *tx_head;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000671 struct i40e_tx_desc *tx_desc;
Alexander Duycka619afe2016-03-07 09:30:03 -0800672 unsigned int total_bytes = 0, total_packets = 0;
673 unsigned int budget = vsi->work_limit;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000674
675 tx_buf = &tx_ring->tx_bi[i];
676 tx_desc = I40E_TX_DESC(tx_ring, i);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000677 i -= tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000678
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000679 tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
680
Alexander Duycka5e9c572013-09-28 06:00:27 +0000681 do {
682 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000683
684 /* if next_to_watch is not set then there is no work pending */
685 if (!eop_desc)
686 break;
687
Alexander Duycka5e9c572013-09-28 06:00:27 +0000688 /* prevent any other reads prior to eop_desc */
689 read_barrier_depends();
690
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000691 /* we have caught up to head, no work left to do */
692 if (tx_head == tx_desc)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000693 break;
694
Alexander Duyckc304fda2013-09-28 06:00:12 +0000695 /* clear next_to_watch to prevent false hangs */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000696 tx_buf->next_to_watch = NULL;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000697
Alexander Duycka5e9c572013-09-28 06:00:27 +0000698 /* update the statistics for this packet */
699 total_bytes += tx_buf->bytecount;
700 total_packets += tx_buf->gso_segs;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000701
Alexander Duycka5e9c572013-09-28 06:00:27 +0000702 /* free the skb */
Alexander Duycka619afe2016-03-07 09:30:03 -0800703 napi_consume_skb(tx_buf->skb, napi_budget);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000704
Alexander Duycka5e9c572013-09-28 06:00:27 +0000705 /* unmap skb header data */
706 dma_unmap_single(tx_ring->dev,
707 dma_unmap_addr(tx_buf, dma),
708 dma_unmap_len(tx_buf, len),
709 DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000710
Alexander Duycka5e9c572013-09-28 06:00:27 +0000711 /* clear tx_buffer data */
712 tx_buf->skb = NULL;
713 dma_unmap_len_set(tx_buf, len, 0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000714
Alexander Duycka5e9c572013-09-28 06:00:27 +0000715 /* unmap remaining buffers */
716 while (tx_desc != eop_desc) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000717
718 tx_buf++;
719 tx_desc++;
720 i++;
Alexander Duycka5e9c572013-09-28 06:00:27 +0000721 if (unlikely(!i)) {
722 i -= tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000723 tx_buf = tx_ring->tx_bi;
724 tx_desc = I40E_TX_DESC(tx_ring, 0);
725 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000726
Alexander Duycka5e9c572013-09-28 06:00:27 +0000727 /* unmap any remaining paged data */
728 if (dma_unmap_len(tx_buf, len)) {
729 dma_unmap_page(tx_ring->dev,
730 dma_unmap_addr(tx_buf, dma),
731 dma_unmap_len(tx_buf, len),
732 DMA_TO_DEVICE);
733 dma_unmap_len_set(tx_buf, len, 0);
734 }
735 }
736
737 /* move us one more past the eop_desc for start of next pkt */
738 tx_buf++;
739 tx_desc++;
740 i++;
741 if (unlikely(!i)) {
742 i -= tx_ring->count;
743 tx_buf = tx_ring->tx_bi;
744 tx_desc = I40E_TX_DESC(tx_ring, 0);
745 }
746
Jesse Brandeburg016890b2015-02-27 09:15:31 +0000747 prefetch(tx_desc);
748
Alexander Duycka5e9c572013-09-28 06:00:27 +0000749 /* update budget accounting */
750 budget--;
751 } while (likely(budget));
752
753 i += tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000754 tx_ring->next_to_clean = i;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000755 u64_stats_update_begin(&tx_ring->syncp);
Alexander Duycka114d0a2013-09-28 06:00:43 +0000756 tx_ring->stats.bytes += total_bytes;
757 tx_ring->stats.packets += total_packets;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000758 u64_stats_update_end(&tx_ring->syncp);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000759 tx_ring->q_vector->tx.total_bytes += total_bytes;
760 tx_ring->q_vector->tx.total_packets += total_packets;
Alexander Duycka5e9c572013-09-28 06:00:27 +0000761
Anjali Singhai58044742015-09-25 18:26:13 -0700762 if (tx_ring->flags & I40E_TXR_FLAGS_WB_ON_ITR) {
Anjali Singhai58044742015-09-25 18:26:13 -0700763 /* check to see if there are < 4 descriptors
764 * waiting to be written back, then kick the hardware to force
765 * them to be written back in case we stay in NAPI.
766 * In this mode on X722 we do not enable Interrupt.
767 */
Mitch Williams88dc9e62016-06-20 09:10:35 -0700768 unsigned int j = i40e_get_tx_pending(tx_ring, false);
Anjali Singhai58044742015-09-25 18:26:13 -0700769
770 if (budget &&
Alexander Duyck1dc8b532016-10-11 15:26:54 -0700771 ((j / WB_STRIDE) == 0) && (j > 0) &&
Alexander Duycka619afe2016-03-07 09:30:03 -0800772 !test_bit(__I40E_DOWN, &vsi->state) &&
Anjali Singhai58044742015-09-25 18:26:13 -0700773 (I40E_DESC_UNUSED(tx_ring) != tx_ring->count))
774 tx_ring->arm_wb = true;
775 }
Jesse Brandeburgd91649f2015-01-07 02:55:01 +0000776
Alexander Duycke486bdf2016-09-12 14:18:40 -0700777 /* notify netdev of completed buffers */
778 netdev_tx_completed_queue(txring_txq(tx_ring),
Alexander Duyck7070ce02013-09-28 06:00:37 +0000779 total_packets, total_bytes);
780
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000781#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
782 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
783 (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
784 /* Make sure that anybody stopping the queue after this
785 * sees the new next_to_clean.
786 */
787 smp_mb();
788 if (__netif_subqueue_stopped(tx_ring->netdev,
789 tx_ring->queue_index) &&
Alexander Duycka619afe2016-03-07 09:30:03 -0800790 !test_bit(__I40E_DOWN, &vsi->state)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000791 netif_wake_subqueue(tx_ring->netdev,
792 tx_ring->queue_index);
793 ++tx_ring->tx_stats.restart_queue;
794 }
795 }
796
Jesse Brandeburgd91649f2015-01-07 02:55:01 +0000797 return !!budget;
798}
799
800/**
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -0800801 * i40e_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
802 * @vsi: the VSI we care about
803 * @q_vector: the vector on which to enable writeback
804 *
805 **/
806static void i40e_enable_wb_on_itr(struct i40e_vsi *vsi,
807 struct i40e_q_vector *q_vector)
808{
809 u16 flags = q_vector->tx.ring[0].flags;
810 u32 val;
811
812 if (!(flags & I40E_TXR_FLAGS_WB_ON_ITR))
813 return;
814
815 if (q_vector->arm_wb_state)
816 return;
817
818 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
819 val = I40E_PFINT_DYN_CTLN_WB_ON_ITR_MASK |
820 I40E_PFINT_DYN_CTLN_ITR_INDX_MASK; /* set noitr */
821
822 wr32(&vsi->back->hw,
823 I40E_PFINT_DYN_CTLN(q_vector->v_idx + vsi->base_vector - 1),
824 val);
825 } else {
826 val = I40E_PFINT_DYN_CTL0_WB_ON_ITR_MASK |
827 I40E_PFINT_DYN_CTL0_ITR_INDX_MASK; /* set noitr */
828
829 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0, val);
830 }
831 q_vector->arm_wb_state = true;
832}
833
834/**
835 * i40e_force_wb - Issue SW Interrupt so HW does a wb
Jesse Brandeburgd91649f2015-01-07 02:55:01 +0000836 * @vsi: the VSI we care about
837 * @q_vector: the vector on which to force writeback
838 *
839 **/
Kiran Patilb03a8c12015-09-24 18:13:15 -0400840void i40e_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
Jesse Brandeburgd91649f2015-01-07 02:55:01 +0000841{
Anjali Singhai Jainecc6a232016-01-13 16:51:43 -0800842 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -0400843 u32 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
844 I40E_PFINT_DYN_CTLN_ITR_INDX_MASK | /* set noitr */
845 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK |
846 I40E_PFINT_DYN_CTLN_SW_ITR_INDX_ENA_MASK;
847 /* allow 00 to be written to the index */
848
849 wr32(&vsi->back->hw,
850 I40E_PFINT_DYN_CTLN(q_vector->v_idx +
851 vsi->base_vector - 1), val);
852 } else {
853 u32 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
854 I40E_PFINT_DYN_CTL0_ITR_INDX_MASK | /* set noitr */
855 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
856 I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK;
857 /* allow 00 to be written to the index */
858
859 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0, val);
860 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000861}
862
863/**
864 * i40e_set_new_dynamic_itr - Find new ITR level
865 * @rc: structure containing ring performance data
866 *
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -0400867 * Returns true if ITR changed, false if not
868 *
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000869 * Stores a new ITR value based on packets and byte counts during
870 * the last interrupt. The advantage of per interrupt computation
871 * is faster updates and more accurate ITR for the current traffic
872 * pattern. Constants in this function were computed based on
873 * theoretical maximum wire speed and thresholds were set based on
874 * testing data as well as attempting to minimize response time
875 * while increasing bulk throughput.
876 **/
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -0400877static bool i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000878{
879 enum i40e_latency_range new_latency_range = rc->latency_range;
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400880 struct i40e_q_vector *qv = rc->ring->q_vector;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000881 u32 new_itr = rc->itr;
882 int bytes_per_int;
Jesse Brandeburg51cc6d92015-09-28 14:16:52 -0400883 int usecs;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000884
885 if (rc->total_packets == 0 || !rc->itr)
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -0400886 return false;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000887
888 /* simple throttlerate management
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400889 * 0-10MB/s lowest (50000 ints/s)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000890 * 10-20MB/s low (20000 ints/s)
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400891 * 20-1249MB/s bulk (18000 ints/s)
892 * > 40000 Rx packets per second (8000 ints/s)
Jesse Brandeburg51cc6d92015-09-28 14:16:52 -0400893 *
894 * The math works out because the divisor is in 10^(-6) which
895 * turns the bytes/us input value into MB/s values, but
896 * make sure to use usecs, as the register values written
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400897 * are in 2 usec increments in the ITR registers, and make sure
898 * to use the smoothed values that the countdown timer gives us.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000899 */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400900 usecs = (rc->itr << 1) * ITR_COUNTDOWN_START;
Jesse Brandeburg51cc6d92015-09-28 14:16:52 -0400901 bytes_per_int = rc->total_bytes / usecs;
Jesse Brandeburgee2319c2015-09-28 14:16:54 -0400902
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -0400903 switch (new_latency_range) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000904 case I40E_LOWEST_LATENCY:
905 if (bytes_per_int > 10)
906 new_latency_range = I40E_LOW_LATENCY;
907 break;
908 case I40E_LOW_LATENCY:
909 if (bytes_per_int > 20)
910 new_latency_range = I40E_BULK_LATENCY;
911 else if (bytes_per_int <= 10)
912 new_latency_range = I40E_LOWEST_LATENCY;
913 break;
914 case I40E_BULK_LATENCY:
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400915 case I40E_ULTRA_LATENCY:
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -0400916 default:
917 if (bytes_per_int <= 20)
918 new_latency_range = I40E_LOW_LATENCY;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000919 break;
920 }
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400921
922 /* this is to adjust RX more aggressively when streaming small
923 * packets. The value of 40000 was picked as it is just beyond
924 * what the hardware can receive per second if in low latency
925 * mode.
926 */
927#define RX_ULTRA_PACKET_RATE 40000
928
929 if ((((rc->total_packets * 1000000) / usecs) > RX_ULTRA_PACKET_RATE) &&
930 (&qv->rx == rc))
931 new_latency_range = I40E_ULTRA_LATENCY;
932
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -0400933 rc->latency_range = new_latency_range;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000934
935 switch (new_latency_range) {
936 case I40E_LOWEST_LATENCY:
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400937 new_itr = I40E_ITR_50K;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000938 break;
939 case I40E_LOW_LATENCY:
940 new_itr = I40E_ITR_20K;
941 break;
942 case I40E_BULK_LATENCY:
Jesse Brandeburgc56625d2015-09-28 14:16:53 -0400943 new_itr = I40E_ITR_18K;
944 break;
945 case I40E_ULTRA_LATENCY:
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000946 new_itr = I40E_ITR_8K;
947 break;
948 default:
949 break;
950 }
951
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000952 rc->total_bytes = 0;
953 rc->total_packets = 0;
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -0400954
955 if (new_itr != rc->itr) {
956 rc->itr = new_itr;
957 return true;
958 }
959
960 return false;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000961}
962
963/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000964 * i40e_clean_programming_status - clean the programming status descriptor
965 * @rx_ring: the rx ring that has this descriptor
966 * @rx_desc: the rx descriptor written back by HW
967 *
968 * Flow director should handle FD_FILTER_STATUS to check its filter programming
969 * status being successful or not and take actions accordingly. FCoE should
970 * handle its context/filter programming/invalidation status and take actions.
971 *
972 **/
973static void i40e_clean_programming_status(struct i40e_ring *rx_ring,
974 union i40e_rx_desc *rx_desc)
975{
976 u64 qw;
977 u8 id;
978
979 qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
980 id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
981 I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
982
983 if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000984 i40e_fd_handle_status(rx_ring, rx_desc, id);
Vasu Dev38e00432014-08-01 13:27:03 -0700985#ifdef I40E_FCOE
986 else if ((id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_PROG_STATUS) ||
987 (id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_INVL_STATUS))
988 i40e_fcoe_handle_status(rx_ring, rx_desc, id);
989#endif
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000990}
991
992/**
993 * i40e_setup_tx_descriptors - Allocate the Tx descriptors
994 * @tx_ring: the tx ring to set up
995 *
996 * Return 0 on success, negative on error
997 **/
998int i40e_setup_tx_descriptors(struct i40e_ring *tx_ring)
999{
1000 struct device *dev = tx_ring->dev;
1001 int bi_size;
1002
1003 if (!dev)
1004 return -ENOMEM;
1005
Jesse Brandeburge908f812015-07-23 16:54:42 -04001006 /* warn if we are about to overwrite the pointer */
1007 WARN_ON(tx_ring->tx_bi);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001008 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
1009 tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
1010 if (!tx_ring->tx_bi)
1011 goto err;
1012
1013 /* round up to nearest 4K */
1014 tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00001015 /* add u32 for head writeback, align after this takes care of
1016 * guaranteeing this is at least one cache line in size
1017 */
1018 tx_ring->size += sizeof(u32);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001019 tx_ring->size = ALIGN(tx_ring->size, 4096);
1020 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
1021 &tx_ring->dma, GFP_KERNEL);
1022 if (!tx_ring->desc) {
1023 dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
1024 tx_ring->size);
1025 goto err;
1026 }
1027
1028 tx_ring->next_to_use = 0;
1029 tx_ring->next_to_clean = 0;
1030 return 0;
1031
1032err:
1033 kfree(tx_ring->tx_bi);
1034 tx_ring->tx_bi = NULL;
1035 return -ENOMEM;
1036}
1037
1038/**
1039 * i40e_clean_rx_ring - Free Rx buffers
1040 * @rx_ring: ring to be cleaned
1041 **/
1042void i40e_clean_rx_ring(struct i40e_ring *rx_ring)
1043{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001044 unsigned long bi_size;
1045 u16 i;
1046
1047 /* ring already cleared, nothing to do */
1048 if (!rx_ring->rx_bi)
1049 return;
1050
Scott Petersone72e5652017-02-09 23:40:25 -08001051 if (rx_ring->skb) {
1052 dev_kfree_skb(rx_ring->skb);
1053 rx_ring->skb = NULL;
1054 }
1055
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001056 /* Free all the Rx ring sk_buffs */
1057 for (i = 0; i < rx_ring->count; i++) {
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001058 struct i40e_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
1059
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001060 if (!rx_bi->page)
1061 continue;
1062
Alexander Duyck59605bc2017-01-30 12:29:35 -08001063 /* Invalidate cache lines that may have been written to by
1064 * device so that we avoid corrupting memory.
1065 */
1066 dma_sync_single_range_for_cpu(rx_ring->dev,
1067 rx_bi->dma,
1068 rx_bi->page_offset,
1069 I40E_RXBUFFER_2048,
1070 DMA_FROM_DEVICE);
1071
1072 /* free resources associated with mapping */
1073 dma_unmap_page_attrs(rx_ring->dev, rx_bi->dma,
1074 PAGE_SIZE,
1075 DMA_FROM_DEVICE,
1076 I40E_RX_DMA_ATTR);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001077 __free_pages(rx_bi->page, 0);
1078
1079 rx_bi->page = NULL;
1080 rx_bi->page_offset = 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001081 }
1082
1083 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
1084 memset(rx_ring->rx_bi, 0, bi_size);
1085
1086 /* Zero out the descriptor ring */
1087 memset(rx_ring->desc, 0, rx_ring->size);
1088
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001089 rx_ring->next_to_alloc = 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001090 rx_ring->next_to_clean = 0;
1091 rx_ring->next_to_use = 0;
1092}
1093
1094/**
1095 * i40e_free_rx_resources - Free Rx resources
1096 * @rx_ring: ring to clean the resources from
1097 *
1098 * Free all receive software resources
1099 **/
1100void i40e_free_rx_resources(struct i40e_ring *rx_ring)
1101{
1102 i40e_clean_rx_ring(rx_ring);
1103 kfree(rx_ring->rx_bi);
1104 rx_ring->rx_bi = NULL;
1105
1106 if (rx_ring->desc) {
1107 dma_free_coherent(rx_ring->dev, rx_ring->size,
1108 rx_ring->desc, rx_ring->dma);
1109 rx_ring->desc = NULL;
1110 }
1111}
1112
1113/**
1114 * i40e_setup_rx_descriptors - Allocate Rx descriptors
1115 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
1116 *
1117 * Returns 0 on success, negative on failure
1118 **/
1119int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring)
1120{
1121 struct device *dev = rx_ring->dev;
1122 int bi_size;
1123
Jesse Brandeburge908f812015-07-23 16:54:42 -04001124 /* warn if we are about to overwrite the pointer */
1125 WARN_ON(rx_ring->rx_bi);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001126 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
1127 rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
1128 if (!rx_ring->rx_bi)
1129 goto err;
1130
Carolyn Wybornyf217d6c2015-02-09 17:42:31 -08001131 u64_stats_init(&rx_ring->syncp);
Carolyn Wyborny638702b2015-01-24 09:58:32 +00001132
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001133 /* Round up to nearest 4K */
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001134 rx_ring->size = rx_ring->count * sizeof(union i40e_32byte_rx_desc);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001135 rx_ring->size = ALIGN(rx_ring->size, 4096);
1136 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
1137 &rx_ring->dma, GFP_KERNEL);
1138
1139 if (!rx_ring->desc) {
1140 dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
1141 rx_ring->size);
1142 goto err;
1143 }
1144
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001145 rx_ring->next_to_alloc = 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001146 rx_ring->next_to_clean = 0;
1147 rx_ring->next_to_use = 0;
1148
1149 return 0;
1150err:
1151 kfree(rx_ring->rx_bi);
1152 rx_ring->rx_bi = NULL;
1153 return -ENOMEM;
1154}
1155
1156/**
1157 * i40e_release_rx_desc - Store the new tail and head values
1158 * @rx_ring: ring to bump
1159 * @val: new head index
1160 **/
1161static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
1162{
1163 rx_ring->next_to_use = val;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001164
1165 /* update next to alloc since we have filled the ring */
1166 rx_ring->next_to_alloc = val;
1167
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001168 /* Force memory writes to complete before letting h/w
1169 * know there are new descriptors to fetch. (Only
1170 * applicable for weak-ordered memory model archs,
1171 * such as IA-64).
1172 */
1173 wmb();
1174 writel(val, rx_ring->tail);
1175}
1176
1177/**
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001178 * i40e_alloc_mapped_page - recycle or make a new page
1179 * @rx_ring: ring to use
1180 * @bi: rx_buffer struct to modify
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001181 *
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001182 * Returns true if the page was successfully allocated or
1183 * reused.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001184 **/
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001185static bool i40e_alloc_mapped_page(struct i40e_ring *rx_ring,
1186 struct i40e_rx_buffer *bi)
Mitch Williamsa132af22015-01-24 09:58:35 +00001187{
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001188 struct page *page = bi->page;
1189 dma_addr_t dma;
Mitch Williamsa132af22015-01-24 09:58:35 +00001190
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001191 /* since we are recycling buffers we should seldom need to alloc */
1192 if (likely(page)) {
1193 rx_ring->rx_stats.page_reuse_count++;
1194 return true;
Mitch Williamsa132af22015-01-24 09:58:35 +00001195 }
1196
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001197 /* alloc new page for storage */
1198 page = dev_alloc_page();
1199 if (unlikely(!page)) {
1200 rx_ring->rx_stats.alloc_page_failed++;
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001201 return false;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001202 }
1203
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001204 /* map page for use */
Alexander Duyck59605bc2017-01-30 12:29:35 -08001205 dma = dma_map_page_attrs(rx_ring->dev, page, 0,
1206 PAGE_SIZE,
1207 DMA_FROM_DEVICE,
1208 I40E_RX_DMA_ATTR);
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001209
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001210 /* if mapping failed free memory back to system since
1211 * there isn't much point in holding memory we can't use
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001212 */
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001213 if (dma_mapping_error(rx_ring->dev, dma)) {
1214 __free_pages(page, 0);
1215 rx_ring->rx_stats.alloc_page_failed++;
1216 return false;
1217 }
1218
1219 bi->dma = dma;
1220 bi->page = page;
1221 bi->page_offset = 0;
1222
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001223 return true;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001224}
1225
1226/**
1227 * i40e_receive_skb - Send a completed packet up the stack
1228 * @rx_ring: rx ring in play
1229 * @skb: packet to send up
1230 * @vlan_tag: vlan tag for packet
1231 **/
1232static void i40e_receive_skb(struct i40e_ring *rx_ring,
1233 struct sk_buff *skb, u16 vlan_tag)
1234{
1235 struct i40e_q_vector *q_vector = rx_ring->q_vector;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001236
Jesse Brandeburga149f2c2016-04-12 08:30:49 -07001237 if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1238 (vlan_tag & VLAN_VID_MASK))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001239 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
1240
Alexander Duyck8b650352015-09-24 09:04:32 -07001241 napi_gro_receive(&q_vector->napi, skb);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001242}
1243
1244/**
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001245 * i40e_alloc_rx_buffers - Replace used receive buffers
1246 * @rx_ring: ring to place buffers on
1247 * @cleaned_count: number of buffers to replace
1248 *
1249 * Returns false if all allocations were successful, true if any fail
1250 **/
1251bool i40e_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
1252{
1253 u16 ntu = rx_ring->next_to_use;
1254 union i40e_rx_desc *rx_desc;
1255 struct i40e_rx_buffer *bi;
1256
1257 /* do nothing if no valid netdev defined */
1258 if (!rx_ring->netdev || !cleaned_count)
1259 return false;
1260
1261 rx_desc = I40E_RX_DESC(rx_ring, ntu);
1262 bi = &rx_ring->rx_bi[ntu];
1263
1264 do {
1265 if (!i40e_alloc_mapped_page(rx_ring, bi))
1266 goto no_buffers;
1267
Alexander Duyck59605bc2017-01-30 12:29:35 -08001268 /* sync the buffer for use by the device */
1269 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
1270 bi->page_offset,
1271 I40E_RXBUFFER_2048,
1272 DMA_FROM_DEVICE);
1273
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001274 /* Refresh the desc even if buffer_addrs didn't change
1275 * because each write-back erases this info.
1276 */
1277 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001278
1279 rx_desc++;
1280 bi++;
1281 ntu++;
1282 if (unlikely(ntu == rx_ring->count)) {
1283 rx_desc = I40E_RX_DESC(rx_ring, 0);
1284 bi = rx_ring->rx_bi;
1285 ntu = 0;
1286 }
1287
1288 /* clear the status bits for the next_to_use descriptor */
1289 rx_desc->wb.qword1.status_error_len = 0;
1290
1291 cleaned_count--;
1292 } while (cleaned_count);
1293
1294 if (rx_ring->next_to_use != ntu)
1295 i40e_release_rx_desc(rx_ring, ntu);
1296
1297 return false;
1298
1299no_buffers:
1300 if (rx_ring->next_to_use != ntu)
1301 i40e_release_rx_desc(rx_ring, ntu);
1302
1303 /* make sure to come back via polling to try again after
1304 * allocation failure
1305 */
1306 return true;
1307}
1308
1309/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001310 * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
1311 * @vsi: the VSI we care about
1312 * @skb: skb currently being received and modified
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001313 * @rx_desc: the receive descriptor
1314 *
1315 * skb->protocol must be set before this function is called
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001316 **/
1317static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
1318 struct sk_buff *skb,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001319 union i40e_rx_desc *rx_desc)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001320{
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001321 struct i40e_rx_ptype_decoded decoded;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001322 u32 rx_error, rx_status;
Alexander Duyck858296c82016-06-14 15:45:42 -07001323 bool ipv4, ipv6;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001324 u8 ptype;
1325 u64 qword;
1326
1327 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1328 ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT;
1329 rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
1330 I40E_RXD_QW1_ERROR_SHIFT;
1331 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1332 I40E_RXD_QW1_STATUS_SHIFT;
1333 decoded = decode_rx_desc_ptype(ptype);
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001334
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001335 skb->ip_summed = CHECKSUM_NONE;
1336
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001337 skb_checksum_none_assert(skb);
1338
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001339 /* Rx csum enabled and ip headers found? */
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001340 if (!(vsi->netdev->features & NETIF_F_RXCSUM))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001341 return;
1342
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001343 /* did the hardware decode the packet and checksum? */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001344 if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001345 return;
1346
1347 /* both known and outer_ip must be set for the below code to work */
1348 if (!(decoded.known && decoded.outer_ip))
1349 return;
1350
Alexander Duyckfad57332016-01-24 21:17:22 -08001351 ipv4 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
1352 (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4);
1353 ipv6 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
1354 (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6);
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001355
1356 if (ipv4 &&
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001357 (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
1358 BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001359 goto checksum_fail;
1360
Jesse Brandeburgddf1d0d2014-02-13 03:48:39 -08001361 /* likely incorrect csum if alternate IP extension headers found */
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001362 if (ipv6 &&
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001363 rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001364 /* don't increment checksum err here, non-fatal err */
Shannon Nelson8ee75a82013-12-21 05:44:46 +00001365 return;
1366
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001367 /* there was some L4 error, count error and punt packet to the stack */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001368 if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001369 goto checksum_fail;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001370
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001371 /* handle packets that were not able to be checksummed due
1372 * to arrival speed, in this case the stack can compute
1373 * the csum.
1374 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001375 if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001376 return;
1377
Alexander Duyck858296c82016-06-14 15:45:42 -07001378 /* If there is an outer header present that might contain a checksum
1379 * we need to bump the checksum level by 1 to reflect the fact that
1380 * we are indicating we validated the inner checksum.
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001381 */
Alexander Duyck858296c82016-06-14 15:45:42 -07001382 if (decoded.tunnel_type >= I40E_RX_PTYPE_TUNNEL_IP_GRENAT)
1383 skb->csum_level = 1;
Alexander Duyckfad57332016-01-24 21:17:22 -08001384
Alexander Duyck858296c82016-06-14 15:45:42 -07001385 /* Only report checksum unnecessary for TCP, UDP, or SCTP */
1386 switch (decoded.inner_prot) {
1387 case I40E_RX_PTYPE_INNER_PROT_TCP:
1388 case I40E_RX_PTYPE_INNER_PROT_UDP:
1389 case I40E_RX_PTYPE_INNER_PROT_SCTP:
1390 skb->ip_summed = CHECKSUM_UNNECESSARY;
1391 /* fall though */
1392 default:
1393 break;
1394 }
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001395
1396 return;
1397
1398checksum_fail:
1399 vsi->back->hw_csum_rx_error++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001400}
1401
1402/**
Anjali Singhai Jain857942f2015-12-09 15:50:21 -08001403 * i40e_ptype_to_htype - get a hash type
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001404 * @ptype: the ptype value from the descriptor
1405 *
1406 * Returns a hash type to be used by skb_set_hash
1407 **/
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001408static inline int i40e_ptype_to_htype(u8 ptype)
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001409{
1410 struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
1411
1412 if (!decoded.known)
1413 return PKT_HASH_TYPE_NONE;
1414
1415 if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1416 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
1417 return PKT_HASH_TYPE_L4;
1418 else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1419 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
1420 return PKT_HASH_TYPE_L3;
1421 else
1422 return PKT_HASH_TYPE_L2;
1423}
1424
1425/**
Anjali Singhai Jain857942f2015-12-09 15:50:21 -08001426 * i40e_rx_hash - set the hash value in the skb
1427 * @ring: descriptor ring
1428 * @rx_desc: specific descriptor
1429 **/
1430static inline void i40e_rx_hash(struct i40e_ring *ring,
1431 union i40e_rx_desc *rx_desc,
1432 struct sk_buff *skb,
1433 u8 rx_ptype)
1434{
1435 u32 hash;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001436 const __le64 rss_mask =
Anjali Singhai Jain857942f2015-12-09 15:50:21 -08001437 cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
1438 I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
1439
Mitch Williamsa876c3b2016-05-03 15:13:18 -07001440 if (!(ring->netdev->features & NETIF_F_RXHASH))
Anjali Singhai Jain857942f2015-12-09 15:50:21 -08001441 return;
1442
1443 if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
1444 hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
1445 skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
1446 }
1447}
1448
1449/**
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001450 * i40e_process_skb_fields - Populate skb header fields from Rx descriptor
1451 * @rx_ring: rx descriptor ring packet is being transacted on
1452 * @rx_desc: pointer to the EOP Rx descriptor
1453 * @skb: pointer to current skb being populated
1454 * @rx_ptype: the packet type decoded by hardware
Mitch Williamsa132af22015-01-24 09:58:35 +00001455 *
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001456 * This function checks the ring, descriptor, and packet information in
1457 * order to populate the hash, checksum, VLAN, protocol, and
1458 * other fields within the skb.
Mitch Williamsa132af22015-01-24 09:58:35 +00001459 **/
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001460static inline
1461void i40e_process_skb_fields(struct i40e_ring *rx_ring,
1462 union i40e_rx_desc *rx_desc, struct sk_buff *skb,
1463 u8 rx_ptype)
1464{
1465 u64 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1466 u32 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1467 I40E_RXD_QW1_STATUS_SHIFT;
Jacob Keller144ed172016-10-05 09:30:42 -07001468 u32 tsynvalid = rx_status & I40E_RXD_QW1_STATUS_TSYNVALID_MASK;
1469 u32 tsyn = (rx_status & I40E_RXD_QW1_STATUS_TSYNINDX_MASK) >>
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001470 I40E_RXD_QW1_STATUS_TSYNINDX_SHIFT;
1471
Jacob Keller12490502016-10-05 09:30:44 -07001472 if (unlikely(tsynvalid))
Jacob Keller144ed172016-10-05 09:30:42 -07001473 i40e_ptp_rx_hwtstamp(rx_ring->vsi->back, skb, tsyn);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001474
1475 i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
1476
1477 /* modifies the skb - consumes the enet header */
1478 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1479
1480 i40e_rx_checksum(rx_ring->vsi, skb, rx_desc);
1481
1482 skb_record_rx_queue(skb, rx_ring->queue_index);
1483}
1484
1485/**
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001486 * i40e_cleanup_headers - Correct empty headers
1487 * @rx_ring: rx descriptor ring packet is being transacted on
1488 * @skb: pointer to current skb being fixed
1489 *
1490 * Also address the case where we are pulling data in on pages only
1491 * and as such no data is present in the skb header.
1492 *
1493 * In addition if skb is not at least 60 bytes we need to pad it so that
1494 * it is large enough to qualify as a valid Ethernet frame.
1495 *
1496 * Returns true if an error was encountered and skb was freed.
1497 **/
1498static bool i40e_cleanup_headers(struct i40e_ring *rx_ring, struct sk_buff *skb)
1499{
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001500 /* if eth_skb_pad returns an error the skb was freed */
1501 if (eth_skb_pad(skb))
1502 return true;
1503
1504 return false;
1505}
1506
1507/**
1508 * i40e_reuse_rx_page - page flip buffer and store it back on the ring
1509 * @rx_ring: rx descriptor ring to store buffers on
1510 * @old_buff: donor buffer to have page reused
1511 *
1512 * Synchronizes page for reuse by the adapter
1513 **/
1514static void i40e_reuse_rx_page(struct i40e_ring *rx_ring,
1515 struct i40e_rx_buffer *old_buff)
1516{
1517 struct i40e_rx_buffer *new_buff;
1518 u16 nta = rx_ring->next_to_alloc;
1519
1520 new_buff = &rx_ring->rx_bi[nta];
1521
1522 /* update, and store next to alloc */
1523 nta++;
1524 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1525
1526 /* transfer page from old buffer to new buffer */
1527 *new_buff = *old_buff;
1528}
1529
1530/**
Scott Peterson9b37c932017-02-09 23:43:30 -08001531 * i40e_page_is_reusable - check if any reuse is possible
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001532 * @page: page struct to check
Scott Peterson9b37c932017-02-09 23:43:30 -08001533 *
1534 * A page is not reusable if it was allocated under low memory
1535 * conditions, or it's not in the same NUMA node as this CPU.
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001536 */
Scott Peterson9b37c932017-02-09 23:43:30 -08001537static inline bool i40e_page_is_reusable(struct page *page)
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001538{
Scott Peterson9b37c932017-02-09 23:43:30 -08001539 return (page_to_nid(page) == numa_mem_id()) &&
1540 !page_is_pfmemalloc(page);
1541}
1542
1543/**
1544 * i40e_can_reuse_rx_page - Determine if this page can be reused by
1545 * the adapter for another receive
1546 *
1547 * @rx_buffer: buffer containing the page
1548 * @page: page address from rx_buffer
1549 * @truesize: actual size of the buffer in this page
1550 *
1551 * If page is reusable, rx_buffer->page_offset is adjusted to point to
1552 * an unused region in the page.
1553 *
1554 * For small pages, @truesize will be a constant value, half the size
1555 * of the memory at page. We'll attempt to alternate between high and
1556 * low halves of the page, with one half ready for use by the hardware
1557 * and the other half being consumed by the stack. We use the page
1558 * ref count to determine whether the stack has finished consuming the
1559 * portion of this page that was passed up with a previous packet. If
1560 * the page ref count is >1, we'll assume the "other" half page is
1561 * still busy, and this page cannot be reused.
1562 *
1563 * For larger pages, @truesize will be the actual space used by the
1564 * received packet (adjusted upward to an even multiple of the cache
1565 * line size). This will advance through the page by the amount
1566 * actually consumed by the received packets while there is still
1567 * space for a buffer. Each region of larger pages will be used at
1568 * most once, after which the page will not be reused.
1569 *
1570 * In either case, if the page is reusable its refcount is increased.
1571 **/
1572static bool i40e_can_reuse_rx_page(struct i40e_rx_buffer *rx_buffer,
1573 struct page *page,
1574 const unsigned int truesize)
1575{
1576#if (PAGE_SIZE >= 8192)
1577 unsigned int last_offset = PAGE_SIZE - I40E_RXBUFFER_2048;
1578#endif
1579
1580 /* Is any reuse possible? */
1581 if (unlikely(!i40e_page_is_reusable(page)))
1582 return false;
1583
1584#if (PAGE_SIZE < 8192)
1585 /* if we are only owner of page we can reuse it */
1586 if (unlikely(page_count(page) != 1))
1587 return false;
1588
1589 /* flip page offset to other buffer */
1590 rx_buffer->page_offset ^= truesize;
1591#else
1592 /* move offset up to the next cache line */
1593 rx_buffer->page_offset += truesize;
1594
1595 if (rx_buffer->page_offset > last_offset)
1596 return false;
1597#endif
1598
1599 /* Inc ref count on page before passing it up to the stack */
1600 get_page(page);
1601
1602 return true;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001603}
1604
1605/**
1606 * i40e_add_rx_frag - Add contents of Rx buffer to sk_buff
1607 * @rx_ring: rx descriptor ring to transact packets on
1608 * @rx_buffer: buffer containing page to add
Scott Peterson7987dcd2017-02-09 23:37:28 -08001609 * @size: packet length from rx_desc
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001610 * @skb: sk_buff to place the data into
1611 *
1612 * This function will add the data contained in rx_buffer->page to the skb.
1613 * This is done either through a direct copy if the data in the buffer is
1614 * less than the skb header size, otherwise it will just attach the page as
1615 * a frag to the skb.
1616 *
1617 * The function will then update the page offset if necessary and return
1618 * true if the buffer can be reused by the adapter.
1619 **/
1620static bool i40e_add_rx_frag(struct i40e_ring *rx_ring,
1621 struct i40e_rx_buffer *rx_buffer,
Scott Peterson7987dcd2017-02-09 23:37:28 -08001622 unsigned int size,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001623 struct sk_buff *skb)
1624{
1625 struct page *page = rx_buffer->page;
Scott Peterson9b37c932017-02-09 23:43:30 -08001626 unsigned char *va = page_address(page) + rx_buffer->page_offset;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001627#if (PAGE_SIZE < 8192)
1628 unsigned int truesize = I40E_RXBUFFER_2048;
1629#else
1630 unsigned int truesize = ALIGN(size, L1_CACHE_BYTES);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001631#endif
Scott Peterson9b37c932017-02-09 23:43:30 -08001632 unsigned int pull_len;
1633
1634 if (unlikely(skb_is_nonlinear(skb)))
1635 goto add_tail_frag;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001636
1637 /* will the data fit in the skb we allocated? if so, just
1638 * copy it as it is pretty small anyway
1639 */
Scott Peterson9b37c932017-02-09 23:43:30 -08001640 if (size <= I40E_RX_HDR_SIZE) {
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001641 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
1642
Scott Peterson9b37c932017-02-09 23:43:30 -08001643 /* page is reusable, we can reuse buffer as-is */
1644 if (likely(i40e_page_is_reusable(page)))
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001645 return true;
1646
1647 /* this page cannot be reused so discard it */
1648 __free_pages(page, 0);
1649 return false;
1650 }
1651
Scott Peterson9b37c932017-02-09 23:43:30 -08001652 /* we need the header to contain the greater of either
1653 * ETH_HLEN or 60 bytes if the skb->len is less than
1654 * 60 for skb_pad.
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001655 */
Scott Peterson9b37c932017-02-09 23:43:30 -08001656 pull_len = eth_get_headlen(va, I40E_RX_HDR_SIZE);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001657
Scott Peterson9b37c932017-02-09 23:43:30 -08001658 /* align pull length to size of long to optimize
1659 * memcpy performance
1660 */
1661 memcpy(__skb_put(skb, pull_len), va, ALIGN(pull_len, sizeof(long)));
1662
1663 /* update all of the pointers */
1664 va += pull_len;
1665 size -= pull_len;
1666
1667add_tail_frag:
1668 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
1669 (unsigned long)va & ~PAGE_MASK, size, truesize);
1670
1671 return i40e_can_reuse_rx_page(rx_buffer, page, truesize);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001672}
1673
1674/**
1675 * i40e_fetch_rx_buffer - Allocate skb and populate it
1676 * @rx_ring: rx descriptor ring to transact packets on
1677 * @rx_desc: descriptor containing info written by hardware
1678 *
1679 * This function allocates an skb on the fly, and populates it with the page
1680 * data from the current receive descriptor, taking care to set up the skb
1681 * correctly, as well as handling calling the page recycle function if
1682 * necessary.
1683 */
1684static inline
1685struct sk_buff *i40e_fetch_rx_buffer(struct i40e_ring *rx_ring,
Scott Petersone72e5652017-02-09 23:40:25 -08001686 union i40e_rx_desc *rx_desc,
1687 struct sk_buff *skb)
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001688{
Scott Peterson7987dcd2017-02-09 23:37:28 -08001689 u64 local_status_error_len =
1690 le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1691 unsigned int size =
1692 (local_status_error_len & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
1693 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001694 struct i40e_rx_buffer *rx_buffer;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001695 struct page *page;
1696
1697 rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
1698 page = rx_buffer->page;
1699 prefetchw(page);
1700
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001701 if (likely(!skb)) {
1702 void *page_addr = page_address(page) + rx_buffer->page_offset;
1703
1704 /* prefetch first cache line of first page */
1705 prefetch(page_addr);
1706#if L1_CACHE_BYTES < 128
1707 prefetch(page_addr + L1_CACHE_BYTES);
1708#endif
1709
1710 /* allocate a skb to store the frags */
1711 skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
1712 I40E_RX_HDR_SIZE,
1713 GFP_ATOMIC | __GFP_NOWARN);
1714 if (unlikely(!skb)) {
1715 rx_ring->rx_stats.alloc_buff_failed++;
1716 return NULL;
1717 }
1718
1719 /* we will be copying header into skb->data in
1720 * pskb_may_pull so it is in our interest to prefetch
1721 * it now to avoid a possible cache miss
1722 */
1723 prefetchw(skb->data);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001724 }
1725
1726 /* we are reusing so sync this buffer for CPU use */
1727 dma_sync_single_range_for_cpu(rx_ring->dev,
1728 rx_buffer->dma,
1729 rx_buffer->page_offset,
Scott Peterson7987dcd2017-02-09 23:37:28 -08001730 size,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001731 DMA_FROM_DEVICE);
1732
1733 /* pull page into skb */
Scott Peterson7987dcd2017-02-09 23:37:28 -08001734 if (i40e_add_rx_frag(rx_ring, rx_buffer, size, skb)) {
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001735 /* hand second half of page back to the ring */
1736 i40e_reuse_rx_page(rx_ring, rx_buffer);
1737 rx_ring->rx_stats.page_reuse_count++;
1738 } else {
1739 /* we are not reusing the buffer so unmap it */
Alexander Duyck59605bc2017-01-30 12:29:35 -08001740 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma, PAGE_SIZE,
1741 DMA_FROM_DEVICE, I40E_RX_DMA_ATTR);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001742 }
1743
1744 /* clear contents of buffer_info */
1745 rx_buffer->page = NULL;
1746
1747 return skb;
1748}
1749
1750/**
1751 * i40e_is_non_eop - process handling of non-EOP buffers
1752 * @rx_ring: Rx ring being processed
1753 * @rx_desc: Rx descriptor for current buffer
1754 * @skb: Current socket buffer containing buffer in progress
1755 *
1756 * This function updates next to clean. If the buffer is an EOP buffer
1757 * this function exits returning false, otherwise it will place the
1758 * sk_buff in the next buffer to be chained and return true indicating
1759 * that this is in fact a non-EOP buffer.
1760 **/
1761static bool i40e_is_non_eop(struct i40e_ring *rx_ring,
1762 union i40e_rx_desc *rx_desc,
1763 struct sk_buff *skb)
1764{
1765 u32 ntc = rx_ring->next_to_clean + 1;
1766
1767 /* fetch, update, and store next to clean */
1768 ntc = (ntc < rx_ring->count) ? ntc : 0;
1769 rx_ring->next_to_clean = ntc;
1770
1771 prefetch(I40E_RX_DESC(rx_ring, ntc));
1772
1773#define staterrlen rx_desc->wb.qword1.status_error_len
1774 if (unlikely(i40e_rx_is_programming_status(le64_to_cpu(staterrlen)))) {
1775 i40e_clean_programming_status(rx_ring, rx_desc);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001776 return true;
1777 }
1778 /* if we are the last buffer then there is nothing else to do */
1779#define I40E_RXD_EOF BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)
1780 if (likely(i40e_test_staterr(rx_desc, I40E_RXD_EOF)))
1781 return false;
1782
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001783 rx_ring->rx_stats.non_eop_descs++;
1784
1785 return true;
1786}
1787
1788/**
1789 * i40e_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
1790 * @rx_ring: rx descriptor ring to transact packets on
1791 * @budget: Total limit on number of packets to process
1792 *
1793 * This function provides a "bounce buffer" approach to Rx interrupt
1794 * processing. The advantage to this is that on systems that have
1795 * expensive overhead for IOMMU access this provides a means of avoiding
1796 * it by maintaining the mapping of the page to the system.
1797 *
1798 * Returns amount of work completed
1799 **/
1800static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
Mitch Williamsa132af22015-01-24 09:58:35 +00001801{
1802 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
Scott Petersone72e5652017-02-09 23:40:25 -08001803 struct sk_buff *skb = rx_ring->skb;
Mitch Williamsa132af22015-01-24 09:58:35 +00001804 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001805 bool failure = false;
Mitch Williamsa132af22015-01-24 09:58:35 +00001806
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001807 while (likely(total_rx_packets < budget)) {
1808 union i40e_rx_desc *rx_desc;
Mitch Williamsa132af22015-01-24 09:58:35 +00001809 u16 vlan_tag;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001810 u8 rx_ptype;
1811 u64 qword;
1812
Mitch Williamsa132af22015-01-24 09:58:35 +00001813 /* return some buffers to hardware, one at a time is too slow */
1814 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001815 failure = failure ||
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001816 i40e_alloc_rx_buffers(rx_ring, cleaned_count);
Mitch Williamsa132af22015-01-24 09:58:35 +00001817 cleaned_count = 0;
1818 }
1819
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001820 rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
1821
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001822 /* status_error_len will always be zero for unused descriptors
1823 * because it's cleared in cleanup, and overlaps with hdr_addr
1824 * which is always zero because packet split isn't used, if the
1825 * hardware wrote DD then it will be non-zero
1826 */
Alexander Duyck99dad8b2016-09-27 11:28:50 -07001827 if (!i40e_test_staterr(rx_desc,
1828 BIT(I40E_RX_DESC_STATUS_DD_SHIFT)))
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001829 break;
1830
Mitch Williamsa132af22015-01-24 09:58:35 +00001831 /* This memory barrier is needed to keep us from reading
1832 * any other fields out of the rx_desc until we know the
1833 * DD bit is set.
1834 */
Alexander Duyck67317162015-04-08 18:49:43 -07001835 dma_rmb();
Mitch Williamsa132af22015-01-24 09:58:35 +00001836
Scott Petersone72e5652017-02-09 23:40:25 -08001837 skb = i40e_fetch_rx_buffer(rx_ring, rx_desc, skb);
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001838 if (!skb)
1839 break;
Mitch Williamsa132af22015-01-24 09:58:35 +00001840
Mitch Williamsa132af22015-01-24 09:58:35 +00001841 cleaned_count++;
1842
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001843 if (i40e_is_non_eop(rx_ring, rx_desc, skb))
Mitch Williamsa132af22015-01-24 09:58:35 +00001844 continue;
Mitch Williamsa132af22015-01-24 09:58:35 +00001845
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001846 /* ERR_MASK will only have valid bits if EOP set, and
1847 * what we are doing here is actually checking
1848 * I40E_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
1849 * the error field
1850 */
1851 if (unlikely(i40e_test_staterr(rx_desc, BIT(I40E_RXD_QW1_ERROR_SHIFT)))) {
Mitch Williamsa132af22015-01-24 09:58:35 +00001852 dev_kfree_skb_any(skb);
Mitch Williamsa132af22015-01-24 09:58:35 +00001853 continue;
1854 }
1855
Scott Petersone72e5652017-02-09 23:40:25 -08001856 if (i40e_cleanup_headers(rx_ring, skb)) {
1857 skb = NULL;
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001858 continue;
Scott Petersone72e5652017-02-09 23:40:25 -08001859 }
Mitch Williamsa132af22015-01-24 09:58:35 +00001860
1861 /* probably a little skewed due to removing CRC */
1862 total_rx_bytes += skb->len;
Mitch Williamsa132af22015-01-24 09:58:35 +00001863
Alexander Duyck99dad8b2016-09-27 11:28:50 -07001864 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1865 rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
1866 I40E_RXD_QW1_PTYPE_SHIFT;
1867
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001868 /* populate checksum, VLAN, and protocol */
1869 i40e_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
Mitch Williamsa132af22015-01-24 09:58:35 +00001870
Mitch Williamsa132af22015-01-24 09:58:35 +00001871#ifdef I40E_FCOE
Jesse Brandeburg1f15d662016-04-01 03:56:06 -07001872 if (unlikely(
1873 i40e_rx_is_fcoe(rx_ptype) &&
1874 !i40e_fcoe_handle_offload(rx_ring, rx_desc, skb))) {
Mitch Williamsa132af22015-01-24 09:58:35 +00001875 dev_kfree_skb_any(skb);
1876 continue;
1877 }
1878#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001879
1880 vlan_tag = (qword & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)) ?
1881 le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) : 0;
1882
Mitch Williamsa132af22015-01-24 09:58:35 +00001883 i40e_receive_skb(rx_ring, skb, vlan_tag);
Scott Petersone72e5652017-02-09 23:40:25 -08001884 skb = NULL;
Mitch Williamsa132af22015-01-24 09:58:35 +00001885
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001886 /* update budget accounting */
1887 total_rx_packets++;
1888 }
Mitch Williamsa132af22015-01-24 09:58:35 +00001889
Scott Petersone72e5652017-02-09 23:40:25 -08001890 rx_ring->skb = skb;
1891
Mitch Williamsa132af22015-01-24 09:58:35 +00001892 u64_stats_update_begin(&rx_ring->syncp);
1893 rx_ring->stats.packets += total_rx_packets;
1894 rx_ring->stats.bytes += total_rx_bytes;
1895 u64_stats_update_end(&rx_ring->syncp);
1896 rx_ring->q_vector->rx.total_packets += total_rx_packets;
1897 rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
1898
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07001899 /* guarantee a trip back through this routine if there was a failure */
Jesse Brandeburgc2e245a2016-01-13 16:51:46 -08001900 return failure ? budget : total_rx_packets;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001901}
1902
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001903static u32 i40e_buildreg_itr(const int type, const u16 itr)
1904{
1905 u32 val;
1906
1907 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08001908 /* Don't clear PBA because that can cause lost interrupts that
1909 * came in while we were cleaning/polling
1910 */
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001911 (type << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT) |
1912 (itr << I40E_PFINT_DYN_CTLN_INTERVAL_SHIFT);
1913
1914 return val;
1915}
1916
1917/* a small macro to shorten up some long lines */
1918#define INTREG I40E_PFINT_DYN_CTLN
Carolyn Wyborny3c234c42016-12-12 15:44:12 -08001919static inline int get_rx_itr(struct i40e_vsi *vsi, int idx)
Jacob Keller65e87c02016-09-12 14:18:44 -07001920{
Carolyn Wyborny3c234c42016-12-12 15:44:12 -08001921 return vsi->rx_rings[idx]->rx_itr_setting;
Jacob Keller65e87c02016-09-12 14:18:44 -07001922}
1923
Carolyn Wyborny3c234c42016-12-12 15:44:12 -08001924static inline int get_tx_itr(struct i40e_vsi *vsi, int idx)
Jacob Keller65e87c02016-09-12 14:18:44 -07001925{
Carolyn Wyborny3c234c42016-12-12 15:44:12 -08001926 return vsi->tx_rings[idx]->tx_itr_setting;
Jacob Keller65e87c02016-09-12 14:18:44 -07001927}
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001928
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001929/**
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001930 * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
1931 * @vsi: the VSI we care about
1932 * @q_vector: q_vector for which itr is being updated and interrupt enabled
1933 *
1934 **/
1935static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
1936 struct i40e_q_vector *q_vector)
1937{
1938 struct i40e_hw *hw = &vsi->back->hw;
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001939 bool rx = false, tx = false;
1940 u32 rxval, txval;
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001941 int vector;
Kan Lianga75e8002016-02-19 09:24:04 -05001942 int idx = q_vector->v_idx;
Jacob Keller65e87c02016-09-12 14:18:44 -07001943 int rx_itr_setting, tx_itr_setting;
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001944
1945 vector = (q_vector->v_idx + vsi->base_vector);
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001946
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04001947 /* avoid dynamic calculation if in countdown mode OR if
1948 * all dynamic is disabled
1949 */
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001950 rxval = txval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
1951
Carolyn Wyborny3c234c42016-12-12 15:44:12 -08001952 rx_itr_setting = get_rx_itr(vsi, idx);
1953 tx_itr_setting = get_tx_itr(vsi, idx);
Jacob Keller65e87c02016-09-12 14:18:44 -07001954
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04001955 if (q_vector->itr_countdown > 0 ||
Jacob Keller65e87c02016-09-12 14:18:44 -07001956 (!ITR_IS_DYNAMIC(rx_itr_setting) &&
1957 !ITR_IS_DYNAMIC(tx_itr_setting))) {
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04001958 goto enable_int;
1959 }
1960
Jacob Keller65e87c02016-09-12 14:18:44 -07001961 if (ITR_IS_DYNAMIC(tx_itr_setting)) {
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001962 rx = i40e_set_new_dynamic_itr(&q_vector->rx);
1963 rxval = i40e_buildreg_itr(I40E_RX_ITR, q_vector->rx.itr);
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001964 }
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001965
Jacob Keller65e87c02016-09-12 14:18:44 -07001966 if (ITR_IS_DYNAMIC(tx_itr_setting)) {
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001967 tx = i40e_set_new_dynamic_itr(&q_vector->tx);
1968 txval = i40e_buildreg_itr(I40E_TX_ITR, q_vector->tx.itr);
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04001969 }
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001970
1971 if (rx || tx) {
1972 /* get the higher of the two ITR adjustments and
1973 * use the same value for both ITR registers
1974 * when in adaptive mode (Rx and/or Tx)
1975 */
1976 u16 itr = max(q_vector->tx.itr, q_vector->rx.itr);
1977
1978 q_vector->tx.itr = q_vector->rx.itr = itr;
1979 txval = i40e_buildreg_itr(I40E_TX_ITR, itr);
1980 tx = true;
1981 rxval = i40e_buildreg_itr(I40E_RX_ITR, itr);
1982 rx = true;
1983 }
1984
1985 /* only need to enable the interrupt once, but need
1986 * to possibly update both ITR values
1987 */
1988 if (rx) {
1989 /* set the INTENA_MSK_MASK so that this first write
1990 * won't actually enable the interrupt, instead just
1991 * updating the ITR (it's bit 31 PF and VF)
1992 */
1993 rxval |= BIT(31);
1994 /* don't check _DOWN because interrupt isn't being enabled */
1995 wr32(hw, INTREG(vector - 1), rxval);
1996 }
1997
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04001998enable_int:
Jesse Brandeburg8f5e39c2015-09-28 14:16:51 -04001999 if (!test_bit(__I40E_DOWN, &vsi->state))
2000 wr32(hw, INTREG(vector - 1), txval);
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04002001
2002 if (q_vector->itr_countdown)
2003 q_vector->itr_countdown--;
2004 else
2005 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Carolyn Wybornyde32e3e2015-06-10 13:42:07 -04002006}
2007
2008/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002009 * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
2010 * @napi: napi struct with our devices info in it
2011 * @budget: amount of work driver is allowed to do this pass, in packets
2012 *
2013 * This function will clean all queues associated with a q_vector.
2014 *
2015 * Returns the amount of work done
2016 **/
2017int i40e_napi_poll(struct napi_struct *napi, int budget)
2018{
2019 struct i40e_q_vector *q_vector =
2020 container_of(napi, struct i40e_q_vector, napi);
2021 struct i40e_vsi *vsi = q_vector->vsi;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002022 struct i40e_ring *ring;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002023 bool clean_complete = true;
Jesse Brandeburgd91649f2015-01-07 02:55:01 +00002024 bool arm_wb = false;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002025 int budget_per_ring;
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07002026 int work_done = 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002027
2028 if (test_bit(__I40E_DOWN, &vsi->state)) {
2029 napi_complete(napi);
2030 return 0;
2031 }
2032
Kiran Patil9c6c1252015-11-06 15:26:02 -08002033 /* Clear hung_detected bit */
2034 clear_bit(I40E_Q_VECTOR_HUNG_DETECT, &q_vector->hung_detected);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002035 /* Since the actual Tx work is minimal, we can give the Tx a larger
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002036 * budget and be more aggressive about cleaning up the Tx descriptors.
2037 */
Jesse Brandeburgd91649f2015-01-07 02:55:01 +00002038 i40e_for_each_ring(ring, q_vector->tx) {
Alexander Duycka619afe2016-03-07 09:30:03 -08002039 if (!i40e_clean_tx_irq(vsi, ring, budget)) {
Alexander Duyckf2edaaa2016-03-07 09:29:57 -08002040 clean_complete = false;
2041 continue;
2042 }
2043 arm_wb |= ring->arm_wb;
Jesse Brandeburg0deda862015-07-23 16:54:34 -04002044 ring->arm_wb = false;
Jesse Brandeburgd91649f2015-01-07 02:55:01 +00002045 }
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002046
Alexander Duyckc67cace2015-09-24 09:04:26 -07002047 /* Handle case where we are called by netpoll with a budget of 0 */
2048 if (budget <= 0)
2049 goto tx_only;
2050
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002051 /* We attempt to distribute budget to each Rx queue fairly, but don't
2052 * allow the budget to go below 1 because that would exit polling early.
2053 */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002054 budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002055
Mitch Williamsa132af22015-01-24 09:58:35 +00002056 i40e_for_each_ring(ring, q_vector->rx) {
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002057 int cleaned = i40e_clean_rx_irq(ring, budget_per_ring);
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07002058
2059 work_done += cleaned;
Alexander Duyckf2edaaa2016-03-07 09:29:57 -08002060 /* if we clean as many as budgeted, we must not be done */
2061 if (cleaned >= budget_per_ring)
2062 clean_complete = false;
Mitch Williamsa132af22015-01-24 09:58:35 +00002063 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002064
2065 /* If work not completed, return budget and polling will return */
Jesse Brandeburgd91649f2015-01-07 02:55:01 +00002066 if (!clean_complete) {
Alan Brady96db7762016-09-14 16:24:38 -07002067 const cpumask_t *aff_mask = &q_vector->affinity_mask;
2068 int cpu_id = smp_processor_id();
2069
2070 /* It is possible that the interrupt affinity has changed but,
2071 * if the cpu is pegged at 100%, polling will never exit while
2072 * traffic continues and the interrupt will be stuck on this
2073 * cpu. We check to make sure affinity is correct before we
2074 * continue to poll, otherwise we must stop polling so the
2075 * interrupt can move to the correct cpu.
2076 */
2077 if (likely(cpumask_test_cpu(cpu_id, aff_mask) ||
2078 !(vsi->back->flags & I40E_FLAG_MSIX_ENABLED))) {
Alexander Duyckc67cace2015-09-24 09:04:26 -07002079tx_only:
Alan Brady96db7762016-09-14 16:24:38 -07002080 if (arm_wb) {
2081 q_vector->tx.ring[0].tx_stats.tx_force_wb++;
2082 i40e_enable_wb_on_itr(vsi, q_vector);
2083 }
2084 return budget;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -04002085 }
Jesse Brandeburgd91649f2015-01-07 02:55:01 +00002086 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002087
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04002088 if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
2089 q_vector->arm_wb_state = false;
2090
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002091 /* Work is done so exit the polling mode and re-enable the interrupt */
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07002092 napi_complete_done(napi, work_done);
Alan Brady96db7762016-09-14 16:24:38 -07002093
2094 /* If we're prematurely stopping polling to fix the interrupt
2095 * affinity we want to make sure polling starts back up so we
2096 * issue a call to i40e_force_wb which triggers a SW interrupt.
2097 */
2098 if (!clean_complete)
2099 i40e_force_wb(vsi, q_vector);
2100 else if (!(vsi->back->flags & I40E_FLAG_MSIX_ENABLED))
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08002101 i40e_irq_dynamic_enable_icr0(vsi->back, false);
Alan Brady96db7762016-09-14 16:24:38 -07002102 else
2103 i40e_update_enable_itr(vsi, q_vector);
2104
Alexander Duyck6beb84a2016-11-08 13:05:16 -08002105 return min(work_done, budget - 1);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002106}
2107
2108/**
2109 * i40e_atr - Add a Flow Director ATR filter
2110 * @tx_ring: ring to add programming descriptor to
2111 * @skb: send buffer
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002112 * @tx_flags: send tx flags
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002113 **/
2114static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
Alexander Duyck6b037cd2016-01-24 21:17:36 -08002115 u32 tx_flags)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002116{
2117 struct i40e_filter_program_desc *fdir_desc;
2118 struct i40e_pf *pf = tx_ring->vsi->back;
2119 union {
2120 unsigned char *network;
2121 struct iphdr *ipv4;
2122 struct ipv6hdr *ipv6;
2123 } hdr;
2124 struct tcphdr *th;
2125 unsigned int hlen;
2126 u32 flex_ptype, dtype_cmd;
Alexander Duyckffcc55c2016-01-25 19:32:54 -08002127 int l4_proto;
Alexander Duyckfc4ac672013-09-28 06:00:22 +00002128 u16 i;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002129
2130 /* make sure ATR is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002131 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002132 return;
2133
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08002134 if ((pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED))
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00002135 return;
2136
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002137 /* if sampling is disabled do nothing */
2138 if (!tx_ring->atr_sample_rate)
2139 return;
2140
Alexander Duyck6b037cd2016-01-24 21:17:36 -08002141 /* Currently only IPv4/IPv6 with TCP is supported */
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002142 if (!(tx_flags & (I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6)))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002143 return;
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002144
Alexander Duyckffcc55c2016-01-25 19:32:54 -08002145 /* snag network header to get L4 type and address */
2146 hdr.network = (tx_flags & I40E_TX_FLAGS_UDP_TUNNEL) ?
2147 skb_inner_network_header(skb) : skb_network_header(skb);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002148
Alexander Duyck6b037cd2016-01-24 21:17:36 -08002149 /* Note: tx_flags gets modified to reflect inner protocols in
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002150 * tx_enable_csum function if encap is enabled.
2151 */
Alexander Duyckffcc55c2016-01-25 19:32:54 -08002152 if (tx_flags & I40E_TX_FLAGS_IPV4) {
2153 /* access ihl as u8 to avoid unaligned access on ia64 */
2154 hlen = (hdr.network[0] & 0x0F) << 2;
2155 l4_proto = hdr.ipv4->protocol;
2156 } else {
2157 hlen = hdr.network - skb->data;
2158 l4_proto = ipv6_find_hdr(skb, &hlen, IPPROTO_TCP, NULL, NULL);
2159 hlen -= hdr.network - skb->data;
2160 }
2161
Alexander Duyck6b037cd2016-01-24 21:17:36 -08002162 if (l4_proto != IPPROTO_TCP)
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002163 return;
2164
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002165 th = (struct tcphdr *)(hdr.network + hlen);
2166
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002167 /* Due to lack of space, no more new filters can be programmed */
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08002168 if (th->syn && (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED))
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002169 return;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08002170 if ((pf->flags & I40E_FLAG_HW_ATR_EVICT_CAPABLE) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08002171 (!(pf->hw_disabled_flags & I40E_FLAG_HW_ATR_EVICT_CAPABLE))) {
Anjali Singhai Jain52eb95e2015-06-05 12:20:33 -04002172 /* HW ATR eviction will take care of removing filters on FIN
2173 * and RST packets.
2174 */
2175 if (th->fin || th->rst)
2176 return;
2177 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002178
2179 tx_ring->atr_count++;
2180
Anjali Singhai Jaince806782014-03-06 08:59:54 +00002181 /* sample on all syn/fin/rst packets or once every atr sample rate */
2182 if (!th->fin &&
2183 !th->syn &&
2184 !th->rst &&
2185 (tx_ring->atr_count < tx_ring->atr_sample_rate))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002186 return;
2187
2188 tx_ring->atr_count = 0;
2189
2190 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +00002191 i = tx_ring->next_to_use;
2192 fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
2193
2194 i++;
2195 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002196
2197 flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
2198 I40E_TXD_FLTR_QW0_QINDEX_MASK;
Alexander Duyck6b037cd2016-01-24 21:17:36 -08002199 flex_ptype |= (tx_flags & I40E_TX_FLAGS_IPV4) ?
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002200 (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
2201 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
2202 (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
2203 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
2204
2205 flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
2206
2207 dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
2208
Anjali Singhai Jaince806782014-03-06 08:59:54 +00002209 dtype_cmd |= (th->fin || th->rst) ?
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002210 (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
2211 I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
2212 (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
2213 I40E_TXD_FLTR_QW1_PCMD_SHIFT);
2214
2215 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
2216 I40E_TXD_FLTR_QW1_DEST_SHIFT;
2217
2218 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
2219 I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
2220
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00002221 dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
Singhai, Anjali6a899022015-12-14 12:21:18 -08002222 if (!(tx_flags & I40E_TX_FLAGS_UDP_TUNNEL))
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04002223 dtype_cmd |=
2224 ((u32)I40E_FD_ATR_STAT_IDX(pf->hw.pf_id) <<
2225 I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
2226 I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
2227 else
2228 dtype_cmd |=
2229 ((u32)I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id) <<
2230 I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
2231 I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00002232
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08002233 if ((pf->flags & I40E_FLAG_HW_ATR_EVICT_CAPABLE) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08002234 (!(pf->hw_disabled_flags & I40E_FLAG_HW_ATR_EVICT_CAPABLE)))
Anjali Singhai Jain52eb95e2015-06-05 12:20:33 -04002235 dtype_cmd |= I40E_TXD_FLTR_QW1_ATR_MASK;
2236
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002237 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
Jesse Brandeburg99753ea2014-06-04 04:22:49 +00002238 fdir_desc->rsvd = cpu_to_le32(0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002239 fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
Jesse Brandeburg99753ea2014-06-04 04:22:49 +00002240 fdir_desc->fd_id = cpu_to_le32(0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002241}
2242
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002243/**
2244 * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
2245 * @skb: send buffer
2246 * @tx_ring: ring to send buffer on
2247 * @flags: the tx flags to be set
2248 *
2249 * Checks the skb and set up correspondingly several generic transmit flags
2250 * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
2251 *
2252 * Returns error code indicate the frame should be dropped upon error and the
2253 * otherwise returns 0 to indicate the flags has been set properly.
2254 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002255#ifdef I40E_FCOE
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002256inline int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002257 struct i40e_ring *tx_ring,
2258 u32 *flags)
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002259#else
2260static inline int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
2261 struct i40e_ring *tx_ring,
2262 u32 *flags)
Vasu Dev38e00432014-08-01 13:27:03 -07002263#endif
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002264{
2265 __be16 protocol = skb->protocol;
2266 u32 tx_flags = 0;
2267
Greg Rose31eaacc2015-03-31 00:45:03 -07002268 if (protocol == htons(ETH_P_8021Q) &&
2269 !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
2270 /* When HW VLAN acceleration is turned off by the user the
2271 * stack sets the protocol to 8021q so that the driver
2272 * can take any steps required to support the SW only
2273 * VLAN handling. In our case the driver doesn't need
2274 * to take any further steps so just set the protocol
2275 * to the encapsulated ethertype.
2276 */
2277 skb->protocol = vlan_get_protocol(skb);
2278 goto out;
2279 }
2280
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002281 /* if we have a HW VLAN tag being added, default to the HW one */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002282 if (skb_vlan_tag_present(skb)) {
2283 tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002284 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
2285 /* else if it is a SW VLAN, check the next protocol and store the tag */
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00002286 } else if (protocol == htons(ETH_P_8021Q)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002287 struct vlan_hdr *vhdr, _vhdr;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002288
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002289 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
2290 if (!vhdr)
2291 return -EINVAL;
2292
2293 protocol = vhdr->h_vlan_encapsulated_proto;
2294 tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
2295 tx_flags |= I40E_TX_FLAGS_SW_VLAN;
2296 }
2297
Neerav Parikhd40d00b2015-02-24 06:58:40 +00002298 if (!(tx_ring->vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2299 goto out;
2300
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002301 /* Insert 802.1p priority into VLAN header */
Vasu Dev38e00432014-08-01 13:27:03 -07002302 if ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
2303 (skb->priority != TC_PRIO_CONTROL)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002304 tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
2305 tx_flags |= (skb->priority & 0x7) <<
2306 I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
2307 if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
2308 struct vlan_ethhdr *vhdr;
Francois Romieudd225bc2014-03-30 03:14:48 +00002309 int rc;
2310
2311 rc = skb_cow_head(skb, 0);
2312 if (rc < 0)
2313 return rc;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002314 vhdr = (struct vlan_ethhdr *)skb->data;
2315 vhdr->h_vlan_TCI = htons(tx_flags >>
2316 I40E_TX_FLAGS_VLAN_SHIFT);
2317 } else {
2318 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
2319 }
2320 }
Neerav Parikhd40d00b2015-02-24 06:58:40 +00002321
2322out:
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002323 *flags = tx_flags;
2324 return 0;
2325}
2326
2327/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002328 * i40e_tso - set up the tso context descriptor
Alexander Duyck52ea3e82016-11-28 16:05:59 -08002329 * @first: pointer to first Tx buffer for xmit
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002330 * @hdr_len: ptr to the size of the packet header
Shannon Nelson9c883bd2015-10-21 19:47:02 -04002331 * @cd_type_cmd_tso_mss: Quad Word 1
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002332 *
2333 * Returns 0 if no TSO can happen, 1 if tso is going, or error
2334 **/
Alexander Duyck52ea3e82016-11-28 16:05:59 -08002335static int i40e_tso(struct i40e_tx_buffer *first, u8 *hdr_len,
2336 u64 *cd_type_cmd_tso_mss)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002337{
Alexander Duyck52ea3e82016-11-28 16:05:59 -08002338 struct sk_buff *skb = first->skb;
Alexander Duyck03f9d6a2016-01-24 21:16:20 -08002339 u64 cd_cmd, cd_tso_len, cd_mss;
Alexander Duyckc7770192016-01-24 21:16:35 -08002340 union {
2341 struct iphdr *v4;
2342 struct ipv6hdr *v6;
2343 unsigned char *hdr;
2344 } ip;
Alexander Duyckc49a7bc2016-01-24 21:16:28 -08002345 union {
2346 struct tcphdr *tcp;
Alexander Duyck54532052016-01-24 21:17:29 -08002347 struct udphdr *udp;
Alexander Duyckc49a7bc2016-01-24 21:16:28 -08002348 unsigned char *hdr;
2349 } l4;
2350 u32 paylen, l4_offset;
Alexander Duyck52ea3e82016-11-28 16:05:59 -08002351 u16 gso_segs, gso_size;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002352 int err;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002353
Shannon Nelsone9f65632016-01-04 10:33:04 -08002354 if (skb->ip_summed != CHECKSUM_PARTIAL)
2355 return 0;
2356
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002357 if (!skb_is_gso(skb))
2358 return 0;
2359
Francois Romieudd225bc2014-03-30 03:14:48 +00002360 err = skb_cow_head(skb, 0);
2361 if (err < 0)
2362 return err;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002363
Alexander Duyckc7770192016-01-24 21:16:35 -08002364 ip.hdr = skb_network_header(skb);
2365 l4.hdr = skb_transport_header(skb);
Anjali Singhaidf230752014-12-19 02:58:16 +00002366
Alexander Duyckc7770192016-01-24 21:16:35 -08002367 /* initialize outer IP header fields */
2368 if (ip.v4->version == 4) {
2369 ip.v4->tot_len = 0;
2370 ip.v4->check = 0;
Alexander Duyckc49a7bc2016-01-24 21:16:28 -08002371 } else {
Alexander Duyckc7770192016-01-24 21:16:35 -08002372 ip.v6->payload_len = 0;
2373 }
2374
Alexander Duyck577389a2016-04-02 00:06:56 -07002375 if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002376 SKB_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07002377 SKB_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002378 SKB_GSO_IPXIP6 |
Alexander Duyck577389a2016-04-02 00:06:56 -07002379 SKB_GSO_UDP_TUNNEL |
Alexander Duyck54532052016-01-24 21:17:29 -08002380 SKB_GSO_UDP_TUNNEL_CSUM)) {
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002381 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
2382 (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
2383 l4.udp->len = 0;
2384
Alexander Duyck54532052016-01-24 21:17:29 -08002385 /* determine offset of outer transport header */
2386 l4_offset = l4.hdr - skb->data;
2387
2388 /* remove payload length from outer checksum */
Alexander Duyck24d41e52016-03-18 16:06:47 -07002389 paylen = skb->len - l4_offset;
Jacob Kellerb9c015d2016-12-12 15:44:17 -08002390 csum_replace_by_diff(&l4.udp->check,
2391 (__force __wsum)htonl(paylen));
Alexander Duyck54532052016-01-24 21:17:29 -08002392 }
2393
Alexander Duyckc7770192016-01-24 21:16:35 -08002394 /* reset pointers to inner headers */
2395 ip.hdr = skb_inner_network_header(skb);
2396 l4.hdr = skb_inner_transport_header(skb);
2397
2398 /* initialize inner IP header fields */
2399 if (ip.v4->version == 4) {
2400 ip.v4->tot_len = 0;
2401 ip.v4->check = 0;
2402 } else {
2403 ip.v6->payload_len = 0;
2404 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002405 }
2406
Alexander Duyckc49a7bc2016-01-24 21:16:28 -08002407 /* determine offset of inner transport header */
2408 l4_offset = l4.hdr - skb->data;
2409
2410 /* remove payload length from inner checksum */
Alexander Duyck24d41e52016-03-18 16:06:47 -07002411 paylen = skb->len - l4_offset;
Jacob Kellerb9c015d2016-12-12 15:44:17 -08002412 csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
Alexander Duyckc49a7bc2016-01-24 21:16:28 -08002413
2414 /* compute length of segmentation header */
2415 *hdr_len = (l4.tcp->doff * 4) + l4_offset;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002416
Alexander Duyck52ea3e82016-11-28 16:05:59 -08002417 /* pull values out of skb_shinfo */
2418 gso_size = skb_shinfo(skb)->gso_size;
2419 gso_segs = skb_shinfo(skb)->gso_segs;
2420
2421 /* update GSO size and bytecount with header size */
2422 first->gso_segs = gso_segs;
2423 first->bytecount += (first->gso_segs - 1) * *hdr_len;
2424
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002425 /* find the field values */
2426 cd_cmd = I40E_TX_CTX_DESC_TSO;
2427 cd_tso_len = skb->len - *hdr_len;
Alexander Duyck52ea3e82016-11-28 16:05:59 -08002428 cd_mss = gso_size;
Alexander Duyck03f9d6a2016-01-24 21:16:20 -08002429 *cd_type_cmd_tso_mss |= (cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
2430 (cd_tso_len << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
2431 (cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002432 return 1;
2433}
2434
2435/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002436 * i40e_tsyn - set up the tsyn context descriptor
2437 * @tx_ring: ptr to the ring to send
2438 * @skb: ptr to the skb we're sending
2439 * @tx_flags: the collected send information
Shannon Nelson9c883bd2015-10-21 19:47:02 -04002440 * @cd_type_cmd_tso_mss: Quad Word 1
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002441 *
2442 * Returns 0 if no Tx timestamp can happen and 1 if the timestamp will happen
2443 **/
2444static int i40e_tsyn(struct i40e_ring *tx_ring, struct sk_buff *skb,
2445 u32 tx_flags, u64 *cd_type_cmd_tso_mss)
2446{
2447 struct i40e_pf *pf;
2448
2449 if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
2450 return 0;
2451
2452 /* Tx timestamps cannot be sampled when doing TSO */
2453 if (tx_flags & I40E_TX_FLAGS_TSO)
2454 return 0;
2455
2456 /* only timestamp the outbound packet if the user has requested it and
2457 * we are not already transmitting a packet to be timestamped
2458 */
2459 pf = i40e_netdev_to_pf(tx_ring->netdev);
Jacob Keller22b47772014-12-14 01:55:09 +00002460 if (!(pf->flags & I40E_FLAG_PTP))
2461 return 0;
2462
Jakub Kicinski9ce34f02014-03-15 14:55:42 +00002463 if (pf->ptp_tx &&
2464 !test_and_set_bit_lock(__I40E_PTP_TX_IN_PROGRESS, &pf->state)) {
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002465 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2466 pf->ptp_tx_skb = skb_get(skb);
2467 } else {
2468 return 0;
2469 }
2470
2471 *cd_type_cmd_tso_mss |= (u64)I40E_TX_CTX_DESC_TSYN <<
2472 I40E_TXD_CTX_QW1_CMD_SHIFT;
2473
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002474 return 1;
2475}
2476
2477/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002478 * i40e_tx_enable_csum - Enable Tx checksum offloads
2479 * @skb: send buffer
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002480 * @tx_flags: pointer to Tx flags currently set
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002481 * @td_cmd: Tx descriptor command bits to set
2482 * @td_offset: Tx descriptor header offsets to set
Jean Sacren554f4542015-10-13 01:06:28 -06002483 * @tx_ring: Tx descriptor ring
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002484 * @cd_tunneling: ptr to context desc bits
2485 **/
Alexander Duyck529f1f62016-01-24 21:17:10 -08002486static int i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
2487 u32 *td_cmd, u32 *td_offset,
2488 struct i40e_ring *tx_ring,
2489 u32 *cd_tunneling)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002490{
Alexander Duyckb96b78f2016-01-24 21:16:42 -08002491 union {
2492 struct iphdr *v4;
2493 struct ipv6hdr *v6;
2494 unsigned char *hdr;
2495 } ip;
2496 union {
2497 struct tcphdr *tcp;
2498 struct udphdr *udp;
2499 unsigned char *hdr;
2500 } l4;
Alexander Duycka3fd9d82016-01-24 21:16:54 -08002501 unsigned char *exthdr;
Jesse Brandeburgd1bd7432016-04-01 03:56:04 -07002502 u32 offset, cmd = 0;
Alexander Duycka3fd9d82016-01-24 21:16:54 -08002503 __be16 frag_off;
Alexander Duyckb96b78f2016-01-24 21:16:42 -08002504 u8 l4_proto = 0;
2505
Alexander Duyck529f1f62016-01-24 21:17:10 -08002506 if (skb->ip_summed != CHECKSUM_PARTIAL)
2507 return 0;
2508
Alexander Duyckb96b78f2016-01-24 21:16:42 -08002509 ip.hdr = skb_network_header(skb);
2510 l4.hdr = skb_transport_header(skb);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002511
Alexander Duyck475b4202016-01-24 21:17:01 -08002512 /* compute outer L2 header size */
2513 offset = ((ip.hdr - skb->data) / 2) << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
2514
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002515 if (skb->encapsulation) {
Jesse Brandeburgd1bd7432016-04-01 03:56:04 -07002516 u32 tunnel = 0;
Alexander Duycka0064722016-01-24 21:16:48 -08002517 /* define outer network header type */
2518 if (*tx_flags & I40E_TX_FLAGS_IPV4) {
Alexander Duyck475b4202016-01-24 21:17:01 -08002519 tunnel |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
2520 I40E_TX_CTX_EXT_IP_IPV4 :
2521 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
2522
Alexander Duycka0064722016-01-24 21:16:48 -08002523 l4_proto = ip.v4->protocol;
2524 } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
Alexander Duyck475b4202016-01-24 21:17:01 -08002525 tunnel |= I40E_TX_CTX_EXT_IP_IPV6;
Alexander Duycka3fd9d82016-01-24 21:16:54 -08002526
2527 exthdr = ip.hdr + sizeof(*ip.v6);
Alexander Duycka0064722016-01-24 21:16:48 -08002528 l4_proto = ip.v6->nexthdr;
Alexander Duycka3fd9d82016-01-24 21:16:54 -08002529 if (l4.hdr != exthdr)
2530 ipv6_skip_exthdr(skb, exthdr - skb->data,
2531 &l4_proto, &frag_off);
Alexander Duycka0064722016-01-24 21:16:48 -08002532 }
2533
2534 /* define outer transport */
2535 switch (l4_proto) {
Anjali Singhai Jain45991202015-02-27 09:15:29 +00002536 case IPPROTO_UDP:
Alexander Duyck475b4202016-01-24 21:17:01 -08002537 tunnel |= I40E_TXD_CTX_UDP_TUNNELING;
Singhai, Anjali6a899022015-12-14 12:21:18 -08002538 *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
Anjali Singhai Jain45991202015-02-27 09:15:29 +00002539 break;
Shannon Nelsonc1d17912015-09-25 19:26:04 +00002540 case IPPROTO_GRE:
Alexander Duyck475b4202016-01-24 21:17:01 -08002541 tunnel |= I40E_TXD_CTX_GRE_TUNNELING;
Alexander Duycka0064722016-01-24 21:16:48 -08002542 *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
Shannon Nelsonc1d17912015-09-25 19:26:04 +00002543 break;
Alexander Duyck577389a2016-04-02 00:06:56 -07002544 case IPPROTO_IPIP:
2545 case IPPROTO_IPV6:
2546 *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
2547 l4.hdr = skb_inner_network_header(skb);
2548 break;
Anjali Singhai Jain45991202015-02-27 09:15:29 +00002549 default:
Alexander Duyck529f1f62016-01-24 21:17:10 -08002550 if (*tx_flags & I40E_TX_FLAGS_TSO)
2551 return -1;
2552
2553 skb_checksum_help(skb);
2554 return 0;
Anjali Singhai Jain45991202015-02-27 09:15:29 +00002555 }
Alexander Duyckb96b78f2016-01-24 21:16:42 -08002556
Alexander Duyck577389a2016-04-02 00:06:56 -07002557 /* compute outer L3 header size */
2558 tunnel |= ((l4.hdr - ip.hdr) / 4) <<
2559 I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
2560
2561 /* switch IP header pointer from outer to inner header */
2562 ip.hdr = skb_inner_network_header(skb);
2563
Alexander Duyck475b4202016-01-24 21:17:01 -08002564 /* compute tunnel header size */
2565 tunnel |= ((ip.hdr - l4.hdr) / 2) <<
2566 I40E_TXD_CTX_QW0_NATLEN_SHIFT;
2567
Alexander Duyck54532052016-01-24 21:17:29 -08002568 /* indicate if we need to offload outer UDP header */
2569 if ((*tx_flags & I40E_TX_FLAGS_TSO) &&
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04002570 !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
Alexander Duyck54532052016-01-24 21:17:29 -08002571 (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
2572 tunnel |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
2573
Alexander Duyck475b4202016-01-24 21:17:01 -08002574 /* record tunnel offload values */
2575 *cd_tunneling |= tunnel;
2576
Alexander Duyckb96b78f2016-01-24 21:16:42 -08002577 /* switch L4 header pointer from outer to inner */
Alexander Duyckb96b78f2016-01-24 21:16:42 -08002578 l4.hdr = skb_inner_transport_header(skb);
Alexander Duycka0064722016-01-24 21:16:48 -08002579 l4_proto = 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002580
Alexander Duycka0064722016-01-24 21:16:48 -08002581 /* reset type as we transition from outer to inner headers */
2582 *tx_flags &= ~(I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6);
2583 if (ip.v4->version == 4)
2584 *tx_flags |= I40E_TX_FLAGS_IPV4;
2585 if (ip.v6->version == 6)
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002586 *tx_flags |= I40E_TX_FLAGS_IPV6;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002587 }
2588
2589 /* Enable IP checksum offloads */
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002590 if (*tx_flags & I40E_TX_FLAGS_IPV4) {
Alexander Duyckb96b78f2016-01-24 21:16:42 -08002591 l4_proto = ip.v4->protocol;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002592 /* the stack computes the IP header already, the only time we
2593 * need the hardware to recompute it is in the case of TSO.
2594 */
Alexander Duyck475b4202016-01-24 21:17:01 -08002595 cmd |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
2596 I40E_TX_DESC_CMD_IIPT_IPV4_CSUM :
2597 I40E_TX_DESC_CMD_IIPT_IPV4;
Anjali Singhai Jain89232c32015-04-16 20:06:00 -04002598 } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
Alexander Duyck475b4202016-01-24 21:17:01 -08002599 cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
Alexander Duycka3fd9d82016-01-24 21:16:54 -08002600
2601 exthdr = ip.hdr + sizeof(*ip.v6);
2602 l4_proto = ip.v6->nexthdr;
2603 if (l4.hdr != exthdr)
2604 ipv6_skip_exthdr(skb, exthdr - skb->data,
2605 &l4_proto, &frag_off);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002606 }
Alexander Duyckb96b78f2016-01-24 21:16:42 -08002607
Alexander Duyck475b4202016-01-24 21:17:01 -08002608 /* compute inner L3 header size */
2609 offset |= ((l4.hdr - ip.hdr) / 4) << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002610
2611 /* Enable L4 checksum offloads */
Alexander Duyckb96b78f2016-01-24 21:16:42 -08002612 switch (l4_proto) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002613 case IPPROTO_TCP:
2614 /* enable checksum offloads */
Alexander Duyck475b4202016-01-24 21:17:01 -08002615 cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
2616 offset |= l4.tcp->doff << I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002617 break;
2618 case IPPROTO_SCTP:
2619 /* enable SCTP checksum offload */
Alexander Duyck475b4202016-01-24 21:17:01 -08002620 cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
2621 offset |= (sizeof(struct sctphdr) >> 2) <<
2622 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002623 break;
2624 case IPPROTO_UDP:
2625 /* enable UDP checksum offload */
Alexander Duyck475b4202016-01-24 21:17:01 -08002626 cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
2627 offset |= (sizeof(struct udphdr) >> 2) <<
2628 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002629 break;
2630 default:
Alexander Duyck529f1f62016-01-24 21:17:10 -08002631 if (*tx_flags & I40E_TX_FLAGS_TSO)
2632 return -1;
2633 skb_checksum_help(skb);
2634 return 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002635 }
Alexander Duyck475b4202016-01-24 21:17:01 -08002636
2637 *td_cmd |= cmd;
2638 *td_offset |= offset;
Alexander Duyck529f1f62016-01-24 21:17:10 -08002639
2640 return 1;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002641}
2642
2643/**
2644 * i40e_create_tx_ctx Build the Tx context descriptor
2645 * @tx_ring: ring to create the descriptor on
2646 * @cd_type_cmd_tso_mss: Quad Word 1
2647 * @cd_tunneling: Quad Word 0 - bits 0-31
2648 * @cd_l2tag2: Quad Word 0 - bits 32-63
2649 **/
2650static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
2651 const u64 cd_type_cmd_tso_mss,
2652 const u32 cd_tunneling, const u32 cd_l2tag2)
2653{
2654 struct i40e_tx_context_desc *context_desc;
Alexander Duyckfc4ac672013-09-28 06:00:22 +00002655 int i = tx_ring->next_to_use;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002656
Jesse Brandeburgff40dd52014-02-14 02:14:41 +00002657 if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
2658 !cd_tunneling && !cd_l2tag2)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002659 return;
2660
2661 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +00002662 context_desc = I40E_TX_CTXTDESC(tx_ring, i);
2663
2664 i++;
2665 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002666
2667 /* cpu_to_le32 and assign to struct fields */
2668 context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
2669 context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
Jesse Brandeburg3efbbb22014-06-04 20:41:54 +00002670 context_desc->rsvd = cpu_to_le16(0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002671 context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
2672}
2673
2674/**
Eric Dumazet4567dc12014-10-07 13:30:23 -07002675 * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
2676 * @tx_ring: the ring to be checked
2677 * @size: the size buffer we want to assure is available
2678 *
2679 * Returns -EBUSY if a stop is needed, else 0
2680 **/
Alexander Duyck4ec441d2016-02-17 11:02:43 -08002681int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
Eric Dumazet4567dc12014-10-07 13:30:23 -07002682{
2683 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
2684 /* Memory barrier before checking head and tail */
2685 smp_mb();
2686
2687 /* Check again in a case another CPU has just made room available. */
2688 if (likely(I40E_DESC_UNUSED(tx_ring) < size))
2689 return -EBUSY;
2690
2691 /* A reprieve! - use start_queue because it doesn't call schedule */
2692 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
2693 ++tx_ring->tx_stats.restart_queue;
2694 return 0;
2695}
2696
2697/**
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07002698 * __i40e_chk_linearize - Check if there are more than 8 buffers per packet
Anjali Singhai71da6192015-02-21 06:42:35 +00002699 * @skb: send buffer
Anjali Singhai71da6192015-02-21 06:42:35 +00002700 *
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07002701 * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire
2702 * and so we need to figure out the cases where we need to linearize the skb.
2703 *
2704 * For TSO we need to count the TSO header and segment payload separately.
2705 * As such we need to check cases where we have 7 fragments or more as we
2706 * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
2707 * the segment payload in the first descriptor, and another 7 for the
2708 * fragments.
Anjali Singhai71da6192015-02-21 06:42:35 +00002709 **/
Alexander Duyck2d374902016-02-17 11:02:50 -08002710bool __i40e_chk_linearize(struct sk_buff *skb)
Anjali Singhai71da6192015-02-21 06:42:35 +00002711{
Alexander Duyck2d374902016-02-17 11:02:50 -08002712 const struct skb_frag_struct *frag, *stale;
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07002713 int nr_frags, sum;
Anjali Singhai71da6192015-02-21 06:42:35 +00002714
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07002715 /* no need to check if number of frags is less than 7 */
Alexander Duyck2d374902016-02-17 11:02:50 -08002716 nr_frags = skb_shinfo(skb)->nr_frags;
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07002717 if (nr_frags < (I40E_MAX_BUFFER_TXD - 1))
Alexander Duyck2d374902016-02-17 11:02:50 -08002718 return false;
Anjali Singhai71da6192015-02-21 06:42:35 +00002719
Alexander Duyck2d374902016-02-17 11:02:50 -08002720 /* We need to walk through the list and validate that each group
Alexander Duyck841493a2016-09-06 18:05:04 -07002721 * of 6 fragments totals at least gso_size.
Alexander Duyck2d374902016-02-17 11:02:50 -08002722 */
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07002723 nr_frags -= I40E_MAX_BUFFER_TXD - 2;
Alexander Duyck2d374902016-02-17 11:02:50 -08002724 frag = &skb_shinfo(skb)->frags[0];
2725
2726 /* Initialize size to the negative value of gso_size minus 1. We
2727 * use this as the worst case scenerio in which the frag ahead
2728 * of us only provides one byte which is why we are limited to 6
2729 * descriptors for a single transmit as the header and previous
2730 * fragment are already consuming 2 descriptors.
2731 */
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07002732 sum = 1 - skb_shinfo(skb)->gso_size;
Alexander Duyck2d374902016-02-17 11:02:50 -08002733
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07002734 /* Add size of frags 0 through 4 to create our initial sum */
2735 sum += skb_frag_size(frag++);
2736 sum += skb_frag_size(frag++);
2737 sum += skb_frag_size(frag++);
2738 sum += skb_frag_size(frag++);
2739 sum += skb_frag_size(frag++);
Alexander Duyck2d374902016-02-17 11:02:50 -08002740
2741 /* Walk through fragments adding latest fragment, testing it, and
2742 * then removing stale fragments from the sum.
2743 */
2744 stale = &skb_shinfo(skb)->frags[0];
2745 for (;;) {
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07002746 sum += skb_frag_size(frag++);
Alexander Duyck2d374902016-02-17 11:02:50 -08002747
2748 /* if sum is negative we failed to make sufficient progress */
2749 if (sum < 0)
2750 return true;
2751
Alexander Duyck841493a2016-09-06 18:05:04 -07002752 if (!nr_frags--)
Alexander Duyck2d374902016-02-17 11:02:50 -08002753 break;
2754
Alexander Duyck3f3f7cb2016-03-30 16:15:37 -07002755 sum -= skb_frag_size(stale++);
Anjali Singhai71da6192015-02-21 06:42:35 +00002756 }
2757
Alexander Duyck2d374902016-02-17 11:02:50 -08002758 return false;
Anjali Singhai71da6192015-02-21 06:42:35 +00002759}
2760
2761/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002762 * i40e_tx_map - Build the Tx descriptor
2763 * @tx_ring: ring to send buffer on
2764 * @skb: send buffer
2765 * @first: first buffer info buffer to use
2766 * @tx_flags: collected send information
2767 * @hdr_len: size of the packet header
2768 * @td_cmd: the command field in the descriptor
2769 * @td_offset: offset for checksum or crc
2770 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002771#ifdef I40E_FCOE
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002772inline void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002773 struct i40e_tx_buffer *first, u32 tx_flags,
2774 const u8 hdr_len, u32 td_cmd, u32 td_offset)
Jesse Brandeburg3e587cf2015-04-16 20:06:10 -04002775#else
2776static inline void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
2777 struct i40e_tx_buffer *first, u32 tx_flags,
2778 const u8 hdr_len, u32 td_cmd, u32 td_offset)
Vasu Dev38e00432014-08-01 13:27:03 -07002779#endif
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002780{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002781 unsigned int data_len = skb->data_len;
2782 unsigned int size = skb_headlen(skb);
Alexander Duycka5e9c572013-09-28 06:00:27 +00002783 struct skb_frag_struct *frag;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002784 struct i40e_tx_buffer *tx_bi;
2785 struct i40e_tx_desc *tx_desc;
Alexander Duycka5e9c572013-09-28 06:00:27 +00002786 u16 i = tx_ring->next_to_use;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002787 u32 td_tag = 0;
2788 dma_addr_t dma;
Alexander Duyck1dc8b532016-10-11 15:26:54 -07002789 u16 desc_count = 1;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002790
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002791 if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
2792 td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
2793 td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
2794 I40E_TX_FLAGS_VLAN_SHIFT;
2795 }
2796
Alexander Duycka5e9c572013-09-28 06:00:27 +00002797 first->tx_flags = tx_flags;
2798
2799 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
2800
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002801 tx_desc = I40E_TX_DESC(tx_ring, i);
Alexander Duycka5e9c572013-09-28 06:00:27 +00002802 tx_bi = first;
2803
2804 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
Alexander Duyck5c4654d2016-02-19 12:17:08 -08002805 unsigned int max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
2806
Alexander Duycka5e9c572013-09-28 06:00:27 +00002807 if (dma_mapping_error(tx_ring->dev, dma))
2808 goto dma_error;
2809
2810 /* record length, and DMA address */
2811 dma_unmap_len_set(tx_bi, len, size);
2812 dma_unmap_addr_set(tx_bi, dma, dma);
2813
Alexander Duyck5c4654d2016-02-19 12:17:08 -08002814 /* align size to end of page */
2815 max_data += -dma & (I40E_MAX_READ_REQ_SIZE - 1);
Alexander Duycka5e9c572013-09-28 06:00:27 +00002816 tx_desc->buffer_addr = cpu_to_le64(dma);
2817
2818 while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002819 tx_desc->cmd_type_offset_bsz =
2820 build_ctob(td_cmd, td_offset,
Alexander Duyck5c4654d2016-02-19 12:17:08 -08002821 max_data, td_tag);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002822
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002823 tx_desc++;
2824 i++;
Anjali Singhai58044742015-09-25 18:26:13 -07002825 desc_count++;
2826
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002827 if (i == tx_ring->count) {
2828 tx_desc = I40E_TX_DESC(tx_ring, 0);
2829 i = 0;
2830 }
Alexander Duycka5e9c572013-09-28 06:00:27 +00002831
Alexander Duyck5c4654d2016-02-19 12:17:08 -08002832 dma += max_data;
2833 size -= max_data;
Alexander Duycka5e9c572013-09-28 06:00:27 +00002834
Alexander Duyck5c4654d2016-02-19 12:17:08 -08002835 max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
Alexander Duycka5e9c572013-09-28 06:00:27 +00002836 tx_desc->buffer_addr = cpu_to_le64(dma);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002837 }
2838
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002839 if (likely(!data_len))
2840 break;
2841
Alexander Duycka5e9c572013-09-28 06:00:27 +00002842 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
2843 size, td_tag);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002844
2845 tx_desc++;
2846 i++;
Anjali Singhai58044742015-09-25 18:26:13 -07002847 desc_count++;
2848
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002849 if (i == tx_ring->count) {
2850 tx_desc = I40E_TX_DESC(tx_ring, 0);
2851 i = 0;
2852 }
2853
Alexander Duycka5e9c572013-09-28 06:00:27 +00002854 size = skb_frag_size(frag);
2855 data_len -= size;
2856
2857 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
2858 DMA_TO_DEVICE);
2859
2860 tx_bi = &tx_ring->tx_bi[i];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002861 }
2862
Alexander Duyck1dc8b532016-10-11 15:26:54 -07002863 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
Alexander Duycka5e9c572013-09-28 06:00:27 +00002864
2865 i++;
2866 if (i == tx_ring->count)
2867 i = 0;
2868
2869 tx_ring->next_to_use = i;
2870
Eric Dumazet4567dc12014-10-07 13:30:23 -07002871 i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
Anjali Singhai58044742015-09-25 18:26:13 -07002872
Alexander Duyck1dc8b532016-10-11 15:26:54 -07002873 /* write last descriptor with EOP bit */
2874 td_cmd |= I40E_TX_DESC_CMD_EOP;
2875
2876 /* We can OR these values together as they both are checked against
2877 * 4 below and at this point desc_count will be used as a boolean value
2878 * after this if/else block.
2879 */
2880 desc_count |= ++tx_ring->packet_stride;
2881
Anjali Singhai58044742015-09-25 18:26:13 -07002882 /* Algorithm to optimize tail and RS bit setting:
Alexander Duyck1dc8b532016-10-11 15:26:54 -07002883 * if queue is stopped
2884 * mark RS bit
2885 * reset packet counter
2886 * else if xmit_more is supported and is true
2887 * advance packet counter to 4
2888 * reset desc_count to 0
Anjali Singhai58044742015-09-25 18:26:13 -07002889 *
Alexander Duyck1dc8b532016-10-11 15:26:54 -07002890 * if desc_count >= 4
2891 * mark RS bit
2892 * reset packet counter
2893 * if desc_count > 0
2894 * update tail
Anjali Singhai58044742015-09-25 18:26:13 -07002895 *
Alexander Duyck1dc8b532016-10-11 15:26:54 -07002896 * Note: If there are less than 4 descriptors
Anjali Singhai58044742015-09-25 18:26:13 -07002897 * pending and interrupts were disabled the service task will
2898 * trigger a force WB.
2899 */
Alexander Duyck1dc8b532016-10-11 15:26:54 -07002900 if (netif_xmit_stopped(txring_txq(tx_ring))) {
2901 goto do_rs;
2902 } else if (skb->xmit_more) {
2903 /* set stride to arm on next packet and reset desc_count */
2904 tx_ring->packet_stride = WB_STRIDE;
2905 desc_count = 0;
2906 } else if (desc_count >= WB_STRIDE) {
2907do_rs:
2908 /* write last descriptor with RS bit set */
2909 td_cmd |= I40E_TX_DESC_CMD_RS;
Anjali Singhai58044742015-09-25 18:26:13 -07002910 tx_ring->packet_stride = 0;
Anjali Singhai58044742015-09-25 18:26:13 -07002911 }
Anjali Singhai58044742015-09-25 18:26:13 -07002912
2913 tx_desc->cmd_type_offset_bsz =
Alexander Duyck1dc8b532016-10-11 15:26:54 -07002914 build_ctob(td_cmd, td_offset, size, td_tag);
2915
2916 /* Force memory writes to complete before letting h/w know there
2917 * are new descriptors to fetch.
2918 *
2919 * We also use this memory barrier to make certain all of the
2920 * status bits have been updated before next_to_watch is written.
2921 */
2922 wmb();
2923
2924 /* set next_to_watch value indicating a packet is present */
2925 first->next_to_watch = tx_desc;
Anjali Singhai58044742015-09-25 18:26:13 -07002926
Alexander Duycka5e9c572013-09-28 06:00:27 +00002927 /* notify HW of packet */
Alexander Duyck1dc8b532016-10-11 15:26:54 -07002928 if (desc_count) {
Anjali Singhai58044742015-09-25 18:26:13 -07002929 writel(i, tx_ring->tail);
Alexander Duyck1dc8b532016-10-11 15:26:54 -07002930
2931 /* we need this if more than one processor can write to our tail
2932 * at a time, it synchronizes IO on IA64/Altix systems
2933 */
2934 mmiowb();
Anjali Singhai58044742015-09-25 18:26:13 -07002935 }
Alexander Duyck1dc8b532016-10-11 15:26:54 -07002936
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002937 return;
2938
2939dma_error:
Alexander Duycka5e9c572013-09-28 06:00:27 +00002940 dev_info(tx_ring->dev, "TX DMA map failed\n");
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002941
2942 /* clear dma mappings for failed tx_bi map */
2943 for (;;) {
2944 tx_bi = &tx_ring->tx_bi[i];
Alexander Duycka5e9c572013-09-28 06:00:27 +00002945 i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002946 if (tx_bi == first)
2947 break;
2948 if (i == 0)
2949 i = tx_ring->count;
2950 i--;
2951 }
2952
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002953 tx_ring->next_to_use = i;
2954}
2955
2956/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002957 * i40e_xmit_frame_ring - Sends buffer on Tx ring
2958 * @skb: send buffer
2959 * @tx_ring: ring to send buffer on
2960 *
2961 * Returns NETDEV_TX_OK if sent, else an error code
2962 **/
2963static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
2964 struct i40e_ring *tx_ring)
2965{
2966 u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
2967 u32 cd_tunneling = 0, cd_l2tag2 = 0;
2968 struct i40e_tx_buffer *first;
2969 u32 td_offset = 0;
2970 u32 tx_flags = 0;
2971 __be16 protocol;
2972 u32 td_cmd = 0;
2973 u8 hdr_len = 0;
Alexander Duyck4ec441d2016-02-17 11:02:43 -08002974 int tso, count;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002975 int tsyn;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002976
Jesse Brandeburgb74118f2015-10-26 19:44:30 -04002977 /* prefetch the data, we'll need it later */
2978 prefetch(skb->data);
2979
Alexander Duyck4ec441d2016-02-17 11:02:43 -08002980 count = i40e_xmit_descriptor_count(skb);
Alexander Duyck2d374902016-02-17 11:02:50 -08002981 if (i40e_chk_linearize(skb, count)) {
Alexander Duyck52ea3e82016-11-28 16:05:59 -08002982 if (__skb_linearize(skb)) {
2983 dev_kfree_skb_any(skb);
2984 return NETDEV_TX_OK;
2985 }
Alexander Duyck5c4654d2016-02-19 12:17:08 -08002986 count = i40e_txd_use_count(skb->len);
Alexander Duyck2d374902016-02-17 11:02:50 -08002987 tx_ring->tx_stats.tx_linearize++;
2988 }
Alexander Duyck4ec441d2016-02-17 11:02:43 -08002989
2990 /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
2991 * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
2992 * + 4 desc gap to avoid the cache line where head is,
2993 * + 1 desc for context descriptor,
2994 * otherwise try next time
2995 */
2996 if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
2997 tx_ring->tx_stats.tx_busy++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002998 return NETDEV_TX_BUSY;
Alexander Duyck4ec441d2016-02-17 11:02:43 -08002999 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003000
Alexander Duyck52ea3e82016-11-28 16:05:59 -08003001 /* record the location of the first descriptor for this packet */
3002 first = &tx_ring->tx_bi[tx_ring->next_to_use];
3003 first->skb = skb;
3004 first->bytecount = skb->len;
3005 first->gso_segs = 1;
3006
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003007 /* prepare the xmit flags */
3008 if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
3009 goto out_drop;
3010
3011 /* obtain protocol of skb */
Vlad Yasevich3d34dd02014-08-25 10:34:52 -04003012 protocol = vlan_get_protocol(skb);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003013
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003014 /* setup IPv4/IPv6 offloads */
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00003015 if (protocol == htons(ETH_P_IP))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003016 tx_flags |= I40E_TX_FLAGS_IPV4;
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00003017 else if (protocol == htons(ETH_P_IPV6))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003018 tx_flags |= I40E_TX_FLAGS_IPV6;
3019
Alexander Duyck52ea3e82016-11-28 16:05:59 -08003020 tso = i40e_tso(first, &hdr_len, &cd_type_cmd_tso_mss);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003021
3022 if (tso < 0)
3023 goto out_drop;
3024 else if (tso)
3025 tx_flags |= I40E_TX_FLAGS_TSO;
3026
Alexander Duyck3bc67972016-02-17 11:02:56 -08003027 /* Always offload the checksum, since it's in the data descriptor */
3028 tso = i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
3029 tx_ring, &cd_tunneling);
3030 if (tso < 0)
3031 goto out_drop;
3032
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003033 tsyn = i40e_tsyn(tx_ring, skb, tx_flags, &cd_type_cmd_tso_mss);
3034
3035 if (tsyn)
3036 tx_flags |= I40E_TX_FLAGS_TSYN;
3037
Jakub Kicinski259afec2014-03-15 14:55:37 +00003038 skb_tx_timestamp(skb);
3039
Alexander Duyckb1941302013-09-28 06:00:32 +00003040 /* always enable CRC insertion offload */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003041 td_cmd |= I40E_TX_DESC_CMD_ICRC;
3042
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003043 i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
3044 cd_tunneling, cd_l2tag2);
3045
3046 /* Add Flow Director ATR if it's enabled.
3047 *
3048 * NOTE: this must always be directly before the data descriptor.
3049 */
Alexander Duyck6b037cd2016-01-24 21:17:36 -08003050 i40e_atr(tx_ring, skb, tx_flags);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003051
3052 i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
3053 td_cmd, td_offset);
3054
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003055 return NETDEV_TX_OK;
3056
3057out_drop:
Alexander Duyck52ea3e82016-11-28 16:05:59 -08003058 dev_kfree_skb_any(first->skb);
3059 first->skb = NULL;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003060 return NETDEV_TX_OK;
3061}
3062
3063/**
3064 * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
3065 * @skb: send buffer
3066 * @netdev: network interface device structure
3067 *
3068 * Returns NETDEV_TX_OK if sent, else an error code
3069 **/
3070netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3071{
3072 struct i40e_netdev_priv *np = netdev_priv(netdev);
3073 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck9f65e152013-09-28 06:00:58 +00003074 struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003075
3076 /* hardware can't handle really short frames, hardware padding works
3077 * beyond this point
3078 */
Alexander Duycka94d9e22014-12-03 08:17:39 -08003079 if (skb_put_padto(skb, I40E_MIN_TX_LEN))
3080 return NETDEV_TX_OK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00003081
3082 return i40e_xmit_frame_ring(skb, tx_ring);
3083}