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