ethdev: new Rx/Tx offloads API
[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_SCATTER;
1368         if (max_rx_pktlen > ETHER_MAX_LEN)
1369                 rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1370
1371         return rx_offload_capa;
1372 }
1373
1374 uint64_t
1375 em_get_rx_queue_offloads_capa(struct rte_eth_dev *dev)
1376 {
1377         uint64_t rx_queue_offload_capa;
1378
1379         /*
1380          * As only one Rx queue can be used, let per queue offloading
1381          * capability be same to per port queue offloading capability
1382          * for better convenience.
1383          */
1384         rx_queue_offload_capa = em_get_rx_port_offloads_capa(dev);
1385
1386         return rx_queue_offload_capa;
1387 }
1388
1389 int
1390 eth_em_rx_queue_setup(struct rte_eth_dev *dev,
1391                 uint16_t queue_idx,
1392                 uint16_t nb_desc,
1393                 unsigned int socket_id,
1394                 const struct rte_eth_rxconf *rx_conf,
1395                 struct rte_mempool *mp)
1396 {
1397         const struct rte_memzone *rz;
1398         struct em_rx_queue *rxq;
1399         struct e1000_hw     *hw;
1400         uint32_t rsize;
1401         uint64_t offloads;
1402
1403         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1404
1405         offloads = rx_conf->offloads | dev->data->dev_conf.rxmode.offloads;
1406
1407         /*
1408          * Validate number of receive descriptors.
1409          * It must not exceed hardware maximum, and must be multiple
1410          * of E1000_ALIGN.
1411          */
1412         if (nb_desc % EM_RXD_ALIGN != 0 ||
1413                         (nb_desc > E1000_MAX_RING_DESC) ||
1414                         (nb_desc < E1000_MIN_RING_DESC)) {
1415                 return -EINVAL;
1416         }
1417
1418         /*
1419          * EM devices don't support drop_en functionality
1420          */
1421         if (rx_conf->rx_drop_en) {
1422                 PMD_INIT_LOG(ERR, "drop_en functionality not supported by "
1423                              "device");
1424                 return -EINVAL;
1425         }
1426
1427         /* Free memory prior to re-allocation if needed. */
1428         if (dev->data->rx_queues[queue_idx] != NULL) {
1429                 em_rx_queue_release(dev->data->rx_queues[queue_idx]);
1430                 dev->data->rx_queues[queue_idx] = NULL;
1431         }
1432
1433         /* Allocate RX ring for max possible mumber of hardware descriptors. */
1434         rsize = sizeof(rxq->rx_ring[0]) * E1000_MAX_RING_DESC;
1435         rz = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx, rsize,
1436                                       RTE_CACHE_LINE_SIZE, socket_id);
1437         if (rz == NULL)
1438                 return -ENOMEM;
1439
1440         /* Allocate the RX queue data structure. */
1441         if ((rxq = rte_zmalloc("ethdev RX queue", sizeof(*rxq),
1442                         RTE_CACHE_LINE_SIZE)) == NULL)
1443                 return -ENOMEM;
1444
1445         /* Allocate software ring. */
1446         if ((rxq->sw_ring = rte_zmalloc("rxq->sw_ring",
1447                         sizeof (rxq->sw_ring[0]) * nb_desc,
1448                         RTE_CACHE_LINE_SIZE)) == NULL) {
1449                 em_rx_queue_release(rxq);
1450                 return -ENOMEM;
1451         }
1452
1453         rxq->mb_pool = mp;
1454         rxq->nb_rx_desc = nb_desc;
1455         rxq->pthresh = rx_conf->rx_thresh.pthresh;
1456         rxq->hthresh = rx_conf->rx_thresh.hthresh;
1457         rxq->wthresh = rx_conf->rx_thresh.wthresh;
1458         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
1459         rxq->queue_id = queue_idx;
1460         rxq->port_id = dev->data->port_id;
1461         rxq->crc_len = (uint8_t)((dev->data->dev_conf.rxmode.offloads &
1462                 DEV_RX_OFFLOAD_CRC_STRIP) ? 0 : ETHER_CRC_LEN);
1463
1464         rxq->rdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDT(queue_idx));
1465         rxq->rdh_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDH(queue_idx));
1466         rxq->rx_ring_phys_addr = rz->iova;
1467         rxq->rx_ring = (struct e1000_rx_desc *) rz->addr;
1468
1469         PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
1470                      rxq->sw_ring, rxq->rx_ring, rxq->rx_ring_phys_addr);
1471
1472         dev->data->rx_queues[queue_idx] = rxq;
1473         em_reset_rx_queue(rxq);
1474         rxq->offloads = offloads;
1475
1476         return 0;
1477 }
1478
1479 uint32_t
1480 eth_em_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1481 {
1482 #define EM_RXQ_SCAN_INTERVAL 4
1483         volatile struct e1000_rx_desc *rxdp;
1484         struct em_rx_queue *rxq;
1485         uint32_t desc = 0;
1486
1487         rxq = dev->data->rx_queues[rx_queue_id];
1488         rxdp = &(rxq->rx_ring[rxq->rx_tail]);
1489
1490         while ((desc < rxq->nb_rx_desc) &&
1491                 (rxdp->status & E1000_RXD_STAT_DD)) {
1492                 desc += EM_RXQ_SCAN_INTERVAL;
1493                 rxdp += EM_RXQ_SCAN_INTERVAL;
1494                 if (rxq->rx_tail + desc >= rxq->nb_rx_desc)
1495                         rxdp = &(rxq->rx_ring[rxq->rx_tail +
1496                                 desc - rxq->nb_rx_desc]);
1497         }
1498
1499         return desc;
1500 }
1501
1502 int
1503 eth_em_rx_descriptor_done(void *rx_queue, uint16_t offset)
1504 {
1505         volatile struct e1000_rx_desc *rxdp;
1506         struct em_rx_queue *rxq = rx_queue;
1507         uint32_t desc;
1508
1509         if (unlikely(offset >= rxq->nb_rx_desc))
1510                 return 0;
1511         desc = rxq->rx_tail + offset;
1512         if (desc >= rxq->nb_rx_desc)
1513                 desc -= rxq->nb_rx_desc;
1514
1515         rxdp = &rxq->rx_ring[desc];
1516         return !!(rxdp->status & E1000_RXD_STAT_DD);
1517 }
1518
1519 int
1520 eth_em_rx_descriptor_status(void *rx_queue, uint16_t offset)
1521 {
1522         struct em_rx_queue *rxq = rx_queue;
1523         volatile uint8_t *status;
1524         uint32_t desc;
1525
1526         if (unlikely(offset >= rxq->nb_rx_desc))
1527                 return -EINVAL;
1528
1529         if (offset >= rxq->nb_rx_desc - rxq->nb_rx_hold)
1530                 return RTE_ETH_RX_DESC_UNAVAIL;
1531
1532         desc = rxq->rx_tail + offset;
1533         if (desc >= rxq->nb_rx_desc)
1534                 desc -= rxq->nb_rx_desc;
1535
1536         status = &rxq->rx_ring[desc].status;
1537         if (*status & E1000_RXD_STAT_DD)
1538                 return RTE_ETH_RX_DESC_DONE;
1539
1540         return RTE_ETH_RX_DESC_AVAIL;
1541 }
1542
1543 int
1544 eth_em_tx_descriptor_status(void *tx_queue, uint16_t offset)
1545 {
1546         struct em_tx_queue *txq = tx_queue;
1547         volatile uint8_t *status;
1548         uint32_t desc;
1549
1550         if (unlikely(offset >= txq->nb_tx_desc))
1551                 return -EINVAL;
1552
1553         desc = txq->tx_tail + offset;
1554         /* go to next desc that has the RS bit */
1555         desc = ((desc + txq->tx_rs_thresh - 1) / txq->tx_rs_thresh) *
1556                 txq->tx_rs_thresh;
1557         if (desc >= txq->nb_tx_desc) {
1558                 desc -= txq->nb_tx_desc;
1559                 if (desc >= txq->nb_tx_desc)
1560                         desc -= txq->nb_tx_desc;
1561         }
1562
1563         status = &txq->tx_ring[desc].upper.fields.status;
1564         if (*status & E1000_TXD_STAT_DD)
1565                 return RTE_ETH_TX_DESC_DONE;
1566
1567         return RTE_ETH_TX_DESC_FULL;
1568 }
1569
1570 void
1571 em_dev_clear_queues(struct rte_eth_dev *dev)
1572 {
1573         uint16_t i;
1574         struct em_tx_queue *txq;
1575         struct em_rx_queue *rxq;
1576
1577         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1578                 txq = dev->data->tx_queues[i];
1579                 if (txq != NULL) {
1580                         em_tx_queue_release_mbufs(txq);
1581                         em_reset_tx_queue(txq);
1582                 }
1583         }
1584
1585         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1586                 rxq = dev->data->rx_queues[i];
1587                 if (rxq != NULL) {
1588                         em_rx_queue_release_mbufs(rxq);
1589                         em_reset_rx_queue(rxq);
1590                 }
1591         }
1592 }
1593
1594 void
1595 em_dev_free_queues(struct rte_eth_dev *dev)
1596 {
1597         uint16_t i;
1598
1599         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1600                 eth_em_rx_queue_release(dev->data->rx_queues[i]);
1601                 dev->data->rx_queues[i] = NULL;
1602         }
1603         dev->data->nb_rx_queues = 0;
1604
1605         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1606                 eth_em_tx_queue_release(dev->data->tx_queues[i]);
1607                 dev->data->tx_queues[i] = NULL;
1608         }
1609         dev->data->nb_tx_queues = 0;
1610 }
1611
1612 /*
1613  * Takes as input/output parameter RX buffer size.
1614  * Returns (BSIZE | BSEX | FLXBUF) fields of RCTL register.
1615  */
1616 static uint32_t
1617 em_rctl_bsize(__rte_unused enum e1000_mac_type hwtyp, uint32_t *bufsz)
1618 {
1619         /*
1620          * For BSIZE & BSEX all configurable sizes are:
1621          * 16384: rctl |= (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX);
1622          *  8192: rctl |= (E1000_RCTL_SZ_8192  | E1000_RCTL_BSEX);
1623          *  4096: rctl |= (E1000_RCTL_SZ_4096  | E1000_RCTL_BSEX);
1624          *  2048: rctl |= E1000_RCTL_SZ_2048;
1625          *  1024: rctl |= E1000_RCTL_SZ_1024;
1626          *   512: rctl |= E1000_RCTL_SZ_512;
1627          *   256: rctl |= E1000_RCTL_SZ_256;
1628          */
1629         static const struct {
1630                 uint32_t bufsz;
1631                 uint32_t rctl;
1632         } bufsz_to_rctl[] = {
1633                 {16384, (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX)},
1634                 {8192,  (E1000_RCTL_SZ_8192  | E1000_RCTL_BSEX)},
1635                 {4096,  (E1000_RCTL_SZ_4096  | E1000_RCTL_BSEX)},
1636                 {2048,  E1000_RCTL_SZ_2048},
1637                 {1024,  E1000_RCTL_SZ_1024},
1638                 {512,   E1000_RCTL_SZ_512},
1639                 {256,   E1000_RCTL_SZ_256},
1640         };
1641
1642         int i;
1643         uint32_t rctl_bsize;
1644
1645         rctl_bsize = *bufsz;
1646
1647         /*
1648          * Starting from 82571 it is possible to specify RX buffer size
1649          * by RCTL.FLXBUF. When this field is different from zero, the
1650          * RX buffer size = RCTL.FLXBUF * 1K
1651          * (e.g. t is possible to specify RX buffer size  1,2,...,15KB).
1652          * It is working ok on real HW, but by some reason doesn't work
1653          * on VMware emulated 82574L.
1654          * So for now, always use BSIZE/BSEX to setup RX buffer size.
1655          * If you don't plan to use it on VMware emulated 82574L and
1656          * would like to specify RX buffer size in 1K granularity,
1657          * uncomment the following lines:
1658          * ***************************************************************
1659          * if (hwtyp >= e1000_82571 && hwtyp <= e1000_82574 &&
1660          *              rctl_bsize >= EM_RCTL_FLXBUF_STEP) {
1661          *      rctl_bsize /= EM_RCTL_FLXBUF_STEP;
1662          *      *bufsz = rctl_bsize;
1663          *      return (rctl_bsize << E1000_RCTL_FLXBUF_SHIFT &
1664          *              E1000_RCTL_FLXBUF_MASK);
1665          * }
1666          * ***************************************************************
1667          */
1668
1669         for (i = 0; i != sizeof(bufsz_to_rctl) / sizeof(bufsz_to_rctl[0]);
1670                         i++) {
1671                 if (rctl_bsize >= bufsz_to_rctl[i].bufsz) {
1672                         *bufsz = bufsz_to_rctl[i].bufsz;
1673                         return bufsz_to_rctl[i].rctl;
1674                 }
1675         }
1676
1677         /* Should never happen. */
1678         return -EINVAL;
1679 }
1680
1681 static int
1682 em_alloc_rx_queue_mbufs(struct em_rx_queue *rxq)
1683 {
1684         struct em_rx_entry *rxe = rxq->sw_ring;
1685         uint64_t dma_addr;
1686         unsigned i;
1687         static const struct e1000_rx_desc rxd_init = {
1688                 .buffer_addr = 0,
1689         };
1690
1691         /* Initialize software ring entries */
1692         for (i = 0; i < rxq->nb_rx_desc; i++) {
1693                 volatile struct e1000_rx_desc *rxd;
1694                 struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
1695
1696                 if (mbuf == NULL) {
1697                         PMD_INIT_LOG(ERR, "RX mbuf alloc failed "
1698                                      "queue_id=%hu", rxq->queue_id);
1699                         return -ENOMEM;
1700                 }
1701
1702                 dma_addr =
1703                         rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
1704
1705                 /* Clear HW ring memory */
1706                 rxq->rx_ring[i] = rxd_init;
1707
1708                 rxd = &rxq->rx_ring[i];
1709                 rxd->buffer_addr = dma_addr;
1710                 rxe[i].mbuf = mbuf;
1711         }
1712
1713         return 0;
1714 }
1715
1716 /*********************************************************************
1717  *
1718  *  Enable receive unit.
1719  *
1720  **********************************************************************/
1721 int
1722 eth_em_rx_init(struct rte_eth_dev *dev)
1723 {
1724         struct e1000_hw *hw;
1725         struct em_rx_queue *rxq;
1726         struct rte_eth_rxmode *rxmode;
1727         uint32_t rctl;
1728         uint32_t rfctl;
1729         uint32_t rxcsum;
1730         uint32_t rctl_bsize;
1731         uint16_t i;
1732         int ret;
1733
1734         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1735         rxmode = &dev->data->dev_conf.rxmode;
1736
1737         /*
1738          * Make sure receives are disabled while setting
1739          * up the descriptor ring.
1740          */
1741         rctl = E1000_READ_REG(hw, E1000_RCTL);
1742         E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
1743
1744         rfctl = E1000_READ_REG(hw, E1000_RFCTL);
1745
1746         /* Disable extended descriptor type. */
1747         rfctl &= ~E1000_RFCTL_EXTEN;
1748         /* Disable accelerated acknowledge */
1749         if (hw->mac.type == e1000_82574)
1750                 rfctl |= E1000_RFCTL_ACK_DIS;
1751
1752         E1000_WRITE_REG(hw, E1000_RFCTL, rfctl);
1753
1754         /*
1755          * XXX TEMPORARY WORKAROUND: on some systems with 82573
1756          * long latencies are observed, like Lenovo X60. This
1757          * change eliminates the problem, but since having positive
1758          * values in RDTR is a known source of problems on other
1759          * platforms another solution is being sought.
1760          */
1761         if (hw->mac.type == e1000_82573)
1762                 E1000_WRITE_REG(hw, E1000_RDTR, 0x20);
1763
1764         dev->rx_pkt_burst = (eth_rx_burst_t)eth_em_recv_pkts;
1765
1766         /* Determine RX bufsize. */
1767         rctl_bsize = EM_MAX_BUF_SIZE;
1768         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1769                 uint32_t buf_size;
1770
1771                 rxq = dev->data->rx_queues[i];
1772                 buf_size = rte_pktmbuf_data_room_size(rxq->mb_pool) -
1773                         RTE_PKTMBUF_HEADROOM;
1774                 rctl_bsize = RTE_MIN(rctl_bsize, buf_size);
1775         }
1776
1777         rctl |= em_rctl_bsize(hw->mac.type, &rctl_bsize);
1778
1779         /* Configure and enable each RX queue. */
1780         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1781                 uint64_t bus_addr;
1782                 uint32_t rxdctl;
1783
1784                 rxq = dev->data->rx_queues[i];
1785
1786                 /* Allocate buffers for descriptor rings and setup queue */
1787                 ret = em_alloc_rx_queue_mbufs(rxq);
1788                 if (ret)
1789                         return ret;
1790
1791                 /*
1792                  * Reset crc_len in case it was changed after queue setup by a
1793                  *  call to configure
1794                  */
1795                 rxq->crc_len =
1796                         (uint8_t)(dev->data->dev_conf.rxmode.offloads &
1797                                 DEV_RX_OFFLOAD_CRC_STRIP ? 0 : ETHER_CRC_LEN);
1798
1799                 bus_addr = rxq->rx_ring_phys_addr;
1800                 E1000_WRITE_REG(hw, E1000_RDLEN(i),
1801                                 rxq->nb_rx_desc *
1802                                 sizeof(*rxq->rx_ring));
1803                 E1000_WRITE_REG(hw, E1000_RDBAH(i),
1804                                 (uint32_t)(bus_addr >> 32));
1805                 E1000_WRITE_REG(hw, E1000_RDBAL(i), (uint32_t)bus_addr);
1806
1807                 E1000_WRITE_REG(hw, E1000_RDH(i), 0);
1808                 E1000_WRITE_REG(hw, E1000_RDT(i), rxq->nb_rx_desc - 1);
1809
1810                 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1811                 rxdctl &= 0xFE000000;
1812                 rxdctl |= rxq->pthresh & 0x3F;
1813                 rxdctl |= (rxq->hthresh & 0x3F) << 8;
1814                 rxdctl |= (rxq->wthresh & 0x3F) << 16;
1815                 rxdctl |= E1000_RXDCTL_GRAN;
1816                 E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
1817
1818                 /*
1819                  * Due to EM devices not having any sort of hardware
1820                  * limit for packet length, jumbo frame of any size
1821                  * can be accepted, thus we have to enable scattered
1822                  * rx if jumbo frames are enabled (or if buffer size
1823                  * is too small to accommodate non-jumbo packets)
1824                  * to avoid splitting packets that don't fit into
1825                  * one buffer.
1826                  */
1827                 if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME ||
1828                                 rctl_bsize < ETHER_MAX_LEN) {
1829                         if (!dev->data->scattered_rx)
1830                                 PMD_INIT_LOG(DEBUG, "forcing scatter mode");
1831                         dev->rx_pkt_burst =
1832                                 (eth_rx_burst_t)eth_em_recv_scattered_pkts;
1833                         dev->data->scattered_rx = 1;
1834                 }
1835         }
1836
1837         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SCATTER) {
1838                 if (!dev->data->scattered_rx)
1839                         PMD_INIT_LOG(DEBUG, "forcing scatter mode");
1840                 dev->rx_pkt_burst = eth_em_recv_scattered_pkts;
1841                 dev->data->scattered_rx = 1;
1842         }
1843
1844         /*
1845          * Setup the Checksum Register.
1846          * Receive Full-Packet Checksum Offload is mutually exclusive with RSS.
1847          */
1848         rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
1849
1850         if (rxmode->offloads & DEV_RX_OFFLOAD_CHECKSUM)
1851                 rxcsum |= E1000_RXCSUM_IPOFL;
1852         else
1853                 rxcsum &= ~E1000_RXCSUM_IPOFL;
1854         E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
1855
1856         /* No MRQ or RSS support for now */
1857
1858         /* Set early receive threshold on appropriate hw */
1859         if ((hw->mac.type == e1000_ich9lan ||
1860                         hw->mac.type == e1000_pch2lan ||
1861                         hw->mac.type == e1000_ich10lan) &&
1862                         rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
1863                 u32 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1864                 E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl | 3);
1865                 E1000_WRITE_REG(hw, E1000_ERT, 0x100 | (1 << 13));
1866         }
1867
1868         if (hw->mac.type == e1000_pch2lan) {
1869                 if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
1870                         e1000_lv_jumbo_workaround_ich8lan(hw, TRUE);
1871                 else
1872                         e1000_lv_jumbo_workaround_ich8lan(hw, FALSE);
1873         }
1874
1875         /* Setup the Receive Control Register. */
1876         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_CRC_STRIP)
1877                 rctl |= E1000_RCTL_SECRC; /* Strip Ethernet CRC. */
1878         else
1879                 rctl &= ~E1000_RCTL_SECRC; /* Do not Strip Ethernet CRC. */
1880
1881         rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1882         rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
1883                 E1000_RCTL_RDMTS_HALF |
1884                 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1885
1886         /* Make sure VLAN Filters are off. */
1887         rctl &= ~E1000_RCTL_VFE;
1888         /* Don't store bad packets. */
1889         rctl &= ~E1000_RCTL_SBP;
1890         /* Legacy descriptor type. */
1891         rctl &= ~E1000_RCTL_DTYP_MASK;
1892
1893         /*
1894          * Configure support of jumbo frames, if any.
1895          */
1896         if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
1897                 rctl |= E1000_RCTL_LPE;
1898         else
1899                 rctl &= ~E1000_RCTL_LPE;
1900
1901         /* Enable Receives. */
1902         E1000_WRITE_REG(hw, E1000_RCTL, rctl);
1903
1904         return 0;
1905 }
1906
1907 /*********************************************************************
1908  *
1909  *  Enable transmit unit.
1910  *
1911  **********************************************************************/
1912 void
1913 eth_em_tx_init(struct rte_eth_dev *dev)
1914 {
1915         struct e1000_hw     *hw;
1916         struct em_tx_queue *txq;
1917         uint32_t tctl;
1918         uint32_t txdctl;
1919         uint16_t i;
1920
1921         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1922
1923         /* Setup the Base and Length of the Tx Descriptor Rings. */
1924         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1925                 uint64_t bus_addr;
1926
1927                 txq = dev->data->tx_queues[i];
1928                 bus_addr = txq->tx_ring_phys_addr;
1929                 E1000_WRITE_REG(hw, E1000_TDLEN(i),
1930                                 txq->nb_tx_desc *
1931                                 sizeof(*txq->tx_ring));
1932                 E1000_WRITE_REG(hw, E1000_TDBAH(i),
1933                                 (uint32_t)(bus_addr >> 32));
1934                 E1000_WRITE_REG(hw, E1000_TDBAL(i), (uint32_t)bus_addr);
1935
1936                 /* Setup the HW Tx Head and Tail descriptor pointers. */
1937                 E1000_WRITE_REG(hw, E1000_TDT(i), 0);
1938                 E1000_WRITE_REG(hw, E1000_TDH(i), 0);
1939
1940                 /* Setup Transmit threshold registers. */
1941                 txdctl = E1000_READ_REG(hw, E1000_TXDCTL(i));
1942                 /*
1943                  * bit 22 is reserved, on some models should always be 0,
1944                  * on others  - always 1.
1945                  */
1946                 txdctl &= E1000_TXDCTL_COUNT_DESC;
1947                 txdctl |= txq->pthresh & 0x3F;
1948                 txdctl |= (txq->hthresh & 0x3F) << 8;
1949                 txdctl |= (txq->wthresh & 0x3F) << 16;
1950                 txdctl |= E1000_TXDCTL_GRAN;
1951                 E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
1952         }
1953
1954         /* Program the Transmit Control Register. */
1955         tctl = E1000_READ_REG(hw, E1000_TCTL);
1956         tctl &= ~E1000_TCTL_CT;
1957         tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
1958                  (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
1959
1960         /* This write will effectively turn on the transmit unit. */
1961         E1000_WRITE_REG(hw, E1000_TCTL, tctl);
1962 }
1963
1964 void
1965 em_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
1966         struct rte_eth_rxq_info *qinfo)
1967 {
1968         struct em_rx_queue *rxq;
1969
1970         rxq = dev->data->rx_queues[queue_id];
1971
1972         qinfo->mp = rxq->mb_pool;
1973         qinfo->scattered_rx = dev->data->scattered_rx;
1974         qinfo->nb_desc = rxq->nb_rx_desc;
1975         qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
1976         qinfo->conf.offloads = rxq->offloads;
1977 }
1978
1979 void
1980 em_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
1981         struct rte_eth_txq_info *qinfo)
1982 {
1983         struct em_tx_queue *txq;
1984
1985         txq = dev->data->tx_queues[queue_id];
1986
1987         qinfo->nb_desc = txq->nb_tx_desc;
1988
1989         qinfo->conf.tx_thresh.pthresh = txq->pthresh;
1990         qinfo->conf.tx_thresh.hthresh = txq->hthresh;
1991         qinfo->conf.tx_thresh.wthresh = txq->wthresh;
1992         qinfo->conf.tx_free_thresh = txq->tx_free_thresh;
1993         qinfo->conf.tx_rs_thresh = txq->tx_rs_thresh;
1994         qinfo->conf.offloads = txq->offloads;
1995 }