]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/ethernet/intel/i40e/i40e_txrx.c
i40e: Drop dead code and flags from Tx hotpath
[mirror_ubuntu-artful-kernel.git] / drivers / net / ethernet / intel / i40e / i40e_txrx.c
CommitLineData
fd0a05ce
JB
1/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
4 * Copyright(c) 2013 Intel Corporation.
5 *
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 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
21 *
22 * Contact Information:
23 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 *
26 ******************************************************************************/
27
28#include "i40e.h"
29
30static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
31 u32 td_tag)
32{
33 return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
34 ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
35 ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
36 ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
37 ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
38}
39
40/**
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;
53 struct i40e_vsi *vsi;
54 struct device *dev;
55 dma_addr_t dma;
56 u32 td_cmd = 0;
57 u16 i;
58
59 /* find existing FDIR VSI */
60 vsi = NULL;
61 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
62 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR)
63 vsi = pf->vsi[i];
64 if (!vsi)
65 return -ENOENT;
66
67 tx_ring = &vsi->tx_rings[0];
68 dev = tx_ring->dev;
69
70 dma = dma_map_single(dev, fdir_data->raw_packet,
71 I40E_FDIR_MAX_RAW_PACKET_LOOKUP, DMA_TO_DEVICE);
72 if (dma_mapping_error(dev, dma))
73 goto dma_fail;
74
75 /* grab the next descriptor */
fc4ac67b
AD
76 i = tx_ring->next_to_use;
77 fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
78 tx_buf = &tx_ring->tx_bi[i];
79
80 i++;
81 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
fd0a05ce
JB
82
83 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32((fdir_data->q_index
84 << I40E_TXD_FLTR_QW0_QINDEX_SHIFT)
85 & I40E_TXD_FLTR_QW0_QINDEX_MASK);
86
87 fdir_desc->qindex_flex_ptype_vsi |= cpu_to_le32((fdir_data->flex_off
88 << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT)
89 & I40E_TXD_FLTR_QW0_FLEXOFF_MASK);
90
91 fdir_desc->qindex_flex_ptype_vsi |= cpu_to_le32((fdir_data->pctype
92 << I40E_TXD_FLTR_QW0_PCTYPE_SHIFT)
93 & I40E_TXD_FLTR_QW0_PCTYPE_MASK);
94
95 /* Use LAN VSI Id if not programmed by user */
96 if (fdir_data->dest_vsi == 0)
97 fdir_desc->qindex_flex_ptype_vsi |=
98 cpu_to_le32((pf->vsi[pf->lan_vsi]->id)
99 << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT);
100 else
101 fdir_desc->qindex_flex_ptype_vsi |=
102 cpu_to_le32((fdir_data->dest_vsi
103 << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT)
104 & I40E_TXD_FLTR_QW0_DEST_VSI_MASK);
105
106 fdir_desc->dtype_cmd_cntindex =
107 cpu_to_le32(I40E_TX_DESC_DTYPE_FILTER_PROG);
108
109 if (add)
110 fdir_desc->dtype_cmd_cntindex |= cpu_to_le32(
111 I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE
112 << I40E_TXD_FLTR_QW1_PCMD_SHIFT);
113 else
114 fdir_desc->dtype_cmd_cntindex |= cpu_to_le32(
115 I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE
116 << I40E_TXD_FLTR_QW1_PCMD_SHIFT);
117
118 fdir_desc->dtype_cmd_cntindex |= cpu_to_le32((fdir_data->dest_ctl
119 << I40E_TXD_FLTR_QW1_DEST_SHIFT)
120 & I40E_TXD_FLTR_QW1_DEST_MASK);
121
122 fdir_desc->dtype_cmd_cntindex |= cpu_to_le32(
123 (fdir_data->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT)
124 & I40E_TXD_FLTR_QW1_FD_STATUS_MASK);
125
126 if (fdir_data->cnt_index != 0) {
127 fdir_desc->dtype_cmd_cntindex |=
128 cpu_to_le32(I40E_TXD_FLTR_QW1_CNT_ENA_MASK);
129 fdir_desc->dtype_cmd_cntindex |=
130 cpu_to_le32((fdir_data->cnt_index
131 << I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT)
132 & I40E_TXD_FLTR_QW1_CNTINDEX_MASK);
133 }
134
135 fdir_desc->fd_id = cpu_to_le32(fdir_data->fd_id);
136
137 /* Now program a dummy descriptor */
fc4ac67b
AD
138 i = tx_ring->next_to_use;
139 tx_desc = I40E_TX_DESC(tx_ring, i);
140
141 i++;
142 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
fd0a05ce
JB
143
144 tx_desc->buffer_addr = cpu_to_le64(dma);
145 td_cmd = I40E_TX_DESC_CMD_EOP |
146 I40E_TX_DESC_CMD_RS |
147 I40E_TX_DESC_CMD_DUMMY;
148
149 tx_desc->cmd_type_offset_bsz =
150 build_ctob(td_cmd, 0, I40E_FDIR_MAX_RAW_PACKET_LOOKUP, 0);
151
fd0a05ce
JB
152 /* Force memory writes to complete before letting h/w
153 * know there are new descriptors to fetch. (Only
154 * applicable for weak-ordered memory model archs,
155 * such as IA-64).
156 */
157 wmb();
158
fc4ac67b
AD
159 /* Mark the data descriptor to be watched */
160 tx_buf->next_to_watch = tx_desc;
161
fd0a05ce
JB
162 writel(tx_ring->next_to_use, tx_ring->tail);
163 return 0;
164
165dma_fail:
166 return -1;
167}
168
169/**
170 * i40e_fd_handle_status - check the Programming Status for FD
171 * @rx_ring: the Rx ring for this descriptor
172 * @qw: the descriptor data
173 * @prog_id: the id originally used for programming
174 *
175 * This is used to verify if the FD programming or invalidation
176 * requested by SW to the HW is successful or not and take actions accordingly.
177 **/
178static void i40e_fd_handle_status(struct i40e_ring *rx_ring, u32 qw, u8 prog_id)
179{
180 struct pci_dev *pdev = rx_ring->vsi->back->pdev;
181 u32 error;
182
183 error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
184 I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
185
186 /* for now just print the Status */
187 dev_info(&pdev->dev, "FD programming id %02x, Status %08x\n",
188 prog_id, error);
189}
190
191/**
a5e9c572 192 * i40e_unmap_and_free_tx_resource - Release a Tx buffer
fd0a05ce
JB
193 * @ring: the ring that owns the buffer
194 * @tx_buffer: the buffer to free
195 **/
a5e9c572
AD
196static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
197 struct i40e_tx_buffer *tx_buffer)
fd0a05ce 198{
a5e9c572
AD
199 if (tx_buffer->skb) {
200 dev_kfree_skb_any(tx_buffer->skb);
201 if (dma_unmap_len(tx_buffer, len))
fd0a05ce 202 dma_unmap_single(ring->dev,
35a1e2ad
AD
203 dma_unmap_addr(tx_buffer, dma),
204 dma_unmap_len(tx_buffer, len),
fd0a05ce 205 DMA_TO_DEVICE);
a5e9c572
AD
206 } else if (dma_unmap_len(tx_buffer, len)) {
207 dma_unmap_page(ring->dev,
208 dma_unmap_addr(tx_buffer, dma),
209 dma_unmap_len(tx_buffer, len),
210 DMA_TO_DEVICE);
fd0a05ce 211 }
a5e9c572
AD
212 tx_buffer->next_to_watch = NULL;
213 tx_buffer->skb = NULL;
35a1e2ad 214 dma_unmap_len_set(tx_buffer, len, 0);
a5e9c572 215 /* tx_buffer must be completely set up in the transmit path */
fd0a05ce
JB
216}
217
218/**
219 * i40e_clean_tx_ring - Free any empty Tx buffers
220 * @tx_ring: ring to be cleaned
221 **/
222void i40e_clean_tx_ring(struct i40e_ring *tx_ring)
223{
fd0a05ce
JB
224 unsigned long bi_size;
225 u16 i;
226
227 /* ring already cleared, nothing to do */
228 if (!tx_ring->tx_bi)
229 return;
230
231 /* Free all the Tx ring sk_buffs */
a5e9c572
AD
232 for (i = 0; i < tx_ring->count; i++)
233 i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
fd0a05ce
JB
234
235 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
236 memset(tx_ring->tx_bi, 0, bi_size);
237
238 /* Zero out the descriptor ring */
239 memset(tx_ring->desc, 0, tx_ring->size);
240
241 tx_ring->next_to_use = 0;
242 tx_ring->next_to_clean = 0;
243}
244
245/**
246 * i40e_free_tx_resources - Free Tx resources per queue
247 * @tx_ring: Tx descriptor ring for a specific queue
248 *
249 * Free all transmit software resources
250 **/
251void i40e_free_tx_resources(struct i40e_ring *tx_ring)
252{
253 i40e_clean_tx_ring(tx_ring);
254 kfree(tx_ring->tx_bi);
255 tx_ring->tx_bi = NULL;
256
257 if (tx_ring->desc) {
258 dma_free_coherent(tx_ring->dev, tx_ring->size,
259 tx_ring->desc, tx_ring->dma);
260 tx_ring->desc = NULL;
261 }
262}
263
264/**
265 * i40e_get_tx_pending - how many tx descriptors not processed
266 * @tx_ring: the ring of descriptors
267 *
268 * Since there is no access to the ring head register
269 * in XL710, we need to use our local copies
270 **/
271static u32 i40e_get_tx_pending(struct i40e_ring *ring)
272{
273 u32 ntu = ((ring->next_to_clean <= ring->next_to_use)
274 ? ring->next_to_use
275 : ring->next_to_use + ring->count);
276 return ntu - ring->next_to_clean;
277}
278
279/**
280 * i40e_check_tx_hang - Is there a hang in the Tx queue
281 * @tx_ring: the ring of descriptors
282 **/
283static bool i40e_check_tx_hang(struct i40e_ring *tx_ring)
284{
285 u32 tx_pending = i40e_get_tx_pending(tx_ring);
286 bool ret = false;
287
288 clear_check_for_tx_hang(tx_ring);
289
290 /* Check for a hung queue, but be thorough. This verifies
291 * that a transmit has been completed since the previous
292 * check AND there is at least one packet pending. The
293 * ARMED bit is set to indicate a potential hang. The
294 * bit is cleared if a pause frame is received to remove
295 * false hang detection due to PFC or 802.3x frames. By
296 * requiring this to fail twice we avoid races with
297 * PFC clearing the ARMED bit and conditions where we
298 * run the check_tx_hang logic with a transmit completion
299 * pending but without time to complete it yet.
300 */
301 if ((tx_ring->tx_stats.tx_done_old == tx_ring->tx_stats.packets) &&
302 tx_pending) {
303 /* make sure it is true for two checks in a row */
304 ret = test_and_set_bit(__I40E_HANG_CHECK_ARMED,
305 &tx_ring->state);
306 } else {
307 /* update completed stats and disarm the hang check */
308 tx_ring->tx_stats.tx_done_old = tx_ring->tx_stats.packets;
309 clear_bit(__I40E_HANG_CHECK_ARMED, &tx_ring->state);
310 }
311
312 return ret;
313}
314
315/**
316 * i40e_clean_tx_irq - Reclaim resources after transmit completes
317 * @tx_ring: tx ring to clean
318 * @budget: how many cleans we're allowed
319 *
320 * Returns true if there's any budget left (e.g. the clean is finished)
321 **/
322static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
323{
324 u16 i = tx_ring->next_to_clean;
325 struct i40e_tx_buffer *tx_buf;
326 struct i40e_tx_desc *tx_desc;
327 unsigned int total_packets = 0;
328 unsigned int total_bytes = 0;
329
330 tx_buf = &tx_ring->tx_bi[i];
331 tx_desc = I40E_TX_DESC(tx_ring, i);
a5e9c572 332 i -= tx_ring->count;
fd0a05ce 333
a5e9c572
AD
334 do {
335 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
fd0a05ce
JB
336
337 /* if next_to_watch is not set then there is no work pending */
338 if (!eop_desc)
339 break;
340
a5e9c572
AD
341 /* prevent any other reads prior to eop_desc */
342 read_barrier_depends();
343
fd0a05ce
JB
344 /* if the descriptor isn't done, no work yet to do */
345 if (!(eop_desc->cmd_type_offset_bsz &
346 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
347 break;
348
c304fdac 349 /* clear next_to_watch to prevent false hangs */
fd0a05ce 350 tx_buf->next_to_watch = NULL;
fd0a05ce 351
a5e9c572
AD
352 /* update the statistics for this packet */
353 total_bytes += tx_buf->bytecount;
354 total_packets += tx_buf->gso_segs;
fd0a05ce 355
a5e9c572
AD
356 /* free the skb */
357 dev_kfree_skb_any(tx_buf->skb);
fd0a05ce 358
a5e9c572
AD
359 /* unmap skb header data */
360 dma_unmap_single(tx_ring->dev,
361 dma_unmap_addr(tx_buf, dma),
362 dma_unmap_len(tx_buf, len),
363 DMA_TO_DEVICE);
fd0a05ce 364
a5e9c572
AD
365 /* clear tx_buffer data */
366 tx_buf->skb = NULL;
367 dma_unmap_len_set(tx_buf, len, 0);
fd0a05ce 368
a5e9c572
AD
369 /* unmap remaining buffers */
370 while (tx_desc != eop_desc) {
fd0a05ce
JB
371
372 tx_buf++;
373 tx_desc++;
374 i++;
a5e9c572
AD
375 if (unlikely(!i)) {
376 i -= tx_ring->count;
fd0a05ce
JB
377 tx_buf = tx_ring->tx_bi;
378 tx_desc = I40E_TX_DESC(tx_ring, 0);
379 }
fd0a05ce 380
a5e9c572
AD
381 /* unmap any remaining paged data */
382 if (dma_unmap_len(tx_buf, len)) {
383 dma_unmap_page(tx_ring->dev,
384 dma_unmap_addr(tx_buf, dma),
385 dma_unmap_len(tx_buf, len),
386 DMA_TO_DEVICE);
387 dma_unmap_len_set(tx_buf, len, 0);
388 }
389 }
390
391 /* move us one more past the eop_desc for start of next pkt */
392 tx_buf++;
393 tx_desc++;
394 i++;
395 if (unlikely(!i)) {
396 i -= tx_ring->count;
397 tx_buf = tx_ring->tx_bi;
398 tx_desc = I40E_TX_DESC(tx_ring, 0);
399 }
400
401 /* update budget accounting */
402 budget--;
403 } while (likely(budget));
404
405 i += tx_ring->count;
fd0a05ce
JB
406 tx_ring->next_to_clean = i;
407 tx_ring->tx_stats.bytes += total_bytes;
408 tx_ring->tx_stats.packets += total_packets;
409 tx_ring->q_vector->tx.total_bytes += total_bytes;
410 tx_ring->q_vector->tx.total_packets += total_packets;
a5e9c572 411
fd0a05ce
JB
412 if (check_for_tx_hang(tx_ring) && i40e_check_tx_hang(tx_ring)) {
413 /* schedule immediate reset if we believe we hung */
414 dev_info(tx_ring->dev, "Detected Tx Unit Hang\n"
415 " VSI <%d>\n"
416 " Tx Queue <%d>\n"
417 " next_to_use <%x>\n"
418 " next_to_clean <%x>\n",
419 tx_ring->vsi->seid,
420 tx_ring->queue_index,
421 tx_ring->next_to_use, i);
422 dev_info(tx_ring->dev, "tx_bi[next_to_clean]\n"
423 " time_stamp <%lx>\n"
424 " jiffies <%lx>\n",
425 tx_ring->tx_bi[i].time_stamp, jiffies);
426
427 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
428
429 dev_info(tx_ring->dev,
430 "tx hang detected on queue %d, resetting adapter\n",
431 tx_ring->queue_index);
432
433 tx_ring->netdev->netdev_ops->ndo_tx_timeout(tx_ring->netdev);
434
435 /* the adapter is about to reset, no point in enabling stuff */
436 return true;
437 }
438
439#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
440 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
441 (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
442 /* Make sure that anybody stopping the queue after this
443 * sees the new next_to_clean.
444 */
445 smp_mb();
446 if (__netif_subqueue_stopped(tx_ring->netdev,
447 tx_ring->queue_index) &&
448 !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
449 netif_wake_subqueue(tx_ring->netdev,
450 tx_ring->queue_index);
451 ++tx_ring->tx_stats.restart_queue;
452 }
453 }
454
455 return budget > 0;
456}
457
458/**
459 * i40e_set_new_dynamic_itr - Find new ITR level
460 * @rc: structure containing ring performance data
461 *
462 * Stores a new ITR value based on packets and byte counts during
463 * the last interrupt. The advantage of per interrupt computation
464 * is faster updates and more accurate ITR for the current traffic
465 * pattern. Constants in this function were computed based on
466 * theoretical maximum wire speed and thresholds were set based on
467 * testing data as well as attempting to minimize response time
468 * while increasing bulk throughput.
469 **/
470static void i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
471{
472 enum i40e_latency_range new_latency_range = rc->latency_range;
473 u32 new_itr = rc->itr;
474 int bytes_per_int;
475
476 if (rc->total_packets == 0 || !rc->itr)
477 return;
478
479 /* simple throttlerate management
480 * 0-10MB/s lowest (100000 ints/s)
481 * 10-20MB/s low (20000 ints/s)
482 * 20-1249MB/s bulk (8000 ints/s)
483 */
484 bytes_per_int = rc->total_bytes / rc->itr;
485 switch (rc->itr) {
486 case I40E_LOWEST_LATENCY:
487 if (bytes_per_int > 10)
488 new_latency_range = I40E_LOW_LATENCY;
489 break;
490 case I40E_LOW_LATENCY:
491 if (bytes_per_int > 20)
492 new_latency_range = I40E_BULK_LATENCY;
493 else if (bytes_per_int <= 10)
494 new_latency_range = I40E_LOWEST_LATENCY;
495 break;
496 case I40E_BULK_LATENCY:
497 if (bytes_per_int <= 20)
498 rc->latency_range = I40E_LOW_LATENCY;
499 break;
500 }
501
502 switch (new_latency_range) {
503 case I40E_LOWEST_LATENCY:
504 new_itr = I40E_ITR_100K;
505 break;
506 case I40E_LOW_LATENCY:
507 new_itr = I40E_ITR_20K;
508 break;
509 case I40E_BULK_LATENCY:
510 new_itr = I40E_ITR_8K;
511 break;
512 default:
513 break;
514 }
515
516 if (new_itr != rc->itr) {
517 /* do an exponential smoothing */
518 new_itr = (10 * new_itr * rc->itr) /
519 ((9 * new_itr) + rc->itr);
520 rc->itr = new_itr & I40E_MAX_ITR;
521 }
522
523 rc->total_bytes = 0;
524 rc->total_packets = 0;
525}
526
527/**
528 * i40e_update_dynamic_itr - Adjust ITR based on bytes per int
529 * @q_vector: the vector to adjust
530 **/
531static void i40e_update_dynamic_itr(struct i40e_q_vector *q_vector)
532{
533 u16 vector = q_vector->vsi->base_vector + q_vector->v_idx;
534 struct i40e_hw *hw = &q_vector->vsi->back->hw;
535 u32 reg_addr;
536 u16 old_itr;
537
538 reg_addr = I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1);
539 old_itr = q_vector->rx.itr;
540 i40e_set_new_dynamic_itr(&q_vector->rx);
541 if (old_itr != q_vector->rx.itr)
542 wr32(hw, reg_addr, q_vector->rx.itr);
543
544 reg_addr = I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1);
545 old_itr = q_vector->tx.itr;
546 i40e_set_new_dynamic_itr(&q_vector->tx);
547 if (old_itr != q_vector->tx.itr)
548 wr32(hw, reg_addr, q_vector->tx.itr);
549
550 i40e_flush(hw);
551}
552
553/**
554 * i40e_clean_programming_status - clean the programming status descriptor
555 * @rx_ring: the rx ring that has this descriptor
556 * @rx_desc: the rx descriptor written back by HW
557 *
558 * Flow director should handle FD_FILTER_STATUS to check its filter programming
559 * status being successful or not and take actions accordingly. FCoE should
560 * handle its context/filter programming/invalidation status and take actions.
561 *
562 **/
563static void i40e_clean_programming_status(struct i40e_ring *rx_ring,
564 union i40e_rx_desc *rx_desc)
565{
566 u64 qw;
567 u8 id;
568
569 qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
570 id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
571 I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
572
573 if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS)
574 i40e_fd_handle_status(rx_ring, qw, id);
575}
576
577/**
578 * i40e_setup_tx_descriptors - Allocate the Tx descriptors
579 * @tx_ring: the tx ring to set up
580 *
581 * Return 0 on success, negative on error
582 **/
583int i40e_setup_tx_descriptors(struct i40e_ring *tx_ring)
584{
585 struct device *dev = tx_ring->dev;
586 int bi_size;
587
588 if (!dev)
589 return -ENOMEM;
590
591 bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
592 tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
593 if (!tx_ring->tx_bi)
594 goto err;
595
596 /* round up to nearest 4K */
597 tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
598 tx_ring->size = ALIGN(tx_ring->size, 4096);
599 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
600 &tx_ring->dma, GFP_KERNEL);
601 if (!tx_ring->desc) {
602 dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
603 tx_ring->size);
604 goto err;
605 }
606
607 tx_ring->next_to_use = 0;
608 tx_ring->next_to_clean = 0;
609 return 0;
610
611err:
612 kfree(tx_ring->tx_bi);
613 tx_ring->tx_bi = NULL;
614 return -ENOMEM;
615}
616
617/**
618 * i40e_clean_rx_ring - Free Rx buffers
619 * @rx_ring: ring to be cleaned
620 **/
621void i40e_clean_rx_ring(struct i40e_ring *rx_ring)
622{
623 struct device *dev = rx_ring->dev;
624 struct i40e_rx_buffer *rx_bi;
625 unsigned long bi_size;
626 u16 i;
627
628 /* ring already cleared, nothing to do */
629 if (!rx_ring->rx_bi)
630 return;
631
632 /* Free all the Rx ring sk_buffs */
633 for (i = 0; i < rx_ring->count; i++) {
634 rx_bi = &rx_ring->rx_bi[i];
635 if (rx_bi->dma) {
636 dma_unmap_single(dev,
637 rx_bi->dma,
638 rx_ring->rx_buf_len,
639 DMA_FROM_DEVICE);
640 rx_bi->dma = 0;
641 }
642 if (rx_bi->skb) {
643 dev_kfree_skb(rx_bi->skb);
644 rx_bi->skb = NULL;
645 }
646 if (rx_bi->page) {
647 if (rx_bi->page_dma) {
648 dma_unmap_page(dev,
649 rx_bi->page_dma,
650 PAGE_SIZE / 2,
651 DMA_FROM_DEVICE);
652 rx_bi->page_dma = 0;
653 }
654 __free_page(rx_bi->page);
655 rx_bi->page = NULL;
656 rx_bi->page_offset = 0;
657 }
658 }
659
660 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
661 memset(rx_ring->rx_bi, 0, bi_size);
662
663 /* Zero out the descriptor ring */
664 memset(rx_ring->desc, 0, rx_ring->size);
665
666 rx_ring->next_to_clean = 0;
667 rx_ring->next_to_use = 0;
668}
669
670/**
671 * i40e_free_rx_resources - Free Rx resources
672 * @rx_ring: ring to clean the resources from
673 *
674 * Free all receive software resources
675 **/
676void i40e_free_rx_resources(struct i40e_ring *rx_ring)
677{
678 i40e_clean_rx_ring(rx_ring);
679 kfree(rx_ring->rx_bi);
680 rx_ring->rx_bi = NULL;
681
682 if (rx_ring->desc) {
683 dma_free_coherent(rx_ring->dev, rx_ring->size,
684 rx_ring->desc, rx_ring->dma);
685 rx_ring->desc = NULL;
686 }
687}
688
689/**
690 * i40e_setup_rx_descriptors - Allocate Rx descriptors
691 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
692 *
693 * Returns 0 on success, negative on failure
694 **/
695int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring)
696{
697 struct device *dev = rx_ring->dev;
698 int bi_size;
699
700 bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
701 rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
702 if (!rx_ring->rx_bi)
703 goto err;
704
705 /* Round up to nearest 4K */
706 rx_ring->size = ring_is_16byte_desc_enabled(rx_ring)
707 ? rx_ring->count * sizeof(union i40e_16byte_rx_desc)
708 : rx_ring->count * sizeof(union i40e_32byte_rx_desc);
709 rx_ring->size = ALIGN(rx_ring->size, 4096);
710 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
711 &rx_ring->dma, GFP_KERNEL);
712
713 if (!rx_ring->desc) {
714 dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
715 rx_ring->size);
716 goto err;
717 }
718
719 rx_ring->next_to_clean = 0;
720 rx_ring->next_to_use = 0;
721
722 return 0;
723err:
724 kfree(rx_ring->rx_bi);
725 rx_ring->rx_bi = NULL;
726 return -ENOMEM;
727}
728
729/**
730 * i40e_release_rx_desc - Store the new tail and head values
731 * @rx_ring: ring to bump
732 * @val: new head index
733 **/
734static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
735{
736 rx_ring->next_to_use = val;
737 /* Force memory writes to complete before letting h/w
738 * know there are new descriptors to fetch. (Only
739 * applicable for weak-ordered memory model archs,
740 * such as IA-64).
741 */
742 wmb();
743 writel(val, rx_ring->tail);
744}
745
746/**
747 * i40e_alloc_rx_buffers - Replace used receive buffers; packet split
748 * @rx_ring: ring to place buffers on
749 * @cleaned_count: number of buffers to replace
750 **/
751void i40e_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
752{
753 u16 i = rx_ring->next_to_use;
754 union i40e_rx_desc *rx_desc;
755 struct i40e_rx_buffer *bi;
756 struct sk_buff *skb;
757
758 /* do nothing if no valid netdev defined */
759 if (!rx_ring->netdev || !cleaned_count)
760 return;
761
762 while (cleaned_count--) {
763 rx_desc = I40E_RX_DESC(rx_ring, i);
764 bi = &rx_ring->rx_bi[i];
765 skb = bi->skb;
766
767 if (!skb) {
768 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
769 rx_ring->rx_buf_len);
770 if (!skb) {
771 rx_ring->rx_stats.alloc_rx_buff_failed++;
772 goto no_buffers;
773 }
774 /* initialize queue mapping */
775 skb_record_rx_queue(skb, rx_ring->queue_index);
776 bi->skb = skb;
777 }
778
779 if (!bi->dma) {
780 bi->dma = dma_map_single(rx_ring->dev,
781 skb->data,
782 rx_ring->rx_buf_len,
783 DMA_FROM_DEVICE);
784 if (dma_mapping_error(rx_ring->dev, bi->dma)) {
785 rx_ring->rx_stats.alloc_rx_buff_failed++;
786 bi->dma = 0;
787 goto no_buffers;
788 }
789 }
790
791 if (ring_is_ps_enabled(rx_ring)) {
792 if (!bi->page) {
793 bi->page = alloc_page(GFP_ATOMIC);
794 if (!bi->page) {
795 rx_ring->rx_stats.alloc_rx_page_failed++;
796 goto no_buffers;
797 }
798 }
799
800 if (!bi->page_dma) {
801 /* use a half page if we're re-using */
802 bi->page_offset ^= PAGE_SIZE / 2;
803 bi->page_dma = dma_map_page(rx_ring->dev,
804 bi->page,
805 bi->page_offset,
806 PAGE_SIZE / 2,
807 DMA_FROM_DEVICE);
808 if (dma_mapping_error(rx_ring->dev,
809 bi->page_dma)) {
810 rx_ring->rx_stats.alloc_rx_page_failed++;
811 bi->page_dma = 0;
812 goto no_buffers;
813 }
814 }
815
816 /* Refresh the desc even if buffer_addrs didn't change
817 * because each write-back erases this info.
818 */
819 rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
820 rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
821 } else {
822 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
823 rx_desc->read.hdr_addr = 0;
824 }
825 i++;
826 if (i == rx_ring->count)
827 i = 0;
828 }
829
830no_buffers:
831 if (rx_ring->next_to_use != i)
832 i40e_release_rx_desc(rx_ring, i);
833}
834
835/**
836 * i40e_receive_skb - Send a completed packet up the stack
837 * @rx_ring: rx ring in play
838 * @skb: packet to send up
839 * @vlan_tag: vlan tag for packet
840 **/
841static void i40e_receive_skb(struct i40e_ring *rx_ring,
842 struct sk_buff *skb, u16 vlan_tag)
843{
844 struct i40e_q_vector *q_vector = rx_ring->q_vector;
845 struct i40e_vsi *vsi = rx_ring->vsi;
846 u64 flags = vsi->back->flags;
847
848 if (vlan_tag & VLAN_VID_MASK)
849 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
850
851 if (flags & I40E_FLAG_IN_NETPOLL)
852 netif_rx(skb);
853 else
854 napi_gro_receive(&q_vector->napi, skb);
855}
856
857/**
858 * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
859 * @vsi: the VSI we care about
860 * @skb: skb currently being received and modified
861 * @rx_status: status value of last descriptor in packet
862 * @rx_error: error value of last descriptor in packet
863 **/
864static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
865 struct sk_buff *skb,
866 u32 rx_status,
867 u32 rx_error)
868{
869 skb->ip_summed = CHECKSUM_NONE;
870
871 /* Rx csum enabled and ip headers found? */
872 if (!(vsi->netdev->features & NETIF_F_RXCSUM &&
873 rx_status & (1 << I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
874 return;
875
876 /* IP or L4 checksum error */
877 if (rx_error & ((1 << I40E_RX_DESC_ERROR_IPE_SHIFT) |
878 (1 << I40E_RX_DESC_ERROR_L4E_SHIFT))) {
879 vsi->back->hw_csum_rx_error++;
880 return;
881 }
882
883 skb->ip_summed = CHECKSUM_UNNECESSARY;
884}
885
886/**
887 * i40e_rx_hash - returns the hash value from the Rx descriptor
888 * @ring: descriptor ring
889 * @rx_desc: specific descriptor
890 **/
891static inline u32 i40e_rx_hash(struct i40e_ring *ring,
892 union i40e_rx_desc *rx_desc)
893{
894 if (ring->netdev->features & NETIF_F_RXHASH) {
895 if ((le64_to_cpu(rx_desc->wb.qword1.status_error_len) >>
896 I40E_RX_DESC_STATUS_FLTSTAT_SHIFT) &
897 I40E_RX_DESC_FLTSTAT_RSS_HASH)
898 return le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
899 }
900 return 0;
901}
902
903/**
904 * i40e_clean_rx_irq - Reclaim resources after receive completes
905 * @rx_ring: rx ring to clean
906 * @budget: how many cleans we're allowed
907 *
908 * Returns true if there's any budget left (e.g. the clean is finished)
909 **/
910static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
911{
912 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
913 u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
914 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
915 const int current_node = numa_node_id();
916 struct i40e_vsi *vsi = rx_ring->vsi;
917 u16 i = rx_ring->next_to_clean;
918 union i40e_rx_desc *rx_desc;
919 u32 rx_error, rx_status;
920 u64 qword;
921
922 rx_desc = I40E_RX_DESC(rx_ring, i);
923 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
924 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK)
925 >> I40E_RXD_QW1_STATUS_SHIFT;
926
927 while (rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)) {
928 union i40e_rx_desc *next_rxd;
929 struct i40e_rx_buffer *rx_bi;
930 struct sk_buff *skb;
931 u16 vlan_tag;
932 if (i40e_rx_is_programming_status(qword)) {
933 i40e_clean_programming_status(rx_ring, rx_desc);
934 I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
935 goto next_desc;
936 }
937 rx_bi = &rx_ring->rx_bi[i];
938 skb = rx_bi->skb;
939 prefetch(skb->data);
940
941 rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK)
942 >> I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
943 rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK)
944 >> I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
945 rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK)
946 >> I40E_RXD_QW1_LENGTH_SPH_SHIFT;
947
948 rx_error = (qword & I40E_RXD_QW1_ERROR_MASK)
949 >> I40E_RXD_QW1_ERROR_SHIFT;
950 rx_hbo = rx_error & (1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
951 rx_error &= ~(1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
952
953 rx_bi->skb = NULL;
954
955 /* This memory barrier is needed to keep us from reading
956 * any other fields out of the rx_desc until we know the
957 * STATUS_DD bit is set
958 */
959 rmb();
960
961 /* Get the header and possibly the whole packet
962 * If this is an skb from previous receive dma will be 0
963 */
964 if (rx_bi->dma) {
965 u16 len;
966
967 if (rx_hbo)
968 len = I40E_RX_HDR_SIZE;
969 else if (rx_sph)
970 len = rx_header_len;
971 else if (rx_packet_len)
972 len = rx_packet_len; /* 1buf/no split found */
973 else
974 len = rx_header_len; /* split always mode */
975
976 skb_put(skb, len);
977 dma_unmap_single(rx_ring->dev,
978 rx_bi->dma,
979 rx_ring->rx_buf_len,
980 DMA_FROM_DEVICE);
981 rx_bi->dma = 0;
982 }
983
984 /* Get the rest of the data if this was a header split */
985 if (ring_is_ps_enabled(rx_ring) && rx_packet_len) {
986
987 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
988 rx_bi->page,
989 rx_bi->page_offset,
990 rx_packet_len);
991
992 skb->len += rx_packet_len;
993 skb->data_len += rx_packet_len;
994 skb->truesize += rx_packet_len;
995
996 if ((page_count(rx_bi->page) == 1) &&
997 (page_to_nid(rx_bi->page) == current_node))
998 get_page(rx_bi->page);
999 else
1000 rx_bi->page = NULL;
1001
1002 dma_unmap_page(rx_ring->dev,
1003 rx_bi->page_dma,
1004 PAGE_SIZE / 2,
1005 DMA_FROM_DEVICE);
1006 rx_bi->page_dma = 0;
1007 }
1008 I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
1009
1010 if (unlikely(
1011 !(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
1012 struct i40e_rx_buffer *next_buffer;
1013
1014 next_buffer = &rx_ring->rx_bi[i];
1015
1016 if (ring_is_ps_enabled(rx_ring)) {
1017 rx_bi->skb = next_buffer->skb;
1018 rx_bi->dma = next_buffer->dma;
1019 next_buffer->skb = skb;
1020 next_buffer->dma = 0;
1021 }
1022 rx_ring->rx_stats.non_eop_descs++;
1023 goto next_desc;
1024 }
1025
1026 /* ERR_MASK will only have valid bits if EOP set */
1027 if (unlikely(rx_error & (1 << I40E_RX_DESC_ERROR_RXE_SHIFT))) {
1028 dev_kfree_skb_any(skb);
1029 goto next_desc;
1030 }
1031
1032 skb->rxhash = i40e_rx_hash(rx_ring, rx_desc);
1033 i40e_rx_checksum(vsi, skb, rx_status, rx_error);
1034
1035 /* probably a little skewed due to removing CRC */
1036 total_rx_bytes += skb->len;
1037 total_rx_packets++;
1038
1039 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1040 vlan_tag = rx_status & (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
1041 ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
1042 : 0;
1043 i40e_receive_skb(rx_ring, skb, vlan_tag);
1044
1045 rx_ring->netdev->last_rx = jiffies;
1046 budget--;
1047next_desc:
1048 rx_desc->wb.qword1.status_error_len = 0;
1049 if (!budget)
1050 break;
1051
1052 cleaned_count++;
1053 /* return some buffers to hardware, one at a time is too slow */
1054 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
1055 i40e_alloc_rx_buffers(rx_ring, cleaned_count);
1056 cleaned_count = 0;
1057 }
1058
1059 /* use prefetched values */
1060 rx_desc = next_rxd;
1061 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1062 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK)
1063 >> I40E_RXD_QW1_STATUS_SHIFT;
1064 }
1065
1066 rx_ring->next_to_clean = i;
1067 rx_ring->rx_stats.packets += total_rx_packets;
1068 rx_ring->rx_stats.bytes += total_rx_bytes;
1069 rx_ring->q_vector->rx.total_packets += total_rx_packets;
1070 rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
1071
1072 if (cleaned_count)
1073 i40e_alloc_rx_buffers(rx_ring, cleaned_count);
1074
1075 return budget > 0;
1076}
1077
1078/**
1079 * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
1080 * @napi: napi struct with our devices info in it
1081 * @budget: amount of work driver is allowed to do this pass, in packets
1082 *
1083 * This function will clean all queues associated with a q_vector.
1084 *
1085 * Returns the amount of work done
1086 **/
1087int i40e_napi_poll(struct napi_struct *napi, int budget)
1088{
1089 struct i40e_q_vector *q_vector =
1090 container_of(napi, struct i40e_q_vector, napi);
1091 struct i40e_vsi *vsi = q_vector->vsi;
1092 bool clean_complete = true;
1093 int budget_per_ring;
1094 int i;
1095
1096 if (test_bit(__I40E_DOWN, &vsi->state)) {
1097 napi_complete(napi);
1098 return 0;
1099 }
1100
1101 /* We attempt to distribute budget to each Rx queue fairly, but don't
1102 * allow the budget to go below 1 because that would exit polling early.
1103 * Since the actual Tx work is minimal, we can give the Tx a larger
1104 * budget and be more aggressive about cleaning up the Tx descriptors.
1105 */
1106 budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
1107 for (i = 0; i < q_vector->num_ringpairs; i++) {
1108 clean_complete &= i40e_clean_tx_irq(q_vector->tx.ring[i],
1109 vsi->work_limit);
1110 clean_complete &= i40e_clean_rx_irq(q_vector->rx.ring[i],
1111 budget_per_ring);
1112 }
1113
1114 /* If work not completed, return budget and polling will return */
1115 if (!clean_complete)
1116 return budget;
1117
1118 /* Work is done so exit the polling mode and re-enable the interrupt */
1119 napi_complete(napi);
1120 if (ITR_IS_DYNAMIC(vsi->rx_itr_setting) ||
1121 ITR_IS_DYNAMIC(vsi->tx_itr_setting))
1122 i40e_update_dynamic_itr(q_vector);
1123
1124 if (!test_bit(__I40E_DOWN, &vsi->state)) {
1125 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
1126 i40e_irq_dynamic_enable(vsi,
1127 q_vector->v_idx + vsi->base_vector);
1128 } else {
1129 struct i40e_hw *hw = &vsi->back->hw;
1130 /* We re-enable the queue 0 cause, but
1131 * don't worry about dynamic_enable
1132 * because we left it on for the other
1133 * possible interrupts during napi
1134 */
1135 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
1136 qval |= I40E_QINT_RQCTL_CAUSE_ENA_MASK;
1137 wr32(hw, I40E_QINT_RQCTL(0), qval);
1138
1139 qval = rd32(hw, I40E_QINT_TQCTL(0));
1140 qval |= I40E_QINT_TQCTL_CAUSE_ENA_MASK;
1141 wr32(hw, I40E_QINT_TQCTL(0), qval);
1142 i40e_flush(hw);
1143 }
1144 }
1145
1146 return 0;
1147}
1148
1149/**
1150 * i40e_atr - Add a Flow Director ATR filter
1151 * @tx_ring: ring to add programming descriptor to
1152 * @skb: send buffer
1153 * @flags: send flags
1154 * @protocol: wire protocol
1155 **/
1156static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
1157 u32 flags, __be16 protocol)
1158{
1159 struct i40e_filter_program_desc *fdir_desc;
1160 struct i40e_pf *pf = tx_ring->vsi->back;
1161 union {
1162 unsigned char *network;
1163 struct iphdr *ipv4;
1164 struct ipv6hdr *ipv6;
1165 } hdr;
1166 struct tcphdr *th;
1167 unsigned int hlen;
1168 u32 flex_ptype, dtype_cmd;
fc4ac67b 1169 u16 i;
fd0a05ce
JB
1170
1171 /* make sure ATR is enabled */
1172 if (!(pf->flags & I40E_FLAG_FDIR_ATR_ENABLED))
1173 return;
1174
1175 /* if sampling is disabled do nothing */
1176 if (!tx_ring->atr_sample_rate)
1177 return;
1178
1179 tx_ring->atr_count++;
1180
1181 /* snag network header to get L4 type and address */
1182 hdr.network = skb_network_header(skb);
1183
1184 /* Currently only IPv4/IPv6 with TCP is supported */
1185 if (protocol == htons(ETH_P_IP)) {
1186 if (hdr.ipv4->protocol != IPPROTO_TCP)
1187 return;
1188
1189 /* access ihl as a u8 to avoid unaligned access on ia64 */
1190 hlen = (hdr.network[0] & 0x0F) << 2;
1191 } else if (protocol == htons(ETH_P_IPV6)) {
1192 if (hdr.ipv6->nexthdr != IPPROTO_TCP)
1193 return;
1194
1195 hlen = sizeof(struct ipv6hdr);
1196 } else {
1197 return;
1198 }
1199
1200 th = (struct tcphdr *)(hdr.network + hlen);
1201
1202 /* sample on all syn/fin packets or once every atr sample rate */
1203 if (!th->fin && !th->syn && (tx_ring->atr_count < tx_ring->atr_sample_rate))
1204 return;
1205
1206 tx_ring->atr_count = 0;
1207
1208 /* grab the next descriptor */
fc4ac67b
AD
1209 i = tx_ring->next_to_use;
1210 fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
1211
1212 i++;
1213 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
fd0a05ce
JB
1214
1215 flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
1216 I40E_TXD_FLTR_QW0_QINDEX_MASK;
1217 flex_ptype |= (protocol == htons(ETH_P_IP)) ?
1218 (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
1219 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
1220 (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
1221 I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
1222
1223 flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
1224
1225 dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
1226
1227 dtype_cmd |= th->fin ?
1228 (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
1229 I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
1230 (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
1231 I40E_TXD_FLTR_QW1_PCMD_SHIFT);
1232
1233 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
1234 I40E_TXD_FLTR_QW1_DEST_SHIFT;
1235
1236 dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
1237 I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
1238
1239 fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
1240 fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
1241}
1242
1243#define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
1244/**
1245 * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
1246 * @skb: send buffer
1247 * @tx_ring: ring to send buffer on
1248 * @flags: the tx flags to be set
1249 *
1250 * Checks the skb and set up correspondingly several generic transmit flags
1251 * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
1252 *
1253 * Returns error code indicate the frame should be dropped upon error and the
1254 * otherwise returns 0 to indicate the flags has been set properly.
1255 **/
1256static int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
1257 struct i40e_ring *tx_ring,
1258 u32 *flags)
1259{
1260 __be16 protocol = skb->protocol;
1261 u32 tx_flags = 0;
1262
1263 /* if we have a HW VLAN tag being added, default to the HW one */
1264 if (vlan_tx_tag_present(skb)) {
1265 tx_flags |= vlan_tx_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
1266 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1267 /* else if it is a SW VLAN, check the next protocol and store the tag */
1268 } else if (protocol == __constant_htons(ETH_P_8021Q)) {
1269 struct vlan_hdr *vhdr, _vhdr;
1270 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
1271 if (!vhdr)
1272 return -EINVAL;
1273
1274 protocol = vhdr->h_vlan_encapsulated_proto;
1275 tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
1276 tx_flags |= I40E_TX_FLAGS_SW_VLAN;
1277 }
1278
1279 /* Insert 802.1p priority into VLAN header */
1280 if ((tx_ring->vsi->back->flags & I40E_FLAG_DCB_ENABLED) &&
1281 ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
1282 (skb->priority != TC_PRIO_CONTROL))) {
1283 tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
1284 tx_flags |= (skb->priority & 0x7) <<
1285 I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
1286 if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
1287 struct vlan_ethhdr *vhdr;
1288 if (skb_header_cloned(skb) &&
1289 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
1290 return -ENOMEM;
1291 vhdr = (struct vlan_ethhdr *)skb->data;
1292 vhdr->h_vlan_TCI = htons(tx_flags >>
1293 I40E_TX_FLAGS_VLAN_SHIFT);
1294 } else {
1295 tx_flags |= I40E_TX_FLAGS_HW_VLAN;
1296 }
1297 }
1298 *flags = tx_flags;
1299 return 0;
1300}
1301
fd0a05ce
JB
1302/**
1303 * i40e_tso - set up the tso context descriptor
1304 * @tx_ring: ptr to the ring to send
1305 * @skb: ptr to the skb we're sending
1306 * @tx_flags: the collected send information
1307 * @protocol: the send protocol
1308 * @hdr_len: ptr to the size of the packet header
1309 * @cd_tunneling: ptr to context descriptor bits
1310 *
1311 * Returns 0 if no TSO can happen, 1 if tso is going, or error
1312 **/
1313static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
1314 u32 tx_flags, __be16 protocol, u8 *hdr_len,
1315 u64 *cd_type_cmd_tso_mss, u32 *cd_tunneling)
1316{
1317 u32 cd_cmd, cd_tso_len, cd_mss;
1318 struct tcphdr *tcph;
1319 struct iphdr *iph;
1320 u32 l4len;
1321 int err;
1322 struct ipv6hdr *ipv6h;
1323
1324 if (!skb_is_gso(skb))
1325 return 0;
1326
1327 if (skb_header_cloned(skb)) {
1328 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1329 if (err)
1330 return err;
1331 }
1332
1333 if (protocol == __constant_htons(ETH_P_IP)) {
1334 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
1335 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1336 iph->tot_len = 0;
1337 iph->check = 0;
1338 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1339 0, IPPROTO_TCP, 0);
1340 } else if (skb_is_gso_v6(skb)) {
1341
1342 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb)
1343 : ipv6_hdr(skb);
1344 tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
1345 ipv6h->payload_len = 0;
1346 tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
1347 0, IPPROTO_TCP, 0);
1348 }
1349
1350 l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
1351 *hdr_len = (skb->encapsulation
1352 ? (skb_inner_transport_header(skb) - skb->data)
1353 : skb_transport_offset(skb)) + l4len;
1354
1355 /* find the field values */
1356 cd_cmd = I40E_TX_CTX_DESC_TSO;
1357 cd_tso_len = skb->len - *hdr_len;
1358 cd_mss = skb_shinfo(skb)->gso_size;
1359 *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT)
1360 | ((u64)cd_tso_len
1361 << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT)
1362 | ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
1363 return 1;
1364}
1365
1366/**
1367 * i40e_tx_enable_csum - Enable Tx checksum offloads
1368 * @skb: send buffer
1369 * @tx_flags: Tx flags currently set
1370 * @td_cmd: Tx descriptor command bits to set
1371 * @td_offset: Tx descriptor header offsets to set
1372 * @cd_tunneling: ptr to context desc bits
1373 **/
1374static void i40e_tx_enable_csum(struct sk_buff *skb, u32 tx_flags,
1375 u32 *td_cmd, u32 *td_offset,
1376 struct i40e_ring *tx_ring,
1377 u32 *cd_tunneling)
1378{
1379 struct ipv6hdr *this_ipv6_hdr;
1380 unsigned int this_tcp_hdrlen;
1381 struct iphdr *this_ip_hdr;
1382 u32 network_hdr_len;
1383 u8 l4_hdr = 0;
1384
1385 if (skb->encapsulation) {
1386 network_hdr_len = skb_inner_network_header_len(skb);
1387 this_ip_hdr = inner_ip_hdr(skb);
1388 this_ipv6_hdr = inner_ipv6_hdr(skb);
1389 this_tcp_hdrlen = inner_tcp_hdrlen(skb);
1390
1391 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1392
1393 if (tx_flags & I40E_TX_FLAGS_TSO) {
1394 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
1395 ip_hdr(skb)->check = 0;
1396 } else {
1397 *cd_tunneling |=
1398 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1399 }
1400 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1401 if (tx_flags & I40E_TX_FLAGS_TSO) {
1402 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
1403 ip_hdr(skb)->check = 0;
1404 } else {
1405 *cd_tunneling |=
1406 I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1407 }
1408 }
1409
1410 /* Now set the ctx descriptor fields */
1411 *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
1412 I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
1413 I40E_TXD_CTX_UDP_TUNNELING |
1414 ((skb_inner_network_offset(skb) -
1415 skb_transport_offset(skb)) >> 1) <<
1416 I40E_TXD_CTX_QW0_NATLEN_SHIFT;
1417
1418 } else {
1419 network_hdr_len = skb_network_header_len(skb);
1420 this_ip_hdr = ip_hdr(skb);
1421 this_ipv6_hdr = ipv6_hdr(skb);
1422 this_tcp_hdrlen = tcp_hdrlen(skb);
1423 }
1424
1425 /* Enable IP checksum offloads */
1426 if (tx_flags & I40E_TX_FLAGS_IPV4) {
1427 l4_hdr = this_ip_hdr->protocol;
1428 /* the stack computes the IP header already, the only time we
1429 * need the hardware to recompute it is in the case of TSO.
1430 */
1431 if (tx_flags & I40E_TX_FLAGS_TSO) {
1432 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
1433 this_ip_hdr->check = 0;
1434 } else {
1435 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
1436 }
1437 /* Now set the td_offset for IP header length */
1438 *td_offset = (network_hdr_len >> 2) <<
1439 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1440 } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
1441 l4_hdr = this_ipv6_hdr->nexthdr;
1442 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
1443 /* Now set the td_offset for IP header length */
1444 *td_offset = (network_hdr_len >> 2) <<
1445 I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
1446 }
1447 /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
1448 *td_offset |= (skb_network_offset(skb) >> 1) <<
1449 I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
1450
1451 /* Enable L4 checksum offloads */
1452 switch (l4_hdr) {
1453 case IPPROTO_TCP:
1454 /* enable checksum offloads */
1455 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
1456 *td_offset |= (this_tcp_hdrlen >> 2) <<
1457 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1458 break;
1459 case IPPROTO_SCTP:
1460 /* enable SCTP checksum offload */
1461 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
1462 *td_offset |= (sizeof(struct sctphdr) >> 2) <<
1463 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1464 break;
1465 case IPPROTO_UDP:
1466 /* enable UDP checksum offload */
1467 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
1468 *td_offset |= (sizeof(struct udphdr) >> 2) <<
1469 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1470 break;
1471 default:
1472 break;
1473 }
1474}
1475
1476/**
1477 * i40e_create_tx_ctx Build the Tx context descriptor
1478 * @tx_ring: ring to create the descriptor on
1479 * @cd_type_cmd_tso_mss: Quad Word 1
1480 * @cd_tunneling: Quad Word 0 - bits 0-31
1481 * @cd_l2tag2: Quad Word 0 - bits 32-63
1482 **/
1483static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
1484 const u64 cd_type_cmd_tso_mss,
1485 const u32 cd_tunneling, const u32 cd_l2tag2)
1486{
1487 struct i40e_tx_context_desc *context_desc;
fc4ac67b 1488 int i = tx_ring->next_to_use;
fd0a05ce
JB
1489
1490 if (!cd_type_cmd_tso_mss && !cd_tunneling && !cd_l2tag2)
1491 return;
1492
1493 /* grab the next descriptor */
fc4ac67b
AD
1494 context_desc = I40E_TX_CTXTDESC(tx_ring, i);
1495
1496 i++;
1497 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
fd0a05ce
JB
1498
1499 /* cpu_to_le32 and assign to struct fields */
1500 context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
1501 context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
1502 context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
1503}
1504
1505/**
1506 * i40e_tx_map - Build the Tx descriptor
1507 * @tx_ring: ring to send buffer on
1508 * @skb: send buffer
1509 * @first: first buffer info buffer to use
1510 * @tx_flags: collected send information
1511 * @hdr_len: size of the packet header
1512 * @td_cmd: the command field in the descriptor
1513 * @td_offset: offset for checksum or crc
1514 **/
1515static void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
1516 struct i40e_tx_buffer *first, u32 tx_flags,
1517 const u8 hdr_len, u32 td_cmd, u32 td_offset)
1518{
fd0a05ce
JB
1519 unsigned int data_len = skb->data_len;
1520 unsigned int size = skb_headlen(skb);
a5e9c572 1521 struct skb_frag_struct *frag;
fd0a05ce
JB
1522 struct i40e_tx_buffer *tx_bi;
1523 struct i40e_tx_desc *tx_desc;
a5e9c572 1524 u16 i = tx_ring->next_to_use;
fd0a05ce
JB
1525 u32 td_tag = 0;
1526 dma_addr_t dma;
1527 u16 gso_segs;
1528
fd0a05ce
JB
1529 if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
1530 td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
1531 td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
1532 I40E_TX_FLAGS_VLAN_SHIFT;
1533 }
1534
a5e9c572
AD
1535 if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
1536 gso_segs = skb_shinfo(skb)->gso_segs;
1537 else
1538 gso_segs = 1;
1539
1540 /* multiply data chunks by size of headers */
1541 first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
1542 first->gso_segs = gso_segs;
1543 first->skb = skb;
1544 first->tx_flags = tx_flags;
1545
1546 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1547
fd0a05ce 1548 tx_desc = I40E_TX_DESC(tx_ring, i);
a5e9c572
AD
1549 tx_bi = first;
1550
1551 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1552 if (dma_mapping_error(tx_ring->dev, dma))
1553 goto dma_error;
1554
1555 /* record length, and DMA address */
1556 dma_unmap_len_set(tx_bi, len, size);
1557 dma_unmap_addr_set(tx_bi, dma, dma);
1558
1559 tx_desc->buffer_addr = cpu_to_le64(dma);
1560
1561 while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
fd0a05ce
JB
1562 tx_desc->cmd_type_offset_bsz =
1563 build_ctob(td_cmd, td_offset,
1564 I40E_MAX_DATA_PER_TXD, td_tag);
1565
fd0a05ce
JB
1566 tx_desc++;
1567 i++;
1568 if (i == tx_ring->count) {
1569 tx_desc = I40E_TX_DESC(tx_ring, 0);
1570 i = 0;
1571 }
fd0a05ce 1572
a5e9c572
AD
1573 dma += I40E_MAX_DATA_PER_TXD;
1574 size -= I40E_MAX_DATA_PER_TXD;
fd0a05ce 1575
a5e9c572
AD
1576 tx_desc->buffer_addr = cpu_to_le64(dma);
1577 }
fd0a05ce
JB
1578
1579 if (likely(!data_len))
1580 break;
1581
a5e9c572
AD
1582 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
1583 size, td_tag);
fd0a05ce
JB
1584
1585 tx_desc++;
1586 i++;
1587 if (i == tx_ring->count) {
1588 tx_desc = I40E_TX_DESC(tx_ring, 0);
1589 i = 0;
1590 }
1591
a5e9c572
AD
1592 size = skb_frag_size(frag);
1593 data_len -= size;
fd0a05ce 1594
a5e9c572
AD
1595 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
1596 DMA_TO_DEVICE);
fd0a05ce 1597
a5e9c572
AD
1598 tx_bi = &tx_ring->tx_bi[i];
1599 }
fd0a05ce 1600
a5e9c572
AD
1601 tx_desc->cmd_type_offset_bsz =
1602 build_ctob(td_cmd, td_offset, size, td_tag) |
1603 cpu_to_le64((u64)I40E_TXD_CMD << I40E_TXD_QW1_CMD_SHIFT);
fd0a05ce 1604
a5e9c572 1605 /* set the timestamp */
fd0a05ce 1606 first->time_stamp = jiffies;
fd0a05ce
JB
1607
1608 /* Force memory writes to complete before letting h/w
1609 * know there are new descriptors to fetch. (Only
1610 * applicable for weak-ordered memory model archs,
1611 * such as IA-64).
1612 */
1613 wmb();
1614
a5e9c572
AD
1615 /* set next_to_watch value indicating a packet is present */
1616 first->next_to_watch = tx_desc;
1617
1618 i++;
1619 if (i == tx_ring->count)
1620 i = 0;
1621
1622 tx_ring->next_to_use = i;
1623
1624 /* notify HW of packet */
fd0a05ce 1625 writel(i, tx_ring->tail);
a5e9c572 1626
fd0a05ce
JB
1627 return;
1628
1629dma_error:
a5e9c572 1630 dev_info(tx_ring->dev, "TX DMA map failed\n");
fd0a05ce
JB
1631
1632 /* clear dma mappings for failed tx_bi map */
1633 for (;;) {
1634 tx_bi = &tx_ring->tx_bi[i];
a5e9c572 1635 i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
fd0a05ce
JB
1636 if (tx_bi == first)
1637 break;
1638 if (i == 0)
1639 i = tx_ring->count;
1640 i--;
1641 }
1642
fd0a05ce
JB
1643 tx_ring->next_to_use = i;
1644}
1645
1646/**
1647 * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
1648 * @tx_ring: the ring to be checked
1649 * @size: the size buffer we want to assure is available
1650 *
1651 * Returns -EBUSY if a stop is needed, else 0
1652 **/
1653static inline int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
1654{
1655 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
1656 smp_mb();
1657
1658 /* Check again in a case another CPU has just made room available. */
1659 if (likely(I40E_DESC_UNUSED(tx_ring) < size))
1660 return -EBUSY;
1661
1662 /* A reprieve! - use start_queue because it doesn't call schedule */
1663 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
1664 ++tx_ring->tx_stats.restart_queue;
1665 return 0;
1666}
1667
1668/**
1669 * i40e_maybe_stop_tx - 1st level check for tx stop conditions
1670 * @tx_ring: the ring to be checked
1671 * @size: the size buffer we want to assure is available
1672 *
1673 * Returns 0 if stop is not needed
1674 **/
1675static int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
1676{
1677 if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
1678 return 0;
1679 return __i40e_maybe_stop_tx(tx_ring, size);
1680}
1681
1682/**
1683 * i40e_xmit_descriptor_count - calculate number of tx descriptors needed
1684 * @skb: send buffer
1685 * @tx_ring: ring to send buffer on
1686 *
1687 * Returns number of data descriptors needed for this skb. Returns 0 to indicate
1688 * there is not enough descriptors available in this ring since we need at least
1689 * one descriptor.
1690 **/
1691static int i40e_xmit_descriptor_count(struct sk_buff *skb,
1692 struct i40e_ring *tx_ring)
1693{
1694#if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
1695 unsigned int f;
1696#endif
1697 int count = 0;
1698
1699 /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
1700 * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
1701 * + 2 desc gap to keep tail from touching head,
1702 * + 1 desc for context descriptor,
1703 * otherwise try next time
1704 */
1705#if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
1706 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1707 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
1708#else
1709 count += skb_shinfo(skb)->nr_frags;
1710#endif
1711 count += TXD_USE_COUNT(skb_headlen(skb));
1712 if (i40e_maybe_stop_tx(tx_ring, count + 3)) {
1713 tx_ring->tx_stats.tx_busy++;
1714 return 0;
1715 }
1716 return count;
1717}
1718
1719/**
1720 * i40e_xmit_frame_ring - Sends buffer on Tx ring
1721 * @skb: send buffer
1722 * @tx_ring: ring to send buffer on
1723 *
1724 * Returns NETDEV_TX_OK if sent, else an error code
1725 **/
1726static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
1727 struct i40e_ring *tx_ring)
1728{
1729 u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
1730 u32 cd_tunneling = 0, cd_l2tag2 = 0;
1731 struct i40e_tx_buffer *first;
1732 u32 td_offset = 0;
1733 u32 tx_flags = 0;
1734 __be16 protocol;
1735 u32 td_cmd = 0;
1736 u8 hdr_len = 0;
1737 int tso;
1738 if (0 == i40e_xmit_descriptor_count(skb, tx_ring))
1739 return NETDEV_TX_BUSY;
1740
1741 /* prepare the xmit flags */
1742 if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
1743 goto out_drop;
1744
1745 /* obtain protocol of skb */
1746 protocol = skb->protocol;
1747
1748 /* record the location of the first descriptor for this packet */
1749 first = &tx_ring->tx_bi[tx_ring->next_to_use];
1750
1751 /* setup IPv4/IPv6 offloads */
1752 if (protocol == __constant_htons(ETH_P_IP))
1753 tx_flags |= I40E_TX_FLAGS_IPV4;
1754 else if (protocol == __constant_htons(ETH_P_IPV6))
1755 tx_flags |= I40E_TX_FLAGS_IPV6;
1756
1757 tso = i40e_tso(tx_ring, skb, tx_flags, protocol, &hdr_len,
1758 &cd_type_cmd_tso_mss, &cd_tunneling);
1759
1760 if (tso < 0)
1761 goto out_drop;
1762 else if (tso)
1763 tx_flags |= I40E_TX_FLAGS_TSO;
1764
1765 skb_tx_timestamp(skb);
1766
b1941306
AD
1767 /* always enable CRC insertion offload */
1768 td_cmd |= I40E_TX_DESC_CMD_ICRC;
1769
fd0a05ce 1770 /* Always offload the checksum, since it's in the data descriptor */
b1941306 1771 if (skb->ip_summed == CHECKSUM_PARTIAL) {
fd0a05ce
JB
1772 tx_flags |= I40E_TX_FLAGS_CSUM;
1773
fd0a05ce
JB
1774 i40e_tx_enable_csum(skb, tx_flags, &td_cmd, &td_offset,
1775 tx_ring, &cd_tunneling);
b1941306 1776 }
fd0a05ce
JB
1777
1778 i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
1779 cd_tunneling, cd_l2tag2);
1780
1781 /* Add Flow Director ATR if it's enabled.
1782 *
1783 * NOTE: this must always be directly before the data descriptor.
1784 */
1785 i40e_atr(tx_ring, skb, tx_flags, protocol);
1786
1787 i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
1788 td_cmd, td_offset);
1789
1790 i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
1791
1792 return NETDEV_TX_OK;
1793
1794out_drop:
1795 dev_kfree_skb_any(skb);
1796 return NETDEV_TX_OK;
1797}
1798
1799/**
1800 * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
1801 * @skb: send buffer
1802 * @netdev: network interface device structure
1803 *
1804 * Returns NETDEV_TX_OK if sent, else an error code
1805 **/
1806netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1807{
1808 struct i40e_netdev_priv *np = netdev_priv(netdev);
1809 struct i40e_vsi *vsi = np->vsi;
1810 struct i40e_ring *tx_ring = &vsi->tx_rings[skb->queue_mapping];
1811
1812 /* hardware can't handle really short frames, hardware padding works
1813 * beyond this point
1814 */
1815 if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
1816 if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
1817 return NETDEV_TX_OK;
1818 skb->len = I40E_MIN_TX_LEN;
1819 skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
1820 }
1821
1822 return i40e_xmit_frame_ring(skb, tx_ring);
1823}