mbuf: extend meaning of QinQ stripped bit
[dpdk.git] / drivers / net / bnxt / bnxt_rxtx_vec_neon.c
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2019-2020 Broadcom All rights reserved. */
3
4 #include <inttypes.h>
5 #include <stdbool.h>
6
7 #include <rte_bitmap.h>
8 #include <rte_byteorder.h>
9 #include <rte_malloc.h>
10 #include <rte_memory.h>
11 #include <rte_vect.h>
12
13 #include "bnxt.h"
14 #include "bnxt_cpr.h"
15 #include "bnxt_ring.h"
16 #include "bnxt_rxr.h"
17 #include "bnxt_rxq.h"
18 #include "hsi_struct_def_dpdk.h"
19 #include "bnxt_rxtx_vec_common.h"
20
21 #include "bnxt_txq.h"
22 #include "bnxt_txr.h"
23
24 /*
25  * RX Ring handling
26  */
27
28 static inline void
29 bnxt_rxq_rearm(struct bnxt_rx_queue *rxq, struct bnxt_rx_ring_info *rxr)
30 {
31         struct rx_prod_pkt_bd *rxbds = &rxr->rx_desc_ring[rxq->rxrearm_start];
32         struct bnxt_sw_rx_bd *rx_bufs = &rxr->rx_buf_ring[rxq->rxrearm_start];
33         struct rte_mbuf *mb0, *mb1;
34         int i;
35
36         const uint64x2_t hdr_room = {0, RTE_PKTMBUF_HEADROOM};
37         const uint64x2_t addrmask = {0, UINT64_MAX};
38
39         /* Pull RTE_BNXT_RXQ_REARM_THRESH more mbufs into the software ring */
40         if (rte_mempool_get_bulk(rxq->mb_pool,
41                                  (void *)rx_bufs,
42                                  RTE_BNXT_RXQ_REARM_THRESH) < 0) {
43                 rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed +=
44                         RTE_BNXT_RXQ_REARM_THRESH;
45
46                 return;
47         }
48
49         /* Initialize the mbufs in vector, process 2 mbufs in one loop */
50         for (i = 0; i < RTE_BNXT_RXQ_REARM_THRESH; i += 2, rx_bufs += 2) {
51                 uint64x2_t buf_addr0, buf_addr1;
52                 uint64x2_t rxbd0, rxbd1;
53
54                 mb0 = rx_bufs[0].mbuf;
55                 mb1 = rx_bufs[1].mbuf;
56
57                 /* Load address fields from both mbufs */
58                 buf_addr0 = vld1q_u64((uint64_t *)&mb0->buf_addr);
59                 buf_addr1 = vld1q_u64((uint64_t *)&mb1->buf_addr);
60
61                 /* Load both rx descriptors (preserving some existing fields) */
62                 rxbd0 = vld1q_u64((uint64_t *)(rxbds + 0));
63                 rxbd1 = vld1q_u64((uint64_t *)(rxbds + 1));
64
65                 /* Add default offset to buffer address. */
66                 buf_addr0 = vaddq_u64(buf_addr0, hdr_room);
67                 buf_addr1 = vaddq_u64(buf_addr1, hdr_room);
68
69                 /* Clear all fields except address. */
70                 buf_addr0 =  vandq_u64(buf_addr0, addrmask);
71                 buf_addr1 =  vandq_u64(buf_addr1, addrmask);
72
73                 /* Clear address field in descriptor. */
74                 rxbd0 = vbicq_u64(rxbd0, addrmask);
75                 rxbd1 = vbicq_u64(rxbd1, addrmask);
76
77                 /* Set address field in descriptor. */
78                 rxbd0 = vaddq_u64(rxbd0, buf_addr0);
79                 rxbd1 = vaddq_u64(rxbd1, buf_addr1);
80
81                 /* Store descriptors to memory. */
82                 vst1q_u64((uint64_t *)(rxbds++), rxbd0);
83                 vst1q_u64((uint64_t *)(rxbds++), rxbd1);
84         }
85
86         rxq->rxrearm_start += RTE_BNXT_RXQ_REARM_THRESH;
87         bnxt_db_write(&rxr->rx_db, rxq->rxrearm_start - 1);
88         if (rxq->rxrearm_start >= rxq->nb_rx_desc)
89                 rxq->rxrearm_start = 0;
90
91         rxq->rxrearm_nb -= RTE_BNXT_RXQ_REARM_THRESH;
92 }
93
94 static uint32_t
95 bnxt_parse_pkt_type(struct rx_pkt_cmpl *rxcmp, struct rx_pkt_cmpl_hi *rxcmp1)
96 {
97         uint32_t l3, pkt_type = 0;
98         uint32_t t_ipcs = 0, ip6 = 0, vlan = 0;
99         uint32_t flags_type;
100
101         vlan = !!(rxcmp1->flags2 &
102                 rte_cpu_to_le_32(RX_PKT_CMPL_FLAGS2_META_FORMAT_VLAN));
103         pkt_type |= vlan ? RTE_PTYPE_L2_ETHER_VLAN : RTE_PTYPE_L2_ETHER;
104
105         t_ipcs = !!(rxcmp1->flags2 &
106                 rte_cpu_to_le_32(RX_PKT_CMPL_FLAGS2_T_IP_CS_CALC));
107         ip6 = !!(rxcmp1->flags2 &
108                  rte_cpu_to_le_32(RX_PKT_CMPL_FLAGS2_IP_TYPE));
109
110         flags_type = rxcmp->flags_type &
111                 rte_cpu_to_le_32(RX_PKT_CMPL_FLAGS_ITYPE_MASK);
112
113         if (!t_ipcs && !ip6)
114                 l3 = RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
115         else if (!t_ipcs && ip6)
116                 l3 = RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
117         else if (t_ipcs && !ip6)
118                 l3 = RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN;
119         else
120                 l3 = RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN;
121
122         switch (flags_type) {
123         case RTE_LE32(RX_PKT_CMPL_FLAGS_ITYPE_ICMP):
124                 if (!t_ipcs)
125                         pkt_type |= l3 | RTE_PTYPE_L4_ICMP;
126                 else
127                         pkt_type |= l3 | RTE_PTYPE_INNER_L4_ICMP;
128                 break;
129
130         case RTE_LE32(RX_PKT_CMPL_FLAGS_ITYPE_TCP):
131                 if (!t_ipcs)
132                         pkt_type |= l3 | RTE_PTYPE_L4_TCP;
133                 else
134                         pkt_type |= l3 | RTE_PTYPE_INNER_L4_TCP;
135                 break;
136
137         case RTE_LE32(RX_PKT_CMPL_FLAGS_ITYPE_UDP):
138                 if (!t_ipcs)
139                         pkt_type |= l3 | RTE_PTYPE_L4_UDP;
140                 else
141                         pkt_type |= l3 | RTE_PTYPE_INNER_L4_UDP;
142                 break;
143
144         case RTE_LE32(RX_PKT_CMPL_FLAGS_ITYPE_IP):
145                 pkt_type |= l3;
146                 break;
147         }
148
149         return pkt_type;
150 }
151
152 static void
153 bnxt_parse_csum(struct rte_mbuf *mbuf, struct rx_pkt_cmpl_hi *rxcmp1)
154 {
155         uint32_t flags;
156
157         flags = flags2_0xf(rxcmp1);
158         /* IP Checksum */
159         if (likely(IS_IP_NONTUNNEL_PKT(flags))) {
160                 if (unlikely(RX_CMP_IP_CS_ERROR(rxcmp1)))
161                         mbuf->ol_flags |= PKT_RX_IP_CKSUM_BAD;
162                 else
163                         mbuf->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
164         } else if (IS_IP_TUNNEL_PKT(flags)) {
165                 if (unlikely(RX_CMP_IP_OUTER_CS_ERROR(rxcmp1) ||
166                              RX_CMP_IP_CS_ERROR(rxcmp1)))
167                         mbuf->ol_flags |= PKT_RX_IP_CKSUM_BAD;
168                 else
169                         mbuf->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
170         } else if (unlikely(RX_CMP_IP_CS_UNKNOWN(rxcmp1))) {
171                 mbuf->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
172         }
173
174         /* L4 Checksum */
175         if (likely(IS_L4_NONTUNNEL_PKT(flags))) {
176                 if (unlikely(RX_CMP_L4_INNER_CS_ERR2(rxcmp1)))
177                         mbuf->ol_flags |= PKT_RX_L4_CKSUM_BAD;
178                 else
179                         mbuf->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
180         } else if (IS_L4_TUNNEL_PKT(flags)) {
181                 if (unlikely(RX_CMP_L4_INNER_CS_ERR2(rxcmp1)))
182                         mbuf->ol_flags |= PKT_RX_L4_CKSUM_BAD;
183                 else
184                         mbuf->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
185                 if (unlikely(RX_CMP_L4_OUTER_CS_ERR2(rxcmp1))) {
186                         mbuf->ol_flags |= PKT_RX_OUTER_L4_CKSUM_BAD;
187                 } else if (unlikely(IS_L4_TUNNEL_PKT_ONLY_INNER_L4_CS
188                                     (flags))) {
189                         mbuf->ol_flags |= PKT_RX_OUTER_L4_CKSUM_UNKNOWN;
190                 } else {
191                         mbuf->ol_flags |= PKT_RX_OUTER_L4_CKSUM_GOOD;
192                 }
193         } else if (unlikely(RX_CMP_L4_CS_UNKNOWN(rxcmp1))) {
194                 mbuf->ol_flags |= PKT_RX_L4_CKSUM_UNKNOWN;
195         }
196 }
197
198 uint16_t
199 bnxt_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
200                    uint16_t nb_pkts)
201 {
202         struct bnxt_rx_queue *rxq = rx_queue;
203         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
204         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
205         uint32_t raw_cons = cpr->cp_raw_cons;
206         uint32_t cons;
207         int nb_rx_pkts = 0;
208         struct rx_pkt_cmpl *rxcmp;
209         bool evt = false;
210         const uint64x2_t mbuf_init = {rxq->mbuf_initializer, 0};
211         const uint8x16_t shuf_msk = {
212                 0xFF, 0xFF, 0xFF, 0xFF,    /* pkt_type (zeroes) */
213                 2, 3, 0xFF, 0xFF,          /* pkt_len */
214                 2, 3,                      /* data_len */
215                 0xFF, 0xFF,                /* vlan_tci (zeroes) */
216                 12, 13, 14, 15             /* rss hash */
217         };
218
219         /* If Rx Q was stopped return */
220         if (unlikely(!rxq->rx_started))
221                 return 0;
222
223         if (rxq->rxrearm_nb >= RTE_BNXT_RXQ_REARM_THRESH)
224                 bnxt_rxq_rearm(rxq, rxr);
225
226         /* Return no more than RTE_BNXT_MAX_RX_BURST per call. */
227         nb_pkts = RTE_MIN(nb_pkts, RTE_BNXT_MAX_RX_BURST);
228
229         /* Make nb_pkts an integer multiple of RTE_BNXT_DESCS_PER_LOOP */
230         nb_pkts = RTE_ALIGN_FLOOR(nb_pkts, RTE_BNXT_DESCS_PER_LOOP);
231         if (!nb_pkts)
232                 return 0;
233
234         /* Handle RX burst request */
235         while (1) {
236                 cons = RING_CMP(cpr->cp_ring_struct, raw_cons);
237
238                 rxcmp = (struct rx_pkt_cmpl *)&cpr->cp_desc_ring[cons];
239
240                 if (!CMP_VALID(rxcmp, raw_cons, cpr->cp_ring_struct))
241                         break;
242
243                 if (likely(CMP_TYPE(rxcmp) == RX_PKT_CMPL_TYPE_RX_L2)) {
244                         struct rx_pkt_cmpl_hi *rxcmp1;
245                         uint32_t tmp_raw_cons;
246                         uint16_t cp_cons;
247                         struct rte_mbuf *mbuf;
248                         uint64x2_t mm_rxcmp;
249                         uint8x16_t pkt_mb;
250
251                         tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
252                         cp_cons = RING_CMP(cpr->cp_ring_struct, tmp_raw_cons);
253                         rxcmp1 = (struct rx_pkt_cmpl_hi *)
254                                                 &cpr->cp_desc_ring[cp_cons];
255
256                         if (!CMP_VALID(rxcmp1, tmp_raw_cons,
257                                        cpr->cp_ring_struct))
258                                 break;
259
260                         raw_cons = tmp_raw_cons;
261                         cons = rxcmp->opaque;
262
263                         mbuf = rxr->rx_buf_ring[cons].mbuf;
264                         rte_prefetch0(mbuf);
265                         rxr->rx_buf_ring[cons].mbuf = NULL;
266
267                         /* Set constant fields from mbuf initializer. */
268                         vst1q_u64((uint64_t *)&mbuf->rearm_data, mbuf_init);
269
270                         /* Set mbuf pkt_len, data_len, and rss_hash fields. */
271                         mm_rxcmp = vld1q_u64((uint64_t *)rxcmp);
272                         pkt_mb = vqtbl1q_u8(vreinterpretq_u8_u64(mm_rxcmp),
273                                             shuf_msk);
274                         vst1q_u64((uint64_t *)&mbuf->rx_descriptor_fields1,
275                                   vreinterpretq_u64_u8(pkt_mb));
276
277                         rte_compiler_barrier();
278
279                         if (rxcmp->flags_type & RX_PKT_CMPL_FLAGS_RSS_VALID)
280                                 mbuf->ol_flags |= PKT_RX_RSS_HASH;
281
282                         if (rxcmp1->flags2 &
283                             RX_PKT_CMPL_FLAGS2_META_FORMAT_VLAN) {
284                                 mbuf->vlan_tci = rxcmp1->metadata &
285                                         (RX_PKT_CMPL_METADATA_VID_MASK |
286                                         RX_PKT_CMPL_METADATA_DE |
287                                         RX_PKT_CMPL_METADATA_PRI_MASK);
288                                 mbuf->ol_flags |=
289                                         PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
290                         }
291
292                         bnxt_parse_csum(mbuf, rxcmp1);
293                         mbuf->packet_type = bnxt_parse_pkt_type(rxcmp, rxcmp1);
294
295                         rx_pkts[nb_rx_pkts++] = mbuf;
296                 } else if (!BNXT_NUM_ASYNC_CPR(rxq->bp)) {
297                         evt =
298                         bnxt_event_hwrm_resp_handler(rxq->bp,
299                                                      (struct cmpl_base *)rxcmp);
300                 }
301
302                 raw_cons = NEXT_RAW_CMP(raw_cons);
303                 if (nb_rx_pkts == nb_pkts || evt)
304                         break;
305         }
306         rxr->rx_prod = RING_ADV(rxr->rx_ring_struct, rxr->rx_prod, nb_rx_pkts);
307
308         rxq->rxrearm_nb += nb_rx_pkts;
309         cpr->cp_raw_cons = raw_cons;
310         cpr->valid = !!(cpr->cp_raw_cons & cpr->cp_ring_struct->ring_size);
311         if (nb_rx_pkts || evt)
312                 bnxt_db_cq(cpr);
313
314         return nb_rx_pkts;
315 }
316
317 static void
318 bnxt_tx_cmp_vec(struct bnxt_tx_queue *txq, int nr_pkts)
319 {
320         struct bnxt_tx_ring_info *txr = txq->tx_ring;
321         struct rte_mbuf **free = txq->free;
322         uint16_t cons = txr->tx_cons;
323         unsigned int blk = 0;
324
325         while (nr_pkts--) {
326                 struct bnxt_sw_tx_bd *tx_buf;
327                 struct rte_mbuf *mbuf;
328
329                 tx_buf = &txr->tx_buf_ring[cons];
330                 cons = RING_NEXT(txr->tx_ring_struct, cons);
331                 mbuf = rte_pktmbuf_prefree_seg(tx_buf->mbuf);
332                 tx_buf->mbuf = NULL;
333
334                 if (blk && mbuf->pool != free[0]->pool) {
335                         rte_mempool_put_bulk(free[0]->pool, (void **)free, blk);
336                         blk = 0;
337                 }
338                 free[blk++] = mbuf;
339         }
340         if (blk)
341                 rte_mempool_put_bulk(free[0]->pool, (void **)free, blk);
342
343         txr->tx_cons = cons;
344 }
345
346 static void
347 bnxt_handle_tx_cp_vec(struct bnxt_tx_queue *txq)
348 {
349         struct bnxt_cp_ring_info *cpr = txq->cp_ring;
350         uint32_t raw_cons = cpr->cp_raw_cons;
351         uint32_t cons;
352         uint32_t nb_tx_pkts = 0;
353         struct tx_cmpl *txcmp;
354         struct cmpl_base *cp_desc_ring = cpr->cp_desc_ring;
355         struct bnxt_ring *cp_ring_struct = cpr->cp_ring_struct;
356         uint32_t ring_mask = cp_ring_struct->ring_mask;
357
358         do {
359                 cons = RING_CMPL(ring_mask, raw_cons);
360                 txcmp = (struct tx_cmpl *)&cp_desc_ring[cons];
361
362                 if (!CMP_VALID(txcmp, raw_cons, cp_ring_struct))
363                         break;
364
365                 if (likely(CMP_TYPE(txcmp) == TX_CMPL_TYPE_TX_L2))
366                         nb_tx_pkts += txcmp->opaque;
367                 else
368                         RTE_LOG_DP(ERR, PMD,
369                                    "Unhandled CMP type %02x\n",
370                                    CMP_TYPE(txcmp));
371                 raw_cons = NEXT_RAW_CMP(raw_cons);
372         } while (nb_tx_pkts < ring_mask);
373
374         cpr->valid = !!(raw_cons & cp_ring_struct->ring_size);
375         if (nb_tx_pkts) {
376                 bnxt_tx_cmp_vec(txq, nb_tx_pkts);
377                 cpr->cp_raw_cons = raw_cons;
378                 bnxt_db_cq(cpr);
379         }
380 }
381
382 static uint16_t
383 bnxt_xmit_fixed_burst_vec(void *tx_queue, struct rte_mbuf **tx_pkts,
384                           uint16_t nb_pkts)
385 {
386         struct bnxt_tx_queue *txq = tx_queue;
387         struct bnxt_tx_ring_info *txr = txq->tx_ring;
388         uint16_t prod = txr->tx_prod;
389         struct rte_mbuf *tx_mbuf;
390         struct tx_bd_long *txbd = NULL;
391         struct bnxt_sw_tx_bd *tx_buf;
392         uint16_t to_send;
393
394         nb_pkts = RTE_MIN(nb_pkts, bnxt_tx_avail(txq));
395
396         if (unlikely(nb_pkts == 0))
397                 return 0;
398
399         /* Handle TX burst request */
400         to_send = nb_pkts;
401         while (to_send) {
402                 tx_mbuf = *tx_pkts++;
403                 rte_prefetch0(tx_mbuf);
404
405                 tx_buf = &txr->tx_buf_ring[prod];
406                 tx_buf->mbuf = tx_mbuf;
407                 tx_buf->nr_bds = 1;
408
409                 txbd = &txr->tx_desc_ring[prod];
410                 txbd->address = tx_mbuf->buf_iova + tx_mbuf->data_off;
411                 txbd->len = tx_mbuf->data_len;
412                 txbd->flags_type = bnxt_xmit_flags_len(tx_mbuf->data_len,
413                                                        TX_BD_FLAGS_NOCMPL);
414                 prod = RING_NEXT(txr->tx_ring_struct, prod);
415                 to_send--;
416         }
417
418         /* Request a completion for last packet in burst */
419         if (txbd) {
420                 txbd->opaque = nb_pkts;
421                 txbd->flags_type &= ~TX_BD_LONG_FLAGS_NO_CMPL;
422         }
423
424         rte_compiler_barrier();
425         bnxt_db_write(&txr->tx_db, prod);
426
427         txr->tx_prod = prod;
428
429         return nb_pkts;
430 }
431
432 uint16_t
433 bnxt_xmit_pkts_vec(void *tx_queue, struct rte_mbuf **tx_pkts,
434                    uint16_t nb_pkts)
435 {
436         int nb_sent = 0;
437         struct bnxt_tx_queue *txq = tx_queue;
438
439         /* Tx queue was stopped; wait for it to be restarted */
440         if (unlikely(!txq->tx_started)) {
441                 PMD_DRV_LOG(DEBUG, "Tx q stopped;return\n");
442                 return 0;
443         }
444
445         /* Handle TX completions */
446         if (bnxt_tx_bds_in_hw(txq) >= txq->tx_free_thresh)
447                 bnxt_handle_tx_cp_vec(txq);
448
449         while (nb_pkts) {
450                 uint16_t ret, num;
451
452                 num = RTE_MIN(nb_pkts, RTE_BNXT_MAX_TX_BURST);
453                 ret = bnxt_xmit_fixed_burst_vec(tx_queue,
454                                                 &tx_pkts[nb_sent],
455                                                 num);
456                 nb_sent += ret;
457                 nb_pkts -= ret;
458                 if (ret < num)
459                         break;
460         }
461
462         return nb_sent;
463 }
464
465 int __rte_cold
466 bnxt_rxq_vec_setup(struct bnxt_rx_queue *rxq)
467 {
468         return bnxt_rxq_vec_setup_common(rxq);
469 }