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