doc: convert prog guide glossary to definition list
[dpdk.git] / lib / librte_pmd_fm10k / fm10k_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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 Intel 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 #include <rte_ethdev.h>
34 #include <rte_common.h>
35 #include "fm10k.h"
36 #include "base/fm10k_type.h"
37
38 #ifdef RTE_PMD_PACKET_PREFETCH
39 #define rte_packet_prefetch(p)  rte_prefetch1(p)
40 #else
41 #define rte_packet_prefetch(p)  do {} while (0)
42 #endif
43
44 #ifdef RTE_LIBRTE_FM10K_DEBUG_RX
45 static inline void dump_rxd(union fm10k_rx_desc *rxd)
46 {
47         PMD_RX_LOG(DEBUG, "+----------------|----------------+");
48         PMD_RX_LOG(DEBUG, "|     GLORT      | PKT HDR & TYPE |");
49         PMD_RX_LOG(DEBUG, "|   0x%08x   |   0x%08x   |", rxd->d.glort,
50                         rxd->d.data);
51         PMD_RX_LOG(DEBUG, "+----------------|----------------+");
52         PMD_RX_LOG(DEBUG, "|   VLAN & LEN   |     STATUS     |");
53         PMD_RX_LOG(DEBUG, "|   0x%08x   |   0x%08x   |", rxd->d.vlan_len,
54                         rxd->d.staterr);
55         PMD_RX_LOG(DEBUG, "+----------------|----------------+");
56         PMD_RX_LOG(DEBUG, "|    RESERVED    |    RSS_HASH    |");
57         PMD_RX_LOG(DEBUG, "|   0x%08x   |   0x%08x   |", 0, rxd->d.rss);
58         PMD_RX_LOG(DEBUG, "+----------------|----------------+");
59         PMD_RX_LOG(DEBUG, "|            TIME TAG             |");
60         PMD_RX_LOG(DEBUG, "|       0x%016lx        |", rxd->q.timestamp);
61         PMD_RX_LOG(DEBUG, "+----------------|----------------+");
62 }
63 #endif
64
65 static inline void
66 rx_desc_to_ol_flags(struct rte_mbuf *m, const union fm10k_rx_desc *d)
67 {
68         uint16_t ptype;
69         static const uint16_t pt_lut[] = { 0,
70                 PKT_RX_IPV4_HDR, PKT_RX_IPV4_HDR_EXT,
71                 PKT_RX_IPV6_HDR, PKT_RX_IPV6_HDR_EXT,
72                 0, 0, 0
73         };
74
75         if (d->w.pkt_info & FM10K_RXD_RSSTYPE_MASK)
76                 m->ol_flags |= PKT_RX_RSS_HASH;
77
78         if (unlikely((d->d.staterr &
79                 (FM10K_RXD_STATUS_IPCS | FM10K_RXD_STATUS_IPE)) ==
80                 (FM10K_RXD_STATUS_IPCS | FM10K_RXD_STATUS_IPE)))
81                 m->ol_flags |= PKT_RX_IP_CKSUM_BAD;
82
83         if (unlikely((d->d.staterr &
84                 (FM10K_RXD_STATUS_L4CS | FM10K_RXD_STATUS_L4E)) ==
85                 (FM10K_RXD_STATUS_L4CS | FM10K_RXD_STATUS_L4E)))
86                 m->ol_flags |= PKT_RX_L4_CKSUM_BAD;
87
88         if (d->d.staterr & FM10K_RXD_STATUS_VEXT)
89                 m->ol_flags |= PKT_RX_VLAN_PKT;
90
91         if (unlikely(d->d.staterr & FM10K_RXD_STATUS_HBO))
92                 m->ol_flags |= PKT_RX_HBUF_OVERFLOW;
93
94         if (unlikely(d->d.staterr & FM10K_RXD_STATUS_RXE))
95                 m->ol_flags |= PKT_RX_RECIP_ERR;
96
97         ptype = (d->d.data & FM10K_RXD_PKTTYPE_MASK_L3) >>
98                                                 FM10K_RXD_PKTTYPE_SHIFT;
99         m->ol_flags |= pt_lut[(uint8_t)ptype];
100 }
101
102 uint16_t
103 fm10k_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
104         uint16_t nb_pkts)
105 {
106         struct rte_mbuf *mbuf;
107         union fm10k_rx_desc desc;
108         struct fm10k_rx_queue *q = rx_queue;
109         uint16_t count = 0;
110         int alloc = 0;
111         uint16_t next_dd;
112         int ret;
113
114         next_dd = q->next_dd;
115
116         nb_pkts = RTE_MIN(nb_pkts, q->alloc_thresh);
117         for (count = 0; count < nb_pkts; ++count) {
118                 mbuf = q->sw_ring[next_dd];
119                 desc = q->hw_ring[next_dd];
120                 if (!(desc.d.staterr & FM10K_RXD_STATUS_DD))
121                         break;
122 #ifdef RTE_LIBRTE_FM10K_DEBUG_RX
123                 dump_rxd(&desc);
124 #endif
125                 rte_pktmbuf_pkt_len(mbuf) = desc.w.length;
126                 rte_pktmbuf_data_len(mbuf) = desc.w.length;
127
128                 mbuf->ol_flags = 0;
129 #ifdef RTE_LIBRTE_FM10K_RX_OLFLAGS_ENABLE
130                 rx_desc_to_ol_flags(mbuf, &desc);
131 #endif
132
133                 mbuf->hash.rss = desc.d.rss;
134
135                 rx_pkts[count] = mbuf;
136                 if (++next_dd == q->nb_desc) {
137                         next_dd = 0;
138                         alloc = 1;
139                 }
140
141                 /* Prefetch next mbuf while processing current one. */
142                 rte_prefetch0(q->sw_ring[next_dd]);
143
144                 /*
145                  * When next RX descriptor is on a cache-line boundary,
146                  * prefetch the next 4 RX descriptors and the next 8 pointers
147                  * to mbufs.
148                  */
149                 if ((next_dd & 0x3) == 0) {
150                         rte_prefetch0(&q->hw_ring[next_dd]);
151                         rte_prefetch0(&q->sw_ring[next_dd]);
152                 }
153         }
154
155         q->next_dd = next_dd;
156
157         if ((q->next_dd > q->next_trigger) || (alloc == 1)) {
158                 ret = rte_mempool_get_bulk(q->mp,
159                                         (void **)&q->sw_ring[q->next_alloc],
160                                         q->alloc_thresh);
161
162                 if (unlikely(ret != 0)) {
163                         uint8_t port = q->port_id;
164                         PMD_RX_LOG(ERR, "Failed to alloc mbuf");
165                         /*
166                          * Need to restore next_dd if we cannot allocate new
167                          * buffers to replenish the old ones.
168                          */
169                         q->next_dd = (q->next_dd + q->nb_desc - count) %
170                                                                 q->nb_desc;
171                         rte_eth_devices[port].data->rx_mbuf_alloc_failed++;
172                         return 0;
173                 }
174
175                 for (; q->next_alloc <= q->next_trigger; ++q->next_alloc) {
176                         mbuf = q->sw_ring[q->next_alloc];
177
178                         /* setup static mbuf fields */
179                         fm10k_pktmbuf_reset(mbuf, q->port_id);
180
181                         /* write descriptor */
182                         desc.q.pkt_addr = MBUF_DMA_ADDR_DEFAULT(mbuf);
183                         desc.q.hdr_addr = MBUF_DMA_ADDR_DEFAULT(mbuf);
184                         q->hw_ring[q->next_alloc] = desc;
185                 }
186                 FM10K_PCI_REG_WRITE(q->tail_ptr, q->next_trigger);
187                 q->next_trigger += q->alloc_thresh;
188                 if (q->next_trigger >= q->nb_desc) {
189                         q->next_trigger = q->alloc_thresh - 1;
190                         q->next_alloc = 0;
191                 }
192         }
193
194         return count;
195 }
196
197 uint16_t
198 fm10k_recv_scattered_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
199                                 uint16_t nb_pkts)
200 {
201         struct rte_mbuf *mbuf;
202         union fm10k_rx_desc desc;
203         struct fm10k_rx_queue *q = rx_queue;
204         uint16_t count = 0;
205         uint16_t nb_rcv, nb_seg;
206         int alloc = 0;
207         uint16_t next_dd;
208         struct rte_mbuf *first_seg = q->pkt_first_seg;
209         struct rte_mbuf *last_seg = q->pkt_last_seg;
210         int ret;
211
212         next_dd = q->next_dd;
213         nb_rcv = 0;
214
215         nb_seg = RTE_MIN(nb_pkts, q->alloc_thresh);
216         for (count = 0; count < nb_seg; count++) {
217                 mbuf = q->sw_ring[next_dd];
218                 desc = q->hw_ring[next_dd];
219                 if (!(desc.d.staterr & FM10K_RXD_STATUS_DD))
220                         break;
221 #ifdef RTE_LIBRTE_FM10K_DEBUG_RX
222                 dump_rxd(&desc);
223 #endif
224
225                 if (++next_dd == q->nb_desc) {
226                         next_dd = 0;
227                         alloc = 1;
228                 }
229
230                 /* Prefetch next mbuf while processing current one. */
231                 rte_prefetch0(q->sw_ring[next_dd]);
232
233                 /*
234                  * When next RX descriptor is on a cache-line boundary,
235                  * prefetch the next 4 RX descriptors and the next 8 pointers
236                  * to mbufs.
237                  */
238                 if ((next_dd & 0x3) == 0) {
239                         rte_prefetch0(&q->hw_ring[next_dd]);
240                         rte_prefetch0(&q->sw_ring[next_dd]);
241                 }
242
243                 /* Fill data length */
244                 rte_pktmbuf_data_len(mbuf) = desc.w.length;
245
246                 /*
247                  * If this is the first buffer of the received packet,
248                  * set the pointer to the first mbuf of the packet and
249                  * initialize its context.
250                  * Otherwise, update the total length and the number of segments
251                  * of the current scattered packet, and update the pointer to
252                  * the last mbuf of the current packet.
253                  */
254                 if (!first_seg) {
255                         first_seg = mbuf;
256                         first_seg->pkt_len = desc.w.length;
257                 } else {
258                         first_seg->pkt_len =
259                                         (uint16_t)(first_seg->pkt_len +
260                                         rte_pktmbuf_data_len(mbuf));
261                         first_seg->nb_segs++;
262                         last_seg->next = mbuf;
263                 }
264
265                 /*
266                  * If this is not the last buffer of the received packet,
267                  * update the pointer to the last mbuf of the current scattered
268                  * packet and continue to parse the RX ring.
269                  */
270                 if (!(desc.d.staterr & FM10K_RXD_STATUS_EOP)) {
271                         last_seg = mbuf;
272                         continue;
273                 }
274
275                 first_seg->ol_flags = 0;
276 #ifdef RTE_LIBRTE_FM10K_RX_OLFLAGS_ENABLE
277                 rx_desc_to_ol_flags(first_seg, &desc);
278 #endif
279                 first_seg->hash.rss = desc.d.rss;
280
281                 /* Prefetch data of first segment, if configured to do so. */
282                 rte_packet_prefetch((char *)first_seg->buf_addr +
283                         first_seg->data_off);
284
285                 /*
286                  * Store the mbuf address into the next entry of the array
287                  * of returned packets.
288                  */
289                 rx_pkts[nb_rcv++] = first_seg;
290
291                 /*
292                  * Setup receipt context for a new packet.
293                  */
294                 first_seg = NULL;
295         }
296
297         q->next_dd = next_dd;
298
299         if ((q->next_dd > q->next_trigger) || (alloc == 1)) {
300                 ret = rte_mempool_get_bulk(q->mp,
301                                         (void **)&q->sw_ring[q->next_alloc],
302                                         q->alloc_thresh);
303
304                 if (unlikely(ret != 0)) {
305                         uint8_t port = q->port_id;
306                         PMD_RX_LOG(ERR, "Failed to alloc mbuf");
307                         /*
308                          * Need to restore next_dd if we cannot allocate new
309                          * buffers to replenish the old ones.
310                          */
311                         q->next_dd = (q->next_dd + q->nb_desc - count) %
312                                                                 q->nb_desc;
313                         rte_eth_devices[port].data->rx_mbuf_alloc_failed++;
314                         return 0;
315                 }
316
317                 for (; q->next_alloc <= q->next_trigger; ++q->next_alloc) {
318                         mbuf = q->sw_ring[q->next_alloc];
319
320                         /* setup static mbuf fields */
321                         fm10k_pktmbuf_reset(mbuf, q->port_id);
322
323                         /* write descriptor */
324                         desc.q.pkt_addr = MBUF_DMA_ADDR_DEFAULT(mbuf);
325                         desc.q.hdr_addr = MBUF_DMA_ADDR_DEFAULT(mbuf);
326                         q->hw_ring[q->next_alloc] = desc;
327                 }
328                 FM10K_PCI_REG_WRITE(q->tail_ptr, q->next_trigger);
329                 q->next_trigger += q->alloc_thresh;
330                 if (q->next_trigger >= q->nb_desc) {
331                         q->next_trigger = q->alloc_thresh - 1;
332                         q->next_alloc = 0;
333                 }
334         }
335
336         q->pkt_first_seg = first_seg;
337         q->pkt_last_seg = last_seg;
338
339         return nb_rcv;
340 }
341
342 static inline void tx_free_descriptors(struct fm10k_tx_queue *q)
343 {
344         uint16_t next_rs, count = 0;
345
346         next_rs = fifo_peek(&q->rs_tracker);
347         if (!(q->hw_ring[next_rs].flags & FM10K_TXD_FLAG_DONE))
348                 return;
349
350         /* the DONE flag is set on this descriptor so remove the ID
351          * from the RS bit tracker and free the buffers */
352         fifo_remove(&q->rs_tracker);
353
354         /* wrap around? if so, free buffers from last_free up to but NOT
355          * including nb_desc */
356         if (q->last_free > next_rs) {
357                 count = q->nb_desc - q->last_free;
358                 while (q->last_free < q->nb_desc) {
359                         rte_pktmbuf_free_seg(q->sw_ring[q->last_free]);
360                         q->sw_ring[q->last_free] = NULL;
361                         ++q->last_free;
362                 }
363                 q->last_free = 0;
364         }
365
366         /* adjust free descriptor count before the next loop */
367         q->nb_free += count + (next_rs + 1 - q->last_free);
368
369         /* free buffers from last_free, up to and including next_rs */
370         while (q->last_free <= next_rs) {
371                 rte_pktmbuf_free_seg(q->sw_ring[q->last_free]);
372                 q->sw_ring[q->last_free] = NULL;
373                 ++q->last_free;
374         }
375
376         if (q->last_free == q->nb_desc)
377                 q->last_free = 0;
378 }
379
380 static inline void tx_xmit_pkt(struct fm10k_tx_queue *q, struct rte_mbuf *mb)
381 {
382         uint16_t last_id;
383         uint8_t flags;
384
385         /* always set the LAST flag on the last descriptor used to
386          * transmit the packet */
387         flags = FM10K_TXD_FLAG_LAST;
388         last_id = q->next_free + mb->nb_segs - 1;
389         if (last_id >= q->nb_desc)
390                 last_id = last_id - q->nb_desc;
391
392         /* but only set the RS flag on the last descriptor if rs_thresh
393          * descriptors will be used since the RS flag was last set */
394         if ((q->nb_used + mb->nb_segs) >= q->rs_thresh) {
395                 flags |= FM10K_TXD_FLAG_RS;
396                 fifo_insert(&q->rs_tracker, last_id);
397                 q->nb_used = 0;
398         } else {
399                 q->nb_used = q->nb_used + mb->nb_segs;
400         }
401
402         q->hw_ring[last_id].flags = flags;
403         q->nb_free -= mb->nb_segs;
404
405         /* set checksum flags on first descriptor of packet. SCTP checksum
406          * offload is not supported, but we do not explicitly check for this
407          * case in favor of greatly simplified processing. */
408         if (mb->ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_L4_MASK))
409                 q->hw_ring[q->next_free].flags |= FM10K_TXD_FLAG_CSUM;
410
411         /* set vlan if requested */
412         if (mb->ol_flags & PKT_TX_VLAN_PKT)
413                 q->hw_ring[q->next_free].vlan = mb->vlan_tci;
414
415         /* fill up the rings */
416         for (; mb != NULL; mb = mb->next) {
417                 q->sw_ring[q->next_free] = mb;
418                 q->hw_ring[q->next_free].buffer_addr =
419                                 rte_cpu_to_le_64(MBUF_DMA_ADDR(mb));
420                 q->hw_ring[q->next_free].buflen =
421                                 rte_cpu_to_le_16(rte_pktmbuf_data_len(mb));
422                 if (++q->next_free == q->nb_desc)
423                         q->next_free = 0;
424         }
425 }
426
427 uint16_t
428 fm10k_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
429         uint16_t nb_pkts)
430 {
431         struct fm10k_tx_queue *q = tx_queue;
432         struct rte_mbuf *mb;
433         uint16_t count;
434
435         for (count = 0; count < nb_pkts; ++count) {
436                 mb = tx_pkts[count];
437
438                 /* running low on descriptors? try to free some... */
439                 if (q->nb_free < q->free_trigger)
440                         tx_free_descriptors(q);
441
442                 /* make sure there are enough free descriptors to transmit the
443                  * entire packet before doing anything */
444                 if (q->nb_free < mb->nb_segs)
445                         break;
446
447                 /* sanity check to make sure the mbuf is valid */
448                 if ((mb->nb_segs == 0) ||
449                     ((mb->nb_segs > 1) && (mb->next == NULL)))
450                         break;
451
452                 /* process the packet */
453                 tx_xmit_pkt(q, mb);
454         }
455
456         /* update the tail pointer if any packets were processed */
457         if (likely(count > 0))
458                 FM10K_PCI_REG_WRITE(q->tail_ptr, q->next_free);
459
460         return count;
461 }