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