2 * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/tcp.h>
34 #include <linux/if_vlan.h>
35 #include <net/geneve.h>
36 #include <net/dsfield.h>
39 #include "ipoib/ipoib.h"
40 #include "en_accel/en_accel.h"
41 #include "en_accel/ipsec_rxtx.h"
45 static void mlx5e_dma_unmap_wqe_err(struct mlx5e_txqsq *sq, u8 num_dma)
49 for (i = 0; i < num_dma; i++) {
50 struct mlx5e_sq_dma *last_pushed_dma =
51 mlx5e_dma_get(sq, --sq->dma_fifo_pc);
53 mlx5e_tx_dma_unmap(sq->pdev, last_pushed_dma);
57 static inline int mlx5e_skb_l2_header_offset(struct sk_buff *skb)
59 #define MLX5E_MIN_INLINE (ETH_HLEN + VLAN_HLEN)
61 return max(skb_network_offset(skb), MLX5E_MIN_INLINE);
64 static inline int mlx5e_skb_l3_header_offset(struct sk_buff *skb)
66 if (skb_transport_header_was_set(skb))
67 return skb_transport_offset(skb);
69 return mlx5e_skb_l2_header_offset(skb);
72 static inline u16 mlx5e_calc_min_inline(enum mlx5_inline_modes mode,
78 case MLX5_INLINE_MODE_NONE:
80 case MLX5_INLINE_MODE_TCP_UDP:
81 hlen = eth_get_headlen(skb->dev, skb->data, skb_headlen(skb));
82 if (hlen == ETH_HLEN && !skb_vlan_tag_present(skb))
85 case MLX5_INLINE_MODE_IP:
86 hlen = mlx5e_skb_l3_header_offset(skb);
88 case MLX5_INLINE_MODE_L2:
90 hlen = mlx5e_skb_l2_header_offset(skb);
92 return min_t(u16, hlen, skb_headlen(skb));
95 #define MLX5_UNSAFE_MEMCPY_DISCLAIMER \
96 "This copy has been bounds-checked earlier in " \
97 "mlx5i_sq_calc_wqe_attr() and intentionally " \
98 "crosses a flex array boundary. Since it is " \
99 "performance sensitive, splitting the copy is " \
102 static inline void mlx5e_insert_vlan(void *start, struct sk_buff *skb, u16 ihs)
104 struct vlan_ethhdr *vhdr = (struct vlan_ethhdr *)start;
105 int cpy1_sz = 2 * ETH_ALEN;
106 int cpy2_sz = ihs - cpy1_sz;
108 memcpy(&vhdr->addrs, skb->data, cpy1_sz);
109 vhdr->h_vlan_proto = skb->vlan_proto;
110 vhdr->h_vlan_TCI = cpu_to_be16(skb_vlan_tag_get(skb));
111 unsafe_memcpy(&vhdr->h_vlan_encapsulated_proto,
114 MLX5_UNSAFE_MEMCPY_DISCLAIMER);
118 mlx5e_txwqe_build_eseg_csum(struct mlx5e_txqsq *sq, struct sk_buff *skb,
119 struct mlx5e_accel_tx_state *accel,
120 struct mlx5_wqe_eth_seg *eseg)
122 if (unlikely(mlx5e_ipsec_txwqe_build_eseg_csum(sq, skb, eseg)))
125 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
126 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM;
127 if (skb->encapsulation) {
128 eseg->cs_flags |= MLX5_ETH_WQE_L3_INNER_CSUM |
129 MLX5_ETH_WQE_L4_INNER_CSUM;
130 sq->stats->csum_partial_inner++;
132 eseg->cs_flags |= MLX5_ETH_WQE_L4_CSUM;
133 sq->stats->csum_partial++;
135 #ifdef CONFIG_MLX5_EN_TLS
136 } else if (unlikely(accel && accel->tls.tls_tisn)) {
137 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
138 sq->stats->csum_partial++;
141 sq->stats->csum_none++;
144 /* Returns the number of header bytes that we plan
145 * to inline later in the transmit descriptor
148 mlx5e_tx_get_gso_ihs(struct mlx5e_txqsq *sq, struct sk_buff *skb, int *hopbyhop)
150 struct mlx5e_sq_stats *stats = sq->stats;
154 if (skb->encapsulation) {
155 ihs = skb_tcp_all_headers(skb);
156 stats->tso_inner_packets++;
157 stats->tso_inner_bytes += skb->len - ihs;
159 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
160 ihs = skb_transport_offset(skb) + sizeof(struct udphdr);
162 ihs = skb_tcp_all_headers(skb);
163 if (ipv6_has_hopopt_jumbo(skb)) {
164 *hopbyhop = sizeof(struct hop_jumbo_hdr);
165 ihs -= sizeof(struct hop_jumbo_hdr);
168 stats->tso_packets++;
169 stats->tso_bytes += skb->len - ihs - *hopbyhop;
176 mlx5e_txwqe_build_dsegs(struct mlx5e_txqsq *sq, struct sk_buff *skb,
177 unsigned char *skb_data, u16 headlen,
178 struct mlx5_wqe_data_seg *dseg)
180 dma_addr_t dma_addr = 0;
185 dma_addr = dma_map_single(sq->pdev, skb_data, headlen,
187 if (unlikely(dma_mapping_error(sq->pdev, dma_addr)))
188 goto dma_unmap_wqe_err;
190 dseg->addr = cpu_to_be64(dma_addr);
191 dseg->lkey = sq->mkey_be;
192 dseg->byte_count = cpu_to_be32(headlen);
194 mlx5e_dma_push(sq, dma_addr, headlen, MLX5E_DMA_MAP_SINGLE);
199 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
200 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
201 int fsz = skb_frag_size(frag);
203 dma_addr = skb_frag_dma_map(sq->pdev, frag, 0, fsz,
205 if (unlikely(dma_mapping_error(sq->pdev, dma_addr)))
206 goto dma_unmap_wqe_err;
208 dseg->addr = cpu_to_be64(dma_addr);
209 dseg->lkey = sq->mkey_be;
210 dseg->byte_count = cpu_to_be32(fsz);
212 mlx5e_dma_push(sq, dma_addr, fsz, MLX5E_DMA_MAP_PAGE);
220 mlx5e_dma_unmap_wqe_err(sq, num_dma);
224 struct mlx5e_tx_attr {
234 struct mlx5e_tx_wqe_attr {
242 mlx5e_tx_wqe_inline_mode(struct mlx5e_txqsq *sq, struct sk_buff *skb,
243 struct mlx5e_accel_tx_state *accel)
247 #ifdef CONFIG_MLX5_EN_TLS
248 if (accel && accel->tls.tls_tisn)
249 return MLX5_INLINE_MODE_TCP_UDP;
252 mode = sq->min_inline_mode;
254 if (skb_vlan_tag_present(skb) &&
255 test_bit(MLX5E_SQ_STATE_VLAN_NEED_L2_INLINE, &sq->state))
256 mode = max_t(u8, MLX5_INLINE_MODE_L2, mode);
261 static void mlx5e_sq_xmit_prepare(struct mlx5e_txqsq *sq, struct sk_buff *skb,
262 struct mlx5e_accel_tx_state *accel,
263 struct mlx5e_tx_attr *attr)
265 struct mlx5e_sq_stats *stats = sq->stats;
267 if (skb_is_gso(skb)) {
269 u16 ihs = mlx5e_tx_get_gso_ihs(sq, skb, &hopbyhop);
271 *attr = (struct mlx5e_tx_attr) {
272 .opcode = MLX5_OPCODE_LSO,
273 .mss = cpu_to_be16(skb_shinfo(skb)->gso_size),
275 .num_bytes = skb->len + (skb_shinfo(skb)->gso_segs - 1) * ihs,
276 .headlen = skb_headlen(skb) - ihs - hopbyhop,
277 .hopbyhop = hopbyhop,
280 stats->packets += skb_shinfo(skb)->gso_segs;
282 u8 mode = mlx5e_tx_wqe_inline_mode(sq, skb, accel);
283 u16 ihs = mlx5e_calc_min_inline(mode, skb);
285 *attr = (struct mlx5e_tx_attr) {
286 .opcode = MLX5_OPCODE_SEND,
287 .mss = cpu_to_be16(0),
289 .num_bytes = max_t(unsigned int, skb->len, ETH_ZLEN),
290 .headlen = skb_headlen(skb) - ihs,
296 attr->insz = mlx5e_accel_tx_ids_len(sq, accel);
297 stats->bytes += attr->num_bytes;
300 static void mlx5e_sq_calc_wqe_attr(struct sk_buff *skb, const struct mlx5e_tx_attr *attr,
301 struct mlx5e_tx_wqe_attr *wqe_attr)
303 u16 ds_cnt = MLX5E_TX_WQE_EMPTY_DS_COUNT;
308 ds_cnt_ids = DIV_ROUND_UP(sizeof(struct mlx5_wqe_inline_seg) + attr->insz,
311 ds_cnt += !!attr->headlen + skb_shinfo(skb)->nr_frags + ds_cnt_ids;
313 u16 inl = attr->ihs - INL_HDR_START_SZ;
315 if (skb_vlan_tag_present(skb))
318 ds_cnt_inl = DIV_ROUND_UP(inl, MLX5_SEND_WQE_DS);
319 ds_cnt += ds_cnt_inl;
322 *wqe_attr = (struct mlx5e_tx_wqe_attr) {
324 .ds_cnt_inl = ds_cnt_inl,
325 .ds_cnt_ids = ds_cnt_ids,
326 .num_wqebbs = DIV_ROUND_UP(ds_cnt, MLX5_SEND_WQEBB_NUM_DS),
330 static void mlx5e_tx_skb_update_hwts_flags(struct sk_buff *skb)
332 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
333 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
336 static void mlx5e_tx_check_stop(struct mlx5e_txqsq *sq)
338 if (unlikely(!mlx5e_wqc_has_room_for(&sq->wq, sq->cc, sq->pc, sq->stop_room))) {
339 netif_tx_stop_queue(sq->txq);
340 sq->stats->stopped++;
344 static void mlx5e_tx_flush(struct mlx5e_txqsq *sq)
346 struct mlx5e_tx_wqe_info *wi;
347 struct mlx5e_tx_wqe *wqe;
350 /* Must not be called when a MPWQE session is active but empty. */
351 mlx5e_tx_mpwqe_ensure_complete(sq);
353 pi = mlx5_wq_cyc_ctr2ix(&sq->wq, sq->pc);
354 wi = &sq->db.wqe_info[pi];
356 *wi = (struct mlx5e_tx_wqe_info) {
360 wqe = mlx5e_post_nop(&sq->wq, sq->sqn, &sq->pc);
361 mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, &wqe->ctrl);
365 mlx5e_txwqe_complete(struct mlx5e_txqsq *sq, struct sk_buff *skb,
366 const struct mlx5e_tx_attr *attr,
367 const struct mlx5e_tx_wqe_attr *wqe_attr, u8 num_dma,
368 struct mlx5e_tx_wqe_info *wi, struct mlx5_wqe_ctrl_seg *cseg,
371 struct mlx5_wq_cyc *wq = &sq->wq;
374 *wi = (struct mlx5e_tx_wqe_info) {
376 .num_bytes = attr->num_bytes,
378 .num_wqebbs = wqe_attr->num_wqebbs,
382 cseg->opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | attr->opcode);
383 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | wqe_attr->ds_cnt);
385 mlx5e_tx_skb_update_hwts_flags(skb);
387 sq->pc += wi->num_wqebbs;
389 mlx5e_tx_check_stop(sq);
391 if (unlikely(sq->ptpsq)) {
392 mlx5e_skb_cb_hwtstamp_init(skb);
393 mlx5e_skb_fifo_push(&sq->ptpsq->skb_fifo, skb);
397 send_doorbell = __netdev_tx_sent_queue(sq->txq, attr->num_bytes, xmit_more);
399 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, cseg);
403 mlx5e_sq_xmit_wqe(struct mlx5e_txqsq *sq, struct sk_buff *skb,
404 const struct mlx5e_tx_attr *attr, const struct mlx5e_tx_wqe_attr *wqe_attr,
405 struct mlx5e_tx_wqe *wqe, u16 pi, bool xmit_more)
407 struct mlx5_wqe_ctrl_seg *cseg;
408 struct mlx5_wqe_eth_seg *eseg;
409 struct mlx5_wqe_data_seg *dseg;
410 struct mlx5e_tx_wqe_info *wi;
413 struct mlx5e_sq_stats *stats = sq->stats;
416 stats->xmit_more += xmit_more;
419 wi = &sq->db.wqe_info[pi];
424 eseg->mss = attr->mss;
427 u8 *start = eseg->inline_hdr.start;
429 if (unlikely(attr->hopbyhop)) {
430 /* remove the HBH header.
431 * Layout: [Ethernet header][IPv6 header][HBH][TCP header]
433 if (skb_vlan_tag_present(skb)) {
434 mlx5e_insert_vlan(start, skb, ETH_HLEN + sizeof(*h6));
436 h6 = (struct ipv6hdr *)(start + sizeof(struct vlan_ethhdr));
438 unsafe_memcpy(start, skb->data,
439 ETH_HLEN + sizeof(*h6),
440 MLX5_UNSAFE_MEMCPY_DISCLAIMER);
441 h6 = (struct ipv6hdr *)(start + ETH_HLEN);
443 h6->nexthdr = IPPROTO_TCP;
444 /* Copy the TCP header after the IPv6 one */
446 skb->data + ETH_HLEN + sizeof(*h6) +
447 sizeof(struct hop_jumbo_hdr),
449 /* Leave ipv6 payload_len set to 0, as LSO v2 specs request. */
450 } else if (skb_vlan_tag_present(skb)) {
451 mlx5e_insert_vlan(start, skb, ihs);
453 stats->added_vlan_packets++;
455 unsafe_memcpy(eseg->inline_hdr.start, skb->data,
457 MLX5_UNSAFE_MEMCPY_DISCLAIMER);
459 eseg->inline_hdr.sz |= cpu_to_be16(ihs);
460 dseg += wqe_attr->ds_cnt_inl;
461 } else if (skb_vlan_tag_present(skb)) {
462 eseg->insert.type = cpu_to_be16(MLX5_ETH_WQE_INSERT_VLAN);
463 if (skb->vlan_proto == cpu_to_be16(ETH_P_8021AD))
464 eseg->insert.type |= cpu_to_be16(MLX5_ETH_WQE_SVLAN);
465 eseg->insert.vlan_tci = cpu_to_be16(skb_vlan_tag_get(skb));
466 stats->added_vlan_packets++;
469 dseg += wqe_attr->ds_cnt_ids;
470 num_dma = mlx5e_txwqe_build_dsegs(sq, skb, skb->data + attr->ihs + attr->hopbyhop,
471 attr->headlen, dseg);
472 if (unlikely(num_dma < 0))
475 mlx5e_txwqe_complete(sq, skb, attr, wqe_attr, num_dma, wi, cseg, xmit_more);
481 dev_kfree_skb_any(skb);
485 static bool mlx5e_tx_skb_supports_mpwqe(struct sk_buff *skb, struct mlx5e_tx_attr *attr)
487 return !skb_is_nonlinear(skb) && !skb_vlan_tag_present(skb) && !attr->ihs &&
491 static bool mlx5e_tx_mpwqe_same_eseg(struct mlx5e_txqsq *sq, struct mlx5_wqe_eth_seg *eseg)
493 struct mlx5e_tx_mpwqe *session = &sq->mpwqe;
495 /* Assumes the session is already running and has at least one packet. */
496 return !memcmp(&session->wqe->eth, eseg, MLX5E_ACCEL_ESEG_LEN);
499 static void mlx5e_tx_mpwqe_session_start(struct mlx5e_txqsq *sq,
500 struct mlx5_wqe_eth_seg *eseg)
502 struct mlx5e_tx_mpwqe *session = &sq->mpwqe;
503 struct mlx5e_tx_wqe *wqe;
506 pi = mlx5e_txqsq_get_next_pi(sq, sq->max_sq_mpw_wqebbs);
507 wqe = MLX5E_TX_FETCH_WQE(sq, pi);
508 net_prefetchw(wqe->data);
510 *session = (struct mlx5e_tx_mpwqe) {
513 .ds_count = MLX5E_TX_WQE_EMPTY_DS_COUNT,
518 memcpy(&session->wqe->eth, eseg, MLX5E_ACCEL_ESEG_LEN);
520 sq->stats->mpwqe_blks++;
523 static bool mlx5e_tx_mpwqe_session_is_active(struct mlx5e_txqsq *sq)
525 return sq->mpwqe.wqe;
528 static void mlx5e_tx_mpwqe_add_dseg(struct mlx5e_txqsq *sq, struct mlx5e_xmit_data *txd)
530 struct mlx5e_tx_mpwqe *session = &sq->mpwqe;
531 struct mlx5_wqe_data_seg *dseg;
533 dseg = (struct mlx5_wqe_data_seg *)session->wqe + session->ds_count;
535 session->pkt_count++;
536 session->bytes_count += txd->len;
538 dseg->addr = cpu_to_be64(txd->dma_addr);
539 dseg->byte_count = cpu_to_be32(txd->len);
540 dseg->lkey = sq->mkey_be;
543 sq->stats->mpwqe_pkts++;
546 static struct mlx5_wqe_ctrl_seg *mlx5e_tx_mpwqe_session_complete(struct mlx5e_txqsq *sq)
548 struct mlx5e_tx_mpwqe *session = &sq->mpwqe;
549 u8 ds_count = session->ds_count;
550 struct mlx5_wqe_ctrl_seg *cseg;
551 struct mlx5e_tx_wqe_info *wi;
554 cseg = &session->wqe->ctrl;
555 cseg->opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | MLX5_OPCODE_ENHANCED_MPSW);
556 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_count);
558 pi = mlx5_wq_cyc_ctr2ix(&sq->wq, sq->pc);
559 wi = &sq->db.wqe_info[pi];
560 *wi = (struct mlx5e_tx_wqe_info) {
562 .num_bytes = session->bytes_count,
563 .num_wqebbs = DIV_ROUND_UP(ds_count, MLX5_SEND_WQEBB_NUM_DS),
564 .num_dma = session->pkt_count,
565 .num_fifo_pkts = session->pkt_count,
568 sq->pc += wi->num_wqebbs;
572 mlx5e_tx_check_stop(sq);
578 mlx5e_sq_xmit_mpwqe(struct mlx5e_txqsq *sq, struct sk_buff *skb,
579 struct mlx5_wqe_eth_seg *eseg, bool xmit_more)
581 struct mlx5_wqe_ctrl_seg *cseg;
582 struct mlx5e_xmit_data txd;
584 txd.data = skb->data;
587 txd.dma_addr = dma_map_single(sq->pdev, txd.data, txd.len, DMA_TO_DEVICE);
588 if (unlikely(dma_mapping_error(sq->pdev, txd.dma_addr)))
591 if (!mlx5e_tx_mpwqe_session_is_active(sq)) {
592 mlx5e_tx_mpwqe_session_start(sq, eseg);
593 } else if (!mlx5e_tx_mpwqe_same_eseg(sq, eseg)) {
594 mlx5e_tx_mpwqe_session_complete(sq);
595 mlx5e_tx_mpwqe_session_start(sq, eseg);
598 sq->stats->xmit_more += xmit_more;
600 mlx5e_dma_push(sq, txd.dma_addr, txd.len, MLX5E_DMA_MAP_SINGLE);
601 mlx5e_skb_fifo_push(&sq->db.skb_fifo, skb);
602 mlx5e_tx_mpwqe_add_dseg(sq, &txd);
603 mlx5e_tx_skb_update_hwts_flags(skb);
605 if (unlikely(mlx5e_tx_mpwqe_is_full(&sq->mpwqe, sq->max_sq_mpw_wqebbs))) {
606 /* Might stop the queue and affect the retval of __netdev_tx_sent_queue. */
607 cseg = mlx5e_tx_mpwqe_session_complete(sq);
609 if (__netdev_tx_sent_queue(sq->txq, txd.len, xmit_more))
610 mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, cseg);
611 } else if (__netdev_tx_sent_queue(sq->txq, txd.len, xmit_more)) {
612 /* Might stop the queue, but we were asked to ring the doorbell anyway. */
613 cseg = mlx5e_tx_mpwqe_session_complete(sq);
615 mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, cseg);
621 mlx5e_dma_unmap_wqe_err(sq, 1);
622 sq->stats->dropped++;
623 dev_kfree_skb_any(skb);
627 void mlx5e_tx_mpwqe_ensure_complete(struct mlx5e_txqsq *sq)
629 /* Unlikely in non-MPWQE workloads; not important in MPWQE workloads. */
630 if (unlikely(mlx5e_tx_mpwqe_session_is_active(sq)))
631 mlx5e_tx_mpwqe_session_complete(sq);
634 static void mlx5e_txwqe_build_eseg(struct mlx5e_priv *priv, struct mlx5e_txqsq *sq,
635 struct sk_buff *skb, struct mlx5e_accel_tx_state *accel,
636 struct mlx5_wqe_eth_seg *eseg, u16 ihs)
638 mlx5e_accel_tx_eseg(priv, skb, eseg, ihs);
639 mlx5e_txwqe_build_eseg_csum(sq, skb, accel, eseg);
642 netdev_tx_t mlx5e_xmit(struct sk_buff *skb, struct net_device *dev)
644 struct mlx5e_priv *priv = netdev_priv(dev);
645 struct mlx5e_accel_tx_state accel = {};
646 struct mlx5e_tx_wqe_attr wqe_attr;
647 struct mlx5e_tx_attr attr;
648 struct mlx5e_tx_wqe *wqe;
649 struct mlx5e_txqsq *sq;
652 /* All changes to txq2sq are performed in sync with mlx5e_xmit, when the
653 * queue being changed is disabled, and smp_wmb guarantees that the
654 * changes are visible before mlx5e_xmit tries to read from txq2sq. It
655 * guarantees that the value of txq2sq[qid] doesn't change while
656 * mlx5e_xmit is running on queue number qid. smb_wmb is paired with
657 * HARD_TX_LOCK around ndo_start_xmit, which serves as an ACQUIRE.
659 sq = priv->txq2sq[skb_get_queue_mapping(skb)];
661 /* Two cases when sq can be NULL:
662 * 1. The HTB node is registered, and mlx5e_select_queue
663 * selected its queue ID, but the SQ itself is not yet created.
664 * 2. HTB SQ creation failed. Similar to the previous case, but
665 * the SQ won't be created.
667 dev_kfree_skb_any(skb);
671 /* May send SKBs and WQEs. */
672 if (unlikely(!mlx5e_accel_tx_begin(dev, sq, skb, &accel)))
675 mlx5e_sq_xmit_prepare(sq, skb, &accel, &attr);
677 if (test_bit(MLX5E_SQ_STATE_MPWQE, &sq->state)) {
678 if (mlx5e_tx_skb_supports_mpwqe(skb, &attr)) {
679 struct mlx5_wqe_eth_seg eseg = {};
681 mlx5e_txwqe_build_eseg(priv, sq, skb, &accel, &eseg, attr.ihs);
682 mlx5e_sq_xmit_mpwqe(sq, skb, &eseg, netdev_xmit_more());
686 mlx5e_tx_mpwqe_ensure_complete(sq);
689 mlx5e_sq_calc_wqe_attr(skb, &attr, &wqe_attr);
690 pi = mlx5e_txqsq_get_next_pi(sq, wqe_attr.num_wqebbs);
691 wqe = MLX5E_TX_FETCH_WQE(sq, pi);
693 /* May update the WQE, but may not post other WQEs. */
694 mlx5e_accel_tx_finish(sq, wqe, &accel,
695 (struct mlx5_wqe_inline_seg *)(wqe->data + wqe_attr.ds_cnt_inl));
696 mlx5e_txwqe_build_eseg(priv, sq, skb, &accel, &wqe->eth, attr.ihs);
697 mlx5e_sq_xmit_wqe(sq, skb, &attr, &wqe_attr, wqe, pi, netdev_xmit_more());
702 void mlx5e_sq_xmit_simple(struct mlx5e_txqsq *sq, struct sk_buff *skb, bool xmit_more)
704 struct mlx5e_tx_wqe_attr wqe_attr;
705 struct mlx5e_tx_attr attr;
706 struct mlx5e_tx_wqe *wqe;
709 mlx5e_sq_xmit_prepare(sq, skb, NULL, &attr);
710 mlx5e_sq_calc_wqe_attr(skb, &attr, &wqe_attr);
711 pi = mlx5e_txqsq_get_next_pi(sq, wqe_attr.num_wqebbs);
712 wqe = MLX5E_TX_FETCH_WQE(sq, pi);
713 mlx5e_txwqe_build_eseg_csum(sq, skb, NULL, &wqe->eth);
714 mlx5e_sq_xmit_wqe(sq, skb, &attr, &wqe_attr, wqe, pi, xmit_more);
717 static void mlx5e_tx_wi_dma_unmap(struct mlx5e_txqsq *sq, struct mlx5e_tx_wqe_info *wi,
722 for (i = 0; i < wi->num_dma; i++) {
723 struct mlx5e_sq_dma *dma = mlx5e_dma_get(sq, (*dma_fifo_cc)++);
725 mlx5e_tx_dma_unmap(sq->pdev, dma);
729 static void mlx5e_consume_skb(struct mlx5e_txqsq *sq, struct sk_buff *skb,
730 struct mlx5_cqe64 *cqe, int napi_budget)
732 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
733 struct skb_shared_hwtstamps hwts = {};
734 u64 ts = get_cqe_ts(cqe);
736 hwts.hwtstamp = mlx5e_cqe_ts_to_ns(sq->ptp_cyc2time, sq->clock, ts);
738 mlx5e_skb_cb_hwtstamp_handler(skb, MLX5E_SKB_CB_CQE_HWTSTAMP,
739 hwts.hwtstamp, sq->ptpsq->cq_stats);
741 skb_tstamp_tx(skb, &hwts);
744 napi_consume_skb(skb, napi_budget);
747 static void mlx5e_tx_wi_consume_fifo_skbs(struct mlx5e_txqsq *sq, struct mlx5e_tx_wqe_info *wi,
748 struct mlx5_cqe64 *cqe, int napi_budget)
752 for (i = 0; i < wi->num_fifo_pkts; i++) {
753 struct sk_buff *skb = mlx5e_skb_fifo_pop(&sq->db.skb_fifo);
755 mlx5e_consume_skb(sq, skb, cqe, napi_budget);
759 bool mlx5e_poll_tx_cq(struct mlx5e_cq *cq, int napi_budget)
761 struct mlx5e_sq_stats *stats;
762 struct mlx5e_txqsq *sq;
763 struct mlx5_cqe64 *cqe;
770 sq = container_of(cq, struct mlx5e_txqsq, cq);
772 if (unlikely(!test_bit(MLX5E_SQ_STATE_ENABLED, &sq->state)))
775 cqe = mlx5_cqwq_get_cqe(&cq->wq);
784 /* sq->cc must be updated only after mlx5_cqwq_update_db_record(),
785 * otherwise a cq overrun may occur
789 /* avoid dirtying sq cache line every cqe */
790 dma_fifo_cc = sq->dma_fifo_cc;
794 struct mlx5e_tx_wqe_info *wi;
799 mlx5_cqwq_pop(&cq->wq);
801 wqe_counter = be16_to_cpu(cqe->wqe_counter);
804 last_wqe = (sqcc == wqe_counter);
806 ci = mlx5_wq_cyc_ctr2ix(&sq->wq, sqcc);
807 wi = &sq->db.wqe_info[ci];
809 sqcc += wi->num_wqebbs;
811 if (likely(wi->skb)) {
812 mlx5e_tx_wi_dma_unmap(sq, wi, &dma_fifo_cc);
813 mlx5e_consume_skb(sq, wi->skb, cqe, napi_budget);
816 nbytes += wi->num_bytes;
820 if (unlikely(mlx5e_ktls_tx_try_handle_resync_dump_comp(sq, wi,
824 if (wi->num_fifo_pkts) {
825 mlx5e_tx_wi_dma_unmap(sq, wi, &dma_fifo_cc);
826 mlx5e_tx_wi_consume_fifo_skbs(sq, wi, cqe, napi_budget);
828 npkts += wi->num_fifo_pkts;
829 nbytes += wi->num_bytes;
833 if (unlikely(get_cqe_opcode(cqe) == MLX5_CQE_REQ_ERR)) {
834 if (!test_and_set_bit(MLX5E_SQ_STATE_RECOVERING,
836 mlx5e_dump_error_cqe(&sq->cq, sq->sqn,
837 (struct mlx5_err_cqe *)cqe);
838 mlx5_wq_cyc_wqe_dump(&sq->wq, ci, wi->num_wqebbs);
839 queue_work(cq->priv->wq, &sq->recover_work);
844 } while ((++i < MLX5E_TX_CQ_POLL_BUDGET) && (cqe = mlx5_cqwq_get_cqe(&cq->wq)));
848 mlx5_cqwq_update_db_record(&cq->wq);
850 /* ensure cq space is freed before enabling more cqes */
853 sq->dma_fifo_cc = dma_fifo_cc;
856 netdev_tx_completed_queue(sq->txq, npkts, nbytes);
858 if (netif_tx_queue_stopped(sq->txq) &&
859 mlx5e_wqc_has_room_for(&sq->wq, sq->cc, sq->pc, sq->stop_room) &&
860 !test_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state)) {
861 netif_tx_wake_queue(sq->txq);
865 return (i == MLX5E_TX_CQ_POLL_BUDGET);
868 static void mlx5e_tx_wi_kfree_fifo_skbs(struct mlx5e_txqsq *sq, struct mlx5e_tx_wqe_info *wi)
872 for (i = 0; i < wi->num_fifo_pkts; i++)
873 dev_kfree_skb_any(mlx5e_skb_fifo_pop(&sq->db.skb_fifo));
876 void mlx5e_free_txqsq_descs(struct mlx5e_txqsq *sq)
878 struct mlx5e_tx_wqe_info *wi;
879 u32 dma_fifo_cc, nbytes = 0;
880 u16 ci, sqcc, npkts = 0;
883 dma_fifo_cc = sq->dma_fifo_cc;
885 while (sqcc != sq->pc) {
886 ci = mlx5_wq_cyc_ctr2ix(&sq->wq, sqcc);
887 wi = &sq->db.wqe_info[ci];
889 sqcc += wi->num_wqebbs;
891 if (likely(wi->skb)) {
892 mlx5e_tx_wi_dma_unmap(sq, wi, &dma_fifo_cc);
893 dev_kfree_skb_any(wi->skb);
896 nbytes += wi->num_bytes;
900 if (unlikely(mlx5e_ktls_tx_try_handle_resync_dump_comp(sq, wi, &dma_fifo_cc)))
903 if (wi->num_fifo_pkts) {
904 mlx5e_tx_wi_dma_unmap(sq, wi, &dma_fifo_cc);
905 mlx5e_tx_wi_kfree_fifo_skbs(sq, wi);
907 npkts += wi->num_fifo_pkts;
908 nbytes += wi->num_bytes;
912 sq->dma_fifo_cc = dma_fifo_cc;
915 netdev_tx_completed_queue(sq->txq, npkts, nbytes);
918 #ifdef CONFIG_MLX5_CORE_IPOIB
920 mlx5i_txwqe_build_datagram(struct mlx5_av *av, u32 dqpn, u32 dqkey,
921 struct mlx5_wqe_datagram_seg *dseg)
923 memcpy(&dseg->av, av, sizeof(struct mlx5_av));
924 dseg->av.dqp_dct = cpu_to_be32(dqpn | MLX5_EXTENDED_UD_AV);
925 dseg->av.key.qkey.qkey = cpu_to_be32(dqkey);
928 static void mlx5i_sq_calc_wqe_attr(struct sk_buff *skb,
929 const struct mlx5e_tx_attr *attr,
930 struct mlx5e_tx_wqe_attr *wqe_attr)
932 u16 ds_cnt = sizeof(struct mlx5i_tx_wqe) / MLX5_SEND_WQE_DS;
935 ds_cnt += !!attr->headlen + skb_shinfo(skb)->nr_frags;
938 u16 inl = attr->ihs - INL_HDR_START_SZ;
940 ds_cnt_inl = DIV_ROUND_UP(inl, MLX5_SEND_WQE_DS);
941 ds_cnt += ds_cnt_inl;
944 *wqe_attr = (struct mlx5e_tx_wqe_attr) {
946 .ds_cnt_inl = ds_cnt_inl,
947 .num_wqebbs = DIV_ROUND_UP(ds_cnt, MLX5_SEND_WQEBB_NUM_DS),
951 void mlx5i_sq_xmit(struct mlx5e_txqsq *sq, struct sk_buff *skb,
952 struct mlx5_av *av, u32 dqpn, u32 dqkey, bool xmit_more)
954 struct mlx5e_tx_wqe_attr wqe_attr;
955 struct mlx5e_tx_attr attr;
956 struct mlx5i_tx_wqe *wqe;
958 struct mlx5_wqe_datagram_seg *datagram;
959 struct mlx5_wqe_ctrl_seg *cseg;
960 struct mlx5_wqe_eth_seg *eseg;
961 struct mlx5_wqe_data_seg *dseg;
962 struct mlx5e_tx_wqe_info *wi;
964 struct mlx5e_sq_stats *stats = sq->stats;
968 mlx5e_sq_xmit_prepare(sq, skb, NULL, &attr);
969 mlx5i_sq_calc_wqe_attr(skb, &attr, &wqe_attr);
971 pi = mlx5e_txqsq_get_next_pi(sq, wqe_attr.num_wqebbs);
972 wqe = MLX5I_SQ_FETCH_WQE(sq, pi);
974 stats->xmit_more += xmit_more;
977 wi = &sq->db.wqe_info[pi];
979 datagram = &wqe->datagram;
983 mlx5i_txwqe_build_datagram(av, dqpn, dqkey, datagram);
985 mlx5e_txwqe_build_eseg_csum(sq, skb, NULL, eseg);
987 eseg->mss = attr.mss;
990 if (unlikely(attr.hopbyhop)) {
993 /* remove the HBH header.
994 * Layout: [Ethernet header][IPv6 header][HBH][TCP header]
996 unsafe_memcpy(eseg->inline_hdr.start, skb->data,
997 ETH_HLEN + sizeof(*h6),
998 MLX5_UNSAFE_MEMCPY_DISCLAIMER);
999 h6 = (struct ipv6hdr *)((char *)eseg->inline_hdr.start + ETH_HLEN);
1000 h6->nexthdr = IPPROTO_TCP;
1001 /* Copy the TCP header after the IPv6 one */
1002 unsafe_memcpy(h6 + 1,
1003 skb->data + ETH_HLEN + sizeof(*h6) +
1004 sizeof(struct hop_jumbo_hdr),
1006 MLX5_UNSAFE_MEMCPY_DISCLAIMER);
1007 /* Leave ipv6 payload_len set to 0, as LSO v2 specs request. */
1009 unsafe_memcpy(eseg->inline_hdr.start, skb->data,
1011 MLX5_UNSAFE_MEMCPY_DISCLAIMER);
1013 eseg->inline_hdr.sz = cpu_to_be16(attr.ihs);
1014 dseg += wqe_attr.ds_cnt_inl;
1017 num_dma = mlx5e_txwqe_build_dsegs(sq, skb, skb->data + attr.ihs + attr.hopbyhop,
1018 attr.headlen, dseg);
1019 if (unlikely(num_dma < 0))
1022 mlx5e_txwqe_complete(sq, skb, &attr, &wqe_attr, num_dma, wi, cseg, xmit_more);
1028 dev_kfree_skb_any(skb);