net/bnxt: add initial Rx code
[dpdk.git] / drivers / net / bnxt / bnxt_rxr.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) Broadcom Limited.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Broadcom Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <inttypes.h>
35 #include <stdbool.h>
36
37 #include <rte_byteorder.h>
38 #include <rte_malloc.h>
39 #include <rte_memory.h>
40
41 #include "bnxt.h"
42 #include "bnxt_cpr.h"
43 #include "bnxt_ring.h"
44 #include "bnxt_rxr.h"
45 #include "bnxt_rxq.h"
46 #include "hsi_struct_def_dpdk.h"
47
48 /*
49  * RX Ring handling
50  */
51
52 static inline struct rte_mbuf *__bnxt_alloc_rx_data(struct rte_mempool *mb)
53 {
54         struct rte_mbuf *data;
55
56         data = rte_mbuf_raw_alloc(mb);
57
58         return data;
59 }
60
61 static inline int bnxt_alloc_rx_data(struct bnxt_rx_queue *rxq,
62                                      struct bnxt_rx_ring_info *rxr,
63                                      uint16_t prod)
64 {
65         struct rx_prod_pkt_bd *rxbd = &rxr->rx_desc_ring[prod];
66         struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
67         struct rte_mbuf *data;
68
69         data = __bnxt_alloc_rx_data(rxq->mb_pool);
70         if (!data)
71                 return -ENOMEM;
72
73         rx_buf->mbuf = data;
74
75         rxbd->addr = rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR(rx_buf->mbuf));
76
77         return 0;
78 }
79
80 static void bnxt_reuse_rx_mbuf(struct bnxt_rx_ring_info *rxr, uint16_t cons,
81                                struct rte_mbuf *mbuf)
82 {
83         uint16_t prod = rxr->rx_prod;
84         struct bnxt_sw_rx_bd *prod_rx_buf;
85         struct rx_prod_pkt_bd *prod_bd, *cons_bd;
86
87         prod_rx_buf = &rxr->rx_buf_ring[prod];
88
89         prod_rx_buf->mbuf = mbuf;
90
91         prod_bd = &rxr->rx_desc_ring[prod];
92         cons_bd = &rxr->rx_desc_ring[cons];
93
94         prod_bd->addr = cons_bd->addr;
95 }
96
97 static uint16_t bnxt_rx_pkt(struct rte_mbuf **rx_pkt,
98                             struct bnxt_rx_queue *rxq, uint32_t *raw_cons)
99 {
100         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
101         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
102         struct rx_pkt_cmpl *rxcmp;
103         struct rx_pkt_cmpl_hi *rxcmp1;
104         uint32_t tmp_raw_cons = *raw_cons;
105         uint16_t cons, prod, cp_cons =
106             RING_CMP(cpr->cp_ring_struct, tmp_raw_cons);
107         struct bnxt_sw_rx_bd *rx_buf;
108         struct rte_mbuf *mbuf;
109         int rc = 0;
110
111         rxcmp = (struct rx_pkt_cmpl *)
112             &cpr->cp_desc_ring[cp_cons];
113
114         tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
115         cp_cons = RING_CMP(cpr->cp_ring_struct, tmp_raw_cons);
116         rxcmp1 = (struct rx_pkt_cmpl_hi *)&cpr->cp_desc_ring[cp_cons];
117
118         if (!CMP_VALID(rxcmp1, tmp_raw_cons, cpr->cp_ring_struct))
119                 return -EBUSY;
120
121         prod = rxr->rx_prod;
122
123         /* EW - GRO deferred to phase 3 */
124         cons = rxcmp->opaque;
125         rx_buf = &rxr->rx_buf_ring[cons];
126         mbuf = rx_buf->mbuf;
127         rte_prefetch0(mbuf);
128
129         mbuf->nb_segs = 1;
130         mbuf->next = NULL;
131         mbuf->pkt_len = rxcmp->len;
132         mbuf->data_len = mbuf->pkt_len;
133         mbuf->port = rxq->port_id;
134         mbuf->ol_flags = 0;
135         if (rxcmp->flags_type & RX_PKT_CMPL_FLAGS_RSS_VALID) {
136                 mbuf->hash.rss = rxcmp->rss_hash;
137                 mbuf->ol_flags |= PKT_RX_RSS_HASH;
138         } else {
139                 mbuf->hash.fdir.id = rxcmp1->cfa_code;
140                 mbuf->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
141         }
142         if (rxcmp1->flags2 & RX_PKT_CMPL_FLAGS2_META_FORMAT_VLAN) {
143                 mbuf->vlan_tci = rxcmp1->metadata &
144                         (RX_PKT_CMPL_METADATA_VID_MASK |
145                         RX_PKT_CMPL_METADATA_DE |
146                         RX_PKT_CMPL_METADATA_PRI_MASK);
147                 mbuf->ol_flags |= PKT_RX_VLAN_PKT;
148         }
149
150         rx_buf->mbuf = NULL;
151         if (rxcmp1->errors_v2 & RX_CMP_L2_ERRORS) {
152                 /* Re-install the mbuf back to the rx ring */
153                 bnxt_reuse_rx_mbuf(rxr, cons, mbuf);
154
155                 rc = -EIO;
156                 goto next_rx;
157         }
158         /*
159          * TODO: Redesign this....
160          * If the allocation fails, the packet does not get received.
161          * Simply returning this will result in slowly falling behind
162          * on the producer ring buffers.
163          * Instead, "filling up" the producer just before ringing the
164          * doorbell could be a better solution since it will let the
165          * producer ring starve until memory is available again pushing
166          * the drops into hardware and getting them out of the driver
167          * allowing recovery to a full producer ring.
168          *
169          * This could also help with cache usage by preventing per-packet
170          * calls in favour of a tight loop with the same function being called
171          * in it.
172          */
173         if (bnxt_alloc_rx_data(rxq, rxr, prod)) {
174                 RTE_LOG(ERR, PMD, "mbuf alloc failed with prod=0x%x\n", prod);
175                 rc = -ENOMEM;
176                 goto next_rx;
177         }
178
179         /*
180          * All MBUFs are allocated with the same size under DPDK,
181          * no optimization for rx_copy_thresh
182          */
183
184         /* AGG buf operation is deferred */
185
186         /* EW - VLAN reception.  Must compare against the ol_flags */
187
188         *rx_pkt = mbuf;
189 next_rx:
190         rxr->rx_prod = RING_NEXT(rxr->rx_ring_struct, prod);
191
192         *raw_cons = tmp_raw_cons;
193
194         return rc;
195 }
196
197 uint16_t bnxt_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
198                                uint16_t nb_pkts)
199 {
200         struct bnxt_rx_queue *rxq = rx_queue;
201         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
202         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
203         uint32_t raw_cons = cpr->cp_raw_cons;
204         uint32_t cons;
205         int nb_rx_pkts = 0;
206         bool rx_event = false;
207         struct rx_pkt_cmpl *rxcmp;
208
209         /* Handle RX burst request */
210         while (1) {
211                 int rc;
212
213                 cons = RING_CMP(cpr->cp_ring_struct, raw_cons);
214                 rte_prefetch0(&cpr->cp_desc_ring[cons]);
215                 rxcmp = (struct rx_pkt_cmpl *)&cpr->cp_desc_ring[cons];
216
217                 if (!CMP_VALID(rxcmp, raw_cons, cpr->cp_ring_struct))
218                         break;
219
220                 /* TODO: Avoid magic numbers... */
221                 if ((CMP_TYPE(rxcmp) & 0x30) == 0x10) {
222                         rc = bnxt_rx_pkt(&rx_pkts[nb_rx_pkts], rxq, &raw_cons);
223                         if (likely(!rc))
224                                 nb_rx_pkts++;
225                         else if (rc == -EBUSY)  /* partial completion */
226                                 break;
227                         rx_event = true;
228                 }
229                 raw_cons = NEXT_RAW_CMP(raw_cons);
230                 if (nb_rx_pkts == nb_pkts)
231                         break;
232         }
233         if (raw_cons == cpr->cp_raw_cons) {
234                 /*
235                  * For PMD, there is no need to keep on pushing to REARM
236                  * the doorbell if there are no new completions
237                  */
238                 return nb_rx_pkts;
239         }
240         cpr->cp_raw_cons = raw_cons;
241
242         B_CP_DIS_DB(cpr, cpr->cp_raw_cons);
243         if (rx_event)
244                 B_RX_DB(rxr->rx_doorbell, rxr->rx_prod);
245         return nb_rx_pkts;
246 }
247
248 void bnxt_free_rx_rings(struct bnxt *bp)
249 {
250         int i;
251
252         for (i = 0; i < (int)bp->rx_nr_rings; i++) {
253                 struct bnxt_rx_queue *rxq = bp->rx_queues[i];
254
255                 if (!rxq)
256                         continue;
257
258                 /* TODO: free() rxq->rx_ring and rxq->rx_ring->rx_ring_struct */
259                 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
260                 /* TODO: free() rxq->cp_ring and rxq->cp_ring->cp_ring_struct */
261                 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
262
263                 rte_free(rxq);
264                 bp->rx_queues[i] = NULL;
265         }
266 }
267
268 void bnxt_init_rx_ring_struct(struct bnxt_rx_queue *rxq)
269 {
270         struct bnxt *bp = rxq->bp;
271         struct bnxt_cp_ring_info *cpr;
272         struct bnxt_rx_ring_info *rxr;
273         struct bnxt_ring *ring;
274
275         rxq->rx_buf_use_size = bp->eth_dev->data->mtu +
276                                ETHER_HDR_LEN + ETHER_CRC_LEN +
277                                (2 * VLAN_TAG_SIZE);
278         rxq->rx_buf_size = rxq->rx_buf_use_size + sizeof(struct rte_mbuf);
279
280         rxr = rxq->rx_ring;
281         ring = rxr->rx_ring_struct;
282         ring->ring_size = rte_align32pow2(rxq->nb_rx_desc);
283         ring->ring_mask = ring->ring_size - 1;
284         ring->bd = (void *)rxr->rx_desc_ring;
285         ring->bd_dma = rxr->rx_desc_mapping;
286         ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_rx_bd);
287         ring->vmem = (void **)&rxr->rx_buf_ring;
288
289         cpr = rxq->cp_ring;
290         ring = cpr->cp_ring_struct;
291         ring->ring_size = rxr->rx_ring_struct->ring_size * 2;
292         ring->ring_mask = ring->ring_size - 1;
293         ring->bd = (void *)cpr->cp_desc_ring;
294         ring->bd_dma = cpr->cp_desc_mapping;
295         ring->vmem_size = 0;
296         ring->vmem = NULL;
297 }
298
299 static void bnxt_init_rxbds(struct bnxt_ring *ring, uint32_t type,
300                             uint16_t len)
301 {
302         uint32_t j;
303         struct rx_prod_pkt_bd *rx_bd_ring = (struct rx_prod_pkt_bd *)ring->bd;
304
305         if (!rx_bd_ring)
306                 return;
307         for (j = 0; j < ring->ring_size; j++) {
308                 rx_bd_ring[j].flags_type = type;
309                 rx_bd_ring[j].len = len;
310                 rx_bd_ring[j].opaque = j;
311         }
312 }
313
314 int bnxt_init_one_rx_ring(struct bnxt_rx_queue *rxq)
315 {
316         struct bnxt_rx_ring_info *rxr;
317         struct bnxt_ring *ring;
318         uint32_t prod, type;
319         unsigned int i;
320
321         type = RX_PROD_PKT_BD_TYPE_RX_PROD_PKT | RX_PROD_PKT_BD_FLAGS_EOP_PAD;
322
323         /* TODO: These need to be allocated */
324         rxr = rxq->rx_ring;
325         ring = rxr->rx_ring_struct;
326         bnxt_init_rxbds(ring, type, rxq->rx_buf_use_size);
327
328         prod = rxr->rx_prod;
329         for (i = 0; i < ring->ring_size; i++) {
330                 if (bnxt_alloc_rx_data(rxq, rxr, prod) != 0) {
331                         RTE_LOG(WARNING, PMD,
332                                 "init'ed rx ring %d with %d/%d mbufs only\n",
333                                 rxq->queue_id, i, ring->ring_size);
334                         break;
335                 }
336                 rxr->rx_prod = prod;
337                 prod = RING_NEXT(rxr->rx_ring_struct, prod);
338         }
339
340         return 0;
341 }