net/bnxt: support set MTU
[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 inline int bnxt_alloc_ag_data(struct bnxt_rx_queue *rxq,
81                                      struct bnxt_rx_ring_info *rxr,
82                                      uint16_t prod)
83 {
84         struct rx_prod_pkt_bd *rxbd = &rxr->ag_desc_ring[prod];
85         struct bnxt_sw_rx_bd *rx_buf = &rxr->ag_buf_ring[prod];
86         struct rte_mbuf *data;
87
88         data = __bnxt_alloc_rx_data(rxq->mb_pool);
89         if (!data)
90                 return -ENOMEM;
91
92         if (rxbd == NULL)
93                 RTE_LOG(ERR, PMD, "Jumbo Frame. rxbd is NULL\n");
94         if (rx_buf == NULL)
95                 RTE_LOG(ERR, PMD, "Jumbo Frame. rx_buf is NULL\n");
96
97
98         rx_buf->mbuf = data;
99
100         rxbd->addr = rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR(rx_buf->mbuf));
101
102         return 0;
103 }
104
105 #ifdef BNXT_DEBUG
106 static void bnxt_reuse_rx_mbuf(struct bnxt_rx_ring_info *rxr, uint16_t cons,
107                                struct rte_mbuf *mbuf)
108 {
109         uint16_t prod = rxr->rx_prod;
110         struct bnxt_sw_rx_bd *prod_rx_buf;
111         struct rx_prod_pkt_bd *prod_bd, *cons_bd;
112
113         prod_rx_buf = &rxr->rx_buf_ring[prod];
114
115         prod_rx_buf->mbuf = mbuf;
116
117         prod_bd = &rxr->rx_desc_ring[prod];
118         cons_bd = &rxr->rx_desc_ring[cons];
119
120         prod_bd->addr = cons_bd->addr;
121 }
122
123 static void bnxt_reuse_ag_mbuf(struct bnxt_rx_ring_info *rxr, uint16_t cons,
124                                struct rte_mbuf *mbuf)
125 {
126         uint16_t prod = rxr->ag_prod;
127         struct bnxt_sw_rx_bd *prod_rx_buf;
128         struct rx_prod_pkt_bd *prod_bd, *cons_bd;
129
130         prod_rx_buf = &rxr->ag_buf_ring[prod];
131
132         prod_rx_buf->mbuf = mbuf;
133
134         prod_bd = &rxr->ag_desc_ring[prod];
135         cons_bd = &rxr->ag_desc_ring[cons];
136
137         prod_bd->addr = cons_bd->addr;
138 }
139 #endif
140
141 static uint16_t bnxt_rx_pkt(struct rte_mbuf **rx_pkt,
142                             struct bnxt_rx_queue *rxq, uint32_t *raw_cons)
143 {
144         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
145         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
146         struct rx_pkt_cmpl *rxcmp;
147         struct rx_pkt_cmpl_hi *rxcmp1;
148         uint32_t tmp_raw_cons = *raw_cons;
149         uint16_t cons, prod, cp_cons =
150             RING_CMP(cpr->cp_ring_struct, tmp_raw_cons);
151         uint16_t ag_cons, ag_prod = rxr->ag_prod;
152         struct bnxt_sw_rx_bd *rx_buf;
153         struct rte_mbuf *mbuf;
154         int rc = 0;
155         uint8_t i;
156         uint8_t agg_buf = 0;
157
158         rxcmp = (struct rx_pkt_cmpl *)
159             &cpr->cp_desc_ring[cp_cons];
160
161         tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
162         cp_cons = RING_CMP(cpr->cp_ring_struct, tmp_raw_cons);
163         rxcmp1 = (struct rx_pkt_cmpl_hi *)&cpr->cp_desc_ring[cp_cons];
164
165         if (!CMP_VALID(rxcmp1, tmp_raw_cons, cpr->cp_ring_struct))
166                 return -EBUSY;
167
168         prod = rxr->rx_prod;
169
170         /* EW - GRO deferred to phase 3 */
171         cons = rxcmp->opaque;
172         rx_buf = &rxr->rx_buf_ring[cons];
173         mbuf = rx_buf->mbuf;
174         rte_prefetch0(mbuf);
175
176         if (mbuf == NULL)
177                 return -ENOMEM;
178
179         mbuf->nb_segs = 1;
180         mbuf->next = NULL;
181         mbuf->pkt_len = rxcmp->len;
182         mbuf->data_len = mbuf->pkt_len;
183         mbuf->port = rxq->port_id;
184         mbuf->ol_flags = 0;
185         if (rxcmp->flags_type & RX_PKT_CMPL_FLAGS_RSS_VALID) {
186                 mbuf->hash.rss = rxcmp->rss_hash;
187                 mbuf->ol_flags |= PKT_RX_RSS_HASH;
188         } else {
189                 mbuf->hash.fdir.id = rxcmp1->cfa_code;
190                 mbuf->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
191         }
192
193         agg_buf = (rxcmp->agg_bufs_v1 & RX_PKT_CMPL_AGG_BUFS_MASK)
194                         >> RX_PKT_CMPL_AGG_BUFS_SFT;
195         if (agg_buf) {
196                 cp_cons = RING_CMP(cpr->cp_ring_struct, tmp_raw_cons + agg_buf);
197                 rxcmp = (struct rx_pkt_cmpl *)
198                                         &cpr->cp_desc_ring[cp_cons];
199                 if (!CMP_VALID(rxcmp, tmp_raw_cons + agg_buf,
200                                cpr->cp_ring_struct))
201                         return -EBUSY;
202                 RTE_LOG(DEBUG, PMD, "JUMBO Frame %d. %x, agg_buf %x,\n",
203                         mbuf->pkt_len, rxcmp->agg_bufs_v1,  agg_buf);
204         }
205
206         for (i = 0; i < agg_buf; i++) {
207                 struct bnxt_sw_rx_bd *ag_buf;
208                 struct rte_mbuf *ag_mbuf;
209                 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
210                 cp_cons = RING_CMP(cpr->cp_ring_struct, tmp_raw_cons);
211                 rxcmp = (struct rx_pkt_cmpl *)
212                                         &cpr->cp_desc_ring[cp_cons];
213                 ag_cons = rxcmp->opaque;
214                 ag_buf = &rxr->ag_buf_ring[ag_cons];
215                 ag_mbuf = ag_buf->mbuf;
216                 ag_mbuf->nb_segs = 1;
217                 ag_mbuf->data_len = rxcmp->len;
218
219                 mbuf->nb_segs++;
220                 mbuf->pkt_len += ag_mbuf->data_len;
221                 if (mbuf->next == NULL) {
222                         mbuf->next = ag_mbuf;
223                 } else {
224                         struct rte_mbuf *temp_mbuf = mbuf;
225
226                         while (temp_mbuf->next != NULL)
227                                 temp_mbuf = temp_mbuf->next;
228                         temp_mbuf->next = ag_mbuf;
229                 }
230                 ag_buf->mbuf = NULL;
231
232                 ag_prod = RING_NEXT(rxr->ag_ring_struct, ag_prod);
233                 if (bnxt_alloc_ag_data(rxq, rxr, ag_prod)) {
234                         RTE_LOG(ERR, PMD,
235                                 "agg mbuf alloc failed: prod=0x%x\n",
236                                 ag_prod);
237                         rc = -ENOMEM;
238                 }
239                 rxr->ag_prod = ag_prod;
240
241 #ifdef BNXT_DEBUG
242                 if (!CMP_VALID((struct cmpl_base *)
243                         &cpr->cp_desc_ring[cp_cons], tmp_raw_cons,
244                         cpr->cp_ring_struct))
245                         return -EBUSY;
246 #endif
247         }
248
249         if (rxcmp1->flags2 & RX_PKT_CMPL_FLAGS2_META_FORMAT_VLAN) {
250                 mbuf->vlan_tci = rxcmp1->metadata &
251                         (RX_PKT_CMPL_METADATA_VID_MASK |
252                         RX_PKT_CMPL_METADATA_DE |
253                         RX_PKT_CMPL_METADATA_PRI_MASK);
254                 mbuf->ol_flags |= PKT_RX_VLAN_PKT;
255         }
256
257         rx_buf->mbuf = NULL;
258 #ifdef BNXT_DEBUG
259         if (rxcmp1->errors_v2 & RX_CMP_L2_ERRORS) {
260                 /* Re-install the mbuf back to the rx ring */
261                 bnxt_reuse_rx_mbuf(rxr, cons, mbuf);
262                 if (agg_buf)
263                         bnxt_reuse_ag_mbuf(rxr, ag_cons, mbuf);
264
265                 rc = -EIO;
266                 goto next_rx;
267         }
268 #endif
269         /*
270          * TODO: Redesign this....
271          * If the allocation fails, the packet does not get received.
272          * Simply returning this will result in slowly falling behind
273          * on the producer ring buffers.
274          * Instead, "filling up" the producer just before ringing the
275          * doorbell could be a better solution since it will let the
276          * producer ring starve until memory is available again pushing
277          * the drops into hardware and getting them out of the driver
278          * allowing recovery to a full producer ring.
279          *
280          * This could also help with cache usage by preventing per-packet
281          * calls in favour of a tight loop with the same function being called
282          * in it.
283          */
284         prod = RING_NEXT(rxr->rx_ring_struct, prod);
285         if (bnxt_alloc_rx_data(rxq, rxr, prod)) {
286                 RTE_LOG(ERR, PMD, "mbuf alloc failed with prod=0x%x\n", prod);
287                 rc = -ENOMEM;
288         }
289         rxr->rx_prod = prod;
290         /*
291          * All MBUFs are allocated with the same size under DPDK,
292          * no optimization for rx_copy_thresh
293          */
294
295         *rx_pkt = mbuf;
296 #ifdef BNXT_DEBUG
297 next_rx:
298 #endif
299         *raw_cons = tmp_raw_cons;
300
301         return rc;
302 }
303
304 uint16_t bnxt_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
305                                uint16_t nb_pkts)
306 {
307         struct bnxt_rx_queue *rxq = rx_queue;
308         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
309         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
310         uint32_t raw_cons = cpr->cp_raw_cons;
311         uint32_t cons;
312         int nb_rx_pkts = 0;
313         struct rx_pkt_cmpl *rxcmp;
314         uint16_t prod = rxr->rx_prod;
315         uint16_t ag_prod = rxr->ag_prod;
316
317         /* Handle RX burst request */
318         while (1) {
319                 int rc;
320
321                 cons = RING_CMP(cpr->cp_ring_struct, raw_cons);
322                 rte_prefetch0(&cpr->cp_desc_ring[cons]);
323                 rxcmp = (struct rx_pkt_cmpl *)&cpr->cp_desc_ring[cons];
324
325                 if (!CMP_VALID(rxcmp, raw_cons, cpr->cp_ring_struct))
326                         break;
327
328                 /* TODO: Avoid magic numbers... */
329                 if ((CMP_TYPE(rxcmp) & 0x30) == 0x10) {
330                         rc = bnxt_rx_pkt(&rx_pkts[nb_rx_pkts], rxq, &raw_cons);
331                         if (likely(!rc))
332                                 nb_rx_pkts++;
333                         else if (rc == -EBUSY)  /* partial completion */
334                                 break;
335                 }
336                 raw_cons = NEXT_RAW_CMP(raw_cons);
337                 if (nb_rx_pkts == nb_pkts)
338                         break;
339         }
340
341         if (prod == rxr->rx_prod && ag_prod == rxr->ag_prod) {
342                 /*
343                  * For PMD, there is no need to keep on pushing to REARM
344                  * the doorbell if there are no new completions
345                  */
346                 return nb_rx_pkts;
347         }
348         cpr->cp_raw_cons = raw_cons;
349
350         B_CP_DIS_DB(cpr, cpr->cp_raw_cons);
351         B_RX_DB(rxr->rx_doorbell, rxr->rx_prod);
352         /* Ring the AGG ring DB */
353         B_RX_DB(rxr->ag_doorbell, rxr->ag_prod);
354         return nb_rx_pkts;
355 }
356
357 void bnxt_free_rx_rings(struct bnxt *bp)
358 {
359         int i;
360
361         for (i = 0; i < (int)bp->rx_nr_rings; i++) {
362                 struct bnxt_rx_queue *rxq = bp->rx_queues[i];
363
364                 if (!rxq)
365                         continue;
366
367                 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
368                 rte_free(rxq->rx_ring->rx_ring_struct);
369
370                 /* Free the Aggregator ring */
371                 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
372                 rte_free(rxq->rx_ring->ag_ring_struct);
373                 rxq->rx_ring->ag_ring_struct = NULL;
374
375                 rte_free(rxq->rx_ring);
376
377                 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
378                 rte_free(rxq->cp_ring->cp_ring_struct);
379                 rte_free(rxq->cp_ring);
380
381                 rte_free(rxq);
382                 bp->rx_queues[i] = NULL;
383         }
384 }
385
386 int bnxt_init_rx_ring_struct(struct bnxt_rx_queue *rxq, unsigned int socket_id)
387 {
388         struct bnxt_cp_ring_info *cpr;
389         struct bnxt_rx_ring_info *rxr;
390         struct bnxt_ring *ring;
391
392         rxq->rx_buf_use_size = BNXT_MAX_MTU + ETHER_HDR_LEN + ETHER_CRC_LEN +
393                                (2 * VLAN_TAG_SIZE);
394         rxq->rx_buf_size = rxq->rx_buf_use_size + sizeof(struct rte_mbuf);
395
396         rxr = rte_zmalloc_socket("bnxt_rx_ring",
397                                  sizeof(struct bnxt_rx_ring_info),
398                                  RTE_CACHE_LINE_SIZE, socket_id);
399         if (rxr == NULL)
400                 return -ENOMEM;
401         rxq->rx_ring = rxr;
402
403         ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
404                                    sizeof(struct bnxt_ring),
405                                    RTE_CACHE_LINE_SIZE, socket_id);
406         if (ring == NULL)
407                 return -ENOMEM;
408         rxr->rx_ring_struct = ring;
409         ring->ring_size = rte_align32pow2(rxq->nb_rx_desc);
410         ring->ring_mask = ring->ring_size - 1;
411         ring->bd = (void *)rxr->rx_desc_ring;
412         ring->bd_dma = rxr->rx_desc_mapping;
413         ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_rx_bd);
414         ring->vmem = (void **)&rxr->rx_buf_ring;
415
416         cpr = rte_zmalloc_socket("bnxt_rx_ring",
417                                  sizeof(struct bnxt_cp_ring_info),
418                                  RTE_CACHE_LINE_SIZE, socket_id);
419         if (cpr == NULL)
420                 return -ENOMEM;
421         rxq->cp_ring = cpr;
422
423         ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
424                                    sizeof(struct bnxt_ring),
425                                    RTE_CACHE_LINE_SIZE, socket_id);
426         if (ring == NULL)
427                 return -ENOMEM;
428         cpr->cp_ring_struct = ring;
429         ring->ring_size = rte_align32pow2(rxr->rx_ring_struct->ring_size *
430                                           (2 + AGG_RING_SIZE_FACTOR));
431         ring->ring_mask = ring->ring_size - 1;
432         ring->bd = (void *)cpr->cp_desc_ring;
433         ring->bd_dma = cpr->cp_desc_mapping;
434         ring->vmem_size = 0;
435         ring->vmem = NULL;
436
437         /* Allocate Aggregator rings */
438         ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
439                                    sizeof(struct bnxt_ring),
440                                    RTE_CACHE_LINE_SIZE, socket_id);
441         if (ring == NULL)
442                 return -ENOMEM;
443         rxr->ag_ring_struct = ring;
444         ring->ring_size = rte_align32pow2(rxq->nb_rx_desc *
445                                           AGG_RING_SIZE_FACTOR);
446         ring->ring_mask = ring->ring_size - 1;
447         ring->bd = (void *)rxr->ag_desc_ring;
448         ring->bd_dma = rxr->ag_desc_mapping;
449         ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_rx_bd);
450         ring->vmem = (void **)&rxr->ag_buf_ring;
451
452         return 0;
453 }
454
455 static void bnxt_init_rxbds(struct bnxt_ring *ring, uint32_t type,
456                             uint16_t len)
457 {
458         uint32_t j;
459         struct rx_prod_pkt_bd *rx_bd_ring = (struct rx_prod_pkt_bd *)ring->bd;
460
461         if (!rx_bd_ring)
462                 return;
463         for (j = 0; j < ring->ring_size; j++) {
464                 rx_bd_ring[j].flags_type = rte_cpu_to_le_16(type);
465                 rx_bd_ring[j].len = rte_cpu_to_le_16(len);
466                 rx_bd_ring[j].opaque = j;
467         }
468 }
469
470 int bnxt_init_one_rx_ring(struct bnxt_rx_queue *rxq)
471 {
472         struct bnxt_rx_ring_info *rxr;
473         struct bnxt_ring *ring;
474         uint32_t prod, type;
475         unsigned int i;
476         uint16_t size;
477
478         size = rte_pktmbuf_data_room_size(rxq->mb_pool) - RTE_PKTMBUF_HEADROOM;
479         if (rxq->rx_buf_use_size <= size)
480                 size = rxq->rx_buf_use_size;
481
482         type = RX_PROD_PKT_BD_TYPE_RX_PROD_PKT;
483
484         rxr = rxq->rx_ring;
485         ring = rxr->rx_ring_struct;
486         bnxt_init_rxbds(ring, type, size);
487
488         prod = rxr->rx_prod;
489         for (i = 0; i < ring->ring_size; i++) {
490                 if (bnxt_alloc_rx_data(rxq, rxr, prod) != 0) {
491                         RTE_LOG(WARNING, PMD,
492                                 "init'ed rx ring %d with %d/%d mbufs only\n",
493                                 rxq->queue_id, i, ring->ring_size);
494                         break;
495                 }
496                 rxr->rx_prod = prod;
497                 prod = RING_NEXT(rxr->rx_ring_struct, prod);
498         }
499         RTE_LOG(DEBUG, PMD, "%s\n", __func__);
500
501         ring = rxr->ag_ring_struct;
502         type = RX_PROD_AGG_BD_TYPE_RX_PROD_AGG;
503         bnxt_init_rxbds(ring, type, size);
504         prod = rxr->ag_prod;
505
506         for (i = 0; i < ring->ring_size; i++) {
507                 if (bnxt_alloc_ag_data(rxq, rxr, prod) != 0) {
508                         RTE_LOG(WARNING, PMD,
509                         "init'ed AG ring %d with %d/%d mbufs only\n",
510                         rxq->queue_id, i, ring->ring_size);
511                         break;
512                 }
513                 rxr->ag_prod = prod;
514                 prod = RING_NEXT(rxr->ag_ring_struct, prod);
515         }
516         RTE_LOG(DEBUG, PMD, "%s AGG Done!\n", __func__);
517
518         return 0;
519 }