c9d308e919655543091092dc1e4102b71db6b60e
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / en / xdp.c
1 /*
2  * Copyright (c) 2018, 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/bpf_trace.h>
34 #include <net/xdp_sock_drv.h>
35 #include "en/xdp.h"
36 #include "en/params.h"
37
38 int mlx5e_xdp_max_mtu(struct mlx5e_params *params, struct mlx5e_xsk_param *xsk)
39 {
40         int hr = mlx5e_get_linear_rq_headroom(params, xsk);
41
42         /* Let S := SKB_DATA_ALIGN(sizeof(struct skb_shared_info)).
43          * The condition checked in mlx5e_rx_is_linear_skb is:
44          *   SKB_DATA_ALIGN(sw_mtu + hard_mtu + hr) + S <= PAGE_SIZE         (1)
45          *   (Note that hw_mtu == sw_mtu + hard_mtu.)
46          * What is returned from this function is:
47          *   max_mtu = PAGE_SIZE - S - hr - hard_mtu                         (2)
48          * After assigning sw_mtu := max_mtu, the left side of (1) turns to
49          * SKB_DATA_ALIGN(PAGE_SIZE - S) + S, which is equal to PAGE_SIZE,
50          * because both PAGE_SIZE and S are already aligned. Any number greater
51          * than max_mtu would make the left side of (1) greater than PAGE_SIZE,
52          * so max_mtu is the maximum MTU allowed.
53          */
54
55         return MLX5E_HW2SW_MTU(params, SKB_MAX_HEAD(hr));
56 }
57
58 static inline bool
59 mlx5e_xmit_xdp_buff(struct mlx5e_xdpsq *sq, struct mlx5e_rq *rq,
60                     struct mlx5e_dma_info *di, struct xdp_buff *xdp)
61 {
62         struct mlx5e_xdp_xmit_data xdptxd;
63         struct mlx5e_xdp_info xdpi;
64         struct xdp_frame *xdpf;
65         dma_addr_t dma_addr;
66
67         xdpf = xdp_convert_buff_to_frame(xdp);
68         if (unlikely(!xdpf))
69                 return false;
70
71         xdptxd.data = xdpf->data;
72         xdptxd.len  = xdpf->len;
73
74         if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL) {
75                 /* The xdp_buff was in the UMEM and was copied into a newly
76                  * allocated page. The UMEM page was returned via the ZCA, and
77                  * this new page has to be mapped at this point and has to be
78                  * unmapped and returned via xdp_return_frame on completion.
79                  */
80
81                 /* Prevent double recycling of the UMEM page. Even in case this
82                  * function returns false, the xdp_buff shouldn't be recycled,
83                  * as it was already done in xdp_convert_zc_to_xdp_frame.
84                  */
85                 __set_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags); /* non-atomic */
86
87                 xdpi.mode = MLX5E_XDP_XMIT_MODE_FRAME;
88
89                 dma_addr = dma_map_single(sq->pdev, xdptxd.data, xdptxd.len,
90                                           DMA_TO_DEVICE);
91                 if (dma_mapping_error(sq->pdev, dma_addr)) {
92                         xdp_return_frame(xdpf);
93                         return false;
94                 }
95
96                 xdptxd.dma_addr     = dma_addr;
97                 xdpi.frame.xdpf     = xdpf;
98                 xdpi.frame.dma_addr = dma_addr;
99         } else {
100                 /* Driver assumes that xdp_convert_buff_to_frame returns
101                  * an xdp_frame that points to the same memory region as
102                  * the original xdp_buff. It allows to map the memory only
103                  * once and to use the DMA_BIDIRECTIONAL mode.
104                  */
105
106                 xdpi.mode = MLX5E_XDP_XMIT_MODE_PAGE;
107
108                 dma_addr = di->addr + (xdpf->data - (void *)xdpf);
109                 dma_sync_single_for_device(sq->pdev, dma_addr, xdptxd.len,
110                                            DMA_TO_DEVICE);
111
112                 xdptxd.dma_addr = dma_addr;
113                 xdpi.page.rq    = rq;
114                 xdpi.page.di    = *di;
115         }
116
117         return sq->xmit_xdp_frame(sq, &xdptxd, &xdpi, 0);
118 }
119
120 /* returns true if packet was consumed by xdp */
121 bool mlx5e_xdp_handle(struct mlx5e_rq *rq, struct mlx5e_dma_info *di,
122                       u32 *len, struct xdp_buff *xdp)
123 {
124         struct bpf_prog *prog = READ_ONCE(rq->xdp_prog);
125         u32 act;
126         int err;
127
128         if (!prog)
129                 return false;
130
131         act = bpf_prog_run_xdp(prog, xdp);
132         switch (act) {
133         case XDP_PASS:
134                 *len = xdp->data_end - xdp->data;
135                 return false;
136         case XDP_TX:
137                 if (unlikely(!mlx5e_xmit_xdp_buff(rq->xdpsq, rq, di, xdp)))
138                         goto xdp_abort;
139                 __set_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags); /* non-atomic */
140                 return true;
141         case XDP_REDIRECT:
142                 /* When XDP enabled then page-refcnt==1 here */
143                 err = xdp_do_redirect(rq->netdev, xdp, prog);
144                 if (unlikely(err))
145                         goto xdp_abort;
146                 __set_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags);
147                 __set_bit(MLX5E_RQ_FLAG_XDP_REDIRECT, rq->flags);
148                 if (xdp->rxq->mem.type != MEM_TYPE_XSK_BUFF_POOL)
149                         mlx5e_page_dma_unmap(rq, di);
150                 rq->stats->xdp_redirect++;
151                 return true;
152         default:
153                 bpf_warn_invalid_xdp_action(act);
154                 /* fall through */
155         case XDP_ABORTED:
156 xdp_abort:
157                 trace_xdp_exception(rq->netdev, prog, act);
158                 /* fall through */
159         case XDP_DROP:
160                 rq->stats->xdp_drop++;
161                 return true;
162         }
163 }
164
165 static u16 mlx5e_xdpsq_get_next_pi(struct mlx5e_xdpsq *sq, u16 size)
166 {
167         struct mlx5_wq_cyc *wq = &sq->wq;
168         u16 pi, contig_wqebbs;
169
170         pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
171         contig_wqebbs = mlx5_wq_cyc_get_contig_wqebbs(wq, pi);
172         if (unlikely(contig_wqebbs < size)) {
173                 struct mlx5e_xdp_wqe_info *wi, *edge_wi;
174
175                 wi = &sq->db.wqe_info[pi];
176                 edge_wi = wi + contig_wqebbs;
177
178                 /* Fill SQ frag edge with NOPs to avoid WQE wrapping two pages. */
179                 for (; wi < edge_wi; wi++) {
180                         *wi = (struct mlx5e_xdp_wqe_info) {
181                                 .num_wqebbs = 1,
182                                 .num_pkts = 0,
183                         };
184                         mlx5e_post_nop(wq, sq->sqn, &sq->pc);
185                 }
186                 sq->stats->nops += contig_wqebbs;
187
188                 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
189         }
190
191         return pi;
192 }
193
194 static void mlx5e_xdp_mpwqe_session_start(struct mlx5e_xdpsq *sq)
195 {
196         struct mlx5e_xdp_mpwqe *session = &sq->mpwqe;
197         struct mlx5e_xdpsq_stats *stats = sq->stats;
198         u16 pi;
199
200         pi = mlx5e_xdpsq_get_next_pi(sq, MLX5_SEND_WQE_MAX_WQEBBS);
201         session->wqe = MLX5E_TX_FETCH_WQE(sq, pi);
202
203         prefetchw(session->wqe->data);
204         session->ds_count  = MLX5E_XDP_TX_EMPTY_DS_COUNT;
205         session->pkt_count = 0;
206
207         mlx5e_xdp_update_inline_state(sq);
208
209         stats->mpwqe++;
210 }
211
212 void mlx5e_xdp_mpwqe_complete(struct mlx5e_xdpsq *sq)
213 {
214         struct mlx5_wq_cyc       *wq    = &sq->wq;
215         struct mlx5e_xdp_mpwqe *session = &sq->mpwqe;
216         struct mlx5_wqe_ctrl_seg *cseg = &session->wqe->ctrl;
217         u16 ds_count = session->ds_count;
218         u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
219         struct mlx5e_xdp_wqe_info *wi = &sq->db.wqe_info[pi];
220
221         cseg->opmod_idx_opcode =
222                 cpu_to_be32((sq->pc << 8) | MLX5_OPCODE_ENHANCED_MPSW);
223         cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_count);
224
225         wi->num_wqebbs = DIV_ROUND_UP(ds_count, MLX5_SEND_WQEBB_NUM_DS);
226         wi->num_pkts   = session->pkt_count;
227
228         sq->pc += wi->num_wqebbs;
229
230         sq->doorbell_cseg = cseg;
231
232         session->wqe = NULL; /* Close session */
233 }
234
235 enum {
236         MLX5E_XDP_CHECK_OK = 1,
237         MLX5E_XDP_CHECK_START_MPWQE = 2,
238 };
239
240 static int mlx5e_xmit_xdp_frame_check_mpwqe(struct mlx5e_xdpsq *sq)
241 {
242         if (unlikely(!sq->mpwqe.wqe)) {
243                 const u16 stop_room = mlx5e_stop_room_for_wqe(MLX5_SEND_WQE_MAX_WQEBBS);
244
245                 if (unlikely(!mlx5e_wqc_has_room_for(&sq->wq, sq->cc, sq->pc,
246                                                      stop_room))) {
247                         /* SQ is full, ring doorbell */
248                         mlx5e_xmit_xdp_doorbell(sq);
249                         sq->stats->full++;
250                         return -EBUSY;
251                 }
252
253                 return MLX5E_XDP_CHECK_START_MPWQE;
254         }
255
256         return MLX5E_XDP_CHECK_OK;
257 }
258
259 static bool mlx5e_xmit_xdp_frame_mpwqe(struct mlx5e_xdpsq *sq,
260                                        struct mlx5e_xdp_xmit_data *xdptxd,
261                                        struct mlx5e_xdp_info *xdpi,
262                                        int check_result)
263 {
264         struct mlx5e_xdp_mpwqe *session = &sq->mpwqe;
265         struct mlx5e_xdpsq_stats *stats = sq->stats;
266
267         if (unlikely(xdptxd->len > sq->hw_mtu)) {
268                 stats->err++;
269                 return false;
270         }
271
272         if (!check_result)
273                 check_result = mlx5e_xmit_xdp_frame_check_mpwqe(sq);
274         if (unlikely(check_result < 0))
275                 return false;
276
277         if (check_result == MLX5E_XDP_CHECK_START_MPWQE) {
278                 /* Start the session when nothing can fail, so it's guaranteed
279                  * that if there is an active session, it has at least one dseg,
280                  * and it's safe to complete it at any time.
281                  */
282                 mlx5e_xdp_mpwqe_session_start(sq);
283         }
284
285         mlx5e_xdp_mpwqe_add_dseg(sq, xdptxd, stats);
286
287         if (unlikely(mlx5e_xdp_no_room_for_inline_pkt(session) ||
288                      session->ds_count == MLX5E_XDP_MPW_MAX_NUM_DS))
289                 mlx5e_xdp_mpwqe_complete(sq);
290
291         mlx5e_xdpi_fifo_push(&sq->db.xdpi_fifo, xdpi);
292         stats->xmit++;
293         return true;
294 }
295
296 static int mlx5e_xmit_xdp_frame_check(struct mlx5e_xdpsq *sq)
297 {
298         if (unlikely(!mlx5e_wqc_has_room_for(&sq->wq, sq->cc, sq->pc, 1))) {
299                 /* SQ is full, ring doorbell */
300                 mlx5e_xmit_xdp_doorbell(sq);
301                 sq->stats->full++;
302                 return -EBUSY;
303         }
304
305         return MLX5E_XDP_CHECK_OK;
306 }
307
308 static bool mlx5e_xmit_xdp_frame(struct mlx5e_xdpsq *sq,
309                                  struct mlx5e_xdp_xmit_data *xdptxd,
310                                  struct mlx5e_xdp_info *xdpi,
311                                  int check_result)
312 {
313         struct mlx5_wq_cyc       *wq   = &sq->wq;
314         u16                       pi   = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
315         struct mlx5e_tx_wqe      *wqe  = mlx5_wq_cyc_get_wqe(wq, pi);
316
317         struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
318         struct mlx5_wqe_eth_seg  *eseg = &wqe->eth;
319         struct mlx5_wqe_data_seg *dseg = wqe->data;
320
321         dma_addr_t dma_addr = xdptxd->dma_addr;
322         u32 dma_len = xdptxd->len;
323
324         struct mlx5e_xdpsq_stats *stats = sq->stats;
325
326         prefetchw(wqe);
327
328         if (unlikely(dma_len < MLX5E_XDP_MIN_INLINE || sq->hw_mtu < dma_len)) {
329                 stats->err++;
330                 return false;
331         }
332
333         if (!check_result)
334                 check_result = mlx5e_xmit_xdp_frame_check(sq);
335         if (unlikely(check_result < 0))
336                 return false;
337
338         cseg->fm_ce_se = 0;
339
340         /* copy the inline part if required */
341         if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
342                 memcpy(eseg->inline_hdr.start, xdptxd->data, MLX5E_XDP_MIN_INLINE);
343                 eseg->inline_hdr.sz = cpu_to_be16(MLX5E_XDP_MIN_INLINE);
344                 dma_len  -= MLX5E_XDP_MIN_INLINE;
345                 dma_addr += MLX5E_XDP_MIN_INLINE;
346                 dseg++;
347         }
348
349         /* write the dma part */
350         dseg->addr       = cpu_to_be64(dma_addr);
351         dseg->byte_count = cpu_to_be32(dma_len);
352
353         cseg->opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | MLX5_OPCODE_SEND);
354
355         sq->pc++;
356
357         sq->doorbell_cseg = cseg;
358
359         mlx5e_xdpi_fifo_push(&sq->db.xdpi_fifo, xdpi);
360         stats->xmit++;
361         return true;
362 }
363
364 static void mlx5e_free_xdpsq_desc(struct mlx5e_xdpsq *sq,
365                                   struct mlx5e_xdp_wqe_info *wi,
366                                   u32 *xsk_frames,
367                                   bool recycle)
368 {
369         struct mlx5e_xdp_info_fifo *xdpi_fifo = &sq->db.xdpi_fifo;
370         u16 i;
371
372         for (i = 0; i < wi->num_pkts; i++) {
373                 struct mlx5e_xdp_info xdpi = mlx5e_xdpi_fifo_pop(xdpi_fifo);
374
375                 switch (xdpi.mode) {
376                 case MLX5E_XDP_XMIT_MODE_FRAME:
377                         /* XDP_TX from the XSK RQ and XDP_REDIRECT */
378                         dma_unmap_single(sq->pdev, xdpi.frame.dma_addr,
379                                          xdpi.frame.xdpf->len, DMA_TO_DEVICE);
380                         xdp_return_frame(xdpi.frame.xdpf);
381                         break;
382                 case MLX5E_XDP_XMIT_MODE_PAGE:
383                         /* XDP_TX from the regular RQ */
384                         mlx5e_page_release_dynamic(xdpi.page.rq, &xdpi.page.di, recycle);
385                         break;
386                 case MLX5E_XDP_XMIT_MODE_XSK:
387                         /* AF_XDP send */
388                         (*xsk_frames)++;
389                         break;
390                 default:
391                         WARN_ON_ONCE(true);
392                 }
393         }
394 }
395
396 bool mlx5e_poll_xdpsq_cq(struct mlx5e_cq *cq)
397 {
398         struct mlx5e_xdpsq *sq;
399         struct mlx5_cqe64 *cqe;
400         u32 xsk_frames = 0;
401         u16 sqcc;
402         int i;
403
404         sq = container_of(cq, struct mlx5e_xdpsq, cq);
405
406         if (unlikely(!test_bit(MLX5E_SQ_STATE_ENABLED, &sq->state)))
407                 return false;
408
409         cqe = mlx5_cqwq_get_cqe(&cq->wq);
410         if (!cqe)
411                 return false;
412
413         /* sq->cc must be updated only after mlx5_cqwq_update_db_record(),
414          * otherwise a cq overrun may occur
415          */
416         sqcc = sq->cc;
417
418         i = 0;
419         do {
420                 struct mlx5e_xdp_wqe_info *wi;
421                 u16 wqe_counter, ci;
422                 bool last_wqe;
423
424                 mlx5_cqwq_pop(&cq->wq);
425
426                 wqe_counter = be16_to_cpu(cqe->wqe_counter);
427
428                 do {
429                         last_wqe = (sqcc == wqe_counter);
430                         ci = mlx5_wq_cyc_ctr2ix(&sq->wq, sqcc);
431                         wi = &sq->db.wqe_info[ci];
432
433                         sqcc += wi->num_wqebbs;
434
435                         mlx5e_free_xdpsq_desc(sq, wi, &xsk_frames, true);
436                 } while (!last_wqe);
437
438                 if (unlikely(get_cqe_opcode(cqe) != MLX5_CQE_REQ)) {
439                         netdev_WARN_ONCE(sq->channel->netdev,
440                                          "Bad OP in XDPSQ CQE: 0x%x\n",
441                                          get_cqe_opcode(cqe));
442                         mlx5e_dump_error_cqe(&sq->cq, sq->sqn,
443                                              (struct mlx5_err_cqe *)cqe);
444                         mlx5_wq_cyc_wqe_dump(&sq->wq, ci, wi->num_wqebbs);
445                 }
446         } while ((++i < MLX5E_TX_CQ_POLL_BUDGET) && (cqe = mlx5_cqwq_get_cqe(&cq->wq)));
447
448         if (xsk_frames)
449                 xsk_umem_complete_tx(sq->umem, xsk_frames);
450
451         sq->stats->cqes += i;
452
453         mlx5_cqwq_update_db_record(&cq->wq);
454
455         /* ensure cq space is freed before enabling more cqes */
456         wmb();
457
458         sq->cc = sqcc;
459         return (i == MLX5E_TX_CQ_POLL_BUDGET);
460 }
461
462 void mlx5e_free_xdpsq_descs(struct mlx5e_xdpsq *sq)
463 {
464         u32 xsk_frames = 0;
465
466         while (sq->cc != sq->pc) {
467                 struct mlx5e_xdp_wqe_info *wi;
468                 u16 ci;
469
470                 ci = mlx5_wq_cyc_ctr2ix(&sq->wq, sq->cc);
471                 wi = &sq->db.wqe_info[ci];
472
473                 sq->cc += wi->num_wqebbs;
474
475                 mlx5e_free_xdpsq_desc(sq, wi, &xsk_frames, false);
476         }
477
478         if (xsk_frames)
479                 xsk_umem_complete_tx(sq->umem, xsk_frames);
480 }
481
482 int mlx5e_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
483                    u32 flags)
484 {
485         struct mlx5e_priv *priv = netdev_priv(dev);
486         struct mlx5e_xdpsq *sq;
487         int drops = 0;
488         int sq_num;
489         int i;
490
491         /* this flag is sufficient, no need to test internal sq state */
492         if (unlikely(!mlx5e_xdp_tx_is_enabled(priv)))
493                 return -ENETDOWN;
494
495         if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
496                 return -EINVAL;
497
498         sq_num = smp_processor_id();
499
500         if (unlikely(sq_num >= priv->channels.num))
501                 return -ENXIO;
502
503         sq = &priv->channels.c[sq_num]->xdpsq;
504
505         for (i = 0; i < n; i++) {
506                 struct xdp_frame *xdpf = frames[i];
507                 struct mlx5e_xdp_xmit_data xdptxd;
508                 struct mlx5e_xdp_info xdpi;
509
510                 xdptxd.data = xdpf->data;
511                 xdptxd.len = xdpf->len;
512                 xdptxd.dma_addr = dma_map_single(sq->pdev, xdptxd.data,
513                                                  xdptxd.len, DMA_TO_DEVICE);
514
515                 if (unlikely(dma_mapping_error(sq->pdev, xdptxd.dma_addr))) {
516                         xdp_return_frame_rx_napi(xdpf);
517                         drops++;
518                         continue;
519                 }
520
521                 xdpi.mode           = MLX5E_XDP_XMIT_MODE_FRAME;
522                 xdpi.frame.xdpf     = xdpf;
523                 xdpi.frame.dma_addr = xdptxd.dma_addr;
524
525                 if (unlikely(!sq->xmit_xdp_frame(sq, &xdptxd, &xdpi, 0))) {
526                         dma_unmap_single(sq->pdev, xdptxd.dma_addr,
527                                          xdptxd.len, DMA_TO_DEVICE);
528                         xdp_return_frame_rx_napi(xdpf);
529                         drops++;
530                 }
531         }
532
533         if (flags & XDP_XMIT_FLUSH) {
534                 if (sq->mpwqe.wqe)
535                         mlx5e_xdp_mpwqe_complete(sq);
536                 mlx5e_xmit_xdp_doorbell(sq);
537         }
538
539         return n - drops;
540 }
541
542 void mlx5e_xdp_rx_poll_complete(struct mlx5e_rq *rq)
543 {
544         struct mlx5e_xdpsq *xdpsq = rq->xdpsq;
545
546         if (xdpsq->mpwqe.wqe)
547                 mlx5e_xdp_mpwqe_complete(xdpsq);
548
549         mlx5e_xmit_xdp_doorbell(xdpsq);
550
551         if (test_bit(MLX5E_RQ_FLAG_XDP_REDIRECT, rq->flags)) {
552                 xdp_do_flush_map();
553                 __clear_bit(MLX5E_RQ_FLAG_XDP_REDIRECT, rq->flags);
554         }
555 }
556
557 void mlx5e_set_xmit_fp(struct mlx5e_xdpsq *sq, bool is_mpw)
558 {
559         sq->xmit_xdp_frame_check = is_mpw ?
560                 mlx5e_xmit_xdp_frame_check_mpwqe : mlx5e_xmit_xdp_frame_check;
561         sq->xmit_xdp_frame = is_mpw ?
562                 mlx5e_xmit_xdp_frame_mpwqe : mlx5e_xmit_xdp_frame;
563 }
564