ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / drivers / net / mlx4 / en_tx.c
blobb229acf1855f89ba8323d6b896af5c159b74a6bf
1 /*
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
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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
30 * SOFTWARE.
34 #include <asm/page.h>
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>
43 #include "mlx4_en.h"
45 enum {
46 MAX_INLINE = 104, /* 128 - 16 - 4 - 4 */
47 MAX_BF = 256,
50 static int inline_thold __read_mostly = MAX_INLINE;
52 module_param_named(inline_thold, inline_thold, int, 0444);
53 MODULE_PARM_DESC(inline_thold, "threshold for using inline data");
55 int mlx4_en_create_tx_ring(struct mlx4_en_priv *priv,
56 struct mlx4_en_tx_ring *ring, int qpn, u32 size,
57 u16 stride)
59 struct mlx4_en_dev *mdev = priv->mdev;
60 int tmp;
61 int err;
63 ring->size = size;
64 ring->size_mask = size - 1;
65 ring->stride = stride;
67 inline_thold = min(inline_thold, MAX_INLINE);
69 spin_lock_init(&ring->comp_lock);
71 tmp = size * sizeof(struct mlx4_en_tx_info);
72 ring->tx_info = vmalloc(tmp);
73 if (!ring->tx_info) {
74 en_err(priv, "Failed allocating tx_info ring\n");
75 return -ENOMEM;
77 en_dbg(DRV, priv, "Allocated tx_info ring at addr:%p size:%d\n",
78 ring->tx_info, tmp);
80 ring->bounce_buf = kmalloc(MAX_DESC_SIZE, GFP_KERNEL);
81 if (!ring->bounce_buf) {
82 en_err(priv, "Failed allocating bounce buffer\n");
83 err = -ENOMEM;
84 goto err_tx;
86 ring->buf_size = ALIGN(size * ring->stride, MLX4_EN_PAGE_SIZE);
88 err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres, ring->buf_size,
89 2 * PAGE_SIZE);
90 if (err) {
91 en_err(priv, "Failed allocating hwq resources\n");
92 goto err_bounce;
95 err = mlx4_en_map_buffer(&ring->wqres.buf);
96 if (err) {
97 en_err(priv, "Failed to map TX buffer\n");
98 goto err_hwq_res;
101 ring->buf = ring->wqres.buf.direct.buf;
103 en_dbg(DRV, priv, "Allocated TX ring (addr:%p) - buf:%p size:%d "
104 "buf_size:%d dma:%llx\n", ring, ring->buf, ring->size,
105 ring->buf_size, (unsigned long long) ring->wqres.buf.direct.map);
107 ring->qpn = qpn;
108 err = mlx4_qp_alloc(mdev->dev, ring->qpn, &ring->qp);
109 if (err) {
110 en_err(priv, "Failed allocating qp %d\n", ring->qpn);
111 goto err_map;
113 ring->qp.event = mlx4_en_sqp_event;
115 err = mlx4_bf_alloc(mdev->dev, &ring->bf);
116 if (err) {
117 en_dbg(DRV, priv, "working without blueflame (%d)", err);
118 ring->bf.uar = &mdev->priv_uar;
119 ring->bf.uar->map = mdev->uar_map;
120 ring->bf_enabled = false;
121 } else
122 ring->bf_enabled = true;
124 return 0;
126 err_map:
127 mlx4_en_unmap_buffer(&ring->wqres.buf);
128 err_hwq_res:
129 mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
130 err_bounce:
131 kfree(ring->bounce_buf);
132 ring->bounce_buf = NULL;
133 err_tx:
134 vfree(ring->tx_info);
135 ring->tx_info = NULL;
136 return err;
139 void mlx4_en_destroy_tx_ring(struct mlx4_en_priv *priv,
140 struct mlx4_en_tx_ring *ring)
142 struct mlx4_en_dev *mdev = priv->mdev;
143 en_dbg(DRV, priv, "Destroying tx ring, qpn: %d\n", ring->qpn);
145 if (ring->bf_enabled)
146 mlx4_bf_free(mdev->dev, &ring->bf);
147 mlx4_qp_remove(mdev->dev, &ring->qp);
148 mlx4_qp_free(mdev->dev, &ring->qp);
149 mlx4_qp_release_range(mdev->dev, ring->qpn, 1);
150 mlx4_en_unmap_buffer(&ring->wqres.buf);
151 mlx4_free_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
152 kfree(ring->bounce_buf);
153 ring->bounce_buf = NULL;
154 vfree(ring->tx_info);
155 ring->tx_info = NULL;
158 int mlx4_en_activate_tx_ring(struct mlx4_en_priv *priv,
159 struct mlx4_en_tx_ring *ring,
160 int cq)
162 struct mlx4_en_dev *mdev = priv->mdev;
163 int err;
165 ring->cqn = cq;
166 ring->prod = 0;
167 ring->cons = 0xffffffff;
168 ring->last_nr_txbb = 1;
169 ring->poll_cnt = 0;
170 ring->blocked = 0;
171 memset(ring->tx_info, 0, ring->size * sizeof(struct mlx4_en_tx_info));
172 memset(ring->buf, 0, ring->buf_size);
174 ring->qp_state = MLX4_QP_STATE_RST;
175 ring->doorbell_qpn = swab32(ring->qp.qpn << 8);
177 mlx4_en_fill_qp_context(priv, ring->size, ring->stride, 1, 0, ring->qpn,
178 ring->cqn, &ring->context);
179 if (ring->bf_enabled)
180 ring->context.usr_page = cpu_to_be32(ring->bf.uar->index);
182 err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, &ring->context,
183 &ring->qp, &ring->qp_state);
185 return err;
188 void mlx4_en_deactivate_tx_ring(struct mlx4_en_priv *priv,
189 struct mlx4_en_tx_ring *ring)
191 struct mlx4_en_dev *mdev = priv->mdev;
193 mlx4_qp_modify(mdev->dev, NULL, ring->qp_state,
194 MLX4_QP_STATE_RST, NULL, 0, 0, &ring->qp);
198 static u32 mlx4_en_free_tx_desc(struct mlx4_en_priv *priv,
199 struct mlx4_en_tx_ring *ring,
200 int index, u8 owner)
202 struct mlx4_en_dev *mdev = priv->mdev;
203 struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
204 struct mlx4_en_tx_desc *tx_desc = ring->buf + index * TXBB_SIZE;
205 struct mlx4_wqe_data_seg *data = (void *) tx_desc + tx_info->data_offset;
206 struct sk_buff *skb = tx_info->skb;
207 struct skb_frag_struct *frag;
208 void *end = ring->buf + ring->buf_size;
209 int frags = skb_shinfo(skb)->nr_frags;
210 int i;
211 __be32 *ptr = (__be32 *)tx_desc;
212 __be32 stamp = cpu_to_be32(STAMP_VAL | (!!owner << STAMP_SHIFT));
214 /* Optimize the common case when there are no wraparounds */
215 if (likely((void *) tx_desc + tx_info->nr_txbb * TXBB_SIZE <= end)) {
216 if (!tx_info->inl) {
217 if (tx_info->linear) {
218 pci_unmap_single(mdev->pdev,
219 (dma_addr_t) be64_to_cpu(data->addr),
220 be32_to_cpu(data->byte_count),
221 PCI_DMA_TODEVICE);
222 ++data;
225 for (i = 0; i < frags; i++) {
226 frag = &skb_shinfo(skb)->frags[i];
227 pci_unmap_page(mdev->pdev,
228 (dma_addr_t) be64_to_cpu(data[i].addr),
229 frag->size, PCI_DMA_TODEVICE);
232 /* Stamp the freed descriptor */
233 for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE; i += STAMP_STRIDE) {
234 *ptr = stamp;
235 ptr += STAMP_DWORDS;
238 } else {
239 if (!tx_info->inl) {
240 if ((void *) data >= end) {
241 data = (struct mlx4_wqe_data_seg *)
242 (ring->buf + ((void *) data - end));
245 if (tx_info->linear) {
246 pci_unmap_single(mdev->pdev,
247 (dma_addr_t) be64_to_cpu(data->addr),
248 be32_to_cpu(data->byte_count),
249 PCI_DMA_TODEVICE);
250 ++data;
253 for (i = 0; i < frags; i++) {
254 /* Check for wraparound before unmapping */
255 if ((void *) data >= end)
256 data = (struct mlx4_wqe_data_seg *) ring->buf;
257 frag = &skb_shinfo(skb)->frags[i];
258 pci_unmap_page(mdev->pdev,
259 (dma_addr_t) be64_to_cpu(data->addr),
260 frag->size, PCI_DMA_TODEVICE);
261 ++data;
264 /* Stamp the freed descriptor */
265 for (i = 0; i < tx_info->nr_txbb * TXBB_SIZE; i += STAMP_STRIDE) {
266 *ptr = stamp;
267 ptr += STAMP_DWORDS;
268 if ((void *) ptr >= end) {
269 ptr = ring->buf;
270 stamp ^= cpu_to_be32(0x80000000);
275 dev_kfree_skb_any(skb);
276 return tx_info->nr_txbb;
280 int mlx4_en_free_tx_buf(struct net_device *dev, struct mlx4_en_tx_ring *ring)
282 struct mlx4_en_priv *priv = netdev_priv(dev);
283 int cnt = 0;
285 /* Skip last polled descriptor */
286 ring->cons += ring->last_nr_txbb;
287 en_dbg(DRV, priv, "Freeing Tx buf - cons:0x%x prod:0x%x\n",
288 ring->cons, ring->prod);
290 if ((u32) (ring->prod - ring->cons) > ring->size) {
291 if (netif_msg_tx_err(priv))
292 en_warn(priv, "Tx consumer passed producer!\n");
293 return 0;
296 while (ring->cons != ring->prod) {
297 ring->last_nr_txbb = mlx4_en_free_tx_desc(priv, ring,
298 ring->cons & ring->size_mask,
299 !!(ring->cons & ring->size));
300 ring->cons += ring->last_nr_txbb;
301 cnt++;
304 if (cnt)
305 en_dbg(DRV, priv, "Freed %d uncompleted tx descriptors\n", cnt);
307 return cnt;
311 static void mlx4_en_process_tx_cq(struct net_device *dev, struct mlx4_en_cq *cq)
313 struct mlx4_en_priv *priv = netdev_priv(dev);
314 struct mlx4_cq *mcq = &cq->mcq;
315 struct mlx4_en_tx_ring *ring = &priv->tx_ring[cq->ring];
316 struct mlx4_cqe *cqe = cq->buf;
317 u16 index;
318 u16 new_index;
319 u32 txbbs_skipped = 0;
320 u32 cq_last_sav;
322 /* index always points to the first TXBB of the last polled descriptor */
323 index = ring->cons & ring->size_mask;
324 new_index = be16_to_cpu(cqe->wqe_index) & ring->size_mask;
325 if (index == new_index)
326 return;
328 if (!priv->port_up)
329 return;
332 * We use a two-stage loop:
333 * - the first samples the HW-updated CQE
334 * - the second frees TXBBs until the last sample
335 * This lets us amortize CQE cache misses, while still polling the CQ
336 * until is quiescent.
338 cq_last_sav = mcq->cons_index;
339 do {
340 do {
341 /* Skip over last polled CQE */
342 index = (index + ring->last_nr_txbb) & ring->size_mask;
343 txbbs_skipped += ring->last_nr_txbb;
345 /* Poll next CQE */
346 ring->last_nr_txbb = mlx4_en_free_tx_desc(
347 priv, ring, index,
348 !!((ring->cons + txbbs_skipped) &
349 ring->size));
350 ++mcq->cons_index;
352 } while (index != new_index);
354 new_index = be16_to_cpu(cqe->wqe_index) & ring->size_mask;
355 } while (index != new_index);
356 AVG_PERF_COUNTER(priv->pstats.tx_coal_avg,
357 (u32) (mcq->cons_index - cq_last_sav));
360 * To prevent CQ overflow we first update CQ consumer and only then
361 * the ring consumer.
363 mlx4_cq_set_ci(mcq);
364 wmb();
365 ring->cons += txbbs_skipped;
367 /* Wakeup Tx queue if this ring stopped it */
368 if (unlikely(ring->blocked)) {
369 if ((u32) (ring->prod - ring->cons) <=
370 ring->size - HEADROOM - MAX_DESC_TXBBS) {
371 ring->blocked = 0;
372 netif_tx_wake_queue(netdev_get_tx_queue(dev, cq->ring));
373 priv->port_stats.wake_queue++;
378 void mlx4_en_tx_irq(struct mlx4_cq *mcq)
380 struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
381 struct mlx4_en_priv *priv = netdev_priv(cq->dev);
382 struct mlx4_en_tx_ring *ring = &priv->tx_ring[cq->ring];
384 if (!spin_trylock(&ring->comp_lock))
385 return;
386 mlx4_en_process_tx_cq(cq->dev, cq);
387 mod_timer(&cq->timer, jiffies + 1);
388 spin_unlock(&ring->comp_lock);
392 void mlx4_en_poll_tx_cq(unsigned long data)
394 struct mlx4_en_cq *cq = (struct mlx4_en_cq *) data;
395 struct mlx4_en_priv *priv = netdev_priv(cq->dev);
396 struct mlx4_en_tx_ring *ring = &priv->tx_ring[cq->ring];
397 u32 inflight;
399 INC_PERF_COUNTER(priv->pstats.tx_poll);
401 if (!spin_trylock_irq(&ring->comp_lock)) {
402 mod_timer(&cq->timer, jiffies + MLX4_EN_TX_POLL_TIMEOUT);
403 return;
405 mlx4_en_process_tx_cq(cq->dev, cq);
406 inflight = (u32) (ring->prod - ring->cons - ring->last_nr_txbb);
408 /* If there are still packets in flight and the timer has not already
409 * been scheduled by the Tx routine then schedule it here to guarantee
410 * completion processing of these packets */
411 if (inflight && priv->port_up)
412 mod_timer(&cq->timer, jiffies + MLX4_EN_TX_POLL_TIMEOUT);
414 spin_unlock_irq(&ring->comp_lock);
417 static struct mlx4_en_tx_desc *mlx4_en_bounce_to_desc(struct mlx4_en_priv *priv,
418 struct mlx4_en_tx_ring *ring,
419 u32 index,
420 unsigned int desc_size)
422 u32 copy = (ring->size - index) * TXBB_SIZE;
423 int i;
425 for (i = desc_size - copy - 4; i >= 0; i -= 4) {
426 if ((i & (TXBB_SIZE - 1)) == 0)
427 wmb();
429 *((u32 *) (ring->buf + i)) =
430 *((u32 *) (ring->bounce_buf + copy + i));
433 for (i = copy - 4; i >= 4 ; i -= 4) {
434 if ((i & (TXBB_SIZE - 1)) == 0)
435 wmb();
437 *((u32 *) (ring->buf + index * TXBB_SIZE + i)) =
438 *((u32 *) (ring->bounce_buf + i));
441 /* Return real descriptor location */
442 return ring->buf + index * TXBB_SIZE;
445 static inline void mlx4_en_xmit_poll(struct mlx4_en_priv *priv, int tx_ind)
447 struct mlx4_en_cq *cq = &priv->tx_cq[tx_ind];
448 struct mlx4_en_tx_ring *ring = &priv->tx_ring[tx_ind];
449 unsigned long flags;
451 /* If we don't have a pending timer, set one up to catch our recent
452 post in case the interface becomes idle */
453 if (!timer_pending(&cq->timer))
454 mod_timer(&cq->timer, jiffies + MLX4_EN_TX_POLL_TIMEOUT);
456 /* Poll the CQ every mlx4_en_TX_MODER_POLL packets */
457 if ((++ring->poll_cnt & (MLX4_EN_TX_POLL_MODER - 1)) == 0)
458 if (spin_trylock_irqsave(&ring->comp_lock, flags)) {
459 mlx4_en_process_tx_cq(priv->dev, cq);
460 spin_unlock_irqrestore(&ring->comp_lock, flags);
464 static void *get_frag_ptr(struct sk_buff *skb)
466 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
467 struct page *page = frag->page;
468 void *ptr;
470 ptr = page_address(page);
471 if (unlikely(!ptr))
472 return NULL;
474 return ptr + frag->page_offset;
477 static int is_inline(struct sk_buff *skb, void **pfrag)
479 void *ptr;
481 if (inline_thold && !skb_is_gso(skb) && skb->len <= inline_thold) {
482 if (skb_shinfo(skb)->nr_frags == 1) {
483 ptr = get_frag_ptr(skb);
484 if (unlikely(!ptr))
485 return 0;
487 if (pfrag)
488 *pfrag = ptr;
490 return 1;
491 } else if (unlikely(skb_shinfo(skb)->nr_frags))
492 return 0;
493 else
494 return 1;
497 return 0;
500 static int inline_size(struct sk_buff *skb)
502 if (skb->len + CTRL_SIZE + sizeof(struct mlx4_wqe_inline_seg)
503 <= MLX4_INLINE_ALIGN)
504 return ALIGN(skb->len + CTRL_SIZE +
505 sizeof(struct mlx4_wqe_inline_seg), 16);
506 else
507 return ALIGN(skb->len + CTRL_SIZE + 2 *
508 sizeof(struct mlx4_wqe_inline_seg), 16);
511 static int get_real_size(struct sk_buff *skb, struct net_device *dev,
512 int *lso_header_size)
514 struct mlx4_en_priv *priv = netdev_priv(dev);
515 int real_size;
517 if (skb_is_gso(skb)) {
518 *lso_header_size = skb_transport_offset(skb) + tcp_hdrlen(skb);
519 real_size = CTRL_SIZE + skb_shinfo(skb)->nr_frags * DS_SIZE +
520 ALIGN(*lso_header_size + 4, DS_SIZE);
521 if (unlikely(*lso_header_size != skb_headlen(skb))) {
522 /* We add a segment for the skb linear buffer only if
523 * it contains data */
524 if (*lso_header_size < skb_headlen(skb))
525 real_size += DS_SIZE;
526 else {
527 if (netif_msg_tx_err(priv))
528 en_warn(priv, "Non-linear headers\n");
529 return 0;
532 } else {
533 *lso_header_size = 0;
534 if (!is_inline(skb, NULL))
535 real_size = CTRL_SIZE + (skb_shinfo(skb)->nr_frags + 1) * DS_SIZE;
536 else
537 real_size = inline_size(skb);
540 return real_size;
543 static void build_inline_wqe(struct mlx4_en_tx_desc *tx_desc, struct sk_buff *skb,
544 int real_size, u16 *vlan_tag, int tx_ind, void *fragptr)
546 struct mlx4_wqe_inline_seg *inl = &tx_desc->inl;
547 int spc = MLX4_INLINE_ALIGN - CTRL_SIZE - sizeof *inl;
549 if (skb->len <= spc) {
550 inl->byte_count = cpu_to_be32(1 << 31 | skb->len);
551 skb_copy_from_linear_data(skb, inl + 1, skb_headlen(skb));
552 if (skb_shinfo(skb)->nr_frags)
553 memcpy(((void *)(inl + 1)) + skb_headlen(skb), fragptr,
554 skb_shinfo(skb)->frags[0].size);
556 } else {
557 inl->byte_count = cpu_to_be32(1 << 31 | spc);
558 if (skb_headlen(skb) <= spc) {
559 skb_copy_from_linear_data(skb, inl + 1, skb_headlen(skb));
560 if (skb_headlen(skb) < spc) {
561 memcpy(((void *)(inl + 1)) + skb_headlen(skb),
562 fragptr, spc - skb_headlen(skb));
563 fragptr += spc - skb_headlen(skb);
565 inl = (void *) (inl + 1) + spc;
566 memcpy(((void *)(inl + 1)), fragptr, skb->len - spc);
567 } else {
568 skb_copy_from_linear_data(skb, inl + 1, spc);
569 inl = (void *) (inl + 1) + spc;
570 skb_copy_from_linear_data_offset(skb, spc, inl + 1,
571 skb_headlen(skb) - spc);
572 if (skb_shinfo(skb)->nr_frags)
573 memcpy(((void *)(inl + 1)) + skb_headlen(skb) - spc,
574 fragptr, skb_shinfo(skb)->frags[0].size);
577 wmb();
578 inl->byte_count = cpu_to_be32(1 << 31 | (skb->len - spc));
580 tx_desc->ctrl.vlan_tag = cpu_to_be16(*vlan_tag);
581 tx_desc->ctrl.ins_vlan = MLX4_WQE_CTRL_INS_VLAN * !!(*vlan_tag);
582 tx_desc->ctrl.fence_size = (real_size / 16) & 0x3f;
585 u16 mlx4_en_select_queue(struct net_device *dev, struct sk_buff *skb)
587 struct mlx4_en_priv *priv = netdev_priv(dev);
588 u16 vlan_tag = 0;
590 /* If we support per priority flow control and the packet contains
591 * a vlan tag, send the packet to the TX ring assigned to that priority
593 if (priv->prof->rx_ppp && vlan_tx_tag_present(skb)) {
594 vlan_tag = vlan_tx_tag_get(skb);
595 return MLX4_EN_NUM_TX_RINGS + (vlan_tag >> 13);
598 return skb_tx_hash(dev, skb);
601 static void mlx4_bf_copy(unsigned long *dst, unsigned long *src, unsigned bytecnt)
603 __iowrite64_copy(dst, src, bytecnt / 8);
606 netdev_tx_t mlx4_en_xmit(struct sk_buff *skb, struct net_device *dev)
608 struct mlx4_en_priv *priv = netdev_priv(dev);
609 struct mlx4_en_dev *mdev = priv->mdev;
610 struct mlx4_en_tx_ring *ring;
611 struct mlx4_en_cq *cq;
612 struct mlx4_en_tx_desc *tx_desc;
613 struct mlx4_wqe_data_seg *data;
614 struct skb_frag_struct *frag;
615 struct mlx4_en_tx_info *tx_info;
616 struct ethhdr *ethh;
617 u64 mac;
618 u32 mac_l, mac_h;
619 int tx_ind = 0;
620 int nr_txbb;
621 int desc_size;
622 int real_size;
623 dma_addr_t dma;
624 u32 index, bf_index;
625 __be32 op_own;
626 u16 vlan_tag = 0;
627 int i;
628 int lso_header_size;
629 void *fragptr;
630 bool bounce = false;
632 if (!priv->port_up)
633 goto tx_drop;
635 real_size = get_real_size(skb, dev, &lso_header_size);
636 if (unlikely(!real_size))
637 goto tx_drop;
639 /* Align descriptor to TXBB size */
640 desc_size = ALIGN(real_size, TXBB_SIZE);
641 nr_txbb = desc_size / TXBB_SIZE;
642 if (unlikely(nr_txbb > MAX_DESC_TXBBS)) {
643 if (netif_msg_tx_err(priv))
644 en_warn(priv, "Oversized header or SG list\n");
645 goto tx_drop;
648 tx_ind = skb->queue_mapping;
649 ring = &priv->tx_ring[tx_ind];
650 if (vlan_tx_tag_present(skb))
651 vlan_tag = vlan_tx_tag_get(skb);
653 /* Check available TXBBs And 2K spare for prefetch */
654 if (unlikely(((int)(ring->prod - ring->cons)) >
655 ring->size - HEADROOM - MAX_DESC_TXBBS)) {
656 /* every full Tx ring stops queue */
657 netif_tx_stop_queue(netdev_get_tx_queue(dev, tx_ind));
658 ring->blocked = 1;
659 priv->port_stats.queue_stopped++;
661 /* Use interrupts to find out when queue opened */
662 cq = &priv->tx_cq[tx_ind];
663 mlx4_en_arm_cq(priv, cq);
664 return NETDEV_TX_BUSY;
667 /* Track current inflight packets for performance analysis */
668 AVG_PERF_COUNTER(priv->pstats.inflight_avg,
669 (u32) (ring->prod - ring->cons - 1));
671 /* Packet is good - grab an index and transmit it */
672 index = ring->prod & ring->size_mask;
673 bf_index = ring->prod;
675 /* See if we have enough space for whole descriptor TXBB for setting
676 * SW ownership on next descriptor; if not, use a bounce buffer. */
677 if (likely(index + nr_txbb <= ring->size))
678 tx_desc = ring->buf + index * TXBB_SIZE;
679 else {
680 tx_desc = (struct mlx4_en_tx_desc *) ring->bounce_buf;
681 bounce = true;
684 /* Save skb in tx_info ring */
685 tx_info = &ring->tx_info[index];
686 tx_info->skb = skb;
687 tx_info->nr_txbb = nr_txbb;
689 /* Prepare ctrl segement apart opcode+ownership, which depends on
690 * whether LSO is used */
691 tx_desc->ctrl.vlan_tag = cpu_to_be16(vlan_tag);
692 tx_desc->ctrl.ins_vlan = MLX4_WQE_CTRL_INS_VLAN * !!vlan_tag;
693 tx_desc->ctrl.fence_size = (real_size / 16) & 0x3f;
694 tx_desc->ctrl.srcrb_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
695 MLX4_WQE_CTRL_SOLICITED);
696 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
697 tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
698 MLX4_WQE_CTRL_TCP_UDP_CSUM);
699 priv->port_stats.tx_chksum_offload++;
702 if (unlikely(priv->validate_loopback)) {
703 /* Copy dst mac address to wqe */
704 skb_reset_mac_header(skb);
705 ethh = eth_hdr(skb);
706 if (ethh && ethh->h_dest) {
707 mac = mlx4_en_mac_to_u64(ethh->h_dest);
708 mac_h = (u32) ((mac & 0xffff00000000ULL) >> 16);
709 mac_l = (u32) (mac & 0xffffffff);
710 tx_desc->ctrl.srcrb_flags |= cpu_to_be32(mac_h);
711 tx_desc->ctrl.imm = cpu_to_be32(mac_l);
715 /* Handle LSO (TSO) packets */
716 if (lso_header_size) {
717 /* Mark opcode as LSO */
718 op_own = cpu_to_be32(MLX4_OPCODE_LSO | (1 << 6)) |
719 ((ring->prod & ring->size) ?
720 cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
722 /* Fill in the LSO prefix */
723 tx_desc->lso.mss_hdr_size = cpu_to_be32(
724 skb_shinfo(skb)->gso_size << 16 | lso_header_size);
726 /* Copy headers;
727 * note that we already verified that it is linear */
728 memcpy(tx_desc->lso.header, skb->data, lso_header_size);
729 data = ((void *) &tx_desc->lso +
730 ALIGN(lso_header_size + 4, DS_SIZE));
732 priv->port_stats.tso_packets++;
733 i = ((skb->len - lso_header_size) / skb_shinfo(skb)->gso_size) +
734 !!((skb->len - lso_header_size) % skb_shinfo(skb)->gso_size);
735 ring->bytes += skb->len + (i - 1) * lso_header_size;
736 ring->packets += i;
737 } else {
738 /* Normal (Non LSO) packet */
739 op_own = cpu_to_be32(MLX4_OPCODE_SEND) |
740 ((ring->prod & ring->size) ?
741 cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
742 data = &tx_desc->data;
743 ring->bytes += max(skb->len, (unsigned int) ETH_ZLEN);
744 ring->packets++;
747 AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, skb->len);
750 /* valid only for none inline segments */
751 tx_info->data_offset = (void *) data - (void *) tx_desc;
753 tx_info->linear = (lso_header_size < skb_headlen(skb) && !is_inline(skb, NULL)) ? 1 : 0;
754 data += skb_shinfo(skb)->nr_frags + tx_info->linear - 1;
756 if (!is_inline(skb, &fragptr)) {
757 /* Map fragments */
758 for (i = skb_shinfo(skb)->nr_frags - 1; i >= 0; i--) {
759 frag = &skb_shinfo(skb)->frags[i];
760 dma = pci_map_page(mdev->dev->pdev, frag->page, frag->page_offset,
761 frag->size, PCI_DMA_TODEVICE);
762 data->addr = cpu_to_be64(dma);
763 data->lkey = cpu_to_be32(mdev->mr.key);
764 wmb();
765 data->byte_count = cpu_to_be32(frag->size);
766 --data;
769 /* Map linear part */
770 if (tx_info->linear) {
771 dma = pci_map_single(mdev->dev->pdev, skb->data + lso_header_size,
772 skb_headlen(skb) - lso_header_size, PCI_DMA_TODEVICE);
773 data->addr = cpu_to_be64(dma);
774 data->lkey = cpu_to_be32(mdev->mr.key);
775 wmb();
776 data->byte_count = cpu_to_be32(skb_headlen(skb) - lso_header_size);
778 tx_info->inl = 0;
779 } else {
780 build_inline_wqe(tx_desc, skb, real_size, &vlan_tag, tx_ind, fragptr);
781 tx_info->inl = 1;
784 ring->prod += nr_txbb;
786 /* If we used a bounce buffer then copy descriptor back into place */
787 if (bounce)
788 tx_desc = mlx4_en_bounce_to_desc(priv, ring, index, desc_size);
790 /* Run destructor before passing skb to HW */
791 if (likely(!skb_shared(skb)))
792 skb_orphan(skb);
794 if (ring->bf_enabled && desc_size <= MAX_BF && !bounce && !vlan_tag) {
795 *(u32 *) (&tx_desc->ctrl.vlan_tag) |= ring->doorbell_qpn;
796 op_own |= htonl((bf_index & 0xffff) << 8);
797 /* Ensure new descirptor hits memory
798 * before setting ownership of this descriptor to HW */
799 wmb();
800 tx_desc->ctrl.owner_opcode = op_own;
802 wmb();
804 mlx4_bf_copy(ring->bf.reg + ring->bf.offset, (unsigned long *) &tx_desc->ctrl,
805 desc_size);
807 wmb();
809 ring->bf.offset ^= ring->bf.buf_size;
810 } else {
811 /* Ensure new descirptor hits memory
812 * before setting ownership of this descriptor to HW */
813 wmb();
814 tx_desc->ctrl.owner_opcode = op_own;
815 wmb();
816 writel(ring->doorbell_qpn, ring->bf.uar->map + MLX4_SEND_DOORBELL);
819 /* Poll CQ here */
820 mlx4_en_xmit_poll(priv, tx_ind);
822 return NETDEV_TX_OK;
824 tx_drop:
825 dev_kfree_skb_any(skb);
826 priv->stats.tx_dropped++;
827 return NETDEV_TX_OK;