Merge tag 'pm-5.8-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-2.6-microblaze.git] / drivers / net / ethernet / mellanox / mlx5 / core / en / txrx.h
1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2019 Mellanox Technologies. */
3
4 #ifndef __MLX5_EN_TXRX_H___
5 #define __MLX5_EN_TXRX_H___
6
7 #include "en.h"
8
9 #define INL_HDR_START_SZ (sizeof(((struct mlx5_wqe_eth_seg *)NULL)->inline_hdr.start))
10
11 enum mlx5e_icosq_wqe_type {
12         MLX5E_ICOSQ_WQE_NOP,
13         MLX5E_ICOSQ_WQE_UMR_RX,
14 };
15
16 static inline bool
17 mlx5e_wqc_has_room_for(struct mlx5_wq_cyc *wq, u16 cc, u16 pc, u16 n)
18 {
19         return (mlx5_wq_cyc_ctr2ix(wq, cc - pc) >= n) || (cc == pc);
20 }
21
22 static inline void *mlx5e_fetch_wqe(struct mlx5_wq_cyc *wq, u16 pi, size_t wqe_size)
23 {
24         void *wqe;
25
26         wqe = mlx5_wq_cyc_get_wqe(wq, pi);
27         memset(wqe, 0, wqe_size);
28
29         return wqe;
30 }
31
32 #define MLX5E_TX_FETCH_WQE(sq, pi) \
33         ((struct mlx5e_tx_wqe *)mlx5e_fetch_wqe(&(sq)->wq, pi, sizeof(struct mlx5e_tx_wqe)))
34
35 static inline struct mlx5e_tx_wqe *
36 mlx5e_post_nop(struct mlx5_wq_cyc *wq, u32 sqn, u16 *pc)
37 {
38         u16                         pi   = mlx5_wq_cyc_ctr2ix(wq, *pc);
39         struct mlx5e_tx_wqe        *wqe  = mlx5_wq_cyc_get_wqe(wq, pi);
40         struct mlx5_wqe_ctrl_seg   *cseg = &wqe->ctrl;
41
42         memset(cseg, 0, sizeof(*cseg));
43
44         cseg->opmod_idx_opcode = cpu_to_be32((*pc << 8) | MLX5_OPCODE_NOP);
45         cseg->qpn_ds           = cpu_to_be32((sqn << 8) | 0x01);
46
47         (*pc)++;
48
49         return wqe;
50 }
51
52 static inline struct mlx5e_tx_wqe *
53 mlx5e_post_nop_fence(struct mlx5_wq_cyc *wq, u32 sqn, u16 *pc)
54 {
55         u16                         pi   = mlx5_wq_cyc_ctr2ix(wq, *pc);
56         struct mlx5e_tx_wqe        *wqe  = mlx5_wq_cyc_get_wqe(wq, pi);
57         struct mlx5_wqe_ctrl_seg   *cseg = &wqe->ctrl;
58
59         memset(cseg, 0, sizeof(*cseg));
60
61         cseg->opmod_idx_opcode = cpu_to_be32((*pc << 8) | MLX5_OPCODE_NOP);
62         cseg->qpn_ds           = cpu_to_be32((sqn << 8) | 0x01);
63         cseg->fm_ce_se         = MLX5_FENCE_MODE_INITIATOR_SMALL;
64
65         (*pc)++;
66
67         return wqe;
68 }
69
70 struct mlx5e_tx_wqe_info {
71         struct sk_buff *skb;
72         u32 num_bytes;
73         u8 num_wqebbs;
74         u8 num_dma;
75 #ifdef CONFIG_MLX5_EN_TLS
76         struct page *resync_dump_frag_page;
77 #endif
78 };
79
80 static inline u16 mlx5e_txqsq_get_next_pi(struct mlx5e_txqsq *sq, u16 size)
81 {
82         struct mlx5_wq_cyc *wq = &sq->wq;
83         u16 pi, contig_wqebbs;
84
85         pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
86         contig_wqebbs = mlx5_wq_cyc_get_contig_wqebbs(wq, pi);
87         if (unlikely(contig_wqebbs < size)) {
88                 struct mlx5e_tx_wqe_info *wi, *edge_wi;
89
90                 wi = &sq->db.wqe_info[pi];
91                 edge_wi = wi + contig_wqebbs;
92
93                 /* Fill SQ frag edge with NOPs to avoid WQE wrapping two pages. */
94                 for (; wi < edge_wi; wi++) {
95                         *wi = (struct mlx5e_tx_wqe_info) {
96                                 .num_wqebbs = 1,
97                         };
98                         mlx5e_post_nop(wq, sq->sqn, &sq->pc);
99                 }
100                 sq->stats->nop += contig_wqebbs;
101
102                 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
103         }
104
105         return pi;
106 }
107
108 struct mlx5e_icosq_wqe_info {
109         u8 wqe_type;
110         u8 num_wqebbs;
111
112         /* Auxiliary data for different wqe types. */
113         union {
114                 struct {
115                         struct mlx5e_rq *rq;
116                 } umr;
117         };
118 };
119
120 static inline u16 mlx5e_icosq_get_next_pi(struct mlx5e_icosq *sq, u16 size)
121 {
122         struct mlx5_wq_cyc *wq = &sq->wq;
123         u16 pi, contig_wqebbs;
124
125         pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
126         contig_wqebbs = mlx5_wq_cyc_get_contig_wqebbs(wq, pi);
127         if (unlikely(contig_wqebbs < size)) {
128                 struct mlx5e_icosq_wqe_info *wi, *edge_wi;
129
130                 wi = &sq->db.wqe_info[pi];
131                 edge_wi = wi + contig_wqebbs;
132
133                 /* Fill SQ frag edge with NOPs to avoid WQE wrapping two pages. */
134                 for (; wi < edge_wi; wi++) {
135                         *wi = (struct mlx5e_icosq_wqe_info) {
136                                 .wqe_type   = MLX5E_ICOSQ_WQE_NOP,
137                                 .num_wqebbs = 1,
138                         };
139                         mlx5e_post_nop(wq, sq->sqn, &sq->pc);
140                 }
141
142                 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
143         }
144
145         return pi;
146 }
147
148 static inline void
149 mlx5e_fill_sq_frag_edge(struct mlx5e_txqsq *sq, struct mlx5_wq_cyc *wq,
150                         u16 pi, u16 nnops)
151 {
152         struct mlx5e_tx_wqe_info *edge_wi, *wi = &sq->db.wqe_info[pi];
153
154         edge_wi = wi + nnops;
155
156         /* fill sq frag edge with nops to avoid wqe wrapping two pages */
157         for (; wi < edge_wi; wi++) {
158                 memset(wi, 0, sizeof(*wi));
159                 wi->num_wqebbs = 1;
160                 mlx5e_post_nop(wq, sq->sqn, &sq->pc);
161         }
162         sq->stats->nop += nnops;
163 }
164
165 static inline void
166 mlx5e_notify_hw(struct mlx5_wq_cyc *wq, u16 pc, void __iomem *uar_map,
167                 struct mlx5_wqe_ctrl_seg *ctrl)
168 {
169         ctrl->fm_ce_se |= MLX5_WQE_CTRL_CQ_UPDATE;
170         /* ensure wqe is visible to device before updating doorbell record */
171         dma_wmb();
172
173         *wq->db = cpu_to_be32(pc);
174
175         /* ensure doorbell record is visible to device before ringing the
176          * doorbell
177          */
178         wmb();
179
180         mlx5_write64((__be32 *)ctrl, uar_map);
181 }
182
183 static inline bool mlx5e_transport_inline_tx_wqe(struct mlx5_wqe_ctrl_seg *cseg)
184 {
185         return cseg && !!cseg->tisn;
186 }
187
188 static inline u8
189 mlx5e_tx_wqe_inline_mode(struct mlx5e_txqsq *sq, struct mlx5_wqe_ctrl_seg *cseg,
190                          struct sk_buff *skb)
191 {
192         u8 mode;
193
194         if (mlx5e_transport_inline_tx_wqe(cseg))
195                 return MLX5_INLINE_MODE_TCP_UDP;
196
197         mode = sq->min_inline_mode;
198
199         if (skb_vlan_tag_present(skb) &&
200             test_bit(MLX5E_SQ_STATE_VLAN_NEED_L2_INLINE, &sq->state))
201                 mode = max_t(u8, MLX5_INLINE_MODE_L2, mode);
202
203         return mode;
204 }
205
206 static inline void mlx5e_cq_arm(struct mlx5e_cq *cq)
207 {
208         struct mlx5_core_cq *mcq;
209
210         mcq = &cq->mcq;
211         mlx5_cq_arm(mcq, MLX5_CQ_DB_REQ_NOT, mcq->uar->map, cq->wq.cc);
212 }
213
214 static inline struct mlx5e_sq_dma *
215 mlx5e_dma_get(struct mlx5e_txqsq *sq, u32 i)
216 {
217         return &sq->db.dma_fifo[i & sq->dma_fifo_mask];
218 }
219
220 static inline void
221 mlx5e_dma_push(struct mlx5e_txqsq *sq, dma_addr_t addr, u32 size,
222                enum mlx5e_dma_map_type map_type)
223 {
224         struct mlx5e_sq_dma *dma = mlx5e_dma_get(sq, sq->dma_fifo_pc++);
225
226         dma->addr = addr;
227         dma->size = size;
228         dma->type = map_type;
229 }
230
231 static inline void
232 mlx5e_tx_dma_unmap(struct device *pdev, struct mlx5e_sq_dma *dma)
233 {
234         switch (dma->type) {
235         case MLX5E_DMA_MAP_SINGLE:
236                 dma_unmap_single(pdev, dma->addr, dma->size, DMA_TO_DEVICE);
237                 break;
238         case MLX5E_DMA_MAP_PAGE:
239                 dma_unmap_page(pdev, dma->addr, dma->size, DMA_TO_DEVICE);
240                 break;
241         default:
242                 WARN_ONCE(true, "mlx5e_tx_dma_unmap unknown DMA type!\n");
243         }
244 }
245
246 static inline void mlx5e_rqwq_reset(struct mlx5e_rq *rq)
247 {
248         if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
249                 mlx5_wq_ll_reset(&rq->mpwqe.wq);
250                 rq->mpwqe.actual_wq_head = 0;
251         } else {
252                 mlx5_wq_cyc_reset(&rq->wqe.wq);
253         }
254 }
255
256 static inline void mlx5e_dump_error_cqe(struct mlx5e_cq *cq, u32 sqn,
257                                         struct mlx5_err_cqe *err_cqe)
258 {
259         struct mlx5_cqwq *wq = &cq->wq;
260         u32 ci;
261
262         ci = mlx5_cqwq_ctr2ix(wq, wq->cc - 1);
263
264         netdev_err(cq->channel->netdev,
265                    "Error cqe on cqn 0x%x, ci 0x%x, sqn 0x%x, opcode 0x%x, syndrome 0x%x, vendor syndrome 0x%x\n",
266                    cq->mcq.cqn, ci, sqn,
267                    get_cqe_opcode((struct mlx5_cqe64 *)err_cqe),
268                    err_cqe->syndrome, err_cqe->vendor_err_synd);
269         mlx5_dump_err_cqe(cq->mdev, err_cqe);
270 }
271
272 /* SW parser related functions */
273
274 struct mlx5e_swp_spec {
275         __be16 l3_proto;
276         u8 l4_proto;
277         u8 is_tun;
278         __be16 tun_l3_proto;
279         u8 tun_l4_proto;
280 };
281
282 static inline void
283 mlx5e_set_eseg_swp(struct sk_buff *skb, struct mlx5_wqe_eth_seg *eseg,
284                    struct mlx5e_swp_spec *swp_spec)
285 {
286         /* SWP offsets are in 2-bytes words */
287         eseg->swp_outer_l3_offset = skb_network_offset(skb) / 2;
288         if (swp_spec->l3_proto == htons(ETH_P_IPV6))
289                 eseg->swp_flags |= MLX5_ETH_WQE_SWP_OUTER_L3_IPV6;
290         if (swp_spec->l4_proto) {
291                 eseg->swp_outer_l4_offset = skb_transport_offset(skb) / 2;
292                 if (swp_spec->l4_proto == IPPROTO_UDP)
293                         eseg->swp_flags |= MLX5_ETH_WQE_SWP_OUTER_L4_UDP;
294         }
295
296         if (swp_spec->is_tun) {
297                 eseg->swp_inner_l3_offset = skb_inner_network_offset(skb) / 2;
298                 if (swp_spec->tun_l3_proto == htons(ETH_P_IPV6))
299                         eseg->swp_flags |= MLX5_ETH_WQE_SWP_INNER_L3_IPV6;
300         } else { /* typically for ipsec when xfrm mode != XFRM_MODE_TUNNEL */
301                 eseg->swp_inner_l3_offset = skb_network_offset(skb) / 2;
302                 if (swp_spec->l3_proto == htons(ETH_P_IPV6))
303                         eseg->swp_flags |= MLX5_ETH_WQE_SWP_INNER_L3_IPV6;
304         }
305         switch (swp_spec->tun_l4_proto) {
306         case IPPROTO_UDP:
307                 eseg->swp_flags |= MLX5_ETH_WQE_SWP_INNER_L4_UDP;
308                 /* fall through */
309         case IPPROTO_TCP:
310                 eseg->swp_inner_l4_offset = skb_inner_transport_offset(skb) / 2;
311                 break;
312         }
313 }
314
315 static inline u16 mlx5e_stop_room_for_wqe(u16 wqe_size)
316 {
317         BUILD_BUG_ON(PAGE_SIZE / MLX5_SEND_WQE_BB < MLX5_SEND_WQE_MAX_WQEBBS);
318
319         /* A WQE must not cross the page boundary, hence two conditions:
320          * 1. Its size must not exceed the page size.
321          * 2. If the WQE size is X, and the space remaining in a page is less
322          *    than X, this space needs to be padded with NOPs. So, one WQE of
323          *    size X may require up to X-1 WQEBBs of padding, which makes the
324          *    stop room of X-1 + X.
325          * WQE size is also limited by the hardware limit.
326          */
327
328         if (__builtin_constant_p(wqe_size))
329                 BUILD_BUG_ON(wqe_size > MLX5_SEND_WQE_MAX_WQEBBS);
330         else
331                 WARN_ON_ONCE(wqe_size > MLX5_SEND_WQE_MAX_WQEBBS);
332
333         return wqe_size * 2 - 1;
334 }
335
336 #endif