update Intel copyright years to 2014
[dpdk.git] / lib / librte_pmd_e1000 / em_rxtx.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/queue.h>
35
36 #include <endian.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <stdint.h>
42 #include <stdarg.h>
43 #include <inttypes.h>
44
45 #include <rte_interrupts.h>
46 #include <rte_byteorder.h>
47 #include <rte_common.h>
48 #include <rte_log.h>
49 #include <rte_debug.h>
50 #include <rte_pci.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
54 #include <rte_launch.h>
55 #include <rte_tailq.h>
56 #include <rte_eal.h>
57 #include <rte_per_lcore.h>
58 #include <rte_lcore.h>
59 #include <rte_atomic.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_ring.h>
62 #include <rte_mempool.h>
63 #include <rte_malloc.h>
64 #include <rte_mbuf.h>
65 #include <rte_ether.h>
66 #include <rte_ethdev.h>
67 #include <rte_prefetch.h>
68 #include <rte_ip.h>
69 #include <rte_udp.h>
70 #include <rte_tcp.h>
71 #include <rte_sctp.h>
72 #include <rte_string_fns.h>
73
74 #include "e1000_logs.h"
75 #include "e1000/e1000_api.h"
76 #include "e1000_ethdev.h"
77 #include "e1000/e1000_osdep.h"
78
79 #define E1000_TXD_VLAN_SHIFT    16
80
81 #define E1000_RXDCTL_GRAN       0x01000000 /* RXDCTL Granularity */
82
83 static inline struct rte_mbuf *
84 rte_rxmbuf_alloc(struct rte_mempool *mp)
85 {
86         struct rte_mbuf *m;
87
88         m = __rte_mbuf_raw_alloc(mp);
89         __rte_mbuf_sanity_check_raw(m, RTE_MBUF_PKT, 0);
90         return (m);
91 }
92
93 #define RTE_MBUF_DATA_DMA_ADDR(mb)             \
94         (uint64_t) ((mb)->buf_physaddr +       \
95         (uint64_t) ((char *)((mb)->pkt.data) - (char *)(mb)->buf_addr))
96
97 #define RTE_MBUF_DATA_DMA_ADDR_DEFAULT(mb) \
98         (uint64_t) ((mb)->buf_physaddr + RTE_PKTMBUF_HEADROOM)
99
100 /**
101  * Structure associated with each descriptor of the RX ring of a RX queue.
102  */
103 struct em_rx_entry {
104         struct rte_mbuf *mbuf; /**< mbuf associated with RX descriptor. */
105 };
106
107 /**
108  * Structure associated with each descriptor of the TX ring of a TX queue.
109  */
110 struct em_tx_entry {
111         struct rte_mbuf *mbuf; /**< mbuf associated with TX desc, if any. */
112         uint16_t next_id; /**< Index of next descriptor in ring. */
113         uint16_t last_id; /**< Index of last scattered descriptor. */
114 };
115
116 /**
117  * Structure associated with each RX queue.
118  */
119 struct em_rx_queue {
120         struct rte_mempool  *mb_pool;   /**< mbuf pool to populate RX ring. */
121         volatile struct e1000_rx_desc *rx_ring; /**< RX ring virtual address. */
122         uint64_t            rx_ring_phys_addr; /**< RX ring DMA address. */
123         volatile uint32_t   *rdt_reg_addr; /**< RDT register address. */
124         volatile uint32_t   *rdh_reg_addr; /**< RDH register address. */
125         struct em_rx_entry *sw_ring;   /**< address of RX software ring. */
126         struct rte_mbuf *pkt_first_seg; /**< First segment of current packet. */
127         struct rte_mbuf *pkt_last_seg;  /**< Last segment of current packet. */
128         uint16_t            nb_rx_desc; /**< number of RX descriptors. */
129         uint16_t            rx_tail;    /**< current value of RDT register. */
130         uint16_t            nb_rx_hold; /**< number of held free RX desc. */
131         uint16_t            rx_free_thresh; /**< max free RX desc to hold. */
132         uint16_t            queue_id;   /**< RX queue index. */
133         uint8_t             port_id;    /**< Device port identifier. */
134         uint8_t             pthresh;    /**< Prefetch threshold register. */
135         uint8_t             hthresh;    /**< Host threshold register. */
136         uint8_t             wthresh;    /**< Write-back threshold register. */
137         uint8_t             crc_len;    /**< 0 if CRC stripped, 4 otherwise. */
138 };
139
140 /**
141  * Hardware context number
142  */
143 enum {
144         EM_CTX_0    = 0, /**< CTX0 */
145         EM_CTX_NUM  = 1, /**< CTX NUM */
146 };
147
148 /**
149  * Structure to check if new context need be built
150  */
151 struct em_ctx_info {
152         uint16_t flags;               /**< ol_flags related to context build. */
153         uint32_t cmp_mask;            /**< compare mask */
154         union rte_vlan_macip hdrlen;  /**< L2 and L3 header lenghts */
155 };
156
157 /**
158  * Structure associated with each TX queue.
159  */
160 struct em_tx_queue {
161         volatile struct e1000_data_desc *tx_ring; /**< TX ring address */
162         uint64_t               tx_ring_phys_addr; /**< TX ring DMA address. */
163         struct em_tx_entry    *sw_ring; /**< virtual address of SW ring. */
164         volatile uint32_t      *tdt_reg_addr; /**< Address of TDT register. */
165         uint16_t               nb_tx_desc;    /**< number of TX descriptors. */
166         uint16_t               tx_tail;  /**< Current value of TDT register. */
167         uint16_t               tx_free_thresh;/**< minimum TX before freeing. */
168         /**< Number of TX descriptors to use before RS bit is set. */
169         uint16_t               tx_rs_thresh;
170         /** Number of TX descriptors used since RS bit was set. */
171         uint16_t               nb_tx_used;
172         /** Index to last TX descriptor to have been cleaned. */
173         uint16_t               last_desc_cleaned;
174         /** Total number of TX descriptors ready to be allocated. */
175         uint16_t               nb_tx_free;
176         uint16_t               queue_id; /**< TX queue index. */
177         uint8_t                port_id;  /**< Device port identifier. */
178         uint8_t                pthresh;  /**< Prefetch threshold register. */
179         uint8_t                hthresh;  /**< Host threshold register. */
180         uint8_t                wthresh;  /**< Write-back threshold register. */
181         struct em_ctx_info ctx_cache;
182         /**< Hardware context history.*/
183 };
184
185 #if 1
186 #define RTE_PMD_USE_PREFETCH
187 #endif
188
189 #ifdef RTE_PMD_USE_PREFETCH
190 #define rte_em_prefetch(p)      rte_prefetch0(p)
191 #else
192 #define rte_em_prefetch(p)      do {} while(0)
193 #endif
194
195 #ifdef RTE_PMD_PACKET_PREFETCH
196 #define rte_packet_prefetch(p) rte_prefetch1(p)
197 #else
198 #define rte_packet_prefetch(p)  do {} while(0)
199 #endif
200
201 #ifndef DEFAULT_TX_FREE_THRESH
202 #define DEFAULT_TX_FREE_THRESH  32
203 #endif /* DEFAULT_TX_FREE_THRESH */
204
205 #ifndef DEFAULT_TX_RS_THRESH
206 #define DEFAULT_TX_RS_THRESH  32
207 #endif /* DEFAULT_TX_RS_THRESH */
208
209
210 /*********************************************************************
211  *
212  *  TX function
213  *
214  **********************************************************************/
215
216 /*
217  * Populates TX context descriptor.
218  */
219 static inline void
220 em_set_xmit_ctx(struct em_tx_queue* txq,
221                 volatile struct e1000_context_desc *ctx_txd,
222                 uint16_t flags,
223                 union rte_vlan_macip hdrlen)
224 {
225         uint32_t cmp_mask, cmd_len;
226         uint16_t ipcse, l2len;
227         struct e1000_context_desc ctx;
228
229         cmp_mask = 0;
230         cmd_len = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_C;
231
232         l2len = hdrlen.f.l2_len;
233         ipcse = (uint16_t)(l2len + hdrlen.f.l3_len);
234
235         /* setup IPCS* fields */
236         ctx.lower_setup.ip_fields.ipcss = (uint8_t)l2len;
237         ctx.lower_setup.ip_fields.ipcso = (uint8_t)(l2len +
238                         offsetof(struct ipv4_hdr, hdr_checksum));
239
240         /*
241          * When doing checksum or TCP segmentation with IPv6 headers,
242          * IPCSE field should be set t0 0.
243          */
244         if (flags & PKT_TX_IP_CKSUM) {
245                 ctx.lower_setup.ip_fields.ipcse =
246                         (uint16_t)rte_cpu_to_le_16(ipcse - 1);
247                 cmd_len |= E1000_TXD_CMD_IP;
248                 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
249         } else {
250                 ctx.lower_setup.ip_fields.ipcse = 0;
251         }
252
253         /* setup TUCS* fields */
254         ctx.upper_setup.tcp_fields.tucss = (uint8_t)ipcse;
255         ctx.upper_setup.tcp_fields.tucse = 0;
256
257         switch (flags & PKT_TX_L4_MASK) {
258         case PKT_TX_UDP_CKSUM:
259                 ctx.upper_setup.tcp_fields.tucso = (uint8_t)(ipcse +
260                                 offsetof(struct udp_hdr, dgram_cksum));
261                 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
262                 break;
263         case PKT_TX_TCP_CKSUM:
264                 ctx.upper_setup.tcp_fields.tucso = (uint8_t)(ipcse +
265                                 offsetof(struct tcp_hdr, cksum));
266                 cmd_len |= E1000_TXD_CMD_TCP;
267                 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
268                 break;
269         default:
270                 ctx.upper_setup.tcp_fields.tucso = 0;
271         }
272
273         ctx.cmd_and_length = rte_cpu_to_le_32(cmd_len);
274         ctx.tcp_seg_setup.data = 0;
275
276         *ctx_txd = ctx;
277
278         txq->ctx_cache.flags = flags;
279         txq->ctx_cache.cmp_mask = cmp_mask;
280         txq->ctx_cache.hdrlen = hdrlen;
281 }
282
283 /*
284  * Check which hardware context can be used. Use the existing match
285  * or create a new context descriptor.
286  */
287 static inline uint32_t
288 what_ctx_update(struct em_tx_queue *txq, uint16_t flags,
289                 union rte_vlan_macip hdrlen)
290 {
291         /* If match with the current context */
292         if (likely (txq->ctx_cache.flags == flags &&
293                         ((txq->ctx_cache.hdrlen.data ^ hdrlen.data) &
294                         txq->ctx_cache.cmp_mask) == 0))
295                 return (EM_CTX_0);
296
297         /* Mismatch */
298         return (EM_CTX_NUM);
299 }
300
301 /* Reset transmit descriptors after they have been used */
302 static inline int
303 em_xmit_cleanup(struct em_tx_queue *txq)
304 {
305         struct em_tx_entry *sw_ring = txq->sw_ring;
306         volatile struct e1000_data_desc *txr = txq->tx_ring;
307         uint16_t last_desc_cleaned = txq->last_desc_cleaned;
308         uint16_t nb_tx_desc = txq->nb_tx_desc;
309         uint16_t desc_to_clean_to;
310         uint16_t nb_tx_to_clean;
311
312         /* Determine the last descriptor needing to be cleaned */
313         desc_to_clean_to = (uint16_t)(last_desc_cleaned + txq->tx_rs_thresh);
314         if (desc_to_clean_to >= nb_tx_desc)
315                 desc_to_clean_to = (uint16_t)(desc_to_clean_to - nb_tx_desc);
316
317         /* Check to make sure the last descriptor to clean is done */
318         desc_to_clean_to = sw_ring[desc_to_clean_to].last_id;
319         if (! (txr[desc_to_clean_to].upper.fields.status & E1000_TXD_STAT_DD))
320         {
321                 PMD_TX_FREE_LOG(DEBUG,
322                                 "TX descriptor %4u is not done"
323                                 "(port=%d queue=%d)",
324                                 desc_to_clean_to,
325                                 txq->port_id, txq->queue_id);
326                 /* Failed to clean any descriptors, better luck next time */
327                 return -(1);
328         }
329
330         /* Figure out how many descriptors will be cleaned */
331         if (last_desc_cleaned > desc_to_clean_to)
332                 nb_tx_to_clean = (uint16_t)((nb_tx_desc - last_desc_cleaned) +
333                                                         desc_to_clean_to);
334         else
335                 nb_tx_to_clean = (uint16_t)(desc_to_clean_to -
336                                                 last_desc_cleaned);
337
338         PMD_TX_FREE_LOG(DEBUG,
339                         "Cleaning %4u TX descriptors: %4u to %4u "
340                         "(port=%d queue=%d)",
341                         nb_tx_to_clean, last_desc_cleaned, desc_to_clean_to,
342                         txq->port_id, txq->queue_id);
343
344         /*
345          * The last descriptor to clean is done, so that means all the
346          * descriptors from the last descriptor that was cleaned
347          * up to the last descriptor with the RS bit set
348          * are done. Only reset the threshold descriptor.
349          */
350         txr[desc_to_clean_to].upper.fields.status = 0;
351
352         /* Update the txq to reflect the last descriptor that was cleaned */
353         txq->last_desc_cleaned = desc_to_clean_to;
354         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + nb_tx_to_clean);
355
356         /* No Error */
357         return (0);
358 }
359
360 static inline uint32_t
361 tx_desc_cksum_flags_to_upper(uint16_t ol_flags)
362 {
363         static const uint32_t l4_olinfo[2] = {0, E1000_TXD_POPTS_TXSM << 8};
364         static const uint32_t l3_olinfo[2] = {0, E1000_TXD_POPTS_IXSM << 8};
365         uint32_t tmp;
366
367         tmp = l4_olinfo[(ol_flags & PKT_TX_L4_MASK) != PKT_TX_L4_NO_CKSUM];
368         tmp |= l3_olinfo[(ol_flags & PKT_TX_IP_CKSUM) != 0];
369         return (tmp);
370 }
371
372 uint16_t
373 eth_em_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
374                 uint16_t nb_pkts)
375 {
376         struct em_tx_queue *txq;
377         struct em_tx_entry *sw_ring;
378         struct em_tx_entry *txe, *txn;
379         volatile struct e1000_data_desc *txr;
380         volatile struct e1000_data_desc *txd;
381         struct rte_mbuf     *tx_pkt;
382         struct rte_mbuf     *m_seg;
383         uint64_t buf_dma_addr;
384         uint32_t popts_spec;
385         uint32_t cmd_type_len;
386         uint16_t slen;
387         uint16_t ol_flags;
388         uint16_t tx_id;
389         uint16_t tx_last;
390         uint16_t nb_tx;
391         uint16_t nb_used;
392         uint16_t tx_ol_req;
393         uint32_t ctx;
394         uint32_t new_ctx;
395         union rte_vlan_macip hdrlen;
396
397         txq = tx_queue;
398         sw_ring = txq->sw_ring;
399         txr     = txq->tx_ring;
400         tx_id   = txq->tx_tail;
401         txe = &sw_ring[tx_id];
402
403         /* Determine if the descriptor ring needs to be cleaned. */
404         if ((txq->nb_tx_desc - txq->nb_tx_free) > txq->tx_free_thresh) {
405                 em_xmit_cleanup(txq);
406         }
407
408         /* TX loop */
409         for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
410                 new_ctx = 0;
411                 tx_pkt = *tx_pkts++;
412
413                 RTE_MBUF_PREFETCH_TO_FREE(txe->mbuf);
414
415                 /*
416                  * Determine how many (if any) context descriptors
417                  * are needed for offload functionality.
418                  */
419                 ol_flags = tx_pkt->ol_flags;
420
421                 /* If hardware offload required */
422                 tx_ol_req = (uint16_t)(ol_flags & (PKT_TX_IP_CKSUM |
423                                                         PKT_TX_L4_MASK));
424                 if (tx_ol_req) {
425                         hdrlen = tx_pkt->pkt.vlan_macip;
426                         /* If new context to be built or reuse the exist ctx. */
427                         ctx = what_ctx_update(txq, tx_ol_req, hdrlen);
428
429                         /* Only allocate context descriptor if required*/
430                         new_ctx = (ctx == EM_CTX_NUM);
431                 }
432
433                 /*
434                  * Keep track of how many descriptors are used this loop
435                  * This will always be the number of segments + the number of
436                  * Context descriptors required to transmit the packet
437                  */
438                 nb_used = (uint16_t)(tx_pkt->pkt.nb_segs + new_ctx);
439
440                 /*
441                  * The number of descriptors that must be allocated for a
442                  * packet is the number of segments of that packet, plus 1
443                  * Context Descriptor for the hardware offload, if any.
444                  * Determine the last TX descriptor to allocate in the TX ring
445                  * for the packet, starting from the current position (tx_id)
446                  * in the ring.
447                  */
448                 tx_last = (uint16_t) (tx_id + nb_used - 1);
449
450                 /* Circular ring */
451                 if (tx_last >= txq->nb_tx_desc)
452                         tx_last = (uint16_t) (tx_last - txq->nb_tx_desc);
453
454                 PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u pktlen=%u"
455                         " tx_first=%u tx_last=%u\n",
456                         (unsigned) txq->port_id,
457                         (unsigned) txq->queue_id,
458                         (unsigned) tx_pkt->pkt.pkt_len,
459                         (unsigned) tx_id,
460                         (unsigned) tx_last);
461
462                 /*
463                  * Make sure there are enough TX descriptors available to
464                  * transmit the entire packet.
465                  * nb_used better be less than or equal to txq->tx_rs_thresh
466                  */
467                 while (unlikely (nb_used > txq->nb_tx_free)) {
468                         PMD_TX_FREE_LOG(DEBUG,
469                                         "Not enough free TX descriptors "
470                                         "nb_used=%4u nb_free=%4u "
471                                         "(port=%d queue=%d)",
472                                         nb_used, txq->nb_tx_free,
473                                         txq->port_id, txq->queue_id);
474
475                         if (em_xmit_cleanup(txq) != 0) {
476                                 /* Could not clean any descriptors */
477                                 if (nb_tx == 0)
478                                         return (0);
479                                 goto end_of_tx;
480                         }
481                 }
482
483                 /*
484                  * By now there are enough free TX descriptors to transmit
485                  * the packet.
486                  */
487
488                 /*
489                  * Set common flags of all TX Data Descriptors.
490                  *
491                  * The following bits must be set in all Data Descriptors:
492                  *    - E1000_TXD_DTYP_DATA
493                  *    - E1000_TXD_DTYP_DEXT
494                  *
495                  * The following bits must be set in the first Data Descriptor
496                  * and are ignored in the other ones:
497                  *    - E1000_TXD_POPTS_IXSM
498                  *    - E1000_TXD_POPTS_TXSM
499                  *
500                  * The following bits must be set in the last Data Descriptor
501                  * and are ignored in the other ones:
502                  *    - E1000_TXD_CMD_VLE
503                  *    - E1000_TXD_CMD_IFCS
504                  *
505                  * The following bits must only be set in the last Data
506                  * Descriptor:
507                  *   - E1000_TXD_CMD_EOP
508                  *
509                  * The following bits can be set in any Data Descriptor, but
510                  * are only set in the last Data Descriptor:
511                  *   - E1000_TXD_CMD_RS
512                  */
513                 cmd_type_len = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
514                         E1000_TXD_CMD_IFCS;
515                 popts_spec = 0;
516
517                 /* Set VLAN Tag offload fields. */
518                 if (ol_flags & PKT_TX_VLAN_PKT) {
519                         cmd_type_len |= E1000_TXD_CMD_VLE;
520                         popts_spec = tx_pkt->pkt.vlan_macip.f.vlan_tci <<
521                                 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->pkt.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->pkt.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="
594                                         "%4u (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 uint16_t
626 rx_desc_status_to_pkt_flags(uint32_t rx_status)
627 {
628         uint16_t pkt_flags;
629
630         /* Check if VLAN present */
631         pkt_flags = (uint16_t)((rx_status & E1000_RXD_STAT_VP) ?
632                                                 PKT_RX_VLAN_PKT : 0);
633
634         return pkt_flags;
635 }
636
637 static inline uint16_t
638 rx_desc_error_to_pkt_flags(uint32_t rx_error)
639 {
640         uint16_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, "\nport_id=%u queue_id=%u rx_id=%u "
717                         "status=0x%x pkt_len=%u\n",
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_rxmbuf_alloc(rxq->mb_pool);
723                 if (nmb == NULL) {
724                         PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
725                                 "queue_id=%u\n",
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->pkt.data = (char*) rxm->buf_addr + RTE_PKTMBUF_HEADROOM;
776                 rte_packet_prefetch(rxm->pkt.data);
777                 rxm->pkt.nb_segs = 1;
778                 rxm->pkt.next = NULL;
779                 rxm->pkt.pkt_len = pkt_len;
780                 rxm->pkt.data_len = pkt_len;
781                 rxm->pkt.in_port = rxq->port_id;
782
783                 rxm->ol_flags = rx_desc_status_to_pkt_flags(status);
784                 rxm->ol_flags = (uint16_t)(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->pkt.vlan_macip.f.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\n",
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, "\nport_id=%u queue_id=%u rx_id=%u "
897                         "status=0x%x data_len=%u\n",
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_rxmbuf_alloc(rxq->mb_pool);
903                 if (nmb == NULL) {
904                         PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
905                                 "queue_id=%u\n", (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->pkt.data_len = data_len;
945                 rxm->pkt.data = (char*) rxm->buf_addr + 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.pkt_len = data_len;
958                         first_seg->pkt.nb_segs = 1;
959                 } else {
960                         first_seg->pkt.pkt_len += data_len;
961                         first_seg->pkt.nb_segs++;
962                         last_seg->pkt.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->pkt.next = NULL;
986                 if (unlikely(rxq->crc_len > 0)) {
987                         first_seg->pkt.pkt_len -= ETHER_CRC_LEN;
988                         if (data_len <= ETHER_CRC_LEN) {
989                                 rte_pktmbuf_free_seg(rxm);
990                                 first_seg->pkt.nb_segs--;
991                                 last_seg->pkt.data_len = (uint16_t)
992                                         (last_seg->pkt.data_len -
993                                          (ETHER_CRC_LEN - data_len));
994                                 last_seg->pkt.next = NULL;
995                         } else
996                                 rxm->pkt.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->pkt.in_port = rxq->port_id;
1008
1009                 first_seg->ol_flags = rx_desc_status_to_pkt_flags(status);
1010                 first_seg->ol_flags = (uint16_t)(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->pkt.vlan_macip.f.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(first_seg->pkt.data);
1018
1019                 /*
1020                  * Store the mbuf address into the next entry of the array
1021                  * of returned packets.
1022                  */
1023                 rx_pkts[nb_rx++] = first_seg;
1024
1025                 /*
1026                  * Setup receipt context for a new packet.
1027                  */
1028                 first_seg = NULL;
1029         }
1030
1031         /*
1032          * Record index of the next RX descriptor to probe.
1033          */
1034         rxq->rx_tail = rx_id;
1035
1036         /*
1037          * Save receive context.
1038          */
1039         rxq->pkt_first_seg = first_seg;
1040         rxq->pkt_last_seg = last_seg;
1041
1042         /*
1043          * If the number of free RX descriptors is greater than the RX free
1044          * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1045          * register.
1046          * Update the RDT with the value of the last processed RX descriptor
1047          * minus 1, to guarantee that the RDT register is never equal to the
1048          * RDH register, which creates a "full" ring situtation from the
1049          * hardware point of view...
1050          */
1051         nb_hold = (uint16_t) (nb_hold + rxq->nb_rx_hold);
1052         if (nb_hold > rxq->rx_free_thresh) {
1053                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
1054                         "nb_hold=%u nb_rx=%u\n",
1055                         (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
1056                         (unsigned) rx_id, (unsigned) nb_hold,
1057                         (unsigned) nb_rx);
1058                 rx_id = (uint16_t) ((rx_id == 0) ?
1059                         (rxq->nb_rx_desc - 1) : (rx_id - 1));
1060                 E1000_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
1061                 nb_hold = 0;
1062         }
1063         rxq->nb_rx_hold = nb_hold;
1064         return (nb_rx);
1065 }
1066
1067 /*
1068  * Rings setup and release.
1069  *
1070  * TDBA/RDBA should be aligned on 16 byte boundary. But TDLEN/RDLEN should be
1071  * multiple of 128 bytes. So we align TDBA/RDBA on 128 byte boundary.
1072  * This will also optimize cache line size effect.
1073  * H/W supports up to cache line size 128.
1074  */
1075 #define EM_ALIGN 128
1076
1077 /*
1078  * Maximum number of Ring Descriptors.
1079  *
1080  * Since RDLEN/TDLEN should be multiple of 128 bytes, the number of ring
1081  * desscriptors should meet the following condition:
1082  * (num_ring_desc * sizeof(struct e1000_rx/tx_desc)) % 128 == 0
1083  */
1084 #define EM_MIN_RING_DESC 32
1085 #define EM_MAX_RING_DESC 4096
1086
1087 #define EM_MAX_BUF_SIZE     16384
1088 #define EM_RCTL_FLXBUF_STEP 1024
1089
1090 static const struct rte_memzone *
1091 ring_dma_zone_reserve(struct rte_eth_dev *dev, const char *ring_name,
1092                 uint16_t queue_id, uint32_t ring_size, int socket_id)
1093 {
1094         const struct rte_memzone *mz;
1095         char z_name[RTE_MEMZONE_NAMESIZE];
1096
1097         rte_snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
1098                 dev->driver->pci_drv.name, ring_name, dev->data->port_id,
1099                 queue_id);
1100
1101         if ((mz = rte_memzone_lookup(z_name)) != 0)
1102                 return (mz);
1103
1104         return rte_memzone_reserve(z_name, ring_size, socket_id, 0);
1105 }
1106
1107 static void
1108 em_tx_queue_release_mbufs(struct em_tx_queue *txq)
1109 {
1110         unsigned i;
1111
1112         if (txq->sw_ring != NULL) {
1113                 for (i = 0; i != txq->nb_tx_desc; i++) {
1114                         if (txq->sw_ring[i].mbuf != NULL) {
1115                                 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
1116                                 txq->sw_ring[i].mbuf = NULL;
1117                         }
1118                 }
1119         }
1120 }
1121
1122 static void
1123 em_tx_queue_release(struct em_tx_queue *txq)
1124 {
1125         if (txq != NULL) {
1126                 em_tx_queue_release_mbufs(txq);
1127                 rte_free(txq->sw_ring);
1128                 rte_free(txq);
1129         }
1130 }
1131
1132 void
1133 eth_em_tx_queue_release(void *txq)
1134 {
1135         em_tx_queue_release(txq);
1136 }
1137
1138 /* (Re)set dynamic em_tx_queue fields to defaults */
1139 static void
1140 em_reset_tx_queue(struct em_tx_queue *txq)
1141 {
1142         uint16_t i, nb_desc, prev;
1143         static const struct e1000_data_desc txd_init = {
1144                 .upper.fields = {.status = E1000_TXD_STAT_DD},
1145         };
1146
1147         nb_desc = txq->nb_tx_desc;
1148
1149         /* Initialize ring entries */
1150
1151         prev = (uint16_t) (nb_desc - 1);
1152
1153         for (i = 0; i < nb_desc; i++) {
1154                 txq->tx_ring[i] = txd_init;
1155                 txq->sw_ring[i].mbuf = NULL;
1156                 txq->sw_ring[i].last_id = i;
1157                 txq->sw_ring[prev].next_id = i;
1158                 prev = i;
1159         }
1160
1161         /*
1162          * Always allow 1 descriptor to be un-allocated to avoid
1163          * a H/W race condition
1164          */
1165         txq->nb_tx_free = (uint16_t)(nb_desc - 1);
1166         txq->last_desc_cleaned = (uint16_t)(nb_desc - 1);
1167         txq->nb_tx_used = 0;
1168         txq->tx_tail = 0;
1169
1170         memset((void*)&txq->ctx_cache, 0, sizeof (txq->ctx_cache));
1171 }
1172
1173 int
1174 eth_em_tx_queue_setup(struct rte_eth_dev *dev,
1175                          uint16_t queue_idx,
1176                          uint16_t nb_desc,
1177                          unsigned int socket_id,
1178                          const struct rte_eth_txconf *tx_conf)
1179 {
1180         const struct rte_memzone *tz;
1181         struct em_tx_queue *txq;
1182         struct e1000_hw     *hw;
1183         uint32_t tsize;
1184         uint16_t tx_rs_thresh, tx_free_thresh;
1185
1186         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1187
1188         /*
1189          * Validate number of transmit descriptors.
1190          * It must not exceed hardware maximum, and must be multiple
1191          * of EM_ALIGN.
1192          */
1193         if (((nb_desc * sizeof(*txq->tx_ring)) % EM_ALIGN) != 0 ||
1194                         (nb_desc > EM_MAX_RING_DESC) ||
1195                         (nb_desc < EM_MIN_RING_DESC)) {
1196                 return -(EINVAL);
1197         }
1198
1199         tx_free_thresh = tx_conf->tx_free_thresh;
1200         if (tx_free_thresh == 0)
1201                 tx_free_thresh = (uint16_t)RTE_MIN(nb_desc / 4,
1202                                         DEFAULT_TX_FREE_THRESH);
1203
1204         tx_rs_thresh = tx_conf->tx_rs_thresh;
1205         if (tx_rs_thresh == 0)
1206                 tx_rs_thresh = (uint16_t)RTE_MIN(tx_free_thresh,
1207                                         DEFAULT_TX_RS_THRESH);
1208
1209         if (tx_free_thresh >= (nb_desc - 3)) {
1210                 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
1211                         "number of TX descriptors minus 3. (tx_free_thresh=%u "
1212                         "port=%d queue=%d)\n", (unsigned int)tx_free_thresh,
1213                                 (int)dev->data->port_id, (int)queue_idx);
1214                 return -(EINVAL);
1215         }
1216         if (tx_rs_thresh > tx_free_thresh) {
1217                 RTE_LOG(ERR, PMD, "tx_rs_thresh must be less than or equal to "
1218                         "tx_free_thresh. (tx_free_thresh=%u tx_rs_thresh=%u "
1219                         "port=%d queue=%d)\n", (unsigned int)tx_free_thresh,
1220                         (unsigned int)tx_rs_thresh, (int)dev->data->port_id,
1221                                                         (int)queue_idx);
1222                 return -(EINVAL);
1223         }
1224
1225         /*
1226          * If rs_bit_thresh is greater than 1, then TX WTHRESH should be
1227          * set to 0. If WTHRESH is greater than zero, the RS bit is ignored
1228          * by the NIC and all descriptors are written back after the NIC
1229          * accumulates WTHRESH descriptors.
1230          */
1231         if (tx_conf->tx_thresh.wthresh != 0 && tx_rs_thresh != 1) {
1232                 RTE_LOG(ERR, PMD, "TX WTHRESH must be set to 0 if "
1233                         "tx_rs_thresh is greater than 1. (tx_rs_thresh=%u "
1234                         "port=%d queue=%d)\n", (unsigned int)tx_rs_thresh,
1235                                 (int)dev->data->port_id, (int)queue_idx);
1236                 return -(EINVAL);
1237         }
1238
1239         /* Free memory prior to re-allocation if needed... */
1240         if (dev->data->tx_queues[queue_idx] != NULL) {
1241                 em_tx_queue_release(dev->data->tx_queues[queue_idx]);
1242                 dev->data->tx_queues[queue_idx] = NULL;
1243         }
1244
1245         /*
1246          * Allocate TX ring hardware descriptors. A memzone large enough to
1247          * handle the maximum ring size is allocated in order to allow for
1248          * resizing in later calls to the queue setup function.
1249          */
1250         tsize = sizeof (txq->tx_ring[0]) * EM_MAX_RING_DESC;
1251         if ((tz = ring_dma_zone_reserve(dev, "tx_ring", queue_idx, tsize,
1252                         socket_id)) == NULL)
1253                 return (-ENOMEM);
1254
1255         /* Allocate the tx queue data structure. */
1256         if ((txq = rte_zmalloc("ethdev TX queue", sizeof(*txq),
1257                         CACHE_LINE_SIZE)) == NULL)
1258                 return (-ENOMEM);
1259
1260         /* Allocate software ring */
1261         if ((txq->sw_ring = rte_zmalloc("txq->sw_ring",
1262                         sizeof(txq->sw_ring[0]) * nb_desc,
1263                         CACHE_LINE_SIZE)) == NULL) {
1264                 em_tx_queue_release(txq);
1265                 return (-ENOMEM);
1266         }
1267
1268         txq->nb_tx_desc = nb_desc;
1269         txq->tx_free_thresh = tx_free_thresh;
1270         txq->tx_rs_thresh = tx_rs_thresh;
1271         txq->pthresh = tx_conf->tx_thresh.pthresh;
1272         txq->hthresh = tx_conf->tx_thresh.hthresh;
1273         txq->wthresh = tx_conf->tx_thresh.wthresh;
1274         if (txq->wthresh > 0 && hw->mac.type == e1000_82576)
1275                 txq->wthresh = 1;
1276         txq->queue_id = queue_idx;
1277         txq->port_id = dev->data->port_id;
1278
1279         txq->tdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_TDT(queue_idx));
1280         txq->tx_ring_phys_addr = (uint64_t) tz->phys_addr;
1281         txq->tx_ring = (struct e1000_data_desc *) tz->addr;
1282
1283         PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64"\n",
1284                 txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr);
1285
1286         em_reset_tx_queue(txq);
1287
1288         dev->data->tx_queues[queue_idx] = txq;
1289         return (0);
1290 }
1291
1292 static void
1293 em_rx_queue_release_mbufs(struct em_rx_queue *rxq)
1294 {
1295         unsigned i;
1296
1297         if (rxq->sw_ring != NULL) {
1298                 for (i = 0; i != rxq->nb_rx_desc; i++) {
1299                         if (rxq->sw_ring[i].mbuf != NULL) {
1300                                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
1301                                 rxq->sw_ring[i].mbuf = NULL;
1302                         }
1303                 }
1304         }
1305 }
1306
1307 static void
1308 em_rx_queue_release(struct em_rx_queue *rxq)
1309 {
1310         if (rxq != NULL) {
1311                 em_rx_queue_release_mbufs(rxq);
1312                 rte_free(rxq->sw_ring);
1313                 rte_free(rxq);
1314         }
1315 }
1316
1317 void
1318 eth_em_rx_queue_release(void *rxq)
1319 {
1320         em_rx_queue_release(rxq);
1321 }
1322
1323 /* Reset dynamic em_rx_queue fields back to defaults */
1324 static void
1325 em_reset_rx_queue(struct em_rx_queue *rxq)
1326 {
1327         rxq->rx_tail = 0;
1328         rxq->nb_rx_hold = 0;
1329         rxq->pkt_first_seg = NULL;
1330         rxq->pkt_last_seg = NULL;
1331 }
1332
1333 int
1334 eth_em_rx_queue_setup(struct rte_eth_dev *dev,
1335                 uint16_t queue_idx,
1336                 uint16_t nb_desc,
1337                 unsigned int socket_id,
1338                 const struct rte_eth_rxconf *rx_conf,
1339                 struct rte_mempool *mp)
1340 {
1341         const struct rte_memzone *rz;
1342         struct em_rx_queue *rxq;
1343         struct e1000_hw     *hw;
1344         uint32_t rsize;
1345
1346         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1347
1348         /*
1349          * Validate number of receive descriptors.
1350          * It must not exceed hardware maximum, and must be multiple
1351          * of EM_ALIGN.
1352          */
1353         if (((nb_desc * sizeof(rxq->rx_ring[0])) % EM_ALIGN) != 0 ||
1354                         (nb_desc > EM_MAX_RING_DESC) ||
1355                         (nb_desc < EM_MIN_RING_DESC)) {
1356                 return (-EINVAL);
1357         }
1358
1359         /*
1360          * EM devices don't support drop_en functionality
1361          */
1362         if (rx_conf->rx_drop_en) {
1363                 RTE_LOG(ERR, PMD, "drop_en functionality not supported by device\n");
1364                 return (-EINVAL);
1365         }
1366
1367         /* Free memory prior to re-allocation if needed. */
1368         if (dev->data->rx_queues[queue_idx] != NULL) {
1369                 em_rx_queue_release(dev->data->rx_queues[queue_idx]);
1370                 dev->data->rx_queues[queue_idx] = NULL;
1371         }
1372
1373         /* Allocate RX ring for max possible mumber of hardware descriptors. */
1374         rsize = sizeof (rxq->rx_ring[0]) * EM_MAX_RING_DESC;
1375         if ((rz = ring_dma_zone_reserve(dev, "rx_ring", queue_idx, rsize,
1376                         socket_id)) == NULL)
1377                 return (-ENOMEM);
1378
1379         /* Allocate the RX queue data structure. */
1380         if ((rxq = rte_zmalloc("ethdev RX queue", sizeof(*rxq),
1381                         CACHE_LINE_SIZE)) == NULL)
1382                 return (-ENOMEM);
1383
1384         /* Allocate software ring. */
1385         if ((rxq->sw_ring = rte_zmalloc("rxq->sw_ring",
1386                         sizeof (rxq->sw_ring[0]) * nb_desc,
1387                         CACHE_LINE_SIZE)) == NULL) {
1388                 em_rx_queue_release(rxq);
1389                 return (-ENOMEM);
1390         }
1391
1392         rxq->mb_pool = mp;
1393         rxq->nb_rx_desc = nb_desc;
1394         rxq->pthresh = rx_conf->rx_thresh.pthresh;
1395         rxq->hthresh = rx_conf->rx_thresh.hthresh;
1396         rxq->wthresh = rx_conf->rx_thresh.wthresh;
1397         if (rxq->wthresh > 0 && hw->mac.type == e1000_82576)
1398                 rxq->wthresh = 1;
1399
1400         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
1401         rxq->queue_id = queue_idx;
1402         rxq->port_id = dev->data->port_id;
1403         rxq->crc_len = (uint8_t) ((dev->data->dev_conf.rxmode.hw_strip_crc) ?
1404                                 0 : ETHER_CRC_LEN);
1405
1406         rxq->rdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDT(queue_idx));
1407         rxq->rdh_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDH(queue_idx));       
1408         rxq->rx_ring_phys_addr = (uint64_t) rz->phys_addr;
1409         rxq->rx_ring = (struct e1000_rx_desc *) rz->addr;
1410
1411         PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64"\n",
1412                 rxq->sw_ring, rxq->rx_ring, rxq->rx_ring_phys_addr);
1413
1414         dev->data->rx_queues[queue_idx] = rxq;
1415         em_reset_rx_queue(rxq);
1416
1417         return (0);
1418 }
1419
1420 uint32_t 
1421 eth_em_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1422 {
1423 #define EM_RXQ_SCAN_INTERVAL 4
1424         volatile struct e1000_rx_desc *rxdp;
1425         struct em_rx_queue *rxq;
1426         uint32_t desc = 0;
1427
1428         if (rx_queue_id >= dev->data->nb_rx_queues) {
1429                 PMD_RX_LOG(DEBUG,"Invalid RX queue_id=%d\n", rx_queue_id);
1430                 return 0;
1431         }
1432
1433         rxq = dev->data->rx_queues[rx_queue_id];
1434         rxdp = &(rxq->rx_ring[rxq->rx_tail]);
1435
1436         while ((desc < rxq->nb_rx_desc) &&
1437                 (rxdp->status & E1000_RXD_STAT_DD)) {
1438                 desc += EM_RXQ_SCAN_INTERVAL;
1439                 rxdp += EM_RXQ_SCAN_INTERVAL;
1440                 if (rxq->rx_tail + desc >= rxq->nb_rx_desc)
1441                         rxdp = &(rxq->rx_ring[rxq->rx_tail +
1442                                 desc - rxq->nb_rx_desc]);
1443         }
1444
1445         return desc;
1446 }
1447
1448 int
1449 eth_em_rx_descriptor_done(void *rx_queue, uint16_t offset)
1450 {
1451         volatile struct e1000_rx_desc *rxdp;
1452         struct em_rx_queue *rxq = rx_queue;
1453         uint32_t desc;
1454
1455         if (unlikely(offset >= rxq->nb_rx_desc))
1456                 return 0;
1457         desc = rxq->rx_tail + offset;
1458         if (desc >= rxq->nb_rx_desc)
1459                 desc -= rxq->nb_rx_desc;
1460
1461         rxdp = &rxq->rx_ring[desc];
1462         return !!(rxdp->status & E1000_RXD_STAT_DD);
1463 }
1464
1465 void
1466 em_dev_clear_queues(struct rte_eth_dev *dev)
1467 {
1468         uint16_t i;
1469         struct em_tx_queue *txq;
1470         struct em_rx_queue *rxq;
1471
1472         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1473                 txq = dev->data->tx_queues[i];
1474                 if (txq != NULL) {
1475                         em_tx_queue_release_mbufs(txq);
1476                         em_reset_tx_queue(txq);
1477                 }
1478         }
1479
1480         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1481                 rxq = dev->data->rx_queues[i];
1482                 if (rxq != NULL) {
1483                         em_rx_queue_release_mbufs(rxq);
1484                         em_reset_rx_queue(rxq);
1485                 }
1486         }
1487 }
1488
1489 /*
1490  * Takes as input/output parameter RX buffer size.
1491  * Returns (BSIZE | BSEX | FLXBUF) fields of RCTL register.
1492  */
1493 static uint32_t
1494 em_rctl_bsize(__rte_unused enum e1000_mac_type hwtyp, uint32_t *bufsz)
1495 {
1496         /*
1497          * For BSIZE & BSEX all configurable sizes are:
1498          * 16384: rctl |= (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX);
1499          *  8192: rctl |= (E1000_RCTL_SZ_8192  | E1000_RCTL_BSEX);
1500          *  4096: rctl |= (E1000_RCTL_SZ_4096  | E1000_RCTL_BSEX);
1501          *  2048: rctl |= E1000_RCTL_SZ_2048;
1502          *  1024: rctl |= E1000_RCTL_SZ_1024;
1503          *   512: rctl |= E1000_RCTL_SZ_512;
1504          *   256: rctl |= E1000_RCTL_SZ_256;
1505          */
1506         static const struct {
1507                 uint32_t bufsz;
1508                 uint32_t rctl;
1509         } bufsz_to_rctl[] = {
1510                 {16384, (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX)},
1511                 {8192,  (E1000_RCTL_SZ_8192  | E1000_RCTL_BSEX)},
1512                 {4096,  (E1000_RCTL_SZ_4096  | E1000_RCTL_BSEX)},
1513                 {2048,  E1000_RCTL_SZ_2048},
1514                 {1024,  E1000_RCTL_SZ_1024},
1515                 {512,   E1000_RCTL_SZ_512},
1516                 {256,   E1000_RCTL_SZ_256},
1517         };
1518
1519         int i;
1520         uint32_t rctl_bsize;
1521
1522         rctl_bsize = *bufsz;
1523
1524         /*
1525          * Starting from 82571 it is possible to specify RX buffer size
1526          * by RCTL.FLXBUF. When this field is different from zero, the
1527          * RX buffer size = RCTL.FLXBUF * 1K
1528          * (e.g. t is possible to specify RX buffer size  1,2,...,15KB).
1529          * It is working ok on real HW, but by some reason doesn't work
1530          * on VMware emulated 82574L.
1531          * So for now, always use BSIZE/BSEX to setup RX buffer size.
1532          * If you don't plan to use it on VMware emulated 82574L and
1533          * would like to specify RX buffer size in 1K granularity,
1534          * uncomment the following lines:
1535          * ***************************************************************
1536          * if (hwtyp >= e1000_82571 && hwtyp <= e1000_82574 &&
1537          *              rctl_bsize >= EM_RCTL_FLXBUF_STEP) {
1538          *      rctl_bsize /= EM_RCTL_FLXBUF_STEP;
1539          *      *bufsz = rctl_bsize;
1540          *      return (rctl_bsize << E1000_RCTL_FLXBUF_SHIFT &
1541          *              E1000_RCTL_FLXBUF_MASK);
1542          * }
1543          * ***************************************************************
1544          */
1545
1546         for (i = 0; i != sizeof(bufsz_to_rctl) / sizeof(bufsz_to_rctl[0]);
1547                         i++) {
1548                 if (rctl_bsize >= bufsz_to_rctl[i].bufsz) {
1549                         *bufsz = bufsz_to_rctl[i].bufsz;
1550                         return (bufsz_to_rctl[i].rctl);
1551                 }
1552         }
1553
1554         /* Should never happen. */
1555         return (-EINVAL);
1556 }
1557
1558 static int
1559 em_alloc_rx_queue_mbufs(struct em_rx_queue *rxq)
1560 {
1561         struct em_rx_entry *rxe = rxq->sw_ring;
1562         uint64_t dma_addr;
1563         unsigned i;
1564         static const struct e1000_rx_desc rxd_init = {
1565                 .buffer_addr = 0,
1566         };
1567
1568         /* Initialize software ring entries */
1569         for (i = 0; i < rxq->nb_rx_desc; i++) {
1570                 volatile struct e1000_rx_desc *rxd;
1571                 struct rte_mbuf *mbuf = rte_rxmbuf_alloc(rxq->mb_pool);
1572
1573                 if (mbuf == NULL) {
1574                         PMD_INIT_LOG(ERR, "RX mbuf alloc failed "
1575                                 "queue_id=%hu\n", rxq->queue_id);
1576                         em_rx_queue_release(rxq);
1577                         return (-ENOMEM);
1578                 }
1579
1580                 dma_addr = rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR_DEFAULT(mbuf));
1581
1582                 /* Clear HW ring memory */
1583                 rxq->rx_ring[i] = rxd_init;
1584
1585                 rxd = &rxq->rx_ring[i];
1586                 rxd->buffer_addr = dma_addr;
1587                 rxe[i].mbuf = mbuf;
1588         }
1589
1590         return 0;
1591 }
1592
1593 /*********************************************************************
1594  *
1595  *  Enable receive unit.
1596  *
1597  **********************************************************************/
1598 int
1599 eth_em_rx_init(struct rte_eth_dev *dev)
1600 {
1601         struct e1000_hw *hw;
1602         struct em_rx_queue *rxq;
1603         uint32_t rctl;
1604         uint32_t rfctl;
1605         uint32_t rxcsum;
1606         uint32_t rctl_bsize;
1607         uint16_t i;
1608         int ret;
1609
1610         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1611
1612         /*
1613          * Make sure receives are disabled while setting
1614          * up the descriptor ring.
1615          */
1616         rctl = E1000_READ_REG(hw, E1000_RCTL);
1617         E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
1618
1619         rfctl = E1000_READ_REG(hw, E1000_RFCTL);
1620
1621         /* Disable extended descriptor type. */
1622         rfctl &= ~E1000_RFCTL_EXTEN;
1623         /* Disable accelerated acknowledge */
1624         if (hw->mac.type == e1000_82574)
1625                 rfctl |= E1000_RFCTL_ACK_DIS;
1626
1627         E1000_WRITE_REG(hw, E1000_RFCTL, rfctl);
1628
1629         /*
1630          * XXX TEMPORARY WORKAROUND: on some systems with 82573
1631          * long latencies are observed, like Lenovo X60. This
1632          * change eliminates the problem, but since having positive
1633          * values in RDTR is a known source of problems on other
1634          * platforms another solution is being sought.
1635          */
1636         if (hw->mac.type == e1000_82573)
1637                 E1000_WRITE_REG(hw, E1000_RDTR, 0x20);
1638
1639         dev->rx_pkt_burst = (eth_rx_burst_t)eth_em_recv_pkts;
1640
1641         /* Determine RX bufsize. */
1642         rctl_bsize = EM_MAX_BUF_SIZE;
1643         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1644                 struct rte_pktmbuf_pool_private *mbp_priv;
1645                 uint32_t buf_size;
1646
1647                 rxq = dev->data->rx_queues[i];
1648                 mbp_priv = rte_mempool_get_priv(rxq->mb_pool);
1649                 buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
1650                 rctl_bsize = RTE_MIN(rctl_bsize, buf_size);
1651         }
1652
1653         rctl |= em_rctl_bsize(hw->mac.type, &rctl_bsize);
1654
1655         /* Configure and enable each RX queue. */
1656         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1657                 uint64_t bus_addr;
1658                 uint32_t rxdctl;
1659
1660                 rxq = dev->data->rx_queues[i];
1661
1662                 /* Allocate buffers for descriptor rings and setup queue */
1663                 ret = em_alloc_rx_queue_mbufs(rxq);
1664                 if (ret)
1665                         return ret;
1666
1667                 /*
1668                  * Reset crc_len in case it was changed after queue setup by a
1669                  *  call to configure
1670                  */
1671                 rxq->crc_len =
1672                         (uint8_t)(dev->data->dev_conf.rxmode.hw_strip_crc ?
1673                                                         0 : ETHER_CRC_LEN);
1674
1675                 bus_addr = rxq->rx_ring_phys_addr;
1676                 E1000_WRITE_REG(hw, E1000_RDLEN(i),
1677                                 rxq->nb_rx_desc *
1678                                 sizeof(*rxq->rx_ring));
1679                 E1000_WRITE_REG(hw, E1000_RDBAH(i),
1680                                 (uint32_t)(bus_addr >> 32));
1681                 E1000_WRITE_REG(hw, E1000_RDBAL(i), (uint32_t)bus_addr);
1682
1683                 E1000_WRITE_REG(hw, E1000_RDH(i), 0);
1684                 E1000_WRITE_REG(hw, E1000_RDT(i), rxq->nb_rx_desc - 1);
1685
1686                 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1687                 rxdctl &= 0xFE000000;
1688                 rxdctl |= rxq->pthresh & 0x3F;
1689                 rxdctl |= (rxq->hthresh & 0x3F) << 8;
1690                 rxdctl |= (rxq->wthresh & 0x3F) << 16;
1691                 rxdctl |= E1000_RXDCTL_GRAN;
1692                 E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
1693
1694                 /*
1695                  * Due to EM devices not having any sort of hardware
1696                  * limit for packet length, jumbo frame of any size
1697                  * can be accepted, thus we have to enable scattered
1698                  * rx if jumbo frames are enabled (or if buffer size
1699                  * is too small to accomodate non-jumbo packets)
1700                  * to avoid splitting packets that don't fit into
1701                  * one buffer.
1702                  */
1703                 if (dev->data->dev_conf.rxmode.jumbo_frame ||
1704                                 rctl_bsize < ETHER_MAX_LEN) {
1705                         dev->rx_pkt_burst =
1706                                 (eth_rx_burst_t)eth_em_recv_scattered_pkts;
1707                         dev->data->scattered_rx = 1;
1708                 }
1709         }
1710
1711         /*
1712          * Setup the Checksum Register.
1713          * Receive Full-Packet Checksum Offload is mutually exclusive with RSS.
1714          */
1715         rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
1716
1717         if (dev->data->dev_conf.rxmode.hw_ip_checksum)
1718                 rxcsum |= E1000_RXCSUM_IPOFL;
1719         else
1720                 rxcsum &= ~E1000_RXCSUM_IPOFL;
1721         E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
1722
1723         /* No MRQ or RSS support for now */
1724
1725         /* Set early receive threshold on appropriate hw */
1726         if ((hw->mac.type == e1000_ich9lan ||
1727                         hw->mac.type == e1000_pch2lan ||
1728                         hw->mac.type == e1000_ich10lan) &&
1729                         dev->data->dev_conf.rxmode.jumbo_frame == 1) {
1730                 u32 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1731                 E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl | 3);
1732                 E1000_WRITE_REG(hw, E1000_ERT, 0x100 | (1 << 13));
1733         }
1734
1735         if (hw->mac.type == e1000_pch2lan) {
1736                 if (dev->data->dev_conf.rxmode.jumbo_frame == 1)
1737                         e1000_lv_jumbo_workaround_ich8lan(hw, TRUE);
1738                 else
1739                         e1000_lv_jumbo_workaround_ich8lan(hw, FALSE);
1740         }
1741
1742         /* Setup the Receive Control Register. */
1743         if (dev->data->dev_conf.rxmode.hw_strip_crc)
1744                 rctl |= E1000_RCTL_SECRC; /* Strip Ethernet CRC. */
1745         else
1746                 rctl &= ~E1000_RCTL_SECRC; /* Do not Strip Ethernet CRC. */
1747
1748         rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1749         rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
1750                 E1000_RCTL_RDMTS_HALF |
1751                 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1752
1753         /* Make sure VLAN Filters are off. */
1754         rctl &= ~E1000_RCTL_VFE;
1755         /* Don't store bad packets. */
1756         rctl &= ~E1000_RCTL_SBP;
1757         /* Legacy descriptor type. */
1758         rctl &= ~E1000_RCTL_DTYP_MASK;
1759
1760         /*
1761          * Configure support of jumbo frames, if any.
1762          */
1763         if (dev->data->dev_conf.rxmode.jumbo_frame == 1)
1764                 rctl |= E1000_RCTL_LPE;
1765         else
1766                 rctl &= ~E1000_RCTL_LPE;
1767
1768         /* Enable Receives. */
1769         E1000_WRITE_REG(hw, E1000_RCTL, rctl);
1770
1771         return 0;
1772 }
1773
1774 /*********************************************************************
1775  *
1776  *  Enable transmit unit.
1777  *
1778  **********************************************************************/
1779 void
1780 eth_em_tx_init(struct rte_eth_dev *dev)
1781 {
1782         struct e1000_hw     *hw;
1783         struct em_tx_queue *txq;
1784         uint32_t tctl;
1785         uint32_t txdctl;
1786         uint16_t i;
1787
1788         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1789
1790         /* Setup the Base and Length of the Tx Descriptor Rings. */
1791         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1792                 uint64_t bus_addr;
1793
1794                 txq = dev->data->tx_queues[i];
1795                 bus_addr = txq->tx_ring_phys_addr;
1796                 E1000_WRITE_REG(hw, E1000_TDLEN(i),
1797                                 txq->nb_tx_desc *
1798                                 sizeof(*txq->tx_ring));
1799                 E1000_WRITE_REG(hw, E1000_TDBAH(i),
1800                                 (uint32_t)(bus_addr >> 32));
1801                 E1000_WRITE_REG(hw, E1000_TDBAL(i), (uint32_t)bus_addr);
1802
1803                 /* Setup the HW Tx Head and Tail descriptor pointers. */
1804                 E1000_WRITE_REG(hw, E1000_TDT(i), 0);
1805                 E1000_WRITE_REG(hw, E1000_TDH(i), 0);
1806
1807                 /* Setup Transmit threshold registers. */
1808                 txdctl = E1000_READ_REG(hw, E1000_TXDCTL(i));
1809                 /*
1810                  * bit 22 is reserved, on some models should always be 0,
1811                  * on others  - always 1.
1812                  */
1813                 txdctl &= E1000_TXDCTL_COUNT_DESC;
1814                 txdctl |= txq->pthresh & 0x3F;
1815                 txdctl |= (txq->hthresh & 0x3F) << 8;
1816                 txdctl |= (txq->wthresh & 0x3F) << 16;
1817                 txdctl |= E1000_TXDCTL_GRAN;
1818                 E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
1819         }
1820
1821         /* Program the Transmit Control Register. */
1822         tctl = E1000_READ_REG(hw, E1000_TCTL);
1823         tctl &= ~E1000_TCTL_CT;
1824         tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
1825                  (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
1826
1827         /* This write will effectively turn on the transmit unit. */
1828         E1000_WRITE_REG(hw, E1000_TCTL, tctl);
1829 }
1830