• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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.
31  */
32 
33 #include <linux/tcp.h>
34 #include <linux/if_vlan.h>
35 #include <net/geneve.h>
36 #include <net/dsfield.h>
37 #include "en.h"
38 #include "en/txrx.h"
39 #include "ipoib/ipoib.h"
40 #include "en_accel/en_accel.h"
41 #include "en_accel/ipsec_rxtx.h"
42 #include "en/ptp.h"
43 
mlx5e_dma_unmap_wqe_err(struct mlx5e_txqsq * sq,u8 num_dma)44 static void mlx5e_dma_unmap_wqe_err(struct mlx5e_txqsq *sq, u8 num_dma)
45 {
46 	int i;
47 
48 	for (i = 0; i < num_dma; i++) {
49 		struct mlx5e_sq_dma *last_pushed_dma =
50 			mlx5e_dma_get(sq, --sq->dma_fifo_pc);
51 
52 		mlx5e_tx_dma_unmap(sq->pdev, last_pushed_dma);
53 	}
54 }
55 
56 #ifdef CONFIG_MLX5_CORE_EN_DCB
mlx5e_get_dscp_up(struct mlx5e_priv * priv,struct sk_buff * skb)57 static inline int mlx5e_get_dscp_up(struct mlx5e_priv *priv, struct sk_buff *skb)
58 {
59 	int dscp_cp = 0;
60 
61 	if (skb->protocol == htons(ETH_P_IP))
62 		dscp_cp = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
63 	else if (skb->protocol == htons(ETH_P_IPV6))
64 		dscp_cp = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
65 
66 	return priv->dcbx_dp.dscp2prio[dscp_cp];
67 }
68 #endif
69 
mlx5e_select_ptpsq(struct net_device * dev,struct sk_buff * skb)70 static u16 mlx5e_select_ptpsq(struct net_device *dev, struct sk_buff *skb)
71 {
72 	struct mlx5e_priv *priv = netdev_priv(dev);
73 	int up = 0;
74 
75 	if (!netdev_get_num_tc(dev))
76 		goto return_txq;
77 
78 #ifdef CONFIG_MLX5_CORE_EN_DCB
79 	if (priv->dcbx_dp.trust_state == MLX5_QPTS_TRUST_DSCP)
80 		up = mlx5e_get_dscp_up(priv, skb);
81 	else
82 #endif
83 		if (skb_vlan_tag_present(skb))
84 			up = skb_vlan_tag_get_prio(skb);
85 
86 return_txq:
87 	return priv->port_ptp_tc2realtxq[up];
88 }
89 
mlx5e_select_htb_queue(struct mlx5e_priv * priv,struct sk_buff * skb,u16 htb_maj_id)90 static int mlx5e_select_htb_queue(struct mlx5e_priv *priv, struct sk_buff *skb,
91 				  u16 htb_maj_id)
92 {
93 	u16 classid;
94 
95 	if ((TC_H_MAJ(skb->priority) >> 16) == htb_maj_id)
96 		classid = TC_H_MIN(skb->priority);
97 	else
98 		classid = READ_ONCE(priv->htb.defcls);
99 
100 	if (!classid)
101 		return 0;
102 
103 	return mlx5e_get_txq_by_classid(priv, classid);
104 }
105 
mlx5e_select_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)106 u16 mlx5e_select_queue(struct net_device *dev, struct sk_buff *skb,
107 		       struct net_device *sb_dev)
108 {
109 	struct mlx5e_priv *priv = netdev_priv(dev);
110 	int num_tc_x_num_ch;
111 	int txq_ix;
112 	int up = 0;
113 	int ch_ix;
114 
115 	/* Sync with mlx5e_update_num_tc_x_num_ch - avoid refetching. */
116 	num_tc_x_num_ch = READ_ONCE(priv->num_tc_x_num_ch);
117 	if (unlikely(dev->real_num_tx_queues > num_tc_x_num_ch)) {
118 		struct mlx5e_ptp *ptp_channel;
119 
120 		/* Order maj_id before defcls - pairs with mlx5e_htb_root_add. */
121 		u16 htb_maj_id = smp_load_acquire(&priv->htb.maj_id);
122 
123 		if (unlikely(htb_maj_id)) {
124 			txq_ix = mlx5e_select_htb_queue(priv, skb, htb_maj_id);
125 			if (txq_ix > 0)
126 				return txq_ix;
127 		}
128 
129 		ptp_channel = READ_ONCE(priv->channels.ptp);
130 		if (unlikely(ptp_channel &&
131 			     test_bit(MLX5E_PTP_STATE_TX, ptp_channel->state) &&
132 			     mlx5e_use_ptpsq(skb)))
133 			return mlx5e_select_ptpsq(dev, skb);
134 
135 		txq_ix = netdev_pick_tx(dev, skb, NULL);
136 		/* Fix netdev_pick_tx() not to choose ptp_channel and HTB txqs.
137 		 * If they are selected, switch to regular queues.
138 		 * Driver to select these queues only at mlx5e_select_ptpsq()
139 		 * and mlx5e_select_htb_queue().
140 		 */
141 		if (unlikely(txq_ix >= num_tc_x_num_ch))
142 			txq_ix %= num_tc_x_num_ch;
143 	} else {
144 		txq_ix = netdev_pick_tx(dev, skb, NULL);
145 	}
146 
147 	if (!netdev_get_num_tc(dev))
148 		return txq_ix;
149 
150 #ifdef CONFIG_MLX5_CORE_EN_DCB
151 	if (priv->dcbx_dp.trust_state == MLX5_QPTS_TRUST_DSCP)
152 		up = mlx5e_get_dscp_up(priv, skb);
153 	else
154 #endif
155 		if (skb_vlan_tag_present(skb))
156 			up = skb_vlan_tag_get_prio(skb);
157 
158 	/* Normalize any picked txq_ix to [0, num_channels),
159 	 * So we can return a txq_ix that matches the channel and
160 	 * packet UP.
161 	 */
162 	ch_ix = priv->txq2sq[txq_ix]->ch_ix;
163 
164 	return priv->channel_tc2realtxq[ch_ix][up];
165 }
166 
mlx5e_skb_l2_header_offset(struct sk_buff * skb)167 static inline int mlx5e_skb_l2_header_offset(struct sk_buff *skb)
168 {
169 #define MLX5E_MIN_INLINE (ETH_HLEN + VLAN_HLEN)
170 
171 	return max(skb_network_offset(skb), MLX5E_MIN_INLINE);
172 }
173 
mlx5e_skb_l3_header_offset(struct sk_buff * skb)174 static inline int mlx5e_skb_l3_header_offset(struct sk_buff *skb)
175 {
176 	if (skb_transport_header_was_set(skb))
177 		return skb_transport_offset(skb);
178 	else
179 		return mlx5e_skb_l2_header_offset(skb);
180 }
181 
mlx5e_calc_min_inline(enum mlx5_inline_modes mode,struct sk_buff * skb)182 static inline u16 mlx5e_calc_min_inline(enum mlx5_inline_modes mode,
183 					struct sk_buff *skb)
184 {
185 	u16 hlen;
186 
187 	switch (mode) {
188 	case MLX5_INLINE_MODE_NONE:
189 		return 0;
190 	case MLX5_INLINE_MODE_TCP_UDP:
191 		hlen = eth_get_headlen(skb->dev, skb->data, skb_headlen(skb));
192 		if (hlen == ETH_HLEN && !skb_vlan_tag_present(skb))
193 			hlen += VLAN_HLEN;
194 		break;
195 	case MLX5_INLINE_MODE_IP:
196 		hlen = mlx5e_skb_l3_header_offset(skb);
197 		break;
198 	case MLX5_INLINE_MODE_L2:
199 	default:
200 		hlen = mlx5e_skb_l2_header_offset(skb);
201 	}
202 	return min_t(u16, hlen, skb_headlen(skb));
203 }
204 
mlx5e_insert_vlan(void * start,struct sk_buff * skb,u16 ihs)205 static inline void mlx5e_insert_vlan(void *start, struct sk_buff *skb, u16 ihs)
206 {
207 	struct vlan_ethhdr *vhdr = (struct vlan_ethhdr *)start;
208 	int cpy1_sz = 2 * ETH_ALEN;
209 	int cpy2_sz = ihs - cpy1_sz;
210 
211 	memcpy(vhdr, skb->data, cpy1_sz);
212 	vhdr->h_vlan_proto = skb->vlan_proto;
213 	vhdr->h_vlan_TCI = cpu_to_be16(skb_vlan_tag_get(skb));
214 	memcpy(&vhdr->h_vlan_encapsulated_proto, skb->data + cpy1_sz, cpy2_sz);
215 }
216 
217 static inline void
mlx5e_txwqe_build_eseg_csum(struct mlx5e_txqsq * sq,struct sk_buff * skb,struct mlx5e_accel_tx_state * accel,struct mlx5_wqe_eth_seg * eseg)218 mlx5e_txwqe_build_eseg_csum(struct mlx5e_txqsq *sq, struct sk_buff *skb,
219 			    struct mlx5e_accel_tx_state *accel,
220 			    struct mlx5_wqe_eth_seg *eseg)
221 {
222 	if (unlikely(mlx5e_ipsec_txwqe_build_eseg_csum(sq, skb, eseg)))
223 		return;
224 
225 	if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
226 		eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM;
227 		if (skb->encapsulation) {
228 			eseg->cs_flags |= MLX5_ETH_WQE_L3_INNER_CSUM |
229 					  MLX5_ETH_WQE_L4_INNER_CSUM;
230 			sq->stats->csum_partial_inner++;
231 		} else {
232 			eseg->cs_flags |= MLX5_ETH_WQE_L4_CSUM;
233 			sq->stats->csum_partial++;
234 		}
235 #ifdef CONFIG_MLX5_EN_TLS
236 	} else if (unlikely(accel && accel->tls.tls_tisn)) {
237 		eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
238 		sq->stats->csum_partial++;
239 #endif
240 	} else
241 		sq->stats->csum_none++;
242 }
243 
244 static inline u16
mlx5e_tx_get_gso_ihs(struct mlx5e_txqsq * sq,struct sk_buff * skb)245 mlx5e_tx_get_gso_ihs(struct mlx5e_txqsq *sq, struct sk_buff *skb)
246 {
247 	struct mlx5e_sq_stats *stats = sq->stats;
248 	u16 ihs;
249 
250 	if (skb->encapsulation) {
251 		ihs = skb_inner_transport_offset(skb) + inner_tcp_hdrlen(skb);
252 		stats->tso_inner_packets++;
253 		stats->tso_inner_bytes += skb->len - ihs;
254 	} else {
255 		if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
256 			ihs = skb_transport_offset(skb) + sizeof(struct udphdr);
257 		else
258 			ihs = skb_transport_offset(skb) + tcp_hdrlen(skb);
259 		stats->tso_packets++;
260 		stats->tso_bytes += skb->len - ihs;
261 	}
262 
263 	return ihs;
264 }
265 
266 static inline int
mlx5e_txwqe_build_dsegs(struct mlx5e_txqsq * sq,struct sk_buff * skb,unsigned char * skb_data,u16 headlen,struct mlx5_wqe_data_seg * dseg)267 mlx5e_txwqe_build_dsegs(struct mlx5e_txqsq *sq, struct sk_buff *skb,
268 			unsigned char *skb_data, u16 headlen,
269 			struct mlx5_wqe_data_seg *dseg)
270 {
271 	dma_addr_t dma_addr = 0;
272 	u8 num_dma          = 0;
273 	int i;
274 
275 	if (headlen) {
276 		dma_addr = dma_map_single(sq->pdev, skb_data, headlen,
277 					  DMA_TO_DEVICE);
278 		if (unlikely(dma_mapping_error(sq->pdev, dma_addr)))
279 			goto dma_unmap_wqe_err;
280 
281 		dseg->addr       = cpu_to_be64(dma_addr);
282 		dseg->lkey       = sq->mkey_be;
283 		dseg->byte_count = cpu_to_be32(headlen);
284 
285 		mlx5e_dma_push(sq, dma_addr, headlen, MLX5E_DMA_MAP_SINGLE);
286 		num_dma++;
287 		dseg++;
288 	}
289 
290 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
291 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
292 		int fsz = skb_frag_size(frag);
293 
294 		dma_addr = skb_frag_dma_map(sq->pdev, frag, 0, fsz,
295 					    DMA_TO_DEVICE);
296 		if (unlikely(dma_mapping_error(sq->pdev, dma_addr)))
297 			goto dma_unmap_wqe_err;
298 
299 		dseg->addr       = cpu_to_be64(dma_addr);
300 		dseg->lkey       = sq->mkey_be;
301 		dseg->byte_count = cpu_to_be32(fsz);
302 
303 		mlx5e_dma_push(sq, dma_addr, fsz, MLX5E_DMA_MAP_PAGE);
304 		num_dma++;
305 		dseg++;
306 	}
307 
308 	return num_dma;
309 
310 dma_unmap_wqe_err:
311 	mlx5e_dma_unmap_wqe_err(sq, num_dma);
312 	return -ENOMEM;
313 }
314 
315 struct mlx5e_tx_attr {
316 	u32 num_bytes;
317 	u16 headlen;
318 	u16 ihs;
319 	__be16 mss;
320 	u16 insz;
321 	u8 opcode;
322 };
323 
324 struct mlx5e_tx_wqe_attr {
325 	u16 ds_cnt;
326 	u16 ds_cnt_inl;
327 	u16 ds_cnt_ids;
328 	u8 num_wqebbs;
329 };
330 
331 static u8
mlx5e_tx_wqe_inline_mode(struct mlx5e_txqsq * sq,struct sk_buff * skb,struct mlx5e_accel_tx_state * accel)332 mlx5e_tx_wqe_inline_mode(struct mlx5e_txqsq *sq, struct sk_buff *skb,
333 			 struct mlx5e_accel_tx_state *accel)
334 {
335 	u8 mode;
336 
337 #ifdef CONFIG_MLX5_EN_TLS
338 	if (accel && accel->tls.tls_tisn)
339 		return MLX5_INLINE_MODE_TCP_UDP;
340 #endif
341 
342 	mode = sq->min_inline_mode;
343 
344 	if (skb_vlan_tag_present(skb) &&
345 	    test_bit(MLX5E_SQ_STATE_VLAN_NEED_L2_INLINE, &sq->state))
346 		mode = max_t(u8, MLX5_INLINE_MODE_L2, mode);
347 
348 	return mode;
349 }
350 
mlx5e_sq_xmit_prepare(struct mlx5e_txqsq * sq,struct sk_buff * skb,struct mlx5e_accel_tx_state * accel,struct mlx5e_tx_attr * attr)351 static void mlx5e_sq_xmit_prepare(struct mlx5e_txqsq *sq, struct sk_buff *skb,
352 				  struct mlx5e_accel_tx_state *accel,
353 				  struct mlx5e_tx_attr *attr)
354 {
355 	struct mlx5e_sq_stats *stats = sq->stats;
356 
357 	if (skb_is_gso(skb)) {
358 		u16 ihs = mlx5e_tx_get_gso_ihs(sq, skb);
359 
360 		*attr = (struct mlx5e_tx_attr) {
361 			.opcode    = MLX5_OPCODE_LSO,
362 			.mss       = cpu_to_be16(skb_shinfo(skb)->gso_size),
363 			.ihs       = ihs,
364 			.num_bytes = skb->len + (skb_shinfo(skb)->gso_segs - 1) * ihs,
365 			.headlen   = skb_headlen(skb) - ihs,
366 		};
367 
368 		stats->packets += skb_shinfo(skb)->gso_segs;
369 	} else {
370 		u8 mode = mlx5e_tx_wqe_inline_mode(sq, skb, accel);
371 		u16 ihs = mlx5e_calc_min_inline(mode, skb);
372 
373 		*attr = (struct mlx5e_tx_attr) {
374 			.opcode    = MLX5_OPCODE_SEND,
375 			.mss       = cpu_to_be16(0),
376 			.ihs       = ihs,
377 			.num_bytes = max_t(unsigned int, skb->len, ETH_ZLEN),
378 			.headlen   = skb_headlen(skb) - ihs,
379 		};
380 
381 		stats->packets++;
382 	}
383 
384 	attr->insz = mlx5e_accel_tx_ids_len(sq, accel);
385 	stats->bytes += attr->num_bytes;
386 }
387 
mlx5e_sq_calc_wqe_attr(struct sk_buff * skb,const struct mlx5e_tx_attr * attr,struct mlx5e_tx_wqe_attr * wqe_attr)388 static void mlx5e_sq_calc_wqe_attr(struct sk_buff *skb, const struct mlx5e_tx_attr *attr,
389 				   struct mlx5e_tx_wqe_attr *wqe_attr)
390 {
391 	u16 ds_cnt = MLX5E_TX_WQE_EMPTY_DS_COUNT;
392 	u16 ds_cnt_inl = 0;
393 	u16 ds_cnt_ids = 0;
394 
395 	if (attr->insz)
396 		ds_cnt_ids = DIV_ROUND_UP(sizeof(struct mlx5_wqe_inline_seg) + attr->insz,
397 					  MLX5_SEND_WQE_DS);
398 
399 	ds_cnt += !!attr->headlen + skb_shinfo(skb)->nr_frags + ds_cnt_ids;
400 	if (attr->ihs) {
401 		u16 inl = attr->ihs - INL_HDR_START_SZ;
402 
403 		if (skb_vlan_tag_present(skb))
404 			inl += VLAN_HLEN;
405 
406 		ds_cnt_inl = DIV_ROUND_UP(inl, MLX5_SEND_WQE_DS);
407 		ds_cnt += ds_cnt_inl;
408 	}
409 
410 	*wqe_attr = (struct mlx5e_tx_wqe_attr) {
411 		.ds_cnt     = ds_cnt,
412 		.ds_cnt_inl = ds_cnt_inl,
413 		.ds_cnt_ids = ds_cnt_ids,
414 		.num_wqebbs = DIV_ROUND_UP(ds_cnt, MLX5_SEND_WQEBB_NUM_DS),
415 	};
416 }
417 
mlx5e_tx_skb_update_hwts_flags(struct sk_buff * skb)418 static void mlx5e_tx_skb_update_hwts_flags(struct sk_buff *skb)
419 {
420 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
421 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
422 }
423 
mlx5e_tx_check_stop(struct mlx5e_txqsq * sq)424 static void mlx5e_tx_check_stop(struct mlx5e_txqsq *sq)
425 {
426 	if (unlikely(!mlx5e_wqc_has_room_for(&sq->wq, sq->cc, sq->pc, sq->stop_room))) {
427 		netif_tx_stop_queue(sq->txq);
428 		sq->stats->stopped++;
429 	}
430 }
431 
mlx5e_tx_flush(struct mlx5e_txqsq * sq)432 static void mlx5e_tx_flush(struct mlx5e_txqsq *sq)
433 {
434 	struct mlx5e_tx_wqe_info *wi;
435 	struct mlx5e_tx_wqe *wqe;
436 	u16 pi;
437 
438 	/* Must not be called when a MPWQE session is active but empty. */
439 	mlx5e_tx_mpwqe_ensure_complete(sq);
440 
441 	pi = mlx5_wq_cyc_ctr2ix(&sq->wq, sq->pc);
442 	wi = &sq->db.wqe_info[pi];
443 
444 	*wi = (struct mlx5e_tx_wqe_info) {
445 		.num_wqebbs = 1,
446 	};
447 
448 	wqe = mlx5e_post_nop(&sq->wq, sq->sqn, &sq->pc);
449 	mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, &wqe->ctrl);
450 }
451 
452 static inline void
mlx5e_txwqe_complete(struct mlx5e_txqsq * sq,struct sk_buff * skb,const struct mlx5e_tx_attr * attr,const struct mlx5e_tx_wqe_attr * wqe_attr,u8 num_dma,struct mlx5e_tx_wqe_info * wi,struct mlx5_wqe_ctrl_seg * cseg,bool xmit_more)453 mlx5e_txwqe_complete(struct mlx5e_txqsq *sq, struct sk_buff *skb,
454 		     const struct mlx5e_tx_attr *attr,
455 		     const struct mlx5e_tx_wqe_attr *wqe_attr, u8 num_dma,
456 		     struct mlx5e_tx_wqe_info *wi, struct mlx5_wqe_ctrl_seg *cseg,
457 		     bool xmit_more)
458 {
459 	struct mlx5_wq_cyc *wq = &sq->wq;
460 	bool send_doorbell;
461 
462 	*wi = (struct mlx5e_tx_wqe_info) {
463 		.skb = skb,
464 		.num_bytes = attr->num_bytes,
465 		.num_dma = num_dma,
466 		.num_wqebbs = wqe_attr->num_wqebbs,
467 		.num_fifo_pkts = 0,
468 	};
469 
470 	cseg->opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | attr->opcode);
471 	cseg->qpn_ds           = cpu_to_be32((sq->sqn << 8) | wqe_attr->ds_cnt);
472 
473 	mlx5e_tx_skb_update_hwts_flags(skb);
474 
475 	sq->pc += wi->num_wqebbs;
476 
477 	mlx5e_tx_check_stop(sq);
478 
479 	if (unlikely(sq->ptpsq)) {
480 		mlx5e_skb_cb_hwtstamp_init(skb);
481 		mlx5e_skb_fifo_push(&sq->ptpsq->skb_fifo, skb);
482 		if (!netif_tx_queue_stopped(sq->txq) &&
483 		    !mlx5e_skb_fifo_has_room(&sq->ptpsq->skb_fifo)) {
484 			netif_tx_stop_queue(sq->txq);
485 			sq->stats->stopped++;
486 		}
487 		skb_get(skb);
488 	}
489 
490 	send_doorbell = __netdev_tx_sent_queue(sq->txq, attr->num_bytes, xmit_more);
491 	if (send_doorbell)
492 		mlx5e_notify_hw(wq, sq->pc, sq->uar_map, cseg);
493 }
494 
495 static void
mlx5e_sq_xmit_wqe(struct mlx5e_txqsq * sq,struct sk_buff * skb,const struct mlx5e_tx_attr * attr,const struct mlx5e_tx_wqe_attr * wqe_attr,struct mlx5e_tx_wqe * wqe,u16 pi,bool xmit_more)496 mlx5e_sq_xmit_wqe(struct mlx5e_txqsq *sq, struct sk_buff *skb,
497 		  const struct mlx5e_tx_attr *attr, const struct mlx5e_tx_wqe_attr *wqe_attr,
498 		  struct mlx5e_tx_wqe *wqe, u16 pi, bool xmit_more)
499 {
500 	struct mlx5_wqe_ctrl_seg *cseg;
501 	struct mlx5_wqe_eth_seg  *eseg;
502 	struct mlx5_wqe_data_seg *dseg;
503 	struct mlx5e_tx_wqe_info *wi;
504 
505 	struct mlx5e_sq_stats *stats = sq->stats;
506 	int num_dma;
507 
508 	stats->xmit_more += xmit_more;
509 
510 	/* fill wqe */
511 	wi   = &sq->db.wqe_info[pi];
512 	cseg = &wqe->ctrl;
513 	eseg = &wqe->eth;
514 	dseg =  wqe->data;
515 
516 	eseg->mss = attr->mss;
517 
518 	if (attr->ihs) {
519 		if (skb_vlan_tag_present(skb)) {
520 			eseg->inline_hdr.sz |= cpu_to_be16(attr->ihs + VLAN_HLEN);
521 			mlx5e_insert_vlan(eseg->inline_hdr.start, skb, attr->ihs);
522 			stats->added_vlan_packets++;
523 		} else {
524 			eseg->inline_hdr.sz |= cpu_to_be16(attr->ihs);
525 			memcpy(eseg->inline_hdr.start, skb->data, attr->ihs);
526 		}
527 		dseg += wqe_attr->ds_cnt_inl;
528 	} else if (skb_vlan_tag_present(skb)) {
529 		eseg->insert.type = cpu_to_be16(MLX5_ETH_WQE_INSERT_VLAN);
530 		if (skb->vlan_proto == cpu_to_be16(ETH_P_8021AD))
531 			eseg->insert.type |= cpu_to_be16(MLX5_ETH_WQE_SVLAN);
532 		eseg->insert.vlan_tci = cpu_to_be16(skb_vlan_tag_get(skb));
533 		stats->added_vlan_packets++;
534 	}
535 
536 	dseg += wqe_attr->ds_cnt_ids;
537 	num_dma = mlx5e_txwqe_build_dsegs(sq, skb, skb->data + attr->ihs,
538 					  attr->headlen, dseg);
539 	if (unlikely(num_dma < 0))
540 		goto err_drop;
541 
542 	mlx5e_txwqe_complete(sq, skb, attr, wqe_attr, num_dma, wi, cseg, xmit_more);
543 
544 	return;
545 
546 err_drop:
547 	stats->dropped++;
548 	dev_kfree_skb_any(skb);
549 	mlx5e_tx_flush(sq);
550 }
551 
mlx5e_tx_skb_supports_mpwqe(struct sk_buff * skb,struct mlx5e_tx_attr * attr)552 static bool mlx5e_tx_skb_supports_mpwqe(struct sk_buff *skb, struct mlx5e_tx_attr *attr)
553 {
554 	return !skb_is_nonlinear(skb) && !skb_vlan_tag_present(skb) && !attr->ihs &&
555 	       !attr->insz;
556 }
557 
mlx5e_tx_mpwqe_same_eseg(struct mlx5e_txqsq * sq,struct mlx5_wqe_eth_seg * eseg)558 static bool mlx5e_tx_mpwqe_same_eseg(struct mlx5e_txqsq *sq, struct mlx5_wqe_eth_seg *eseg)
559 {
560 	struct mlx5e_tx_mpwqe *session = &sq->mpwqe;
561 
562 	/* Assumes the session is already running and has at least one packet. */
563 	return !memcmp(&session->wqe->eth, eseg, MLX5E_ACCEL_ESEG_LEN);
564 }
565 
mlx5e_tx_mpwqe_session_start(struct mlx5e_txqsq * sq,struct mlx5_wqe_eth_seg * eseg)566 static void mlx5e_tx_mpwqe_session_start(struct mlx5e_txqsq *sq,
567 					 struct mlx5_wqe_eth_seg *eseg)
568 {
569 	struct mlx5e_tx_mpwqe *session = &sq->mpwqe;
570 	struct mlx5e_tx_wqe *wqe;
571 	u16 pi;
572 
573 	pi = mlx5e_txqsq_get_next_pi(sq, MLX5E_TX_MPW_MAX_WQEBBS);
574 	wqe = MLX5E_TX_FETCH_WQE(sq, pi);
575 	net_prefetchw(wqe->data);
576 
577 	*session = (struct mlx5e_tx_mpwqe) {
578 		.wqe = wqe,
579 		.bytes_count = 0,
580 		.ds_count = MLX5E_TX_WQE_EMPTY_DS_COUNT,
581 		.pkt_count = 0,
582 		.inline_on = 0,
583 	};
584 
585 	memcpy(&session->wqe->eth, eseg, MLX5E_ACCEL_ESEG_LEN);
586 
587 	sq->stats->mpwqe_blks++;
588 }
589 
mlx5e_tx_mpwqe_session_is_active(struct mlx5e_txqsq * sq)590 static bool mlx5e_tx_mpwqe_session_is_active(struct mlx5e_txqsq *sq)
591 {
592 	return sq->mpwqe.wqe;
593 }
594 
mlx5e_tx_mpwqe_add_dseg(struct mlx5e_txqsq * sq,struct mlx5e_xmit_data * txd)595 static void mlx5e_tx_mpwqe_add_dseg(struct mlx5e_txqsq *sq, struct mlx5e_xmit_data *txd)
596 {
597 	struct mlx5e_tx_mpwqe *session = &sq->mpwqe;
598 	struct mlx5_wqe_data_seg *dseg;
599 
600 	dseg = (struct mlx5_wqe_data_seg *)session->wqe + session->ds_count;
601 
602 	session->pkt_count++;
603 	session->bytes_count += txd->len;
604 
605 	dseg->addr = cpu_to_be64(txd->dma_addr);
606 	dseg->byte_count = cpu_to_be32(txd->len);
607 	dseg->lkey = sq->mkey_be;
608 	session->ds_count++;
609 
610 	sq->stats->mpwqe_pkts++;
611 }
612 
mlx5e_tx_mpwqe_session_complete(struct mlx5e_txqsq * sq)613 static struct mlx5_wqe_ctrl_seg *mlx5e_tx_mpwqe_session_complete(struct mlx5e_txqsq *sq)
614 {
615 	struct mlx5e_tx_mpwqe *session = &sq->mpwqe;
616 	u8 ds_count = session->ds_count;
617 	struct mlx5_wqe_ctrl_seg *cseg;
618 	struct mlx5e_tx_wqe_info *wi;
619 	u16 pi;
620 
621 	cseg = &session->wqe->ctrl;
622 	cseg->opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | MLX5_OPCODE_ENHANCED_MPSW);
623 	cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_count);
624 
625 	pi = mlx5_wq_cyc_ctr2ix(&sq->wq, sq->pc);
626 	wi = &sq->db.wqe_info[pi];
627 	*wi = (struct mlx5e_tx_wqe_info) {
628 		.skb = NULL,
629 		.num_bytes = session->bytes_count,
630 		.num_wqebbs = DIV_ROUND_UP(ds_count, MLX5_SEND_WQEBB_NUM_DS),
631 		.num_dma = session->pkt_count,
632 		.num_fifo_pkts = session->pkt_count,
633 	};
634 
635 	sq->pc += wi->num_wqebbs;
636 
637 	session->wqe = NULL;
638 
639 	mlx5e_tx_check_stop(sq);
640 
641 	return cseg;
642 }
643 
644 static void
mlx5e_sq_xmit_mpwqe(struct mlx5e_txqsq * sq,struct sk_buff * skb,struct mlx5_wqe_eth_seg * eseg,bool xmit_more)645 mlx5e_sq_xmit_mpwqe(struct mlx5e_txqsq *sq, struct sk_buff *skb,
646 		    struct mlx5_wqe_eth_seg *eseg, bool xmit_more)
647 {
648 	struct mlx5_wqe_ctrl_seg *cseg;
649 	struct mlx5e_xmit_data txd;
650 
651 	txd.data = skb->data;
652 	txd.len = skb->len;
653 
654 	txd.dma_addr = dma_map_single(sq->pdev, txd.data, txd.len, DMA_TO_DEVICE);
655 	if (unlikely(dma_mapping_error(sq->pdev, txd.dma_addr)))
656 		goto err_unmap;
657 
658 	if (!mlx5e_tx_mpwqe_session_is_active(sq)) {
659 		mlx5e_tx_mpwqe_session_start(sq, eseg);
660 	} else if (!mlx5e_tx_mpwqe_same_eseg(sq, eseg)) {
661 		mlx5e_tx_mpwqe_session_complete(sq);
662 		mlx5e_tx_mpwqe_session_start(sq, eseg);
663 	}
664 
665 	sq->stats->xmit_more += xmit_more;
666 
667 	mlx5e_dma_push(sq, txd.dma_addr, txd.len, MLX5E_DMA_MAP_SINGLE);
668 	mlx5e_skb_fifo_push(&sq->db.skb_fifo, skb);
669 	mlx5e_tx_mpwqe_add_dseg(sq, &txd);
670 	mlx5e_tx_skb_update_hwts_flags(skb);
671 
672 	if (unlikely(mlx5e_tx_mpwqe_is_full(&sq->mpwqe))) {
673 		/* Might stop the queue and affect the retval of __netdev_tx_sent_queue. */
674 		cseg = mlx5e_tx_mpwqe_session_complete(sq);
675 
676 		if (__netdev_tx_sent_queue(sq->txq, txd.len, xmit_more))
677 			mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, cseg);
678 	} else if (__netdev_tx_sent_queue(sq->txq, txd.len, xmit_more)) {
679 		/* Might stop the queue, but we were asked to ring the doorbell anyway. */
680 		cseg = mlx5e_tx_mpwqe_session_complete(sq);
681 
682 		mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, cseg);
683 	}
684 
685 	return;
686 
687 err_unmap:
688 	mlx5e_dma_unmap_wqe_err(sq, 1);
689 	sq->stats->dropped++;
690 	dev_kfree_skb_any(skb);
691 	mlx5e_tx_flush(sq);
692 }
693 
mlx5e_tx_mpwqe_ensure_complete(struct mlx5e_txqsq * sq)694 void mlx5e_tx_mpwqe_ensure_complete(struct mlx5e_txqsq *sq)
695 {
696 	/* Unlikely in non-MPWQE workloads; not important in MPWQE workloads. */
697 	if (unlikely(mlx5e_tx_mpwqe_session_is_active(sq)))
698 		mlx5e_tx_mpwqe_session_complete(sq);
699 }
700 
mlx5e_txwqe_build_eseg(struct mlx5e_priv * priv,struct mlx5e_txqsq * sq,struct sk_buff * skb,struct mlx5e_accel_tx_state * accel,struct mlx5_wqe_eth_seg * eseg,u16 ihs)701 static void mlx5e_txwqe_build_eseg(struct mlx5e_priv *priv, struct mlx5e_txqsq *sq,
702 				   struct sk_buff *skb, struct mlx5e_accel_tx_state *accel,
703 				   struct mlx5_wqe_eth_seg *eseg, u16 ihs)
704 {
705 	mlx5e_accel_tx_eseg(priv, skb, eseg, ihs);
706 	mlx5e_txwqe_build_eseg_csum(sq, skb, accel, eseg);
707 }
708 
mlx5e_xmit(struct sk_buff * skb,struct net_device * dev)709 netdev_tx_t mlx5e_xmit(struct sk_buff *skb, struct net_device *dev)
710 {
711 	struct mlx5e_priv *priv = netdev_priv(dev);
712 	struct mlx5e_accel_tx_state accel = {};
713 	struct mlx5e_tx_wqe_attr wqe_attr;
714 	struct mlx5e_tx_attr attr;
715 	struct mlx5e_tx_wqe *wqe;
716 	struct mlx5e_txqsq *sq;
717 	u16 pi;
718 
719 	sq = priv->txq2sq[skb_get_queue_mapping(skb)];
720 	if (unlikely(!sq)) {
721 		dev_kfree_skb_any(skb);
722 		return NETDEV_TX_OK;
723 	}
724 
725 	/* May send SKBs and WQEs. */
726 	if (unlikely(!mlx5e_accel_tx_begin(dev, sq, skb, &accel)))
727 		return NETDEV_TX_OK;
728 
729 	mlx5e_sq_xmit_prepare(sq, skb, &accel, &attr);
730 
731 	if (test_bit(MLX5E_SQ_STATE_MPWQE, &sq->state)) {
732 		if (mlx5e_tx_skb_supports_mpwqe(skb, &attr)) {
733 			struct mlx5_wqe_eth_seg eseg = {};
734 
735 			mlx5e_txwqe_build_eseg(priv, sq, skb, &accel, &eseg, attr.ihs);
736 			mlx5e_sq_xmit_mpwqe(sq, skb, &eseg, netdev_xmit_more());
737 			return NETDEV_TX_OK;
738 		}
739 
740 		mlx5e_tx_mpwqe_ensure_complete(sq);
741 	}
742 
743 	mlx5e_sq_calc_wqe_attr(skb, &attr, &wqe_attr);
744 	pi = mlx5e_txqsq_get_next_pi(sq, wqe_attr.num_wqebbs);
745 	wqe = MLX5E_TX_FETCH_WQE(sq, pi);
746 
747 	/* May update the WQE, but may not post other WQEs. */
748 	mlx5e_accel_tx_finish(sq, wqe, &accel,
749 			      (struct mlx5_wqe_inline_seg *)(wqe->data + wqe_attr.ds_cnt_inl));
750 	mlx5e_txwqe_build_eseg(priv, sq, skb, &accel, &wqe->eth, attr.ihs);
751 	mlx5e_sq_xmit_wqe(sq, skb, &attr, &wqe_attr, wqe, pi, netdev_xmit_more());
752 
753 	return NETDEV_TX_OK;
754 }
755 
mlx5e_sq_xmit_simple(struct mlx5e_txqsq * sq,struct sk_buff * skb,bool xmit_more)756 void mlx5e_sq_xmit_simple(struct mlx5e_txqsq *sq, struct sk_buff *skb, bool xmit_more)
757 {
758 	struct mlx5e_tx_wqe_attr wqe_attr;
759 	struct mlx5e_tx_attr attr;
760 	struct mlx5e_tx_wqe *wqe;
761 	u16 pi;
762 
763 	mlx5e_sq_xmit_prepare(sq, skb, NULL, &attr);
764 	mlx5e_sq_calc_wqe_attr(skb, &attr, &wqe_attr);
765 	pi = mlx5e_txqsq_get_next_pi(sq, wqe_attr.num_wqebbs);
766 	wqe = MLX5E_TX_FETCH_WQE(sq, pi);
767 	mlx5e_txwqe_build_eseg_csum(sq, skb, NULL, &wqe->eth);
768 	mlx5e_sq_xmit_wqe(sq, skb, &attr, &wqe_attr, wqe, pi, xmit_more);
769 }
770 
mlx5e_tx_wi_dma_unmap(struct mlx5e_txqsq * sq,struct mlx5e_tx_wqe_info * wi,u32 * dma_fifo_cc)771 static void mlx5e_tx_wi_dma_unmap(struct mlx5e_txqsq *sq, struct mlx5e_tx_wqe_info *wi,
772 				  u32 *dma_fifo_cc)
773 {
774 	int i;
775 
776 	for (i = 0; i < wi->num_dma; i++) {
777 		struct mlx5e_sq_dma *dma = mlx5e_dma_get(sq, (*dma_fifo_cc)++);
778 
779 		mlx5e_tx_dma_unmap(sq->pdev, dma);
780 	}
781 }
782 
mlx5e_consume_skb(struct mlx5e_txqsq * sq,struct sk_buff * skb,struct mlx5_cqe64 * cqe,int napi_budget)783 static void mlx5e_consume_skb(struct mlx5e_txqsq *sq, struct sk_buff *skb,
784 			      struct mlx5_cqe64 *cqe, int napi_budget)
785 {
786 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
787 		struct skb_shared_hwtstamps hwts = {};
788 		u64 ts = get_cqe_ts(cqe);
789 
790 		hwts.hwtstamp = mlx5e_cqe_ts_to_ns(sq->ptp_cyc2time, sq->clock, ts);
791 		if (sq->ptpsq)
792 			mlx5e_skb_cb_hwtstamp_handler(skb, MLX5E_SKB_CB_CQE_HWTSTAMP,
793 						      hwts.hwtstamp, sq->ptpsq->cq_stats);
794 		else
795 			skb_tstamp_tx(skb, &hwts);
796 	}
797 
798 	napi_consume_skb(skb, napi_budget);
799 }
800 
mlx5e_tx_wi_consume_fifo_skbs(struct mlx5e_txqsq * sq,struct mlx5e_tx_wqe_info * wi,struct mlx5_cqe64 * cqe,int napi_budget)801 static void mlx5e_tx_wi_consume_fifo_skbs(struct mlx5e_txqsq *sq, struct mlx5e_tx_wqe_info *wi,
802 					  struct mlx5_cqe64 *cqe, int napi_budget)
803 {
804 	int i;
805 
806 	for (i = 0; i < wi->num_fifo_pkts; i++) {
807 		struct sk_buff *skb = mlx5e_skb_fifo_pop(&sq->db.skb_fifo);
808 
809 		mlx5e_consume_skb(sq, skb, cqe, napi_budget);
810 	}
811 }
812 
mlx5e_txqsq_wake(struct mlx5e_txqsq * sq)813 void mlx5e_txqsq_wake(struct mlx5e_txqsq *sq)
814 {
815 	if (netif_tx_queue_stopped(sq->txq) &&
816 	    mlx5e_wqc_has_room_for(&sq->wq, sq->cc, sq->pc, sq->stop_room) &&
817 	    mlx5e_ptpsq_fifo_has_room(sq) &&
818 	    !test_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state)) {
819 		netif_tx_wake_queue(sq->txq);
820 		sq->stats->wake++;
821 	}
822 }
823 
mlx5e_poll_tx_cq(struct mlx5e_cq * cq,int napi_budget)824 bool mlx5e_poll_tx_cq(struct mlx5e_cq *cq, int napi_budget)
825 {
826 	struct mlx5e_sq_stats *stats;
827 	struct mlx5e_txqsq *sq;
828 	struct mlx5_cqe64 *cqe;
829 	u32 dma_fifo_cc;
830 	u32 nbytes;
831 	u16 npkts;
832 	u16 sqcc;
833 	int i;
834 
835 	sq = container_of(cq, struct mlx5e_txqsq, cq);
836 
837 	if (unlikely(!test_bit(MLX5E_SQ_STATE_ENABLED, &sq->state)))
838 		return false;
839 
840 	cqe = mlx5_cqwq_get_cqe(&cq->wq);
841 	if (!cqe)
842 		return false;
843 
844 	stats = sq->stats;
845 
846 	npkts = 0;
847 	nbytes = 0;
848 
849 	/* sq->cc must be updated only after mlx5_cqwq_update_db_record(),
850 	 * otherwise a cq overrun may occur
851 	 */
852 	sqcc = sq->cc;
853 
854 	/* avoid dirtying sq cache line every cqe */
855 	dma_fifo_cc = sq->dma_fifo_cc;
856 
857 	i = 0;
858 	do {
859 		struct mlx5e_tx_wqe_info *wi;
860 		u16 wqe_counter;
861 		bool last_wqe;
862 		u16 ci;
863 
864 		mlx5_cqwq_pop(&cq->wq);
865 
866 		wqe_counter = be16_to_cpu(cqe->wqe_counter);
867 
868 		do {
869 			last_wqe = (sqcc == wqe_counter);
870 
871 			ci = mlx5_wq_cyc_ctr2ix(&sq->wq, sqcc);
872 			wi = &sq->db.wqe_info[ci];
873 
874 			sqcc += wi->num_wqebbs;
875 
876 			if (likely(wi->skb)) {
877 				mlx5e_tx_wi_dma_unmap(sq, wi, &dma_fifo_cc);
878 				mlx5e_consume_skb(sq, wi->skb, cqe, napi_budget);
879 
880 				npkts++;
881 				nbytes += wi->num_bytes;
882 				continue;
883 			}
884 
885 			if (unlikely(mlx5e_ktls_tx_try_handle_resync_dump_comp(sq, wi,
886 									       &dma_fifo_cc)))
887 				continue;
888 
889 			if (wi->num_fifo_pkts) {
890 				mlx5e_tx_wi_dma_unmap(sq, wi, &dma_fifo_cc);
891 				mlx5e_tx_wi_consume_fifo_skbs(sq, wi, cqe, napi_budget);
892 
893 				npkts += wi->num_fifo_pkts;
894 				nbytes += wi->num_bytes;
895 			}
896 		} while (!last_wqe);
897 
898 		if (unlikely(get_cqe_opcode(cqe) == MLX5_CQE_REQ_ERR)) {
899 			if (!test_and_set_bit(MLX5E_SQ_STATE_RECOVERING,
900 					      &sq->state)) {
901 				mlx5e_dump_error_cqe(&sq->cq, sq->sqn,
902 						     (struct mlx5_err_cqe *)cqe);
903 				mlx5_wq_cyc_wqe_dump(&sq->wq, ci, wi->num_wqebbs);
904 				queue_work(cq->priv->wq, &sq->recover_work);
905 			}
906 			stats->cqe_err++;
907 		}
908 
909 	} while ((++i < MLX5E_TX_CQ_POLL_BUDGET) && (cqe = mlx5_cqwq_get_cqe(&cq->wq)));
910 
911 	stats->cqes += i;
912 
913 	mlx5_cqwq_update_db_record(&cq->wq);
914 
915 	/* ensure cq space is freed before enabling more cqes */
916 	wmb();
917 
918 	sq->dma_fifo_cc = dma_fifo_cc;
919 	sq->cc = sqcc;
920 
921 	netdev_tx_completed_queue(sq->txq, npkts, nbytes);
922 
923 	mlx5e_txqsq_wake(sq);
924 
925 	return (i == MLX5E_TX_CQ_POLL_BUDGET);
926 }
927 
mlx5e_tx_wi_kfree_fifo_skbs(struct mlx5e_txqsq * sq,struct mlx5e_tx_wqe_info * wi)928 static void mlx5e_tx_wi_kfree_fifo_skbs(struct mlx5e_txqsq *sq, struct mlx5e_tx_wqe_info *wi)
929 {
930 	int i;
931 
932 	for (i = 0; i < wi->num_fifo_pkts; i++)
933 		dev_kfree_skb_any(mlx5e_skb_fifo_pop(&sq->db.skb_fifo));
934 }
935 
mlx5e_free_txqsq_descs(struct mlx5e_txqsq * sq)936 void mlx5e_free_txqsq_descs(struct mlx5e_txqsq *sq)
937 {
938 	struct mlx5e_tx_wqe_info *wi;
939 	u32 dma_fifo_cc, nbytes = 0;
940 	u16 ci, sqcc, npkts = 0;
941 
942 	sqcc = sq->cc;
943 	dma_fifo_cc = sq->dma_fifo_cc;
944 
945 	while (sqcc != sq->pc) {
946 		ci = mlx5_wq_cyc_ctr2ix(&sq->wq, sqcc);
947 		wi = &sq->db.wqe_info[ci];
948 
949 		sqcc += wi->num_wqebbs;
950 
951 		if (likely(wi->skb)) {
952 			mlx5e_tx_wi_dma_unmap(sq, wi, &dma_fifo_cc);
953 			dev_kfree_skb_any(wi->skb);
954 
955 			npkts++;
956 			nbytes += wi->num_bytes;
957 			continue;
958 		}
959 
960 		if (unlikely(mlx5e_ktls_tx_try_handle_resync_dump_comp(sq, wi, &dma_fifo_cc)))
961 			continue;
962 
963 		if (wi->num_fifo_pkts) {
964 			mlx5e_tx_wi_dma_unmap(sq, wi, &dma_fifo_cc);
965 			mlx5e_tx_wi_kfree_fifo_skbs(sq, wi);
966 
967 			npkts += wi->num_fifo_pkts;
968 			nbytes += wi->num_bytes;
969 		}
970 	}
971 
972 	sq->dma_fifo_cc = dma_fifo_cc;
973 	sq->cc = sqcc;
974 
975 	netdev_tx_completed_queue(sq->txq, npkts, nbytes);
976 }
977 
978 #ifdef CONFIG_MLX5_CORE_IPOIB
979 static inline void
mlx5i_txwqe_build_datagram(struct mlx5_av * av,u32 dqpn,u32 dqkey,struct mlx5_wqe_datagram_seg * dseg)980 mlx5i_txwqe_build_datagram(struct mlx5_av *av, u32 dqpn, u32 dqkey,
981 			   struct mlx5_wqe_datagram_seg *dseg)
982 {
983 	memcpy(&dseg->av, av, sizeof(struct mlx5_av));
984 	dseg->av.dqp_dct = cpu_to_be32(dqpn | MLX5_EXTENDED_UD_AV);
985 	dseg->av.key.qkey.qkey = cpu_to_be32(dqkey);
986 }
987 
mlx5i_sq_calc_wqe_attr(struct sk_buff * skb,const struct mlx5e_tx_attr * attr,struct mlx5e_tx_wqe_attr * wqe_attr)988 static void mlx5i_sq_calc_wqe_attr(struct sk_buff *skb,
989 				   const struct mlx5e_tx_attr *attr,
990 				   struct mlx5e_tx_wqe_attr *wqe_attr)
991 {
992 	u16 ds_cnt = sizeof(struct mlx5i_tx_wqe) / MLX5_SEND_WQE_DS;
993 	u16 ds_cnt_inl = 0;
994 
995 	ds_cnt += !!attr->headlen + skb_shinfo(skb)->nr_frags;
996 
997 	if (attr->ihs) {
998 		u16 inl = attr->ihs - INL_HDR_START_SZ;
999 
1000 		ds_cnt_inl = DIV_ROUND_UP(inl, MLX5_SEND_WQE_DS);
1001 		ds_cnt += ds_cnt_inl;
1002 	}
1003 
1004 	*wqe_attr = (struct mlx5e_tx_wqe_attr) {
1005 		.ds_cnt     = ds_cnt,
1006 		.ds_cnt_inl = ds_cnt_inl,
1007 		.num_wqebbs = DIV_ROUND_UP(ds_cnt, MLX5_SEND_WQEBB_NUM_DS),
1008 	};
1009 }
1010 
mlx5i_sq_xmit(struct mlx5e_txqsq * sq,struct sk_buff * skb,struct mlx5_av * av,u32 dqpn,u32 dqkey,bool xmit_more)1011 void mlx5i_sq_xmit(struct mlx5e_txqsq *sq, struct sk_buff *skb,
1012 		   struct mlx5_av *av, u32 dqpn, u32 dqkey, bool xmit_more)
1013 {
1014 	struct mlx5e_tx_wqe_attr wqe_attr;
1015 	struct mlx5e_tx_attr attr;
1016 	struct mlx5i_tx_wqe *wqe;
1017 
1018 	struct mlx5_wqe_datagram_seg *datagram;
1019 	struct mlx5_wqe_ctrl_seg *cseg;
1020 	struct mlx5_wqe_eth_seg  *eseg;
1021 	struct mlx5_wqe_data_seg *dseg;
1022 	struct mlx5e_tx_wqe_info *wi;
1023 
1024 	struct mlx5e_sq_stats *stats = sq->stats;
1025 	int num_dma;
1026 	u16 pi;
1027 
1028 	mlx5e_sq_xmit_prepare(sq, skb, NULL, &attr);
1029 	mlx5i_sq_calc_wqe_attr(skb, &attr, &wqe_attr);
1030 
1031 	pi = mlx5e_txqsq_get_next_pi(sq, wqe_attr.num_wqebbs);
1032 	wqe = MLX5I_SQ_FETCH_WQE(sq, pi);
1033 
1034 	stats->xmit_more += xmit_more;
1035 
1036 	/* fill wqe */
1037 	wi       = &sq->db.wqe_info[pi];
1038 	cseg     = &wqe->ctrl;
1039 	datagram = &wqe->datagram;
1040 	eseg     = &wqe->eth;
1041 	dseg     =  wqe->data;
1042 
1043 	mlx5i_txwqe_build_datagram(av, dqpn, dqkey, datagram);
1044 
1045 	mlx5e_txwqe_build_eseg_csum(sq, skb, NULL, eseg);
1046 
1047 	eseg->mss = attr.mss;
1048 
1049 	if (attr.ihs) {
1050 		memcpy(eseg->inline_hdr.start, skb->data, attr.ihs);
1051 		eseg->inline_hdr.sz = cpu_to_be16(attr.ihs);
1052 		dseg += wqe_attr.ds_cnt_inl;
1053 	}
1054 
1055 	num_dma = mlx5e_txwqe_build_dsegs(sq, skb, skb->data + attr.ihs,
1056 					  attr.headlen, dseg);
1057 	if (unlikely(num_dma < 0))
1058 		goto err_drop;
1059 
1060 	mlx5e_txwqe_complete(sq, skb, &attr, &wqe_attr, num_dma, wi, cseg, xmit_more);
1061 
1062 	return;
1063 
1064 err_drop:
1065 	stats->dropped++;
1066 	dev_kfree_skb_any(skb);
1067 	mlx5e_tx_flush(sq);
1068 }
1069 #endif
1070