ethdev: add new offload flag to keep CRC
[dpdk.git] / drivers / net / axgbe / axgbe_rxtx.c
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
3  *   Copyright(c) 2018 Synopsys, Inc. All rights reserved.
4  */
5
6 #include "axgbe_ethdev.h"
7 #include "axgbe_rxtx.h"
8 #include "axgbe_phy.h"
9
10 #include <rte_time.h>
11 #include <rte_mempool.h>
12 #include <rte_mbuf.h>
13
14 static void
15 axgbe_rx_queue_release(struct axgbe_rx_queue *rx_queue)
16 {
17         uint16_t i;
18         struct rte_mbuf **sw_ring;
19
20         if (rx_queue) {
21                 sw_ring = rx_queue->sw_ring;
22                 if (sw_ring) {
23                         for (i = 0; i < rx_queue->nb_desc; i++) {
24                                 if (sw_ring[i])
25                                         rte_pktmbuf_free(sw_ring[i]);
26                         }
27                         rte_free(sw_ring);
28                 }
29                 rte_free(rx_queue);
30         }
31 }
32
33 void axgbe_dev_rx_queue_release(void *rxq)
34 {
35         axgbe_rx_queue_release(rxq);
36 }
37
38 int axgbe_dev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
39                              uint16_t nb_desc, unsigned int socket_id,
40                              const struct rte_eth_rxconf *rx_conf,
41                              struct rte_mempool *mp)
42 {
43         PMD_INIT_FUNC_TRACE();
44         uint32_t size;
45         const struct rte_memzone *dma;
46         struct axgbe_rx_queue *rxq;
47         uint32_t rx_desc = nb_desc;
48         struct axgbe_port *pdata =  dev->data->dev_private;
49
50         /*
51          * validate Rx descriptors count
52          * should be power of 2 and less than h/w supported
53          */
54         if ((!rte_is_power_of_2(rx_desc)) ||
55             rx_desc > pdata->rx_desc_count)
56                 return -EINVAL;
57         /* First allocate the rx queue data structure */
58         rxq = rte_zmalloc_socket("ethdev RX queue",
59                                  sizeof(struct axgbe_rx_queue),
60                                  RTE_CACHE_LINE_SIZE, socket_id);
61         if (!rxq) {
62                 PMD_INIT_LOG(ERR, "rte_zmalloc for rxq failed!");
63                 return -ENOMEM;
64         }
65
66         rxq->cur = 0;
67         rxq->dirty = 0;
68         rxq->pdata = pdata;
69         rxq->mb_pool = mp;
70         rxq->queue_id = queue_idx;
71         rxq->port_id = dev->data->port_id;
72         rxq->nb_desc = rx_desc;
73         rxq->dma_regs = (void *)((uint8_t *)pdata->xgmac_regs + DMA_CH_BASE +
74                 (DMA_CH_INC * rxq->queue_id));
75         rxq->dma_tail_reg = (volatile uint32_t *)((uint8_t *)rxq->dma_regs +
76                                                   DMA_CH_RDTR_LO);
77         if (rte_eth_dev_must_keep_crc(dev->data->dev_conf.rxmode.offloads))
78                 rxq->crc_len = ETHER_CRC_LEN;
79         else
80                 rxq->crc_len = 0;
81
82         /* CRC strip in AXGBE supports per port not per queue */
83         pdata->crc_strip_enable = (rxq->crc_len == 0) ? 1 : 0;
84         rxq->free_thresh = rx_conf->rx_free_thresh ?
85                 rx_conf->rx_free_thresh : AXGBE_RX_FREE_THRESH;
86         if (rxq->free_thresh >  rxq->nb_desc)
87                 rxq->free_thresh = rxq->nb_desc >> 3;
88
89         /* Allocate RX ring hardware descriptors */
90         size = rxq->nb_desc * sizeof(union axgbe_rx_desc);
91         dma = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx, size, 128,
92                                        socket_id);
93         if (!dma) {
94                 PMD_DRV_LOG(ERR, "ring_dma_zone_reserve for rx_ring failed\n");
95                 axgbe_rx_queue_release(rxq);
96                 return -ENOMEM;
97         }
98         rxq->ring_phys_addr = (uint64_t)dma->phys_addr;
99         rxq->desc = (volatile union axgbe_rx_desc *)dma->addr;
100         memset((void *)rxq->desc, 0, size);
101         /* Allocate software ring */
102         size = rxq->nb_desc * sizeof(struct rte_mbuf *);
103         rxq->sw_ring = rte_zmalloc_socket("sw_ring", size,
104                                           RTE_CACHE_LINE_SIZE,
105                                           socket_id);
106         if (!rxq->sw_ring) {
107                 PMD_DRV_LOG(ERR, "rte_zmalloc for sw_ring failed\n");
108                 axgbe_rx_queue_release(rxq);
109                 return -ENOMEM;
110         }
111         dev->data->rx_queues[queue_idx] = rxq;
112         if (!pdata->rx_queues)
113                 pdata->rx_queues = dev->data->rx_queues;
114
115         return 0;
116 }
117
118 static void axgbe_prepare_rx_stop(struct axgbe_port *pdata,
119                                   unsigned int queue)
120 {
121         unsigned int rx_status;
122         unsigned long rx_timeout;
123
124         /* The Rx engine cannot be stopped if it is actively processing
125          * packets. Wait for the Rx queue to empty the Rx fifo.  Don't
126          * wait forever though...
127          */
128         rx_timeout = rte_get_timer_cycles() + (AXGBE_DMA_STOP_TIMEOUT *
129                                                rte_get_timer_hz());
130
131         while (time_before(rte_get_timer_cycles(), rx_timeout)) {
132                 rx_status = AXGMAC_MTL_IOREAD(pdata, queue, MTL_Q_RQDR);
133                 if ((AXGMAC_GET_BITS(rx_status, MTL_Q_RQDR, PRXQ) == 0) &&
134                     (AXGMAC_GET_BITS(rx_status, MTL_Q_RQDR, RXQSTS) == 0))
135                         break;
136
137                 rte_delay_us(900);
138         }
139
140         if (!time_before(rte_get_timer_cycles(), rx_timeout))
141                 PMD_DRV_LOG(ERR,
142                             "timed out waiting for Rx queue %u to empty\n",
143                             queue);
144 }
145
146 void axgbe_dev_disable_rx(struct rte_eth_dev *dev)
147 {
148         struct axgbe_rx_queue *rxq;
149         struct axgbe_port *pdata = dev->data->dev_private;
150         unsigned int i;
151
152         /* Disable MAC Rx */
153         AXGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 0);
154         AXGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 0);
155         AXGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 0);
156         AXGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 0);
157
158         /* Prepare for Rx DMA channel stop */
159         for (i = 0; i < dev->data->nb_rx_queues; i++) {
160                 rxq = dev->data->rx_queues[i];
161                 axgbe_prepare_rx_stop(pdata, i);
162         }
163         /* Disable each Rx queue */
164         AXGMAC_IOWRITE(pdata, MAC_RQC0R, 0);
165         for (i = 0; i < dev->data->nb_rx_queues; i++) {
166                 rxq = dev->data->rx_queues[i];
167                 /* Disable Rx DMA channel */
168                 AXGMAC_DMA_IOWRITE_BITS(rxq, DMA_CH_RCR, SR, 0);
169         }
170 }
171
172 void axgbe_dev_enable_rx(struct rte_eth_dev *dev)
173 {
174         struct axgbe_rx_queue *rxq;
175         struct axgbe_port *pdata = dev->data->dev_private;
176         unsigned int i;
177         unsigned int reg_val = 0;
178
179         for (i = 0; i < dev->data->nb_rx_queues; i++) {
180                 rxq = dev->data->rx_queues[i];
181                 /* Enable Rx DMA channel */
182                 AXGMAC_DMA_IOWRITE_BITS(rxq, DMA_CH_RCR, SR, 1);
183         }
184
185         reg_val = 0;
186         for (i = 0; i < pdata->rx_q_count; i++)
187                 reg_val |= (0x02 << (i << 1));
188         AXGMAC_IOWRITE(pdata, MAC_RQC0R, reg_val);
189
190         /* Enable MAC Rx */
191         AXGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 1);
192         /* Frame is forwarded after stripping CRC to application*/
193         if (pdata->crc_strip_enable) {
194                 AXGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 1);
195                 AXGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 1);
196         }
197         AXGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 1);
198 }
199
200 /* Rx function one to one refresh */
201 uint16_t
202 axgbe_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
203                 uint16_t nb_pkts)
204 {
205         PMD_INIT_FUNC_TRACE();
206         uint16_t nb_rx = 0;
207         struct axgbe_rx_queue *rxq = rx_queue;
208         volatile union axgbe_rx_desc *desc;
209         uint64_t old_dirty = rxq->dirty;
210         struct rte_mbuf *mbuf, *tmbuf;
211         unsigned int err;
212         uint32_t error_status;
213         uint16_t idx, pidx, pkt_len;
214
215         idx = AXGBE_GET_DESC_IDX(rxq, rxq->cur);
216         while (nb_rx < nb_pkts) {
217                 if (unlikely(idx == rxq->nb_desc))
218                         idx = 0;
219
220                 desc = &rxq->desc[idx];
221
222                 if (AXGMAC_GET_BITS_LE(desc->write.desc3, RX_NORMAL_DESC3, OWN))
223                         break;
224                 tmbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
225                 if (unlikely(!tmbuf)) {
226                         PMD_DRV_LOG(ERR, "RX mbuf alloc failed port_id = %u"
227                                     " queue_id = %u\n",
228                                     (unsigned int)rxq->port_id,
229                                     (unsigned int)rxq->queue_id);
230                         rte_eth_devices[
231                                 rxq->port_id].data->rx_mbuf_alloc_failed++;
232                         break;
233                 }
234                 pidx = idx + 1;
235                 if (unlikely(pidx == rxq->nb_desc))
236                         pidx = 0;
237
238                 rte_prefetch0(rxq->sw_ring[pidx]);
239                 if ((pidx & 0x3) == 0) {
240                         rte_prefetch0(&rxq->desc[pidx]);
241                         rte_prefetch0(&rxq->sw_ring[pidx]);
242                 }
243
244                 mbuf = rxq->sw_ring[idx];
245                 /* Check for any errors and free mbuf*/
246                 err = AXGMAC_GET_BITS_LE(desc->write.desc3,
247                                          RX_NORMAL_DESC3, ES);
248                 error_status = 0;
249                 if (unlikely(err)) {
250                         error_status = desc->write.desc3 & AXGBE_ERR_STATUS;
251                         if ((error_status != AXGBE_L3_CSUM_ERR) &&
252                             (error_status != AXGBE_L4_CSUM_ERR)) {
253                                 rxq->errors++;
254                                 rte_pktmbuf_free(mbuf);
255                                 goto err_set;
256                         }
257                 }
258                 if (rxq->pdata->rx_csum_enable) {
259                         mbuf->ol_flags = 0;
260                         mbuf->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
261                         mbuf->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
262                         if (unlikely(error_status == AXGBE_L3_CSUM_ERR)) {
263                                 mbuf->ol_flags &= ~PKT_RX_IP_CKSUM_GOOD;
264                                 mbuf->ol_flags |= PKT_RX_IP_CKSUM_BAD;
265                                 mbuf->ol_flags &= ~PKT_RX_L4_CKSUM_GOOD;
266                                 mbuf->ol_flags |= PKT_RX_L4_CKSUM_UNKNOWN;
267                         } else if (
268                                 unlikely(error_status == AXGBE_L4_CSUM_ERR)) {
269                                 mbuf->ol_flags &= ~PKT_RX_L4_CKSUM_GOOD;
270                                 mbuf->ol_flags |= PKT_RX_L4_CKSUM_BAD;
271                         }
272                 }
273                 rte_prefetch1(rte_pktmbuf_mtod(mbuf, void *));
274                 /* Get the RSS hash */
275                 if (AXGMAC_GET_BITS_LE(desc->write.desc3, RX_NORMAL_DESC3, RSV))
276                         mbuf->hash.rss = rte_le_to_cpu_32(desc->write.desc1);
277                 pkt_len = AXGMAC_GET_BITS_LE(desc->write.desc3, RX_NORMAL_DESC3,
278                                              PL) - rxq->crc_len;
279                 /* Mbuf populate */
280                 mbuf->next = NULL;
281                 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
282                 mbuf->nb_segs = 1;
283                 mbuf->port = rxq->port_id;
284                 mbuf->pkt_len = pkt_len;
285                 mbuf->data_len = pkt_len;
286                 rxq->bytes += pkt_len;
287                 rx_pkts[nb_rx++] = mbuf;
288 err_set:
289                 rxq->cur++;
290                 rxq->sw_ring[idx++] = tmbuf;
291                 desc->read.baddr =
292                         rte_cpu_to_le_64(rte_mbuf_data_iova_default(tmbuf));
293                 memset((void *)(&desc->read.desc2), 0, 8);
294                 AXGMAC_SET_BITS_LE(desc->read.desc3, RX_NORMAL_DESC3, OWN, 1);
295                 rxq->dirty++;
296         }
297         rxq->pkts += nb_rx;
298         if (rxq->dirty != old_dirty) {
299                 rte_wmb();
300                 idx = AXGBE_GET_DESC_IDX(rxq, rxq->dirty - 1);
301                 AXGMAC_DMA_IOWRITE(rxq, DMA_CH_RDTR_LO,
302                                    low32_value(rxq->ring_phys_addr +
303                                    (idx * sizeof(union axgbe_rx_desc))));
304         }
305
306         return nb_rx;
307 }
308
309 /* Tx Apis */
310 static void axgbe_tx_queue_release(struct axgbe_tx_queue *tx_queue)
311 {
312         uint16_t i;
313         struct rte_mbuf **sw_ring;
314
315         if (tx_queue) {
316                 sw_ring = tx_queue->sw_ring;
317                 if (sw_ring) {
318                         for (i = 0; i < tx_queue->nb_desc; i++) {
319                                 if (sw_ring[i])
320                                         rte_pktmbuf_free(sw_ring[i]);
321                         }
322                         rte_free(sw_ring);
323                 }
324                 rte_free(tx_queue);
325         }
326 }
327
328 void axgbe_dev_tx_queue_release(void *txq)
329 {
330         axgbe_tx_queue_release(txq);
331 }
332
333 int axgbe_dev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
334                              uint16_t nb_desc, unsigned int socket_id,
335                              const struct rte_eth_txconf *tx_conf)
336 {
337         PMD_INIT_FUNC_TRACE();
338         uint32_t tx_desc;
339         struct axgbe_port *pdata;
340         struct axgbe_tx_queue *txq;
341         unsigned int tsize;
342         const struct rte_memzone *tz;
343
344         tx_desc = nb_desc;
345         pdata = (struct axgbe_port *)dev->data->dev_private;
346
347         /*
348          * validate tx descriptors count
349          * should be power of 2 and less than h/w supported
350          */
351         if ((!rte_is_power_of_2(tx_desc)) ||
352             tx_desc > pdata->tx_desc_count ||
353             tx_desc < AXGBE_MIN_RING_DESC)
354                 return -EINVAL;
355
356         /* First allocate the tx queue data structure */
357         txq = rte_zmalloc("ethdev TX queue", sizeof(struct axgbe_tx_queue),
358                           RTE_CACHE_LINE_SIZE);
359         if (!txq)
360                 return -ENOMEM;
361         txq->pdata = pdata;
362
363         txq->nb_desc = tx_desc;
364         txq->free_thresh = tx_conf->tx_free_thresh ?
365                 tx_conf->tx_free_thresh : AXGBE_TX_FREE_THRESH;
366         if (txq->free_thresh > txq->nb_desc)
367                 txq->free_thresh = (txq->nb_desc >> 1);
368         txq->free_batch_cnt = txq->free_thresh;
369
370         /* In vector_tx path threshold should be multiple of queue_size*/
371         if (txq->nb_desc % txq->free_thresh != 0)
372                 txq->vector_disable = 1;
373
374         if ((tx_conf->txq_flags & (uint32_t)ETH_TXQ_FLAGS_NOOFFLOADS) !=
375             ETH_TXQ_FLAGS_NOOFFLOADS) {
376                 txq->vector_disable = 1;
377         }
378
379         /* Allocate TX ring hardware descriptors */
380         tsize = txq->nb_desc * sizeof(struct axgbe_tx_desc);
381         tz = rte_eth_dma_zone_reserve(dev, "tx_ring", queue_idx,
382                                       tsize, AXGBE_DESC_ALIGN, socket_id);
383         if (!tz) {
384                 axgbe_tx_queue_release(txq);
385                 return -ENOMEM;
386         }
387         memset(tz->addr, 0, tsize);
388         txq->ring_phys_addr = (uint64_t)tz->phys_addr;
389         txq->desc = tz->addr;
390         txq->queue_id = queue_idx;
391         txq->port_id = dev->data->port_id;
392         txq->dma_regs = (void *)((uint8_t *)pdata->xgmac_regs + DMA_CH_BASE +
393                 (DMA_CH_INC * txq->queue_id));
394         txq->dma_tail_reg = (volatile uint32_t *)((uint8_t *)txq->dma_regs +
395                                                   DMA_CH_TDTR_LO);
396         txq->cur = 0;
397         txq->dirty = 0;
398         txq->nb_desc_free = txq->nb_desc;
399         /* Allocate software ring */
400         tsize = txq->nb_desc * sizeof(struct rte_mbuf *);
401         txq->sw_ring = rte_zmalloc("tx_sw_ring", tsize,
402                                    RTE_CACHE_LINE_SIZE);
403         if (!txq->sw_ring) {
404                 axgbe_tx_queue_release(txq);
405                 return -ENOMEM;
406         }
407         dev->data->tx_queues[queue_idx] = txq;
408         if (!pdata->tx_queues)
409                 pdata->tx_queues = dev->data->tx_queues;
410
411         if (txq->vector_disable)
412                 dev->tx_pkt_burst = &axgbe_xmit_pkts;
413         else
414 #ifdef RTE_ARCH_X86
415                 dev->tx_pkt_burst = &axgbe_xmit_pkts_vec;
416 #else
417                 dev->tx_pkt_burst = &axgbe_xmit_pkts;
418 #endif
419
420         return 0;
421 }
422
423 static void axgbe_txq_prepare_tx_stop(struct axgbe_port *pdata,
424                                       unsigned int queue)
425 {
426         unsigned int tx_status;
427         unsigned long tx_timeout;
428
429         /* The Tx engine cannot be stopped if it is actively processing
430          * packets. Wait for the Tx queue to empty the Tx fifo.  Don't
431          * wait forever though...
432          */
433         tx_timeout = rte_get_timer_cycles() + (AXGBE_DMA_STOP_TIMEOUT *
434                                                rte_get_timer_hz());
435         while (time_before(rte_get_timer_cycles(), tx_timeout)) {
436                 tx_status = AXGMAC_MTL_IOREAD(pdata, queue, MTL_Q_TQDR);
437                 if ((AXGMAC_GET_BITS(tx_status, MTL_Q_TQDR, TRCSTS) != 1) &&
438                     (AXGMAC_GET_BITS(tx_status, MTL_Q_TQDR, TXQSTS) == 0))
439                         break;
440
441                 rte_delay_us(900);
442         }
443
444         if (!time_before(rte_get_timer_cycles(), tx_timeout))
445                 PMD_DRV_LOG(ERR,
446                             "timed out waiting for Tx queue %u to empty\n",
447                             queue);
448 }
449
450 static void axgbe_prepare_tx_stop(struct axgbe_port *pdata,
451                                   unsigned int queue)
452 {
453         unsigned int tx_dsr, tx_pos, tx_qidx;
454         unsigned int tx_status;
455         unsigned long tx_timeout;
456
457         if (AXGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) > 0x20)
458                 return axgbe_txq_prepare_tx_stop(pdata, queue);
459
460         /* Calculate the status register to read and the position within */
461         if (queue < DMA_DSRX_FIRST_QUEUE) {
462                 tx_dsr = DMA_DSR0;
463                 tx_pos = (queue * DMA_DSR_Q_WIDTH) + DMA_DSR0_TPS_START;
464         } else {
465                 tx_qidx = queue - DMA_DSRX_FIRST_QUEUE;
466
467                 tx_dsr = DMA_DSR1 + ((tx_qidx / DMA_DSRX_QPR) * DMA_DSRX_INC);
468                 tx_pos = ((tx_qidx % DMA_DSRX_QPR) * DMA_DSR_Q_WIDTH) +
469                         DMA_DSRX_TPS_START;
470         }
471
472         /* The Tx engine cannot be stopped if it is actively processing
473          * descriptors. Wait for the Tx engine to enter the stopped or
474          * suspended state.  Don't wait forever though...
475          */
476         tx_timeout = rte_get_timer_cycles() + (AXGBE_DMA_STOP_TIMEOUT *
477                                                rte_get_timer_hz());
478         while (time_before(rte_get_timer_cycles(), tx_timeout)) {
479                 tx_status = AXGMAC_IOREAD(pdata, tx_dsr);
480                 tx_status = GET_BITS(tx_status, tx_pos, DMA_DSR_TPS_WIDTH);
481                 if ((tx_status == DMA_TPS_STOPPED) ||
482                     (tx_status == DMA_TPS_SUSPENDED))
483                         break;
484
485                 rte_delay_us(900);
486         }
487
488         if (!time_before(rte_get_timer_cycles(), tx_timeout))
489                 PMD_DRV_LOG(ERR,
490                             "timed out waiting for Tx DMA channel %u to stop\n",
491                             queue);
492 }
493
494 void axgbe_dev_disable_tx(struct rte_eth_dev *dev)
495 {
496         struct axgbe_tx_queue *txq;
497         struct axgbe_port *pdata = dev->data->dev_private;
498         unsigned int i;
499
500         /* Prepare for stopping DMA channel */
501         for (i = 0; i < pdata->tx_q_count; i++) {
502                 txq = dev->data->tx_queues[i];
503                 axgbe_prepare_tx_stop(pdata, i);
504         }
505         /* Disable MAC Tx */
506         AXGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
507         /* Disable each Tx queue*/
508         for (i = 0; i < pdata->tx_q_count; i++)
509                 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN,
510                                         0);
511         /* Disable each  Tx DMA channel */
512         for (i = 0; i < dev->data->nb_tx_queues; i++) {
513                 txq = dev->data->tx_queues[i];
514                 AXGMAC_DMA_IOWRITE_BITS(txq, DMA_CH_TCR, ST, 0);
515         }
516 }
517
518 void axgbe_dev_enable_tx(struct rte_eth_dev *dev)
519 {
520         struct axgbe_tx_queue *txq;
521         struct axgbe_port *pdata = dev->data->dev_private;
522         unsigned int i;
523
524         for (i = 0; i < dev->data->nb_tx_queues; i++) {
525                 txq = dev->data->tx_queues[i];
526                 /* Enable Tx DMA channel */
527                 AXGMAC_DMA_IOWRITE_BITS(txq, DMA_CH_TCR, ST, 1);
528         }
529         /* Enable Tx queue*/
530         for (i = 0; i < pdata->tx_q_count; i++)
531                 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN,
532                                         MTL_Q_ENABLED);
533         /* Enable MAC Tx */
534         AXGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
535 }
536
537 /* Free Tx conformed mbufs */
538 static void axgbe_xmit_cleanup(struct axgbe_tx_queue *txq)
539 {
540         volatile struct axgbe_tx_desc *desc;
541         uint16_t idx;
542
543         idx = AXGBE_GET_DESC_IDX(txq, txq->dirty);
544         while (txq->cur != txq->dirty) {
545                 if (unlikely(idx == txq->nb_desc))
546                         idx = 0;
547                 desc = &txq->desc[idx];
548                 /* Check for ownership */
549                 if (AXGMAC_GET_BITS_LE(desc->desc3, TX_NORMAL_DESC3, OWN))
550                         return;
551                 memset((void *)&desc->desc2, 0, 8);
552                 /* Free mbuf */
553                 rte_pktmbuf_free(txq->sw_ring[idx]);
554                 txq->sw_ring[idx++] = NULL;
555                 txq->dirty++;
556         }
557 }
558
559 /* Tx Descriptor formation
560  * Considering each mbuf requires one desc
561  * mbuf is linear
562  */
563 static int axgbe_xmit_hw(struct axgbe_tx_queue *txq,
564                          struct rte_mbuf *mbuf)
565 {
566         volatile struct axgbe_tx_desc *desc;
567         uint16_t idx;
568         uint64_t mask;
569
570         idx = AXGBE_GET_DESC_IDX(txq, txq->cur);
571         desc = &txq->desc[idx];
572
573         /* Update buffer address  and length */
574         desc->baddr = rte_mbuf_data_iova(mbuf);
575         AXGMAC_SET_BITS_LE(desc->desc2, TX_NORMAL_DESC2, HL_B1L,
576                            mbuf->pkt_len);
577         /* Total msg length to transmit */
578         AXGMAC_SET_BITS_LE(desc->desc3, TX_NORMAL_DESC3, FL,
579                            mbuf->pkt_len);
580         /* Mark it as First and Last Descriptor */
581         AXGMAC_SET_BITS_LE(desc->desc3, TX_NORMAL_DESC3, FD, 1);
582         AXGMAC_SET_BITS_LE(desc->desc3, TX_NORMAL_DESC3, LD, 1);
583         /* Mark it as a NORMAL descriptor */
584         AXGMAC_SET_BITS_LE(desc->desc3, TX_NORMAL_DESC3, CTXT, 0);
585         /* configure h/w Offload */
586         mask = mbuf->ol_flags & PKT_TX_L4_MASK;
587         if ((mask == PKT_TX_TCP_CKSUM) || (mask == PKT_TX_UDP_CKSUM))
588                 AXGMAC_SET_BITS_LE(desc->desc3, TX_NORMAL_DESC3, CIC, 0x3);
589         else if (mbuf->ol_flags & PKT_TX_IP_CKSUM)
590                 AXGMAC_SET_BITS_LE(desc->desc3, TX_NORMAL_DESC3, CIC, 0x1);
591         rte_wmb();
592
593         /* Set OWN bit */
594         AXGMAC_SET_BITS_LE(desc->desc3, TX_NORMAL_DESC3, OWN, 1);
595         rte_wmb();
596
597         /* Save mbuf */
598         txq->sw_ring[idx] = mbuf;
599         /* Update current index*/
600         txq->cur++;
601         /* Update stats */
602         txq->bytes += mbuf->pkt_len;
603
604         return 0;
605 }
606
607 /* Eal supported tx wrapper*/
608 uint16_t
609 axgbe_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
610                 uint16_t nb_pkts)
611 {
612         PMD_INIT_FUNC_TRACE();
613
614         if (unlikely(nb_pkts == 0))
615                 return nb_pkts;
616
617         struct axgbe_tx_queue *txq;
618         uint16_t nb_desc_free;
619         uint16_t nb_pkt_sent = 0;
620         uint16_t idx;
621         uint32_t tail_addr;
622         struct rte_mbuf *mbuf;
623
624         txq  = (struct axgbe_tx_queue *)tx_queue;
625         nb_desc_free = txq->nb_desc - (txq->cur - txq->dirty);
626
627         if (unlikely(nb_desc_free <= txq->free_thresh)) {
628                 axgbe_xmit_cleanup(txq);
629                 nb_desc_free = txq->nb_desc - (txq->cur - txq->dirty);
630                 if (unlikely(nb_desc_free == 0))
631                         return 0;
632         }
633         nb_pkts = RTE_MIN(nb_desc_free, nb_pkts);
634         while (nb_pkts--) {
635                 mbuf = *tx_pkts++;
636                 if (axgbe_xmit_hw(txq, mbuf))
637                         goto out;
638                 nb_pkt_sent++;
639         }
640 out:
641         /* Sync read and write */
642         rte_mb();
643         idx = AXGBE_GET_DESC_IDX(txq, txq->cur);
644         tail_addr = low32_value(txq->ring_phys_addr +
645                                 idx * sizeof(struct axgbe_tx_desc));
646         /* Update tail reg with next immediate address to kick Tx DMA channel*/
647         AXGMAC_DMA_IOWRITE(txq, DMA_CH_TDTR_LO, tail_addr);
648         txq->pkts += nb_pkt_sent;
649         return nb_pkt_sent;
650 }
651
652 void axgbe_dev_clear_queues(struct rte_eth_dev *dev)
653 {
654         PMD_INIT_FUNC_TRACE();
655         uint8_t i;
656         struct axgbe_rx_queue *rxq;
657         struct axgbe_tx_queue *txq;
658
659         for (i = 0; i < dev->data->nb_rx_queues; i++) {
660                 rxq = dev->data->rx_queues[i];
661
662                 if (rxq) {
663                         axgbe_rx_queue_release(rxq);
664                         dev->data->rx_queues[i] = NULL;
665                 }
666         }
667
668         for (i = 0; i < dev->data->nb_tx_queues; i++) {
669                 txq = dev->data->tx_queues[i];
670
671                 if (txq) {
672                         axgbe_tx_queue_release(txq);
673                         dev->data->tx_queues[i] = NULL;
674                 }
675         }
676 }