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