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