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.
6 #include "axgbe_ethdev.h"
7 #include "axgbe_common.h"
9 #include "axgbe_rxtx.h"
11 static uint32_t bitrev32(uint32_t x)
13 x = (x >> 16) | (x << 16);
14 x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
15 x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
16 x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
17 x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
21 /*MSB set bit from 32 to 1*/
22 static int get_lastbit_set(int x)
28 if (!(x & 0xffff0000)) {
32 if (!(x & 0xff000000)) {
36 if (!(x & 0xf0000000)) {
40 if (!(x & 0xc0000000)) {
44 if (!(x & 0x80000000)) {
51 static inline unsigned int axgbe_get_max_frame(struct axgbe_port *pdata)
53 return pdata->eth_dev->data->mtu + RTE_ETHER_HDR_LEN +
54 RTE_ETHER_CRC_LEN + VLAN_HLEN;
58 static int mdio_complete(struct axgbe_port *pdata)
60 if (!AXGMAC_IOREAD_BITS(pdata, MAC_MDIOSCCDR, BUSY))
66 static int axgbe_write_ext_mii_regs(struct axgbe_port *pdata, int addr,
69 unsigned int mdio_sca, mdio_sccd;
73 AXGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, REG, reg);
74 AXGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, DA, addr);
75 AXGMAC_IOWRITE(pdata, MAC_MDIOSCAR, mdio_sca);
78 AXGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, DATA, val);
79 AXGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, CMD, 1);
80 AXGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, BUSY, 1);
81 AXGMAC_IOWRITE(pdata, MAC_MDIOSCCDR, mdio_sccd);
83 timeout = rte_get_timer_cycles() + rte_get_timer_hz();
84 while (time_before(rte_get_timer_cycles(), timeout)) {
86 if (mdio_complete(pdata))
90 PMD_DRV_LOG(ERR, "Mdio write operation timed out\n");
94 static int axgbe_read_ext_mii_regs(struct axgbe_port *pdata, int addr,
97 unsigned int mdio_sca, mdio_sccd;
101 AXGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, REG, reg);
102 AXGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, DA, addr);
103 AXGMAC_IOWRITE(pdata, MAC_MDIOSCAR, mdio_sca);
106 AXGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, CMD, 3);
107 AXGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, BUSY, 1);
108 AXGMAC_IOWRITE(pdata, MAC_MDIOSCCDR, mdio_sccd);
110 timeout = rte_get_timer_cycles() + rte_get_timer_hz();
112 while (time_before(rte_get_timer_cycles(), timeout)) {
114 if (mdio_complete(pdata))
118 PMD_DRV_LOG(ERR, "Mdio read operation timed out\n");
122 return AXGMAC_IOREAD_BITS(pdata, MAC_MDIOSCCDR, DATA);
125 static int axgbe_set_ext_mii_mode(struct axgbe_port *pdata, unsigned int port,
126 enum axgbe_mdio_mode mode)
128 unsigned int reg_val = 0;
131 case AXGBE_MDIO_MODE_CL22:
132 if (port > AXGMAC_MAX_C22_PORT)
134 reg_val |= (1 << port);
136 case AXGBE_MDIO_MODE_CL45:
141 AXGMAC_IOWRITE(pdata, MAC_MDIOCL22R, reg_val);
146 static int axgbe_read_mmd_regs_v2(struct axgbe_port *pdata,
147 int prtad __rte_unused, int mmd_reg)
149 unsigned int mmd_address, index, offset;
152 if (mmd_reg & MII_ADDR_C45)
153 mmd_address = mmd_reg & ~MII_ADDR_C45;
155 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
157 /* The PCS registers are accessed using mmio. The underlying
158 * management interface uses indirect addressing to access the MMD
159 * register sets. This requires accessing of the PCS register in two
160 * phases, an address phase and a data phase.
162 * The mmio interface is based on 16-bit offsets and values. All
163 * register offsets must therefore be adjusted by left shifting the
164 * offset 1 bit and reading 16 bits of data.
167 index = mmd_address & ~pdata->xpcs_window_mask;
168 offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
170 pthread_mutex_lock(&pdata->xpcs_mutex);
172 XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
173 mmd_data = XPCS16_IOREAD(pdata, offset);
175 pthread_mutex_unlock(&pdata->xpcs_mutex);
180 static void axgbe_write_mmd_regs_v2(struct axgbe_port *pdata,
181 int prtad __rte_unused,
182 int mmd_reg, int mmd_data)
184 unsigned int mmd_address, index, offset;
186 if (mmd_reg & MII_ADDR_C45)
187 mmd_address = mmd_reg & ~MII_ADDR_C45;
189 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
191 /* The PCS registers are accessed using mmio. The underlying
192 * management interface uses indirect addressing to access the MMD
193 * register sets. This requires accessing of the PCS register in two
194 * phases, an address phase and a data phase.
196 * The mmio interface is based on 16-bit offsets and values. All
197 * register offsets must therefore be adjusted by left shifting the
198 * offset 1 bit and writing 16 bits of data.
201 index = mmd_address & ~pdata->xpcs_window_mask;
202 offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
204 pthread_mutex_lock(&pdata->xpcs_mutex);
206 XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
207 XPCS16_IOWRITE(pdata, offset, mmd_data);
209 pthread_mutex_unlock(&pdata->xpcs_mutex);
212 static int axgbe_read_mmd_regs(struct axgbe_port *pdata, int prtad,
215 switch (pdata->vdata->xpcs_access) {
216 case AXGBE_XPCS_ACCESS_V1:
217 PMD_DRV_LOG(ERR, "PHY_Version 1 is not supported\n");
219 case AXGBE_XPCS_ACCESS_V2:
221 return axgbe_read_mmd_regs_v2(pdata, prtad, mmd_reg);
225 static void axgbe_write_mmd_regs(struct axgbe_port *pdata, int prtad,
226 int mmd_reg, int mmd_data)
228 switch (pdata->vdata->xpcs_access) {
229 case AXGBE_XPCS_ACCESS_V1:
230 PMD_DRV_LOG(ERR, "PHY_Version 1 is not supported\n");
232 case AXGBE_XPCS_ACCESS_V2:
234 return axgbe_write_mmd_regs_v2(pdata, prtad, mmd_reg, mmd_data);
238 static int axgbe_set_speed(struct axgbe_port *pdata, int speed)
256 if (AXGMAC_IOREAD_BITS(pdata, MAC_TCR, SS) != ss)
257 AXGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, ss);
262 static int axgbe_disable_tx_flow_control(struct axgbe_port *pdata)
264 unsigned int max_q_count, q_count;
265 unsigned int reg, reg_val;
268 /* Clear MTL flow control */
269 for (i = 0; i < pdata->rx_q_count; i++)
270 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, 0);
272 /* Clear MAC flow control */
273 max_q_count = AXGMAC_MAX_FLOW_CONTROL_QUEUES;
274 q_count = RTE_MIN(pdata->tx_q_count,
277 for (i = 0; i < q_count; i++) {
278 reg_val = AXGMAC_IOREAD(pdata, reg);
279 AXGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 0);
280 AXGMAC_IOWRITE(pdata, reg, reg_val);
282 reg += MAC_QTFCR_INC;
288 static int axgbe_enable_tx_flow_control(struct axgbe_port *pdata)
290 unsigned int max_q_count, q_count;
291 unsigned int reg, reg_val;
294 /* Set MTL flow control */
295 for (i = 0; i < pdata->rx_q_count; i++) {
296 unsigned int ehfc = 0;
298 /* Flow control thresholds are established */
299 if (pdata->rx_rfd[i])
302 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, ehfc);
304 PMD_DRV_LOG(DEBUG, "flow control %s for RXq%u\n",
305 ehfc ? "enabled" : "disabled", i);
308 /* Set MAC flow control */
309 max_q_count = AXGMAC_MAX_FLOW_CONTROL_QUEUES;
310 q_count = RTE_MIN(pdata->tx_q_count,
313 for (i = 0; i < q_count; i++) {
314 reg_val = AXGMAC_IOREAD(pdata, reg);
316 /* Enable transmit flow control */
317 AXGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 1);
319 AXGMAC_SET_BITS(reg_val, MAC_Q0TFCR, PT, 0xffff);
321 AXGMAC_IOWRITE(pdata, reg, reg_val);
323 reg += MAC_QTFCR_INC;
329 static int axgbe_disable_rx_flow_control(struct axgbe_port *pdata)
331 AXGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 0);
336 static int axgbe_enable_rx_flow_control(struct axgbe_port *pdata)
338 AXGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 1);
343 static int axgbe_config_tx_flow_control(struct axgbe_port *pdata)
346 axgbe_enable_tx_flow_control(pdata);
348 axgbe_disable_tx_flow_control(pdata);
353 static int axgbe_config_rx_flow_control(struct axgbe_port *pdata)
356 axgbe_enable_rx_flow_control(pdata);
358 axgbe_disable_rx_flow_control(pdata);
363 static void axgbe_config_flow_control(struct axgbe_port *pdata)
365 axgbe_config_tx_flow_control(pdata);
366 axgbe_config_rx_flow_control(pdata);
368 AXGMAC_IOWRITE_BITS(pdata, MAC_RFCR, PFCE, 0);
371 static void axgbe_queue_flow_control_threshold(struct axgbe_port *pdata,
373 unsigned int q_fifo_size)
375 unsigned int frame_fifo_size;
376 unsigned int rfa, rfd;
378 frame_fifo_size = AXGMAC_FLOW_CONTROL_ALIGN(axgbe_get_max_frame(pdata));
380 /* This path deals with just maximum frame sizes which are
381 * limited to a jumbo frame of 9,000 (plus headers, etc.)
382 * so we can never exceed the maximum allowable RFA/RFD
385 if (q_fifo_size <= 2048) {
386 /* rx_rfd to zero to signal no flow control */
387 pdata->rx_rfa[queue] = 0;
388 pdata->rx_rfd[queue] = 0;
392 if (q_fifo_size <= 4096) {
393 /* Between 2048 and 4096 */
394 pdata->rx_rfa[queue] = 0; /* Full - 1024 bytes */
395 pdata->rx_rfd[queue] = 1; /* Full - 1536 bytes */
399 if (q_fifo_size <= frame_fifo_size) {
400 /* Between 4096 and max-frame */
401 pdata->rx_rfa[queue] = 2; /* Full - 2048 bytes */
402 pdata->rx_rfd[queue] = 5; /* Full - 3584 bytes */
406 if (q_fifo_size <= (frame_fifo_size * 3)) {
407 /* Between max-frame and 3 max-frames,
408 * trigger if we get just over a frame of data and
409 * resume when we have just under half a frame left.
411 rfa = q_fifo_size - frame_fifo_size;
412 rfd = rfa + (frame_fifo_size / 2);
414 /* Above 3 max-frames - trigger when just over
415 * 2 frames of space available
417 rfa = frame_fifo_size * 2;
418 rfa += AXGMAC_FLOW_CONTROL_UNIT;
419 rfd = rfa + frame_fifo_size;
422 pdata->rx_rfa[queue] = AXGMAC_FLOW_CONTROL_VALUE(rfa);
423 pdata->rx_rfd[queue] = AXGMAC_FLOW_CONTROL_VALUE(rfd);
426 static void axgbe_calculate_flow_control_threshold(struct axgbe_port *pdata)
428 unsigned int q_fifo_size;
431 for (i = 0; i < pdata->rx_q_count; i++) {
432 q_fifo_size = (pdata->fifo + 1) * AXGMAC_FIFO_UNIT;
434 axgbe_queue_flow_control_threshold(pdata, i, q_fifo_size);
438 static void axgbe_config_flow_control_threshold(struct axgbe_port *pdata)
442 for (i = 0; i < pdata->rx_q_count; i++) {
443 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFA,
445 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFD,
450 static int axgbe_enable_rx_vlan_stripping(struct axgbe_port *pdata)
452 /* Put the VLAN tag in the Rx descriptor */
453 AXGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLRXS, 1);
455 /* Don't check the VLAN type */
456 AXGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, DOVLTC, 1);
458 /* Check only C-TAG (0x8100) packets */
459 AXGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ERSVLM, 0);
461 /* Don't consider an S-TAG (0x88A8) packet as a VLAN packet */
462 AXGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ESVL, 0);
464 /* Enable VLAN tag stripping */
465 AXGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0x3);
469 static int axgbe_disable_rx_vlan_stripping(struct axgbe_port *pdata)
471 AXGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0);
475 static int axgbe_enable_rx_vlan_filtering(struct axgbe_port *pdata)
477 /* Enable VLAN filtering */
478 AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 1);
480 /* Enable VLAN Hash Table filtering */
481 AXGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTHM, 1);
483 /* Disable VLAN tag inverse matching */
484 AXGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTIM, 0);
486 /* Only filter on the lower 12-bits of the VLAN tag */
487 AXGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ETV, 1);
489 /* In order for the VLAN Hash Table filtering to be effective,
490 * the VLAN tag identifier in the VLAN Tag Register must not
491 * be zero. Set the VLAN tag identifier to "1" to enable the
492 * VLAN Hash Table filtering. This implies that a VLAN tag of
493 * 1 will always pass filtering.
495 AXGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VL, 1);
499 static int axgbe_disable_rx_vlan_filtering(struct axgbe_port *pdata)
501 /* Disable VLAN filtering */
502 AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 0);
506 static u32 axgbe_vid_crc32_le(__le16 vid_le)
508 u32 poly = 0xedb88320; /* CRCPOLY_LE */
511 unsigned char *data = (unsigned char *)&vid_le;
512 unsigned char data_byte = 0;
515 bits = get_lastbit_set(VLAN_VID_MASK);
516 for (i = 0; i < bits; i++) {
518 data_byte = data[i / 8];
520 temp = ((crc & 1) ^ data_byte) & 1;
530 static int axgbe_update_vlan_hash_table(struct axgbe_port *pdata)
535 u16 vlan_hash_table = 0;
536 unsigned int reg = 0;
537 unsigned long vid_idx, vid_valid;
539 /* Generate the VLAN Hash Table value */
540 for (vid = 0; vid < VLAN_N_VID; vid++) {
541 vid_idx = VLAN_TABLE_IDX(vid);
542 vid_valid = pdata->active_vlans[vid_idx];
543 vid_valid = (unsigned long)vid_valid >> (vid - (64 * vid_idx));
546 "vid:%d pdata->active_vlans[%ld]=0x%lx\n",
547 vid, vid_idx, pdata->active_vlans[vid_idx]);
551 vid_le = rte_cpu_to_le_16(vid);
552 crc = bitrev32(~axgbe_vid_crc32_le(vid_le)) >> 28;
553 vlan_hash_table |= (1 << crc);
554 PMD_DRV_LOG(DEBUG, "crc = %d vlan_hash_table = 0x%x\n",
555 crc, vlan_hash_table);
557 /* Set the VLAN Hash Table filtering register */
558 AXGMAC_IOWRITE_BITS(pdata, MAC_VLANHTR, VLHT, vlan_hash_table);
559 reg = AXGMAC_IOREAD(pdata, MAC_VLANHTR);
560 PMD_DRV_LOG(DEBUG, "vlan_hash_table reg val = 0x%x\n", reg);
564 static int __axgbe_exit(struct axgbe_port *pdata)
566 unsigned int count = 2000;
568 /* Issue a software reset */
569 AXGMAC_IOWRITE_BITS(pdata, DMA_MR, SWR, 1);
572 /* Poll Until Poll Condition */
573 while (--count && AXGMAC_IOREAD_BITS(pdata, DMA_MR, SWR))
582 static int axgbe_exit(struct axgbe_port *pdata)
586 /* To guard against possible incorrectly generated interrupts,
587 * issue the software reset twice.
589 ret = __axgbe_exit(pdata);
593 return __axgbe_exit(pdata);
596 static int axgbe_flush_tx_queues(struct axgbe_port *pdata)
598 unsigned int i, count;
600 if (AXGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) < 0x21)
603 for (i = 0; i < pdata->tx_q_count; i++)
604 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, FTQ, 1);
606 /* Poll Until Poll Condition */
607 for (i = 0; i < pdata->tx_q_count; i++) {
609 while (--count && AXGMAC_MTL_IOREAD_BITS(pdata, i,
620 static void axgbe_config_dma_bus(struct axgbe_port *pdata)
622 /* Set enhanced addressing mode */
623 AXGMAC_IOWRITE_BITS(pdata, DMA_SBMR, EAME, 1);
625 /* Out standing read/write requests*/
626 AXGMAC_IOWRITE_BITS(pdata, DMA_SBMR, RD_OSR, 0x3f);
627 AXGMAC_IOWRITE_BITS(pdata, DMA_SBMR, WR_OSR, 0x3f);
629 /* Set the System Bus mode */
630 AXGMAC_IOWRITE_BITS(pdata, DMA_SBMR, UNDEF, 1);
631 AXGMAC_IOWRITE_BITS(pdata, DMA_SBMR, BLEN_32, 1);
632 AXGMAC_IOWRITE_BITS(pdata, DMA_SBMR, AAL, 1);
635 static void axgbe_config_dma_cache(struct axgbe_port *pdata)
637 unsigned int arcache, awcache, arwcache;
640 AXGMAC_SET_BITS(arcache, DMA_AXIARCR, DRC, 0x3);
641 AXGMAC_IOWRITE(pdata, DMA_AXIARCR, arcache);
644 AXGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWC, 0x3);
645 AXGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPC, 0x3);
646 AXGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPD, 0x1);
647 AXGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHC, 0x3);
648 AXGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHD, 0x1);
649 AXGMAC_SET_BITS(awcache, DMA_AXIAWCR, RDC, 0x3);
650 AXGMAC_SET_BITS(awcache, DMA_AXIAWCR, RDD, 0x1);
651 AXGMAC_IOWRITE(pdata, DMA_AXIAWCR, awcache);
654 AXGMAC_SET_BITS(arwcache, DMA_AXIAWRCR, TDWD, 0x1);
655 AXGMAC_SET_BITS(arwcache, DMA_AXIAWRCR, TDWC, 0x3);
656 AXGMAC_SET_BITS(arwcache, DMA_AXIAWRCR, RDRC, 0x3);
657 AXGMAC_IOWRITE(pdata, DMA_AXIAWRCR, arwcache);
660 static void axgbe_config_edma_control(struct axgbe_port *pdata)
662 AXGMAC_IOWRITE(pdata, EDMA_TX_CONTROL, 0x5);
663 AXGMAC_IOWRITE(pdata, EDMA_RX_CONTROL, 0x5);
666 static int axgbe_config_osp_mode(struct axgbe_port *pdata)
668 /* Force DMA to operate on second packet before closing descriptors
671 struct axgbe_tx_queue *txq;
674 for (i = 0; i < pdata->eth_dev->data->nb_tx_queues; i++) {
675 txq = pdata->eth_dev->data->tx_queues[i];
676 AXGMAC_DMA_IOWRITE_BITS(txq, DMA_CH_TCR, OSP,
683 static int axgbe_config_pblx8(struct axgbe_port *pdata)
685 struct axgbe_tx_queue *txq;
688 for (i = 0; i < pdata->eth_dev->data->nb_tx_queues; i++) {
689 txq = pdata->eth_dev->data->tx_queues[i];
690 AXGMAC_DMA_IOWRITE_BITS(txq, DMA_CH_CR, PBLX8,
696 static int axgbe_config_tx_pbl_val(struct axgbe_port *pdata)
698 struct axgbe_tx_queue *txq;
701 for (i = 0; i < pdata->eth_dev->data->nb_tx_queues; i++) {
702 txq = pdata->eth_dev->data->tx_queues[i];
703 AXGMAC_DMA_IOWRITE_BITS(txq, DMA_CH_TCR, PBL,
710 static int axgbe_config_rx_pbl_val(struct axgbe_port *pdata)
712 struct axgbe_rx_queue *rxq;
715 for (i = 0; i < pdata->eth_dev->data->nb_rx_queues; i++) {
716 rxq = pdata->eth_dev->data->rx_queues[i];
717 AXGMAC_DMA_IOWRITE_BITS(rxq, DMA_CH_RCR, PBL,
724 static void axgbe_config_rx_buffer_size(struct axgbe_port *pdata)
726 struct axgbe_rx_queue *rxq;
729 for (i = 0; i < pdata->eth_dev->data->nb_rx_queues; i++) {
730 rxq = pdata->eth_dev->data->rx_queues[i];
732 rxq->buf_size = rte_pktmbuf_data_room_size(rxq->mb_pool) -
733 RTE_PKTMBUF_HEADROOM;
734 rxq->buf_size = (rxq->buf_size + AXGBE_RX_BUF_ALIGN - 1) &
735 ~(AXGBE_RX_BUF_ALIGN - 1);
737 if (rxq->buf_size > pdata->rx_buf_size)
738 pdata->rx_buf_size = rxq->buf_size;
740 AXGMAC_DMA_IOWRITE_BITS(rxq, DMA_CH_RCR, RBSZ,
745 static int axgbe_write_rss_reg(struct axgbe_port *pdata, unsigned int type,
746 unsigned int index, unsigned int val)
750 if (AXGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB))
753 AXGMAC_IOWRITE(pdata, MAC_RSSDR, val);
755 AXGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, RSSIA, index);
756 AXGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, ADDRT, type);
757 AXGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, CT, 0);
758 AXGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, OB, 1);
762 if (!AXGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB))
771 int axgbe_write_rss_hash_key(struct axgbe_port *pdata)
773 struct rte_eth_rss_conf *rss_conf;
774 unsigned int key_regs = sizeof(pdata->rss_key) / sizeof(u32);
778 rss_conf = &pdata->eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
780 if (!rss_conf->rss_key)
781 key = (unsigned int *)&pdata->rss_key;
783 key = (unsigned int *)&rss_conf->rss_key;
786 ret = axgbe_write_rss_reg(pdata, AXGBE_RSS_HASH_KEY_TYPE,
795 int axgbe_write_rss_lookup_table(struct axgbe_port *pdata)
800 for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++) {
801 ret = axgbe_write_rss_reg(pdata,
802 AXGBE_RSS_LOOKUP_TABLE_TYPE, i,
803 pdata->rss_table[i]);
811 static int axgbe_enable_rss(struct axgbe_port *pdata)
815 /* Program the hash key */
816 ret = axgbe_write_rss_hash_key(pdata);
820 /* Program the lookup table */
821 ret = axgbe_write_rss_lookup_table(pdata);
825 /* Set the RSS options */
826 AXGMAC_IOWRITE(pdata, MAC_RSSCR, pdata->rss_options);
829 AXGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 1);
834 static void axgbe_rss_options(struct axgbe_port *pdata)
836 struct rte_eth_rss_conf *rss_conf;
839 rss_conf = &pdata->eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
840 pdata->rss_hf = rss_conf->rss_hf;
841 rss_hf = rss_conf->rss_hf;
843 if (rss_hf & (ETH_RSS_IPV4 | ETH_RSS_IPV6))
844 AXGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, IP2TE, 1);
845 if (rss_hf & (ETH_RSS_NONFRAG_IPV4_TCP | ETH_RSS_NONFRAG_IPV6_TCP))
846 AXGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, TCP4TE, 1);
847 if (rss_hf & (ETH_RSS_NONFRAG_IPV4_UDP | ETH_RSS_NONFRAG_IPV6_UDP))
848 AXGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, UDP4TE, 1);
851 static int axgbe_config_rss(struct axgbe_port *pdata)
855 if (pdata->rss_enable) {
856 /* Initialize RSS hash key and lookup table */
857 uint32_t *key = (uint32_t *)pdata->rss_key;
859 for (i = 0; i < sizeof(pdata->rss_key) / 4; i++)
860 *key++ = (uint32_t)rte_rand();
861 for (i = 0; i < AXGBE_RSS_MAX_TABLE_SIZE; i++)
862 AXGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH,
863 i % pdata->eth_dev->data->nb_rx_queues);
864 axgbe_rss_options(pdata);
865 if (axgbe_enable_rss(pdata)) {
866 PMD_DRV_LOG(ERR, "Error in enabling RSS support\n");
870 AXGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 0);
876 static void axgbe_enable_dma_interrupts(struct axgbe_port *pdata)
878 struct axgbe_tx_queue *txq;
879 unsigned int dma_ch_isr, dma_ch_ier;
882 for (i = 0; i < pdata->eth_dev->data->nb_tx_queues; i++) {
883 txq = pdata->eth_dev->data->tx_queues[i];
885 /* Clear all the interrupts which are set */
886 dma_ch_isr = AXGMAC_DMA_IOREAD(txq, DMA_CH_SR);
887 AXGMAC_DMA_IOWRITE(txq, DMA_CH_SR, dma_ch_isr);
889 /* Clear all interrupt enable bits */
892 /* Enable following interrupts
893 * NIE - Normal Interrupt Summary Enable
894 * AIE - Abnormal Interrupt Summary Enable
895 * FBEE - Fatal Bus Error Enable
897 AXGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, NIE, 0);
898 AXGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, AIE, 1);
899 AXGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
901 /* Enable following Rx interrupts
902 * RBUE - Receive Buffer Unavailable Enable
903 * RIE - Receive Interrupt Enable (unless using
904 * per channel interrupts in edge triggered
907 AXGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 0);
909 AXGMAC_DMA_IOWRITE(txq, DMA_CH_IER, dma_ch_ier);
913 static void wrapper_tx_desc_init(struct axgbe_port *pdata)
915 struct axgbe_tx_queue *txq;
918 for (i = 0; i < pdata->eth_dev->data->nb_tx_queues; i++) {
919 txq = pdata->eth_dev->data->tx_queues[i];
922 /* Update the total number of Tx descriptors */
923 AXGMAC_DMA_IOWRITE(txq, DMA_CH_TDRLR, txq->nb_desc - 1);
924 /* Update the starting address of descriptor ring */
925 AXGMAC_DMA_IOWRITE(txq, DMA_CH_TDLR_HI,
926 high32_value(txq->ring_phys_addr));
927 AXGMAC_DMA_IOWRITE(txq, DMA_CH_TDLR_LO,
928 low32_value(txq->ring_phys_addr));
932 static int wrapper_rx_desc_init(struct axgbe_port *pdata)
934 struct axgbe_rx_queue *rxq;
935 struct rte_mbuf *mbuf;
936 volatile union axgbe_rx_desc *desc;
939 for (i = 0; i < pdata->eth_dev->data->nb_rx_queues; i++) {
940 rxq = pdata->eth_dev->data->rx_queues[i];
942 /* Initialize software ring entries */
946 desc = AXGBE_GET_DESC_PT(rxq, 0);
948 for (j = 0; j < rxq->nb_desc; j++) {
949 mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
951 PMD_DRV_LOG(ERR, "RX mbuf alloc failed queue_id = %u, idx = %d\n",
952 (unsigned int)rxq->queue_id, j);
953 axgbe_dev_rx_queue_release(pdata->eth_dev, i);
956 rxq->sw_ring[j] = mbuf;
959 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
961 mbuf->port = rxq->port_id;
964 rte_mbuf_data_iova_default(mbuf));
966 AXGMAC_SET_BITS_LE(desc->read.desc3,
967 RX_NORMAL_DESC3, OWN, 1);
972 /* Update the total number of Rx descriptors */
973 AXGMAC_DMA_IOWRITE(rxq, DMA_CH_RDRLR,
975 /* Update the starting address of descriptor ring */
976 AXGMAC_DMA_IOWRITE(rxq, DMA_CH_RDLR_HI,
977 high32_value(rxq->ring_phys_addr));
978 AXGMAC_DMA_IOWRITE(rxq, DMA_CH_RDLR_LO,
979 low32_value(rxq->ring_phys_addr));
980 /* Update the Rx Descriptor Tail Pointer */
981 AXGMAC_DMA_IOWRITE(rxq, DMA_CH_RDTR_LO,
982 low32_value(rxq->ring_phys_addr +
984 sizeof(union axgbe_rx_desc)));
989 static void axgbe_config_mtl_mode(struct axgbe_port *pdata)
993 /* Set Tx to weighted round robin scheduling algorithm */
994 AXGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_WRR);
996 /* Set Tx traffic classes to use WRR algorithm with equal weights */
997 for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
998 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
1000 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW, 1);
1003 /* Set Rx to strict priority algorithm */
1004 AXGMAC_IOWRITE_BITS(pdata, MTL_OMR, RAA, MTL_RAA_SP);
1007 static int axgbe_config_tsf_mode(struct axgbe_port *pdata, unsigned int val)
1011 for (i = 0; i < pdata->tx_q_count; i++)
1012 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TSF, val);
1017 static int axgbe_config_rsf_mode(struct axgbe_port *pdata, unsigned int val)
1021 for (i = 0; i < pdata->rx_q_count; i++)
1022 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RSF, val);
1027 static int axgbe_config_tx_threshold(struct axgbe_port *pdata,
1032 for (i = 0; i < pdata->tx_q_count; i++)
1033 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TTC, val);
1038 static int axgbe_config_rx_threshold(struct axgbe_port *pdata,
1043 for (i = 0; i < pdata->rx_q_count; i++)
1044 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RTC, val);
1049 /*Distrubting fifo size */
1050 static void axgbe_config_rx_fifo_size(struct axgbe_port *pdata)
1052 unsigned int fifo_size;
1053 unsigned int q_fifo_size;
1054 unsigned int p_fifo, i;
1056 fifo_size = RTE_MIN(pdata->rx_max_fifo_size,
1057 pdata->hw_feat.rx_fifo_size);
1058 q_fifo_size = fifo_size / pdata->rx_q_count;
1060 /* Calculate the fifo setting by dividing the queue's fifo size
1061 * by the fifo allocation increment (with 0 representing the
1062 * base allocation increment so decrement the result
1065 p_fifo = q_fifo_size / AXGMAC_FIFO_UNIT;
1069 for (i = 0; i < pdata->rx_q_count; i++)
1070 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RQS, p_fifo);
1071 pdata->fifo = p_fifo;
1073 /*Calculate and config Flow control threshold*/
1074 axgbe_calculate_flow_control_threshold(pdata);
1075 axgbe_config_flow_control_threshold(pdata);
1077 PMD_DRV_LOG(DEBUG, "%d Rx hardware queues, %d byte fifo per queue\n",
1078 pdata->rx_q_count, q_fifo_size);
1081 static void axgbe_config_tx_fifo_size(struct axgbe_port *pdata)
1083 unsigned int fifo_size;
1084 unsigned int q_fifo_size;
1085 unsigned int p_fifo, i;
1087 fifo_size = RTE_MIN(pdata->tx_max_fifo_size,
1088 pdata->hw_feat.tx_fifo_size);
1089 q_fifo_size = fifo_size / pdata->tx_q_count;
1091 /* Calculate the fifo setting by dividing the queue's fifo size
1092 * by the fifo allocation increment (with 0 representing the
1093 * base allocation increment so decrement the result
1096 p_fifo = q_fifo_size / AXGMAC_FIFO_UNIT;
1100 for (i = 0; i < pdata->tx_q_count; i++)
1101 AXGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TQS, p_fifo);
1103 PMD_DRV_LOG(DEBUG, "%d Tx hardware queues, %d byte fifo per queue\n",
1104 pdata->tx_q_count, q_fifo_size);
1107 static void axgbe_config_queue_mapping(struct axgbe_port *pdata)
1109 unsigned int qptc, qptc_extra, queue;
1110 unsigned int i, j, reg, reg_val;
1112 /* Map the MTL Tx Queues to Traffic Classes
1113 * Note: Tx Queues >= Traffic Classes
1115 qptc = pdata->tx_q_count / pdata->hw_feat.tc_cnt;
1116 qptc_extra = pdata->tx_q_count % pdata->hw_feat.tc_cnt;
1118 for (i = 0, queue = 0; i < pdata->hw_feat.tc_cnt; i++) {
1119 for (j = 0; j < qptc; j++) {
1120 PMD_DRV_LOG(DEBUG, "TXq%u mapped to TC%u\n", queue, i);
1121 AXGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
1124 if (i < qptc_extra) {
1125 PMD_DRV_LOG(DEBUG, "TXq%u mapped to TC%u\n", queue, i);
1126 AXGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
1131 if (pdata->rss_enable) {
1132 /* Select dynamic mapping of MTL Rx queue to DMA Rx channel */
1135 for (i = 0; i < pdata->rx_q_count;) {
1136 reg_val |= (0x80 << ((i++ % MTL_RQDCM_Q_PER_REG) << 3));
1138 if ((i % MTL_RQDCM_Q_PER_REG) &&
1139 (i != pdata->rx_q_count))
1142 AXGMAC_IOWRITE(pdata, reg, reg_val);
1144 reg += MTL_RQDCM_INC;
1150 static void axgbe_enable_mtl_interrupts(struct axgbe_port *pdata)
1152 unsigned int mtl_q_isr;
1153 unsigned int q_count, i;
1155 q_count = RTE_MAX(pdata->hw_feat.tx_q_cnt, pdata->hw_feat.rx_q_cnt);
1156 for (i = 0; i < q_count; i++) {
1157 /* Clear all the interrupts which are set */
1158 mtl_q_isr = AXGMAC_MTL_IOREAD(pdata, i, MTL_Q_ISR);
1159 AXGMAC_MTL_IOWRITE(pdata, i, MTL_Q_ISR, mtl_q_isr);
1161 /* No MTL interrupts to be enabled */
1162 AXGMAC_MTL_IOWRITE(pdata, i, MTL_Q_IER, 0);
1166 static uint32_t crc32_le(uint32_t crc, uint8_t *p, uint32_t len)
1171 for (i = 0; i < 8; i++)
1172 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
1177 void axgbe_set_mac_hash_table(struct axgbe_port *pdata, u8 *addr, bool add)
1179 uint32_t crc, htable_index, htable_bitmask;
1181 crc = bitrev32(~crc32_le(~0, addr, RTE_ETHER_ADDR_LEN));
1182 crc >>= pdata->hash_table_shift;
1183 htable_index = crc >> 5;
1184 htable_bitmask = 1 << (crc & 0x1f);
1187 pdata->uc_hash_table[htable_index] |= htable_bitmask;
1188 pdata->uc_hash_mac_addr++;
1190 pdata->uc_hash_table[htable_index] &= ~htable_bitmask;
1191 pdata->uc_hash_mac_addr--;
1193 PMD_DRV_LOG(DEBUG, "%s MAC hash table Bit %d at Index %#x\n",
1194 add ? "set" : "clear", (crc & 0x1f), htable_index);
1196 AXGMAC_IOWRITE(pdata, MAC_HTR(htable_index),
1197 pdata->uc_hash_table[htable_index]);
1200 void axgbe_set_mac_addn_addr(struct axgbe_port *pdata, u8 *addr, uint32_t index)
1202 unsigned int mac_addr_hi, mac_addr_lo;
1209 mac_addr = (u8 *)&mac_addr_lo;
1210 mac_addr[0] = addr[0];
1211 mac_addr[1] = addr[1];
1212 mac_addr[2] = addr[2];
1213 mac_addr[3] = addr[3];
1214 mac_addr = (u8 *)&mac_addr_hi;
1215 mac_addr[0] = addr[4];
1216 mac_addr[1] = addr[5];
1218 /*Address Enable: Use this Addr for Perfect Filtering */
1219 AXGMAC_SET_BITS(mac_addr_hi, MAC_MACA1HR, AE, 1);
1222 PMD_DRV_LOG(DEBUG, "%s mac address at %#x\n",
1223 addr ? "set" : "clear", index);
1225 AXGMAC_IOWRITE(pdata, MAC_MACAHR(index), mac_addr_hi);
1226 AXGMAC_IOWRITE(pdata, MAC_MACALR(index), mac_addr_lo);
1229 static int axgbe_set_mac_address(struct axgbe_port *pdata, u8 *addr)
1231 unsigned int mac_addr_hi, mac_addr_lo;
1233 mac_addr_hi = (addr[5] << 8) | (addr[4] << 0);
1234 mac_addr_lo = (addr[3] << 24) | (addr[2] << 16) |
1235 (addr[1] << 8) | (addr[0] << 0);
1237 AXGMAC_IOWRITE(pdata, MAC_MACA0HR, mac_addr_hi);
1238 AXGMAC_IOWRITE(pdata, MAC_MACA0LR, mac_addr_lo);
1243 static void axgbe_config_mac_hash_table(struct axgbe_port *pdata)
1245 struct axgbe_hw_features *hw_feat = &pdata->hw_feat;
1247 pdata->hash_table_shift = 0;
1248 pdata->hash_table_count = 0;
1249 pdata->uc_hash_mac_addr = 0;
1250 memset(pdata->uc_hash_table, 0, sizeof(pdata->uc_hash_table));
1252 if (hw_feat->hash_table_size) {
1253 pdata->hash_table_shift = 26 - (hw_feat->hash_table_size >> 7);
1254 pdata->hash_table_count = hw_feat->hash_table_size / 32;
1258 static void axgbe_config_mac_address(struct axgbe_port *pdata)
1260 axgbe_set_mac_address(pdata, pdata->mac_addr.addr_bytes);
1263 static void axgbe_config_jumbo_enable(struct axgbe_port *pdata)
1267 val = (pdata->rx_buf_size > AXGMAC_STD_PACKET_MTU) ? 1 : 0;
1269 AXGMAC_IOWRITE_BITS(pdata, MAC_RCR, JE, val);
1272 static void axgbe_config_mac_speed(struct axgbe_port *pdata)
1274 axgbe_set_speed(pdata, pdata->phy_speed);
1277 static void axgbe_config_checksum_offload(struct axgbe_port *pdata)
1279 if (pdata->rx_csum_enable)
1280 AXGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 1);
1282 AXGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 0);
1285 static void axgbe_config_mmc(struct axgbe_port *pdata)
1287 struct axgbe_mmc_stats *stats = &pdata->mmc_stats;
1290 memset(stats, 0, sizeof(*stats));
1292 /* Set counters to reset on read */
1293 AXGMAC_IOWRITE_BITS(pdata, MMC_CR, ROR, 1);
1295 /* Reset the counters */
1296 AXGMAC_IOWRITE_BITS(pdata, MMC_CR, CR, 1);
1299 static int axgbe_init(struct axgbe_port *pdata)
1303 /* Flush Tx queues */
1304 ret = axgbe_flush_tx_queues(pdata);
1307 /* Initialize DMA related features */
1308 axgbe_config_dma_bus(pdata);
1309 axgbe_config_dma_cache(pdata);
1310 axgbe_config_edma_control(pdata);
1311 axgbe_config_osp_mode(pdata);
1312 axgbe_config_pblx8(pdata);
1313 axgbe_config_tx_pbl_val(pdata);
1314 axgbe_config_rx_pbl_val(pdata);
1315 axgbe_config_rx_buffer_size(pdata);
1316 axgbe_config_rss(pdata);
1317 wrapper_tx_desc_init(pdata);
1318 ret = wrapper_rx_desc_init(pdata);
1321 axgbe_enable_dma_interrupts(pdata);
1323 /* Initialize MTL related features */
1324 axgbe_config_mtl_mode(pdata);
1325 axgbe_config_queue_mapping(pdata);
1326 axgbe_config_tsf_mode(pdata, pdata->tx_sf_mode);
1327 axgbe_config_rsf_mode(pdata, pdata->rx_sf_mode);
1328 axgbe_config_tx_threshold(pdata, pdata->tx_threshold);
1329 axgbe_config_rx_threshold(pdata, pdata->rx_threshold);
1330 axgbe_config_tx_fifo_size(pdata);
1331 axgbe_config_rx_fifo_size(pdata);
1333 axgbe_enable_mtl_interrupts(pdata);
1335 /* Initialize MAC related features */
1336 axgbe_config_mac_hash_table(pdata);
1337 axgbe_config_mac_address(pdata);
1338 axgbe_config_jumbo_enable(pdata);
1339 axgbe_config_flow_control(pdata);
1340 axgbe_config_mac_speed(pdata);
1341 axgbe_config_checksum_offload(pdata);
1342 axgbe_config_mmc(pdata);
1347 void axgbe_init_function_ptrs_dev(struct axgbe_hw_if *hw_if)
1349 hw_if->exit = axgbe_exit;
1350 hw_if->config_flow_control = axgbe_config_flow_control;
1352 hw_if->init = axgbe_init;
1354 hw_if->read_mmd_regs = axgbe_read_mmd_regs;
1355 hw_if->write_mmd_regs = axgbe_write_mmd_regs;
1357 hw_if->set_speed = axgbe_set_speed;
1359 hw_if->set_ext_mii_mode = axgbe_set_ext_mii_mode;
1360 hw_if->read_ext_mii_regs = axgbe_read_ext_mii_regs;
1361 hw_if->write_ext_mii_regs = axgbe_write_ext_mii_regs;
1363 hw_if->config_tx_flow_control = axgbe_config_tx_flow_control;
1364 hw_if->config_rx_flow_control = axgbe_config_rx_flow_control;
1367 hw_if->enable_rx_vlan_stripping = axgbe_enable_rx_vlan_stripping;
1368 hw_if->disable_rx_vlan_stripping = axgbe_disable_rx_vlan_stripping;
1369 hw_if->enable_rx_vlan_filtering = axgbe_enable_rx_vlan_filtering;
1370 hw_if->disable_rx_vlan_filtering = axgbe_disable_rx_vlan_filtering;
1371 hw_if->update_vlan_hash_table = axgbe_update_vlan_hash_table;