tailq: remove unneeded inclusions
[dpdk.git] / lib / librte_pmd_e1000 / em_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 <sys/queue.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <stdint.h>
41 #include <stdarg.h>
42 #include <inttypes.h>
43
44 #include <rte_interrupts.h>
45 #include <rte_byteorder.h>
46 #include <rte_common.h>
47 #include <rte_log.h>
48 #include <rte_debug.h>
49 #include <rte_pci.h>
50 #include <rte_memory.h>
51 #include <rte_memcpy.h>
52 #include <rte_memzone.h>
53 #include <rte_launch.h>
54 #include <rte_eal.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_atomic.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_ring.h>
60 #include <rte_mempool.h>
61 #include <rte_malloc.h>
62 #include <rte_mbuf.h>
63 #include <rte_ether.h>
64 #include <rte_ethdev.h>
65 #include <rte_prefetch.h>
66 #include <rte_ip.h>
67 #include <rte_udp.h>
68 #include <rte_tcp.h>
69 #include <rte_sctp.h>
70 #include <rte_string_fns.h>
71
72 #include "e1000_logs.h"
73 #include "e1000/e1000_api.h"
74 #include "e1000_ethdev.h"
75 #include "e1000/e1000_osdep.h"
76
77 #define E1000_TXD_VLAN_SHIFT    16
78
79 #define E1000_RXDCTL_GRAN       0x01000000 /* RXDCTL Granularity */
80
81 static inline struct rte_mbuf *
82 rte_rxmbuf_alloc(struct rte_mempool *mp)
83 {
84         struct rte_mbuf *m;
85
86         m = __rte_mbuf_raw_alloc(mp);
87         __rte_mbuf_sanity_check_raw(m, 0);
88         return (m);
89 }
90
91 #define RTE_MBUF_DATA_DMA_ADDR(mb)             \
92         (uint64_t) ((mb)->buf_physaddr + (mb)->data_off)
93
94 #define RTE_MBUF_DATA_DMA_ADDR_DEFAULT(mb) \
95         (uint64_t) ((mb)->buf_physaddr + RTE_PKTMBUF_HEADROOM)
96
97 /**
98  * Structure associated with each descriptor of the RX ring of a RX queue.
99  */
100 struct em_rx_entry {
101         struct rte_mbuf *mbuf; /**< mbuf associated with RX descriptor. */
102 };
103
104 /**
105  * Structure associated with each descriptor of the TX ring of a TX queue.
106  */
107 struct em_tx_entry {
108         struct rte_mbuf *mbuf; /**< mbuf associated with TX desc, if any. */
109         uint16_t next_id; /**< Index of next descriptor in ring. */
110         uint16_t last_id; /**< Index of last scattered descriptor. */
111 };
112
113 /**
114  * Structure associated with each RX queue.
115  */
116 struct em_rx_queue {
117         struct rte_mempool  *mb_pool;   /**< mbuf pool to populate RX ring. */
118         volatile struct e1000_rx_desc *rx_ring; /**< RX ring virtual address. */
119         uint64_t            rx_ring_phys_addr; /**< RX ring DMA address. */
120         volatile uint32_t   *rdt_reg_addr; /**< RDT register address. */
121         volatile uint32_t   *rdh_reg_addr; /**< RDH register address. */
122         struct em_rx_entry *sw_ring;   /**< address of RX software ring. */
123         struct rte_mbuf *pkt_first_seg; /**< First segment of current packet. */
124         struct rte_mbuf *pkt_last_seg;  /**< Last segment of current packet. */
125         uint16_t            nb_rx_desc; /**< number of RX descriptors. */
126         uint16_t            rx_tail;    /**< current value of RDT register. */
127         uint16_t            nb_rx_hold; /**< number of held free RX desc. */
128         uint16_t            rx_free_thresh; /**< max free RX desc to hold. */
129         uint16_t            queue_id;   /**< RX queue index. */
130         uint8_t             port_id;    /**< Device port identifier. */
131         uint8_t             pthresh;    /**< Prefetch threshold register. */
132         uint8_t             hthresh;    /**< Host threshold register. */
133         uint8_t             wthresh;    /**< Write-back threshold register. */
134         uint8_t             crc_len;    /**< 0 if CRC stripped, 4 otherwise. */
135 };
136
137 /**
138  * Hardware context number
139  */
140 enum {
141         EM_CTX_0    = 0, /**< CTX0 */
142         EM_CTX_NUM  = 1, /**< CTX NUM */
143 };
144
145 /** Offload features */
146 union em_vlan_macip {
147         uint32_t data;
148         struct {
149                 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
150                 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
151                 uint16_t vlan_tci;
152                 /**< VLAN Tag Control Identifier (CPU order). */
153         } f;
154 };
155
156 /*
157  * Compare mask for vlan_macip_len.data,
158  * should be in sync with em_vlan_macip.f layout.
159  * */
160 #define TX_VLAN_CMP_MASK        0xFFFF0000  /**< VLAN length - 16-bits. */
161 #define TX_MAC_LEN_CMP_MASK     0x0000FE00  /**< MAC length - 7-bits. */
162 #define TX_IP_LEN_CMP_MASK      0x000001FF  /**< IP  length - 9-bits. */
163 /** MAC+IP  length. */
164 #define TX_MACIP_LEN_CMP_MASK   (TX_MAC_LEN_CMP_MASK | TX_IP_LEN_CMP_MASK)
165
166 /**
167  * Structure to check if new context need be built
168  */
169 struct em_ctx_info {
170         uint64_t flags;              /**< ol_flags related to context build. */
171         uint32_t cmp_mask;           /**< compare mask */
172         union em_vlan_macip hdrlen;  /**< L2 and L3 header lenghts */
173 };
174
175 /**
176  * Structure associated with each TX queue.
177  */
178 struct em_tx_queue {
179         volatile struct e1000_data_desc *tx_ring; /**< TX ring address */
180         uint64_t               tx_ring_phys_addr; /**< TX ring DMA address. */
181         struct em_tx_entry    *sw_ring; /**< virtual address of SW ring. */
182         volatile uint32_t      *tdt_reg_addr; /**< Address of TDT register. */
183         uint16_t               nb_tx_desc;    /**< number of TX descriptors. */
184         uint16_t               tx_tail;  /**< Current value of TDT register. */
185         uint16_t               tx_free_thresh;/**< minimum TX before freeing. */
186         /**< Number of TX descriptors to use before RS bit is set. */
187         uint16_t               tx_rs_thresh;
188         /** Number of TX descriptors used since RS bit was set. */
189         uint16_t               nb_tx_used;
190         /** Index to last TX descriptor to have been cleaned. */
191         uint16_t               last_desc_cleaned;
192         /** Total number of TX descriptors ready to be allocated. */
193         uint16_t               nb_tx_free;
194         uint16_t               queue_id; /**< TX queue index. */
195         uint8_t                port_id;  /**< Device port identifier. */
196         uint8_t                pthresh;  /**< Prefetch threshold register. */
197         uint8_t                hthresh;  /**< Host threshold register. */
198         uint8_t                wthresh;  /**< Write-back threshold register. */
199         struct em_ctx_info ctx_cache;
200         /**< Hardware context history.*/
201 };
202
203 #if 1
204 #define RTE_PMD_USE_PREFETCH
205 #endif
206
207 #ifdef RTE_PMD_USE_PREFETCH
208 #define rte_em_prefetch(p)      rte_prefetch0(p)
209 #else
210 #define rte_em_prefetch(p)      do {} while(0)
211 #endif
212
213 #ifdef RTE_PMD_PACKET_PREFETCH
214 #define rte_packet_prefetch(p) rte_prefetch1(p)
215 #else
216 #define rte_packet_prefetch(p)  do {} while(0)
217 #endif
218
219 #ifndef DEFAULT_TX_FREE_THRESH
220 #define DEFAULT_TX_FREE_THRESH  32
221 #endif /* DEFAULT_TX_FREE_THRESH */
222
223 #ifndef DEFAULT_TX_RS_THRESH
224 #define DEFAULT_TX_RS_THRESH  32
225 #endif /* DEFAULT_TX_RS_THRESH */
226
227
228 /*********************************************************************
229  *
230  *  TX function
231  *
232  **********************************************************************/
233
234 /*
235  * Populates TX context descriptor.
236  */
237 static inline void
238 em_set_xmit_ctx(struct em_tx_queue* txq,
239                 volatile struct e1000_context_desc *ctx_txd,
240                 uint64_t flags,
241                 union em_vlan_macip hdrlen)
242 {
243         uint32_t cmp_mask, cmd_len;
244         uint16_t ipcse, l2len;
245         struct e1000_context_desc ctx;
246
247         cmp_mask = 0;
248         cmd_len = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_C;
249
250         l2len = hdrlen.f.l2_len;
251         ipcse = (uint16_t)(l2len + hdrlen.f.l3_len);
252
253         /* setup IPCS* fields */
254         ctx.lower_setup.ip_fields.ipcss = (uint8_t)l2len;
255         ctx.lower_setup.ip_fields.ipcso = (uint8_t)(l2len +
256                         offsetof(struct ipv4_hdr, hdr_checksum));
257
258         /*
259          * When doing checksum or TCP segmentation with IPv6 headers,
260          * IPCSE field should be set t0 0.
261          */
262         if (flags & PKT_TX_IP_CKSUM) {
263                 ctx.lower_setup.ip_fields.ipcse =
264                         (uint16_t)rte_cpu_to_le_16(ipcse - 1);
265                 cmd_len |= E1000_TXD_CMD_IP;
266                 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
267         } else {
268                 ctx.lower_setup.ip_fields.ipcse = 0;
269         }
270
271         /* setup TUCS* fields */
272         ctx.upper_setup.tcp_fields.tucss = (uint8_t)ipcse;
273         ctx.upper_setup.tcp_fields.tucse = 0;
274
275         switch (flags & PKT_TX_L4_MASK) {
276         case PKT_TX_UDP_CKSUM:
277                 ctx.upper_setup.tcp_fields.tucso = (uint8_t)(ipcse +
278                                 offsetof(struct udp_hdr, dgram_cksum));
279                 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
280                 break;
281         case PKT_TX_TCP_CKSUM:
282                 ctx.upper_setup.tcp_fields.tucso = (uint8_t)(ipcse +
283                                 offsetof(struct tcp_hdr, cksum));
284                 cmd_len |= E1000_TXD_CMD_TCP;
285                 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
286                 break;
287         default:
288                 ctx.upper_setup.tcp_fields.tucso = 0;
289         }
290
291         ctx.cmd_and_length = rte_cpu_to_le_32(cmd_len);
292         ctx.tcp_seg_setup.data = 0;
293
294         *ctx_txd = ctx;
295
296         txq->ctx_cache.flags = flags;
297         txq->ctx_cache.cmp_mask = cmp_mask;
298         txq->ctx_cache.hdrlen = hdrlen;
299 }
300
301 /*
302  * Check which hardware context can be used. Use the existing match
303  * or create a new context descriptor.
304  */
305 static inline uint32_t
306 what_ctx_update(struct em_tx_queue *txq, uint64_t flags,
307                 union em_vlan_macip hdrlen)
308 {
309         /* If match with the current context */
310         if (likely (txq->ctx_cache.flags == flags &&
311                         ((txq->ctx_cache.hdrlen.data ^ hdrlen.data) &
312                         txq->ctx_cache.cmp_mask) == 0))
313                 return (EM_CTX_0);
314
315         /* Mismatch */
316         return (EM_CTX_NUM);
317 }
318
319 /* Reset transmit descriptors after they have been used */
320 static inline int
321 em_xmit_cleanup(struct em_tx_queue *txq)
322 {
323         struct em_tx_entry *sw_ring = txq->sw_ring;
324         volatile struct e1000_data_desc *txr = txq->tx_ring;
325         uint16_t last_desc_cleaned = txq->last_desc_cleaned;
326         uint16_t nb_tx_desc = txq->nb_tx_desc;
327         uint16_t desc_to_clean_to;
328         uint16_t nb_tx_to_clean;
329
330         /* Determine the last descriptor needing to be cleaned */
331         desc_to_clean_to = (uint16_t)(last_desc_cleaned + txq->tx_rs_thresh);
332         if (desc_to_clean_to >= nb_tx_desc)
333                 desc_to_clean_to = (uint16_t)(desc_to_clean_to - nb_tx_desc);
334
335         /* Check to make sure the last descriptor to clean is done */
336         desc_to_clean_to = sw_ring[desc_to_clean_to].last_id;
337         if (! (txr[desc_to_clean_to].upper.fields.status & E1000_TXD_STAT_DD))
338         {
339                 PMD_TX_FREE_LOG(DEBUG,
340                                 "TX descriptor %4u is not done"
341                                 "(port=%d queue=%d)", desc_to_clean_to,
342                                 txq->port_id, txq->queue_id);
343                 /* Failed to clean any descriptors, better luck next time */
344                 return -(1);
345         }
346
347         /* Figure out how many descriptors will be cleaned */
348         if (last_desc_cleaned > desc_to_clean_to)
349                 nb_tx_to_clean = (uint16_t)((nb_tx_desc - last_desc_cleaned) +
350                                                         desc_to_clean_to);
351         else
352                 nb_tx_to_clean = (uint16_t)(desc_to_clean_to -
353                                                 last_desc_cleaned);
354
355         PMD_TX_FREE_LOG(DEBUG,
356                         "Cleaning %4u TX descriptors: %4u to %4u "
357                         "(port=%d queue=%d)", nb_tx_to_clean,
358                         last_desc_cleaned, desc_to_clean_to, txq->port_id,
359                         txq->queue_id);
360
361         /*
362          * The last descriptor to clean is done, so that means all the
363          * descriptors from the last descriptor that was cleaned
364          * up to the last descriptor with the RS bit set
365          * are done. Only reset the threshold descriptor.
366          */
367         txr[desc_to_clean_to].upper.fields.status = 0;
368
369         /* Update the txq to reflect the last descriptor that was cleaned */
370         txq->last_desc_cleaned = desc_to_clean_to;
371         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + nb_tx_to_clean);
372
373         /* No Error */
374         return (0);
375 }
376
377 static inline uint32_t
378 tx_desc_cksum_flags_to_upper(uint64_t ol_flags)
379 {
380         static const uint32_t l4_olinfo[2] = {0, E1000_TXD_POPTS_TXSM << 8};
381         static const uint32_t l3_olinfo[2] = {0, E1000_TXD_POPTS_IXSM << 8};
382         uint32_t tmp;
383
384         tmp = l4_olinfo[(ol_flags & PKT_TX_L4_MASK) != PKT_TX_L4_NO_CKSUM];
385         tmp |= l3_olinfo[(ol_flags & PKT_TX_IP_CKSUM) != 0];
386         return (tmp);
387 }
388
389 uint16_t
390 eth_em_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
391                 uint16_t nb_pkts)
392 {
393         struct em_tx_queue *txq;
394         struct em_tx_entry *sw_ring;
395         struct em_tx_entry *txe, *txn;
396         volatile struct e1000_data_desc *txr;
397         volatile struct e1000_data_desc *txd;
398         struct rte_mbuf     *tx_pkt;
399         struct rte_mbuf     *m_seg;
400         uint64_t buf_dma_addr;
401         uint32_t popts_spec;
402         uint32_t cmd_type_len;
403         uint16_t slen;
404         uint64_t ol_flags;
405         uint16_t tx_id;
406         uint16_t tx_last;
407         uint16_t nb_tx;
408         uint16_t nb_used;
409         uint64_t tx_ol_req;
410         uint32_t ctx;
411         uint32_t new_ctx;
412         union em_vlan_macip hdrlen;
413
414         txq = tx_queue;
415         sw_ring = txq->sw_ring;
416         txr     = txq->tx_ring;
417         tx_id   = txq->tx_tail;
418         txe = &sw_ring[tx_id];
419
420         /* Determine if the descriptor ring needs to be cleaned. */
421         if ((txq->nb_tx_desc - txq->nb_tx_free) > txq->tx_free_thresh) {
422                 em_xmit_cleanup(txq);
423         }
424
425         /* TX loop */
426         for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
427                 new_ctx = 0;
428                 tx_pkt = *tx_pkts++;
429
430                 RTE_MBUF_PREFETCH_TO_FREE(txe->mbuf);
431
432                 /*
433                  * Determine how many (if any) context descriptors
434                  * are needed for offload functionality.
435                  */
436                 ol_flags = tx_pkt->ol_flags;
437
438                 /* If hardware offload required */
439                 tx_ol_req = (ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_L4_MASK));
440                 if (tx_ol_req) {
441                         hdrlen.f.vlan_tci = tx_pkt->vlan_tci;
442                         hdrlen.f.l2_len = tx_pkt->l2_len;
443                         hdrlen.f.l3_len = tx_pkt->l3_len;
444                         /* If new context to be built or reuse the exist ctx. */
445                         ctx = what_ctx_update(txq, tx_ol_req, hdrlen);
446
447                         /* Only allocate context descriptor if required*/
448                         new_ctx = (ctx == EM_CTX_NUM);
449                 }
450
451                 /*
452                  * Keep track of how many descriptors are used this loop
453                  * This will always be the number of segments + the number of
454                  * Context descriptors required to transmit the packet
455                  */
456                 nb_used = (uint16_t)(tx_pkt->nb_segs + new_ctx);
457
458                 /*
459                  * The number of descriptors that must be allocated for a
460                  * packet is the number of segments of that packet, plus 1
461                  * Context Descriptor for the hardware offload, if any.
462                  * Determine the last TX descriptor to allocate in the TX ring
463                  * for the packet, starting from the current position (tx_id)
464                  * in the ring.
465                  */
466                 tx_last = (uint16_t) (tx_id + nb_used - 1);
467
468                 /* Circular ring */
469                 if (tx_last >= txq->nb_tx_desc)
470                         tx_last = (uint16_t) (tx_last - txq->nb_tx_desc);
471
472                 PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u pktlen=%u"
473                            " tx_first=%u tx_last=%u",
474                            (unsigned) txq->port_id,
475                            (unsigned) txq->queue_id,
476                            (unsigned) tx_pkt->pkt_len,
477                            (unsigned) tx_id,
478                            (unsigned) tx_last);
479
480                 /*
481                  * Make sure there are enough TX descriptors available to
482                  * transmit the entire packet.
483                  * nb_used better be less than or equal to txq->tx_rs_thresh
484                  */
485                 while (unlikely (nb_used > txq->nb_tx_free)) {
486                         PMD_TX_FREE_LOG(DEBUG, "Not enough free TX descriptors "
487                                         "nb_used=%4u nb_free=%4u "
488                                         "(port=%d queue=%d)",
489                                         nb_used, txq->nb_tx_free,
490                                         txq->port_id, txq->queue_id);
491
492                         if (em_xmit_cleanup(txq) != 0) {
493                                 /* Could not clean any descriptors */
494                                 if (nb_tx == 0)
495                                         return (0);
496                                 goto end_of_tx;
497                         }
498                 }
499
500                 /*
501                  * By now there are enough free TX descriptors to transmit
502                  * the packet.
503                  */
504
505                 /*
506                  * Set common flags of all TX Data Descriptors.
507                  *
508                  * The following bits must be set in all Data Descriptors:
509                  *    - E1000_TXD_DTYP_DATA
510                  *    - E1000_TXD_DTYP_DEXT
511                  *
512                  * The following bits must be set in the first Data Descriptor
513                  * and are ignored in the other ones:
514                  *    - E1000_TXD_POPTS_IXSM
515                  *    - E1000_TXD_POPTS_TXSM
516                  *
517                  * The following bits must be set in the last Data Descriptor
518                  * and are ignored in the other ones:
519                  *    - E1000_TXD_CMD_VLE
520                  *    - E1000_TXD_CMD_IFCS
521                  *
522                  * The following bits must only be set in the last Data
523                  * Descriptor:
524                  *   - E1000_TXD_CMD_EOP
525                  *
526                  * The following bits can be set in any Data Descriptor, but
527                  * are only set in the last Data Descriptor:
528                  *   - E1000_TXD_CMD_RS
529                  */
530                 cmd_type_len = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
531                         E1000_TXD_CMD_IFCS;
532                 popts_spec = 0;
533
534                 /* Set VLAN Tag offload fields. */
535                 if (ol_flags & PKT_TX_VLAN_PKT) {
536                         cmd_type_len |= E1000_TXD_CMD_VLE;
537                         popts_spec = tx_pkt->vlan_tci << E1000_TXD_VLAN_SHIFT;
538                 }
539
540                 if (tx_ol_req) {
541                         /*
542                          * Setup the TX Context Descriptor if required
543                          */
544                         if (new_ctx) {
545                                 volatile struct e1000_context_desc *ctx_txd;
546
547                                 ctx_txd = (volatile struct e1000_context_desc *)
548                                         &txr[tx_id];
549
550                                 txn = &sw_ring[txe->next_id];
551                                 RTE_MBUF_PREFETCH_TO_FREE(txn->mbuf);
552
553                                 if (txe->mbuf != NULL) {
554                                         rte_pktmbuf_free_seg(txe->mbuf);
555                                         txe->mbuf = NULL;
556                                 }
557
558                                 em_set_xmit_ctx(txq, ctx_txd, tx_ol_req,
559                                         hdrlen);
560
561                                 txe->last_id = tx_last;
562                                 tx_id = txe->next_id;
563                                 txe = txn;
564                         }
565
566                         /*
567                          * Setup the TX Data Descriptor,
568                          * This path will go through
569                          * whatever new/reuse the context descriptor
570                          */
571                         popts_spec |= tx_desc_cksum_flags_to_upper(ol_flags);
572                 }
573
574                 m_seg = tx_pkt;
575                 do {
576                         txd = &txr[tx_id];
577                         txn = &sw_ring[txe->next_id];
578
579                         if (txe->mbuf != NULL)
580                                 rte_pktmbuf_free_seg(txe->mbuf);
581                         txe->mbuf = m_seg;
582
583                         /*
584                          * Set up Transmit Data Descriptor.
585                          */
586                         slen = m_seg->data_len;
587                         buf_dma_addr = RTE_MBUF_DATA_DMA_ADDR(m_seg);
588
589                         txd->buffer_addr = rte_cpu_to_le_64(buf_dma_addr);
590                         txd->lower.data = rte_cpu_to_le_32(cmd_type_len | slen);
591                         txd->upper.data = rte_cpu_to_le_32(popts_spec);
592
593                         txe->last_id = tx_last;
594                         tx_id = txe->next_id;
595                         txe = txn;
596                         m_seg = m_seg->next;
597                 } while (m_seg != NULL);
598
599                 /*
600                  * The last packet data descriptor needs End Of Packet (EOP)
601                  */
602                 cmd_type_len |= E1000_TXD_CMD_EOP;
603                 txq->nb_tx_used = (uint16_t)(txq->nb_tx_used + nb_used);
604                 txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_used);
605
606                 /* Set RS bit only on threshold packets' last descriptor */
607                 if (txq->nb_tx_used >= txq->tx_rs_thresh) {
608                         PMD_TX_FREE_LOG(DEBUG,
609                                         "Setting RS bit on TXD id=%4u "
610                                         "(port=%d queue=%d)",
611                                         tx_last, txq->port_id, txq->queue_id);
612
613                         cmd_type_len |= E1000_TXD_CMD_RS;
614
615                         /* Update txq RS bit counters */
616                         txq->nb_tx_used = 0;
617                 }
618                 txd->lower.data |= rte_cpu_to_le_32(cmd_type_len);
619         }
620 end_of_tx:
621         rte_wmb();
622
623         /*
624          * Set the Transmit Descriptor Tail (TDT)
625          */
626         PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
627                 (unsigned) txq->port_id, (unsigned) txq->queue_id,
628                 (unsigned) tx_id, (unsigned) nb_tx);
629         E1000_PCI_REG_WRITE(txq->tdt_reg_addr, tx_id);
630         txq->tx_tail = tx_id;
631
632         return (nb_tx);
633 }
634
635 /*********************************************************************
636  *
637  *  RX functions
638  *
639  **********************************************************************/
640
641 static inline uint64_t
642 rx_desc_status_to_pkt_flags(uint32_t rx_status)
643 {
644         uint64_t pkt_flags;
645
646         /* Check if VLAN present */
647         pkt_flags = ((rx_status & E1000_RXD_STAT_VP) ?  PKT_RX_VLAN_PKT : 0);
648
649         return pkt_flags;
650 }
651
652 static inline uint64_t
653 rx_desc_error_to_pkt_flags(uint32_t rx_error)
654 {
655         uint64_t pkt_flags = 0;
656
657         if (rx_error & E1000_RXD_ERR_IPE)
658                 pkt_flags |= PKT_RX_IP_CKSUM_BAD;
659         if (rx_error & E1000_RXD_ERR_TCPE)
660                 pkt_flags |= PKT_RX_L4_CKSUM_BAD;
661         return (pkt_flags);
662 }
663
664 uint16_t
665 eth_em_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
666                 uint16_t nb_pkts)
667 {
668         volatile struct e1000_rx_desc *rx_ring;
669         volatile struct e1000_rx_desc *rxdp;
670         struct em_rx_queue *rxq;
671         struct em_rx_entry *sw_ring;
672         struct em_rx_entry *rxe;
673         struct rte_mbuf *rxm;
674         struct rte_mbuf *nmb;
675         struct e1000_rx_desc rxd;
676         uint64_t dma_addr;
677         uint16_t pkt_len;
678         uint16_t rx_id;
679         uint16_t nb_rx;
680         uint16_t nb_hold;
681         uint8_t status;
682
683         rxq = rx_queue;
684
685         nb_rx = 0;
686         nb_hold = 0;
687         rx_id = rxq->rx_tail;
688         rx_ring = rxq->rx_ring;
689         sw_ring = rxq->sw_ring;
690         while (nb_rx < nb_pkts) {
691                 /*
692                  * The order of operations here is important as the DD status
693                  * bit must not be read after any other descriptor fields.
694                  * rx_ring and rxdp are pointing to volatile data so the order
695                  * of accesses cannot be reordered by the compiler. If they were
696                  * not volatile, they could be reordered which could lead to
697                  * using invalid descriptor fields when read from rxd.
698                  */
699                 rxdp = &rx_ring[rx_id];
700                 status = rxdp->status;
701                 if (! (status & E1000_RXD_STAT_DD))
702                         break;
703                 rxd = *rxdp;
704
705                 /*
706                  * End of packet.
707                  *
708                  * If the E1000_RXD_STAT_EOP flag is not set, the RX packet is
709                  * likely to be invalid and to be dropped by the various
710                  * validation checks performed by the network stack.
711                  *
712                  * Allocate a new mbuf to replenish the RX ring descriptor.
713                  * If the allocation fails:
714                  *    - arrange for that RX descriptor to be the first one
715                  *      being parsed the next time the receive function is
716                  *      invoked [on the same queue].
717                  *
718                  *    - Stop parsing the RX ring and return immediately.
719                  *
720                  * This policy do not drop the packet received in the RX
721                  * descriptor for which the allocation of a new mbuf failed.
722                  * Thus, it allows that packet to be later retrieved if
723                  * mbuf have been freed in the mean time.
724                  * As a side effect, holding RX descriptors instead of
725                  * systematically giving them back to the NIC may lead to
726                  * RX ring exhaustion situations.
727                  * However, the NIC can gracefully prevent such situations
728                  * to happen by sending specific "back-pressure" flow control
729                  * frames to its peer(s).
730                  */
731                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
732                            "status=0x%x pkt_len=%u",
733                            (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
734                            (unsigned) rx_id, (unsigned) status,
735                            (unsigned) rte_le_to_cpu_16(rxd.length));
736
737                 nmb = rte_rxmbuf_alloc(rxq->mb_pool);
738                 if (nmb == NULL) {
739                         PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
740                                    "queue_id=%u",
741                                    (unsigned) rxq->port_id,
742                                    (unsigned) rxq->queue_id);
743                         rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed++;
744                         break;
745                 }
746
747                 nb_hold++;
748                 rxe = &sw_ring[rx_id];
749                 rx_id++;
750                 if (rx_id == rxq->nb_rx_desc)
751                         rx_id = 0;
752
753                 /* Prefetch next mbuf while processing current one. */
754                 rte_em_prefetch(sw_ring[rx_id].mbuf);
755
756                 /*
757                  * When next RX descriptor is on a cache-line boundary,
758                  * prefetch the next 4 RX descriptors and the next 8 pointers
759                  * to mbufs.
760                  */
761                 if ((rx_id & 0x3) == 0) {
762                         rte_em_prefetch(&rx_ring[rx_id]);
763                         rte_em_prefetch(&sw_ring[rx_id]);
764                 }
765
766                 /* Rearm RXD: attach new mbuf and reset status to zero. */
767
768                 rxm = rxe->mbuf;
769                 rxe->mbuf = nmb;
770                 dma_addr =
771                         rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR_DEFAULT(nmb));
772                 rxdp->buffer_addr = dma_addr;
773                 rxdp->status = 0;
774
775                 /*
776                  * Initialize the returned mbuf.
777                  * 1) setup generic mbuf fields:
778                  *    - number of segments,
779                  *    - next segment,
780                  *    - packet length,
781                  *    - RX port identifier.
782                  * 2) integrate hardware offload data, if any:
783                  *    - RSS flag & hash,
784                  *    - IP checksum flag,
785                  *    - VLAN TCI, if any,
786                  *    - error flags.
787                  */
788                 pkt_len = (uint16_t) (rte_le_to_cpu_16(rxd.length) -
789                                 rxq->crc_len);
790                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
791                 rte_packet_prefetch((char *)rxm->buf_addr + rxm->data_off);
792                 rxm->nb_segs = 1;
793                 rxm->next = NULL;
794                 rxm->pkt_len = pkt_len;
795                 rxm->data_len = pkt_len;
796                 rxm->port = rxq->port_id;
797
798                 rxm->ol_flags = rx_desc_status_to_pkt_flags(status);
799                 rxm->ol_flags = rxm->ol_flags |
800                                 rx_desc_error_to_pkt_flags(rxd.errors);
801
802                 /* Only valid if PKT_RX_VLAN_PKT set in pkt_flags */
803                 rxm->vlan_tci = rte_le_to_cpu_16(rxd.special);
804
805                 /*
806                  * Store the mbuf address into the next entry of the array
807                  * of returned packets.
808                  */
809                 rx_pkts[nb_rx++] = rxm;
810         }
811         rxq->rx_tail = rx_id;
812
813         /*
814          * If the number of free RX descriptors is greater than the RX free
815          * threshold of the queue, advance the Receive Descriptor Tail (RDT)
816          * register.
817          * Update the RDT with the value of the last processed RX descriptor
818          * minus 1, to guarantee that the RDT register is never equal to the
819          * RDH register, which creates a "full" ring situtation from the
820          * hardware point of view...
821          */
822         nb_hold = (uint16_t) (nb_hold + rxq->nb_rx_hold);
823         if (nb_hold > rxq->rx_free_thresh) {
824                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
825                            "nb_hold=%u nb_rx=%u",
826                            (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
827                            (unsigned) rx_id, (unsigned) nb_hold,
828                            (unsigned) nb_rx);
829                 rx_id = (uint16_t) ((rx_id == 0) ?
830                         (rxq->nb_rx_desc - 1) : (rx_id - 1));
831                 E1000_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
832                 nb_hold = 0;
833         }
834         rxq->nb_rx_hold = nb_hold;
835         return (nb_rx);
836 }
837
838 uint16_t
839 eth_em_recv_scattered_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
840                          uint16_t nb_pkts)
841 {
842         struct em_rx_queue *rxq;
843         volatile struct e1000_rx_desc *rx_ring;
844         volatile struct e1000_rx_desc *rxdp;
845         struct em_rx_entry *sw_ring;
846         struct em_rx_entry *rxe;
847         struct rte_mbuf *first_seg;
848         struct rte_mbuf *last_seg;
849         struct rte_mbuf *rxm;
850         struct rte_mbuf *nmb;
851         struct e1000_rx_desc rxd;
852         uint64_t dma; /* Physical address of mbuf data buffer */
853         uint16_t rx_id;
854         uint16_t nb_rx;
855         uint16_t nb_hold;
856         uint16_t data_len;
857         uint8_t status;
858
859         rxq = rx_queue;
860
861         nb_rx = 0;
862         nb_hold = 0;
863         rx_id = rxq->rx_tail;
864         rx_ring = rxq->rx_ring;
865         sw_ring = rxq->sw_ring;
866
867         /*
868          * Retrieve RX context of current packet, if any.
869          */
870         first_seg = rxq->pkt_first_seg;
871         last_seg = rxq->pkt_last_seg;
872
873         while (nb_rx < nb_pkts) {
874         next_desc:
875                 /*
876                  * The order of operations here is important as the DD status
877                  * bit must not be read after any other descriptor fields.
878                  * rx_ring and rxdp are pointing to volatile data so the order
879                  * of accesses cannot be reordered by the compiler. If they were
880                  * not volatile, they could be reordered which could lead to
881                  * using invalid descriptor fields when read from rxd.
882                  */
883                 rxdp = &rx_ring[rx_id];
884                 status = rxdp->status;
885                 if (! (status & E1000_RXD_STAT_DD))
886                         break;
887                 rxd = *rxdp;
888
889                 /*
890                  * Descriptor done.
891                  *
892                  * Allocate a new mbuf to replenish the RX ring descriptor.
893                  * If the allocation fails:
894                  *    - arrange for that RX descriptor to be the first one
895                  *      being parsed the next time the receive function is
896                  *      invoked [on the same queue].
897                  *
898                  *    - Stop parsing the RX ring and return immediately.
899                  *
900                  * This policy does not drop the packet received in the RX
901                  * descriptor for which the allocation of a new mbuf failed.
902                  * Thus, it allows that packet to be later retrieved if
903                  * mbuf have been freed in the mean time.
904                  * As a side effect, holding RX descriptors instead of
905                  * systematically giving them back to the NIC may lead to
906                  * RX ring exhaustion situations.
907                  * However, the NIC can gracefully prevent such situations
908                  * to happen by sending specific "back-pressure" flow control
909                  * frames to its peer(s).
910                  */
911                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
912                            "status=0x%x data_len=%u",
913                            (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
914                            (unsigned) rx_id, (unsigned) status,
915                            (unsigned) rte_le_to_cpu_16(rxd.length));
916
917                 nmb = rte_rxmbuf_alloc(rxq->mb_pool);
918                 if (nmb == NULL) {
919                         PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
920                                    "queue_id=%u", (unsigned) rxq->port_id,
921                                    (unsigned) rxq->queue_id);
922                         rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed++;
923                         break;
924                 }
925
926                 nb_hold++;
927                 rxe = &sw_ring[rx_id];
928                 rx_id++;
929                 if (rx_id == rxq->nb_rx_desc)
930                         rx_id = 0;
931
932                 /* Prefetch next mbuf while processing current one. */
933                 rte_em_prefetch(sw_ring[rx_id].mbuf);
934
935                 /*
936                  * When next RX descriptor is on a cache-line boundary,
937                  * prefetch the next 4 RX descriptors and the next 8 pointers
938                  * to mbufs.
939                  */
940                 if ((rx_id & 0x3) == 0) {
941                         rte_em_prefetch(&rx_ring[rx_id]);
942                         rte_em_prefetch(&sw_ring[rx_id]);
943                 }
944
945                 /*
946                  * Update RX descriptor with the physical address of the new
947                  * data buffer of the new allocated mbuf.
948                  */
949                 rxm = rxe->mbuf;
950                 rxe->mbuf = nmb;
951                 dma = rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR_DEFAULT(nmb));
952                 rxdp->buffer_addr = dma;
953                 rxdp->status = 0;
954
955                 /*
956                  * Set data length & data buffer address of mbuf.
957                  */
958                 data_len = rte_le_to_cpu_16(rxd.length);
959                 rxm->data_len = data_len;
960                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
961
962                 /*
963                  * If this is the first buffer of the received packet,
964                  * set the pointer to the first mbuf of the packet and
965                  * initialize its context.
966                  * Otherwise, update the total length and the number of segments
967                  * of the current scattered packet, and update the pointer to
968                  * the last mbuf of the current packet.
969                  */
970                 if (first_seg == NULL) {
971                         first_seg = rxm;
972                         first_seg->pkt_len = data_len;
973                         first_seg->nb_segs = 1;
974                 } else {
975                         first_seg->pkt_len += data_len;
976                         first_seg->nb_segs++;
977                         last_seg->next = rxm;
978                 }
979
980                 /*
981                  * If this is not the last buffer of the received packet,
982                  * update the pointer to the last mbuf of the current scattered
983                  * packet and continue to parse the RX ring.
984                  */
985                 if (! (status & E1000_RXD_STAT_EOP)) {
986                         last_seg = rxm;
987                         goto next_desc;
988                 }
989
990                 /*
991                  * This is the last buffer of the received packet.
992                  * If the CRC is not stripped by the hardware:
993                  *   - Subtract the CRC length from the total packet length.
994                  *   - If the last buffer only contains the whole CRC or a part
995                  *     of it, free the mbuf associated to the last buffer.
996                  *     If part of the CRC is also contained in the previous
997                  *     mbuf, subtract the length of that CRC part from the
998                  *     data length of the previous mbuf.
999                  */
1000                 rxm->next = NULL;
1001                 if (unlikely(rxq->crc_len > 0)) {
1002                         first_seg->pkt_len -= ETHER_CRC_LEN;
1003                         if (data_len <= ETHER_CRC_LEN) {
1004                                 rte_pktmbuf_free_seg(rxm);
1005                                 first_seg->nb_segs--;
1006                                 last_seg->data_len = (uint16_t)
1007                                         (last_seg->data_len -
1008                                          (ETHER_CRC_LEN - data_len));
1009                                 last_seg->next = NULL;
1010                         } else
1011                                 rxm->data_len =
1012                                         (uint16_t) (data_len - ETHER_CRC_LEN);
1013                 }
1014
1015                 /*
1016                  * Initialize the first mbuf of the returned packet:
1017                  *    - RX port identifier,
1018                  *    - hardware offload data, if any:
1019                  *      - IP checksum flag,
1020                  *      - error flags.
1021                  */
1022                 first_seg->port = rxq->port_id;
1023
1024                 first_seg->ol_flags = rx_desc_status_to_pkt_flags(status);
1025                 first_seg->ol_flags = first_seg->ol_flags |
1026                                         rx_desc_error_to_pkt_flags(rxd.errors);
1027
1028                 /* Only valid if PKT_RX_VLAN_PKT set in pkt_flags */
1029                 rxm->vlan_tci = rte_le_to_cpu_16(rxd.special);
1030
1031                 /* Prefetch data of first segment, if configured to do so. */
1032                 rte_packet_prefetch((char *)first_seg->buf_addr +
1033                         first_seg->data_off);
1034
1035                 /*
1036                  * Store the mbuf address into the next entry of the array
1037                  * of returned packets.
1038                  */
1039                 rx_pkts[nb_rx++] = first_seg;
1040
1041                 /*
1042                  * Setup receipt context for a new packet.
1043                  */
1044                 first_seg = NULL;
1045         }
1046
1047         /*
1048          * Record index of the next RX descriptor to probe.
1049          */
1050         rxq->rx_tail = rx_id;
1051
1052         /*
1053          * Save receive context.
1054          */
1055         rxq->pkt_first_seg = first_seg;
1056         rxq->pkt_last_seg = last_seg;
1057
1058         /*
1059          * If the number of free RX descriptors is greater than the RX free
1060          * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1061          * register.
1062          * Update the RDT with the value of the last processed RX descriptor
1063          * minus 1, to guarantee that the RDT register is never equal to the
1064          * RDH register, which creates a "full" ring situtation from the
1065          * hardware point of view...
1066          */
1067         nb_hold = (uint16_t) (nb_hold + rxq->nb_rx_hold);
1068         if (nb_hold > rxq->rx_free_thresh) {
1069                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
1070                            "nb_hold=%u nb_rx=%u",
1071                            (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
1072                            (unsigned) rx_id, (unsigned) nb_hold,
1073                            (unsigned) nb_rx);
1074                 rx_id = (uint16_t) ((rx_id == 0) ?
1075                         (rxq->nb_rx_desc - 1) : (rx_id - 1));
1076                 E1000_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
1077                 nb_hold = 0;
1078         }
1079         rxq->nb_rx_hold = nb_hold;
1080         return (nb_rx);
1081 }
1082
1083 /*
1084  * Rings setup and release.
1085  *
1086  * TDBA/RDBA should be aligned on 16 byte boundary. But TDLEN/RDLEN should be
1087  * multiple of 128 bytes. So we align TDBA/RDBA on 128 byte boundary.
1088  * This will also optimize cache line size effect.
1089  * H/W supports up to cache line size 128.
1090  */
1091 #define EM_ALIGN 128
1092
1093 /*
1094  * Maximum number of Ring Descriptors.
1095  *
1096  * Since RDLEN/TDLEN should be multiple of 128 bytes, the number of ring
1097  * desscriptors should meet the following condition:
1098  * (num_ring_desc * sizeof(struct e1000_rx/tx_desc)) % 128 == 0
1099  */
1100 #define EM_MIN_RING_DESC 32
1101 #define EM_MAX_RING_DESC 4096
1102
1103 #define EM_MAX_BUF_SIZE     16384
1104 #define EM_RCTL_FLXBUF_STEP 1024
1105
1106 static const struct rte_memzone *
1107 ring_dma_zone_reserve(struct rte_eth_dev *dev, const char *ring_name,
1108                 uint16_t queue_id, uint32_t ring_size, int socket_id)
1109 {
1110         const struct rte_memzone *mz;
1111         char z_name[RTE_MEMZONE_NAMESIZE];
1112
1113         snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
1114                 dev->driver->pci_drv.name, ring_name, dev->data->port_id,
1115                 queue_id);
1116
1117         if ((mz = rte_memzone_lookup(z_name)) != 0)
1118                 return (mz);
1119
1120 #ifdef RTE_LIBRTE_XEN_DOM0
1121         return rte_memzone_reserve_bounded(z_name, ring_size,
1122                         socket_id, 0, RTE_CACHE_LINE_SIZE, RTE_PGSIZE_2M);
1123 #else
1124         return rte_memzone_reserve(z_name, ring_size, socket_id, 0);
1125 #endif
1126 }
1127
1128 static void
1129 em_tx_queue_release_mbufs(struct em_tx_queue *txq)
1130 {
1131         unsigned i;
1132
1133         if (txq->sw_ring != NULL) {
1134                 for (i = 0; i != txq->nb_tx_desc; i++) {
1135                         if (txq->sw_ring[i].mbuf != NULL) {
1136                                 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
1137                                 txq->sw_ring[i].mbuf = NULL;
1138                         }
1139                 }
1140         }
1141 }
1142
1143 static void
1144 em_tx_queue_release(struct em_tx_queue *txq)
1145 {
1146         if (txq != NULL) {
1147                 em_tx_queue_release_mbufs(txq);
1148                 rte_free(txq->sw_ring);
1149                 rte_free(txq);
1150         }
1151 }
1152
1153 void
1154 eth_em_tx_queue_release(void *txq)
1155 {
1156         em_tx_queue_release(txq);
1157 }
1158
1159 /* (Re)set dynamic em_tx_queue fields to defaults */
1160 static void
1161 em_reset_tx_queue(struct em_tx_queue *txq)
1162 {
1163         uint16_t i, nb_desc, prev;
1164         static const struct e1000_data_desc txd_init = {
1165                 .upper.fields = {.status = E1000_TXD_STAT_DD},
1166         };
1167
1168         nb_desc = txq->nb_tx_desc;
1169
1170         /* Initialize ring entries */
1171
1172         prev = (uint16_t) (nb_desc - 1);
1173
1174         for (i = 0; i < nb_desc; i++) {
1175                 txq->tx_ring[i] = txd_init;
1176                 txq->sw_ring[i].mbuf = NULL;
1177                 txq->sw_ring[i].last_id = i;
1178                 txq->sw_ring[prev].next_id = i;
1179                 prev = i;
1180         }
1181
1182         /*
1183          * Always allow 1 descriptor to be un-allocated to avoid
1184          * a H/W race condition
1185          */
1186         txq->nb_tx_free = (uint16_t)(nb_desc - 1);
1187         txq->last_desc_cleaned = (uint16_t)(nb_desc - 1);
1188         txq->nb_tx_used = 0;
1189         txq->tx_tail = 0;
1190
1191         memset((void*)&txq->ctx_cache, 0, sizeof (txq->ctx_cache));
1192 }
1193
1194 int
1195 eth_em_tx_queue_setup(struct rte_eth_dev *dev,
1196                          uint16_t queue_idx,
1197                          uint16_t nb_desc,
1198                          unsigned int socket_id,
1199                          const struct rte_eth_txconf *tx_conf)
1200 {
1201         const struct rte_memzone *tz;
1202         struct em_tx_queue *txq;
1203         struct e1000_hw     *hw;
1204         uint32_t tsize;
1205         uint16_t tx_rs_thresh, tx_free_thresh;
1206
1207         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1208
1209         /*
1210          * Validate number of transmit descriptors.
1211          * It must not exceed hardware maximum, and must be multiple
1212          * of EM_ALIGN.
1213          */
1214         if (((nb_desc * sizeof(*txq->tx_ring)) % EM_ALIGN) != 0 ||
1215                         (nb_desc > EM_MAX_RING_DESC) ||
1216                         (nb_desc < EM_MIN_RING_DESC)) {
1217                 return -(EINVAL);
1218         }
1219
1220         tx_free_thresh = tx_conf->tx_free_thresh;
1221         if (tx_free_thresh == 0)
1222                 tx_free_thresh = (uint16_t)RTE_MIN(nb_desc / 4,
1223                                         DEFAULT_TX_FREE_THRESH);
1224
1225         tx_rs_thresh = tx_conf->tx_rs_thresh;
1226         if (tx_rs_thresh == 0)
1227                 tx_rs_thresh = (uint16_t)RTE_MIN(tx_free_thresh,
1228                                         DEFAULT_TX_RS_THRESH);
1229
1230         if (tx_free_thresh >= (nb_desc - 3)) {
1231                 PMD_INIT_LOG(ERR, "tx_free_thresh must be less than the "
1232                              "number of TX descriptors minus 3. "
1233                              "(tx_free_thresh=%u port=%d queue=%d)",
1234                              (unsigned int)tx_free_thresh,
1235                              (int)dev->data->port_id, (int)queue_idx);
1236                 return -(EINVAL);
1237         }
1238         if (tx_rs_thresh > tx_free_thresh) {
1239                 PMD_INIT_LOG(ERR, "tx_rs_thresh must be less than or equal to "
1240                              "tx_free_thresh. (tx_free_thresh=%u "
1241                              "tx_rs_thresh=%u port=%d queue=%d)",
1242                              (unsigned int)tx_free_thresh,
1243                              (unsigned int)tx_rs_thresh,
1244                              (int)dev->data->port_id,
1245                              (int)queue_idx);
1246                 return -(EINVAL);
1247         }
1248
1249         /*
1250          * If rs_bit_thresh is greater than 1, then TX WTHRESH should be
1251          * set to 0. If WTHRESH is greater than zero, the RS bit is ignored
1252          * by the NIC and all descriptors are written back after the NIC
1253          * accumulates WTHRESH descriptors.
1254          */
1255         if (tx_conf->tx_thresh.wthresh != 0 && tx_rs_thresh != 1) {
1256                 PMD_INIT_LOG(ERR, "TX WTHRESH must be set to 0 if "
1257                              "tx_rs_thresh is greater than 1. (tx_rs_thresh=%u "
1258                              "port=%d queue=%d)", (unsigned int)tx_rs_thresh,
1259                              (int)dev->data->port_id, (int)queue_idx);
1260                 return -(EINVAL);
1261         }
1262
1263         /* Free memory prior to re-allocation if needed... */
1264         if (dev->data->tx_queues[queue_idx] != NULL) {
1265                 em_tx_queue_release(dev->data->tx_queues[queue_idx]);
1266                 dev->data->tx_queues[queue_idx] = NULL;
1267         }
1268
1269         /*
1270          * Allocate TX ring hardware descriptors. A memzone large enough to
1271          * handle the maximum ring size is allocated in order to allow for
1272          * resizing in later calls to the queue setup function.
1273          */
1274         tsize = sizeof (txq->tx_ring[0]) * EM_MAX_RING_DESC;
1275         if ((tz = ring_dma_zone_reserve(dev, "tx_ring", queue_idx, tsize,
1276                         socket_id)) == NULL)
1277                 return (-ENOMEM);
1278
1279         /* Allocate the tx queue data structure. */
1280         if ((txq = rte_zmalloc("ethdev TX queue", sizeof(*txq),
1281                         RTE_CACHE_LINE_SIZE)) == NULL)
1282                 return (-ENOMEM);
1283
1284         /* Allocate software ring */
1285         if ((txq->sw_ring = rte_zmalloc("txq->sw_ring",
1286                         sizeof(txq->sw_ring[0]) * nb_desc,
1287                         RTE_CACHE_LINE_SIZE)) == NULL) {
1288                 em_tx_queue_release(txq);
1289                 return (-ENOMEM);
1290         }
1291
1292         txq->nb_tx_desc = nb_desc;
1293         txq->tx_free_thresh = tx_free_thresh;
1294         txq->tx_rs_thresh = tx_rs_thresh;
1295         txq->pthresh = tx_conf->tx_thresh.pthresh;
1296         txq->hthresh = tx_conf->tx_thresh.hthresh;
1297         txq->wthresh = tx_conf->tx_thresh.wthresh;
1298         txq->queue_id = queue_idx;
1299         txq->port_id = dev->data->port_id;
1300
1301         txq->tdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_TDT(queue_idx));
1302 #ifndef RTE_LIBRTE_XEN_DOM0
1303         txq->tx_ring_phys_addr = (uint64_t) tz->phys_addr;
1304 #else
1305         txq->tx_ring_phys_addr = rte_mem_phy2mch(tz->memseg_id, tz->phys_addr);
1306 #endif
1307         txq->tx_ring = (struct e1000_data_desc *) tz->addr;
1308
1309         PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
1310                      txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr);
1311
1312         em_reset_tx_queue(txq);
1313
1314         dev->data->tx_queues[queue_idx] = txq;
1315         return (0);
1316 }
1317
1318 static void
1319 em_rx_queue_release_mbufs(struct em_rx_queue *rxq)
1320 {
1321         unsigned i;
1322
1323         if (rxq->sw_ring != NULL) {
1324                 for (i = 0; i != rxq->nb_rx_desc; i++) {
1325                         if (rxq->sw_ring[i].mbuf != NULL) {
1326                                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
1327                                 rxq->sw_ring[i].mbuf = NULL;
1328                         }
1329                 }
1330         }
1331 }
1332
1333 static void
1334 em_rx_queue_release(struct em_rx_queue *rxq)
1335 {
1336         if (rxq != NULL) {
1337                 em_rx_queue_release_mbufs(rxq);
1338                 rte_free(rxq->sw_ring);
1339                 rte_free(rxq);
1340         }
1341 }
1342
1343 void
1344 eth_em_rx_queue_release(void *rxq)
1345 {
1346         em_rx_queue_release(rxq);
1347 }
1348
1349 /* Reset dynamic em_rx_queue fields back to defaults */
1350 static void
1351 em_reset_rx_queue(struct em_rx_queue *rxq)
1352 {
1353         rxq->rx_tail = 0;
1354         rxq->nb_rx_hold = 0;
1355         rxq->pkt_first_seg = NULL;
1356         rxq->pkt_last_seg = NULL;
1357 }
1358
1359 int
1360 eth_em_rx_queue_setup(struct rte_eth_dev *dev,
1361                 uint16_t queue_idx,
1362                 uint16_t nb_desc,
1363                 unsigned int socket_id,
1364                 const struct rte_eth_rxconf *rx_conf,
1365                 struct rte_mempool *mp)
1366 {
1367         const struct rte_memzone *rz;
1368         struct em_rx_queue *rxq;
1369         struct e1000_hw     *hw;
1370         uint32_t rsize;
1371
1372         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1373
1374         /*
1375          * Validate number of receive descriptors.
1376          * It must not exceed hardware maximum, and must be multiple
1377          * of EM_ALIGN.
1378          */
1379         if (((nb_desc * sizeof(rxq->rx_ring[0])) % EM_ALIGN) != 0 ||
1380                         (nb_desc > EM_MAX_RING_DESC) ||
1381                         (nb_desc < EM_MIN_RING_DESC)) {
1382                 return (-EINVAL);
1383         }
1384
1385         /*
1386          * EM devices don't support drop_en functionality
1387          */
1388         if (rx_conf->rx_drop_en) {
1389                 PMD_INIT_LOG(ERR, "drop_en functionality not supported by "
1390                              "device");
1391                 return (-EINVAL);
1392         }
1393
1394         /* Free memory prior to re-allocation if needed. */
1395         if (dev->data->rx_queues[queue_idx] != NULL) {
1396                 em_rx_queue_release(dev->data->rx_queues[queue_idx]);
1397                 dev->data->rx_queues[queue_idx] = NULL;
1398         }
1399
1400         /* Allocate RX ring for max possible mumber of hardware descriptors. */
1401         rsize = sizeof (rxq->rx_ring[0]) * EM_MAX_RING_DESC;
1402         if ((rz = ring_dma_zone_reserve(dev, "rx_ring", queue_idx, rsize,
1403                         socket_id)) == NULL)
1404                 return (-ENOMEM);
1405
1406         /* Allocate the RX queue data structure. */
1407         if ((rxq = rte_zmalloc("ethdev RX queue", sizeof(*rxq),
1408                         RTE_CACHE_LINE_SIZE)) == NULL)
1409                 return (-ENOMEM);
1410
1411         /* Allocate software ring. */
1412         if ((rxq->sw_ring = rte_zmalloc("rxq->sw_ring",
1413                         sizeof (rxq->sw_ring[0]) * nb_desc,
1414                         RTE_CACHE_LINE_SIZE)) == NULL) {
1415                 em_rx_queue_release(rxq);
1416                 return (-ENOMEM);
1417         }
1418
1419         rxq->mb_pool = mp;
1420         rxq->nb_rx_desc = nb_desc;
1421         rxq->pthresh = rx_conf->rx_thresh.pthresh;
1422         rxq->hthresh = rx_conf->rx_thresh.hthresh;
1423         rxq->wthresh = rx_conf->rx_thresh.wthresh;
1424         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
1425         rxq->queue_id = queue_idx;
1426         rxq->port_id = dev->data->port_id;
1427         rxq->crc_len = (uint8_t) ((dev->data->dev_conf.rxmode.hw_strip_crc) ?
1428                                 0 : ETHER_CRC_LEN);
1429
1430         rxq->rdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDT(queue_idx));
1431         rxq->rdh_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDH(queue_idx));
1432 #ifndef RTE_LIBRTE_XEN_DOM0
1433         rxq->rx_ring_phys_addr = (uint64_t) rz->phys_addr;
1434 #else
1435         rxq->rx_ring_phys_addr = rte_mem_phy2mch(rz->memseg_id, rz->phys_addr);
1436 #endif
1437         rxq->rx_ring = (struct e1000_rx_desc *) rz->addr;
1438
1439         PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
1440                      rxq->sw_ring, rxq->rx_ring, rxq->rx_ring_phys_addr);
1441
1442         dev->data->rx_queues[queue_idx] = rxq;
1443         em_reset_rx_queue(rxq);
1444
1445         return (0);
1446 }
1447
1448 uint32_t
1449 eth_em_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1450 {
1451 #define EM_RXQ_SCAN_INTERVAL 4
1452         volatile struct e1000_rx_desc *rxdp;
1453         struct em_rx_queue *rxq;
1454         uint32_t desc = 0;
1455
1456         if (rx_queue_id >= dev->data->nb_rx_queues) {
1457                 PMD_RX_LOG(DEBUG, "Invalid RX queue_id=%d", rx_queue_id);
1458                 return 0;
1459         }
1460
1461         rxq = dev->data->rx_queues[rx_queue_id];
1462         rxdp = &(rxq->rx_ring[rxq->rx_tail]);
1463
1464         while ((desc < rxq->nb_rx_desc) &&
1465                 (rxdp->status & E1000_RXD_STAT_DD)) {
1466                 desc += EM_RXQ_SCAN_INTERVAL;
1467                 rxdp += EM_RXQ_SCAN_INTERVAL;
1468                 if (rxq->rx_tail + desc >= rxq->nb_rx_desc)
1469                         rxdp = &(rxq->rx_ring[rxq->rx_tail +
1470                                 desc - rxq->nb_rx_desc]);
1471         }
1472
1473         return desc;
1474 }
1475
1476 int
1477 eth_em_rx_descriptor_done(void *rx_queue, uint16_t offset)
1478 {
1479         volatile struct e1000_rx_desc *rxdp;
1480         struct em_rx_queue *rxq = rx_queue;
1481         uint32_t desc;
1482
1483         if (unlikely(offset >= rxq->nb_rx_desc))
1484                 return 0;
1485         desc = rxq->rx_tail + offset;
1486         if (desc >= rxq->nb_rx_desc)
1487                 desc -= rxq->nb_rx_desc;
1488
1489         rxdp = &rxq->rx_ring[desc];
1490         return !!(rxdp->status & E1000_RXD_STAT_DD);
1491 }
1492
1493 void
1494 em_dev_clear_queues(struct rte_eth_dev *dev)
1495 {
1496         uint16_t i;
1497         struct em_tx_queue *txq;
1498         struct em_rx_queue *rxq;
1499
1500         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1501                 txq = dev->data->tx_queues[i];
1502                 if (txq != NULL) {
1503                         em_tx_queue_release_mbufs(txq);
1504                         em_reset_tx_queue(txq);
1505                 }
1506         }
1507
1508         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1509                 rxq = dev->data->rx_queues[i];
1510                 if (rxq != NULL) {
1511                         em_rx_queue_release_mbufs(rxq);
1512                         em_reset_rx_queue(rxq);
1513                 }
1514         }
1515 }
1516
1517 /*
1518  * Takes as input/output parameter RX buffer size.
1519  * Returns (BSIZE | BSEX | FLXBUF) fields of RCTL register.
1520  */
1521 static uint32_t
1522 em_rctl_bsize(__rte_unused enum e1000_mac_type hwtyp, uint32_t *bufsz)
1523 {
1524         /*
1525          * For BSIZE & BSEX all configurable sizes are:
1526          * 16384: rctl |= (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX);
1527          *  8192: rctl |= (E1000_RCTL_SZ_8192  | E1000_RCTL_BSEX);
1528          *  4096: rctl |= (E1000_RCTL_SZ_4096  | E1000_RCTL_BSEX);
1529          *  2048: rctl |= E1000_RCTL_SZ_2048;
1530          *  1024: rctl |= E1000_RCTL_SZ_1024;
1531          *   512: rctl |= E1000_RCTL_SZ_512;
1532          *   256: rctl |= E1000_RCTL_SZ_256;
1533          */
1534         static const struct {
1535                 uint32_t bufsz;
1536                 uint32_t rctl;
1537         } bufsz_to_rctl[] = {
1538                 {16384, (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX)},
1539                 {8192,  (E1000_RCTL_SZ_8192  | E1000_RCTL_BSEX)},
1540                 {4096,  (E1000_RCTL_SZ_4096  | E1000_RCTL_BSEX)},
1541                 {2048,  E1000_RCTL_SZ_2048},
1542                 {1024,  E1000_RCTL_SZ_1024},
1543                 {512,   E1000_RCTL_SZ_512},
1544                 {256,   E1000_RCTL_SZ_256},
1545         };
1546
1547         int i;
1548         uint32_t rctl_bsize;
1549
1550         rctl_bsize = *bufsz;
1551
1552         /*
1553          * Starting from 82571 it is possible to specify RX buffer size
1554          * by RCTL.FLXBUF. When this field is different from zero, the
1555          * RX buffer size = RCTL.FLXBUF * 1K
1556          * (e.g. t is possible to specify RX buffer size  1,2,...,15KB).
1557          * It is working ok on real HW, but by some reason doesn't work
1558          * on VMware emulated 82574L.
1559          * So for now, always use BSIZE/BSEX to setup RX buffer size.
1560          * If you don't plan to use it on VMware emulated 82574L and
1561          * would like to specify RX buffer size in 1K granularity,
1562          * uncomment the following lines:
1563          * ***************************************************************
1564          * if (hwtyp >= e1000_82571 && hwtyp <= e1000_82574 &&
1565          *              rctl_bsize >= EM_RCTL_FLXBUF_STEP) {
1566          *      rctl_bsize /= EM_RCTL_FLXBUF_STEP;
1567          *      *bufsz = rctl_bsize;
1568          *      return (rctl_bsize << E1000_RCTL_FLXBUF_SHIFT &
1569          *              E1000_RCTL_FLXBUF_MASK);
1570          * }
1571          * ***************************************************************
1572          */
1573
1574         for (i = 0; i != sizeof(bufsz_to_rctl) / sizeof(bufsz_to_rctl[0]);
1575                         i++) {
1576                 if (rctl_bsize >= bufsz_to_rctl[i].bufsz) {
1577                         *bufsz = bufsz_to_rctl[i].bufsz;
1578                         return (bufsz_to_rctl[i].rctl);
1579                 }
1580         }
1581
1582         /* Should never happen. */
1583         return (-EINVAL);
1584 }
1585
1586 static int
1587 em_alloc_rx_queue_mbufs(struct em_rx_queue *rxq)
1588 {
1589         struct em_rx_entry *rxe = rxq->sw_ring;
1590         uint64_t dma_addr;
1591         unsigned i;
1592         static const struct e1000_rx_desc rxd_init = {
1593                 .buffer_addr = 0,
1594         };
1595
1596         /* Initialize software ring entries */
1597         for (i = 0; i < rxq->nb_rx_desc; i++) {
1598                 volatile struct e1000_rx_desc *rxd;
1599                 struct rte_mbuf *mbuf = rte_rxmbuf_alloc(rxq->mb_pool);
1600
1601                 if (mbuf == NULL) {
1602                         PMD_INIT_LOG(ERR, "RX mbuf alloc failed "
1603                                      "queue_id=%hu", rxq->queue_id);
1604                         return (-ENOMEM);
1605                 }
1606
1607                 dma_addr = rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR_DEFAULT(mbuf));
1608
1609                 /* Clear HW ring memory */
1610                 rxq->rx_ring[i] = rxd_init;
1611
1612                 rxd = &rxq->rx_ring[i];
1613                 rxd->buffer_addr = dma_addr;
1614                 rxe[i].mbuf = mbuf;
1615         }
1616
1617         return 0;
1618 }
1619
1620 /*********************************************************************
1621  *
1622  *  Enable receive unit.
1623  *
1624  **********************************************************************/
1625 int
1626 eth_em_rx_init(struct rte_eth_dev *dev)
1627 {
1628         struct e1000_hw *hw;
1629         struct em_rx_queue *rxq;
1630         uint32_t rctl;
1631         uint32_t rfctl;
1632         uint32_t rxcsum;
1633         uint32_t rctl_bsize;
1634         uint16_t i;
1635         int ret;
1636
1637         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1638
1639         /*
1640          * Make sure receives are disabled while setting
1641          * up the descriptor ring.
1642          */
1643         rctl = E1000_READ_REG(hw, E1000_RCTL);
1644         E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
1645
1646         rfctl = E1000_READ_REG(hw, E1000_RFCTL);
1647
1648         /* Disable extended descriptor type. */
1649         rfctl &= ~E1000_RFCTL_EXTEN;
1650         /* Disable accelerated acknowledge */
1651         if (hw->mac.type == e1000_82574)
1652                 rfctl |= E1000_RFCTL_ACK_DIS;
1653
1654         E1000_WRITE_REG(hw, E1000_RFCTL, rfctl);
1655
1656         /*
1657          * XXX TEMPORARY WORKAROUND: on some systems with 82573
1658          * long latencies are observed, like Lenovo X60. This
1659          * change eliminates the problem, but since having positive
1660          * values in RDTR is a known source of problems on other
1661          * platforms another solution is being sought.
1662          */
1663         if (hw->mac.type == e1000_82573)
1664                 E1000_WRITE_REG(hw, E1000_RDTR, 0x20);
1665
1666         dev->rx_pkt_burst = (eth_rx_burst_t)eth_em_recv_pkts;
1667
1668         /* Determine RX bufsize. */
1669         rctl_bsize = EM_MAX_BUF_SIZE;
1670         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1671                 struct rte_pktmbuf_pool_private *mbp_priv;
1672                 uint32_t buf_size;
1673
1674                 rxq = dev->data->rx_queues[i];
1675                 mbp_priv = rte_mempool_get_priv(rxq->mb_pool);
1676                 buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
1677                 rctl_bsize = RTE_MIN(rctl_bsize, buf_size);
1678         }
1679
1680         rctl |= em_rctl_bsize(hw->mac.type, &rctl_bsize);
1681
1682         /* Configure and enable each RX queue. */
1683         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1684                 uint64_t bus_addr;
1685                 uint32_t rxdctl;
1686
1687                 rxq = dev->data->rx_queues[i];
1688
1689                 /* Allocate buffers for descriptor rings and setup queue */
1690                 ret = em_alloc_rx_queue_mbufs(rxq);
1691                 if (ret)
1692                         return ret;
1693
1694                 /*
1695                  * Reset crc_len in case it was changed after queue setup by a
1696                  *  call to configure
1697                  */
1698                 rxq->crc_len =
1699                         (uint8_t)(dev->data->dev_conf.rxmode.hw_strip_crc ?
1700                                                         0 : ETHER_CRC_LEN);
1701
1702                 bus_addr = rxq->rx_ring_phys_addr;
1703                 E1000_WRITE_REG(hw, E1000_RDLEN(i),
1704                                 rxq->nb_rx_desc *
1705                                 sizeof(*rxq->rx_ring));
1706                 E1000_WRITE_REG(hw, E1000_RDBAH(i),
1707                                 (uint32_t)(bus_addr >> 32));
1708                 E1000_WRITE_REG(hw, E1000_RDBAL(i), (uint32_t)bus_addr);
1709
1710                 E1000_WRITE_REG(hw, E1000_RDH(i), 0);
1711                 E1000_WRITE_REG(hw, E1000_RDT(i), rxq->nb_rx_desc - 1);
1712
1713                 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1714                 rxdctl &= 0xFE000000;
1715                 rxdctl |= rxq->pthresh & 0x3F;
1716                 rxdctl |= (rxq->hthresh & 0x3F) << 8;
1717                 rxdctl |= (rxq->wthresh & 0x3F) << 16;
1718                 rxdctl |= E1000_RXDCTL_GRAN;
1719                 E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
1720
1721                 /*
1722                  * Due to EM devices not having any sort of hardware
1723                  * limit for packet length, jumbo frame of any size
1724                  * can be accepted, thus we have to enable scattered
1725                  * rx if jumbo frames are enabled (or if buffer size
1726                  * is too small to accommodate non-jumbo packets)
1727                  * to avoid splitting packets that don't fit into
1728                  * one buffer.
1729                  */
1730                 if (dev->data->dev_conf.rxmode.jumbo_frame ||
1731                                 rctl_bsize < ETHER_MAX_LEN) {
1732                         if (!dev->data->scattered_rx)
1733                                 PMD_INIT_LOG(DEBUG, "forcing scatter mode");
1734                         dev->rx_pkt_burst =
1735                                 (eth_rx_burst_t)eth_em_recv_scattered_pkts;
1736                         dev->data->scattered_rx = 1;
1737                 }
1738         }
1739
1740         if (dev->data->dev_conf.rxmode.enable_scatter) {
1741                 if (!dev->data->scattered_rx)
1742                         PMD_INIT_LOG(DEBUG, "forcing scatter mode");
1743                 dev->rx_pkt_burst = eth_em_recv_scattered_pkts;
1744                 dev->data->scattered_rx = 1;
1745         }
1746
1747         /*
1748          * Setup the Checksum Register.
1749          * Receive Full-Packet Checksum Offload is mutually exclusive with RSS.
1750          */
1751         rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
1752
1753         if (dev->data->dev_conf.rxmode.hw_ip_checksum)
1754                 rxcsum |= E1000_RXCSUM_IPOFL;
1755         else
1756                 rxcsum &= ~E1000_RXCSUM_IPOFL;
1757         E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
1758
1759         /* No MRQ or RSS support for now */
1760
1761         /* Set early receive threshold on appropriate hw */
1762         if ((hw->mac.type == e1000_ich9lan ||
1763                         hw->mac.type == e1000_pch2lan ||
1764                         hw->mac.type == e1000_ich10lan) &&
1765                         dev->data->dev_conf.rxmode.jumbo_frame == 1) {
1766                 u32 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1767                 E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl | 3);
1768                 E1000_WRITE_REG(hw, E1000_ERT, 0x100 | (1 << 13));
1769         }
1770
1771         if (hw->mac.type == e1000_pch2lan) {
1772                 if (dev->data->dev_conf.rxmode.jumbo_frame == 1)
1773                         e1000_lv_jumbo_workaround_ich8lan(hw, TRUE);
1774                 else
1775                         e1000_lv_jumbo_workaround_ich8lan(hw, FALSE);
1776         }
1777
1778         /* Setup the Receive Control Register. */
1779         if (dev->data->dev_conf.rxmode.hw_strip_crc)
1780                 rctl |= E1000_RCTL_SECRC; /* Strip Ethernet CRC. */
1781         else
1782                 rctl &= ~E1000_RCTL_SECRC; /* Do not Strip Ethernet CRC. */
1783
1784         rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1785         rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
1786                 E1000_RCTL_RDMTS_HALF |
1787                 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1788
1789         /* Make sure VLAN Filters are off. */
1790         rctl &= ~E1000_RCTL_VFE;
1791         /* Don't store bad packets. */
1792         rctl &= ~E1000_RCTL_SBP;
1793         /* Legacy descriptor type. */
1794         rctl &= ~E1000_RCTL_DTYP_MASK;
1795
1796         /*
1797          * Configure support of jumbo frames, if any.
1798          */
1799         if (dev->data->dev_conf.rxmode.jumbo_frame == 1)
1800                 rctl |= E1000_RCTL_LPE;
1801         else
1802                 rctl &= ~E1000_RCTL_LPE;
1803
1804         /* Enable Receives. */
1805         E1000_WRITE_REG(hw, E1000_RCTL, rctl);
1806
1807         return 0;
1808 }
1809
1810 /*********************************************************************
1811  *
1812  *  Enable transmit unit.
1813  *
1814  **********************************************************************/
1815 void
1816 eth_em_tx_init(struct rte_eth_dev *dev)
1817 {
1818         struct e1000_hw     *hw;
1819         struct em_tx_queue *txq;
1820         uint32_t tctl;
1821         uint32_t txdctl;
1822         uint16_t i;
1823
1824         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1825
1826         /* Setup the Base and Length of the Tx Descriptor Rings. */
1827         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1828                 uint64_t bus_addr;
1829
1830                 txq = dev->data->tx_queues[i];
1831                 bus_addr = txq->tx_ring_phys_addr;
1832                 E1000_WRITE_REG(hw, E1000_TDLEN(i),
1833                                 txq->nb_tx_desc *
1834                                 sizeof(*txq->tx_ring));
1835                 E1000_WRITE_REG(hw, E1000_TDBAH(i),
1836                                 (uint32_t)(bus_addr >> 32));
1837                 E1000_WRITE_REG(hw, E1000_TDBAL(i), (uint32_t)bus_addr);
1838
1839                 /* Setup the HW Tx Head and Tail descriptor pointers. */
1840                 E1000_WRITE_REG(hw, E1000_TDT(i), 0);
1841                 E1000_WRITE_REG(hw, E1000_TDH(i), 0);
1842
1843                 /* Setup Transmit threshold registers. */
1844                 txdctl = E1000_READ_REG(hw, E1000_TXDCTL(i));
1845                 /*
1846                  * bit 22 is reserved, on some models should always be 0,
1847                  * on others  - always 1.
1848                  */
1849                 txdctl &= E1000_TXDCTL_COUNT_DESC;
1850                 txdctl |= txq->pthresh & 0x3F;
1851                 txdctl |= (txq->hthresh & 0x3F) << 8;
1852                 txdctl |= (txq->wthresh & 0x3F) << 16;
1853                 txdctl |= E1000_TXDCTL_GRAN;
1854                 E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
1855         }
1856
1857         /* Program the Transmit Control Register. */
1858         tctl = E1000_READ_REG(hw, E1000_TCTL);
1859         tctl &= ~E1000_TCTL_CT;
1860         tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
1861                  (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
1862
1863         /* This write will effectively turn on the transmit unit. */
1864         E1000_WRITE_REG(hw, E1000_TCTL, tctl);
1865 }
1866