blob: 5cc27fba8ad51f66f8e36f4c1b0b8ebac82b4d32 [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;
Mitch Williams505682c2014-05-20 08:01:37 +000065 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000066 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 dcc = I40E_TX_DESC_DTYPE_FILTER_PROG;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000104
105 if (add)
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000106 dcc |= I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
107 I40E_TXD_FLTR_QW1_PCMD_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000108 else
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000109 dcc |= I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
110 I40E_TXD_FLTR_QW1_PCMD_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000111
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000112 dcc |= (fdir_data->dest_ctl << I40E_TXD_FLTR_QW1_DEST_SHIFT) &
113 I40E_TXD_FLTR_QW1_DEST_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000114
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000115 dcc |= (fdir_data->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT) &
116 I40E_TXD_FLTR_QW1_FD_STATUS_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000117
118 if (fdir_data->cnt_index != 0) {
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000119 dcc |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
120 dcc |= ((u32)fdir_data->cnt_index <<
121 I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +0000122 I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000123 }
124
Jesse Brandeburg99753ea2014-06-04 04:22:49 +0000125 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(fpt);
126 fdir_desc->rsvd = cpu_to_le32(0);
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;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000187 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
188 0x45, 0, 0, 0x1c, 0, 0, 0x40, 0, 0x40, 0x11, 0, 0, 0, 0, 0, 0,
189 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
190
191 memcpy(raw_packet, packet, I40E_UDPIP_DUMMY_PACKET_LEN);
192
193 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
194 udp = (struct udphdr *)(raw_packet + IP_HEADER_OFFSET
195 + sizeof(struct iphdr));
196
197 ip->daddr = fd_data->dst_ip[0];
198 udp->dest = fd_data->dst_port;
199 ip->saddr = fd_data->src_ip[0];
200 udp->source = fd_data->src_port;
201
Kevin Scottb2d36c02014-04-09 05:58:59 +0000202 fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
203 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
204 if (ret) {
205 dev_info(&pf->pdev->dev,
206 "Filter command send failed for PCTYPE %d (ret = %d)\n",
207 fd_data->pctype, ret);
208 err = true;
209 } else {
210 dev_info(&pf->pdev->dev,
211 "Filter OK for PCTYPE %d (ret = %d)\n",
212 fd_data->pctype, ret);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000213 }
214
215 return err ? -EOPNOTSUPP : 0;
216}
217
218#define I40E_TCPIP_DUMMY_PACKET_LEN 54
219/**
220 * i40e_add_del_fdir_tcpv4 - Add/Remove TCPv4 filters
221 * @vsi: pointer to the targeted VSI
222 * @fd_data: the flow director data required for the FDir descriptor
223 * @raw_packet: the pre-allocated packet buffer for FDir
224 * @add: true adds a filter, false removes it
225 *
226 * Returns 0 if the filters were successfully added or removed
227 **/
228static int i40e_add_del_fdir_tcpv4(struct i40e_vsi *vsi,
229 struct i40e_fdir_filter *fd_data,
230 u8 *raw_packet, bool add)
231{
232 struct i40e_pf *pf = vsi->back;
233 struct tcphdr *tcp;
234 struct iphdr *ip;
235 bool err = false;
236 int ret;
237 /* Dummy packet */
238 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
239 0x45, 0, 0, 0x28, 0, 0, 0x40, 0, 0x40, 0x6, 0, 0, 0, 0, 0, 0,
240 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0x11,
241 0x0, 0x72, 0, 0, 0, 0};
242
243 memcpy(raw_packet, packet, I40E_TCPIP_DUMMY_PACKET_LEN);
244
245 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
246 tcp = (struct tcphdr *)(raw_packet + IP_HEADER_OFFSET
247 + sizeof(struct iphdr));
248
249 ip->daddr = fd_data->dst_ip[0];
250 tcp->dest = fd_data->dst_port;
251 ip->saddr = fd_data->src_ip[0];
252 tcp->source = fd_data->src_port;
253
254 if (add) {
255 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED) {
256 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 flow being applied\n");
257 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
258 }
259 }
260
Kevin Scottb2d36c02014-04-09 05:58:59 +0000261 fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000262 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
263
264 if (ret) {
265 dev_info(&pf->pdev->dev,
266 "Filter command send failed for PCTYPE %d (ret = %d)\n",
267 fd_data->pctype, ret);
268 err = true;
269 } else {
270 dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d (ret = %d)\n",
271 fd_data->pctype, ret);
272 }
273
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000274 return err ? -EOPNOTSUPP : 0;
275}
276
277/**
278 * i40e_add_del_fdir_sctpv4 - Add/Remove SCTPv4 Flow Director filters for
279 * a specific flow spec
280 * @vsi: pointer to the targeted VSI
281 * @fd_data: the flow director data required for the FDir descriptor
282 * @raw_packet: the pre-allocated packet buffer for FDir
283 * @add: true adds a filter, false removes it
284 *
Jean Sacren21d3efd2014-03-17 18:14:39 +0000285 * Always returns -EOPNOTSUPP
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000286 **/
287static int i40e_add_del_fdir_sctpv4(struct i40e_vsi *vsi,
288 struct i40e_fdir_filter *fd_data,
289 u8 *raw_packet, bool add)
290{
291 return -EOPNOTSUPP;
292}
293
294#define I40E_IP_DUMMY_PACKET_LEN 34
295/**
296 * i40e_add_del_fdir_ipv4 - Add/Remove IPv4 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 *
303 * Returns 0 if the filters were successfully added or removed
304 **/
305static int i40e_add_del_fdir_ipv4(struct i40e_vsi *vsi,
306 struct i40e_fdir_filter *fd_data,
307 u8 *raw_packet, bool add)
308{
309 struct i40e_pf *pf = vsi->back;
310 struct iphdr *ip;
311 bool err = false;
312 int ret;
313 int i;
314 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
315 0x45, 0, 0, 0x14, 0, 0, 0x40, 0, 0x40, 0x10, 0, 0, 0, 0, 0, 0,
316 0, 0, 0, 0};
317
318 memcpy(raw_packet, packet, I40E_IP_DUMMY_PACKET_LEN);
319 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
320
321 ip->saddr = fd_data->src_ip[0];
322 ip->daddr = fd_data->dst_ip[0];
323 ip->protocol = 0;
324
325 for (i = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
326 i <= I40E_FILTER_PCTYPE_FRAG_IPV4; i++) {
327 fd_data->pctype = i;
328 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
329
330 if (ret) {
331 dev_info(&pf->pdev->dev,
332 "Filter command send failed for PCTYPE %d (ret = %d)\n",
333 fd_data->pctype, ret);
334 err = true;
335 } else {
336 dev_info(&pf->pdev->dev,
337 "Filter OK for PCTYPE %d (ret = %d)\n",
338 fd_data->pctype, ret);
339 }
340 }
341
342 return err ? -EOPNOTSUPP : 0;
343}
344
345/**
346 * i40e_add_del_fdir - Build raw packets to add/del fdir filter
347 * @vsi: pointer to the targeted VSI
348 * @cmd: command to get or set RX flow classification rules
349 * @add: true adds a filter, false removes it
350 *
351 **/
352int i40e_add_del_fdir(struct i40e_vsi *vsi,
353 struct i40e_fdir_filter *input, bool add)
354{
355 struct i40e_pf *pf = vsi->back;
356 u8 *raw_packet;
357 int ret;
358
359 /* Populate the Flow Director that we have at the moment
360 * and allocate the raw packet buffer for the calling functions
361 */
362 raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
363 if (!raw_packet)
364 return -ENOMEM;
365
366 switch (input->flow_type & ~FLOW_EXT) {
367 case TCP_V4_FLOW:
368 ret = i40e_add_del_fdir_tcpv4(vsi, input, raw_packet,
369 add);
370 break;
371 case UDP_V4_FLOW:
372 ret = i40e_add_del_fdir_udpv4(vsi, input, raw_packet,
373 add);
374 break;
375 case SCTP_V4_FLOW:
376 ret = i40e_add_del_fdir_sctpv4(vsi, input, raw_packet,
377 add);
378 break;
379 case IPV4_FLOW:
380 ret = i40e_add_del_fdir_ipv4(vsi, input, raw_packet,
381 add);
382 break;
383 case IP_USER_FLOW:
384 switch (input->ip4_proto) {
385 case IPPROTO_TCP:
386 ret = i40e_add_del_fdir_tcpv4(vsi, input,
387 raw_packet, add);
388 break;
389 case IPPROTO_UDP:
390 ret = i40e_add_del_fdir_udpv4(vsi, input,
391 raw_packet, add);
392 break;
393 case IPPROTO_SCTP:
394 ret = i40e_add_del_fdir_sctpv4(vsi, input,
395 raw_packet, add);
396 break;
397 default:
398 ret = i40e_add_del_fdir_ipv4(vsi, input,
399 raw_packet, add);
400 break;
401 }
402 break;
403 default:
Jakub Kicinskic5ffe7e2014-04-02 10:33:22 +0000404 dev_info(&pf->pdev->dev, "Could not specify spec type %d\n",
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000405 input->flow_type);
406 ret = -EINVAL;
407 }
408
409 kfree(raw_packet);
410 return ret;
411}
412
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000413/**
414 * i40e_fd_handle_status - check the Programming Status for FD
415 * @rx_ring: the Rx ring for this descriptor
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000416 * @rx_desc: the Rx descriptor for programming Status, not a packet descriptor.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000417 * @prog_id: the id originally used for programming
418 *
419 * This is used to verify if the FD programming or invalidation
420 * requested by SW to the HW is successful or not and take actions accordingly.
421 **/
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000422static void i40e_fd_handle_status(struct i40e_ring *rx_ring,
423 union i40e_rx_desc *rx_desc, u8 prog_id)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000424{
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000425 struct i40e_pf *pf = rx_ring->vsi->back;
426 struct pci_dev *pdev = pf->pdev;
427 u32 fcnt_prog, fcnt_avail;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000428 u32 error;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000429 u64 qw;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000430
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000431 qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000432 error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
433 I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
434
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000435 if (error == (0x1 << I40E_RX_PROG_STATUS_DESC_FD_TBL_FULL_SHIFT)) {
436 dev_warn(&pdev->dev, "ntuple filter loc = %d, could not be added\n",
437 rx_desc->wb.qword0.hi_dword.fd_id);
438
439 /* filter programming failed most likely due to table full */
Anjali Singhai Jain12957382014-06-04 04:22:47 +0000440 fcnt_prog = i40e_get_cur_guaranteed_fd_count(pf);
441 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000442 /* If ATR is running fcnt_prog can quickly change,
443 * if we are very close to full, it makes sense to disable
444 * FD ATR/SB and then re-enable it when there is room.
445 */
446 if (fcnt_prog >= (fcnt_avail - I40E_FDIR_BUFFER_FULL_MARGIN)) {
447 /* Turn off ATR first */
Jesse Brandeburgd3a90b72014-04-16 01:21:40 +0000448 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000449 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
450 dev_warn(&pdev->dev, "FD filter space full, ATR for further flows will be turned off\n");
451 pf->auto_disable_flags |=
452 I40E_FLAG_FD_ATR_ENABLED;
453 pf->flags |= I40E_FLAG_FDIR_REQUIRES_REINIT;
Jesse Brandeburgd3a90b72014-04-16 01:21:40 +0000454 } else if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000455 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
456 dev_warn(&pdev->dev, "FD filter space full, new ntuple rules will not be added\n");
457 pf->auto_disable_flags |=
458 I40E_FLAG_FD_SB_ENABLED;
459 pf->flags |= I40E_FLAG_FDIR_REQUIRES_REINIT;
460 }
461 } else {
Jakub Kicinskic5ffe7e2014-04-02 10:33:22 +0000462 dev_info(&pdev->dev, "FD filter programming error\n");
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000463 }
464 } else if (error ==
465 (0x1 << I40E_RX_PROG_STATUS_DESC_NO_FD_ENTRY_SHIFT)) {
Anjali Singhai Jain13c28842014-03-06 09:00:04 +0000466 if (I40E_DEBUG_FD & pf->hw.debug_mask)
467 dev_info(&pdev->dev, "ntuple filter loc = %d, could not be removed\n",
468 rx_desc->wb.qword0.hi_dword.fd_id);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000469 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000470}
471
472/**
Alexander Duycka5e9c572013-09-28 06:00:27 +0000473 * i40e_unmap_and_free_tx_resource - Release a Tx buffer
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000474 * @ring: the ring that owns the buffer
475 * @tx_buffer: the buffer to free
476 **/
Alexander Duycka5e9c572013-09-28 06:00:27 +0000477static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
478 struct i40e_tx_buffer *tx_buffer)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000479{
Alexander Duycka5e9c572013-09-28 06:00:27 +0000480 if (tx_buffer->skb) {
481 dev_kfree_skb_any(tx_buffer->skb);
482 if (dma_unmap_len(tx_buffer, len))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000483 dma_unmap_single(ring->dev,
Alexander Duyck35a1e2a2013-09-28 06:00:17 +0000484 dma_unmap_addr(tx_buffer, dma),
485 dma_unmap_len(tx_buffer, len),
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000486 DMA_TO_DEVICE);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000487 } else if (dma_unmap_len(tx_buffer, len)) {
488 dma_unmap_page(ring->dev,
489 dma_unmap_addr(tx_buffer, dma),
490 dma_unmap_len(tx_buffer, len),
491 DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000492 }
Alexander Duycka5e9c572013-09-28 06:00:27 +0000493 tx_buffer->next_to_watch = NULL;
494 tx_buffer->skb = NULL;
Alexander Duyck35a1e2a2013-09-28 06:00:17 +0000495 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000496 /* tx_buffer must be completely set up in the transmit path */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000497}
498
499/**
500 * i40e_clean_tx_ring - Free any empty Tx buffers
501 * @tx_ring: ring to be cleaned
502 **/
503void i40e_clean_tx_ring(struct i40e_ring *tx_ring)
504{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000505 unsigned long bi_size;
506 u16 i;
507
508 /* ring already cleared, nothing to do */
509 if (!tx_ring->tx_bi)
510 return;
511
512 /* Free all the Tx ring sk_buffs */
Alexander Duycka5e9c572013-09-28 06:00:27 +0000513 for (i = 0; i < tx_ring->count; i++)
514 i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000515
516 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
517 memset(tx_ring->tx_bi, 0, bi_size);
518
519 /* Zero out the descriptor ring */
520 memset(tx_ring->desc, 0, tx_ring->size);
521
522 tx_ring->next_to_use = 0;
523 tx_ring->next_to_clean = 0;
Alexander Duyck7070ce02013-09-28 06:00:37 +0000524
525 if (!tx_ring->netdev)
526 return;
527
528 /* cleanup Tx queue statistics */
529 netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
530 tx_ring->queue_index));
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000531}
532
533/**
534 * i40e_free_tx_resources - Free Tx resources per queue
535 * @tx_ring: Tx descriptor ring for a specific queue
536 *
537 * Free all transmit software resources
538 **/
539void i40e_free_tx_resources(struct i40e_ring *tx_ring)
540{
541 i40e_clean_tx_ring(tx_ring);
542 kfree(tx_ring->tx_bi);
543 tx_ring->tx_bi = NULL;
544
545 if (tx_ring->desc) {
546 dma_free_coherent(tx_ring->dev, tx_ring->size,
547 tx_ring->desc, tx_ring->dma);
548 tx_ring->desc = NULL;
549 }
550}
551
552/**
553 * i40e_get_tx_pending - how many tx descriptors not processed
554 * @tx_ring: the ring of descriptors
555 *
556 * Since there is no access to the ring head register
557 * in XL710, we need to use our local copies
558 **/
559static u32 i40e_get_tx_pending(struct i40e_ring *ring)
560{
561 u32 ntu = ((ring->next_to_clean <= ring->next_to_use)
562 ? ring->next_to_use
563 : ring->next_to_use + ring->count);
564 return ntu - ring->next_to_clean;
565}
566
567/**
568 * i40e_check_tx_hang - Is there a hang in the Tx queue
569 * @tx_ring: the ring of descriptors
570 **/
571static bool i40e_check_tx_hang(struct i40e_ring *tx_ring)
572{
573 u32 tx_pending = i40e_get_tx_pending(tx_ring);
574 bool ret = false;
575
576 clear_check_for_tx_hang(tx_ring);
577
578 /* Check for a hung queue, but be thorough. This verifies
579 * that a transmit has been completed since the previous
580 * check AND there is at least one packet pending. The
581 * ARMED bit is set to indicate a potential hang. The
582 * bit is cleared if a pause frame is received to remove
583 * false hang detection due to PFC or 802.3x frames. By
584 * requiring this to fail twice we avoid races with
585 * PFC clearing the ARMED bit and conditions where we
586 * run the check_tx_hang logic with a transmit completion
587 * pending but without time to complete it yet.
588 */
Alexander Duycka114d0a2013-09-28 06:00:43 +0000589 if ((tx_ring->tx_stats.tx_done_old == tx_ring->stats.packets) &&
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000590 tx_pending) {
591 /* make sure it is true for two checks in a row */
592 ret = test_and_set_bit(__I40E_HANG_CHECK_ARMED,
593 &tx_ring->state);
594 } else {
595 /* update completed stats and disarm the hang check */
Alexander Duycka114d0a2013-09-28 06:00:43 +0000596 tx_ring->tx_stats.tx_done_old = tx_ring->stats.packets;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000597 clear_bit(__I40E_HANG_CHECK_ARMED, &tx_ring->state);
598 }
599
600 return ret;
601}
602
603/**
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000604 * i40e_get_head - Retrieve head from head writeback
605 * @tx_ring: tx ring to fetch head of
606 *
607 * Returns value of Tx ring head based on value stored
608 * in head write-back location
609 **/
610static inline u32 i40e_get_head(struct i40e_ring *tx_ring)
611{
612 void *head = (struct i40e_tx_desc *)tx_ring->desc + tx_ring->count;
613
614 return le32_to_cpu(*(volatile __le32 *)head);
615}
616
617/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000618 * i40e_clean_tx_irq - Reclaim resources after transmit completes
619 * @tx_ring: tx ring to clean
620 * @budget: how many cleans we're allowed
621 *
622 * Returns true if there's any budget left (e.g. the clean is finished)
623 **/
624static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
625{
626 u16 i = tx_ring->next_to_clean;
627 struct i40e_tx_buffer *tx_buf;
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000628 struct i40e_tx_desc *tx_head;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000629 struct i40e_tx_desc *tx_desc;
630 unsigned int total_packets = 0;
631 unsigned int total_bytes = 0;
632
633 tx_buf = &tx_ring->tx_bi[i];
634 tx_desc = I40E_TX_DESC(tx_ring, i);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000635 i -= tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000636
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000637 tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
638
Alexander Duycka5e9c572013-09-28 06:00:27 +0000639 do {
640 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000641
642 /* if next_to_watch is not set then there is no work pending */
643 if (!eop_desc)
644 break;
645
Alexander Duycka5e9c572013-09-28 06:00:27 +0000646 /* prevent any other reads prior to eop_desc */
647 read_barrier_depends();
648
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000649 /* we have caught up to head, no work left to do */
650 if (tx_head == tx_desc)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000651 break;
652
Alexander Duyckc304fda2013-09-28 06:00:12 +0000653 /* clear next_to_watch to prevent false hangs */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000654 tx_buf->next_to_watch = NULL;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000655
Alexander Duycka5e9c572013-09-28 06:00:27 +0000656 /* update the statistics for this packet */
657 total_bytes += tx_buf->bytecount;
658 total_packets += tx_buf->gso_segs;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000659
Alexander Duycka5e9c572013-09-28 06:00:27 +0000660 /* free the skb */
661 dev_kfree_skb_any(tx_buf->skb);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000662
Alexander Duycka5e9c572013-09-28 06:00:27 +0000663 /* unmap skb header data */
664 dma_unmap_single(tx_ring->dev,
665 dma_unmap_addr(tx_buf, dma),
666 dma_unmap_len(tx_buf, len),
667 DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000668
Alexander Duycka5e9c572013-09-28 06:00:27 +0000669 /* clear tx_buffer data */
670 tx_buf->skb = NULL;
671 dma_unmap_len_set(tx_buf, len, 0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000672
Alexander Duycka5e9c572013-09-28 06:00:27 +0000673 /* unmap remaining buffers */
674 while (tx_desc != eop_desc) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000675
676 tx_buf++;
677 tx_desc++;
678 i++;
Alexander Duycka5e9c572013-09-28 06:00:27 +0000679 if (unlikely(!i)) {
680 i -= tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000681 tx_buf = tx_ring->tx_bi;
682 tx_desc = I40E_TX_DESC(tx_ring, 0);
683 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000684
Alexander Duycka5e9c572013-09-28 06:00:27 +0000685 /* unmap any remaining paged data */
686 if (dma_unmap_len(tx_buf, len)) {
687 dma_unmap_page(tx_ring->dev,
688 dma_unmap_addr(tx_buf, dma),
689 dma_unmap_len(tx_buf, len),
690 DMA_TO_DEVICE);
691 dma_unmap_len_set(tx_buf, len, 0);
692 }
693 }
694
695 /* move us one more past the eop_desc for start of next pkt */
696 tx_buf++;
697 tx_desc++;
698 i++;
699 if (unlikely(!i)) {
700 i -= tx_ring->count;
701 tx_buf = tx_ring->tx_bi;
702 tx_desc = I40E_TX_DESC(tx_ring, 0);
703 }
704
705 /* update budget accounting */
706 budget--;
707 } while (likely(budget));
708
709 i += tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000710 tx_ring->next_to_clean = i;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000711 u64_stats_update_begin(&tx_ring->syncp);
Alexander Duycka114d0a2013-09-28 06:00:43 +0000712 tx_ring->stats.bytes += total_bytes;
713 tx_ring->stats.packets += total_packets;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000714 u64_stats_update_end(&tx_ring->syncp);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000715 tx_ring->q_vector->tx.total_bytes += total_bytes;
716 tx_ring->q_vector->tx.total_packets += total_packets;
Alexander Duycka5e9c572013-09-28 06:00:27 +0000717
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000718 if (check_for_tx_hang(tx_ring) && i40e_check_tx_hang(tx_ring)) {
719 /* schedule immediate reset if we believe we hung */
720 dev_info(tx_ring->dev, "Detected Tx Unit Hang\n"
721 " VSI <%d>\n"
722 " Tx Queue <%d>\n"
723 " next_to_use <%x>\n"
724 " next_to_clean <%x>\n",
725 tx_ring->vsi->seid,
726 tx_ring->queue_index,
727 tx_ring->next_to_use, i);
728 dev_info(tx_ring->dev, "tx_bi[next_to_clean]\n"
729 " time_stamp <%lx>\n"
730 " jiffies <%lx>\n",
731 tx_ring->tx_bi[i].time_stamp, jiffies);
732
733 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
734
735 dev_info(tx_ring->dev,
736 "tx hang detected on queue %d, resetting adapter\n",
737 tx_ring->queue_index);
738
739 tx_ring->netdev->netdev_ops->ndo_tx_timeout(tx_ring->netdev);
740
741 /* the adapter is about to reset, no point in enabling stuff */
742 return true;
743 }
744
Alexander Duyck7070ce02013-09-28 06:00:37 +0000745 netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
746 tx_ring->queue_index),
747 total_packets, total_bytes);
748
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000749#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
750 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
751 (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
752 /* Make sure that anybody stopping the queue after this
753 * sees the new next_to_clean.
754 */
755 smp_mb();
756 if (__netif_subqueue_stopped(tx_ring->netdev,
757 tx_ring->queue_index) &&
758 !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
759 netif_wake_subqueue(tx_ring->netdev,
760 tx_ring->queue_index);
761 ++tx_ring->tx_stats.restart_queue;
762 }
763 }
764
765 return budget > 0;
766}
767
768/**
769 * i40e_set_new_dynamic_itr - Find new ITR level
770 * @rc: structure containing ring performance data
771 *
772 * Stores a new ITR value based on packets and byte counts during
773 * the last interrupt. The advantage of per interrupt computation
774 * is faster updates and more accurate ITR for the current traffic
775 * pattern. Constants in this function were computed based on
776 * theoretical maximum wire speed and thresholds were set based on
777 * testing data as well as attempting to minimize response time
778 * while increasing bulk throughput.
779 **/
780static void i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
781{
782 enum i40e_latency_range new_latency_range = rc->latency_range;
783 u32 new_itr = rc->itr;
784 int bytes_per_int;
785
786 if (rc->total_packets == 0 || !rc->itr)
787 return;
788
789 /* simple throttlerate management
790 * 0-10MB/s lowest (100000 ints/s)
791 * 10-20MB/s low (20000 ints/s)
792 * 20-1249MB/s bulk (8000 ints/s)
793 */
794 bytes_per_int = rc->total_bytes / rc->itr;
795 switch (rc->itr) {
796 case I40E_LOWEST_LATENCY:
797 if (bytes_per_int > 10)
798 new_latency_range = I40E_LOW_LATENCY;
799 break;
800 case I40E_LOW_LATENCY:
801 if (bytes_per_int > 20)
802 new_latency_range = I40E_BULK_LATENCY;
803 else if (bytes_per_int <= 10)
804 new_latency_range = I40E_LOWEST_LATENCY;
805 break;
806 case I40E_BULK_LATENCY:
807 if (bytes_per_int <= 20)
808 rc->latency_range = I40E_LOW_LATENCY;
809 break;
810 }
811
812 switch (new_latency_range) {
813 case I40E_LOWEST_LATENCY:
814 new_itr = I40E_ITR_100K;
815 break;
816 case I40E_LOW_LATENCY:
817 new_itr = I40E_ITR_20K;
818 break;
819 case I40E_BULK_LATENCY:
820 new_itr = I40E_ITR_8K;
821 break;
822 default:
823 break;
824 }
825
826 if (new_itr != rc->itr) {
827 /* do an exponential smoothing */
828 new_itr = (10 * new_itr * rc->itr) /
829 ((9 * new_itr) + rc->itr);
830 rc->itr = new_itr & I40E_MAX_ITR;
831 }
832
833 rc->total_bytes = 0;
834 rc->total_packets = 0;
835}
836
837/**
838 * i40e_update_dynamic_itr - Adjust ITR based on bytes per int
839 * @q_vector: the vector to adjust
840 **/
841static void i40e_update_dynamic_itr(struct i40e_q_vector *q_vector)
842{
843 u16 vector = q_vector->vsi->base_vector + q_vector->v_idx;
844 struct i40e_hw *hw = &q_vector->vsi->back->hw;
845 u32 reg_addr;
846 u16 old_itr;
847
848 reg_addr = I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1);
849 old_itr = q_vector->rx.itr;
850 i40e_set_new_dynamic_itr(&q_vector->rx);
851 if (old_itr != q_vector->rx.itr)
852 wr32(hw, reg_addr, q_vector->rx.itr);
853
854 reg_addr = I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1);
855 old_itr = q_vector->tx.itr;
856 i40e_set_new_dynamic_itr(&q_vector->tx);
857 if (old_itr != q_vector->tx.itr)
858 wr32(hw, reg_addr, q_vector->tx.itr);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000859}
860
861/**
862 * i40e_clean_programming_status - clean the programming status descriptor
863 * @rx_ring: the rx ring that has this descriptor
864 * @rx_desc: the rx descriptor written back by HW
865 *
866 * Flow director should handle FD_FILTER_STATUS to check its filter programming
867 * status being successful or not and take actions accordingly. FCoE should
868 * handle its context/filter programming/invalidation status and take actions.
869 *
870 **/
871static void i40e_clean_programming_status(struct i40e_ring *rx_ring,
872 union i40e_rx_desc *rx_desc)
873{
874 u64 qw;
875 u8 id;
876
877 qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
878 id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
879 I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
880
881 if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000882 i40e_fd_handle_status(rx_ring, rx_desc, id);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000883}
884
885/**
886 * i40e_setup_tx_descriptors - Allocate the Tx descriptors
887 * @tx_ring: the tx ring to set up
888 *
889 * Return 0 on success, negative on error
890 **/
891int i40e_setup_tx_descriptors(struct i40e_ring *tx_ring)
892{
893 struct device *dev = tx_ring->dev;
894 int bi_size;
895
896 if (!dev)
897 return -ENOMEM;
898
899 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
900 tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
901 if (!tx_ring->tx_bi)
902 goto err;
903
904 /* round up to nearest 4K */
905 tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000906 /* add u32 for head writeback, align after this takes care of
907 * guaranteeing this is at least one cache line in size
908 */
909 tx_ring->size += sizeof(u32);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000910 tx_ring->size = ALIGN(tx_ring->size, 4096);
911 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
912 &tx_ring->dma, GFP_KERNEL);
913 if (!tx_ring->desc) {
914 dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
915 tx_ring->size);
916 goto err;
917 }
918
919 tx_ring->next_to_use = 0;
920 tx_ring->next_to_clean = 0;
921 return 0;
922
923err:
924 kfree(tx_ring->tx_bi);
925 tx_ring->tx_bi = NULL;
926 return -ENOMEM;
927}
928
929/**
930 * i40e_clean_rx_ring - Free Rx buffers
931 * @rx_ring: ring to be cleaned
932 **/
933void i40e_clean_rx_ring(struct i40e_ring *rx_ring)
934{
935 struct device *dev = rx_ring->dev;
936 struct i40e_rx_buffer *rx_bi;
937 unsigned long bi_size;
938 u16 i;
939
940 /* ring already cleared, nothing to do */
941 if (!rx_ring->rx_bi)
942 return;
943
944 /* Free all the Rx ring sk_buffs */
945 for (i = 0; i < rx_ring->count; i++) {
946 rx_bi = &rx_ring->rx_bi[i];
947 if (rx_bi->dma) {
948 dma_unmap_single(dev,
949 rx_bi->dma,
950 rx_ring->rx_buf_len,
951 DMA_FROM_DEVICE);
952 rx_bi->dma = 0;
953 }
954 if (rx_bi->skb) {
955 dev_kfree_skb(rx_bi->skb);
956 rx_bi->skb = NULL;
957 }
958 if (rx_bi->page) {
959 if (rx_bi->page_dma) {
960 dma_unmap_page(dev,
961 rx_bi->page_dma,
962 PAGE_SIZE / 2,
963 DMA_FROM_DEVICE);
964 rx_bi->page_dma = 0;
965 }
966 __free_page(rx_bi->page);
967 rx_bi->page = NULL;
968 rx_bi->page_offset = 0;
969 }
970 }
971
972 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
973 memset(rx_ring->rx_bi, 0, bi_size);
974
975 /* Zero out the descriptor ring */
976 memset(rx_ring->desc, 0, rx_ring->size);
977
978 rx_ring->next_to_clean = 0;
979 rx_ring->next_to_use = 0;
980}
981
982/**
983 * i40e_free_rx_resources - Free Rx resources
984 * @rx_ring: ring to clean the resources from
985 *
986 * Free all receive software resources
987 **/
988void i40e_free_rx_resources(struct i40e_ring *rx_ring)
989{
990 i40e_clean_rx_ring(rx_ring);
991 kfree(rx_ring->rx_bi);
992 rx_ring->rx_bi = NULL;
993
994 if (rx_ring->desc) {
995 dma_free_coherent(rx_ring->dev, rx_ring->size,
996 rx_ring->desc, rx_ring->dma);
997 rx_ring->desc = NULL;
998 }
999}
1000
1001/**
1002 * i40e_setup_rx_descriptors - Allocate Rx descriptors
1003 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
1004 *
1005 * Returns 0 on success, negative on failure
1006 **/
1007int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring)
1008{
1009 struct device *dev = rx_ring->dev;
1010 int bi_size;
1011
1012 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
1013 rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
1014 if (!rx_ring->rx_bi)
1015 goto err;
1016
1017 /* Round up to nearest 4K */
1018 rx_ring->size = ring_is_16byte_desc_enabled(rx_ring)
1019 ? rx_ring->count * sizeof(union i40e_16byte_rx_desc)
1020 : rx_ring->count * sizeof(union i40e_32byte_rx_desc);
1021 rx_ring->size = ALIGN(rx_ring->size, 4096);
1022 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
1023 &rx_ring->dma, GFP_KERNEL);
1024
1025 if (!rx_ring->desc) {
1026 dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
1027 rx_ring->size);
1028 goto err;
1029 }
1030
1031 rx_ring->next_to_clean = 0;
1032 rx_ring->next_to_use = 0;
1033
1034 return 0;
1035err:
1036 kfree(rx_ring->rx_bi);
1037 rx_ring->rx_bi = NULL;
1038 return -ENOMEM;
1039}
1040
1041/**
1042 * i40e_release_rx_desc - Store the new tail and head values
1043 * @rx_ring: ring to bump
1044 * @val: new head index
1045 **/
1046static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
1047{
1048 rx_ring->next_to_use = val;
1049 /* Force memory writes to complete before letting h/w
1050 * know there are new descriptors to fetch. (Only
1051 * applicable for weak-ordered memory model archs,
1052 * such as IA-64).
1053 */
1054 wmb();
1055 writel(val, rx_ring->tail);
1056}
1057
1058/**
1059 * i40e_alloc_rx_buffers - Replace used receive buffers; packet split
1060 * @rx_ring: ring to place buffers on
1061 * @cleaned_count: number of buffers to replace
1062 **/
1063void i40e_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
1064{
1065 u16 i = rx_ring->next_to_use;
1066 union i40e_rx_desc *rx_desc;
1067 struct i40e_rx_buffer *bi;
1068 struct sk_buff *skb;
1069
1070 /* do nothing if no valid netdev defined */
1071 if (!rx_ring->netdev || !cleaned_count)
1072 return;
1073
1074 while (cleaned_count--) {
1075 rx_desc = I40E_RX_DESC(rx_ring, i);
1076 bi = &rx_ring->rx_bi[i];
1077 skb = bi->skb;
1078
1079 if (!skb) {
1080 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
1081 rx_ring->rx_buf_len);
1082 if (!skb) {
Mitch Williams420136c2013-12-18 13:45:59 +00001083 rx_ring->rx_stats.alloc_buff_failed++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001084 goto no_buffers;
1085 }
1086 /* initialize queue mapping */
1087 skb_record_rx_queue(skb, rx_ring->queue_index);
1088 bi->skb = skb;
1089 }
1090
1091 if (!bi->dma) {
1092 bi->dma = dma_map_single(rx_ring->dev,
1093 skb->data,
1094 rx_ring->rx_buf_len,
1095 DMA_FROM_DEVICE);
1096 if (dma_mapping_error(rx_ring->dev, bi->dma)) {
Mitch Williams420136c2013-12-18 13:45:59 +00001097 rx_ring->rx_stats.alloc_buff_failed++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001098 bi->dma = 0;
1099 goto no_buffers;
1100 }
1101 }
1102
1103 if (ring_is_ps_enabled(rx_ring)) {
1104 if (!bi->page) {
1105 bi->page = alloc_page(GFP_ATOMIC);
1106 if (!bi->page) {
Mitch Williams420136c2013-12-18 13:45:59 +00001107 rx_ring->rx_stats.alloc_page_failed++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001108 goto no_buffers;
1109 }
1110 }
1111
1112 if (!bi->page_dma) {
1113 /* use a half page if we're re-using */
1114 bi->page_offset ^= PAGE_SIZE / 2;
1115 bi->page_dma = dma_map_page(rx_ring->dev,
1116 bi->page,
1117 bi->page_offset,
1118 PAGE_SIZE / 2,
1119 DMA_FROM_DEVICE);
1120 if (dma_mapping_error(rx_ring->dev,
1121 bi->page_dma)) {
Mitch Williams420136c2013-12-18 13:45:59 +00001122 rx_ring->rx_stats.alloc_page_failed++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001123 bi->page_dma = 0;
1124 goto no_buffers;
1125 }
1126 }
1127
1128 /* Refresh the desc even if buffer_addrs didn't change
1129 * because each write-back erases this info.
1130 */
1131 rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
1132 rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
1133 } else {
1134 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
1135 rx_desc->read.hdr_addr = 0;
1136 }
1137 i++;
1138 if (i == rx_ring->count)
1139 i = 0;
1140 }
1141
1142no_buffers:
1143 if (rx_ring->next_to_use != i)
1144 i40e_release_rx_desc(rx_ring, i);
1145}
1146
1147/**
1148 * i40e_receive_skb - Send a completed packet up the stack
1149 * @rx_ring: rx ring in play
1150 * @skb: packet to send up
1151 * @vlan_tag: vlan tag for packet
1152 **/
1153static void i40e_receive_skb(struct i40e_ring *rx_ring,
1154 struct sk_buff *skb, u16 vlan_tag)
1155{
1156 struct i40e_q_vector *q_vector = rx_ring->q_vector;
1157 struct i40e_vsi *vsi = rx_ring->vsi;
1158 u64 flags = vsi->back->flags;
1159
1160 if (vlan_tag & VLAN_VID_MASK)
1161 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
1162
1163 if (flags & I40E_FLAG_IN_NETPOLL)
1164 netif_rx(skb);
1165 else
1166 napi_gro_receive(&q_vector->napi, skb);
1167}
1168
1169/**
1170 * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
1171 * @vsi: the VSI we care about
1172 * @skb: skb currently being received and modified
1173 * @rx_status: status value of last descriptor in packet
1174 * @rx_error: error value of last descriptor in packet
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001175 * @rx_ptype: ptype value of last descriptor in packet
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001176 **/
1177static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
1178 struct sk_buff *skb,
1179 u32 rx_status,
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001180 u32 rx_error,
1181 u16 rx_ptype)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001182{
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001183 struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(rx_ptype);
1184 bool ipv4 = false, ipv6 = false;
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001185 bool ipv4_tunnel, ipv6_tunnel;
1186 __wsum rx_udp_csum;
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001187 struct iphdr *iph;
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001188 __sum16 csum;
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001189
1190 ipv4_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT4_MAC_PAY3) &&
1191 (rx_ptype < I40E_RX_PTYPE_GRENAT4_MACVLAN_IPV6_ICMP_PAY4);
1192 ipv6_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT6_MAC_PAY3) &&
1193 (rx_ptype < I40E_RX_PTYPE_GRENAT6_MACVLAN_IPV6_ICMP_PAY4);
1194
1195 skb->encapsulation = ipv4_tunnel || ipv6_tunnel;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001196 skb->ip_summed = CHECKSUM_NONE;
1197
1198 /* Rx csum enabled and ip headers found? */
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001199 if (!(vsi->netdev->features & NETIF_F_RXCSUM))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001200 return;
1201
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001202 /* did the hardware decode the packet and checksum? */
1203 if (!(rx_status & (1 << I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
1204 return;
1205
1206 /* both known and outer_ip must be set for the below code to work */
1207 if (!(decoded.known && decoded.outer_ip))
1208 return;
1209
1210 if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1211 decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4)
1212 ipv4 = true;
1213 else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1214 decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6)
1215 ipv6 = true;
1216
1217 if (ipv4 &&
1218 (rx_error & ((1 << I40E_RX_DESC_ERROR_IPE_SHIFT) |
1219 (1 << I40E_RX_DESC_ERROR_EIPE_SHIFT))))
1220 goto checksum_fail;
1221
Jesse Brandeburgddf1d0d2014-02-13 03:48:39 -08001222 /* likely incorrect csum if alternate IP extension headers found */
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001223 if (ipv6 &&
1224 decoded.inner_prot == I40E_RX_PTYPE_INNER_PROT_TCP &&
1225 rx_error & (1 << I40E_RX_DESC_ERROR_L4E_SHIFT) &&
1226 rx_status & (1 << I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
1227 /* don't increment checksum err here, non-fatal err */
Shannon Nelson8ee75a82013-12-21 05:44:46 +00001228 return;
1229
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001230 /* there was some L4 error, count error and punt packet to the stack */
1231 if (rx_error & (1 << I40E_RX_DESC_ERROR_L4E_SHIFT))
1232 goto checksum_fail;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001233
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001234 /* handle packets that were not able to be checksummed due
1235 * to arrival speed, in this case the stack can compute
1236 * the csum.
1237 */
1238 if (rx_error & (1 << I40E_RX_DESC_ERROR_PPRS_SHIFT))
1239 return;
1240
1241 /* If VXLAN traffic has an outer UDPv4 checksum we need to check
1242 * it in the driver, hardware does not do it for us.
1243 * Since L3L4P bit was set we assume a valid IHL value (>=5)
1244 * so the total length of IPv4 header is IHL*4 bytes
1245 * The UDP_0 bit *may* bet set if the *inner* header is UDP
1246 */
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001247 if (ipv4_tunnel &&
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001248 (decoded.inner_prot != I40E_RX_PTYPE_INNER_PROT_UDP) &&
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001249 !(rx_status & (1 << I40E_RX_DESC_STATUS_UDP_0_SHIFT))) {
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001250 skb->transport_header = skb->mac_header +
1251 sizeof(struct ethhdr) +
1252 (ip_hdr(skb)->ihl * 4);
1253
1254 /* Add 4 bytes for VLAN tagged packets */
1255 skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
1256 skb->protocol == htons(ETH_P_8021AD))
1257 ? VLAN_HLEN : 0;
1258
1259 rx_udp_csum = udp_csum(skb);
1260 iph = ip_hdr(skb);
1261 csum = csum_tcpudp_magic(
1262 iph->saddr, iph->daddr,
1263 (skb->len - skb_transport_offset(skb)),
1264 IPPROTO_UDP, rx_udp_csum);
1265
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001266 if (udp_hdr(skb)->check != csum)
1267 goto checksum_fail;
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001268 }
1269
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001270 skb->ip_summed = CHECKSUM_UNNECESSARY;
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001271
1272 return;
1273
1274checksum_fail:
1275 vsi->back->hw_csum_rx_error++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001276}
1277
1278/**
1279 * i40e_rx_hash - returns the hash value from the Rx descriptor
1280 * @ring: descriptor ring
1281 * @rx_desc: specific descriptor
1282 **/
1283static inline u32 i40e_rx_hash(struct i40e_ring *ring,
1284 union i40e_rx_desc *rx_desc)
1285{
Jesse Brandeburg8a494922013-11-20 10:02:49 +00001286 const __le64 rss_mask =
1287 cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
1288 I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
1289
1290 if ((ring->netdev->features & NETIF_F_RXHASH) &&
1291 (rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask)
1292 return le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
1293 else
1294 return 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001295}
1296
1297/**
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001298 * i40e_ptype_to_hash - get a hash type
1299 * @ptype: the ptype value from the descriptor
1300 *
1301 * Returns a hash type to be used by skb_set_hash
1302 **/
1303static inline enum pkt_hash_types i40e_ptype_to_hash(u8 ptype)
1304{
1305 struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
1306
1307 if (!decoded.known)
1308 return PKT_HASH_TYPE_NONE;
1309
1310 if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1311 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
1312 return PKT_HASH_TYPE_L4;
1313 else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1314 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
1315 return PKT_HASH_TYPE_L3;
1316 else
1317 return PKT_HASH_TYPE_L2;
1318}
1319
1320/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001321 * i40e_clean_rx_irq - Reclaim resources after receive completes
1322 * @rx_ring: rx ring to clean
1323 * @budget: how many cleans we're allowed
1324 *
1325 * Returns true if there's any budget left (e.g. the clean is finished)
1326 **/
1327static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
1328{
1329 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1330 u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
1331 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
1332 const int current_node = numa_node_id();
1333 struct i40e_vsi *vsi = rx_ring->vsi;
1334 u16 i = rx_ring->next_to_clean;
1335 union i40e_rx_desc *rx_desc;
1336 u32 rx_error, rx_status;
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001337 u8 rx_ptype;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001338 u64 qword;
1339
Eric W. Biederman390f86d2014-03-14 17:59:10 -07001340 if (budget <= 0)
1341 return 0;
1342
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001343 rx_desc = I40E_RX_DESC(rx_ring, i);
1344 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Jesse Brandeburg6838b532014-01-14 00:49:52 -08001345 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1346 I40E_RXD_QW1_STATUS_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001347
1348 while (rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)) {
1349 union i40e_rx_desc *next_rxd;
1350 struct i40e_rx_buffer *rx_bi;
1351 struct sk_buff *skb;
1352 u16 vlan_tag;
1353 if (i40e_rx_is_programming_status(qword)) {
1354 i40e_clean_programming_status(rx_ring, rx_desc);
1355 I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
1356 goto next_desc;
1357 }
1358 rx_bi = &rx_ring->rx_bi[i];
1359 skb = rx_bi->skb;
1360 prefetch(skb->data);
1361
Mitch Williams829af3a2013-12-18 13:46:00 +00001362 rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
1363 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
1364 rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
1365 I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
1366 rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
1367 I40E_RXD_QW1_LENGTH_SPH_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001368
Mitch Williams829af3a2013-12-18 13:46:00 +00001369 rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
1370 I40E_RXD_QW1_ERROR_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001371 rx_hbo = rx_error & (1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
1372 rx_error &= ~(1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
1373
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001374 rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
1375 I40E_RXD_QW1_PTYPE_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001376 rx_bi->skb = NULL;
1377
1378 /* This memory barrier is needed to keep us from reading
1379 * any other fields out of the rx_desc until we know the
1380 * STATUS_DD bit is set
1381 */
1382 rmb();
1383
1384 /* Get the header and possibly the whole packet
1385 * If this is an skb from previous receive dma will be 0
1386 */
1387 if (rx_bi->dma) {
1388 u16 len;
1389
1390 if (rx_hbo)
1391 len = I40E_RX_HDR_SIZE;
1392 else if (rx_sph)
1393 len = rx_header_len;
1394 else if (rx_packet_len)
1395 len = rx_packet_len; /* 1buf/no split found */
1396 else
1397 len = rx_header_len; /* split always mode */
1398
1399 skb_put(skb, len);
1400 dma_unmap_single(rx_ring->dev,
1401 rx_bi->dma,
1402 rx_ring->rx_buf_len,
1403 DMA_FROM_DEVICE);
1404 rx_bi->dma = 0;
1405 }
1406
1407 /* Get the rest of the data if this was a header split */
1408 if (ring_is_ps_enabled(rx_ring) && rx_packet_len) {
1409
1410 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
1411 rx_bi->page,
1412 rx_bi->page_offset,
1413 rx_packet_len);
1414
1415 skb->len += rx_packet_len;
1416 skb->data_len += rx_packet_len;
1417 skb->truesize += rx_packet_len;
1418
1419 if ((page_count(rx_bi->page) == 1) &&
1420 (page_to_nid(rx_bi->page) == current_node))
1421 get_page(rx_bi->page);
1422 else
1423 rx_bi->page = NULL;
1424
1425 dma_unmap_page(rx_ring->dev,
1426 rx_bi->page_dma,
1427 PAGE_SIZE / 2,
1428 DMA_FROM_DEVICE);
1429 rx_bi->page_dma = 0;
1430 }
1431 I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
1432
1433 if (unlikely(
1434 !(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
1435 struct i40e_rx_buffer *next_buffer;
1436
1437 next_buffer = &rx_ring->rx_bi[i];
1438
1439 if (ring_is_ps_enabled(rx_ring)) {
1440 rx_bi->skb = next_buffer->skb;
1441 rx_bi->dma = next_buffer->dma;
1442 next_buffer->skb = skb;
1443 next_buffer->dma = 0;
1444 }
1445 rx_ring->rx_stats.non_eop_descs++;
1446 goto next_desc;
1447 }
1448
1449 /* ERR_MASK will only have valid bits if EOP set */
1450 if (unlikely(rx_error & (1 << I40E_RX_DESC_ERROR_RXE_SHIFT))) {
1451 dev_kfree_skb_any(skb);
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001452 /* TODO: shouldn't we increment a counter indicating the
1453 * drop?
1454 */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001455 goto next_desc;
1456 }
1457
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001458 skb_set_hash(skb, i40e_rx_hash(rx_ring, rx_desc),
1459 i40e_ptype_to_hash(rx_ptype));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001460 if (unlikely(rx_status & I40E_RXD_QW1_STATUS_TSYNVALID_MASK)) {
1461 i40e_ptp_rx_hwtstamp(vsi->back, skb, (rx_status &
1462 I40E_RXD_QW1_STATUS_TSYNINDX_MASK) >>
1463 I40E_RXD_QW1_STATUS_TSYNINDX_SHIFT);
1464 rx_ring->last_rx_timestamp = jiffies;
1465 }
1466
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001467 /* probably a little skewed due to removing CRC */
1468 total_rx_bytes += skb->len;
1469 total_rx_packets++;
1470
1471 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001472
1473 i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
1474
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001475 vlan_tag = rx_status & (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
1476 ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
1477 : 0;
1478 i40e_receive_skb(rx_ring, skb, vlan_tag);
1479
1480 rx_ring->netdev->last_rx = jiffies;
1481 budget--;
1482next_desc:
1483 rx_desc->wb.qword1.status_error_len = 0;
1484 if (!budget)
1485 break;
1486
1487 cleaned_count++;
1488 /* return some buffers to hardware, one at a time is too slow */
1489 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
1490 i40e_alloc_rx_buffers(rx_ring, cleaned_count);
1491 cleaned_count = 0;
1492 }
1493
1494 /* use prefetched values */
1495 rx_desc = next_rxd;
1496 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Mitch Williams829af3a2013-12-18 13:46:00 +00001497 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1498 I40E_RXD_QW1_STATUS_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001499 }
1500
1501 rx_ring->next_to_clean = i;
Alexander Duyck980e9b12013-09-28 06:01:03 +00001502 u64_stats_update_begin(&rx_ring->syncp);
Alexander Duycka114d0a2013-09-28 06:00:43 +00001503 rx_ring->stats.packets += total_rx_packets;
1504 rx_ring->stats.bytes += total_rx_bytes;
Alexander Duyck980e9b12013-09-28 06:01:03 +00001505 u64_stats_update_end(&rx_ring->syncp);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001506 rx_ring->q_vector->rx.total_packets += total_rx_packets;
1507 rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
1508
1509 if (cleaned_count)
1510 i40e_alloc_rx_buffers(rx_ring, cleaned_count);
1511
1512 return budget > 0;
1513}
1514
1515/**
1516 * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
1517 * @napi: napi struct with our devices info in it
1518 * @budget: amount of work driver is allowed to do this pass, in packets
1519 *
1520 * This function will clean all queues associated with a q_vector.
1521 *
1522 * Returns the amount of work done
1523 **/
1524int i40e_napi_poll(struct napi_struct *napi, int budget)
1525{
1526 struct i40e_q_vector *q_vector =
1527 container_of(napi, struct i40e_q_vector, napi);
1528 struct i40e_vsi *vsi = q_vector->vsi;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001529 struct i40e_ring *ring;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001530 bool clean_complete = true;
1531 int budget_per_ring;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001532
1533 if (test_bit(__I40E_DOWN, &vsi->state)) {
1534 napi_complete(napi);
1535 return 0;
1536 }
1537
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001538 /* Since the actual Tx work is minimal, we can give the Tx a larger
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001539 * budget and be more aggressive about cleaning up the Tx descriptors.
1540 */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001541 i40e_for_each_ring(ring, q_vector->tx)
1542 clean_complete &= i40e_clean_tx_irq(ring, vsi->work_limit);
1543
1544 /* We attempt to distribute budget to each Rx queue fairly, but don't
1545 * allow the budget to go below 1 because that would exit polling early.
1546 */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001547 budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001548
1549 i40e_for_each_ring(ring, q_vector->rx)
1550 clean_complete &= i40e_clean_rx_irq(ring, budget_per_ring);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001551
1552 /* If work not completed, return budget and polling will return */
1553 if (!clean_complete)
1554 return budget;
1555
1556 /* Work is done so exit the polling mode and re-enable the interrupt */
1557 napi_complete(napi);
1558 if (ITR_IS_DYNAMIC(vsi->rx_itr_setting) ||
1559 ITR_IS_DYNAMIC(vsi->tx_itr_setting))
1560 i40e_update_dynamic_itr(q_vector);
1561
1562 if (!test_bit(__I40E_DOWN, &vsi->state)) {
1563 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
1564 i40e_irq_dynamic_enable(vsi,
1565 q_vector->v_idx + vsi->base_vector);
1566 } else {
1567 struct i40e_hw *hw = &vsi->back->hw;
1568 /* We re-enable the queue 0 cause, but
1569 * don't worry about dynamic_enable
1570 * because we left it on for the other
1571 * possible interrupts during napi
1572 */
1573 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
1574 qval |= I40E_QINT_RQCTL_CAUSE_ENA_MASK;
1575 wr32(hw, I40E_QINT_RQCTL(0), qval);
1576
1577 qval = rd32(hw, I40E_QINT_TQCTL(0));
1578 qval |= I40E_QINT_TQCTL_CAUSE_ENA_MASK;
1579 wr32(hw, I40E_QINT_TQCTL(0), qval);
Shannon Nelson116a57d2013-09-28 07:13:59 +00001580
1581 i40e_irq_dynamic_enable_icr0(vsi->back);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001582 }
1583 }
1584
1585 return 0;
1586}
1587
1588/**
1589 * i40e_atr - Add a Flow Director ATR filter
1590 * @tx_ring: ring to add programming descriptor to
1591 * @skb: send buffer
1592 * @flags: send flags
1593 * @protocol: wire protocol
1594 **/
1595static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
1596 u32 flags, __be16 protocol)
1597{
1598 struct i40e_filter_program_desc *fdir_desc;
1599 struct i40e_pf *pf = tx_ring->vsi->back;
1600 union {
1601 unsigned char *network;
1602 struct iphdr *ipv4;
1603 struct ipv6hdr *ipv6;
1604 } hdr;
1605 struct tcphdr *th;
1606 unsigned int hlen;
1607 u32 flex_ptype, dtype_cmd;
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001608 u16 i;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001609
1610 /* make sure ATR is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08001611 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001612 return;
1613
1614 /* if sampling is disabled do nothing */
1615 if (!tx_ring->atr_sample_rate)
1616 return;
1617
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001618 /* snag network header to get L4 type and address */
1619 hdr.network = skb_network_header(skb);
1620
1621 /* Currently only IPv4/IPv6 with TCP is supported */
1622 if (protocol == htons(ETH_P_IP)) {
1623 if (hdr.ipv4->protocol != IPPROTO_TCP)
1624 return;
1625
1626 /* access ihl as a u8 to avoid unaligned access on ia64 */
1627 hlen = (hdr.network[0] & 0x0F) << 2;
1628 } else if (protocol == htons(ETH_P_IPV6)) {
1629 if (hdr.ipv6->nexthdr != IPPROTO_TCP)
1630 return;
1631
1632 hlen = sizeof(struct ipv6hdr);
1633 } else {
1634 return;
1635 }
1636
1637 th = (struct tcphdr *)(hdr.network + hlen);
1638
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00001639 /* Due to lack of space, no more new filters can be programmed */
1640 if (th->syn && (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1641 return;
1642
1643 tx_ring->atr_count++;
1644
Anjali Singhai Jaince806782014-03-06 08:59:54 +00001645 /* sample on all syn/fin/rst packets or once every atr sample rate */
1646 if (!th->fin &&
1647 !th->syn &&
1648 !th->rst &&
1649 (tx_ring->atr_count < tx_ring->atr_sample_rate))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001650 return;
1651
1652 tx_ring->atr_count = 0;
1653
1654 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001655 i = tx_ring->next_to_use;
1656 fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
1657
1658 i++;
1659 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001660
1661 flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
1662 I40E_TXD_FLTR_QW0_QINDEX_MASK;
1663 flex_ptype |= (protocol == htons(ETH_P_IP)) ?
1664 (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
1665 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
1666 (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
1667 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
1668
1669 flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
1670
1671 dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
1672
Anjali Singhai Jaince806782014-03-06 08:59:54 +00001673 dtype_cmd |= (th->fin || th->rst) ?
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001674 (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
1675 I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
1676 (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
1677 I40E_TXD_FLTR_QW1_PCMD_SHIFT);
1678
1679 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
1680 I40E_TXD_FLTR_QW1_DEST_SHIFT;
1681
1682 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
1683 I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
1684
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001685 dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
1686 dtype_cmd |=
1687 ((u32)pf->fd_atr_cnt_idx << I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
1688 I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
1689
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001690 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
Jesse Brandeburg99753ea2014-06-04 04:22:49 +00001691 fdir_desc->rsvd = cpu_to_le32(0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001692 fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
Jesse Brandeburg99753ea2014-06-04 04:22:49 +00001693 fdir_desc->fd_id = cpu_to_le32(0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001694}
1695
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001696/**
1697 * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
1698 * @skb: send buffer
1699 * @tx_ring: ring to send buffer on
1700 * @flags: the tx flags to be set
1701 *
1702 * Checks the skb and set up correspondingly several generic transmit flags
1703 * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
1704 *
1705 * Returns error code indicate the frame should be dropped upon error and the
1706 * otherwise returns 0 to indicate the flags has been set properly.
1707 **/
1708static int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
1709 struct i40e_ring *tx_ring,
1710 u32 *flags)
1711{
1712 __be16 protocol = skb->protocol;
1713 u32 tx_flags = 0;
1714
1715 /* if we have a HW VLAN tag being added, default to the HW one */
1716 if (vlan_tx_tag_present(skb)) {
1717 tx_flags |= vlan_tx_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
1718 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1719 /* else if it is a SW VLAN, check the next protocol and store the tag */
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00001720 } else if (protocol == htons(ETH_P_8021Q)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001721 struct vlan_hdr *vhdr, _vhdr;
1722 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
1723 if (!vhdr)
1724 return -EINVAL;
1725
1726 protocol = vhdr->h_vlan_encapsulated_proto;
1727 tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
1728 tx_flags |= I40E_TX_FLAGS_SW_VLAN;
1729 }
1730
1731 /* Insert 802.1p priority into VLAN header */
1732 if ((tx_ring->vsi->back->flags & I40E_FLAG_DCB_ENABLED) &&
1733 ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
1734 (skb->priority != TC_PRIO_CONTROL))) {
1735 tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
1736 tx_flags |= (skb->priority & 0x7) <<
1737 I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
1738 if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
1739 struct vlan_ethhdr *vhdr;
Francois Romieudd225bc2014-03-30 03:14:48 +00001740 int rc;
1741
1742 rc = skb_cow_head(skb, 0);
1743 if (rc < 0)
1744 return rc;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001745 vhdr = (struct vlan_ethhdr *)skb->data;
1746 vhdr->h_vlan_TCI = htons(tx_flags >>
1747 I40E_TX_FLAGS_VLAN_SHIFT);
1748 } else {
1749 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1750 }
1751 }
1752 *flags = tx_flags;
1753 return 0;
1754}
1755
1756/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001757 * i40e_tso - set up the tso context descriptor
1758 * @tx_ring: ptr to the ring to send
1759 * @skb: ptr to the skb we're sending
1760 * @tx_flags: the collected send information
1761 * @protocol: the send protocol
1762 * @hdr_len: ptr to the size of the packet header
1763 * @cd_tunneling: ptr to context descriptor bits
1764 *
1765 * Returns 0 if no TSO can happen, 1 if tso is going, or error
1766 **/
1767static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
1768 u32 tx_flags, __be16 protocol, u8 *hdr_len,
1769 u64 *cd_type_cmd_tso_mss, u32 *cd_tunneling)
1770{
1771 u32 cd_cmd, cd_tso_len, cd_mss;
Francois Romieudd225bc2014-03-30 03:14:48 +00001772 struct ipv6hdr *ipv6h;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001773 struct tcphdr *tcph;
1774 struct iphdr *iph;
1775 u32 l4len;
1776 int err;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001777
1778 if (!skb_is_gso(skb))
1779 return 0;
1780
Francois Romieudd225bc2014-03-30 03:14:48 +00001781 err = skb_cow_head(skb, 0);
1782 if (err < 0)
1783 return err;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001784
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00001785 if (protocol == htons(ETH_P_IP)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001786 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
1787 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1788 iph->tot_len = 0;
1789 iph->check = 0;
1790 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1791 0, IPPROTO_TCP, 0);
1792 } else if (skb_is_gso_v6(skb)) {
1793
1794 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb)
1795 : ipv6_hdr(skb);
1796 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1797 ipv6h->payload_len = 0;
1798 tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
1799 0, IPPROTO_TCP, 0);
1800 }
1801
1802 l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
1803 *hdr_len = (skb->encapsulation
1804 ? (skb_inner_transport_header(skb) - skb->data)
1805 : skb_transport_offset(skb)) + l4len;
1806
1807 /* find the field values */
1808 cd_cmd = I40E_TX_CTX_DESC_TSO;
1809 cd_tso_len = skb->len - *hdr_len;
1810 cd_mss = skb_shinfo(skb)->gso_size;
Mitch Williams829af3a2013-12-18 13:46:00 +00001811 *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
1812 ((u64)cd_tso_len <<
1813 I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
1814 ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001815 return 1;
1816}
1817
1818/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001819 * i40e_tsyn - set up the tsyn context descriptor
1820 * @tx_ring: ptr to the ring to send
1821 * @skb: ptr to the skb we're sending
1822 * @tx_flags: the collected send information
1823 *
1824 * Returns 0 if no Tx timestamp can happen and 1 if the timestamp will happen
1825 **/
1826static int i40e_tsyn(struct i40e_ring *tx_ring, struct sk_buff *skb,
1827 u32 tx_flags, u64 *cd_type_cmd_tso_mss)
1828{
1829 struct i40e_pf *pf;
1830
1831 if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
1832 return 0;
1833
1834 /* Tx timestamps cannot be sampled when doing TSO */
1835 if (tx_flags & I40E_TX_FLAGS_TSO)
1836 return 0;
1837
1838 /* only timestamp the outbound packet if the user has requested it and
1839 * we are not already transmitting a packet to be timestamped
1840 */
1841 pf = i40e_netdev_to_pf(tx_ring->netdev);
1842 if (pf->ptp_tx && !pf->ptp_tx_skb) {
1843 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1844 pf->ptp_tx_skb = skb_get(skb);
1845 } else {
1846 return 0;
1847 }
1848
1849 *cd_type_cmd_tso_mss |= (u64)I40E_TX_CTX_DESC_TSYN <<
1850 I40E_TXD_CTX_QW1_CMD_SHIFT;
1851
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001852 return 1;
1853}
1854
1855/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001856 * i40e_tx_enable_csum - Enable Tx checksum offloads
1857 * @skb: send buffer
1858 * @tx_flags: Tx flags currently set
1859 * @td_cmd: Tx descriptor command bits to set
1860 * @td_offset: Tx descriptor header offsets to set
1861 * @cd_tunneling: ptr to context desc bits
1862 **/
1863static void i40e_tx_enable_csum(struct sk_buff *skb, u32 tx_flags,
1864 u32 *td_cmd, u32 *td_offset,
1865 struct i40e_ring *tx_ring,
1866 u32 *cd_tunneling)
1867{
1868 struct ipv6hdr *this_ipv6_hdr;
1869 unsigned int this_tcp_hdrlen;
1870 struct iphdr *this_ip_hdr;
1871 u32 network_hdr_len;
1872 u8 l4_hdr = 0;
1873
1874 if (skb->encapsulation) {
1875 network_hdr_len = skb_inner_network_header_len(skb);
1876 this_ip_hdr = inner_ip_hdr(skb);
1877 this_ipv6_hdr = inner_ipv6_hdr(skb);
1878 this_tcp_hdrlen = inner_tcp_hdrlen(skb);
1879
1880 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1881
1882 if (tx_flags & I40E_TX_FLAGS_TSO) {
1883 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
1884 ip_hdr(skb)->check = 0;
1885 } else {
1886 *cd_tunneling |=
1887 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1888 }
1889 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1890 if (tx_flags & I40E_TX_FLAGS_TSO) {
1891 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
1892 ip_hdr(skb)->check = 0;
1893 } else {
1894 *cd_tunneling |=
1895 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1896 }
1897 }
1898
1899 /* Now set the ctx descriptor fields */
1900 *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
1901 I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
1902 I40E_TXD_CTX_UDP_TUNNELING |
1903 ((skb_inner_network_offset(skb) -
1904 skb_transport_offset(skb)) >> 1) <<
1905 I40E_TXD_CTX_QW0_NATLEN_SHIFT;
1906
1907 } else {
1908 network_hdr_len = skb_network_header_len(skb);
1909 this_ip_hdr = ip_hdr(skb);
1910 this_ipv6_hdr = ipv6_hdr(skb);
1911 this_tcp_hdrlen = tcp_hdrlen(skb);
1912 }
1913
1914 /* Enable IP checksum offloads */
1915 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1916 l4_hdr = this_ip_hdr->protocol;
1917 /* the stack computes the IP header already, the only time we
1918 * need the hardware to recompute it is in the case of TSO.
1919 */
1920 if (tx_flags & I40E_TX_FLAGS_TSO) {
1921 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
1922 this_ip_hdr->check = 0;
1923 } else {
1924 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
1925 }
1926 /* Now set the td_offset for IP header length */
1927 *td_offset = (network_hdr_len >> 2) <<
1928 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1929 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1930 l4_hdr = this_ipv6_hdr->nexthdr;
1931 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
1932 /* Now set the td_offset for IP header length */
1933 *td_offset = (network_hdr_len >> 2) <<
1934 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1935 }
1936 /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
1937 *td_offset |= (skb_network_offset(skb) >> 1) <<
1938 I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
1939
1940 /* Enable L4 checksum offloads */
1941 switch (l4_hdr) {
1942 case IPPROTO_TCP:
1943 /* enable checksum offloads */
1944 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
1945 *td_offset |= (this_tcp_hdrlen >> 2) <<
1946 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1947 break;
1948 case IPPROTO_SCTP:
1949 /* enable SCTP checksum offload */
1950 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
1951 *td_offset |= (sizeof(struct sctphdr) >> 2) <<
1952 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1953 break;
1954 case IPPROTO_UDP:
1955 /* enable UDP checksum offload */
1956 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
1957 *td_offset |= (sizeof(struct udphdr) >> 2) <<
1958 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1959 break;
1960 default:
1961 break;
1962 }
1963}
1964
1965/**
1966 * i40e_create_tx_ctx Build the Tx context descriptor
1967 * @tx_ring: ring to create the descriptor on
1968 * @cd_type_cmd_tso_mss: Quad Word 1
1969 * @cd_tunneling: Quad Word 0 - bits 0-31
1970 * @cd_l2tag2: Quad Word 0 - bits 32-63
1971 **/
1972static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
1973 const u64 cd_type_cmd_tso_mss,
1974 const u32 cd_tunneling, const u32 cd_l2tag2)
1975{
1976 struct i40e_tx_context_desc *context_desc;
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001977 int i = tx_ring->next_to_use;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001978
Jesse Brandeburgff40dd52014-02-14 02:14:41 +00001979 if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
1980 !cd_tunneling && !cd_l2tag2)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001981 return;
1982
1983 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001984 context_desc = I40E_TX_CTXTDESC(tx_ring, i);
1985
1986 i++;
1987 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001988
1989 /* cpu_to_le32 and assign to struct fields */
1990 context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
1991 context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
1992 context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
1993}
1994
1995/**
1996 * i40e_tx_map - Build the Tx descriptor
1997 * @tx_ring: ring to send buffer on
1998 * @skb: send buffer
1999 * @first: first buffer info buffer to use
2000 * @tx_flags: collected send information
2001 * @hdr_len: size of the packet header
2002 * @td_cmd: the command field in the descriptor
2003 * @td_offset: offset for checksum or crc
2004 **/
2005static void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
2006 struct i40e_tx_buffer *first, u32 tx_flags,
2007 const u8 hdr_len, u32 td_cmd, u32 td_offset)
2008{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002009 unsigned int data_len = skb->data_len;
2010 unsigned int size = skb_headlen(skb);
Alexander Duycka5e9c572013-09-28 06:00:27 +00002011 struct skb_frag_struct *frag;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002012 struct i40e_tx_buffer *tx_bi;
2013 struct i40e_tx_desc *tx_desc;
Alexander Duycka5e9c572013-09-28 06:00:27 +00002014 u16 i = tx_ring->next_to_use;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002015 u32 td_tag = 0;
2016 dma_addr_t dma;
2017 u16 gso_segs;
2018
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002019 if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
2020 td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
2021 td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
2022 I40E_TX_FLAGS_VLAN_SHIFT;
2023 }
2024
Alexander Duycka5e9c572013-09-28 06:00:27 +00002025 if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
2026 gso_segs = skb_shinfo(skb)->gso_segs;
2027 else
2028 gso_segs = 1;
2029
2030 /* multiply data chunks by size of headers */
2031 first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
2032 first->gso_segs = gso_segs;
2033 first->skb = skb;
2034 first->tx_flags = tx_flags;
2035
2036 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
2037
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002038 tx_desc = I40E_TX_DESC(tx_ring, i);
Alexander Duycka5e9c572013-09-28 06:00:27 +00002039 tx_bi = first;
2040
2041 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
2042 if (dma_mapping_error(tx_ring->dev, dma))
2043 goto dma_error;
2044
2045 /* record length, and DMA address */
2046 dma_unmap_len_set(tx_bi, len, size);
2047 dma_unmap_addr_set(tx_bi, dma, dma);
2048
2049 tx_desc->buffer_addr = cpu_to_le64(dma);
2050
2051 while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002052 tx_desc->cmd_type_offset_bsz =
2053 build_ctob(td_cmd, td_offset,
2054 I40E_MAX_DATA_PER_TXD, td_tag);
2055
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002056 tx_desc++;
2057 i++;
2058 if (i == tx_ring->count) {
2059 tx_desc = I40E_TX_DESC(tx_ring, 0);
2060 i = 0;
2061 }
Alexander Duycka5e9c572013-09-28 06:00:27 +00002062
2063 dma += I40E_MAX_DATA_PER_TXD;
2064 size -= I40E_MAX_DATA_PER_TXD;
2065
2066 tx_desc->buffer_addr = cpu_to_le64(dma);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002067 }
2068
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002069 if (likely(!data_len))
2070 break;
2071
Alexander Duycka5e9c572013-09-28 06:00:27 +00002072 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
2073 size, td_tag);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002074
2075 tx_desc++;
2076 i++;
2077 if (i == tx_ring->count) {
2078 tx_desc = I40E_TX_DESC(tx_ring, 0);
2079 i = 0;
2080 }
2081
Alexander Duycka5e9c572013-09-28 06:00:27 +00002082 size = skb_frag_size(frag);
2083 data_len -= size;
2084
2085 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
2086 DMA_TO_DEVICE);
2087
2088 tx_bi = &tx_ring->tx_bi[i];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002089 }
2090
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002091 /* Place RS bit on last descriptor of any packet that spans across the
2092 * 4th descriptor (WB_STRIDE aka 0x3) in a 64B cacheline.
2093 */
2094#define WB_STRIDE 0x3
2095 if (((i & WB_STRIDE) != WB_STRIDE) &&
2096 (first <= &tx_ring->tx_bi[i]) &&
2097 (first >= &tx_ring->tx_bi[i & ~WB_STRIDE])) {
2098 tx_desc->cmd_type_offset_bsz =
2099 build_ctob(td_cmd, td_offset, size, td_tag) |
2100 cpu_to_le64((u64)I40E_TX_DESC_CMD_EOP <<
2101 I40E_TXD_QW1_CMD_SHIFT);
2102 } else {
2103 tx_desc->cmd_type_offset_bsz =
2104 build_ctob(td_cmd, td_offset, size, td_tag) |
2105 cpu_to_le64((u64)I40E_TXD_CMD <<
2106 I40E_TXD_QW1_CMD_SHIFT);
2107 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002108
Alexander Duyck7070ce02013-09-28 06:00:37 +00002109 netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
2110 tx_ring->queue_index),
2111 first->bytecount);
2112
Alexander Duycka5e9c572013-09-28 06:00:27 +00002113 /* set the timestamp */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002114 first->time_stamp = jiffies;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002115
2116 /* Force memory writes to complete before letting h/w
2117 * know there are new descriptors to fetch. (Only
2118 * applicable for weak-ordered memory model archs,
2119 * such as IA-64).
2120 */
2121 wmb();
2122
Alexander Duycka5e9c572013-09-28 06:00:27 +00002123 /* set next_to_watch value indicating a packet is present */
2124 first->next_to_watch = tx_desc;
2125
2126 i++;
2127 if (i == tx_ring->count)
2128 i = 0;
2129
2130 tx_ring->next_to_use = i;
2131
2132 /* notify HW of packet */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002133 writel(i, tx_ring->tail);
Alexander Duycka5e9c572013-09-28 06:00:27 +00002134
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002135 return;
2136
2137dma_error:
Alexander Duycka5e9c572013-09-28 06:00:27 +00002138 dev_info(tx_ring->dev, "TX DMA map failed\n");
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002139
2140 /* clear dma mappings for failed tx_bi map */
2141 for (;;) {
2142 tx_bi = &tx_ring->tx_bi[i];
Alexander Duycka5e9c572013-09-28 06:00:27 +00002143 i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002144 if (tx_bi == first)
2145 break;
2146 if (i == 0)
2147 i = tx_ring->count;
2148 i--;
2149 }
2150
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002151 tx_ring->next_to_use = i;
2152}
2153
2154/**
2155 * __i40e_maybe_stop_tx - 2nd 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 -EBUSY if a stop is needed, else 0
2160 **/
2161static inline int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
2162{
2163 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
Greg Rose8e9dca52013-12-18 13:45:53 +00002164 /* Memory barrier before checking head and tail */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002165 smp_mb();
2166
2167 /* Check again in a case another CPU has just made room available. */
2168 if (likely(I40E_DESC_UNUSED(tx_ring) < size))
2169 return -EBUSY;
2170
2171 /* A reprieve! - use start_queue because it doesn't call schedule */
2172 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
2173 ++tx_ring->tx_stats.restart_queue;
2174 return 0;
2175}
2176
2177/**
2178 * i40e_maybe_stop_tx - 1st level check for tx stop conditions
2179 * @tx_ring: the ring to be checked
2180 * @size: the size buffer we want to assure is available
2181 *
2182 * Returns 0 if stop is not needed
2183 **/
2184static int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
2185{
2186 if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
2187 return 0;
2188 return __i40e_maybe_stop_tx(tx_ring, size);
2189}
2190
2191/**
2192 * i40e_xmit_descriptor_count - calculate number of tx descriptors needed
2193 * @skb: send buffer
2194 * @tx_ring: ring to send buffer on
2195 *
2196 * Returns number of data descriptors needed for this skb. Returns 0 to indicate
2197 * there is not enough descriptors available in this ring since we need at least
2198 * one descriptor.
2199 **/
2200static int i40e_xmit_descriptor_count(struct sk_buff *skb,
2201 struct i40e_ring *tx_ring)
2202{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002203 unsigned int f;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002204 int count = 0;
2205
2206 /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
2207 * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
Jesse Brandeburgbe560522014-02-06 05:51:13 +00002208 * + 4 desc gap to avoid the cache line where head is,
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002209 * + 1 desc for context descriptor,
2210 * otherwise try next time
2211 */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002212 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
2213 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
Jesse Brandeburg980093e2014-05-10 04:49:12 +00002214
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002215 count += TXD_USE_COUNT(skb_headlen(skb));
Jesse Brandeburgbe560522014-02-06 05:51:13 +00002216 if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002217 tx_ring->tx_stats.tx_busy++;
2218 return 0;
2219 }
2220 return count;
2221}
2222
2223/**
2224 * i40e_xmit_frame_ring - Sends buffer on Tx ring
2225 * @skb: send buffer
2226 * @tx_ring: ring to send buffer on
2227 *
2228 * Returns NETDEV_TX_OK if sent, else an error code
2229 **/
2230static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
2231 struct i40e_ring *tx_ring)
2232{
2233 u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
2234 u32 cd_tunneling = 0, cd_l2tag2 = 0;
2235 struct i40e_tx_buffer *first;
2236 u32 td_offset = 0;
2237 u32 tx_flags = 0;
2238 __be16 protocol;
2239 u32 td_cmd = 0;
2240 u8 hdr_len = 0;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002241 int tsyn;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002242 int tso;
2243 if (0 == i40e_xmit_descriptor_count(skb, tx_ring))
2244 return NETDEV_TX_BUSY;
2245
2246 /* prepare the xmit flags */
2247 if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
2248 goto out_drop;
2249
2250 /* obtain protocol of skb */
2251 protocol = skb->protocol;
2252
2253 /* record the location of the first descriptor for this packet */
2254 first = &tx_ring->tx_bi[tx_ring->next_to_use];
2255
2256 /* setup IPv4/IPv6 offloads */
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00002257 if (protocol == htons(ETH_P_IP))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002258 tx_flags |= I40E_TX_FLAGS_IPV4;
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00002259 else if (protocol == htons(ETH_P_IPV6))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002260 tx_flags |= I40E_TX_FLAGS_IPV6;
2261
2262 tso = i40e_tso(tx_ring, skb, tx_flags, protocol, &hdr_len,
2263 &cd_type_cmd_tso_mss, &cd_tunneling);
2264
2265 if (tso < 0)
2266 goto out_drop;
2267 else if (tso)
2268 tx_flags |= I40E_TX_FLAGS_TSO;
2269
2270 skb_tx_timestamp(skb);
2271
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002272 tsyn = i40e_tsyn(tx_ring, skb, tx_flags, &cd_type_cmd_tso_mss);
2273
2274 if (tsyn)
2275 tx_flags |= I40E_TX_FLAGS_TSYN;
2276
Alexander Duyckb1941302013-09-28 06:00:32 +00002277 /* always enable CRC insertion offload */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002278 td_cmd |= I40E_TX_DESC_CMD_ICRC;
2279
Alexander Duyckb1941302013-09-28 06:00:32 +00002280 /* Always offload the checksum, since it's in the data descriptor */
2281 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2282 tx_flags |= I40E_TX_FLAGS_CSUM;
2283
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002284 i40e_tx_enable_csum(skb, tx_flags, &td_cmd, &td_offset,
2285 tx_ring, &cd_tunneling);
Alexander Duyckb1941302013-09-28 06:00:32 +00002286 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002287
2288 i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
2289 cd_tunneling, cd_l2tag2);
2290
2291 /* Add Flow Director ATR if it's enabled.
2292 *
2293 * NOTE: this must always be directly before the data descriptor.
2294 */
2295 i40e_atr(tx_ring, skb, tx_flags, protocol);
2296
2297 i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
2298 td_cmd, td_offset);
2299
2300 i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
2301
2302 return NETDEV_TX_OK;
2303
2304out_drop:
2305 dev_kfree_skb_any(skb);
2306 return NETDEV_TX_OK;
2307}
2308
2309/**
2310 * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
2311 * @skb: send buffer
2312 * @netdev: network interface device structure
2313 *
2314 * Returns NETDEV_TX_OK if sent, else an error code
2315 **/
2316netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2317{
2318 struct i40e_netdev_priv *np = netdev_priv(netdev);
2319 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck9f65e152013-09-28 06:00:58 +00002320 struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002321
2322 /* hardware can't handle really short frames, hardware padding works
2323 * beyond this point
2324 */
2325 if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
2326 if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
2327 return NETDEV_TX_OK;
2328 skb->len = I40E_MIN_TX_LEN;
2329 skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
2330 }
2331
2332 return i40e_xmit_frame_ring(skb, tx_ring);
2333}