net/bnxt: check for error conditions in Tx path
[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         /* Check if number of Tx descriptors is above HW limit */
147         if (unlikely(tx_buf->nr_bds > BNXT_MAX_TSO_SEGS)) {
148                 PMD_DRV_LOG(ERR,
149                             "Num descriptors %d exceeds HW limit\n",
150                             tx_buf->nr_bds);
151                 return -ENOSPC;
152         }
153
154         /* If packet length is less than minimum packet size, pad it */
155         if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) < BNXT_MIN_PKT_SIZE)) {
156                 uint8_t pad = BNXT_MIN_PKT_SIZE - rte_pktmbuf_pkt_len(tx_pkt);
157                 char *seg = rte_pktmbuf_append(tx_pkt, pad);
158
159                 if (!seg) {
160                         PMD_DRV_LOG(ERR,
161                                     "Failed to pad mbuf by %d bytes\n",
162                                     pad);
163                         return -ENOMEM;
164                 }
165
166                 /* Note: data_len, pkt len are updated in rte_pktmbuf_append */
167                 memset(seg, 0, pad);
168         }
169
170         /* Check non zero data_len */
171         RTE_VERIFY(tx_pkt->data_len);
172
173         if (unlikely(bnxt_tx_avail(txr) < tx_buf->nr_bds))
174                 return -ENOMEM;
175
176         txbd = &txr->tx_desc_ring[txr->tx_prod];
177         txbd->opaque = *coal_pkts;
178         txbd->flags_type = tx_buf->nr_bds << TX_BD_LONG_FLAGS_BD_CNT_SFT;
179         txbd->flags_type |= TX_BD_SHORT_FLAGS_COAL_NOW;
180         if (!*cmpl_next) {
181                 txbd->flags_type |= TX_BD_LONG_FLAGS_NO_CMPL;
182         } else {
183                 *coal_pkts = 0;
184                 *cmpl_next = false;
185         }
186         txbd->len = tx_pkt->data_len;
187         if (tx_pkt->pkt_len >= 2014)
188                 txbd->flags_type |= TX_BD_LONG_FLAGS_LHINT_GTE2K;
189         else
190                 txbd->flags_type |= lhint_arr[tx_pkt->pkt_len >> 9];
191         txbd->address = rte_cpu_to_le_64(rte_mbuf_data_iova(tx_buf->mbuf));
192
193         if (long_bd) {
194                 txbd->flags_type |= TX_BD_LONG_TYPE_TX_BD_LONG;
195                 vlan_tag_flags = 0;
196                 cfa_action = 0;
197                 if (tx_buf->mbuf->ol_flags & PKT_TX_VLAN_PKT) {
198                         /* shurd: Should this mask at
199                          * TX_BD_LONG_CFA_META_VLAN_VID_MASK?
200                          */
201                         vlan_tag_flags = TX_BD_LONG_CFA_META_KEY_VLAN_TAG |
202                                 tx_buf->mbuf->vlan_tci;
203                         /* Currently supports 8021Q, 8021AD vlan offloads
204                          * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
205                          */
206                         /* DPDK only supports 802.11q VLAN packets */
207                         vlan_tag_flags |=
208                                         TX_BD_LONG_CFA_META_VLAN_TPID_TPID8100;
209                 }
210
211                 txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
212
213                 txbd1 = (struct tx_bd_long_hi *)
214                                         &txr->tx_desc_ring[txr->tx_prod];
215                 txbd1->lflags = 0;
216                 txbd1->cfa_meta = vlan_tag_flags;
217                 txbd1->cfa_action = cfa_action;
218
219                 if (tx_pkt->ol_flags & PKT_TX_TCP_SEG) {
220                         uint16_t hdr_size;
221
222                         /* TSO */
223                         txbd1->lflags |= TX_BD_LONG_LFLAGS_LSO |
224                                          TX_BD_LONG_LFLAGS_T_IPID;
225                         hdr_size = tx_pkt->l2_len + tx_pkt->l3_len +
226                                         tx_pkt->l4_len + tx_pkt->outer_l2_len +
227                                         tx_pkt->outer_l3_len;
228                         /* The hdr_size is multiple of 16bit units not 8bit.
229                          * Hence divide by 2.
230                          */
231                         txbd1->hdr_size = hdr_size >> 1;
232                         txbd1->mss = tx_pkt->tso_segsz;
233                         RTE_VERIFY(txbd1->mss);
234
235                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_TCP_UDP_CKSUM) ==
236                            PKT_TX_OIP_IIP_TCP_UDP_CKSUM) {
237                         /* Outer IP, Inner IP, Inner TCP/UDP CSO */
238                         txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
239                         txbd1->mss = 0;
240                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_TCP_CKSUM) ==
241                            PKT_TX_OIP_IIP_TCP_CKSUM) {
242                         /* Outer IP, Inner IP, Inner TCP/UDP CSO */
243                         txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
244                         txbd1->mss = 0;
245                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_UDP_CKSUM) ==
246                            PKT_TX_OIP_IIP_UDP_CKSUM) {
247                         /* Outer IP, Inner IP, Inner TCP/UDP CSO */
248                         txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
249                         txbd1->mss = 0;
250                 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_TCP_UDP_CKSUM) ==
251                            PKT_TX_IIP_TCP_UDP_CKSUM) {
252                         /* (Inner) IP, (Inner) TCP/UDP CSO */
253                         txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
254                         txbd1->mss = 0;
255                 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_UDP_CKSUM) ==
256                            PKT_TX_IIP_UDP_CKSUM) {
257                         /* (Inner) IP, (Inner) TCP/UDP CSO */
258                         txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
259                         txbd1->mss = 0;
260                 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_TCP_CKSUM) ==
261                            PKT_TX_IIP_TCP_CKSUM) {
262                         /* (Inner) IP, (Inner) TCP/UDP CSO */
263                         txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
264                         txbd1->mss = 0;
265                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_TCP_UDP_CKSUM) ==
266                            PKT_TX_OIP_TCP_UDP_CKSUM) {
267                         /* Outer IP, (Inner) TCP/UDP CSO */
268                         txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
269                         txbd1->mss = 0;
270                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_UDP_CKSUM) ==
271                            PKT_TX_OIP_UDP_CKSUM) {
272                         /* Outer IP, (Inner) TCP/UDP CSO */
273                         txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
274                         txbd1->mss = 0;
275                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_TCP_CKSUM) ==
276                            PKT_TX_OIP_TCP_CKSUM) {
277                         /* Outer IP, (Inner) TCP/UDP CSO */
278                         txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
279                         txbd1->mss = 0;
280                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_CKSUM) ==
281                            PKT_TX_OIP_IIP_CKSUM) {
282                         /* Outer IP, Inner IP CSO */
283                         txbd1->lflags |= TX_BD_FLG_TIP_IP_CHKSUM;
284                         txbd1->mss = 0;
285                 } else if ((tx_pkt->ol_flags & PKT_TX_TCP_UDP_CKSUM) ==
286                            PKT_TX_TCP_UDP_CKSUM) {
287                         /* TCP/UDP CSO */
288                         txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
289                         txbd1->mss = 0;
290                 } else if ((tx_pkt->ol_flags & PKT_TX_TCP_CKSUM) ==
291                            PKT_TX_TCP_CKSUM) {
292                         /* TCP/UDP CSO */
293                         txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
294                         txbd1->mss = 0;
295                 } else if ((tx_pkt->ol_flags & PKT_TX_UDP_CKSUM) ==
296                            PKT_TX_UDP_CKSUM) {
297                         /* TCP/UDP CSO */
298                         txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
299                         txbd1->mss = 0;
300                 } else if ((tx_pkt->ol_flags & PKT_TX_IP_CKSUM) ==
301                            PKT_TX_IP_CKSUM) {
302                         /* IP CSO */
303                         txbd1->lflags |= TX_BD_LONG_LFLAGS_IP_CHKSUM;
304                         txbd1->mss = 0;
305                 } else if ((tx_pkt->ol_flags & PKT_TX_OUTER_IP_CKSUM) ==
306                            PKT_TX_OUTER_IP_CKSUM) {
307                         /* IP CSO */
308                         txbd1->lflags |= TX_BD_LONG_LFLAGS_T_IP_CHKSUM;
309                         txbd1->mss = 0;
310                 }
311         } else {
312                 txbd->flags_type |= TX_BD_SHORT_TYPE_TX_BD_SHORT;
313         }
314
315         m_seg = tx_pkt->next;
316         while (m_seg) {
317                 /* Check non zero data_len */
318                 RTE_VERIFY(m_seg->data_len);
319                 txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
320                 tx_buf = &txr->tx_buf_ring[txr->tx_prod];
321
322                 txbd = &txr->tx_desc_ring[txr->tx_prod];
323                 txbd->address = rte_cpu_to_le_64(rte_mbuf_data_iova(m_seg));
324                 txbd->flags_type |= TX_BD_SHORT_TYPE_TX_BD_SHORT;
325                 txbd->len = m_seg->data_len;
326
327                 m_seg = m_seg->next;
328         }
329
330         txbd->flags_type |= TX_BD_LONG_FLAGS_PACKET_END;
331
332         txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
333
334         return 0;
335 }
336
337 static void bnxt_tx_cmp(struct bnxt_tx_queue *txq, int nr_pkts)
338 {
339         struct bnxt_tx_ring_info *txr = txq->tx_ring;
340         uint16_t cons = txr->tx_cons;
341         int i, j;
342
343         for (i = 0; i < nr_pkts; i++) {
344                 struct bnxt_sw_tx_bd *tx_buf;
345                 struct rte_mbuf *mbuf;
346
347                 tx_buf = &txr->tx_buf_ring[cons];
348                 cons = RING_NEXT(txr->tx_ring_struct, cons);
349                 mbuf = tx_buf->mbuf;
350                 tx_buf->mbuf = NULL;
351
352                 /* EW - no need to unmap DMA memory? */
353
354                 for (j = 1; j < tx_buf->nr_bds; j++)
355                         cons = RING_NEXT(txr->tx_ring_struct, cons);
356                 rte_pktmbuf_free(mbuf);
357         }
358
359         txr->tx_cons = cons;
360 }
361
362 static int bnxt_handle_tx_cp(struct bnxt_tx_queue *txq)
363 {
364         struct bnxt_cp_ring_info *cpr = txq->cp_ring;
365         uint32_t raw_cons = cpr->cp_raw_cons;
366         uint32_t cons;
367         uint32_t nb_tx_pkts = 0;
368         struct tx_cmpl *txcmp;
369         struct cmpl_base *cp_desc_ring = cpr->cp_desc_ring;
370         struct bnxt_ring *cp_ring_struct = cpr->cp_ring_struct;
371         uint32_t ring_mask = cp_ring_struct->ring_mask;
372         uint32_t opaque = 0;
373
374         if (((txq->tx_ring->tx_prod - txq->tx_ring->tx_cons) &
375                 txq->tx_ring->tx_ring_struct->ring_mask) < txq->tx_free_thresh)
376                 return 0;
377
378         do {
379                 cons = RING_CMPL(ring_mask, raw_cons);
380                 txcmp = (struct tx_cmpl *)&cpr->cp_desc_ring[cons];
381                 rte_prefetch_non_temporal(&cp_desc_ring[(cons + 2) &
382                                                         ring_mask]);
383
384                 if (!CMPL_VALID(txcmp, cpr->valid))
385                         break;
386                 opaque = rte_cpu_to_le_32(txcmp->opaque);
387                 NEXT_CMPL(cpr, cons, cpr->valid, 1);
388                 rte_prefetch0(&cp_desc_ring[cons]);
389
390                 if (CMP_TYPE(txcmp) == TX_CMPL_TYPE_TX_L2)
391                         nb_tx_pkts += opaque;
392                 else
393                         RTE_LOG_DP(ERR, PMD,
394                                         "Unhandled CMP type %02x\n",
395                                         CMP_TYPE(txcmp));
396                 raw_cons = cons;
397         } while (nb_tx_pkts < ring_mask);
398
399         if (nb_tx_pkts) {
400                 bnxt_tx_cmp(txq, nb_tx_pkts);
401                 cpr->cp_raw_cons = raw_cons;
402                 B_CP_DB(cpr, cpr->cp_raw_cons, ring_mask);
403         }
404
405         return nb_tx_pkts;
406 }
407
408 uint16_t bnxt_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
409                                uint16_t nb_pkts)
410 {
411         struct bnxt_tx_queue *txq = tx_queue;
412         uint16_t nb_tx_pkts = 0;
413         uint16_t coal_pkts = 0;
414         uint16_t cmpl_next = txq->cmpl_next;
415
416         /* Handle TX completions */
417         bnxt_handle_tx_cp(txq);
418
419         /* Tx queue was stopped; wait for it to be restarted */
420         if (txq->tx_deferred_start) {
421                 PMD_DRV_LOG(DEBUG, "Tx q stopped;return\n");
422                 return 0;
423         }
424
425         txq->cmpl_next = 0;
426         /* Handle TX burst request */
427         for (nb_tx_pkts = 0; nb_tx_pkts < nb_pkts; nb_tx_pkts++) {
428                 int rc;
429
430                 /* Request a completion on first and last packet */
431                 cmpl_next |= (nb_pkts == nb_tx_pkts + 1);
432                 coal_pkts++;
433                 rc = bnxt_start_xmit(tx_pkts[nb_tx_pkts], txq,
434                                 &coal_pkts, &cmpl_next);
435
436                 if (unlikely(rc)) {
437                         /* Request a completion in next cycle */
438                         txq->cmpl_next = 1;
439                         break;
440                 }
441         }
442
443         if (nb_tx_pkts)
444                 B_TX_DB(txq->tx_ring->tx_doorbell, txq->tx_ring->tx_prod);
445
446         return nb_tx_pkts;
447 }
448
449 int bnxt_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
450 {
451         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
452         struct bnxt_tx_queue *txq = bp->tx_queues[tx_queue_id];
453
454         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
455         txq->tx_deferred_start = false;
456         PMD_DRV_LOG(DEBUG, "Tx queue started\n");
457
458         return 0;
459 }
460
461 int bnxt_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
462 {
463         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
464         struct bnxt_tx_queue *txq = bp->tx_queues[tx_queue_id];
465
466         /* Handle TX completions */
467         bnxt_handle_tx_cp(txq);
468
469         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
470         txq->tx_deferred_start = true;
471         PMD_DRV_LOG(DEBUG, "Tx queue stopped\n");
472
473         return 0;
474 }