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