New upstream version 18.11-rc1
[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_MULTI_SEGS  |
1164                 DEV_TX_OFFLOAD_VLAN_INSERT |
1165                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
1166                 DEV_TX_OFFLOAD_UDP_CKSUM   |
1167                 DEV_TX_OFFLOAD_TCP_CKSUM;
1168
1169         return tx_offload_capa;
1170 }
1171
1172 uint64_t
1173 em_get_tx_queue_offloads_capa(struct rte_eth_dev *dev)
1174 {
1175         uint64_t tx_queue_offload_capa;
1176
1177         /*
1178          * As only one Tx queue can be used, let per queue offloading
1179          * capability be same to per port queue offloading capability
1180          * for better convenience.
1181          */
1182         tx_queue_offload_capa = em_get_tx_port_offloads_capa(dev);
1183
1184         return tx_queue_offload_capa;
1185 }
1186
1187 int
1188 eth_em_tx_queue_setup(struct rte_eth_dev *dev,
1189                          uint16_t queue_idx,
1190                          uint16_t nb_desc,
1191                          unsigned int socket_id,
1192                          const struct rte_eth_txconf *tx_conf)
1193 {
1194         const struct rte_memzone *tz;
1195         struct em_tx_queue *txq;
1196         struct e1000_hw     *hw;
1197         uint32_t tsize;
1198         uint16_t tx_rs_thresh, tx_free_thresh;
1199         uint64_t offloads;
1200
1201         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1202
1203         offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
1204
1205         /*
1206          * Validate number of transmit descriptors.
1207          * It must not exceed hardware maximum, and must be multiple
1208          * of E1000_ALIGN.
1209          */
1210         if (nb_desc % EM_TXD_ALIGN != 0 ||
1211                         (nb_desc > E1000_MAX_RING_DESC) ||
1212                         (nb_desc < E1000_MIN_RING_DESC)) {
1213                 return -(EINVAL);
1214         }
1215
1216         tx_free_thresh = tx_conf->tx_free_thresh;
1217         if (tx_free_thresh == 0)
1218                 tx_free_thresh = (uint16_t)RTE_MIN(nb_desc / 4,
1219                                         DEFAULT_TX_FREE_THRESH);
1220
1221         tx_rs_thresh = tx_conf->tx_rs_thresh;
1222         if (tx_rs_thresh == 0)
1223                 tx_rs_thresh = (uint16_t)RTE_MIN(tx_free_thresh,
1224                                         DEFAULT_TX_RS_THRESH);
1225
1226         if (tx_free_thresh >= (nb_desc - 3)) {
1227                 PMD_INIT_LOG(ERR, "tx_free_thresh must be less than the "
1228                              "number of TX descriptors minus 3. "
1229                              "(tx_free_thresh=%u port=%d queue=%d)",
1230                              (unsigned int)tx_free_thresh,
1231                              (int)dev->data->port_id, (int)queue_idx);
1232                 return -(EINVAL);
1233         }
1234         if (tx_rs_thresh > tx_free_thresh) {
1235                 PMD_INIT_LOG(ERR, "tx_rs_thresh must be less than or equal to "
1236                              "tx_free_thresh. (tx_free_thresh=%u "
1237                              "tx_rs_thresh=%u port=%d queue=%d)",
1238                              (unsigned int)tx_free_thresh,
1239                              (unsigned int)tx_rs_thresh,
1240                              (int)dev->data->port_id,
1241                              (int)queue_idx);
1242                 return -(EINVAL);
1243         }
1244
1245         /*
1246          * If rs_bit_thresh is greater than 1, then TX WTHRESH should be
1247          * set to 0. If WTHRESH is greater than zero, the RS bit is ignored
1248          * by the NIC and all descriptors are written back after the NIC
1249          * accumulates WTHRESH descriptors.
1250          */
1251         if (tx_conf->tx_thresh.wthresh != 0 && tx_rs_thresh != 1) {
1252                 PMD_INIT_LOG(ERR, "TX WTHRESH must be set to 0 if "
1253                              "tx_rs_thresh is greater than 1. (tx_rs_thresh=%u "
1254                              "port=%d queue=%d)", (unsigned int)tx_rs_thresh,
1255                              (int)dev->data->port_id, (int)queue_idx);
1256                 return -(EINVAL);
1257         }
1258
1259         /* Free memory prior to re-allocation if needed... */
1260         if (dev->data->tx_queues[queue_idx] != NULL) {
1261                 em_tx_queue_release(dev->data->tx_queues[queue_idx]);
1262                 dev->data->tx_queues[queue_idx] = NULL;
1263         }
1264
1265         /*
1266          * Allocate TX ring hardware descriptors. A memzone large enough to
1267          * handle the maximum ring size is allocated in order to allow for
1268          * resizing in later calls to the queue setup function.
1269          */
1270         tsize = sizeof(txq->tx_ring[0]) * E1000_MAX_RING_DESC;
1271         tz = rte_eth_dma_zone_reserve(dev, "tx_ring", queue_idx, tsize,
1272                                       RTE_CACHE_LINE_SIZE, socket_id);
1273         if (tz == NULL)
1274                 return -ENOMEM;
1275
1276         /* Allocate the tx queue data structure. */
1277         if ((txq = rte_zmalloc("ethdev TX queue", sizeof(*txq),
1278                         RTE_CACHE_LINE_SIZE)) == NULL)
1279                 return -ENOMEM;
1280
1281         /* Allocate software ring */
1282         if ((txq->sw_ring = rte_zmalloc("txq->sw_ring",
1283                         sizeof(txq->sw_ring[0]) * nb_desc,
1284                         RTE_CACHE_LINE_SIZE)) == NULL) {
1285                 em_tx_queue_release(txq);
1286                 return -ENOMEM;
1287         }
1288
1289         txq->nb_tx_desc = nb_desc;
1290         txq->tx_free_thresh = tx_free_thresh;
1291         txq->tx_rs_thresh = tx_rs_thresh;
1292         txq->pthresh = tx_conf->tx_thresh.pthresh;
1293         txq->hthresh = tx_conf->tx_thresh.hthresh;
1294         txq->wthresh = tx_conf->tx_thresh.wthresh;
1295         txq->queue_id = queue_idx;
1296         txq->port_id = dev->data->port_id;
1297
1298         txq->tdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_TDT(queue_idx));
1299         txq->tx_ring_phys_addr = tz->iova;
1300         txq->tx_ring = (struct e1000_data_desc *) tz->addr;
1301
1302         PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
1303                      txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr);
1304
1305         em_reset_tx_queue(txq);
1306
1307         dev->data->tx_queues[queue_idx] = txq;
1308         txq->offloads = offloads;
1309         return 0;
1310 }
1311
1312 static void
1313 em_rx_queue_release_mbufs(struct em_rx_queue *rxq)
1314 {
1315         unsigned i;
1316
1317         if (rxq->sw_ring != NULL) {
1318                 for (i = 0; i != rxq->nb_rx_desc; i++) {
1319                         if (rxq->sw_ring[i].mbuf != NULL) {
1320                                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
1321                                 rxq->sw_ring[i].mbuf = NULL;
1322                         }
1323                 }
1324         }
1325 }
1326
1327 static void
1328 em_rx_queue_release(struct em_rx_queue *rxq)
1329 {
1330         if (rxq != NULL) {
1331                 em_rx_queue_release_mbufs(rxq);
1332                 rte_free(rxq->sw_ring);
1333                 rte_free(rxq);
1334         }
1335 }
1336
1337 void
1338 eth_em_rx_queue_release(void *rxq)
1339 {
1340         em_rx_queue_release(rxq);
1341 }
1342
1343 /* Reset dynamic em_rx_queue fields back to defaults */
1344 static void
1345 em_reset_rx_queue(struct em_rx_queue *rxq)
1346 {
1347         rxq->rx_tail = 0;
1348         rxq->nb_rx_hold = 0;
1349         rxq->pkt_first_seg = NULL;
1350         rxq->pkt_last_seg = NULL;
1351 }
1352
1353 uint64_t
1354 em_get_rx_port_offloads_capa(struct rte_eth_dev *dev)
1355 {
1356         uint64_t rx_offload_capa;
1357         uint32_t max_rx_pktlen;
1358
1359         max_rx_pktlen = em_get_max_pktlen(dev);
1360
1361         rx_offload_capa =
1362                 DEV_RX_OFFLOAD_VLAN_STRIP  |
1363                 DEV_RX_OFFLOAD_VLAN_FILTER |
1364                 DEV_RX_OFFLOAD_IPV4_CKSUM  |
1365                 DEV_RX_OFFLOAD_UDP_CKSUM   |
1366                 DEV_RX_OFFLOAD_TCP_CKSUM   |
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          * It's an optimization that does nothing on single-queue devices,
1422          * so just log the issue and carry on.
1423          */
1424         if (rx_conf->rx_drop_en) {
1425                 PMD_INIT_LOG(NOTICE, "drop_en functionality not supported by "
1426                              "device");
1427         }
1428
1429         /* Free memory prior to re-allocation if needed. */
1430         if (dev->data->rx_queues[queue_idx] != NULL) {
1431                 em_rx_queue_release(dev->data->rx_queues[queue_idx]);
1432                 dev->data->rx_queues[queue_idx] = NULL;
1433         }
1434
1435         /* Allocate RX ring for max possible mumber of hardware descriptors. */
1436         rsize = sizeof(rxq->rx_ring[0]) * E1000_MAX_RING_DESC;
1437         rz = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx, rsize,
1438                                       RTE_CACHE_LINE_SIZE, socket_id);
1439         if (rz == NULL)
1440                 return -ENOMEM;
1441
1442         /* Allocate the RX queue data structure. */
1443         if ((rxq = rte_zmalloc("ethdev RX queue", sizeof(*rxq),
1444                         RTE_CACHE_LINE_SIZE)) == NULL)
1445                 return -ENOMEM;
1446
1447         /* Allocate software ring. */
1448         if ((rxq->sw_ring = rte_zmalloc("rxq->sw_ring",
1449                         sizeof (rxq->sw_ring[0]) * nb_desc,
1450                         RTE_CACHE_LINE_SIZE)) == NULL) {
1451                 em_rx_queue_release(rxq);
1452                 return -ENOMEM;
1453         }
1454
1455         rxq->mb_pool = mp;
1456         rxq->nb_rx_desc = nb_desc;
1457         rxq->pthresh = rx_conf->rx_thresh.pthresh;
1458         rxq->hthresh = rx_conf->rx_thresh.hthresh;
1459         rxq->wthresh = rx_conf->rx_thresh.wthresh;
1460         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
1461         rxq->queue_id = queue_idx;
1462         rxq->port_id = dev->data->port_id;
1463         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
1464                 rxq->crc_len = ETHER_CRC_LEN;
1465         else
1466                 rxq->crc_len = 0;
1467
1468         rxq->rdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDT(queue_idx));
1469         rxq->rdh_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDH(queue_idx));
1470         rxq->rx_ring_phys_addr = rz->iova;
1471         rxq->rx_ring = (struct e1000_rx_desc *) rz->addr;
1472
1473         PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
1474                      rxq->sw_ring, rxq->rx_ring, rxq->rx_ring_phys_addr);
1475
1476         dev->data->rx_queues[queue_idx] = rxq;
1477         em_reset_rx_queue(rxq);
1478         rxq->offloads = offloads;
1479
1480         return 0;
1481 }
1482
1483 uint32_t
1484 eth_em_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1485 {
1486 #define EM_RXQ_SCAN_INTERVAL 4
1487         volatile struct e1000_rx_desc *rxdp;
1488         struct em_rx_queue *rxq;
1489         uint32_t desc = 0;
1490
1491         rxq = dev->data->rx_queues[rx_queue_id];
1492         rxdp = &(rxq->rx_ring[rxq->rx_tail]);
1493
1494         while ((desc < rxq->nb_rx_desc) &&
1495                 (rxdp->status & E1000_RXD_STAT_DD)) {
1496                 desc += EM_RXQ_SCAN_INTERVAL;
1497                 rxdp += EM_RXQ_SCAN_INTERVAL;
1498                 if (rxq->rx_tail + desc >= rxq->nb_rx_desc)
1499                         rxdp = &(rxq->rx_ring[rxq->rx_tail +
1500                                 desc - rxq->nb_rx_desc]);
1501         }
1502
1503         return desc;
1504 }
1505
1506 int
1507 eth_em_rx_descriptor_done(void *rx_queue, uint16_t offset)
1508 {
1509         volatile struct e1000_rx_desc *rxdp;
1510         struct em_rx_queue *rxq = rx_queue;
1511         uint32_t desc;
1512
1513         if (unlikely(offset >= rxq->nb_rx_desc))
1514                 return 0;
1515         desc = rxq->rx_tail + offset;
1516         if (desc >= rxq->nb_rx_desc)
1517                 desc -= rxq->nb_rx_desc;
1518
1519         rxdp = &rxq->rx_ring[desc];
1520         return !!(rxdp->status & E1000_RXD_STAT_DD);
1521 }
1522
1523 int
1524 eth_em_rx_descriptor_status(void *rx_queue, uint16_t offset)
1525 {
1526         struct em_rx_queue *rxq = rx_queue;
1527         volatile uint8_t *status;
1528         uint32_t desc;
1529
1530         if (unlikely(offset >= rxq->nb_rx_desc))
1531                 return -EINVAL;
1532
1533         if (offset >= rxq->nb_rx_desc - rxq->nb_rx_hold)
1534                 return RTE_ETH_RX_DESC_UNAVAIL;
1535
1536         desc = rxq->rx_tail + offset;
1537         if (desc >= rxq->nb_rx_desc)
1538                 desc -= rxq->nb_rx_desc;
1539
1540         status = &rxq->rx_ring[desc].status;
1541         if (*status & E1000_RXD_STAT_DD)
1542                 return RTE_ETH_RX_DESC_DONE;
1543
1544         return RTE_ETH_RX_DESC_AVAIL;
1545 }
1546
1547 int
1548 eth_em_tx_descriptor_status(void *tx_queue, uint16_t offset)
1549 {
1550         struct em_tx_queue *txq = tx_queue;
1551         volatile uint8_t *status;
1552         uint32_t desc;
1553
1554         if (unlikely(offset >= txq->nb_tx_desc))
1555                 return -EINVAL;
1556
1557         desc = txq->tx_tail + offset;
1558         /* go to next desc that has the RS bit */
1559         desc = ((desc + txq->tx_rs_thresh - 1) / txq->tx_rs_thresh) *
1560                 txq->tx_rs_thresh;
1561         if (desc >= txq->nb_tx_desc) {
1562                 desc -= txq->nb_tx_desc;
1563                 if (desc >= txq->nb_tx_desc)
1564                         desc -= txq->nb_tx_desc;
1565         }
1566
1567         status = &txq->tx_ring[desc].upper.fields.status;
1568         if (*status & E1000_TXD_STAT_DD)
1569                 return RTE_ETH_TX_DESC_DONE;
1570
1571         return RTE_ETH_TX_DESC_FULL;
1572 }
1573
1574 void
1575 em_dev_clear_queues(struct rte_eth_dev *dev)
1576 {
1577         uint16_t i;
1578         struct em_tx_queue *txq;
1579         struct em_rx_queue *rxq;
1580
1581         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1582                 txq = dev->data->tx_queues[i];
1583                 if (txq != NULL) {
1584                         em_tx_queue_release_mbufs(txq);
1585                         em_reset_tx_queue(txq);
1586                 }
1587         }
1588
1589         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1590                 rxq = dev->data->rx_queues[i];
1591                 if (rxq != NULL) {
1592                         em_rx_queue_release_mbufs(rxq);
1593                         em_reset_rx_queue(rxq);
1594                 }
1595         }
1596 }
1597
1598 void
1599 em_dev_free_queues(struct rte_eth_dev *dev)
1600 {
1601         uint16_t i;
1602
1603         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1604                 eth_em_rx_queue_release(dev->data->rx_queues[i]);
1605                 dev->data->rx_queues[i] = NULL;
1606         }
1607         dev->data->nb_rx_queues = 0;
1608
1609         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1610                 eth_em_tx_queue_release(dev->data->tx_queues[i]);
1611                 dev->data->tx_queues[i] = NULL;
1612         }
1613         dev->data->nb_tx_queues = 0;
1614 }
1615
1616 /*
1617  * Takes as input/output parameter RX buffer size.
1618  * Returns (BSIZE | BSEX | FLXBUF) fields of RCTL register.
1619  */
1620 static uint32_t
1621 em_rctl_bsize(__rte_unused enum e1000_mac_type hwtyp, uint32_t *bufsz)
1622 {
1623         /*
1624          * For BSIZE & BSEX all configurable sizes are:
1625          * 16384: rctl |= (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX);
1626          *  8192: rctl |= (E1000_RCTL_SZ_8192  | E1000_RCTL_BSEX);
1627          *  4096: rctl |= (E1000_RCTL_SZ_4096  | E1000_RCTL_BSEX);
1628          *  2048: rctl |= E1000_RCTL_SZ_2048;
1629          *  1024: rctl |= E1000_RCTL_SZ_1024;
1630          *   512: rctl |= E1000_RCTL_SZ_512;
1631          *   256: rctl |= E1000_RCTL_SZ_256;
1632          */
1633         static const struct {
1634                 uint32_t bufsz;
1635                 uint32_t rctl;
1636         } bufsz_to_rctl[] = {
1637                 {16384, (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX)},
1638                 {8192,  (E1000_RCTL_SZ_8192  | E1000_RCTL_BSEX)},
1639                 {4096,  (E1000_RCTL_SZ_4096  | E1000_RCTL_BSEX)},
1640                 {2048,  E1000_RCTL_SZ_2048},
1641                 {1024,  E1000_RCTL_SZ_1024},
1642                 {512,   E1000_RCTL_SZ_512},
1643                 {256,   E1000_RCTL_SZ_256},
1644         };
1645
1646         int i;
1647         uint32_t rctl_bsize;
1648
1649         rctl_bsize = *bufsz;
1650
1651         /*
1652          * Starting from 82571 it is possible to specify RX buffer size
1653          * by RCTL.FLXBUF. When this field is different from zero, the
1654          * RX buffer size = RCTL.FLXBUF * 1K
1655          * (e.g. t is possible to specify RX buffer size  1,2,...,15KB).
1656          * It is working ok on real HW, but by some reason doesn't work
1657          * on VMware emulated 82574L.
1658          * So for now, always use BSIZE/BSEX to setup RX buffer size.
1659          * If you don't plan to use it on VMware emulated 82574L and
1660          * would like to specify RX buffer size in 1K granularity,
1661          * uncomment the following lines:
1662          * ***************************************************************
1663          * if (hwtyp >= e1000_82571 && hwtyp <= e1000_82574 &&
1664          *              rctl_bsize >= EM_RCTL_FLXBUF_STEP) {
1665          *      rctl_bsize /= EM_RCTL_FLXBUF_STEP;
1666          *      *bufsz = rctl_bsize;
1667          *      return (rctl_bsize << E1000_RCTL_FLXBUF_SHIFT &
1668          *              E1000_RCTL_FLXBUF_MASK);
1669          * }
1670          * ***************************************************************
1671          */
1672
1673         for (i = 0; i != sizeof(bufsz_to_rctl) / sizeof(bufsz_to_rctl[0]);
1674                         i++) {
1675                 if (rctl_bsize >= bufsz_to_rctl[i].bufsz) {
1676                         *bufsz = bufsz_to_rctl[i].bufsz;
1677                         return bufsz_to_rctl[i].rctl;
1678                 }
1679         }
1680
1681         /* Should never happen. */
1682         return -EINVAL;
1683 }
1684
1685 static int
1686 em_alloc_rx_queue_mbufs(struct em_rx_queue *rxq)
1687 {
1688         struct em_rx_entry *rxe = rxq->sw_ring;
1689         uint64_t dma_addr;
1690         unsigned i;
1691         static const struct e1000_rx_desc rxd_init = {
1692                 .buffer_addr = 0,
1693         };
1694
1695         /* Initialize software ring entries */
1696         for (i = 0; i < rxq->nb_rx_desc; i++) {
1697                 volatile struct e1000_rx_desc *rxd;
1698                 struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
1699
1700                 if (mbuf == NULL) {
1701                         PMD_INIT_LOG(ERR, "RX mbuf alloc failed "
1702                                      "queue_id=%hu", rxq->queue_id);
1703                         return -ENOMEM;
1704                 }
1705
1706                 dma_addr =
1707                         rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
1708
1709                 /* Clear HW ring memory */
1710                 rxq->rx_ring[i] = rxd_init;
1711
1712                 rxd = &rxq->rx_ring[i];
1713                 rxd->buffer_addr = dma_addr;
1714                 rxe[i].mbuf = mbuf;
1715         }
1716
1717         return 0;
1718 }
1719
1720 /*********************************************************************
1721  *
1722  *  Enable receive unit.
1723  *
1724  **********************************************************************/
1725 int
1726 eth_em_rx_init(struct rte_eth_dev *dev)
1727 {
1728         struct e1000_hw *hw;
1729         struct em_rx_queue *rxq;
1730         struct rte_eth_rxmode *rxmode;
1731         uint32_t rctl;
1732         uint32_t rfctl;
1733         uint32_t rxcsum;
1734         uint32_t rctl_bsize;
1735         uint16_t i;
1736         int ret;
1737
1738         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1739         rxmode = &dev->data->dev_conf.rxmode;
1740
1741         /*
1742          * Make sure receives are disabled while setting
1743          * up the descriptor ring.
1744          */
1745         rctl = E1000_READ_REG(hw, E1000_RCTL);
1746         E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
1747
1748         rfctl = E1000_READ_REG(hw, E1000_RFCTL);
1749
1750         /* Disable extended descriptor type. */
1751         rfctl &= ~E1000_RFCTL_EXTEN;
1752         /* Disable accelerated acknowledge */
1753         if (hw->mac.type == e1000_82574)
1754                 rfctl |= E1000_RFCTL_ACK_DIS;
1755
1756         E1000_WRITE_REG(hw, E1000_RFCTL, rfctl);
1757
1758         /*
1759          * XXX TEMPORARY WORKAROUND: on some systems with 82573
1760          * long latencies are observed, like Lenovo X60. This
1761          * change eliminates the problem, but since having positive
1762          * values in RDTR is a known source of problems on other
1763          * platforms another solution is being sought.
1764          */
1765         if (hw->mac.type == e1000_82573)
1766                 E1000_WRITE_REG(hw, E1000_RDTR, 0x20);
1767
1768         dev->rx_pkt_burst = (eth_rx_burst_t)eth_em_recv_pkts;
1769
1770         /* Determine RX bufsize. */
1771         rctl_bsize = EM_MAX_BUF_SIZE;
1772         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1773                 uint32_t buf_size;
1774
1775                 rxq = dev->data->rx_queues[i];
1776                 buf_size = rte_pktmbuf_data_room_size(rxq->mb_pool) -
1777                         RTE_PKTMBUF_HEADROOM;
1778                 rctl_bsize = RTE_MIN(rctl_bsize, buf_size);
1779         }
1780
1781         rctl |= em_rctl_bsize(hw->mac.type, &rctl_bsize);
1782
1783         /* Configure and enable each RX queue. */
1784         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1785                 uint64_t bus_addr;
1786                 uint32_t rxdctl;
1787
1788                 rxq = dev->data->rx_queues[i];
1789
1790                 /* Allocate buffers for descriptor rings and setup queue */
1791                 ret = em_alloc_rx_queue_mbufs(rxq);
1792                 if (ret)
1793                         return ret;
1794
1795                 /*
1796                  * Reset crc_len in case it was changed after queue setup by a
1797                  *  call to configure
1798                  */
1799                 if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
1800                         rxq->crc_len = ETHER_CRC_LEN;
1801                 else
1802                         rxq->crc_len = 0;
1803
1804                 bus_addr = rxq->rx_ring_phys_addr;
1805                 E1000_WRITE_REG(hw, E1000_RDLEN(i),
1806                                 rxq->nb_rx_desc *
1807                                 sizeof(*rxq->rx_ring));
1808                 E1000_WRITE_REG(hw, E1000_RDBAH(i),
1809                                 (uint32_t)(bus_addr >> 32));
1810                 E1000_WRITE_REG(hw, E1000_RDBAL(i), (uint32_t)bus_addr);
1811
1812                 E1000_WRITE_REG(hw, E1000_RDH(i), 0);
1813                 E1000_WRITE_REG(hw, E1000_RDT(i), rxq->nb_rx_desc - 1);
1814
1815                 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1816                 rxdctl &= 0xFE000000;
1817                 rxdctl |= rxq->pthresh & 0x3F;
1818                 rxdctl |= (rxq->hthresh & 0x3F) << 8;
1819                 rxdctl |= (rxq->wthresh & 0x3F) << 16;
1820                 rxdctl |= E1000_RXDCTL_GRAN;
1821                 E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
1822
1823                 /*
1824                  * Due to EM devices not having any sort of hardware
1825                  * limit for packet length, jumbo frame of any size
1826                  * can be accepted, thus we have to enable scattered
1827                  * rx if jumbo frames are enabled (or if buffer size
1828                  * is too small to accommodate non-jumbo packets)
1829                  * to avoid splitting packets that don't fit into
1830                  * one buffer.
1831                  */
1832                 if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME ||
1833                                 rctl_bsize < ETHER_MAX_LEN) {
1834                         if (!dev->data->scattered_rx)
1835                                 PMD_INIT_LOG(DEBUG, "forcing scatter mode");
1836                         dev->rx_pkt_burst =
1837                                 (eth_rx_burst_t)eth_em_recv_scattered_pkts;
1838                         dev->data->scattered_rx = 1;
1839                 }
1840         }
1841
1842         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SCATTER) {
1843                 if (!dev->data->scattered_rx)
1844                         PMD_INIT_LOG(DEBUG, "forcing scatter mode");
1845                 dev->rx_pkt_burst = eth_em_recv_scattered_pkts;
1846                 dev->data->scattered_rx = 1;
1847         }
1848
1849         /*
1850          * Setup the Checksum Register.
1851          * Receive Full-Packet Checksum Offload is mutually exclusive with RSS.
1852          */
1853         rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
1854
1855         if (rxmode->offloads & DEV_RX_OFFLOAD_CHECKSUM)
1856                 rxcsum |= E1000_RXCSUM_IPOFL;
1857         else
1858                 rxcsum &= ~E1000_RXCSUM_IPOFL;
1859         E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
1860
1861         /* No MRQ or RSS support for now */
1862
1863         /* Set early receive threshold on appropriate hw */
1864         if ((hw->mac.type == e1000_ich9lan ||
1865                         hw->mac.type == e1000_pch2lan ||
1866                         hw->mac.type == e1000_ich10lan) &&
1867                         rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
1868                 u32 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1869                 E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl | 3);
1870                 E1000_WRITE_REG(hw, E1000_ERT, 0x100 | (1 << 13));
1871         }
1872
1873         if (hw->mac.type == e1000_pch2lan) {
1874                 if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
1875                         e1000_lv_jumbo_workaround_ich8lan(hw, TRUE);
1876                 else
1877                         e1000_lv_jumbo_workaround_ich8lan(hw, FALSE);
1878         }
1879
1880         /* Setup the Receive Control Register. */
1881         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
1882                 rctl &= ~E1000_RCTL_SECRC; /* Do not Strip Ethernet CRC. */
1883         else
1884                 rctl |= E1000_RCTL_SECRC; /* Strip Ethernet CRC. */
1885
1886         rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1887         rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
1888                 E1000_RCTL_RDMTS_HALF |
1889                 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1890
1891         /* Make sure VLAN Filters are off. */
1892         rctl &= ~E1000_RCTL_VFE;
1893         /* Don't store bad packets. */
1894         rctl &= ~E1000_RCTL_SBP;
1895         /* Legacy descriptor type. */
1896         rctl &= ~E1000_RCTL_DTYP_MASK;
1897
1898         /*
1899          * Configure support of jumbo frames, if any.
1900          */
1901         if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
1902                 rctl |= E1000_RCTL_LPE;
1903         else
1904                 rctl &= ~E1000_RCTL_LPE;
1905
1906         /* Enable Receives. */
1907         E1000_WRITE_REG(hw, E1000_RCTL, rctl);
1908
1909         return 0;
1910 }
1911
1912 /*********************************************************************
1913  *
1914  *  Enable transmit unit.
1915  *
1916  **********************************************************************/
1917 void
1918 eth_em_tx_init(struct rte_eth_dev *dev)
1919 {
1920         struct e1000_hw     *hw;
1921         struct em_tx_queue *txq;
1922         uint32_t tctl;
1923         uint32_t txdctl;
1924         uint16_t i;
1925
1926         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1927
1928         /* Setup the Base and Length of the Tx Descriptor Rings. */
1929         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1930                 uint64_t bus_addr;
1931
1932                 txq = dev->data->tx_queues[i];
1933                 bus_addr = txq->tx_ring_phys_addr;
1934                 E1000_WRITE_REG(hw, E1000_TDLEN(i),
1935                                 txq->nb_tx_desc *
1936                                 sizeof(*txq->tx_ring));
1937                 E1000_WRITE_REG(hw, E1000_TDBAH(i),
1938                                 (uint32_t)(bus_addr >> 32));
1939                 E1000_WRITE_REG(hw, E1000_TDBAL(i), (uint32_t)bus_addr);
1940
1941                 /* Setup the HW Tx Head and Tail descriptor pointers. */
1942                 E1000_WRITE_REG(hw, E1000_TDT(i), 0);
1943                 E1000_WRITE_REG(hw, E1000_TDH(i), 0);
1944
1945                 /* Setup Transmit threshold registers. */
1946                 txdctl = E1000_READ_REG(hw, E1000_TXDCTL(i));
1947                 /*
1948                  * bit 22 is reserved, on some models should always be 0,
1949                  * on others  - always 1.
1950                  */
1951                 txdctl &= E1000_TXDCTL_COUNT_DESC;
1952                 txdctl |= txq->pthresh & 0x3F;
1953                 txdctl |= (txq->hthresh & 0x3F) << 8;
1954                 txdctl |= (txq->wthresh & 0x3F) << 16;
1955                 txdctl |= E1000_TXDCTL_GRAN;
1956                 E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
1957         }
1958
1959         /* Program the Transmit Control Register. */
1960         tctl = E1000_READ_REG(hw, E1000_TCTL);
1961         tctl &= ~E1000_TCTL_CT;
1962         tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
1963                  (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
1964
1965         /* This write will effectively turn on the transmit unit. */
1966         E1000_WRITE_REG(hw, E1000_TCTL, tctl);
1967 }
1968
1969 void
1970 em_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
1971         struct rte_eth_rxq_info *qinfo)
1972 {
1973         struct em_rx_queue *rxq;
1974
1975         rxq = dev->data->rx_queues[queue_id];
1976
1977         qinfo->mp = rxq->mb_pool;
1978         qinfo->scattered_rx = dev->data->scattered_rx;
1979         qinfo->nb_desc = rxq->nb_rx_desc;
1980         qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
1981         qinfo->conf.offloads = rxq->offloads;
1982 }
1983
1984 void
1985 em_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
1986         struct rte_eth_txq_info *qinfo)
1987 {
1988         struct em_tx_queue *txq;
1989
1990         txq = dev->data->tx_queues[queue_id];
1991
1992         qinfo->nb_desc = txq->nb_tx_desc;
1993
1994         qinfo->conf.tx_thresh.pthresh = txq->pthresh;
1995         qinfo->conf.tx_thresh.hthresh = txq->hthresh;
1996         qinfo->conf.tx_thresh.wthresh = txq->wthresh;
1997         qinfo->conf.tx_free_thresh = txq->tx_free_thresh;
1998         qinfo->conf.tx_rs_thresh = txq->tx_rs_thresh;
1999         qinfo->conf.offloads = txq->offloads;
2000 }