f2548587d52f2950bbc752157e0532c818593619
[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, 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         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 #ifdef RTE_LIBRTE_XEN_DOM0
1104         return rte_memzone_reserve_bounded(z_name, ring_size,
1105                         socket_id, 0, CACHE_LINE_SIZE, RTE_PGSIZE_2M);
1106 #else
1107         return rte_memzone_reserve(z_name, ring_size, socket_id, 0);
1108 #endif
1109 }
1110
1111 static void
1112 em_tx_queue_release_mbufs(struct em_tx_queue *txq)
1113 {
1114         unsigned i;
1115
1116         if (txq->sw_ring != NULL) {
1117                 for (i = 0; i != txq->nb_tx_desc; i++) {
1118                         if (txq->sw_ring[i].mbuf != NULL) {
1119                                 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
1120                                 txq->sw_ring[i].mbuf = NULL;
1121                         }
1122                 }
1123         }
1124 }
1125
1126 static void
1127 em_tx_queue_release(struct em_tx_queue *txq)
1128 {
1129         if (txq != NULL) {
1130                 em_tx_queue_release_mbufs(txq);
1131                 rte_free(txq->sw_ring);
1132                 rte_free(txq);
1133         }
1134 }
1135
1136 void
1137 eth_em_tx_queue_release(void *txq)
1138 {
1139         em_tx_queue_release(txq);
1140 }
1141
1142 /* (Re)set dynamic em_tx_queue fields to defaults */
1143 static void
1144 em_reset_tx_queue(struct em_tx_queue *txq)
1145 {
1146         uint16_t i, nb_desc, prev;
1147         static const struct e1000_data_desc txd_init = {
1148                 .upper.fields = {.status = E1000_TXD_STAT_DD},
1149         };
1150
1151         nb_desc = txq->nb_tx_desc;
1152
1153         /* Initialize ring entries */
1154
1155         prev = (uint16_t) (nb_desc - 1);
1156
1157         for (i = 0; i < nb_desc; i++) {
1158                 txq->tx_ring[i] = txd_init;
1159                 txq->sw_ring[i].mbuf = NULL;
1160                 txq->sw_ring[i].last_id = i;
1161                 txq->sw_ring[prev].next_id = i;
1162                 prev = i;
1163         }
1164
1165         /*
1166          * Always allow 1 descriptor to be un-allocated to avoid
1167          * a H/W race condition
1168          */
1169         txq->nb_tx_free = (uint16_t)(nb_desc - 1);
1170         txq->last_desc_cleaned = (uint16_t)(nb_desc - 1);
1171         txq->nb_tx_used = 0;
1172         txq->tx_tail = 0;
1173
1174         memset((void*)&txq->ctx_cache, 0, sizeof (txq->ctx_cache));
1175 }
1176
1177 int
1178 eth_em_tx_queue_setup(struct rte_eth_dev *dev,
1179                          uint16_t queue_idx,
1180                          uint16_t nb_desc,
1181                          unsigned int socket_id,
1182                          const struct rte_eth_txconf *tx_conf)
1183 {
1184         const struct rte_memzone *tz;
1185         struct em_tx_queue *txq;
1186         struct e1000_hw     *hw;
1187         uint32_t tsize;
1188         uint16_t tx_rs_thresh, tx_free_thresh;
1189
1190         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1191
1192         /*
1193          * Validate number of transmit descriptors.
1194          * It must not exceed hardware maximum, and must be multiple
1195          * of EM_ALIGN.
1196          */
1197         if (((nb_desc * sizeof(*txq->tx_ring)) % EM_ALIGN) != 0 ||
1198                         (nb_desc > EM_MAX_RING_DESC) ||
1199                         (nb_desc < EM_MIN_RING_DESC)) {
1200                 return -(EINVAL);
1201         }
1202
1203         tx_free_thresh = tx_conf->tx_free_thresh;
1204         if (tx_free_thresh == 0)
1205                 tx_free_thresh = (uint16_t)RTE_MIN(nb_desc / 4,
1206                                         DEFAULT_TX_FREE_THRESH);
1207
1208         tx_rs_thresh = tx_conf->tx_rs_thresh;
1209         if (tx_rs_thresh == 0)
1210                 tx_rs_thresh = (uint16_t)RTE_MIN(tx_free_thresh,
1211                                         DEFAULT_TX_RS_THRESH);
1212
1213         if (tx_free_thresh >= (nb_desc - 3)) {
1214                 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
1215                         "number of TX descriptors minus 3. (tx_free_thresh=%u "
1216                         "port=%d queue=%d)\n", (unsigned int)tx_free_thresh,
1217                                 (int)dev->data->port_id, (int)queue_idx);
1218                 return -(EINVAL);
1219         }
1220         if (tx_rs_thresh > tx_free_thresh) {
1221                 RTE_LOG(ERR, PMD, "tx_rs_thresh must be less than or equal to "
1222                         "tx_free_thresh. (tx_free_thresh=%u tx_rs_thresh=%u "
1223                         "port=%d queue=%d)\n", (unsigned int)tx_free_thresh,
1224                         (unsigned int)tx_rs_thresh, (int)dev->data->port_id,
1225                                                         (int)queue_idx);
1226                 return -(EINVAL);
1227         }
1228
1229         /*
1230          * If rs_bit_thresh is greater than 1, then TX WTHRESH should be
1231          * set to 0. If WTHRESH is greater than zero, the RS bit is ignored
1232          * by the NIC and all descriptors are written back after the NIC
1233          * accumulates WTHRESH descriptors.
1234          */
1235         if (tx_conf->tx_thresh.wthresh != 0 && tx_rs_thresh != 1) {
1236                 RTE_LOG(ERR, PMD, "TX WTHRESH must be set to 0 if "
1237                         "tx_rs_thresh is greater than 1. (tx_rs_thresh=%u "
1238                         "port=%d queue=%d)\n", (unsigned int)tx_rs_thresh,
1239                                 (int)dev->data->port_id, (int)queue_idx);
1240                 return -(EINVAL);
1241         }
1242
1243         /* Free memory prior to re-allocation if needed... */
1244         if (dev->data->tx_queues[queue_idx] != NULL) {
1245                 em_tx_queue_release(dev->data->tx_queues[queue_idx]);
1246                 dev->data->tx_queues[queue_idx] = NULL;
1247         }
1248
1249         /*
1250          * Allocate TX ring hardware descriptors. A memzone large enough to
1251          * handle the maximum ring size is allocated in order to allow for
1252          * resizing in later calls to the queue setup function.
1253          */
1254         tsize = sizeof (txq->tx_ring[0]) * EM_MAX_RING_DESC;
1255         if ((tz = ring_dma_zone_reserve(dev, "tx_ring", queue_idx, tsize,
1256                         socket_id)) == NULL)
1257                 return (-ENOMEM);
1258
1259         /* Allocate the tx queue data structure. */
1260         if ((txq = rte_zmalloc("ethdev TX queue", sizeof(*txq),
1261                         CACHE_LINE_SIZE)) == NULL)
1262                 return (-ENOMEM);
1263
1264         /* Allocate software ring */
1265         if ((txq->sw_ring = rte_zmalloc("txq->sw_ring",
1266                         sizeof(txq->sw_ring[0]) * nb_desc,
1267                         CACHE_LINE_SIZE)) == NULL) {
1268                 em_tx_queue_release(txq);
1269                 return (-ENOMEM);
1270         }
1271
1272         txq->nb_tx_desc = nb_desc;
1273         txq->tx_free_thresh = tx_free_thresh;
1274         txq->tx_rs_thresh = tx_rs_thresh;
1275         txq->pthresh = tx_conf->tx_thresh.pthresh;
1276         txq->hthresh = tx_conf->tx_thresh.hthresh;
1277         txq->wthresh = tx_conf->tx_thresh.wthresh;
1278         txq->queue_id = queue_idx;
1279         txq->port_id = dev->data->port_id;
1280
1281         txq->tdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_TDT(queue_idx));
1282 #ifndef RTE_LIBRTE_XEN_DOM0
1283         txq->tx_ring_phys_addr = (uint64_t) tz->phys_addr;
1284 #else
1285         txq->tx_ring_phys_addr = rte_mem_phy2mch(tz->memseg_id, tz->phys_addr);
1286 #endif
1287         txq->tx_ring = (struct e1000_data_desc *) tz->addr;
1288
1289         PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64"\n",
1290                 txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr);
1291
1292         em_reset_tx_queue(txq);
1293
1294         dev->data->tx_queues[queue_idx] = txq;
1295         return (0);
1296 }
1297
1298 static void
1299 em_rx_queue_release_mbufs(struct em_rx_queue *rxq)
1300 {
1301         unsigned i;
1302
1303         if (rxq->sw_ring != NULL) {
1304                 for (i = 0; i != rxq->nb_rx_desc; i++) {
1305                         if (rxq->sw_ring[i].mbuf != NULL) {
1306                                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
1307                                 rxq->sw_ring[i].mbuf = NULL;
1308                         }
1309                 }
1310         }
1311 }
1312
1313 static void
1314 em_rx_queue_release(struct em_rx_queue *rxq)
1315 {
1316         if (rxq != NULL) {
1317                 em_rx_queue_release_mbufs(rxq);
1318                 rte_free(rxq->sw_ring);
1319                 rte_free(rxq);
1320         }
1321 }
1322
1323 void
1324 eth_em_rx_queue_release(void *rxq)
1325 {
1326         em_rx_queue_release(rxq);
1327 }
1328
1329 /* Reset dynamic em_rx_queue fields back to defaults */
1330 static void
1331 em_reset_rx_queue(struct em_rx_queue *rxq)
1332 {
1333         rxq->rx_tail = 0;
1334         rxq->nb_rx_hold = 0;
1335         rxq->pkt_first_seg = NULL;
1336         rxq->pkt_last_seg = NULL;
1337 }
1338
1339 int
1340 eth_em_rx_queue_setup(struct rte_eth_dev *dev,
1341                 uint16_t queue_idx,
1342                 uint16_t nb_desc,
1343                 unsigned int socket_id,
1344                 const struct rte_eth_rxconf *rx_conf,
1345                 struct rte_mempool *mp)
1346 {
1347         const struct rte_memzone *rz;
1348         struct em_rx_queue *rxq;
1349         struct e1000_hw     *hw;
1350         uint32_t rsize;
1351
1352         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1353
1354         /*
1355          * Validate number of receive descriptors.
1356          * It must not exceed hardware maximum, and must be multiple
1357          * of EM_ALIGN.
1358          */
1359         if (((nb_desc * sizeof(rxq->rx_ring[0])) % EM_ALIGN) != 0 ||
1360                         (nb_desc > EM_MAX_RING_DESC) ||
1361                         (nb_desc < EM_MIN_RING_DESC)) {
1362                 return (-EINVAL);
1363         }
1364
1365         /*
1366          * EM devices don't support drop_en functionality
1367          */
1368         if (rx_conf->rx_drop_en) {
1369                 RTE_LOG(ERR, PMD, "drop_en functionality not supported by device\n");
1370                 return (-EINVAL);
1371         }
1372
1373         /* Free memory prior to re-allocation if needed. */
1374         if (dev->data->rx_queues[queue_idx] != NULL) {
1375                 em_rx_queue_release(dev->data->rx_queues[queue_idx]);
1376                 dev->data->rx_queues[queue_idx] = NULL;
1377         }
1378
1379         /* Allocate RX ring for max possible mumber of hardware descriptors. */
1380         rsize = sizeof (rxq->rx_ring[0]) * EM_MAX_RING_DESC;
1381         if ((rz = ring_dma_zone_reserve(dev, "rx_ring", queue_idx, rsize,
1382                         socket_id)) == NULL)
1383                 return (-ENOMEM);
1384
1385         /* Allocate the RX queue data structure. */
1386         if ((rxq = rte_zmalloc("ethdev RX queue", sizeof(*rxq),
1387                         CACHE_LINE_SIZE)) == NULL)
1388                 return (-ENOMEM);
1389
1390         /* Allocate software ring. */
1391         if ((rxq->sw_ring = rte_zmalloc("rxq->sw_ring",
1392                         sizeof (rxq->sw_ring[0]) * nb_desc,
1393                         CACHE_LINE_SIZE)) == NULL) {
1394                 em_rx_queue_release(rxq);
1395                 return (-ENOMEM);
1396         }
1397
1398         rxq->mb_pool = mp;
1399         rxq->nb_rx_desc = nb_desc;
1400         rxq->pthresh = rx_conf->rx_thresh.pthresh;
1401         rxq->hthresh = rx_conf->rx_thresh.hthresh;
1402         rxq->wthresh = rx_conf->rx_thresh.wthresh;
1403         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
1404         rxq->queue_id = queue_idx;
1405         rxq->port_id = dev->data->port_id;
1406         rxq->crc_len = (uint8_t) ((dev->data->dev_conf.rxmode.hw_strip_crc) ?
1407                                 0 : ETHER_CRC_LEN);
1408
1409         rxq->rdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDT(queue_idx));
1410         rxq->rdh_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDH(queue_idx));
1411 #ifndef RTE_LIBRTE_XEN_DOM0
1412         rxq->rx_ring_phys_addr = (uint64_t) rz->phys_addr;
1413 #else
1414         rxq->rx_ring_phys_addr = rte_mem_phy2mch(rz->memseg_id, rz->phys_addr);
1415 #endif
1416         rxq->rx_ring = (struct e1000_rx_desc *) rz->addr;
1417
1418         PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64"\n",
1419                 rxq->sw_ring, rxq->rx_ring, rxq->rx_ring_phys_addr);
1420
1421         dev->data->rx_queues[queue_idx] = rxq;
1422         em_reset_rx_queue(rxq);
1423
1424         return (0);
1425 }
1426
1427 uint32_t
1428 eth_em_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1429 {
1430 #define EM_RXQ_SCAN_INTERVAL 4
1431         volatile struct e1000_rx_desc *rxdp;
1432         struct em_rx_queue *rxq;
1433         uint32_t desc = 0;
1434
1435         if (rx_queue_id >= dev->data->nb_rx_queues) {
1436                 PMD_RX_LOG(DEBUG,"Invalid RX queue_id=%d\n", rx_queue_id);
1437                 return 0;
1438         }
1439
1440         rxq = dev->data->rx_queues[rx_queue_id];
1441         rxdp = &(rxq->rx_ring[rxq->rx_tail]);
1442
1443         while ((desc < rxq->nb_rx_desc) &&
1444                 (rxdp->status & E1000_RXD_STAT_DD)) {
1445                 desc += EM_RXQ_SCAN_INTERVAL;
1446                 rxdp += EM_RXQ_SCAN_INTERVAL;
1447                 if (rxq->rx_tail + desc >= rxq->nb_rx_desc)
1448                         rxdp = &(rxq->rx_ring[rxq->rx_tail +
1449                                 desc - rxq->nb_rx_desc]);
1450         }
1451
1452         return desc;
1453 }
1454
1455 int
1456 eth_em_rx_descriptor_done(void *rx_queue, uint16_t offset)
1457 {
1458         volatile struct e1000_rx_desc *rxdp;
1459         struct em_rx_queue *rxq = rx_queue;
1460         uint32_t desc;
1461
1462         if (unlikely(offset >= rxq->nb_rx_desc))
1463                 return 0;
1464         desc = rxq->rx_tail + offset;
1465         if (desc >= rxq->nb_rx_desc)
1466                 desc -= rxq->nb_rx_desc;
1467
1468         rxdp = &rxq->rx_ring[desc];
1469         return !!(rxdp->status & E1000_RXD_STAT_DD);
1470 }
1471
1472 void
1473 em_dev_clear_queues(struct rte_eth_dev *dev)
1474 {
1475         uint16_t i;
1476         struct em_tx_queue *txq;
1477         struct em_rx_queue *rxq;
1478
1479         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1480                 txq = dev->data->tx_queues[i];
1481                 if (txq != NULL) {
1482                         em_tx_queue_release_mbufs(txq);
1483                         em_reset_tx_queue(txq);
1484                 }
1485         }
1486
1487         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1488                 rxq = dev->data->rx_queues[i];
1489                 if (rxq != NULL) {
1490                         em_rx_queue_release_mbufs(rxq);
1491                         em_reset_rx_queue(rxq);
1492                 }
1493         }
1494 }
1495
1496 /*
1497  * Takes as input/output parameter RX buffer size.
1498  * Returns (BSIZE | BSEX | FLXBUF) fields of RCTL register.
1499  */
1500 static uint32_t
1501 em_rctl_bsize(__rte_unused enum e1000_mac_type hwtyp, uint32_t *bufsz)
1502 {
1503         /*
1504          * For BSIZE & BSEX all configurable sizes are:
1505          * 16384: rctl |= (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX);
1506          *  8192: rctl |= (E1000_RCTL_SZ_8192  | E1000_RCTL_BSEX);
1507          *  4096: rctl |= (E1000_RCTL_SZ_4096  | E1000_RCTL_BSEX);
1508          *  2048: rctl |= E1000_RCTL_SZ_2048;
1509          *  1024: rctl |= E1000_RCTL_SZ_1024;
1510          *   512: rctl |= E1000_RCTL_SZ_512;
1511          *   256: rctl |= E1000_RCTL_SZ_256;
1512          */
1513         static const struct {
1514                 uint32_t bufsz;
1515                 uint32_t rctl;
1516         } bufsz_to_rctl[] = {
1517                 {16384, (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX)},
1518                 {8192,  (E1000_RCTL_SZ_8192  | E1000_RCTL_BSEX)},
1519                 {4096,  (E1000_RCTL_SZ_4096  | E1000_RCTL_BSEX)},
1520                 {2048,  E1000_RCTL_SZ_2048},
1521                 {1024,  E1000_RCTL_SZ_1024},
1522                 {512,   E1000_RCTL_SZ_512},
1523                 {256,   E1000_RCTL_SZ_256},
1524         };
1525
1526         int i;
1527         uint32_t rctl_bsize;
1528
1529         rctl_bsize = *bufsz;
1530
1531         /*
1532          * Starting from 82571 it is possible to specify RX buffer size
1533          * by RCTL.FLXBUF. When this field is different from zero, the
1534          * RX buffer size = RCTL.FLXBUF * 1K
1535          * (e.g. t is possible to specify RX buffer size  1,2,...,15KB).
1536          * It is working ok on real HW, but by some reason doesn't work
1537          * on VMware emulated 82574L.
1538          * So for now, always use BSIZE/BSEX to setup RX buffer size.
1539          * If you don't plan to use it on VMware emulated 82574L and
1540          * would like to specify RX buffer size in 1K granularity,
1541          * uncomment the following lines:
1542          * ***************************************************************
1543          * if (hwtyp >= e1000_82571 && hwtyp <= e1000_82574 &&
1544          *              rctl_bsize >= EM_RCTL_FLXBUF_STEP) {
1545          *      rctl_bsize /= EM_RCTL_FLXBUF_STEP;
1546          *      *bufsz = rctl_bsize;
1547          *      return (rctl_bsize << E1000_RCTL_FLXBUF_SHIFT &
1548          *              E1000_RCTL_FLXBUF_MASK);
1549          * }
1550          * ***************************************************************
1551          */
1552
1553         for (i = 0; i != sizeof(bufsz_to_rctl) / sizeof(bufsz_to_rctl[0]);
1554                         i++) {
1555                 if (rctl_bsize >= bufsz_to_rctl[i].bufsz) {
1556                         *bufsz = bufsz_to_rctl[i].bufsz;
1557                         return (bufsz_to_rctl[i].rctl);
1558                 }
1559         }
1560
1561         /* Should never happen. */
1562         return (-EINVAL);
1563 }
1564
1565 static int
1566 em_alloc_rx_queue_mbufs(struct em_rx_queue *rxq)
1567 {
1568         struct em_rx_entry *rxe = rxq->sw_ring;
1569         uint64_t dma_addr;
1570         unsigned i;
1571         static const struct e1000_rx_desc rxd_init = {
1572                 .buffer_addr = 0,
1573         };
1574
1575         /* Initialize software ring entries */
1576         for (i = 0; i < rxq->nb_rx_desc; i++) {
1577                 volatile struct e1000_rx_desc *rxd;
1578                 struct rte_mbuf *mbuf = rte_rxmbuf_alloc(rxq->mb_pool);
1579
1580                 if (mbuf == NULL) {
1581                         PMD_INIT_LOG(ERR, "RX mbuf alloc failed "
1582                                 "queue_id=%hu\n", rxq->queue_id);
1583                         return (-ENOMEM);
1584                 }
1585
1586                 dma_addr = rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR_DEFAULT(mbuf));
1587
1588                 /* Clear HW ring memory */
1589                 rxq->rx_ring[i] = rxd_init;
1590
1591                 rxd = &rxq->rx_ring[i];
1592                 rxd->buffer_addr = dma_addr;
1593                 rxe[i].mbuf = mbuf;
1594         }
1595
1596         return 0;
1597 }
1598
1599 /*********************************************************************
1600  *
1601  *  Enable receive unit.
1602  *
1603  **********************************************************************/
1604 int
1605 eth_em_rx_init(struct rte_eth_dev *dev)
1606 {
1607         struct e1000_hw *hw;
1608         struct em_rx_queue *rxq;
1609         uint32_t rctl;
1610         uint32_t rfctl;
1611         uint32_t rxcsum;
1612         uint32_t rctl_bsize;
1613         uint16_t i;
1614         int ret;
1615
1616         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1617
1618         /*
1619          * Make sure receives are disabled while setting
1620          * up the descriptor ring.
1621          */
1622         rctl = E1000_READ_REG(hw, E1000_RCTL);
1623         E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
1624
1625         rfctl = E1000_READ_REG(hw, E1000_RFCTL);
1626
1627         /* Disable extended descriptor type. */
1628         rfctl &= ~E1000_RFCTL_EXTEN;
1629         /* Disable accelerated acknowledge */
1630         if (hw->mac.type == e1000_82574)
1631                 rfctl |= E1000_RFCTL_ACK_DIS;
1632
1633         E1000_WRITE_REG(hw, E1000_RFCTL, rfctl);
1634
1635         /*
1636          * XXX TEMPORARY WORKAROUND: on some systems with 82573
1637          * long latencies are observed, like Lenovo X60. This
1638          * change eliminates the problem, but since having positive
1639          * values in RDTR is a known source of problems on other
1640          * platforms another solution is being sought.
1641          */
1642         if (hw->mac.type == e1000_82573)
1643                 E1000_WRITE_REG(hw, E1000_RDTR, 0x20);
1644
1645         dev->rx_pkt_burst = (eth_rx_burst_t)eth_em_recv_pkts;
1646
1647         /* Determine RX bufsize. */
1648         rctl_bsize = EM_MAX_BUF_SIZE;
1649         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1650                 struct rte_pktmbuf_pool_private *mbp_priv;
1651                 uint32_t buf_size;
1652
1653                 rxq = dev->data->rx_queues[i];
1654                 mbp_priv = rte_mempool_get_priv(rxq->mb_pool);
1655                 buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
1656                 rctl_bsize = RTE_MIN(rctl_bsize, buf_size);
1657         }
1658
1659         rctl |= em_rctl_bsize(hw->mac.type, &rctl_bsize);
1660
1661         /* Configure and enable each RX queue. */
1662         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1663                 uint64_t bus_addr;
1664                 uint32_t rxdctl;
1665
1666                 rxq = dev->data->rx_queues[i];
1667
1668                 /* Allocate buffers for descriptor rings and setup queue */
1669                 ret = em_alloc_rx_queue_mbufs(rxq);
1670                 if (ret)
1671                         return ret;
1672
1673                 /*
1674                  * Reset crc_len in case it was changed after queue setup by a
1675                  *  call to configure
1676                  */
1677                 rxq->crc_len =
1678                         (uint8_t)(dev->data->dev_conf.rxmode.hw_strip_crc ?
1679                                                         0 : ETHER_CRC_LEN);
1680
1681                 bus_addr = rxq->rx_ring_phys_addr;
1682                 E1000_WRITE_REG(hw, E1000_RDLEN(i),
1683                                 rxq->nb_rx_desc *
1684                                 sizeof(*rxq->rx_ring));
1685                 E1000_WRITE_REG(hw, E1000_RDBAH(i),
1686                                 (uint32_t)(bus_addr >> 32));
1687                 E1000_WRITE_REG(hw, E1000_RDBAL(i), (uint32_t)bus_addr);
1688
1689                 E1000_WRITE_REG(hw, E1000_RDH(i), 0);
1690                 E1000_WRITE_REG(hw, E1000_RDT(i), rxq->nb_rx_desc - 1);
1691
1692                 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1693                 rxdctl &= 0xFE000000;
1694                 rxdctl |= rxq->pthresh & 0x3F;
1695                 rxdctl |= (rxq->hthresh & 0x3F) << 8;
1696                 rxdctl |= (rxq->wthresh & 0x3F) << 16;
1697                 rxdctl |= E1000_RXDCTL_GRAN;
1698                 E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
1699
1700                 /*
1701                  * Due to EM devices not having any sort of hardware
1702                  * limit for packet length, jumbo frame of any size
1703                  * can be accepted, thus we have to enable scattered
1704                  * rx if jumbo frames are enabled (or if buffer size
1705                  * is too small to accommodate non-jumbo packets)
1706                  * to avoid splitting packets that don't fit into
1707                  * one buffer.
1708                  */
1709                 if (dev->data->dev_conf.rxmode.jumbo_frame ||
1710                                 rctl_bsize < ETHER_MAX_LEN) {
1711                         dev->rx_pkt_burst =
1712                                 (eth_rx_burst_t)eth_em_recv_scattered_pkts;
1713                         dev->data->scattered_rx = 1;
1714                 }
1715         }
1716
1717         if (dev->data->dev_conf.rxmode.enable_scatter) {
1718                 dev->rx_pkt_burst = eth_em_recv_scattered_pkts;
1719                 dev->data->scattered_rx = 1;
1720         }
1721
1722         /*
1723          * Setup the Checksum Register.
1724          * Receive Full-Packet Checksum Offload is mutually exclusive with RSS.
1725          */
1726         rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
1727
1728         if (dev->data->dev_conf.rxmode.hw_ip_checksum)
1729                 rxcsum |= E1000_RXCSUM_IPOFL;
1730         else
1731                 rxcsum &= ~E1000_RXCSUM_IPOFL;
1732         E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
1733
1734         /* No MRQ or RSS support for now */
1735
1736         /* Set early receive threshold on appropriate hw */
1737         if ((hw->mac.type == e1000_ich9lan ||
1738                         hw->mac.type == e1000_pch2lan ||
1739                         hw->mac.type == e1000_ich10lan) &&
1740                         dev->data->dev_conf.rxmode.jumbo_frame == 1) {
1741                 u32 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1742                 E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl | 3);
1743                 E1000_WRITE_REG(hw, E1000_ERT, 0x100 | (1 << 13));
1744         }
1745
1746         if (hw->mac.type == e1000_pch2lan) {
1747                 if (dev->data->dev_conf.rxmode.jumbo_frame == 1)
1748                         e1000_lv_jumbo_workaround_ich8lan(hw, TRUE);
1749                 else
1750                         e1000_lv_jumbo_workaround_ich8lan(hw, FALSE);
1751         }
1752
1753         /* Setup the Receive Control Register. */
1754         if (dev->data->dev_conf.rxmode.hw_strip_crc)
1755                 rctl |= E1000_RCTL_SECRC; /* Strip Ethernet CRC. */
1756         else
1757                 rctl &= ~E1000_RCTL_SECRC; /* Do not Strip Ethernet CRC. */
1758
1759         rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1760         rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
1761                 E1000_RCTL_RDMTS_HALF |
1762                 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1763
1764         /* Make sure VLAN Filters are off. */
1765         rctl &= ~E1000_RCTL_VFE;
1766         /* Don't store bad packets. */
1767         rctl &= ~E1000_RCTL_SBP;
1768         /* Legacy descriptor type. */
1769         rctl &= ~E1000_RCTL_DTYP_MASK;
1770
1771         /*
1772          * Configure support of jumbo frames, if any.
1773          */
1774         if (dev->data->dev_conf.rxmode.jumbo_frame == 1)
1775                 rctl |= E1000_RCTL_LPE;
1776         else
1777                 rctl &= ~E1000_RCTL_LPE;
1778
1779         /* Enable Receives. */
1780         E1000_WRITE_REG(hw, E1000_RCTL, rctl);
1781
1782         return 0;
1783 }
1784
1785 /*********************************************************************
1786  *
1787  *  Enable transmit unit.
1788  *
1789  **********************************************************************/
1790 void
1791 eth_em_tx_init(struct rte_eth_dev *dev)
1792 {
1793         struct e1000_hw     *hw;
1794         struct em_tx_queue *txq;
1795         uint32_t tctl;
1796         uint32_t txdctl;
1797         uint16_t i;
1798
1799         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1800
1801         /* Setup the Base and Length of the Tx Descriptor Rings. */
1802         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1803                 uint64_t bus_addr;
1804
1805                 txq = dev->data->tx_queues[i];
1806                 bus_addr = txq->tx_ring_phys_addr;
1807                 E1000_WRITE_REG(hw, E1000_TDLEN(i),
1808                                 txq->nb_tx_desc *
1809                                 sizeof(*txq->tx_ring));
1810                 E1000_WRITE_REG(hw, E1000_TDBAH(i),
1811                                 (uint32_t)(bus_addr >> 32));
1812                 E1000_WRITE_REG(hw, E1000_TDBAL(i), (uint32_t)bus_addr);
1813
1814                 /* Setup the HW Tx Head and Tail descriptor pointers. */
1815                 E1000_WRITE_REG(hw, E1000_TDT(i), 0);
1816                 E1000_WRITE_REG(hw, E1000_TDH(i), 0);
1817
1818                 /* Setup Transmit threshold registers. */
1819                 txdctl = E1000_READ_REG(hw, E1000_TXDCTL(i));
1820                 /*
1821                  * bit 22 is reserved, on some models should always be 0,
1822                  * on others  - always 1.
1823                  */
1824                 txdctl &= E1000_TXDCTL_COUNT_DESC;
1825                 txdctl |= txq->pthresh & 0x3F;
1826                 txdctl |= (txq->hthresh & 0x3F) << 8;
1827                 txdctl |= (txq->wthresh & 0x3F) << 16;
1828                 txdctl |= E1000_TXDCTL_GRAN;
1829                 E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
1830         }
1831
1832         /* Program the Transmit Control Register. */
1833         tctl = E1000_READ_REG(hw, E1000_TCTL);
1834         tctl &= ~E1000_TCTL_CT;
1835         tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
1836                  (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
1837
1838         /* This write will effectively turn on the transmit unit. */
1839         E1000_WRITE_REG(hw, E1000_TCTL, tctl);
1840 }
1841