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