New upstream version 16.11.5
[deb_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_default(data));
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->data_off = RTE_PKTMBUF_HEADROOM;
130         mbuf->nb_segs = 1;
131         mbuf->next = NULL;
132         mbuf->pkt_len = rxcmp->len;
133         mbuf->data_len = mbuf->pkt_len;
134         mbuf->port = rxq->port_id;
135         mbuf->ol_flags = 0;
136         if (rxcmp->flags_type & RX_PKT_CMPL_FLAGS_RSS_VALID) {
137                 mbuf->hash.rss = rxcmp->rss_hash;
138                 mbuf->ol_flags |= PKT_RX_RSS_HASH;
139         } else {
140                 mbuf->hash.fdir.id = rxcmp1->cfa_code;
141                 mbuf->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
142         }
143         if (rxcmp1->flags2 & RX_PKT_CMPL_FLAGS2_META_FORMAT_VLAN) {
144                 mbuf->vlan_tci = rxcmp1->metadata &
145                         (RX_PKT_CMPL_METADATA_VID_MASK |
146                         RX_PKT_CMPL_METADATA_DE |
147                         RX_PKT_CMPL_METADATA_PRI_MASK);
148                 mbuf->ol_flags |= PKT_RX_VLAN_PKT;
149         }
150
151         rx_buf->mbuf = NULL;
152
153         if (likely(RX_CMP_IP_CS_OK(rxcmp1)))
154                 mbuf->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
155         else
156                 mbuf->ol_flags |= PKT_RX_IP_CKSUM_BAD;
157
158         if (likely(RX_CMP_L4_CS_OK(rxcmp1)))
159                 mbuf->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
160         else
161                 mbuf->ol_flags |= PKT_RX_L4_CKSUM_BAD;
162
163         if (rxcmp1->errors_v2 & RX_CMP_L2_ERRORS) {
164                 /* Re-install the mbuf back to the rx ring */
165                 bnxt_reuse_rx_mbuf(rxr, cons, mbuf);
166
167                 rc = -EIO;
168                 goto next_rx;
169         }
170         /*
171          * TODO: Redesign this....
172          * If the allocation fails, the packet does not get received.
173          * Simply returning this will result in slowly falling behind
174          * on the producer ring buffers.
175          * Instead, "filling up" the producer just before ringing the
176          * doorbell could be a better solution since it will let the
177          * producer ring starve until memory is available again pushing
178          * the drops into hardware and getting them out of the driver
179          * allowing recovery to a full producer ring.
180          *
181          * This could also help with cache usage by preventing per-packet
182          * calls in favour of a tight loop with the same function being called
183          * in it.
184          */
185         if (bnxt_alloc_rx_data(rxq, rxr, prod)) {
186                 RTE_LOG(ERR, PMD, "mbuf alloc failed with prod=0x%x\n", prod);
187                 rc = -ENOMEM;
188                 goto next_rx;
189         }
190
191         /*
192          * All MBUFs are allocated with the same size under DPDK,
193          * no optimization for rx_copy_thresh
194          */
195
196         /* AGG buf operation is deferred */
197
198         /* EW - VLAN reception.  Must compare against the ol_flags */
199
200         *rx_pkt = mbuf;
201 next_rx:
202         rxr->rx_prod = RING_NEXT(rxr->rx_ring_struct, prod);
203
204         *raw_cons = tmp_raw_cons;
205
206         return rc;
207 }
208
209 uint16_t bnxt_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
210                                uint16_t nb_pkts)
211 {
212         struct bnxt_rx_queue *rxq = rx_queue;
213         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
214         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
215         uint32_t raw_cons = cpr->cp_raw_cons;
216         uint32_t cons;
217         int nb_rx_pkts = 0;
218         bool rx_event = false;
219         struct rx_pkt_cmpl *rxcmp;
220
221         /* Handle RX burst request */
222         while (1) {
223                 int rc;
224
225                 cons = RING_CMP(cpr->cp_ring_struct, raw_cons);
226                 rte_prefetch0(&cpr->cp_desc_ring[cons]);
227                 rxcmp = (struct rx_pkt_cmpl *)&cpr->cp_desc_ring[cons];
228
229                 if (!CMP_VALID(rxcmp, raw_cons, cpr->cp_ring_struct))
230                         break;
231
232                 /* TODO: Avoid magic numbers... */
233                 if ((CMP_TYPE(rxcmp) & 0x30) == 0x10) {
234                         rc = bnxt_rx_pkt(&rx_pkts[nb_rx_pkts], rxq, &raw_cons);
235                         if (likely(!rc))
236                                 nb_rx_pkts++;
237                         else if (rc == -EBUSY)  /* partial completion */
238                                 break;
239                         rx_event = true;
240                 }
241                 raw_cons = NEXT_RAW_CMP(raw_cons);
242                 if (nb_rx_pkts == nb_pkts)
243                         break;
244         }
245         if (raw_cons == cpr->cp_raw_cons) {
246                 /*
247                  * For PMD, there is no need to keep on pushing to REARM
248                  * the doorbell if there are no new completions
249                  */
250                 return nb_rx_pkts;
251         }
252         cpr->cp_raw_cons = raw_cons;
253
254         B_CP_DIS_DB(cpr, cpr->cp_raw_cons);
255         if (rx_event)
256                 B_RX_DB(rxr->rx_doorbell, rxr->rx_prod);
257         return nb_rx_pkts;
258 }
259
260 void bnxt_free_rx_rings(struct bnxt *bp)
261 {
262         int i;
263
264         for (i = 0; i < (int)bp->rx_nr_rings; i++) {
265                 struct bnxt_rx_queue *rxq = bp->rx_queues[i];
266
267                 if (!rxq)
268                         continue;
269
270                 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
271                 rte_free(rxq->rx_ring->rx_ring_struct);
272                 rte_free(rxq->rx_ring);
273
274                 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
275                 rte_free(rxq->cp_ring->cp_ring_struct);
276                 rte_free(rxq->cp_ring);
277
278                 rte_free(rxq);
279                 bp->rx_queues[i] = NULL;
280         }
281 }
282
283 int bnxt_init_rx_ring_struct(struct bnxt_rx_queue *rxq, unsigned int socket_id)
284 {
285         struct bnxt *bp = rxq->bp;
286         struct bnxt_cp_ring_info *cpr;
287         struct bnxt_rx_ring_info *rxr;
288         struct bnxt_ring *ring;
289
290         rxq->rx_buf_use_size = bp->eth_dev->data->mtu +
291                                ETHER_HDR_LEN + ETHER_CRC_LEN +
292                                (2 * VLAN_TAG_SIZE);
293         rxq->rx_buf_size = rxq->rx_buf_use_size + sizeof(struct rte_mbuf);
294
295         rxr = rte_zmalloc_socket("bnxt_rx_ring",
296                                  sizeof(struct bnxt_rx_ring_info),
297                                  RTE_CACHE_LINE_SIZE, socket_id);
298         if (rxr == NULL)
299                 return -ENOMEM;
300         rxq->rx_ring = rxr;
301
302         ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
303                                    sizeof(struct bnxt_ring),
304                                    RTE_CACHE_LINE_SIZE, socket_id);
305         if (ring == NULL)
306                 return -ENOMEM;
307         rxr->rx_ring_struct = ring;
308         ring->ring_size = rte_align32pow2(rxq->nb_rx_desc);
309         ring->ring_mask = ring->ring_size - 1;
310         ring->bd = (void *)rxr->rx_desc_ring;
311         ring->bd_dma = rxr->rx_desc_mapping;
312         ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_rx_bd);
313         ring->vmem = (void **)&rxr->rx_buf_ring;
314
315         cpr = rte_zmalloc_socket("bnxt_rx_ring",
316                                  sizeof(struct bnxt_cp_ring_info),
317                                  RTE_CACHE_LINE_SIZE, socket_id);
318         if (cpr == NULL)
319                 return -ENOMEM;
320         rxq->cp_ring = cpr;
321
322         ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
323                                    sizeof(struct bnxt_ring),
324                                    RTE_CACHE_LINE_SIZE, socket_id);
325         if (ring == NULL)
326                 return -ENOMEM;
327         cpr->cp_ring_struct = ring;
328         ring->ring_size = rxr->rx_ring_struct->ring_size * 2;
329         ring->ring_mask = ring->ring_size - 1;
330         ring->bd = (void *)cpr->cp_desc_ring;
331         ring->bd_dma = cpr->cp_desc_mapping;
332         ring->vmem_size = 0;
333         ring->vmem = NULL;
334
335         return 0;
336 }
337
338 static void bnxt_init_rxbds(struct bnxt_ring *ring, uint32_t type,
339                             uint16_t len)
340 {
341         uint32_t j;
342         struct rx_prod_pkt_bd *rx_bd_ring = (struct rx_prod_pkt_bd *)ring->bd;
343
344         if (!rx_bd_ring)
345                 return;
346         for (j = 0; j < ring->ring_size; j++) {
347                 rx_bd_ring[j].flags_type = type;
348                 rx_bd_ring[j].len = len;
349                 rx_bd_ring[j].opaque = j;
350         }
351 }
352
353 int bnxt_init_one_rx_ring(struct bnxt_rx_queue *rxq)
354 {
355         struct bnxt_rx_ring_info *rxr;
356         struct bnxt_ring *ring;
357         uint32_t prod, type;
358         unsigned int i;
359
360         type = RX_PROD_PKT_BD_TYPE_RX_PROD_PKT | RX_PROD_PKT_BD_FLAGS_EOP_PAD;
361
362         rxr = rxq->rx_ring;
363         ring = rxr->rx_ring_struct;
364         bnxt_init_rxbds(ring, type, rxq->rx_buf_use_size);
365
366         prod = rxr->rx_prod;
367         for (i = 0; i < ring->ring_size; i++) {
368                 if (bnxt_alloc_rx_data(rxq, rxr, prod) != 0) {
369                         RTE_LOG(WARNING, PMD,
370                                 "init'ed rx ring %d with %d/%d mbufs only\n",
371                                 rxq->queue_id, i, ring->ring_size);
372                         break;
373                 }
374                 rxr->rx_prod = prod;
375                 prod = RING_NEXT(rxr->rx_ring_struct, prod);
376         }
377
378         return 0;
379 }