xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/mellanox/mlx4/en_tx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This software is available to you under a choice of one of two
5*4882a593Smuzhiyun  * licenses.  You may choose to be licensed under the terms of the GNU
6*4882a593Smuzhiyun  * General Public License (GPL) Version 2, available from the file
7*4882a593Smuzhiyun  * COPYING in the main directory of this source tree, or the
8*4882a593Smuzhiyun  * OpenIB.org BSD license below:
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *     Redistribution and use in source and binary forms, with or
11*4882a593Smuzhiyun  *     without modification, are permitted provided that the following
12*4882a593Smuzhiyun  *     conditions are met:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *      - Redistributions of source code must retain the above
15*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
16*4882a593Smuzhiyun  *        disclaimer.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  *      - Redistributions in binary form must reproduce the above
19*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
20*4882a593Smuzhiyun  *        disclaimer in the documentation and/or other materials
21*4882a593Smuzhiyun  *        provided with the distribution.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*4882a593Smuzhiyun  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*4882a593Smuzhiyun  * SOFTWARE.
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #include <asm/page.h>
35*4882a593Smuzhiyun #include <linux/mlx4/cq.h>
36*4882a593Smuzhiyun #include <linux/slab.h>
37*4882a593Smuzhiyun #include <linux/mlx4/qp.h>
38*4882a593Smuzhiyun #include <linux/skbuff.h>
39*4882a593Smuzhiyun #include <linux/if_vlan.h>
40*4882a593Smuzhiyun #include <linux/prefetch.h>
41*4882a593Smuzhiyun #include <linux/vmalloc.h>
42*4882a593Smuzhiyun #include <linux/tcp.h>
43*4882a593Smuzhiyun #include <linux/ip.h>
44*4882a593Smuzhiyun #include <linux/ipv6.h>
45*4882a593Smuzhiyun #include <linux/moduleparam.h>
46*4882a593Smuzhiyun #include <linux/indirect_call_wrapper.h>
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include "mlx4_en.h"
49*4882a593Smuzhiyun 
mlx4_en_create_tx_ring(struct mlx4_en_priv * priv,struct mlx4_en_tx_ring ** pring,u32 size,u16 stride,int node,int queue_index)50*4882a593Smuzhiyun int mlx4_en_create_tx_ring(struct mlx4_en_priv *priv,
51*4882a593Smuzhiyun 			   struct mlx4_en_tx_ring **pring, u32 size,
52*4882a593Smuzhiyun 			   u16 stride, int node, int queue_index)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	struct mlx4_en_dev *mdev = priv->mdev;
55*4882a593Smuzhiyun 	struct mlx4_en_tx_ring *ring;
56*4882a593Smuzhiyun 	int tmp;
57*4882a593Smuzhiyun 	int err;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, node);
60*4882a593Smuzhiyun 	if (!ring) {
61*4882a593Smuzhiyun 		en_err(priv, "Failed allocating TX ring\n");
62*4882a593Smuzhiyun 		return -ENOMEM;
63*4882a593Smuzhiyun 	}
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	ring->size = size;
66*4882a593Smuzhiyun 	ring->size_mask = size - 1;
67*4882a593Smuzhiyun 	ring->sp_stride = stride;
68*4882a593Smuzhiyun 	ring->full_size = ring->size - HEADROOM - MAX_DESC_TXBBS;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	tmp = size * sizeof(struct mlx4_en_tx_info);
71*4882a593Smuzhiyun 	ring->tx_info = kvmalloc_node(tmp, GFP_KERNEL, node);
72*4882a593Smuzhiyun 	if (!ring->tx_info) {
73*4882a593Smuzhiyun 		err = -ENOMEM;
74*4882a593Smuzhiyun 		goto err_ring;
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	en_dbg(DRV, priv, "Allocated tx_info ring at addr:%p size:%d\n",
78*4882a593Smuzhiyun 		 ring->tx_info, tmp);
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	ring->bounce_buf = kmalloc_node(MAX_DESC_SIZE, GFP_KERNEL, node);
81*4882a593Smuzhiyun 	if (!ring->bounce_buf) {
82*4882a593Smuzhiyun 		ring->bounce_buf = kmalloc(MAX_DESC_SIZE, GFP_KERNEL);
83*4882a593Smuzhiyun 		if (!ring->bounce_buf) {
84*4882a593Smuzhiyun 			err = -ENOMEM;
85*4882a593Smuzhiyun 			goto err_info;
86*4882a593Smuzhiyun 		}
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 	ring->buf_size = ALIGN(size * ring->sp_stride, MLX4_EN_PAGE_SIZE);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	/* Allocate HW buffers on provided NUMA node */
91*4882a593Smuzhiyun 	set_dev_node(&mdev->dev->persist->pdev->dev, node);
92*4882a593Smuzhiyun 	err = mlx4_alloc_hwq_res(mdev->dev, &ring->sp_wqres, ring->buf_size);
93*4882a593Smuzhiyun 	set_dev_node(&mdev->dev->persist->pdev->dev, mdev->dev->numa_node);
94*4882a593Smuzhiyun 	if (err) {
95*4882a593Smuzhiyun 		en_err(priv, "Failed allocating hwq resources\n");
96*4882a593Smuzhiyun 		goto err_bounce;
97*4882a593Smuzhiyun 	}
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	ring->buf = ring->sp_wqres.buf.direct.buf;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	en_dbg(DRV, priv, "Allocated TX ring (addr:%p) - buf:%p size:%d buf_size:%d dma:%llx\n",
102*4882a593Smuzhiyun 	       ring, ring->buf, ring->size, ring->buf_size,
103*4882a593Smuzhiyun 	       (unsigned long long) ring->sp_wqres.buf.direct.map);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	err = mlx4_qp_reserve_range(mdev->dev, 1, 1, &ring->qpn,
106*4882a593Smuzhiyun 				    MLX4_RESERVE_ETH_BF_QP,
107*4882a593Smuzhiyun 				    MLX4_RES_USAGE_DRIVER);
108*4882a593Smuzhiyun 	if (err) {
109*4882a593Smuzhiyun 		en_err(priv, "failed reserving qp for TX ring\n");
110*4882a593Smuzhiyun 		goto err_hwq_res;
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	err = mlx4_qp_alloc(mdev->dev, ring->qpn, &ring->sp_qp);
114*4882a593Smuzhiyun 	if (err) {
115*4882a593Smuzhiyun 		en_err(priv, "Failed allocating qp %d\n", ring->qpn);
116*4882a593Smuzhiyun 		goto err_reserve;
117*4882a593Smuzhiyun 	}
118*4882a593Smuzhiyun 	ring->sp_qp.event = mlx4_en_sqp_event;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	err = mlx4_bf_alloc(mdev->dev, &ring->bf, node);
121*4882a593Smuzhiyun 	if (err) {
122*4882a593Smuzhiyun 		en_dbg(DRV, priv, "working without blueflame (%d)\n", err);
123*4882a593Smuzhiyun 		ring->bf.uar = &mdev->priv_uar;
124*4882a593Smuzhiyun 		ring->bf.uar->map = mdev->uar_map;
125*4882a593Smuzhiyun 		ring->bf_enabled = false;
126*4882a593Smuzhiyun 		ring->bf_alloced = false;
127*4882a593Smuzhiyun 		priv->pflags &= ~MLX4_EN_PRIV_FLAGS_BLUEFLAME;
128*4882a593Smuzhiyun 	} else {
129*4882a593Smuzhiyun 		ring->bf_alloced = true;
130*4882a593Smuzhiyun 		ring->bf_enabled = !!(priv->pflags &
131*4882a593Smuzhiyun 				      MLX4_EN_PRIV_FLAGS_BLUEFLAME);
132*4882a593Smuzhiyun 	}
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	ring->hwtstamp_tx_type = priv->hwtstamp_config.tx_type;
135*4882a593Smuzhiyun 	ring->queue_index = queue_index;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	if (queue_index < priv->num_tx_rings_p_up)
138*4882a593Smuzhiyun 		cpumask_set_cpu(cpumask_local_spread(queue_index,
139*4882a593Smuzhiyun 						     priv->mdev->dev->numa_node),
140*4882a593Smuzhiyun 				&ring->sp_affinity_mask);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	*pring = ring;
143*4882a593Smuzhiyun 	return 0;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun err_reserve:
146*4882a593Smuzhiyun 	mlx4_qp_release_range(mdev->dev, ring->qpn, 1);
147*4882a593Smuzhiyun err_hwq_res:
148*4882a593Smuzhiyun 	mlx4_free_hwq_res(mdev->dev, &ring->sp_wqres, ring->buf_size);
149*4882a593Smuzhiyun err_bounce:
150*4882a593Smuzhiyun 	kfree(ring->bounce_buf);
151*4882a593Smuzhiyun 	ring->bounce_buf = NULL;
152*4882a593Smuzhiyun err_info:
153*4882a593Smuzhiyun 	kvfree(ring->tx_info);
154*4882a593Smuzhiyun 	ring->tx_info = NULL;
155*4882a593Smuzhiyun err_ring:
156*4882a593Smuzhiyun 	kfree(ring);
157*4882a593Smuzhiyun 	*pring = NULL;
158*4882a593Smuzhiyun 	return err;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
mlx4_en_destroy_tx_ring(struct mlx4_en_priv * priv,struct mlx4_en_tx_ring ** pring)161*4882a593Smuzhiyun void mlx4_en_destroy_tx_ring(struct mlx4_en_priv *priv,
162*4882a593Smuzhiyun 			     struct mlx4_en_tx_ring **pring)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	struct mlx4_en_dev *mdev = priv->mdev;
165*4882a593Smuzhiyun 	struct mlx4_en_tx_ring *ring = *pring;
166*4882a593Smuzhiyun 	en_dbg(DRV, priv, "Destroying tx ring, qpn: %d\n", ring->qpn);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	if (ring->bf_alloced)
169*4882a593Smuzhiyun 		mlx4_bf_free(mdev->dev, &ring->bf);
170*4882a593Smuzhiyun 	mlx4_qp_remove(mdev->dev, &ring->sp_qp);
171*4882a593Smuzhiyun 	mlx4_qp_free(mdev->dev, &ring->sp_qp);
172*4882a593Smuzhiyun 	mlx4_qp_release_range(priv->mdev->dev, ring->qpn, 1);
173*4882a593Smuzhiyun 	mlx4_free_hwq_res(mdev->dev, &ring->sp_wqres, ring->buf_size);
174*4882a593Smuzhiyun 	kfree(ring->bounce_buf);
175*4882a593Smuzhiyun 	ring->bounce_buf = NULL;
176*4882a593Smuzhiyun 	kvfree(ring->tx_info);
177*4882a593Smuzhiyun 	ring->tx_info = NULL;
178*4882a593Smuzhiyun 	kfree(ring);
179*4882a593Smuzhiyun 	*pring = NULL;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
mlx4_en_activate_tx_ring(struct mlx4_en_priv * priv,struct mlx4_en_tx_ring * ring,int cq,int user_prio)182*4882a593Smuzhiyun int mlx4_en_activate_tx_ring(struct mlx4_en_priv *priv,
183*4882a593Smuzhiyun 			     struct mlx4_en_tx_ring *ring,
184*4882a593Smuzhiyun 			     int cq, int user_prio)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	struct mlx4_en_dev *mdev = priv->mdev;
187*4882a593Smuzhiyun 	int err;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	ring->sp_cqn = cq;
190*4882a593Smuzhiyun 	ring->prod = 0;
191*4882a593Smuzhiyun 	ring->cons = 0xffffffff;
192*4882a593Smuzhiyun 	ring->last_nr_txbb = 1;
193*4882a593Smuzhiyun 	memset(ring->tx_info, 0, ring->size * sizeof(struct mlx4_en_tx_info));
194*4882a593Smuzhiyun 	memset(ring->buf, 0, ring->buf_size);
195*4882a593Smuzhiyun 	ring->free_tx_desc = mlx4_en_free_tx_desc;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	ring->sp_qp_state = MLX4_QP_STATE_RST;
198*4882a593Smuzhiyun 	ring->doorbell_qpn = cpu_to_be32(ring->sp_qp.qpn << 8);
199*4882a593Smuzhiyun 	ring->mr_key = cpu_to_be32(mdev->mr.key);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	mlx4_en_fill_qp_context(priv, ring->size, ring->sp_stride, 1, 0, ring->qpn,
202*4882a593Smuzhiyun 				ring->sp_cqn, user_prio, &ring->sp_context);
203*4882a593Smuzhiyun 	if (ring->bf_alloced)
204*4882a593Smuzhiyun 		ring->sp_context.usr_page =
205*4882a593Smuzhiyun 			cpu_to_be32(mlx4_to_hw_uar_index(mdev->dev,
206*4882a593Smuzhiyun 							 ring->bf.uar->index));
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	err = mlx4_qp_to_ready(mdev->dev, &ring->sp_wqres.mtt, &ring->sp_context,
209*4882a593Smuzhiyun 			       &ring->sp_qp, &ring->sp_qp_state);
210*4882a593Smuzhiyun 	if (!cpumask_empty(&ring->sp_affinity_mask))
211*4882a593Smuzhiyun 		netif_set_xps_queue(priv->dev, &ring->sp_affinity_mask,
212*4882a593Smuzhiyun 				    ring->queue_index);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	return err;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
mlx4_en_deactivate_tx_ring(struct mlx4_en_priv * priv,struct mlx4_en_tx_ring * ring)217*4882a593Smuzhiyun void mlx4_en_deactivate_tx_ring(struct mlx4_en_priv *priv,
218*4882a593Smuzhiyun 				struct mlx4_en_tx_ring *ring)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	struct mlx4_en_dev *mdev = priv->mdev;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	mlx4_qp_modify(mdev->dev, NULL, ring->sp_qp_state,
223*4882a593Smuzhiyun 		       MLX4_QP_STATE_RST, NULL, 0, 0, &ring->sp_qp);
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
mlx4_en_is_tx_ring_full(struct mlx4_en_tx_ring * ring)226*4882a593Smuzhiyun static inline bool mlx4_en_is_tx_ring_full(struct mlx4_en_tx_ring *ring)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	return ring->prod - ring->cons > ring->full_size;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun 
mlx4_en_stamp_wqe(struct mlx4_en_priv * priv,struct mlx4_en_tx_ring * ring,int index,u8 owner)231*4882a593Smuzhiyun static void mlx4_en_stamp_wqe(struct mlx4_en_priv *priv,
232*4882a593Smuzhiyun 			      struct mlx4_en_tx_ring *ring, int index,
233*4882a593Smuzhiyun 			      u8 owner)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun 	__be32 stamp = cpu_to_be32(STAMP_VAL | (!!owner << STAMP_SHIFT));
236*4882a593Smuzhiyun 	struct mlx4_en_tx_desc *tx_desc = ring->buf + (index << LOG_TXBB_SIZE);
237*4882a593Smuzhiyun 	struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
238*4882a593Smuzhiyun 	void *end = ring->buf + ring->buf_size;
239*4882a593Smuzhiyun 	__be32 *ptr = (__be32 *)tx_desc;
240*4882a593Smuzhiyun 	int i;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	/* Optimize the common case when there are no wraparounds */
243*4882a593Smuzhiyun 	if (likely((void *)tx_desc +
244*4882a593Smuzhiyun 		   (tx_info->nr_txbb << LOG_TXBB_SIZE) <= end)) {
245*4882a593Smuzhiyun 		/* Stamp the freed descriptor */
246*4882a593Smuzhiyun 		for (i = 0; i < tx_info->nr_txbb << LOG_TXBB_SIZE;
247*4882a593Smuzhiyun 		     i += STAMP_STRIDE) {
248*4882a593Smuzhiyun 			*ptr = stamp;
249*4882a593Smuzhiyun 			ptr += STAMP_DWORDS;
250*4882a593Smuzhiyun 		}
251*4882a593Smuzhiyun 	} else {
252*4882a593Smuzhiyun 		/* Stamp the freed descriptor */
253*4882a593Smuzhiyun 		for (i = 0; i < tx_info->nr_txbb << LOG_TXBB_SIZE;
254*4882a593Smuzhiyun 		     i += STAMP_STRIDE) {
255*4882a593Smuzhiyun 			*ptr = stamp;
256*4882a593Smuzhiyun 			ptr += STAMP_DWORDS;
257*4882a593Smuzhiyun 			if ((void *)ptr >= end) {
258*4882a593Smuzhiyun 				ptr = ring->buf;
259*4882a593Smuzhiyun 				stamp ^= cpu_to_be32(0x80000000);
260*4882a593Smuzhiyun 			}
261*4882a593Smuzhiyun 		}
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun INDIRECT_CALLABLE_DECLARE(u32 mlx4_en_free_tx_desc(struct mlx4_en_priv *priv,
266*4882a593Smuzhiyun 						   struct mlx4_en_tx_ring *ring,
267*4882a593Smuzhiyun 						   int index, u64 timestamp,
268*4882a593Smuzhiyun 						   int napi_mode));
269*4882a593Smuzhiyun 
mlx4_en_free_tx_desc(struct mlx4_en_priv * priv,struct mlx4_en_tx_ring * ring,int index,u64 timestamp,int napi_mode)270*4882a593Smuzhiyun u32 mlx4_en_free_tx_desc(struct mlx4_en_priv *priv,
271*4882a593Smuzhiyun 			 struct mlx4_en_tx_ring *ring,
272*4882a593Smuzhiyun 			 int index, u64 timestamp,
273*4882a593Smuzhiyun 			 int napi_mode)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun 	struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
276*4882a593Smuzhiyun 	struct mlx4_en_tx_desc *tx_desc = ring->buf + (index << LOG_TXBB_SIZE);
277*4882a593Smuzhiyun 	struct mlx4_wqe_data_seg *data = (void *) tx_desc + tx_info->data_offset;
278*4882a593Smuzhiyun 	void *end = ring->buf + ring->buf_size;
279*4882a593Smuzhiyun 	struct sk_buff *skb = tx_info->skb;
280*4882a593Smuzhiyun 	int nr_maps = tx_info->nr_maps;
281*4882a593Smuzhiyun 	int i;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	/* We do not touch skb here, so prefetch skb->users location
284*4882a593Smuzhiyun 	 * to speedup consume_skb()
285*4882a593Smuzhiyun 	 */
286*4882a593Smuzhiyun 	prefetchw(&skb->users);
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	if (unlikely(timestamp)) {
289*4882a593Smuzhiyun 		struct skb_shared_hwtstamps hwts;
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 		mlx4_en_fill_hwtstamps(priv->mdev, &hwts, timestamp);
292*4882a593Smuzhiyun 		skb_tstamp_tx(skb, &hwts);
293*4882a593Smuzhiyun 	}
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	if (!tx_info->inl) {
296*4882a593Smuzhiyun 		if (tx_info->linear)
297*4882a593Smuzhiyun 			dma_unmap_single(priv->ddev,
298*4882a593Smuzhiyun 					 tx_info->map0_dma,
299*4882a593Smuzhiyun 					 tx_info->map0_byte_count,
300*4882a593Smuzhiyun 					 PCI_DMA_TODEVICE);
301*4882a593Smuzhiyun 		else
302*4882a593Smuzhiyun 			dma_unmap_page(priv->ddev,
303*4882a593Smuzhiyun 				       tx_info->map0_dma,
304*4882a593Smuzhiyun 				       tx_info->map0_byte_count,
305*4882a593Smuzhiyun 				       PCI_DMA_TODEVICE);
306*4882a593Smuzhiyun 		/* Optimize the common case when there are no wraparounds */
307*4882a593Smuzhiyun 		if (likely((void *)tx_desc +
308*4882a593Smuzhiyun 			   (tx_info->nr_txbb << LOG_TXBB_SIZE) <= end)) {
309*4882a593Smuzhiyun 			for (i = 1; i < nr_maps; i++) {
310*4882a593Smuzhiyun 				data++;
311*4882a593Smuzhiyun 				dma_unmap_page(priv->ddev,
312*4882a593Smuzhiyun 					(dma_addr_t)be64_to_cpu(data->addr),
313*4882a593Smuzhiyun 					be32_to_cpu(data->byte_count),
314*4882a593Smuzhiyun 					PCI_DMA_TODEVICE);
315*4882a593Smuzhiyun 			}
316*4882a593Smuzhiyun 		} else {
317*4882a593Smuzhiyun 			if ((void *)data >= end)
318*4882a593Smuzhiyun 				data = ring->buf + ((void *)data - end);
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 			for (i = 1; i < nr_maps; i++) {
321*4882a593Smuzhiyun 				data++;
322*4882a593Smuzhiyun 				/* Check for wraparound before unmapping */
323*4882a593Smuzhiyun 				if ((void *) data >= end)
324*4882a593Smuzhiyun 					data = ring->buf;
325*4882a593Smuzhiyun 				dma_unmap_page(priv->ddev,
326*4882a593Smuzhiyun 					(dma_addr_t)be64_to_cpu(data->addr),
327*4882a593Smuzhiyun 					be32_to_cpu(data->byte_count),
328*4882a593Smuzhiyun 					PCI_DMA_TODEVICE);
329*4882a593Smuzhiyun 			}
330*4882a593Smuzhiyun 		}
331*4882a593Smuzhiyun 	}
332*4882a593Smuzhiyun 	napi_consume_skb(skb, napi_mode);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	return tx_info->nr_txbb;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun INDIRECT_CALLABLE_DECLARE(u32 mlx4_en_recycle_tx_desc(struct mlx4_en_priv *priv,
338*4882a593Smuzhiyun 						      struct mlx4_en_tx_ring *ring,
339*4882a593Smuzhiyun 						      int index, u64 timestamp,
340*4882a593Smuzhiyun 						      int napi_mode));
341*4882a593Smuzhiyun 
mlx4_en_recycle_tx_desc(struct mlx4_en_priv * priv,struct mlx4_en_tx_ring * ring,int index,u64 timestamp,int napi_mode)342*4882a593Smuzhiyun u32 mlx4_en_recycle_tx_desc(struct mlx4_en_priv *priv,
343*4882a593Smuzhiyun 			    struct mlx4_en_tx_ring *ring,
344*4882a593Smuzhiyun 			    int index, u64 timestamp,
345*4882a593Smuzhiyun 			    int napi_mode)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	struct mlx4_en_tx_info *tx_info = &ring->tx_info[index];
348*4882a593Smuzhiyun 	struct mlx4_en_rx_alloc frame = {
349*4882a593Smuzhiyun 		.page = tx_info->page,
350*4882a593Smuzhiyun 		.dma = tx_info->map0_dma,
351*4882a593Smuzhiyun 	};
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	if (!napi_mode || !mlx4_en_rx_recycle(ring->recycle_ring, &frame)) {
354*4882a593Smuzhiyun 		dma_unmap_page(priv->ddev, tx_info->map0_dma,
355*4882a593Smuzhiyun 			       PAGE_SIZE, priv->dma_dir);
356*4882a593Smuzhiyun 		put_page(tx_info->page);
357*4882a593Smuzhiyun 	}
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	return tx_info->nr_txbb;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun 
mlx4_en_free_tx_buf(struct net_device * dev,struct mlx4_en_tx_ring * ring)362*4882a593Smuzhiyun int mlx4_en_free_tx_buf(struct net_device *dev, struct mlx4_en_tx_ring *ring)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	struct mlx4_en_priv *priv = netdev_priv(dev);
365*4882a593Smuzhiyun 	int cnt = 0;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	/* Skip last polled descriptor */
368*4882a593Smuzhiyun 	ring->cons += ring->last_nr_txbb;
369*4882a593Smuzhiyun 	en_dbg(DRV, priv, "Freeing Tx buf - cons:0x%x prod:0x%x\n",
370*4882a593Smuzhiyun 		 ring->cons, ring->prod);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	if ((u32) (ring->prod - ring->cons) > ring->size) {
373*4882a593Smuzhiyun 		if (netif_msg_tx_err(priv))
374*4882a593Smuzhiyun 			en_warn(priv, "Tx consumer passed producer!\n");
375*4882a593Smuzhiyun 		return 0;
376*4882a593Smuzhiyun 	}
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	while (ring->cons != ring->prod) {
379*4882a593Smuzhiyun 		ring->last_nr_txbb = ring->free_tx_desc(priv, ring,
380*4882a593Smuzhiyun 						ring->cons & ring->size_mask,
381*4882a593Smuzhiyun 						0, 0 /* Non-NAPI caller */);
382*4882a593Smuzhiyun 		ring->cons += ring->last_nr_txbb;
383*4882a593Smuzhiyun 		cnt++;
384*4882a593Smuzhiyun 	}
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	if (ring->tx_queue)
387*4882a593Smuzhiyun 		netdev_tx_reset_queue(ring->tx_queue);
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	if (cnt)
390*4882a593Smuzhiyun 		en_dbg(DRV, priv, "Freed %d uncompleted tx descriptors\n", cnt);
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	return cnt;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun 
mlx4_en_handle_err_cqe(struct mlx4_en_priv * priv,struct mlx4_err_cqe * err_cqe,u16 cqe_index,struct mlx4_en_tx_ring * ring)395*4882a593Smuzhiyun static void mlx4_en_handle_err_cqe(struct mlx4_en_priv *priv, struct mlx4_err_cqe *err_cqe,
396*4882a593Smuzhiyun 				   u16 cqe_index, struct mlx4_en_tx_ring *ring)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun 	struct mlx4_en_dev *mdev = priv->mdev;
399*4882a593Smuzhiyun 	struct mlx4_en_tx_info *tx_info;
400*4882a593Smuzhiyun 	struct mlx4_en_tx_desc *tx_desc;
401*4882a593Smuzhiyun 	u16 wqe_index;
402*4882a593Smuzhiyun 	int desc_size;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	en_err(priv, "CQE error - cqn 0x%x, ci 0x%x, vendor syndrome: 0x%x syndrome: 0x%x\n",
405*4882a593Smuzhiyun 	       ring->sp_cqn, cqe_index, err_cqe->vendor_err_syndrome, err_cqe->syndrome);
406*4882a593Smuzhiyun 	print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET, 16, 1, err_cqe, sizeof(*err_cqe),
407*4882a593Smuzhiyun 		       false);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	wqe_index = be16_to_cpu(err_cqe->wqe_index) & ring->size_mask;
410*4882a593Smuzhiyun 	tx_info = &ring->tx_info[wqe_index];
411*4882a593Smuzhiyun 	desc_size = tx_info->nr_txbb << LOG_TXBB_SIZE;
412*4882a593Smuzhiyun 	en_err(priv, "Related WQE - qpn 0x%x, wqe index 0x%x, wqe size 0x%x\n", ring->qpn,
413*4882a593Smuzhiyun 	       wqe_index, desc_size);
414*4882a593Smuzhiyun 	tx_desc = ring->buf + (wqe_index << LOG_TXBB_SIZE);
415*4882a593Smuzhiyun 	print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET, 16, 1, tx_desc, desc_size, false);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	if (test_and_set_bit(MLX4_EN_STATE_FLAG_RESTARTING, &priv->state))
418*4882a593Smuzhiyun 		return;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	en_err(priv, "Scheduling port restart\n");
421*4882a593Smuzhiyun 	queue_work(mdev->workqueue, &priv->restart_task);
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun 
mlx4_en_process_tx_cq(struct net_device * dev,struct mlx4_en_cq * cq,int napi_budget)424*4882a593Smuzhiyun int mlx4_en_process_tx_cq(struct net_device *dev,
425*4882a593Smuzhiyun 			  struct mlx4_en_cq *cq, int napi_budget)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	struct mlx4_en_priv *priv = netdev_priv(dev);
428*4882a593Smuzhiyun 	struct mlx4_cq *mcq = &cq->mcq;
429*4882a593Smuzhiyun 	struct mlx4_en_tx_ring *ring = priv->tx_ring[cq->type][cq->ring];
430*4882a593Smuzhiyun 	struct mlx4_cqe *cqe;
431*4882a593Smuzhiyun 	u16 index, ring_index, stamp_index;
432*4882a593Smuzhiyun 	u32 txbbs_skipped = 0;
433*4882a593Smuzhiyun 	u32 txbbs_stamp = 0;
434*4882a593Smuzhiyun 	u32 cons_index = mcq->cons_index;
435*4882a593Smuzhiyun 	int size = cq->size;
436*4882a593Smuzhiyun 	u32 size_mask = ring->size_mask;
437*4882a593Smuzhiyun 	struct mlx4_cqe *buf = cq->buf;
438*4882a593Smuzhiyun 	u32 packets = 0;
439*4882a593Smuzhiyun 	u32 bytes = 0;
440*4882a593Smuzhiyun 	int factor = priv->cqe_factor;
441*4882a593Smuzhiyun 	int done = 0;
442*4882a593Smuzhiyun 	int budget = priv->tx_work_limit;
443*4882a593Smuzhiyun 	u32 last_nr_txbb;
444*4882a593Smuzhiyun 	u32 ring_cons;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	if (unlikely(!priv->port_up))
447*4882a593Smuzhiyun 		return 0;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	netdev_txq_bql_complete_prefetchw(ring->tx_queue);
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	index = cons_index & size_mask;
452*4882a593Smuzhiyun 	cqe = mlx4_en_get_cqe(buf, index, priv->cqe_size) + factor;
453*4882a593Smuzhiyun 	last_nr_txbb = READ_ONCE(ring->last_nr_txbb);
454*4882a593Smuzhiyun 	ring_cons = READ_ONCE(ring->cons);
455*4882a593Smuzhiyun 	ring_index = ring_cons & size_mask;
456*4882a593Smuzhiyun 	stamp_index = ring_index;
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	/* Process all completed CQEs */
459*4882a593Smuzhiyun 	while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
460*4882a593Smuzhiyun 			cons_index & size) && (done < budget)) {
461*4882a593Smuzhiyun 		u16 new_index;
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 		/*
464*4882a593Smuzhiyun 		 * make sure we read the CQE after we read the
465*4882a593Smuzhiyun 		 * ownership bit
466*4882a593Smuzhiyun 		 */
467*4882a593Smuzhiyun 		dma_rmb();
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 		if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
470*4882a593Smuzhiyun 			     MLX4_CQE_OPCODE_ERROR))
471*4882a593Smuzhiyun 			if (!test_and_set_bit(MLX4_EN_TX_RING_STATE_RECOVERING, &ring->state))
472*4882a593Smuzhiyun 				mlx4_en_handle_err_cqe(priv, (struct mlx4_err_cqe *)cqe, index,
473*4882a593Smuzhiyun 						       ring);
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 		/* Skip over last polled CQE */
476*4882a593Smuzhiyun 		new_index = be16_to_cpu(cqe->wqe_index) & size_mask;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 		do {
479*4882a593Smuzhiyun 			u64 timestamp = 0;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 			txbbs_skipped += last_nr_txbb;
482*4882a593Smuzhiyun 			ring_index = (ring_index + last_nr_txbb) & size_mask;
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 			if (unlikely(ring->tx_info[ring_index].ts_requested))
485*4882a593Smuzhiyun 				timestamp = mlx4_en_get_cqe_ts(cqe);
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 			/* free next descriptor */
488*4882a593Smuzhiyun 			last_nr_txbb = INDIRECT_CALL_2(ring->free_tx_desc,
489*4882a593Smuzhiyun 						       mlx4_en_free_tx_desc,
490*4882a593Smuzhiyun 						       mlx4_en_recycle_tx_desc,
491*4882a593Smuzhiyun 					priv, ring, ring_index,
492*4882a593Smuzhiyun 					timestamp, napi_budget);
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 			mlx4_en_stamp_wqe(priv, ring, stamp_index,
495*4882a593Smuzhiyun 					  !!((ring_cons + txbbs_stamp) &
496*4882a593Smuzhiyun 						ring->size));
497*4882a593Smuzhiyun 			stamp_index = ring_index;
498*4882a593Smuzhiyun 			txbbs_stamp = txbbs_skipped;
499*4882a593Smuzhiyun 			packets++;
500*4882a593Smuzhiyun 			bytes += ring->tx_info[ring_index].nr_bytes;
501*4882a593Smuzhiyun 		} while ((++done < budget) && (ring_index != new_index));
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 		++cons_index;
504*4882a593Smuzhiyun 		index = cons_index & size_mask;
505*4882a593Smuzhiyun 		cqe = mlx4_en_get_cqe(buf, index, priv->cqe_size) + factor;
506*4882a593Smuzhiyun 	}
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	/*
509*4882a593Smuzhiyun 	 * To prevent CQ overflow we first update CQ consumer and only then
510*4882a593Smuzhiyun 	 * the ring consumer.
511*4882a593Smuzhiyun 	 */
512*4882a593Smuzhiyun 	mcq->cons_index = cons_index;
513*4882a593Smuzhiyun 	mlx4_cq_set_ci(mcq);
514*4882a593Smuzhiyun 	wmb();
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	/* we want to dirty this cache line once */
517*4882a593Smuzhiyun 	WRITE_ONCE(ring->last_nr_txbb, last_nr_txbb);
518*4882a593Smuzhiyun 	WRITE_ONCE(ring->cons, ring_cons + txbbs_skipped);
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	if (cq->type == TX_XDP)
521*4882a593Smuzhiyun 		return done;
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	netdev_tx_completed_queue(ring->tx_queue, packets, bytes);
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	/* Wakeup Tx queue if this stopped, and ring is not full.
526*4882a593Smuzhiyun 	 */
527*4882a593Smuzhiyun 	if (netif_tx_queue_stopped(ring->tx_queue) &&
528*4882a593Smuzhiyun 	    !mlx4_en_is_tx_ring_full(ring)) {
529*4882a593Smuzhiyun 		netif_tx_wake_queue(ring->tx_queue);
530*4882a593Smuzhiyun 		ring->wake_queue++;
531*4882a593Smuzhiyun 	}
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	return done;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun 
mlx4_en_tx_irq(struct mlx4_cq * mcq)536*4882a593Smuzhiyun void mlx4_en_tx_irq(struct mlx4_cq *mcq)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun 	struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
539*4882a593Smuzhiyun 	struct mlx4_en_priv *priv = netdev_priv(cq->dev);
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	if (likely(priv->port_up))
542*4882a593Smuzhiyun 		napi_schedule_irqoff(&cq->napi);
543*4882a593Smuzhiyun 	else
544*4882a593Smuzhiyun 		mlx4_en_arm_cq(priv, cq);
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun /* TX CQ polling - called by NAPI */
mlx4_en_poll_tx_cq(struct napi_struct * napi,int budget)548*4882a593Smuzhiyun int mlx4_en_poll_tx_cq(struct napi_struct *napi, int budget)
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun 	struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
551*4882a593Smuzhiyun 	struct net_device *dev = cq->dev;
552*4882a593Smuzhiyun 	struct mlx4_en_priv *priv = netdev_priv(dev);
553*4882a593Smuzhiyun 	int work_done;
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 	work_done = mlx4_en_process_tx_cq(dev, cq, budget);
556*4882a593Smuzhiyun 	if (work_done >= budget)
557*4882a593Smuzhiyun 		return budget;
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	if (napi_complete_done(napi, work_done))
560*4882a593Smuzhiyun 		mlx4_en_arm_cq(priv, cq);
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	return 0;
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun 
mlx4_en_bounce_to_desc(struct mlx4_en_priv * priv,struct mlx4_en_tx_ring * ring,u32 index,unsigned int desc_size)565*4882a593Smuzhiyun static struct mlx4_en_tx_desc *mlx4_en_bounce_to_desc(struct mlx4_en_priv *priv,
566*4882a593Smuzhiyun 						      struct mlx4_en_tx_ring *ring,
567*4882a593Smuzhiyun 						      u32 index,
568*4882a593Smuzhiyun 						      unsigned int desc_size)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun 	u32 copy = (ring->size - index) << LOG_TXBB_SIZE;
571*4882a593Smuzhiyun 	int i;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	for (i = desc_size - copy - 4; i >= 0; i -= 4) {
574*4882a593Smuzhiyun 		if ((i & (TXBB_SIZE - 1)) == 0)
575*4882a593Smuzhiyun 			wmb();
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 		*((u32 *) (ring->buf + i)) =
578*4882a593Smuzhiyun 			*((u32 *) (ring->bounce_buf + copy + i));
579*4882a593Smuzhiyun 	}
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	for (i = copy - 4; i >= 4 ; i -= 4) {
582*4882a593Smuzhiyun 		if ((i & (TXBB_SIZE - 1)) == 0)
583*4882a593Smuzhiyun 			wmb();
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 		*((u32 *)(ring->buf + (index << LOG_TXBB_SIZE) + i)) =
586*4882a593Smuzhiyun 			*((u32 *) (ring->bounce_buf + i));
587*4882a593Smuzhiyun 	}
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	/* Return real descriptor location */
590*4882a593Smuzhiyun 	return ring->buf + (index << LOG_TXBB_SIZE);
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun /* Decide if skb can be inlined in tx descriptor to avoid dma mapping
594*4882a593Smuzhiyun  *
595*4882a593Smuzhiyun  * It seems strange we do not simply use skb_copy_bits().
596*4882a593Smuzhiyun  * This would allow to inline all skbs iff skb->len <= inline_thold
597*4882a593Smuzhiyun  *
598*4882a593Smuzhiyun  * Note that caller already checked skb was not a gso packet
599*4882a593Smuzhiyun  */
is_inline(int inline_thold,const struct sk_buff * skb,const struct skb_shared_info * shinfo,void ** pfrag)600*4882a593Smuzhiyun static bool is_inline(int inline_thold, const struct sk_buff *skb,
601*4882a593Smuzhiyun 		      const struct skb_shared_info *shinfo,
602*4882a593Smuzhiyun 		      void **pfrag)
603*4882a593Smuzhiyun {
604*4882a593Smuzhiyun 	void *ptr;
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	if (skb->len > inline_thold || !inline_thold)
607*4882a593Smuzhiyun 		return false;
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	if (shinfo->nr_frags == 1) {
610*4882a593Smuzhiyun 		ptr = skb_frag_address_safe(&shinfo->frags[0]);
611*4882a593Smuzhiyun 		if (unlikely(!ptr))
612*4882a593Smuzhiyun 			return false;
613*4882a593Smuzhiyun 		*pfrag = ptr;
614*4882a593Smuzhiyun 		return true;
615*4882a593Smuzhiyun 	}
616*4882a593Smuzhiyun 	if (shinfo->nr_frags)
617*4882a593Smuzhiyun 		return false;
618*4882a593Smuzhiyun 	return true;
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun 
inline_size(const struct sk_buff * skb)621*4882a593Smuzhiyun static int inline_size(const struct sk_buff *skb)
622*4882a593Smuzhiyun {
623*4882a593Smuzhiyun 	if (skb->len + CTRL_SIZE + sizeof(struct mlx4_wqe_inline_seg)
624*4882a593Smuzhiyun 	    <= MLX4_INLINE_ALIGN)
625*4882a593Smuzhiyun 		return ALIGN(skb->len + CTRL_SIZE +
626*4882a593Smuzhiyun 			     sizeof(struct mlx4_wqe_inline_seg), 16);
627*4882a593Smuzhiyun 	else
628*4882a593Smuzhiyun 		return ALIGN(skb->len + CTRL_SIZE + 2 *
629*4882a593Smuzhiyun 			     sizeof(struct mlx4_wqe_inline_seg), 16);
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun 
get_real_size(const struct sk_buff * skb,const struct skb_shared_info * shinfo,struct net_device * dev,int * lso_header_size,bool * inline_ok,void ** pfrag)632*4882a593Smuzhiyun static int get_real_size(const struct sk_buff *skb,
633*4882a593Smuzhiyun 			 const struct skb_shared_info *shinfo,
634*4882a593Smuzhiyun 			 struct net_device *dev,
635*4882a593Smuzhiyun 			 int *lso_header_size,
636*4882a593Smuzhiyun 			 bool *inline_ok,
637*4882a593Smuzhiyun 			 void **pfrag)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun 	struct mlx4_en_priv *priv = netdev_priv(dev);
640*4882a593Smuzhiyun 	int real_size;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	if (shinfo->gso_size) {
643*4882a593Smuzhiyun 		*inline_ok = false;
644*4882a593Smuzhiyun 		if (skb->encapsulation)
645*4882a593Smuzhiyun 			*lso_header_size = (skb_inner_transport_header(skb) - skb->data) + inner_tcp_hdrlen(skb);
646*4882a593Smuzhiyun 		else
647*4882a593Smuzhiyun 			*lso_header_size = skb_transport_offset(skb) + tcp_hdrlen(skb);
648*4882a593Smuzhiyun 		real_size = CTRL_SIZE + shinfo->nr_frags * DS_SIZE +
649*4882a593Smuzhiyun 			ALIGN(*lso_header_size + 4, DS_SIZE);
650*4882a593Smuzhiyun 		if (unlikely(*lso_header_size != skb_headlen(skb))) {
651*4882a593Smuzhiyun 			/* We add a segment for the skb linear buffer only if
652*4882a593Smuzhiyun 			 * it contains data */
653*4882a593Smuzhiyun 			if (*lso_header_size < skb_headlen(skb))
654*4882a593Smuzhiyun 				real_size += DS_SIZE;
655*4882a593Smuzhiyun 			else {
656*4882a593Smuzhiyun 				if (netif_msg_tx_err(priv))
657*4882a593Smuzhiyun 					en_warn(priv, "Non-linear headers\n");
658*4882a593Smuzhiyun 				return 0;
659*4882a593Smuzhiyun 			}
660*4882a593Smuzhiyun 		}
661*4882a593Smuzhiyun 	} else {
662*4882a593Smuzhiyun 		*lso_header_size = 0;
663*4882a593Smuzhiyun 		*inline_ok = is_inline(priv->prof->inline_thold, skb,
664*4882a593Smuzhiyun 				       shinfo, pfrag);
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 		if (*inline_ok)
667*4882a593Smuzhiyun 			real_size = inline_size(skb);
668*4882a593Smuzhiyun 		else
669*4882a593Smuzhiyun 			real_size = CTRL_SIZE +
670*4882a593Smuzhiyun 				    (shinfo->nr_frags + 1) * DS_SIZE;
671*4882a593Smuzhiyun 	}
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	return real_size;
674*4882a593Smuzhiyun }
675*4882a593Smuzhiyun 
build_inline_wqe(struct mlx4_en_tx_desc * tx_desc,const struct sk_buff * skb,const struct skb_shared_info * shinfo,void * fragptr)676*4882a593Smuzhiyun static void build_inline_wqe(struct mlx4_en_tx_desc *tx_desc,
677*4882a593Smuzhiyun 			     const struct sk_buff *skb,
678*4882a593Smuzhiyun 			     const struct skb_shared_info *shinfo,
679*4882a593Smuzhiyun 			     void *fragptr)
680*4882a593Smuzhiyun {
681*4882a593Smuzhiyun 	struct mlx4_wqe_inline_seg *inl = &tx_desc->inl;
682*4882a593Smuzhiyun 	int spc = MLX4_INLINE_ALIGN - CTRL_SIZE - sizeof(*inl);
683*4882a593Smuzhiyun 	unsigned int hlen = skb_headlen(skb);
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	if (skb->len <= spc) {
686*4882a593Smuzhiyun 		if (likely(skb->len >= MIN_PKT_LEN)) {
687*4882a593Smuzhiyun 			inl->byte_count = cpu_to_be32(1 << 31 | skb->len);
688*4882a593Smuzhiyun 		} else {
689*4882a593Smuzhiyun 			inl->byte_count = cpu_to_be32(1 << 31 | MIN_PKT_LEN);
690*4882a593Smuzhiyun 			memset(((void *)(inl + 1)) + skb->len, 0,
691*4882a593Smuzhiyun 			       MIN_PKT_LEN - skb->len);
692*4882a593Smuzhiyun 		}
693*4882a593Smuzhiyun 		skb_copy_from_linear_data(skb, inl + 1, hlen);
694*4882a593Smuzhiyun 		if (shinfo->nr_frags)
695*4882a593Smuzhiyun 			memcpy(((void *)(inl + 1)) + hlen, fragptr,
696*4882a593Smuzhiyun 			       skb_frag_size(&shinfo->frags[0]));
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	} else {
699*4882a593Smuzhiyun 		inl->byte_count = cpu_to_be32(1 << 31 | spc);
700*4882a593Smuzhiyun 		if (hlen <= spc) {
701*4882a593Smuzhiyun 			skb_copy_from_linear_data(skb, inl + 1, hlen);
702*4882a593Smuzhiyun 			if (hlen < spc) {
703*4882a593Smuzhiyun 				memcpy(((void *)(inl + 1)) + hlen,
704*4882a593Smuzhiyun 				       fragptr, spc - hlen);
705*4882a593Smuzhiyun 				fragptr +=  spc - hlen;
706*4882a593Smuzhiyun 			}
707*4882a593Smuzhiyun 			inl = (void *) (inl + 1) + spc;
708*4882a593Smuzhiyun 			memcpy(((void *)(inl + 1)), fragptr, skb->len - spc);
709*4882a593Smuzhiyun 		} else {
710*4882a593Smuzhiyun 			skb_copy_from_linear_data(skb, inl + 1, spc);
711*4882a593Smuzhiyun 			inl = (void *) (inl + 1) + spc;
712*4882a593Smuzhiyun 			skb_copy_from_linear_data_offset(skb, spc, inl + 1,
713*4882a593Smuzhiyun 							 hlen - spc);
714*4882a593Smuzhiyun 			if (shinfo->nr_frags)
715*4882a593Smuzhiyun 				memcpy(((void *)(inl + 1)) + hlen - spc,
716*4882a593Smuzhiyun 				       fragptr,
717*4882a593Smuzhiyun 				       skb_frag_size(&shinfo->frags[0]));
718*4882a593Smuzhiyun 		}
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 		dma_wmb();
721*4882a593Smuzhiyun 		inl->byte_count = cpu_to_be32(1 << 31 | (skb->len - spc));
722*4882a593Smuzhiyun 	}
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun 
mlx4_en_select_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)725*4882a593Smuzhiyun u16 mlx4_en_select_queue(struct net_device *dev, struct sk_buff *skb,
726*4882a593Smuzhiyun 			 struct net_device *sb_dev)
727*4882a593Smuzhiyun {
728*4882a593Smuzhiyun 	struct mlx4_en_priv *priv = netdev_priv(dev);
729*4882a593Smuzhiyun 	u16 rings_p_up = priv->num_tx_rings_p_up;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	if (netdev_get_num_tc(dev))
732*4882a593Smuzhiyun 		return netdev_pick_tx(dev, skb, NULL);
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	return netdev_pick_tx(dev, skb, NULL) % rings_p_up;
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun 
mlx4_bf_copy(void __iomem * dst,const void * src,unsigned int bytecnt)737*4882a593Smuzhiyun static void mlx4_bf_copy(void __iomem *dst, const void *src,
738*4882a593Smuzhiyun 			 unsigned int bytecnt)
739*4882a593Smuzhiyun {
740*4882a593Smuzhiyun 	__iowrite64_copy(dst, src, bytecnt / 8);
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun 
mlx4_en_xmit_doorbell(struct mlx4_en_tx_ring * ring)743*4882a593Smuzhiyun void mlx4_en_xmit_doorbell(struct mlx4_en_tx_ring *ring)
744*4882a593Smuzhiyun {
745*4882a593Smuzhiyun 	wmb();
746*4882a593Smuzhiyun 	/* Since there is no iowrite*_native() that writes the
747*4882a593Smuzhiyun 	 * value as is, without byteswapping - using the one
748*4882a593Smuzhiyun 	 * the doesn't do byteswapping in the relevant arch
749*4882a593Smuzhiyun 	 * endianness.
750*4882a593Smuzhiyun 	 */
751*4882a593Smuzhiyun #if defined(__LITTLE_ENDIAN)
752*4882a593Smuzhiyun 	iowrite32(
753*4882a593Smuzhiyun #else
754*4882a593Smuzhiyun 	iowrite32be(
755*4882a593Smuzhiyun #endif
756*4882a593Smuzhiyun 		  (__force u32)ring->doorbell_qpn,
757*4882a593Smuzhiyun 		  ring->bf.uar->map + MLX4_SEND_DOORBELL);
758*4882a593Smuzhiyun }
759*4882a593Smuzhiyun 
mlx4_en_tx_write_desc(struct mlx4_en_tx_ring * ring,struct mlx4_en_tx_desc * tx_desc,union mlx4_wqe_qpn_vlan qpn_vlan,int desc_size,int bf_index,__be32 op_own,bool bf_ok,bool send_doorbell)760*4882a593Smuzhiyun static void mlx4_en_tx_write_desc(struct mlx4_en_tx_ring *ring,
761*4882a593Smuzhiyun 				  struct mlx4_en_tx_desc *tx_desc,
762*4882a593Smuzhiyun 				  union mlx4_wqe_qpn_vlan qpn_vlan,
763*4882a593Smuzhiyun 				  int desc_size, int bf_index,
764*4882a593Smuzhiyun 				  __be32 op_own, bool bf_ok,
765*4882a593Smuzhiyun 				  bool send_doorbell)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun 	tx_desc->ctrl.qpn_vlan = qpn_vlan;
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	if (bf_ok) {
770*4882a593Smuzhiyun 		op_own |= htonl((bf_index & 0xffff) << 8);
771*4882a593Smuzhiyun 		/* Ensure new descriptor hits memory
772*4882a593Smuzhiyun 		 * before setting ownership of this descriptor to HW
773*4882a593Smuzhiyun 		 */
774*4882a593Smuzhiyun 		dma_wmb();
775*4882a593Smuzhiyun 		tx_desc->ctrl.owner_opcode = op_own;
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 		wmb();
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 		mlx4_bf_copy(ring->bf.reg + ring->bf.offset, &tx_desc->ctrl,
780*4882a593Smuzhiyun 			     desc_size);
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 		wmb();
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 		ring->bf.offset ^= ring->bf.buf_size;
785*4882a593Smuzhiyun 	} else {
786*4882a593Smuzhiyun 		/* Ensure new descriptor hits memory
787*4882a593Smuzhiyun 		 * before setting ownership of this descriptor to HW
788*4882a593Smuzhiyun 		 */
789*4882a593Smuzhiyun 		dma_wmb();
790*4882a593Smuzhiyun 		tx_desc->ctrl.owner_opcode = op_own;
791*4882a593Smuzhiyun 		if (send_doorbell)
792*4882a593Smuzhiyun 			mlx4_en_xmit_doorbell(ring);
793*4882a593Smuzhiyun 		else
794*4882a593Smuzhiyun 			ring->xmit_more++;
795*4882a593Smuzhiyun 	}
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun 
mlx4_en_build_dma_wqe(struct mlx4_en_priv * priv,struct skb_shared_info * shinfo,struct mlx4_wqe_data_seg * data,struct sk_buff * skb,int lso_header_size,__be32 mr_key,struct mlx4_en_tx_info * tx_info)798*4882a593Smuzhiyun static bool mlx4_en_build_dma_wqe(struct mlx4_en_priv *priv,
799*4882a593Smuzhiyun 				  struct skb_shared_info *shinfo,
800*4882a593Smuzhiyun 				  struct mlx4_wqe_data_seg *data,
801*4882a593Smuzhiyun 				  struct sk_buff *skb,
802*4882a593Smuzhiyun 				  int lso_header_size,
803*4882a593Smuzhiyun 				  __be32 mr_key,
804*4882a593Smuzhiyun 				  struct mlx4_en_tx_info *tx_info)
805*4882a593Smuzhiyun {
806*4882a593Smuzhiyun 	struct device *ddev = priv->ddev;
807*4882a593Smuzhiyun 	dma_addr_t dma = 0;
808*4882a593Smuzhiyun 	u32 byte_count = 0;
809*4882a593Smuzhiyun 	int i_frag;
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	/* Map fragments if any */
812*4882a593Smuzhiyun 	for (i_frag = shinfo->nr_frags - 1; i_frag >= 0; i_frag--) {
813*4882a593Smuzhiyun 		const skb_frag_t *frag = &shinfo->frags[i_frag];
814*4882a593Smuzhiyun 		byte_count = skb_frag_size(frag);
815*4882a593Smuzhiyun 		dma = skb_frag_dma_map(ddev, frag,
816*4882a593Smuzhiyun 				       0, byte_count,
817*4882a593Smuzhiyun 				       DMA_TO_DEVICE);
818*4882a593Smuzhiyun 		if (dma_mapping_error(ddev, dma))
819*4882a593Smuzhiyun 			goto tx_drop_unmap;
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 		data->addr = cpu_to_be64(dma);
822*4882a593Smuzhiyun 		data->lkey = mr_key;
823*4882a593Smuzhiyun 		dma_wmb();
824*4882a593Smuzhiyun 		data->byte_count = cpu_to_be32(byte_count);
825*4882a593Smuzhiyun 		--data;
826*4882a593Smuzhiyun 	}
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	/* Map linear part if needed */
829*4882a593Smuzhiyun 	if (tx_info->linear) {
830*4882a593Smuzhiyun 		byte_count = skb_headlen(skb) - lso_header_size;
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 		dma = dma_map_single(ddev, skb->data +
833*4882a593Smuzhiyun 				     lso_header_size, byte_count,
834*4882a593Smuzhiyun 				     PCI_DMA_TODEVICE);
835*4882a593Smuzhiyun 		if (dma_mapping_error(ddev, dma))
836*4882a593Smuzhiyun 			goto tx_drop_unmap;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 		data->addr = cpu_to_be64(dma);
839*4882a593Smuzhiyun 		data->lkey = mr_key;
840*4882a593Smuzhiyun 		dma_wmb();
841*4882a593Smuzhiyun 		data->byte_count = cpu_to_be32(byte_count);
842*4882a593Smuzhiyun 	}
843*4882a593Smuzhiyun 	/* tx completion can avoid cache line miss for common cases */
844*4882a593Smuzhiyun 	tx_info->map0_dma = dma;
845*4882a593Smuzhiyun 	tx_info->map0_byte_count = byte_count;
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	return true;
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun tx_drop_unmap:
850*4882a593Smuzhiyun 	en_err(priv, "DMA mapping error\n");
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 	while (++i_frag < shinfo->nr_frags) {
853*4882a593Smuzhiyun 		++data;
854*4882a593Smuzhiyun 		dma_unmap_page(ddev, (dma_addr_t)be64_to_cpu(data->addr),
855*4882a593Smuzhiyun 			       be32_to_cpu(data->byte_count),
856*4882a593Smuzhiyun 			       PCI_DMA_TODEVICE);
857*4882a593Smuzhiyun 	}
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 	return false;
860*4882a593Smuzhiyun }
861*4882a593Smuzhiyun 
mlx4_en_xmit(struct sk_buff * skb,struct net_device * dev)862*4882a593Smuzhiyun netdev_tx_t mlx4_en_xmit(struct sk_buff *skb, struct net_device *dev)
863*4882a593Smuzhiyun {
864*4882a593Smuzhiyun 	struct skb_shared_info *shinfo = skb_shinfo(skb);
865*4882a593Smuzhiyun 	struct mlx4_en_priv *priv = netdev_priv(dev);
866*4882a593Smuzhiyun 	union mlx4_wqe_qpn_vlan	qpn_vlan = {};
867*4882a593Smuzhiyun 	struct mlx4_en_tx_ring *ring;
868*4882a593Smuzhiyun 	struct mlx4_en_tx_desc *tx_desc;
869*4882a593Smuzhiyun 	struct mlx4_wqe_data_seg *data;
870*4882a593Smuzhiyun 	struct mlx4_en_tx_info *tx_info;
871*4882a593Smuzhiyun 	u32 __maybe_unused ring_cons;
872*4882a593Smuzhiyun 	int tx_ind;
873*4882a593Smuzhiyun 	int nr_txbb;
874*4882a593Smuzhiyun 	int desc_size;
875*4882a593Smuzhiyun 	int real_size;
876*4882a593Smuzhiyun 	u32 index, bf_index;
877*4882a593Smuzhiyun 	__be32 op_own;
878*4882a593Smuzhiyun 	int lso_header_size;
879*4882a593Smuzhiyun 	void *fragptr = NULL;
880*4882a593Smuzhiyun 	bool bounce = false;
881*4882a593Smuzhiyun 	bool send_doorbell;
882*4882a593Smuzhiyun 	bool stop_queue;
883*4882a593Smuzhiyun 	bool inline_ok;
884*4882a593Smuzhiyun 	u8 data_offset;
885*4882a593Smuzhiyun 	bool bf_ok;
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	tx_ind = skb_get_queue_mapping(skb);
888*4882a593Smuzhiyun 	ring = priv->tx_ring[TX][tx_ind];
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	if (unlikely(!priv->port_up))
891*4882a593Smuzhiyun 		goto tx_drop;
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	/* fetch ring->cons far ahead before needing it to avoid stall */
894*4882a593Smuzhiyun 	ring_cons = READ_ONCE(ring->cons);
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	real_size = get_real_size(skb, shinfo, dev, &lso_header_size,
897*4882a593Smuzhiyun 				  &inline_ok, &fragptr);
898*4882a593Smuzhiyun 	if (unlikely(!real_size))
899*4882a593Smuzhiyun 		goto tx_drop_count;
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	/* Align descriptor to TXBB size */
902*4882a593Smuzhiyun 	desc_size = ALIGN(real_size, TXBB_SIZE);
903*4882a593Smuzhiyun 	nr_txbb = desc_size >> LOG_TXBB_SIZE;
904*4882a593Smuzhiyun 	if (unlikely(nr_txbb > MAX_DESC_TXBBS)) {
905*4882a593Smuzhiyun 		if (netif_msg_tx_err(priv))
906*4882a593Smuzhiyun 			en_warn(priv, "Oversized header or SG list\n");
907*4882a593Smuzhiyun 		goto tx_drop_count;
908*4882a593Smuzhiyun 	}
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	bf_ok = ring->bf_enabled;
911*4882a593Smuzhiyun 	if (skb_vlan_tag_present(skb)) {
912*4882a593Smuzhiyun 		u16 vlan_proto;
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 		qpn_vlan.vlan_tag = cpu_to_be16(skb_vlan_tag_get(skb));
915*4882a593Smuzhiyun 		vlan_proto = be16_to_cpu(skb->vlan_proto);
916*4882a593Smuzhiyun 		if (vlan_proto == ETH_P_8021AD)
917*4882a593Smuzhiyun 			qpn_vlan.ins_vlan = MLX4_WQE_CTRL_INS_SVLAN;
918*4882a593Smuzhiyun 		else if (vlan_proto == ETH_P_8021Q)
919*4882a593Smuzhiyun 			qpn_vlan.ins_vlan = MLX4_WQE_CTRL_INS_CVLAN;
920*4882a593Smuzhiyun 		else
921*4882a593Smuzhiyun 			qpn_vlan.ins_vlan = 0;
922*4882a593Smuzhiyun 		bf_ok = false;
923*4882a593Smuzhiyun 	}
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	netdev_txq_bql_enqueue_prefetchw(ring->tx_queue);
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	/* Track current inflight packets for performance analysis */
928*4882a593Smuzhiyun 	AVG_PERF_COUNTER(priv->pstats.inflight_avg,
929*4882a593Smuzhiyun 			 (u32)(ring->prod - ring_cons - 1));
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	/* Packet is good - grab an index and transmit it */
932*4882a593Smuzhiyun 	index = ring->prod & ring->size_mask;
933*4882a593Smuzhiyun 	bf_index = ring->prod;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	/* See if we have enough space for whole descriptor TXBB for setting
936*4882a593Smuzhiyun 	 * SW ownership on next descriptor; if not, use a bounce buffer. */
937*4882a593Smuzhiyun 	if (likely(index + nr_txbb <= ring->size))
938*4882a593Smuzhiyun 		tx_desc = ring->buf + (index << LOG_TXBB_SIZE);
939*4882a593Smuzhiyun 	else {
940*4882a593Smuzhiyun 		tx_desc = (struct mlx4_en_tx_desc *) ring->bounce_buf;
941*4882a593Smuzhiyun 		bounce = true;
942*4882a593Smuzhiyun 		bf_ok = false;
943*4882a593Smuzhiyun 	}
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	/* Save skb in tx_info ring */
946*4882a593Smuzhiyun 	tx_info = &ring->tx_info[index];
947*4882a593Smuzhiyun 	tx_info->skb = skb;
948*4882a593Smuzhiyun 	tx_info->nr_txbb = nr_txbb;
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	if (!lso_header_size) {
951*4882a593Smuzhiyun 		data = &tx_desc->data;
952*4882a593Smuzhiyun 		data_offset = offsetof(struct mlx4_en_tx_desc, data);
953*4882a593Smuzhiyun 	} else {
954*4882a593Smuzhiyun 		int lso_align = ALIGN(lso_header_size + 4, DS_SIZE);
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun 		data = (void *)&tx_desc->lso + lso_align;
957*4882a593Smuzhiyun 		data_offset = offsetof(struct mlx4_en_tx_desc, lso) + lso_align;
958*4882a593Smuzhiyun 	}
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	/* valid only for none inline segments */
961*4882a593Smuzhiyun 	tx_info->data_offset = data_offset;
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun 	tx_info->inl = inline_ok;
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 	tx_info->linear = lso_header_size < skb_headlen(skb) && !inline_ok;
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 	tx_info->nr_maps = shinfo->nr_frags + tx_info->linear;
968*4882a593Smuzhiyun 	data += tx_info->nr_maps - 1;
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	if (!tx_info->inl)
971*4882a593Smuzhiyun 		if (!mlx4_en_build_dma_wqe(priv, shinfo, data, skb,
972*4882a593Smuzhiyun 					   lso_header_size, ring->mr_key,
973*4882a593Smuzhiyun 					   tx_info))
974*4882a593Smuzhiyun 			goto tx_drop_count;
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	/*
977*4882a593Smuzhiyun 	 * For timestamping add flag to skb_shinfo and
978*4882a593Smuzhiyun 	 * set flag for further reference
979*4882a593Smuzhiyun 	 */
980*4882a593Smuzhiyun 	tx_info->ts_requested = 0;
981*4882a593Smuzhiyun 	if (unlikely(ring->hwtstamp_tx_type == HWTSTAMP_TX_ON &&
982*4882a593Smuzhiyun 		     shinfo->tx_flags & SKBTX_HW_TSTAMP)) {
983*4882a593Smuzhiyun 		shinfo->tx_flags |= SKBTX_IN_PROGRESS;
984*4882a593Smuzhiyun 		tx_info->ts_requested = 1;
985*4882a593Smuzhiyun 	}
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun 	/* Prepare ctrl segement apart opcode+ownership, which depends on
988*4882a593Smuzhiyun 	 * whether LSO is used */
989*4882a593Smuzhiyun 	tx_desc->ctrl.srcrb_flags = priv->ctrl_flags;
990*4882a593Smuzhiyun 	if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
991*4882a593Smuzhiyun 		if (!skb->encapsulation)
992*4882a593Smuzhiyun 			tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
993*4882a593Smuzhiyun 								 MLX4_WQE_CTRL_TCP_UDP_CSUM);
994*4882a593Smuzhiyun 		else
995*4882a593Smuzhiyun 			tx_desc->ctrl.srcrb_flags |= cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM);
996*4882a593Smuzhiyun 		ring->tx_csum++;
997*4882a593Smuzhiyun 	}
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 	if (priv->flags & MLX4_EN_FLAG_ENABLE_HW_LOOPBACK) {
1000*4882a593Smuzhiyun 		struct ethhdr *ethh;
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 		/* Copy dst mac address to wqe. This allows loopback in eSwitch,
1003*4882a593Smuzhiyun 		 * so that VFs and PF can communicate with each other
1004*4882a593Smuzhiyun 		 */
1005*4882a593Smuzhiyun 		ethh = (struct ethhdr *)skb->data;
1006*4882a593Smuzhiyun 		tx_desc->ctrl.srcrb_flags16[0] = get_unaligned((__be16 *)ethh->h_dest);
1007*4882a593Smuzhiyun 		tx_desc->ctrl.imm = get_unaligned((__be32 *)(ethh->h_dest + 2));
1008*4882a593Smuzhiyun 	}
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun 	/* Handle LSO (TSO) packets */
1011*4882a593Smuzhiyun 	if (lso_header_size) {
1012*4882a593Smuzhiyun 		int i;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 		/* Mark opcode as LSO */
1015*4882a593Smuzhiyun 		op_own = cpu_to_be32(MLX4_OPCODE_LSO | (1 << 6)) |
1016*4882a593Smuzhiyun 			((ring->prod & ring->size) ?
1017*4882a593Smuzhiyun 				cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 		/* Fill in the LSO prefix */
1020*4882a593Smuzhiyun 		tx_desc->lso.mss_hdr_size = cpu_to_be32(
1021*4882a593Smuzhiyun 			shinfo->gso_size << 16 | lso_header_size);
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 		/* Copy headers;
1024*4882a593Smuzhiyun 		 * note that we already verified that it is linear */
1025*4882a593Smuzhiyun 		memcpy(tx_desc->lso.header, skb->data, lso_header_size);
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 		ring->tso_packets++;
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 		i = shinfo->gso_segs;
1030*4882a593Smuzhiyun 		tx_info->nr_bytes = skb->len + (i - 1) * lso_header_size;
1031*4882a593Smuzhiyun 		ring->packets += i;
1032*4882a593Smuzhiyun 	} else {
1033*4882a593Smuzhiyun 		/* Normal (Non LSO) packet */
1034*4882a593Smuzhiyun 		op_own = cpu_to_be32(MLX4_OPCODE_SEND) |
1035*4882a593Smuzhiyun 			((ring->prod & ring->size) ?
1036*4882a593Smuzhiyun 			 cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
1037*4882a593Smuzhiyun 		tx_info->nr_bytes = max_t(unsigned int, skb->len, ETH_ZLEN);
1038*4882a593Smuzhiyun 		ring->packets++;
1039*4882a593Smuzhiyun 	}
1040*4882a593Smuzhiyun 	ring->bytes += tx_info->nr_bytes;
1041*4882a593Smuzhiyun 	AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, skb->len);
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	if (tx_info->inl)
1044*4882a593Smuzhiyun 		build_inline_wqe(tx_desc, skb, shinfo, fragptr);
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 	if (skb->encapsulation) {
1047*4882a593Smuzhiyun 		union {
1048*4882a593Smuzhiyun 			struct iphdr *v4;
1049*4882a593Smuzhiyun 			struct ipv6hdr *v6;
1050*4882a593Smuzhiyun 			unsigned char *hdr;
1051*4882a593Smuzhiyun 		} ip;
1052*4882a593Smuzhiyun 		u8 proto;
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 		ip.hdr = skb_inner_network_header(skb);
1055*4882a593Smuzhiyun 		proto = (ip.v4->version == 4) ? ip.v4->protocol :
1056*4882a593Smuzhiyun 						ip.v6->nexthdr;
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 		if (proto == IPPROTO_TCP || proto == IPPROTO_UDP)
1059*4882a593Smuzhiyun 			op_own |= cpu_to_be32(MLX4_WQE_CTRL_IIP | MLX4_WQE_CTRL_ILP);
1060*4882a593Smuzhiyun 		else
1061*4882a593Smuzhiyun 			op_own |= cpu_to_be32(MLX4_WQE_CTRL_IIP);
1062*4882a593Smuzhiyun 	}
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun 	ring->prod += nr_txbb;
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	/* If we used a bounce buffer then copy descriptor back into place */
1067*4882a593Smuzhiyun 	if (unlikely(bounce))
1068*4882a593Smuzhiyun 		tx_desc = mlx4_en_bounce_to_desc(priv, ring, index, desc_size);
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 	skb_tx_timestamp(skb);
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	/* Check available TXBBs And 2K spare for prefetch */
1073*4882a593Smuzhiyun 	stop_queue = mlx4_en_is_tx_ring_full(ring);
1074*4882a593Smuzhiyun 	if (unlikely(stop_queue)) {
1075*4882a593Smuzhiyun 		netif_tx_stop_queue(ring->tx_queue);
1076*4882a593Smuzhiyun 		ring->queue_stopped++;
1077*4882a593Smuzhiyun 	}
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 	send_doorbell = __netdev_tx_sent_queue(ring->tx_queue,
1080*4882a593Smuzhiyun 					       tx_info->nr_bytes,
1081*4882a593Smuzhiyun 					       netdev_xmit_more());
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	real_size = (real_size / 16) & 0x3f;
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 	bf_ok &= desc_size <= MAX_BF && send_doorbell;
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 	if (bf_ok)
1088*4882a593Smuzhiyun 		qpn_vlan.bf_qpn = ring->doorbell_qpn | cpu_to_be32(real_size);
1089*4882a593Smuzhiyun 	else
1090*4882a593Smuzhiyun 		qpn_vlan.fence_size = real_size;
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	mlx4_en_tx_write_desc(ring, tx_desc, qpn_vlan, desc_size, bf_index,
1093*4882a593Smuzhiyun 			      op_own, bf_ok, send_doorbell);
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	if (unlikely(stop_queue)) {
1096*4882a593Smuzhiyun 		/* If queue was emptied after the if (stop_queue) , and before
1097*4882a593Smuzhiyun 		 * the netif_tx_stop_queue() - need to wake the queue,
1098*4882a593Smuzhiyun 		 * or else it will remain stopped forever.
1099*4882a593Smuzhiyun 		 * Need a memory barrier to make sure ring->cons was not
1100*4882a593Smuzhiyun 		 * updated before queue was stopped.
1101*4882a593Smuzhiyun 		 */
1102*4882a593Smuzhiyun 		smp_rmb();
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 		if (unlikely(!mlx4_en_is_tx_ring_full(ring))) {
1105*4882a593Smuzhiyun 			netif_tx_wake_queue(ring->tx_queue);
1106*4882a593Smuzhiyun 			ring->wake_queue++;
1107*4882a593Smuzhiyun 		}
1108*4882a593Smuzhiyun 	}
1109*4882a593Smuzhiyun 	return NETDEV_TX_OK;
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun tx_drop_count:
1112*4882a593Smuzhiyun 	ring->tx_dropped++;
1113*4882a593Smuzhiyun tx_drop:
1114*4882a593Smuzhiyun 	dev_kfree_skb_any(skb);
1115*4882a593Smuzhiyun 	return NETDEV_TX_OK;
1116*4882a593Smuzhiyun }
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun #define MLX4_EN_XDP_TX_NRTXBB  1
1119*4882a593Smuzhiyun #define MLX4_EN_XDP_TX_REAL_SZ (((CTRL_SIZE + MLX4_EN_XDP_TX_NRTXBB * DS_SIZE) \
1120*4882a593Smuzhiyun 				 / 16) & 0x3f)
1121*4882a593Smuzhiyun 
mlx4_en_init_tx_xdp_ring_descs(struct mlx4_en_priv * priv,struct mlx4_en_tx_ring * ring)1122*4882a593Smuzhiyun void mlx4_en_init_tx_xdp_ring_descs(struct mlx4_en_priv *priv,
1123*4882a593Smuzhiyun 				    struct mlx4_en_tx_ring *ring)
1124*4882a593Smuzhiyun {
1125*4882a593Smuzhiyun 	int i;
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 	for (i = 0; i < ring->size; i++) {
1128*4882a593Smuzhiyun 		struct mlx4_en_tx_info *tx_info = &ring->tx_info[i];
1129*4882a593Smuzhiyun 		struct mlx4_en_tx_desc *tx_desc = ring->buf +
1130*4882a593Smuzhiyun 			(i << LOG_TXBB_SIZE);
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 		tx_info->map0_byte_count = PAGE_SIZE;
1133*4882a593Smuzhiyun 		tx_info->nr_txbb = MLX4_EN_XDP_TX_NRTXBB;
1134*4882a593Smuzhiyun 		tx_info->data_offset = offsetof(struct mlx4_en_tx_desc, data);
1135*4882a593Smuzhiyun 		tx_info->ts_requested = 0;
1136*4882a593Smuzhiyun 		tx_info->nr_maps = 1;
1137*4882a593Smuzhiyun 		tx_info->linear = 1;
1138*4882a593Smuzhiyun 		tx_info->inl = 0;
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 		tx_desc->data.lkey = ring->mr_key;
1141*4882a593Smuzhiyun 		tx_desc->ctrl.qpn_vlan.fence_size = MLX4_EN_XDP_TX_REAL_SZ;
1142*4882a593Smuzhiyun 		tx_desc->ctrl.srcrb_flags = priv->ctrl_flags;
1143*4882a593Smuzhiyun 	}
1144*4882a593Smuzhiyun }
1145*4882a593Smuzhiyun 
mlx4_en_xmit_frame(struct mlx4_en_rx_ring * rx_ring,struct mlx4_en_rx_alloc * frame,struct mlx4_en_priv * priv,unsigned int length,int tx_ind,bool * doorbell_pending)1146*4882a593Smuzhiyun netdev_tx_t mlx4_en_xmit_frame(struct mlx4_en_rx_ring *rx_ring,
1147*4882a593Smuzhiyun 			       struct mlx4_en_rx_alloc *frame,
1148*4882a593Smuzhiyun 			       struct mlx4_en_priv *priv, unsigned int length,
1149*4882a593Smuzhiyun 			       int tx_ind, bool *doorbell_pending)
1150*4882a593Smuzhiyun {
1151*4882a593Smuzhiyun 	struct mlx4_en_tx_desc *tx_desc;
1152*4882a593Smuzhiyun 	struct mlx4_en_tx_info *tx_info;
1153*4882a593Smuzhiyun 	struct mlx4_wqe_data_seg *data;
1154*4882a593Smuzhiyun 	struct mlx4_en_tx_ring *ring;
1155*4882a593Smuzhiyun 	dma_addr_t dma;
1156*4882a593Smuzhiyun 	__be32 op_own;
1157*4882a593Smuzhiyun 	int index;
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 	if (unlikely(!priv->port_up))
1160*4882a593Smuzhiyun 		goto tx_drop;
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	ring = priv->tx_ring[TX_XDP][tx_ind];
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	if (unlikely(mlx4_en_is_tx_ring_full(ring)))
1165*4882a593Smuzhiyun 		goto tx_drop_count;
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun 	index = ring->prod & ring->size_mask;
1168*4882a593Smuzhiyun 	tx_info = &ring->tx_info[index];
1169*4882a593Smuzhiyun 
1170*4882a593Smuzhiyun 	/* Track current inflight packets for performance analysis */
1171*4882a593Smuzhiyun 	AVG_PERF_COUNTER(priv->pstats.inflight_avg,
1172*4882a593Smuzhiyun 			 (u32)(ring->prod - READ_ONCE(ring->cons) - 1));
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 	tx_desc = ring->buf + (index << LOG_TXBB_SIZE);
1175*4882a593Smuzhiyun 	data = &tx_desc->data;
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun 	dma = frame->dma;
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun 	tx_info->page = frame->page;
1180*4882a593Smuzhiyun 	frame->page = NULL;
1181*4882a593Smuzhiyun 	tx_info->map0_dma = dma;
1182*4882a593Smuzhiyun 	tx_info->nr_bytes = max_t(unsigned int, length, ETH_ZLEN);
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun 	dma_sync_single_range_for_device(priv->ddev, dma, frame->page_offset,
1185*4882a593Smuzhiyun 					 length, PCI_DMA_TODEVICE);
1186*4882a593Smuzhiyun 
1187*4882a593Smuzhiyun 	data->addr = cpu_to_be64(dma + frame->page_offset);
1188*4882a593Smuzhiyun 	dma_wmb();
1189*4882a593Smuzhiyun 	data->byte_count = cpu_to_be32(length);
1190*4882a593Smuzhiyun 
1191*4882a593Smuzhiyun 	/* tx completion can avoid cache line miss for common cases */
1192*4882a593Smuzhiyun 
1193*4882a593Smuzhiyun 	op_own = cpu_to_be32(MLX4_OPCODE_SEND) |
1194*4882a593Smuzhiyun 		((ring->prod & ring->size) ?
1195*4882a593Smuzhiyun 		 cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 	rx_ring->xdp_tx++;
1198*4882a593Smuzhiyun 	AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, length);
1199*4882a593Smuzhiyun 
1200*4882a593Smuzhiyun 	ring->prod += MLX4_EN_XDP_TX_NRTXBB;
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 	/* Ensure new descriptor hits memory
1203*4882a593Smuzhiyun 	 * before setting ownership of this descriptor to HW
1204*4882a593Smuzhiyun 	 */
1205*4882a593Smuzhiyun 	dma_wmb();
1206*4882a593Smuzhiyun 	tx_desc->ctrl.owner_opcode = op_own;
1207*4882a593Smuzhiyun 	ring->xmit_more++;
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun 	*doorbell_pending = true;
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 	return NETDEV_TX_OK;
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun tx_drop_count:
1214*4882a593Smuzhiyun 	rx_ring->xdp_tx_full++;
1215*4882a593Smuzhiyun 	*doorbell_pending = true;
1216*4882a593Smuzhiyun tx_drop:
1217*4882a593Smuzhiyun 	return NETDEV_TX_BUSY;
1218*4882a593Smuzhiyun }
1219