blob: a6cc8d66e21444975aca9092d4cc9c373c12d70d [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)
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +000042#define I40E_FD_CLEAN_DELAY 10
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000043/**
44 * i40e_program_fdir_filter - Program a Flow Director filter
Joseph Gasparakis17a73f62014-02-12 01:45:30 +000045 * @fdir_data: Packet data that will be filter parameters
46 * @raw_packet: the pre-allocated packet buffer for FDir
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000047 * @pf: The pf pointer
48 * @add: True for add/update, False for remove
49 **/
Joseph Gasparakis17a73f62014-02-12 01:45:30 +000050int i40e_program_fdir_filter(struct i40e_fdir_filter *fdir_data, u8 *raw_packet,
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000051 struct i40e_pf *pf, bool add)
52{
53 struct i40e_filter_program_desc *fdir_desc;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +000054 struct i40e_tx_buffer *tx_buf, *first;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000055 struct i40e_tx_desc *tx_desc;
56 struct i40e_ring *tx_ring;
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000057 unsigned int fpt, dcc;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000058 struct i40e_vsi *vsi;
59 struct device *dev;
60 dma_addr_t dma;
61 u32 td_cmd = 0;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +000062 u16 delay = 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000063 u16 i;
64
65 /* find existing FDIR VSI */
66 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +000067 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000068 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR)
69 vsi = pf->vsi[i];
70 if (!vsi)
71 return -ENOENT;
72
Alexander Duyck9f65e15b2013-09-28 06:00:58 +000073 tx_ring = vsi->tx_rings[0];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000074 dev = tx_ring->dev;
75
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +000076 /* we need two descriptors to add/del a filter and we can wait */
77 do {
78 if (I40E_DESC_UNUSED(tx_ring) > 1)
79 break;
80 msleep_interruptible(1);
81 delay++;
82 } while (delay < I40E_FD_CLEAN_DELAY);
83
84 if (!(I40E_DESC_UNUSED(tx_ring) > 1))
85 return -EAGAIN;
86
Joseph Gasparakis17a73f62014-02-12 01:45:30 +000087 dma = dma_map_single(dev, raw_packet,
88 I40E_FDIR_MAX_RAW_PACKET_SIZE, DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000089 if (dma_mapping_error(dev, dma))
90 goto dma_fail;
91
92 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +000093 i = tx_ring->next_to_use;
94 fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +000095 first = &tx_ring->tx_bi[i];
96 memset(first, 0, sizeof(struct i40e_tx_buffer));
Alexander Duyckfc4ac672013-09-28 06:00:22 +000097
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +000098 tx_ring->next_to_use = ((i + 1) < tx_ring->count) ? i + 1 : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000099
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000100 fpt = (fdir_data->q_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
101 I40E_TXD_FLTR_QW0_QINDEX_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000102
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000103 fpt |= (fdir_data->flex_off << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT) &
104 I40E_TXD_FLTR_QW0_FLEXOFF_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000105
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000106 fpt |= (fdir_data->pctype << I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) &
107 I40E_TXD_FLTR_QW0_PCTYPE_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000108
109 /* Use LAN VSI Id if not programmed by user */
110 if (fdir_data->dest_vsi == 0)
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000111 fpt |= (pf->vsi[pf->lan_vsi]->id) <<
112 I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000113 else
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000114 fpt |= ((u32)fdir_data->dest_vsi <<
115 I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT) &
116 I40E_TXD_FLTR_QW0_DEST_VSI_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000117
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000118 dcc = I40E_TX_DESC_DTYPE_FILTER_PROG;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000119
120 if (add)
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000121 dcc |= I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
122 I40E_TXD_FLTR_QW1_PCMD_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000123 else
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000124 dcc |= I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
125 I40E_TXD_FLTR_QW1_PCMD_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000126
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000127 dcc |= (fdir_data->dest_ctl << I40E_TXD_FLTR_QW1_DEST_SHIFT) &
128 I40E_TXD_FLTR_QW1_DEST_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000129
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000130 dcc |= (fdir_data->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT) &
131 I40E_TXD_FLTR_QW1_FD_STATUS_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000132
133 if (fdir_data->cnt_index != 0) {
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000134 dcc |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
135 dcc |= ((u32)fdir_data->cnt_index <<
136 I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +0000137 I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000138 }
139
Jesse Brandeburg99753ea2014-06-04 04:22:49 +0000140 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(fpt);
141 fdir_desc->rsvd = cpu_to_le32(0);
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000142 fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dcc);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000143 fdir_desc->fd_id = cpu_to_le32(fdir_data->fd_id);
144
145 /* Now program a dummy descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000146 i = tx_ring->next_to_use;
147 tx_desc = I40E_TX_DESC(tx_ring, i);
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000148 tx_buf = &tx_ring->tx_bi[i];
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000149
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000150 tx_ring->next_to_use = ((i + 1) < tx_ring->count) ? i + 1 : 0;
151
152 memset(tx_buf, 0, sizeof(struct i40e_tx_buffer));
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000153
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000154 /* record length, and DMA address */
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000155 dma_unmap_len_set(tx_buf, len, I40E_FDIR_MAX_RAW_PACKET_SIZE);
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000156 dma_unmap_addr_set(tx_buf, dma, dma);
157
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000158 tx_desc->buffer_addr = cpu_to_le64(dma);
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000159 td_cmd = I40E_TXD_CMD | I40E_TX_DESC_CMD_DUMMY;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000160
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000161 tx_buf->tx_flags = I40E_TX_FLAGS_FD_SB;
162 tx_buf->raw_buf = (void *)raw_packet;
163
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000164 tx_desc->cmd_type_offset_bsz =
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000165 build_ctob(td_cmd, 0, I40E_FDIR_MAX_RAW_PACKET_SIZE, 0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000166
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000167 /* set the timestamp */
168 tx_buf->time_stamp = jiffies;
169
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000170 /* Force memory writes to complete before letting h/w
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000171 * know there are new descriptors to fetch.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000172 */
173 wmb();
174
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000175 /* Mark the data descriptor to be watched */
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000176 first->next_to_watch = tx_desc;
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000177
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000178 writel(tx_ring->next_to_use, tx_ring->tail);
179 return 0;
180
181dma_fail:
182 return -1;
183}
184
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000185#define IP_HEADER_OFFSET 14
186#define I40E_UDPIP_DUMMY_PACKET_LEN 42
187/**
188 * i40e_add_del_fdir_udpv4 - Add/Remove UDPv4 filters
189 * @vsi: pointer to the targeted VSI
190 * @fd_data: the flow director data required for the FDir descriptor
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000191 * @add: true adds a filter, false removes it
192 *
193 * Returns 0 if the filters were successfully added or removed
194 **/
195static int i40e_add_del_fdir_udpv4(struct i40e_vsi *vsi,
196 struct i40e_fdir_filter *fd_data,
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000197 bool add)
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000198{
199 struct i40e_pf *pf = vsi->back;
200 struct udphdr *udp;
201 struct iphdr *ip;
202 bool err = false;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000203 u8 *raw_packet;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000204 int ret;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000205 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
206 0x45, 0, 0, 0x1c, 0, 0, 0x40, 0, 0x40, 0x11, 0, 0, 0, 0, 0, 0,
207 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
208
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000209 raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
210 if (!raw_packet)
211 return -ENOMEM;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000212 memcpy(raw_packet, packet, I40E_UDPIP_DUMMY_PACKET_LEN);
213
214 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
215 udp = (struct udphdr *)(raw_packet + IP_HEADER_OFFSET
216 + sizeof(struct iphdr));
217
218 ip->daddr = fd_data->dst_ip[0];
219 udp->dest = fd_data->dst_port;
220 ip->saddr = fd_data->src_ip[0];
221 udp->source = fd_data->src_port;
222
Kevin Scottb2d36c02014-04-09 05:58:59 +0000223 fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
224 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
225 if (ret) {
226 dev_info(&pf->pdev->dev,
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000227 "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
228 fd_data->pctype, fd_data->fd_id, ret);
Kevin Scottb2d36c02014-04-09 05:58:59 +0000229 err = true;
230 } else {
231 dev_info(&pf->pdev->dev,
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000232 "PCTYPE:%d, Filter send OK for fd_id:%d\n",
233 fd_data->pctype, fd_data->fd_id);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000234 }
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000235 return err ? -EOPNOTSUPP : 0;
236}
237
238#define I40E_TCPIP_DUMMY_PACKET_LEN 54
239/**
240 * i40e_add_del_fdir_tcpv4 - Add/Remove TCPv4 filters
241 * @vsi: pointer to the targeted VSI
242 * @fd_data: the flow director data required for the FDir descriptor
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000243 * @add: true adds a filter, false removes it
244 *
245 * Returns 0 if the filters were successfully added or removed
246 **/
247static int i40e_add_del_fdir_tcpv4(struct i40e_vsi *vsi,
248 struct i40e_fdir_filter *fd_data,
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000249 bool add)
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000250{
251 struct i40e_pf *pf = vsi->back;
252 struct tcphdr *tcp;
253 struct iphdr *ip;
254 bool err = false;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000255 u8 *raw_packet;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000256 int ret;
257 /* Dummy packet */
258 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
259 0x45, 0, 0, 0x28, 0, 0, 0x40, 0, 0x40, 0x6, 0, 0, 0, 0, 0, 0,
260 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0x11,
261 0x0, 0x72, 0, 0, 0, 0};
262
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000263 raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
264 if (!raw_packet)
265 return -ENOMEM;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000266 memcpy(raw_packet, packet, I40E_TCPIP_DUMMY_PACKET_LEN);
267
268 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
269 tcp = (struct tcphdr *)(raw_packet + IP_HEADER_OFFSET
270 + sizeof(struct iphdr));
271
272 ip->daddr = fd_data->dst_ip[0];
273 tcp->dest = fd_data->dst_port;
274 ip->saddr = fd_data->src_ip[0];
275 tcp->source = fd_data->src_port;
276
277 if (add) {
278 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED) {
279 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 flow being applied\n");
280 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
281 }
282 }
283
Kevin Scottb2d36c02014-04-09 05:58:59 +0000284 fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000285 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
286
287 if (ret) {
288 dev_info(&pf->pdev->dev,
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000289 "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
290 fd_data->pctype, fd_data->fd_id, ret);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000291 err = true;
292 } else {
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000293 dev_info(&pf->pdev->dev,
294 "PCTYPE:%d, Filter send OK for fd_id:%d\n",
295 fd_data->pctype, fd_data->fd_id);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000296 }
297
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000298 return err ? -EOPNOTSUPP : 0;
299}
300
301/**
302 * i40e_add_del_fdir_sctpv4 - Add/Remove SCTPv4 Flow Director filters for
303 * a specific flow spec
304 * @vsi: pointer to the targeted VSI
305 * @fd_data: the flow director data required for the FDir descriptor
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000306 * @add: true adds a filter, false removes it
307 *
Jean Sacren21d3efd2014-03-17 18:14:39 +0000308 * Always returns -EOPNOTSUPP
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000309 **/
310static int i40e_add_del_fdir_sctpv4(struct i40e_vsi *vsi,
311 struct i40e_fdir_filter *fd_data,
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000312 bool add)
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000313{
314 return -EOPNOTSUPP;
315}
316
317#define I40E_IP_DUMMY_PACKET_LEN 34
318/**
319 * i40e_add_del_fdir_ipv4 - Add/Remove IPv4 Flow Director filters for
320 * a specific flow spec
321 * @vsi: pointer to the targeted VSI
322 * @fd_data: the flow director data required for the FDir descriptor
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000323 * @add: true adds a filter, false removes it
324 *
325 * Returns 0 if the filters were successfully added or removed
326 **/
327static int i40e_add_del_fdir_ipv4(struct i40e_vsi *vsi,
328 struct i40e_fdir_filter *fd_data,
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000329 bool add)
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000330{
331 struct i40e_pf *pf = vsi->back;
332 struct iphdr *ip;
333 bool err = false;
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000334 u8 *raw_packet;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000335 int ret;
336 int i;
337 static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
338 0x45, 0, 0, 0x14, 0, 0, 0x40, 0, 0x40, 0x10, 0, 0, 0, 0, 0, 0,
339 0, 0, 0, 0};
340
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000341 for (i = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
342 i <= I40E_FILTER_PCTYPE_FRAG_IPV4; i++) {
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000343 raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
344 if (!raw_packet)
345 return -ENOMEM;
346 memcpy(raw_packet, packet, I40E_IP_DUMMY_PACKET_LEN);
347 ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
348
349 ip->saddr = fd_data->src_ip[0];
350 ip->daddr = fd_data->dst_ip[0];
351 ip->protocol = 0;
352
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000353 fd_data->pctype = i;
354 ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
355
356 if (ret) {
357 dev_info(&pf->pdev->dev,
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000358 "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
359 fd_data->pctype, fd_data->fd_id, ret);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000360 err = true;
361 } else {
362 dev_info(&pf->pdev->dev,
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000363 "PCTYPE:%d, Filter send OK for fd_id:%d\n",
364 fd_data->pctype, fd_data->fd_id);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000365 }
366 }
367
368 return err ? -EOPNOTSUPP : 0;
369}
370
371/**
372 * i40e_add_del_fdir - Build raw packets to add/del fdir filter
373 * @vsi: pointer to the targeted VSI
374 * @cmd: command to get or set RX flow classification rules
375 * @add: true adds a filter, false removes it
376 *
377 **/
378int i40e_add_del_fdir(struct i40e_vsi *vsi,
379 struct i40e_fdir_filter *input, bool add)
380{
381 struct i40e_pf *pf = vsi->back;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000382 int ret;
383
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000384 switch (input->flow_type & ~FLOW_EXT) {
385 case TCP_V4_FLOW:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000386 ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000387 break;
388 case UDP_V4_FLOW:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000389 ret = i40e_add_del_fdir_udpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000390 break;
391 case SCTP_V4_FLOW:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000392 ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000393 break;
394 case IPV4_FLOW:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000395 ret = i40e_add_del_fdir_ipv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000396 break;
397 case IP_USER_FLOW:
398 switch (input->ip4_proto) {
399 case IPPROTO_TCP:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000400 ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000401 break;
402 case IPPROTO_UDP:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000403 ret = i40e_add_del_fdir_udpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000404 break;
405 case IPPROTO_SCTP:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000406 ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000407 break;
408 default:
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000409 ret = i40e_add_del_fdir_ipv4(vsi, input, add);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000410 break;
411 }
412 break;
413 default:
Jakub Kicinskic5ffe7e2014-04-02 10:33:22 +0000414 dev_info(&pf->pdev->dev, "Could not specify spec type %d\n",
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000415 input->flow_type);
416 ret = -EINVAL;
417 }
418
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000419 /* The buffer allocated here is freed by the i40e_clean_tx_ring() */
Joseph Gasparakis17a73f62014-02-12 01:45:30 +0000420 return ret;
421}
422
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000423/**
424 * i40e_fd_handle_status - check the Programming Status for FD
425 * @rx_ring: the Rx ring for this descriptor
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000426 * @rx_desc: the Rx descriptor for programming Status, not a packet descriptor.
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000427 * @prog_id: the id originally used for programming
428 *
429 * This is used to verify if the FD programming or invalidation
430 * requested by SW to the HW is successful or not and take actions accordingly.
431 **/
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000432static void i40e_fd_handle_status(struct i40e_ring *rx_ring,
433 union i40e_rx_desc *rx_desc, u8 prog_id)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000434{
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000435 struct i40e_pf *pf = rx_ring->vsi->back;
436 struct pci_dev *pdev = pf->pdev;
437 u32 fcnt_prog, fcnt_avail;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000438 u32 error;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000439 u64 qw;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000440
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000441 qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000442 error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
443 I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
444
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000445 if (error == (0x1 << I40E_RX_PROG_STATUS_DESC_FD_TBL_FULL_SHIFT)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000446
447 /* filter programming failed most likely due to table full */
Anjali Singhai Jain12957382014-06-04 04:22:47 +0000448 fcnt_prog = i40e_get_cur_guaranteed_fd_count(pf);
449 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000450 /* If ATR is running fcnt_prog can quickly change,
451 * if we are very close to full, it makes sense to disable
452 * FD ATR/SB and then re-enable it when there is room.
453 */
454 if (fcnt_prog >= (fcnt_avail - I40E_FDIR_BUFFER_FULL_MARGIN)) {
455 /* Turn off ATR first */
Anjali Singhai Jainb814ba62014-06-04 20:41:48 +0000456 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
457 !(pf->auto_disable_flags &
458 I40E_FLAG_FD_ATR_ENABLED)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000459 dev_warn(&pdev->dev, "FD filter space full, ATR for further flows will be turned off\n");
460 pf->auto_disable_flags |=
461 I40E_FLAG_FD_ATR_ENABLED;
462 pf->flags |= I40E_FLAG_FDIR_REQUIRES_REINIT;
Anjali Singhai Jainb814ba62014-06-04 20:41:48 +0000463 } else if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
464 !(pf->auto_disable_flags &
465 I40E_FLAG_FD_SB_ENABLED)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000466 dev_warn(&pdev->dev, "FD filter space full, new ntuple rules will not be added\n");
467 pf->auto_disable_flags |=
468 I40E_FLAG_FD_SB_ENABLED;
469 pf->flags |= I40E_FLAG_FDIR_REQUIRES_REINIT;
470 }
471 } else {
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000472 dev_info(&pdev->dev,
473 "FD filter programming failed due to incorrect filter parameters\n");
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000474 }
475 } else if (error ==
476 (0x1 << I40E_RX_PROG_STATUS_DESC_NO_FD_ENTRY_SHIFT)) {
Anjali Singhai Jain13c28842014-03-06 09:00:04 +0000477 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Carolyn Wybornye99bdd32014-07-09 07:46:12 +0000478 dev_info(&pdev->dev, "ntuple filter fd_id = %d, could not be removed\n",
Anjali Singhai Jain13c28842014-03-06 09:00:04 +0000479 rx_desc->wb.qword0.hi_dword.fd_id);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000480 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000481}
482
483/**
Alexander Duycka5e9c572013-09-28 06:00:27 +0000484 * i40e_unmap_and_free_tx_resource - Release a Tx buffer
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000485 * @ring: the ring that owns the buffer
486 * @tx_buffer: the buffer to free
487 **/
Alexander Duycka5e9c572013-09-28 06:00:27 +0000488static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
489 struct i40e_tx_buffer *tx_buffer)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000490{
Alexander Duycka5e9c572013-09-28 06:00:27 +0000491 if (tx_buffer->skb) {
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +0000492 if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
493 kfree(tx_buffer->raw_buf);
494 else
495 dev_kfree_skb_any(tx_buffer->skb);
496
Alexander Duycka5e9c572013-09-28 06:00:27 +0000497 if (dma_unmap_len(tx_buffer, len))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000498 dma_unmap_single(ring->dev,
Alexander Duyck35a1e2a2013-09-28 06:00:17 +0000499 dma_unmap_addr(tx_buffer, dma),
500 dma_unmap_len(tx_buffer, len),
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000501 DMA_TO_DEVICE);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000502 } else if (dma_unmap_len(tx_buffer, len)) {
503 dma_unmap_page(ring->dev,
504 dma_unmap_addr(tx_buffer, dma),
505 dma_unmap_len(tx_buffer, len),
506 DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000507 }
Alexander Duycka5e9c572013-09-28 06:00:27 +0000508 tx_buffer->next_to_watch = NULL;
509 tx_buffer->skb = NULL;
Alexander Duyck35a1e2a2013-09-28 06:00:17 +0000510 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000511 /* tx_buffer must be completely set up in the transmit path */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000512}
513
514/**
515 * i40e_clean_tx_ring - Free any empty Tx buffers
516 * @tx_ring: ring to be cleaned
517 **/
518void i40e_clean_tx_ring(struct i40e_ring *tx_ring)
519{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000520 unsigned long bi_size;
521 u16 i;
522
523 /* ring already cleared, nothing to do */
524 if (!tx_ring->tx_bi)
525 return;
526
527 /* Free all the Tx ring sk_buffs */
Alexander Duycka5e9c572013-09-28 06:00:27 +0000528 for (i = 0; i < tx_ring->count; i++)
529 i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000530
531 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
532 memset(tx_ring->tx_bi, 0, bi_size);
533
534 /* Zero out the descriptor ring */
535 memset(tx_ring->desc, 0, tx_ring->size);
536
537 tx_ring->next_to_use = 0;
538 tx_ring->next_to_clean = 0;
Alexander Duyck7070ce02013-09-28 06:00:37 +0000539
540 if (!tx_ring->netdev)
541 return;
542
543 /* cleanup Tx queue statistics */
544 netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
545 tx_ring->queue_index));
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000546}
547
548/**
549 * i40e_free_tx_resources - Free Tx resources per queue
550 * @tx_ring: Tx descriptor ring for a specific queue
551 *
552 * Free all transmit software resources
553 **/
554void i40e_free_tx_resources(struct i40e_ring *tx_ring)
555{
556 i40e_clean_tx_ring(tx_ring);
557 kfree(tx_ring->tx_bi);
558 tx_ring->tx_bi = NULL;
559
560 if (tx_ring->desc) {
561 dma_free_coherent(tx_ring->dev, tx_ring->size,
562 tx_ring->desc, tx_ring->dma);
563 tx_ring->desc = NULL;
564 }
565}
566
567/**
568 * i40e_get_tx_pending - how many tx descriptors not processed
569 * @tx_ring: the ring of descriptors
570 *
571 * Since there is no access to the ring head register
572 * in XL710, we need to use our local copies
573 **/
574static u32 i40e_get_tx_pending(struct i40e_ring *ring)
575{
576 u32 ntu = ((ring->next_to_clean <= ring->next_to_use)
577 ? ring->next_to_use
578 : ring->next_to_use + ring->count);
579 return ntu - ring->next_to_clean;
580}
581
582/**
583 * i40e_check_tx_hang - Is there a hang in the Tx queue
584 * @tx_ring: the ring of descriptors
585 **/
586static bool i40e_check_tx_hang(struct i40e_ring *tx_ring)
587{
588 u32 tx_pending = i40e_get_tx_pending(tx_ring);
589 bool ret = false;
590
591 clear_check_for_tx_hang(tx_ring);
592
593 /* Check for a hung queue, but be thorough. This verifies
594 * that a transmit has been completed since the previous
595 * check AND there is at least one packet pending. The
596 * ARMED bit is set to indicate a potential hang. The
597 * bit is cleared if a pause frame is received to remove
598 * false hang detection due to PFC or 802.3x frames. By
599 * requiring this to fail twice we avoid races with
600 * PFC clearing the ARMED bit and conditions where we
601 * run the check_tx_hang logic with a transmit completion
602 * pending but without time to complete it yet.
603 */
Alexander Duycka114d0a2013-09-28 06:00:43 +0000604 if ((tx_ring->tx_stats.tx_done_old == tx_ring->stats.packets) &&
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000605 tx_pending) {
606 /* make sure it is true for two checks in a row */
607 ret = test_and_set_bit(__I40E_HANG_CHECK_ARMED,
608 &tx_ring->state);
609 } else {
610 /* update completed stats and disarm the hang check */
Alexander Duycka114d0a2013-09-28 06:00:43 +0000611 tx_ring->tx_stats.tx_done_old = tx_ring->stats.packets;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000612 clear_bit(__I40E_HANG_CHECK_ARMED, &tx_ring->state);
613 }
614
615 return ret;
616}
617
618/**
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000619 * i40e_get_head - Retrieve head from head writeback
620 * @tx_ring: tx ring to fetch head of
621 *
622 * Returns value of Tx ring head based on value stored
623 * in head write-back location
624 **/
625static inline u32 i40e_get_head(struct i40e_ring *tx_ring)
626{
627 void *head = (struct i40e_tx_desc *)tx_ring->desc + tx_ring->count;
628
629 return le32_to_cpu(*(volatile __le32 *)head);
630}
631
632/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000633 * i40e_clean_tx_irq - Reclaim resources after transmit completes
634 * @tx_ring: tx ring to clean
635 * @budget: how many cleans we're allowed
636 *
637 * Returns true if there's any budget left (e.g. the clean is finished)
638 **/
639static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
640{
641 u16 i = tx_ring->next_to_clean;
642 struct i40e_tx_buffer *tx_buf;
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000643 struct i40e_tx_desc *tx_head;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000644 struct i40e_tx_desc *tx_desc;
645 unsigned int total_packets = 0;
646 unsigned int total_bytes = 0;
647
648 tx_buf = &tx_ring->tx_bi[i];
649 tx_desc = I40E_TX_DESC(tx_ring, i);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000650 i -= tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000651
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000652 tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
653
Alexander Duycka5e9c572013-09-28 06:00:27 +0000654 do {
655 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000656
657 /* if next_to_watch is not set then there is no work pending */
658 if (!eop_desc)
659 break;
660
Alexander Duycka5e9c572013-09-28 06:00:27 +0000661 /* prevent any other reads prior to eop_desc */
662 read_barrier_depends();
663
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +0000664 /* we have caught up to head, no work left to do */
665 if (tx_head == tx_desc)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000666 break;
667
Alexander Duyckc304fda2013-09-28 06:00:12 +0000668 /* clear next_to_watch to prevent false hangs */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000669 tx_buf->next_to_watch = NULL;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000670
Alexander Duycka5e9c572013-09-28 06:00:27 +0000671 /* update the statistics for this packet */
672 total_bytes += tx_buf->bytecount;
673 total_packets += tx_buf->gso_segs;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000674
Alexander Duycka5e9c572013-09-28 06:00:27 +0000675 /* free the skb */
676 dev_kfree_skb_any(tx_buf->skb);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000677
Alexander Duycka5e9c572013-09-28 06:00:27 +0000678 /* unmap skb header data */
679 dma_unmap_single(tx_ring->dev,
680 dma_unmap_addr(tx_buf, dma),
681 dma_unmap_len(tx_buf, len),
682 DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000683
Alexander Duycka5e9c572013-09-28 06:00:27 +0000684 /* clear tx_buffer data */
685 tx_buf->skb = NULL;
686 dma_unmap_len_set(tx_buf, len, 0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000687
Alexander Duycka5e9c572013-09-28 06:00:27 +0000688 /* unmap remaining buffers */
689 while (tx_desc != eop_desc) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000690
691 tx_buf++;
692 tx_desc++;
693 i++;
Alexander Duycka5e9c572013-09-28 06:00:27 +0000694 if (unlikely(!i)) {
695 i -= tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000696 tx_buf = tx_ring->tx_bi;
697 tx_desc = I40E_TX_DESC(tx_ring, 0);
698 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000699
Alexander Duycka5e9c572013-09-28 06:00:27 +0000700 /* unmap any remaining paged data */
701 if (dma_unmap_len(tx_buf, len)) {
702 dma_unmap_page(tx_ring->dev,
703 dma_unmap_addr(tx_buf, dma),
704 dma_unmap_len(tx_buf, len),
705 DMA_TO_DEVICE);
706 dma_unmap_len_set(tx_buf, len, 0);
707 }
708 }
709
710 /* move us one more past the eop_desc for start of next pkt */
711 tx_buf++;
712 tx_desc++;
713 i++;
714 if (unlikely(!i)) {
715 i -= tx_ring->count;
716 tx_buf = tx_ring->tx_bi;
717 tx_desc = I40E_TX_DESC(tx_ring, 0);
718 }
719
720 /* update budget accounting */
721 budget--;
722 } while (likely(budget));
723
724 i += tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000725 tx_ring->next_to_clean = i;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000726 u64_stats_update_begin(&tx_ring->syncp);
Alexander Duycka114d0a2013-09-28 06:00:43 +0000727 tx_ring->stats.bytes += total_bytes;
728 tx_ring->stats.packets += total_packets;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000729 u64_stats_update_end(&tx_ring->syncp);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000730 tx_ring->q_vector->tx.total_bytes += total_bytes;
731 tx_ring->q_vector->tx.total_packets += total_packets;
Alexander Duycka5e9c572013-09-28 06:00:27 +0000732
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000733 if (check_for_tx_hang(tx_ring) && i40e_check_tx_hang(tx_ring)) {
734 /* schedule immediate reset if we believe we hung */
735 dev_info(tx_ring->dev, "Detected Tx Unit Hang\n"
736 " VSI <%d>\n"
737 " Tx Queue <%d>\n"
738 " next_to_use <%x>\n"
739 " next_to_clean <%x>\n",
740 tx_ring->vsi->seid,
741 tx_ring->queue_index,
742 tx_ring->next_to_use, i);
743 dev_info(tx_ring->dev, "tx_bi[next_to_clean]\n"
744 " time_stamp <%lx>\n"
745 " jiffies <%lx>\n",
746 tx_ring->tx_bi[i].time_stamp, jiffies);
747
748 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
749
750 dev_info(tx_ring->dev,
751 "tx hang detected on queue %d, resetting adapter\n",
752 tx_ring->queue_index);
753
754 tx_ring->netdev->netdev_ops->ndo_tx_timeout(tx_ring->netdev);
755
756 /* the adapter is about to reset, no point in enabling stuff */
757 return true;
758 }
759
Alexander Duyck7070ce02013-09-28 06:00:37 +0000760 netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
761 tx_ring->queue_index),
762 total_packets, total_bytes);
763
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000764#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
765 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
766 (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
767 /* Make sure that anybody stopping the queue after this
768 * sees the new next_to_clean.
769 */
770 smp_mb();
771 if (__netif_subqueue_stopped(tx_ring->netdev,
772 tx_ring->queue_index) &&
773 !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
774 netif_wake_subqueue(tx_ring->netdev,
775 tx_ring->queue_index);
776 ++tx_ring->tx_stats.restart_queue;
777 }
778 }
779
780 return budget > 0;
781}
782
783/**
784 * i40e_set_new_dynamic_itr - Find new ITR level
785 * @rc: structure containing ring performance data
786 *
787 * Stores a new ITR value based on packets and byte counts during
788 * the last interrupt. The advantage of per interrupt computation
789 * is faster updates and more accurate ITR for the current traffic
790 * pattern. Constants in this function were computed based on
791 * theoretical maximum wire speed and thresholds were set based on
792 * testing data as well as attempting to minimize response time
793 * while increasing bulk throughput.
794 **/
795static void i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
796{
797 enum i40e_latency_range new_latency_range = rc->latency_range;
798 u32 new_itr = rc->itr;
799 int bytes_per_int;
800
801 if (rc->total_packets == 0 || !rc->itr)
802 return;
803
804 /* simple throttlerate management
805 * 0-10MB/s lowest (100000 ints/s)
806 * 10-20MB/s low (20000 ints/s)
807 * 20-1249MB/s bulk (8000 ints/s)
808 */
809 bytes_per_int = rc->total_bytes / rc->itr;
810 switch (rc->itr) {
811 case I40E_LOWEST_LATENCY:
812 if (bytes_per_int > 10)
813 new_latency_range = I40E_LOW_LATENCY;
814 break;
815 case I40E_LOW_LATENCY:
816 if (bytes_per_int > 20)
817 new_latency_range = I40E_BULK_LATENCY;
818 else if (bytes_per_int <= 10)
819 new_latency_range = I40E_LOWEST_LATENCY;
820 break;
821 case I40E_BULK_LATENCY:
822 if (bytes_per_int <= 20)
823 rc->latency_range = I40E_LOW_LATENCY;
824 break;
825 }
826
827 switch (new_latency_range) {
828 case I40E_LOWEST_LATENCY:
829 new_itr = I40E_ITR_100K;
830 break;
831 case I40E_LOW_LATENCY:
832 new_itr = I40E_ITR_20K;
833 break;
834 case I40E_BULK_LATENCY:
835 new_itr = I40E_ITR_8K;
836 break;
837 default:
838 break;
839 }
840
841 if (new_itr != rc->itr) {
842 /* do an exponential smoothing */
843 new_itr = (10 * new_itr * rc->itr) /
844 ((9 * new_itr) + rc->itr);
845 rc->itr = new_itr & I40E_MAX_ITR;
846 }
847
848 rc->total_bytes = 0;
849 rc->total_packets = 0;
850}
851
852/**
853 * i40e_update_dynamic_itr - Adjust ITR based on bytes per int
854 * @q_vector: the vector to adjust
855 **/
856static void i40e_update_dynamic_itr(struct i40e_q_vector *q_vector)
857{
858 u16 vector = q_vector->vsi->base_vector + q_vector->v_idx;
859 struct i40e_hw *hw = &q_vector->vsi->back->hw;
860 u32 reg_addr;
861 u16 old_itr;
862
863 reg_addr = I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1);
864 old_itr = q_vector->rx.itr;
865 i40e_set_new_dynamic_itr(&q_vector->rx);
866 if (old_itr != q_vector->rx.itr)
867 wr32(hw, reg_addr, q_vector->rx.itr);
868
869 reg_addr = I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1);
870 old_itr = q_vector->tx.itr;
871 i40e_set_new_dynamic_itr(&q_vector->tx);
872 if (old_itr != q_vector->tx.itr)
873 wr32(hw, reg_addr, q_vector->tx.itr);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000874}
875
876/**
877 * i40e_clean_programming_status - clean the programming status descriptor
878 * @rx_ring: the rx ring that has this descriptor
879 * @rx_desc: the rx descriptor written back by HW
880 *
881 * Flow director should handle FD_FILTER_STATUS to check its filter programming
882 * status being successful or not and take actions accordingly. FCoE should
883 * handle its context/filter programming/invalidation status and take actions.
884 *
885 **/
886static void i40e_clean_programming_status(struct i40e_ring *rx_ring,
887 union i40e_rx_desc *rx_desc)
888{
889 u64 qw;
890 u8 id;
891
892 qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
893 id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
894 I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
895
896 if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +0000897 i40e_fd_handle_status(rx_ring, rx_desc, id);
Vasu Dev38e00432014-08-01 13:27:03 -0700898#ifdef I40E_FCOE
899 else if ((id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_PROG_STATUS) ||
900 (id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_INVL_STATUS))
901 i40e_fcoe_handle_status(rx_ring, rx_desc, id);
902#endif
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{
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001203 struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(rx_ptype);
1204 bool ipv4 = false, ipv6 = false;
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001205 bool ipv4_tunnel, ipv6_tunnel;
1206 __wsum rx_udp_csum;
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001207 struct iphdr *iph;
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001208 __sum16 csum;
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001209
1210 ipv4_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT4_MAC_PAY3) &&
1211 (rx_ptype < I40E_RX_PTYPE_GRENAT4_MACVLAN_IPV6_ICMP_PAY4);
1212 ipv6_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT6_MAC_PAY3) &&
1213 (rx_ptype < I40E_RX_PTYPE_GRENAT6_MACVLAN_IPV6_ICMP_PAY4);
1214
1215 skb->encapsulation = ipv4_tunnel || ipv6_tunnel;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001216 skb->ip_summed = CHECKSUM_NONE;
1217
1218 /* Rx csum enabled and ip headers found? */
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001219 if (!(vsi->netdev->features & NETIF_F_RXCSUM))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001220 return;
1221
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001222 /* did the hardware decode the packet and checksum? */
1223 if (!(rx_status & (1 << I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
1224 return;
1225
1226 /* both known and outer_ip must be set for the below code to work */
1227 if (!(decoded.known && decoded.outer_ip))
1228 return;
1229
1230 if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1231 decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4)
1232 ipv4 = true;
1233 else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1234 decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6)
1235 ipv6 = true;
1236
1237 if (ipv4 &&
1238 (rx_error & ((1 << I40E_RX_DESC_ERROR_IPE_SHIFT) |
1239 (1 << I40E_RX_DESC_ERROR_EIPE_SHIFT))))
1240 goto checksum_fail;
1241
Jesse Brandeburgddf1d0d2014-02-13 03:48:39 -08001242 /* likely incorrect csum if alternate IP extension headers found */
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001243 if (ipv6 &&
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001244 rx_status & (1 << I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
1245 /* don't increment checksum err here, non-fatal err */
Shannon Nelson8ee75a82013-12-21 05:44:46 +00001246 return;
1247
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001248 /* there was some L4 error, count error and punt packet to the stack */
1249 if (rx_error & (1 << I40E_RX_DESC_ERROR_L4E_SHIFT))
1250 goto checksum_fail;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001251
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001252 /* handle packets that were not able to be checksummed due
1253 * to arrival speed, in this case the stack can compute
1254 * the csum.
1255 */
1256 if (rx_error & (1 << I40E_RX_DESC_ERROR_PPRS_SHIFT))
1257 return;
1258
1259 /* If VXLAN traffic has an outer UDPv4 checksum we need to check
1260 * it in the driver, hardware does not do it for us.
1261 * Since L3L4P bit was set we assume a valid IHL value (>=5)
1262 * so the total length of IPv4 header is IHL*4 bytes
1263 * The UDP_0 bit *may* bet set if the *inner* header is UDP
1264 */
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001265 if (ipv4_tunnel &&
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001266 (decoded.inner_prot != I40E_RX_PTYPE_INNER_PROT_UDP) &&
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001267 !(rx_status & (1 << I40E_RX_DESC_STATUS_UDP_0_SHIFT))) {
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001268 skb->transport_header = skb->mac_header +
1269 sizeof(struct ethhdr) +
1270 (ip_hdr(skb)->ihl * 4);
1271
1272 /* Add 4 bytes for VLAN tagged packets */
1273 skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
1274 skb->protocol == htons(ETH_P_8021AD))
1275 ? VLAN_HLEN : 0;
1276
1277 rx_udp_csum = udp_csum(skb);
1278 iph = ip_hdr(skb);
1279 csum = csum_tcpudp_magic(
1280 iph->saddr, iph->daddr,
1281 (skb->len - skb_transport_offset(skb)),
1282 IPPROTO_UDP, rx_udp_csum);
1283
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001284 if (udp_hdr(skb)->check != csum)
1285 goto checksum_fail;
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001286 }
1287
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001288 skb->ip_summed = CHECKSUM_UNNECESSARY;
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001289
1290 return;
1291
1292checksum_fail:
1293 vsi->back->hw_csum_rx_error++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001294}
1295
1296/**
1297 * i40e_rx_hash - returns the hash value from the Rx descriptor
1298 * @ring: descriptor ring
1299 * @rx_desc: specific descriptor
1300 **/
1301static inline u32 i40e_rx_hash(struct i40e_ring *ring,
1302 union i40e_rx_desc *rx_desc)
1303{
Jesse Brandeburg8a494922013-11-20 10:02:49 +00001304 const __le64 rss_mask =
1305 cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
1306 I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
1307
1308 if ((ring->netdev->features & NETIF_F_RXHASH) &&
1309 (rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask)
1310 return le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
1311 else
1312 return 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001313}
1314
1315/**
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001316 * i40e_ptype_to_hash - get a hash type
1317 * @ptype: the ptype value from the descriptor
1318 *
1319 * Returns a hash type to be used by skb_set_hash
1320 **/
1321static inline enum pkt_hash_types i40e_ptype_to_hash(u8 ptype)
1322{
1323 struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
1324
1325 if (!decoded.known)
1326 return PKT_HASH_TYPE_NONE;
1327
1328 if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1329 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
1330 return PKT_HASH_TYPE_L4;
1331 else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
1332 decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
1333 return PKT_HASH_TYPE_L3;
1334 else
1335 return PKT_HASH_TYPE_L2;
1336}
1337
1338/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001339 * i40e_clean_rx_irq - Reclaim resources after receive completes
1340 * @rx_ring: rx ring to clean
1341 * @budget: how many cleans we're allowed
1342 *
1343 * Returns true if there's any budget left (e.g. the clean is finished)
1344 **/
1345static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
1346{
1347 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1348 u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
1349 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
1350 const int current_node = numa_node_id();
1351 struct i40e_vsi *vsi = rx_ring->vsi;
1352 u16 i = rx_ring->next_to_clean;
1353 union i40e_rx_desc *rx_desc;
1354 u32 rx_error, rx_status;
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001355 u8 rx_ptype;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001356 u64 qword;
1357
Eric W. Biederman390f86d2014-03-14 17:59:10 -07001358 if (budget <= 0)
1359 return 0;
1360
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001361 rx_desc = I40E_RX_DESC(rx_ring, i);
1362 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Jesse Brandeburg6838b532014-01-14 00:49:52 -08001363 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1364 I40E_RXD_QW1_STATUS_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001365
1366 while (rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)) {
1367 union i40e_rx_desc *next_rxd;
1368 struct i40e_rx_buffer *rx_bi;
1369 struct sk_buff *skb;
1370 u16 vlan_tag;
1371 if (i40e_rx_is_programming_status(qword)) {
1372 i40e_clean_programming_status(rx_ring, rx_desc);
1373 I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
1374 goto next_desc;
1375 }
1376 rx_bi = &rx_ring->rx_bi[i];
1377 skb = rx_bi->skb;
1378 prefetch(skb->data);
1379
Mitch Williams829af3a2013-12-18 13:46:00 +00001380 rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
1381 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
1382 rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
1383 I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
1384 rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
1385 I40E_RXD_QW1_LENGTH_SPH_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001386
Mitch Williams829af3a2013-12-18 13:46:00 +00001387 rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
1388 I40E_RXD_QW1_ERROR_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001389 rx_hbo = rx_error & (1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
1390 rx_error &= ~(1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
1391
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001392 rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
1393 I40E_RXD_QW1_PTYPE_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001394 rx_bi->skb = NULL;
1395
1396 /* This memory barrier is needed to keep us from reading
1397 * any other fields out of the rx_desc until we know the
1398 * STATUS_DD bit is set
1399 */
1400 rmb();
1401
1402 /* Get the header and possibly the whole packet
1403 * If this is an skb from previous receive dma will be 0
1404 */
1405 if (rx_bi->dma) {
1406 u16 len;
1407
1408 if (rx_hbo)
1409 len = I40E_RX_HDR_SIZE;
1410 else if (rx_sph)
1411 len = rx_header_len;
1412 else if (rx_packet_len)
1413 len = rx_packet_len; /* 1buf/no split found */
1414 else
1415 len = rx_header_len; /* split always mode */
1416
1417 skb_put(skb, len);
1418 dma_unmap_single(rx_ring->dev,
1419 rx_bi->dma,
1420 rx_ring->rx_buf_len,
1421 DMA_FROM_DEVICE);
1422 rx_bi->dma = 0;
1423 }
1424
1425 /* Get the rest of the data if this was a header split */
1426 if (ring_is_ps_enabled(rx_ring) && rx_packet_len) {
1427
1428 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
1429 rx_bi->page,
1430 rx_bi->page_offset,
1431 rx_packet_len);
1432
1433 skb->len += rx_packet_len;
1434 skb->data_len += rx_packet_len;
1435 skb->truesize += rx_packet_len;
1436
1437 if ((page_count(rx_bi->page) == 1) &&
1438 (page_to_nid(rx_bi->page) == current_node))
1439 get_page(rx_bi->page);
1440 else
1441 rx_bi->page = NULL;
1442
1443 dma_unmap_page(rx_ring->dev,
1444 rx_bi->page_dma,
1445 PAGE_SIZE / 2,
1446 DMA_FROM_DEVICE);
1447 rx_bi->page_dma = 0;
1448 }
1449 I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
1450
1451 if (unlikely(
1452 !(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
1453 struct i40e_rx_buffer *next_buffer;
1454
1455 next_buffer = &rx_ring->rx_bi[i];
1456
1457 if (ring_is_ps_enabled(rx_ring)) {
1458 rx_bi->skb = next_buffer->skb;
1459 rx_bi->dma = next_buffer->dma;
1460 next_buffer->skb = skb;
1461 next_buffer->dma = 0;
1462 }
1463 rx_ring->rx_stats.non_eop_descs++;
1464 goto next_desc;
1465 }
1466
1467 /* ERR_MASK will only have valid bits if EOP set */
1468 if (unlikely(rx_error & (1 << I40E_RX_DESC_ERROR_RXE_SHIFT))) {
1469 dev_kfree_skb_any(skb);
Jesse Brandeburg8a3c91c2014-05-20 08:01:43 +00001470 /* TODO: shouldn't we increment a counter indicating the
1471 * drop?
1472 */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001473 goto next_desc;
1474 }
1475
Jesse Brandeburg206812b2014-02-12 01:45:33 +00001476 skb_set_hash(skb, i40e_rx_hash(rx_ring, rx_desc),
1477 i40e_ptype_to_hash(rx_ptype));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001478 if (unlikely(rx_status & I40E_RXD_QW1_STATUS_TSYNVALID_MASK)) {
1479 i40e_ptp_rx_hwtstamp(vsi->back, skb, (rx_status &
1480 I40E_RXD_QW1_STATUS_TSYNINDX_MASK) >>
1481 I40E_RXD_QW1_STATUS_TSYNINDX_SHIFT);
1482 rx_ring->last_rx_timestamp = jiffies;
1483 }
1484
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001485 /* probably a little skewed due to removing CRC */
1486 total_rx_bytes += skb->len;
1487 total_rx_packets++;
1488
1489 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001490
1491 i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
1492
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001493 vlan_tag = rx_status & (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
1494 ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
1495 : 0;
Vasu Dev38e00432014-08-01 13:27:03 -07001496#ifdef I40E_FCOE
1497 if (!i40e_fcoe_handle_offload(rx_ring, rx_desc, skb)) {
1498 dev_kfree_skb_any(skb);
1499 goto next_desc;
1500 }
1501#endif
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001502 i40e_receive_skb(rx_ring, skb, vlan_tag);
1503
1504 rx_ring->netdev->last_rx = jiffies;
1505 budget--;
1506next_desc:
1507 rx_desc->wb.qword1.status_error_len = 0;
1508 if (!budget)
1509 break;
1510
1511 cleaned_count++;
1512 /* return some buffers to hardware, one at a time is too slow */
1513 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
1514 i40e_alloc_rx_buffers(rx_ring, cleaned_count);
1515 cleaned_count = 0;
1516 }
1517
1518 /* use prefetched values */
1519 rx_desc = next_rxd;
1520 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Mitch Williams829af3a2013-12-18 13:46:00 +00001521 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1522 I40E_RXD_QW1_STATUS_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001523 }
1524
1525 rx_ring->next_to_clean = i;
Alexander Duyck980e9b12013-09-28 06:01:03 +00001526 u64_stats_update_begin(&rx_ring->syncp);
Alexander Duycka114d0a2013-09-28 06:00:43 +00001527 rx_ring->stats.packets += total_rx_packets;
1528 rx_ring->stats.bytes += total_rx_bytes;
Alexander Duyck980e9b12013-09-28 06:01:03 +00001529 u64_stats_update_end(&rx_ring->syncp);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001530 rx_ring->q_vector->rx.total_packets += total_rx_packets;
1531 rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
1532
1533 if (cleaned_count)
1534 i40e_alloc_rx_buffers(rx_ring, cleaned_count);
1535
1536 return budget > 0;
1537}
1538
1539/**
1540 * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
1541 * @napi: napi struct with our devices info in it
1542 * @budget: amount of work driver is allowed to do this pass, in packets
1543 *
1544 * This function will clean all queues associated with a q_vector.
1545 *
1546 * Returns the amount of work done
1547 **/
1548int i40e_napi_poll(struct napi_struct *napi, int budget)
1549{
1550 struct i40e_q_vector *q_vector =
1551 container_of(napi, struct i40e_q_vector, napi);
1552 struct i40e_vsi *vsi = q_vector->vsi;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001553 struct i40e_ring *ring;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001554 bool clean_complete = true;
1555 int budget_per_ring;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001556
1557 if (test_bit(__I40E_DOWN, &vsi->state)) {
1558 napi_complete(napi);
1559 return 0;
1560 }
1561
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001562 /* Since the actual Tx work is minimal, we can give the Tx a larger
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001563 * budget and be more aggressive about cleaning up the Tx descriptors.
1564 */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001565 i40e_for_each_ring(ring, q_vector->tx)
1566 clean_complete &= i40e_clean_tx_irq(ring, vsi->work_limit);
1567
1568 /* We attempt to distribute budget to each Rx queue fairly, but don't
1569 * allow the budget to go below 1 because that would exit polling early.
1570 */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001571 budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001572
1573 i40e_for_each_ring(ring, q_vector->rx)
1574 clean_complete &= i40e_clean_rx_irq(ring, budget_per_ring);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001575
1576 /* If work not completed, return budget and polling will return */
1577 if (!clean_complete)
1578 return budget;
1579
1580 /* Work is done so exit the polling mode and re-enable the interrupt */
1581 napi_complete(napi);
1582 if (ITR_IS_DYNAMIC(vsi->rx_itr_setting) ||
1583 ITR_IS_DYNAMIC(vsi->tx_itr_setting))
1584 i40e_update_dynamic_itr(q_vector);
1585
1586 if (!test_bit(__I40E_DOWN, &vsi->state)) {
1587 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
1588 i40e_irq_dynamic_enable(vsi,
1589 q_vector->v_idx + vsi->base_vector);
1590 } else {
1591 struct i40e_hw *hw = &vsi->back->hw;
1592 /* We re-enable the queue 0 cause, but
1593 * don't worry about dynamic_enable
1594 * because we left it on for the other
1595 * possible interrupts during napi
1596 */
1597 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
1598 qval |= I40E_QINT_RQCTL_CAUSE_ENA_MASK;
1599 wr32(hw, I40E_QINT_RQCTL(0), qval);
1600
1601 qval = rd32(hw, I40E_QINT_TQCTL(0));
1602 qval |= I40E_QINT_TQCTL_CAUSE_ENA_MASK;
1603 wr32(hw, I40E_QINT_TQCTL(0), qval);
Shannon Nelson116a57d2013-09-28 07:13:59 +00001604
1605 i40e_irq_dynamic_enable_icr0(vsi->back);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001606 }
1607 }
1608
1609 return 0;
1610}
1611
1612/**
1613 * i40e_atr - Add a Flow Director ATR filter
1614 * @tx_ring: ring to add programming descriptor to
1615 * @skb: send buffer
1616 * @flags: send flags
1617 * @protocol: wire protocol
1618 **/
1619static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
1620 u32 flags, __be16 protocol)
1621{
1622 struct i40e_filter_program_desc *fdir_desc;
1623 struct i40e_pf *pf = tx_ring->vsi->back;
1624 union {
1625 unsigned char *network;
1626 struct iphdr *ipv4;
1627 struct ipv6hdr *ipv6;
1628 } hdr;
1629 struct tcphdr *th;
1630 unsigned int hlen;
1631 u32 flex_ptype, dtype_cmd;
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001632 u16 i;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001633
1634 /* make sure ATR is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08001635 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001636 return;
1637
1638 /* if sampling is disabled do nothing */
1639 if (!tx_ring->atr_sample_rate)
1640 return;
1641
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001642 /* snag network header to get L4 type and address */
1643 hdr.network = skb_network_header(skb);
1644
1645 /* Currently only IPv4/IPv6 with TCP is supported */
1646 if (protocol == htons(ETH_P_IP)) {
1647 if (hdr.ipv4->protocol != IPPROTO_TCP)
1648 return;
1649
1650 /* access ihl as a u8 to avoid unaligned access on ia64 */
1651 hlen = (hdr.network[0] & 0x0F) << 2;
1652 } else if (protocol == htons(ETH_P_IPV6)) {
1653 if (hdr.ipv6->nexthdr != IPPROTO_TCP)
1654 return;
1655
1656 hlen = sizeof(struct ipv6hdr);
1657 } else {
1658 return;
1659 }
1660
1661 th = (struct tcphdr *)(hdr.network + hlen);
1662
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00001663 /* Due to lack of space, no more new filters can be programmed */
1664 if (th->syn && (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1665 return;
1666
1667 tx_ring->atr_count++;
1668
Anjali Singhai Jaince806782014-03-06 08:59:54 +00001669 /* sample on all syn/fin/rst packets or once every atr sample rate */
1670 if (!th->fin &&
1671 !th->syn &&
1672 !th->rst &&
1673 (tx_ring->atr_count < tx_ring->atr_sample_rate))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001674 return;
1675
1676 tx_ring->atr_count = 0;
1677
1678 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001679 i = tx_ring->next_to_use;
1680 fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
1681
1682 i++;
1683 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001684
1685 flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
1686 I40E_TXD_FLTR_QW0_QINDEX_MASK;
1687 flex_ptype |= (protocol == htons(ETH_P_IP)) ?
1688 (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
1689 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
1690 (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
1691 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
1692
1693 flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
1694
1695 dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
1696
Anjali Singhai Jaince806782014-03-06 08:59:54 +00001697 dtype_cmd |= (th->fin || th->rst) ?
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001698 (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
1699 I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
1700 (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
1701 I40E_TXD_FLTR_QW1_PCMD_SHIFT);
1702
1703 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
1704 I40E_TXD_FLTR_QW1_DEST_SHIFT;
1705
1706 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
1707 I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
1708
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001709 dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
1710 dtype_cmd |=
1711 ((u32)pf->fd_atr_cnt_idx << I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
1712 I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
1713
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001714 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
Jesse Brandeburg99753ea2014-06-04 04:22:49 +00001715 fdir_desc->rsvd = cpu_to_le32(0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001716 fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
Jesse Brandeburg99753ea2014-06-04 04:22:49 +00001717 fdir_desc->fd_id = cpu_to_le32(0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001718}
1719
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001720/**
1721 * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
1722 * @skb: send buffer
1723 * @tx_ring: ring to send buffer on
1724 * @flags: the tx flags to be set
1725 *
1726 * Checks the skb and set up correspondingly several generic transmit flags
1727 * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
1728 *
1729 * Returns error code indicate the frame should be dropped upon error and the
1730 * otherwise returns 0 to indicate the flags has been set properly.
1731 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001732#ifdef I40E_FCOE
1733int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
1734 struct i40e_ring *tx_ring,
1735 u32 *flags)
1736#else
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001737static int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
1738 struct i40e_ring *tx_ring,
1739 u32 *flags)
Vasu Dev38e00432014-08-01 13:27:03 -07001740#endif
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001741{
1742 __be16 protocol = skb->protocol;
1743 u32 tx_flags = 0;
1744
1745 /* if we have a HW VLAN tag being added, default to the HW one */
1746 if (vlan_tx_tag_present(skb)) {
1747 tx_flags |= vlan_tx_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
1748 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1749 /* else if it is a SW VLAN, check the next protocol and store the tag */
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00001750 } else if (protocol == htons(ETH_P_8021Q)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001751 struct vlan_hdr *vhdr, _vhdr;
1752 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
1753 if (!vhdr)
1754 return -EINVAL;
1755
1756 protocol = vhdr->h_vlan_encapsulated_proto;
1757 tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
1758 tx_flags |= I40E_TX_FLAGS_SW_VLAN;
1759 }
1760
1761 /* Insert 802.1p priority into VLAN header */
Vasu Dev38e00432014-08-01 13:27:03 -07001762 if ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
1763 (skb->priority != TC_PRIO_CONTROL)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001764 tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
1765 tx_flags |= (skb->priority & 0x7) <<
1766 I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
1767 if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
1768 struct vlan_ethhdr *vhdr;
Francois Romieudd225bc2014-03-30 03:14:48 +00001769 int rc;
1770
1771 rc = skb_cow_head(skb, 0);
1772 if (rc < 0)
1773 return rc;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001774 vhdr = (struct vlan_ethhdr *)skb->data;
1775 vhdr->h_vlan_TCI = htons(tx_flags >>
1776 I40E_TX_FLAGS_VLAN_SHIFT);
1777 } else {
1778 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1779 }
1780 }
1781 *flags = tx_flags;
1782 return 0;
1783}
1784
1785/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001786 * i40e_tso - set up the tso context descriptor
1787 * @tx_ring: ptr to the ring to send
1788 * @skb: ptr to the skb we're sending
1789 * @tx_flags: the collected send information
1790 * @protocol: the send protocol
1791 * @hdr_len: ptr to the size of the packet header
1792 * @cd_tunneling: ptr to context descriptor bits
1793 *
1794 * Returns 0 if no TSO can happen, 1 if tso is going, or error
1795 **/
1796static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
1797 u32 tx_flags, __be16 protocol, u8 *hdr_len,
1798 u64 *cd_type_cmd_tso_mss, u32 *cd_tunneling)
1799{
1800 u32 cd_cmd, cd_tso_len, cd_mss;
Francois Romieudd225bc2014-03-30 03:14:48 +00001801 struct ipv6hdr *ipv6h;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001802 struct tcphdr *tcph;
1803 struct iphdr *iph;
1804 u32 l4len;
1805 int err;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001806
1807 if (!skb_is_gso(skb))
1808 return 0;
1809
Francois Romieudd225bc2014-03-30 03:14:48 +00001810 err = skb_cow_head(skb, 0);
1811 if (err < 0)
1812 return err;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001813
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00001814 if (protocol == htons(ETH_P_IP)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001815 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
1816 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1817 iph->tot_len = 0;
1818 iph->check = 0;
1819 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1820 0, IPPROTO_TCP, 0);
1821 } else if (skb_is_gso_v6(skb)) {
1822
1823 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb)
1824 : ipv6_hdr(skb);
1825 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1826 ipv6h->payload_len = 0;
1827 tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
1828 0, IPPROTO_TCP, 0);
1829 }
1830
1831 l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
1832 *hdr_len = (skb->encapsulation
1833 ? (skb_inner_transport_header(skb) - skb->data)
1834 : skb_transport_offset(skb)) + l4len;
1835
1836 /* find the field values */
1837 cd_cmd = I40E_TX_CTX_DESC_TSO;
1838 cd_tso_len = skb->len - *hdr_len;
1839 cd_mss = skb_shinfo(skb)->gso_size;
Mitch Williams829af3a2013-12-18 13:46:00 +00001840 *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
1841 ((u64)cd_tso_len <<
1842 I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
1843 ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001844 return 1;
1845}
1846
1847/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001848 * i40e_tsyn - set up the tsyn context descriptor
1849 * @tx_ring: ptr to the ring to send
1850 * @skb: ptr to the skb we're sending
1851 * @tx_flags: the collected send information
1852 *
1853 * Returns 0 if no Tx timestamp can happen and 1 if the timestamp will happen
1854 **/
1855static int i40e_tsyn(struct i40e_ring *tx_ring, struct sk_buff *skb,
1856 u32 tx_flags, u64 *cd_type_cmd_tso_mss)
1857{
1858 struct i40e_pf *pf;
1859
1860 if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
1861 return 0;
1862
1863 /* Tx timestamps cannot be sampled when doing TSO */
1864 if (tx_flags & I40E_TX_FLAGS_TSO)
1865 return 0;
1866
1867 /* only timestamp the outbound packet if the user has requested it and
1868 * we are not already transmitting a packet to be timestamped
1869 */
1870 pf = i40e_netdev_to_pf(tx_ring->netdev);
Jakub Kicinski9ce34f02014-03-15 14:55:42 +00001871 if (pf->ptp_tx &&
1872 !test_and_set_bit_lock(__I40E_PTP_TX_IN_PROGRESS, &pf->state)) {
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001873 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1874 pf->ptp_tx_skb = skb_get(skb);
1875 } else {
1876 return 0;
1877 }
1878
1879 *cd_type_cmd_tso_mss |= (u64)I40E_TX_CTX_DESC_TSYN <<
1880 I40E_TXD_CTX_QW1_CMD_SHIFT;
1881
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001882 return 1;
1883}
1884
1885/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001886 * i40e_tx_enable_csum - Enable Tx checksum offloads
1887 * @skb: send buffer
1888 * @tx_flags: Tx flags currently set
1889 * @td_cmd: Tx descriptor command bits to set
1890 * @td_offset: Tx descriptor header offsets to set
1891 * @cd_tunneling: ptr to context desc bits
1892 **/
1893static void i40e_tx_enable_csum(struct sk_buff *skb, u32 tx_flags,
1894 u32 *td_cmd, u32 *td_offset,
1895 struct i40e_ring *tx_ring,
1896 u32 *cd_tunneling)
1897{
1898 struct ipv6hdr *this_ipv6_hdr;
1899 unsigned int this_tcp_hdrlen;
1900 struct iphdr *this_ip_hdr;
1901 u32 network_hdr_len;
1902 u8 l4_hdr = 0;
1903
1904 if (skb->encapsulation) {
1905 network_hdr_len = skb_inner_network_header_len(skb);
1906 this_ip_hdr = inner_ip_hdr(skb);
1907 this_ipv6_hdr = inner_ipv6_hdr(skb);
1908 this_tcp_hdrlen = inner_tcp_hdrlen(skb);
1909
1910 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1911
1912 if (tx_flags & I40E_TX_FLAGS_TSO) {
1913 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
1914 ip_hdr(skb)->check = 0;
1915 } else {
1916 *cd_tunneling |=
1917 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1918 }
1919 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1920 if (tx_flags & I40E_TX_FLAGS_TSO) {
1921 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
1922 ip_hdr(skb)->check = 0;
1923 } else {
1924 *cd_tunneling |=
1925 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1926 }
1927 }
1928
1929 /* Now set the ctx descriptor fields */
1930 *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
1931 I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
1932 I40E_TXD_CTX_UDP_TUNNELING |
1933 ((skb_inner_network_offset(skb) -
1934 skb_transport_offset(skb)) >> 1) <<
1935 I40E_TXD_CTX_QW0_NATLEN_SHIFT;
1936
1937 } else {
1938 network_hdr_len = skb_network_header_len(skb);
1939 this_ip_hdr = ip_hdr(skb);
1940 this_ipv6_hdr = ipv6_hdr(skb);
1941 this_tcp_hdrlen = tcp_hdrlen(skb);
1942 }
1943
1944 /* Enable IP checksum offloads */
1945 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1946 l4_hdr = this_ip_hdr->protocol;
1947 /* the stack computes the IP header already, the only time we
1948 * need the hardware to recompute it is in the case of TSO.
1949 */
1950 if (tx_flags & I40E_TX_FLAGS_TSO) {
1951 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
1952 this_ip_hdr->check = 0;
1953 } else {
1954 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
1955 }
1956 /* Now set the td_offset for IP header length */
1957 *td_offset = (network_hdr_len >> 2) <<
1958 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1959 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1960 l4_hdr = this_ipv6_hdr->nexthdr;
1961 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
1962 /* Now set the td_offset for IP header length */
1963 *td_offset = (network_hdr_len >> 2) <<
1964 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1965 }
1966 /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
1967 *td_offset |= (skb_network_offset(skb) >> 1) <<
1968 I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
1969
1970 /* Enable L4 checksum offloads */
1971 switch (l4_hdr) {
1972 case IPPROTO_TCP:
1973 /* enable checksum offloads */
1974 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
1975 *td_offset |= (this_tcp_hdrlen >> 2) <<
1976 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1977 break;
1978 case IPPROTO_SCTP:
1979 /* enable SCTP checksum offload */
1980 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
1981 *td_offset |= (sizeof(struct sctphdr) >> 2) <<
1982 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1983 break;
1984 case IPPROTO_UDP:
1985 /* enable UDP checksum offload */
1986 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
1987 *td_offset |= (sizeof(struct udphdr) >> 2) <<
1988 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1989 break;
1990 default:
1991 break;
1992 }
1993}
1994
1995/**
1996 * i40e_create_tx_ctx Build the Tx context descriptor
1997 * @tx_ring: ring to create the descriptor on
1998 * @cd_type_cmd_tso_mss: Quad Word 1
1999 * @cd_tunneling: Quad Word 0 - bits 0-31
2000 * @cd_l2tag2: Quad Word 0 - bits 32-63
2001 **/
2002static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
2003 const u64 cd_type_cmd_tso_mss,
2004 const u32 cd_tunneling, const u32 cd_l2tag2)
2005{
2006 struct i40e_tx_context_desc *context_desc;
Alexander Duyckfc4ac672013-09-28 06:00:22 +00002007 int i = tx_ring->next_to_use;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002008
Jesse Brandeburgff40dd52014-02-14 02:14:41 +00002009 if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
2010 !cd_tunneling && !cd_l2tag2)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002011 return;
2012
2013 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +00002014 context_desc = I40E_TX_CTXTDESC(tx_ring, i);
2015
2016 i++;
2017 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002018
2019 /* cpu_to_le32 and assign to struct fields */
2020 context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
2021 context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
Jesse Brandeburg3efbbb22014-06-04 20:41:54 +00002022 context_desc->rsvd = cpu_to_le16(0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002023 context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
2024}
2025
2026/**
2027 * i40e_tx_map - Build the Tx descriptor
2028 * @tx_ring: ring to send buffer on
2029 * @skb: send buffer
2030 * @first: first buffer info buffer to use
2031 * @tx_flags: collected send information
2032 * @hdr_len: size of the packet header
2033 * @td_cmd: the command field in the descriptor
2034 * @td_offset: offset for checksum or crc
2035 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002036#ifdef I40E_FCOE
2037void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
2038 struct i40e_tx_buffer *first, u32 tx_flags,
2039 const u8 hdr_len, u32 td_cmd, u32 td_offset)
2040#else
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002041static void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
2042 struct i40e_tx_buffer *first, u32 tx_flags,
2043 const u8 hdr_len, u32 td_cmd, u32 td_offset)
Vasu Dev38e00432014-08-01 13:27:03 -07002044#endif
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002045{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002046 unsigned int data_len = skb->data_len;
2047 unsigned int size = skb_headlen(skb);
Alexander Duycka5e9c572013-09-28 06:00:27 +00002048 struct skb_frag_struct *frag;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002049 struct i40e_tx_buffer *tx_bi;
2050 struct i40e_tx_desc *tx_desc;
Alexander Duycka5e9c572013-09-28 06:00:27 +00002051 u16 i = tx_ring->next_to_use;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002052 u32 td_tag = 0;
2053 dma_addr_t dma;
2054 u16 gso_segs;
2055
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002056 if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
2057 td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
2058 td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
2059 I40E_TX_FLAGS_VLAN_SHIFT;
2060 }
2061
Alexander Duycka5e9c572013-09-28 06:00:27 +00002062 if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
2063 gso_segs = skb_shinfo(skb)->gso_segs;
2064 else
2065 gso_segs = 1;
2066
2067 /* multiply data chunks by size of headers */
2068 first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
2069 first->gso_segs = gso_segs;
2070 first->skb = skb;
2071 first->tx_flags = tx_flags;
2072
2073 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
2074
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002075 tx_desc = I40E_TX_DESC(tx_ring, i);
Alexander Duycka5e9c572013-09-28 06:00:27 +00002076 tx_bi = first;
2077
2078 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
2079 if (dma_mapping_error(tx_ring->dev, dma))
2080 goto dma_error;
2081
2082 /* record length, and DMA address */
2083 dma_unmap_len_set(tx_bi, len, size);
2084 dma_unmap_addr_set(tx_bi, dma, dma);
2085
2086 tx_desc->buffer_addr = cpu_to_le64(dma);
2087
2088 while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002089 tx_desc->cmd_type_offset_bsz =
2090 build_ctob(td_cmd, td_offset,
2091 I40E_MAX_DATA_PER_TXD, td_tag);
2092
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002093 tx_desc++;
2094 i++;
2095 if (i == tx_ring->count) {
2096 tx_desc = I40E_TX_DESC(tx_ring, 0);
2097 i = 0;
2098 }
Alexander Duycka5e9c572013-09-28 06:00:27 +00002099
2100 dma += I40E_MAX_DATA_PER_TXD;
2101 size -= I40E_MAX_DATA_PER_TXD;
2102
2103 tx_desc->buffer_addr = cpu_to_le64(dma);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002104 }
2105
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002106 if (likely(!data_len))
2107 break;
2108
Alexander Duycka5e9c572013-09-28 06:00:27 +00002109 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
2110 size, td_tag);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002111
2112 tx_desc++;
2113 i++;
2114 if (i == tx_ring->count) {
2115 tx_desc = I40E_TX_DESC(tx_ring, 0);
2116 i = 0;
2117 }
2118
Alexander Duycka5e9c572013-09-28 06:00:27 +00002119 size = skb_frag_size(frag);
2120 data_len -= size;
2121
2122 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
2123 DMA_TO_DEVICE);
2124
2125 tx_bi = &tx_ring->tx_bi[i];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002126 }
2127
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002128 /* Place RS bit on last descriptor of any packet that spans across the
2129 * 4th descriptor (WB_STRIDE aka 0x3) in a 64B cacheline.
2130 */
2131#define WB_STRIDE 0x3
2132 if (((i & WB_STRIDE) != WB_STRIDE) &&
2133 (first <= &tx_ring->tx_bi[i]) &&
2134 (first >= &tx_ring->tx_bi[i & ~WB_STRIDE])) {
2135 tx_desc->cmd_type_offset_bsz =
2136 build_ctob(td_cmd, td_offset, size, td_tag) |
2137 cpu_to_le64((u64)I40E_TX_DESC_CMD_EOP <<
2138 I40E_TXD_QW1_CMD_SHIFT);
2139 } else {
2140 tx_desc->cmd_type_offset_bsz =
2141 build_ctob(td_cmd, td_offset, size, td_tag) |
2142 cpu_to_le64((u64)I40E_TXD_CMD <<
2143 I40E_TXD_QW1_CMD_SHIFT);
2144 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002145
Alexander Duyck7070ce02013-09-28 06:00:37 +00002146 netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
2147 tx_ring->queue_index),
2148 first->bytecount);
2149
Alexander Duycka5e9c572013-09-28 06:00:27 +00002150 /* set the timestamp */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002151 first->time_stamp = jiffies;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002152
2153 /* Force memory writes to complete before letting h/w
2154 * know there are new descriptors to fetch. (Only
2155 * applicable for weak-ordered memory model archs,
2156 * such as IA-64).
2157 */
2158 wmb();
2159
Alexander Duycka5e9c572013-09-28 06:00:27 +00002160 /* set next_to_watch value indicating a packet is present */
2161 first->next_to_watch = tx_desc;
2162
2163 i++;
2164 if (i == tx_ring->count)
2165 i = 0;
2166
2167 tx_ring->next_to_use = i;
2168
2169 /* notify HW of packet */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002170 writel(i, tx_ring->tail);
Alexander Duycka5e9c572013-09-28 06:00:27 +00002171
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002172 return;
2173
2174dma_error:
Alexander Duycka5e9c572013-09-28 06:00:27 +00002175 dev_info(tx_ring->dev, "TX DMA map failed\n");
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002176
2177 /* clear dma mappings for failed tx_bi map */
2178 for (;;) {
2179 tx_bi = &tx_ring->tx_bi[i];
Alexander Duycka5e9c572013-09-28 06:00:27 +00002180 i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002181 if (tx_bi == first)
2182 break;
2183 if (i == 0)
2184 i = tx_ring->count;
2185 i--;
2186 }
2187
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002188 tx_ring->next_to_use = i;
2189}
2190
2191/**
2192 * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
2193 * @tx_ring: the ring to be checked
2194 * @size: the size buffer we want to assure is available
2195 *
2196 * Returns -EBUSY if a stop is needed, else 0
2197 **/
2198static inline int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
2199{
2200 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
Greg Rose8e9dca52013-12-18 13:45:53 +00002201 /* Memory barrier before checking head and tail */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002202 smp_mb();
2203
2204 /* Check again in a case another CPU has just made room available. */
2205 if (likely(I40E_DESC_UNUSED(tx_ring) < size))
2206 return -EBUSY;
2207
2208 /* A reprieve! - use start_queue because it doesn't call schedule */
2209 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
2210 ++tx_ring->tx_stats.restart_queue;
2211 return 0;
2212}
2213
2214/**
2215 * i40e_maybe_stop_tx - 1st level check for tx stop conditions
2216 * @tx_ring: the ring to be checked
2217 * @size: the size buffer we want to assure is available
2218 *
2219 * Returns 0 if stop is not needed
2220 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002221#ifdef I40E_FCOE
2222int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
2223#else
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002224static int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
Vasu Dev38e00432014-08-01 13:27:03 -07002225#endif
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002226{
2227 if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
2228 return 0;
2229 return __i40e_maybe_stop_tx(tx_ring, size);
2230}
2231
2232/**
2233 * i40e_xmit_descriptor_count - calculate number of tx descriptors needed
2234 * @skb: send buffer
2235 * @tx_ring: ring to send buffer on
2236 *
2237 * Returns number of data descriptors needed for this skb. Returns 0 to indicate
2238 * there is not enough descriptors available in this ring since we need at least
2239 * one descriptor.
2240 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002241#ifdef I40E_FCOE
2242int i40e_xmit_descriptor_count(struct sk_buff *skb,
2243 struct i40e_ring *tx_ring)
2244#else
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002245static int i40e_xmit_descriptor_count(struct sk_buff *skb,
2246 struct i40e_ring *tx_ring)
Vasu Dev38e00432014-08-01 13:27:03 -07002247#endif
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002248{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002249 unsigned int f;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002250 int count = 0;
2251
2252 /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
2253 * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
Jesse Brandeburgbe560522014-02-06 05:51:13 +00002254 * + 4 desc gap to avoid the cache line where head is,
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002255 * + 1 desc for context descriptor,
2256 * otherwise try next time
2257 */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002258 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
2259 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
Jesse Brandeburg980093e2014-05-10 04:49:12 +00002260
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002261 count += TXD_USE_COUNT(skb_headlen(skb));
Jesse Brandeburgbe560522014-02-06 05:51:13 +00002262 if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002263 tx_ring->tx_stats.tx_busy++;
2264 return 0;
2265 }
2266 return count;
2267}
2268
2269/**
2270 * i40e_xmit_frame_ring - Sends buffer on Tx ring
2271 * @skb: send buffer
2272 * @tx_ring: ring to send buffer on
2273 *
2274 * Returns NETDEV_TX_OK if sent, else an error code
2275 **/
2276static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
2277 struct i40e_ring *tx_ring)
2278{
2279 u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
2280 u32 cd_tunneling = 0, cd_l2tag2 = 0;
2281 struct i40e_tx_buffer *first;
2282 u32 td_offset = 0;
2283 u32 tx_flags = 0;
2284 __be16 protocol;
2285 u32 td_cmd = 0;
2286 u8 hdr_len = 0;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002287 int tsyn;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002288 int tso;
2289 if (0 == i40e_xmit_descriptor_count(skb, tx_ring))
2290 return NETDEV_TX_BUSY;
2291
2292 /* prepare the xmit flags */
2293 if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
2294 goto out_drop;
2295
2296 /* obtain protocol of skb */
2297 protocol = skb->protocol;
2298
2299 /* record the location of the first descriptor for this packet */
2300 first = &tx_ring->tx_bi[tx_ring->next_to_use];
2301
2302 /* setup IPv4/IPv6 offloads */
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00002303 if (protocol == htons(ETH_P_IP))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002304 tx_flags |= I40E_TX_FLAGS_IPV4;
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00002305 else if (protocol == htons(ETH_P_IPV6))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002306 tx_flags |= I40E_TX_FLAGS_IPV6;
2307
2308 tso = i40e_tso(tx_ring, skb, tx_flags, protocol, &hdr_len,
2309 &cd_type_cmd_tso_mss, &cd_tunneling);
2310
2311 if (tso < 0)
2312 goto out_drop;
2313 else if (tso)
2314 tx_flags |= I40E_TX_FLAGS_TSO;
2315
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002316 tsyn = i40e_tsyn(tx_ring, skb, tx_flags, &cd_type_cmd_tso_mss);
2317
2318 if (tsyn)
2319 tx_flags |= I40E_TX_FLAGS_TSYN;
2320
Jakub Kicinski259afec2014-03-15 14:55:37 +00002321 skb_tx_timestamp(skb);
2322
Alexander Duyckb1941302013-09-28 06:00:32 +00002323 /* always enable CRC insertion offload */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002324 td_cmd |= I40E_TX_DESC_CMD_ICRC;
2325
Alexander Duyckb1941302013-09-28 06:00:32 +00002326 /* Always offload the checksum, since it's in the data descriptor */
2327 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2328 tx_flags |= I40E_TX_FLAGS_CSUM;
2329
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002330 i40e_tx_enable_csum(skb, tx_flags, &td_cmd, &td_offset,
2331 tx_ring, &cd_tunneling);
Alexander Duyckb1941302013-09-28 06:00:32 +00002332 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002333
2334 i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
2335 cd_tunneling, cd_l2tag2);
2336
2337 /* Add Flow Director ATR if it's enabled.
2338 *
2339 * NOTE: this must always be directly before the data descriptor.
2340 */
2341 i40e_atr(tx_ring, skb, tx_flags, protocol);
2342
2343 i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
2344 td_cmd, td_offset);
2345
2346 i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
2347
2348 return NETDEV_TX_OK;
2349
2350out_drop:
2351 dev_kfree_skb_any(skb);
2352 return NETDEV_TX_OK;
2353}
2354
2355/**
2356 * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
2357 * @skb: send buffer
2358 * @netdev: network interface device structure
2359 *
2360 * Returns NETDEV_TX_OK if sent, else an error code
2361 **/
2362netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2363{
2364 struct i40e_netdev_priv *np = netdev_priv(netdev);
2365 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002366 struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00002367
2368 /* hardware can't handle really short frames, hardware padding works
2369 * beyond this point
2370 */
2371 if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
2372 if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
2373 return NETDEV_TX_OK;
2374 skb->len = I40E_MIN_TX_LEN;
2375 skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
2376 }
2377
2378 return i40e_xmit_frame_ring(skb, tx_ring);
2379}