blob: 946d8b1d9a4d056e66322b4fcd108ab4112d2599 [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
27#include "i40e.h"
28
29static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
30 u32 td_tag)
31{
32 return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
33 ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
34 ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
35 ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
36 ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
37}
38
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000039#define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000040/**
41 * i40e_program_fdir_filter - Program a Flow Director filter
42 * @fdir_input: Packet data that will be filter parameters
43 * @pf: The pf pointer
44 * @add: True for add/update, False for remove
45 **/
46int i40e_program_fdir_filter(struct i40e_fdir_data *fdir_data,
47 struct i40e_pf *pf, bool add)
48{
49 struct i40e_filter_program_desc *fdir_desc;
50 struct i40e_tx_buffer *tx_buf;
51 struct i40e_tx_desc *tx_desc;
52 struct i40e_ring *tx_ring;
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000053 unsigned int fpt, dcc;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000054 struct i40e_vsi *vsi;
55 struct device *dev;
56 dma_addr_t dma;
57 u32 td_cmd = 0;
58 u16 i;
59
60 /* find existing FDIR VSI */
61 vsi = NULL;
62 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
63 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR)
64 vsi = pf->vsi[i];
65 if (!vsi)
66 return -ENOENT;
67
Alexander Duyck9f65e152013-09-28 06:00:58 +000068 tx_ring = vsi->tx_rings[0];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000069 dev = tx_ring->dev;
70
71 dma = dma_map_single(dev, fdir_data->raw_packet,
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000072 I40E_FDIR_MAX_RAW_PACKET_LOOKUP, DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000073 if (dma_mapping_error(dev, dma))
74 goto dma_fail;
75
76 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +000077 i = tx_ring->next_to_use;
78 fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
Alexander Duyckfc4ac672013-09-28 06:00:22 +000079
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000080 tx_ring->next_to_use = (i + 1 < tx_ring->count) ? i + 1 : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000081
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000082 fpt = (fdir_data->q_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
83 I40E_TXD_FLTR_QW0_QINDEX_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000084
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000085 fpt |= (fdir_data->flex_off << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT) &
86 I40E_TXD_FLTR_QW0_FLEXOFF_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000087
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000088 fpt |= (fdir_data->pctype << I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) &
89 I40E_TXD_FLTR_QW0_PCTYPE_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000090
91 /* Use LAN VSI Id if not programmed by user */
92 if (fdir_data->dest_vsi == 0)
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000093 fpt |= (pf->vsi[pf->lan_vsi]->id) <<
94 I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000095 else
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +000096 fpt |= ((u32)fdir_data->dest_vsi <<
97 I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT) &
98 I40E_TXD_FLTR_QW0_DEST_VSI_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +000099
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000100 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(fpt);
101
102 dcc = I40E_TX_DESC_DTYPE_FILTER_PROG;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000103
104 if (add)
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000105 dcc |= I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
106 I40E_TXD_FLTR_QW1_PCMD_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000107 else
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000108 dcc |= I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
109 I40E_TXD_FLTR_QW1_PCMD_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000110
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000111 dcc |= (fdir_data->dest_ctl << I40E_TXD_FLTR_QW1_DEST_SHIFT) &
112 I40E_TXD_FLTR_QW1_DEST_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000113
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000114 dcc |= (fdir_data->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT) &
115 I40E_TXD_FLTR_QW1_FD_STATUS_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000116
117 if (fdir_data->cnt_index != 0) {
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000118 dcc |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
119 dcc |= ((u32)fdir_data->cnt_index <<
120 I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
121 I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000122 }
123
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000124 fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dcc);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000125 fdir_desc->fd_id = cpu_to_le32(fdir_data->fd_id);
126
127 /* Now program a dummy descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000128 i = tx_ring->next_to_use;
129 tx_desc = I40E_TX_DESC(tx_ring, i);
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000130 tx_buf = &tx_ring->tx_bi[i];
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000131
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000132 tx_ring->next_to_use = (i + 1 < tx_ring->count) ? i + 1 : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000133
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000134 /* record length, and DMA address */
135 dma_unmap_len_set(tx_buf, len, I40E_FDIR_MAX_RAW_PACKET_LOOKUP);
136 dma_unmap_addr_set(tx_buf, dma, dma);
137
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000138 tx_desc->buffer_addr = cpu_to_le64(dma);
Jesse Brandeburgeaefbd02013-09-28 07:13:54 +0000139 td_cmd = I40E_TXD_CMD | I40E_TX_DESC_CMD_DUMMY;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000140
141 tx_desc->cmd_type_offset_bsz =
142 build_ctob(td_cmd, 0, I40E_FDIR_MAX_RAW_PACKET_LOOKUP, 0);
143
Anjali Singhai Jain298deef2013-11-28 06:39:33 +0000144 /* set the timestamp */
145 tx_buf->time_stamp = jiffies;
146
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000147 /* Force memory writes to complete before letting h/w
148 * know there are new descriptors to fetch. (Only
149 * applicable for weak-ordered memory model archs,
150 * such as IA-64).
151 */
152 wmb();
153
Alexander Duyckfc4ac672013-09-28 06:00:22 +0000154 /* Mark the data descriptor to be watched */
155 tx_buf->next_to_watch = tx_desc;
156
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000157 writel(tx_ring->next_to_use, tx_ring->tail);
158 return 0;
159
160dma_fail:
161 return -1;
162}
163
164/**
165 * i40e_fd_handle_status - check the Programming Status for FD
166 * @rx_ring: the Rx ring for this descriptor
167 * @qw: the descriptor data
168 * @prog_id: the id originally used for programming
169 *
170 * This is used to verify if the FD programming or invalidation
171 * requested by SW to the HW is successful or not and take actions accordingly.
172 **/
173static void i40e_fd_handle_status(struct i40e_ring *rx_ring, u32 qw, u8 prog_id)
174{
175 struct pci_dev *pdev = rx_ring->vsi->back->pdev;
176 u32 error;
177
178 error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
179 I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
180
181 /* for now just print the Status */
182 dev_info(&pdev->dev, "FD programming id %02x, Status %08x\n",
183 prog_id, error);
184}
185
186/**
Alexander Duycka5e9c572013-09-28 06:00:27 +0000187 * i40e_unmap_and_free_tx_resource - Release a Tx buffer
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000188 * @ring: the ring that owns the buffer
189 * @tx_buffer: the buffer to free
190 **/
Alexander Duycka5e9c572013-09-28 06:00:27 +0000191static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
192 struct i40e_tx_buffer *tx_buffer)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000193{
Alexander Duycka5e9c572013-09-28 06:00:27 +0000194 if (tx_buffer->skb) {
195 dev_kfree_skb_any(tx_buffer->skb);
196 if (dma_unmap_len(tx_buffer, len))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000197 dma_unmap_single(ring->dev,
Alexander Duyck35a1e2a2013-09-28 06:00:17 +0000198 dma_unmap_addr(tx_buffer, dma),
199 dma_unmap_len(tx_buffer, len),
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000200 DMA_TO_DEVICE);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000201 } else if (dma_unmap_len(tx_buffer, len)) {
202 dma_unmap_page(ring->dev,
203 dma_unmap_addr(tx_buffer, dma),
204 dma_unmap_len(tx_buffer, len),
205 DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000206 }
Alexander Duycka5e9c572013-09-28 06:00:27 +0000207 tx_buffer->next_to_watch = NULL;
208 tx_buffer->skb = NULL;
Alexander Duyck35a1e2a2013-09-28 06:00:17 +0000209 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000210 /* tx_buffer must be completely set up in the transmit path */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000211}
212
213/**
214 * i40e_clean_tx_ring - Free any empty Tx buffers
215 * @tx_ring: ring to be cleaned
216 **/
217void i40e_clean_tx_ring(struct i40e_ring *tx_ring)
218{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000219 unsigned long bi_size;
220 u16 i;
221
222 /* ring already cleared, nothing to do */
223 if (!tx_ring->tx_bi)
224 return;
225
226 /* Free all the Tx ring sk_buffs */
Alexander Duycka5e9c572013-09-28 06:00:27 +0000227 for (i = 0; i < tx_ring->count; i++)
228 i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000229
230 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
231 memset(tx_ring->tx_bi, 0, bi_size);
232
233 /* Zero out the descriptor ring */
234 memset(tx_ring->desc, 0, tx_ring->size);
235
236 tx_ring->next_to_use = 0;
237 tx_ring->next_to_clean = 0;
Alexander Duyck7070ce02013-09-28 06:00:37 +0000238
239 if (!tx_ring->netdev)
240 return;
241
242 /* cleanup Tx queue statistics */
243 netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
244 tx_ring->queue_index));
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000245}
246
247/**
248 * i40e_free_tx_resources - Free Tx resources per queue
249 * @tx_ring: Tx descriptor ring for a specific queue
250 *
251 * Free all transmit software resources
252 **/
253void i40e_free_tx_resources(struct i40e_ring *tx_ring)
254{
255 i40e_clean_tx_ring(tx_ring);
256 kfree(tx_ring->tx_bi);
257 tx_ring->tx_bi = NULL;
258
259 if (tx_ring->desc) {
260 dma_free_coherent(tx_ring->dev, tx_ring->size,
261 tx_ring->desc, tx_ring->dma);
262 tx_ring->desc = NULL;
263 }
264}
265
266/**
267 * i40e_get_tx_pending - how many tx descriptors not processed
268 * @tx_ring: the ring of descriptors
269 *
270 * Since there is no access to the ring head register
271 * in XL710, we need to use our local copies
272 **/
273static u32 i40e_get_tx_pending(struct i40e_ring *ring)
274{
275 u32 ntu = ((ring->next_to_clean <= ring->next_to_use)
276 ? ring->next_to_use
277 : ring->next_to_use + ring->count);
278 return ntu - ring->next_to_clean;
279}
280
281/**
282 * i40e_check_tx_hang - Is there a hang in the Tx queue
283 * @tx_ring: the ring of descriptors
284 **/
285static bool i40e_check_tx_hang(struct i40e_ring *tx_ring)
286{
287 u32 tx_pending = i40e_get_tx_pending(tx_ring);
288 bool ret = false;
289
290 clear_check_for_tx_hang(tx_ring);
291
292 /* Check for a hung queue, but be thorough. This verifies
293 * that a transmit has been completed since the previous
294 * check AND there is at least one packet pending. The
295 * ARMED bit is set to indicate a potential hang. The
296 * bit is cleared if a pause frame is received to remove
297 * false hang detection due to PFC or 802.3x frames. By
298 * requiring this to fail twice we avoid races with
299 * PFC clearing the ARMED bit and conditions where we
300 * run the check_tx_hang logic with a transmit completion
301 * pending but without time to complete it yet.
302 */
Alexander Duycka114d0a2013-09-28 06:00:43 +0000303 if ((tx_ring->tx_stats.tx_done_old == tx_ring->stats.packets) &&
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000304 tx_pending) {
305 /* make sure it is true for two checks in a row */
306 ret = test_and_set_bit(__I40E_HANG_CHECK_ARMED,
307 &tx_ring->state);
308 } else {
309 /* update completed stats and disarm the hang check */
Alexander Duycka114d0a2013-09-28 06:00:43 +0000310 tx_ring->tx_stats.tx_done_old = tx_ring->stats.packets;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000311 clear_bit(__I40E_HANG_CHECK_ARMED, &tx_ring->state);
312 }
313
314 return ret;
315}
316
317/**
318 * i40e_clean_tx_irq - Reclaim resources after transmit completes
319 * @tx_ring: tx ring to clean
320 * @budget: how many cleans we're allowed
321 *
322 * Returns true if there's any budget left (e.g. the clean is finished)
323 **/
324static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
325{
326 u16 i = tx_ring->next_to_clean;
327 struct i40e_tx_buffer *tx_buf;
328 struct i40e_tx_desc *tx_desc;
329 unsigned int total_packets = 0;
330 unsigned int total_bytes = 0;
331
332 tx_buf = &tx_ring->tx_bi[i];
333 tx_desc = I40E_TX_DESC(tx_ring, i);
Alexander Duycka5e9c572013-09-28 06:00:27 +0000334 i -= tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000335
Alexander Duycka5e9c572013-09-28 06:00:27 +0000336 do {
337 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000338
339 /* if next_to_watch is not set then there is no work pending */
340 if (!eop_desc)
341 break;
342
Alexander Duycka5e9c572013-09-28 06:00:27 +0000343 /* prevent any other reads prior to eop_desc */
344 read_barrier_depends();
345
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000346 /* if the descriptor isn't done, no work yet to do */
347 if (!(eop_desc->cmd_type_offset_bsz &
348 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
349 break;
350
Alexander Duyckc304fda2013-09-28 06:00:12 +0000351 /* clear next_to_watch to prevent false hangs */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000352 tx_buf->next_to_watch = NULL;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000353
Alexander Duycka5e9c572013-09-28 06:00:27 +0000354 /* update the statistics for this packet */
355 total_bytes += tx_buf->bytecount;
356 total_packets += tx_buf->gso_segs;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000357
Alexander Duycka5e9c572013-09-28 06:00:27 +0000358 /* free the skb */
359 dev_kfree_skb_any(tx_buf->skb);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000360
Alexander Duycka5e9c572013-09-28 06:00:27 +0000361 /* unmap skb header data */
362 dma_unmap_single(tx_ring->dev,
363 dma_unmap_addr(tx_buf, dma),
364 dma_unmap_len(tx_buf, len),
365 DMA_TO_DEVICE);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000366
Alexander Duycka5e9c572013-09-28 06:00:27 +0000367 /* clear tx_buffer data */
368 tx_buf->skb = NULL;
369 dma_unmap_len_set(tx_buf, len, 0);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000370
Alexander Duycka5e9c572013-09-28 06:00:27 +0000371 /* unmap remaining buffers */
372 while (tx_desc != eop_desc) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000373
374 tx_buf++;
375 tx_desc++;
376 i++;
Alexander Duycka5e9c572013-09-28 06:00:27 +0000377 if (unlikely(!i)) {
378 i -= tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000379 tx_buf = tx_ring->tx_bi;
380 tx_desc = I40E_TX_DESC(tx_ring, 0);
381 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000382
Alexander Duycka5e9c572013-09-28 06:00:27 +0000383 /* unmap any remaining paged data */
384 if (dma_unmap_len(tx_buf, len)) {
385 dma_unmap_page(tx_ring->dev,
386 dma_unmap_addr(tx_buf, dma),
387 dma_unmap_len(tx_buf, len),
388 DMA_TO_DEVICE);
389 dma_unmap_len_set(tx_buf, len, 0);
390 }
391 }
392
393 /* move us one more past the eop_desc for start of next pkt */
394 tx_buf++;
395 tx_desc++;
396 i++;
397 if (unlikely(!i)) {
398 i -= tx_ring->count;
399 tx_buf = tx_ring->tx_bi;
400 tx_desc = I40E_TX_DESC(tx_ring, 0);
401 }
402
403 /* update budget accounting */
404 budget--;
405 } while (likely(budget));
406
407 i += tx_ring->count;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000408 tx_ring->next_to_clean = i;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000409 u64_stats_update_begin(&tx_ring->syncp);
Alexander Duycka114d0a2013-09-28 06:00:43 +0000410 tx_ring->stats.bytes += total_bytes;
411 tx_ring->stats.packets += total_packets;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000412 u64_stats_update_end(&tx_ring->syncp);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000413 tx_ring->q_vector->tx.total_bytes += total_bytes;
414 tx_ring->q_vector->tx.total_packets += total_packets;
Alexander Duycka5e9c572013-09-28 06:00:27 +0000415
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000416 if (check_for_tx_hang(tx_ring) && i40e_check_tx_hang(tx_ring)) {
417 /* schedule immediate reset if we believe we hung */
418 dev_info(tx_ring->dev, "Detected Tx Unit Hang\n"
419 " VSI <%d>\n"
420 " Tx Queue <%d>\n"
421 " next_to_use <%x>\n"
422 " next_to_clean <%x>\n",
423 tx_ring->vsi->seid,
424 tx_ring->queue_index,
425 tx_ring->next_to_use, i);
426 dev_info(tx_ring->dev, "tx_bi[next_to_clean]\n"
427 " time_stamp <%lx>\n"
428 " jiffies <%lx>\n",
429 tx_ring->tx_bi[i].time_stamp, jiffies);
430
431 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
432
433 dev_info(tx_ring->dev,
434 "tx hang detected on queue %d, resetting adapter\n",
435 tx_ring->queue_index);
436
437 tx_ring->netdev->netdev_ops->ndo_tx_timeout(tx_ring->netdev);
438
439 /* the adapter is about to reset, no point in enabling stuff */
440 return true;
441 }
442
Alexander Duyck7070ce02013-09-28 06:00:37 +0000443 netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
444 tx_ring->queue_index),
445 total_packets, total_bytes);
446
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000447#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
448 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
449 (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
450 /* Make sure that anybody stopping the queue after this
451 * sees the new next_to_clean.
452 */
453 smp_mb();
454 if (__netif_subqueue_stopped(tx_ring->netdev,
455 tx_ring->queue_index) &&
456 !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
457 netif_wake_subqueue(tx_ring->netdev,
458 tx_ring->queue_index);
459 ++tx_ring->tx_stats.restart_queue;
460 }
461 }
462
463 return budget > 0;
464}
465
466/**
467 * i40e_set_new_dynamic_itr - Find new ITR level
468 * @rc: structure containing ring performance data
469 *
470 * Stores a new ITR value based on packets and byte counts during
471 * the last interrupt. The advantage of per interrupt computation
472 * is faster updates and more accurate ITR for the current traffic
473 * pattern. Constants in this function were computed based on
474 * theoretical maximum wire speed and thresholds were set based on
475 * testing data as well as attempting to minimize response time
476 * while increasing bulk throughput.
477 **/
478static void i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
479{
480 enum i40e_latency_range new_latency_range = rc->latency_range;
481 u32 new_itr = rc->itr;
482 int bytes_per_int;
483
484 if (rc->total_packets == 0 || !rc->itr)
485 return;
486
487 /* simple throttlerate management
488 * 0-10MB/s lowest (100000 ints/s)
489 * 10-20MB/s low (20000 ints/s)
490 * 20-1249MB/s bulk (8000 ints/s)
491 */
492 bytes_per_int = rc->total_bytes / rc->itr;
493 switch (rc->itr) {
494 case I40E_LOWEST_LATENCY:
495 if (bytes_per_int > 10)
496 new_latency_range = I40E_LOW_LATENCY;
497 break;
498 case I40E_LOW_LATENCY:
499 if (bytes_per_int > 20)
500 new_latency_range = I40E_BULK_LATENCY;
501 else if (bytes_per_int <= 10)
502 new_latency_range = I40E_LOWEST_LATENCY;
503 break;
504 case I40E_BULK_LATENCY:
505 if (bytes_per_int <= 20)
506 rc->latency_range = I40E_LOW_LATENCY;
507 break;
508 }
509
510 switch (new_latency_range) {
511 case I40E_LOWEST_LATENCY:
512 new_itr = I40E_ITR_100K;
513 break;
514 case I40E_LOW_LATENCY:
515 new_itr = I40E_ITR_20K;
516 break;
517 case I40E_BULK_LATENCY:
518 new_itr = I40E_ITR_8K;
519 break;
520 default:
521 break;
522 }
523
524 if (new_itr != rc->itr) {
525 /* do an exponential smoothing */
526 new_itr = (10 * new_itr * rc->itr) /
527 ((9 * new_itr) + rc->itr);
528 rc->itr = new_itr & I40E_MAX_ITR;
529 }
530
531 rc->total_bytes = 0;
532 rc->total_packets = 0;
533}
534
535/**
536 * i40e_update_dynamic_itr - Adjust ITR based on bytes per int
537 * @q_vector: the vector to adjust
538 **/
539static void i40e_update_dynamic_itr(struct i40e_q_vector *q_vector)
540{
541 u16 vector = q_vector->vsi->base_vector + q_vector->v_idx;
542 struct i40e_hw *hw = &q_vector->vsi->back->hw;
543 u32 reg_addr;
544 u16 old_itr;
545
546 reg_addr = I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1);
547 old_itr = q_vector->rx.itr;
548 i40e_set_new_dynamic_itr(&q_vector->rx);
549 if (old_itr != q_vector->rx.itr)
550 wr32(hw, reg_addr, q_vector->rx.itr);
551
552 reg_addr = I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1);
553 old_itr = q_vector->tx.itr;
554 i40e_set_new_dynamic_itr(&q_vector->tx);
555 if (old_itr != q_vector->tx.itr)
556 wr32(hw, reg_addr, q_vector->tx.itr);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000557}
558
559/**
560 * i40e_clean_programming_status - clean the programming status descriptor
561 * @rx_ring: the rx ring that has this descriptor
562 * @rx_desc: the rx descriptor written back by HW
563 *
564 * Flow director should handle FD_FILTER_STATUS to check its filter programming
565 * status being successful or not and take actions accordingly. FCoE should
566 * handle its context/filter programming/invalidation status and take actions.
567 *
568 **/
569static void i40e_clean_programming_status(struct i40e_ring *rx_ring,
570 union i40e_rx_desc *rx_desc)
571{
572 u64 qw;
573 u8 id;
574
575 qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
576 id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
577 I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
578
579 if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS)
580 i40e_fd_handle_status(rx_ring, qw, id);
581}
582
583/**
584 * i40e_setup_tx_descriptors - Allocate the Tx descriptors
585 * @tx_ring: the tx ring to set up
586 *
587 * Return 0 on success, negative on error
588 **/
589int i40e_setup_tx_descriptors(struct i40e_ring *tx_ring)
590{
591 struct device *dev = tx_ring->dev;
592 int bi_size;
593
594 if (!dev)
595 return -ENOMEM;
596
597 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
598 tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
599 if (!tx_ring->tx_bi)
600 goto err;
601
602 /* round up to nearest 4K */
603 tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
604 tx_ring->size = ALIGN(tx_ring->size, 4096);
605 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
606 &tx_ring->dma, GFP_KERNEL);
607 if (!tx_ring->desc) {
608 dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
609 tx_ring->size);
610 goto err;
611 }
612
613 tx_ring->next_to_use = 0;
614 tx_ring->next_to_clean = 0;
615 return 0;
616
617err:
618 kfree(tx_ring->tx_bi);
619 tx_ring->tx_bi = NULL;
620 return -ENOMEM;
621}
622
623/**
624 * i40e_clean_rx_ring - Free Rx buffers
625 * @rx_ring: ring to be cleaned
626 **/
627void i40e_clean_rx_ring(struct i40e_ring *rx_ring)
628{
629 struct device *dev = rx_ring->dev;
630 struct i40e_rx_buffer *rx_bi;
631 unsigned long bi_size;
632 u16 i;
633
634 /* ring already cleared, nothing to do */
635 if (!rx_ring->rx_bi)
636 return;
637
638 /* Free all the Rx ring sk_buffs */
639 for (i = 0; i < rx_ring->count; i++) {
640 rx_bi = &rx_ring->rx_bi[i];
641 if (rx_bi->dma) {
642 dma_unmap_single(dev,
643 rx_bi->dma,
644 rx_ring->rx_buf_len,
645 DMA_FROM_DEVICE);
646 rx_bi->dma = 0;
647 }
648 if (rx_bi->skb) {
649 dev_kfree_skb(rx_bi->skb);
650 rx_bi->skb = NULL;
651 }
652 if (rx_bi->page) {
653 if (rx_bi->page_dma) {
654 dma_unmap_page(dev,
655 rx_bi->page_dma,
656 PAGE_SIZE / 2,
657 DMA_FROM_DEVICE);
658 rx_bi->page_dma = 0;
659 }
660 __free_page(rx_bi->page);
661 rx_bi->page = NULL;
662 rx_bi->page_offset = 0;
663 }
664 }
665
666 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
667 memset(rx_ring->rx_bi, 0, bi_size);
668
669 /* Zero out the descriptor ring */
670 memset(rx_ring->desc, 0, rx_ring->size);
671
672 rx_ring->next_to_clean = 0;
673 rx_ring->next_to_use = 0;
674}
675
676/**
677 * i40e_free_rx_resources - Free Rx resources
678 * @rx_ring: ring to clean the resources from
679 *
680 * Free all receive software resources
681 **/
682void i40e_free_rx_resources(struct i40e_ring *rx_ring)
683{
684 i40e_clean_rx_ring(rx_ring);
685 kfree(rx_ring->rx_bi);
686 rx_ring->rx_bi = NULL;
687
688 if (rx_ring->desc) {
689 dma_free_coherent(rx_ring->dev, rx_ring->size,
690 rx_ring->desc, rx_ring->dma);
691 rx_ring->desc = NULL;
692 }
693}
694
695/**
696 * i40e_setup_rx_descriptors - Allocate Rx descriptors
697 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
698 *
699 * Returns 0 on success, negative on failure
700 **/
701int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring)
702{
703 struct device *dev = rx_ring->dev;
704 int bi_size;
705
706 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
707 rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
708 if (!rx_ring->rx_bi)
709 goto err;
710
711 /* Round up to nearest 4K */
712 rx_ring->size = ring_is_16byte_desc_enabled(rx_ring)
713 ? rx_ring->count * sizeof(union i40e_16byte_rx_desc)
714 : rx_ring->count * sizeof(union i40e_32byte_rx_desc);
715 rx_ring->size = ALIGN(rx_ring->size, 4096);
716 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
717 &rx_ring->dma, GFP_KERNEL);
718
719 if (!rx_ring->desc) {
720 dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
721 rx_ring->size);
722 goto err;
723 }
724
725 rx_ring->next_to_clean = 0;
726 rx_ring->next_to_use = 0;
727
728 return 0;
729err:
730 kfree(rx_ring->rx_bi);
731 rx_ring->rx_bi = NULL;
732 return -ENOMEM;
733}
734
735/**
736 * i40e_release_rx_desc - Store the new tail and head values
737 * @rx_ring: ring to bump
738 * @val: new head index
739 **/
740static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
741{
742 rx_ring->next_to_use = val;
743 /* Force memory writes to complete before letting h/w
744 * know there are new descriptors to fetch. (Only
745 * applicable for weak-ordered memory model archs,
746 * such as IA-64).
747 */
748 wmb();
749 writel(val, rx_ring->tail);
750}
751
752/**
753 * i40e_alloc_rx_buffers - Replace used receive buffers; packet split
754 * @rx_ring: ring to place buffers on
755 * @cleaned_count: number of buffers to replace
756 **/
757void i40e_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
758{
759 u16 i = rx_ring->next_to_use;
760 union i40e_rx_desc *rx_desc;
761 struct i40e_rx_buffer *bi;
762 struct sk_buff *skb;
763
764 /* do nothing if no valid netdev defined */
765 if (!rx_ring->netdev || !cleaned_count)
766 return;
767
768 while (cleaned_count--) {
769 rx_desc = I40E_RX_DESC(rx_ring, i);
770 bi = &rx_ring->rx_bi[i];
771 skb = bi->skb;
772
773 if (!skb) {
774 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
775 rx_ring->rx_buf_len);
776 if (!skb) {
Mitch Williams420136c2013-12-18 13:45:59 +0000777 rx_ring->rx_stats.alloc_buff_failed++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000778 goto no_buffers;
779 }
780 /* initialize queue mapping */
781 skb_record_rx_queue(skb, rx_ring->queue_index);
782 bi->skb = skb;
783 }
784
785 if (!bi->dma) {
786 bi->dma = dma_map_single(rx_ring->dev,
787 skb->data,
788 rx_ring->rx_buf_len,
789 DMA_FROM_DEVICE);
790 if (dma_mapping_error(rx_ring->dev, bi->dma)) {
Mitch Williams420136c2013-12-18 13:45:59 +0000791 rx_ring->rx_stats.alloc_buff_failed++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000792 bi->dma = 0;
793 goto no_buffers;
794 }
795 }
796
797 if (ring_is_ps_enabled(rx_ring)) {
798 if (!bi->page) {
799 bi->page = alloc_page(GFP_ATOMIC);
800 if (!bi->page) {
Mitch Williams420136c2013-12-18 13:45:59 +0000801 rx_ring->rx_stats.alloc_page_failed++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000802 goto no_buffers;
803 }
804 }
805
806 if (!bi->page_dma) {
807 /* use a half page if we're re-using */
808 bi->page_offset ^= PAGE_SIZE / 2;
809 bi->page_dma = dma_map_page(rx_ring->dev,
810 bi->page,
811 bi->page_offset,
812 PAGE_SIZE / 2,
813 DMA_FROM_DEVICE);
814 if (dma_mapping_error(rx_ring->dev,
815 bi->page_dma)) {
Mitch Williams420136c2013-12-18 13:45:59 +0000816 rx_ring->rx_stats.alloc_page_failed++;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000817 bi->page_dma = 0;
818 goto no_buffers;
819 }
820 }
821
822 /* Refresh the desc even if buffer_addrs didn't change
823 * because each write-back erases this info.
824 */
825 rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
826 rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
827 } else {
828 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
829 rx_desc->read.hdr_addr = 0;
830 }
831 i++;
832 if (i == rx_ring->count)
833 i = 0;
834 }
835
836no_buffers:
837 if (rx_ring->next_to_use != i)
838 i40e_release_rx_desc(rx_ring, i);
839}
840
841/**
842 * i40e_receive_skb - Send a completed packet up the stack
843 * @rx_ring: rx ring in play
844 * @skb: packet to send up
845 * @vlan_tag: vlan tag for packet
846 **/
847static void i40e_receive_skb(struct i40e_ring *rx_ring,
848 struct sk_buff *skb, u16 vlan_tag)
849{
850 struct i40e_q_vector *q_vector = rx_ring->q_vector;
851 struct i40e_vsi *vsi = rx_ring->vsi;
852 u64 flags = vsi->back->flags;
853
854 if (vlan_tag & VLAN_VID_MASK)
855 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
856
857 if (flags & I40E_FLAG_IN_NETPOLL)
858 netif_rx(skb);
859 else
860 napi_gro_receive(&q_vector->napi, skb);
861}
862
863/**
864 * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
865 * @vsi: the VSI we care about
866 * @skb: skb currently being received and modified
867 * @rx_status: status value of last descriptor in packet
868 * @rx_error: error value of last descriptor in packet
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +0000869 * @rx_ptype: ptype value of last descriptor in packet
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000870 **/
871static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
872 struct sk_buff *skb,
873 u32 rx_status,
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +0000874 u32 rx_error,
875 u16 rx_ptype)
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000876{
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +0000877 bool ipv4_tunnel, ipv6_tunnel;
878 __wsum rx_udp_csum;
879 __sum16 csum;
880 struct iphdr *iph;
881
882 ipv4_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT4_MAC_PAY3) &&
883 (rx_ptype < I40E_RX_PTYPE_GRENAT4_MACVLAN_IPV6_ICMP_PAY4);
884 ipv6_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT6_MAC_PAY3) &&
885 (rx_ptype < I40E_RX_PTYPE_GRENAT6_MACVLAN_IPV6_ICMP_PAY4);
886
887 skb->encapsulation = ipv4_tunnel || ipv6_tunnel;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000888 skb->ip_summed = CHECKSUM_NONE;
889
890 /* Rx csum enabled and ip headers found? */
891 if (!(vsi->netdev->features & NETIF_F_RXCSUM &&
892 rx_status & (1 << I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
893 return;
894
Shannon Nelson8ee75a82013-12-21 05:44:46 +0000895 /* likely incorrect csum if alternate IP extention headers found */
896 if (rx_status & (1 << I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
897 return;
898
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +0000899 /* IP or L4 or outmost IP checksum error */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000900 if (rx_error & ((1 << I40E_RX_DESC_ERROR_IPE_SHIFT) |
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +0000901 (1 << I40E_RX_DESC_ERROR_L4E_SHIFT) |
902 (1 << I40E_RX_DESC_ERROR_EIPE_SHIFT))) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000903 vsi->back->hw_csum_rx_error++;
904 return;
905 }
906
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +0000907 if (ipv4_tunnel &&
908 !(rx_status & (1 << I40E_RX_DESC_STATUS_UDP_0_SHIFT))) {
909 /* If VXLAN traffic has an outer UDPv4 checksum we need to check
910 * it in the driver, hardware does not do it for us.
911 * Since L3L4P bit was set we assume a valid IHL value (>=5)
912 * so the total length of IPv4 header is IHL*4 bytes
913 */
914 skb->transport_header = skb->mac_header +
915 sizeof(struct ethhdr) +
916 (ip_hdr(skb)->ihl * 4);
917
918 /* Add 4 bytes for VLAN tagged packets */
919 skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
920 skb->protocol == htons(ETH_P_8021AD))
921 ? VLAN_HLEN : 0;
922
923 rx_udp_csum = udp_csum(skb);
924 iph = ip_hdr(skb);
925 csum = csum_tcpudp_magic(
926 iph->saddr, iph->daddr,
927 (skb->len - skb_transport_offset(skb)),
928 IPPROTO_UDP, rx_udp_csum);
929
930 if (udp_hdr(skb)->check != csum) {
931 vsi->back->hw_csum_rx_error++;
932 return;
933 }
934 }
935
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000936 skb->ip_summed = CHECKSUM_UNNECESSARY;
937}
938
939/**
940 * i40e_rx_hash - returns the hash value from the Rx descriptor
941 * @ring: descriptor ring
942 * @rx_desc: specific descriptor
943 **/
944static inline u32 i40e_rx_hash(struct i40e_ring *ring,
945 union i40e_rx_desc *rx_desc)
946{
Jesse Brandeburg8a494922013-11-20 10:02:49 +0000947 const __le64 rss_mask =
948 cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
949 I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
950
951 if ((ring->netdev->features & NETIF_F_RXHASH) &&
952 (rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask)
953 return le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
954 else
955 return 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000956}
957
958/**
959 * i40e_clean_rx_irq - Reclaim resources after receive completes
960 * @rx_ring: rx ring to clean
961 * @budget: how many cleans we're allowed
962 *
963 * Returns true if there's any budget left (e.g. the clean is finished)
964 **/
965static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
966{
967 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
968 u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
969 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
970 const int current_node = numa_node_id();
971 struct i40e_vsi *vsi = rx_ring->vsi;
972 u16 i = rx_ring->next_to_clean;
973 union i40e_rx_desc *rx_desc;
974 u32 rx_error, rx_status;
975 u64 qword;
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +0000976 u16 rx_ptype;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +0000977
978 rx_desc = I40E_RX_DESC(rx_ring, i);
979 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
980 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK)
981 >> I40E_RXD_QW1_STATUS_SHIFT;
982
983 while (rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)) {
984 union i40e_rx_desc *next_rxd;
985 struct i40e_rx_buffer *rx_bi;
986 struct sk_buff *skb;
987 u16 vlan_tag;
988 if (i40e_rx_is_programming_status(qword)) {
989 i40e_clean_programming_status(rx_ring, rx_desc);
990 I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
991 goto next_desc;
992 }
993 rx_bi = &rx_ring->rx_bi[i];
994 skb = rx_bi->skb;
995 prefetch(skb->data);
996
Mitch Williams829af3a2013-12-18 13:46:00 +0000997 rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
998 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
999 rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
1000 I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
1001 rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
1002 I40E_RXD_QW1_LENGTH_SPH_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001003
Mitch Williams829af3a2013-12-18 13:46:00 +00001004 rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
1005 I40E_RXD_QW1_ERROR_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001006 rx_hbo = rx_error & (1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
1007 rx_error &= ~(1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
1008
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001009 rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
1010 I40E_RXD_QW1_PTYPE_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001011 rx_bi->skb = NULL;
1012
1013 /* This memory barrier is needed to keep us from reading
1014 * any other fields out of the rx_desc until we know the
1015 * STATUS_DD bit is set
1016 */
1017 rmb();
1018
1019 /* Get the header and possibly the whole packet
1020 * If this is an skb from previous receive dma will be 0
1021 */
1022 if (rx_bi->dma) {
1023 u16 len;
1024
1025 if (rx_hbo)
1026 len = I40E_RX_HDR_SIZE;
1027 else if (rx_sph)
1028 len = rx_header_len;
1029 else if (rx_packet_len)
1030 len = rx_packet_len; /* 1buf/no split found */
1031 else
1032 len = rx_header_len; /* split always mode */
1033
1034 skb_put(skb, len);
1035 dma_unmap_single(rx_ring->dev,
1036 rx_bi->dma,
1037 rx_ring->rx_buf_len,
1038 DMA_FROM_DEVICE);
1039 rx_bi->dma = 0;
1040 }
1041
1042 /* Get the rest of the data if this was a header split */
1043 if (ring_is_ps_enabled(rx_ring) && rx_packet_len) {
1044
1045 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
1046 rx_bi->page,
1047 rx_bi->page_offset,
1048 rx_packet_len);
1049
1050 skb->len += rx_packet_len;
1051 skb->data_len += rx_packet_len;
1052 skb->truesize += rx_packet_len;
1053
1054 if ((page_count(rx_bi->page) == 1) &&
1055 (page_to_nid(rx_bi->page) == current_node))
1056 get_page(rx_bi->page);
1057 else
1058 rx_bi->page = NULL;
1059
1060 dma_unmap_page(rx_ring->dev,
1061 rx_bi->page_dma,
1062 PAGE_SIZE / 2,
1063 DMA_FROM_DEVICE);
1064 rx_bi->page_dma = 0;
1065 }
1066 I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
1067
1068 if (unlikely(
1069 !(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
1070 struct i40e_rx_buffer *next_buffer;
1071
1072 next_buffer = &rx_ring->rx_bi[i];
1073
1074 if (ring_is_ps_enabled(rx_ring)) {
1075 rx_bi->skb = next_buffer->skb;
1076 rx_bi->dma = next_buffer->dma;
1077 next_buffer->skb = skb;
1078 next_buffer->dma = 0;
1079 }
1080 rx_ring->rx_stats.non_eop_descs++;
1081 goto next_desc;
1082 }
1083
1084 /* ERR_MASK will only have valid bits if EOP set */
1085 if (unlikely(rx_error & (1 << I40E_RX_DESC_ERROR_RXE_SHIFT))) {
1086 dev_kfree_skb_any(skb);
1087 goto next_desc;
1088 }
1089
1090 skb->rxhash = i40e_rx_hash(rx_ring, rx_desc);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001091 /* probably a little skewed due to removing CRC */
1092 total_rx_bytes += skb->len;
1093 total_rx_packets++;
1094
1095 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
Joseph Gasparakis8144f0f2013-12-28 05:27:57 +00001096
1097 i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
1098
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001099 vlan_tag = rx_status & (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
1100 ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
1101 : 0;
1102 i40e_receive_skb(rx_ring, skb, vlan_tag);
1103
1104 rx_ring->netdev->last_rx = jiffies;
1105 budget--;
1106next_desc:
1107 rx_desc->wb.qword1.status_error_len = 0;
1108 if (!budget)
1109 break;
1110
1111 cleaned_count++;
1112 /* return some buffers to hardware, one at a time is too slow */
1113 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
1114 i40e_alloc_rx_buffers(rx_ring, cleaned_count);
1115 cleaned_count = 0;
1116 }
1117
1118 /* use prefetched values */
1119 rx_desc = next_rxd;
1120 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
Mitch Williams829af3a2013-12-18 13:46:00 +00001121 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
1122 I40E_RXD_QW1_STATUS_SHIFT;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001123 }
1124
1125 rx_ring->next_to_clean = i;
Alexander Duyck980e9b12013-09-28 06:01:03 +00001126 u64_stats_update_begin(&rx_ring->syncp);
Alexander Duycka114d0a2013-09-28 06:00:43 +00001127 rx_ring->stats.packets += total_rx_packets;
1128 rx_ring->stats.bytes += total_rx_bytes;
Alexander Duyck980e9b12013-09-28 06:01:03 +00001129 u64_stats_update_end(&rx_ring->syncp);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001130 rx_ring->q_vector->rx.total_packets += total_rx_packets;
1131 rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
1132
1133 if (cleaned_count)
1134 i40e_alloc_rx_buffers(rx_ring, cleaned_count);
1135
1136 return budget > 0;
1137}
1138
1139/**
1140 * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
1141 * @napi: napi struct with our devices info in it
1142 * @budget: amount of work driver is allowed to do this pass, in packets
1143 *
1144 * This function will clean all queues associated with a q_vector.
1145 *
1146 * Returns the amount of work done
1147 **/
1148int i40e_napi_poll(struct napi_struct *napi, int budget)
1149{
1150 struct i40e_q_vector *q_vector =
1151 container_of(napi, struct i40e_q_vector, napi);
1152 struct i40e_vsi *vsi = q_vector->vsi;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001153 struct i40e_ring *ring;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001154 bool clean_complete = true;
1155 int budget_per_ring;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001156
1157 if (test_bit(__I40E_DOWN, &vsi->state)) {
1158 napi_complete(napi);
1159 return 0;
1160 }
1161
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001162 /* Since the actual Tx work is minimal, we can give the Tx a larger
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001163 * budget and be more aggressive about cleaning up the Tx descriptors.
1164 */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001165 i40e_for_each_ring(ring, q_vector->tx)
1166 clean_complete &= i40e_clean_tx_irq(ring, vsi->work_limit);
1167
1168 /* We attempt to distribute budget to each Rx queue fairly, but don't
1169 * allow the budget to go below 1 because that would exit polling early.
1170 */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001171 budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00001172
1173 i40e_for_each_ring(ring, q_vector->rx)
1174 clean_complete &= i40e_clean_rx_irq(ring, budget_per_ring);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001175
1176 /* If work not completed, return budget and polling will return */
1177 if (!clean_complete)
1178 return budget;
1179
1180 /* Work is done so exit the polling mode and re-enable the interrupt */
1181 napi_complete(napi);
1182 if (ITR_IS_DYNAMIC(vsi->rx_itr_setting) ||
1183 ITR_IS_DYNAMIC(vsi->tx_itr_setting))
1184 i40e_update_dynamic_itr(q_vector);
1185
1186 if (!test_bit(__I40E_DOWN, &vsi->state)) {
1187 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
1188 i40e_irq_dynamic_enable(vsi,
1189 q_vector->v_idx + vsi->base_vector);
1190 } else {
1191 struct i40e_hw *hw = &vsi->back->hw;
1192 /* We re-enable the queue 0 cause, but
1193 * don't worry about dynamic_enable
1194 * because we left it on for the other
1195 * possible interrupts during napi
1196 */
1197 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
1198 qval |= I40E_QINT_RQCTL_CAUSE_ENA_MASK;
1199 wr32(hw, I40E_QINT_RQCTL(0), qval);
1200
1201 qval = rd32(hw, I40E_QINT_TQCTL(0));
1202 qval |= I40E_QINT_TQCTL_CAUSE_ENA_MASK;
1203 wr32(hw, I40E_QINT_TQCTL(0), qval);
Shannon Nelson116a57d2013-09-28 07:13:59 +00001204
1205 i40e_irq_dynamic_enable_icr0(vsi->back);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001206 }
1207 }
1208
1209 return 0;
1210}
1211
1212/**
1213 * i40e_atr - Add a Flow Director ATR filter
1214 * @tx_ring: ring to add programming descriptor to
1215 * @skb: send buffer
1216 * @flags: send flags
1217 * @protocol: wire protocol
1218 **/
1219static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
1220 u32 flags, __be16 protocol)
1221{
1222 struct i40e_filter_program_desc *fdir_desc;
1223 struct i40e_pf *pf = tx_ring->vsi->back;
1224 union {
1225 unsigned char *network;
1226 struct iphdr *ipv4;
1227 struct ipv6hdr *ipv6;
1228 } hdr;
1229 struct tcphdr *th;
1230 unsigned int hlen;
1231 u32 flex_ptype, dtype_cmd;
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001232 u16 i;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001233
1234 /* make sure ATR is enabled */
1235 if (!(pf->flags & I40E_FLAG_FDIR_ATR_ENABLED))
1236 return;
1237
1238 /* if sampling is disabled do nothing */
1239 if (!tx_ring->atr_sample_rate)
1240 return;
1241
1242 tx_ring->atr_count++;
1243
1244 /* snag network header to get L4 type and address */
1245 hdr.network = skb_network_header(skb);
1246
1247 /* Currently only IPv4/IPv6 with TCP is supported */
1248 if (protocol == htons(ETH_P_IP)) {
1249 if (hdr.ipv4->protocol != IPPROTO_TCP)
1250 return;
1251
1252 /* access ihl as a u8 to avoid unaligned access on ia64 */
1253 hlen = (hdr.network[0] & 0x0F) << 2;
1254 } else if (protocol == htons(ETH_P_IPV6)) {
1255 if (hdr.ipv6->nexthdr != IPPROTO_TCP)
1256 return;
1257
1258 hlen = sizeof(struct ipv6hdr);
1259 } else {
1260 return;
1261 }
1262
1263 th = (struct tcphdr *)(hdr.network + hlen);
1264
1265 /* sample on all syn/fin packets or once every atr sample rate */
1266 if (!th->fin && !th->syn && (tx_ring->atr_count < tx_ring->atr_sample_rate))
1267 return;
1268
1269 tx_ring->atr_count = 0;
1270
1271 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001272 i = tx_ring->next_to_use;
1273 fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
1274
1275 i++;
1276 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001277
1278 flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
1279 I40E_TXD_FLTR_QW0_QINDEX_MASK;
1280 flex_ptype |= (protocol == htons(ETH_P_IP)) ?
1281 (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
1282 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
1283 (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
1284 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
1285
1286 flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
1287
1288 dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
1289
1290 dtype_cmd |= th->fin ?
1291 (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
1292 I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
1293 (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
1294 I40E_TXD_FLTR_QW1_PCMD_SHIFT);
1295
1296 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
1297 I40E_TXD_FLTR_QW1_DEST_SHIFT;
1298
1299 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
1300 I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
1301
1302 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
1303 fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
1304}
1305
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001306/**
1307 * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
1308 * @skb: send buffer
1309 * @tx_ring: ring to send buffer on
1310 * @flags: the tx flags to be set
1311 *
1312 * Checks the skb and set up correspondingly several generic transmit flags
1313 * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
1314 *
1315 * Returns error code indicate the frame should be dropped upon error and the
1316 * otherwise returns 0 to indicate the flags has been set properly.
1317 **/
1318static int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
1319 struct i40e_ring *tx_ring,
1320 u32 *flags)
1321{
1322 __be16 protocol = skb->protocol;
1323 u32 tx_flags = 0;
1324
1325 /* if we have a HW VLAN tag being added, default to the HW one */
1326 if (vlan_tx_tag_present(skb)) {
1327 tx_flags |= vlan_tx_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
1328 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1329 /* else if it is a SW VLAN, check the next protocol and store the tag */
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00001330 } else if (protocol == htons(ETH_P_8021Q)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001331 struct vlan_hdr *vhdr, _vhdr;
1332 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
1333 if (!vhdr)
1334 return -EINVAL;
1335
1336 protocol = vhdr->h_vlan_encapsulated_proto;
1337 tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
1338 tx_flags |= I40E_TX_FLAGS_SW_VLAN;
1339 }
1340
1341 /* Insert 802.1p priority into VLAN header */
1342 if ((tx_ring->vsi->back->flags & I40E_FLAG_DCB_ENABLED) &&
1343 ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
1344 (skb->priority != TC_PRIO_CONTROL))) {
1345 tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
1346 tx_flags |= (skb->priority & 0x7) <<
1347 I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
1348 if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
1349 struct vlan_ethhdr *vhdr;
1350 if (skb_header_cloned(skb) &&
1351 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
1352 return -ENOMEM;
1353 vhdr = (struct vlan_ethhdr *)skb->data;
1354 vhdr->h_vlan_TCI = htons(tx_flags >>
1355 I40E_TX_FLAGS_VLAN_SHIFT);
1356 } else {
1357 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1358 }
1359 }
1360 *flags = tx_flags;
1361 return 0;
1362}
1363
1364/**
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001365 * i40e_tso - set up the tso context descriptor
1366 * @tx_ring: ptr to the ring to send
1367 * @skb: ptr to the skb we're sending
1368 * @tx_flags: the collected send information
1369 * @protocol: the send protocol
1370 * @hdr_len: ptr to the size of the packet header
1371 * @cd_tunneling: ptr to context descriptor bits
1372 *
1373 * Returns 0 if no TSO can happen, 1 if tso is going, or error
1374 **/
1375static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
1376 u32 tx_flags, __be16 protocol, u8 *hdr_len,
1377 u64 *cd_type_cmd_tso_mss, u32 *cd_tunneling)
1378{
1379 u32 cd_cmd, cd_tso_len, cd_mss;
1380 struct tcphdr *tcph;
1381 struct iphdr *iph;
1382 u32 l4len;
1383 int err;
1384 struct ipv6hdr *ipv6h;
1385
1386 if (!skb_is_gso(skb))
1387 return 0;
1388
1389 if (skb_header_cloned(skb)) {
1390 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1391 if (err)
1392 return err;
1393 }
1394
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00001395 if (protocol == htons(ETH_P_IP)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001396 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
1397 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1398 iph->tot_len = 0;
1399 iph->check = 0;
1400 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1401 0, IPPROTO_TCP, 0);
1402 } else if (skb_is_gso_v6(skb)) {
1403
1404 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb)
1405 : ipv6_hdr(skb);
1406 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1407 ipv6h->payload_len = 0;
1408 tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
1409 0, IPPROTO_TCP, 0);
1410 }
1411
1412 l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
1413 *hdr_len = (skb->encapsulation
1414 ? (skb_inner_transport_header(skb) - skb->data)
1415 : skb_transport_offset(skb)) + l4len;
1416
1417 /* find the field values */
1418 cd_cmd = I40E_TX_CTX_DESC_TSO;
1419 cd_tso_len = skb->len - *hdr_len;
1420 cd_mss = skb_shinfo(skb)->gso_size;
Mitch Williams829af3a2013-12-18 13:46:00 +00001421 *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
1422 ((u64)cd_tso_len <<
1423 I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
1424 ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001425 return 1;
1426}
1427
1428/**
1429 * i40e_tx_enable_csum - Enable Tx checksum offloads
1430 * @skb: send buffer
1431 * @tx_flags: Tx flags currently set
1432 * @td_cmd: Tx descriptor command bits to set
1433 * @td_offset: Tx descriptor header offsets to set
1434 * @cd_tunneling: ptr to context desc bits
1435 **/
1436static void i40e_tx_enable_csum(struct sk_buff *skb, u32 tx_flags,
1437 u32 *td_cmd, u32 *td_offset,
1438 struct i40e_ring *tx_ring,
1439 u32 *cd_tunneling)
1440{
1441 struct ipv6hdr *this_ipv6_hdr;
1442 unsigned int this_tcp_hdrlen;
1443 struct iphdr *this_ip_hdr;
1444 u32 network_hdr_len;
1445 u8 l4_hdr = 0;
1446
1447 if (skb->encapsulation) {
1448 network_hdr_len = skb_inner_network_header_len(skb);
1449 this_ip_hdr = inner_ip_hdr(skb);
1450 this_ipv6_hdr = inner_ipv6_hdr(skb);
1451 this_tcp_hdrlen = inner_tcp_hdrlen(skb);
1452
1453 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1454
1455 if (tx_flags & I40E_TX_FLAGS_TSO) {
1456 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
1457 ip_hdr(skb)->check = 0;
1458 } else {
1459 *cd_tunneling |=
1460 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1461 }
1462 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1463 if (tx_flags & I40E_TX_FLAGS_TSO) {
1464 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
1465 ip_hdr(skb)->check = 0;
1466 } else {
1467 *cd_tunneling |=
1468 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1469 }
1470 }
1471
1472 /* Now set the ctx descriptor fields */
1473 *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
1474 I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
1475 I40E_TXD_CTX_UDP_TUNNELING |
1476 ((skb_inner_network_offset(skb) -
1477 skb_transport_offset(skb)) >> 1) <<
1478 I40E_TXD_CTX_QW0_NATLEN_SHIFT;
1479
1480 } else {
1481 network_hdr_len = skb_network_header_len(skb);
1482 this_ip_hdr = ip_hdr(skb);
1483 this_ipv6_hdr = ipv6_hdr(skb);
1484 this_tcp_hdrlen = tcp_hdrlen(skb);
1485 }
1486
1487 /* Enable IP checksum offloads */
1488 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1489 l4_hdr = this_ip_hdr->protocol;
1490 /* the stack computes the IP header already, the only time we
1491 * need the hardware to recompute it is in the case of TSO.
1492 */
1493 if (tx_flags & I40E_TX_FLAGS_TSO) {
1494 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
1495 this_ip_hdr->check = 0;
1496 } else {
1497 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
1498 }
1499 /* Now set the td_offset for IP header length */
1500 *td_offset = (network_hdr_len >> 2) <<
1501 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1502 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1503 l4_hdr = this_ipv6_hdr->nexthdr;
1504 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
1505 /* Now set the td_offset for IP header length */
1506 *td_offset = (network_hdr_len >> 2) <<
1507 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1508 }
1509 /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
1510 *td_offset |= (skb_network_offset(skb) >> 1) <<
1511 I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
1512
1513 /* Enable L4 checksum offloads */
1514 switch (l4_hdr) {
1515 case IPPROTO_TCP:
1516 /* enable checksum offloads */
1517 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
1518 *td_offset |= (this_tcp_hdrlen >> 2) <<
1519 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1520 break;
1521 case IPPROTO_SCTP:
1522 /* enable SCTP checksum offload */
1523 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
1524 *td_offset |= (sizeof(struct sctphdr) >> 2) <<
1525 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1526 break;
1527 case IPPROTO_UDP:
1528 /* enable UDP checksum offload */
1529 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
1530 *td_offset |= (sizeof(struct udphdr) >> 2) <<
1531 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1532 break;
1533 default:
1534 break;
1535 }
1536}
1537
1538/**
1539 * i40e_create_tx_ctx Build the Tx context descriptor
1540 * @tx_ring: ring to create the descriptor on
1541 * @cd_type_cmd_tso_mss: Quad Word 1
1542 * @cd_tunneling: Quad Word 0 - bits 0-31
1543 * @cd_l2tag2: Quad Word 0 - bits 32-63
1544 **/
1545static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
1546 const u64 cd_type_cmd_tso_mss,
1547 const u32 cd_tunneling, const u32 cd_l2tag2)
1548{
1549 struct i40e_tx_context_desc *context_desc;
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001550 int i = tx_ring->next_to_use;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001551
1552 if (!cd_type_cmd_tso_mss && !cd_tunneling && !cd_l2tag2)
1553 return;
1554
1555 /* grab the next descriptor */
Alexander Duyckfc4ac672013-09-28 06:00:22 +00001556 context_desc = I40E_TX_CTXTDESC(tx_ring, i);
1557
1558 i++;
1559 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001560
1561 /* cpu_to_le32 and assign to struct fields */
1562 context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
1563 context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
1564 context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
1565}
1566
1567/**
1568 * i40e_tx_map - Build the Tx descriptor
1569 * @tx_ring: ring to send buffer on
1570 * @skb: send buffer
1571 * @first: first buffer info buffer to use
1572 * @tx_flags: collected send information
1573 * @hdr_len: size of the packet header
1574 * @td_cmd: the command field in the descriptor
1575 * @td_offset: offset for checksum or crc
1576 **/
1577static void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
1578 struct i40e_tx_buffer *first, u32 tx_flags,
1579 const u8 hdr_len, u32 td_cmd, u32 td_offset)
1580{
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001581 unsigned int data_len = skb->data_len;
1582 unsigned int size = skb_headlen(skb);
Alexander Duycka5e9c572013-09-28 06:00:27 +00001583 struct skb_frag_struct *frag;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001584 struct i40e_tx_buffer *tx_bi;
1585 struct i40e_tx_desc *tx_desc;
Alexander Duycka5e9c572013-09-28 06:00:27 +00001586 u16 i = tx_ring->next_to_use;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001587 u32 td_tag = 0;
1588 dma_addr_t dma;
1589 u16 gso_segs;
1590
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001591 if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
1592 td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
1593 td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
1594 I40E_TX_FLAGS_VLAN_SHIFT;
1595 }
1596
Alexander Duycka5e9c572013-09-28 06:00:27 +00001597 if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
1598 gso_segs = skb_shinfo(skb)->gso_segs;
1599 else
1600 gso_segs = 1;
1601
1602 /* multiply data chunks by size of headers */
1603 first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
1604 first->gso_segs = gso_segs;
1605 first->skb = skb;
1606 first->tx_flags = tx_flags;
1607
1608 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1609
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001610 tx_desc = I40E_TX_DESC(tx_ring, i);
Alexander Duycka5e9c572013-09-28 06:00:27 +00001611 tx_bi = first;
1612
1613 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1614 if (dma_mapping_error(tx_ring->dev, dma))
1615 goto dma_error;
1616
1617 /* record length, and DMA address */
1618 dma_unmap_len_set(tx_bi, len, size);
1619 dma_unmap_addr_set(tx_bi, dma, dma);
1620
1621 tx_desc->buffer_addr = cpu_to_le64(dma);
1622
1623 while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001624 tx_desc->cmd_type_offset_bsz =
1625 build_ctob(td_cmd, td_offset,
1626 I40E_MAX_DATA_PER_TXD, td_tag);
1627
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001628 tx_desc++;
1629 i++;
1630 if (i == tx_ring->count) {
1631 tx_desc = I40E_TX_DESC(tx_ring, 0);
1632 i = 0;
1633 }
Alexander Duycka5e9c572013-09-28 06:00:27 +00001634
1635 dma += I40E_MAX_DATA_PER_TXD;
1636 size -= I40E_MAX_DATA_PER_TXD;
1637
1638 tx_desc->buffer_addr = cpu_to_le64(dma);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001639 }
1640
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001641 if (likely(!data_len))
1642 break;
1643
Alexander Duycka5e9c572013-09-28 06:00:27 +00001644 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
1645 size, td_tag);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001646
1647 tx_desc++;
1648 i++;
1649 if (i == tx_ring->count) {
1650 tx_desc = I40E_TX_DESC(tx_ring, 0);
1651 i = 0;
1652 }
1653
Alexander Duycka5e9c572013-09-28 06:00:27 +00001654 size = skb_frag_size(frag);
1655 data_len -= size;
1656
1657 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
1658 DMA_TO_DEVICE);
1659
1660 tx_bi = &tx_ring->tx_bi[i];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001661 }
1662
Alexander Duycka5e9c572013-09-28 06:00:27 +00001663 tx_desc->cmd_type_offset_bsz =
1664 build_ctob(td_cmd, td_offset, size, td_tag) |
1665 cpu_to_le64((u64)I40E_TXD_CMD << I40E_TXD_QW1_CMD_SHIFT);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001666
Alexander Duyck7070ce02013-09-28 06:00:37 +00001667 netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
1668 tx_ring->queue_index),
1669 first->bytecount);
1670
Alexander Duycka5e9c572013-09-28 06:00:27 +00001671 /* set the timestamp */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001672 first->time_stamp = jiffies;
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001673
1674 /* Force memory writes to complete before letting h/w
1675 * know there are new descriptors to fetch. (Only
1676 * applicable for weak-ordered memory model archs,
1677 * such as IA-64).
1678 */
1679 wmb();
1680
Alexander Duycka5e9c572013-09-28 06:00:27 +00001681 /* set next_to_watch value indicating a packet is present */
1682 first->next_to_watch = tx_desc;
1683
1684 i++;
1685 if (i == tx_ring->count)
1686 i = 0;
1687
1688 tx_ring->next_to_use = i;
1689
1690 /* notify HW of packet */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001691 writel(i, tx_ring->tail);
Alexander Duycka5e9c572013-09-28 06:00:27 +00001692
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001693 return;
1694
1695dma_error:
Alexander Duycka5e9c572013-09-28 06:00:27 +00001696 dev_info(tx_ring->dev, "TX DMA map failed\n");
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001697
1698 /* clear dma mappings for failed tx_bi map */
1699 for (;;) {
1700 tx_bi = &tx_ring->tx_bi[i];
Alexander Duycka5e9c572013-09-28 06:00:27 +00001701 i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001702 if (tx_bi == first)
1703 break;
1704 if (i == 0)
1705 i = tx_ring->count;
1706 i--;
1707 }
1708
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001709 tx_ring->next_to_use = i;
1710}
1711
1712/**
1713 * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
1714 * @tx_ring: the ring to be checked
1715 * @size: the size buffer we want to assure is available
1716 *
1717 * Returns -EBUSY if a stop is needed, else 0
1718 **/
1719static inline int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
1720{
1721 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
Greg Rose8e9dca52013-12-18 13:45:53 +00001722 /* Memory barrier before checking head and tail */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001723 smp_mb();
1724
1725 /* Check again in a case another CPU has just made room available. */
1726 if (likely(I40E_DESC_UNUSED(tx_ring) < size))
1727 return -EBUSY;
1728
1729 /* A reprieve! - use start_queue because it doesn't call schedule */
1730 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
1731 ++tx_ring->tx_stats.restart_queue;
1732 return 0;
1733}
1734
1735/**
1736 * i40e_maybe_stop_tx - 1st level check for tx stop conditions
1737 * @tx_ring: the ring to be checked
1738 * @size: the size buffer we want to assure is available
1739 *
1740 * Returns 0 if stop is not needed
1741 **/
1742static int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
1743{
1744 if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
1745 return 0;
1746 return __i40e_maybe_stop_tx(tx_ring, size);
1747}
1748
1749/**
1750 * i40e_xmit_descriptor_count - calculate number of tx descriptors needed
1751 * @skb: send buffer
1752 * @tx_ring: ring to send buffer on
1753 *
1754 * Returns number of data descriptors needed for this skb. Returns 0 to indicate
1755 * there is not enough descriptors available in this ring since we need at least
1756 * one descriptor.
1757 **/
1758static int i40e_xmit_descriptor_count(struct sk_buff *skb,
1759 struct i40e_ring *tx_ring)
1760{
1761#if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
1762 unsigned int f;
1763#endif
1764 int count = 0;
1765
1766 /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
1767 * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
1768 * + 2 desc gap to keep tail from touching head,
1769 * + 1 desc for context descriptor,
1770 * otherwise try next time
1771 */
1772#if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
1773 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1774 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
1775#else
1776 count += skb_shinfo(skb)->nr_frags;
1777#endif
1778 count += TXD_USE_COUNT(skb_headlen(skb));
1779 if (i40e_maybe_stop_tx(tx_ring, count + 3)) {
1780 tx_ring->tx_stats.tx_busy++;
1781 return 0;
1782 }
1783 return count;
1784}
1785
1786/**
1787 * i40e_xmit_frame_ring - Sends buffer on Tx ring
1788 * @skb: send buffer
1789 * @tx_ring: ring to send buffer on
1790 *
1791 * Returns NETDEV_TX_OK if sent, else an error code
1792 **/
1793static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
1794 struct i40e_ring *tx_ring)
1795{
1796 u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
1797 u32 cd_tunneling = 0, cd_l2tag2 = 0;
1798 struct i40e_tx_buffer *first;
1799 u32 td_offset = 0;
1800 u32 tx_flags = 0;
1801 __be16 protocol;
1802 u32 td_cmd = 0;
1803 u8 hdr_len = 0;
1804 int tso;
1805 if (0 == i40e_xmit_descriptor_count(skb, tx_ring))
1806 return NETDEV_TX_BUSY;
1807
1808 /* prepare the xmit flags */
1809 if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
1810 goto out_drop;
1811
1812 /* obtain protocol of skb */
1813 protocol = skb->protocol;
1814
1815 /* record the location of the first descriptor for this packet */
1816 first = &tx_ring->tx_bi[tx_ring->next_to_use];
1817
1818 /* setup IPv4/IPv6 offloads */
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00001819 if (protocol == htons(ETH_P_IP))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001820 tx_flags |= I40E_TX_FLAGS_IPV4;
Jesse Brandeburg0e2fe46c2013-11-28 06:39:29 +00001821 else if (protocol == htons(ETH_P_IPV6))
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001822 tx_flags |= I40E_TX_FLAGS_IPV6;
1823
1824 tso = i40e_tso(tx_ring, skb, tx_flags, protocol, &hdr_len,
1825 &cd_type_cmd_tso_mss, &cd_tunneling);
1826
1827 if (tso < 0)
1828 goto out_drop;
1829 else if (tso)
1830 tx_flags |= I40E_TX_FLAGS_TSO;
1831
1832 skb_tx_timestamp(skb);
1833
Alexander Duyckb1941302013-09-28 06:00:32 +00001834 /* always enable CRC insertion offload */
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001835 td_cmd |= I40E_TX_DESC_CMD_ICRC;
1836
Alexander Duyckb1941302013-09-28 06:00:32 +00001837 /* Always offload the checksum, since it's in the data descriptor */
1838 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1839 tx_flags |= I40E_TX_FLAGS_CSUM;
1840
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001841 i40e_tx_enable_csum(skb, tx_flags, &td_cmd, &td_offset,
1842 tx_ring, &cd_tunneling);
Alexander Duyckb1941302013-09-28 06:00:32 +00001843 }
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001844
1845 i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
1846 cd_tunneling, cd_l2tag2);
1847
1848 /* Add Flow Director ATR if it's enabled.
1849 *
1850 * NOTE: this must always be directly before the data descriptor.
1851 */
1852 i40e_atr(tx_ring, skb, tx_flags, protocol);
1853
1854 i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
1855 td_cmd, td_offset);
1856
1857 i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
1858
1859 return NETDEV_TX_OK;
1860
1861out_drop:
1862 dev_kfree_skb_any(skb);
1863 return NETDEV_TX_OK;
1864}
1865
1866/**
1867 * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
1868 * @skb: send buffer
1869 * @netdev: network interface device structure
1870 *
1871 * Returns NETDEV_TX_OK if sent, else an error code
1872 **/
1873netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1874{
1875 struct i40e_netdev_priv *np = netdev_priv(netdev);
1876 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck9f65e152013-09-28 06:00:58 +00001877 struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
Jesse Brandeburgfd0a05c2013-09-11 08:39:51 +00001878
1879 /* hardware can't handle really short frames, hardware padding works
1880 * beyond this point
1881 */
1882 if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
1883 if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
1884 return NETDEV_TX_OK;
1885 skb->len = I40E_MIN_TX_LEN;
1886 skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
1887 }
1888
1889 return i40e_xmit_frame_ring(skb, tx_ring);
1890}