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