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