blob: 8d0ef445fa4416df82f6eabd7844c6a7d02d52b1 [file] [log] [blame]
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Greg Rosedc641b72013-12-18 13:45:51 +00004 * Copyright(c) 2013 - 2014 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>
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000028#include "i40e.h"
Jesse Brandeburg206812b2014-02-12 01:45:33 +000029#include "i40e_prototype.h"
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000030
31static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
32 u32 td_tag)
33{
34 return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
35 ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
36 ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
37 ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
38 ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
39}
40
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000041#define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000042/**
43 * i40e_program_fdir_filter - Program a Flow Director filter
Joseph Gasparakis17a73f62014-02-12 01:45:30 +000044 * @fdir_data: Packet data that will be filter parameters
45 * @raw_packet: the pre-allocated packet buffer for FDir
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000046 * @pf: The pf pointer
47 * @add: True for add/update, False for remove
48 **/
Joseph Gasparakis17a73f62014-02-12 01:45:30 +000049int i40e_program_fdir_filter(struct i40e_fdir_filter *fdir_data, u8 *raw_packet,
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000050 struct i40e_pf *pf, bool add)
51{
52 struct i40e_filter_program_desc *fdir_desc;
53 struct i40e_tx_buffer *tx_buf;
54 struct i40e_tx_desc *tx_desc;
55 struct i40e_ring *tx_ring;
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000056 unsigned int fpt, dcc;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000057 struct i40e_vsi *vsi;
58 struct device *dev;
59 dma_addr_t dma;
60 u32 td_cmd = 0;
61 u16 i;
62
63 /* find existing FDIR VSI */
64 vsi = NULL;
65 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
66 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR)
67 vsi = pf->vsi[i];
68 if (!vsi)
69 return -ENOENT;
70
Alexander Duyck9f65e152013-09-28 06:00:58 +000071 tx_ring = vsi->tx_rings[0];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000072 dev = tx_ring->dev;
73
Joseph Gasparakis17a73f62014-02-12 01:45:30 +000074 dma = dma_map_single(dev, raw_packet,
75 I40E_FDIR_MAX_RAW_PACKET_SIZE, DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000076 if (dma_mapping_error(dev, dma))
77 goto dma_fail;
78
79 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +000080 i = tx_ring->next_to_use;
81 fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
Alexander Duyckfc4ac672013-09-28 06:00:22 +000082
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000083 tx_ring->next_to_use = (i + 1 < tx_ring->count) ? i + 1 : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000084
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000085 fpt = (fdir_data->q_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
86 I40E_TXD_FLTR_QW0_QINDEX_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000087
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000088 fpt |= (fdir_data->flex_off << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT) &
89 I40E_TXD_FLTR_QW0_FLEXOFF_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000090
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000091 fpt |= (fdir_data->pctype << I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) &
92 I40E_TXD_FLTR_QW0_PCTYPE_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000093
94 /* Use LAN VSI Id if not programmed by user */
95 if (fdir_data->dest_vsi == 0)
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000096 fpt |= (pf->vsi[pf->lan_vsi]->id) <<
97 I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000098 else
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000099 fpt |= ((u32)fdir_data->dest_vsi <<
100 I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT) &
101 I40E_TXD_FLTR_QW0_DEST_VSI_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000102
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000103 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(fpt);
104
105 dcc = I40E_TX_DESC_DTYPE_FILTER_PROG;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000106
107 if (add)
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000108 dcc |= I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
109 I40E_TXD_FLTR_QW1_PCMD_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000110 else
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000111 dcc |= I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
112 I40E_TXD_FLTR_QW1_PCMD_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000113
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000114 dcc |= (fdir_data->dest_ctl << I40E_TXD_FLTR_QW1_DEST_SHIFT) &
115 I40E_TXD_FLTR_QW1_DEST_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000116
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000117 dcc |= (fdir_data->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT) &
118 I40E_TXD_FLTR_QW1_FD_STATUS_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000119
120 if (fdir_data->cnt_index != 0) {
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000121 dcc |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
122 dcc |= ((u32)fdir_data->cnt_index <<
123 I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
124 I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000125 }
126
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000127 fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dcc);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000128 fdir_desc->fd_id = cpu_to_le32(fdir_data->fd_id);
129
130 /* Now program a dummy descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000131 i = tx_ring->next_to_use;
132 tx_desc = I40E_TX_DESC(tx_ring, i);
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000133 tx_buf = &tx_ring->tx_bi[i];
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000134
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000135 tx_ring->next_to_use = (i + 1 < tx_ring->count) ? i + 1 : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000136
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000137 /* record length, and DMA address */
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000138 dma_unmap_len_set(tx_buf, len, I40E_FDIR_MAX_RAW_PACKET_SIZE);
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000139 dma_unmap_addr_set(tx_buf, dma, dma);
140
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000141 tx_desc->buffer_addr = cpu_to_le64(dma);
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000142 td_cmd = I40E_TXD_CMD | I40E_TX_DESC_CMD_DUMMY;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000143
144 tx_desc->cmd_type_offset_bsz =
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000145 build_ctob(td_cmd, 0, I40E_FDIR_MAX_RAW_PACKET_SIZE, 0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000146
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000147 /* set the timestamp */
148 tx_buf->time_stamp = jiffies;
149
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000150 /* Force memory writes to complete before letting h/w
151 * know there are new descriptors to fetch. (Only
152 * applicable for weak-ordered memory model archs,
153 * such as IA-64).
154 */
155 wmb();
156
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000157 /* Mark the data descriptor to be watched */
158 tx_buf->next_to_watch = tx_desc;
159
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000160 writel(tx_ring->next_to_use, tx_ring->tail);
161 return 0;
162
163dma_fail:
164 return -1;
165}
166
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000167#define IP_HEADER_OFFSET 14
168#define I40E_UDPIP_DUMMY_PACKET_LEN 42
169/**
170 * i40e_add_del_fdir_udpv4 - Add/Remove UDPv4 filters
171 * @vsi: pointer to the targeted VSI
172 * @fd_data: the flow director data required for the FDir descriptor
173 * @raw_packet: the pre-allocated packet buffer for FDir
174 * @add: true adds a filter, false removes it
175 *
176 * Returns 0 if the filters were successfully added or removed
177 **/
178static int i40e_add_del_fdir_udpv4(struct i40e_vsi *vsi,
179 struct i40e_fdir_filter *fd_data,
180 u8 *raw_packet, bool add)
181{
182 struct i40e_pf *pf = vsi->back;
183 struct udphdr *udp;
184 struct iphdr *ip;
185 bool err = false;
186 int ret;
187 int i;
188 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
189 0x45, 0, 0, 0x1c, 0, 0, 0x40, 0, 0x40, 0x11, 0, 0, 0, 0, 0, 0,
190 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
191
192 memcpy(raw_packet, packet, I40E_UDPIP_DUMMY_PACKET_LEN);
193
194 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
195 udp = (struct udphdr *)(raw_packet + IP_HEADER_OFFSET
196 + sizeof(struct iphdr));
197
198 ip->daddr = fd_data->dst_ip[0];
199 udp->dest = fd_data->dst_port;
200 ip->saddr = fd_data->src_ip[0];
201 udp->source = fd_data->src_port;
202
203 for (i = I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP;
204 i <= I40E_FILTER_PCTYPE_NONF_IPV4_UDP; i++) {
205 fd_data->pctype = i;
206 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
207
208 if (ret) {
209 dev_info(&pf->pdev->dev,
210 "Filter command send failed for PCTYPE %d (ret = %d)\n",
211 fd_data->pctype, ret);
212 err = true;
213 } else {
214 dev_info(&pf->pdev->dev,
215 "Filter OK for PCTYPE %d (ret = %d)\n",
216 fd_data->pctype, ret);
217 }
218 }
219
220 return err ? -EOPNOTSUPP : 0;
221}
222
223#define I40E_TCPIP_DUMMY_PACKET_LEN 54
224/**
225 * i40e_add_del_fdir_tcpv4 - Add/Remove TCPv4 filters
226 * @vsi: pointer to the targeted VSI
227 * @fd_data: the flow director data required for the FDir descriptor
228 * @raw_packet: the pre-allocated packet buffer for FDir
229 * @add: true adds a filter, false removes it
230 *
231 * Returns 0 if the filters were successfully added or removed
232 **/
233static int i40e_add_del_fdir_tcpv4(struct i40e_vsi *vsi,
234 struct i40e_fdir_filter *fd_data,
235 u8 *raw_packet, bool add)
236{
237 struct i40e_pf *pf = vsi->back;
238 struct tcphdr *tcp;
239 struct iphdr *ip;
240 bool err = false;
241 int ret;
242 /* Dummy packet */
243 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
244 0x45, 0, 0, 0x28, 0, 0, 0x40, 0, 0x40, 0x6, 0, 0, 0, 0, 0, 0,
245 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0x11,
246 0x0, 0x72, 0, 0, 0, 0};
247
248 memcpy(raw_packet, packet, I40E_TCPIP_DUMMY_PACKET_LEN);
249
250 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
251 tcp = (struct tcphdr *)(raw_packet + IP_HEADER_OFFSET
252 + sizeof(struct iphdr));
253
254 ip->daddr = fd_data->dst_ip[0];
255 tcp->dest = fd_data->dst_port;
256 ip->saddr = fd_data->src_ip[0];
257 tcp->source = fd_data->src_port;
258
259 if (add) {
260 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED) {
261 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 flow being applied\n");
262 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
263 }
264 }
265
266 fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN;
267 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
268
269 if (ret) {
270 dev_info(&pf->pdev->dev,
271 "Filter command send failed for PCTYPE %d (ret = %d)\n",
272 fd_data->pctype, ret);
273 err = true;
274 } else {
275 dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d (ret = %d)\n",
276 fd_data->pctype, ret);
277 }
278
279 fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
280
281 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
282 if (ret) {
283 dev_info(&pf->pdev->dev,
284 "Filter command send failed for PCTYPE %d (ret = %d)\n",
285 fd_data->pctype, ret);
286 err = true;
287 } else {
288 dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d (ret = %d)\n",
289 fd_data->pctype, ret);
290 }
291
292 return err ? -EOPNOTSUPP : 0;
293}
294
295/**
296 * i40e_add_del_fdir_sctpv4 - Add/Remove SCTPv4 Flow Director filters for
297 * a specific flow spec
298 * @vsi: pointer to the targeted VSI
299 * @fd_data: the flow director data required for the FDir descriptor
300 * @raw_packet: the pre-allocated packet buffer for FDir
301 * @add: true adds a filter, false removes it
302 *
Jean Sacren21d3efd2014-03-17 18:14:39 +0000303 * Always returns -EOPNOTSUPP
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000304 **/
305static int i40e_add_del_fdir_sctpv4(struct i40e_vsi *vsi,
306 struct i40e_fdir_filter *fd_data,
307 u8 *raw_packet, bool add)
308{
309 return -EOPNOTSUPP;
310}
311
312#define I40E_IP_DUMMY_PACKET_LEN 34
313/**
314 * i40e_add_del_fdir_ipv4 - Add/Remove IPv4 Flow Director filters for
315 * a specific flow spec
316 * @vsi: pointer to the targeted VSI
317 * @fd_data: the flow director data required for the FDir descriptor
318 * @raw_packet: the pre-allocated packet buffer for FDir
319 * @add: true adds a filter, false removes it
320 *
321 * Returns 0 if the filters were successfully added or removed
322 **/
323static int i40e_add_del_fdir_ipv4(struct i40e_vsi *vsi,
324 struct i40e_fdir_filter *fd_data,
325 u8 *raw_packet, bool add)
326{
327 struct i40e_pf *pf = vsi->back;
328 struct iphdr *ip;
329 bool err = false;
330 int ret;
331 int i;
332 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
333 0x45, 0, 0, 0x14, 0, 0, 0x40, 0, 0x40, 0x10, 0, 0, 0, 0, 0, 0,
334 0, 0, 0, 0};
335
336 memcpy(raw_packet, packet, I40E_IP_DUMMY_PACKET_LEN);
337 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
338
339 ip->saddr = fd_data->src_ip[0];
340 ip->daddr = fd_data->dst_ip[0];
341 ip->protocol = 0;
342
343 for (i = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
344 i <= I40E_FILTER_PCTYPE_FRAG_IPV4; i++) {
345 fd_data->pctype = i;
346 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
347
348 if (ret) {
349 dev_info(&pf->pdev->dev,
350 "Filter command send failed for PCTYPE %d (ret = %d)\n",
351 fd_data->pctype, ret);
352 err = true;
353 } else {
354 dev_info(&pf->pdev->dev,
355 "Filter OK for PCTYPE %d (ret = %d)\n",
356 fd_data->pctype, ret);
357 }
358 }
359
360 return err ? -EOPNOTSUPP : 0;
361}
362
363/**
364 * i40e_add_del_fdir - Build raw packets to add/del fdir filter
365 * @vsi: pointer to the targeted VSI
366 * @cmd: command to get or set RX flow classification rules
367 * @add: true adds a filter, false removes it
368 *
369 **/
370int i40e_add_del_fdir(struct i40e_vsi *vsi,
371 struct i40e_fdir_filter *input, bool add)
372{
373 struct i40e_pf *pf = vsi->back;
374 u8 *raw_packet;
375 int ret;
376
377 /* Populate the Flow Director that we have at the moment
378 * and allocate the raw packet buffer for the calling functions
379 */
380 raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
381 if (!raw_packet)
382 return -ENOMEM;
383
384 switch (input->flow_type & ~FLOW_EXT) {
385 case TCP_V4_FLOW:
386 ret = i40e_add_del_fdir_tcpv4(vsi, input, raw_packet,
387 add);
388 break;
389 case UDP_V4_FLOW:
390 ret = i40e_add_del_fdir_udpv4(vsi, input, raw_packet,
391 add);
392 break;
393 case SCTP_V4_FLOW:
394 ret = i40e_add_del_fdir_sctpv4(vsi, input, raw_packet,
395 add);
396 break;
397 case IPV4_FLOW:
398 ret = i40e_add_del_fdir_ipv4(vsi, input, raw_packet,
399 add);
400 break;
401 case IP_USER_FLOW:
402 switch (input->ip4_proto) {
403 case IPPROTO_TCP:
404 ret = i40e_add_del_fdir_tcpv4(vsi, input,
405 raw_packet, add);
406 break;
407 case IPPROTO_UDP:
408 ret = i40e_add_del_fdir_udpv4(vsi, input,
409 raw_packet, add);
410 break;
411 case IPPROTO_SCTP:
412 ret = i40e_add_del_fdir_sctpv4(vsi, input,
413 raw_packet, add);
414 break;
415 default:
416 ret = i40e_add_del_fdir_ipv4(vsi, input,
417 raw_packet, add);
418 break;
419 }
420 break;
421 default:
Jakub Kicinskic5ffe7e2014-04-02 10:33:22 +0000422 dev_info(&pf->pdev->dev, "Could not specify spec type %d\n",
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000423 input->flow_type);
424 ret = -EINVAL;
425 }
426
427 kfree(raw_packet);
428 return ret;
429}
430
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000431/**
432 * i40e_fd_handle_status - check the Programming Status for FD
433 * @rx_ring: the Rx ring for this descriptor
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000434 * @rx_desc: the Rx descriptor for programming Status, not a packet descriptor.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000435 * @prog_id: the id originally used for programming
436 *
437 * This is used to verify if the FD programming or invalidation
438 * requested by SW to the HW is successful or not and take actions accordingly.
439 **/
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000440static void i40e_fd_handle_status(struct i40e_ring *rx_ring,
441 union i40e_rx_desc *rx_desc, u8 prog_id)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000442{
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000443 struct i40e_pf *pf = rx_ring->vsi->back;
444 struct pci_dev *pdev = pf->pdev;
445 u32 fcnt_prog, fcnt_avail;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000446 u32 error;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000447 u64 qw;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000448
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000449 qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000450 error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
451 I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
452
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000453 if (error == (0x1 << I40E_RX_PROG_STATUS_DESC_FD_TBL_FULL_SHIFT)) {
454 dev_warn(&pdev->dev, "ntuple filter loc = %d, could not be added\n",
455 rx_desc->wb.qword0.hi_dword.fd_id);
456
457 /* filter programming failed most likely due to table full */
458 fcnt_prog = i40e_get_current_fd_count(pf);
459 fcnt_avail = pf->hw.fdir_shared_filter_count +
460 pf->fdir_pf_filter_count;
461
462 /* If ATR is running fcnt_prog can quickly change,
463 * if we are very close to full, it makes sense to disable
464 * FD ATR/SB and then re-enable it when there is room.
465 */
466 if (fcnt_prog >= (fcnt_avail - I40E_FDIR_BUFFER_FULL_MARGIN)) {
467 /* Turn off ATR first */
468 if (pf->flags | I40E_FLAG_FD_ATR_ENABLED) {
469 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
470 dev_warn(&pdev->dev, "FD filter space full, ATR for further flows will be turned off\n");
471 pf->auto_disable_flags |=
472 I40E_FLAG_FD_ATR_ENABLED;
473 pf->flags |= I40E_FLAG_FDIR_REQUIRES_REINIT;
474 } else if (pf->flags | I40E_FLAG_FD_SB_ENABLED) {
475 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
476 dev_warn(&pdev->dev, "FD filter space full, new ntuple rules will not be added\n");
477 pf->auto_disable_flags |=
478 I40E_FLAG_FD_SB_ENABLED;
479 pf->flags |= I40E_FLAG_FDIR_REQUIRES_REINIT;
480 }
481 } else {
Jakub Kicinskic5ffe7e2014-04-02 10:33:22 +0000482 dev_info(&pdev->dev, "FD filter programming error\n");
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000483 }
484 } else if (error ==
485 (0x1 << I40E_RX_PROG_STATUS_DESC_NO_FD_ENTRY_SHIFT)) {
Anjali Singhai Jain13c28842014-03-06 09:00:04 +0000486 if (I40E_DEBUG_FD & pf->hw.debug_mask)
487 dev_info(&pdev->dev, "ntuple filter loc = %d, could not be removed\n",
488 rx_desc->wb.qword0.hi_dword.fd_id);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000489 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000490}
491
492/**
Alexander Duycka5e9c572013-09-28 06:00:27 +0000493 * i40e_unmap_and_free_tx_resource - Release a Tx buffer
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000494 * @ring: the ring that owns the buffer
495 * @tx_buffer: the buffer to free
496 **/
Alexander Duycka5e9c572013-09-28 06:00:27 +0000497static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
498 struct i40e_tx_buffer *tx_buffer)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000499{
Alexander Duycka5e9c572013-09-28 06:00:27 +0000500 if (tx_buffer->skb) {
501 dev_kfree_skb_any(tx_buffer->skb);
502 if (dma_unmap_len(tx_buffer, len))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000503 dma_unmap_single(ring->dev,
Alexander Duyck35a1e2a2013-09-28 06:00:17 +0000504 dma_unmap_addr(tx_buffer, dma),
505 dma_unmap_len(tx_buffer, len),
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000506 DMA_TO_DEVICE);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000507 } else if (dma_unmap_len(tx_buffer, len)) {
508 dma_unmap_page(ring->dev,
509 dma_unmap_addr(tx_buffer, dma),
510 dma_unmap_len(tx_buffer, len),
511 DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000512 }
Alexander Duycka5e9c572013-09-28 06:00:27 +0000513 tx_buffer->next_to_watch = NULL;
514 tx_buffer->skb = NULL;
Alexander Duyck35a1e2a2013-09-28 06:00:17 +0000515 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000516 /* tx_buffer must be completely set up in the transmit path */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000517}
518
519/**
520 * i40e_clean_tx_ring - Free any empty Tx buffers
521 * @tx_ring: ring to be cleaned
522 **/
523void i40e_clean_tx_ring(struct i40e_ring *tx_ring)
524{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000525 unsigned long bi_size;
526 u16 i;
527
528 /* ring already cleared, nothing to do */
529 if (!tx_ring->tx_bi)
530 return;
531
532 /* Free all the Tx ring sk_buffs */
Alexander Duycka5e9c572013-09-28 06:00:27 +0000533 for (i = 0; i < tx_ring->count; i++)
534 i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000535
536 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
537 memset(tx_ring->tx_bi, 0, bi_size);
538
539 /* Zero out the descriptor ring */
540 memset(tx_ring->desc, 0, tx_ring->size);
541
542 tx_ring->next_to_use = 0;
543 tx_ring->next_to_clean = 0;
Alexander Duyck7070ce02013-09-28 06:00:37 +0000544
545 if (!tx_ring->netdev)
546 return;
547
548 /* cleanup Tx queue statistics */
549 netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
550 tx_ring->queue_index));
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000551}
552
553/**
554 * i40e_free_tx_resources - Free Tx resources per queue
555 * @tx_ring: Tx descriptor ring for a specific queue
556 *
557 * Free all transmit software resources
558 **/
559void i40e_free_tx_resources(struct i40e_ring *tx_ring)
560{
561 i40e_clean_tx_ring(tx_ring);
562 kfree(tx_ring->tx_bi);
563 tx_ring->tx_bi = NULL;
564
565 if (tx_ring->desc) {
566 dma_free_coherent(tx_ring->dev, tx_ring->size,
567 tx_ring->desc, tx_ring->dma);
568 tx_ring->desc = NULL;
569 }
570}
571
572/**
573 * i40e_get_tx_pending - how many tx descriptors not processed
574 * @tx_ring: the ring of descriptors
575 *
576 * Since there is no access to the ring head register
577 * in XL710, we need to use our local copies
578 **/
579static u32 i40e_get_tx_pending(struct i40e_ring *ring)
580{
581 u32 ntu = ((ring->next_to_clean <= ring->next_to_use)
582 ? ring->next_to_use
583 : ring->next_to_use + ring->count);
584 return ntu - ring->next_to_clean;
585}
586
587/**
588 * i40e_check_tx_hang - Is there a hang in the Tx queue
589 * @tx_ring: the ring of descriptors
590 **/
591static bool i40e_check_tx_hang(struct i40e_ring *tx_ring)
592{
593 u32 tx_pending = i40e_get_tx_pending(tx_ring);
594 bool ret = false;
595
596 clear_check_for_tx_hang(tx_ring);
597
598 /* Check for a hung queue, but be thorough. This verifies
599 * that a transmit has been completed since the previous
600 * check AND there is at least one packet pending. The
601 * ARMED bit is set to indicate a potential hang. The
602 * bit is cleared if a pause frame is received to remove
603 * false hang detection due to PFC or 802.3x frames. By
604 * requiring this to fail twice we avoid races with
605 * PFC clearing the ARMED bit and conditions where we
606 * run the check_tx_hang logic with a transmit completion
607 * pending but without time to complete it yet.
608 */
Alexander Duycka114d0a2013-09-28 06:00:43 +0000609 if ((tx_ring->tx_stats.tx_done_old == tx_ring->stats.packets) &&
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000610 tx_pending) {
611 /* make sure it is true for two checks in a row */
612 ret = test_and_set_bit(__I40E_HANG_CHECK_ARMED,
613 &tx_ring->state);
614 } else {
615 /* update completed stats and disarm the hang check */
Alexander Duycka114d0a2013-09-28 06:00:43 +0000616 tx_ring->tx_stats.tx_done_old = tx_ring->stats.packets;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000617 clear_bit(__I40E_HANG_CHECK_ARMED, &tx_ring->state);
618 }
619
620 return ret;
621}
622
623/**
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000624 * i40e_get_head - Retrieve head from head writeback
625 * @tx_ring: tx ring to fetch head of
626 *
627 * Returns value of Tx ring head based on value stored
628 * in head write-back location
629 **/
630static inline u32 i40e_get_head(struct i40e_ring *tx_ring)
631{
632 void *head = (struct i40e_tx_desc *)tx_ring->desc + tx_ring->count;
633
634 return le32_to_cpu(*(volatile __le32 *)head);
635}
636
637/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000638 * i40e_clean_tx_irq - Reclaim resources after transmit completes
639 * @tx_ring: tx ring to clean
640 * @budget: how many cleans we're allowed
641 *
642 * Returns true if there's any budget left (e.g. the clean is finished)
643 **/
644static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
645{
646 u16 i = tx_ring->next_to_clean;
647 struct i40e_tx_buffer *tx_buf;
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000648 struct i40e_tx_desc *tx_head;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000649 struct i40e_tx_desc *tx_desc;
650 unsigned int total_packets = 0;
651 unsigned int total_bytes = 0;
652
653 tx_buf = &tx_ring->tx_bi[i];
654 tx_desc = I40E_TX_DESC(tx_ring, i);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000655 i -= tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000656
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000657 tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
658
Alexander Duycka5e9c572013-09-28 06:00:27 +0000659 do {
660 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000661
662 /* if next_to_watch is not set then there is no work pending */
663 if (!eop_desc)
664 break;
665
Alexander Duycka5e9c572013-09-28 06:00:27 +0000666 /* prevent any other reads prior to eop_desc */
667 read_barrier_depends();
668
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000669 /* we have caught up to head, no work left to do */
670 if (tx_head == tx_desc)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000671 break;
672
Alexander Duyckc304fda2013-09-28 06:00:12 +0000673 /* clear next_to_watch to prevent false hangs */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000674 tx_buf->next_to_watch = NULL;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000675
Alexander Duycka5e9c572013-09-28 06:00:27 +0000676 /* update the statistics for this packet */
677 total_bytes += tx_buf->bytecount;
678 total_packets += tx_buf->gso_segs;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000679
Alexander Duycka5e9c572013-09-28 06:00:27 +0000680 /* free the skb */
681 dev_kfree_skb_any(tx_buf->skb);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000682
Alexander Duycka5e9c572013-09-28 06:00:27 +0000683 /* unmap skb header data */
684 dma_unmap_single(tx_ring->dev,
685 dma_unmap_addr(tx_buf, dma),
686 dma_unmap_len(tx_buf, len),
687 DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000688
Alexander Duycka5e9c572013-09-28 06:00:27 +0000689 /* clear tx_buffer data */
690 tx_buf->skb = NULL;
691 dma_unmap_len_set(tx_buf, len, 0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000692
Alexander Duycka5e9c572013-09-28 06:00:27 +0000693 /* unmap remaining buffers */
694 while (tx_desc != eop_desc) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000695
696 tx_buf++;
697 tx_desc++;
698 i++;
Alexander Duycka5e9c572013-09-28 06:00:27 +0000699 if (unlikely(!i)) {
700 i -= tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000701 tx_buf = tx_ring->tx_bi;
702 tx_desc = I40E_TX_DESC(tx_ring, 0);
703 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000704
Alexander Duycka5e9c572013-09-28 06:00:27 +0000705 /* unmap any remaining paged data */
706 if (dma_unmap_len(tx_buf, len)) {
707 dma_unmap_page(tx_ring->dev,
708 dma_unmap_addr(tx_buf, dma),
709 dma_unmap_len(tx_buf, len),
710 DMA_TO_DEVICE);
711 dma_unmap_len_set(tx_buf, len, 0);
712 }
713 }
714
715 /* move us one more past the eop_desc for start of next pkt */
716 tx_buf++;
717 tx_desc++;
718 i++;
719 if (unlikely(!i)) {
720 i -= tx_ring->count;
721 tx_buf = tx_ring->tx_bi;
722 tx_desc = I40E_TX_DESC(tx_ring, 0);
723 }
724
725 /* update budget accounting */
726 budget--;
727 } while (likely(budget));
728
729 i += tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000730 tx_ring->next_to_clean = i;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000731 u64_stats_update_begin(&tx_ring->syncp);
Alexander Duycka114d0a2013-09-28 06:00:43 +0000732 tx_ring->stats.bytes += total_bytes;
733 tx_ring->stats.packets += total_packets;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000734 u64_stats_update_end(&tx_ring->syncp);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000735 tx_ring->q_vector->tx.total_bytes += total_bytes;
736 tx_ring->q_vector->tx.total_packets += total_packets;
Alexander Duycka5e9c572013-09-28 06:00:27 +0000737
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000738 if (check_for_tx_hang(tx_ring) && i40e_check_tx_hang(tx_ring)) {
739 /* schedule immediate reset if we believe we hung */
740 dev_info(tx_ring->dev, "Detected Tx Unit Hang\n"
741 " VSI <%d>\n"
742 " Tx Queue <%d>\n"
743 " next_to_use <%x>\n"
744 " next_to_clean <%x>\n",
745 tx_ring->vsi->seid,
746 tx_ring->queue_index,
747 tx_ring->next_to_use, i);
748 dev_info(tx_ring->dev, "tx_bi[next_to_clean]\n"
749 " time_stamp <%lx>\n"
750 " jiffies <%lx>\n",
751 tx_ring->tx_bi[i].time_stamp, jiffies);
752
753 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
754
755 dev_info(tx_ring->dev,
756 "tx hang detected on queue %d, resetting adapter\n",
757 tx_ring->queue_index);
758
759 tx_ring->netdev->netdev_ops->ndo_tx_timeout(tx_ring->netdev);
760
761 /* the adapter is about to reset, no point in enabling stuff */
762 return true;
763 }
764
Alexander Duyck7070ce02013-09-28 06:00:37 +0000765 netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
766 tx_ring->queue_index),
767 total_packets, total_bytes);
768
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000769#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
770 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
771 (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
772 /* Make sure that anybody stopping the queue after this
773 * sees the new next_to_clean.
774 */
775 smp_mb();
776 if (__netif_subqueue_stopped(tx_ring->netdev,
777 tx_ring->queue_index) &&
778 !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
779 netif_wake_subqueue(tx_ring->netdev,
780 tx_ring->queue_index);
781 ++tx_ring->tx_stats.restart_queue;
782 }
783 }
784
785 return budget > 0;
786}
787
788/**
789 * i40e_set_new_dynamic_itr - Find new ITR level
790 * @rc: structure containing ring performance data
791 *
792 * Stores a new ITR value based on packets and byte counts during
793 * the last interrupt. The advantage of per interrupt computation
794 * is faster updates and more accurate ITR for the current traffic
795 * pattern. Constants in this function were computed based on
796 * theoretical maximum wire speed and thresholds were set based on
797 * testing data as well as attempting to minimize response time
798 * while increasing bulk throughput.
799 **/
800static void i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
801{
802 enum i40e_latency_range new_latency_range = rc->latency_range;
803 u32 new_itr = rc->itr;
804 int bytes_per_int;
805
806 if (rc->total_packets == 0 || !rc->itr)
807 return;
808
809 /* simple throttlerate management
810 * 0-10MB/s lowest (100000 ints/s)
811 * 10-20MB/s low (20000 ints/s)
812 * 20-1249MB/s bulk (8000 ints/s)
813 */
814 bytes_per_int = rc->total_bytes / rc->itr;
815 switch (rc->itr) {
816 case I40E_LOWEST_LATENCY:
817 if (bytes_per_int > 10)
818 new_latency_range = I40E_LOW_LATENCY;
819 break;
820 case I40E_LOW_LATENCY:
821 if (bytes_per_int > 20)
822 new_latency_range = I40E_BULK_LATENCY;
823 else if (bytes_per_int <= 10)
824 new_latency_range = I40E_LOWEST_LATENCY;
825 break;
826 case I40E_BULK_LATENCY:
827 if (bytes_per_int <= 20)
828 rc->latency_range = I40E_LOW_LATENCY;
829 break;
830 }
831
832 switch (new_latency_range) {
833 case I40E_LOWEST_LATENCY:
834 new_itr = I40E_ITR_100K;
835 break;
836 case I40E_LOW_LATENCY:
837 new_itr = I40E_ITR_20K;
838 break;
839 case I40E_BULK_LATENCY:
840 new_itr = I40E_ITR_8K;
841 break;
842 default:
843 break;
844 }
845
846 if (new_itr != rc->itr) {
847 /* do an exponential smoothing */
848 new_itr = (10 * new_itr * rc->itr) /
849 ((9 * new_itr) + rc->itr);
850 rc->itr = new_itr & I40E_MAX_ITR;
851 }
852
853 rc->total_bytes = 0;
854 rc->total_packets = 0;
855}
856
857/**
858 * i40e_update_dynamic_itr - Adjust ITR based on bytes per int
859 * @q_vector: the vector to adjust
860 **/
861static void i40e_update_dynamic_itr(struct i40e_q_vector *q_vector)
862{
863 u16 vector = q_vector->vsi->base_vector + q_vector->v_idx;
864 struct i40e_hw *hw = &q_vector->vsi->back->hw;
865 u32 reg_addr;
866 u16 old_itr;
867
868 reg_addr = I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1);
869 old_itr = q_vector->rx.itr;
870 i40e_set_new_dynamic_itr(&q_vector->rx);
871 if (old_itr != q_vector->rx.itr)
872 wr32(hw, reg_addr, q_vector->rx.itr);
873
874 reg_addr = I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1);
875 old_itr = q_vector->tx.itr;
876 i40e_set_new_dynamic_itr(&q_vector->tx);
877 if (old_itr != q_vector->tx.itr)
878 wr32(hw, reg_addr, q_vector->tx.itr);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000879}
880
881/**
882 * i40e_clean_programming_status - clean the programming status descriptor
883 * @rx_ring: the rx ring that has this descriptor
884 * @rx_desc: the rx descriptor written back by HW
885 *
886 * Flow director should handle FD_FILTER_STATUS to check its filter programming
887 * status being successful or not and take actions accordingly. FCoE should
888 * handle its context/filter programming/invalidation status and take actions.
889 *
890 **/
891static void i40e_clean_programming_status(struct i40e_ring *rx_ring,
892 union i40e_rx_desc *rx_desc)
893{
894 u64 qw;
895 u8 id;
896
897 qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
898 id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
899 I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
900
901 if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000902 i40e_fd_handle_status(rx_ring, rx_desc, id);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000903}
904
905/**
906 * i40e_setup_tx_descriptors - Allocate the Tx descriptors
907 * @tx_ring: the tx ring to set up
908 *
909 * Return 0 on success, negative on error
910 **/
911int i40e_setup_tx_descriptors(struct i40e_ring *tx_ring)
912{
913 struct device *dev = tx_ring->dev;
914 int bi_size;
915
916 if (!dev)
917 return -ENOMEM;
918
919 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
920 tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
921 if (!tx_ring->tx_bi)
922 goto err;
923
924 /* round up to nearest 4K */
925 tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000926 /* add u32 for head writeback, align after this takes care of
927 * guaranteeing this is at least one cache line in size
928 */
929 tx_ring->size += sizeof(u32);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000930 tx_ring->size = ALIGN(tx_ring->size, 4096);
931 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
932 &tx_ring->dma, GFP_KERNEL);
933 if (!tx_ring->desc) {
934 dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
935 tx_ring->size);
936 goto err;
937 }
938
939 tx_ring->next_to_use = 0;
940 tx_ring->next_to_clean = 0;
941 return 0;
942
943err:
944 kfree(tx_ring->tx_bi);
945 tx_ring->tx_bi = NULL;
946 return -ENOMEM;
947}
948
949/**
950 * i40e_clean_rx_ring - Free Rx buffers
951 * @rx_ring: ring to be cleaned
952 **/
953void i40e_clean_rx_ring(struct i40e_ring *rx_ring)
954{
955 struct device *dev = rx_ring->dev;
956 struct i40e_rx_buffer *rx_bi;
957 unsigned long bi_size;
958 u16 i;
959
960 /* ring already cleared, nothing to do */
961 if (!rx_ring->rx_bi)
962 return;
963
964 /* Free all the Rx ring sk_buffs */
965 for (i = 0; i < rx_ring->count; i++) {
966 rx_bi = &rx_ring->rx_bi[i];
967 if (rx_bi->dma) {
968 dma_unmap_single(dev,
969 rx_bi->dma,
970 rx_ring->rx_buf_len,
971 DMA_FROM_DEVICE);
972 rx_bi->dma = 0;
973 }
974 if (rx_bi->skb) {
975 dev_kfree_skb(rx_bi->skb);
976 rx_bi->skb = NULL;
977 }
978 if (rx_bi->page) {
979 if (rx_bi->page_dma) {
980 dma_unmap_page(dev,
981 rx_bi->page_dma,
982 PAGE_SIZE / 2,
983 DMA_FROM_DEVICE);
984 rx_bi->page_dma = 0;
985 }
986 __free_page(rx_bi->page);
987 rx_bi->page = NULL;
988 rx_bi->page_offset = 0;
989 }
990 }
991
992 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
993 memset(rx_ring->rx_bi, 0, bi_size);
994
995 /* Zero out the descriptor ring */
996 memset(rx_ring->desc, 0, rx_ring->size);
997
998 rx_ring->next_to_clean = 0;
999 rx_ring->next_to_use = 0;
1000}
1001
1002/**
1003 * i40e_free_rx_resources - Free Rx resources
1004 * @rx_ring: ring to clean the resources from
1005 *
1006 * Free all receive software resources
1007 **/
1008void i40e_free_rx_resources(struct i40e_ring *rx_ring)
1009{
1010 i40e_clean_rx_ring(rx_ring);
1011 kfree(rx_ring->rx_bi);
1012 rx_ring->rx_bi = NULL;
1013
1014 if (rx_ring->desc) {
1015 dma_free_coherent(rx_ring->dev, rx_ring->size,
1016 rx_ring->desc, rx_ring->dma);
1017 rx_ring->desc = NULL;
1018 }
1019}
1020
1021/**
1022 * i40e_setup_rx_descriptors - Allocate Rx descriptors
1023 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
1024 *
1025 * Returns 0 on success, negative on failure
1026 **/
1027int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring)
1028{
1029 struct device *dev = rx_ring->dev;
1030 int bi_size;
1031
1032 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
1033 rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
1034 if (!rx_ring->rx_bi)
1035 goto err;
1036
1037 /* Round up to nearest 4K */
1038 rx_ring->size = ring_is_16byte_desc_enabled(rx_ring)
1039 ? rx_ring->count * sizeof(union i40e_16byte_rx_desc)
1040 : rx_ring->count * sizeof(union i40e_32byte_rx_desc);
1041 rx_ring->size = ALIGN(rx_ring->size, 4096);
1042 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
1043 &rx_ring->dma, GFP_KERNEL);
1044
1045 if (!rx_ring->desc) {
1046 dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
1047 rx_ring->size);
1048 goto err;
1049 }
1050
1051 rx_ring->next_to_clean = 0;
1052 rx_ring->next_to_use = 0;
1053
1054 return 0;
1055err:
1056 kfree(rx_ring->rx_bi);
1057 rx_ring->rx_bi = NULL;
1058 return -ENOMEM;
1059}
1060
1061/**
1062 * i40e_release_rx_desc - Store the new tail and head values
1063 * @rx_ring: ring to bump
1064 * @val: new head index
1065 **/
1066static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
1067{
1068 rx_ring->next_to_use = val;
1069 /* Force memory writes to complete before letting h/w
1070 * know there are new descriptors to fetch. (Only
1071 * applicable for weak-ordered memory model archs,
1072 * such as IA-64).
1073 */
1074 wmb();
1075 writel(val, rx_ring->tail);
1076}
1077
1078/**
1079 * i40e_alloc_rx_buffers - Replace used receive buffers; packet split
1080 * @rx_ring: ring to place buffers on
1081 * @cleaned_count: number of buffers to replace
1082 **/
1083void i40e_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
1084{
1085 u16 i = rx_ring->next_to_use;
1086 union i40e_rx_desc *rx_desc;
1087 struct i40e_rx_buffer *bi;
1088 struct sk_buff *skb;
1089
1090 /* do nothing if no valid netdev defined */
1091 if (!rx_ring->netdev || !cleaned_count)
1092 return;
1093
1094 while (cleaned_count--) {
1095 rx_desc = I40E_RX_DESC(rx_ring, i);
1096 bi = &rx_ring->rx_bi[i];
1097 skb = bi->skb;
1098
1099 if (!skb) {
1100 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
1101 rx_ring->rx_buf_len);
1102 if (!skb) {
Mitch Williams420136c2013-12-18 13:45:59 +00001103 rx_ring->rx_stats.alloc_buff_failed++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001104 goto no_buffers;
1105 }
1106 /* initialize queue mapping */
1107 skb_record_rx_queue(skb, rx_ring->queue_index);
1108 bi->skb = skb;
1109 }
1110
1111 if (!bi->dma) {
1112 bi->dma = dma_map_single(rx_ring->dev,
1113 skb->data,
1114 rx_ring->rx_buf_len,
1115 DMA_FROM_DEVICE);
1116 if (dma_mapping_error(rx_ring->dev, bi->dma)) {
Mitch Williams420136c2013-12-18 13:45:59 +00001117 rx_ring->rx_stats.alloc_buff_failed++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001118 bi->dma = 0;
1119 goto no_buffers;
1120 }
1121 }
1122
1123 if (ring_is_ps_enabled(rx_ring)) {
1124 if (!bi->page) {
1125 bi->page = alloc_page(GFP_ATOMIC);
1126 if (!bi->page) {
Mitch Williams420136c2013-12-18 13:45:59 +00001127 rx_ring->rx_stats.alloc_page_failed++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001128 goto no_buffers;
1129 }
1130 }
1131
1132 if (!bi->page_dma) {
1133 /* use a half page if we're re-using */
1134 bi->page_offset ^= PAGE_SIZE / 2;
1135 bi->page_dma = dma_map_page(rx_ring->dev,
1136 bi->page,
1137 bi->page_offset,
1138 PAGE_SIZE / 2,
1139 DMA_FROM_DEVICE);
1140 if (dma_mapping_error(rx_ring->dev,
1141 bi->page_dma)) {
Mitch Williams420136c2013-12-18 13:45:59 +00001142 rx_ring->rx_stats.alloc_page_failed++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001143 bi->page_dma = 0;
1144 goto no_buffers;
1145 }
1146 }
1147
1148 /* Refresh the desc even if buffer_addrs didn't change
1149 * because each write-back erases this info.
1150 */
1151 rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
1152 rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
1153 } else {
1154 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
1155 rx_desc->read.hdr_addr = 0;
1156 }
1157 i++;
1158 if (i == rx_ring->count)
1159 i = 0;
1160 }
1161
1162no_buffers:
1163 if (rx_ring->next_to_use != i)
1164 i40e_release_rx_desc(rx_ring, i);
1165}
1166
1167/**
1168 * i40e_receive_skb - Send a completed packet up the stack
1169 * @rx_ring: rx ring in play
1170 * @skb: packet to send up
1171 * @vlan_tag: vlan tag for packet
1172 **/
1173static void i40e_receive_skb(struct i40e_ring *rx_ring,
1174 struct sk_buff *skb, u16 vlan_tag)
1175{
1176 struct i40e_q_vector *q_vector = rx_ring->q_vector;
1177 struct i40e_vsi *vsi = rx_ring->vsi;
1178 u64 flags = vsi->back->flags;
1179
1180 if (vlan_tag & VLAN_VID_MASK)
1181 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
1182
1183 if (flags & I40E_FLAG_IN_NETPOLL)
1184 netif_rx(skb);
1185 else
1186 napi_gro_receive(&q_vector->napi, skb);
1187}
1188
1189/**
1190 * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
1191 * @vsi: the VSI we care about
1192 * @skb: skb currently being received and modified
1193 * @rx_status: status value of last descriptor in packet
1194 * @rx_error: error value of last descriptor in packet
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001195 * @rx_ptype: ptype value of last descriptor in packet
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001196 **/
1197static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
1198 struct sk_buff *skb,
1199 u32 rx_status,
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001200 u32 rx_error,
1201 u16 rx_ptype)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001202{
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001203 bool ipv4_tunnel, ipv6_tunnel;
1204 __wsum rx_udp_csum;
1205 __sum16 csum;
1206 struct iphdr *iph;
1207
1208 ipv4_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT4_MAC_PAY3) &&
1209 (rx_ptype < I40E_RX_PTYPE_GRENAT4_MACVLAN_IPV6_ICMP_PAY4);
1210 ipv6_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT6_MAC_PAY3) &&
1211 (rx_ptype < I40E_RX_PTYPE_GRENAT6_MACVLAN_IPV6_ICMP_PAY4);
1212
1213 skb->encapsulation = ipv4_tunnel || ipv6_tunnel;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001214 skb->ip_summed = CHECKSUM_NONE;
1215
1216 /* Rx csum enabled and ip headers found? */
1217 if (!(vsi->netdev->features & NETIF_F_RXCSUM &&
1218 rx_status & (1 << I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
1219 return;
1220
Jesse Brandeburgddf1d0d2014-02-13 03:48:39 -08001221 /* likely incorrect csum if alternate IP extension headers found */
Shannon Nelson8ee75a82013-12-21 05:44:46 +00001222 if (rx_status & (1 << I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
1223 return;
1224
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001225 /* IP or L4 or outmost IP checksum error */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001226 if (rx_error & ((1 << I40E_RX_DESC_ERROR_IPE_SHIFT) |
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001227 (1 << I40E_RX_DESC_ERROR_L4E_SHIFT) |
1228 (1 << I40E_RX_DESC_ERROR_EIPE_SHIFT))) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001229 vsi->back->hw_csum_rx_error++;
1230 return;
1231 }
1232
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001233 if (ipv4_tunnel &&
1234 !(rx_status & (1 << I40E_RX_DESC_STATUS_UDP_0_SHIFT))) {
1235 /* If VXLAN traffic has an outer UDPv4 checksum we need to check
1236 * it in the driver, hardware does not do it for us.
1237 * Since L3L4P bit was set we assume a valid IHL value (>=5)
1238 * so the total length of IPv4 header is IHL*4 bytes
1239 */
1240 skb->transport_header = skb->mac_header +
1241 sizeof(struct ethhdr) +
1242 (ip_hdr(skb)->ihl * 4);
1243
1244 /* Add 4 bytes for VLAN tagged packets */
1245 skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
1246 skb->protocol == htons(ETH_P_8021AD))
1247 ? VLAN_HLEN : 0;
1248
1249 rx_udp_csum = udp_csum(skb);
1250 iph = ip_hdr(skb);
1251 csum = csum_tcpudp_magic(
1252 iph->saddr, iph->daddr,
1253 (skb->len - skb_transport_offset(skb)),
1254 IPPROTO_UDP, rx_udp_csum);
1255
1256 if (udp_hdr(skb)->check != csum) {
1257 vsi->back->hw_csum_rx_error++;
1258 return;
1259 }
1260 }
1261
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001262 skb->ip_summed = CHECKSUM_UNNECESSARY;
1263}
1264
1265/**
1266 * i40e_rx_hash - returns the hash value from the Rx descriptor
1267 * @ring: descriptor ring
1268 * @rx_desc: specific descriptor
1269 **/
1270static inline u32 i40e_rx_hash(struct i40e_ring *ring,
1271 union i40e_rx_desc *rx_desc)
1272{
Jesse Brandeburg8a494922013-11-20 10:02:49 +00001273 const __le64 rss_mask =
1274 cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
1275 I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
1276
1277 if ((ring->netdev->features & NETIF_F_RXHASH) &&
1278 (rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask)
1279 return le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
1280 else
1281 return 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001282}
1283
1284/**
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001285 * i40e_ptype_to_hash - get a hash type
1286 * @ptype: the ptype value from the descriptor
1287 *
1288 * Returns a hash type to be used by skb_set_hash
1289 **/
1290static inline enum pkt_hash_types i40e_ptype_to_hash(u8 ptype)
1291{
1292 struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
1293
1294 if (!decoded.known)
1295 return PKT_HASH_TYPE_NONE;
1296
1297 if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1298 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
1299 return PKT_HASH_TYPE_L4;
1300 else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1301 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
1302 return PKT_HASH_TYPE_L3;
1303 else
1304 return PKT_HASH_TYPE_L2;
1305}
1306
1307/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001308 * i40e_clean_rx_irq - Reclaim resources after receive completes
1309 * @rx_ring: rx ring to clean
1310 * @budget: how many cleans we're allowed
1311 *
1312 * Returns true if there's any budget left (e.g. the clean is finished)
1313 **/
1314static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
1315{
1316 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1317 u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
1318 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
1319 const int current_node = numa_node_id();
1320 struct i40e_vsi *vsi = rx_ring->vsi;
1321 u16 i = rx_ring->next_to_clean;
1322 union i40e_rx_desc *rx_desc;
1323 u32 rx_error, rx_status;
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001324 u8 rx_ptype;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001325 u64 qword;
1326
Eric W. Biederman390f86d2014-03-14 17:59:10 -07001327 if (budget <= 0)
1328 return 0;
1329
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001330 rx_desc = I40E_RX_DESC(rx_ring, i);
1331 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Jesse Brandeburg6838b532014-01-14 00:49:52 -08001332 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1333 I40E_RXD_QW1_STATUS_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001334
1335 while (rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)) {
1336 union i40e_rx_desc *next_rxd;
1337 struct i40e_rx_buffer *rx_bi;
1338 struct sk_buff *skb;
1339 u16 vlan_tag;
1340 if (i40e_rx_is_programming_status(qword)) {
1341 i40e_clean_programming_status(rx_ring, rx_desc);
1342 I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
1343 goto next_desc;
1344 }
1345 rx_bi = &rx_ring->rx_bi[i];
1346 skb = rx_bi->skb;
1347 prefetch(skb->data);
1348
Mitch Williams829af3ac2013-12-18 13:46:00 +00001349 rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
1350 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
1351 rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
1352 I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
1353 rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
1354 I40E_RXD_QW1_LENGTH_SPH_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001355
Mitch Williams829af3ac2013-12-18 13:46:00 +00001356 rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
1357 I40E_RXD_QW1_ERROR_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001358 rx_hbo = rx_error & (1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
1359 rx_error &= ~(1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
1360
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001361 rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
1362 I40E_RXD_QW1_PTYPE_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001363 rx_bi->skb = NULL;
1364
1365 /* This memory barrier is needed to keep us from reading
1366 * any other fields out of the rx_desc until we know the
1367 * STATUS_DD bit is set
1368 */
1369 rmb();
1370
1371 /* Get the header and possibly the whole packet
1372 * If this is an skb from previous receive dma will be 0
1373 */
1374 if (rx_bi->dma) {
1375 u16 len;
1376
1377 if (rx_hbo)
1378 len = I40E_RX_HDR_SIZE;
1379 else if (rx_sph)
1380 len = rx_header_len;
1381 else if (rx_packet_len)
1382 len = rx_packet_len; /* 1buf/no split found */
1383 else
1384 len = rx_header_len; /* split always mode */
1385
1386 skb_put(skb, len);
1387 dma_unmap_single(rx_ring->dev,
1388 rx_bi->dma,
1389 rx_ring->rx_buf_len,
1390 DMA_FROM_DEVICE);
1391 rx_bi->dma = 0;
1392 }
1393
1394 /* Get the rest of the data if this was a header split */
1395 if (ring_is_ps_enabled(rx_ring) && rx_packet_len) {
1396
1397 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
1398 rx_bi->page,
1399 rx_bi->page_offset,
1400 rx_packet_len);
1401
1402 skb->len += rx_packet_len;
1403 skb->data_len += rx_packet_len;
1404 skb->truesize += rx_packet_len;
1405
1406 if ((page_count(rx_bi->page) == 1) &&
1407 (page_to_nid(rx_bi->page) == current_node))
1408 get_page(rx_bi->page);
1409 else
1410 rx_bi->page = NULL;
1411
1412 dma_unmap_page(rx_ring->dev,
1413 rx_bi->page_dma,
1414 PAGE_SIZE / 2,
1415 DMA_FROM_DEVICE);
1416 rx_bi->page_dma = 0;
1417 }
1418 I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
1419
1420 if (unlikely(
1421 !(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
1422 struct i40e_rx_buffer *next_buffer;
1423
1424 next_buffer = &rx_ring->rx_bi[i];
1425
1426 if (ring_is_ps_enabled(rx_ring)) {
1427 rx_bi->skb = next_buffer->skb;
1428 rx_bi->dma = next_buffer->dma;
1429 next_buffer->skb = skb;
1430 next_buffer->dma = 0;
1431 }
1432 rx_ring->rx_stats.non_eop_descs++;
1433 goto next_desc;
1434 }
1435
1436 /* ERR_MASK will only have valid bits if EOP set */
1437 if (unlikely(rx_error & (1 << I40E_RX_DESC_ERROR_RXE_SHIFT))) {
1438 dev_kfree_skb_any(skb);
1439 goto next_desc;
1440 }
1441
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001442 skb_set_hash(skb, i40e_rx_hash(rx_ring, rx_desc),
1443 i40e_ptype_to_hash(rx_ptype));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001444 if (unlikely(rx_status & I40E_RXD_QW1_STATUS_TSYNVALID_MASK)) {
1445 i40e_ptp_rx_hwtstamp(vsi->back, skb, (rx_status &
1446 I40E_RXD_QW1_STATUS_TSYNINDX_MASK) >>
1447 I40E_RXD_QW1_STATUS_TSYNINDX_SHIFT);
1448 rx_ring->last_rx_timestamp = jiffies;
1449 }
1450
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001451 /* probably a little skewed due to removing CRC */
1452 total_rx_bytes += skb->len;
1453 total_rx_packets++;
1454
1455 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001456
1457 i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
1458
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001459 vlan_tag = rx_status & (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
1460 ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
1461 : 0;
1462 i40e_receive_skb(rx_ring, skb, vlan_tag);
1463
1464 rx_ring->netdev->last_rx = jiffies;
1465 budget--;
1466next_desc:
1467 rx_desc->wb.qword1.status_error_len = 0;
1468 if (!budget)
1469 break;
1470
1471 cleaned_count++;
1472 /* return some buffers to hardware, one at a time is too slow */
1473 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
1474 i40e_alloc_rx_buffers(rx_ring, cleaned_count);
1475 cleaned_count = 0;
1476 }
1477
1478 /* use prefetched values */
1479 rx_desc = next_rxd;
1480 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Mitch Williams829af3ac2013-12-18 13:46:00 +00001481 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1482 I40E_RXD_QW1_STATUS_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001483 }
1484
1485 rx_ring->next_to_clean = i;
Alexander Duyck980e9b12013-09-28 06:01:03 +00001486 u64_stats_update_begin(&rx_ring->syncp);
Alexander Duycka114d0a2013-09-28 06:00:43 +00001487 rx_ring->stats.packets += total_rx_packets;
1488 rx_ring->stats.bytes += total_rx_bytes;
Alexander Duyck980e9b12013-09-28 06:01:03 +00001489 u64_stats_update_end(&rx_ring->syncp);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001490 rx_ring->q_vector->rx.total_packets += total_rx_packets;
1491 rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
1492
1493 if (cleaned_count)
1494 i40e_alloc_rx_buffers(rx_ring, cleaned_count);
1495
1496 return budget > 0;
1497}
1498
1499/**
1500 * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
1501 * @napi: napi struct with our devices info in it
1502 * @budget: amount of work driver is allowed to do this pass, in packets
1503 *
1504 * This function will clean all queues associated with a q_vector.
1505 *
1506 * Returns the amount of work done
1507 **/
1508int i40e_napi_poll(struct napi_struct *napi, int budget)
1509{
1510 struct i40e_q_vector *q_vector =
1511 container_of(napi, struct i40e_q_vector, napi);
1512 struct i40e_vsi *vsi = q_vector->vsi;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001513 struct i40e_ring *ring;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001514 bool clean_complete = true;
1515 int budget_per_ring;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001516
1517 if (test_bit(__I40E_DOWN, &vsi->state)) {
1518 napi_complete(napi);
1519 return 0;
1520 }
1521
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001522 /* Since the actual Tx work is minimal, we can give the Tx a larger
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001523 * budget and be more aggressive about cleaning up the Tx descriptors.
1524 */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001525 i40e_for_each_ring(ring, q_vector->tx)
1526 clean_complete &= i40e_clean_tx_irq(ring, vsi->work_limit);
1527
1528 /* We attempt to distribute budget to each Rx queue fairly, but don't
1529 * allow the budget to go below 1 because that would exit polling early.
1530 */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001531 budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001532
1533 i40e_for_each_ring(ring, q_vector->rx)
1534 clean_complete &= i40e_clean_rx_irq(ring, budget_per_ring);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001535
1536 /* If work not completed, return budget and polling will return */
1537 if (!clean_complete)
1538 return budget;
1539
1540 /* Work is done so exit the polling mode and re-enable the interrupt */
1541 napi_complete(napi);
1542 if (ITR_IS_DYNAMIC(vsi->rx_itr_setting) ||
1543 ITR_IS_DYNAMIC(vsi->tx_itr_setting))
1544 i40e_update_dynamic_itr(q_vector);
1545
1546 if (!test_bit(__I40E_DOWN, &vsi->state)) {
1547 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
1548 i40e_irq_dynamic_enable(vsi,
1549 q_vector->v_idx + vsi->base_vector);
1550 } else {
1551 struct i40e_hw *hw = &vsi->back->hw;
1552 /* We re-enable the queue 0 cause, but
1553 * don't worry about dynamic_enable
1554 * because we left it on for the other
1555 * possible interrupts during napi
1556 */
1557 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
1558 qval |= I40E_QINT_RQCTL_CAUSE_ENA_MASK;
1559 wr32(hw, I40E_QINT_RQCTL(0), qval);
1560
1561 qval = rd32(hw, I40E_QINT_TQCTL(0));
1562 qval |= I40E_QINT_TQCTL_CAUSE_ENA_MASK;
1563 wr32(hw, I40E_QINT_TQCTL(0), qval);
Shannon Nelson116a57d2013-09-28 07:13:59 +00001564
1565 i40e_irq_dynamic_enable_icr0(vsi->back);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001566 }
1567 }
1568
1569 return 0;
1570}
1571
1572/**
1573 * i40e_atr - Add a Flow Director ATR filter
1574 * @tx_ring: ring to add programming descriptor to
1575 * @skb: send buffer
1576 * @flags: send flags
1577 * @protocol: wire protocol
1578 **/
1579static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
1580 u32 flags, __be16 protocol)
1581{
1582 struct i40e_filter_program_desc *fdir_desc;
1583 struct i40e_pf *pf = tx_ring->vsi->back;
1584 union {
1585 unsigned char *network;
1586 struct iphdr *ipv4;
1587 struct ipv6hdr *ipv6;
1588 } hdr;
1589 struct tcphdr *th;
1590 unsigned int hlen;
1591 u32 flex_ptype, dtype_cmd;
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001592 u16 i;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001593
1594 /* make sure ATR is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08001595 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001596 return;
1597
1598 /* if sampling is disabled do nothing */
1599 if (!tx_ring->atr_sample_rate)
1600 return;
1601
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001602 /* snag network header to get L4 type and address */
1603 hdr.network = skb_network_header(skb);
1604
1605 /* Currently only IPv4/IPv6 with TCP is supported */
1606 if (protocol == htons(ETH_P_IP)) {
1607 if (hdr.ipv4->protocol != IPPROTO_TCP)
1608 return;
1609
1610 /* access ihl as a u8 to avoid unaligned access on ia64 */
1611 hlen = (hdr.network[0] & 0x0F) << 2;
1612 } else if (protocol == htons(ETH_P_IPV6)) {
1613 if (hdr.ipv6->nexthdr != IPPROTO_TCP)
1614 return;
1615
1616 hlen = sizeof(struct ipv6hdr);
1617 } else {
1618 return;
1619 }
1620
1621 th = (struct tcphdr *)(hdr.network + hlen);
1622
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00001623 /* Due to lack of space, no more new filters can be programmed */
1624 if (th->syn && (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1625 return;
1626
1627 tx_ring->atr_count++;
1628
Anjali Singhai Jaince806782014-03-06 08:59:54 +00001629 /* sample on all syn/fin/rst packets or once every atr sample rate */
1630 if (!th->fin &&
1631 !th->syn &&
1632 !th->rst &&
1633 (tx_ring->atr_count < tx_ring->atr_sample_rate))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001634 return;
1635
1636 tx_ring->atr_count = 0;
1637
1638 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001639 i = tx_ring->next_to_use;
1640 fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
1641
1642 i++;
1643 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001644
1645 flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
1646 I40E_TXD_FLTR_QW0_QINDEX_MASK;
1647 flex_ptype |= (protocol == htons(ETH_P_IP)) ?
1648 (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
1649 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
1650 (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
1651 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
1652
1653 flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
1654
1655 dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
1656
Anjali Singhai Jaince806782014-03-06 08:59:54 +00001657 dtype_cmd |= (th->fin || th->rst) ?
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001658 (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
1659 I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
1660 (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
1661 I40E_TXD_FLTR_QW1_PCMD_SHIFT);
1662
1663 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
1664 I40E_TXD_FLTR_QW1_DEST_SHIFT;
1665
1666 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
1667 I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
1668
1669 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
1670 fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
1671}
1672
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001673/**
1674 * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
1675 * @skb: send buffer
1676 * @tx_ring: ring to send buffer on
1677 * @flags: the tx flags to be set
1678 *
1679 * Checks the skb and set up correspondingly several generic transmit flags
1680 * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
1681 *
1682 * Returns error code indicate the frame should be dropped upon error and the
1683 * otherwise returns 0 to indicate the flags has been set properly.
1684 **/
1685static int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
1686 struct i40e_ring *tx_ring,
1687 u32 *flags)
1688{
1689 __be16 protocol = skb->protocol;
1690 u32 tx_flags = 0;
1691
1692 /* if we have a HW VLAN tag being added, default to the HW one */
1693 if (vlan_tx_tag_present(skb)) {
1694 tx_flags |= vlan_tx_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
1695 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1696 /* else if it is a SW VLAN, check the next protocol and store the tag */
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00001697 } else if (protocol == htons(ETH_P_8021Q)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001698 struct vlan_hdr *vhdr, _vhdr;
1699 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
1700 if (!vhdr)
1701 return -EINVAL;
1702
1703 protocol = vhdr->h_vlan_encapsulated_proto;
1704 tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
1705 tx_flags |= I40E_TX_FLAGS_SW_VLAN;
1706 }
1707
1708 /* Insert 802.1p priority into VLAN header */
1709 if ((tx_ring->vsi->back->flags & I40E_FLAG_DCB_ENABLED) &&
1710 ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
1711 (skb->priority != TC_PRIO_CONTROL))) {
1712 tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
1713 tx_flags |= (skb->priority & 0x7) <<
1714 I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
1715 if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
1716 struct vlan_ethhdr *vhdr;
Francois Romieudd225bc2014-03-30 03:14:48 +00001717 int rc;
1718
1719 rc = skb_cow_head(skb, 0);
1720 if (rc < 0)
1721 return rc;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001722 vhdr = (struct vlan_ethhdr *)skb->data;
1723 vhdr->h_vlan_TCI = htons(tx_flags >>
1724 I40E_TX_FLAGS_VLAN_SHIFT);
1725 } else {
1726 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1727 }
1728 }
1729 *flags = tx_flags;
1730 return 0;
1731}
1732
1733/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001734 * i40e_tso - set up the tso context descriptor
1735 * @tx_ring: ptr to the ring to send
1736 * @skb: ptr to the skb we're sending
1737 * @tx_flags: the collected send information
1738 * @protocol: the send protocol
1739 * @hdr_len: ptr to the size of the packet header
1740 * @cd_tunneling: ptr to context descriptor bits
1741 *
1742 * Returns 0 if no TSO can happen, 1 if tso is going, or error
1743 **/
1744static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
1745 u32 tx_flags, __be16 protocol, u8 *hdr_len,
1746 u64 *cd_type_cmd_tso_mss, u32 *cd_tunneling)
1747{
1748 u32 cd_cmd, cd_tso_len, cd_mss;
Francois Romieudd225bc2014-03-30 03:14:48 +00001749 struct ipv6hdr *ipv6h;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001750 struct tcphdr *tcph;
1751 struct iphdr *iph;
1752 u32 l4len;
1753 int err;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001754
1755 if (!skb_is_gso(skb))
1756 return 0;
1757
Francois Romieudd225bc2014-03-30 03:14:48 +00001758 err = skb_cow_head(skb, 0);
1759 if (err < 0)
1760 return err;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001761
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00001762 if (protocol == htons(ETH_P_IP)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001763 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
1764 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1765 iph->tot_len = 0;
1766 iph->check = 0;
1767 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1768 0, IPPROTO_TCP, 0);
1769 } else if (skb_is_gso_v6(skb)) {
1770
1771 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb)
1772 : ipv6_hdr(skb);
1773 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1774 ipv6h->payload_len = 0;
1775 tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
1776 0, IPPROTO_TCP, 0);
1777 }
1778
1779 l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
1780 *hdr_len = (skb->encapsulation
1781 ? (skb_inner_transport_header(skb) - skb->data)
1782 : skb_transport_offset(skb)) + l4len;
1783
1784 /* find the field values */
1785 cd_cmd = I40E_TX_CTX_DESC_TSO;
1786 cd_tso_len = skb->len - *hdr_len;
1787 cd_mss = skb_shinfo(skb)->gso_size;
Mitch Williams829af3ac2013-12-18 13:46:00 +00001788 *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
1789 ((u64)cd_tso_len <<
1790 I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
1791 ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001792 return 1;
1793}
1794
1795/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001796 * i40e_tsyn - set up the tsyn context descriptor
1797 * @tx_ring: ptr to the ring to send
1798 * @skb: ptr to the skb we're sending
1799 * @tx_flags: the collected send information
1800 *
1801 * Returns 0 if no Tx timestamp can happen and 1 if the timestamp will happen
1802 **/
1803static int i40e_tsyn(struct i40e_ring *tx_ring, struct sk_buff *skb,
1804 u32 tx_flags, u64 *cd_type_cmd_tso_mss)
1805{
1806 struct i40e_pf *pf;
1807
1808 if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
1809 return 0;
1810
1811 /* Tx timestamps cannot be sampled when doing TSO */
1812 if (tx_flags & I40E_TX_FLAGS_TSO)
1813 return 0;
1814
1815 /* only timestamp the outbound packet if the user has requested it and
1816 * we are not already transmitting a packet to be timestamped
1817 */
1818 pf = i40e_netdev_to_pf(tx_ring->netdev);
1819 if (pf->ptp_tx && !pf->ptp_tx_skb) {
1820 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1821 pf->ptp_tx_skb = skb_get(skb);
1822 } else {
1823 return 0;
1824 }
1825
1826 *cd_type_cmd_tso_mss |= (u64)I40E_TX_CTX_DESC_TSYN <<
1827 I40E_TXD_CTX_QW1_CMD_SHIFT;
1828
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001829 return 1;
1830}
1831
1832/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001833 * i40e_tx_enable_csum - Enable Tx checksum offloads
1834 * @skb: send buffer
1835 * @tx_flags: Tx flags currently set
1836 * @td_cmd: Tx descriptor command bits to set
1837 * @td_offset: Tx descriptor header offsets to set
1838 * @cd_tunneling: ptr to context desc bits
1839 **/
1840static void i40e_tx_enable_csum(struct sk_buff *skb, u32 tx_flags,
1841 u32 *td_cmd, u32 *td_offset,
1842 struct i40e_ring *tx_ring,
1843 u32 *cd_tunneling)
1844{
1845 struct ipv6hdr *this_ipv6_hdr;
1846 unsigned int this_tcp_hdrlen;
1847 struct iphdr *this_ip_hdr;
1848 u32 network_hdr_len;
1849 u8 l4_hdr = 0;
1850
1851 if (skb->encapsulation) {
1852 network_hdr_len = skb_inner_network_header_len(skb);
1853 this_ip_hdr = inner_ip_hdr(skb);
1854 this_ipv6_hdr = inner_ipv6_hdr(skb);
1855 this_tcp_hdrlen = inner_tcp_hdrlen(skb);
1856
1857 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1858
1859 if (tx_flags & I40E_TX_FLAGS_TSO) {
1860 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
1861 ip_hdr(skb)->check = 0;
1862 } else {
1863 *cd_tunneling |=
1864 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1865 }
1866 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1867 if (tx_flags & I40E_TX_FLAGS_TSO) {
1868 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
1869 ip_hdr(skb)->check = 0;
1870 } else {
1871 *cd_tunneling |=
1872 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1873 }
1874 }
1875
1876 /* Now set the ctx descriptor fields */
1877 *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
1878 I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
1879 I40E_TXD_CTX_UDP_TUNNELING |
1880 ((skb_inner_network_offset(skb) -
1881 skb_transport_offset(skb)) >> 1) <<
1882 I40E_TXD_CTX_QW0_NATLEN_SHIFT;
1883
1884 } else {
1885 network_hdr_len = skb_network_header_len(skb);
1886 this_ip_hdr = ip_hdr(skb);
1887 this_ipv6_hdr = ipv6_hdr(skb);
1888 this_tcp_hdrlen = tcp_hdrlen(skb);
1889 }
1890
1891 /* Enable IP checksum offloads */
1892 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1893 l4_hdr = this_ip_hdr->protocol;
1894 /* the stack computes the IP header already, the only time we
1895 * need the hardware to recompute it is in the case of TSO.
1896 */
1897 if (tx_flags & I40E_TX_FLAGS_TSO) {
1898 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
1899 this_ip_hdr->check = 0;
1900 } else {
1901 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
1902 }
1903 /* Now set the td_offset for IP header length */
1904 *td_offset = (network_hdr_len >> 2) <<
1905 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1906 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1907 l4_hdr = this_ipv6_hdr->nexthdr;
1908 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
1909 /* Now set the td_offset for IP header length */
1910 *td_offset = (network_hdr_len >> 2) <<
1911 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1912 }
1913 /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
1914 *td_offset |= (skb_network_offset(skb) >> 1) <<
1915 I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
1916
1917 /* Enable L4 checksum offloads */
1918 switch (l4_hdr) {
1919 case IPPROTO_TCP:
1920 /* enable checksum offloads */
1921 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
1922 *td_offset |= (this_tcp_hdrlen >> 2) <<
1923 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1924 break;
1925 case IPPROTO_SCTP:
1926 /* enable SCTP checksum offload */
1927 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
1928 *td_offset |= (sizeof(struct sctphdr) >> 2) <<
1929 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1930 break;
1931 case IPPROTO_UDP:
1932 /* enable UDP checksum offload */
1933 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
1934 *td_offset |= (sizeof(struct udphdr) >> 2) <<
1935 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1936 break;
1937 default:
1938 break;
1939 }
1940}
1941
1942/**
1943 * i40e_create_tx_ctx Build the Tx context descriptor
1944 * @tx_ring: ring to create the descriptor on
1945 * @cd_type_cmd_tso_mss: Quad Word 1
1946 * @cd_tunneling: Quad Word 0 - bits 0-31
1947 * @cd_l2tag2: Quad Word 0 - bits 32-63
1948 **/
1949static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
1950 const u64 cd_type_cmd_tso_mss,
1951 const u32 cd_tunneling, const u32 cd_l2tag2)
1952{
1953 struct i40e_tx_context_desc *context_desc;
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001954 int i = tx_ring->next_to_use;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001955
Jesse Brandeburgff40dd52014-02-14 02:14:41 +00001956 if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
1957 !cd_tunneling && !cd_l2tag2)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001958 return;
1959
1960 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001961 context_desc = I40E_TX_CTXTDESC(tx_ring, i);
1962
1963 i++;
1964 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001965
1966 /* cpu_to_le32 and assign to struct fields */
1967 context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
1968 context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
1969 context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
1970}
1971
1972/**
1973 * i40e_tx_map - Build the Tx descriptor
1974 * @tx_ring: ring to send buffer on
1975 * @skb: send buffer
1976 * @first: first buffer info buffer to use
1977 * @tx_flags: collected send information
1978 * @hdr_len: size of the packet header
1979 * @td_cmd: the command field in the descriptor
1980 * @td_offset: offset for checksum or crc
1981 **/
1982static void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
1983 struct i40e_tx_buffer *first, u32 tx_flags,
1984 const u8 hdr_len, u32 td_cmd, u32 td_offset)
1985{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001986 unsigned int data_len = skb->data_len;
1987 unsigned int size = skb_headlen(skb);
Alexander Duycka5e9c572013-09-28 06:00:27 +00001988 struct skb_frag_struct *frag;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001989 struct i40e_tx_buffer *tx_bi;
1990 struct i40e_tx_desc *tx_desc;
Alexander Duycka5e9c572013-09-28 06:00:27 +00001991 u16 i = tx_ring->next_to_use;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001992 u32 td_tag = 0;
1993 dma_addr_t dma;
1994 u16 gso_segs;
1995
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001996 if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
1997 td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
1998 td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
1999 I40E_TX_FLAGS_VLAN_SHIFT;
2000 }
2001
Alexander Duycka5e9c572013-09-28 06:00:27 +00002002 if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
2003 gso_segs = skb_shinfo(skb)->gso_segs;
2004 else
2005 gso_segs = 1;
2006
2007 /* multiply data chunks by size of headers */
2008 first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
2009 first->gso_segs = gso_segs;
2010 first->skb = skb;
2011 first->tx_flags = tx_flags;
2012
2013 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
2014
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002015 tx_desc = I40E_TX_DESC(tx_ring, i);
Alexander Duycka5e9c572013-09-28 06:00:27 +00002016 tx_bi = first;
2017
2018 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
2019 if (dma_mapping_error(tx_ring->dev, dma))
2020 goto dma_error;
2021
2022 /* record length, and DMA address */
2023 dma_unmap_len_set(tx_bi, len, size);
2024 dma_unmap_addr_set(tx_bi, dma, dma);
2025
2026 tx_desc->buffer_addr = cpu_to_le64(dma);
2027
2028 while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002029 tx_desc->cmd_type_offset_bsz =
2030 build_ctob(td_cmd, td_offset,
2031 I40E_MAX_DATA_PER_TXD, td_tag);
2032
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002033 tx_desc++;
2034 i++;
2035 if (i == tx_ring->count) {
2036 tx_desc = I40E_TX_DESC(tx_ring, 0);
2037 i = 0;
2038 }
Alexander Duycka5e9c572013-09-28 06:00:27 +00002039
2040 dma += I40E_MAX_DATA_PER_TXD;
2041 size -= I40E_MAX_DATA_PER_TXD;
2042
2043 tx_desc->buffer_addr = cpu_to_le64(dma);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002044 }
2045
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002046 if (likely(!data_len))
2047 break;
2048
Alexander Duycka5e9c572013-09-28 06:00:27 +00002049 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
2050 size, td_tag);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002051
2052 tx_desc++;
2053 i++;
2054 if (i == tx_ring->count) {
2055 tx_desc = I40E_TX_DESC(tx_ring, 0);
2056 i = 0;
2057 }
2058
Alexander Duycka5e9c572013-09-28 06:00:27 +00002059 size = skb_frag_size(frag);
2060 data_len -= size;
2061
2062 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
2063 DMA_TO_DEVICE);
2064
2065 tx_bi = &tx_ring->tx_bi[i];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002066 }
2067
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002068 /* Place RS bit on last descriptor of any packet that spans across the
2069 * 4th descriptor (WB_STRIDE aka 0x3) in a 64B cacheline.
2070 */
2071#define WB_STRIDE 0x3
2072 if (((i & WB_STRIDE) != WB_STRIDE) &&
2073 (first <= &tx_ring->tx_bi[i]) &&
2074 (first >= &tx_ring->tx_bi[i & ~WB_STRIDE])) {
2075 tx_desc->cmd_type_offset_bsz =
2076 build_ctob(td_cmd, td_offset, size, td_tag) |
2077 cpu_to_le64((u64)I40E_TX_DESC_CMD_EOP <<
2078 I40E_TXD_QW1_CMD_SHIFT);
2079 } else {
2080 tx_desc->cmd_type_offset_bsz =
2081 build_ctob(td_cmd, td_offset, size, td_tag) |
2082 cpu_to_le64((u64)I40E_TXD_CMD <<
2083 I40E_TXD_QW1_CMD_SHIFT);
2084 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002085
Alexander Duyck7070ce02013-09-28 06:00:37 +00002086 netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
2087 tx_ring->queue_index),
2088 first->bytecount);
2089
Alexander Duycka5e9c572013-09-28 06:00:27 +00002090 /* set the timestamp */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002091 first->time_stamp = jiffies;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002092
2093 /* Force memory writes to complete before letting h/w
2094 * know there are new descriptors to fetch. (Only
2095 * applicable for weak-ordered memory model archs,
2096 * such as IA-64).
2097 */
2098 wmb();
2099
Alexander Duycka5e9c572013-09-28 06:00:27 +00002100 /* set next_to_watch value indicating a packet is present */
2101 first->next_to_watch = tx_desc;
2102
2103 i++;
2104 if (i == tx_ring->count)
2105 i = 0;
2106
2107 tx_ring->next_to_use = i;
2108
2109 /* notify HW of packet */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002110 writel(i, tx_ring->tail);
Alexander Duycka5e9c572013-09-28 06:00:27 +00002111
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002112 return;
2113
2114dma_error:
Alexander Duycka5e9c572013-09-28 06:00:27 +00002115 dev_info(tx_ring->dev, "TX DMA map failed\n");
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002116
2117 /* clear dma mappings for failed tx_bi map */
2118 for (;;) {
2119 tx_bi = &tx_ring->tx_bi[i];
Alexander Duycka5e9c572013-09-28 06:00:27 +00002120 i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002121 if (tx_bi == first)
2122 break;
2123 if (i == 0)
2124 i = tx_ring->count;
2125 i--;
2126 }
2127
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002128 tx_ring->next_to_use = i;
2129}
2130
2131/**
2132 * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
2133 * @tx_ring: the ring to be checked
2134 * @size: the size buffer we want to assure is available
2135 *
2136 * Returns -EBUSY if a stop is needed, else 0
2137 **/
2138static inline int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
2139{
2140 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
Greg Rose8e9dca52013-12-18 13:45:53 +00002141 /* Memory barrier before checking head and tail */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002142 smp_mb();
2143
2144 /* Check again in a case another CPU has just made room available. */
2145 if (likely(I40E_DESC_UNUSED(tx_ring) < size))
2146 return -EBUSY;
2147
2148 /* A reprieve! - use start_queue because it doesn't call schedule */
2149 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
2150 ++tx_ring->tx_stats.restart_queue;
2151 return 0;
2152}
2153
2154/**
2155 * i40e_maybe_stop_tx - 1st level check for tx stop conditions
2156 * @tx_ring: the ring to be checked
2157 * @size: the size buffer we want to assure is available
2158 *
2159 * Returns 0 if stop is not needed
2160 **/
2161static int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
2162{
2163 if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
2164 return 0;
2165 return __i40e_maybe_stop_tx(tx_ring, size);
2166}
2167
2168/**
2169 * i40e_xmit_descriptor_count - calculate number of tx descriptors needed
2170 * @skb: send buffer
2171 * @tx_ring: ring to send buffer on
2172 *
2173 * Returns number of data descriptors needed for this skb. Returns 0 to indicate
2174 * there is not enough descriptors available in this ring since we need at least
2175 * one descriptor.
2176 **/
2177static int i40e_xmit_descriptor_count(struct sk_buff *skb,
2178 struct i40e_ring *tx_ring)
2179{
2180#if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
2181 unsigned int f;
2182#endif
2183 int count = 0;
2184
2185 /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
2186 * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
Jesse Brandeburgbe560522014-02-06 05:51:13 +00002187 * + 4 desc gap to avoid the cache line where head is,
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002188 * + 1 desc for context descriptor,
2189 * otherwise try next time
2190 */
2191#if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
2192 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
2193 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
2194#else
2195 count += skb_shinfo(skb)->nr_frags;
2196#endif
2197 count += TXD_USE_COUNT(skb_headlen(skb));
Jesse Brandeburgbe560522014-02-06 05:51:13 +00002198 if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002199 tx_ring->tx_stats.tx_busy++;
2200 return 0;
2201 }
2202 return count;
2203}
2204
2205/**
2206 * i40e_xmit_frame_ring - Sends buffer on Tx ring
2207 * @skb: send buffer
2208 * @tx_ring: ring to send buffer on
2209 *
2210 * Returns NETDEV_TX_OK if sent, else an error code
2211 **/
2212static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
2213 struct i40e_ring *tx_ring)
2214{
2215 u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
2216 u32 cd_tunneling = 0, cd_l2tag2 = 0;
2217 struct i40e_tx_buffer *first;
2218 u32 td_offset = 0;
2219 u32 tx_flags = 0;
2220 __be16 protocol;
2221 u32 td_cmd = 0;
2222 u8 hdr_len = 0;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002223 int tsyn;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002224 int tso;
2225 if (0 == i40e_xmit_descriptor_count(skb, tx_ring))
2226 return NETDEV_TX_BUSY;
2227
2228 /* prepare the xmit flags */
2229 if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
2230 goto out_drop;
2231
2232 /* obtain protocol of skb */
2233 protocol = skb->protocol;
2234
2235 /* record the location of the first descriptor for this packet */
2236 first = &tx_ring->tx_bi[tx_ring->next_to_use];
2237
2238 /* setup IPv4/IPv6 offloads */
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00002239 if (protocol == htons(ETH_P_IP))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002240 tx_flags |= I40E_TX_FLAGS_IPV4;
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00002241 else if (protocol == htons(ETH_P_IPV6))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002242 tx_flags |= I40E_TX_FLAGS_IPV6;
2243
2244 tso = i40e_tso(tx_ring, skb, tx_flags, protocol, &hdr_len,
2245 &cd_type_cmd_tso_mss, &cd_tunneling);
2246
2247 if (tso < 0)
2248 goto out_drop;
2249 else if (tso)
2250 tx_flags |= I40E_TX_FLAGS_TSO;
2251
2252 skb_tx_timestamp(skb);
2253
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002254 tsyn = i40e_tsyn(tx_ring, skb, tx_flags, &cd_type_cmd_tso_mss);
2255
2256 if (tsyn)
2257 tx_flags |= I40E_TX_FLAGS_TSYN;
2258
Alexander Duyckb1941302013-09-28 06:00:32 +00002259 /* always enable CRC insertion offload */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002260 td_cmd |= I40E_TX_DESC_CMD_ICRC;
2261
Alexander Duyckb1941302013-09-28 06:00:32 +00002262 /* Always offload the checksum, since it's in the data descriptor */
2263 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2264 tx_flags |= I40E_TX_FLAGS_CSUM;
2265
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002266 i40e_tx_enable_csum(skb, tx_flags, &td_cmd, &td_offset,
2267 tx_ring, &cd_tunneling);
Alexander Duyckb1941302013-09-28 06:00:32 +00002268 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002269
2270 i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
2271 cd_tunneling, cd_l2tag2);
2272
2273 /* Add Flow Director ATR if it's enabled.
2274 *
2275 * NOTE: this must always be directly before the data descriptor.
2276 */
2277 i40e_atr(tx_ring, skb, tx_flags, protocol);
2278
2279 i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
2280 td_cmd, td_offset);
2281
2282 i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
2283
2284 return NETDEV_TX_OK;
2285
2286out_drop:
2287 dev_kfree_skb_any(skb);
2288 return NETDEV_TX_OK;
2289}
2290
2291/**
2292 * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
2293 * @skb: send buffer
2294 * @netdev: network interface device structure
2295 *
2296 * Returns NETDEV_TX_OK if sent, else an error code
2297 **/
2298netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2299{
2300 struct i40e_netdev_priv *np = netdev_priv(netdev);
2301 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck9f65e152013-09-28 06:00:58 +00002302 struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002303
2304 /* hardware can't handle really short frames, hardware padding works
2305 * beyond this point
2306 */
2307 if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
2308 if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
2309 return NETDEV_TX_OK;
2310 skb->len = I40E_MIN_TX_LEN;
2311 skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
2312 }
2313
2314 return i40e_xmit_frame_ring(skb, tx_ring);
2315}