2 * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <linux/mlx4/cq.h>
36 #include <linux/slab.h>
37 #include <linux/mlx4/qp.h>
38 #include <linux/skbuff.h>
39 #include <linux/if_vlan.h>
40 #include <linux/vmalloc.h>
41 #include <linux/tcp.h>
46 MAX_INLINE
= 104, /* 128 - 16 - 4 - 4 */
49 static int inline_thold __read_mostly
= MAX_INLINE
;
51 module_param_named(inline_thold
, inline_thold
, int, 0444);
52 MODULE_PARM_DESC(inline_thold
, "threshold for using inline data");
54 int mlx4_en_create_tx_ring(struct mlx4_en_priv
*priv
,
55 struct mlx4_en_tx_ring
*ring
, u32 size
,
58 struct mlx4_en_dev
*mdev
= priv
->mdev
;
63 ring
->size_mask
= size
- 1;
64 ring
->stride
= stride
;
66 inline_thold
= min(inline_thold
, MAX_INLINE
);
68 spin_lock_init(&ring
->comp_lock
);
70 tmp
= size
* sizeof(struct mlx4_en_tx_info
);
71 ring
->tx_info
= vmalloc(tmp
);
73 en_err(priv
, "Failed allocating tx_info ring\n");
76 en_dbg(DRV
, priv
, "Allocated tx_info ring at addr:%p size:%d\n",
79 ring
->bounce_buf
= kmalloc(MAX_DESC_SIZE
, GFP_KERNEL
);
80 if (!ring
->bounce_buf
) {
81 en_err(priv
, "Failed allocating bounce buffer\n");
85 ring
->buf_size
= ALIGN(size
* ring
->stride
, MLX4_EN_PAGE_SIZE
);
87 err
= mlx4_alloc_hwq_res(mdev
->dev
, &ring
->wqres
, ring
->buf_size
,
90 en_err(priv
, "Failed allocating hwq resources\n");
94 err
= mlx4_en_map_buffer(&ring
->wqres
.buf
);
96 en_err(priv
, "Failed to map TX buffer\n");
100 ring
->buf
= ring
->wqres
.buf
.direct
.buf
;
102 en_dbg(DRV
, priv
, "Allocated TX ring (addr:%p) - buf:%p size:%d "
103 "buf_size:%d dma:%llx\n", ring
, ring
->buf
, ring
->size
,
104 ring
->buf_size
, (unsigned long long) ring
->wqres
.buf
.direct
.map
);
106 err
= mlx4_qp_reserve_range(mdev
->dev
, 1, 1, &ring
->qpn
);
108 en_err(priv
, "Failed reserving qp for tx ring.\n");
112 err
= mlx4_qp_alloc(mdev
->dev
, ring
->qpn
, &ring
->qp
);
114 en_err(priv
, "Failed allocating qp %d\n", ring
->qpn
);
117 ring
->qp
.event
= mlx4_en_sqp_event
;
122 mlx4_qp_release_range(mdev
->dev
, ring
->qpn
, 1);
124 mlx4_en_unmap_buffer(&ring
->wqres
.buf
);
126 mlx4_free_hwq_res(mdev
->dev
, &ring
->wqres
, ring
->buf_size
);
128 kfree(ring
->bounce_buf
);
129 ring
->bounce_buf
= NULL
;
131 vfree(ring
->tx_info
);
132 ring
->tx_info
= NULL
;
136 void mlx4_en_destroy_tx_ring(struct mlx4_en_priv
*priv
,
137 struct mlx4_en_tx_ring
*ring
)
139 struct mlx4_en_dev
*mdev
= priv
->mdev
;
140 en_dbg(DRV
, priv
, "Destroying tx ring, qpn: %d\n", ring
->qpn
);
142 mlx4_qp_remove(mdev
->dev
, &ring
->qp
);
143 mlx4_qp_free(mdev
->dev
, &ring
->qp
);
144 mlx4_qp_release_range(mdev
->dev
, ring
->qpn
, 1);
145 mlx4_en_unmap_buffer(&ring
->wqres
.buf
);
146 mlx4_free_hwq_res(mdev
->dev
, &ring
->wqres
, ring
->buf_size
);
147 kfree(ring
->bounce_buf
);
148 ring
->bounce_buf
= NULL
;
149 vfree(ring
->tx_info
);
150 ring
->tx_info
= NULL
;
153 int mlx4_en_activate_tx_ring(struct mlx4_en_priv
*priv
,
154 struct mlx4_en_tx_ring
*ring
,
157 struct mlx4_en_dev
*mdev
= priv
->mdev
;
162 ring
->cons
= 0xffffffff;
163 ring
->last_nr_txbb
= 1;
166 memset(ring
->tx_info
, 0, ring
->size
* sizeof(struct mlx4_en_tx_info
));
167 memset(ring
->buf
, 0, ring
->buf_size
);
169 ring
->qp_state
= MLX4_QP_STATE_RST
;
170 ring
->doorbell_qpn
= swab32(ring
->qp
.qpn
<< 8);
172 mlx4_en_fill_qp_context(priv
, ring
->size
, ring
->stride
, 1, 0, ring
->qpn
,
173 ring
->cqn
, &ring
->context
);
175 err
= mlx4_qp_to_ready(mdev
->dev
, &ring
->wqres
.mtt
, &ring
->context
,
176 &ring
->qp
, &ring
->qp_state
);
181 void mlx4_en_deactivate_tx_ring(struct mlx4_en_priv
*priv
,
182 struct mlx4_en_tx_ring
*ring
)
184 struct mlx4_en_dev
*mdev
= priv
->mdev
;
186 mlx4_qp_modify(mdev
->dev
, NULL
, ring
->qp_state
,
187 MLX4_QP_STATE_RST
, NULL
, 0, 0, &ring
->qp
);
191 static u32
mlx4_en_free_tx_desc(struct mlx4_en_priv
*priv
,
192 struct mlx4_en_tx_ring
*ring
,
195 struct mlx4_en_dev
*mdev
= priv
->mdev
;
196 struct mlx4_en_tx_info
*tx_info
= &ring
->tx_info
[index
];
197 struct mlx4_en_tx_desc
*tx_desc
= ring
->buf
+ index
* TXBB_SIZE
;
198 struct mlx4_wqe_data_seg
*data
= (void *) tx_desc
+ tx_info
->data_offset
;
199 struct sk_buff
*skb
= tx_info
->skb
;
200 struct skb_frag_struct
*frag
;
201 void *end
= ring
->buf
+ ring
->buf_size
;
202 int frags
= skb_shinfo(skb
)->nr_frags
;
204 __be32
*ptr
= (__be32
*)tx_desc
;
205 __be32 stamp
= cpu_to_be32(STAMP_VAL
| (!!owner
<< STAMP_SHIFT
));
207 /* Optimize the common case when there are no wraparounds */
208 if (likely((void *) tx_desc
+ tx_info
->nr_txbb
* TXBB_SIZE
<= end
)) {
210 if (tx_info
->linear
) {
211 pci_unmap_single(mdev
->pdev
,
212 (dma_addr_t
) be64_to_cpu(data
->addr
),
213 be32_to_cpu(data
->byte_count
),
218 for (i
= 0; i
< frags
; i
++) {
219 frag
= &skb_shinfo(skb
)->frags
[i
];
220 pci_unmap_page(mdev
->pdev
,
221 (dma_addr_t
) be64_to_cpu(data
[i
].addr
),
222 frag
->size
, PCI_DMA_TODEVICE
);
225 /* Stamp the freed descriptor */
226 for (i
= 0; i
< tx_info
->nr_txbb
* TXBB_SIZE
; i
+= STAMP_STRIDE
) {
233 if ((void *) data
>= end
) {
234 data
= (struct mlx4_wqe_data_seg
*)
235 (ring
->buf
+ ((void *) data
- end
));
238 if (tx_info
->linear
) {
239 pci_unmap_single(mdev
->pdev
,
240 (dma_addr_t
) be64_to_cpu(data
->addr
),
241 be32_to_cpu(data
->byte_count
),
246 for (i
= 0; i
< frags
; i
++) {
247 /* Check for wraparound before unmapping */
248 if ((void *) data
>= end
)
249 data
= (struct mlx4_wqe_data_seg
*) ring
->buf
;
250 frag
= &skb_shinfo(skb
)->frags
[i
];
251 pci_unmap_page(mdev
->pdev
,
252 (dma_addr_t
) be64_to_cpu(data
->addr
),
253 frag
->size
, PCI_DMA_TODEVICE
);
257 /* Stamp the freed descriptor */
258 for (i
= 0; i
< tx_info
->nr_txbb
* TXBB_SIZE
; i
+= STAMP_STRIDE
) {
261 if ((void *) ptr
>= end
) {
263 stamp
^= cpu_to_be32(0x80000000);
268 dev_kfree_skb_any(skb
);
269 return tx_info
->nr_txbb
;
273 int mlx4_en_free_tx_buf(struct net_device
*dev
, struct mlx4_en_tx_ring
*ring
)
275 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
278 /* Skip last polled descriptor */
279 ring
->cons
+= ring
->last_nr_txbb
;
280 en_dbg(DRV
, priv
, "Freeing Tx buf - cons:0x%x prod:0x%x\n",
281 ring
->cons
, ring
->prod
);
283 if ((u32
) (ring
->prod
- ring
->cons
) > ring
->size
) {
284 if (netif_msg_tx_err(priv
))
285 en_warn(priv
, "Tx consumer passed producer!\n");
289 while (ring
->cons
!= ring
->prod
) {
290 ring
->last_nr_txbb
= mlx4_en_free_tx_desc(priv
, ring
,
291 ring
->cons
& ring
->size_mask
,
292 !!(ring
->cons
& ring
->size
));
293 ring
->cons
+= ring
->last_nr_txbb
;
298 en_dbg(DRV
, priv
, "Freed %d uncompleted tx descriptors\n", cnt
);
304 static void mlx4_en_process_tx_cq(struct net_device
*dev
, struct mlx4_en_cq
*cq
)
306 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
307 struct mlx4_cq
*mcq
= &cq
->mcq
;
308 struct mlx4_en_tx_ring
*ring
= &priv
->tx_ring
[cq
->ring
];
309 struct mlx4_cqe
*cqe
= cq
->buf
;
312 u32 txbbs_skipped
= 0;
315 /* index always points to the first TXBB of the last polled descriptor */
316 index
= ring
->cons
& ring
->size_mask
;
317 new_index
= be16_to_cpu(cqe
->wqe_index
) & ring
->size_mask
;
318 if (index
== new_index
)
325 * We use a two-stage loop:
326 * - the first samples the HW-updated CQE
327 * - the second frees TXBBs until the last sample
328 * This lets us amortize CQE cache misses, while still polling the CQ
329 * until is quiescent.
331 cq_last_sav
= mcq
->cons_index
;
334 /* Skip over last polled CQE */
335 index
= (index
+ ring
->last_nr_txbb
) & ring
->size_mask
;
336 txbbs_skipped
+= ring
->last_nr_txbb
;
339 ring
->last_nr_txbb
= mlx4_en_free_tx_desc(
341 !!((ring
->cons
+ txbbs_skipped
) &
345 } while (index
!= new_index
);
347 new_index
= be16_to_cpu(cqe
->wqe_index
) & ring
->size_mask
;
348 } while (index
!= new_index
);
349 AVG_PERF_COUNTER(priv
->pstats
.tx_coal_avg
,
350 (u32
) (mcq
->cons_index
- cq_last_sav
));
353 * To prevent CQ overflow we first update CQ consumer and only then
358 ring
->cons
+= txbbs_skipped
;
360 /* Wakeup Tx queue if this ring stopped it */
361 if (unlikely(ring
->blocked
)) {
362 if ((u32
) (ring
->prod
- ring
->cons
) <=
363 ring
->size
- HEADROOM
- MAX_DESC_TXBBS
) {
365 netif_tx_wake_queue(netdev_get_tx_queue(dev
, cq
->ring
));
366 priv
->port_stats
.wake_queue
++;
371 void mlx4_en_tx_irq(struct mlx4_cq
*mcq
)
373 struct mlx4_en_cq
*cq
= container_of(mcq
, struct mlx4_en_cq
, mcq
);
374 struct mlx4_en_priv
*priv
= netdev_priv(cq
->dev
);
375 struct mlx4_en_tx_ring
*ring
= &priv
->tx_ring
[cq
->ring
];
377 if (!spin_trylock(&ring
->comp_lock
))
379 mlx4_en_process_tx_cq(cq
->dev
, cq
);
380 mod_timer(&cq
->timer
, jiffies
+ 1);
381 spin_unlock(&ring
->comp_lock
);
385 void mlx4_en_poll_tx_cq(unsigned long data
)
387 struct mlx4_en_cq
*cq
= (struct mlx4_en_cq
*) data
;
388 struct mlx4_en_priv
*priv
= netdev_priv(cq
->dev
);
389 struct mlx4_en_tx_ring
*ring
= &priv
->tx_ring
[cq
->ring
];
392 INC_PERF_COUNTER(priv
->pstats
.tx_poll
);
394 if (!spin_trylock_irq(&ring
->comp_lock
)) {
395 mod_timer(&cq
->timer
, jiffies
+ MLX4_EN_TX_POLL_TIMEOUT
);
398 mlx4_en_process_tx_cq(cq
->dev
, cq
);
399 inflight
= (u32
) (ring
->prod
- ring
->cons
- ring
->last_nr_txbb
);
401 /* If there are still packets in flight and the timer has not already
402 * been scheduled by the Tx routine then schedule it here to guarantee
403 * completion processing of these packets */
404 if (inflight
&& priv
->port_up
)
405 mod_timer(&cq
->timer
, jiffies
+ MLX4_EN_TX_POLL_TIMEOUT
);
407 spin_unlock_irq(&ring
->comp_lock
);
410 static struct mlx4_en_tx_desc
*mlx4_en_bounce_to_desc(struct mlx4_en_priv
*priv
,
411 struct mlx4_en_tx_ring
*ring
,
413 unsigned int desc_size
)
415 u32 copy
= (ring
->size
- index
) * TXBB_SIZE
;
418 for (i
= desc_size
- copy
- 4; i
>= 0; i
-= 4) {
419 if ((i
& (TXBB_SIZE
- 1)) == 0)
422 *((u32
*) (ring
->buf
+ i
)) =
423 *((u32
*) (ring
->bounce_buf
+ copy
+ i
));
426 for (i
= copy
- 4; i
>= 4 ; i
-= 4) {
427 if ((i
& (TXBB_SIZE
- 1)) == 0)
430 *((u32
*) (ring
->buf
+ index
* TXBB_SIZE
+ i
)) =
431 *((u32
*) (ring
->bounce_buf
+ i
));
434 /* Return real descriptor location */
435 return ring
->buf
+ index
* TXBB_SIZE
;
438 static inline void mlx4_en_xmit_poll(struct mlx4_en_priv
*priv
, int tx_ind
)
440 struct mlx4_en_cq
*cq
= &priv
->tx_cq
[tx_ind
];
441 struct mlx4_en_tx_ring
*ring
= &priv
->tx_ring
[tx_ind
];
444 /* If we don't have a pending timer, set one up to catch our recent
445 post in case the interface becomes idle */
446 if (!timer_pending(&cq
->timer
))
447 mod_timer(&cq
->timer
, jiffies
+ MLX4_EN_TX_POLL_TIMEOUT
);
449 /* Poll the CQ every mlx4_en_TX_MODER_POLL packets */
450 if ((++ring
->poll_cnt
& (MLX4_EN_TX_POLL_MODER
- 1)) == 0)
451 if (spin_trylock_irqsave(&ring
->comp_lock
, flags
)) {
452 mlx4_en_process_tx_cq(priv
->dev
, cq
);
453 spin_unlock_irqrestore(&ring
->comp_lock
, flags
);
457 static void *get_frag_ptr(struct sk_buff
*skb
)
459 struct skb_frag_struct
*frag
= &skb_shinfo(skb
)->frags
[0];
460 struct page
*page
= frag
->page
;
463 ptr
= page_address(page
);
467 return ptr
+ frag
->page_offset
;
470 static int is_inline(struct sk_buff
*skb
, void **pfrag
)
474 if (inline_thold
&& !skb_is_gso(skb
) && skb
->len
<= inline_thold
) {
475 if (skb_shinfo(skb
)->nr_frags
== 1) {
476 ptr
= get_frag_ptr(skb
);
484 } else if (unlikely(skb_shinfo(skb
)->nr_frags
))
493 static int inline_size(struct sk_buff
*skb
)
495 if (skb
->len
+ CTRL_SIZE
+ sizeof(struct mlx4_wqe_inline_seg
)
496 <= MLX4_INLINE_ALIGN
)
497 return ALIGN(skb
->len
+ CTRL_SIZE
+
498 sizeof(struct mlx4_wqe_inline_seg
), 16);
500 return ALIGN(skb
->len
+ CTRL_SIZE
+ 2 *
501 sizeof(struct mlx4_wqe_inline_seg
), 16);
504 static int get_real_size(struct sk_buff
*skb
, struct net_device
*dev
,
505 int *lso_header_size
)
507 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
510 if (skb_is_gso(skb
)) {
511 *lso_header_size
= skb_transport_offset(skb
) + tcp_hdrlen(skb
);
512 real_size
= CTRL_SIZE
+ skb_shinfo(skb
)->nr_frags
* DS_SIZE
+
513 ALIGN(*lso_header_size
+ 4, DS_SIZE
);
514 if (unlikely(*lso_header_size
!= skb_headlen(skb
))) {
515 /* We add a segment for the skb linear buffer only if
516 * it contains data */
517 if (*lso_header_size
< skb_headlen(skb
))
518 real_size
+= DS_SIZE
;
520 if (netif_msg_tx_err(priv
))
521 en_warn(priv
, "Non-linear headers\n");
526 *lso_header_size
= 0;
527 if (!is_inline(skb
, NULL
))
528 real_size
= CTRL_SIZE
+ (skb_shinfo(skb
)->nr_frags
+ 1) * DS_SIZE
;
530 real_size
= inline_size(skb
);
536 static void build_inline_wqe(struct mlx4_en_tx_desc
*tx_desc
, struct sk_buff
*skb
,
537 int real_size
, u16
*vlan_tag
, int tx_ind
, void *fragptr
)
539 struct mlx4_wqe_inline_seg
*inl
= &tx_desc
->inl
;
540 int spc
= MLX4_INLINE_ALIGN
- CTRL_SIZE
- sizeof *inl
;
542 if (skb
->len
<= spc
) {
543 inl
->byte_count
= cpu_to_be32(1 << 31 | skb
->len
);
544 skb_copy_from_linear_data(skb
, inl
+ 1, skb_headlen(skb
));
545 if (skb_shinfo(skb
)->nr_frags
)
546 memcpy(((void *)(inl
+ 1)) + skb_headlen(skb
), fragptr
,
547 skb_shinfo(skb
)->frags
[0].size
);
550 inl
->byte_count
= cpu_to_be32(1 << 31 | spc
);
551 if (skb_headlen(skb
) <= spc
) {
552 skb_copy_from_linear_data(skb
, inl
+ 1, skb_headlen(skb
));
553 if (skb_headlen(skb
) < spc
) {
554 memcpy(((void *)(inl
+ 1)) + skb_headlen(skb
),
555 fragptr
, spc
- skb_headlen(skb
));
556 fragptr
+= spc
- skb_headlen(skb
);
558 inl
= (void *) (inl
+ 1) + spc
;
559 memcpy(((void *)(inl
+ 1)), fragptr
, skb
->len
- spc
);
561 skb_copy_from_linear_data(skb
, inl
+ 1, spc
);
562 inl
= (void *) (inl
+ 1) + spc
;
563 skb_copy_from_linear_data_offset(skb
, spc
, inl
+ 1,
564 skb_headlen(skb
) - spc
);
565 if (skb_shinfo(skb
)->nr_frags
)
566 memcpy(((void *)(inl
+ 1)) + skb_headlen(skb
) - spc
,
567 fragptr
, skb_shinfo(skb
)->frags
[0].size
);
571 inl
->byte_count
= cpu_to_be32(1 << 31 | (skb
->len
- spc
));
573 tx_desc
->ctrl
.vlan_tag
= cpu_to_be16(*vlan_tag
);
574 tx_desc
->ctrl
.ins_vlan
= MLX4_WQE_CTRL_INS_VLAN
* !!(*vlan_tag
);
575 tx_desc
->ctrl
.fence_size
= (real_size
/ 16) & 0x3f;
578 u16
mlx4_en_select_queue(struct net_device
*dev
, struct sk_buff
*skb
)
580 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
583 /* If we support per priority flow control and the packet contains
584 * a vlan tag, send the packet to the TX ring assigned to that priority
586 if (priv
->prof
->rx_ppp
&& vlan_tx_tag_present(skb
)) {
587 vlan_tag
= vlan_tx_tag_get(skb
);
588 return MLX4_EN_NUM_TX_RINGS
+ (vlan_tag
>> 13);
591 return skb_tx_hash(dev
, skb
);
594 netdev_tx_t
mlx4_en_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
596 struct mlx4_en_priv
*priv
= netdev_priv(dev
);
597 struct mlx4_en_dev
*mdev
= priv
->mdev
;
598 struct mlx4_en_tx_ring
*ring
;
599 struct mlx4_en_cq
*cq
;
600 struct mlx4_en_tx_desc
*tx_desc
;
601 struct mlx4_wqe_data_seg
*data
;
602 struct skb_frag_struct
*frag
;
603 struct mlx4_en_tx_info
*tx_info
;
622 real_size
= get_real_size(skb
, dev
, &lso_header_size
);
623 if (unlikely(!real_size
))
626 /* Allign descriptor to TXBB size */
627 desc_size
= ALIGN(real_size
, TXBB_SIZE
);
628 nr_txbb
= desc_size
/ TXBB_SIZE
;
629 if (unlikely(nr_txbb
> MAX_DESC_TXBBS
)) {
630 if (netif_msg_tx_err(priv
))
631 en_warn(priv
, "Oversized header or SG list\n");
635 tx_ind
= skb
->queue_mapping
;
636 ring
= &priv
->tx_ring
[tx_ind
];
637 if (vlan_tx_tag_present(skb
))
638 vlan_tag
= vlan_tx_tag_get(skb
);
640 /* Check available TXBBs And 2K spare for prefetch */
641 if (unlikely(((int)(ring
->prod
- ring
->cons
)) >
642 ring
->size
- HEADROOM
- MAX_DESC_TXBBS
)) {
643 /* every full Tx ring stops queue */
644 netif_tx_stop_queue(netdev_get_tx_queue(dev
, tx_ind
));
646 priv
->port_stats
.queue_stopped
++;
648 /* Use interrupts to find out when queue opened */
649 cq
= &priv
->tx_cq
[tx_ind
];
650 mlx4_en_arm_cq(priv
, cq
);
651 return NETDEV_TX_BUSY
;
654 /* Track current inflight packets for performance analysis */
655 AVG_PERF_COUNTER(priv
->pstats
.inflight_avg
,
656 (u32
) (ring
->prod
- ring
->cons
- 1));
658 /* Packet is good - grab an index and transmit it */
659 index
= ring
->prod
& ring
->size_mask
;
661 /* See if we have enough space for whole descriptor TXBB for setting
662 * SW ownership on next descriptor; if not, use a bounce buffer. */
663 if (likely(index
+ nr_txbb
<= ring
->size
))
664 tx_desc
= ring
->buf
+ index
* TXBB_SIZE
;
666 tx_desc
= (struct mlx4_en_tx_desc
*) ring
->bounce_buf
;
668 /* Save skb in tx_info ring */
669 tx_info
= &ring
->tx_info
[index
];
671 tx_info
->nr_txbb
= nr_txbb
;
673 /* Prepare ctrl segement apart opcode+ownership, which depends on
674 * whether LSO is used */
675 tx_desc
->ctrl
.vlan_tag
= cpu_to_be16(vlan_tag
);
676 tx_desc
->ctrl
.ins_vlan
= MLX4_WQE_CTRL_INS_VLAN
* !!vlan_tag
;
677 tx_desc
->ctrl
.fence_size
= (real_size
/ 16) & 0x3f;
678 tx_desc
->ctrl
.srcrb_flags
= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE
|
679 MLX4_WQE_CTRL_SOLICITED
);
680 if (likely(skb
->ip_summed
== CHECKSUM_PARTIAL
)) {
681 tx_desc
->ctrl
.srcrb_flags
|= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM
|
682 MLX4_WQE_CTRL_TCP_UDP_CSUM
);
683 priv
->port_stats
.tx_chksum_offload
++;
686 if (unlikely(priv
->validate_loopback
)) {
687 /* Copy dst mac address to wqe */
688 skb_reset_mac_header(skb
);
690 if (ethh
&& ethh
->h_dest
) {
691 mac
= mlx4_en_mac_to_u64(ethh
->h_dest
);
692 mac_h
= (u32
) ((mac
& 0xffff00000000ULL
) >> 16);
693 mac_l
= (u32
) (mac
& 0xffffffff);
694 tx_desc
->ctrl
.srcrb_flags
|= cpu_to_be32(mac_h
);
695 tx_desc
->ctrl
.imm
= cpu_to_be32(mac_l
);
699 /* Handle LSO (TSO) packets */
700 if (lso_header_size
) {
701 /* Mark opcode as LSO */
702 op_own
= cpu_to_be32(MLX4_OPCODE_LSO
| (1 << 6)) |
703 ((ring
->prod
& ring
->size
) ?
704 cpu_to_be32(MLX4_EN_BIT_DESC_OWN
) : 0);
706 /* Fill in the LSO prefix */
707 tx_desc
->lso
.mss_hdr_size
= cpu_to_be32(
708 skb_shinfo(skb
)->gso_size
<< 16 | lso_header_size
);
711 * note that we already verified that it is linear */
712 memcpy(tx_desc
->lso
.header
, skb
->data
, lso_header_size
);
713 data
= ((void *) &tx_desc
->lso
+
714 ALIGN(lso_header_size
+ 4, DS_SIZE
));
716 priv
->port_stats
.tso_packets
++;
717 i
= ((skb
->len
- lso_header_size
) / skb_shinfo(skb
)->gso_size
) +
718 !!((skb
->len
- lso_header_size
) % skb_shinfo(skb
)->gso_size
);
719 ring
->bytes
+= skb
->len
+ (i
- 1) * lso_header_size
;
722 /* Normal (Non LSO) packet */
723 op_own
= cpu_to_be32(MLX4_OPCODE_SEND
) |
724 ((ring
->prod
& ring
->size
) ?
725 cpu_to_be32(MLX4_EN_BIT_DESC_OWN
) : 0);
726 data
= &tx_desc
->data
;
727 ring
->bytes
+= max(skb
->len
, (unsigned int) ETH_ZLEN
);
731 AVG_PERF_COUNTER(priv
->pstats
.tx_pktsz_avg
, skb
->len
);
734 /* valid only for none inline segments */
735 tx_info
->data_offset
= (void *) data
- (void *) tx_desc
;
737 tx_info
->linear
= (lso_header_size
< skb_headlen(skb
) && !is_inline(skb
, NULL
)) ? 1 : 0;
738 data
+= skb_shinfo(skb
)->nr_frags
+ tx_info
->linear
- 1;
740 if (!is_inline(skb
, &fragptr
)) {
742 for (i
= skb_shinfo(skb
)->nr_frags
- 1; i
>= 0; i
--) {
743 frag
= &skb_shinfo(skb
)->frags
[i
];
744 dma
= pci_map_page(mdev
->dev
->pdev
, frag
->page
, frag
->page_offset
,
745 frag
->size
, PCI_DMA_TODEVICE
);
746 data
->addr
= cpu_to_be64(dma
);
747 data
->lkey
= cpu_to_be32(mdev
->mr
.key
);
749 data
->byte_count
= cpu_to_be32(frag
->size
);
753 /* Map linear part */
754 if (tx_info
->linear
) {
755 dma
= pci_map_single(mdev
->dev
->pdev
, skb
->data
+ lso_header_size
,
756 skb_headlen(skb
) - lso_header_size
, PCI_DMA_TODEVICE
);
757 data
->addr
= cpu_to_be64(dma
);
758 data
->lkey
= cpu_to_be32(mdev
->mr
.key
);
760 data
->byte_count
= cpu_to_be32(skb_headlen(skb
) - lso_header_size
);
764 build_inline_wqe(tx_desc
, skb
, real_size
, &vlan_tag
, tx_ind
, fragptr
);
768 ring
->prod
+= nr_txbb
;
770 /* If we used a bounce buffer then copy descriptor back into place */
771 if (tx_desc
== (struct mlx4_en_tx_desc
*) ring
->bounce_buf
)
772 tx_desc
= mlx4_en_bounce_to_desc(priv
, ring
, index
, desc_size
);
774 /* Run destructor before passing skb to HW */
775 if (likely(!skb_shared(skb
)))
778 /* Ensure new descirptor hits memory
779 * before setting ownership of this descriptor to HW */
781 tx_desc
->ctrl
.owner_opcode
= op_own
;
785 writel(ring
->doorbell_qpn
, mdev
->uar_map
+ MLX4_SEND_DOORBELL
);
788 mlx4_en_xmit_poll(priv
, tx_ind
);
793 dev_kfree_skb_any(skb
);
794 priv
->stats
.tx_dropped
++;