3a0d73af20b4e023a4beba478b5ab10ef0003ed2
[dpdk.git] / drivers / net / bnxt / bnxt_txr.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2018 Broadcom
3  * All rights reserved.
4  */
5
6 #include <inttypes.h>
7
8 #include <rte_byteorder.h>
9 #include <rte_malloc.h>
10
11 #include "bnxt.h"
12 #include "bnxt_cpr.h"
13 #include "bnxt_ring.h"
14 #include "bnxt_txq.h"
15 #include "bnxt_txr.h"
16 #include "hsi_struct_def_dpdk.h"
17 #include <stdbool.h>
18
19 /*
20  * TX Ring handling
21  */
22
23 void bnxt_free_tx_rings(struct bnxt *bp)
24 {
25         int i;
26
27         for (i = 0; i < (int)bp->tx_nr_rings; i++) {
28                 struct bnxt_tx_queue *txq = bp->tx_queues[i];
29
30                 if (!txq)
31                         continue;
32
33                 bnxt_free_ring(txq->tx_ring->tx_ring_struct);
34                 rte_free(txq->tx_ring->tx_ring_struct);
35                 rte_free(txq->tx_ring);
36
37                 bnxt_free_ring(txq->cp_ring->cp_ring_struct);
38                 rte_free(txq->cp_ring->cp_ring_struct);
39                 rte_free(txq->cp_ring);
40
41                 rte_free(txq);
42                 bp->tx_queues[i] = NULL;
43         }
44 }
45
46 int bnxt_init_one_tx_ring(struct bnxt_tx_queue *txq)
47 {
48         struct bnxt_tx_ring_info *txr = txq->tx_ring;
49         struct bnxt_ring *ring = txr->tx_ring_struct;
50
51         txq->tx_wake_thresh = ring->ring_size / 2;
52         ring->fw_ring_id = INVALID_HW_RING_ID;
53
54         return 0;
55 }
56
57 int bnxt_init_tx_ring_struct(struct bnxt_tx_queue *txq, unsigned int socket_id)
58 {
59         struct bnxt_cp_ring_info *cpr;
60         struct bnxt_tx_ring_info *txr;
61         struct bnxt_ring *ring;
62
63         txr = rte_zmalloc_socket("bnxt_tx_ring",
64                                  sizeof(struct bnxt_tx_ring_info),
65                                  RTE_CACHE_LINE_SIZE, socket_id);
66         if (txr == NULL)
67                 return -ENOMEM;
68         txq->tx_ring = txr;
69
70         ring = rte_zmalloc_socket("bnxt_tx_ring_struct",
71                                   sizeof(struct bnxt_ring),
72                                   RTE_CACHE_LINE_SIZE, socket_id);
73         if (ring == NULL)
74                 return -ENOMEM;
75         txr->tx_ring_struct = ring;
76         ring->ring_size = rte_align32pow2(txq->nb_tx_desc);
77         ring->ring_mask = ring->ring_size - 1;
78         ring->bd = (void *)txr->tx_desc_ring;
79         ring->bd_dma = txr->tx_desc_mapping;
80         ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_tx_bd);
81         ring->vmem = (void **)&txr->tx_buf_ring;
82
83         cpr = rte_zmalloc_socket("bnxt_tx_ring",
84                                  sizeof(struct bnxt_cp_ring_info),
85                                  RTE_CACHE_LINE_SIZE, socket_id);
86         if (cpr == NULL)
87                 return -ENOMEM;
88         txq->cp_ring = cpr;
89
90         ring = rte_zmalloc_socket("bnxt_tx_ring_struct",
91                                   sizeof(struct bnxt_ring),
92                                   RTE_CACHE_LINE_SIZE, socket_id);
93         if (ring == NULL)
94                 return -ENOMEM;
95         cpr->cp_ring_struct = ring;
96         ring->ring_size = txr->tx_ring_struct->ring_size;
97         ring->ring_mask = ring->ring_size - 1;
98         ring->bd = (void *)cpr->cp_desc_ring;
99         ring->bd_dma = cpr->cp_desc_mapping;
100         ring->vmem_size = 0;
101         ring->vmem = NULL;
102
103         return 0;
104 }
105
106 static inline uint32_t bnxt_tx_avail(struct bnxt_tx_ring_info *txr)
107 {
108         /* Tell compiler to fetch tx indices from memory. */
109         rte_compiler_barrier();
110
111         return txr->tx_ring_struct->ring_size -
112                 ((txr->tx_prod - txr->tx_cons) &
113                         txr->tx_ring_struct->ring_mask) - 1;
114 }
115
116 static uint16_t bnxt_start_xmit(struct rte_mbuf *tx_pkt,
117                                 struct bnxt_tx_queue *txq,
118                                 uint16_t *coal_pkts,
119                                 uint16_t *cmpl_next)
120 {
121         struct bnxt_tx_ring_info *txr = txq->tx_ring;
122         struct tx_bd_long *txbd;
123         struct tx_bd_long_hi *txbd1 = NULL;
124         uint32_t vlan_tag_flags, cfa_action;
125         bool long_bd = false;
126         struct rte_mbuf *m_seg;
127         struct bnxt_sw_tx_bd *tx_buf;
128         static const uint32_t lhint_arr[4] = {
129                 TX_BD_LONG_FLAGS_LHINT_LT512,
130                 TX_BD_LONG_FLAGS_LHINT_LT1K,
131                 TX_BD_LONG_FLAGS_LHINT_LT2K,
132                 TX_BD_LONG_FLAGS_LHINT_LT2K
133         };
134
135         if (tx_pkt->ol_flags & (PKT_TX_TCP_SEG | PKT_TX_TCP_CKSUM |
136                                 PKT_TX_UDP_CKSUM | PKT_TX_IP_CKSUM |
137                                 PKT_TX_VLAN_PKT | PKT_TX_OUTER_IP_CKSUM |
138                                 PKT_TX_TUNNEL_GRE | PKT_TX_TUNNEL_VXLAN |
139                                 PKT_TX_TUNNEL_GENEVE))
140                 long_bd = true;
141
142         tx_buf = &txr->tx_buf_ring[txr->tx_prod];
143         tx_buf->mbuf = tx_pkt;
144         tx_buf->nr_bds = long_bd + tx_pkt->nb_segs;
145
146         if (unlikely(bnxt_tx_avail(txr) < tx_buf->nr_bds))
147                 return -ENOMEM;
148
149         txbd = &txr->tx_desc_ring[txr->tx_prod];
150         txbd->opaque = *coal_pkts;
151         txbd->flags_type = tx_buf->nr_bds << TX_BD_LONG_FLAGS_BD_CNT_SFT;
152         txbd->flags_type |= TX_BD_SHORT_FLAGS_COAL_NOW;
153         if (!*cmpl_next) {
154                 txbd->flags_type |= TX_BD_LONG_FLAGS_NO_CMPL;
155         } else {
156                 *coal_pkts = 0;
157                 *cmpl_next = false;
158         }
159         txbd->len = tx_pkt->data_len;
160         if (tx_pkt->pkt_len >= 2014)
161                 txbd->flags_type |= TX_BD_LONG_FLAGS_LHINT_GTE2K;
162         else
163                 txbd->flags_type |= lhint_arr[tx_pkt->pkt_len >> 9];
164         txbd->address = rte_cpu_to_le_64(rte_mbuf_data_iova(tx_buf->mbuf));
165
166         if (long_bd) {
167                 txbd->flags_type |= TX_BD_LONG_TYPE_TX_BD_LONG;
168                 vlan_tag_flags = 0;
169                 cfa_action = 0;
170                 if (tx_buf->mbuf->ol_flags & PKT_TX_VLAN_PKT) {
171                         /* shurd: Should this mask at
172                          * TX_BD_LONG_CFA_META_VLAN_VID_MASK?
173                          */
174                         vlan_tag_flags = TX_BD_LONG_CFA_META_KEY_VLAN_TAG |
175                                 tx_buf->mbuf->vlan_tci;
176                         /* Currently supports 8021Q, 8021AD vlan offloads
177                          * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
178                          */
179                         /* DPDK only supports 802.11q VLAN packets */
180                         vlan_tag_flags |=
181                                         TX_BD_LONG_CFA_META_VLAN_TPID_TPID8100;
182                 }
183
184                 txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
185
186                 txbd1 = (struct tx_bd_long_hi *)
187                                         &txr->tx_desc_ring[txr->tx_prod];
188                 txbd1->lflags = 0;
189                 txbd1->cfa_meta = vlan_tag_flags;
190                 txbd1->cfa_action = cfa_action;
191
192                 if (tx_pkt->ol_flags & PKT_TX_TCP_SEG) {
193                         uint16_t hdr_size;
194
195                         /* TSO */
196                         txbd1->lflags |= TX_BD_LONG_LFLAGS_LSO |
197                                          TX_BD_LONG_LFLAGS_T_IPID;
198                         hdr_size = tx_pkt->l2_len + tx_pkt->l3_len +
199                                         tx_pkt->l4_len + tx_pkt->outer_l2_len +
200                                         tx_pkt->outer_l3_len;
201                         /* The hdr_size is multiple of 16bit units not 8bit.
202                          * Hence divide by 2.
203                          */
204                         txbd1->hdr_size = hdr_size >> 1;
205                         txbd1->mss = tx_pkt->tso_segsz;
206
207                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_TCP_UDP_CKSUM) ==
208                            PKT_TX_OIP_IIP_TCP_UDP_CKSUM) {
209                         /* Outer IP, Inner IP, Inner TCP/UDP CSO */
210                         txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
211                         txbd1->mss = 0;
212                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_TCP_CKSUM) ==
213                            PKT_TX_OIP_IIP_TCP_CKSUM) {
214                         /* Outer IP, Inner IP, Inner TCP/UDP CSO */
215                         txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
216                         txbd1->mss = 0;
217                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_UDP_CKSUM) ==
218                            PKT_TX_OIP_IIP_UDP_CKSUM) {
219                         /* Outer IP, Inner IP, Inner TCP/UDP CSO */
220                         txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
221                         txbd1->mss = 0;
222                 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_TCP_UDP_CKSUM) ==
223                            PKT_TX_IIP_TCP_UDP_CKSUM) {
224                         /* (Inner) IP, (Inner) TCP/UDP CSO */
225                         txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
226                         txbd1->mss = 0;
227                 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_UDP_CKSUM) ==
228                            PKT_TX_IIP_UDP_CKSUM) {
229                         /* (Inner) IP, (Inner) TCP/UDP CSO */
230                         txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
231                         txbd1->mss = 0;
232                 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_TCP_CKSUM) ==
233                            PKT_TX_IIP_TCP_CKSUM) {
234                         /* (Inner) IP, (Inner) TCP/UDP CSO */
235                         txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
236                         txbd1->mss = 0;
237                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_TCP_UDP_CKSUM) ==
238                            PKT_TX_OIP_TCP_UDP_CKSUM) {
239                         /* Outer IP, (Inner) TCP/UDP CSO */
240                         txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
241                         txbd1->mss = 0;
242                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_UDP_CKSUM) ==
243                            PKT_TX_OIP_UDP_CKSUM) {
244                         /* Outer IP, (Inner) TCP/UDP CSO */
245                         txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
246                         txbd1->mss = 0;
247                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_TCP_CKSUM) ==
248                            PKT_TX_OIP_TCP_CKSUM) {
249                         /* Outer IP, (Inner) TCP/UDP CSO */
250                         txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
251                         txbd1->mss = 0;
252                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_CKSUM) ==
253                            PKT_TX_OIP_IIP_CKSUM) {
254                         /* Outer IP, Inner IP CSO */
255                         txbd1->lflags |= TX_BD_FLG_TIP_IP_CHKSUM;
256                         txbd1->mss = 0;
257                 } else if ((tx_pkt->ol_flags & PKT_TX_TCP_UDP_CKSUM) ==
258                            PKT_TX_TCP_UDP_CKSUM) {
259                         /* TCP/UDP CSO */
260                         txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
261                         txbd1->mss = 0;
262                 } else if ((tx_pkt->ol_flags & PKT_TX_TCP_CKSUM) ==
263                            PKT_TX_TCP_CKSUM) {
264                         /* TCP/UDP CSO */
265                         txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
266                         txbd1->mss = 0;
267                 } else if ((tx_pkt->ol_flags & PKT_TX_UDP_CKSUM) ==
268                            PKT_TX_UDP_CKSUM) {
269                         /* TCP/UDP CSO */
270                         txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
271                         txbd1->mss = 0;
272                 } else if ((tx_pkt->ol_flags & PKT_TX_IP_CKSUM) ==
273                            PKT_TX_IP_CKSUM) {
274                         /* IP CSO */
275                         txbd1->lflags |= TX_BD_LONG_LFLAGS_IP_CHKSUM;
276                         txbd1->mss = 0;
277                 } else if ((tx_pkt->ol_flags & PKT_TX_OUTER_IP_CKSUM) ==
278                            PKT_TX_OUTER_IP_CKSUM) {
279                         /* IP CSO */
280                         txbd1->lflags |= TX_BD_LONG_LFLAGS_T_IP_CHKSUM;
281                         txbd1->mss = 0;
282                 }
283         } else {
284                 txbd->flags_type |= TX_BD_SHORT_TYPE_TX_BD_SHORT;
285         }
286
287         m_seg = tx_pkt->next;
288         /* i is set at the end of the if(long_bd) block */
289         while (m_seg) {
290                 txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
291                 tx_buf = &txr->tx_buf_ring[txr->tx_prod];
292
293                 txbd = &txr->tx_desc_ring[txr->tx_prod];
294                 txbd->address = rte_cpu_to_le_64(rte_mbuf_data_iova(m_seg));
295                 txbd->flags_type |= TX_BD_SHORT_TYPE_TX_BD_SHORT;
296                 txbd->len = m_seg->data_len;
297
298                 m_seg = m_seg->next;
299         }
300
301         txbd->flags_type |= TX_BD_LONG_FLAGS_PACKET_END;
302
303         txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
304
305         return 0;
306 }
307
308 static void bnxt_tx_cmp(struct bnxt_tx_queue *txq, int nr_pkts)
309 {
310         struct bnxt_tx_ring_info *txr = txq->tx_ring;
311         uint16_t cons = txr->tx_cons;
312         int i, j;
313
314         for (i = 0; i < nr_pkts; i++) {
315                 struct bnxt_sw_tx_bd *tx_buf;
316                 struct rte_mbuf *mbuf;
317
318                 tx_buf = &txr->tx_buf_ring[cons];
319                 cons = RING_NEXT(txr->tx_ring_struct, cons);
320                 mbuf = tx_buf->mbuf;
321                 tx_buf->mbuf = NULL;
322
323                 /* EW - no need to unmap DMA memory? */
324
325                 for (j = 1; j < tx_buf->nr_bds; j++)
326                         cons = RING_NEXT(txr->tx_ring_struct, cons);
327                 rte_pktmbuf_free(mbuf);
328         }
329
330         txr->tx_cons = cons;
331 }
332
333 static int bnxt_handle_tx_cp(struct bnxt_tx_queue *txq)
334 {
335         struct bnxt_cp_ring_info *cpr = txq->cp_ring;
336         uint32_t raw_cons = cpr->cp_raw_cons;
337         uint32_t cons;
338         uint32_t nb_tx_pkts = 0;
339         struct tx_cmpl *txcmp;
340         struct cmpl_base *cp_desc_ring = cpr->cp_desc_ring;
341         struct bnxt_ring *cp_ring_struct = cpr->cp_ring_struct;
342         uint32_t ring_mask = cp_ring_struct->ring_mask;
343         uint32_t opaque = 0;
344
345         if (((txq->tx_ring->tx_prod - txq->tx_ring->tx_cons) &
346                 txq->tx_ring->tx_ring_struct->ring_mask) < txq->tx_free_thresh)
347                 return 0;
348
349         do {
350                 cons = RING_CMPL(ring_mask, raw_cons);
351                 txcmp = (struct tx_cmpl *)&cpr->cp_desc_ring[cons];
352                 rte_prefetch_non_temporal(&cp_desc_ring[(cons + 2) &
353                                                         ring_mask]);
354
355                 if (!CMPL_VALID(txcmp, cpr->valid))
356                         break;
357                 opaque = rte_cpu_to_le_32(txcmp->opaque);
358                 NEXT_CMPL(cpr, cons, cpr->valid, 1);
359                 rte_prefetch0(&cp_desc_ring[cons]);
360
361                 if (CMP_TYPE(txcmp) == TX_CMPL_TYPE_TX_L2)
362                         nb_tx_pkts += opaque;
363                 else
364                         RTE_LOG_DP(ERR, PMD,
365                                         "Unhandled CMP type %02x\n",
366                                         CMP_TYPE(txcmp));
367                 raw_cons = cons;
368         } while (nb_tx_pkts < ring_mask);
369
370         if (nb_tx_pkts) {
371                 bnxt_tx_cmp(txq, nb_tx_pkts);
372                 cpr->cp_raw_cons = raw_cons;
373                 B_CP_DB(cpr, cpr->cp_raw_cons, ring_mask);
374         }
375
376         return nb_tx_pkts;
377 }
378
379 uint16_t bnxt_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
380                                uint16_t nb_pkts)
381 {
382         struct bnxt_tx_queue *txq = tx_queue;
383         uint16_t nb_tx_pkts = 0;
384         uint16_t coal_pkts = 0;
385         uint16_t cmpl_next = txq->cmpl_next;
386
387         /* Handle TX completions */
388         bnxt_handle_tx_cp(txq);
389
390         /* Tx queue was stopped; wait for it to be restarted */
391         if (txq->tx_deferred_start) {
392                 PMD_DRV_LOG(DEBUG, "Tx q stopped;return\n");
393                 return 0;
394         }
395
396         txq->cmpl_next = 0;
397         /* Handle TX burst request */
398         for (nb_tx_pkts = 0; nb_tx_pkts < nb_pkts; nb_tx_pkts++) {
399                 int rc;
400
401                 /* Request a completion on first and last packet */
402                 cmpl_next |= (nb_pkts == nb_tx_pkts + 1);
403                 coal_pkts++;
404                 rc = bnxt_start_xmit(tx_pkts[nb_tx_pkts], txq,
405                                 &coal_pkts, &cmpl_next);
406
407                 if (unlikely(rc)) {
408                         /* Request a completion in next cycle */
409                         txq->cmpl_next = 1;
410                         break;
411                 }
412         }
413
414         if (nb_tx_pkts)
415                 B_TX_DB(txq->tx_ring->tx_doorbell, txq->tx_ring->tx_prod);
416
417         return nb_tx_pkts;
418 }
419
420 int bnxt_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
421 {
422         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
423         struct bnxt_tx_queue *txq = bp->tx_queues[tx_queue_id];
424
425         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
426         txq->tx_deferred_start = false;
427         PMD_DRV_LOG(DEBUG, "Tx queue started\n");
428
429         return 0;
430 }
431
432 int bnxt_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
433 {
434         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
435         struct bnxt_tx_queue *txq = bp->tx_queues[tx_queue_id];
436
437         /* Handle TX completions */
438         bnxt_handle_tx_cp(txq);
439
440         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
441         txq->tx_deferred_start = true;
442         PMD_DRV_LOG(DEBUG, "Tx queue stopped\n");
443
444         return 0;
445 }